server-context.c 25.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* 
 * Copyright (C) 2010-2011 Daiki Ueno <ueno@unixuser.org>
 * Copyright (C) 2010-2011 Red Hat, Inc.
 * 
 * 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/>.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif  /* HAVE_CONFIG_H */

Daiki Ueno's avatar
Daiki Ueno committed
22
23
24
#include <gtk/gtk.h>
#include <glib/gi18n.h>

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "eek/eek.h"

#if HAVE_CLUTTER_GTK
#include <clutter-gtk/clutter-gtk.h>
#include "eek/eek-clutter.h"
#else  /* HAVE_CLUTTER_GTK */
#include "eek/eek-gtk.h"
#endif  /* !HAVE_CLUTTER_GTK */

#include "server-context.h"

#define CSW 640
#define CSH 480

enum {
    PROP_0,
    PROP_OBJECT_PATH,
    PROP_CONNECTION,
    PROP_LAST
};

static const gchar introspection_xml[] =
    "<node>"
    "  <interface name='com.redhat.Eekboard.Context'>"
49
50
51
52
53
54
55
    "    <method name='AddKeyboard'>"
    "      <arg direction='in' type='v' name='keyboard'/>"
    "      <arg direction='out' type='u' name='keyboard_id'/>"
    "    </method>"
    "    <method name='RemoveKeyboard'>"
    "      <arg direction='in' type='u' name='keyboard_id'/>"
    "    </method>"
56
    "    <method name='SetKeyboard'>"
57
    "      <arg type='u' name='keyboard_id'/>"
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
    "    </method>"
    "    <method name='ShowKeyboard'/>"
    "    <method name='HideKeyboard'/>"
    "    <method name='SetGroup'>"
    "      <arg type='i' name='group'/>"
    "    </method>"
    "    <method name='PressKey'>"
    "      <arg type='u' name='keycode'/>"
    "    </method>"
    "    <method name='ReleaseKey'>"
    "      <arg type='u' name='keycode'/>"
    "    </method>"
    /* signals */
    "    <signal name='Enabled'/>"
    "    <signal name='Disabled'/>"
    "    <signal name='KeyPressed'>"
    "      <arg type='u' name='keycode'/>"
    "    </signal>"
    "    <signal name='KeyReleased'>"
    "      <arg type='u' name='keycode'/>"
    "    </signal>"
    "    <signal name='KeyboardVisibilityChanged'>"
    "      <arg type='b' name='visible'/>"
    "    </signal>"
    "  </interface>"
    "</node>";

typedef struct _ServerContextClass ServerContextClass;

struct _ServerContext {
    GObject parent;
    GDBusConnection *connection;
    GDBusNodeInfo *introspection_data;
    guint registration_id;
    char *object_path;
93
    char *client_connection;
94
95

    gboolean enabled;
96
    gboolean last_keyboard_visible;
97
98
99

    GtkWidget *window;
    GtkWidget *widget;
100
    guint keyboard_id;
101
    EekKeyboard *keyboard;
102
    GHashTable *keyboard_hash;
103
104
105

    gulong key_pressed_handler;
    gulong key_released_handler;
106
    gulong notify_visible_handler;
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
};

struct _ServerContextClass {
    GObjectClass parent_class;
};

G_DEFINE_TYPE (ServerContext, server_context, G_TYPE_OBJECT);

static void disconnect_keyboard_signals (ServerContext         *context);
static void handle_method_call          (GDBusConnection       *connection,
                                         const gchar           *sender,
                                         const gchar           *object_path,
                                         const gchar           *interface_name,
                                         const gchar           *method_name,
                                         GVariant              *parameters,
                                         GDBusMethodInvocation *invocation,
                                         gpointer               user_data);

static const GDBusInterfaceVTable interface_vtable =
{
  handle_method_call,
  NULL,
  NULL
};

