Keyboard chooses which key is Command (find out, PC, Macintosh) and runs dtkeys at once; Mouse has "The focus follows the pointer" and "The active window comes to the front", applied as the Style Manager applies them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
391 lines
13 KiB
C
391 lines
13 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 <stdlib.h>
|
|
#include <string.h>
|
|
#include <strings.h>
|
|
#include <sys/types.h>
|
|
#include <sys/wait.h>
|
|
#include <unistd.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 <Dt/WsmP.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);
|
|
}
|
|
|
|
/* ---- the kind of keyboard: which key is Command (dtkeys) ------------------- */
|
|
|
|
static Widget kbAuto, kbPc, kbMac;
|
|
|
|
static const char *
|
|
KeyboardKind(void)
|
|
{
|
|
static char kind[16];
|
|
const char *home = getenv("HOME");
|
|
char path[1024], line[128];
|
|
FILE *f;
|
|
|
|
snprintf(kind, sizeof kind, "auto");
|
|
if (home == NULL)
|
|
return kind;
|
|
snprintf(path, sizeof path, "%s/.dt/keyboard", home);
|
|
if ((f = fopen(path, "r")) == NULL)
|
|
return kind;
|
|
while (fgets(line, sizeof line, f) != NULL)
|
|
if (sscanf(line, "type: %15s", kind) == 1)
|
|
break;
|
|
fclose(f);
|
|
return kind;
|
|
}
|
|
|
|
/* ~/.dt/keyboard and dtkeys at once (programs/dtkeys/dtkeys.c) */
|
|
static void
|
|
KindChanged(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
const char *kind = (const char *) data, *home = getenv("HOME");
|
|
char path[1024];
|
|
FILE *f;
|
|
pid_t pid;
|
|
|
|
(void) w;
|
|
if (!((XmToggleButtonCallbackStruct *) call)->set || home == NULL)
|
|
return;
|
|
snprintf(path, sizeof path, "%s/.dt/keyboard", home);
|
|
if ((f = fopen(path, "w")) != NULL) {
|
|
fprintf(f, "# the kind of keyboard, for dtkeys: auto, pc or mac\n"
|
|
"type: %s\n", kind);
|
|
fclose(f);
|
|
}
|
|
if ((pid = fork()) == 0) {
|
|
execl(CDE_INSTALLATION_TOP "/bin/dtkeys", "dtkeys", "auto",
|
|
(char *) NULL);
|
|
_exit(127);
|
|
} else if (pid > 0)
|
|
(void) waitpid(pid, NULL, 0);
|
|
}
|
|
|
|
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);
|
|
{
|
|
const char *kind = KeyboardKind();
|
|
Widget radio;
|
|
|
|
(void) Text(box, "kindLabel", "Keyboard (the key left of the "
|
|
"space bar is Command, the one left of it Option):");
|
|
radio = XtVaCreateManagedWidget("kind", xmRowColumnWidgetClass,
|
|
box, XmNorientation, XmHORIZONTAL, XmNradioBehavior, True,
|
|
NULL);
|
|
kbAuto = XtVaCreateManagedWidget("Find out", xmToggleButtonWidgetClass,
|
|
radio, XmNset, strcmp(kind, "pc") != 0 &&
|
|
strcmp(kind, "mac") != 0, NULL);
|
|
kbPc = XtVaCreateManagedWidget("PC keyboard",
|
|
xmToggleButtonWidgetClass, radio,
|
|
XmNset, strcmp(kind, "pc") == 0, NULL);
|
|
kbMac = XtVaCreateManagedWidget("Macintosh keyboard",
|
|
xmToggleButtonWidgetClass, radio,
|
|
XmNset, strcmp(kind, "mac") == 0, NULL);
|
|
XtAddCallback(kbAuto, XmNvalueChangedCallback, KindChanged,
|
|
(XtPointer) "auto");
|
|
XtAddCallback(kbPc, XmNvalueChangedCallback, KindChanged,
|
|
(XtPointer) "pc");
|
|
XtAddCallback(kbMac, XmNvalueChangedCallback, KindChanged,
|
|
(XtPointer) "mac");
|
|
}
|
|
(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);
|
|
}
|
|
|
|
/* ---- windows: the focus (dtwm's resources, as dtstyle writes them) ------- */
|
|
|
|
static Widget wfPointer, wfRaise, wfApply;
|
|
|
|
/* libDtSvc (DtUtil1/WmRestart.c) has no header for it; dtstyle declares it
|
|
* the same way (programs/dtstyle/Dtwm.c) */
|
|
int _DtWmRestartNoConfirm(Display *display, Window root);
|
|
|
|
/* a dtwm resource as the session has it, DEF when it has none */
|
|
static Boolean
|
|
DtwmResource(Display *dpy, const char *name, const char *cls,
|
|
const char *yes, Boolean def)
|
|
{
|
|
XrmDatabase db = XtDatabase(dpy);
|
|
char *type;
|
|
XrmValue v;
|
|
|
|
if (db == NULL || !XrmGetResource(db, name, cls, &type, &v) ||
|
|
v.addr == NULL)
|
|
return def;
|
|
return strcasecmp((char *) v.addr, yes) == 0;
|
|
}
|
|
|
|
static void
|
|
FocusChanged(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
(void) w;
|
|
(void) data;
|
|
(void) call;
|
|
XtSetSensitive(wfApply, True);
|
|
}
|
|
|
|
/* the resources, then dtwm started again: it reads them only at start
|
|
* (programs/dtstyle/Dtwm.c, okWarnCB) */
|
|
static void
|
|
FocusApply(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
Display *dpy = XtDisplay(w);
|
|
char res[160];
|
|
|
|
(void) data;
|
|
(void) call;
|
|
snprintf(res, sizeof res, "Dtwm.keyboardFocusPolicy: %s\n"
|
|
"Dtwm*focusAutoRaise: %s\n",
|
|
XmToggleButtonGetState(wfPointer) ? "pointer" : "explicit",
|
|
XmToggleButtonGetState(wfRaise) ? "True" : "False");
|
|
_DtAddToResource(dpy, res);
|
|
(void) _DtWmRestartNoConfirm(dpy, DefaultRootWindow(dpy));
|
|
XtSetSensitive(wfApply, False);
|
|
}
|
|
|
|
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);
|
|
/* MCDE's default: the focus follows the pointer, the window stays
|
|
* where it is (sys.resources) */
|
|
(void) Text(box, "windowsLabel", "Windows:");
|
|
wfPointer = XtVaCreateManagedWidget("The focus follows the pointer",
|
|
xmToggleButtonWidgetClass, box, XmNset,
|
|
DtwmResource(dpy, "dtwm.keyboardFocusPolicy",
|
|
"Dtwm.KeyboardFocusPolicy", "pointer", True), NULL);
|
|
wfRaise = XtVaCreateManagedWidget("The active window comes to the front",
|
|
xmToggleButtonWidgetClass, box, XmNset,
|
|
DtwmResource(dpy, "dtwm.focusAutoRaise", "Dtwm.FocusAutoRaise",
|
|
"true", False), NULL);
|
|
wfApply = Button(box, "Apply (restarts the window manager)",
|
|
FocusApply, NULL);
|
|
XtSetSensitive(wfApply, False);
|
|
XtAddCallback(wfPointer, XmNvalueChangedCallback, FocusChanged, NULL);
|
|
XtAddCallback(wfRaise, XmNvalueChangedCallback, FocusChanged, NULL);
|
|
return box;
|
|
}
|