~ubuntu-branches/debian/sid/guake/sid

« back to all changes in this revision

Viewing changes to src/globalhotkeys/eggaccelerators.c

  • Committer: Package Import Robot
  • Author(s): Daniel Echeverry
  • Date: 2015-04-26 19:15:06 UTC
  • mfrom: (1.1.7)
  • mto: This revision was merged to the branch mainline in revision 26.
  • Revision ID: package-import@ubuntu.com-20150426191506-mo8037vk6pueer5b
Tags: upstream-0.7.0
ImportĀ upstreamĀ versionĀ 0.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* eggaccelerators.c
2
 
 * Copyright (C) 2002  Red Hat, Inc.; Copyright 1998, 2001 Tim Janik
3
 
 * Developed by Havoc Pennington, Tim Janik
4
 
 *
5
 
 * This library is free software; you can redistribute it and/or
6
 
 * modify it under the terms of the GNU Library General Public
7
 
 * License as published by the Free Software Foundation; either
8
 
 * version 2 of the License, or (at your option) any later version.
9
 
 *
10
 
 * This library is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 
 * Library General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU Library General Public
16
 
 * License along with this library; if not, write to the
17
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
 
 * Boston, MA 02111-1307, USA.
19
 
 */
20
 
 
21
 
#include "eggaccelerators.h"
22
 
 
23
 
#include <stdlib.h>
24
 
#include <string.h>
25
 
#include <gdk/gdkx.h>
26
 
#include <gdk/gdkkeysyms.h>
27
 
#include <gtk/gtkaccelgroup.h>
28
 
 
29
 
enum
30
 
{
31
 
  EGG_MODMAP_ENTRY_SHIFT   = 0,
32
 
  EGG_MODMAP_ENTRY_LOCK    = 1,
33
 
  EGG_MODMAP_ENTRY_CONTROL = 2,
34
 
  EGG_MODMAP_ENTRY_MOD1    = 3,
35
 
  EGG_MODMAP_ENTRY_MOD2    = 4,
36
 
  EGG_MODMAP_ENTRY_MOD3    = 5,
37
 
  EGG_MODMAP_ENTRY_MOD4    = 6,
38
 
  EGG_MODMAP_ENTRY_MOD5    = 7,
39
 
  EGG_MODMAP_ENTRY_LAST    = 8
40
 
};
41
 
 
42
 
#define MODMAP_ENTRY_TO_MODIFIER(x) (1 << (x))
43
 
 
44
 
typedef struct
45
 
{
46
 
  EggVirtualModifierType mapping[EGG_MODMAP_ENTRY_LAST];
47
 
 
48
 
} EggModmap;
49
 
 
50
 
const EggModmap* egg_keymap_get_modmap (GdkKeymap *keymap);
51
 
 
52
 
static inline gboolean
53
 
is_alt (const gchar *string)
54
 
{
55
 
  return ((string[0] == '<') &&
56
 
          (string[1] == 'a' || string[1] == 'A') &&
57
 
          (string[2] == 'l' || string[2] == 'L') &&
58
 
          (string[3] == 't' || string[3] == 'T') &&
59
 
          (string[4] == '>'));
60
 
}
61
 
 
62
 
static inline gboolean
63
 
is_ctl (const gchar *string)
64
 
{
65
 
  return ((string[0] == '<') &&
66
 
          (string[1] == 'c' || string[1] == 'C') &&
67
 
          (string[2] == 't' || string[2] == 'T') &&
68
 
          (string[3] == 'l' || string[3] == 'L') &&
69
 
          (string[4] == '>'));
70
 
}
71
 
 
72
 
static inline gboolean
73
 
is_modx (const gchar *string)
74
 
{
75
 
  return ((string[0] == '<') &&
76
 
          (string[1] == 'm' || string[1] == 'M') &&
77
 
          (string[2] == 'o' || string[2] == 'O') &&
78
 
          (string[3] == 'd' || string[3] == 'D') &&
79
 
          (string[4] >= '1' && string[4] <= '5') &&
80
 
          (string[5] == '>'));
81
 
}
82
 
 
83
 
static inline gboolean
84
 
is_ctrl (const gchar *string)
85
 
