~noskcaj/ubuntu/saucy/sflphone/merge-1.2.3-2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
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
373
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
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
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
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
527
/*
 *  Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
 *  Author: Julien Bonjean <julien.bonjean@savoirfairelinux.com>
 *
 *  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, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
 */

#include <string.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <X11/Xlib.h>
#include <X11/XF86keysym.h>
#include <gdk/gdkx.h>
#include <dbus/dbus-glib.h>
#include <stdlib.h>
#include <stdio.h>

#include "shortcuts.h"
#include "mainwindow.h"
#include "callable_obj.h"
#include "contacts/calltab.h"
#include "sflphone_const.h"
#include "dbus.h"
#include "actions.h"

static void
ungrab_key(guint key, GdkModifierType mask, GdkWindow *root);

static void
grab_key(guint key, GdkModifierType mask, GdkWindow *root);

// used to store accelerator config
static Accelerator* accelerators_list;

// used to store config (for dbus calls)
static GHashTable* shortcutsMap;


/*
 * XLib functions
 */

/*
 * filter used when an event is catched
 */
static GdkFilterReturn
filter_keys(const GdkXEvent *xevent, G_GNUC_UNUSED const GdkEvent *event, G_GNUC_UNUSED gpointer data)
{
    if (((XEvent *) xevent)->type != KeyPress)
        return GDK_FILTER_CONTINUE;

    XKeyEvent *key = (XKeyEvent *) xevent;
    GdkModifierType keystate = key->state & ~(Mod2Mask | Mod5Mask | LockMask);

    for (int i = 0; accelerators_list[i].action; ++i)
        if (accelerators_list[i].key == key->keycode && accelerators_list[i].mask == keystate) {
            accelerators_list[i].callback(accelerators_list[i].data);
            return GDK_FILTER_REMOVE;
        }

    return GDK_FILTER_CONTINUE;
}

/*
 * Callbacks
 */
static void
toggle_pick_up_hang_up_callback(SFLPhoneClient *client)
{
    callable_obj_t * selectedCall = calltab_get_selected_call(active_calltree_tab);
    conference_obj_t * selectedConf = calltab_get_selected_conf(active_calltree_tab);

    g_debug("Shortcuts: Toggle pickup/hangup callback");

    if (selectedCall) {
        switch (selectedCall->_state) {
            case CALL_STATE_INCOMING:
            case CALL_STATE_TRANSFER:
                sflphone_pick_up(client);
                break;
            case CALL_STATE_DIALING:
            case CALL_STATE_HOLD:
            case CALL_STATE_CURRENT:
            case CALL_STATE_RINGING:
                sflphone_hang_up(client);
                break;
            default:
                break;
        }
    } else if (selectedConf) {
        dbus_hang_up_conference(selectedConf);
    } else
        sflphone_pick_up(client);
}

static void
pick_up_callback(gpointer data)
{
    sflphone_pick_up(data);
}

static void
hang_up_callback(gpointer data)
{
    sflphone_hang_up(data);
}

static void
toggle_hold_callback(G_GNUC_UNUSED gpointer data)
{
    callable_obj_t * selectedCall = calltab_get_selected_call(current_calls_tab);
    conference_obj_t * selectedConf = calltab_get_selected_conf(active_calltree_tab);

    if (selectedCall) {
        switch (selectedCall->_state) {
            case CALL_STATE_CURRENT:
                sflphone_on_hold();
                break;
            case CALL_STATE_HOLD:
                sflphone_off_hold();
                break;
            default:
                break;
        }
    } else if (selectedConf)
        dbus_hold_conference(selectedConf);
    else {
        g_warning("Shortcuts: Error: No callable object selected");
    }
}

static void
popup_window_callback(SFLPhoneClient *client)
{
    gtk_widget_hide(client->win);
    gtk_widget_show(client->win);
}

static void
default_callback(G_GNUC_UNUSED gpointer data)
{
    g_warning("Shortcuts: Error: Missing shortcut callback");
}

/*
 * return callback corresponding to a specific action
 */
static void*
get_action_callback(const gchar* action)
{
    if (g_strcmp0(action, SHORTCUT_PICKUP) == 0)
        return pick_up_callback;

    if (g_strcmp0(action, SHORTCUT_HANGUP) == 0)
        return hang_up_callback;

    if (g_strcmp0(action, SHORTCUT_POPUP) == 0)
        return popup_window_callback;

    if (g_strcmp0(action, SHORTCUT_TOGGLEPICKUPHANGUP) == 0)
        return toggle_pick_up_hang_up_callback;

    if (g_strcmp0(action, SHORTCUT_TOGGLEHOLD) == 0)
        return toggle_hold_callback;

    return default_callback;
}

/*
 * Handle bindings
 */

/*
 * Remove all existing bindings
 */
