summaryrefslogtreecommitdiff
path: root/core/src/astal.vala
blob: 134293b9b3c7ae9093cb9e0cd64990cd07f2e340 (plain)
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
namespace Astal {
[DBus (name="io.Astal.Application")]
public class Application : Gtk.Application {
    private List<Gtk.CssProvider> css_providers = new List<Gtk.CssProvider>();
    private SocketService service;
    private DBusConnection conn;
    private string _instance_name;

    public string socket_path { get; private set; }

    [DBus (visible=false)]
    public signal void monitor_added(Gdk.Monitor monitor);

    [DBus (visible=false)]
    public signal void monitor_removed(Gdk.Monitor monitor);

    [DBus (visible=false)]
    public List<weak Gdk.Monitor> monitors {
        owned get {
            var display = Gdk.Display.get_default();
            var list = new List<weak Gdk.Monitor>();
            for (var i = 0; i <= display.get_n_monitors(); ++i) {
                var mon = display.get_monitor(i);
                if (mon != null) {
                    list.append(mon);
                }
            }
            return list;
        }
    }

    [DBus (visible=false)]
    public string instance_name {
        get { return _instance_name; }
        set {
            application_id = "io.Astal." + value;
            _instance_name = value;
        }
    }

    [DBus (visible=false)]
    public List<Gtk.Window> windows {
        get { return get_windows(); }
    }

    [DBus (visible=false)]
    public Gtk.Settings settings {
        get { return Gtk.Settings.get_default(); }
    }

    [DBus (visible=false)]
    public Gdk.Screen screen {
        get { return Gdk.Screen.get_default(); }
    }

    [DBus (visible=false)]
    public string gtk_theme {
        owned get { return settings.gtk_theme_name; }
        set { settings.gtk_theme_name = value; }
    }

    [DBus (visible=false)]
    public string icon_theme {
        owned get { return settings.gtk_icon_theme_name; }
        set { settings.gtk_icon_theme_name = value; }
    }

    [DBus (visible=false)]
    public string cursor_theme {
        owned get { return settings.gtk_cursor_theme_name; }
        set { settings.gtk_cursor_theme_name = value; }
    }

    [DBus (visible=false)]
    public void reset_css() {
        foreach(var provider in css_providers) {
            Gtk.StyleContext.remove_provider_for_screen(screen, provider);
        }
        css_providers = new List<Gtk.CssProvider>();
    }

    public void inspector() throws DBusError, IOError {
        Gtk.Window.set_interactive_debugging(true);
    }

    [DBus (visible=false)]
    public Gtk.Window? get_window(string name) {
        foreach(var win in windows) {
            if (win.name == name)
                return win;
        }

        critical("no window with name \"%s\"".printf(name));
        return null;
    }

    public void toggle_window(string window) throws DBusError, IOError {
        var win = get_window(window);
        if (win != null) {
            win.visible = !win.visible;
        } else {
            throw new IOError.FAILED("window not found");
        }
    }

    [DBus (visible=false)]
    public void apply_css(string style, bool reset = false) {
        var provider = new Gtk.CssProvider();

        if (reset)
            reset_css();

        try {
            if (FileUtils.test(style, FileTest.EXISTS))
                provider.load_from_path(style);
            else
                provider.load_from_data(style);
        } catch (Error err) {
            critical(err.message);
        }

        Gtk.StyleContext.add_provider_for_screen(
            screen, provider, Gtk.STYLE_PROVIDER_PRIORITY_USER);

        css_providers.append(provider);
    }

    [DBus (visible=false)]
    public void add_icons(string? path) {
        if (path != null)
            Gtk.IconTheme.get_default().prepend_search_path(path);
    }

    private async void _socket_request(SocketConnection conn) {
        string message = yield read_sock(conn);
        request(message != null ? message.strip() : "", conn);
    }

    [DBus (visible=false)]
    public virtual void request(string msg, SocketConnection conn) {
        write_sock.begin(conn, @"missing response implementation on $application_id");
    }