{
86
 
  return ((string[0] == '<') &&
87
 
          (string[1] == 'c' || string[1] == 'C') &&
88
 
          (string[2] == 't' || string[2] == 'T') &&
89
 
          (string[3] == 'r' || string[3] == 'R') &&
90
 
          (string[4] == 'l' || string[4] == 'L') &&
91
 
          (string[5] == '>'));
92
 
}
93
 
 
94
 
static inline gboolean
95
 
is_shft (const gchar *string)
96
 
{
97
 
  return ((string[0] == '<') &&
98
 
          (string[1] == 's' || string[1] == 'S') &&
99
 
          (string[2] == 'h' || string[2] == 'H') &&
100
 
          (string[3] == 'f' || string[3] == 'F') &&
101
 
          (string[4] == 't' || string[4] == 'T') &&
102
 
          (string[5] == '>'));
103
 
}
104
 
 
105
 
static inline gboolean
106
 
is_shift (const gchar *string)
107
 
{
108
 
  return ((string[0] == '<') &&
109
 
          (string[1] == 's' || string[1] == 'S') &&
110
 
          (string[2] == 'h' || string[2] == 'H') &&
111
 
          (string[3] == 'i' || string[3] == 'I') &&
112
 
          (string[4] == 'f' || string[4] == 'F') &&
113
 
          (string[5] == 't' || string[5] == 'T') &&
114
 
          (string[6] == '>'));
115
 
}
116
 
 
117
 
static inline gboolean
118
 
is_control (const gchar *string)
119
 
{
120
 
  return ((string[0] == '<') &&
121
 
          (string[1] == 'c' || string[1] == 'C') &&
122
 
          (string[2] == 'o' || string[2] == 'O') &&
123
 
          (string[3] == 'n' || string[3] == 'N') &&
124
 
          (string[4] == 't' || string[4] == 'T') &&
125
 
          (string[5] == 'r' || string[5] == 'R') &&
126
 
          (string[6] == 'o' || string[6] == 'O') &&
127
 
          (string[7] == 'l' || string[7] == 'L') &&
128
 
          (string[8] == '>'));
129
 
}
130
 
 
131
 
static inline gboolean
132
 
is_release (const gchar *string)
133
 
{
134
 
  return ((string[0] == '<') &&
135
 
          (string[1] == 'r' || string[1] == 'R') &&
136
 
          (string[2] == 'e' || string[2] == 'E') &&
137
 
          (string[3] == 'l' || string[3] == 'L') &&
138
 
          (string[4] == 'e' || string[4] == 'E') &&
139
 
          (string[5] == 'a' || string[5] == 'A') &&
140
 
          (string[6] == 's' || string[6] == 'S') &&
141
 
          (string[7] == 'e' || string[7] == 'E') &&
142
 
          (string[8] == '>'));
143
 
}
144
 
 
145
 
static inline gboolean
146
 
is_meta (const gchar *string)
147
 
{
148
 
  return ((string[0] == '<') &&
149
 
          (string[1] == 'm' || string[1] == 'M') &&
150
 
          (string[2] == 'e' || string[2] == 'E') &&
151
 
          (string[3] == 't' || string[3] == 'T') &&
152
 
          (string[4] == 'a' || string[4] == 'A') &&
153
 
          (string[5] == '>'));
154
 
}
155
 
 
156
 
static inline gboolean
157
 
is_super (const gchar *string)
158
 
{
159
 
  return ((string[0] == '<') &&
160
 
          (string[1] == 's' || string[1] == 'S') &&
161
 
          (string[2] == 'u' || string[2] == 'U') &&
162
 
          (string[3] == 'p' || string[3] == 'P') &&
163
 
          (string[4] == 'e' || string[4] == 'E') &&
164
 
          (string[5] == 'r' || string[5] == 'R') &&
165
 
          (string[6] == '>'));
166
 
}
167
 
 
168
 
static inline gboolean
169
 
is_hyper (const gchar *string)
170
 
{
171
 
  return ((string[0] == '<') &&
172
 
          (string[1] == 'h' || string[1] == 'H') &&
173
 
          (string[2] == 'y' || string[2] == 'Y') &&
174
 
          (string[3] == 'p' || string[3] == 'P') &&
175
 
          (string[4] == 'e' || string[4] == 'E') &&
176
 
          (string[5] == 'r' || string[5] == 'R') &&
177
 
          (string[6] == '>'));
178
 
}
179
 
 
180
 
