File: hotkey.c

package info (click to toggle)
amiwm 0.22pl2-5
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 1,048 kB
  • sloc: ansic: 10,195; perl: 443; makefile: 258; yacc: 245; lex: 215; sh: 211
file content (152 lines) | stat: -rw-r--r-- 3,748 bytes parent folder | download | duplicates (4)
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
#include "libami.h"
#include <X11/keysym.h>
#include <stdlib.h>
#include <string.h>

static struct hotkey {
  struct hotkey *next;
  KeySym keysym;
  unsigned int mod0;
  int keycode;
  unsigned int modifiers;
  int meta, position;
  int grab;
  void (*callback)(XEvent *, void *);
  void *client_data;
} *hotkeys = NULL;

static XModifierKeymap *modmap=NULL;
static KeySym *keymap=NULL;
static int kbd_mincode, kbd_maxcode, kbd_maxsym;
static unsigned int meta_mask, switch_mask;
static Display *dpy=NULL;

static void lookup_meta()
{
  int i, j, k;
  KeySym *kp;
  unsigned int alt_mask = 0;

  meta_mask = switch_mask = 0;
  for(i=3; i<8; i++)
    for(j=0; j<modmap->max_keypermod; j++)
      for(kp=keymap+(modmap->modifiermap[i*modmap->max_keypermod+j]-
		     kbd_mincode)*kbd_maxsym, k=0; k<kbd_maxsym; k++)
	switch(*kp++) {
	case XK_Meta_L:
	case XK_Meta_R:
	  meta_mask|=1<<i;
	  break;
	case XK_Mode_switch:
	  switch_mask|=1<<i;
	  break;
	case XK_Alt_L:
	case XK_Alt_R:
	  alt_mask|=1<<i;
	  break;
	}
  if(meta_mask == 0)
    meta_mask = (alt_mask? alt_mask :
		 (switch_mask? switch_mask : Mod1Mask));
}

static void lookup_hotkey(struct hotkey *hk)
{
  if(!keymap) {
    XDisplayKeycodes(dpy, &kbd_mincode, &kbd_maxcode);
    keymap=XGetKeyboardMapping(dpy, kbd_mincode, kbd_maxcode-kbd_mincode+1,
			       &kbd_maxsym);
  }
  if(!modmap) {
    modmap=XGetModifierMapping(dpy);
    if(keymap)
      lookup_meta();
  }
  hk->modifiers=hk->mod0|(hk->meta? meta_mask:0);
  if(!(hk->keycode=XKeysymToKeycode(dpy, hk->keysym)))
    if((hk->keysym&~0x20)>=XK_A && (hk->keysym&~0x20)<=XK_Z)
      hk->keycode=XKeysymToKeycode(dpy, hk->keysym^0x20);

  if(hk->keycode && keymap)
    if(keymap[(hk->keycode-kbd_mincode)*kbd_maxsym])
      ;
    else if(kbd_maxsym>1 && keymap[(hk->keycode-kbd_mincode)*kbd_maxsym+1])
      hk->modifiers|=ShiftMask;
    else if(kbd_maxsym>2 && keymap[(hk->keycode-kbd_mincode)*kbd_maxsym+2])
      hk->modifiers|=switch_mask;
    else if(kbd_maxsym>3 && keymap[(hk->keycode-kbd_mincode)*kbd_maxsym+3])
      hk->modifiers|=ShiftMask|switch_mask;
}

static void rethink_keymap()
{
  struct hotkey *hk;

  if(keymap) {
    XFree(keymap);
    keymap=NULL;
  }
  if(modmap) {
    XFreeModifiermap(modmap);
    modmap=NULL;
  }
  for(hk=hotkeys; hk; hk=hk->next) {
    int oldkc=hk->keycode;
    unsigned oldmd=hk->modifiers;
    lookup_hotkey(hk);
    if(hk->grab>=0 && (hk->keycode!=oldkc || hk->modifiers!=oldmd)) {
      md_ungrabkey(hk->grab);
      hk->grab=-1;
    }
    if((hk->position & IN_WINDOW_MASK) && hk->grab<0)
      hk->grab=md_grabkey(hk->keycode, hk->modifiers);
  }
}

static int mappingfunc(XEvent *e)
{
  if(e->xmapping.request==MappingKeyboard ||
     e->xmapping.request==MappingModifier) {
    e->xmapping.display=dpy;
    XRefreshKeyboardMapping(&e->xmapping);
  }
  rethink_keymap();
  return 0;
}

static int keypressfunc(XEvent *e)
{
  struct hotkey *hk;

  for(hk=hotkeys; hk; hk=hk->next)
    if(hk->keycode==e->xkey.keycode &&
       (hk->modifiers==e->xkey.state || (hk->modifiers & AnyModifier))) {
      hk->callback(e, hk->client_data);
      return 1;
    }
  
  return 0;
}

void cx_hotkey(KeySym ks, unsigned int mod, int m, int pos,
	       void (*cb)(XEvent *, void *), void *cd)
{
  struct hotkey *hk=malloc(sizeof(struct hotkey));
  if(hk) {
    if(!dpy) {
      dpy=md_display();
      cx_event_broker(KeyPress, KeyPressMask, keypressfunc);
      cx_event_broker(MappingNotify, 0, mappingfunc);
    }
    hk->next=hotkeys;
    hk->keysym=ks;
    hk->mod0=mod;
    hk->meta=m;
    hk->position=pos;
    hk->callback=cb;
    hk->client_data=cd;
    lookup_hotkey(hk);
    hk->grab=((pos&IN_WINDOW_MASK)? md_grabkey(hk->keycode, hk->modifiers):-1);
    hotkeys=hk;
  }
}