#if HAVE_CLUTTER_GTK
static void
on_allocation_changed (ClutterActor          *stage,
                       ClutterActorBox       *box,
                       ClutterAllocationFlags flags,
                       gpointer               user_data)
{
    ClutterActor *actor = user_data;
    clutter_actor_set_size (actor,
                            box->x2 - box->x1,
                            box->y2 - box->y1);
}
#endif

static void
on_destroy (GtkWidget *widget, gpointer user_data)
{
    ServerContext *context = user_data;

    g_assert (widget == context->window);
    context->window = NULL;
    context->widget = NULL;
}

static void
on_notify_visible (GObject *object, GParamSpec *spec, gpointer user_data)
{
    ServerContext *context = user_data;
    gboolean visible;
    GError *error;

    g_object_get (object, "visible", &visible, NULL);

    if (context->connection && context->enabled) {
        error = NULL;
        g_dbus_connection_emit_signal (context->connection,
                                       NULL,
                                       context->object_path,
                                       SERVER_CONTEXT_INTERFACE,
                                       "KeyboardVisibilityChanged",
                                       g_variant_new ("(b)", visible),
                                       &error);
        g_assert_no_error (error);
    }
}

178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
static void
on_realize (GtkWidget *widget,
            gpointer   user_data)
{
    ServerContext *context = user_data;

    g_assert (context && context->window == widget);

    /* make the window not maximizable */
    gdk_window_set_functions (gtk_widget_get_window (widget),
                              GDK_FUNC_RESIZE |
                              GDK_FUNC_MOVE |
                              GDK_FUNC_MINIMIZE |
                              GDK_FUNC_CLOSE);
}

Daiki Ueno's avatar
Daiki Ueno committed
194
195
#define DEFAULT_THEME (THEMEDIR "/default.css")

196
197
198
199
200
201
202
203
static void
update_widget (ServerContext *context)
{
    GdkScreen *screen;
    GdkWindow *root;
    gint monitor;
    GdkRectangle rect;
    EekBounds bounds;
Daiki Ueno's avatar
Daiki Ueno committed
204
    EekTheme *theme;
205
206
207
208
209
210
211
212
#if HAVE_CLUTTER_GTK
    ClutterActor *stage, *actor;
    ClutterColor stage_color = { 0xff, 0xff, 0xff, 0xff };
#endif

    if (context->widget)
        gtk_widget_destroy (context->widget);

Daiki Ueno's avatar
Daiki Ueno committed
213
    theme = eek_theme_new (DEFAULT_THEME, NULL, NULL);
214
215
216
217
    eek_element_get_bounds (EEK_ELEMENT(context->keyboard), &bounds);
#if HAVE_CLUTTER_GTK
    context->widget = gtk_clutter_embed_new ();
    stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED(context->widget));
Daiki Ueno's avatar
Daiki Ueno committed
218
    actor = eek_clutter_keyboard_new (context->keyboard);
Daiki Ueno's avatar
Daiki Ueno committed
219
220
    if (theme)
        eek_clutter_keyboard_set_theme (EEK_CLUTTER_KEYBOARD(actor), theme);
221
222
223
224
225
226
227
228
229
230
231
232
233
    clutter_container_add_actor (CLUTTER_CONTAINER(stage), actor);

    clutter_stage_set_color (CLUTTER_STAGE(stage), &stage_color);
    clutter_stage_set_user_resizable (CLUTTER_STAGE(stage), TRUE);
    clutter_stage_set_minimum_size (CLUTTER_STAGE(stage),
                                    bounds.width / 3,
                                    bounds.height / 3);
    g_signal_connect (stage,
                      "allocation-changed",
                      G_CALLBACK(on_allocation_changed),
                      actor);
#else
    context->widget = eek_gtk_keyboard_new (context->keyboard);
Daiki Ueno's avatar
Daiki Ueno committed
234
235
    if (theme)
        eek_gtk_keyboard_set_theme (EEK_GTK_KEYBOARD(context->widget), theme);
236
237
238
#endif
    gtk_widget_set_size_request (context->widget, bounds.width, bounds.height);

