eek-keyboard.c 25.2 KB
Newer Older
Daiki Ueno's avatar
Daiki Ueno committed
1
/* 
Daiki Ueno's avatar
Daiki Ueno committed
2
3
 * Copyright (C) 2010-2011 Daiki Ueno <ueno@unixuser.org>
 * Copyright (C) 2010-2011 Red Hat, Inc.
Daiki Ueno's avatar
Daiki Ueno committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

/**
 * SECTION:eek-keyboard
Daiki Ueno's avatar
Daiki Ueno committed
23
 * @short_description: Base class of a keyboard
Daiki Ueno's avatar
Daiki Ueno committed
24
25
 * @see_also: #EekSection
 *
Daiki Ueno's avatar
Daiki Ueno committed
26
27
 * The #EekKeyboardClass class represents a keyboard, which consists
 * of one or more sections of the #EekSectionClass class.
Daiki Ueno's avatar
Daiki Ueno committed
28
29
30
31
32
33
34
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif  /* HAVE_CONFIG_H */

#include "eek-keyboard.h"
Daiki Ueno's avatar
Daiki Ueno committed
35
36
#include "eek-section.h"
#include "eek-key.h"
37
#include "eek-symbol.h"
38
#include "eek-serializable.h"
Daiki Ueno's avatar
Daiki Ueno committed
39
40
41

enum {
    PROP_0,
42
    PROP_LAYOUT,
43
    PROP_MODIFIER_BEHAVIOR,
Daiki Ueno's avatar
Daiki Ueno committed
44
45
46
47
48
49
50
51
52
53
54
    PROP_LAST
};

enum {
    KEY_PRESSED,
    KEY_RELEASED,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0, };

55
56
57
58
59
static void eek_serializable_iface_init (EekSerializableIface *iface);

G_DEFINE_TYPE_WITH_CODE (EekKeyboard, eek_keyboard, EEK_TYPE_CONTAINER,
                         G_IMPLEMENT_INTERFACE (EEK_TYPE_SERIALIZABLE,
                                                eek_serializable_iface_init));
Daiki Ueno's avatar
Daiki Ueno committed
60
61
62
63
64
65
66

#define EEK_KEYBOARD_GET_PRIVATE(obj)                                  \
    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EEK_TYPE_KEYBOARD, EekKeyboardPrivate))


struct _EekKeyboardPrivate
{
Daiki Ueno's avatar
Daiki Ueno committed
67
    EekLayout *layout;
68
69
    EekModifierBehavior modifier_behavior;
    EekModifierType modifiers;
70
    GArray *outline_array;
71
72

