flashback-shell.c 16.1 KB
Newer Older
1
/*
2
 * Copyright (C) 2015-2020 Alberts Muktupāvels
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

18
19
20
#include "config.h"
#include "flashback-shell.h"

21
#include <gtk/gtk.h>
Alberts Muktupāvels's avatar
Alberts Muktupāvels committed
22
#include <libcommon/gf-keybindings.h>
23
24

#include "dbus/gf-shell-gen.h"
25
#include "flashback-monitor-labeler.h"
26
#include "flashback-osd.h"
27
#include "gf-shell-introspect.h"
28
29
30
31
32
33
34
35
36
37
38
39

#define SHELL_DBUS_NAME "org.gnome.Shell"
#define SHELL_DBUS_PATH "/org/gnome/Shell"

typedef struct
{
	const gchar    *sender;
	FlashbackShell *shell;
} GrabberData;

struct _FlashbackShell
{
40
  GObject                  parent;
41

42
43
  gint                     bus_name;
  GDBusInterfaceSkeleton  *iface;
44

45
  /* key-grabber */
46
  GfKeybindings           *keybindings;
47
48
49
50
  GHashTable              *grabbed_accelerators;
  GHashTable              *grabbers;

  /* monitor labeler */
51
  GfMonitorManager        *monitor_manager;
52
  FlashbackMonitorLabeler *labeler;
53
54
55

  /* osd */
  FlashbackOsd            *osd;
56
57

  GfShellIntrospect       *introspect;
58
59
60
61
};

G_DEFINE_TYPE (FlashbackShell, flashback_shell, G_TYPE_OBJECT)

62
static GVariant *
63
64
65
66
build_parameters (const gchar *device_node,
                  guint        device_id,
                  guint        timestamp,
                  guint        action_mode)
67
68
69
70
71
72
{
  GVariantBuilder *builder;
  GVariant *parameters;

  builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));

73
74
75
76
  if (device_node != NULL)
      g_variant_builder_add (builder, "{sv}", "device-node",
                             g_variant_new_string (device_node));

77
78
79
80
81
82
83
84
85
86
87
88
89
  g_variant_builder_add (builder, "{sv}", "device-id",
                         g_variant_new_uint32 (device_id));
  g_variant_builder_add (builder, "{sv}", "timestamp",
                         g_variant_new_uint32 (timestamp));
  g_variant_builder_add (builder, "{sv}", "action-mode",
                         g_variant_new_uint32 (action_mode));

  parameters = g_variant_new ("a{sv}", builder);
  g_variant_builder_unref (builder);

  return parameters;
}

90
static void
91
92
accelerator_activated (GfKeybindings *keybindings,
                       guint          action,
93
94
95
                       const gchar   *device_node,
                       guint          device_id,
                       guint          timestamp,
96
                       gpointer       user_data)
97
98
{
	FlashbackShell *shell;
99
	GfShellGen *shell_gen;
100
	GVariant *parameters;
101
102

	shell = FLASHBACK_SHELL (user_data);
103
	shell_gen = GF_SHELL_GEN (shell->iface);
104
	parameters = build_parameters (device_node, device_id, timestamp, 0);
105

106
	gf_shell_gen_emit_accelerator_activated (shell_gen, action, parameters);
107
108
109
110
}

static gint
real_grab (FlashbackShell *shell,
111
112
113
           const gchar    *accelerator,
           guint           mode_flags,
           guint           grab_flags)
114
{
115
  return gf_keybindings_grab (shell->keybindings, accelerator);
116
117
118
119
120
121
}

static gboolean
real_ungrab (FlashbackShell *shell,
             gint            action)
{
122
  return gf_keybindings_ungrab (shell->keybindings, action);
123
124
}

125
126
127
128
129
130
131
132
static void
remove_watch (gpointer key,
              gpointer value,
              gpointer user_data)
{
  g_bus_unwatch_name (GPOINTER_TO_UINT (value));
}

133
static gboolean
134
remove_accelerator (gpointer key,
135
136
137
138
139
140
141
142
143
144
145
146
147
148
                    gpointer value,
                    gpointer user_data)
{
  guint action;
  gchar *sender;
  GrabberData *data;

  action = GPOINTER_TO_UINT (key);
  sender = (gchar *) value;
  data = (GrabberData *) user_data;

  if (g_str_equal (sender, data->sender))
    {
      if (real_ungrab (data->shell, action))
149
        return TRUE;
150
    }
151
152

  return FALSE;
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
}

