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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
23 #include "config.h" /* for HAVE_XCONVERTCASE */
28 #include <X11/keysym.h>
30 typedef struct _Panel
{
37 CallbackRec callbacks
;
49 WMLabel
*instructionsL
;
61 #define ICON_FILE "keyshortcuts"
64 * Must be in the same order as the corresponding items in
65 * "actions list" in createPanel()
67 static char *keyOptions
[] = {
93 "NextWorkspaceLayerKey",
94 "PrevWorkspaceLayerKey",
105 "WindowShortcut1Key",
106 "WindowShortcut2Key",
107 "WindowShortcut3Key",
108 "WindowShortcut4Key",
109 "WindowShortcut5Key",
110 "WindowShortcut6Key",
111 "WindowShortcut7Key",
112 "WindowShortcut8Key",
113 "WindowShortcut9Key",
114 "WindowShortcut10Key",
122 #endif /* XKB_MODELOCK */
125 #ifndef HAVE_XCONVERTCASE
128 static void XConvertCase(register KeySym sym
, KeySym
* lower
, KeySym
* upper
)
133 case 0: /* Latin 1 */
134 if ((sym
>= XK_A
) && (sym
<= XK_Z
))
135 *lower
+= (XK_a
- XK_A
);
136 else if ((sym
>= XK_a
) && (sym
<= XK_z
))
137 *upper
-= (XK_a
- XK_A
);
138 else if ((sym
>= XK_Agrave
) && (sym
<= XK_Odiaeresis
))
139 *lower
+= (XK_agrave
- XK_Agrave
);
140 else if ((sym
>= XK_agrave
) && (sym
<= XK_odiaeresis
))
141 *upper
-= (XK_agrave
- XK_Agrave
);
142 else if ((sym
>= XK_Ooblique
) && (sym
<= XK_Thorn
))
143 *lower
+= (XK_oslash
- XK_Ooblique
);
144 else if ((sym
>= XK_oslash
) && (sym
<= XK_thorn
))
145 *upper
-= (XK_oslash
- XK_Ooblique
);
147 case 1: /* Latin 2 */
148 /* Assume the KeySym is a legal value (ignore discontinuities) */
149 if (sym
== XK_Aogonek
)
151 else if (sym
>= XK_Lstroke
&& sym
<= XK_Sacute
)
152 *lower
+= (XK_lstroke
- XK_Lstroke
);
153 else if (sym
>= XK_Scaron
&& sym
<= XK_Zacute
)
154 *lower
+= (XK_scaron
- XK_Scaron
);
155 else if (sym
>= XK_Zcaron
&& sym
<= XK_Zabovedot
)
156 *lower
+= (XK_zcaron
- XK_Zcaron
);
157 else if (sym
== XK_aogonek
)
159 else if (sym
>= XK_lstroke
&& sym
<= XK_sacute
)
160 *upper
-= (XK_lstroke
- XK_Lstroke
);
161 else if (sym
>= XK_scaron
&& sym
<= XK_zacute
)
162 *upper
-= (XK_scaron
- XK_Scaron
);
163 else if (sym
>= XK_zcaron
&& sym
<= XK_zabovedot
)
164 *upper
-= (XK_zcaron
- XK_Zcaron
);
165 else if (sym
>= XK_Racute
&& sym
<= XK_Tcedilla
)
166 *lower
+= (XK_racute
- XK_Racute
);
167 else if (sym
>= XK_racute
&& sym
<= XK_tcedilla
)
168 *upper
-= (XK_racute
- XK_Racute
);
170 case 2: /* Latin 3 */
171 /* Assume the KeySym is a legal value (ignore discontinuities) */
172 if (sym
>= XK_Hstroke
&& sym
<= XK_Hcircumflex
)
173 *lower
+= (XK_hstroke
- XK_Hstroke
);
174 else if (sym
>= XK_Gbreve
&& sym
<= XK_Jcircumflex
)
175 *lower
+= (XK_gbreve
- XK_Gbreve
);
176 else if (sym
>= XK_hstroke
&& sym
<= XK_hcircumflex
)
177 *upper
-= (XK_hstroke
- XK_Hstroke
);
178 else if (sym
>= XK_gbreve
&& sym
<= XK_jcircumflex
)
179 *upper
-= (XK_gbreve
- XK_Gbreve
);
180 else if (sym
>= XK_Cabovedot
&& sym
<= XK_Scircumflex
)
181 *lower
+= (XK_cabovedot
- XK_Cabovedot
);
182 else if (sym
>= XK_cabovedot
&& sym
<= XK_scircumflex
)
183 *upper
-= (XK_cabovedot
- XK_Cabovedot
);
185 case 3: /* Latin 4 */
186 /* Assume the KeySym is a legal value (ignore discontinuities) */
187 if (sym
>= XK_Rcedilla
&& sym
<= XK_Tslash
)
188 *lower
+= (XK_rcedilla
- XK_Rcedilla
);
189 else if (sym
>= XK_rcedilla
&& sym
<= XK_tslash
)
190 *upper
-= (XK_rcedilla
- XK_Rcedilla
);
191 else if (sym
== XK_ENG
)
193 else if (sym
== XK_eng
)
195 else if (sym
>= XK_Amacron
&& sym
<= XK_Umacron
)
196 *lower
+= (XK_amacron
- XK_Amacron
);
197 else if (sym
>= XK_amacron
&& sym
<= XK_umacron
)
198 *upper
-= (XK_amacron
- XK_Amacron
);
200 case 6: /* Cyrillic */
201 /* Assume the KeySym is a legal value (ignore discontinuities) */
202 if (sym
>= XK_Serbian_DJE
&& sym
<= XK_Serbian_DZE
)
203 *lower
-= (XK_Serbian_DJE
- XK_Serbian_dje
);
204 else if (sym
>= XK_Serbian_dje
&& sym
<= XK_Serbian_dze
)
205 *upper
+= (XK_Serbian_DJE
- XK_Serbian_dje
);
206 else if (sym
>= XK_Cyrillic_YU
&& sym
<= XK_Cyrillic_HARDSIGN
)
207 *lower
-= (XK_Cyrillic_YU
- XK_Cyrillic_yu
);
208 else if (sym
>= XK_Cyrillic_yu
&& sym
<= XK_Cyrillic_hardsign
)
209 *upper
+= (XK_Cyrillic_YU
- XK_Cyrillic_yu
);
212 /* Assume the KeySym is a legal value (ignore discontinuities) */
213 if (sym
>= XK_Greek_ALPHAaccent
&& sym
<= XK_Greek_OMEGAaccent
)
214 *lower
+= (XK_Greek_alphaaccent
- XK_Greek_ALPHAaccent
);
215 else if (sym
>= XK_Greek_alphaaccent
&& sym
<= XK_Greek_omegaaccent
&&
216 sym
!= XK_Greek_iotaaccentdieresis
&& sym
!= XK_Greek_upsilonaccentdieresis
)
217 *upper
-= (XK_Greek_alphaaccent
- XK_Greek_ALPHAaccent
);
218 else if (sym
>= XK_Greek_ALPHA
&& sym
<= XK_Greek_OMEGA
)
219 *lower
+= (XK_Greek_alpha
- XK_Greek_ALPHA
);
220 else if (sym
>= XK_Greek_alpha
&& sym
<= XK_Greek_omega
&& sym
!= XK_Greek_finalsmallsigma
)
221 *upper
-= (XK_Greek_alpha
- XK_Greek_ALPHA
);
223 case 0x14: /* Armenian */
224 if (sym
>= XK_Armenian_AYB
&& sym
<= XK_Armenian_fe
) {
233 static char *captureShortcut(Display
* dpy
, _Panel
* panel
)
236 KeySym ksym
, lksym
, uksym
;
240 while (panel
->capturing
) {
241 XAllowEvents(dpy
, AsyncKeyboard
, CurrentTime
);
242 WMNextEvent(dpy
, &ev
);
243 if (ev
.type
== KeyPress
&& ev
.xkey
.keycode
!= 0) {
244 ksym
= XKeycodeToKeysym(dpy
, ev
.xkey
.keycode
, 0);
245 if (!IsModifierKey(ksym
)) {
246 XConvertCase(ksym
, &lksym
, &uksym
);
247 key
= XKeysymToString(uksym
);
249 panel
->capturing
= 0;
261 if (ev
.xkey
.state
& ControlMask
) {
262 strcat(buffer
, "Control+");
264 if (ev
.xkey
.state
& ShiftMask
) {
265 strcat(buffer
, "Shift+");
267 if (ev
.xkey
.state
& Mod1Mask
) {
268 strcat(buffer
, "Mod1+");
270 if (ev
.xkey
.state
& Mod2Mask
) {
271 strcat(buffer
, "Mod2+");
273 if (ev
.xkey
.state
& Mod3Mask
) {
274 strcat(buffer
, "Mod3+");
276 if (ev
.xkey
.state
& Mod4Mask
) {
277 strcat(buffer
, "Mod4+");
279 if (ev
.xkey
.state
& Mod5Mask
) {
280 strcat(buffer
, "Mod5+");
284 return wstrdup(buffer
);
287 static void captureClick(WMWidget
* w
, void *data
)
289 _Panel
*panel
= (_Panel
*) data
;
290 Display
*dpy
= WMScreenDisplay(WMWidgetScreen(panel
->parent
));
293 if (!panel
->capturing
) {
294 panel
->capturing
= 1;
295 WMSetButtonText(w
, _("Cancel"));
296 WMSetLabelText(panel
->instructionsL
,
297 _("Press the desired shortcut key(s) or click Cancel to stop capturing."));
298 XGrabKeyboard(dpy
, WMWidgetXID(panel
->parent
), True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
);
299 shortcut
= captureShortcut(dpy
, panel
);
301 int row
= WMGetListSelectedItemRow(panel
->actLs
);
303 WMSetTextFieldText(panel
->shoT
, shortcut
);
305 if (panel
->shortcuts
[row
])
306 wfree(panel
->shortcuts
[row
]);
307 panel
->shortcuts
[row
] = shortcut
;
309 WMRedisplayWidget(panel
->actLs
);
315 panel
->capturing
= 0;
316 WMSetButtonText(w
, _("Capture"));
317 WMSetLabelText(panel
->instructionsL
, _("Click on Capture to interactively define the shortcut key."));
318 XUngrabKeyboard(dpy
, CurrentTime
);
321 static void clearShortcut(WMWidget
* w
, void *data
)
323 _Panel
*panel
= (_Panel
*) data
;
324 int row
= WMGetListSelectedItemRow(panel
->actLs
);
326 WMSetTextFieldText(panel
->shoT
, NULL
);
329 if (panel
->shortcuts
[row
])
330 wfree(panel
->shortcuts
[row
]);
331 panel
->shortcuts
[row
] = NULL
;
332 WMRedisplayWidget(panel
->actLs
);
336 static void typedKeys(void *observerData
, WMNotification
* notification
)
338 _Panel
*panel
= (_Panel
*) observerData
;
339 int row
= WMGetListSelectedItemRow(panel
->actLs
);
344 if (panel
->shortcuts
[row
])
345 wfree(panel
->shortcuts
[row
]);
346 panel
->shortcuts
[row
] = WMGetTextFieldText(panel
->shoT
);
347 if (strlen(panel
->shortcuts
[row
]) == 0) {
348 wfree(panel
->shortcuts
[row
]);
349 panel
->shortcuts
[row
] = NULL
;
351 WMRedisplayWidget(panel
->actLs
);
354 static void listClick(WMWidget
* w
, void *data
)
356 _Panel
*panel
= (_Panel
*) data
;
357 int row
= WMGetListSelectedItemRow(w
);
359 WMSetTextFieldText(panel
->shoT
, panel
->shortcuts
[row
]);
362 static char *trimstr(char *str
)
371 while (isspace(p
[i
]) && i
> 0) {
379 static void showData(_Panel
* panel
)
384 for (i
= 0; i
< panel
->actionCount
; i
++) {
386 str
= GetStringForKey(keyOptions
[i
]);
387 if (panel
->shortcuts
[i
])
388 wfree(panel
->shortcuts
[i
]);
390 panel
->shortcuts
[i
] = trimstr(str
);
392 panel
->shortcuts
[i
] = NULL
;
394 if (panel
->shortcuts
[i
] &&
395 (strcasecmp(panel
->shortcuts
[i
], "none") == 0 || strlen(panel
->shortcuts
[i
]) == 0)) {
396 wfree(panel
->shortcuts
[i
]);
397 panel
->shortcuts
[i
] = NULL
;
400 WMRedisplayWidget(panel
->actLs
);
403 static void paintItem(WMList
* lPtr
, int index
, Drawable d
, char *text
, int state
, WMRect
* rect
)
405 int width
, height
, x
, y
;
406 _Panel
*panel
= (_Panel
*) WMGetHangedData(lPtr
);
407 WMScreen
*scr
= WMWidgetScreen(lPtr
);
408 Display
*dpy
= WMScreenDisplay(scr
);
409 WMColor
*backColor
= (state
& WLDSSelected
) ? panel
->white
: panel
->gray
;
411 width
= rect
->size
.width
;
412 height
= rect
->size
.height
;
416 XFillRectangle(dpy
, d
, WMColorGC(backColor
), x
, y
, width
, height
);
418 if (panel
->shortcuts
[index
]) {
419 WMPixmap
*pix
= WMGetSystemPixmap(scr
, WSICheckMark
);
420 WMSize size
= WMGetPixmapSize(pix
);
422 WMDrawPixmap(pix
, d
, x
+ (20 - size
.width
) / 2, (height
- size
.height
) / 2 + y
);
423 WMReleasePixmap(pix
);
426 WMDrawString(scr
, d
, panel
->black
, panel
->font
, x
+ 20, y
, text
, strlen(text
));
429 static void createPanel(Panel
* p
)
431 _Panel
*panel
= (_Panel
*) p
;
432 WMScreen
*scr
= WMWidgetScreen(panel
->parent
);
436 panel
->capturing
= 0;
438 panel
->white
= WMWhiteColor(scr
);
439 panel
->black
= WMBlackColor(scr
);
440 panel
->gray
= WMGrayColor(scr
);
441 panel
->font
= WMSystemFontOfSize(scr
, 12);
443 panel
->box
= WMCreateBox(panel
->parent
);
444 WMSetViewExpandsToParent(WMWidgetView(panel
->box
), 2, 2, 2, 2);
446 boldFont
= WMBoldSystemFontOfSize(scr
, 12);
448 /* **************** Actions **************** */
449 panel
->actL
= WMCreateLabel(panel
->box
);
450 WMResizeWidget(panel
->actL
, 280, 20);
451 WMMoveWidget(panel
->actL
, 20, 10);
452 WMSetLabelFont(panel
->actL
, boldFont
);
453 WMSetLabelText(panel
->actL
, _("Actions"));
454 WMSetLabelRelief(panel
->actL
, WRSunken
);
455 WMSetLabelTextAlignment(panel
->actL
, WACenter
);
456 color
= WMDarkGrayColor(scr
);
457 WMSetWidgetBackgroundColor(panel
->actL
, color
);
458 WMReleaseColor(color
);
459 WMSetLabelTextColor(panel
->actL
, panel
->white
);
461 panel
->actLs
= WMCreateList(panel
->box
);
462 WMResizeWidget(panel
->actLs
, 280, 190);
463 WMMoveWidget(panel
->actLs
, 20, 32);
464 WMSetListUserDrawProc(panel
->actLs
, paintItem
);
465 WMHangData(panel
->actLs
, panel
);
467 WMAddListItem(panel
->actLs
, _("Open applications menu"));
468 WMAddListItem(panel
->actLs
, _("Open window list menu"));
469 WMAddListItem(panel
->actLs
, _("Open window commands menu"));
470 WMAddListItem(panel
->actLs
, _("Hide active application"));
471 WMAddListItem(panel
->actLs
, _("Hide other applications"));
472 WMAddListItem(panel
->actLs
, _("Miniaturize active window"));
473 WMAddListItem(panel
->actLs
, _("Close active window"));
474 WMAddListItem(panel
->actLs
, _("Maximize active window"));
475 WMAddListItem(panel
->actLs
, _("Maximize active window vertically"));
476 WMAddListItem(panel
->actLs
, _("Maximize active window horizontally"));
477 WMAddListItem(panel
->actLs
, _("Maximize active window left half"));
478 WMAddListItem(panel
->actLs
, _("Maximize active window right half"));
479 WMAddListItem(panel
->actLs
, _("Maximus: Tiled maximization "));
480 WMAddListItem(panel
->actLs
, _("Raise active window"));
481 WMAddListItem(panel
->actLs
, _("Lower active window"));
482 WMAddListItem(panel
->actLs
, _("Raise/Lower window under mouse pointer"));
483 WMAddListItem(panel
->actLs
, _("Shade active window"));
484 WMAddListItem(panel
->actLs
, _("Move/Resize active window"));
485 WMAddListItem(panel
->actLs
, _("Select active window"));
486 WMAddListItem(panel
->actLs
, _("Focus next window"));
487 WMAddListItem(panel
->actLs
, _("Focus previous window"));
488 WMAddListItem(panel
->actLs
, _("Focus next group window"));
489 WMAddListItem(panel
->actLs
, _("Focus previous group window"));
490 WMAddListItem(panel
->actLs
, _("Switch to next workspace"));
491 WMAddListItem(panel
->actLs
, _("Switch to previous workspace"));
492 WMAddListItem(panel
->actLs
, _("Switch to next ten workspaces"));
493 WMAddListItem(panel
->actLs
, _("Switch to previous ten workspaces"));
494 WMAddListItem(panel
->actLs
, _("Switch to workspace 1"));
495 WMAddListItem(panel
->actLs
, _("Switch to workspace 2"));
496 WMAddListItem(panel
->actLs
, _("Switch to workspace 3"));
497 WMAddListItem(panel
->actLs
, _("Switch to workspace 4"));
498 WMAddListItem(panel
->actLs
, _("Switch to workspace 5"));
499 WMAddListItem(panel
->actLs
, _("Switch to workspace 6"));
500 WMAddListItem(panel
->actLs
, _("Switch to workspace 7"));
501 WMAddListItem(panel
->actLs
, _("Switch to workspace 8"));
502 WMAddListItem(panel
->actLs
, _("Switch to workspace 9"));
503 WMAddListItem(panel
->actLs
, _("Switch to workspace 10"));
504 WMAddListItem(panel
->actLs
, _("Shortcut for window 1"));
505 WMAddListItem(panel
->actLs
, _("Shortcut for window 2"));
506 WMAddListItem(panel
->actLs
, _("Shortcut for window 3"));
507 WMAddListItem(panel
->actLs
, _("Shortcut for window 4"));
508 WMAddListItem(panel
->actLs
, _("Shortcut for window 5"));
509 WMAddListItem(panel
->actLs
, _("Shortcut for window 6"));
510 WMAddListItem(panel
->actLs
, _("Shortcut for window 7"));
511 WMAddListItem(panel
->actLs
, _("Shortcut for window 8"));
512 WMAddListItem(panel
->actLs
, _("Shortcut for window 9"));
513 WMAddListItem(panel
->actLs
, _("Shortcut for window 10"));
514 WMAddListItem(panel
->actLs
, _("Switch to Next Screen/Monitor"));
515 WMAddListItem(panel
->actLs
, _("Raise/Lower Dock"));
516 WMAddListItem(panel
->actLs
, _("Raise/Lower Clip"));
518 WMAddListItem(panel
->actLs
, _("Toggle keyboard language"));
519 #endif /* XKB_MODELOCK */
521 WMSetListAction(panel
->actLs
, listClick
, panel
);
523 panel
->actionCount
= WMGetListNumberOfRows(panel
->actLs
);
524 panel
->shortcuts
= wmalloc(sizeof(char *) * panel
->actionCount
);
525 memset(panel
->shortcuts
, 0, sizeof(char *) * panel
->actionCount
);
527 /***************** Shortcut ****************/
529 panel
->shoF
= WMCreateFrame(panel
->box
);
530 WMResizeWidget(panel
->shoF
, 190, 210);
531 WMMoveWidget(panel
->shoF
, 315, 10);
532 WMSetFrameTitle(panel
->shoF
, _("Shortcut"));
534 panel
->shoT
= WMCreateTextField(panel
->shoF
);
535 WMResizeWidget(panel
->shoT
, 160, 20);
536 WMMoveWidget(panel
->shoT
, 15, 65);
537 WMAddNotificationObserver(typedKeys
, panel
, WMTextDidChangeNotification
, panel
->shoT
);
539 panel
->cleB
= WMCreateCommandButton(panel
->shoF
);
540 WMResizeWidget(panel
->cleB
, 75, 24);
541 WMMoveWidget(panel
->cleB
, 15, 95);
542 WMSetButtonText(panel
->cleB
, _("Clear"));
543 WMSetButtonAction(panel
->cleB
, clearShortcut
, panel
);
545 panel
->defB
= WMCreateCommandButton(panel
->shoF
);
546 WMResizeWidget(panel
->defB
, 75, 24);
547 WMMoveWidget(panel
->defB
, 100, 95);
548 WMSetButtonText(panel
->defB
, _("Capture"));
549 WMSetButtonAction(panel
->defB
, captureClick
, panel
);
551 panel
->instructionsL
= WMCreateLabel(panel
->shoF
);
552 WMResizeWidget(panel
->instructionsL
, 160, 55);
553 WMMoveWidget(panel
->instructionsL
, 15, 140);
554 WMSetLabelTextAlignment(panel
->instructionsL
, WACenter
);
555 WMSetLabelWraps(panel
->instructionsL
, True
);
556 WMSetLabelText(panel
->instructionsL
, _("Click on Capture to interactively define the shortcut key."));
558 WMMapSubwidgets(panel
->shoF
);
560 WMReleaseFont(boldFont
);
562 WMRealizeWidget(panel
->box
);
563 WMMapSubwidgets(panel
->box
);
568 static void storeData(_Panel
* panel
)
573 for (i
= 0; i
< panel
->actionCount
; i
++) {
575 if (panel
->shortcuts
[i
]) {
576 str
= trimstr(panel
->shortcuts
[i
]);
577 if (strlen(str
) == 0) {
583 SetStringForKey(str
, keyOptions
[i
]);
586 SetStringForKey("None", keyOptions
[i
]);
591 Panel
*InitKeyboardShortcuts(WMScreen
* scr
, WMWidget
* parent
)
595 panel
= wmalloc(sizeof(_Panel
));
596 memset(panel
, 0, sizeof(_Panel
));
598 panel
->sectionName
= _("Keyboard Shortcut Preferences");
600 panel
->description
= _("Change the keyboard shortcuts for actions such\n"
601 "as changing workspaces and opening menus.");
603 panel
->parent
= parent
;
605 panel
->callbacks
.createWidgets
= createPanel
;
606 panel
->callbacks
.updateDomain
= storeData
;
608 AddSection(panel
, ICON_FILE
);