summaryrefslogtreecommitdiff
path: root/console-client/xkb/xkbdata.c
blob: a6e2522fff16fadd1a284f909ecf38610e95d1d3 (plain)
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
/*  xkbdata.c -- Manage XKB datastructures.

    Copyright (C) 2003  Marco Gerards
   
    Written by Marco Gerards <marco@student.han.nl>
    
    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 2 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.  */

/* Generate a key for the string S.  XXX: There are many more efficient
   algorithms, this one should be replaced by one of those.  */

#include <stdlib.h>
#include <string.h>
#include <hurd/ihash.h>
#include "xkb.h"

static int
name_hash (char *s)
{
  int i = 0;
  while (*s)
      i += *(s++);
  return i;
}


/* A keyname with a keycode and realmodifier bound to it.  */
struct keyname
{
  int keycode;
  int rmods;
};

static struct hurd_ihash kn_mapping;

/* Initialize the keyname hashtable.  */
static void
keyname_init ()
{
  hurd_ihash_init (&kn_mapping, HURD_IHASH_NO_LOCP);
  debug_printf ("Kn_mapping init");
  /* XXX: error.  */
}

static inline int
keyname_hash(char *keyname)
{
  char tmp[4] = { 0 };
  strncpy(tmp, keyname, sizeof tmp);
  return tmp[0] + (tmp[1] << 8) + (tmp[2] << 16) + (tmp[3] << 24);
}

/* Assign the name KEYNAME to the keycode KEYCODE.  */
error_t
keyname_add (char *keyname, int keycode)
{
  struct keyname *kn;
  int kn_int;

  kn = malloc (sizeof (struct keyname));
  if (!kn)
    return ENOMEM;

  /* XXX: 4 characters can be mapped into a int, it is safe to assume
     this will not be changed.  */
  if (strlen (keyname) > 4)
    {
      debug_printf ("The keyname `%s' consist of more than 4 characters;"
		    " 4 characters is the maximum.\n", keyname);
      /* XXX: Abort?  */
      return 0;
    }
  
  kn->keycode = keycode;
  kn->rmods = 0;

  kn_int = keyname_hash(keyname);
  debug_printf ("add key %s(%d) hash: %d\n", keyname, keycode, kn_int);
  hurd_ihash_add (&kn_mapping, kn_int, kn);

  return 0;
}

/* Find the numberic representation of the keycode with the name
   KEYNAME.  */
int
keyname_find (char *keyname)
{
  struct keyname *kn;
  int kn_int;

  /* XXX: 4 characters can be mapped into a int, it is safe to assume
     this will not be changed.  */
  if (strlen (keyname) > 4)
    {
      debug_printf ("The keyname `%s' consist of more than 4 characters;"
		    " 4 characters is the maximum.\n", keyname);
      /* XXX: Abort?  */
      return 0;
    } 
  kn_int = keyname_hash(keyname);

  kn = hurd_ihash_find (&kn_mapping, kn_int);
  if (kn)
    return kn->keycode;
/*   int h = name_hash (keyname); */
/*   struct keyname *kn; */
/*   for (kn = knhash[KNHASH(h)]; kn; kn = kn->hnext) */
/*     { */
/*       if (strcmp (kn->keyname, keyname)) */
/* 	continue; */
      
/*       return kn->keycode; */
/*     } */

  /* XXX: Is 0 an invalid keycode?  */
  return 0;
}


/* Keytypes and keytype maps.  */

/* The dummy gets used when the original may not be overwritten.  */
static struct keytype dummy_keytype;

#define	KTHSZ	16
#if	((KTHSZ&(KTHSZ-1)) == 0)
#define	KTHASH(ktttl)	((ktttl)&(KTHSZ-1))
#else
#define	KTHASH(ktttl)	(((unsigned)(kt))%KTHSZ)
#endif

/* All keytypes.  */
struct keytype *kthash[KTHSZ];

/* Initialize the keytypes hashtable.  */
static void
keytype_init ()
{
  int n;
  for (n = 0; n < KTHSZ; n++)
    kthash[n] = 0;
}

/* Search the keytype with the name NAME.  */
struct keytype *
keytype_find (char *name)
{
  int nhash = name_hash (name);
  struct keytype *kt;

  for (kt = kthash[KTHASH(nhash)]; kt; kt = kt->hnext)
    if (!strcmp (name, kt->name))
      return kt;
  return NULL;
}

/* Remove the keytype KT.  */
void
keytype_delete (struct keytype *kt)
{
  struct typemap *map;


  *kt->prevp = kt->hnext;
  if (kt->hnext)
    kt->hnext->prevp = kt->prevp;
      
  map = kt->maps;
  while (map)
    {
      struct typemap *nextmap = map->next;
      free (map);
      map = nextmap;
    }
  
}

/* Create a new keytype with the name NAME.  */
error_t
keytype_new (char *name, struct keytype **new_kt)
{
  struct keytype *kt;
  struct keytype *ktlist;
  int nhash;
  
  nhash = name_hash (name);
  debug_printf ("New: %s\n", name);

  kt = keytype_find (name);

  if (kt)
    {
      /* If the merge mode is augement don't replace it.  */
      if (merge_mode == augment)
	{
	  *new_kt = &dummy_keytype;
	  return 0;
	}
      else /* This keytype should replace the old one, remove the old one.  */
	keytype_delete (kt);
    }

  ktlist = kthash[KTHASH(nhash)];
  kt = calloc (1, sizeof (struct keytype));
  if (kt == NULL)
    return ENOMEM;

  kt->hnext = ktlist;
  kt->name = strdup (name);
  kt->prevp = &kthash[KTHASH(nhash)];
  kt->maps = NULL;
  if (kthash[KTHASH(nhash)])
    kthash[KTHASH(nhash)]->prevp = &(kt->hnext);
  kthash[KTHASH(nhash)] = kt;

  *new_kt = kt;
  return 0;
}

