mirror of
https://github.com/zoriya/astal.git
synced 2026-05-31 01:25:32 +00:00
fix: message api
This commit is contained in:
@@ -80,7 +80,7 @@ class AstalJS extends Astal.Application {
|
||||
})
|
||||
|
||||
if (!this.acquire_socket())
|
||||
return client(msg => this.message(msg)!, ...programArgs)
|
||||
return client(msg => Astal.Application.send_message(this.instance_name, msg)!, ...programArgs)
|
||||
|
||||
if (css)
|
||||
this.apply_css(css, false)
|
||||
|
||||
@@ -75,7 +75,7 @@ function Astal.Application:start(config)
|
||||
|
||||
if not app:acquire_socket() then
|
||||
return config.client(function(msg)
|
||||
return app:message(msg)
|
||||
return Astal.Application.send_message(self.instance_name, msg)
|
||||
end, table.unpack(arg))
|
||||
end
|
||||
|
||||
|
||||
+18
-16
@@ -117,7 +117,7 @@ public class Application : Gtk.Application {
|
||||
|
||||
[DBus (visible=false)]
|
||||
public virtual void request(string msg, SocketConnection conn) {
|
||||
write_sock.begin(conn, "missing response implementation on ".concat(application_id));
|
||||
write_sock.begin(conn, @"missing response implementation on $application_id");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,10 +126,8 @@ public class Application : Gtk.Application {
|
||||
*/
|
||||
[DBus (visible=false)]
|
||||
public bool acquire_socket() {
|
||||
socket_path = GLib.Environment.get_user_runtime_dir().concat(
|
||||
"/",
|
||||
instance_name,
|
||||
".sock");
|
||||
var rundir = GLib.Environment.get_user_runtime_dir();
|
||||
socket_path = @"$rundir/$instance_name.sock";
|
||||
|
||||
if (FileUtils.test(socket_path, GLib.FileTest.EXISTS)) {
|
||||
info("socket %s exists", socket_path);
|
||||
@@ -146,11 +144,10 @@ public class Application : Gtk.Application {
|
||||
null);
|
||||
|
||||
service.incoming.connect((conn) => {
|
||||
_socket_request.begin(conn);
|
||||
_socket_request.begin(conn, (_, res) => _socket_request.end(res));
|
||||
return false;
|
||||
});
|
||||
|
||||
|
||||
Bus.own_name(
|
||||
BusType.SESSION,
|
||||
"io.Astal." + instance_name,
|
||||
@@ -176,6 +173,8 @@ public class Application : Gtk.Application {
|
||||
}
|
||||
|
||||
public string message(string? msg) throws DBusError, IOError {
|
||||
var rundir = GLib.Environment.get_user_runtime_dir();
|
||||
var socket_path = @"$rundir/$instance_name.sock";
|
||||
var client = new SocketClient();
|
||||
|
||||
if (msg == null)
|
||||
@@ -280,17 +279,20 @@ public class Application : Gtk.Application {
|
||||
}
|
||||
}
|
||||
|
||||
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"
|
||||
);
|
||||
public static string send_message(string instance_name, string msg) {
|
||||
var rundir = GLib.Environment.get_user_runtime_dir();
|
||||
var socket_path = @"$rundir/$instance_name.sock";
|
||||
var client = new SocketClient();
|
||||
|
||||
return proxy.message(message);
|
||||
try {
|
||||
var conn = client.connect(new UnixSocketAddress(socket_path), null);
|
||||
conn.output_stream.write(msg.concat("\x04").data);
|
||||
|
||||
var stream = new DataInputStream(conn.input_stream);
|
||||
return stream.read_upto("\x04", -1, null, null);
|
||||
} catch (Error err) {
|
||||
throw new IOError.FAILED(@"could not write to app '$instance'");
|
||||
printerr(err.message);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-8
@@ -46,7 +46,7 @@ int main(string[] argv) {
|
||||
}
|
||||
|
||||
if (version) {
|
||||
print("@VERSION@");
|
||||
print(Astal.VERSION);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -80,13 +80,8 @@ int main(string[] argv) {
|
||||
request = request.concat(" ", argv[i]);
|
||||
}
|
||||
|
||||
try {
|
||||
var reply = Astal.Application.send_message(instance_name, request);
|
||||
print("%s\n", reply);
|
||||
} catch (IOError err) {
|
||||
printerr("%s\n", err.message);
|
||||
return 1;
|
||||
}
|
||||
var reply = Astal.Application.send_message(instance_name, request);
|
||||
print("%s\n", reply);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user