move message logic to Application class

This commit is contained in:
Aylur
2024-07-15 21:57:21 +02:00
parent 68b58c9409
commit be163b3103
2 changed files with 22 additions and 30 deletions
+17 -3
View File
@@ -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) {
+5 -27
View File
@@ -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;
}