    /**
     * should be called before `run()`
     * the return value indicates if instance is already running
     */
    [DBus (visible=false)]
    public bool acquire_socket() {
        foreach (var instance in get_instances()) {
            if (instance == instance_name) {
                return false;
            }
        }

        var rundir = GLib.Environment.get_user_runtime_dir();
        socket_path = @"$rundir/$instance_name.sock";

        if (FileUtils.test(socket_path, GLib.FileTest.EXISTS)) {
            try {
                File.new_for_path(socket_path).delete(null);
            } catch (Error err) {
                critical(err.message);
            }
        }

        try {
            service = new SocketService();
            service.add_address(
                new UnixSocketAddress(socket_path),
                SocketType.STREAM,
                SocketProtocol.DEFAULT,
                null,
                null);

            service.incoming.connect((conn) => {
                _socket_request.begin(conn, (_, res) => _socket_request.end(res));
                return false;
            });

            Bus.own_name(
                BusType.SESSION,
                "io.Astal." + instance_name,
                BusNameOwnerFlags.NONE,
                (conn) => {
                    try {
                    this.conn = conn;
                        conn.register_object("/io/Astal/Application", this);
                    } catch (Error err) {
                        critical(err.message);
                    }
                },
                () => {},
                () => {});

            info("socket acquired: %s\n", socket_path);
            return true;
        } catch (Error err) {
            critical("could not acquire socket %s\n", application_id);
            critical(err.message);
            return false;
        }
    }

    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)
            msg = "";

        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) {
            printerr(err.message);
            return "";
        }
    }

    public new void quit() throws DBusError, IOError {
        if (service != null) {
            if (FileUtils.test(socket_path, GLib.FileTest.EXISTS)){
                try {
                    File.new_for_path(socket_path).delete(null);
                } catch (Error err) {
                    warning(err.message);
                }
            }
        }

        base.quit();
    }

    construct {
        if (instance_name == null)
            instance_name = "astal";

        activate.connect(() => {
            var display = Gdk.Display.get_default();
            display.monitor_added.connect((mon) => {
                monitor_added(mon);
                notify_property("monitors");
            });
            display.monitor_removed.connect((mon) => {
                monitor_removed(mon);
                notify_property("monitors");
            });
        });

        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);
    }

    public static List<string> get_instances() {
        var list = new List<string>();
        var prefix = "io.Astal.";

        try {
            DBusImpl dbus = Bus.get_proxy_sync(
                BusType.SESSION,
                "org.freedesktop.DBus",
                "/org/freedesktop/DBus"
            );

            foreach (var busname in dbus.list_names()) {
                if (busname.has_prefix(prefix))
                    list.append(busname.replace(prefix, ""));
            }
        } catch (Error err) {
            critical(err.message);
        }

        return list;
    }

    public static void quit_instance(string instance) {
        try {
            IApplication proxy = Bus.get_proxy_sync(
                BusType.SESSION,
                "io.Astal." + instance,
                "/io/Astal/Application"
            );

            proxy.quit();
        } catch (Error err) {
            critical(err.message);
        }
    }

    public static void open_inspector(string instance) {
        try {
            IApplication proxy = Bus.get_proxy_sync(
                BusType.SESSION,
                "io.Astal." + instance,
                "/io/Astal/Application"
            );

            proxy.inspector();
        } catch (Error err) {
            critical(err.message);
        }
    }

    public static void toggle_window_by_name(string instance, string window) {
        try {
            IApplication proxy = Bus.get_proxy_sync(
                BusType.SESSION,
                "io.Astal." + instance,
                "/io/Astal/Application"
            );

            proxy.toggle_window(window);
        } catch (Error err) {
            critical(err.message);
        }
    }

    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();

        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) {
            printerr(err.message);
            return "";
        }
    }
}

[DBus (name="org.freedesktop.DBus")]
private interface DBusImpl : DBusProxy {
    public abstract string[] list_names() throws GLib.Error;
}

[DBus (name="io.Astal.Application")]
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) {
    try {
        var stream = new DataInputStream(conn.input_stream);
        return yield stream.read_upto_async("\x04", -1, Priority.DEFAULT, null, null);
    } catch (Error err) {
        critical(err.message);
        return err.message;
    }
}

public async void write_sock(SocketConnection conn, string response) {
    try {
        yield conn.output_stream.write_async(
            response.concat("\x04").data,
            Priority.DEFAULT);
    } catch (Error err) {
        critical(err.message);
    }
}
}