static void
remove_bindings()
{
    if (!accelerators_list)
        return;

    GdkDisplay *display = NULL;
    GdkScreen *screen = NULL;
    GdkWindow *root = NULL;
    int i, j = 0;

    display = gdk_display_get_default();

    for (i = 0; i < gdk_display_get_n_screens(display); i++) {
        screen = gdk_display_get_screen(display, i);

        if (screen != NULL) {
            j = 0;
            root = gdk_screen_get_root_window(screen);

            // remove filter
            gdk_window_remove_filter(root, (GdkFilterFunc) filter_keys, NULL);

            // unbind shortcuts
            while (accelerators_list[j].action != NULL) {
                if (accelerators_list[j].key != 0) {
                    ungrab_key(accelerators_list[j].key,
                               accelerators_list[j].mask, root);
                }

                j++;
            }
        }
    }
}

/*
 * Create all bindings, using stored configuration
 */
static void
create_bindings()
{
    GdkDisplay *display;
    GdkScreen *screen;
    GdkWindow *root;
    int i, j = 0;

    display = gdk_display_get_default();

    for (i = 0; i < gdk_display_get_n_screens(display); i++) {
        screen = gdk_display_get_screen(display, i);

        if (screen != NULL) {
            j = 0;
            root = gdk_screen_get_root_window(screen);

            // add filter
            gdk_window_add_filter(root, (GdkFilterFunc) filter_keys, NULL);

            // bind shortcuts
            while (accelerators_list[j].action != NULL) {
                if (accelerators_list[j].key != 0) {
                    grab_key(accelerators_list[j].key,
                             accelerators_list[j].mask, root);
                }

                j++;
            }
        }
    }
}

/*
 * Initialize a specific binding
 */
static void
initialize_binding(const gchar* action, guint key, GdkModifierType mask)
{
    int i = 0;

    while (accelerators_list[i].action != NULL) {
        if (g_strcmp0(action, accelerators_list[i].action) == 0) {
            break;
        }

        i++;
    }

    if (accelerators_list[i].action == NULL) {
        g_warning("Shortcut: Error: Cannot find corresponding action");
        return;
    }

    // update config value
    accelerators_list[i].key = key;
    accelerators_list[i].mask = mask;

    // update bindings
    create_bindings();
}

/*
 * Prepare accelerators list
 */
static void
initialize_accelerators_list(SFLPhoneClient *client)
{
    GList* shortcutsKeysElement, *shortcutsKeys;
    int i = 0;

    shortcutsKeys = g_hash_table_get_keys(shortcutsMap);

    accelerators_list = (Accelerator*) malloc(
                            (g_list_length(shortcutsKeys) + 1) * sizeof(Accelerator));

    for (shortcutsKeysElement = shortcutsKeys; shortcutsKeysElement; shortcutsKeysElement
            = shortcutsKeysElement->next) {
        gchar* action = shortcutsKeysElement->data;

        accelerators_list[i].action = g_strdup(action);
        accelerators_list[i].callback = get_action_callback(action);
        accelerators_list[i].data = client;
        accelerators_list[i].mask = 0;
        accelerators_list[i].key = 0;

        i++;
    }

    g_list_free(shortcutsKeys);

    // last element must be null
    accelerators_list[i].action = 0;
    accelerators_list[i].callback = 0;
    accelerators_list[i].mask = 0;
    accelerators_list[i].key = 0;
}

static void
update_shortcuts_map(const gchar* action, guint key, GdkModifierType mask)
{
    // Bindings: MASKxCODE
    gchar *buffer = g_strdup_printf("%dx%d", mask, key);
    g_hash_table_replace(shortcutsMap, g_strdup(action), buffer);
}

static void
update_bindings_data(guint accel_index, guint key, GdkModifierType mask)
{
    int i = 0;

    // we need to be sure this code is not already affected
    // to another action
    while (accelerators_list[i].action != NULL) {
        if (accelerators_list[i].key == key && accelerators_list[i].mask == mask
                && accelerators_list[i].key != 0) {
            g_debug("Shortcuts: Existing mapping found %d+%d", mask, key);

            // disable old binding
            accelerators_list[i].key = 0;
            accelerators_list[i].mask = 0;

            // update config table
            update_shortcuts_map(accelerators_list[i].action, 0, 0);
        }

        i++;
    }

    // store new key
    accelerators_list[accel_index].key = key;
    accelerators_list[accel_index].mask = mask;

    // update value in hashtable (used for dbus calls)
    update_shortcuts_map(accelerators_list[accel_index].action,
                         accelerators_list[accel_index].key,
                         accelerators_list[accel_index].mask);
}

/*
 * "Public" functions
 */

/*
 * Update current bindings with a new value
 */
