summaryrefslogtreecommitdiff
path: root/keys.c
blob: 9b793892483a1e33456f56b10701c43e6741799a (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
465
/***************************************************************************
        xbindkeys : a program to bind keys to commands under X11.
                           -------------------
    begin                : Sat Oct 13 14:11:34 CEST 2001
    copyright            : (C) 2001 by Philippe Brochard
    email                : hocwp@free.fr
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "keys.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <X11/keysym.h>
#include "xbindkeys.h"
#include "keys.h"
#include "config.h"
#include "options.h"
#include "grab_key.h"


#define STR_KEY_LEN 1000

int nb_keys;
Keys_t *keys;

extern char rc_file[512];

static char *modifier_string[] = { "Control", "Shift", "Alt", "Mod2",
  "Mod3", "Mod4", "Mod5"
};


int
init_keys (void)
{
  nb_keys = 0;
  keys = NULL;

  return 0;
}

void
close_keys (void)
{
  int i;

  for (i = 0; i < nb_keys; i++)
    free_key (&keys[i]);

  if (keys != NULL)
    {
      free (keys);
      keys = NULL;
    }

  nb_keys = 0;
}



int
add_key (KeyType_t type, EventType_t event_type, KeySym keysym, KeyCode keycode,
	 unsigned int button, unsigned int modifier, char *command, SCM function)
{
  Keys_t *keys_bis = NULL;
  int i;

  if (keysym == 0 && keycode == 0 && button == 0)
    {
      fprintf (stderr, "Warning: unknown key in RC file : %s\n", rc_file);
      return (-1);
    }

  /* make new array keys_bis */
  keys_bis = (Keys_t *) malloc ((nb_keys + 1) * sizeof (Keys_t));
  if (keys_bis == NULL)
    return (-1);

  if (keys != NULL)
    {
      /* copy keys in keys_bis */
      for (i = 0; i < nb_keys; i++)
	{
	  keys_bis[i] = keys[i];
	}

      /* delete old keys array */
      free (keys);
    }

  /* make keys_bis as keys */
  keys = keys_bis;

  modifier &= ~(numlock_mask | capslock_mask | scrolllock_mask);

#ifdef GUILE_FLAG
  if(function) scm_gc_protect_object(function);
#endif
  /* set key */
  if (type == SYM)
    {
      set_keysym (&keys[nb_keys], event_type, keysym, modifier, command, function);
    }
  else if (type == BUTTON)
    {
      set_button (&keys[nb_keys], event_type, button, modifier, command, function);
    }
  else
    {
      set_keycode (&keys[nb_keys], event_type, keycode, modifier, command, function);
    }

  /* new key */
  nb_keys += 1;

  return (0);
}






void
show_key_binding (Display * d)
{
  int i;
  int last_verbose = verbose;

  verbose = 1;

  for (i = 0; i < nb_keys; i++)
    {
      print_key (d, &keys[i]);
    }

  verbose = last_verbose;
}



void
modifier_to_string (unsigned int modifier, char *str)
{
  str[0] = '\0';

  if (modifier & ControlMask)
    {
      if (str[0])
	strncat (str, "+", STR_KEY_LEN);
      strncat (str, modifier_string[0], STR_KEY_LEN);
    }

  if (modifier & ShiftMask)
    {
      if (str[0])
	strncat (str, "+", STR_KEY_LEN);
      strncat (str, modifier_string[1], STR_KEY_LEN);
    }


  if (modifier & Mod1Mask)
    {
      if (str[0])
	strncat (str, "+", STR_KEY_LEN);
      strncat (str, modifier_string[2], STR_KEY_LEN);
    }

  if (modifier & Mod2Mask)
    {
      if (str[0])
	strncat (str, "+", STR_KEY_LEN);
      strncat (str, modifier_string[3], STR_KEY_LEN);
    }

  if (modifier & Mod3Mask)
    {
      if (str[0])
	strncat (str, "+", STR_KEY_LEN);
      strncat (str, modifier_string[4], STR_KEY_LEN);
    }

  if (modifier & Mod4Mask)
    {
      if (str[0])
	strncat (str, "+", STR_KEY_LEN);
      strncat (str, modifier_string[5], STR_KEY_LEN);
    }

  if (modifier & Mod5Mask)
    {
      if (str[0])
	strncat (str, "+", STR_KEY_LEN);
      strncat (str, modifier_string[6], STR_KEY_LEN);
    }
}


