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",
116 #ifdef VIRTUAL_DESKTOP
117 "VirtualEdgeLeftKey",
118 "VirtualEdgeRightKey",
120 "VirtualEdgeDownKey",
128 #endif /* XKB_MODELOCK */
131 #ifndef HAVE_XCONVERTCASE
134 static void XConvertCase(register KeySym sym
, KeySym
* lower
, KeySym
* upper
)
139 case 0: /* Latin 1 */
140 if ((sym
>= XK_A
) && (sym
<= XK_Z
))
141 *lower
+= (XK_a
- XK_A
);
142 else if ((sym
>= XK_a
) && (sym
<= XK_z
))
143 *upper
-= (XK_a
- XK_A
);
144 else if ((sym
>= XK_Agrave
) && (sym
<= XK_Odiaeresis
))
145 *lower
+= (XK_agrave
- XK_Agrave
);
146 else if ((sym
>= XK_agrave
) && (sym
<= XK_odiaeresis
))
147 *upper
-= (XK_agrave
- XK_Agrave
);
148 else if ((sym
>= XK_Ooblique
) && (sym
<= XK_Thorn
))
149 *lower
+= (XK_oslash
- XK_Ooblique
);
150 else if ((sym
>= XK_oslash
) && (sym
<= XK_thorn
))
151 *upper
-= (XK_oslash
- XK_Ooblique
);
153 case 1: /* Latin 2 */
154 /* Assume the KeySym is a legal value (ignore discontinuities) */
155 if (sym
== XK_Aogonek
)
157 else if (sym
>= XK_Lstroke
&& sym
<= XK_Sacute
)
158 *lower
+= (XK_lstroke
- XK_Lstroke
);
159 else if (sym
>= XK_Scaron
&& sym
<= XK_Zacute
)
160 *lower
+= (XK_scaron
- XK_Scaron
);
161 else if (sym
>= XK_Zcaron
&& sym
<= XK_Zabovedot
)
162 *lower
+= (XK_zcaron
- XK_Zcaron
);
163 else if (sym
== XK_aogonek
)
165 else if (sym
>= XK_lstroke
&& sym
<= XK_sacute
)
166 *upper
-= (XK_lstroke
- XK_Lstroke
);
167 else if (sym
>= XK_scaron
&& sym
<= XK_zacute
)
168 *upper
-= (XK_scaron
- XK_Scaron
);
169 else if (sym
>= XK_zcaron
&& sym
<= XK_zabovedot
)
170 *upper
-= (XK_zcaron
- XK_Zcaron
);
171 else if (sym
>= XK_Racute
&& sym
<= XK_Tcedilla
)
172 *lower
+= (XK_racute
- XK_Racute
);
173 else if (sym
>= XK_racute
&& sym
<= XK_tcedilla
)
174 *upper
-= (XK_racute
- XK_Racute
);
176 case 2: /* Latin 3 */
177 /* Assume the KeySym is a legal value (ignore discontinuities) */
178 if (sym
>= XK_Hstroke
&& sym
<= XK_Hcircumflex
)
179 *lower
+= (XK_hstroke
- XK_Hstroke
);
180 else if (sym
>= XK_Gbreve
&& sym
<= XK_Jcircumflex
)
181 *lower
+= (XK_gbreve
- XK_Gbreve
);
182 else if (sym
>= XK_hstroke
&& sym
<= XK_hcircumflex
)
183 *upper
-= (XK_hstroke
- XK_Hstroke
);
184 else if (sym
>= XK_gbreve
&& sym
<= XK_jcircumflex
)
185 *upper
-= (XK_gbreve
- XK_Gbreve
);
186 else if (sym
>= XK_Cabovedot
&& sym
<= XK_Scircumflex
)
187 *lower
+= (XK_cabovedot
- XK_Cabovedot
);
188 else if (sym
>= XK_cabovedot
&& sym
<= XK_scircumflex
)
189 *upper
-= (XK_cabovedot
- XK_Cabovedot
);
191 case 3: /* Latin 4 */
192 /* Assume the KeySym is a legal value (ignore discontinuities) */
193 if (sym
>= XK_Rcedilla
&& sym
<= XK_Tslash
)
194 *lower
+= (XK_rcedilla
- XK_Rcedilla
);
195 else if (sym
>= XK_rcedilla
&& sym
<= XK_tslash
)
196 *upper
-= (XK_rcedilla
- XK_Rcedilla
);
197 else if (sym
== XK_ENG
)
199 else if (sym
== XK_eng
)
201 else if (sym
>= XK_Amacron
&& sym
<= XK_Umacron
)
202 *lower
+= (XK_amacron
- XK_Amacron
);
203 else if (sym
>= XK_amacron
&& sym
<= XK_umacron
)
204 *upper
-= (XK_amacron
- XK_Amacron
);
206 case 6: /* Cyrillic */
207 /* Assume the KeySym is a legal value (ignore discontinuities) */
208 if (sym
>= XK_Serbian_DJE
&& sym
<= XK_Serbian_DZE
)
209 *lower
-= (XK_Serbian_DJE
- XK_Serbian_dje
);
210 else if (sym
>= XK_Serbian_dje
&& sym
<= XK_Serbian_dze
)
211 *upper
+= (XK_Serbian_DJE
- XK_Serbian_dje
);
212 else if (sym
>= XK_Cyrillic_YU
&& sym
<= XK_Cyrillic_HARDSIGN
)
213 *lower
-= (XK_Cyrillic_YU
- XK_Cyrillic_yu
);
214 else if (sym
>= XK_Cyrillic_yu
&& sym
<= XK_Cyrillic_hardsign
)
215 *upper
+= (XK_Cyrillic_YU
- XK_Cyrillic_yu
);
218 /* Assume the KeySym is a legal value (ignore discontinuities) */
219 if (sym
>= XK_Greek_ALPHAaccent
&& sym
<= XK_Greek_OMEGAaccent
)
220 *lower
+= (XK_Greek_alphaaccent
- XK_Greek_ALPHAaccent
);
221 else if (sym
>= XK_Greek_alphaaccent
&& sym
<= XK_Greek_omegaaccent
&&
222 sym
!= XK_Greek_iotaaccentdieresis
&& sym
!= XK_Greek_upsilonaccentdieresis
)
223 *upper
-= (XK_Greek_alphaaccent
- XK_Greek_ALPHAaccent
);
224 else if (sym
>= XK_Greek_ALPHA
&& sym
<= XK_Greek_OMEGA
)
225 *lower
+= (XK_Greek_alpha
- XK_Greek_ALPHA
);
226 else if (sym
>= XK_Greek_alpha
&& sym
<= XK_Greek_omega
&& sym
!= XK_Greek_finalsmallsigma
)
227 *upper
-= (XK_Greek_alpha
- XK_Greek_ALPHA
);
229 case 0x14: /* Armenian */
230 if (sym
>= XK_Armenian_AYB
&& sym
<= XK_Armenian_fe
) {
239 static char *captureShortcut(Display
* dpy
, _Panel
* panel
)
242 KeySym ksym
, lksym
, uksym
;
246 while (panel
->capturing
) {
247 XAllowEvents(dpy
, AsyncKeyboard
, CurrentTime
);
248 WMNextEvent(dpy
, &ev
);
249 if (ev
.type
== KeyPress
&& ev
.xkey
.keycode
!= 0) {
250 ksym
= XKeycodeToKeysym(dpy
, ev
.xkey
.keycode
, 0);
251 if (!IsModifierKey(ksym
)) {
252 XConvertCase(ksym
, &lksym
, &uksym
);
253 key
= XKeysymToString(uksym
);
255 panel
->capturing
= 0;
267 if (ev
.xkey
.state
& ControlMask
) {
268 strcat(buffer
, "Control+");
270 if (ev
.xkey
.state
& ShiftMask
) {
271 strcat(buffer
, "Shift+");
273 if (ev
.xkey
.state
& Mod1Mask
) {
274 strcat(buffer
, "Mod1+");
276 if (ev
.xkey
.state
& Mod2Mask
) {
277 strcat(buffer
, "Mod2+");
279 if (ev
.xkey
.state
& Mod3Mask
) {
280 strcat(buffer
, "Mod3+");
282 if (ev
.xkey
.state
& Mod4Mask
) {
283 strcat(buffer
, "Mod4+");
285 if (ev
.xkey
.state
& Mod5Mask
) {
286 strcat(buffer
, "Mod5+");
290 return wstrdup(buffer
);
293 static void captureClick(WMWidget
* w
, void *data
)
295 _Panel
*panel
= (_Panel
*) data
;
296 Display
*dpy
= WMScreenDisplay(WMWidgetScreen(panel
->parent
));
299 if (!panel
->capturing
) {
300 panel
->capturing
= 1;
301 WMSetButtonText(w
, _("Cancel"));
302 WMSetLabelText(panel
->instructionsL
,
303 _("Press the desired shortcut key(s) or click Cancel to stop capturing."));
304 XGrabKeyboard(dpy
, WMWidgetXID(panel
->parent
), True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
);
305 shortcut
= captureShortcut(dpy
, panel
);
307 int row
= WMGetListSelectedItemRow(panel
->actLs
);
309 WMSetTextFieldText(panel
->shoT
, shortcut
);
311 if (panel
->shortcuts
[row
])
312 wfree(panel
->shortcuts
[row
]);
313 panel
->shortcuts
[row
] = shortcut
;
315 WMRedisplayWidget(panel
->actLs
);
321 panel
->capturing
= 0;
322 WMSetButtonText(w
, _("Capture"));
323 WMSetLabelText(panel
->instructionsL
, _("Click on Capture to interactively define the shortcut key."));
324 XUngrabKeyboard(dpy
, CurrentTime
);
327 static void clearShortcut(WMWidget
* w
, void *data
)
329 _Panel
*panel
= (_Panel
*) data
;
330 int row
= WMGetListSelectedItemRow(panel
->actLs
);
332 WMSetTextFieldText(panel
->shoT
, NULL
);
335 if (panel
->shortcuts
[row
])
336 wfree(panel
->shortcuts
[row
]);
337 panel
->shortcuts
[row
] = NULL
;
338 WMRedisplayWidget(panel
->actLs
);
342 static void typedKeys(void *observerData
, WMNotification
* notification
)
344 _Panel
*panel
= (_Panel
*) observerData
;
345 int row
= WMGetListSelectedItemRow(panel
->actLs
);
350 if (panel
->shortcuts
[row
])
351 wfree(panel
->shortcuts
[row
]);
352 panel
->shortcuts
[row
] = WMGetTextFieldText(panel
->shoT
);
353 if (strlen(panel
->shortcuts
[row
]) == 0) {
354 wfree(panel
->shortcuts
[row
]);
355 panel
->shortcuts
[row
] = NULL
;
357 WMRedisplayWidget(panel
->actLs
);
360 static void listClick(WMWidget
* w
, void *data
)
362 _Panel
*panel
= (_Panel
*) data
;
363 int row
= WMGetListSelectedItemRow(w
);
365 WMSetTextFieldText(panel
->shoT
, panel
->shortcuts
[row
]);
368 static char *trimstr(char *str
)
377 while (isspace(p
[i
]) && i
> 0) {
385 static void showData(_Panel
* panel
)
390 for (i
= 0; i
< panel
->actionCount
; i
++) {
392 str
= GetStringForKey(keyOptions
[i
]);
393 if (panel
->shortcuts
[i
])
394 wfree(panel
->shortcuts
[i
]);
396 panel
->shortcuts
[i
] = trimstr(str
);
398 panel
->shortcuts
[i
] = NULL
;
400 if (panel
->shortcuts
[i
] &&
401 (strcasecmp(panel
->shortcuts
[i
], "none") == 0 || strlen(panel
->shortcuts
[i
]) == 0)) {
402 wfree(panel
->shortcuts
[i
]);
403 panel
->shortcuts
[i
] = NULL
;
406 WMRedisplayWidget(panel
->actLs
);
409 static void paintItem(WMList
* lPtr
, int index
, Drawable d
, char *text
, int state
, WMRect
* rect
)
411 int width
, height
, x
, y
;
412 _Panel
*panel
= (_Panel
*) WMGetHangedData(lPtr
);
413 WMScreen
*scr
= WMWidgetScreen(lPtr
);
414 Display
*dpy
= WMScreenDisplay(scr
);
415 WMColor
*backColor
= (state
& WLDSSelected
) ? panel
->white
: panel
->gray
;
417 width
= rect
->size
.width
;
418 height
= rect
->size
.height
;
422 XFillRectangle(dpy
, d
, WMColorGC(backColor
), x
, y
, width
, height
);
424 if (panel
->shortcuts
[index
]) {
425 WMPixmap
*pix
= WMGetSystemPixmap(scr
, WSICheckMark
);
426 WMSize size
= WMGetPixmapSize(pix
);
428 WMDrawPixmap(pix
, d
, x
+ (20 - size
.width
) / 2, (height
- size
.height
) / 2 + y
);
429 WMReleasePixmap(pix
);
432 WMDrawString(scr
, d
, panel
->black
, panel
->font
, x
+ 20, y
, text
, strlen(text
));
435 static void createPanel(Panel
* p
)
437 _Panel
*panel
= (_Panel
*) p
;
438 WMScreen
*scr
= WMWidgetScreen(panel
->parent
);
442 panel
->capturing
= 0;
444 panel
->white
= WMWhiteColor(scr
);
445 panel
->black
= WMBlackColor(scr
);
446 panel
->gray
= WMGrayColor(scr
);
447 panel
->font
= WMSystemFontOfSize(scr
, 12);
449 panel
->box
= WMCreateBox(panel
->parent
);
450 WMSetViewExpandsToParent(WMWidgetView(panel
->box
), 2, 2, 2, 2);
452 boldFont
= WMBoldSystemFontOfSize(scr
, 12);
454 /* **************** Actions **************** */
455 panel
->actL
= WMCreateLabel(panel
->box
);
456 WMResizeWidget(panel
->actL
, 280, 20);
457 WMMoveWidget(panel
->actL
, 20, 10);
458 WMSetLabelFont(panel
->actL
, boldFont
);
459 WMSetLabelText(panel
->actL
, _("Actions"));
460 WMSetLabelRelief(panel
->actL
, WRSunken
);
461 WMSetLabelTextAlignment(panel
->actL
, WACenter
);
462 color
= WMDarkGrayColor(scr
);
463 WMSetWidgetBackgroundColor(panel
->actL
, color
);
464 WMReleaseColor(color
);
465 WMSetLabelTextColor(panel
->actL
, panel
->white
);
467 panel
->actLs
= WMCreateList(panel
->box
);
468 WMResizeWidget(panel
->actLs
, 280, 190);
469 WMMoveWidget(panel
->actLs
, 20, 32);
470 WMSetListUserDrawProc(panel
->actLs
, paintItem
);
471 WMHangData(panel
->actLs
, panel
);
473 WMAddListItem(panel
->actLs
, _("Open applications menu"));
474 WMAddListItem(panel
->actLs
, _("Open window list menu"));
475 WMAddListItem(panel
->actLs
, _("Open window commands menu"));
476 WMAddListItem(panel
->actLs
, _("Hide active application"));
477 WMAddListItem(panel
->actLs
, _("Hide other applications"));
478 WMAddListItem(panel
->actLs
, _("Miniaturize active window"));
479 WMAddListItem(panel
->actLs
, _("Close active window"));
480 WMAddListItem(panel
->actLs
, _("Maximize active window"));
481 WMAddListItem(panel
->actLs
, _("Maximize active window vertically"));
482 WMAddListItem(panel
->actLs
, _("Maximize active window horizontally"));
483 WMAddListItem(panel
->actLs
, _("Maximize active window left half"));
484 WMAddListItem(panel
->actLs
, _("Maximize active window right half"));
485 WMAddListItem(panel
->actLs
, _("Maximus: Tiled maximization "));
486 WMAddListItem(panel
->actLs
, _("Raise active window"));
487 WMAddListItem(panel
->actLs
, _("Lower active window"));
488 WMAddListItem(panel
->actLs
, _("Raise/Lower window under mouse pointer"));
489 WMAddListItem(panel
->actLs
, _("Shade active window"));
490 WMAddListItem(panel
->actLs
, _("Move/Resize active window"));
491 WMAddListItem(panel
->actLs
, _("Select active window"));
492 WMAddListItem(panel
->actLs
, _("Focus next window"));
493 WMAddListItem(panel
->actLs
, _("Focus previous window"));
494 WMAddListItem(panel
->actLs
, _("Focus next group window"));
495 WMAddListItem(panel
->actLs
, _("Focus previous group window"));
496 WMAddListItem(panel
->actLs
, _("Switch to next workspace"));
497 WMAddListItem(panel
->actLs
, _("Switch to previous workspace"));
498 WMAddListItem(panel
->actLs
, _("Switch to next ten workspaces"));
499 WMAddListItem(panel
->actLs
, _("Switch to previous ten workspaces"));
500 WMAddListItem(panel
->actLs
, _("Switch to workspace 1"));
501 WMAddListItem(panel
->actLs
, _("Switch to workspace 2"));
502 WMAddListItem(panel
->actLs
, _("Switch to workspace 3"));
503 WMAddListItem(panel
->actLs
, _("Switch to workspace 4"));
504 WMAddListItem(panel
->actLs
, _("Switch to workspace 5"));
505 WMAddListItem(panel
->actLs
, _("Switch to workspace 6"));
506 WMAddListItem(panel
->actLs
, _("Switch to workspace 7"));
507 WMAddListItem(panel
->actLs
, _("Switch to workspace 8"));
508 WMAddListItem(panel
->actLs
, _("Switch to workspace 9"));
509 WMAddListItem(panel
->actLs
, _("Switch to workspace 10"));
510 WMAddListItem(panel
->actLs
, _("Shortcut for window 1"));
511 WMAddListItem(panel
->actLs
, _("Shortcut for window 2"));
512 WMAddListItem(panel
->actLs
, _("Shortcut for window 3"));
513 WMAddListItem(panel
->actLs
, _("Shortcut for window 4"));
514 WMAddListItem(panel
->actLs
, _("Shortcut for window 5"));
515 WMAddListItem(panel
->actLs
, _("Shortcut for window 6"));
516 WMAddListItem(panel
->actLs
, _("Shortcut for window 7"));
517 WMAddListItem(panel
->actLs
, _("Shortcut for window 8"));
518 WMAddListItem(panel
->actLs
, _("Shortcut for window 9"));
519 WMAddListItem(panel
->actLs
, _("Shortcut for window 10"));
520 WMAddListItem(panel
->actLs
, _("Switch to Next Screen/Monitor"));
521 #ifdef VIRTUAL_DESKTOP
522 WMAddListItem(panel
->actLs
, _("Move VirtualDesktop to next left edge"));
523 WMAddListItem(panel
->actLs
, _("Move VirtualDesktop to next right edge"));
524 WMAddListItem(panel
->actLs
, _("Move VirtualDesktop to next top edge"));
525 WMAddListItem(panel
->actLs
, _("Move VirtualDesktop to next bottom edge"));
527 WMAddListItem(panel
->actLs
, _("Raise/Lower Dock"));
528 WMAddListItem(panel
->actLs
, _("Raise/Lower Clip"));
530 WMAddListItem(panel
->actLs
, _("Toggle keyboard language"));
531 #endif /* XKB_MODELOCK */
533 WMSetListAction(panel
->actLs
, listClick
, panel
);
535 panel
->actionCount
= WMGetListNumberOfRows(panel
->actLs
);
536 panel
->shortcuts
= wmalloc(sizeof(char *) * panel
->actionCount
);
537 memset(panel
->shortcuts
, 0, sizeof(char *) * panel
->actionCount
);
539 /***************** Shortcut ****************/
541 panel
->shoF
= WMCreateFrame(panel
->box
);
542 WMResizeWidget(panel
->shoF
, 190, 210);
543 WMMoveWidget(panel
->shoF
, 315, 10);
544 WMSetFrameTitle(panel
->shoF
, _("Shortcut"));
546 panel
->shoT
= WMCreateTextField(panel
->shoF
);
547 WMResizeWidget(panel
->shoT
, 160, 20);
548 WMMoveWidget(panel
->shoT
, 15, 65);
549 WMAddNotificationObserver(typedKeys
, panel
, WMTextDidChangeNotification
, panel
->shoT
);
551 panel
->cleB
= WMCreateCommandButton(panel
->shoF
);
552 WMResizeWidget(panel
->cleB
, 75, 24);
553 WMMoveWidget(panel
->cleB
, 15, 95);
554 WMSetButtonText(panel
->cleB
, _("Clear"));
555 WMSetButtonAction(panel
->cleB
, clearShortcut
, panel
);
557 panel
->defB
= WMCreateCommandButton(panel
->shoF
);
558 WMResizeWidget(panel
->defB
, 75, 24);
559 WMMoveWidget(panel
->defB
, 100, 95);
560 WMSetButtonText(panel
->defB
, _("Capture"));
561 WMSetButtonAction(panel
->defB
, captureClick
, panel
);
563 panel
->instructionsL
= WMCreateLabel(panel
->shoF
);
564 WMResizeWidget(panel
->instructionsL
, 160, 55);
565 WMMoveWidget(panel
->instructionsL
, 15, 140);
566 WMSetLabelTextAlignment(panel
->instructionsL
, WACenter
);
567 WMSetLabelWraps(panel
->instructionsL
, True
);
568 WMSetLabelText(panel
->instructionsL
, _("Click on Capture to interactively define the shortcut key."));
570 WMMapSubwidgets(panel
->shoF
);
572 WMReleaseFont(boldFont
);
574 WMRealizeWidget(panel
->box
);
575 WMMapSubwidgets(panel
->box
);
580 static void storeData(_Panel
* panel
)
585 for (i
= 0; i
< panel
->actionCount
; i
++) {
587 if (panel
->shortcuts
[i
]) {
588 str
= trimstr(panel
->shortcuts
[i
]);
589 if (strlen(str
) == 0) {
595 SetStringForKey(str
, keyOptions
[i
]);
598 SetStringForKey("None", keyOptions
[i
]);
603 Panel
*InitKeyboardShortcuts(WMScreen
* scr
, WMWidget
* parent
)
607 panel
= wmalloc(sizeof(_Panel
));
608 memset(panel
, 0, sizeof(_Panel
));
610 panel
->sectionName
= _("Keyboard Shortcut Preferences");
612 panel
->description
= _("Change the keyboard shortcuts for actions such\n"
613 "as changing workspaces and opening menus.");
615 panel
->parent
= parent
;
617 panel
->callbacks
.createWidgets
= createPanel
;
618 panel
->callbacks
.updateDomain
= storeData
;
620 AddSection(panel
, ICON_FILE
);