server-context.c 26.9 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
    "    </method>"
Daiki Ueno's avatar
Daiki Ueno committed
59
60
61
    "    <method name='SetFullscreen'>"
    "      <arg type='b' name='fullscreen'/>"
    "    </method>"
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
    "    <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;
96
    char *client_connection;
97
98

    gboolean enabled;
99
    gboolean last_keyboard_visible;
Daiki Ueno's avatar
Daiki Ueno committed
100
    gboolean fullscreen;
101
102
103

    GtkWidget *window;
    GtkWidget *widget;
104
    guint keyboard_id;
105
    EekKeyboard *keyboard;
106
    GHashTable *keyboard_hash;
107
108
109

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

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

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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
198
199
#define DEFAULT_THEME (THEMEDIR "/default.css")

200
static void
Daiki Ueno's avatar
Daiki Ueno committed
201
set_geometry (ServerContext *context)
202
203
204
205
206
207
{
    GdkScreen *screen;
    GdkWindow *root;
    gint monitor;
    GdkRectangle rect;
    EekBounds bounds;
Daiki Ueno's avatar
Daiki Ueno committed
208
209
210
211
212
213
214
215
216

    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);
    eek_element_get_bounds (EEK_ELEMENT(context->keyboard), &bounds);

    if (context->fullscreen) {
        gtk_window_set_decorated (GTK_WINDOW(context->window), FALSE);
Daiki Ueno's avatar
Daiki Ueno committed
217
        gtk_window_set_resizable (GTK_WINDOW(context->window), FALSE);
Daiki Ueno's avatar
Daiki Ueno committed
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
        gtk_widget_set_size_request (context->widget,
                                     rect.width,
                                     rect.height / 2);
        gtk_window_move (GTK_WINDOW(context->window),
                         0,
                         rect.height / 2);
        gtk_window_set_opacity (GTK_WINDOW(context->window), 0.8);
    } else {
#if HAVE_CLUTTER_GTK
        ClutterActor *stage =
            gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED(context->widget));
        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
        gtk_widget_set_size_request (context->widget,
                                     bounds.width,
                                     bounds.height);
#endif
        gtk_window_move (GTK_WINDOW(context->window),
                         MAX(rect.width - 20 - bounds.width, 0),
                         MAX(rect.height - 40 - bounds.height, 0));
    }
}

static void
update_widget (ServerContext *context)
{
    EekBounds bounds;
Daiki Ueno's avatar
Daiki Ueno committed
252
    EekTheme *theme;
253
254
255
256
257
258
259
260
#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
261
    theme = eek_theme_new (DEFAULT_THEME, NULL, NULL);
262
263
264
265
    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
266
    actor = eek_clutter_keyboard_new (context->keyboard);
Daiki Ueno's avatar
Daiki Ueno committed
267
268
    if (theme)
        eek_clutter_keyboard_set_theme (EEK_CLUTTER_KEYBOARD(actor), theme);
269
270
271
272
273
    clutter_container_add_actor (CLUTTER_CONTAINER(stage), actor);

    clutter_stage_set_color (CLUTTER_STAGE(stage), &stage_color);
#else
    context->widget = eek_gtk_keyboard_new (context->keyboard);
Daiki Ueno's avatar
Daiki Ueno committed
274
275
    if (theme)
        eek_gtk_keyboard_set_theme (EEK_GTK_KEYBOARD(context->widget), theme);
276
277
#endif

Daiki Ueno's avatar
Daiki Ueno committed
278
279
280
281
282
283
284
285
286
287
    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
288
        gtk_window_set_title (GTK_WINDOW(context->window), _("Keyboard"));
Daiki Ueno's avatar
Daiki Ueno committed
289
290
291
        gtk_window_set_icon_name (GTK_WINDOW(context->window), "eekboard");
        gtk_window_set_keep_above (GTK_WINDOW(context->window), TRUE);

292
293
294
        g_signal_connect (context->window, "realize",
                          G_CALLBACK(on_realize), context);

Daiki Ueno's avatar
Daiki Ueno committed
295
        set_geometry (context);
Daiki Ueno's avatar
Daiki Ueno committed
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
    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;
    }

