aboutsummaryrefslogtreecommitdiff
path: root/src/global_shortcuts.c
blob: 7037eb89360fbb0db2f72f32b86bf1fff221cc5b (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
#include "global_shortcuts.h"
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/random.h>
#include <gio/gio.h>

/* TODO: Remove G_DBUS_CALL_FLAGS_NO_AUTO_START and G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START? also in gpu screen recorder equivalent */
/* TODO: More error handling and clean up resources after done */
/* TODO: Use GArray instead of GVariant where possible */

static bool generate_random_characters(char *buffer, int buffer_size, const char *alphabet, size_t alphabet_size) {
    /* TODO: Use other functions on other platforms than linux */
    if(getrandom(buffer, buffer_size, 0) < buffer_size) {
        fprintf(stderr, "gsr error: generate_random_characters: failed to get random bytes, error: %s\n", strerror(errno));
        return false;
    }

    for(int i = 0; i < buffer_size; ++i) {
        unsigned char c = *(unsigned char*)&buffer[i];
        buffer[i] = alphabet[c % alphabet_size];
    }

    return true;
}

static void gsr_dbus_portal_get_unique_handle_token(gsr_global_shortcuts *self, char *buffer, int size) {
    snprintf(buffer, size, "gpu_screen_recorder_gtk_handle_%s_%u", self->random_str, self->handle_counter++);
}

/* Assumes shortcuts is an array */
static void handle_shortcuts_data(GVariant *shortcuts, gsr_shortcut_callback callback, void *userdata) {
    for(guint i = 0; i < g_variant_n_children(shortcuts); ++i) {
        gchar *shortcut_id = NULL;
        GVariant *shortcut_values = NULL;
        g_variant_get_child(shortcuts, i, "(s@a{sv})", &shortcut_id, &shortcut_values);

        if(!shortcut_id || !shortcut_values)
            continue;

        // gchar *description = NULL;
        // g_variant_lookup(shortcut_values, "description", "s", &description);

        gchar *trigger_description = NULL;
        g_variant_lookup(shortcut_values, "trigger_description", "s", &trigger_description);

        gsr_shortcut shortcut;
        shortcut.id = shortcut_id;
        shortcut.trigger_description = trigger_description ? trigger_description : "";
        callback(shortcut, userdata);
    }
}

typedef struct {
    gsr_global_shortcuts *self;
    gsr_shortcut_callback callback;
    void *userdata;
} signal_list_bind_userdata;

static void dbus_signal_list_bind(GDBusProxy *proxy, gchar *sender_name, gchar *signal_name, GVariant *parameters, signal_list_bind_userdata *userdata) {
    (void)proxy;
    (void)sender_name;
    if(g_strcmp0(signal_name, "Response") != 0)
        goto done;

    guint32 response = 0;
    GVariant *results = NULL;
    g_variant_get(parameters, "(u@a{sv})", &response, &results);

    if(response != 0 || !results)
        goto done;

    GVariant *shortcuts = g_variant_lookup_value(results, "shortcuts", G_VARIANT_TYPE("a(sa{sv})"));
    if(!shortcuts)
        goto done;

    handle_shortcuts_data(shortcuts, userdata->callback, userdata->userdata);

    done:
    free(userdata);
}

typedef struct {
    gsr_global_shortcuts *self;
    gsr_deactivated_callback deactivated_callback;
    gsr_shortcut_callback shortcut_changed_callback;
    void *userdata;
} signal_userdata;

static void signal_callback(GDBusConnection *connection,
                            const gchar     *sender_name,
                            const gchar     *object_path,
                            const gchar     *interface_name,
                            const gchar     *signal_name,
                            GVariant        *parameters,
                            gpointer        userdata)
{
    (void)connection;
    (void)sender_name;
    (void)object_path;
    (void)interface_name;
    (void)signal_name;
    (void)parameters;
    signal_userdata *cu = userdata;
    
    /* Button released */
    if(strcmp(signal_name, "Deactivated") == 0) {
        gchar *session_handle = NULL;
        gchar *shortcut_id = NULL;
        gchar *timestamp = NULL;
        GVariant *options = NULL;
        g_variant_get(parameters, "(ost@a{sv})", &session_handle, &shortcut_id, &timestamp, &options);

        if(session_handle && shortcut_id && strcmp(session_handle, cu->self->session_handle) == 0)
            cu->deactivated_callback(shortcut_id, cu->userdata);
    } else if(strcmp(signal_name, "ShortcutsChanged") == 0) {
        gchar *session_handle = NULL;
        GVariant *shortcuts = NULL;
        g_variant_get(parameters, "(o@a(sa{sv}))", &session_handle, &shortcuts);

        if(session_handle && shortcuts && strcmp(session_handle, cu->self->session_handle) == 0)
            handle_shortcuts_data(shortcuts, cu->shortcut_changed_callback, cu->userdata);
    }
}

typedef struct {
    gsr_global_shortcuts *self;
    gsr_init_callback callback;
    void *userdata;
} signal_create_session_userdata;

static void dbus_signal_create_session(GDBusProxy *proxy, gchar *sender_name, gchar *signal_name, GVariant *parameters, signal_create_session_userdata *cu) {
    (void)proxy;
    (void)sender_name;
    if(g_strcmp0(signal_name, "Response") != 0)
        goto done;

    guint32 response = 0;
    GVariant *results = NULL;
    g_variant_get(parameters, "(u@a{sv})", &response, &results);

    if(response != 0 || !results)
        goto done;

    gchar *session_handle = NULL;
    if(g_variant_lookup(results, "session_handle", "s", &session_handle) && session_handle) {
        cu->self->session_handle = strdup(session_handle);
        cu->self->session_created = true;
        cu->callback(cu->userdata);
    }

    done:
    free(cu);
}

static bool gsr_global_shortcuts_create_session(gsr_global_shortcuts *self, gsr_init_callback callback, void *userdata) {
    char handle_token[64];
    gsr_dbus_portal_get_unique_handle_token(self, handle_token, sizeof(handle_token));

    char session_handle_token[64];
    snprintf(session_handle_token, sizeof(session_handle_token), "gpu_screen_recorder_gtk");
    
    GVariantBuilder builder;
    g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}"));
    g_variant_builder_add(&builder, "{sv}", "handle_token", g_variant_new_string(handle_token));
    g_variant_builder_add(&builder, "{sv}", "session_handle_token", g_variant_new_string(session_handle_token));
    GVariant *aa = g_variant_builder_end(&builder);

    GVariant *ret = g_dbus_connection_call_sync(self->gdbus_con, "org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop", "org.freedesktop.portal.GlobalShortcuts", "CreateSession", g_variant_new_tuple(&aa, 1), NULL, G_DBUS_CALL_FLAGS_NO_AUTO_START, 1000, NULL, NULL);

    if(ret) {
        const gchar *val = NULL;
        g_variant_get(ret, "(&o)", &val);
        if(!val)
            return false;
        //g_variant_unref(ret);

        GDBusProxy *proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, NULL, "org.freedesktop.portal.Desktop", val, "org.freedesktop.portal.Request", NULL, NULL);
        if(!proxy)
            return false;
        //g_object_unref(proxy);

        signal_create_session_userdata *cu = malloc(sizeof(signal_create_session_userdata));
        cu->self = self;
        cu->callback = callback;
        cu->userdata = userdata;
        g_signal_connect(proxy, "g-signal", G_CALLBACK(dbus_signal_create_session), cu);
        return true;
    } else {
        return false;
    }
}