static void
name_vanished_handler (GDBusConnection *connection,
                       const gchar     *name,
                       gpointer         user_data)
{
  FlashbackShell *shell;
  guint id;
  GrabberData *data;

  shell = FLASHBACK_SHELL (user_data);
  id = GPOINTER_TO_UINT (g_hash_table_lookup (shell->grabbers, name));
  data = g_new0 (GrabberData, 1);

  data->sender = name;
  data->shell = shell;

171
  g_hash_table_foreach_remove (shell->grabbed_accelerators,
172
                               remove_accelerator, data);
173
174
175
176
177
178
179
180
181
  g_free (data);

  g_bus_unwatch_name (id);
  g_hash_table_remove (shell->grabbers, name);
}

static guint
grab_accelerator (FlashbackShell *shell,
                  const gchar    *accelerator,
182
183
                  guint           mode_flags,
                  guint           grab_flags,
184
185
186
187
                  const gchar    *sender)
{
  guint action;

188
  action = real_grab (shell, accelerator, mode_flags, grab_flags);
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  g_hash_table_insert (shell->grabbed_accelerators,
                       GUINT_TO_POINTER (action), g_strdup (sender));

  if (g_hash_table_lookup (shell->grabbers, sender) == NULL)
    {
      guint id = g_bus_watch_name (G_BUS_TYPE_SESSION,
                                   sender,
                                   G_BUS_NAME_WATCHER_FLAGS_NONE,
                                   NULL,
                                   (GBusNameVanishedCallback) name_vanished_handler,
                                   shell,
                                   NULL);
      g_hash_table_insert (shell->grabbers, g_strdup (sender), GUINT_TO_POINTER (id));
    }

  return action;
}

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
static gboolean
ungrab_accelerator (FlashbackShell *shell,
                    guint           action,
                    const gchar    *sender)
{
  const gchar *grabbed_by;
  gboolean success;

  grabbed_by = g_hash_table_lookup (shell->grabbed_accelerators,
                                    GUINT_TO_POINTER (action));

  if (g_strcmp0 (grabbed_by, sender) != 0)
    return FALSE;

  success = real_ungrab (shell, action);

  if (success)
    g_hash_table_remove (shell->grabbed_accelerators, GUINT_TO_POINTER (action));

  return success;
}

229
static gboolean
230
handle_eval (GfShellGen            *shell_gen,
231
232
             GDBusMethodInvocation *invocation,
             const gchar            action,
233
             FlashbackShell        *shell)
234
{
235
  gf_shell_gen_complete_eval (shell_gen, invocation, FALSE, "");
236
237
238
239
240

  return TRUE;
}

static gboolean
241
handle_focus_search (GfShellGen            *shell_gen,
242
                     GDBusMethodInvocation *invocation,
243
                     FlashbackShell        *shell)
244
{
245
  gf_shell_gen_complete_focus_search (shell_gen, invocation);
246
247
248
249
250

  return TRUE;
}

static gboolean
251
handle_show_osd (GfShellGen            *shell_gen,
252
253
                 GDBusMethodInvocation *invocation,
                 GVariant              *params,
254
                 FlashbackShell        *shell)
255
{
256
  flashback_osd_show (shell->osd, shell->monitor_manager, params);
257

258
  gf_shell_gen_complete_show_osd (shell_gen, invocation);
259
260
261
262

  return TRUE;
}

263
static gboolean
264
handle_show_monitor_labels (GfShellGen            *shell_gen,
265
266
                            GDBusMethodInvocation *invocation,
                            GVariant              *params,
267
                            FlashbackShell        *shell)
268
269
270
271
272
273
274
{
  const gchar *sender;

  sender = g_dbus_method_invocation_get_sender (invocation);

  g_assert (shell->monitor_manager != NULL);

275
276
  flashback_monitor_labeler_show (shell->labeler, shell->monitor_manager,
                                  sender, params);
277

278
  gf_shell_gen_complete_show_monitor_labels (shell_gen, invocation);
279
280
281
282

  return TRUE;
}

283
static gboolean
284
handle_hide_monitor_labels (GfShellGen            *shell_gen,
285
                            GDBusMethodInvocation *invocation,
286
                            FlashbackShell        *shell)
287
{
288
289
290
291
292
293
  const gchar *sender;

  sender = g_dbus_method_invocation_get_sender (invocation);

  flashback_monitor_labeler_hide (shell->labeler, sender);

294
  gf_shell_gen_complete_hide_monitor_labels (shell_gen, invocation);
295
296
297
298
299

  return TRUE;
}

static gboolean
300
handle_focus_app (GfShellGen            *shell_gen,
301
302
                  GDBusMethodInvocation *invocation,
                  const gchar            id,
303
                  FlashbackShell        *shell)