339
340
341
342
343
    if (context->keyboard_hash) {
        g_hash_table_destroy (context->keyboard_hash);
        context->keyboard_hash = NULL;
    }

344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
    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);
}

368
369
370
371
372
static void
server_context_finalize (GObject *object)
{
    ServerContext *context = SERVER_CONTEXT(object);

373
    g_free (context->object_path);
374
375
376
377
378
    g_free (context->client_connection);

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

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
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;
406
    gobject_class->finalize = server_context_finalize;
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439

    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;

440
441
442
    context->enabled = FALSE;
    context->last_keyboard_visible = FALSE;

443
    context->keyboard = NULL;
444
445
446
447
448
449
    context->keyboard_hash =
        g_hash_table_new_full (g_direct_hash,
                               g_direct_equal,
                               NULL,
                               (GDestroyNotify)g_object_unref);

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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
    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;

527
    if (g_strcmp0 (method_name, "AddKeyboard") == 0) {
528
        GVariant *variant;
529
530
        EekSerializable *serializable;
        static guint keyboard_id = 0;
531
532

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

534
535
536
537
538
539
540
541
        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;
        }
542
543
544
545
546
        eek_keyboard_set_modifier_behavior (EEK_KEYBOARD(serializable),
                                            EEK_MODIFIER_BEHAVIOR_LATCH);

        g_hash_table_insert (context->keyboard_hash,
                             GUINT_TO_POINTER(++keyboard_id),
547
                             serializable);
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
        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;

601
602
603
604
605
606
607
608
609
610
611
        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);
612
613
614
            /* avoid to send KeyboardVisibilityChanged */
            g_signal_handler_block (context->window,
                                    context->notify_visible_handler);
615
616
617
            update_widget (context);
            if (was_visible)
                gtk_widget_show_all (context->window);
618
619
            g_signal_handler_unblock (context->window,
                                      context->notify_visible_handler);
620
621
622
623
624
625
        }

        g_dbus_method_invocation_return_value (invocation, NULL);
        return;
    }

Daiki Ueno's avatar
Daiki Ueno committed
626
627
628
629
630
631
632
633
634
635
    if (g_strcmp0 (method_name, "SetFullscreen") == 0) {
        gboolean fullscreen;

        g_variant_get (parameters, "(b)", &fullscreen);

        if (context->fullscreen == fullscreen) {
            g_dbus_method_invocation_return_value (invocation, NULL);
            return;
        }
        context->fullscreen = fullscreen;
Daiki Ueno's avatar
Daiki Ueno committed
636
637
        if (context->window)
            set_geometry (context);
Daiki Ueno's avatar
Daiki Ueno committed
638
639
640
641
642

        g_dbus_method_invocation_return_value (invocation, NULL);
        return;
    }

643
644
645
646
647
648
649
650
651
652
653
654
    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);
655
        eek_element_set_group (EEK_ELEMENT(context->keyboard), group);
656
657
658

        if (context->window) {
            gboolean was_visible = gtk_widget_get_visible (context->window);
659
660
661
662

            /* avoid to send KeyboardVisibilityChanged */
            g_signal_handler_block (context->window,
                                    context->notify_visible_handler);
663
664
665
            update_widget (context);
            if (was_visible)
                gtk_widget_show_all (context->window);
666
667
            g_signal_handler_unblock (context->window,
                                      context->notify_visible_handler);
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
        }

        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;

765
    context->enabled = enabled;
766
767
768
769
770
771
772
773
774
775
    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);
776
777
        if (context->last_keyboard_visible && context->window)
            gtk_widget_show_all (context->window);
778
779
780
781
782
783
784
785
786
787
    } 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);
788
789
790
791
792
        if (context->window) {
            context->last_keyboard_visible =
                gtk_widget_get_visible (context->window);
            gtk_widget_hide (context->window);
        }
793
794
    }
}
795
796
797
798
799
800
801
802
803
804
805
806
807

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