bool gsr_global_shortcuts_init(gsr_global_shortcuts *self, gsr_init_callback callback, void *userdata) {
    memset(self, 0, sizeof(*self));

    self->random_str[DBUS_RANDOM_STR_SIZE] = '\0';
    if(!generate_random_characters(self->random_str, DBUS_RANDOM_STR_SIZE, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 62)) {
        fprintf(stderr, "gsr error: gsr_global_shortcuts_init: failed to generate random string\n");
        return false;
    }

    self->gdbus_con = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
    if(!self->gdbus_con) {
        fprintf(stderr, "gsr error: gsr_global_shortcuts_init: g_bus_get_sync failed\n");
        return false;
    }

    if(!gsr_global_shortcuts_create_session(self, callback, userdata)) {
        gsr_global_shortcuts_deinit(self);
        return false;
    }

    return true;
}

void gsr_global_shortcuts_deinit(gsr_global_shortcuts *self) {
    if(self->gdbus_con) {
        /* TODO: Re-add this. Right now it causes errors as the connection is already closed, but checking if it's already closed here has no effect */
        //g_dbus_connection_close(self->gdbus_con, NULL, NULL, NULL);
        self->gdbus_con = NULL;
    }

    if(self->session_handle) {
        free(self->session_handle);
        self->session_handle = NULL;
    }
}