Daiki Ueno's avatar
Daiki Ueno committed
239
240
241
242
243
244
245
246
247
248
    if (!context->window) {
        context->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        g_signal_connect (context->window, "destroy",
                          G_CALLBACK(on_destroy), context);
        context->notify_visible_handler =
            g_signal_connect (context->window, "notify::visible",
                              G_CALLBACK(on_notify_visible), context);

        gtk_widget_set_can_focus (context->window, FALSE);
        g_object_set (G_OBJECT(context->window), "accept_focus", FALSE, NULL);
Daiki Ueno's avatar
Daiki Ueno committed
249
        gtk_window_set_title (GTK_WINDOW(context->window), _("Keyboard"));
Daiki Ueno's avatar
Daiki Ueno committed
250
251
252
        gtk_window_set_icon_name (GTK_WINDOW(context->window), "eekboard");
        gtk_window_set_keep_above (GTK_WINDOW(context->window), TRUE);

253
254
255
        g_signal_connect (context->window, "realize",
                          G_CALLBACK(on_realize), context);

Daiki Ueno's avatar
Daiki Ueno committed
256
257
258
259
260
261
262
263
        screen = gdk_screen_get_default ();
        root = gtk_widget_get_root_window (context->window);
        monitor = gdk_screen_get_monitor_at_window (screen, root);
        gdk_screen_get_monitor_geometry (screen, monitor, &rect);
        gtk_window_move (GTK_WINDOW(context->window),
                         MAX(rect.width - 20 - bounds.width, 0),
                         MAX(rect.height - 40 - bounds.height, 0));
    }
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
    gtk_container_add (GTK_CONTAINER(context->window), context->widget);
}

static void
server_context_set_property (GObject      *object,
                             guint         prop_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
    ServerContext *context = SERVER_CONTEXT(object);
    GDBusConnection *connection;

    switch (prop_id) {
    case PROP_OBJECT_PATH:
        if (context->object_path)
            g_free (context->object_path);
        context->object_path = g_strdup (g_value_get_string (value));
        break;
    case PROP_CONNECTION:
        connection = g_value_get_object (value);
        if (context->connection)
            g_object_unref (context->connection);
        context->connection = g_object_ref (connection);
        break;
    default:
        g_object_set_property (object,
                               g_param_spec_get_name (pspec),
                               value);
        break;
    }
}

static void
server_context_dispose (GObject *object)
{
    ServerContext *context = SERVER_CONTEXT(object);

    if (context->keyboard) {
        disconnect_keyboard_signals (context);
        context->keyboard = NULL;
    }

306
307
308
309
310
    if (context->keyboard_hash) {
        g_hash_table_destroy (context->keyboard_hash);
        context->keyboard_hash = NULL;
    }

311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
    if (context->window) {
        gtk_widget_destroy (context->window);
        context->window = NULL;
    }

    if (context->connection) {
        if (context->registration_id > 0) {
            g_dbus_connection_unregister_object (context->connection,
                                                 context->registration_id);
            context->registration_id = 0;
        }

        g_object_unref (context->connection);
        context->connection = NULL;
    }

    if (context->introspection_data) {
        g_dbus_node_info_unref (context->introspection_data);
        context->introspection_data = NULL;
    }

    G_OBJECT_CLASS (server_context_parent_class)->dispose (object);
}