is_primary (const gchar *string)
181
 
{
182
 
 return ((string[0] == '<') &&
183
 
 (string[1] == 'p' || string[1] == 'P') &&
184
 
 (string[2] == 'r' || string[2] == 'R') &&
185
 
 (string[3] == 'i' || string[3] == 'I') &&
186
 
 (string[4] == 'm' || string[4] == 'M') &&
187
 
 (string[5] == 'a' || string[5] == 'A') &&
188
 
 (string[6] == 'r' || string[6] == 'R') &&
189
 
 (string[7] == 'y' || string[7] == 'Y') &&
190
 
 (string[8] == '>'));
191
 
}
192
 
 
193
 
static inline gboolean
194
 
is_keycode (const gchar *string)
195
 
{
196
 
  return ((string[0] == '0') &&
197
 
          (string[1] == 'x'));
198
 
}
199
 
 
200
 
/**
201
 
 * egg_accelerator_parse_virtual:
202
 
 * @accelerator:      string representing an accelerator
203
 
 * @accelerator_key:  return location for accelerator keyval
204
 
 * @accelerator_mods: return location for accelerator modifier mask
205
 
 *
206
 
 * Parses a string representing a virtual accelerator. The format
207
 
 * looks like "&lt;Control&gt;a" or "&lt;Shift&gt;&lt;Alt&gt;F1" or
208
 
 * "&lt;Release&gt;z" (the last one is for key release).  The parser
209
 
 * is fairly liberal and allows lower or upper case, and also
210
 
 * abbreviations such as "&lt;Ctl&gt;" and "&lt;Ctrl&gt;".
211
 
 *
212
 
 * If the parse fails, @accelerator_key and @accelerator_mods will
213
 
 * be set to 0 (zero) and %FALSE will be returned. If the string contains
214
 
 * only modifiers, @accelerator_key will be set to 0 but %TRUE will be
215
 
 * returned.
216
 
 *
217
 
 * The virtual vs. concrete accelerator distinction is a relic of
218
 
 * how the X Window System works; there are modifiers Mod2-Mod5 that
219
 
 * can represent various keyboard keys (numlock, meta, hyper, etc.),
220
 
 * the virtual modifier represents the keyboard key, the concrete
221
 
 * modifier the actual Mod2-Mod5 bits in the key press event.
222
 
 *
223
 
 * Returns: %TRUE on success.
224
 
 */
225
 
gboolean
226
 
egg_accelerator_parse_virtual (const gchar            *accelerator,
227
 
                               guint                  *accelerator_key,
228
 
                               guint                  *keycode,
229
 
                               EggVirtualModifierType *accelerator_mods)
230
 