/* Add a level (LEVEL) to modifiers (MODS) mapping to the current
   keytype.  */
error_t
keytype_mapadd (struct keytype *kt, modmap_t mods, int level)
{
  struct typemap *map;
  modmap_t nulmap = {0, 0};

  map = malloc (sizeof (struct typemap));
  if (!map)
    return ENOMEM;

  map->level = level;
  map->mods = mods;
  map->preserve = nulmap;
  /* By default modifiers shouldn't be preserved.  */
  map->next = kt->maps;
  
  kt->maps = map;

  return 0;
}

/* For the current keytype the modifiers PRESERVE should be preserved
   when the modifiers MODS are pressed.  */
error_t
keytype_preserve_add (struct keytype *kt, modmap_t mods, modmap_t preserve)
{
  error_t err;
  struct typemap *map;

  map = kt->maps;
  while (map)
    {
      if (mods.rmods == map->mods.rmods && mods.vmods == map->mods.vmods)
	{
	  map->preserve = preserve;
	  return 0;
	}
      map = map->next;
    }

  /* No map has been found, add the default map.  */
  err = keytype_mapadd (kt, mods, 0);
  if (err)
    return err;

  keytype_preserve_add (kt, mods, preserve);

  return 0;
}


/* Interpretations.  */

struct xkb_interpret *last_interp;
struct xkb_interpret default_interpretation;


/* Add a new interpretation.  */
error_t
interpret_new (xkb_interpret_t **new_interpret, symbol ks)
{
  struct xkb_interpret *new_interp;

  new_interp = malloc (sizeof (struct xkb_interpret));
  if (!new_interp)
    return ENOMEM;

  memcpy (new_interp, &default_interpretation, sizeof (struct xkb_interpret));
  new_interp->symbol = ks;

  if (ks)
    {
      new_interp->next = interpretations;
      interpretations = new_interp;

      if (!last_interp)
	last_interp = new_interp;
    }
  else
    {
      if (last_interp)
	last_interp->next = new_interp;
      
      last_interp = new_interp;
      
      if (!interpretations)
	interpretations = new_interp; 
    }

  *new_interpret = new_interp;

  return 0;
}


/* XXX: Dead code!?  */
/* Virtual modifiers name to number mapping.  */
/* Last number assigned to a virtual modifier.  */
static int lastvmod = 0;

/* One virtual modifiername -> vmod number mapping.  */
struct vmodname
{
  char *name;
  struct vmodname *next;
};

/* A list of virtualmodifier names and its numberic representation.  */
static struct vmodname *vmodnamel;

/* Get the number assigned to the virtualmodifier with the name
   VMODNAME.  */
int
vmod_find (char *vmodname)
{
  int i = 0;
  struct vmodname *vmn = vmodnamel;

  while (vmn)
    {
      if (!strcmp (vmn->name, vmodname))
	return (lastvmod - i);
      vmn = vmn->next;
      i++;
    }

  return 0;
}

/* Give the virtualmodifier VMODNAME a number and add it to the
   hashtable.  */
error_t
vmod_add (char *vmodname)
{
  struct vmodname *vmn;

  if (vmod_find (vmodname))
    return 0;

  vmn = malloc (sizeof (struct vmodname));
  if (vmn == NULL)
    return ENOMEM;

  vmn->name = vmodname;
  vmn->next = vmodnamel;
  vmodnamel = vmn;

  lastvmod++;
  if (lastvmod > 16)
	  debug_printf("warning: only sixteen virtual modifiers are supported, %s will not be functional.\n", vmodname);

  return 0;
}


/* XXX: Use this, no pointers.  */
struct ksrm
{
  symbol ks;

  int rmods;
};
static struct hurd_ihash ksrm_mapping;

/* Initialize the list for keysyms to realmodifiers mappings.  */
void
ksrm_init ()
{
  hurd_ihash_init (&ksrm_mapping, HURD_IHASH_NO_LOCP);
  debug_printf ("KSRM MAP IHASH CREATED \n");
}

/* Add keysym to realmodifier mapping.  */
error_t
ksrm_add (symbol ks, int rmod)
{
  hurd_ihash_add (&ksrm_mapping, ks, (void *) rmod);

  return 0;
}

/* Apply the rkms (realmods to keysyms) table to all keysyms.  */
void
ksrm_apply (void)
{
  keycode_t kc;
  for (kc = 0; kc < max_keys; kc++)
    {
      int group;
      for (group = 0; group < 4; group++)
	{
	  int cursym;
	  for (cursym = 0; cursym < keys[kc].groups[group].width; cursym++)
	    {
	      symbol ks = keys[kc].groups[group].symbols[cursym];
	      int rmods = (int) hurd_ihash_find (&ksrm_mapping, ks);

		if (rmods)
		  {
		    keys[kc].mods.rmods = rmods;
		  }
	    }
	}
    }
}


/* void */
/* indicator_new (xkb_indicator_t **,  */


/* Keycode to realmodifier mapping.  */

/* Set the current rmod for the key with keyname KEYNAME.  */
/* XXX: It shouldn't be applied immediately because the key can be
   replaced.  */
void
set_rmod_keycode (char *keyname, int rmod)
{
  keycode_t kc = keyname_find (keyname);
  keys[kc].mods.rmods = rmod;
  debug_printf ("%s (kc %d) rmod: %d\n", keyname, kc, rmod);
}

/* Initialize XKB data structures.  */
error_t
xkb_data_init (void)
{
  keyname_init ();
  keytype_init ();
  ksrm_init ();

  return 0;
}