ltk/event_loop/
session.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>

//! `xdg-session-management-v1` client glue plus the runtime hooks that
//! persist `App::save_state` and turn signals into a clean exit.

use std::os::fd::{ AsRawFd, FromRawFd, OwnedFd };
use std::sync::atomic::{ AtomicI32, Ordering };
use std::time::Duration;

use calloop::timer::{ TimeoutAction, Timer };
use calloop::LoopHandle;
use smithay_client_toolkit::reexports::client::globals::GlobalList;
use smithay_client_toolkit::reexports::client::{ Connection, Dispatch, QueueHandle };
use smithay_client_toolkit::shell::xdg::window::Window;

use crate::app::App;
use crate::protocol::xdg_session_management_v1::
{
	xdg_session_manager_v1::{ self, XdgSessionManagerV1 },
	xdg_session_v1::{ self, XdgSessionV1 },
	xdg_toplevel_session_v1::{ self, XdgToplevelSessionV1 },
};
use crate::session_state::{ RestoreReason, Startup, StateStore };

use super::error::RunError;
use super::AppData;

pub( crate ) const TOPLEVEL_NAME: &str = "main";
pub( crate ) const SAVE_INTERVAL: Duration = Duration::from_secs( 30 );
pub( crate ) const RESTORE_ENV: &str = "LTK_SESSION_RESTORE";

pub( crate ) struct SessionRuntime
{
	pub session:          Option<XdgSessionV1>,
	pub toplevel_session: Option<XdgToplevelSessionV1>,
	/// `None` when persistence is off: layer / lock surfaces, an unusable
	/// app_id, a concurrent instance, or after `replaced`.
	pub store:            Option<StateStore>,
	pub reason:           RestoreReason,
	pub replaced:         bool,
}

impl SessionRuntime
{
	pub fn disabled() -> Self
	{
		Self {
			session:          None,
			toplevel_session: None,
			store:            None,
			reason:           RestoreReason::Launch,
			replaced:         false,
		}
	}

	/// Phase 1, before any window exists: read the restore hint from the
	/// environment, decide the reason, hand saved bytes to the app and mark
	/// the run as live.
	pub fn bootstrap<A: App>( app: &mut A ) -> Self
	{
		let env_restore = std::env::var_os( RESTORE_ENV ).is_some_and( |v| v == "1" );
		if std::env::var_os( RESTORE_ENV ).is_some()
		{
			// SAFETY: removing an env var is sound only when no other thread
			// is reading the environment concurrently. We are still in the
			// init phase before `set_channel_sender`, so the app has had
			// no opportunity to spawn worker threads yet.
			unsafe { std::env::remove_var( RESTORE_ENV ); }
		}

		let mut rt = Self::disabled();
		let Some( mut store ) = StateStore::open( app.app_id() ) else { return rt };
		match store.decide( env_restore )
		{
			Startup::Concurrent =>
			{
				eprintln!( "ltk: another instance of {} is running; session persistence disabled", app.app_id() );
				return rt;
			}
			Startup::Reason( reason ) => rt.reason = reason,
		}
		if rt.reason != RestoreReason::Launch
		{
			if let Some( bytes ) = store.load_state()
			{
				app.restore_state( bytes );
			}
		}
		store.mark_running();
		rt.store = Some( store );
		rt
	}

	/// Phase 2: bind the manager and open the session. Silent when the
	/// compositor lacks the global. The manager proxy is not kept: it has
	/// no state of its own and the session outlives it server-side.
	pub fn bind<A: App>( &mut self, globals: &GlobalList, qh: &QueueHandle<AppData<A>> )
	{
		let Some( store ) = &self.store else { return };
		let manager: Option<XdgSessionManagerV1> = globals.bind( qh, 1..=1, () ).ok();
		let Some( manager ) = manager else { return };
		let reason = match self.reason
		{
			RestoreReason::Launch         => xdg_session_manager_v1::Reason::Launch,
			RestoreReason::Recover        => xdg_session_manager_v1::Reason::Recover,
			RestoreReason::SessionRestore => xdg_session_manager_v1::Reason::SessionRestore,
		};
		self.session = Some( manager.get_session( reason, store.session_id(), qh, () ) );
	}

