e34b2d83f41f1c9a858cb96234a5d7ffa5f57041
[wmaker-crm.git] / WPrefs.app / KeyboardShortcuts.c
blobe34b2d83f41f1c9a858cb96234a5d7ffa5f57041
1 /* KeyboardShortcuts.c- keyboard shortcut bindings
3 * WPrefs - Window Maker Preferences Program
5 * Copyright (c) 1998-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "config.h" /* for HAVE_XCONVERTCASE */
24 #include "WPrefs.h"
25 #include <ctype.h>
27 #include <X11/keysym.h>
28 #include <X11/XKBlib.h>
30 typedef struct _Panel {
31 WMBox *box;
33 char *sectionName;
35 char *description;
37 CallbackRec callbacks;
39 WMWidget *parent;
41 WMLabel *actL;
42 WMList *actLs;
44 WMFrame *shoF;
45 WMTextField *shoT;
46 WMButton *cleB;
47 WMButton *defB;
49 WMLabel *instructionsL;
51 WMColor *white;
52 WMColor *black;
53 WMColor *gray;
54 WMFont *font;
56 Bool capturing;
57 char **shortcuts;
58 int actionCount;
59 } _Panel;
61 #define ICON_FILE "keyshortcuts"
64 * List of user definable shortcut keys
65 * First parameter is the internal keyword known by WMaker
66 * Second is the text displayed to the user
68 static const struct {
69 const char *key;
70 const char *title;
71 } keyOptions[] = {
72 { "RootMenuKey", N_("Open applications menu") },
73 { "WindowListKey", N_("Open window list menu") },
74 { "WindowMenuKey", N_("Open window commands menu") },
75 { "HideKey", N_("Hide active application") },
76 { "HideOthersKey", N_("Hide other applications") },
77 { "MiniaturizeKey", N_("Miniaturize active window") },
78 { "MinimizeAllKey", N_("Miniaturize all windows") },
79 { "CloseKey", N_("Close active window") },
80 { "MaximizeKey", N_("Maximize active window") },
81 { "VMaximizeKey", N_("Maximize active window vertically") },
82 { "HMaximizeKey", N_("Maximize active window horizontally") },
83 { "LHMaximizeKey", N_("Maximize active window left half") },
84 { "RHMaximizeKey", N_("Maximize active window right half") },
85 { "THMaximizeKey", N_("Maximize active window top half") },
86 { "BHMaximizeKey", N_("Maximize active window bottom half") },
87 { "MaximusKey", N_("Maximus: Tiled maximization ") },
88 { "RaiseKey", N_("Raise active window") },
89 { "LowerKey", N_("Lower active window") },
90 { "RaiseLowerKey", N_("Raise/Lower window under mouse pointer") },
91 { "ShadeKey", N_("Shade active window") },
92 { "MoveResizeKey", N_("Move/Resize active window") },
93 { "SelectKey", N_("Select active window") },
94 { "FocusNextKey", N_("Focus next window") },
95 { "FocusPrevKey", N_("Focus previous window") },
96 { "GroupNextKey", N_("Focus next group window") },
97 { "GroupPrevKey", N_("Focus previous group window") },
99 /* Workspace Related */
100 { "NextWorkspaceKey", N_("Switch to next workspace") },
101 { "PrevWorkspaceKey", N_("Switch to previous workspace") },
102 { "LastWorkspaceKey", N_("Switch to last used workspace") },
103 { "NextWorkspaceLayerKey", N_("Switch to next ten workspaces") },
104 { "PrevWorkspaceLayerKey", N_("Switch to previous ten workspaces") },
105 { "Workspace1Key", N_("Switch to workspace 1") },
106 { "Workspace2Key", N_("Switch to workspace 2") },
107 { "Workspace3Key", N_("Switch to workspace 3") },
108 { "Workspace4Key", N_("Switch to workspace 4") },
109 { "Workspace5Key", N_("Switch to workspace 5") },
110 { "Workspace6Key", N_("Switch to workspace 6") },
111 { "Workspace7Key", N_("Switch to workspace 7") },
112 { "Workspace8Key", N_("Switch to workspace 8") },
113 { "Workspace9Key", N_("Switch to workspace 9") },
114 { "Workspace10Key", N_("Switch to workspace 10") },
115 { "MoveToNextWorkspaceKey", N_("Move window to next workspace") },
116 { "MoveToPrevWorkspaceKey", N_("Move window to previous workspace") },
117 { "MoveToLastWorkspaceKey", N_("Move window to last used workspace") },
118 { "MoveToNextWorkspaceLayerKey", N_("Move window to next ten workspaces") },
119 { "MoveToPrevWorkspaceLayerKey", N_("Move window to previous ten workspaces") },
120 { "MoveToWorkspace1Key", N_("Move window to workspace 1") },
121 { "MoveToWorkspace2Key", N_("Move window to workspace 2") },
122 { "MoveToWorkspace3Key", N_("Move window to workspace 3") },
123 { "MoveToWorkspace4Key", N_("Move window to workspace 4") },
124 { "MoveToWorkspace5Key", N_("Move window to workspace 5") },
125 { "MoveToWorkspace6Key", N_("Move window to workspace 6") },
126 { "MoveToWorkspace7Key", N_("Move window to workspace 7") },
127 { "MoveToWorkspace8Key", N_("Move window to workspace 8") },
128 { "MoveToWorkspace9Key", N_("Move window to workspace 9") },
129 { "MoveToWorkspace10Key", N_("Move window to workspace 10") },
131 /* Window Selection */
132 { "WindowShortcut1Key", N_("Shortcut for window 1") },
133 { "WindowShortcut2Key", N_("Shortcut for window 2") },
134 { "WindowShortcut3Key", N_("Shortcut for window 3") },
135 { "WindowShortcut4Key", N_("Shortcut for window 4") },
136 { "WindowShortcut5Key", N_("Shortcut for window 5") },
137 { "WindowShortcut6Key", N_("Shortcut for window 6") },
138 { "WindowShortcut7Key", N_("Shortcut for window 7") },
139 { "WindowShortcut8Key", N_("Shortcut for window 8") },
140 { "WindowShortcut9Key", N_("Shortcut for window 9") },
141 { "WindowShortcut10Key", N_("Shortcut for window 10") },
143 /* Misc. */
144 { "WindowRelaunchKey", N_("Launch new instance of application") },
145 { "ScreenSwitchKey", N_("Switch to Next Screen/Monitor") },
146 { "DockRaiseLowerKey", N_("Raise/Lower Dock") },
147 { "ClipRaiseLowerKey", N_("Raise/Lower Clip") }
148 #ifdef XKB_MODELOCK
149 ,{ "ToggleKbdModeKey", N_("Toggle keyboard language") }
150 #endif /* XKB_MODELOCK */
153 #ifndef HAVE_XCONVERTCASE
154 /* from Xlib */
156 static void XConvertCase(register KeySym sym, KeySym * lower, KeySym * upper)
158 *lower = sym;
159 *upper = sym;
160 switch (sym >> 8) {
161 case 0: /* Latin 1 */
162 if ((sym >= XK_A) && (sym <= XK_Z))
163 *lower += (XK_a - XK_A);
164 else if ((sym >= XK_a) && (sym <= XK_z))
165 *upper -= (XK_a - XK_A);
166 else if ((sym >= XK_Agrave) && (sym <= XK_Odiaeresis))
167 *lower += (XK_agrave - XK_Agrave);
168 else if ((sym >= XK_agrave) && (sym <= XK_odiaeresis))
169 *upper -= (XK_agrave - XK_Agrave);
170 else if ((sym >= XK_Ooblique) && (sym <= XK_Thorn))
171 *lower += (XK_oslash - XK_Ooblique);
172 else if ((sym >= XK_oslash) && (sym <= XK_thorn))
173 *upper -= (XK_oslash - XK_Ooblique);
174 break;
175 case 1: /* Latin 2 */
176 /* Assume the KeySym is a legal value (ignore discontinuities) */
177 if (sym == XK_Aogonek)
178 *lower = XK_aogonek;
179 else if (sym >= XK_Lstroke && sym <= XK_Sacute)
180 *lower += (XK_lstroke - XK_Lstroke);
181 else if (sym >= XK_Scaron && sym <= XK_Zacute)
182 *lower += (XK_scaron - XK_Scaron);
183 else if (sym >= XK_Zcaron && sym <= XK_Zabovedot)
184 *lower += (XK_zcaron - XK_Zcaron);
185 else if (sym == XK_aogonek)
186 *upper = XK_Aogonek;
187 else if (sym >= XK_lstroke && sym <= XK_sacute)
188 *upper -= (XK_lstroke - XK_Lstroke);
189 else if (sym >= XK_scaron && sym <= XK_zacute)
190 *upper -= (XK_scaron - XK_Scaron);
191 else if (sym >= XK_zcaron && sym <= XK_zabovedot)
192 *upper -= (XK_zcaron - XK_Zcaron);
193 else if (sym >= XK_Racute && sym <= XK_Tcedilla)
194 *lower += (XK_racute - XK_Racute);
195 else if (sym >= XK_racute && sym <= XK_tcedilla)
196 *upper -= (XK_racute - XK_Racute);
197 break;
198 case 2: /* Latin 3 */
199 /* Assume the KeySym is a legal value (ignore discontinuities) */
200 if (sym >= XK_Hstroke && sym <= XK_Hcircumflex)
201 *lower += (XK_hstroke - XK_Hstroke);
202 else if (sym >= XK_Gbreve && sym <= XK_Jcircumflex)
203 *lower += (XK_gbreve - XK_Gbreve);
204 else if (sym >= XK_hstroke && sym <= XK_hcircumflex)
205 *upper -= (XK_hstroke - XK_Hstroke);
206 else if (sym >= XK_gbreve && sym <= XK_jcircumflex)
207 *upper -= (XK_gbreve - XK_Gbreve);
208 else if (sym >= XK_Cabovedot && sym <= XK_Scircumflex)
209 *lower += (XK_cabovedot - XK_Cabovedot);
210 else if (sym >= XK_cabovedot && sym <= XK_scircumflex)
211 *upper -= (XK_cabovedot - XK_Cabovedot);
212 break;
213 case 3: /* Latin 4 */
214 /* Assume the KeySym is a legal value (ignore discontinuities) */
215 if (sym >= XK_Rcedilla && sym <= XK_Tslash)
216 *lower += (XK_rcedilla - XK_Rcedilla);
217 else if (sym >= XK_rcedilla && sym <= XK_tslash)
218 *upper -= (XK_rcedilla - XK_Rcedilla);
219 else if (sym == XK_ENG)
220 *lower = XK_eng;
221 else if (sym == XK_eng)
222 *upper = XK_ENG;
223 else if (sym >= XK_Amacron && sym <= XK_Umacron)
224 *lower += (XK_amacron - XK_Amacron);
225 else if (sym >= XK_amacron && sym <= XK_umacron)
226 *upper -= (XK_amacron - XK_Amacron);
227 break;
228 case 6: /* Cyrillic */
229 /* Assume the KeySym is a legal value (ignore discontinuities) */
230 if (sym >= XK_Serbian_DJE && sym <= XK_Serbian_DZE)
231 *lower -= (XK_Serbian_DJE - XK_Serbian_dje);
232 else if (sym >= XK_Serbian_dje && sym <= XK_Serbian_dze)
233 *upper += (XK_Serbian_DJE - XK_Serbian_dje);
234 else if (sym >= XK_Cyrillic_YU && sym <= XK_Cyrillic_HARDSIGN)
235 *lower -= (XK_Cyrillic_YU - XK_Cyrillic_yu);
236 else if (sym >= XK_Cyrillic_yu && sym <= XK_Cyrillic_hardsign)
237 *upper += (XK_Cyrillic_YU - XK_Cyrillic_yu);
238 break;
239 case 7: /* Greek */
240 /* Assume the KeySym is a legal value (ignore discontinuities) */
241 if (sym >= XK_Greek_ALPHAaccent && sym <= XK_Greek_OMEGAaccent)
242 *lower += (XK_Greek_alphaaccent - XK_Greek_ALPHAaccent);
243 else if (sym >= XK_Greek_alphaaccent && sym <= XK_Greek_omegaaccent &&
244 sym != XK_Greek_iotaaccentdieresis && sym != XK_Greek_upsilonaccentdieresis)
245 *upper -= (XK_Greek_alphaaccent - XK_Greek_ALPHAaccent);
246 else if (sym >= XK_Greek_ALPHA && sym <= XK_Greek_OMEGA)
247 *lower += (XK_Greek_alpha - XK_Greek_ALPHA);
248 else if (sym >= XK_Greek_alpha && sym <= XK_Greek_omega && sym != XK_Greek_finalsmallsigma)
249 *upper -= (XK_Greek_alpha - XK_Greek_ALPHA);
250 break;
251 case 0x14: /* Armenian */
252 if (sym >= XK_Armenian_AYB && sym <= XK_Armenian_fe) {
253 *lower = sym | 1;
254 *upper = sym & ~1;
256 break;
259 #endif
261 char *capture_shortcut(Display *dpy, Bool *capturing, Bool convert_case)
263 XEvent ev;
264 KeySym ksym, lksym, uksym;
265 char buffer[64];
266 char *key = NULL;
268 while (*capturing) {
269 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
270 WMNextEvent(dpy, &ev);
271 if (ev.type == KeyPress && ev.xkey.keycode != 0) {
272 ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0);
273 if (!IsModifierKey(ksym)) {
274 if (convert_case) {
275 XConvertCase(ksym, &lksym, &uksym);
276 key = XKeysymToString(uksym);
277 } else {
278 key = XKeysymToString(ksym);
281 *capturing = 0;
282 break;
285 WMHandleEvent(&ev);
288 if (!key)
289 return NULL;
291 buffer[0] = 0;
293 if (ev.xkey.state & ControlMask) {
294 strcat(buffer, "Control+");
296 if (ev.xkey.state & ShiftMask) {
297 strcat(buffer, "Shift+");
299 if (ev.xkey.state & Mod1Mask) {
300 strcat(buffer, "Mod1+");
302 if (ev.xkey.state & Mod2Mask) {
303 strcat(buffer, "Mod2+");
305 if (ev.xkey.state & Mod3Mask) {
306 strcat(buffer, "Mod3+");
308 if (ev.xkey.state & Mod4Mask) {
309 strcat(buffer, "Mod4+");
311 if (ev.xkey.state & Mod5Mask) {
312 strcat(buffer, "Mod5+");
314 strcat(buffer, key);
316 return wstrdup(buffer);
319 static void captureClick(WMWidget * w, void *data)
321 _Panel *panel = (_Panel *) data;
322 Display *dpy = WMScreenDisplay(WMWidgetScreen(panel->parent));
323 char *shortcut;
325 if (!panel->capturing) {
326 panel->capturing = 1;
327 WMSetButtonText(w, _("Cancel"));
328 WMSetLabelText(panel->instructionsL,
329 _("Press the desired shortcut key(s) or click Cancel to stop capturing."));
330 XGrabKeyboard(dpy, WMWidgetXID(panel->parent), True, GrabModeAsync, GrabModeAsync, CurrentTime);
331 shortcut = capture_shortcut(dpy, &panel->capturing, 1);
332 if (shortcut) {
333 int row = WMGetListSelectedItemRow(panel->actLs);
335 WMSetTextFieldText(panel->shoT, shortcut);
336 if (row >= 0) {
337 if (panel->shortcuts[row])
338 wfree(panel->shortcuts[row]);
339 panel->shortcuts[row] = shortcut;
341 WMRedisplayWidget(panel->actLs);
342 } else {
343 wfree(shortcut);
347 panel->capturing = 0;
348 WMSetButtonText(w, _("Capture"));
349 WMSetLabelText(panel->instructionsL, _("Click on Capture to interactively define the shortcut key."));
350 XUngrabKeyboard(dpy, CurrentTime);
353 static void clearShortcut(WMWidget * w, void *data)
355 _Panel *panel = (_Panel *) data;
356 int row = WMGetListSelectedItemRow(panel->actLs);
358 WMSetTextFieldText(panel->shoT, NULL);
360 if (row >= 0) {
361 if (panel->shortcuts[row])
362 wfree(panel->shortcuts[row]);
363 panel->shortcuts[row] = NULL;
364 WMRedisplayWidget(panel->actLs);
368 static void typedKeys(void *observerData, WMNotification * notification)
370 _Panel *panel = (_Panel *) observerData;
371 int row = WMGetListSelectedItemRow(panel->actLs);
373 if (row < 0)
374 return;
376 if (panel->shortcuts[row])
377 wfree(panel->shortcuts[row]);
378 panel->shortcuts[row] = WMGetTextFieldText(panel->shoT);
379 if (strlen(panel->shortcuts[row]) == 0) {
380 wfree(panel->shortcuts[row]);
381 panel->shortcuts[row] = NULL;
383 WMRedisplayWidget(panel->actLs);
386 static void listClick(WMWidget * w, void *data)
388 _Panel *panel = (_Panel *) data;
389 int row = WMGetListSelectedItemRow(w);
391 WMSetTextFieldText(panel->shoT, panel->shortcuts[row]);
394 static void showData(_Panel * panel)
396 char *str;
397 int i;
399 for (i = 0; i < panel->actionCount; i++) {
401 str = GetStringForKey(keyOptions[i].key);
402 if (panel->shortcuts[i])
403 wfree(panel->shortcuts[i]);
404 if (str)
405 panel->shortcuts[i] = wtrimspace(str);
406 else
407 panel->shortcuts[i] = NULL;
409 if (panel->shortcuts[i] &&
410 (strcasecmp(panel->shortcuts[i], "none") == 0 || strlen(panel->shortcuts[i]) == 0)) {
411 wfree(panel->shortcuts[i]);
412 panel->shortcuts[i] = NULL;
415 WMRedisplayWidget(panel->actLs);
418 static void paintItem(WMList * lPtr, int index, Drawable d, char *text, int state, WMRect * rect)
420 int width, height, x, y;
421 _Panel *panel = (_Panel *) WMGetHangedData(lPtr);
422 WMScreen *scr = WMWidgetScreen(lPtr);
423 Display *dpy = WMScreenDisplay(scr);
424 WMColor *backColor = (state & WLDSSelected) ? panel->white : panel->gray;
426 width = rect->size.width;
427 height = rect->size.height;
428 x = rect->pos.x;
429 y = rect->pos.y;
431 XFillRectangle(dpy, d, WMColorGC(backColor), x, y, width, height);
433 if (panel->shortcuts[index]) {
434 WMPixmap *pix = WMGetSystemPixmap(scr, WSICheckMark);
435 WMSize size = WMGetPixmapSize(pix);
437 WMDrawPixmap(pix, d, x + (20 - size.width) / 2, (height - size.height) / 2 + y);
438 WMReleasePixmap(pix);
441 WMDrawString(scr, d, panel->black, panel->font, x + 20, y, text, strlen(text));
444 static void createPanel(Panel * p)
446 _Panel *panel = (_Panel *) p;
447 WMScreen *scr = WMWidgetScreen(panel->parent);
448 WMColor *color;
449 WMFont *boldFont;
450 int i;
452 panel->capturing = 0;
454 panel->white = WMWhiteColor(scr);
455 panel->black = WMBlackColor(scr);
456 panel->gray = WMGrayColor(scr);
457 panel->font = WMSystemFontOfSize(scr, 12);
459 panel->box = WMCreateBox(panel->parent);
460 WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
462 boldFont = WMBoldSystemFontOfSize(scr, 12);
464 /* **************** Actions **************** */
465 panel->actL = WMCreateLabel(panel->box);
466 WMResizeWidget(panel->actL, 280, 20);
467 WMMoveWidget(panel->actL, 20, 10);
468 WMSetLabelFont(panel->actL, boldFont);
469 WMSetLabelText(panel->actL, _("Actions"));
470 WMSetLabelRelief(panel->actL, WRSunken);
471 WMSetLabelTextAlignment(panel->actL, WACenter);
472 color = WMDarkGrayColor(scr);
473 WMSetWidgetBackgroundColor(panel->actL, color);
474 WMReleaseColor(color);
475 WMSetLabelTextColor(panel->actL, panel->white);
477 panel->actLs = WMCreateList(panel->box);
478 WMResizeWidget(panel->actLs, 280, 190);
479 WMMoveWidget(panel->actLs, 20, 32);
480 WMSetListUserDrawProc(panel->actLs, paintItem);
481 WMHangData(panel->actLs, panel);
483 for (i = 0; i < sizeof(keyOptions)/sizeof(keyOptions[0]); i++) {
484 WMAddListItem(panel->actLs, _(keyOptions[i].title));
486 WMSetListAction(panel->actLs, listClick, panel);
488 panel->actionCount = WMGetListNumberOfRows(panel->actLs);
489 panel->shortcuts = wmalloc(sizeof(char *) * panel->actionCount);
491 /***************** Shortcut ****************/
493 panel->shoF = WMCreateFrame(panel->box);
494 WMResizeWidget(panel->shoF, 190, 210);
495 WMMoveWidget(panel->shoF, 315, 10);
496 WMSetFrameTitle(panel->shoF, _("Shortcut"));
498 panel->shoT = WMCreateTextField(panel->shoF);
499 WMResizeWidget(panel->shoT, 160, 20);
500 WMMoveWidget(panel->shoT, 15, 65);
501 WMAddNotificationObserver(typedKeys, panel, WMTextDidChangeNotification, panel->shoT);
503 panel->cleB = WMCreateCommandButton(panel->shoF);
504 WMResizeWidget(panel->cleB, 75, 24);
505 WMMoveWidget(panel->cleB, 15, 95);
506 WMSetButtonText(panel->cleB, _("Clear"));
507 WMSetButtonAction(panel->cleB, clearShortcut, panel);
509 panel->defB = WMCreateCommandButton(panel->shoF);
510 WMResizeWidget(panel->defB, 75, 24);
511 WMMoveWidget(panel->defB, 100, 95);
512 WMSetButtonText(panel->defB, _("Capture"));
513 WMSetButtonAction(panel->defB, captureClick, panel);
515 panel->instructionsL = WMCreateLabel(panel->shoF);
516 WMResizeWidget(panel->instructionsL, 160, 55);
517 WMMoveWidget(panel->instructionsL, 15, 140);
518 WMSetLabelTextAlignment(panel->instructionsL, WACenter);
519 WMSetLabelWraps(panel->instructionsL, True);
520 WMSetLabelText(panel->instructionsL, _("Click on Capture to interactively define the shortcut key."));
522 WMMapSubwidgets(panel->shoF);
524 WMReleaseFont(boldFont);
526 WMRealizeWidget(panel->box);
527 WMMapSubwidgets(panel->box);
529 showData(panel);
532 static void storeData(_Panel * panel)
534 int i;
535 char *str;
537 for (i = 0; i < panel->actionCount; i++) {
538 str = NULL;
539 if (panel->shortcuts[i]) {
540 str = wtrimspace(panel->shortcuts[i]);
541 if (strlen(str) == 0) {
542 wfree(str);
543 str = NULL;
546 if (str) {
547 SetStringForKey(str, keyOptions[i].key);
548 wfree(str);
549 } else {
550 SetStringForKey("None", keyOptions[i].key);
555 Panel *InitKeyboardShortcuts(WMScreen * scr, WMWidget * parent)
557 _Panel *panel;
559 panel = wmalloc(sizeof(_Panel));
561 panel->sectionName = _("Keyboard Shortcut Preferences");
563 panel->description = _("Change the keyboard shortcuts for actions such\n"
564 "as changing workspaces and opening menus.");
566 panel->parent = parent;
568 panel->callbacks.createWidgets = createPanel;
569 panel->callbacks.updateDomain = storeData;
571 AddSection(panel, ICON_FILE);
573 return panel;