{
231
 
  guint keyval;
232
 
  GdkModifierType mods;
233
 
  gint len;
234
 
  gboolean bad_keyval;
235
 
 
236
 
  if (accelerator_key)
237
 
    *accelerator_key = 0;
238
 
  if (accelerator_mods)
239
 
    *accelerator_mods = 0;
240
 
  if (keycode)
241
 
    *keycode = 0;
242
 
 
243
 
  g_return_val_if_fail (accelerator != NULL, FALSE);
244
 
 
245
 
  bad_keyval = FALSE;
246
 
 
247
 
  keyval = 0;
248
 
  mods = 0;
249
 
  len = strlen (accelerator);
250
 
  while (len)
251
 
    {
252
 
      if (*accelerator == '<')
253
 
        {
254
 
          if (len >= 9 && is_release (accelerator))
255
 
            {
256
 
              accelerator += 9;
257
 
              len -= 9;
258
 
              mods |= EGG_VIRTUAL_RELEASE_MASK;
259
 
            }
260
 
          else if (len >= 9 && is_control (accelerator))
261
 
            {
262
 
              accelerator += 9;
263
 
              len -= 9;
264
 
              mods |= EGG_VIRTUAL_CONTROL_MASK;
265
 
            }
266
 
          else if (len >= 7 && is_shift (accelerator))
267
 
            {
268
 
              accelerator += 7;
269
 
              len -= 7;
270
 
              mods |= EGG_VIRTUAL_SHIFT_MASK;
271
 
            }
272
 
          else if (len >= 6 && is_shft (accelerator))
273
 
            {
274
 
              accelerator += 6;
275
 
              len -= 6;
276
 
              mods |= EGG_VIRTUAL_SHIFT_MASK;
277
 
            }
278
 
          else if (len >= 6 && is_ctrl (accelerator))
279
 
            {
280
 
              accelerator += 6;
281
 
              len -= 6;
282
 
              mods |= EGG_VIRTUAL_CONTROL_MASK;
283
 
            }
284
 
          else if (len >= 6 && is_modx (accelerator))
285
 
            {
286
 
              static const guint mod_vals[] = {
287
 
                EGG_VIRTUAL_ALT_MASK, EGG_VIRTUAL_MOD2_MASK, EGG_VIRTUAL_MOD3_MASK,
288
 
                EGG_VIRTUAL_MOD4_MASK, EGG_VIRTUAL_MOD5_MASK
289
 
              };
290
 
 
291
 
              len -= 6;
292
 
              accelerator += 4;
293
 
              mods |= mod_vals[*accelerator - '1'];
294
 
              accelerator += 2;
295
 
            }
296
 
          else if (len >= 5 && is_ctl (accelerator))
297
 
            {
298
 
              accelerator += 5;
299
 
              len -= 5;
300
 
              mods |= EGG_VIRTUAL_CONTROL_MASK;
301
 
            }
302
 
          else if (len >= 5 && is_alt (accelerator))
303
 
            {
304
 
              accelerator += 5;
305
 
              len -= 5;
306
 
              mods |= EGG_VIRTUAL_ALT_MASK;
307
 
            }
308
 
          else if (len >= 6 && is_meta (accelerator))
309
 
            {
310
 
              accelerator += 6;
311
 
              len -= 6;
312
 
              mods |= EGG_VIRTUAL_META_MASK;
313
 
            }
314
 
          else if (len >= 7 && is_hyper (accelerator))
315
 
            {
316
 
              accelerator += 7;
317
 
              len -= 7;
318
 
              mods |= EGG_VIRTUAL_HYPER_MASK;
319
 
            }
320
 
          else if (len >= 7 && is_super (accelerator))
321
 
            {
322
 
              accelerator += 7;
323
 
              len -= 7;
324
 
              mods |= EGG_VIRTUAL_SUPER_MASK;
325
 
            }
326
 
          else if (len >= 9 && is_primary (accelerator))
327
 
      {
328
 
        accelerator += 9;
329
 
        len -= 9;
330
 
        mods |= EGG_VIRTUAL_CONTROL_MASK;
331
 
      }
332
 
          else
333
 
            {
334
 
              gchar last_ch;
335
 
 
336
 
              last_ch = *accelerator;
337
 
              while (last_ch && last_ch != '>')
338
 
                {
339
 
                  last_ch = *accelerator;
340
 
                  accelerator += 1;
341
 
                  len -= 1;
342
 
                }
343
 
            }
344
 
        }
345
 
      else
346
 
        {
347
 
          keyval = gdk_keyval_from_name (accelerator);
348
 
 
349
 
          if (keyval == 0)
350
 
            {
351
 
              /* If keyval is 0, than maybe it's a keycode.  Check for 0x## */
352
 
              if (len >= 4 && is_keycode (accelerator))
353
 
                {
354
 
                  char keystring[5];
355
 
                  gchar *endptr;
356
 
                  gint tmp_keycode;
357
 
 
358
 
                  memcpy (keystring, accelerator, 4);
359
 
                  keystring [4] = '\000';
360
 
 
361
 
                  tmp_keycode = strtol (keystring, &endptr, 16);
362
 
 
363
 
                  if (endptr == NULL || *endptr != '\000')
364
 
                    {
365
 
                      bad_keyval = TRUE;
366
 
                    }
367
 
                  else
368
 
                    {
369
 
                      *keycode = tmp_keycode;
370
 
                      /* 0x00 is an invalid keycode too. */
371
 
                      if (*keycode == 0)
372
 
                        bad_keyval = TRUE;
373
 
                    }
374
 
                }
375
 
            }
376
 
 
377
 
          accelerator += len;
378
 
          len -= len;
379
 
        }
380
 
    }
381
 
 
382
 
  if (accelerator_key)
383
 
    *accelerator_key = gdk_keyval_to_lower (keyval);
384
 
  if (accelerator_mods)
385
 
    *accelerator_mods = mods;
386
 
 
387
 
  return !bad_keyval;
388
 
}
389
 
 
390
 
