diff options
author | Aylur <[email protected]> | 2024-07-15 21:57:21 +0200 |
---|---|---|
committer | Aylur <[email protected]> | 2024-07-15 21:57:21 +0200 |
commit | be163b310398ad7f454d3ece574a476abfa216e9 (patch) | |
tree | 987d551633d3b2d39c2992a9b4dfb07967f2d5c2 /src | |
parent | 68b58c94094ebbb44e75be82ece77badaa0cd68f (diff) |
move message logic to Application class
Diffstat (limited to 'src')
-rw-r--r-- | src/astal.vala | 20 | ||||
-rw-r--r-- | src/cli.vala | 32 |
2 files changed, 22 insertions, 30 deletions
diff --git a/src/astal.vala b/src/astal.vala index 3e94dfc..4f8021e 100644 --- a/src/astal.vala +++ b/src/astal.vala @@ -175,8 +175,7 @@ public class Application : Gtk.Application { } } - [DBus (visible=false)] - public string? message(string? msg) { + public string message(string? msg) throws DBusError, IOError { var client = new SocketClient(); if (msg == null) @@ -190,7 +189,7 @@ public class Application : Gtk.Application { return stream.read_upto("\x04", -1, null, null); } catch (Error err) { printerr(err.message); - return null; + return ""; } } @@ -280,6 +279,20 @@ public class Application : Gtk.Application { critical(err.message); } } + + public static string send_message(string instance, string message) throws IOError { + try { + IApplication proxy = Bus.get_proxy_sync( + BusType.SESSION, + "io.Astal." + instance, + "/io/Astal/Application" + ); + + return proxy.message(message); + } catch (Error err) { + throw new IOError.FAILED(@"could not write to app '$instance'"); + } + } } [DBus (name="org.freedesktop.DBus")] @@ -292,6 +305,7 @@ private interface IApplication : DBusProxy { public abstract void quit() throws GLib.Error; public abstract void inspector() throws GLib.Error; public abstract void toggle_window(string window) throws GLib.Error; + public abstract string message(string window) throws GLib.Error; } public async string read_sock(SocketConnection conn) { diff --git a/src/cli.vala b/src/cli.vala index 3da4586..3e3eb15 100644 --- a/src/cli.vala +++ b/src/cli.vala @@ -18,7 +18,7 @@ private const GLib.OptionEntry[] options = { { null }, }; -async int main(string[] argv) { +int main(string[] argv) { try { var opts = new OptionContext(); opts.add_main_entries(options, null); @@ -80,33 +80,11 @@ async int main(string[] argv) { request = request.concat(" ", argv[i]); } - var client = new SocketClient(); - var rundir = GLib.Environment.get_user_runtime_dir(); - var socket = rundir.concat("/", instance_name, ".sock"); - try { - var conn = client.connect(new UnixSocketAddress(socket), null); - - try { - yield conn.output_stream.write_async( - request.concat("\x04").data, - Priority.DEFAULT); - } catch (Error err) { - printerr("could not write to app '%s'", instance_name); - } - - var stream = new DataInputStream(conn.input_stream); - size_t size; - - try { - var res = yield stream.read_upto_async("\x04", -1, Priority.DEFAULT, null, out size); - if (res != null) - print("%s", res); - } catch (Error err) { - printerr(err.message); - } - } catch (Error err) { - printerr("could not connect to app '%s'", instance_name); + var reply = Astal.Application.send_message(instance_name, request); + print("%s\n", reply); + } catch (IOError err) { + printerr("%s\n", err.message); return 1; } |