void
print_key (Display * d, Keys_t * key)
{
  char str[STR_KEY_LEN];
  int keysyms_per_keycode_return;

  if (verbose)
    {
      if (key->type == SYM)
	{
	  modifier_to_string (key->modifier, str);

	  printf ("\"%s\"\n    %s%s%s%s\n",
		  key->command != NULL ? key->command : ( key->function != 0 ? "(Scheme function)" : "NoCommand" ),
		  key->event_type == PRESS ? "" : "Release + ",
		  str, str[0] ? " + " : "", XKeysymToString (key->key.sym));
	}
      else if (key->type == BUTTON)
	{
	  printf ("\"%s\"\n    %sm:0x%x + b:%d   (mouse)\n",
		  key->command != NULL ? key->command : ( key->function != 0 ? "(Scheme function)" : "NoCommand" ),
		  key->event_type == PRESS ? "" : "Release + ",
		  key->modifier, key->key.button);
	}
      else
	{
	  printf ("\"%s\"\n    %sm:0x%x + c:%d\n",
		  key->command != NULL ? key->command : ( key->function != 0 ? "(Scheme function)" : "NoCommand" ),
		  key->event_type == PRESS ? "" : "Release + ",
		  key->modifier, key->key.code);
	  if (d != NULL)
	    {
	      modifier_to_string (key->modifier, str);
	      printf ("    %s%s%s%s\n",
		      str,
		      str[0] ? " + " : "",
		      key->event_type == PRESS ? "" : "Release + ",
		      (XKeysymToString (*XGetKeyboardMapping(d, key->key.code, 1, &keysyms_per_keycode_return)) != NULL) ?
                      XKeysymToString (*XGetKeyboardMapping(d, key->key.code, 1, &keysyms_per_keycode_return)) : "NoSymbol");
	    }
	}
    }
}


void
set_keysym (Keys_t * key, EventType_t event_type, KeySym keysym,
	    unsigned int modifier, char *command, SCM function)
{
	int size;

  key->type = SYM;
  key->event_type = event_type;
  key->key.sym = keysym;
  key->modifier = modifier;

  if (command != NULL)
	{
      size = (strlen (command) + 1) * sizeof (char);
      key->command = (char *) malloc (size);
      if (key->command != NULL)
        strncpy (key->command, command, size);
    }
  else
    {
      key->command = NULL;
    }

  key->function = function;
}

void
set_keycode (Keys_t * key, EventType_t event_type, KeyCode keycode,
	     unsigned int modifier, char *command, SCM function)
{
  int size;

  key->type = CODE;
  key->event_type = event_type;
  key->key.code = keycode;
  key->modifier = modifier;

  if (command != NULL)
	  {
      size = (strlen (command) + 1) * sizeof (char);
      key->command = (char *) malloc (size);
      if (key->command != NULL)
        strncpy (key->command, command, size);
    }
  else
    {
      key->command = NULL;
    }

  key->function = function;
}


void
set_button (Keys_t * key, EventType_t event_type, unsigned int button,
	    unsigned int modifier, char *command, SCM function)
{
  int size;

  key->type = BUTTON;
  key->event_type = event_type;
  key->key.button = button;
  key->modifier = modifier;

  if (command != NULL)
    {
      size = (strlen (command) + 1) * sizeof (char);
      key->command = (char *) malloc (size);
      if (key->command != NULL)
        strncpy (key->command, command, size);
    }
  else
    {
      key->command = NULL;
    }

  key->function = function;
}




void
free_key (Keys_t * key)
{
  key->type = SYM;
  key->event_type = PRESS;
  key->key.sym = 0;
  key->modifier = 0;

  if (key->command != NULL)
    free (key->command);

#ifdef GUILE_FLAG
  if(key->function) scm_gc_unprotect_object(key->function);
#endif
  key->function = 0;
}




int
remove_key (KeyType_t type, EventType_t event_type, KeySym keysym, KeyCode keycode,
	    unsigned int button, unsigned int modifier)
{
  int i, found_index = -1;
  Keys_t *keys_bis = NULL;

  if (keys == NULL)
    {
      return (-1);
    }

  modifier &= ~(numlock_mask | capslock_mask | scrolllock_mask);

  for (i = 0; i < nb_keys; i++)
    {
      if (keys[i].type == type &&
	  keys[i].event_type == event_type &&
	  keys[i].modifier == modifier &&
	  ((type == SYM && keys[i].key.sym == keysym) ||
	   (type == CODE && keys[i].key.code == keycode) ||
	   (type == BUTTON && keys[i].key.button == button)))
	found_index = i;
    }

  if (found_index != -1)
    {
      if (verbose)
	printf ("Removing key index %d\n", found_index);

      /* make new array keys_bis */
      keys_bis = (Keys_t *) malloc ((nb_keys - 1) * sizeof (Keys_t));
      if (keys_bis == NULL)
	return (-1);

      for (i = 0; i < found_index; i++)
	keys_bis[i] = keys[i];

      for (i = found_index + 1; i < nb_keys; i++)
	keys_bis[i - 1] = keys[i];

      free_key (&keys[found_index]);
      free (keys);

      /* make keys_bis as keys */
      keys = keys_bis;

      nb_keys -= 1;
    }

  return (0);
}







void
run_command (char *command)
{
#ifdef FORK_FLAG
  pid_t pid;

  if (verbose)
    printf ("Start program with fork+exec call\n");

  //  if (fork() == 0)
  //  execlp ("sh", "sh", "-c", key->command, NULL);
  if (!(pid = fork()))
    {
      setsid();
      switch (fork())
	{
	case 0: execlp ("sh", "sh", "-c", command, (char *) NULL);
	  break;
	default: _exit(0);
	  break;
	}
    }
  if (pid > 0)
    wait(NULL);
#else
  if (verbose)
    printf ("Start program with system call\n");

  system (command);
#endif
}


void
start_command_key (Keys_t * key)
{
  if (key->command == NULL)
    {
#ifdef GUILE_FLAG
      if (key->function != 0)
	{
	  scm_call_0 (key->function);
	}
#endif
      return;
    }

  run_command (key->command);
}