/**
391
 
 * egg_virtual_accelerator_name:
392
 
 * @accelerator_key:  accelerator keyval
393
 
 * @accelerator_mods: accelerator modifier mask
394
 
 * @returns:          a newly-allocated accelerator name
395
 
 *
396
 
 * Converts an accelerator keyval and modifier mask
397
 
 * into a string parseable by egg_accelerator_parse_virtual().
398
 
 * For example, if you pass in #GDK_q and #EGG_VIRTUAL_CONTROL_MASK,
399
 
 * this function returns "&lt;Control&gt;q".
400
 
 *
401
 
 * The caller of this function must free the returned string.
402
 
 */
403
 
gchar*
404
 
egg_virtual_accelerator_name (guint                  accelerator_key,
405
 
                              guint                  keycode,
406
 
                              EggVirtualModifierType accelerator_mods)
407
 
{
408
 
  static const gchar text_release[] = "<Release>";
409
 
  static const gchar text_shift[] = "<Shift>";
410
 
  static const gchar text_control[] = "<Control>";
411
 
  static const gchar text_mod1[] = "<Alt>";
412
 
  static const gchar text_mod2[] = "<Mod2>";
413
 
  static const gchar text_mod3[] = "<Mod3>";
414
 
  static const gchar text_mod4[] = "<Mod4>";
415
 
  static const gchar text_mod5[] = "<Mod5>";
416
 
  static const gchar text_meta[] = "<Meta>";
417
 
  static const gchar text_super[] = "<Super>";
418
 
  static const gchar text_hyper[] = "<Hyper>";
419
 
  guint l;
420
 
  gchar *keyval_name, *str = NULL;
421
 
  gchar *accelerator;
422
 
 
423
 
  accelerator_mods &= EGG_VIRTUAL_MODIFIER_MASK;
424
 
 
425
 
  if (!accelerator_key)
426
 
    {
427
 
      str = g_strdup_printf ("0x%02x", keycode);
428
 
      keyval_name = str;
429
 
    }
430
 
  else
431
 
    {
432
 
      keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
433
 
      if (!keyval_name)
434
 
        keyval_name = "";
435
 
    }
436
 
 
437
 
  l = 0;
438
 
  if (accelerator_mods & EGG_VIRTUAL_RELEASE_MASK)
439
 
    l += sizeof (text_release) - 1;
440
 
  if (accelerator_mods & EGG_VIRTUAL_SHIFT_MASK)
441
 
    l += sizeof (text_shift) - 1;
442
 
  if (accelerator_mods & EGG_VIRTUAL_CONTROL_MASK)
443
 
    l += sizeof (text_control) - 1;
444
 
  if (accelerator_mods & EGG_VIRTUAL_ALT_MASK)
445
 
    l += sizeof (text_mod1) - 1;
446
 
  if (accelerator_mods & EGG_VIRTUAL_MOD2_MASK)
447
 
    l += sizeof (text_mod2) - 1;
448
 
  if (accelerator_mods & EGG_VIRTUAL_MOD3_MASK)
449
 
    l += sizeof (text_mod3) - 1;
450
 
  if (accelerator_mods & EGG_VIRTUAL_MOD4_MASK)
451
 
    l += sizeof (text_mod4) - 1;
452
 
  if (accelerator_mods & EGG_VIRTUAL_MOD5_MASK)
453
 
    l += sizeof (text_mod5) - 1;
454
 
  if (accelerator_mods & EGG_VIRTUAL_META_MASK)
455
 
    l += sizeof (text_meta) - 1;
456
 
  if (accelerator_mods & EGG_VIRTUAL_HYPER_MASK)
457
 
    l += sizeof (text_hyper) - 1;
458
 
  if (accelerator_mods & EGG_VIRTUAL_SUPER_MASK)
459
 
    l += sizeof (text_super) - 1;
460
 
  l += strlen (keyval_name);
461
 
 
462
 
  accelerator = g_new (gchar, l + 1);
463
 
 
464
 
  l = 0;
465
 
  accelerator[l] = 0;
466
 
  if (accelerator_mods & EGG_VIRTUAL_RELEASE_MASK)
467
 
    {
468
 
      strcpy (accelerator + l, text_release);
469
 
      l += sizeof (text_release) - 1;
470
 
    }
471
 
  if (accelerator_mods & EGG_VIRTUAL_SHIFT_MASK)
472
 
    {
473
 
      strcpy (accelerator + l, text_shift);
474
 
      l += sizeof (text_shift) - 1;
475
 
    }
476
 
  if (accelerator_mods & EGG_VIRTUAL_CONTROL_MASK)
477
 
    {
478
 
      strcpy (accelerator + l, text_control);
479
 
      l += sizeof (text_control) - 1;
480
 
    }
481
 
  if (accelerator_mods & EGG_VIRTUAL_ALT_MASK)
482
 
    {
483
 
      strcpy (accelerator + l, text_mod1);
484
 
      l += sizeof (text_mod1) - 1;
485
 
    }
486
 
  if (accelerator_mods & EGG_VIRTUAL_MOD2_MASK)
487
 
    {
488
 
      strcpy (accelerator + l, text_mod2);
489
 
      l += sizeof (text_mod2) - 1;
490
 
    }
491
 
  if (accelerator_mods & EGG_VIRTUAL_MOD3_MASK)
492
 
    {
493
 
      strcpy (accelerator + l, text_mod3);
494
 
      l += sizeof (text_mod3) - 1;
495
 
    }
496
 
  if (accelerator_mods & EGG_VIRTUAL_MOD4_MASK)
497
 
    {
498
 
      strcpy (accelerator + l, text_mod4);
499
 
      l += sizeof (text_mod4) - 1;
500
 
    }
501
 
  if (accelerator_mods & EGG_VIRTUAL_MOD5_MASK)
502
 
    {
503
 
      strcpy (accelerator + l, text_mod5);
504
 
      l += sizeof (text_mod5) - 1;
505
 
    }
506
 
  if (accelerator_mods & EGG_VIRTUAL_META_MASK)
507
 
    {
508
 
      strcpy (accelerator + l, text_meta);
509
 
      l += sizeof (text_meta) - 1;
510
 
    }
511
 
  if (accelerator_mods & EGG_VIRTUAL_HYPER_MASK)
512
 
    {
513
 
      strcpy (accelerator + l, text_hyper);
514
 
      l += sizeof (text_hyper) - 1;
515
 
    }
516
 
  if (accelerator_mods & EGG_VIRTUAL_SUPER_MASK)
517
 
    {
518
 
      strcpy (accelerator + l, text_super);
519
 
      l += sizeof (text_super) - 1;
520
 
    }
521
 
 
522
 
  strcpy (accelerator + l, keyval_name);
523
 
  g_free (str);
524
 
 
525
 
  return accelerator;
526
 
}
527
 
 
528
 
