~ubuntu-branches/ubuntu/maverick/cairo-dock/maverick

« back to all changes in this revision

Viewing changes to src/eggaccelerators.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthieu Baerts (matttbe)
  • Date: 2010-08-09 23:26:12 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20100809232612-yp4c6ig3jt1bzpdv
Tags: 2.2.0~0beta4-0ubuntu1
* New Upstream Version (LP: #614624)
* Fixed a few bugs on LP:
 - LP: #518453: Dock appears under all windows
                 (Compiz - fullscreen window)
 - LP: #521369: Separator are not removed when closing
                 grouped windows
 - LP: #521762: Some sentences are not correct
 - LP: #526466: Icons of apps with same class shouldn't
                 be stacked by default
 - LP: #535083: Dialogues looks ugly when a lot of them
                 appears at the same time
 - More details on the 'ChangeLog' file
* debian/rules:
 - Autotools has been replaced by CMake
 - Man pages are now included in the source code
* debian/copyright:
 - Updated with the new pathes and new files
* debian/control:
 - Autotools has been replaced by CMake
 - Added libcurl4-gnutls-dev as Build-deps
 - Bump Standard-Version to 3.9.1
* debian/cairo-dock-core.install:
 - Man pages are now included in the source code
 - All sonames are now installed into lib32 or lib64
* debian/cairo-dock-dev.install:
 - pkgconfig is now installed into lib32 or lib64

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