    /* modifiers dynamically assigned at run time */
73
    EekModifierType num_lock_mask;
74
    EekModifierType alt_gr_mask;
Daiki Ueno's avatar
Daiki Ueno committed
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
static EekSerializableIface *eek_keyboard_parent_serializable_iface;

static GVariant *_g_variant_new_outline (EekOutline *outline);
static EekOutline *_g_variant_get_outline (GVariant *variant);

static GVariant *
_g_variant_new_outline (EekOutline *outline)
{
    GVariantBuilder builder, array;
    gint i;

    g_variant_builder_init (&builder, G_VARIANT_TYPE ("(div)"));
    g_variant_builder_add (&builder, "d", outline->corner_radius);
    g_variant_builder_add (&builder, "i", outline->num_points);
    g_variant_builder_init (&array, G_VARIANT_TYPE ("a(dd)"));
    for (i = 0; i < outline->num_points; i++)
        g_variant_builder_add (&array,
                               "(dd)",
                               outline->points[i].x,
                               outline->points[i].y);
    g_variant_builder_add (&builder, "v", g_variant_builder_end (&array));
    return g_variant_builder_end (&builder);
}

static EekOutline *
_g_variant_get_outline (GVariant *variant)
{
    EekOutline *outline;
    GVariant *array;
    GVariantIter iter;
    gdouble x, y;
    gint i;

    outline = g_slice_new0 (EekOutline);

    g_variant_get_child (variant, 0, "d", &outline->corner_radius);
    g_variant_get_child (variant, 1, "i", &outline->num_points);

    outline->points = g_slice_alloc0 (sizeof (EekPoint) * outline->num_points);

    g_variant_get_child (variant, 2, "v", &array);
    g_variant_iter_init (&iter, array);
    for (i = 0; i < outline->num_points; i++) {
        if (!g_variant_iter_next (&iter, "(dd)", &x, &y)) {
            eek_outline_free (outline);
            g_return_val_if_reached (NULL);
        }
        outline->points[i].x = x;
        outline->points[i].y = y;
    }

    return outline;
}

static void
eek_keyboard_real_serialize (EekSerializable *self,
                             GVariantBuilder *builder)
{
    EekKeyboardPrivate *priv = EEK_KEYBOARD_GET_PRIVATE(self);
    GVariantBuilder array;
    guint i;

    eek_keyboard_parent_serializable_iface->serialize (self, builder);

    g_variant_builder_init (&array, G_VARIANT_TYPE ("av"));
    for (i = 0; i < priv->outline_array->len; i++) {
        EekOutline *outline =
            eek_keyboard_get_outline (EEK_KEYBOARD(self), i + 1);
        g_variant_builder_add (&array, "v",
                               _g_variant_new_outline (outline));
    }
    g_variant_builder_add (builder, "v", g_variant_builder_end (&array));
149
    g_variant_builder_add (builder, "u", priv->num_lock_mask);
Daiki Ueno's avatar
Daiki Ueno committed
150
    g_variant_builder_add (builder, "u", priv->alt_gr_mask);
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
}

static gsize
eek_keyboard_real_deserialize (EekSerializable *self,
                               GVariant        *variant,
                               gsize            index)
{
    EekKeyboardPrivate *priv = EEK_KEYBOARD_GET_PRIVATE(self);
    GVariant *array, *outline;
    GVariantIter iter;

    index = eek_keyboard_parent_serializable_iface->deserialize (self,
                                                                 variant,
                                                                 index);

    g_variant_get_child (variant, index++, "v", &array);

    g_variant_iter_init (&iter, array);
    while (g_variant_iter_next (&iter, "v", &outline)) {
        EekOutline *_outline = _g_variant_get_outline (outline);
        g_array_append_val (priv->outline_array, *_outline);
172
173
        /* don't use eek_outline_free here, so as to keep _outline->points */
        g_slice_free (EekOutline, _outline);
174
    }
175
    g_variant_get_child (variant, index++, "u", &priv->num_lock_mask);
Daiki Ueno's avatar
Daiki Ueno committed
176
    g_variant_get_child (variant, index++, "u", &priv->alt_gr_mask);
177
178
179
180
181
182
183
184
185
186
187
188
189
190

    return index;
}

static void
eek_serializable_iface_init (EekSerializableIface *iface)
{
    eek_keyboard_parent_serializable_iface =
        g_type_interface_peek_parent (iface);

    iface->serialize = eek_keyboard_real_serialize;
    iface->deserialize = eek_keyboard_real_deserialize;
}

Daiki Ueno's avatar
Daiki Ueno committed
191
static void
Daiki Ueno's avatar
Daiki Ueno committed
192
193
194
on_key_pressed (EekSection  *section,
                EekKey      *key,
                EekKeyboard *keyboard)
Daiki Ueno's avatar
Daiki Ueno committed
195
196
197
198
199
{
    g_signal_emit_by_name (keyboard, "key-pressed", key);
}

static void
Daiki Ueno's avatar
Daiki Ueno committed
200
201
202
on_key_released (EekSection  *section,
                 EekKey      *key,
                 EekKeyboard *keyboard)
Daiki Ueno's avatar
Daiki Ueno committed
203
{
Daiki Ueno's avatar
Daiki Ueno committed
204
205
206
    g_signal_emit_by_name (keyboard, "key-released", key);
}

207
208
209
210
211
212
213
214
215
static void
on_symbol_index_changed (EekSection *section,
                         gint group,
                         gint level,
                         EekKeyboard *keyboard)
{
    g_signal_emit_by_name (keyboard, "symbol-index-changed", group, level);
}

Daiki Ueno's avatar
Daiki Ueno committed
216
217
218
219
static EekSection *
eek_keyboard_real_create_section (EekKeyboard *self)
{
    EekSection *section;
Daiki Ueno's avatar
Daiki Ueno committed
220

Daiki Ueno's avatar
Daiki Ueno committed
221
222
223
224
225
226
227
228
    section = g_object_new (EEK_TYPE_SECTION, NULL);
    g_return_val_if_fail (section, NULL);

    EEK_CONTAINER_GET_CLASS(self)->add_child (EEK_CONTAINER(self),
                                              EEK_ELEMENT(section));
    return section;
}

Daiki Ueno's avatar
Daiki Ueno committed
229
struct _FindKeyByKeycodeCallbackData {
Daiki Ueno's avatar
Daiki Ueno committed
230
231
232
    EekKey *key;
    guint keycode;
};
Daiki Ueno's avatar
Daiki Ueno committed
233
typedef struct _FindKeyByKeycodeCallbackData FindKeyByKeycodeCallbackData;
Daiki Ueno's avatar
Daiki Ueno committed
234
235

static gint
Daiki Ueno's avatar
Daiki Ueno committed
236
find_key_by_keycode_section_callback (EekElement *element, gpointer user_data)
Daiki Ueno's avatar
Daiki Ueno committed
237
{
Daiki Ueno's avatar
Daiki Ueno committed
238
    FindKeyByKeycodeCallbackData *data = user_data;
Daiki Ueno's avatar
Daiki Ueno committed
239
240
241
242
243
244
245

    data->key = eek_section_find_key_by_keycode (EEK_SECTION(element),
                                                 data->keycode);
    if (data->key)
        return 0;
    return -1;
}
Daiki Ueno's avatar
Daiki Ueno committed
246

Daiki Ueno's avatar
Daiki Ueno committed
247
248
249
250
static EekKey *
eek_keyboard_real_find_key_by_keycode (EekKeyboard *self,
                                       guint        keycode)
{
Daiki Ueno's avatar
Daiki Ueno committed
251
    FindKeyByKeycodeCallbackData data;
Daiki Ueno's avatar
Daiki Ueno committed
252
253
254

    data.keycode = keycode;
    if (eek_container_find (EEK_CONTAINER(self),
Daiki Ueno's avatar
Daiki Ueno committed
255
                            find_key_by_keycode_section_callback,
Daiki Ueno's avatar
Daiki Ueno committed
256
257
258
259
260
                            &data))
        return data.key;
    return NULL;
}

Daiki Ueno's avatar
Daiki Ueno committed
261
static void
Daiki Ueno's avatar
Daiki Ueno committed
262
263
264
265
eek_keyboard_set_property (GObject      *object,
                           guint         prop_id,
                           const GValue *value,
                           GParamSpec   *pspec)
Daiki Ueno's avatar
Daiki Ueno committed
266
{
267
    EekKeyboardPrivate *priv = EEK_KEYBOARD_GET_PRIVATE(object);
Daiki Ueno's avatar
Daiki Ueno committed
268
269

    switch (prop_id) {
270
271
    case PROP_LAYOUT:
        priv->layout = g_value_get_object (value);
272
273
        if (priv->layout)
            g_object_ref (priv->layout);
274
        break;
275
276
277
278
    case PROP_MODIFIER_BEHAVIOR:
        eek_keyboard_set_modifier_behavior (EEK_KEYBOARD(object),
                                            g_value_get_int (value));
        break;
Daiki Ueno's avatar
Daiki Ueno committed
279
280
281
282
283
284
285
286
287
288
289
290
291
292
    default:
        g_object_set_property (object,
                               g_param_spec_get_name (pspec),
                               value);
        break;
    }
}

static void
eek_keyboard_get_property (GObject    *object,
                           guint       prop_id,
                           GValue     *value,
                           GParamSpec *pspec)
{
293
    EekKeyboardPrivate *priv = EEK_KEYBOARD_GET_PRIVATE(object);
Daiki Ueno's avatar
Daiki Ueno committed
294
295

    switch (prop_id) {
296
297
298
    case PROP_LAYOUT:
        g_value_set_object (value, priv->layout);
        break;
299
300
301
302
    case PROP_MODIFIER_BEHAVIOR:
        g_value_set_int (value,
                         eek_keyboard_get_modifier_behavior (EEK_KEYBOARD(object)));
        break;
Daiki Ueno's avatar
Daiki Ueno committed
303
304
305
306
307
308
309
310
    default:
        g_object_get_property (object,
                               g_param_spec_get_name (pspec),
                               value);
        break;
    }
}

311
312
313
314
static void
set_level_from_modifiers (EekKeyboard *self)
{
    EekKeyboardPrivate *priv = EEK_KEYBOARD_GET_PRIVATE(self);
315
    gint level = 0;
316

317
    if (priv->modifiers & priv->alt_gr_mask)
318
319
320
        level |= 2;
    if (priv->modifiers & EEK_SHIFT_MASK)
        level |= 1;
321
    eek_element_set_level (EEK_ELEMENT(self), level);
322
323
324
325
326
327
328
}

static void
eek_keyboard_real_key_pressed (EekKeyboard *self,
                               EekKey      *key)
{
    EekKeyboardPrivate *priv = EEK_KEYBOARD_GET_PRIVATE(self);
329
    EekSymbol *symbol;
330
331
    EekModifierType modifier;

332
    symbol = eek_key_get_symbol_with_fallback (key, 0, 0);
333
334
335
336
    if (!symbol)
        return;

    modifier = eek_symbol_get_modifier_mask (symbol);
337
    if (priv->modifier_behavior == EEK_MODIFIER_BEHAVIOR_NONE) {
338
        priv->modifiers |= modifier;
339
        set_level_from_modifiers (self);
340
341
342
343
344
    }
}

static void
eek_keyboard_real_key_released (EekKeyboard *self,
Daiki Ueno's avatar
Daiki Ueno committed
345
                                EekKey      *key)
346
347
{
    EekKeyboardPrivate *priv = EEK_KEYBOARD_GET_PRIVATE(self);
348
    EekSymbol *symbol;
349
350
    EekModifierType modifier;

351
    symbol = eek_key_get_symbol_with_fallback (key, 0, 0);
352
353
354
355
    if (!symbol)
        return;

    modifier = eek_symbol_get_modifier_mask (symbol);
356
357
358
359
360
361
362
363
    switch (priv->modifier_behavior) {
    case EEK_MODIFIER_BEHAVIOR_NONE:
        priv->modifiers &= ~modifier;
        break;
    case EEK_MODIFIER_BEHAVIOR_LOCK:
        priv->modifiers ^= modifier;
        break;
    case EEK_MODIFIER_BEHAVIOR_LATCH:
364
365
366
367
        if (modifier == priv->alt_gr_mask || modifier == EEK_SHIFT_MASK)
            priv->modifiers ^= modifier;
        else
            priv->modifiers = (priv->modifiers ^ modifier) & modifier;
368
        break;
369
370
371
372
    }
    set_level_from_modifiers (self);
}

Daiki Ueno's avatar
Daiki Ueno committed
373
374
375
376
377
378
379
380
381
382
383
384
385
static void
eek_keyboard_dispose (GObject *object)
{
    EekKeyboardPrivate *priv = EEK_KEYBOARD_GET_PRIVATE(object);

    if (priv->layout) {
        g_object_unref (priv->layout);
        priv->layout = NULL;
    }

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

386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
static void
eek_keyboard_finalize (GObject *object)
{
    EekKeyboardPrivate *priv = EEK_KEYBOARD_GET_PRIVATE(object);
    gint i;

    for (i = 0; i < priv->outline_array->len; i++) {
        EekOutline *outline = &g_array_index (priv->outline_array,
                                              EekOutline,
                                              i);
        g_slice_free1 (sizeof (EekPoint) * outline->num_points,
                       outline->points);
    }
    g_array_free (priv->outline_array, TRUE);
        
    G_OBJECT_CLASS (eek_keyboard_parent_class)->finalize (object);
}

static void
eek_keyboard_real_child_added (EekContainer *self,
                               EekElement   *element)
{
    g_signal_connect (element, "key-pressed",
                      G_CALLBACK(on_key_pressed), self);
    g_signal_connect (element, "key-released",
                      G_CALLBACK(on_key_released), self);
412
413
    g_signal_connect (element, "symbol-index-changed",
                      G_CALLBACK(on_symbol_index_changed), self);
414
415
416
417
418
419
420
421
422
423
}

static void
eek_keyboard_real_child_removed (EekContainer *self,
                                 EekElement   *element)
{
    g_signal_handlers_disconnect_by_func (element, on_key_pressed, self);
    g_signal_handlers_disconnect_by_func (element, on_key_released, self);
}

Daiki Ueno's avatar
Daiki Ueno committed
424
425
426
static void
eek_keyboard_class_init (EekKeyboardClass *klass)
{
427
    EekContainerClass *container_class = EEK_CONTAINER_CLASS (klass);
Daiki Ueno's avatar
Daiki Ueno committed
428
429
430
431
432
433
434
    GObjectClass      *gobject_class = G_OBJECT_CLASS (klass);
    GParamSpec        *pspec;

    g_type_class_add_private (gobject_class,
                              sizeof (EekKeyboardPrivate));

    klass->create_section = eek_keyboard_real_create_section;
Daiki Ueno's avatar
Daiki Ueno committed
435
    klass->find_key_by_keycode = eek_keyboard_real_find_key_by_keycode;
Daiki Ueno's avatar
Daiki Ueno committed
436

437
438
439
    /* signals */
    klass->key_pressed = eek_keyboard_real_key_pressed;
    klass->key_released = eek_keyboard_real_key_released;
Daiki Ueno's avatar
Daiki Ueno committed
440

441
442
443
    container_class->child_added = eek_keyboard_real_child_added;
    container_class->child_removed = eek_keyboard_real_child_removed;

Daiki Ueno's avatar
Daiki Ueno committed
444
445
    gobject_class->get_property = eek_keyboard_get_property;
    gobject_class->set_property = eek_keyboard_set_property;
Daiki Ueno's avatar
Daiki Ueno committed
446
    gobject_class->dispose = eek_keyboard_dispose;
447
    gobject_class->finalize = eek_keyboard_finalize;
Daiki Ueno's avatar
Daiki Ueno committed
448

449
450
451
452
453
454
455
456
457
458
459
460
461
462
    /**
     * EekKeyboard:layout:
     *
     * The layout used to create this #EekKeyboard.
     */
    pspec = g_param_spec_object ("layout",
                                 "Layout",
                                 "Layout used to create the keyboard",
                                 EEK_TYPE_LAYOUT,
                                 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
    g_object_class_install_property (gobject_class,
                                     PROP_LAYOUT,
                                     pspec);

463
464
465
466
467
468
469
470
471
472
473
474
475
476
    /**
     * EekKeyboard:modifier-behavior:
     *
     * The modifier handling mode of #EekKeyboard.
     */
    pspec = g_param_spec_int ("modifier-behavior",
                              "Modifier behavior",
                              "Modifier handling mode of the keyboard",
                              0, G_MAXINT, EEK_MODIFIER_BEHAVIOR_NONE,
                              G_PARAM_READWRITE);
    g_object_class_install_property (gobject_class,
                                     PROP_MODIFIER_BEHAVIOR,
                                     pspec);

Daiki Ueno's avatar
Daiki Ueno committed
477
478
479
480
481
482
483
484
    /**
     * EekKeyboard::key-pressed:
     * @keyboard: an #EekKeyboard
     * @key: an #EekKey
     *
     * The ::key-pressed signal is emitted each time a key in @keyboard
     * is shifted to the pressed state.
     */
Daiki Ueno's avatar
Daiki Ueno committed
485
    signals[KEY_PRESSED] =
486
        g_signal_new (I_("key-pressed"),
Daiki Ueno's avatar
Daiki Ueno committed
487
                      G_TYPE_FROM_CLASS(gobject_class),
488
489
                      G_SIGNAL_RUN_LAST,
                      G_STRUCT_OFFSET(EekKeyboardClass, key_pressed),
Daiki Ueno's avatar
Daiki Ueno committed
490
491
492
493
494
495
496
                      NULL,
                      NULL,
                      g_cclosure_marshal_VOID__OBJECT,
                      G_TYPE_NONE,
                      1,
                      EEK_TYPE_KEY);

Daiki Ueno's avatar
Daiki Ueno committed
497
498
499
500
501
502
503
504
    /**
     * EekKeyboard::key-released:
     * @keyboard: an #EekKeyboard
     * @key: an #EekKey
     *
     * The ::key-released signal is emitted each time a key in @keyboard
     * is shifted to the released state.
     */
Daiki Ueno's avatar
Daiki Ueno committed
505
    signals[KEY_RELEASED] =
506
        g_signal_new (I_("key-released"),
Daiki Ueno's avatar
Daiki Ueno committed
507
                      G_TYPE_FROM_CLASS(gobject_class),
508
509
                      G_SIGNAL_RUN_LAST,
                      G_STRUCT_OFFSET(EekKeyboardClass, key_released),
Daiki Ueno's avatar
Daiki Ueno committed
510
511
512
513
514
515
516
517
518
519
520
521
522
523
                      NULL,
                      NULL,
                      g_cclosure_marshal_VOID__OBJECT,
                      G_TYPE_NONE,
                      1,
                      EEK_TYPE_KEY);
}

static void
eek_keyboard_init (EekKeyboard *self)
{
    EekKeyboardPrivate *priv;

    priv = self->priv = EEK_KEYBOARD_GET_PRIVATE(self);
Daiki Ueno's avatar
Daiki Ueno committed
524
    priv->layout = NULL;
525
526
    priv->modifier_behavior = EEK_MODIFIER_BEHAVIOR_NONE;
    priv->modifiers = 0;
527
    priv->outline_array = g_array_new (FALSE, TRUE, sizeof (EekOutline));
528
    priv->num_lock_mask = 0;
529
    eek_element_set_symbol_index (EEK_ELEMENT(self), 0, 0);
Daiki Ueno's avatar
Daiki Ueno committed
530
531
}

532
/**
533
 * eek_keyboard_set_symbol_index:
534
535
536
537
 * @keyboard: an #EekKeyboard
 * @group: row index of the symbol matrix of keys on @keyboard
 * @level: column index of the symbol matrix of keys on @keyboard
 *
538
539
540
541
 * Set the default index of the symbol matrices of keys in @keyboard.
 * To unset, pass -1 as group/level.
 *
 * Deprecated: 1.0: Use eek_element_set_symbol_index()
542
543
 */
void
544
eek_keyboard_set_symbol_index (EekKeyboard *keyboard,
545
546
547
                               gint         group,
                               gint         level)
{
Daiki Ueno's avatar
Daiki Ueno committed
548
    g_return_if_fail (EEK_IS_KEYBOARD(keyboard));
549
    eek_element_set_symbol_index (EEK_ELEMENT(keyboard), group, level);
550
551
552
}

/**
553
 * eek_keyboard_get_symbol_index:
554
 * @keyboard: an #EekKeyboard
555
556
557
558
559
 * @group: a pointer where the group value of the symbol index will be stored
 * @level: a pointer where the level value of the symbol index will be stored
 *
 * Get the default index of the symbol matrices of keys in @keyboard.
 * If the index is not set, -1 will be returned.
560
 *
561
 * Deprecated: 1.0: Use eek_element_get_symbol_index()
562
563
 */
void
564
eek_keyboard_get_symbol_index (EekKeyboard *keyboard,
565
566
567
                               gint        *group,
                               gint        *level)
{
Daiki Ueno's avatar
Daiki Ueno committed
568
    g_return_if_fail (EEK_IS_KEYBOARD(keyboard));
569
    eek_element_get_symbol_index(EEK_ELEMENT(keyboard), group, level);
570
571
}

572
573
574
575
576
/**
 * eek_keyboard_set_group:
 * @keyboard: an #EekKeyboard
 * @group: group index of @keyboard
 *
577
578
579
580
581
 * Set the group value of the default symbol index of @keyboard.  To
 * unset, pass -1 as @group.
 *
 * See also: eek_keyboard_set_symbol_index()
 * Deprecated: 1.0: Use eek_element_set_group()
582
583
584
585
586
 */
void
eek_keyboard_set_group (EekKeyboard *keyboard,
                        gint         group)
{
587
    eek_element_set_group (EEK_ELEMENT(keyboard), group);
588
589
590
591
592
593
594
}

/**
 * eek_keyboard_set_level:
 * @keyboard: an #EekKeyboard
 * @level: level index of @keyboard
 *
595
596
597
598
599
 * Set the level value of the default symbol index of @keyboard.  To
 * unset, pass -1 as @level.
 *
 * See also: eek_keyboard_set_symbol_index()
 * Deprecated: 1.0: Use eek_element_set_level()
600
601
602
603
604
 */
void
eek_keyboard_set_level (EekKeyboard *keyboard,
                        gint         level)
{
605
    eek_element_set_level (EEK_ELEMENT(keyboard), level);
606
607
608
609
610
611
}

/**
 * eek_keyboard_get_group:
 * @keyboard: an #EekKeyboard
 *
612
613
614
615
616
 * Return the group value of the default symbol index of @keyboard.
 * If the value is not set, -1 will be returned.
 *
 * See also: eek_keyboard_get_symbol_index()
 * Deprecated: 1.0: Use eek_element_get_group()
617
618
619
620
 */
gint
eek_keyboard_get_group (EekKeyboard *keyboard)
{
621
    return eek_element_get_group (EEK_ELEMENT(keyboard));
622
623
624
625
626
627
}

/**
 * eek_keyboard_get_level:
 * @keyboard: an #EekKeyboard
 *
628
629
630
631
632
 * Return the level value of the default symbol index of @keyboard.
 * If the value is not set, -1 will be returned.
 *
 * See also: eek_keyboard_get_symbol_index()
 * Deprecated: 1.0: Use eek_element_get_level()
633
634
635
636
 */
gint
eek_keyboard_get_level (EekKeyboard *keyboard)
{
637
    return eek_element_get_level (EEK_ELEMENT(keyboard));
638
639
}

Daiki Ueno's avatar
Daiki Ueno committed
640
641
/**
 * eek_keyboard_create_section:
Daiki Ueno's avatar
Daiki Ueno committed
642
 * @keyboard: an #EekKeyboard
Daiki Ueno's avatar
Daiki Ueno committed
643
 *
Daiki Ueno's avatar
Daiki Ueno committed
644
645
646
 * Create an #EekSection instance and append it to @keyboard.  This
 * function is rarely called by application but called by #EekLayout
 * implementation.
Daiki Ueno's avatar
Daiki Ueno committed
647
648
 */
EekSection *
Daiki Ueno's avatar
Daiki Ueno committed
649
eek_keyboard_create_section (EekKeyboard *keyboard)
Daiki Ueno's avatar
Daiki Ueno committed
650
{
Daiki Ueno's avatar
Daiki Ueno committed
651
652
653
654
    EekSection *section;
    g_return_val_if_fail (EEK_IS_KEYBOARD(keyboard), NULL);
    section = EEK_KEYBOARD_GET_CLASS(keyboard)->create_section (keyboard);
    return section;
Daiki Ueno's avatar
Daiki Ueno committed
655
656
}

Daiki Ueno's avatar
Daiki Ueno committed
657
658
659
660
/**
 * eek_keyboard_find_key_by_keycode:
 * @keyboard: an #EekKeyboard
 * @keycode: a keycode
Daiki Ueno's avatar
Daiki Ueno committed
661
 * @returns: (transfer none): #EekKey whose keycode is @keycode
Daiki Ueno's avatar
Daiki Ueno committed
662
663
664
 *
 * Find an #EekKey whose keycode is @keycode.
 */
Daiki Ueno's avatar
Daiki Ueno committed
665
666
667
668
EekKey *
eek_keyboard_find_key_by_keycode (EekKeyboard *keyboard,
                                  guint        keycode)
{
669
670
671
    g_assert (EEK_IS_KEYBOARD(keyboard));
    return EEK_KEYBOARD_GET_CLASS(keyboard)->
        find_key_by_keycode (keyboard, keycode);
Daiki Ueno's avatar
Daiki Ueno committed
672
}
673

Daiki Ueno's avatar
Daiki Ueno committed
674
675
676
677
678
679
680
/**
 * eek_keyboard_get_layout:
 * @keyboard: an #EekKeyboard
 *
 * Get the layout used to create @keyboard.
 * Returns: an #EekLayout
 */
681
682
EekLayout *
eek_keyboard_get_layout (EekKeyboard *keyboard)
683
{
684
    EekKeyboardPrivate *priv;
685

686
687
688
689
    g_assert (EEK_IS_KEYBOARD(keyboard));
    priv = EEK_KEYBOARD_GET_PRIVATE(keyboard);
    return priv->layout;
}
690

Daiki Ueno's avatar
Daiki Ueno committed
691
692
693
694
695
696
697
698
/**
 * eek_keyboard_get_size:
 * @keyboard: an #EekKeyboard
 * @width: width of @keyboard
 * @height: height of @keyboard
 *
 * Get the size of @keyboard.
 */
699
700
701
702
703
704
void
eek_keyboard_get_size (EekKeyboard *keyboard,
                       gdouble     *width,
                       gdouble     *height)
{
    EekBounds bounds;
705

706
707
708
709
    g_assert (EEK_IS_KEYBOARD(keyboard));
    eek_element_get_bounds (EEK_ELEMENT(keyboard), &bounds);
    *width = bounds.width;
    *height = bounds.height;
710
}
711

Daiki Ueno's avatar
Daiki Ueno committed
712
713
714
715
716
717
718
/**
 * eek_keyboard_set_modifier_behavior:
 * @keyboard: an #EekKeyboard
 * @modifier_behavior: modifier behavior of @keyboard
 *
 * Set the modifier handling mode of @keyboard.
 */
719
720
721
722
723
724
725
726
727
728
729
730
void
eek_keyboard_set_modifier_behavior (EekKeyboard        *keyboard,
                                    EekModifierBehavior modifier_behavior)
{
    EekKeyboardPrivate *priv;

    g_return_if_fail (EEK_IS_KEYBOARD(keyboard));
    priv = EEK_KEYBOARD_GET_PRIVATE(keyboard);

    priv->modifier_behavior = modifier_behavior;
}

Daiki Ueno's avatar
Daiki Ueno committed
731
732
733
734
735
736
737
/**
 * eek_keyboard_get_modifier_behavior:
 * @keyboard: an #EekKeyboard
 *
 * Get the modifier handling mode of @keyboard.
 * Returns: #EekModifierBehavior
 */
738
739
740
741
742
743
744
745
746
747
748
EekModifierBehavior
eek_keyboard_get_modifier_behavior (EekKeyboard *keyboard)
{
    EekKeyboardPrivate *priv;

    g_assert (EEK_IS_KEYBOARD(keyboard));
    priv = EEK_KEYBOARD_GET_PRIVATE(keyboard);

    return priv->modifier_behavior;
}

Daiki Ueno's avatar
Daiki Ueno committed
749
750
751
752
753
754
755
/**
 * eek_keyboard_get_modifiers:
 * @keyboard: an #EekKeyboard
 *
 * Get the current modifier status of @keyboard.
 * Returns: #EekModifierType
 */
756
757
758
759
760
761
762
763
764
765
EekModifierType
eek_keyboard_get_modifiers (EekKeyboard *keyboard)
{
    EekKeyboardPrivate *priv;

    g_assert (EEK_IS_KEYBOARD(keyboard));
    priv = EEK_KEYBOARD_GET_PRIVATE(keyboard);

    return priv->modifiers;
}
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786

/**
 * eek_keyboard_add_outline:
 * @keyboard: an #EekKeyboard
 * @outline: an #EekOutline
 *
 * Register an outline of @keyboard.
 * Returns: an unsigned long id of the registered outline, for later reference
 */
gulong
eek_keyboard_add_outline (EekKeyboard *keyboard,
                          EekOutline  *outline)
{
    EekKeyboardPrivate *priv;
    EekOutline *_outline;

    g_assert (EEK_IS_KEYBOARD(keyboard));
    priv = EEK_KEYBOARD_GET_PRIVATE(keyboard);

    _outline = eek_outline_copy (outline);
    g_array_append_val (priv->outline_array, *_outline);
Daiki Ueno's avatar
Daiki Ueno committed
787
788
    /* don't use eek_outline_free here, so as to keep _outline->points */
    g_slice_free (EekOutline, _outline);
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
    return priv->outline_array->len;
}

/**
 * eek_keyboard_get_outline:
 * @keyboard: an #EekKeyboard
 * @oref: an unsigned long id
 *
 * Get an outline associated with @oref in @keyboard.
 * Returns: an #EekOutline, which should not be released
 */
EekOutline *
eek_keyboard_get_outline (EekKeyboard *keyboard,
                          gulong oref)
{
    EekKeyboardPrivate *priv;

    g_assert (EEK_IS_KEYBOARD(keyboard));
    priv = EEK_KEYBOARD_GET_PRIVATE(keyboard);

    if (oref > priv->outline_array->len)
        return NULL;

    return &g_array_index (priv->outline_array, EekOutline, oref - 1);
}
814

815
816
817
818
819
820
821
/**
 * eek_keyboard_set_num_lock_mask:
 * @keyboard: an #EekKeyboard
 * @num_lock_mask: an #EekModifierType
 *
 * Set modifier mask used as Num_Lock.
 */
822
823
824
825
826
827
828
829
830
831
832
833
void
eek_keyboard_set_num_lock_mask (EekKeyboard    *keyboard,
                                EekModifierType num_lock_mask)
{
    EekKeyboardPrivate *priv;

    g_assert (EEK_IS_KEYBOARD(keyboard));
    priv = EEK_KEYBOARD_GET_PRIVATE(keyboard);

    priv->num_lock_mask = num_lock_mask;
}

834
835
836
837
838
839
840
/**
 * eek_keyboard_get_num_lock_mask:
 * @keyboard: an #EekKeyboard
 *
 * Get modifier mask used as Num_Lock.
 * Returns: an #EekModifierType
 */
841
842
843
844
845
846
847
848
849
850
EekModifierType
eek_keyboard_get_num_lock_mask (EekKeyboard *keyboard)
{
    EekKeyboardPrivate *priv;

    g_assert (EEK_IS_KEYBOARD(keyboard));
    priv = EEK_KEYBOARD_GET_PRIVATE(keyboard);

    return priv->num_lock_mask;
}
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887

/**
 * eek_keyboard_set_alt_gr_mask:
 * @keyboard: an #EekKeyboard
 * @alt_gr_mask: an #EekModifierType
 *
 * Set modifier mask used as Alt_Gr.
 */
void
eek_keyboard_set_alt_gr_mask (EekKeyboard    *keyboard,
                              EekModifierType alt_gr_mask)
{
    EekKeyboardPrivate *priv;

    g_assert (EEK_IS_KEYBOARD(keyboard));
    priv = EEK_KEYBOARD_GET_PRIVATE(keyboard);

    priv->alt_gr_mask = alt_gr_mask;
}

/**
 * eek_keyboard_get_alt_gr_mask:
 * @keyboard: an #EekKeyboard
 *
 * Get modifier mask used as Alt_Gr.
 * Returns: an #EekModifierType
 */
EekModifierType
eek_keyboard_get_alt_gr_mask (EekKeyboard *keyboard)
{
    EekKeyboardPrivate *priv;

    g_assert (EEK_IS_KEYBOARD(keyboard));
    priv = EEK_KEYBOARD_GET_PRIVATE(keyboard);

    return priv->alt_gr_mask;
}