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
, "none")==0)
220 else if (strcasecmp(str
, "left")==0)
222 else if (strcasecmp(str
, "middle")==0)
224 else if (strcasecmp(str
, "right")==0)
226 else if (strcasecmp(str
, "button1")==0)
228 else if (strcasecmp(str
, "button2")==0)
230 else if (strcasecmp(str
, "button3")==0)
232 else if (strcasecmp(str
, "button4")==0)
234 else if (strcasecmp(str
, "button5")==0) {
243 getMouseParameters(Display
*dpy
, float *accel
, int *thre
)
247 XGetPointerControl(dpy
, &n
, &d
, thre
);
249 *accel
= (float)n
/(float)d
;
255 showData(_Panel
*panel
)
259 int a
=-1, b
=-1, c
=-1;
262 Display
*dpy
= WMScreenDisplay(WMWidgetScreen(panel
->win
));
264 str
= GetStringForKey("SelectWindowsMouseButton");
269 wwarning(_("bad value %s for option %s"),str
, "SelectWindowsMouseButton");
276 WMSetPopUpButtonSelectedItem(panel
->selP
, a
);
278 str
= GetStringForKey("WindowListMouseButton");
283 wwarning(_("bad value %s for option %s"), str
, "WindowListMouseButton");
290 WMSetPopUpButtonSelectedItem(panel
->listP
, b
);
292 str
= GetStringForKey("ApplicationMenuMouseButton");
297 wwarning(_("bad value %s for option %s"), str
, "ApplicationMenuMouseButton");
304 WMSetPopUpButtonSelectedItem(panel
->appP
, c
);
306 WMSetButtonSelected(panel
->disaB
, GetBoolForKey("DisableWSMouseActions"));
309 getMouseParameters(dpy
, &accel
, &a
);
310 panel
->maxThreshold
= WidthOfScreen(DefaultScreenOfDisplay(dpy
));
311 if (a
> panel
->maxThreshold
) {
312 panel
->maxThreshold
= a
;
314 sprintf(buffer
, "%i", a
);
315 WMSetTextFieldText(panel
->threT
, buffer
);
317 WMSetSliderValue(panel
->speedS
, (accel
- 0.25)/0.25);
319 panel
->acceleration
= accel
;
320 sprintf(buffer
, "%.2f", accel
);
321 WMSetTextFieldText(panel
->acceT
, buffer
);
324 b
= GetIntegerForKey("DoubleClickTime");
325 /* find best match */
327 for (i
=0; i
<5; i
++) {
332 WMPerformButtonClick(panel
->ddelaB
[a
]);
333 sprintf(buffer
, "%i", b
);
334 WMSetTextFieldText(panel
->ddelaT
, buffer
);
337 str
= GetStringForKey("ModifierKey");
340 a
= ModifierFromKey(dpy
, str
);
343 str
= modifierNames
[a
];
346 for (i
=0; i
<WMGetPopUpButtonNumberOfItems(panel
->grabP
); i
++) {
347 if (strstr(WMGetPopUpButtonItem(panel
->grabP
, i
), str
)) {
348 WMSetPopUpButtonSelectedItem(panel
->grabP
, i
);
356 sscanf(WMGetPopUpButtonItem(panel
->grabP
, 0), "%s", buffer
);
357 WMSetPopUpButtonSelectedItem(panel
->grabP
, 0);
358 wwarning(_("modifier key %s for option ModifierKey was not recognized. Using %s as default"),
366 fillModifierPopUp(WMPopUpButton
*pop
)
368 XModifierKeymap
*mapping
;
369 Display
*dpy
= WMScreenDisplay(WMWidgetScreen(pop
));
375 mapping
= XGetModifierMapping(dpy
);
377 if (!mapping
|| mapping
->max_keypermod
==0) {
378 WMAddPopUpButtonItem(pop
, "Mod1");
379 WMAddPopUpButtonItem(pop
, "Mod2");
380 WMAddPopUpButtonItem(pop
, "Mod3");
381 WMAddPopUpButtonItem(pop
, "Mod4");
382 WMAddPopUpButtonItem(pop
, "Mod5");
383 wwarning(_("could not retrieve keyboard modifier mapping"));
388 for (j
=0; j
<8; j
++) {
398 memset(array
, 0, sizeof(char*)*8);
399 for (i
=0; i
< mapping
->max_keypermod
; i
++) {
400 idx
= i
+j
*mapping
->max_keypermod
;
401 if (mapping
->modifiermap
[idx
]!=0) {
403 for (l
=0; l
<4; l
++) {
404 ksym
= XKeycodeToKeysym(dpy
, mapping
->modifiermap
[idx
], l
);
409 str
= XKeysymToString(ksym
);
412 if (str
&& !strstr(str
, "_Lock") && !strstr(str
, "Shift")
413 && !strstr(str
, "Control")) {
414 array
[a
++] = wstrdup(str
);
419 for (k
=0; k
<a
; k
++) {
422 tmp
= wstrdup(array
[k
]);
423 ptr
= strstr(tmp
, "_L");
426 ptr
= strstr(tmp
, "_R");
429 sprintf(buffer
, "%s (%s)", modifierNames
[j
], tmp
);
430 WMAddPopUpButtonItem(pop
, buffer
);
431 for (i
=k
+1; i
<a
; i
++) {
432 if (array
[i
] == NULL
)
434 if (strstr(array
[i
], tmp
)) {
450 XFreeModifiermap(mapping
);
456 createPanel(Panel
*p
)
458 _Panel
*panel
= (_Panel
*)p
;
459 WMScreen
*scr
= WMWidgetScreen(panel
->win
);
470 panel
->frame
= WMCreateFrame(panel
->win
);
471 WMResizeWidget(panel
->frame
, FRAME_WIDTH
, FRAME_HEIGHT
);
472 WMMoveWidget(panel
->frame
, FRAME_LEFT
, FRAME_TOP
);
474 /**************** Mouse Speed ****************/
475 panel
->speedF
= WMCreateFrame(panel
->frame
);
476 WMResizeWidget(panel
->speedF
, 245, 100);
477 WMMoveWidget(panel
->speedF
, 15, 15);
478 WMSetFrameTitle(panel
->speedF
, _("Mouse Speed"));
480 panel
->speedL
= WMCreateLabel(panel
->speedF
);
481 WMResizeWidget(panel
->speedL
, 40, 46);
482 WMMoveWidget(panel
->speedL
, 15, 14);
483 WMSetLabelImagePosition(panel
->speedL
, WIPImageOnly
);
484 path
= LocateImage(SPEED_ICON_FILE
);
486 icon
= WMCreateBlendedPixmapFromFile(scr
, path
, &color
);
488 WMSetLabelImage(panel
->speedL
, icon
);
489 WMReleasePixmap(icon
);
491 wwarning(_("could not load icon %s"), path
);
496 panel
->speedS
= WMCreateSlider(panel
->speedF
);
497 WMResizeWidget(panel
->speedS
, 160, 15);
498 WMMoveWidget(panel
->speedS
, 70, 35);
499 WMSetSliderMinValue(panel
->speedS
, 0);
500 WMSetSliderMaxValue(panel
->speedS
, 40);
501 WMSetSliderContinuous(panel
->speedS
, False
);
502 WMSetSliderAction(panel
->speedS
, speedChange
, panel
);
504 panel
->acceL
= WMCreateLabel(panel
->speedF
);
505 WMResizeWidget(panel
->acceL
, 70, 16);
506 WMMoveWidget(panel
->acceL
, 10, 67);
507 WMSetLabelTextAlignment(panel
->acceL
, WARight
);
508 WMSetLabelText(panel
->acceL
, _("Acceler.:"));
510 panel
->acceT
= WMCreateTextField(panel
->speedF
);
511 WMResizeWidget(panel
->acceT
, 40, 20);
512 WMMoveWidget(panel
->acceT
, 80, 65);
513 WMAddNotificationObserver(returnPressed
, panel
,
514 WMTextDidEndEditingNotification
, panel
->acceT
);
517 panel
->threL
= WMCreateLabel(panel
->speedF
);
518 WMResizeWidget(panel
->threL
, 80, 16);
519 WMMoveWidget(panel
->threL
, 120, 67);
520 WMSetLabelTextAlignment(panel
->threL
, WARight
);
521 WMSetLabelText(panel
->threL
, _("Threshold:"));
523 panel
->threT
= WMCreateTextField(panel
->speedF
);
524 WMResizeWidget(panel
->threT
, 30, 20);
525 WMMoveWidget(panel
->threT
, 200, 65);
526 WMAddNotificationObserver(returnPressed
, panel
,
527 WMTextDidEndEditingNotification
, panel
->threT
);
529 WMMapSubwidgets(panel
->speedF
);
531 /***************** Doubleclick Delay ****************/
533 panel
->ddelaF
= WMCreateFrame(panel
->frame
);
534 WMResizeWidget(panel
->ddelaF
, 245, 95);
535 WMMoveWidget(panel
->ddelaF
, 15, 125);
536 WMSetFrameTitle(panel
->ddelaF
, _("Double-Click Delay"));
538 buf1
= wmalloc(strlen(DELAY_ICON
)+2);
539 buf2
= wmalloc(strlen(DELAY_ICON_S
)+2);
541 for (i
= 0; i
< 5; i
++) {
542 panel
->ddelaB
[i
] = WMCreateCustomButton(panel
->ddelaF
,
544 WMResizeWidget(panel
->ddelaB
[i
], 25, 25);
545 WMMoveWidget(panel
->ddelaB
[i
], 30+(40*i
), 20);
546 WMSetButtonBordered(panel
->ddelaB
[i
], False
);
547 WMSetButtonImagePosition(panel
->ddelaB
[i
], WIPImageOnly
);
548 WMSetButtonAction(panel
->ddelaB
[i
], doubleClick
, panel
);
550 WMGroupButtons(panel
->ddelaB
[0], panel
->ddelaB
[i
]);
552 sprintf(buf1
, DELAY_ICON
, i
+1);
553 sprintf(buf2
, DELAY_ICON_S
, i
+1);
554 path
= LocateImage(buf1
);
556 icon
= WMCreatePixmapFromFile(scr
, path
);
558 WMSetButtonImage(panel
->ddelaB
[i
], icon
);
559 WMReleasePixmap(icon
);
561 wwarning(_("could not load icon file %s"), path
);
565 path
= LocateImage(buf2
);
567 icon
= WMCreatePixmapFromFile(scr
, path
);
569 WMSetButtonAltImage(panel
->ddelaB
[i
], icon
);
570 WMReleasePixmap(icon
);
572 wwarning(_("could not load icon file %s"), path
);
580 panel
->tester
= CreateDoubleTest(panel
->ddelaF
, _("Test"));
581 WMResizeWidget(panel
->tester
, 84, 29);
582 WMMoveWidget(panel
->tester
, 35, 55);
584 panel
->ddelaT
= WMCreateTextField(panel
->ddelaF
);
585 WMResizeWidget(panel
->ddelaT
, 40, 20);
586 WMMoveWidget(panel
->ddelaT
, 140, 60);
588 panel
->ddelaL
= WMCreateLabel(panel
->ddelaF
);
589 WMResizeWidget(panel
->ddelaL
, 40, 16);
590 WMMoveWidget(panel
->ddelaL
, 185, 65);
595 font
= WMSystemFontOfSize(scr
, 10);
596 color
= WMDarkGrayColor(scr
);
597 WMSetLabelTextColor(panel
->ddelaL
, color
);
598 WMSetLabelFont(panel
->ddelaL
, font
);
600 WMReleaseColor(color
);
602 WMSetLabelText(panel
->ddelaL
, "msec");
604 WMMapSubwidgets(panel
->ddelaF
);
606 /* ************** Workspace Action Buttons **************** */
607 panel
->menuF
= WMCreateFrame(panel
->frame
);
608 WMResizeWidget(panel
->menuF
, 240, 145);
609 WMMoveWidget(panel
->menuF
, 270, 15);
610 WMSetFrameTitle(panel
->menuF
, _("Workspace Mouse Actions"));
612 panel
->disaB
= WMCreateSwitchButton(panel
->menuF
);
613 WMResizeWidget(panel
->disaB
, 205, 18);
614 WMMoveWidget(panel
->disaB
, 10, 20);
615 WMSetButtonText(panel
->disaB
, _("Disable mouse actions"));
618 panel
->appL
= WMCreateLabel(panel
->menuF
);
619 WMResizeWidget(panel
->appL
, 125, 16);
620 WMMoveWidget(panel
->appL
, 5, 45);
621 WMSetLabelTextAlignment(panel
->appL
, WARight
);
622 WMSetLabelText(panel
->appL
, _("Applications menu"));
624 panel
->appP
= WMCreatePopUpButton(panel
->menuF
);
625 WMResizeWidget(panel
->appP
, 95, 20);
626 WMMoveWidget(panel
->appP
, 135, 45);
628 panel
->listL
= WMCreateLabel(panel
->menuF
);
629 WMResizeWidget(panel
->listL
, 125, 16);
630 WMMoveWidget(panel
->listL
, 5, 80);
631 WMSetLabelTextAlignment(panel
->listL
, WARight
);
632 WMSetLabelText(panel
->listL
, _("Window list menu"));
634 panel
->listP
= WMCreatePopUpButton(panel
->menuF
);
635 WMResizeWidget(panel
->listP
, 95, 20);
636 WMMoveWidget(panel
->listP
, 135, 80);
639 panel
->selL
= WMCreateLabel(panel
->menuF
);
640 WMResizeWidget(panel
->selL
, 125, 16);
641 WMMoveWidget(panel
->selL
, 5, 115);
642 WMSetLabelTextAlignment(panel
->selL
, WARight
);
643 WMSetLabelText(panel
->selL
, _("Select windows"));
645 panel
->selP
= WMCreatePopUpButton(panel
->menuF
);
646 WMResizeWidget(panel
->selP
, 95, 20);
647 WMMoveWidget(panel
->selP
, 135, 115);
649 for (i
= 0; i
< sizeof(buttonNames
)/sizeof(char*); i
++) {
650 WMAddPopUpButtonItem(panel
->appP
, buttonNames
[i
]);
651 WMAddPopUpButtonItem(panel
->selP
, buttonNames
[i
]);
652 WMAddPopUpButtonItem(panel
->listP
, buttonNames
[i
]);
655 WMMapSubwidgets(panel
->menuF
);
657 /* ************** Grab Modifier **************** */
658 panel
->grabF
= WMCreateFrame(panel
->frame
);
659 WMResizeWidget(panel
->grabF
, 240, 55);
660 WMMoveWidget(panel
->grabF
, 270, 165);
661 WMSetFrameTitle(panel
->grabF
, _("Mouse Grab Modifier"));
663 WMSetBalloonTextForView(_("Keyboard modifier to use for actions that\n"
664 "involve dragging windows with the mouse,\n"
665 "clicking inside the window."),
666 WMWidgetView(panel
->grabF
));
668 panel
->grabP
= WMCreatePopUpButton(panel
->grabF
);
669 WMResizeWidget(panel
->grabP
, 160, 20);
670 WMMoveWidget(panel
->grabP
, 40, 25);
672 fillModifierPopUp(panel
->grabP
);
674 WMMapSubwidgets(panel
->grabF
);
676 WMRealizeWidget(panel
->frame
);
677 WMMapSubwidgets(panel
->frame
);
685 storeCommandInScript(char *cmd
, char *line
)
691 path
= wstrappend(wusergnusteppath(), "/Library/WindowMaker/autostart");
693 f
= fopen(path
, "r");
695 f
= fopen(path
, "w");
697 wsyserror(_("could not create %s"), path
);
700 fprintf(f
, "#!/bin/sh\n");
704 int len
= strlen(cmd
);
709 tmppath
= wstrappend(wusergnusteppath(),
710 "/Library/WindowMaker/autostart.tmp");
711 fo
= fopen(tmppath
, "w");
713 wsyserror(_("could not create temporary file %s"), tmppath
);
719 if (!fgets(buffer
, 127, f
)) {
722 if (buffer
[0] == '\n') {
723 /* don't write empty lines, else the file will grow
724 * indefinitely (one '\n' added at end of file on each save).
728 if (strncmp(buffer
, cmd
, len
)==0) {
744 if (rename(tmppath
, path
)!=0) {
745 wsyserror(_("could not rename file %s to %s\n"), tmppath
, path
);
749 sprintf(buffer
, "chmod u+x %s", path
);
760 storeData(_Panel
*panel
)
765 static char *button
[6] = {"None", "left", "middle", "right", "Button4", "Button5"};
766 WMUserDefaults
*udb
= WMGetStandardUserDefaults();
768 if (!WMGetUDBoolForKey(udb
, "NoXSetStuff")) {
769 tmp
= WMGetTextFieldText(panel
->threT
);
770 if (strlen(tmp
)==0) {
775 sprintf(buffer
, XSET
" m %i/%i %s\n", (int)(panel
->acceleration
*10),
777 storeCommandInScript(XSET
" m", buffer
);
782 tmp
= WMGetTextFieldText(panel
->ddelaT
);
783 if (sscanf(tmp
, "%i", &i
) == 1 && i
> 0)
784 SetIntegerForKey(i
, "DoubleClickTime");
786 SetBoolForKey(WMGetButtonSelected(panel
->disaB
), "DisableWSMouseActions");
788 i
= WMGetPopUpButtonSelectedItem(panel
->appP
);
789 SetStringForKey(button
[i
], "ApplicationMenuMouseButton");
791 i
= WMGetPopUpButtonSelectedItem(panel
->listP
);
792 SetStringForKey(button
[i
], "WindowListMouseButton");
794 i
= WMGetPopUpButtonSelectedItem(panel
->selP
);
795 SetStringForKey(button
[i
], "SelectWindowsMouseButton");
797 tmp
= WMGetPopUpButtonItem(panel
->grabP
,
798 WMGetPopUpButtonSelectedItem(panel
->grabP
));
800 p
= strchr(tmp
, ' ');
803 SetStringForKey(tmp
, "ModifierKey");
810 InitMouseSettings(WMScreen
*scr
, WMWindow
*win
)
814 panel
= wmalloc(sizeof(_Panel
));
815 memset(panel
, 0, sizeof(_Panel
));
817 panel
->sectionName
= _("Mouse Preferences");
819 panel
->description
= _("Mouse speed/acceleration, double click delay,\n"
820 "mouse button bindings etc.");
824 panel
->callbacks
.createWidgets
= createPanel
;
825 panel
->callbacks
.updateDomain
= storeData
;
827 AddSection(panel
, ICON_FILE
);