2 /* MouseSettings.c- mouse options (some are equivalent to xset)
4 * WPrefs - Window Maker Preferences Program
6 * Copyright (c) 1998-2003 Alfredo K. Kojima
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
26 #include <X11/Xutil.h>
30 /* double-click tester */
35 typedef struct _Panel {
42 CallbackRec callbacks;
65 WMPopUpButton *button1P;
66 WMPopUpButton *button2P;
67 WMPopUpButton *button3P;
68 WMPopUpButton *wheelP;
75 /**/ int maxThreshold;
79 #define ICON_FILE "mousesettings"
81 #define SPEED_ICON_FILE "mousespeed"
83 #define DELAY_ICON "timer%i"
84 #define DELAY_ICON_S "timer%is"
86 /* need access to the double click variables */
87 #include <WINGs/WINGsP.h>
89 static char *modifierNames[8];
91 static char *buttonActions[4];
93 static char *wheelActions[2];
95 #define DELAY(i) ((i)*75+170)
97 int ModifierFromKey(Display * dpy, char *key);
99 static void setMouseAccel(WMScreen * scr, float accel, int threshold)
106 XChangePointerControl(WMScreenDisplay(scr), True, True, n, d, threshold);
109 static void speedChange(WMWidget * w, void *data)
111 _Panel *panel = (_Panel *) data;
120 tmp = WMGetTextFieldText(panel->acceT);
121 if (sscanf(tmp, "%f", &accel) != 1 || accel < 0) {
122 WMRunAlertPanel(WMWidgetScreen(panel->acceT), GetWindow(panel),
124 _("Invalid mouse acceleration value. Must be a positive real value."),
125 _("OK"), NULL, NULL);
129 panel->acceleration = accel;
132 i = (int)WMGetSliderValue(panel->speedS);
134 panel->acceleration = 0.25 + (i * 0.25);
136 sprintf(buffer, "%.2f", 0.25 + (i * 0.25));
137 WMSetTextFieldText(panel->acceT, buffer);
140 tmp = WMGetTextFieldText(panel->threT);
141 if (sscanf(tmp, "%i", &threshold) != 1 || threshold < 0 || threshold > panel->maxThreshold) {
142 WMRunAlertPanel(WMWidgetScreen(panel->parent), GetWindow(panel), _("Error"),
144 ("Invalid mouse acceleration threshold value. Must be the number of pixels to travel before accelerating."),
145 _("OK"), NULL, NULL);
147 setMouseAccel(WMWidgetScreen(panel->parent), panel->acceleration, threshold);
152 static void returnPressed(void *observerData, WMNotification * notification)
154 _Panel *panel = (_Panel *) observerData;
156 speedChange(NULL, panel);
159 static void doubleClick(WMWidget * w, void *data)
161 _Panel *panel = (_Panel *) data;
163 extern _WINGsConfiguration WINGsConfiguration;
166 for (i = 0; i < 5; i++) {
167 if (panel->ddelaB[i] == w)
170 WINGsConfiguration.doubleClickDelay = DELAY(i);
172 sprintf(buffer, "%i", DELAY(i));
173 WMSetTextFieldText(panel->ddelaT, buffer);
176 static int getButtonAction(char *str)
181 if (strcasecmp(str, "None") == 0)
183 else if (strcasecmp(str, "OpenApplicationsMenu") == 0)
185 else if (strcasecmp(str, "OpenWindowListMenu") == 0)
187 else if (strcasecmp(str, "SelectWindows") == 0)
194 static int getWheelAction(char *str)
199 if (strcasecmp(str, "None") == 0)
201 else if (strcasecmp(str, "SwitchWorkspaces") == 0)
208 static void getMouseParameters(Display * dpy, float *accel, int *thre)
212 XGetPointerControl(dpy, &n, &d, thre);
214 *accel = (float)n / (float)d;
217 static void showData(_Panel * panel)
221 int a = -1, b = -1, c = -1, w = -1;
224 Display *dpy = WMScreenDisplay(WMWidgetScreen(panel->parent));
226 str = GetStringForKey("MouseLeftButtonAction");
227 i = getButtonAction(str);
231 wwarning(_("bad value %s for option %s"), str, "MouseLeftButtonAction");
236 WMSetPopUpButtonSelectedItem(panel->button1P, a);
238 str = GetStringForKey("MouseMiddleButtonAction");
239 i = getButtonAction(str);
243 wwarning(_("bad value %s for option %s"), str, "MouseMiddleButtonAction");
248 WMSetPopUpButtonSelectedItem(panel->button2P, b);
250 str = GetStringForKey("MouseRightButtonAction");
251 i = getButtonAction(str);
255 wwarning(_("bad value %s for option %s"), str, "MouseRightButtonAction");
260 WMSetPopUpButtonSelectedItem(panel->button3P, c);
262 str = GetStringForKey("MouseWheelAction");
263 i = getWheelAction(str);
267 wwarning(_("bad value %s for option %s"), str, "MouseWheelAction");
272 WMSetPopUpButtonSelectedItem(panel->wheelP, w);
274 WMSetButtonSelected(panel->disaB, GetBoolForKey("DisableWSMouseActions"));
276 /**/ getMouseParameters(dpy, &accel, &a);
277 panel->maxThreshold = WidthOfScreen(DefaultScreenOfDisplay(dpy));
278 if (a > panel->maxThreshold) {
279 panel->maxThreshold = a;
281 sprintf(buffer, "%i", a);
282 WMSetTextFieldText(panel->threT, buffer);
284 WMSetSliderValue(panel->speedS, (accel - 0.25) / 0.25);
286 panel->acceleration = accel;
287 sprintf(buffer, "%.2f", accel);
288 WMSetTextFieldText(panel->acceT, buffer);
290 /**/ b = GetIntegerForKey("DoubleClickTime");
291 /* find best match */
293 for (i = 0; i < 5; i++) {
298 WMPerformButtonClick(panel->ddelaB[a]);
299 sprintf(buffer, "%i", b);
300 WMSetTextFieldText(panel->ddelaT, buffer);
302 /**/ str = GetStringForKey("ModifierKey");
305 a = ModifierFromKey(dpy, str);
308 str = modifierNames[a];
311 for (i = 0; i < WMGetPopUpButtonNumberOfItems(panel->grabP); i++) {
312 if (strstr(WMGetPopUpButtonItem(panel->grabP, i), str)) {
313 WMSetPopUpButtonSelectedItem(panel->grabP, i);
321 sscanf(WMGetPopUpButtonItem(panel->grabP, 0), "%s", buffer);
322 WMSetPopUpButtonSelectedItem(panel->grabP, 0);
323 wwarning(_("modifier key %s for option ModifierKey was not recognized. Using %s as default"),
328 static void fillModifierPopUp(WMPopUpButton * pop)
330 XModifierKeymap *mapping;
331 Display *dpy = WMScreenDisplay(WMWidgetScreen(pop));
336 mapping = XGetModifierMapping(dpy);
338 if (!mapping || mapping->max_keypermod == 0) {
339 WMAddPopUpButtonItem(pop, "Mod1");
340 WMAddPopUpButtonItem(pop, "Mod2");
341 WMAddPopUpButtonItem(pop, "Mod3");
342 WMAddPopUpButtonItem(pop, "Mod4");
343 WMAddPopUpButtonItem(pop, "Mod5");
344 wwarning(_("could not retrieve keyboard modifier mapping"));
348 for (j = 0; j < 8; j++) {
358 memset(array, 0, sizeof(char *) * 8);
359 for (i = 0; i < mapping->max_keypermod; i++) {
360 idx = i + j * mapping->max_keypermod;
361 if (mapping->modifiermap[idx] != 0) {
363 for (l = 0; l < 4; l++) {
364 ksym = XKeycodeToKeysym(dpy, mapping->modifiermap[idx], l);
365 if (ksym != NoSymbol)
368 if (ksym != NoSymbol)
369 str = XKeysymToString(ksym);
372 if (str && !strstr(str, "_Lock") && !strstr(str, "Shift")
373 && !strstr(str, "Control")) {
374 array[a++] = wstrdup(str);
379 for (k = 0; k < a; k++) {
380 if (array[k] == NULL)
382 tmp = wstrdup(array[k]);
383 ptr = strstr(tmp, "_L");
386 ptr = strstr(tmp, "_R");
389 sprintf(buffer, "%s (%s)", modifierNames[j], tmp);
390 /*sprintf(buffer, "%s", tmp); */
391 WMAddPopUpButtonItem(pop, buffer);
392 for (i = k + 1; i < a; i++) {
393 if (array[i] == NULL)
395 if (strstr(array[i], tmp)) {
411 XFreeModifiermap(mapping);
414 static void createPanel(Panel * p)
416 _Panel *panel = (_Panel *) p;
417 WMScreen *scr = WMWidgetScreen(panel->parent);
428 panel->box = WMCreateBox(panel->parent);
429 WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
431 /**************** Mouse Speed ****************/
432 panel->speedF = WMCreateFrame(panel->box);
433 WMResizeWidget(panel->speedF, 245, 100);
434 WMMoveWidget(panel->speedF, 15, 5);
435 WMSetFrameTitle(panel->speedF, _("Mouse Speed"));
437 panel->speedL = WMCreateLabel(panel->speedF);
438 WMResizeWidget(panel->speedL, 40, 46);
439 WMMoveWidget(panel->speedL, 15, 14);
440 WMSetLabelImagePosition(panel->speedL, WIPImageOnly);
441 path = LocateImage(SPEED_ICON_FILE);
443 icon = WMCreateBlendedPixmapFromFile(scr, path, &color);
445 WMSetLabelImage(panel->speedL, icon);
446 WMReleasePixmap(icon);
448 wwarning(_("could not load icon %s"), path);
453 panel->speedS = WMCreateSlider(panel->speedF);
454 WMResizeWidget(panel->speedS, 160, 15);
455 WMMoveWidget(panel->speedS, 70, 35);
456 WMSetSliderMinValue(panel->speedS, 0);
457 WMSetSliderMaxValue(panel->speedS, 40);
458 WMSetSliderContinuous(panel->speedS, False);
459 WMSetSliderAction(panel->speedS, speedChange, panel);
461 panel->acceL = WMCreateLabel(panel->speedF);
462 WMResizeWidget(panel->acceL, 70, 16);
463 WMMoveWidget(panel->acceL, 10, 67);
464 WMSetLabelTextAlignment(panel->acceL, WARight);
465 WMSetLabelText(panel->acceL, _("Acceler.:"));
467 panel->acceT = WMCreateTextField(panel->speedF);
468 WMResizeWidget(panel->acceT, 40, 20);
469 WMMoveWidget(panel->acceT, 80, 65);
470 WMAddNotificationObserver(returnPressed, panel, WMTextDidEndEditingNotification, panel->acceT);
472 panel->threL = WMCreateLabel(panel->speedF);
473 WMResizeWidget(panel->threL, 80, 16);
474 WMMoveWidget(panel->threL, 120, 67);
475 WMSetLabelTextAlignment(panel->threL, WARight);
476 WMSetLabelText(panel->threL, _("Threshold:"));
478 panel->threT = WMCreateTextField(panel->speedF);
479 WMResizeWidget(panel->threT, 30, 20);
480 WMMoveWidget(panel->threT, 200, 65);
481 WMAddNotificationObserver(returnPressed, panel, WMTextDidEndEditingNotification, panel->threT);
483 WMMapSubwidgets(panel->speedF);
485 /***************** Doubleclick Delay ****************/
487 panel->ddelaF = WMCreateFrame(panel->box);
488 WMResizeWidget(panel->ddelaF, 245, 105);
489 WMMoveWidget(panel->ddelaF, 15, 115);
490 WMSetFrameTitle(panel->ddelaF, _("Double-Click Delay"));
492 buf1 = wmalloc(strlen(DELAY_ICON) + 2);
493 buf2 = wmalloc(strlen(DELAY_ICON_S) + 2);
495 for (i = 0; i < 5; i++) {
496 panel->ddelaB[i] = WMCreateCustomButton(panel->ddelaF, WBBStateChangeMask);
497 WMResizeWidget(panel->ddelaB[i], 25, 25);
498 WMMoveWidget(panel->ddelaB[i], 30 + (40 * i), 25);
499 WMSetButtonBordered(panel->ddelaB[i], False);
500 WMSetButtonImagePosition(panel->ddelaB[i], WIPImageOnly);
501 WMSetButtonAction(panel->ddelaB[i], doubleClick, panel);
503 WMGroupButtons(panel->ddelaB[0], panel->ddelaB[i]);
505 sprintf(buf1, DELAY_ICON, i + 1);
506 sprintf(buf2, DELAY_ICON_S, i + 1);
507 path = LocateImage(buf1);
509 icon = WMCreatePixmapFromFile(scr, path);
511 WMSetButtonImage(panel->ddelaB[i], icon);
512 WMReleasePixmap(icon);
514 wwarning(_("could not load icon file %s"), path);
518 path = LocateImage(buf2);
520 icon = WMCreatePixmapFromFile(scr, path);
522 WMSetButtonAltImage(panel->ddelaB[i], icon);
523 WMReleasePixmap(icon);
525 wwarning(_("could not load icon file %s"), path);
533 panel->tester = CreateDoubleTest(panel->ddelaF, _("Test"));
534 WMResizeWidget(panel->tester, 84, 29);
535 WMMoveWidget(panel->tester, 35, 60);
537 panel->ddelaT = WMCreateTextField(panel->ddelaF);
538 WMResizeWidget(panel->ddelaT, 40, 20);
539 WMMoveWidget(panel->ddelaT, 140, 65);
541 panel->ddelaL = WMCreateLabel(panel->ddelaF);
542 WMResizeWidget(panel->ddelaL, 40, 16);
543 WMMoveWidget(panel->ddelaL, 185, 70);
548 font = WMSystemFontOfSize(scr, 10);
549 color = WMDarkGrayColor(scr);
550 WMSetLabelTextColor(panel->ddelaL, color);
551 WMSetLabelFont(panel->ddelaL, font);
553 WMReleaseColor(color);
555 WMSetLabelText(panel->ddelaL, _("msec"));
557 WMMapSubwidgets(panel->ddelaF);
559 /* ************** Workspace Action Buttons **************** */
560 panel->menuF = WMCreateFrame(panel->box);
561 WMResizeWidget(panel->menuF, 240, 160);
562 WMMoveWidget(panel->menuF, 270, 5);
563 WMSetFrameTitle(panel->menuF, _("Workspace Mouse Actions"));
565 panel->disaB = WMCreateSwitchButton(panel->menuF);
566 WMResizeWidget(panel->disaB, 205, 18);
567 WMMoveWidget(panel->disaB, 10, 18);
568 WMSetButtonText(panel->disaB, _("Disable mouse actions"));
570 panel->button1L = WMCreateLabel(panel->menuF);
571 WMResizeWidget(panel->button1L, 87, 20);
572 WMMoveWidget(panel->button1L, 5, 45);
573 WMSetLabelTextAlignment(panel->button1L, WARight);
574 WMSetLabelText(panel->button1L, _("Left Button"));
576 panel->button1P = WMCreatePopUpButton(panel->menuF);
577 WMResizeWidget(panel->button1P, 135, 20);
578 WMMoveWidget(panel->button1P, 95, 45);
580 panel->button2L = WMCreateLabel(panel->menuF);
581 WMResizeWidget(panel->button2L, 87, 20);
582 WMMoveWidget(panel->button2L, 5, 73);
583 WMSetLabelTextAlignment(panel->button2L, WARight);
584 WMSetLabelText(panel->button2L, _("Middle Button"));
586 panel->button2P = WMCreatePopUpButton(panel->menuF);
587 WMResizeWidget(panel->button2P, 135, 20);
588 WMMoveWidget(panel->button2P, 95, 73);
590 panel->button3L = WMCreateLabel(panel->menuF);
591 WMResizeWidget(panel->button3L, 87, 20);
592 WMMoveWidget(panel->button3L, 5, 101);
593 WMSetLabelTextAlignment(panel->button3L, WARight);
594 WMSetLabelText(panel->button3L, _("Right Button"));
596 panel->button3P = WMCreatePopUpButton(panel->menuF);
597 WMResizeWidget(panel->button3P, 135, 20);
598 WMMoveWidget(panel->button3P, 95, 101);
600 panel->wheelL = WMCreateLabel(panel->menuF);
601 WMResizeWidget(panel->wheelL, 87, 20);
602 WMMoveWidget(panel->wheelL, 5, 129);
603 WMSetLabelTextAlignment(panel->wheelL, WARight);
604 WMSetLabelText(panel->wheelL, _("Mouse Wheel"));
606 panel->wheelP = WMCreatePopUpButton(panel->menuF);
607 WMResizeWidget(panel->wheelP, 135, 20);
608 WMMoveWidget(panel->wheelP, 95, 129);
610 for (i = 0; i < sizeof(buttonActions) / sizeof(char *); i++) {
611 WMAddPopUpButtonItem(panel->button1P, buttonActions[i]);
612 WMAddPopUpButtonItem(panel->button2P, buttonActions[i]);
613 WMAddPopUpButtonItem(panel->button3P, buttonActions[i]);
616 for (i = 0; i < sizeof(wheelActions) / sizeof(char *); i++) {
617 WMAddPopUpButtonItem(panel->wheelP, wheelActions[i]);
620 WMMapSubwidgets(panel->menuF);
622 /* ************** Grab Modifier **************** */
623 panel->grabF = WMCreateFrame(panel->box);
624 WMResizeWidget(panel->grabF, 240, 50);
625 WMMoveWidget(panel->grabF, 270, 170);
626 WMSetFrameTitle(panel->grabF, _("Mouse Grab Modifier"));
628 WMSetBalloonTextForView(_("Keyboard modifier to use for actions that\n"
629 "involve dragging windows with the mouse,\n"
630 "clicking inside the window."), WMWidgetView(panel->grabF));
632 panel->grabP = WMCreatePopUpButton(panel->grabF);
633 WMResizeWidget(panel->grabP, 160, 20);
634 WMMoveWidget(panel->grabP, 40, 20);
636 fillModifierPopUp(panel->grabP);
638 WMMapSubwidgets(panel->grabF);
640 WMRealizeWidget(panel->box);
641 WMMapSubwidgets(panel->box);
646 static void storeCommandInScript(char *cmd, char *line)
652 path = wstrconcat(wusergnusteppath(), "/Library/WindowMaker/autostart");
654 f = fopen(path, "rb");
656 f = fopen(path, "wb");
658 wsyserror(_("could not create %s"), path);
661 fprintf(f, "#!/bin/sh\n");
665 int len = strlen(cmd);
670 tmppath = wstrconcat(wusergnusteppath(), "/Library/WindowMaker/autostart.tmp");
671 fo = fopen(tmppath, "wb");
673 wsyserror(_("could not create temporary file %s"), tmppath);
679 if (!fgets(buffer, 127, f)) {
682 if (buffer[0] == '\n') {
683 /* don't write empty lines, else the file will grow
684 * indefinitely (one '\n' added at end of file on each save).
688 if (strncmp(buffer, cmd, len) == 0) {
704 if (rename(tmppath, path) != 0) {
705 wsyserror(_("could not rename file %s to %s\n"), tmppath, path);
709 sprintf(buffer, "chmod u+x %s", path);
718 static void storeData(_Panel * panel)
723 static char *button[4] = { "None", "OpenApplicationsMenu", "OpenWindowListMenu", "SelectWindows" };
724 static char *wheel[2] = { "None", "SwitchWorkspaces" };
725 WMUserDefaults *udb = WMGetStandardUserDefaults();
727 if (!WMGetUDBoolForKey(udb, "NoXSetStuff")) {
728 tmp = WMGetTextFieldText(panel->threT);
729 if (strlen(tmp) == 0) {
734 sprintf(buffer, XSET " m %i/%i %s\n", (int)(panel->acceleration * 10), 10, tmp);
735 storeCommandInScript(XSET " m", buffer);
740 tmp = WMGetTextFieldText(panel->ddelaT);
741 if (sscanf(tmp, "%i", &i) == 1 && i > 0)
742 SetIntegerForKey(i, "DoubleClickTime");
744 SetBoolForKey(WMGetButtonSelected(panel->disaB), "DisableWSMouseActions");
746 i = WMGetPopUpButtonSelectedItem(panel->button1P);
747 SetStringForKey(button[i], "MouseLeftButtonAction");
749 i = WMGetPopUpButtonSelectedItem(panel->button2P);
750 SetStringForKey(button[i], "MouseMiddleButtonAction");
752 i = WMGetPopUpButtonSelectedItem(panel->button3P);
753 SetStringForKey(button[i], "MouseRightButtonAction");
755 i = WMGetPopUpButtonSelectedItem(panel->wheelP);
756 SetStringForKey(wheel[i], "MouseWheelAction");
758 tmp = WMGetPopUpButtonItem(panel->grabP, WMGetPopUpButtonSelectedItem(panel->grabP));
760 p = strchr(tmp, ' ');
763 SetStringForKey(tmp, "ModifierKey");
768 Panel *InitMouseSettings(WMScreen * scr, WMWidget * parent)
772 modifierNames[0] = wstrdup(_("Shift"));
773 modifierNames[1] = wstrdup(_("Lock"));
774 modifierNames[2] = wstrdup(_("Control"));
775 modifierNames[3] = wstrdup(_("Mod1"));
776 modifierNames[4] = wstrdup(_("Mod2"));
777 modifierNames[5] = wstrdup(_("Mod3"));
778 modifierNames[6] = wstrdup(_("Mod4"));
779 modifierNames[7] = wstrdup(_("Mod5"));
781 buttonActions[0] = wstrdup(_("None"));
782 buttonActions[1] = wstrdup(_("Applications Menu"));
783 buttonActions[2] = wstrdup(_("Window List Menu"));
784 buttonActions[3] = wstrdup(_("Select Windows"));
786 wheelActions[0] = wstrdup(_("None"));
787 wheelActions[1] = wstrdup(_("Switch Workspaces"));
789 panel = wmalloc(sizeof(_Panel));
790 memset(panel, 0, sizeof(_Panel));
792 panel->sectionName = _("Mouse Preferences");
794 panel->description = _("Mouse speed/acceleration, double click delay,\n" "mouse button bindings etc.");
796 panel->parent = parent;
798 panel->callbacks.createWidgets = createPanel;
799 panel->callbacks.updateDomain = storeData;
801 AddSection(panel, ICON_FILE);