	/// Phase 3: register the main toplevel. Must run before the window's
	/// first commit or the compositor raises `already_mapped`.
	pub fn attach_toplevel<A: App>( &mut self, window: &Window, qh: &QueueHandle<AppData<A>> )
	{
		let Some( session ) = &self.session else { return };
		self.toplevel_session =
			Some( session.restore_toplevel( window.xdg_toplevel(), TOPLEVEL_NAME.to_string(), qh, () ) );
	}

	pub fn periodic_save<A: App>( &mut self, app: &A )
	{
		if self.replaced { return; }
		if let Some( store ) = &mut self.store
		{
			store.save_state_if_changed( app.save_state() );
		}
	}

	pub fn on_exit<A: App>( &mut self, app: &A )
	{
		if self.replaced { return; }
		if let Some( store ) = &mut self.store
		{
			store.mark_clean_exit( app.save_state() );
		}
	}

	pub fn on_replaced( &mut self )
	{
		eprintln!( "ltk: session taken over by another instance; this one stops persisting state" );
		if let Some( t ) = self.toplevel_session.take() { t.destroy(); }
		if let Some( s ) = self.session.take() { s.destroy(); }
		self.replaced = true;
		self.store = None;
	}
}

static SIGNAL_PIPE_WR: AtomicI32 = AtomicI32::new( -1 );

/// Async-signal-safe: an atomic load and a `write`, errno preserved.
extern "C" fn on_termination_signal( sig: libc::c_int )
{
	unsafe
	{
		let errno = *libc::__errno_location();
		let fd = SIGNAL_PIPE_WR.load( Ordering::Acquire );
		if fd >= 0
		{
			let byte = sig as u8;
			let _ = libc::write( fd, ( &byte as *const u8 ).cast(), 1 );
		}
		*libc::__errno_location() = errno;
	}
}

