Files
mcde/programs/dtsystemsettings/input.c
T
lotherkandClaude Opus 5.5 c0bf505c0a mcde: keyboard and mouse in System Settings; dtsession keeps them
dtsystemsettings draws the Keyboard pane (key repeat, its delay and rate,
key click, a field to try it) and the Mouse pane (primary button, pointer
speed, threshold, double-click time) instead of opening dtstyle.  A change
takes effect at once through Xlib and goes to dtsession as dtstyle sends
it (_DT_SM_KEYBOARD_INFO, _DT_SM_POINTER_INFO; *multiClickTime through
_DtAddToResource).

dtsession saves and restores the XKB repeat delay and interval
(XkbGetAutoRepeatRate at logout, XkbSetAutoRepeatRate at login), which the
core keyboard state does not carry.  And it keeps every pointer button: the
map was char[5], the parse of the property wrote past it without a bound,
and the restore cut the map to 5, which the server refuses on a mouse with
more buttons.

Measured on Xvfb (build/mcde-shots): the sliders set the server (xset q:
delay 490, rate 29, click 20; acceleration 3/1, multiClickTime 280; xmodmap
-pp 1->3, 3->1).  Through a whole dtsession: logout wrote RepeatDelay 490,
RepeatInterval 34, ButtonMap 3,2,1,4,...,10 (10 buttons), AccelNum 3; the
next login restored delay 490 rate 29, acceleration 3/1 and the swapped
buttons.  The double-click time is saved (dt.resources) but its restore is
not measured: a dtsession started without Xsession gets no resources
loaded (3 in xrdb, against 108 in a real session).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 07:30:58 +00:00

242 lines
8.1 KiB
C

