a4d4c576d80da3a40f716c7362202648cf7fbdfb
[wmaker-crm.git] / WPrefs.app / KeyboardShortcuts.c
bloba4d4c576d80da3a40f716c7362202648cf7fbdfb
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 { "MaximusKey", N_("Maximus: Tiled maximization ") },
86 { "RaiseKey", N_("Raise active window") },
87 { "LowerKey", N_("Lower active window") },
88 { "RaiseLowerKey", N_("Raise/Lower window under mouse pointer") },
89 { "ShadeKey", N_("Shade active window") },
90 { "MoveResizeKey", N_("Move/Resize active window") },
91 { "SelectKey", N_("Select active window") },
92 { "FocusNextKey", N_("Focus next window") },
93 { "FocusPrevKey", N_("Focus previous window") },
94 { "GroupNextKey", N_("Focus next group window") },
95 { "GroupPrevKey", N_("Focus previous group window") },
97 /* Workspace Related */
98 { "NextWorkspaceKey", N_("Switch to next workspace") },
99 { "PrevWorkspaceKey", N_("Switch to previous workspace") },
100 { "LastWorkspaceKey", N_("Switch to last used workspace") },
101 { "NextWorkspaceLayerKey", N_("Switch to next ten workspaces") },
102 { "PrevWorkspaceLayerKey", N_("Switch to previous ten workspaces") },
103 { "Workspace1Key", N_("Switch to workspace 1") },
104 { "Workspace2Key", N_("Switch to workspace 2") },
105 { "Workspace3Key", N_("Switch to workspace 3") },
106 { "Workspace4Key", N_("Switch to workspace 4") },
107 { "Workspace5Key", N_("Switch to workspace 5") },
108 { "Workspace6Key", N_("Switch to workspace 6") },
109 { "Workspace7Key", N_("Switch to workspace 7") },
110 { "Workspace8Key", N_("Switch to workspace 8") },
111 { "Workspace9Key", N_("Switch to workspace 9") },
112 { "Workspace10Key", N_("Switch to workspace 10") },
113 { "MoveToNextWorkspaceKey", N_("Move window to next workspace") },
114 { "MoveToPrevWorkspaceKey", N_("Move window to previous workspace") },
115 { "MoveToLastWorkspaceKey", N_("Move window to last used workspace") },
116 { "MoveToNextWorkspaceLayerKey", N_("Move window to next ten workspaces") },
117 { "MoveToPrevWorkspaceLayerKey", N_("Move window to previous ten workspaces") },
118 { "MoveToWorkspace1Key", N_("Move window to workspace 1") },
119 { "MoveToWorkspace2Key", N_("Move window to workspace 2") },
120 { "MoveToWorkspace3Key", N_("Move window to workspace 3") },
121 { "MoveToWorkspace4Key", N_("Move window to workspace 4") },
122 { "MoveToWorkspace5Key", N_("Move window to workspace 5") },
123 { "MoveToWorkspace6Key", N_("Move window to workspace 6") },
124 { "MoveToWorkspace7Key", N_("Move window to workspace 7") },
125 { "MoveToWorkspace8Key", N_("Move window to workspace 8") },
126 { "MoveToWorkspace9Key", N_("Move window to workspace 9") },
127 { "MoveToWorkspace10Key", N_("Move window to workspace 10") },
129 /* Window Selection */
130 { "WindowShortcut1Key", N_("Shortcut for window 1") },
131 { "WindowShortcut2Key", N_("Shortcut for window 2") },
132 { "WindowShortcut3Key", N_("Shortcut for window 3") },
133 { "WindowShortcut4Key", N_("Shortcut for window 4") },
134 { "WindowShortcut5Key", N_("Shortcut for window 5") },
135 { "WindowShortcut6Key", N_("Shortcut for window 6") },
136 { "WindowShortcut7Key", N_("Shortcut for window 7") },
137 { "WindowShortcut8Key", N_("Shortcut for window 8") },
138 { "WindowShortcut9Key", N_("Shortcut for window 9") },
139 { "WindowShortcut10Key", N_("Shortcut for window 10") },
141 /* Misc. */
142 { "WindowRelaunchKey", N_("Launch new instance of application") },
143 { "ScreenSwitchKey", N_("Switch to Next Screen/Monitor") },
144 { "DockRaiseLowerKey", N_("Raise/Lower Dock") },
145 { "ClipRaiseLowerKey", N_("Raise/Lower Clip") }
146 #ifdef XKB_MODELOCK
147 ,{ "ToggleKbdModeKey", N_("Toggle keyboard language") }
148 #endif /* XKB_MODELOCK */
151 #ifndef HAVE_XCONVERTCASE
152 /* from Xlib */
154 static void XConvertCase(register KeySym sym, KeySym * lower, KeySym * upper)
156 *lower = sym;
157 *upper = sym;
158 switch (sym >> 8) {
159 case 0: /* Latin 1 */
160 if ((sym >= XK_A) && (sym <= XK_Z))
161 *lower += (XK_a - XK_A);
162 else if ((sym >= XK_a) && (sym <= XK_z))
163 *upper -= (XK_a - XK_A);
164 else if ((sym >= XK_Agrave) && (sym <= XK_Odiaeresis))
165 *lower += (XK_agrave - XK_Agrave);
166 else if ((sym >= XK_agrave) && (sym <= XK_odiaeresis))
167 *upper -= (XK_agrave - XK_Agrave);
168 else if ((sym >= XK_Ooblique) && (sym <= XK_Thorn))
169 *lower += (XK_oslash - XK_Ooblique);
170 else if ((sym >= XK_oslash) && (sym <= XK_thorn))
171 *upper -= (XK_oslash - XK_Ooblique);
172 break;
173 case 1: /* Latin 2 */
174 /* Assume the KeySym is a legal value (ignore discontinuities) */
175 if (sym == XK_Aogonek)
176 *lower = XK_aogonek;
177 else if (sym >= XK_Lstroke && sym <= XK_Sacute)
178 *lower += (XK_lstroke - XK_Lstroke);
179 else if (sym >= XK_Scaron && sym <= XK_Zacute)
180 *lower += (XK_scaron - XK_Scaron);
181 else if (sym >= XK_Zcaron && sym <= XK_Zabovedot)
182 *lower += (XK_zcaron - XK_Zcaron);
183 else if (sym == XK_aogonek)
184 *upper = XK_Aogonek;
185 else if (sym >= XK_lstroke && sym <= XK_sacute)
186 *upper -= (XK_lstroke - XK_Lstroke);
187 else if (sym >= XK_scaron && sym <= XK_zacute)
188 *upper -= (XK_scaron - XK_Scaron);
189 else if (sym >= XK_zcaron && sym <= XK_zabovedot)
190 *upper -= (XK_zcaron - XK_Zcaron);
191 else if (sym >= XK_Racute && sym <= XK_Tcedilla)
192 *lower += (XK_racute - XK_Racute);
193 else if (sym >= XK_racute && sym <= XK_tcedilla)
194 *upper -= (XK_racute - XK_Racute);
195 break;
196 case 2: /* Latin 3 */
197 /* Assume the KeySym is a legal value (ignore discontinuities) */
198 if (sym >= XK_Hstroke && sym <= XK_Hcircumflex)
199 *lower += (XK_hstroke - XK_Hstroke);
200 else if (sym >= XK_Gbreve && sym <= XK_Jcircumflex)
201 *lower += (XK_gbreve - XK_Gbreve);
202 else if (sym >= XK_hstroke && sym <= XK_hcircumflex)
203 *upper -= (XK_hstroke - XK_Hstroke);
204 else if (sym >= XK_gbreve && sym <= XK_jcircumflex)
205 *upper -= (XK_gbreve - XK_Gbreve);
206 else if (sym >= XK_Cabovedot && sym <= XK_Scircumflex)
207 *lower += (XK_cabovedot - XK_Cabovedot);
208 else if (sym >= XK_cabovedot && sym <= XK_scircumflex)
209 *upper -= (XK_cabovedot - XK_Cabovedot);
210 break;
211 case 3: /* Latin 4 */
212 /* Assume the KeySym is a legal value (ignore discontinuities) */
213 if (sym >= XK_Rcedilla && sym <= XK_Tslash)
214 *lower += (XK_rcedilla - XK_Rcedilla);
215 else if (sym >= XK_rcedilla && sym <= XK_tslash)
216 *upper -= (XK_rcedilla - XK_Rcedilla);
217 else if (sym == XK_ENG)
218 *lower = XK_eng;
219 else if (sym == XK_eng)
220 *upper = XK_ENG;
221 else if (sym >= XK_Amacron && sym <= XK_Umacron)
222 *lower += (XK_amacron - XK_Amacron);
223 else if (sym >= XK_amacron && sym <= XK_umacron)
224 *upper -= (XK_amacron - XK_Amacron);
225 break;
226 case 6: /* Cyrillic */
227 /* Assume the KeySym is a legal value (ignore discontinuities) */
228 if (sym >= XK_Serbian_DJE && sym <= XK_Serbian_DZE)
229 *lower -= (XK_Serbian_DJE - XK_Serbian_dje);
230 else if (sym >= XK_Serbian_dje && sym <= XK_Serbian_dze)
231 *upper += (XK_Serbian_DJE - XK_Serbian_dje);
232 else if (sym >= XK_Cyrillic_YU && sym <= XK_Cyrillic_HARDSIGN)
233 *lower -= (XK_Cyrillic_YU - XK_Cyrillic_yu);
234 else if (sym >= XK_Cyrillic_yu && sym <= XK_Cyrillic_hardsign)
235 *upper += (XK_Cyrillic_YU - XK_Cyrillic_yu);
236 break;
237 case 7: /* Greek */
238 /* Assume the KeySym is a legal value (ignore discontinuities) */
239 if (sym >= XK_Greek_ALPHAaccent && sym <= XK_Greek_OMEGAaccent)
240 *lower += (XK_Greek_alphaaccent - XK_Greek_ALPHAaccent);
241 else if (sym >= XK_Greek_alphaaccent && sym <= XK_Greek_omegaaccent &&
242 sym != XK_Greek_iotaaccentdieresis && sym != XK_Greek_upsilonaccentdieresis)
243 *upper -= (XK_Greek_alphaaccent - XK_Greek_ALPHAaccent);
244 else if (sym >= XK_Greek_ALPHA && sym <= XK_Greek_OMEGA)
245 *lower += (XK_Greek_alpha - XK_Greek_ALPHA);
246 else if (sym >= XK_Greek_alpha && sym <= XK_Greek_omega && sym != XK_Greek_finalsmallsigma)
247 *upper -= (XK_Greek_alpha - XK_Greek_ALPHA);
248 break;
249 case 0x14: /* Armenian */
250 if (sym >= XK_Armenian_AYB && sym <= XK_Armenian_fe) {
251 *lower = sym | 1;
252 *upper = sym & ~1;
254 break;
257 #endif
259 char *capture_shortcut(Display *dpy, Bool *capturing, Bool convert_case)
261 XEvent ev;
262 KeySym ksym, lksym, uksym;
263 char buffer[64];
264 char *key = NULL;
266 while (*capturing) {
267 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
268 WMNextEvent(dpy, &ev);
269 if (ev.type == KeyPress && ev.xkey.keycode != 0) {
270 ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0);
271 if (!IsModifierKey(ksym)) {
272 if (convert_case) {
273 XConvertCase(ksym, &lksym, &uksym);
274 key = XKeysymToString(uksym);
275 } else {
276 key = XKeysymToString(ksym);
279 *capturing = 0;
280 break;
283 WMHandleEvent(&ev);
286 if (!key)
287 return NULL;
289 buffer[0] = 0;
291 if (ev.xkey.state & ControlMask) {
292 strcat(buffer, "Control+");
294 if (ev.xkey.state & ShiftMask) {
295 strcat(buffer, "Shift+");
297 if (ev.xkey.state & Mod1Mask) {
298 strcat(buffer, "Mod1+");
300 if (ev.xkey.state & Mod2Mask) {
301 strcat(buffer, "Mod2+");
303 if (ev.xkey.state & Mod3Mask) {
304 strcat(buffer, "Mod3+");
306 if (ev.xkey.state & Mod4Mask) {
307 strcat(buffer, "Mod4+");
309 if (ev.xkey.state & Mod5Mask) {
310 strcat(buffer, "Mod5+");
312 strcat(buffer, key);
314 return wstrdup(buffer);
317 static void captureClick(WMWidget * w, void *data)
319 _Panel *panel = (_Panel *) data;
320 Display *dpy = WMScreenDisplay(WMWidgetScreen(panel->parent));
321 char *shortcut;
323 if (!panel->capturing) {
324 panel->capturing = 1;
325 WMSetButtonText(w, _("Cancel"));
326 WMSetLabelText(panel->instructionsL,
327 _("Press the desired shortcut key(s) or click Cancel to stop capturing."));
328 XGrabKeyboard(dpy, WMWidgetXID(panel->parent), True, GrabModeAsync, GrabModeAsync, CurrentTime);
329 shortcut = capture_shortcut(dpy, &panel->capturing, 1);
330 if (shortcut) {
331 int row = WMGetListSelectedItemRow(panel->actLs);
333 WMSetTextFieldText(panel->shoT, shortcut);
334 if (row >= 0) {
335 if (panel->shortcuts[row])
336 wfree(panel->shortcuts[row]);
337 panel->shortcuts[row] = shortcut;
339 WMRedisplayWidget(panel->actLs);
340 } else {
341 wfree(shortcut);
345 panel->capturing = 0;
346 WMSetButtonText(w, _("Capture"));
347 WMSetLabelText(panel->instructionsL, _("Click on Capture to interactively define the shortcut key."));
348 XUngrabKeyboard(dpy, CurrentTime);
351 static void clearShortcut(WMWidget * w, void *data)
353 _Panel *panel = (_Panel *) data;
354 int row = WMGetListSelectedItemRow(panel->actLs);
356 WMSetTextFieldText(panel->shoT, NULL);
358 if (row >= 0) {
359 if (panel->shortcuts[row])
360 wfree(panel->shortcuts[row]);
361 panel->shortcuts[row] = NULL;
362 WMRedisplayWidget(panel->actLs);
366 static void typedKeys(void *observerData, WMNotification * notification)
368 _Panel *panel = (_Panel *) observerData;
369 int row = WMGetListSelectedItemRow(panel->actLs);
371 if (row < 0)
372 return;
374 if (panel->shortcuts[row])
375 wfree(panel->shortcuts[row]);
376 panel->shortcuts[row] = WMGetTextFieldText(panel->shoT);
377 if (strlen(panel->shortcuts[row]) == 0) {
378 wfree(panel->shortcuts[row]);
379 panel->shortcuts[row] = NULL;
381 WMRedisplayWidget(panel->actLs);
384 static void listClick(WMWidget * w, void *data)
386 _Panel *panel = (_Panel *) data;
387 int row = WMGetListSelectedItemRow(w);
389 WMSetTextFieldText(panel->shoT, panel->shortcuts[row]);
392 static void showData(_Panel * panel)
394 char *str;
395 int i;
397 for (i = 0; i < panel->actionCount; i++) {
399 str = GetStringForKey(keyOptions[i].key);
400 if (panel->shortcuts[i])
401 wfree(panel->shortcuts[i]);
402 if (str)
403 panel->shortcuts[i] = wtrimspace(str);
404 else
405 panel->shortcuts[i] = NULL;
407 if (panel->shortcuts[i] &&
408 (strcasecmp(panel->shortcuts[i], "none") == 0 || strlen(panel->shortcuts[i]) == 0)) {
409 wfree(panel->shortcuts[i]);
410 panel->shortcuts[i] = NULL;
413 WMRedisplayWidget(panel->actLs);
416 static void paintItem(WMList * lPtr, int index, Drawable d, char *text, int state, WMRect * rect)
418 int width, height, x, y;
419 _Panel *panel = (_Panel *) WMGetHangedData(lPtr);
420 WMScreen *scr = WMWidgetScreen(lPtr);
421 Display *dpy = WMScreenDisplay(scr);
422 WMColor *backColor = (state & WLDSSelected) ? panel->white : panel->gray;
424 width = rect->size.width;
425 height = rect->size.height;
426 x = rect->pos.x;
427 y = rect->pos.y;
429 XFillRectangle(dpy, d, WMColorGC(backColor), x, y, width, height);
431 if (panel->shortcuts[index]) {
432 WMPixmap *pix = WMGetSystemPixmap(scr, WSICheckMark);
433 WMSize size = WMGetPixmapSize(pix);
435 WMDrawPixmap(pix, d, x + (20 - size.width) / 2, (height - size.height) / 2 + y);
436 WMReleasePixmap(pix);
439 WMDrawString(scr, d, panel->black, panel->font, x + 20, y, text, strlen(text));
442 static void createPanel(Panel * p)
444 _Panel *panel = (_Panel *) p;
445 WMScreen *scr = WMWidgetScreen(panel->parent);
446 WMColor *color;
447 WMFont *boldFont;
448 int i;
450 panel->capturing = 0;
452 panel->white = WMWhiteColor(scr);
453 panel->black = WMBlackColor(scr);
454 panel->gray = WMGrayColor(scr);
455 panel->font = WMSystemFontOfSize(scr, 12);
457 panel->box = WMCreateBox(panel->parent);
458 WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
460 boldFont = WMBoldSystemFontOfSize(scr, 12);
462 /* **************** Actions **************** */
463 panel->actL = WMCreateLabel(panel->box);
464 WMResizeWidget(panel->actL, 280, 20);
465 WMMoveWidget(panel->actL, 20, 10);
466 WMSetLabelFont(panel->actL, boldFont);
467 WMSetLabelText(panel->actL, _("Actions"));
468 WMSetLabelRelief(panel->actL, WRSunken);
469 WMSetLabelTextAlignment(panel->actL, WACenter);
470 color = WMDarkGrayColor(scr);
471 WMSetWidgetBackgroundColor(panel->actL, color);
472 WMReleaseColor(color);
473 WMSetLabelTextColor(panel->actL, panel->white);
475 panel->actLs = WMCreateList(panel->box);
476 WMResizeWidget(panel->actLs, 280, 190);
477 WMMoveWidget(panel->actLs, 20, 32);
478 WMSetListUserDrawProc(panel->actLs, paintItem);
479 WMHangData(panel->actLs, panel);
481 for (i = 0; i < sizeof(keyOptions)/sizeof(keyOptions[0]); i++) {
482 WMAddListItem(panel->actLs, _(keyOptions[i].title));
484 WMSetListAction(panel->actLs, listClick, panel);
486 panel->actionCount = WMGetListNumberOfRows(panel->actLs);
487 panel->shortcuts = wmalloc(sizeof(char *) * panel->actionCount);
489 /***************** Shortcut ****************/
491 panel->shoF = WMCreateFrame(panel->box);
492 WMResizeWidget(panel->shoF, 190, 210);
493 WMMoveWidget(panel->shoF, 315, 10);
494 WMSetFrameTitle(panel->shoF, _("Shortcut"));
496 panel->shoT = WMCreateTextField(panel->shoF);
497 WMResizeWidget(panel->shoT, 160, 20);
498 WMMoveWidget(panel->shoT, 15, 65);
499 WMAddNotificationObserver(typedKeys, panel, WMTextDidChangeNotification, panel->shoT);
501 panel->cleB = WMCreateCommandButton(panel->shoF);
502 WMResizeWidget(panel->cleB, 75, 24);
503 WMMoveWidget(panel->cleB, 15, 95);
504 WMSetButtonText(panel->cleB, _("Clear"));
505 WMSetButtonAction(panel->cleB, clearShortcut, panel);
507 panel->defB = WMCreateCommandButton(panel->shoF);
508 WMResizeWidget(panel->defB, 75, 24);
509 WMMoveWidget(panel->defB, 100, 95);
510 WMSetButtonText(panel->defB, _("Capture"));
511 WMSetButtonAction(panel->defB, captureClick, panel);
513 panel->instructionsL = WMCreateLabel(panel->shoF);
514 WMResizeWidget(panel->instructionsL, 160, 55);
515 WMMoveWidget(panel->instructionsL, 15, 140);
516 WMSetLabelTextAlignment(panel->instructionsL, WACenter);
517 WMSetLabelWraps(panel->instructionsL, True);
518 WMSetLabelText(panel->instructionsL, _("Click on Capture to interactively define the shortcut key."));
520 WMMapSubwidgets(panel->shoF);
522 WMReleaseFont(boldFont);
524 WMRealizeWidget(panel->box);
525 WMMapSubwidgets(panel->box);
527 showData(panel);
530 static void storeData(_Panel * panel)
532 int i;
533 char *str;
535 for (i = 0; i < panel->actionCount; i++) {
536 str = NULL;
537 if (panel->shortcuts[i]) {
538 str = wtrimspace(panel->shortcuts[i]);
539 if (strlen(str) == 0) {
540 wfree(str);
541 str = NULL;
544 if (str) {
545 SetStringForKey(str, keyOptions[i].key);
546 wfree(str);
547 } else {
548 SetStringForKey("None", keyOptions[i].key);
553 Panel *InitKeyboardShortcuts(WMScreen * scr, WMWidget * parent)
555 _Panel *panel;
557 panel = wmalloc(sizeof(_Panel));
559 panel->sectionName = _("Keyboard Shortcut Preferences");
561 panel->description = _("Change the keyboard shortcuts for actions such\n"
562 "as changing workspaces and opening menus.");
564 panel->parent = parent;
566 panel->callbacks.createWidgets = createPanel;
567 panel->callbacks.updateDomain = storeData;
569 AddSection(panel, ICON_FILE);
571 return panel;