/**
529
 
 * egg_virtual_accelerator_label:
530
 
 * @accelerator_key:  accelerator keyval
531
 
 * @accelerator_mods: accelerator modifier mask
532
 
 * @returns:          a newly-allocated accelerator label
533
 
 *
534
 
 * Converts an accelerator keyval and modifier mask
535
 
 * into a (possibly translated) string that can be displayed to
536
 
 * a user.
537
 
 * For example, if you pass in #GDK_q and #EGG_VIRTUAL_CONTROL_MASK,
538
 
 * and you use a German locale, this function returns "Strg+Q".
539
 
 *
540
 
 * The caller of this function must free the returned string.
541
 
 */
542
 
gchar*
543
 
egg_virtual_accelerator_label (guint                  accelerator_key,
544
 
                               guint                  keycode,
545
 
                               EggVirtualModifierType accelerator_mods)
546
 
{
547
 
  gchar *gtk_label;
548
 
  GdkModifierType gdkmods;
549
 
 
550
 
  egg_keymap_resolve_virtual_modifiers (gdk_keymap_get_default (),
551
 
                                        accelerator_mods, &gdkmods);
552
 
  gtk_label = gtk_accelerator_get_label (accelerator_key, gdkmods);
553
 
 
554
 
  if (!accelerator_key)
555
 
    {
556
 
        gchar *label;
557
 
        label = g_strdup_printf ("%s0x%02x", gtk_label, keycode);
558
 
        g_free (gtk_label);
559
 
        return label;
560
 
    }
561
 
 
562
 
  return gtk_label;
563
 
}
564
 
 
565
 