/// Self-pipe rather than a signalfd: a `sigaction` handler is process-
/// wide, so the clean exit works no matter which thread the kernel
/// picks, without blocking the signal in every thread — a mask that
/// children would inherit across exec.
pub( crate ) fn install_signal_source<A: App>( handle: &LoopHandle<'static, AppData<A>> ) -> Result<(), RunError>
{
	use calloop::generic::Generic;
	use calloop::{ Interest, Mode, PostAction };

	let mut fds = [ -1i32; 2 ];
	if unsafe { libc::pipe2( fds.as_mut_ptr(), libc::O_CLOEXEC | libc::O_NONBLOCK ) } != 0
	{
		return Err( RunError::EventLoop( format!( "signal pipe2: {}", std::io::Error::last_os_error() ) ) );
	}
	SIGNAL_PIPE_WR.store( fds[ 1 ], Ordering::Release );

	unsafe
	{
		let mut action: libc::sigaction = std::mem::zeroed();
		action.sa_sigaction = on_termination_signal as usize;
		action.sa_flags = libc::SA_RESTART;
		libc::sigemptyset( &mut action.sa_mask );
		for sig in [ libc::SIGTERM, libc::SIGINT ]
		{
			if libc::sigaction( sig, &action, std::ptr::null_mut() ) != 0
			{
				return Err( RunError::EventLoop( format!( "sigaction: {}", std::io::Error::last_os_error() ) ) );
			}
		}
	}

	let read_end = unsafe { OwnedFd::from_raw_fd( fds[ 0 ] ) };
	handle
		.insert_source( Generic::new( read_end, Interest::READ, Mode::Level ), |_, fd, data: &mut AppData<A>|
		{
			let mut sig = None;
			let mut buf = [ 0u8; 16 ];
			loop
			{
				let n = unsafe { libc::read( fd.as_raw_fd(), buf.as_mut_ptr().cast(), buf.len() ) };
				if n <= 0 { break; }
				sig = Some( buf[ 0 ] as i32 );
			}
			if let Some( sig ) = sig
			{
				let name = match sig
				{
					libc::SIGTERM => "SIGTERM",
					libc::SIGINT  => "SIGINT",
					_             => "signal",
				};
				eprintln!( "ltk: {name} received, exiting cleanly" );
				data.exit_requested = true;
			}
			Ok( PostAction::Continue )
		} )
		.map_err( |e| RunError::EventLoop( format!( "signal pipe insert_source: {e:?}" ) ) )?;
	Ok( () )
}

pub( crate ) fn install_save_timer<A: App>( handle: &LoopHandle<'static, AppData<A>> ) -> Result<(), RunError>
{
	handle
		.insert_source( Timer::from_duration( SAVE_INTERVAL ), |_, _, data: &mut AppData<A>|
		{
			data.session.periodic_save( &data.app );
			TimeoutAction::ToDuration( SAVE_INTERVAL )
		} )
		.map_err( |e| RunError::EventLoop( format!( "save timer insert_source: {e:?}" ) ) )?;
	Ok( () )
}

impl<A: App> Dispatch<XdgSessionManagerV1, ()> for AppData<A>
{
	fn event(
		_state: &mut Self,
		_proxy: &XdgSessionManagerV1,
		_event: xdg_session_manager_v1::Event,
		_data:  &(),
		_conn:  &Connection,
		_qh:    &QueueHandle<Self>,
	)
	{
	}
}

impl<A: App> Dispatch<XdgSessionV1, ()> for AppData<A>
{
	fn event(
		state:  &mut Self,
		_proxy: &XdgSessionV1,
		event:  xdg_session_v1::Event,
		_data:  &(),
		_conn:  &Connection,
		_qh:    &QueueHandle<Self>,
	)
	{
		match event
		{
			xdg_session_v1::Event::Created { session_id } =>
			{
				if let Some( store ) = &mut state.session.store
				{
					store.set_session_id( session_id );
				}
			}
			xdg_session_v1::Event::Restored => {}
			xdg_session_v1::Event::Replaced => state.session.on_replaced(),
		}
	}
}

impl<A: App> Dispatch<XdgToplevelSessionV1, ()> for AppData<A>
{
	fn event(
		_state: &mut Self,
		_proxy: &XdgToplevelSessionV1,
		_event: xdg_toplevel_session_v1::Event,
		_data:  &(),
		_conn:  &Connection,
		_qh:    &QueueHandle<Self>,
	)
	{
	}
}

#[ cfg( test ) ]
mod tests
{
	use super::*;

	fn read_one( fd: i32 ) -> Option<u8>
	{
		let mut buf = [ 0u8; 4 ];
		let n = unsafe { libc::read( fd, buf.as_mut_ptr().cast(), buf.len() ) };
		( n == 1 ).then( || buf[ 0 ] )
	}

	#[ test ]
	fn handler_writes_signal_to_pipe()
	{
		let mut fds = [ -1i32; 2 ];
		assert_eq!( unsafe { libc::pipe2( fds.as_mut_ptr(), libc::O_CLOEXEC | libc::O_NONBLOCK ) }, 0 );
		SIGNAL_PIPE_WR.store( fds[ 1 ], Ordering::Release );

		on_termination_signal( libc::SIGTERM );
		assert_eq!( read_one( fds[ 0 ] ), Some( libc::SIGTERM as u8 ) );

		// The real wiring: raise() delivers synchronously to this
		// thread, so the handler has run before it returns.
		unsafe
		{
			let mut action: libc::sigaction = std::mem::zeroed();
			action.sa_sigaction = on_termination_signal as usize;
			action.sa_flags = libc::SA_RESTART;
			libc::sigemptyset( &mut action.sa_mask );
			assert_eq!( libc::sigaction( libc::SIGTERM, &action, std::ptr::null_mut() ), 0 );
			libc::raise( libc::SIGTERM );
		}
		assert_eq!( read_one( fds[ 0 ] ), Some( libc::SIGTERM as u8 ) );

		unsafe
		{
			let mut dfl: libc::sigaction = std::mem::zeroed();
			dfl.sa_sigaction = libc::SIG_DFL;
			libc::sigemptyset( &mut dfl.sa_mask );
			libc::sigaction( libc::SIGTERM, &dfl, std::ptr::null_mut() );
		}
		SIGNAL_PIPE_WR.store( -1, Ordering::Release );
		unsafe
		{
			libc::close( fds[ 0 ] );
			libc::close( fds[ 1 ] );
		}
	}
}