void
shortcuts_update_bindings(guint shortcut_index, guint key, GdkModifierType mask)
{
    // first remove all existing bindings
    remove_bindings();

    // update data
    update_bindings_data(shortcut_index, key, mask);

    // recreate all bindings
    create_bindings();

    // update configuration
    dbus_set_shortcuts(shortcutsMap);
}

/*
 * Initialize bindings with configuration retrieved from dbus
 */
void
shortcuts_initialize_bindings(SFLPhoneClient *client)
{
    GList* shortcutsKeys, *shortcutsKeysElement = NULL;
    gchar* action, *maskAndKey, *token1, *token2 = NULL;
    guint mask, key = 0;

    g_debug("Shortcuts: Initialize bindings");

    // get shortcuts stored in config through dbus
    shortcutsMap = dbus_get_shortcuts();

    // initialize list of keys
    initialize_accelerators_list(client);

    // iterate through keys to initialize bindings
    shortcutsKeys = g_hash_table_get_keys(shortcutsMap);

    for (shortcutsKeysElement = shortcutsKeys; shortcutsKeysElement; shortcutsKeysElement
            = shortcutsKeysElement->next) {
        action = shortcutsKeysElement->data;
        maskAndKey = g_hash_table_lookup(shortcutsMap, action);

        token1 = strtok(maskAndKey, "x");
        token2 = strtok(NULL, "x");

        mask = 0;
        key = 0;

        // Value not setted
        if (token1 && token2) {
            g_debug("Shortcuts: token1 %s, token2 %s", token1, token2);

            mask = atoi(token1);
            key = atoi(token2);
        }

        if (key != 0)
            initialize_binding(action, key, mask);
    }
}

/*
 * Initialize bindings with configuration retrieved from dbus
 */
void
shortcuts_destroy_bindings()
{
    int i = 0;
    if (!accelerators_list)
        return;

    // remove bindings
    remove_bindings();

    // free pointers
    while (accelerators_list[i].action != NULL) {
        g_free(accelerators_list[i].action);
        i++;
    }

    free(accelerators_list);
}

Accelerator*
shortcuts_get_list()
{
    return accelerators_list;
}

/*
 * Remove key "catcher" from GDK layer
 */
static void
ungrab_key(guint key, GdkModifierType mask, GdkWindow *root)
{
    g_debug("Shortcuts: Ungrabbing key %d+%d", mask, key);

    gdk_error_trap_push();
    Display *d = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
    XID x = GDK_WINDOW_XID(root);

    XUngrabKey(d, key, mask | 0, x);
    XUngrabKey(d, key, mask | Mod2Mask, x);
    XUngrabKey(d, key, mask | Mod5Mask, x);
    XUngrabKey(d, key, mask | LockMask, x);
    XUngrabKey(d, key, mask | Mod2Mask | LockMask, x);
    XUngrabKey(d, key, mask | Mod2Mask | Mod5Mask, x);
    XUngrabKey(d, key, mask | Mod2Mask | LockMask, x);
    XUngrabKey(d, key, mask | Mod5Mask | LockMask, x);
    XUngrabKey(d, key, mask | Mod2Mask | Mod5Mask | LockMask, x);

    gdk_flush();

    if (gdk_error_trap_pop())
        g_warning("Shortcuts: Error: Ungrabbing key %d+%d", mask, key);
}

/*
 * Add key "catcher" to GDK layer
 */
static void
grab_key(guint key, GdkModifierType mask, GdkWindow *root)
{
    gdk_error_trap_push();

    g_debug("Shortcuts: Grabbing key %d+%d", mask, key);

    Display *d = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
    XID x = GDK_WINDOW_XID(root);

    XGrabKey(d, key, mask | 0, x, True, GrabModeAsync, GrabModeAsync);
    XGrabKey(d, key, mask | Mod2Mask, x, True, GrabModeAsync, GrabModeAsync);
    XGrabKey(d, key, mask | Mod5Mask, x, True, GrabModeAsync, GrabModeAsync);
    XGrabKey(d, key, mask | LockMask, x, True, GrabModeAsync, GrabModeAsync);
    XGrabKey(d, key, mask | Mod2Mask | Mod5Mask, x, True, GrabModeAsync, GrabModeAsync);
    XGrabKey(d, key, mask | Mod2Mask | LockMask, x, True, GrabModeAsync, GrabModeAsync);
    XGrabKey(d, key, mask | Mod5Mask | LockMask, x, True, GrabModeAsync, GrabModeAsync);
    XGrabKey(d, key, mask | Mod2Mask | Mod5Mask | LockMask, x, True, GrabModeAsync, GrabModeAsync);

    gdk_flush();

    if (gdk_error_trap_pop())
        g_warning("Shortcuts: Error: Grabbing key %d+%d", mask, key);
}