void
566
 
egg_keymap_resolve_virtual_modifiers (GdkKeymap              *keymap,
567
 
                                      EggVirtualModifierType  virtual_mods,
568
 
                                      GdkModifierType        *concrete_mods)
569
 
{
570
 
  GdkModifierType concrete;
571
 
  int i;
572
 
  const EggModmap *modmap;
573
 
 
574
 
  g_return_if_fail (GDK_IS_KEYMAP (keymap));
575
 
  g_return_if_fail (concrete_mods != NULL);
576
 
 
577
 
  modmap = egg_keymap_get_modmap (keymap);
578
 
 
579
 
  /* Not so sure about this algorithm. */
580
 
 
581
 
  concrete = 0;
582
 
  i = 0;
583
 
  while (i < EGG_MODMAP_ENTRY_LAST)
584
 
    {
585
 
      if (modmap->mapping[i] & virtual_mods)
586
 
        concrete |= (1 << i);
587
 
 
588
 
      ++i;
589
 
    }
590
 
 
591
 
  *concrete_mods = concrete;
592
 
}
593
 
 
594
 
void
595
 
egg_keymap_virtualize_modifiers (GdkKeymap              *keymap,
596
 
                                 GdkModifierType         concrete_mods,
597
 
                                 EggVirtualModifierType *virtual_mods)
598
 
{
599
 
  GdkModifierType virtual;
600
 
  int i;
601
 
  const EggModmap *modmap;
602
 
 
603
 
  g_return_if_fail (GDK_IS_KEYMAP (keymap));
604
 
  g_return_if_fail (virtual_mods != NULL);
605
 
 
606
 
  modmap = egg_keymap_get_modmap (keymap);
607
 
 
608
 
  /* Not so sure about this algorithm. */
609
 
 
610
 
  virtual = 0;
611
 
  i = 0;
612
 
  while (i < EGG_MODMAP_ENTRY_LAST)
613
 
    {
614
 
      if ((1 << i) & concrete_mods)
615
 
        {
616
 
          EggVirtualModifierType cleaned;
617
 
 
618
 
          cleaned = modmap->mapping[i] & ~(EGG_VIRTUAL_MOD2_MASK |
619
 
                                           EGG_VIRTUAL_MOD3_MASK |
620
 
                                           EGG_VIRTUAL_MOD4_MASK |
621
 
                                           EGG_VIRTUAL_MOD5_MASK);
622
 
 
623
 
          if (cleaned != 0)
624
 
            {
625
 
              virtual |= cleaned;
626
 
            }
627
 
          else
628
 
            {
629
 
              /* Rather than dropping mod2->mod5 if not bound,
630
 
               * go ahead and use the concrete names
631
 
               */
632
 
              virtual |= modmap->mapping[i];
633
 
            }
634
 
        }
635
 
 
636
 
      ++i;
637
 
    }
638
 
 
639
 
  *virtual_mods = virtual;
640
 
}
641
 
 
642
 
static void
643
 
reload_modmap (GdkKeymap *keymap,
644
 
               EggModmap *modmap)
645
 