bool gsr_global_shortcuts_list_shortcuts(gsr_global_shortcuts *self, gsr_shortcut_callback callback, void *userdata) {
    if(!self->session_created)
        return false;

    char handle_token[64];
    gsr_dbus_portal_get_unique_handle_token(self, handle_token, sizeof(handle_token));

    GVariant *session_handle_obj = g_variant_new_object_path(self->session_handle);

    GVariantBuilder builder;
    g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}"));
    g_variant_builder_add(&builder, "{sv}", "handle_token", g_variant_new_string(handle_token));
    GVariant *aa = g_variant_builder_end(&builder);

    GVariant *args[2] = { session_handle_obj, aa };

    GVariant *ret = g_dbus_connection_call_sync(self->gdbus_con, "org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop", "org.freedesktop.portal.GlobalShortcuts", "ListShortcuts", g_variant_new_tuple(args, 2), NULL, G_DBUS_CALL_FLAGS_NO_AUTO_START, 1000, NULL, NULL);
    if(ret) {
        const gchar *val = NULL;
        g_variant_get(ret, "(&o)", &val);
        if(!val)
            return false;

        GDBusProxy *proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, NULL, "org.freedesktop.portal.Desktop", val, "org.freedesktop.portal.Request", NULL, NULL);
        if(!proxy)
            return false;
        //g_object_unref(proxy);

        signal_list_bind_userdata *cu = malloc(sizeof(signal_list_bind_userdata));
        cu->self = self;
        cu->callback = callback;
        cu->userdata = userdata;
        g_signal_connect(proxy, "g-signal", G_CALLBACK(dbus_signal_list_bind), cu);
        return true;
    } else {
        return false;
    }
}

bool gsr_global_shortcuts_bind_shortcuts(gsr_global_shortcuts *self, const gsr_bind_shortcut *shortcuts, int num_shortcuts, gsr_shortcut_callback callback, void *userdata) {
    if(!self->session_created)
        return false;

    char handle_token[64];
    gsr_dbus_portal_get_unique_handle_token(self, handle_token, sizeof(handle_token));

    GVariant *session_handle_obj = g_variant_new_object_path(self->session_handle);

    GVariantBuilder builder;
    g_variant_builder_init(&builder, G_VARIANT_TYPE("a(sa{sv})"));

    for(int i = 0; i < num_shortcuts; ++i) {
        GVariantBuilder shortcuts_builder;
        g_variant_builder_init(&shortcuts_builder, G_VARIANT_TYPE("a{sv}"));
        g_variant_builder_add(&shortcuts_builder, "{sv}", "description", g_variant_new_string(shortcuts[i].description));
        g_variant_builder_add(&shortcuts_builder, "{sv}", "preferred_trigger", g_variant_new_string(shortcuts[i].shortcut.trigger_description));
        GVariant *shortcuts_data = g_variant_builder_end(&shortcuts_builder);
        GVariant *ss_l[2] = { g_variant_new_string(shortcuts[i].shortcut.id), shortcuts_data };
        g_variant_builder_add_value(&builder, g_variant_new_tuple(ss_l, 2));
    }
    GVariant *aa = g_variant_builder_end(&builder);

    GVariantBuilder builder_zzz;
    g_variant_builder_init(&builder_zzz, G_VARIANT_TYPE("a{sv}"));
    g_variant_builder_add(&builder_zzz, "{sv}", "handle_token", g_variant_new_string(handle_token));
    GVariant *bb = g_variant_builder_end(&builder_zzz);

    GVariant *parent_window = g_variant_new_string("");
    GVariant *args[4] = { session_handle_obj, aa, parent_window, bb };

    GVariant *ret = g_dbus_connection_call_sync(self->gdbus_con, "org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop", "org.freedesktop.portal.GlobalShortcuts", "BindShortcuts", g_variant_new_tuple(args, 4), NULL, G_DBUS_CALL_FLAGS_NO_AUTO_START, -1, NULL, NULL);
    if(ret) {
        const gchar *val = NULL;
        g_variant_get(ret, "(&o)", &val);
        if(!val)
            return false;

        GDBusProxy *proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, NULL, "org.freedesktop.portal.Desktop", val, "org.freedesktop.portal.Request", NULL, NULL);
        if(!proxy)
            return false;
        //g_object_unref(proxy);

        signal_list_bind_userdata *cu = malloc(sizeof(signal_list_bind_userdata));
        cu->self = self;
        cu->callback = callback;
        cu->userdata = userdata;
        g_signal_connect(proxy, "g-signal", G_CALLBACK(dbus_signal_list_bind), cu);
        return true;
    } else {
        return false;
    }
}

bool gsr_global_shortcuts_subscribe_activated_signal(gsr_global_shortcuts *self, gsr_deactivated_callback deactivated_callback, gsr_shortcut_callback shortcut_changed_callback, void *userdata) {
    if(!self->session_created)
        return false;

    signal_userdata *cu = malloc(sizeof(signal_userdata));
    cu->self = self;
    cu->deactivated_callback = deactivated_callback;
    cu->shortcut_changed_callback = shortcut_changed_callback;
    cu->userdata = userdata;
    g_dbus_connection_signal_subscribe(self->gdbus_con, "org.freedesktop.portal.Desktop", "org.freedesktop.portal.GlobalShortcuts", NULL, "/org/freedesktop/portal/desktop", NULL, G_DBUS_SIGNAL_FLAGS_NONE, signal_callback, cu, free);
    return true;
}