/*
* input.c -- dtsystemsettings: the keyboard and the mouse
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* What CDE's style manager sets for them, in the window itself: key repeat,
* its delay and rate, key click; the primary button, the pointer speed, the
* double-click time. A change takes effect at once, with Xlib --
* XChangeKeyboardControl(3), XkbSetAutoRepeatRate(3), XChangePointerControl(3),
* XSetPointerMapping(3) -- and goes to the session manager the way dtstyle
* sends it (programs/dtstyle/Protocol.c, SmNewKeyboardSettings and
* SmNewPointerSettings): a property on dtsession's window, which dtsession
* writes into the session at logout and sets again at the next login
* (dtsession/SmSave.c, SmRestore.c; the repeat delay and rate are MCDE's
* addition there). The double-click time is a resource, *multiClickTime,
* merged into the server's resources with _DtAddToResource(3) as dtstyle does.
*
* These are settings of the X server for this user's session, not of the
* operating system: no McdeSys call is needed.
*
* C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md).
*/
#include <stdio.h>
#include <string.h>
#include <X11/Intrinsic.h>
#include <X11/Xatom.h>
#include <X11/XKBlib.h>
#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Xm/RowColumn.h>
#include <Xm/Scale.h>
#include <Xm/TextF.h>
#include <Xm/ToggleB.h>
#include <Dt/DtP.h>
#include <Dt/SessionM.h>
#include <Dt/SessionP.h>
#include "ui.h"
/* ---- the session manager (dtstyle/Protocol.c) ---------------------------- */
static Window
SmWindow(Display *dpy)
{
Window w;
if (_DtGetSmWindow(dpy, RootWindow(dpy, 0), &w) != Success)
return None;
return w;
}
/* ---- shared ------------------------------------------------------------- */
static Widget
Scale(Widget parent, const char *name, const char *label, int min, int max,
int value, XtCallbackProc cb)
{
Widget w;
(void) Text(parent, "label", label);
w = XtVaCreateManagedWidget(name, xmScaleWidgetClass, parent,
XmNorientation, XmHORIZONTAL, XmNminimum, min, XmNmaximum, max,
XmNvalue, value < min ? min : value > max ? max : value,
XmNshowValue, True, XmNscaleWidth, 320, NULL);
XtAddCallback(w, XmNvalueChangedCallback, cb, NULL);
return w;
}
static int
ScaleValue(Widget w)
{
int v = 0;
XmScaleGetValue(w, &v);
return v;
}
/* ---- the keyboard ------------------------------------------------------- */
static Widget kbRepeat, kbDelay, kbRate, kbClick;
static void
KeyboardApply(Widget w, XtPointer data, XtPointer call)
{
Display *dpy = XtDisplay(kbRepeat);
XKeyboardControl kc;
PropDtSmKeyboardInfo prop;
Window sm;
Boolean on = XmToggleButtonGetState(kbRepeat);
int rate = ScaleValue(kbRate);
(void) w;
(void) data;
(void) call;
kc.auto_repeat_mode = on ? AutoRepeatModeOn : AutoRepeatModeOff;
kc.key_click_percent = ScaleValue(kbClick);
XChangeKeyboardControl(dpy, KBAutoRepeatMode | KBKeyClickPercent, &kc);
/* rate: keys a second; XKB takes the interval in milliseconds */
(void) XkbSetAutoRepeatRate(dpy, XkbUseCoreKbd,
(unsigned int) ScaleValue(kbDelay),
(unsigned int) (1000 / (rate > 0 ? rate : 1)));
XtSetSensitive(kbDelay, on);
XtSetSensitive(kbRate, on);
if ((sm = SmWindow(dpy)) != None) {
Atom a = XInternAtom(dpy, _XA_DT_SM_KEYBOARD_INFO, False);
prop.flags = 0;
prop.smKeyClickPercent = (unsigned long) kc.key_click_percent;
prop.smGlobalAutoRepeat = (unsigned long) kc.auto_repeat_mode;
XChangeProperty(dpy, sm, a, a, 32, PropModeReplace,
(unsigned char *) &prop, PROP_DT_SM_KEYBOARD_INFO_ELEMENTS);
}
XFlush(dpy);
}
Widget
MakeKeyboard(Widget parent)
{
Display *dpy = XtDisplay(parent);
Widget box = XtVaCreateWidget("keyboard", xmRowColumnWidgetClass, parent,
XmNspacing, 6, NULL);
XKeyboardState ks;
unsigned int delay = 660, interval = 40;
Widget field;
XGetKeyboardControl(dpy, &ks);
(void) XkbGetAutoRepeatRate(dpy, XkbUseCoreKbd, &delay, &interval);
kbRepeat = XtVaCreateManagedWidget("Keys repeat when held down",
xmToggleButtonWidgetClass, box,
XmNset, ks.global_auto_repeat == AutoRepeatModeOn, NULL);
XtAddCallback(kbRepeat, XmNvalueChangedCallback, KeyboardApply, NULL);
kbDelay = Scale(box, "delay", "Delay until repeat (milliseconds):",
150, 1000, (int) delay, KeyboardApply);
kbRate = Scale(box, "rate", "Repeat rate (keys a second):", 2, 50,
interval > 0 ? (int) ((1000 + interval / 2) / interval) : 25,
KeyboardApply);
kbClick = Scale(box, "click", "Key click volume:", 0, 100,
ks.key_click_percent, KeyboardApply);
XtSetSensitive(kbDelay, ks.global_auto_repeat == AutoRepeatModeOn);
XtSetSensitive(kbRate, ks.global_auto_repeat == AutoRepeatModeOn);
(void) Text(box, "label", "Try it here:");
field = XtVaCreateManagedWidget("try", xmTextFieldWidgetClass, box,
XmNcolumns, 40, NULL);
(void) field;
return box;
}
/* ---- the mouse ---------------------------------------------------------- */
#define MAX_BUTTONS 256
static Widget msLeft, msRight, msSpeed, msThreshold, msDouble;
static unsigned char msMap[MAX_BUTTONS];
static int msButtons;
/* "left" or "right" handed: buttons 1 and 3 change places, the others stay */
static Boolean
LeftHanded(void)
{
return msButtons >= 3 && msMap[0] == 3 && msMap[2] == 1;
}
static void
MouseApply(Widget w, XtPointer data, XtPointer call)
{
Display *dpy = XtDisplay(msSpeed);
char map[4 * MAX_BUTTONS], res[64], prop[4 * MAX_BUTTONS + 128];
Boolean left = XmToggleButtonGetState(msLeft);
int speed = ScaleValue(msSpeed), threshold = ScaleValue(msThreshold);
int dclick = ScaleValue(msDouble), i;
size_t at = 0;
Window sm;
(void) data;
/* the radio pair: the one switched off does nothing */
if (w != NULL && call != NULL && XmIsToggleButton(w) &&
!((XmToggleButtonCallbackStruct *) call)->set)
return;
if (msButtons >= 3 && left != LeftHanded()) {
unsigned char t = msMap[0];
msMap[0] = msMap[2];
msMap[2] = t;
/* the server says busy while a button is down; once again */
if (XSetPointerMapping(dpy, msMap, msButtons) != MappingSuccess)
(void) XSetPointerMapping(dpy, msMap, msButtons);
}
XChangePointerControl(dpy, True, True, speed, 1, threshold);
/* dtstyle's resource line (programs/dtstyle/Mouse.c,
* multiClickTimeString) */
snprintf(res, sizeof res, "*multiClickTime:%d\n", dclick);
_DtAddToResource(dpy, res);
for (i = 0; i < msButtons && at + 8 < sizeof map; i++)
at += (size_t) snprintf(map + at, sizeof map - at, "%s%d",
i > 0 ? "_" : "", msMap[i]);
map[at] = '\0';
/* what dtsession reads (dtsession/SmSave.c): "BUTTONS MAP ACCEL
* THRESHOLD RESOURCE", the map joined by "_" */
snprintf(prop, sizeof prop, "%d %s %d %d %s", msButtons, map, speed,
threshold, res);
if ((sm = SmWindow(dpy)) != None)
XChangeProperty(dpy, sm,
XInternAtom(dpy, _XA_DT_SM_POINTER_INFO, False), XA_STRING,
8, PropModeReplace, (unsigned char *) prop,
(int) strlen(prop));
XFlush(dpy);
}
Widget
MakeMouse(Widget parent)
{
Display *dpy = XtDisplay(parent);
Widget box = XtVaCreateWidget("mouse", xmRowColumnWidgetClass, parent,
XmNspacing, 6, NULL);
Widget radio;
int num = 2, den = 1, thr = 4, dclick = XtGetMultiClickTime(dpy);
msButtons = XGetPointerMapping(dpy, msMap, MAX_BUTTONS);
XGetPointerControl(dpy, &num, &den, &thr);
(void) Text(box, "label", "Primary button:");
radio = XtVaCreateManagedWidget("hand", xmRowColumnWidgetClass, box,
XmNorientation, XmHORIZONTAL, XmNradioBehavior, True, NULL);
msRight = XtVaCreateManagedWidget("Left (right-handed)",
xmToggleButtonWidgetClass, radio, XmNset, !LeftHanded(), NULL);
msLeft = XtVaCreateManagedWidget("Right (left-handed)",
xmToggleButtonWidgetClass, radio, XmNset, LeftHanded(), NULL);
XtAddCallback(msRight, XmNvalueChangedCallback, MouseApply, NULL);
XtAddCallback(msLeft, XmNvalueChangedCallback, MouseApply, NULL);
if (msButtons < 3)
XtSetSensitive(radio, False);
msSpeed = Scale(box, "speed", "Pointer speed (acceleration):", 1, 10,
den > 0 ? num / den : num, MouseApply);
msThreshold = Scale(box, "threshold",
"Speeds up after (pixels):", 1, 20, thr, MouseApply);
msDouble = Scale(box, "double", "Double-click time (milliseconds):",
100, 900, dclick, MouseApply);
return box;
}