WPrefs: add new mouse buttons configuration support
[wmaker-crm.git] / WPrefs.app / MouseSettings.c
blob749feeeef04f28f6eef8978fb9c54d41e365b4f6
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 along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "WPrefs.h"
25 #include <X11/Xutil.h>
26 #include <X11/XKBlib.h>
28 #include <unistd.h>
29 #include <math.h>
32 #define XSET "xset"
34 typedef struct _Panel {
35 WMBox *box;
37 char *sectionName;
39 char *description;
41 CallbackRec callbacks;
43 WMWidget *parent;
45 WMFrame *speedF;
46 WMLabel *speedL;
47 WMSlider *speedS;
48 WMLabel *acceL;
49 WMTextField *acceT;
50 WMLabel *threL;
51 WMTextField *threT;
53 WMFrame *ddelaF;
54 WMButton *ddelaB[5];
55 WMTextField *ddelaT;
56 WMLabel *ddelaL;
57 DoubleTest *tester;
59 WMFrame *menuF;
60 WMLabel *button1L;
61 WMLabel *button2L;
62 WMLabel *button3L;
63 WMLabel *button8L;
64 WMLabel *button9L;
65 WMLabel *wheelL;
66 WMLabel *wheelTiltL;
67 WMPopUpButton *button1P;
68 WMPopUpButton *button2P;
69 WMPopUpButton *button3P;
70 WMPopUpButton *button8P;
71 WMPopUpButton *button9P;
72 WMPopUpButton *wheelP;
73 WMPopUpButton *wheelTiltP;
75 WMButton *disaB;
77 WMFrame *grabF;
78 WMPopUpButton *grabP;
80 /**/ int maxThreshold;
81 float acceleration;
82 } _Panel;
84 #define ICON_FILE "mousesettings"
86 #define SPEED_ICON_FILE "mousespeed"
88 #define DELAY_ICON "timer%i"
89 #define DELAY_ICON_S "timer%is"
91 /* need access to the double click variables */
92 #include <WINGs/WINGsP.h>
94 static char *modifierNames[8];
96 static char *buttonActions[4];
98 static char *wheelActions[2];
100 #define DELAY(i) ((i)*75+170)
103 static void setMouseAccel(WMScreen * scr, float accel, int threshold)
105 int n, d;
107 d = 10;
108 n = accel * d;
110 XChangePointerControl(WMScreenDisplay(scr), True, True, n, d, threshold);
113 static void speedChange(WMWidget * w, void *data)
115 _Panel *panel = (_Panel *) data;
116 int i;
117 char buffer[64];
118 int threshold;
119 char *tmp;
121 if (w == NULL) {
122 float accel;
124 tmp = WMGetTextFieldText(panel->acceT);
125 if (sscanf(tmp, "%f", &accel) != 1 || accel < 0) {
126 WMRunAlertPanel(WMWidgetScreen(panel->acceT), GetWindow(),
127 _("Error"),
128 _("Invalid mouse acceleration value. Must be a positive real value."),
129 _("OK"), NULL, NULL);
130 wfree(tmp);
131 return;
133 panel->acceleration = accel;
134 wfree(tmp);
135 } else {
136 i = (int)WMGetSliderValue(panel->speedS);
138 panel->acceleration = 0.25 + (i * 0.25);
140 sprintf(buffer, "%.2f", 0.25 + (i * 0.25));
141 WMSetTextFieldText(panel->acceT, buffer);
144 tmp = WMGetTextFieldText(panel->threT);
145 if (sscanf(tmp, "%i", &threshold) != 1 || threshold < 0 || threshold > panel->maxThreshold) {
146 WMRunAlertPanel(WMWidgetScreen(panel->parent), GetWindow(), _("Error"),
148 ("Invalid mouse acceleration threshold value. Must be the number of pixels to travel before accelerating."),
149 _("OK"), NULL, NULL);
150 } else {
151 setMouseAccel(WMWidgetScreen(panel->parent), panel->acceleration, threshold);
153 wfree(tmp);
156 static void returnPressed(void *observerData, WMNotification * notification)
158 _Panel *panel = (_Panel *) observerData;
160 /* Parameter not used, but tell the compiler that it is ok */
161 (void) notification;
163 speedChange(NULL, panel);
166 static void doubleClick(WMWidget * w, void *data)
168 _Panel *panel = (_Panel *) data;
169 int i;
170 char buffer[32];
172 for (i = 0; i < 5; i++) {
173 if (panel->ddelaB[i] == w)
174 break;
176 WINGsConfiguration.doubleClickDelay = DELAY(i);
178 sprintf(buffer, "%i", DELAY(i));
179 WMSetTextFieldText(panel->ddelaT, buffer);
182 static int getButtonAction(const char *str)
184 if (!str)
185 return -2;
187 if (strcasecmp(str, "None") == 0)
188 return 0;
189 else if (strcasecmp(str, "OpenApplicationsMenu") == 0)
190 return 1;
191 else if (strcasecmp(str, "OpenWindowListMenu") == 0)
192 return 2;
193 else if (strcasecmp(str, "SelectWindows") == 0)
194 return 3;
195 else
196 return -1;
200 static int getWheelAction(const char *str)
202 if (!str)
203 return -2;
205 if (strcasecmp(str, "None") == 0)
206 return 0;
207 else if (strcasecmp(str, "SwitchWorkspaces") == 0)
208 return 1;
209 else
210 return -1;
214 static void getMouseParameters(Display * dpy, float *accel, int *thre)
216 int n, d;
218 XGetPointerControl(dpy, &n, &d, thre);
220 *accel = (float)n / (float)d;
223 static void showData(_Panel * panel)
225 char *str;
226 int i;
227 int a = -1, b = -1, c = -1, w = -1;
228 float accel;
229 char buffer[32];
230 Display *dpy = WMScreenDisplay(WMWidgetScreen(panel->parent));
232 str = GetStringForKey("MouseLeftButtonAction");
233 i = getButtonAction(str);
234 if (i < 0) {
235 a = 3;
236 if (i == -1) {
237 wwarning(_("bad value %s for option %s"), str, "MouseLeftButtonAction");
239 } else {
240 a = i;
242 WMSetPopUpButtonSelectedItem(panel->button1P, a);
244 str = GetStringForKey("MouseMiddleButtonAction");
245 i = getButtonAction(str);
246 if (i < 0) {
247 b = 2;
248 if (i == -1) {
249 wwarning(_("bad value %s for option %s"), str, "MouseMiddleButtonAction");
251 } else {
252 b = i;
254 WMSetPopUpButtonSelectedItem(panel->button2P, b);
256 str = GetStringForKey("MouseRightButtonAction");
257 i = getButtonAction(str);
258 if (i < 0) {
259 c = 1;
260 if (i == -1) {
261 wwarning(_("bad value %s for option %s"), str, "MouseRightButtonAction");
263 } else {
264 c = i;
266 WMSetPopUpButtonSelectedItem(panel->button3P, c);
268 str = GetStringForKey("MouseBackwardButtonAction");
269 i = getButtonAction(str);
270 if (i < 0) {
271 b = 0;
272 if (i == -1) {
273 wwarning(_("bad value %s for option %s"), str, "MouseBackwardButtonAction");
275 } else {
276 b = i;
278 WMSetPopUpButtonSelectedItem(panel->button8P, b);
280 str = GetStringForKey("MouseForwardButtonAction");
281 i = getButtonAction(str);
282 if (i < 0) {
283 b = 0;
284 if (i == -1) {
285 wwarning(_("bad value %s for option %s"), str, "MouseForwardButtonAction");
287 } else {
288 b = i;
290 WMSetPopUpButtonSelectedItem(panel->button9P, b);
292 str = GetStringForKey("MouseWheelAction");
293 i = getWheelAction(str);
294 if (i < 0) {
295 w = 0;
296 if (i == -1) {
297 wwarning(_("bad value %s for option %s"), str, "MouseWheelAction");
299 } else {
300 w = i;
302 WMSetPopUpButtonSelectedItem(panel->wheelP, w);
304 str = GetStringForKey("MouseWheelTiltAction");
305 i = getWheelAction(str);
306 if (i < 0) {
307 w = 0;
308 if (i == -1) {
309 wwarning(_("bad value %s for option %s"), str, "MouseWheelTiltAction");
311 } else {
312 w = i;
314 WMSetPopUpButtonSelectedItem(panel->wheelTiltP, w);
316 WMSetButtonSelected(panel->disaB, GetBoolForKey("DisableWSMouseActions"));
318 /**/ getMouseParameters(dpy, &accel, &a);
319 panel->maxThreshold = WidthOfScreen(DefaultScreenOfDisplay(dpy));
320 if (a > panel->maxThreshold) {
321 panel->maxThreshold = a;
323 sprintf(buffer, "%i", a);
324 WMSetTextFieldText(panel->threT, buffer);
326 WMSetSliderValue(panel->speedS, (accel - 0.25) / 0.25);
328 panel->acceleration = accel;
329 sprintf(buffer, "%.2f", accel);
330 WMSetTextFieldText(panel->acceT, buffer);
332 /**/ b = GetIntegerForKey("DoubleClickTime");
333 /* find best match */
334 a = -1;
335 for (i = 0; i < 5; i++) {
336 if (DELAY(i) == b)
337 a = i;
339 if (a >= 0)
340 WMPerformButtonClick(panel->ddelaB[a]);
341 sprintf(buffer, "%i", b);
342 WMSetTextFieldText(panel->ddelaT, buffer);
344 /**/ str = GetStringForKey("ModifierKey");
345 if (!str)
346 str = "mod1";
347 a = ModifierFromKey(dpy, str);
349 if (a != -1) {
350 str = modifierNames[a];
352 a = 0;
353 for (i = 0; i < WMGetPopUpButtonNumberOfItems(panel->grabP); i++) {
354 if (strstr(WMGetPopUpButtonItem(panel->grabP, i), str)) {
355 WMSetPopUpButtonSelectedItem(panel->grabP, i);
356 a = 1;
357 break;
362 if (a < 1) {
363 char *previous;
365 previous = WMGetPopUpButtonItem(panel->grabP, 0);
366 if (previous != NULL)
367 WMSetPopUpButtonSelectedItem(panel->grabP, 0);
368 wwarning(_("modifier key %s for option ModifierKey was not recognized. Using %s as default"),
369 str, previous?previous:"(empty)");
373 static void fillModifierPopUp(WMPopUpButton * pop)
375 XModifierKeymap *mapping;
376 Display *dpy = WMScreenDisplay(WMWidgetScreen(pop));
377 int i, j;
378 char *str;
379 char buffer[64];
381 mapping = XGetModifierMapping(dpy);
383 if (!mapping || mapping->max_keypermod == 0) {
384 WMAddPopUpButtonItem(pop, "Mod1");
385 WMAddPopUpButtonItem(pop, "Mod2");
386 WMAddPopUpButtonItem(pop, "Mod3");
387 WMAddPopUpButtonItem(pop, "Mod4");
388 WMAddPopUpButtonItem(pop, "Mod5");
389 wwarning(_("could not retrieve keyboard modifier mapping"));
390 return;
393 for (j = 0; j < 8; j++) {
394 int idx;
395 char *array[8];
396 int a;
397 KeySym ksym;
398 int k;
399 char *ptr;
400 char *tmp;
402 a = 0;
403 memset(array, 0, sizeof(char *) * 8);
404 for (i = 0; i < mapping->max_keypermod; i++) {
405 idx = i + j * mapping->max_keypermod;
406 if (mapping->modifiermap[idx] != 0) {
407 int l;
408 for (l = 0; l < 4; l++) {
409 if (xext_xkb_supported)
410 ksym = XkbKeycodeToKeysym(dpy, mapping->modifiermap[idx], 0, l);
411 else
412 ksym = XKeycodeToKeysym(dpy, mapping->modifiermap[idx], 0);
413 if (ksym != NoSymbol)
414 break;
416 if (ksym != NoSymbol)
417 str = XKeysymToString(ksym);
418 else
419 str = NULL;
420 if (str && !strstr(str, "_Lock") && !strstr(str, "Shift")
421 && !strstr(str, "Control")) {
422 array[a++] = wstrdup(str);
427 for (k = 0; k < a; k++) {
428 if (array[k] == NULL)
429 continue;
430 tmp = wstrdup(array[k]);
431 ptr = strstr(tmp, "_L");
432 if (ptr)
433 *ptr = 0;
434 ptr = strstr(tmp, "_R");
435 if (ptr)
436 *ptr = 0;
437 sprintf(buffer, "%s (%s)", modifierNames[j], tmp);
438 /*sprintf(buffer, "%s", tmp); */
439 WMAddPopUpButtonItem(pop, buffer);
440 for (i = k + 1; i < a; i++) {
441 if (array[i] == NULL)
442 continue;
443 if (strstr(array[i], tmp)) {
444 wfree(array[i]);
445 array[i] = NULL;
446 break;
449 wfree(tmp);
452 while (--a > 0) {
453 if (array[a])
454 wfree(array[a]);
458 if (mapping)
459 XFreeModifiermap(mapping);
462 static void createPanel(Panel * p)
464 _Panel *panel = (_Panel *) p;
465 WMScreen *scr = WMWidgetScreen(panel->parent);
466 WMPixmap *icon;
467 char *buf1, *buf2;
468 int i;
469 RColor color;
470 char *path;
472 color.red = 0xae;
473 color.green = 0xaa;
474 color.blue = 0xae;
476 panel->box = WMCreateBox(panel->parent);
477 WMSetViewExpandsToParent(WMWidgetView(panel->box), 2, 2, 2, 2);
479 /**************** Mouse Speed ****************/
480 panel->speedF = WMCreateFrame(panel->box);
481 WMResizeWidget(panel->speedF, 225, 90);
482 WMMoveWidget(panel->speedF, 15, 5);
483 WMSetFrameTitle(panel->speedF, _("Mouse Speed"));
485 panel->speedL = WMCreateLabel(panel->speedF);
486 WMResizeWidget(panel->speedL, 40, 46);
487 WMMoveWidget(panel->speedL, 10, 14);
488 WMSetLabelImagePosition(panel->speedL, WIPImageOnly);
489 path = LocateImage(SPEED_ICON_FILE);
490 if (path) {
491 icon = WMCreateBlendedPixmapFromFile(scr, path, &color);
492 if (icon) {
493 WMSetLabelImage(panel->speedL, icon);
494 WMReleasePixmap(icon);
495 } else {
496 wwarning(_("could not load icon %s"), path);
498 wfree(path);
501 panel->speedS = WMCreateSlider(panel->speedF);
502 WMResizeWidget(panel->speedS, 150, 15);
503 WMMoveWidget(panel->speedS, 60, 35);
504 WMSetSliderMinValue(panel->speedS, 0);
505 WMSetSliderMaxValue(panel->speedS, 40);
506 WMSetSliderContinuous(panel->speedS, False);
507 WMSetSliderAction(panel->speedS, speedChange, panel);
509 panel->acceL = WMCreateLabel(panel->speedF);
510 WMResizeWidget(panel->acceL, 50, 16);
511 WMMoveWidget(panel->acceL, 10, 65);
512 WMSetLabelTextAlignment(panel->acceL, WARight);
513 WMSetLabelText(panel->acceL, _("Accel.:"));
515 panel->acceT = WMCreateTextField(panel->speedF);
516 WMResizeWidget(panel->acceT, 40, 20);
517 WMMoveWidget(panel->acceT, 60, 63);
518 WMAddNotificationObserver(returnPressed, panel, WMTextDidEndEditingNotification, panel->acceT);
520 panel->threL = WMCreateLabel(panel->speedF);
521 WMResizeWidget(panel->threL, 80, 16);
522 WMMoveWidget(panel->threL, 100, 65);
523 WMSetLabelTextAlignment(panel->threL, WARight);
524 WMSetLabelText(panel->threL, _("Threshold:"));
526 panel->threT = WMCreateTextField(panel->speedF);
527 WMResizeWidget(panel->threT, 30, 20);
528 WMMoveWidget(panel->threT, 180, 63);
529 WMAddNotificationObserver(returnPressed, panel, WMTextDidEndEditingNotification, panel->threT);
531 WMMapSubwidgets(panel->speedF);
533 /* ************** Grab Modifier **************** */
534 panel->grabF = WMCreateFrame(panel->box);
535 WMResizeWidget(panel->grabF, 225, 45);
536 WMMoveWidget(panel->grabF, 15, 95);
537 WMSetFrameTitle(panel->grabF, _("Mouse Grab Modifier"));
539 WMSetBalloonTextForView(_("Keyboard modifier to use for actions that\n"
540 "involve dragging windows with the mouse,\n"
541 "clicking inside the window."), WMWidgetView(panel->grabF));
543 panel->grabP = WMCreatePopUpButton(panel->grabF);
544 WMResizeWidget(panel->grabP, 160, 20);
545 WMMoveWidget(panel->grabP, 50, 18);
547 fillModifierPopUp(panel->grabP);
548 WMMapSubwidgets(panel->grabF);
550 /***************** Doubleclick Delay ****************/
552 panel->ddelaF = WMCreateFrame(panel->box);
553 WMResizeWidget(panel->ddelaF, 225, 87);
554 WMMoveWidget(panel->ddelaF, 15, 140);
555 WMSetFrameTitle(panel->ddelaF, _("Double-Click Delay"));
557 buf1 = wmalloc(strlen(DELAY_ICON) + 2);
558 buf2 = wmalloc(strlen(DELAY_ICON_S) + 2);
560 for (i = 0; i < 5; i++) {
561 panel->ddelaB[i] = WMCreateCustomButton(panel->ddelaF, WBBStateChangeMask);
562 WMResizeWidget(panel->ddelaB[i], 25, 25);
563 WMMoveWidget(panel->ddelaB[i], 20 + (40 * i), 20);
564 WMSetButtonBordered(panel->ddelaB[i], False);
565 WMSetButtonImagePosition(panel->ddelaB[i], WIPImageOnly);
566 WMSetButtonAction(panel->ddelaB[i], doubleClick, panel);
567 if (i > 0) {
568 WMGroupButtons(panel->ddelaB[0], panel->ddelaB[i]);
570 sprintf(buf1, DELAY_ICON, i + 1);
571 sprintf(buf2, DELAY_ICON_S, i + 1);
572 path = LocateImage(buf1);
573 if (path) {
574 icon = WMCreatePixmapFromFile(scr, path);
575 if (icon) {
576 WMSetButtonImage(panel->ddelaB[i], icon);
577 WMReleasePixmap(icon);
578 } else {
579 wwarning(_("could not load icon file %s"), path);
581 wfree(path);
583 path = LocateImage(buf2);
584 if (path) {
585 icon = WMCreatePixmapFromFile(scr, path);
586 if (icon) {
587 WMSetButtonAltImage(panel->ddelaB[i], icon);
588 WMReleasePixmap(icon);
589 } else {
590 wwarning(_("could not load icon file %s"), path);
592 wfree(path);
595 wfree(buf1);
596 wfree(buf2);
598 panel->tester = CreateDoubleTest(panel->ddelaF, _("Test"));
599 WMResizeWidget(panel->tester, 84, 29);
600 WMMoveWidget(panel->tester, 25, 52);
602 panel->ddelaT = WMCreateTextField(panel->ddelaF);
603 WMResizeWidget(panel->ddelaT, 40, 20);
604 WMMoveWidget(panel->ddelaT, 130, 57);
606 panel->ddelaL = WMCreateLabel(panel->ddelaF);
607 WMResizeWidget(panel->ddelaL, 40, 16);
608 WMMoveWidget(panel->ddelaL, 175, 63);
610 WMFont *font;
611 WMColor *color;
613 font = WMSystemFontOfSize(scr, 10);
614 color = WMDarkGrayColor(scr);
615 WMSetLabelTextColor(panel->ddelaL, color);
616 WMSetLabelFont(panel->ddelaL, font);
617 WMReleaseFont(font);
618 WMReleaseColor(color);
620 WMSetLabelText(panel->ddelaL, _("msec"));
622 WMMapSubwidgets(panel->ddelaF);
624 /* ************** Workspace Action Buttons **************** */
626 panel->menuF = WMCreateFrame(panel->box);
627 WMResizeWidget(panel->menuF, 260, 222);
628 WMMoveWidget(panel->menuF, 250, 5);
629 WMSetFrameTitle(panel->menuF, _("Workspace Mouse Actions"));
631 panel->disaB = WMCreateSwitchButton(panel->menuF);
632 WMResizeWidget(panel->disaB, 205, 18);
633 WMMoveWidget(panel->disaB, 10, 15);
634 WMSetButtonText(panel->disaB, _("Disable mouse actions"));
636 panel->button1L = WMCreateLabel(panel->menuF);
637 WMResizeWidget(panel->button1L, 107, 20);
638 WMMoveWidget(panel->button1L, 5, 40);
639 WMSetLabelTextAlignment(panel->button1L, WARight);
640 WMSetLabelText(panel->button1L, _("Left Button"));
642 panel->button1P = WMCreatePopUpButton(panel->menuF);
643 WMResizeWidget(panel->button1P, 135, 20);
644 WMMoveWidget(panel->button1P, 115, 40);
646 panel->button2L = WMCreateLabel(panel->menuF);
647 WMResizeWidget(panel->button2L, 107, 20);
648 WMMoveWidget(panel->button2L, 5, 65);
649 WMSetLabelTextAlignment(panel->button2L, WARight);
650 WMSetLabelText(panel->button2L, _("Middle Button"));
652 panel->button2P = WMCreatePopUpButton(panel->menuF);
653 WMResizeWidget(panel->button2P, 135, 20);
654 WMMoveWidget(panel->button2P, 115, 65);
656 panel->button3L = WMCreateLabel(panel->menuF);
657 WMResizeWidget(panel->button3L, 107, 20);
658 WMMoveWidget(panel->button3L, 5, 90);
659 WMSetLabelTextAlignment(panel->button3L, WARight);
660 WMSetLabelText(panel->button3L, _("Right Button"));
662 panel->button3P = WMCreatePopUpButton(panel->menuF);
663 WMResizeWidget(panel->button3P, 135, 20);
664 WMMoveWidget(panel->button3P, 115, 90);
666 panel->button8L = WMCreateLabel(panel->menuF);
667 WMResizeWidget(panel->button8L, 107, 20);
668 WMMoveWidget(panel->button8L, 5, 115);
669 WMSetLabelTextAlignment(panel->button8L, WARight);
670 WMSetLabelText(panel->button8L, _("Back Button"));
672 panel->button8P = WMCreatePopUpButton(panel->menuF);
673 WMResizeWidget(panel->button8P, 135, 20);
674 WMMoveWidget(panel->button8P, 115, 115);
676 panel->button9L = WMCreateLabel(panel->menuF);
677 WMResizeWidget(panel->button9L, 107, 20);
678 WMMoveWidget(panel->button9L, 5, 140);
679 WMSetLabelTextAlignment(panel->button9L, WARight);
680 WMSetLabelText(panel->button9L, _("Forward Button"));
682 panel->button9P = WMCreatePopUpButton(panel->menuF);
683 WMResizeWidget(panel->button9P, 135, 20);
684 WMMoveWidget(panel->button9P, 115, 140);
686 panel->wheelL = WMCreateLabel(panel->menuF);
687 WMResizeWidget(panel->wheelL, 107, 20);
688 WMMoveWidget(panel->wheelL, 5, 165);
689 WMSetLabelTextAlignment(panel->wheelL, WARight);
690 WMSetLabelText(panel->wheelL, _("Mouse Wheel"));
692 panel->wheelP = WMCreatePopUpButton(panel->menuF);
693 WMResizeWidget(panel->wheelP, 135, 20);
694 WMMoveWidget(panel->wheelP, 115, 165);
696 panel->wheelTiltL = WMCreateLabel(panel->menuF);
697 WMResizeWidget(panel->wheelTiltL, 107, 20);
698 WMMoveWidget(panel->wheelTiltL, 5, 190);
699 WMSetLabelTextAlignment(panel->wheelTiltL, WARight);
700 WMSetLabelText(panel->wheelTiltL, _("Mouse Wheel Tilt"));
702 panel->wheelTiltP = WMCreatePopUpButton(panel->menuF);
703 WMResizeWidget(panel->wheelTiltP, 135, 20);
704 WMMoveWidget(panel->wheelTiltP, 115, 190);
706 for (i = 0; i < wlengthof(buttonActions); i++) {
707 WMAddPopUpButtonItem(panel->button1P, buttonActions[i]);
708 WMAddPopUpButtonItem(panel->button2P, buttonActions[i]);
709 WMAddPopUpButtonItem(panel->button3P, buttonActions[i]);
710 WMAddPopUpButtonItem(panel->button8P, buttonActions[i]);
711 WMAddPopUpButtonItem(panel->button9P, buttonActions[i]);
714 for (i = 0; i < wlengthof(wheelActions); i++) {
715 WMAddPopUpButtonItem(panel->wheelP, wheelActions[i]);
716 WMAddPopUpButtonItem(panel->wheelTiltP, wheelActions[i]);
719 WMMapSubwidgets(panel->menuF);
721 WMRealizeWidget(panel->box);
722 WMMapSubwidgets(panel->box);
724 showData(panel);
727 static void storeCommandInScript(const char *cmd, const char *line)
729 char *path;
730 FILE *f;
731 char buffer[128];
733 path = wstrconcat(wusergnusteppath(), "/Library/WindowMaker/autostart");
735 f = fopen(path, "rb");
736 if (!f) {
737 f = fopen(path, "wb");
738 if (!f) {
739 werror(_("could not create %s"), path);
740 goto end;
742 fprintf(f, "#!/bin/sh\n");
743 fputs(line, f);
744 fputs("\n", f);
745 } else {
746 int len = strlen(cmd);
747 int ok = 0;
748 char *tmppath;
749 FILE *fo;
751 tmppath = wstrconcat(wusergnusteppath(), "/Library/WindowMaker/autostart.tmp");
752 fo = fopen(tmppath, "wb");
753 if (!fo) {
754 werror(_("could not create temporary file %s"), tmppath);
755 wfree(tmppath);
756 goto end;
759 while (!feof(f)) {
760 if (!fgets(buffer, 127, f)) {
761 break;
763 if (buffer[0] == '\n') {
764 /* don't write empty lines, else the file will grow
765 * indefinitely (one '\n' added at end of file on each save).
767 continue;
769 if (strncmp(buffer, cmd, len) == 0) {
770 if (!ok) {
771 fputs(line, fo);
772 fputs("\n", fo);
773 ok = 1;
775 } else {
776 fputs(buffer, fo);
779 if (!ok) {
780 fputs(line, fo);
781 fputs("\n", fo);
783 fsync(fileno(fo));
784 fclose(fo);
786 if (rename(tmppath, path) != 0) {
787 werror(_("could not rename file %s to %s"), tmppath, path);
789 wfree(tmppath);
791 sprintf(buffer, "chmod u+x %s", path);
792 if (system(buffer) == -1)
793 werror(_("could not execute command \"%s\""), buffer);
795 end:
796 wfree(path);
797 if (f) {
798 fsync(fileno(f)); /* this may be rw */
799 fclose(f);
803 static void storeData(_Panel * panel)
805 char buffer[64];
806 int i;
807 char *tmp, *p;
808 static char *button[4] = { "None", "OpenApplicationsMenu", "OpenWindowListMenu", "SelectWindows" };
809 static char *wheel[2] = { "None", "SwitchWorkspaces" };
810 WMUserDefaults *udb = WMGetStandardUserDefaults();
812 if (!WMGetUDBoolForKey(udb, "NoXSetStuff")) {
813 tmp = WMGetTextFieldText(panel->threT);
814 if (strlen(tmp) == 0) {
815 wfree(tmp);
816 tmp = wstrdup("4");
819 sprintf(buffer, XSET " m %i/%i %s\n", (int)(panel->acceleration * 10), 10, tmp);
820 storeCommandInScript(XSET " m", buffer);
822 wfree(tmp);
825 tmp = WMGetTextFieldText(panel->ddelaT);
826 if (sscanf(tmp, "%i", &i) == 1 && i > 0)
827 SetIntegerForKey(i, "DoubleClickTime");
828 wfree(tmp);
830 SetBoolForKey(WMGetButtonSelected(panel->disaB), "DisableWSMouseActions");
832 i = WMGetPopUpButtonSelectedItem(panel->button1P);
833 SetStringForKey(button[i], "MouseLeftButtonAction");
835 i = WMGetPopUpButtonSelectedItem(panel->button2P);
836 SetStringForKey(button[i], "MouseMiddleButtonAction");
838 i = WMGetPopUpButtonSelectedItem(panel->button3P);
839 SetStringForKey(button[i], "MouseRightButtonAction");
841 i = WMGetPopUpButtonSelectedItem(panel->button8P);
842 SetStringForKey(button[i], "MouseBackwardButtonAction");
844 i = WMGetPopUpButtonSelectedItem(panel->button9P);
845 SetStringForKey(button[i], "MouseForwardButtonAction");
847 i = WMGetPopUpButtonSelectedItem(panel->wheelP);
848 SetStringForKey(wheel[i], "MouseWheelAction");
850 i = WMGetPopUpButtonSelectedItem(panel->wheelTiltP);
851 SetStringForKey(wheel[i], "MouseWheelTiltAction");
853 tmp = WMGetPopUpButtonItem(panel->grabP, WMGetPopUpButtonSelectedItem(panel->grabP));
854 tmp = wstrdup(tmp);
855 p = strchr(tmp, ' ');
856 if (p != NULL)
857 *p = '\0';
859 SetStringForKey(tmp, "ModifierKey");
861 wfree(tmp);
864 Panel *InitMouseSettings(WMWidget *parent)
866 _Panel *panel;
868 modifierNames[0] = wstrdup(_("Shift"));
869 modifierNames[1] = wstrdup(_("Lock"));
870 modifierNames[2] = wstrdup(_("Control"));
871 modifierNames[3] = wstrdup(_("Mod1"));
872 modifierNames[4] = wstrdup(_("Mod2"));
873 modifierNames[5] = wstrdup(_("Mod3"));
874 modifierNames[6] = wstrdup(_("Mod4"));
875 modifierNames[7] = wstrdup(_("Mod5"));
877 buttonActions[0] = wstrdup(_("None"));
878 buttonActions[1] = wstrdup(_("Applications Menu"));
879 buttonActions[2] = wstrdup(_("Window List Menu"));
880 buttonActions[3] = wstrdup(_("Select Windows"));
882 wheelActions[0] = wstrdup(_("None"));
883 wheelActions[1] = wstrdup(_("Switch Workspaces"));
885 panel = wmalloc(sizeof(_Panel));
887 panel->sectionName = _("Mouse Preferences");
889 panel->description = _("Mouse speed/acceleration, double click delay,\n" "mouse button bindings etc.");
891 panel->parent = parent;
893 panel->callbacks.createWidgets = createPanel;
894 panel->callbacks.updateDomain = storeData;
896 AddSection(panel, ICON_FILE);
898 return panel;