304
{
305
  gf_shell_gen_complete_focus_app (shell_gen, invocation);
306
307
308
309
310

  return TRUE;
}

static gboolean
311
handle_show_applications (GfShellGen            *shell_gen,
312
                          GDBusMethodInvocation *invocation,
313
                          FlashbackShell        *shell)
314
{
315
  gf_shell_gen_complete_show_applications (shell_gen, invocation);
316
317
318
319
320

  return TRUE;
}

static gboolean
321
handle_grab_accelerator (GfShellGen            *shell_gen,
322
323
                         GDBusMethodInvocation *invocation,
                         const gchar           *accelerator,
324
325
                         guint                  mode_flags,
                         guint                  grab_flags,
326
                         FlashbackShell        *shell)
327
328
329
330
331
{
  const gchar *sender;
  guint action;

  sender = g_dbus_method_invocation_get_sender (invocation);
332
  action = grab_accelerator (shell, accelerator, mode_flags, grab_flags, sender);
333

334
  gf_shell_gen_complete_grab_accelerator (shell_gen, invocation, action);
335
336
337
338
339

  return TRUE;
}

static gboolean
340
handle_grab_accelerators (GfShellGen            *shell_gen,
341
342
                          GDBusMethodInvocation *invocation,
                          GVariant              *accelerators,
343
                          FlashbackShell        *shell)
344
345
346
347
348
349
350
351
352
353
354
355
356
357
{
  GVariantBuilder builder;
  GVariantIter iter;
  GVariant *child;
  const gchar *sender;

  g_variant_builder_init (&builder, G_VARIANT_TYPE("au"));
  g_variant_iter_init (&iter, accelerators);

  sender = g_dbus_method_invocation_get_sender (invocation);

  while ((child = g_variant_iter_next_value (&iter)))
    {
      gchar *accelerator;
358
359
      guint mode_flags;
      guint grab_flags;
360
361
      guint action;

362
      g_variant_get (child, "(suu)", &accelerator, &mode_flags, &grab_flags);
363

364
      action = grab_accelerator (shell, accelerator, mode_flags, grab_flags, sender);
365
366
367
368
369
370
      g_variant_builder_add (&builder, "u", action);

      g_free (accelerator);
      g_variant_unref (child);
    }

371
372
373
  gf_shell_gen_complete_grab_accelerators (shell_gen,
                                           invocation,
                                           g_variant_builder_end (&builder));
374
375
376
377
378

  return TRUE;
}

static gboolean
379
handle_ungrab_accelerator (GfShellGen            *shell_gen,
380
381
                           GDBusMethodInvocation *invocation,
                           guint                  action,
382
                           FlashbackShell        *shell)
383
{
384
385
  const gchar *sender;
  gboolean success;
386

387
388
389
  sender = g_dbus_method_invocation_get_sender (invocation);
  success = ungrab_accelerator (shell, action, sender);

390
  gf_shell_gen_complete_ungrab_accelerator (shell_gen, invocation, success);
391
392
393
394
395

  return TRUE;
}

static gboolean
396
handle_ungrab_accelerators (GfShellGen            *shell_gen,
397
398
                            GDBusMethodInvocation *invocation,
                            GVariant              *actions,
399
                            FlashbackShell        *shell)
400
401
402
403
404
405
406
407
408
409
410
{
  const char *sender;
  gboolean success;
  GVariantIter iter;
  GVariant *child;

  sender = g_dbus_method_invocation_get_sender (invocation);
  success = TRUE;

  g_variant_iter_init (&iter, actions);
  while ((child = g_variant_iter_next_value (&iter)))
411
    {
412
413
414
415
      guint action;

      g_variant_get (child, "u", &action);
      g_variant_unref (child);
416

417
      success &= ungrab_accelerator (shell, action, sender);
418
419
    }

420
  gf_shell_gen_complete_ungrab_accelerators (shell_gen, invocation, success);
421
422
423
424
425
426
427
428
429
430
431

  return TRUE;
}

