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"
63 /* must be in the same order as the corresponding items in actions list */
64 static char *keyOptions
[] = {
88 "NextWorkspaceLayerKey",
89 "PrevWorkspaceLayerKey",
100 "WindowShortcut1Key",
101 "WindowShortcut2Key",
102 "WindowShortcut3Key",
103 "WindowShortcut4Key",
104 "WindowShortcut5Key",
105 "WindowShortcut6Key",
106 "WindowShortcut7Key",
107 "WindowShortcut8Key",
108 "WindowShortcut9Key",
109 "WindowShortcut10Key",
111 #ifdef VIRTUAL_DESKTOP
112 "VirtualEdgeLeftKey",
113 "VirtualEdgeRightKey",
115 "VirtualEdgeDownKey",
124 #endif /* XKB_MODELOCK */
127 #ifndef HAVE_XCONVERTCASE
130 static void XConvertCase(register KeySym sym
, KeySym
* lower
, KeySym
* upper
)
135 case 0: /* Latin 1 */
136 if ((sym
>= XK_A
) && (sym
<= XK_Z
))
137 *lower
+= (XK_a
- XK_A
);
138 else if ((sym
>= XK_a
) && (sym
<= XK_z
))
139 *upper
-= (XK_a
- XK_A
);
140 else if ((sym
>= XK_Agrave
) && (sym
<= XK_Odiaeresis
))
141 *lower
+= (XK_agrave
- XK_Agrave
);
142 else if ((sym
>= XK_agrave
) && (sym
<= XK_odiaeresis
))
143 *upper
-= (XK_agrave
- XK_Agrave
);
144 else if ((sym
>= XK_Ooblique
) && (sym
<= XK_Thorn
))
145 *lower
+= (XK_oslash
- XK_Ooblique
);
146 else if ((sym
>= XK_oslash
) && (sym
<= XK_thorn
))
147 *upper
-= (XK_oslash
- XK_Ooblique
);
149 case 1: /* Latin 2 */
150 /* Assume the KeySym is a legal value (ignore discontinuities) */
151 if (sym
== XK_Aogonek
)
153 else if (sym
>= XK_Lstroke
&& sym
<= XK_Sacute
)
154 *lower
+= (XK_lstroke
- XK_Lstroke
);
155 else if (sym
>= XK_Scaron
&& sym
<= XK_Zacute
)
156 *lower
+= (XK_scaron
- XK_Scaron
);
157 else if (sym
>= XK_Zcaron
&& sym
<= XK_Zabovedot
)
158 *lower
+= (XK_zcaron
- XK_Zcaron
);
159 else if (sym
== XK_aogonek
)
161 else if (sym
>= XK_lstroke
&& sym
<= XK_sacute
)
162 *upper
-= (XK_lstroke
- XK_Lstroke
);
163 else if (sym
>= XK_scaron
&& sym
<= XK_zacute
)
164 *upper
-= (XK_scaron
- XK_Scaron
);
165 else if (sym
>= XK_zcaron
&& sym
<= XK_zabovedot
)
166 *upper
-= (XK_zcaron
- XK_Zcaron
);
167 else if (sym
>= XK_Racute
&& sym
<= XK_Tcedilla
)
168 *lower
+= (XK_racute
- XK_Racute
);
169 else if (sym
>= XK_racute
&& sym
<= XK_tcedilla
)
170 *upper
-= (XK_racute
- XK_Racute
);
172 case 2: /* Latin 3 */
173 /* Assume the KeySym is a legal value (ignore discontinuities) */
174 if (sym
>= XK_Hstroke
&& sym
<= XK_Hcircumflex
)
175 *lower
+= (XK_hstroke
- XK_Hstroke
);
176 else if (sym
>= XK_Gbreve
&& sym
<= XK_Jcircumflex
)
177 *lower
+= (XK_gbreve
- XK_Gbreve
);
178 else if (sym
>= XK_hstroke
&& sym
<= XK_hcircumflex
)
179 *upper
-= (XK_hstroke
- XK_Hstroke
);
180 else if (sym
>= XK_gbreve
&& sym
<= XK_jcircumflex
)
181 *upper
-= (XK_gbreve
- XK_Gbreve
);
182 else if (sym
>= XK_Cabovedot
&& sym
<= XK_Scircumflex
)
183 *lower
+= (XK_cabovedot
- XK_Cabovedot
);
184 else if (sym
>= XK_cabovedot
&& sym
<= XK_scircumflex
)
185 *upper
-= (XK_cabovedot
- XK_Cabovedot
);
187 case 3: /* Latin 4 */
188 /* Assume the KeySym is a legal value (ignore discontinuities) */
189 if (sym
>= XK_Rcedilla
&& sym
<= XK_Tslash
)
190 *lower
+= (XK_rcedilla
- XK_Rcedilla
);
191 else if (sym
>= XK_rcedilla
&& sym
<= XK_tslash
)
192 *upper
-= (XK_rcedilla
- XK_Rcedilla
);
193 else if (sym
== XK_ENG
)
195 else if (sym
== XK_eng
)
197 else if (sym
>= XK_Amacron
&& sym
<= XK_Umacron
)
198 *lower
+= (XK_amacron
- XK_Amacron
);
199 else if (sym
>= XK_amacron
&& sym
<= XK_umacron
)
200 *upper
-= (XK_amacron
- XK_Amacron
);
202 case 6: /* Cyrillic */
203 /* Assume the KeySym is a legal value (ignore discontinuities) */
204 if (sym
>= XK_Serbian_DJE
&& sym
<= XK_Serbian_DZE
)
205 *lower
-= (XK_Serbian_DJE
- XK_Serbian_dje
);
206 else if (sym
>= XK_Serbian_dje
&& sym
<= XK_Serbian_dze
)
207 *upper
+= (XK_Serbian_DJE
- XK_Serbian_dje
);
208 else if (sym
>= XK_Cyrillic_YU
&& sym
<= XK_Cyrillic_HARDSIGN
)
209 *lower
-= (XK_Cyrillic_YU
- XK_Cyrillic_yu
);
210 else if (sym
>= XK_Cyrillic_yu
&& sym
<= XK_Cyrillic_hardsign
)
211 *upper
+= (XK_Cyrillic_YU
- XK_Cyrillic_yu
);
214 /* Assume the KeySym is a legal value (ignore discontinuities) */
215 if (sym
>= XK_Greek_ALPHAaccent
&& sym
<= XK_Greek_OMEGAaccent
)
216 *lower
+= (XK_Greek_alphaaccent
- XK_Greek_ALPHAaccent
);
217 else if (sym
>= XK_Greek_alphaaccent
&& sym
<= XK_Greek_omegaaccent
&&
218 sym
!= XK_Greek_iotaaccentdieresis
&& sym
!= XK_Greek_upsilonaccentdieresis
)
219 *upper
-= (XK_Greek_alphaaccent
- XK_Greek_ALPHAaccent
);
220 else if (sym
>= XK_Greek_ALPHA
&& sym
<= XK_Greek_OMEGA
)
221 *lower
+= (XK_Greek_alpha
- XK_Greek_ALPHA
);
222 else if (sym
>= XK_Greek_alpha
&& sym
<= XK_Greek_omega
&& sym
!= XK_Greek_finalsmallsigma
)
223 *upper
-= (XK_Greek_alpha
- XK_Greek_ALPHA
);
225 case 0x14: /* Armenian */
226 if (sym
>= XK_Armenian_AYB
&& sym
<= XK_Armenian_fe
) {
235 static char *captureShortcut(Display
* dpy
, _Panel
* panel
)
238 KeySym ksym
, lksym
, uksym
;
242 while (panel
->capturing
) {
243 XAllowEvents(dpy
, AsyncKeyboard
, CurrentTime
);
244 WMNextEvent(dpy
, &ev
);
245 if (ev
.type
== KeyPress
&& ev
.xkey
.keycode
!= 0) {
246 ksym
= XKeycodeToKeysym(dpy
, ev
.xkey
.keycode
, 0);
247 if (!IsModifierKey(ksym
)) {
248 XConvertCase(ksym
, &lksym
, &uksym
);
249 key
= XKeysymToString(uksym
);
251 panel
->capturing
= 0;
263 if (ev
.xkey
.state
& ControlMask
) {
264 strcat(buffer
, "Control+");
266 if (ev
.xkey
.state
& ShiftMask
) {
267 strcat(buffer
, "Shift+");
269 if (ev
.xkey
.state
& Mod1Mask
) {
270 strcat(buffer
, "Mod1+");
272 if (ev
.xkey
.state
& Mod2Mask
) {
273 strcat(buffer
, "Mod2+");
275 if (ev
.xkey
.state
& Mod3Mask
) {
276 strcat(buffer
, "Mod3+");
278 if (ev
.xkey
.state
& Mod4Mask
) {
279 strcat(buffer
, "Mod4+");
281 if (ev
.xkey
.state
& Mod5Mask
) {
282 strcat(buffer
, "Mod5+");
286 return wstrdup(buffer
);
289 static void captureClick(WMWidget
* w
, void *data
)
291 _Panel
*panel
= (_Panel
*) data
;
292 Display
*dpy
= WMScreenDisplay(WMWidgetScreen(panel
->parent
));
295 if (!panel
->capturing
) {
296 panel
->capturing
= 1;
297 WMSetButtonText(w
, _("Cancel"));
298 WMSetLabelText(panel
->instructionsL
,
299 _("Press the desired shortcut key(s) or click Cancel to stop capturing."));
300 XGrabKeyboard(dpy
, WMWidgetXID(panel
->parent
), True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
);
301 shortcut
= captureShortcut(dpy
, panel
);
303 int row
= WMGetListSelectedItemRow(panel
->actLs
);
305 WMSetTextFieldText(panel
->shoT
, shortcut
);
307 if (panel
->shortcuts
[row
])
308 wfree(panel
->shortcuts
[row
]);
309 panel
->shortcuts
[row
] = shortcut
;
311 WMRedisplayWidget(panel
->actLs
);
317 panel
->capturing
= 0;
318 WMSetButtonText(w
, _("Capture"));
319 WMSetLabelText(panel
->instructionsL
, _("Click on Capture to interactively define the shortcut key."));
320 XUngrabKeyboard(dpy
, CurrentTime
);
323 static void clearShortcut(WMWidget
* w
, void *data
)
325 _Panel
*panel
= (_Panel
*) data
;
326 int row
= WMGetListSelectedItemRow(panel
->actLs
);
328 WMSetTextFieldText(panel
->shoT
, NULL
);
331 if (panel
->shortcuts
[row
])
332 wfree(panel
->shortcuts
[row
]);
333 panel
->shortcuts
[row
] = NULL
;
334 WMRedisplayWidget(panel
->actLs
);
338 static void typedKeys(void *observerData
, WMNotification
* notification
)
340 _Panel
*panel
= (_Panel
*) observerData
;
341 int row
= WMGetListSelectedItemRow(panel
->actLs
);
346 if (panel
->shortcuts
[row
])
347 wfree(panel
->shortcuts
[row
]);
348 panel
->shortcuts
[row
] = WMGetTextFieldText(panel
->shoT
);
349 if (strlen(panel
->shortcuts
[row
]) == 0) {
350 wfree(panel
->shortcuts
[row
]);
351 panel
->shortcuts
[row
] = NULL
;
353 WMRedisplayWidget(panel
->actLs
);
356 static void listClick(WMWidget
* w
, void *data
)
358 _Panel
*panel
= (_Panel
*) data
;
359 int row
= WMGetListSelectedItemRow(w
);
361 WMSetTextFieldText(panel
->shoT
, panel
->shortcuts
[row
]);
364 static char *trimstr(char *str
)
373 while (isspace(p
[i
]) && i
> 0) {
381 static void showData(_Panel
* panel
)
386 for (i
= 0; i
< panel
->actionCount
; i
++) {
388 str
= GetStringForKey(keyOptions
[i
]);
389 if (panel
->shortcuts
[i
])
390 wfree(panel
->shortcuts
[i
]);
392 panel
->shortcuts
[i
] = trimstr(str
);
394 panel
->shortcuts
[i
] = NULL
;
396 if (panel
->shortcuts
[i
] &&
397 (strcasecmp(panel
->shortcuts
[i
], "none") == 0 || strlen(panel
->shortcuts
[i
]) == 0)) {
398 wfree(panel
->shortcuts
[i
]);
399 panel
->shortcuts
[i
] = NULL
;
402 WMRedisplayWidget(panel
->actLs
);
405 static void paintItem(WMList
* lPtr
, int index
, Drawable d
, char *text
, int state
, WMRect
* rect
)
407 int width
, height
, x
, y
;
408 _Panel
*panel
= (_Panel
*) WMGetHangedData(lPtr
);
409 WMScreen
*scr
= WMWidgetScreen(lPtr
);
410 Display
*dpy
= WMScreenDisplay(scr
);
411 WMColor
*backColor
= (state
& WLDSSelected
) ? panel
->white
: panel
->gray
;
413 width
= rect
->size
.width
;
414 height
= rect
->size
.height
;
418 XFillRectangle(dpy
, d
, WMColorGC(backColor
), x
, y
, width
, height
);
420 if (panel
->shortcuts
[index
]) {
421 WMPixmap
*pix
= WMGetSystemPixmap(scr
, WSICheckMark
);
422 WMSize size
= WMGetPixmapSize(pix
);
424 WMDrawPixmap(pix
, d
, x
+ (20 - size
.width
) / 2, (height
- size
.height
) / 2 + y
);
425 WMReleasePixmap(pix
);
428 WMDrawString(scr
, d
, panel
->black
, panel
->font
, x
+ 20, y
, text
, strlen(text
));
431 static void createPanel(Panel
* p
)
433 _Panel
*panel
= (_Panel
*) p
;
434 WMScreen
*scr
= WMWidgetScreen(panel
->parent
);
438 panel
->capturing
= 0;
440 panel
->white
= WMWhiteColor(scr
);
441 panel
->black
= WMBlackColor(scr
);
442 panel
->gray
= WMGrayColor(scr
);
443 panel
->font
= WMSystemFontOfSize(scr
, 12);
445 panel
->box
= WMCreateBox(panel
->parent
);
446 WMSetViewExpandsToParent(WMWidgetView(panel
->box
), 2, 2, 2, 2);
448 boldFont
= WMBoldSystemFontOfSize(scr
, 12);
450 /* **************** Actions **************** */
451 panel
->actL
= WMCreateLabel(panel
->box
);
452 WMResizeWidget(panel
->actL
, 280, 20);
453 WMMoveWidget(panel
->actL
, 20, 10);
454 WMSetLabelFont(panel
->actL
, boldFont
);
455 WMSetLabelText(panel
->actL
, _("Actions"));
456 WMSetLabelRelief(panel
->actL
, WRSunken
);
457 WMSetLabelTextAlignment(panel
->actL
, WACenter
);
458 color
= WMDarkGrayColor(scr
);
459 WMSetWidgetBackgroundColor(panel
->actL
, color
);
460 WMReleaseColor(color
);
461 WMSetLabelTextColor(panel
->actL
, panel
->white
);
463 panel
->actLs
= WMCreateList(panel
->box
);
464 WMResizeWidget(panel
->actLs
, 280, 190);
465 WMMoveWidget(panel
->actLs
, 20, 32);
466 WMSetListUserDrawProc(panel
->actLs
, paintItem
);
467 WMHangData(panel
->actLs
, panel
);
469 WMAddListItem(panel
->actLs
, _("Open applications menu"));
470 WMAddListItem(panel
->actLs
, _("Open window list menu"));
471 WMAddListItem(panel
->actLs
, _("Open window commands menu"));
472 WMAddListItem(panel
->actLs
, _("Hide active application"));
473 WMAddListItem(panel
->actLs
, _("Hide other applications"));
474 WMAddListItem(panel
->actLs
, _("Miniaturize active window"));
475 WMAddListItem(panel
->actLs
, _("Close active window"));
476 WMAddListItem(panel
->actLs
, _("Maximize active window"));
477 WMAddListItem(panel
->actLs
, _("Maximize active window vertically"));
478 WMAddListItem(panel
->actLs
, _("Maximize active window horizontally"));
479 WMAddListItem(panel
->actLs
, _("Maximize active window left half"));
480 WMAddListItem(panel
->actLs
, _("Maximize active window right half"));
481 WMAddListItem(panel
->actLs
, _("Maximus: Tiled maximization "));
482 WMAddListItem(panel
->actLs
, _("Raise active window"));
483 WMAddListItem(panel
->actLs
, _("Lower active window"));
484 WMAddListItem(panel
->actLs
, _("Raise/Lower window under mouse pointer"));
485 WMAddListItem(panel
->actLs
, _("Shade active window"));
486 WMAddListItem(panel
->actLs
, _("Move/Resize active window"));
487 WMAddListItem(panel
->actLs
, _("Select active window"));
488 WMAddListItem(panel
->actLs
, _("Focus next window"));
489 WMAddListItem(panel
->actLs
, _("Focus previous 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 #ifdef VIRTUAL_DESKTOP
516 WMAddListItem(panel
->actLs
, _("Move VirtualDesktop to next left edge"));
517 WMAddListItem(panel
->actLs
, _("Move VirtualDesktop to next right edge"));
518 WMAddListItem(panel
->actLs
, _("Move VirtualDesktop to next top edge"));
519 WMAddListItem(panel
->actLs
, _("Move VirtualDesktop to next bottom edge"));
521 WMAddListItem(panel
->actLs
, _("Raise Clip"));
522 WMAddListItem(panel
->actLs
, _("Lower Clip"));
523 WMAddListItem(panel
->actLs
, _("Raise/Lower Clip"));
525 WMAddListItem(panel
->actLs
, _("Toggle keyboard language"));
526 #endif /* XKB_MODELOCK */
528 WMSetListAction(panel
->actLs
, listClick
, panel
);
530 panel
->actionCount
= WMGetListNumberOfRows(panel
->actLs
);
531 panel
->shortcuts
= wmalloc(sizeof(char *) * panel
->actionCount
);
532 memset(panel
->shortcuts
, 0, sizeof(char *) * panel
->actionCount
);
534 /***************** Shortcut ****************/
536 panel
->shoF
= WMCreateFrame(panel
->box
);
537 WMResizeWidget(panel
->shoF
, 190, 210);
538 WMMoveWidget(panel
->shoF
, 315, 10);
539 WMSetFrameTitle(panel
->shoF
, _("Shortcut"));
541 panel
->shoT
= WMCreateTextField(panel
->shoF
);
542 WMResizeWidget(panel
->shoT
, 160, 20);
543 WMMoveWidget(panel
->shoT
, 15, 65);
544 WMAddNotificationObserver(typedKeys
, panel
, WMTextDidChangeNotification
, panel
->shoT
);
546 panel
->cleB
= WMCreateCommandButton(panel
->shoF
);
547 WMResizeWidget(panel
->cleB
, 75, 24);
548 WMMoveWidget(panel
->cleB
, 15, 95);
549 WMSetButtonText(panel
->cleB
, _("Clear"));
550 WMSetButtonAction(panel
->cleB
, clearShortcut
, panel
);
552 panel
->defB
= WMCreateCommandButton(panel
->shoF
);
553 WMResizeWidget(panel
->defB
, 75, 24);
554 WMMoveWidget(panel
->defB
, 100, 95);
555 WMSetButtonText(panel
->defB
, _("Capture"));
556 WMSetButtonAction(panel
->defB
, captureClick
, panel
);
558 panel
->instructionsL
= WMCreateLabel(panel
->shoF
);
559 WMResizeWidget(panel
->instructionsL
, 160, 55);
560 WMMoveWidget(panel
->instructionsL
, 15, 140);
561 WMSetLabelTextAlignment(panel
->instructionsL
, WACenter
);
562 WMSetLabelWraps(panel
->instructionsL
, True
);
563 WMSetLabelText(panel
->instructionsL
, _("Click on Capture to interactively define the shortcut key."));
565 WMMapSubwidgets(panel
->shoF
);
567 WMReleaseFont(boldFont
);
569 WMRealizeWidget(panel
->box
);
570 WMMapSubwidgets(panel
->box
);
575 static void storeData(_Panel
* panel
)
580 for (i
= 0; i
< panel
->actionCount
; i
++) {
582 if (panel
->shortcuts
[i
]) {
583 str
= trimstr(panel
->shortcuts
[i
]);
584 if (strlen(str
) == 0) {
590 SetStringForKey(str
, keyOptions
[i
]);
593 SetStringForKey("None", keyOptions
[i
]);
598 Panel
*InitKeyboardShortcuts(WMScreen
* scr
, WMWidget
* parent
)
602 panel
= wmalloc(sizeof(_Panel
));
603 memset(panel
, 0, sizeof(_Panel
));
605 panel
->sectionName
= _("Keyboard Shortcut Preferences");
607 panel
->description
= _("Change the keyboard shortcuts for actions such\n"
608 "as changing workspaces and opening menus.");
610 panel
->parent
= parent
;
612 panel
->callbacks
.createWidgets
= createPanel
;
613 panel
->callbacks
.updateDomain
= storeData
;
615 AddSection(panel
, ICON_FILE
);