Renamed package to ion1, and made it conflict with package 'ion'.
[ion1.git] / src / shortcut.c
blob8f3e856a525a09f87d19299eb9a56af80678114a
1 /*
2 * ion/shortcut.c
4 * Copyright (c) Lukas Schroeder 2002
5 * See the included file LICENSE for details.
6 */
8 #include <ctype.h>
10 #include "common.h"
11 #include "binding.h"
12 #include "global.h"
13 #include "key.h"
14 #include "event.h"
15 #include "cursor.h"
16 #include "client.h"
17 #include "shortcut.h"
18 #include "grab.h"
20 bool shortcut_is_valid(int cut)
22 return ((cut>='0' && cut<='9') || (cut>='A' && cut<='z'));
25 void shortcut_remove(int cut)
27 WThing *thing;
29 if(!shortcut_is_valid(cut))
30 return;
32 thing=wglobal.shortcuts[cut];
33 if(!thing)
34 return;
36 if(WTHING_IS(thing, WFrame)){
37 WFrame *frame=(WFrame*)thing;
38 frame->shortcut=0;
41 wglobal.shortcuts[cut]=NULL;
44 static void shortcut_remove_from_frame(WFrame *frame)
46 shortcut_remove(frame->shortcut);
47 assert(frame->shortcut==0);
48 frame_recalc_bar(frame);
51 void shortcut_set(WThing *thing, int cut)
53 bool valid=shortcut_is_valid(cut);
55 if(valid){
56 WThing *oldthing=wglobal.shortcuts[cut];
57 if(oldthing && WTHING_IS(thing, WFrame))
58 shortcut_remove_from_frame((WFrame*)oldthing);
61 if(WTHING_IS(thing, WFrame))
62 shortcut_remove_from_frame((WFrame*)thing);
64 if(!valid)
65 return;
67 shortcut_remove(cut);
69 wglobal.shortcuts[cut]=thing;
70 if(WTHING_IS(thing, WFrame)){
71 WFrame *frame=(WFrame*)thing;
72 frame->shortcut=cut;
73 frame_recalc_bar(frame);
77 static char *decode_keysym(XKeyEvent *ev)
79 static char buffer[10];
80 int len;
82 len = XLookupString(ev, buffer, 10, NULL, NULL);
83 if(!len || !shortcut_is_valid((int)buffer[0]))
84 buffer[0]=0;
85 buffer[1]=0;
86 return buffer;
89 static bool setshortcut_handler(WThing *thing, XEvent *ev)
91 KeySym sym;
92 char *sc;
94 if(ismod(ev->xkey.keycode))
95 return FALSE;
97 sc=decode_keysym(&ev->xkey);
98 shortcut_set(thing, (int)sc[0]);
99 return TRUE;
102 static bool gotoshortcut_handler(WThing *thing, XEvent *ev)
104 WThing *target;
105 char *sc;
107 if(ismod(ev->xkey.keycode))
108 return FALSE;
110 sc=decode_keysym(&ev->xkey);
111 if(sc[0] && shortcut_is_valid((int)sc[0])){
113 target=wglobal.shortcuts[(int)sc[0]];
114 if(!target)
115 return TRUE;
117 if(WTHING_IS(target, WFrame)){
118 goto_window((WWindow*)target);
119 }else if(WTHING_IS(target, WClient))
120 goto_client((WClient*)target);
121 /* else: "Ooops. target is not a WFrame" */
123 /*skip_focusenter();*/
124 return TRUE;
127 void set_shortcut(WFrame *frame)
129 grab_establish((WThing*)frame, setshortcut_handler, FocusChangeMask|KeyReleaseMask);
132 void goto_shortcut(WFrame *frame)
134 grab_establish((WThing*)frame, gotoshortcut_handler, FocusChangeMask|KeyReleaseMask);