static void
name_appeared_handler (GDBusConnection *connection,
                       const gchar     *name,
                       const gchar     *name_owner,
                       gpointer         user_data)
{
  FlashbackShell *shell;
432
  GfShellGen *skeleton;
433
434
435
  GError *error;

  shell = FLASHBACK_SHELL (user_data);
436
  skeleton = gf_shell_gen_skeleton_new ();
437
438
439
440
441
442
443

  g_signal_connect (skeleton, "handle-eval",
                    G_CALLBACK (handle_eval), shell);
  g_signal_connect (skeleton, "handle-focus-search",
                    G_CALLBACK (handle_focus_search), shell);
  g_signal_connect (skeleton, "handle-show-osd",
                    G_CALLBACK (handle_show_osd), shell);
444
445
  g_signal_connect (skeleton, "handle-show-monitor-labels",
                    G_CALLBACK (handle_show_monitor_labels), shell);
446
  g_signal_connect (skeleton, "handle-hide-monitor-labels",
447
                    G_CALLBACK (handle_hide_monitor_labels), shell);
448
449
450
451
452
453
454
455
456
457
  g_signal_connect (skeleton, "handle-focus-app",
                    G_CALLBACK (handle_focus_app), shell);
  g_signal_connect (skeleton, "handle-show-applications",
                    G_CALLBACK (handle_show_applications), shell);
  g_signal_connect (skeleton, "handle-grab-accelerator",
                    G_CALLBACK (handle_grab_accelerator), shell);
  g_signal_connect (skeleton, "handle-grab-accelerators",
                    G_CALLBACK (handle_grab_accelerators), shell);
  g_signal_connect (skeleton, "handle-ungrab-accelerator",
                    G_CALLBACK (handle_ungrab_accelerator), shell);
458
459
  g_signal_connect (skeleton, "handle-ungrab-accelerators",
                    G_CALLBACK (handle_ungrab_accelerators), shell);
460

461
462
463
  gf_shell_gen_set_mode (skeleton, "");
  gf_shell_gen_set_overview_active (skeleton, FALSE);
  gf_shell_gen_set_shell_version (skeleton, "");
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490

  error = NULL;
  shell->iface = G_DBUS_INTERFACE_SKELETON (skeleton);

	if (!g_dbus_interface_skeleton_export (shell->iface, connection,
	                                       SHELL_DBUS_PATH,
	                                       &error))
  {
    g_warning ("Failed to export interface: %s", error->message);
    g_error_free (error);
    return;
  }
}

static void
flashback_shell_finalize (GObject *object)
{
  FlashbackShell *shell;

  shell = FLASHBACK_SHELL (object);

  if (shell->bus_name)
    {
      g_bus_unwatch_name (shell->bus_name);
      shell->bus_name = 0;
    }

Alberts Muktupāvels's avatar
Alberts Muktupāvels committed
491
492
493
494
495
496
497
498
  if (shell->iface != NULL)
    {
      g_dbus_interface_skeleton_unexport (shell->iface);

      g_object_unref (shell->iface);
      shell->iface = NULL;
    }

499
500
501
502
503
504
505
506
  if (shell->grabbed_accelerators)
    {
      g_hash_table_destroy (shell->grabbed_accelerators);
      shell->grabbed_accelerators = NULL;
    }

  if (shell->grabbers)
    {
507
      g_hash_table_foreach (shell->grabbers, remove_watch, NULL);
508
509
510
511
      g_hash_table_destroy (shell->grabbers);
      shell->grabbers = NULL;
    }

512
  g_clear_object (&shell->keybindings);
513
  g_clear_object (&shell->labeler);
514
  g_clear_object (&shell->osd);
515
  g_clear_object (&shell->introspect);
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535

  G_OBJECT_CLASS (flashback_shell_parent_class)->finalize (object);
}

static void
flashback_shell_class_init (FlashbackShellClass *shell_class)
{
  GObjectClass *object_class;

  object_class = G_OBJECT_CLASS (shell_class);

  object_class->finalize = flashback_shell_finalize;
}

static void
flashback_shell_init (FlashbackShell *shell)
{
  shell->grabbed_accelerators = g_hash_table_new_full (NULL, NULL, NULL, g_free);
  shell->grabbers = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);

536
537
  shell->keybindings = gf_keybindings_new ();

538
539
  g_signal_connect (shell->keybindings, "accelerator-activated",
                    G_CALLBACK (accelerator_activated), shell);
540

541
  shell->labeler = flashback_monitor_labeler_new ();
542
  shell->osd = flashback_osd_new ();
543

544
545
  shell->introspect = gf_shell_introspect_new ();

546
547
548
549
550
551
552
553
554
555
556
557
558
559
  shell->bus_name = g_bus_watch_name (G_BUS_TYPE_SESSION,
                                      SHELL_DBUS_NAME,
                                      G_BUS_NAME_WATCHER_FLAGS_NONE,
                                      name_appeared_handler,
                                      NULL,
                                      shell,
                                      NULL);
}

FlashbackShell *
flashback_shell_new (void)
{
	return g_object_new (FLASHBACK_TYPE_SHELL, NULL);
}
560
561

void
562
563
flashback_shell_set_monitor_manager (FlashbackShell   *shell,
                                     GfMonitorManager *monitor_manager)
564
{
565
  shell->monitor_manager = monitor_manager;
566
}