{
646
 
  XModifierKeymap *xmodmap;
647
 
  int map_size;
648
 
  int i;
649
 
 
650
 
  /* FIXME multihead */
651
 
  xmodmap = XGetModifierMapping (gdk_x11_get_default_xdisplay ());
652
 
 
653
 
  memset (modmap->mapping, 0, sizeof (modmap->mapping));
654
 
 
655
 
  /* there are 8 modifiers, and the first 3 are shift, shift lock,
656
 
   * and control
657
 
   */
658
 
  map_size = 8 * xmodmap->max_keypermod;
659
 
  i = 3 * xmodmap->max_keypermod;
660
 
  while (i < map_size)
661
 
    {
662
 
      /* get the key code at this point in the map,
663
 
       * see if its keysym is one we're interested in
664
 
       */
665
 
      int keycode = xmodmap->modifiermap[i];
666
 
      GdkKeymapKey *keys;
667
 
      guint *keyvals;
668
 
      int n_entries;
669
 
      int j;
670
 
      EggVirtualModifierType mask;
671
 
 
672
 
      keys = NULL;
673
 
      keyvals = NULL;
674
 
      n_entries = 0;
675
 
 
676
 
      gdk_keymap_get_entries_for_keycode (keymap,
677
 
                                          keycode,
678
 
                                          &keys, &keyvals, &n_entries);
679
 
 
680
 
      mask = 0;
681
 
      j = 0;
682
 
      while (j < n_entries)
683
 
        {
684
 
          if (keyvals[j] == GDK_Num_Lock)
685
 
            mask |= EGG_VIRTUAL_NUM_LOCK_MASK;
686
 
          else if (keyvals[j] == GDK_Scroll_Lock)
687
 
            mask |= EGG_VIRTUAL_SCROLL_LOCK_MASK;
688
 
          else if (keyvals[j] == GDK_Meta_L ||
689
 
                   keyvals[j] == GDK_Meta_R)
690
 
            mask |= EGG_VIRTUAL_META_MASK;
691
 
          else if (keyvals[j] == GDK_Hyper_L ||
692
 
                   keyvals[j] == GDK_Hyper_R)
693
 
            mask |= EGG_VIRTUAL_HYPER_MASK;
694
 
          else if (keyvals[j] == GDK_Super_L ||
695
 
                   keyvals[j] == GDK_Super_R)
696
 
            mask |= EGG_VIRTUAL_SUPER_MASK;
697
 
          else if (keyvals[j] == GDK_Mode_switch)
698
 
            mask |= EGG_VIRTUAL_MODE_SWITCH_MASK;
699
 
 
700
 
          ++j;
701
 
        }
702
 
 
703
 
      /* Mod1Mask is 1 << 3 for example, i.e. the
704
 
       * fourth modifier, i / keyspermod is the modifier
705
 
       * index
706
 
       */
707
 
      modmap->mapping[i/xmodmap->max_keypermod] |= mask;
708
 
 
709
 
      g_free (keyvals);
710
 
      g_free (keys);
711
 
 
712
 
      ++i;
713
 
    }
714
 
 
715
 
  /* Add in the not-really-virtual fixed entries */
716
 
  modmap->mapping[EGG_MODMAP_ENTRY_SHIFT] |= EGG_VIRTUAL_SHIFT_MASK;
717
 
  modmap->mapping[EGG_MODMAP_ENTRY_CONTROL] |= EGG_VIRTUAL_CONTROL_MASK;
718
 
  modmap->mapping[EGG_MODMAP_ENTRY_LOCK] |= EGG_VIRTUAL_LOCK_MASK;
719
 
  modmap->mapping[EGG_MODMAP_ENTRY_MOD1] |= EGG_VIRTUAL_ALT_MASK;
720
 
  modmap->mapping[EGG_MODMAP_ENTRY_MOD2] |= EGG_VIRTUAL_MOD2_MASK;
721
 
  modmap->mapping[EGG_MODMAP_ENTRY_MOD3] |= EGG_VIRTUAL_MOD3_MASK;
722
 
  modmap->mapping[EGG_MODMAP_ENTRY_MOD4] |= EGG_VIRTUAL_MOD4_MASK;
723
 
  modmap->mapping[EGG_MODMAP_ENTRY_MOD5] |= EGG_VIRTUAL_MOD5_MASK;
724
 
 
725
 
  XFreeModifiermap (xmodmap);
726
 
}
727
 
 
728
 
const EggModmap*
729
 
egg_keymap_get_modmap (GdkKeymap *keymap)
730
 
{
731
 
  EggModmap *modmap;
732
 
 
733
 
  /* This is all a hack, much simpler when we can just
734
 
   * modify GDK directly.
735
 
   */
736
 
 
737
 
  modmap = g_object_get_data (G_OBJECT (keymap),
738
 
                              "egg-modmap");
739
 
 
740
 
  if (modmap == NULL)
741
 
    {
742
 
      modmap = g_new0 (EggModmap, 1);
743
 
 
744
 
      /* FIXME modify keymap change events with an event filter
745
 
       * and force a reload if we get one
746
 
       */
747
 
 
748
 
      reload_modmap (keymap, modmap);
749
 
 
750
 
      g_object_set_data_full (G_OBJECT (keymap),
751
 
                              "egg-modmap",
752
 
                              modmap,
753
 
                              g_free);
754
 
    }
755
 
 
756
 
  g_assert (modmap != NULL);
757
 
 
758
 
  return modmap;
759
 
}