diff options
author | Aylur <[email protected]> | 2024-12-30 14:29:07 +0100 |
---|---|---|
committer | Aylur <[email protected]> | 2024-12-30 14:29:07 +0100 |
commit | 998b57d645b75c144d0e591cc7888ea3d9b59a03 (patch) | |
tree | 10c9f6e6664bbc467a216889dca49fbdf0fa42c1 /lib | |
parent | 20535dbf04520e50a751073cec25e3d07a21f231 (diff) |
feat: AstalIO.Daemon
This class can be used for applications running as a daemon
that don't use Gtk but only run in the background benefitting from the
astal cli
Diffstat (limited to 'lib')
-rw-r--r-- | lib/astal/io/daemon.vala | 88 | ||||
-rw-r--r-- | lib/astal/io/meson.build | 1 |
2 files changed, 89 insertions, 0 deletions
diff --git a/lib/astal/io/daemon.vala b/lib/astal/io/daemon.vala new file mode 100644 index 0000000..7f3173e --- /dev/null +++ b/lib/astal/io/daemon.vala @@ -0,0 +1,88 @@ +[DBus (name="io.Astal.Application")] +public class AstalIO.Daemon : GLib.Application, AstalIO.Application { + private SocketService service; + private DBusConnection conn; + private string _instance_name = "astal"; + private string socket_path { get; private set; } + + /** + * A unique instance name. + * + * This is the identifier used by the AstalIO package and the CLI. + */ + [DBus (visible=false)] + public string instance_name { + owned get { return _instance_name; } + construct set { + _instance_name = value != null ? value : "astal"; + application_id = @"io.Astal.$_instance_name"; + } + } + + /** + * Handler for an incoming request. + * + * @param msg Body of the message + * @param conn The connection which expects the response. + */ + [DBus (visible=false)] + public virtual void request(string msg, SocketConnection conn) { + AstalIO.write_sock.begin(conn, @"missing response implementation on $application_id"); + } + + /** + * Attempt to acquire the astal socket for this app identified by its [[email protected]:instance_name]. + * If the socket is in use by another app with the same name an [[email protected]_OCCUPIED] is thrown. + */ + [DBus (visible=false)] + public void acquire_socket() throws Error { + string path; + service = AstalIO.acquire_socket(this, out path); + socket_path = path; + + Bus.own_name( + BusType.SESSION, + application_id, + BusNameOwnerFlags.NONE, + (conn) => { + try { + this.conn = conn; + conn.register_object("/io/Astal/Application", this); + } catch (Error err) { + critical(err.message); + } + }, + () => {}, + () => {} + ); + } + + public void inspector() throws DBusError, IOError { + throw new DBusError.FAILED("Daemon does not implement inspector"); + } + + public void toggle_window(string window) throws DBusError, IOError { + throw new DBusError.FAILED("Daemon does not implement toggle_window"); + } + + /** + * Quit and stop the socket if it was acquired. + */ + public new void quit() throws DBusError, IOError { + if (service != null) { + service.stop(); + service.close(); + } + + base.quit(); + } + + construct { + hold(); + + shutdown.connect(() => { try { quit(); } catch(Error err) {} }); + Unix.signal_add(1, () => { try { quit(); } catch(Error err) {} }, Priority.HIGH); + Unix.signal_add(2, () => { try { quit(); } catch(Error err) {} }, Priority.HIGH); + Unix.signal_add(15, () => { try { quit(); } catch(Error err) {} }, Priority.HIGH); + } +} diff --git a/lib/astal/io/meson.build b/lib/astal/io/meson.build index 023dece..522c572 100644 --- a/lib/astal/io/meson.build +++ b/lib/astal/io/meson.build @@ -38,6 +38,7 @@ deps = [ sources = [config] + files( 'application.vala', + 'daemon.vala', 'file.vala', 'process.vala', 'time.vala', |