2 /* MouseSettings.c- mouse options (some are equivalent to xset)
4 * WPrefs - Window Maker Preferences Program
6 * Copyright (c) 1998 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,
27 #include <X11/Xutil.h>
31 /* double-click tester */
40 typedef struct _Panel
{
47 CallbackRec callbacks
;
84 #define ICON_FILE "mousesettings"
86 #define SPEED_ICON_FILE "mousespeed"
88 #define DELAY_ICON "timer%i"
89 #define DELAY_ICON_S "timer%is"
92 /* need access to the double click variables */
97 static char *modifierNames
[] = {
109 static char *buttonNames
[] = {
119 #define DELAY(i) ((i)*75+170)
122 int ModifierFromKey(Display
*dpy
, char *key
);
126 setMouseAccel(WMScreen
*scr
, float accel
, int threshold
)
133 XChangePointerControl(WMScreenDisplay(scr
), True
, True
, n
, d
, threshold
);
138 speedChange(WMWidget
*w
, void *data
)
140 _Panel
*panel
= (_Panel
*)data
;
149 tmp
= WMGetTextFieldText(panel
->acceT
);
150 if (sscanf(tmp
, "%f", &accel
)!=1 || accel
< 0) {
151 WMRunAlertPanel(WMWidgetScreen(panel
->acceT
), GetWindow(panel
),
153 _("Invalid mouse acceleration value. Must be a positive real value."),
154 _("OK"), NULL
, NULL
);
158 panel
->acceleration
= accel
;
161 i
= (int)WMGetSliderValue(panel
->speedS
);
163 panel
->acceleration
= 0.25+(i
*0.25);
165 sprintf(buffer
, "%.2f", 0.25+(i
*0.25));
166 WMSetTextFieldText(panel
->acceT
, buffer
);
169 tmp
= WMGetTextFieldText(panel
->threT
);
170 if (sscanf(tmp
, "%i", &threshold
)!=1 || threshold
< 0
171 || threshold
> panel
->maxThreshold
) {
172 WMRunAlertPanel(WMWidgetScreen(panel
->win
), GetWindow(panel
), _("Error"),
173 _("Invalid mouse acceleration threshold value. Must be the number of pixels to travel before accelerating."),
174 _("OK"), NULL
, NULL
);
176 setMouseAccel(WMWidgetScreen(panel
->win
), panel
->acceleration
,
184 returnPressed(void *observerData
, WMNotification
*notification
)
186 _Panel
*panel
= (_Panel
*)observerData
;
188 speedChange(NULL
, panel
);
193 doubleClick(WMWidget
*w
, void *data
)
195 _Panel
*panel
= (_Panel
*)data
;
197 extern _WINGsConfiguration WINGsConfiguration
;
200 for (i
=0; i
<5; i
++) {
201 if (panel
->ddelaB
[i
]==w
)
204 WINGsConfiguration
.doubleClickDelay
= DELAY(i
);
206 sprintf(buffer
, "%i", DELAY(i
));
207 WMSetTextFieldText(panel
->ddelaT
, buffer
);
218 if (strcasecmp(str
, "left")==0)
220 else if (strcasecmp(str
, "middle")==0)
222 else if (strcasecmp(str
, "right")==0)
224 else if (strcasecmp(str
, "button1")==0)
226 else if (strcasecmp(str
, "button2")==0)
228 else if (strcasecmp(str
, "button3")==0)
230 else if (strcasecmp(str
, "button4")==0)
232 else if (strcasecmp(str
, "button5")==0) {
241 getMouseParameters(Display
*dpy
, float *accel
, int *thre
)
245 XGetPointerControl(dpy
, &n
, &d
, thre
);
247 *accel
= (float)n
/(float)d
;
253 showData(_Panel
*panel
)
257 int a
=-1, b
=-1, c
=-1;
260 Display
*dpy
= WMScreenDisplay(WMWidgetScreen(panel
->win
));
262 str
= GetStringForKey("SelectWindowsMouseButton");
267 wwarning(_("bad value %s for option %s"),str
, "SelectWindowsMouseButton");
274 WMSetPopUpButtonSelectedItem(panel
->selP
, a
);
276 str
= GetStringForKey("WindowListMouseButton");
281 wwarning(_("bad value %s for option %s"), str
, "WindowListMouseButton");
288 WMSetPopUpButtonSelectedItem(panel
->listP
, b
);
290 str
= GetStringForKey("ApplicationMenuMouseButton");
295 wwarning(_("bad value %s for option %s"), str
, "ApplicationMenuMouseButton");
302 WMSetPopUpButtonSelectedItem(panel
->appP
, c
);
304 WMSetButtonSelected(panel
->disaB
, GetBoolForKey("DisableWSMouseActions"));
307 getMouseParameters(dpy
, &accel
, &a
);
308 panel
->maxThreshold
= WidthOfScreen(DefaultScreenOfDisplay(dpy
));
309 if (a
> panel
->maxThreshold
) {
310 panel
->maxThreshold
= a
;
312 sprintf(buffer
, "%i", a
);
313 WMSetTextFieldText(panel
->threT
, buffer
);
315 WMSetSliderValue(panel
->speedS
, (accel
- 0.25)/0.25);
317 panel
->acceleration
= accel
;
318 sprintf(buffer
, "%.2f", accel
);
319 WMSetTextFieldText(panel
->acceT
, buffer
);
322 b
= GetIntegerForKey("DoubleClickTime");
323 /* find best match */
325 for (i
=0; i
<5; i
++) {
330 WMPerformButtonClick(panel
->ddelaB
[a
]);
331 sprintf(buffer
, "%i", b
);
332 WMSetTextFieldText(panel
->ddelaT
, buffer
);
335 str
= GetStringForKey("ModifierKey");
338 a
= ModifierFromKey(dpy
, str
);
341 str
= modifierNames
[a
];
344 for (i
=0; i
<WMGetPopUpButtonNumberOfItems(panel
->grabP
); i
++) {
345 if (strstr(WMGetPopUpButtonItem(panel
->grabP
, i
), str
)) {
346 WMSetPopUpButtonSelectedItem(panel
->grabP
, i
);
354 sscanf(WMGetPopUpButtonItem(panel
->grabP
, 0), "%s", buffer
);
355 WMSetPopUpButtonSelectedItem(panel
->grabP
, 0);
356 wwarning(_("modifier key %s for option ModifierKey was not recognized. Using %s as default"),
364 fillModifierPopUp(WMPopUpButton
*pop
)
366 XModifierKeymap
*mapping
;
367 Display
*dpy
= WMScreenDisplay(WMWidgetScreen(pop
));
373 mapping
= XGetModifierMapping(dpy
);
375 if (!mapping
|| mapping
->max_keypermod
==0) {
376 WMAddPopUpButtonItem(pop
, "Mod1");
377 WMAddPopUpButtonItem(pop
, "Mod2");
378 WMAddPopUpButtonItem(pop
, "Mod3");
379 WMAddPopUpButtonItem(pop
, "Mod4");
380 WMAddPopUpButtonItem(pop
, "Mod5");
381 wwarning(_("could not retrieve keyboard modifier mapping"));
386 for (j
=0; j
<8; j
++) {
396 memset(array
, 0, sizeof(char*)*8);
397 for (i
=0; i
< mapping
->max_keypermod
; i
++) {
398 idx
= i
+j
*mapping
->max_keypermod
;
399 if (mapping
->modifiermap
[idx
]!=0) {
401 for (l
=0; l
<4; l
++) {
402 ksym
= XKeycodeToKeysym(dpy
, mapping
->modifiermap
[idx
], l
);
407 str
= XKeysymToString(ksym
);
410 if (str
&& !strstr(str
, "_Lock") && !strstr(str
, "Shift")
411 && !strstr(str
, "Control")) {
412 array
[a
++] = wstrdup(str
);
417 for (k
=0; k
<a
; k
++) {
420 tmp
= wstrdup(array
[k
]);
421 ptr
= strstr(tmp
, "_L");
424 ptr
= strstr(tmp
, "_R");
427 sprintf(buffer
, "%s (%s)", modifierNames
[j
], tmp
);
428 WMAddPopUpButtonItem(pop
, buffer
);
429 for (i
=k
+1; i
<a
; i
++) {
430 if (array
[i
] == NULL
)
432 if (strstr(array
[i
], tmp
)) {
448 XFreeModifiermap(mapping
);
454 createPanel(Panel
*p
)
456 _Panel
*panel
= (_Panel
*)p
;
457 WMScreen
*scr
= WMWidgetScreen(panel
->win
);
468 panel
->frame
= WMCreateFrame(panel
->win
);
469 WMResizeWidget(panel
->frame
, FRAME_WIDTH
, FRAME_HEIGHT
);
470 WMMoveWidget(panel
->frame
, FRAME_LEFT
, FRAME_TOP
);
472 /**************** Mouse Speed ****************/
473 panel
->speedF
= WMCreateFrame(panel
->frame
);
474 WMResizeWidget(panel
->speedF
, 245, 100);
475 WMMoveWidget(panel
->speedF
, 15, 15);
476 WMSetFrameTitle(panel
->speedF
, _("Mouse Speed"));
478 panel
->speedL
= WMCreateLabel(panel
->speedF
);
479 WMResizeWidget(panel
->speedL
, 40, 46);
480 WMMoveWidget(panel
->speedL
, 15, 14);
481 WMSetLabelImagePosition(panel
->speedL
, WIPImageOnly
);
482 path
= LocateImage(SPEED_ICON_FILE
);
484 icon
= WMCreateBlendedPixmapFromFile(scr
, path
, &color
);
486 WMSetLabelImage(panel
->speedL
, icon
);
487 WMReleasePixmap(icon
);
489 wwarning(_("could not load icon %s"), path
);
494 panel
->speedS
= WMCreateSlider(panel
->speedF
);
495 WMResizeWidget(panel
->speedS
, 160, 15);
496 WMMoveWidget(panel
->speedS
, 70, 35);
497 WMSetSliderMinValue(panel
->speedS
, 0);
498 WMSetSliderMaxValue(panel
->speedS
, 40);
499 WMSetSliderContinuous(panel
->speedS
, False
);
500 WMSetSliderAction(panel
->speedS
, speedChange
, panel
);
502 panel
->acceL
= WMCreateLabel(panel
->speedF
);
503 WMResizeWidget(panel
->acceL
, 70, 16);
504 WMMoveWidget(panel
->acceL
, 10, 67);
505 WMSetLabelTextAlignment(panel
->acceL
, WARight
);
506 WMSetLabelText(panel
->acceL
, _("Acceler.:"));
508 panel
->acceT
= WMCreateTextField(panel
->speedF
);
509 WMResizeWidget(panel
->acceT
, 40, 20);
510 WMMoveWidget(panel
->acceT
, 80, 65);
511 WMAddNotificationObserver(returnPressed
, panel
,
512 WMTextDidEndEditingNotification
, panel
->acceT
);
515 panel
->threL
= WMCreateLabel(panel
->speedF
);
516 WMResizeWidget(panel
->threL
, 80, 16);
517 WMMoveWidget(panel
->threL
, 120, 67);
518 WMSetLabelTextAlignment(panel
->threL
, WARight
);
519 WMSetLabelText(panel
->threL
, _("Threshold:"));
521 panel
->threT
= WMCreateTextField(panel
->speedF
);
522 WMResizeWidget(panel
->threT
, 30, 20);
523 WMMoveWidget(panel
->threT
, 200, 65);
524 WMAddNotificationObserver(returnPressed
, panel
,
525 WMTextDidEndEditingNotification
, panel
->threT
);
527 WMMapSubwidgets(panel
->speedF
);
529 /***************** Doubleclick Delay ****************/
531 panel
->ddelaF
= WMCreateFrame(panel
->frame
);
532 WMResizeWidget(panel
->ddelaF
, 245, 95);
533 WMMoveWidget(panel
->ddelaF
, 15, 125);
534 WMSetFrameTitle(panel
->ddelaF
, _("Double-Click Delay"));
536 buf1
= wmalloc(strlen(DELAY_ICON
)+2);
537 buf2
= wmalloc(strlen(DELAY_ICON_S
)+2);
539 for (i
= 0; i
< 5; i
++) {
540 panel
->ddelaB
[i
] = WMCreateCustomButton(panel
->ddelaF
,
542 WMResizeWidget(panel
->ddelaB
[i
], 25, 25);
543 WMMoveWidget(panel
->ddelaB
[i
], 30+(40*i
), 20);
544 WMSetButtonBordered(panel
->ddelaB
[i
], False
);
545 WMSetButtonImagePosition(panel
->ddelaB
[i
], WIPImageOnly
);
546 WMSetButtonAction(panel
->ddelaB
[i
], doubleClick
, panel
);
548 WMGroupButtons(panel
->ddelaB
[0], panel
->ddelaB
[i
]);
550 sprintf(buf1
, DELAY_ICON
, i
+1);
551 sprintf(buf2
, DELAY_ICON_S
, i
+1);
552 path
= LocateImage(buf1
);
554 icon
= WMCreatePixmapFromFile(scr
, path
);
556 WMSetButtonImage(panel
->ddelaB
[i
], icon
);
557 WMReleasePixmap(icon
);
559 wwarning(_("could not load icon file %s"), path
);
563 path
= LocateImage(buf2
);
565 icon
= WMCreatePixmapFromFile(scr
, path
);
567 WMSetButtonAltImage(panel
->ddelaB
[i
], icon
);
568 WMReleasePixmap(icon
);
570 wwarning(_("could not load icon file %s"), path
);
578 panel
->tester
= CreateDoubleTest(panel
->ddelaF
, _("Test"));
579 WMResizeWidget(panel
->tester
, 84, 29);
580 WMMoveWidget(panel
->tester
, 35, 55);
582 panel
->ddelaT
= WMCreateTextField(panel
->ddelaF
);
583 WMResizeWidget(panel
->ddelaT
, 40, 20);
584 WMMoveWidget(panel
->ddelaT
, 140, 60);
586 panel
->ddelaL
= WMCreateLabel(panel
->ddelaF
);
587 WMResizeWidget(panel
->ddelaL
, 40, 16);
588 WMMoveWidget(panel
->ddelaL
, 185, 65);
593 font
= WMSystemFontOfSize(scr
, 10);
594 color
= WMDarkGrayColor(scr
);
595 WMSetLabelTextColor(panel
->ddelaL
, color
);
596 WMSetLabelFont(panel
->ddelaL
, font
);
598 WMReleaseColor(color
);
600 WMSetLabelText(panel
->ddelaL
, "msec");
602 WMMapSubwidgets(panel
->ddelaF
);
604 /* ************** Workspace Action Buttons **************** */
605 panel
->menuF
= WMCreateFrame(panel
->frame
);
606 WMResizeWidget(panel
->menuF
, 240, 145);
607 WMMoveWidget(panel
->menuF
, 270, 15);
608 WMSetFrameTitle(panel
->menuF
, _("Workspace Mouse Actions"));
610 panel
->disaB
= WMCreateSwitchButton(panel
->menuF
);
611 WMResizeWidget(panel
->disaB
, 205, 18);
612 WMMoveWidget(panel
->disaB
, 10, 20);
613 WMSetButtonText(panel
->disaB
, _("Disable mouse actions"));
616 panel
->appL
= WMCreateLabel(panel
->menuF
);
617 WMResizeWidget(panel
->appL
, 125, 16);
618 WMMoveWidget(panel
->appL
, 5, 45);
619 WMSetLabelTextAlignment(panel
->appL
, WARight
);
620 WMSetLabelText(panel
->appL
, _("Applications menu"));
622 panel
->appP
= WMCreatePopUpButton(panel
->menuF
);
623 WMResizeWidget(panel
->appP
, 95, 20);
624 WMMoveWidget(panel
->appP
, 135, 45);
626 panel
->listL
= WMCreateLabel(panel
->menuF
);
627 WMResizeWidget(panel
->listL
, 125, 16);
628 WMMoveWidget(panel
->listL
, 5, 80);
629 WMSetLabelTextAlignment(panel
->listL
, WARight
);
630 WMSetLabelText(panel
->listL
, _("Window list menu"));
632 panel
->listP
= WMCreatePopUpButton(panel
->menuF
);
633 WMResizeWidget(panel
->listP
, 95, 20);
634 WMMoveWidget(panel
->listP
, 135, 80);
637 panel
->selL
= WMCreateLabel(panel
->menuF
);
638 WMResizeWidget(panel
->selL
, 125, 16);
639 WMMoveWidget(panel
->selL
, 5, 115);
640 WMSetLabelTextAlignment(panel
->selL
, WARight
);
641 WMSetLabelText(panel
->selL
, _("Select windows"));
643 panel
->selP
= WMCreatePopUpButton(panel
->menuF
);
644 WMResizeWidget(panel
->selP
, 95, 20);
645 WMMoveWidget(panel
->selP
, 135, 115);
647 for (i
= 0; i
< sizeof(buttonNames
)/sizeof(char*); i
++) {
648 WMAddPopUpButtonItem(panel
->appP
, buttonNames
[i
]);
649 WMAddPopUpButtonItem(panel
->selP
, buttonNames
[i
]);
650 WMAddPopUpButtonItem(panel
->listP
, buttonNames
[i
]);
653 WMMapSubwidgets(panel
->menuF
);
655 /* ************** Grab Modifier **************** */
656 panel
->grabF
= WMCreateFrame(panel
->frame
);
657 WMResizeWidget(panel
->grabF
, 240, 55);
658 WMMoveWidget(panel
->grabF
, 270, 165);
659 WMSetFrameTitle(panel
->grabF
, _("Mouse Grab Modifier"));
661 WMSetBalloonTextForView(_("Keyboard modifier to use for actions that\n"
662 "involve dragging windows with the mouse,\n"
663 "clicking inside the window."),
664 WMWidgetView(panel
->grabF
));
666 panel
->grabP
= WMCreatePopUpButton(panel
->grabF
);
667 WMResizeWidget(panel
->grabP
, 160, 20);
668 WMMoveWidget(panel
->grabP
, 40, 25);
670 fillModifierPopUp(panel
->grabP
);
672 WMMapSubwidgets(panel
->grabF
);
674 WMRealizeWidget(panel
->frame
);
675 WMMapSubwidgets(panel
->frame
);
683 storeCommandInScript(char *cmd
, char *line
)
689 path
= wstrappend(wusergnusteppath(), "/Library/WindowMaker/autostart");
691 f
= fopen(path
, "r");
693 f
= fopen(path
, "w");
695 wsyserror(_("could not create %s"), path
);
698 fprintf(f
, "#!/bin/sh\n");
702 int len
= strlen(cmd
);
707 tmppath
= wstrappend(wusergnusteppath(),
708 "/Library/WindowMaker/autostart.tmp");
709 fo
= fopen(tmppath
, "w");
711 wsyserror(_("could not create temporary file %s"), tmppath
);
716 if (!fgets(buffer
, 127, f
)) {
719 if (buffer
[0] == '\n') {
720 /* don't write empty lines, else the file will grow
721 * indefinitely (one '\n' added at end of file on each save).
725 if (strncmp(buffer
, cmd
, len
)==0) {
741 if (rename(tmppath
, path
)!=0) {
742 wsyserror(_("could not rename file %s to %s\n"), tmppath
, path
);
746 sprintf(buffer
, "chmod u+x %s", path
);
757 storeData(_Panel
*panel
)
762 static char *button
[6] = {"None", "left", "middle", "right", "Button4", "Button5"};
763 WMUserDefaults
*udb
= WMGetStandardUserDefaults();
765 if (!WMGetUDBoolForKey(udb
, "NoXSetStuff")) {
766 tmp
= WMGetTextFieldText(panel
->threT
);
767 if (strlen(tmp
)==0) {
772 sprintf(buffer
, XSET
" m %i/%i %s\n", (int)(panel
->acceleration
*10),
774 storeCommandInScript(XSET
" m", buffer
);
779 tmp
= WMGetTextFieldText(panel
->ddelaT
);
780 if (sscanf(tmp
, "%i", &i
) == 1 && i
> 0)
781 SetIntegerForKey(i
, "DoubleClickTime");
783 SetBoolForKey(WMGetButtonSelected(panel
->disaB
), "DisableWSMouseActions");
785 i
= WMGetPopUpButtonSelectedItem(panel
->appP
);
786 SetStringForKey(button
[i
], "ApplicationMenuMouseButton");
788 i
= WMGetPopUpButtonSelectedItem(panel
->listP
);
789 SetStringForKey(button
[i
], "WindowListMouseButton");
791 i
= WMGetPopUpButtonSelectedItem(panel
->selP
);
792 SetStringForKey(button
[i
], "SelectWindowsMouseButton");
794 tmp
= WMGetPopUpButtonItem(panel
->grabP
,
795 WMGetPopUpButtonSelectedItem(panel
->grabP
));
797 p
= strchr(tmp
, ' ');
800 SetStringForKey(tmp
, "ModifierKey");
807 InitMouseSettings(WMScreen
*scr
, WMWindow
*win
)
811 panel
= wmalloc(sizeof(_Panel
));
812 memset(panel
, 0, sizeof(_Panel
));
814 panel
->sectionName
= _("Mouse Preferences");
816 panel
->description
= _("Mouse speed/acceleration, double click delay,\n"
817 "mouse button bindings etc.");
821 panel
->callbacks
.createWidgets
= createPanel
;
822 panel
->callbacks
.updateDomain
= storeData
;
824 AddSection(panel
, ICON_FILE
);