335
336
337
338
339
static void
server_context_finalize (GObject *object)
{
    ServerContext *context = SERVER_CONTEXT(object);

340
    g_free (context->object_path);
341
342
343
344
345
    g_free (context->client_connection);

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

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
static void
server_context_constructed (GObject *object)
{
    ServerContext *context = SERVER_CONTEXT (object);
    if (context->connection && context->object_path) {
        GError *error = NULL;

        context->registration_id = g_dbus_connection_register_object
            (context->connection,
             context->object_path,
             context->introspection_data->interfaces[0],
             &interface_vtable,
             context,
             NULL,
             &error);
    }
}

static void
server_context_class_init (ServerContextClass *klass)
{
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
    GParamSpec *pspec;

    gobject_class->constructed = server_context_constructed;
    gobject_class->set_property = server_context_set_property;
    gobject_class->dispose = server_context_dispose;
373
    gobject_class->finalize = server_context_finalize;
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406

    pspec = g_param_spec_string ("object-path",
                                 "Object-path",
                                 "Object-path",
                                 NULL,
                                 G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE);
    g_object_class_install_property (gobject_class,
                                     PROP_OBJECT_PATH,
                                     pspec);

    pspec = g_param_spec_object ("connection",
                                 "Connection",
                                 "Connection",
                                 G_TYPE_DBUS_CONNECTION,
                                 G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE);
    g_object_class_install_property (gobject_class,
                                     PROP_CONNECTION,
                                     pspec);
}

static void
server_context_init (ServerContext *context)
{
    GError *error;

    context->connection = NULL;
    error = NULL;
    context->introspection_data =
        g_dbus_node_info_new_for_xml (introspection_xml, &error);
    g_assert (context->introspection_data != NULL);
    context->registration_id = 0;
    context->object_path = NULL;

407
408
409
    context->enabled = FALSE;
    context->last_keyboard_visible = FALSE;

410
    context->keyboard = NULL;
411
412
413
414
415
416
    context->keyboard_hash =
        g_hash_table_new_full (g_direct_hash,
                               g_direct_equal,
                               NULL,
                               (GDestroyNotify)g_object_unref);

417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
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
491
492
493
    context->widget = NULL;
    context->window = NULL;
    context->key_pressed_handler = 0;
    context->key_released_handler = 0;
}

static void
on_key_pressed (EekKeyboard *keyboard,
                EekKey      *key,
                gpointer     user_data)
{
    ServerContext *context = user_data;

    if (context->connection && context->enabled) {
        guint keycode = eek_key_get_keycode (key);
        GError *error;

        error = NULL;
        g_dbus_connection_emit_signal (context->connection,
                                       NULL,
                                       context->object_path,
                                       SERVER_CONTEXT_INTERFACE,
                                       "KeyPressed",
                                       g_variant_new ("(u)", keycode),
                                       &error);
        g_assert_no_error (error);
    }
}

static void
on_key_released (EekKeyboard *keyboard,
                 EekKey      *key,
                 gpointer     user_data)
{
    ServerContext *context = user_data;

    if (context->connection && context->enabled) {
        guint keycode = eek_key_get_keycode (key);
        GError *error;

        error = NULL;
        g_dbus_connection_emit_signal (context->connection,
                                       NULL,
                                       context->object_path,
                                       SERVER_CONTEXT_INTERFACE,
                                       "KeyReleased",
                                       g_variant_new ("(u)", keycode),
                                       &error);
        g_assert_no_error (error);
    }
}

static void
disconnect_keyboard_signals (ServerContext *context)
{
    if (g_signal_handler_is_connected (context->keyboard,
                                       context->key_pressed_handler))
        g_signal_handler_disconnect (context->keyboard,
                                     context->key_pressed_handler);
    if (g_signal_handler_is_connected (context->keyboard,
                                       context->key_released_handler))
        g_signal_handler_disconnect (context->keyboard,
                                     context->key_released_handler);
}

static void
handle_method_call (GDBusConnection       *connection,
                    const gchar           *sender,
                    const gchar           *object_path,
                    const gchar           *interface_name,
                    const gchar           *method_name,
                    GVariant              *parameters,
                    GDBusMethodInvocation *invocation,
                    gpointer               user_data)
{
    ServerContext *context = user_data;

494
    if (g_strcmp0 (method_name, "AddKeyboard") == 0) {
495
        GVariant *variant;
496
497
        EekSerializable *serializable;
        static guint keyboard_id = 0;
498
499

        g_variant_get (parameters, "(v)", &variant);
500

501
502
503
504
505
506
507
508
        serializable = eek_serializable_deserialize (variant);
        if (!EEK_IS_KEYBOARD(serializable)) {
            g_dbus_method_invocation_return_error (invocation,
                                                   G_IO_ERROR,
                                                   G_IO_ERROR_FAILED_HANDLED,
                                                   "not a keyboard");
            return;
        }
509
510
511
512
513
        eek_keyboard_set_modifier_behavior (EEK_KEYBOARD(serializable),
                                            EEK_MODIFIER_BEHAVIOR_LATCH);

        g_hash_table_insert (context->keyboard_hash,
                             GUINT_TO_POINTER(++keyboard_id),
514
                             serializable);
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
        g_dbus_method_invocation_return_value (invocation,
                                               g_variant_new ("(u)",
                                                              keyboard_id));
        return;
    }

    if (g_strcmp0 (method_name, "RemoveKeyboard") == 0) {
        guint keyboard_id;

        g_variant_get (parameters, "(u)", &keyboard_id);

        if (keyboard_id == context->keyboard_id) {
            disconnect_keyboard_signals (context);
            if (context->window) {
                gtk_widget_hide (context->window);
                gtk_widget_destroy (context->widget);
            }

            context->keyboard = NULL;
        }

        g_hash_table_remove (context->keyboard_hash,
                             GUINT_TO_POINTER(keyboard_id));
        g_dbus_method_invocation_return_value (invocation, NULL);
        return;
    }

    if (g_strcmp0 (method_name, "SetKeyboard") == 0) {
        EekKeyboard *keyboard;
        guint keyboard_id;

        g_variant_get (parameters, "(u)", &keyboard_id);

        keyboard = g_hash_table_lookup (context->keyboard_hash,
                                        GUINT_TO_POINTER(keyboard_id));
        if (!keyboard) {
            g_dbus_method_invocation_return_error (invocation,
                                                   G_IO_ERROR,
                                                   G_IO_ERROR_FAILED_HANDLED,
                                                   "no such keyboard");
            return;
        }

        if (keyboard == context->keyboard) {
            g_dbus_method_invocation_return_value (invocation, NULL);
            return;
        }

        if (context->keyboard)
            disconnect_keyboard_signals (context);

        context->keyboard = keyboard;

568
569
570
571
572
573
574
575
576
577
578
        context->key_pressed_handler =
            g_signal_connect (context->keyboard, "key-pressed",
                              G_CALLBACK(on_key_pressed),
                              context);
        context->key_released_handler =
            g_signal_connect (context->keyboard, "key-released",
                              G_CALLBACK(on_key_released),
                              context);
        
        if (context->window) {
            gboolean was_visible = gtk_widget_get_visible (context->window);
579
580
581
            /* avoid to send KeyboardVisibilityChanged */
            g_signal_handler_block (context->window,
                                    context->notify_visible_handler);
582
583
584
            update_widget (context);
            if (was_visible)
                gtk_widget_show_all (context->window);
585
586
            g_signal_handler_unblock (context->window,
                                      context->notify_visible_handler);
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
        }

        g_dbus_method_invocation_return_value (invocation, NULL);
        return;
    }

    if (g_strcmp0 (method_name, "SetGroup") == 0) {
        gint group;

        if (!context->keyboard) {
            g_dbus_method_invocation_return_error (invocation,
                                                   G_IO_ERROR,
                                                   G_IO_ERROR_FAILED_HANDLED,
                                                   "keyboard is not set");
            return;
        }

        g_variant_get (parameters, "(i)", &group);
605
        eek_element_set_group (EEK_ELEMENT(context->keyboard), group);
606
607
608

        if (context->window) {
            gboolean was_visible = gtk_widget_get_visible (context->window);
609
610
611
612

            /* avoid to send KeyboardVisibilityChanged */
            g_signal_handler_block (context->window,
                                    context->notify_visible_handler);
613
614
615
            update_widget (context);
            if (was_visible)
                gtk_widget_show_all (context->window);
616
617
            g_signal_handler_unblock (context->window,
                                      context->notify_visible_handler);
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
        }

        g_dbus_method_invocation_return_value (invocation, NULL);
        return;
    }

    if (g_strcmp0 (method_name, "ShowKeyboard") == 0) {
        if (!context->keyboard) {
            g_dbus_method_invocation_return_error (invocation,
                                                   G_IO_ERROR,
                                                   G_IO_ERROR_FAILED_HANDLED,
                                                   "keyboard is not set");
            return;
        }

        if (!context->window)
            update_widget (context);
        g_assert (context->window);
        gtk_widget_show_all (context->window);
        g_dbus_method_invocation_return_value (invocation, NULL);
        return;
    }

    if (g_strcmp0 (method_name, "HideKeyboard") == 0) {
        if (context->window)
            gtk_widget_hide (context->window);
        g_dbus_method_invocation_return_value (invocation, NULL);
        return;
    }

    if (g_strcmp0 (method_name, "PressKey") == 0 ||
        g_strcmp0 (method_name, "ReleaseKey") == 0) {
        EekKey *key;
        guint keycode;

        if (!context->keyboard) {
            g_dbus_method_invocation_return_error (invocation,
                                                   G_IO_ERROR,
                                                   G_IO_ERROR_FAILED_HANDLED,
                                                   "keyboard is not set");
            return;
        }

        g_variant_get (parameters, "(u)", &keycode);
        key = eek_keyboard_find_key_by_keycode (context->keyboard, keycode);

        if (!key) {
            g_dbus_method_invocation_return_error (invocation,
                                                   G_IO_ERROR,
                                                   G_IO_ERROR_FAILED_HANDLED,
                                                   "key for %u is not found",
                                                   keycode);
            return;
        }

        if (g_strcmp0 (method_name, "PressKey") == 0) {
            g_signal_handler_block (context->keyboard,
                                    context->key_pressed_handler);
            g_signal_emit_by_name (key, "pressed");
            g_signal_handler_unblock (context->keyboard,
                                      context->key_pressed_handler);
        } else {
            g_signal_handler_block (context->keyboard,
                                    context->key_released_handler);
            g_signal_emit_by_name (key, "released");
            g_signal_handler_unblock (context->keyboard,
                                      context->key_released_handler);
        }

        g_dbus_method_invocation_return_value (invocation, NULL);
        return;
    }

    g_return_if_reached ();
}

ServerContext *
server_context_new (const gchar     *object_path,
                    GDBusConnection *connection)
{
    return g_object_new (SERVER_TYPE_CONTEXT,
                         "object-path", object_path,
                         "connection", connection,
                         NULL);
}

void
server_context_set_enabled (ServerContext *context, gboolean enabled)
{
    GError *error;

    g_return_if_fail (SERVER_IS_CONTEXT(context));
    g_return_if_fail (context->connection);

    if (context->enabled == enabled)
        return;

715
    context->enabled = enabled;
716
717
718
719
720
721
722
723
724
725
    if (enabled) {
        error = NULL;
        g_dbus_connection_emit_signal (context->connection,
                                       NULL,
                                       context->object_path,
                                       SERVER_CONTEXT_INTERFACE,
                                       "Enabled",
                                       NULL,
                                       &error);
        g_assert_no_error (error);
726
727
        if (context->last_keyboard_visible && context->window)
            gtk_widget_show_all (context->window);
728
729
730
731
732
733
734
735
736
737
    } else {
        error = NULL;
        g_dbus_connection_emit_signal (context->connection,
                                       NULL,
                                       context->object_path,
                                       SERVER_CONTEXT_INTERFACE,
                                       "Disabled",
                                       NULL,
                                       &error);
        g_assert_no_error (error);
738
739
740
741
742
        if (context->window) {
            context->last_keyboard_visible =
                gtk_widget_get_visible (context->window);
            gtk_widget_hide (context->window);
        }
743
744
    }
}
745
746
747
748
749
750
751
752
753
754
755
756
757

void
server_context_set_client_connection (ServerContext *context,
                                      const gchar   *client_connection)
{
    context->client_connection = g_strdup (client_connection);
}

const gchar *
server_context_get_client_connection (ServerContext *context)
{
    return context->client_connection;
}