Files
mcde/programs/dtsystemsettings/input.c
T
lotherkandClaude Sonnet 5.5 d53ea094e7 licence: CNCL for what Makronix wrote itself
LICENSE at the root; the 103 files new in ext/src/mcde (none was in the CDE
import) carry the CNCL line instead of the LGPL notice.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 11:48:02 +00:00

1134 lines
33 KiB
C

/*
* input.c -- dtsystemsettings: the keyboard and the mouse
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* 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/stat.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/List.h>
#include <Xm/PushBG.h>
#include <Xm/RowColumn.h>
#include <Xm/Separator.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;
}
/* the pane puts its own states on the toggles: their callbacks stand aside */
static Boolean inputSyncing;
/* ---- 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 Boolean kbRepeatState;
static int kbKindState; /* 0 auto, 1 pc, 2 mac */
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;
if (inputSyncing)
return;
kbRepeatState = on;
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 keyboard file (~/.dt/keyboard, shared with dtkeys) ------------- */
static const char *const modNames[4] = { "capslock", "control", "option",
"command" };
/* the kind and the four modifier functions from ~/.dt/keyboard; MODS may be
* NULL when only the kind is wanted */
static void
KeyboardRead(char *type, size_t tl, char mods[4][16])
{
const char *home = getenv("HOME");
char path[1024], line[256];
FILE *f;
int i;
(void)snprintf(type, tl, "auto");
if (mods != NULL)
for (i = 0; i < 4; i++)
mods[i][0] = '\0';
if (home == NULL || (size_t) snprintf(path, sizeof path,
"%s/.dt/keyboard", home) >= sizeof path)
return;
if ((f = fopen(path, "r")) == NULL)
return;
while (fgets(line, sizeof line, f) != NULL) {
char v[16];
if (sscanf(line, "type: %15s", v) == 1) {
(void)snprintf(type, tl, "%s", v);
continue;
}
if (mods == NULL)
continue;
for (i = 0; i < 4; i++) {
size_t l = strlen(modNames[i]);
if (strncmp(line, modNames[i], l) == 0 &&
line[l] == ':' &&
sscanf(line + l + 1, "%15s", v) == 1)
(void)snprintf(mods[i], 16, "%s", v);
}
}
fclose(f);
}
static void
KeyboardWrite(const char *type, char mods[4][16])
{
const char *home = getenv("HOME");
char path[1024];
FILE *f;
int i;
if (home == NULL || (size_t) snprintf(path, sizeof path,
"%s/.dt/keyboard", home) >= sizeof path)
return;
if ((f = fopen(path, "w")) == NULL)
return;
fprintf(f, "# the keyboard, for dtkeys: the kind (auto, pc, mac) and\n"
"# what the four modifier keys give (System Settings > Keyboard "
"& Mouse)\n");
fprintf(f, "type: %s\n", type);
for (i = 0; i < 4; i++)
if (mods != NULL && mods[i][0] != '\0')
fprintf(f, "%s: %s\n", modNames[i], mods[i]);
fclose(f);
}
/* ---- the kind of keyboard: which key is Command (dtkeys) ------------------- */
static Widget kbAuto, kbPc, kbMac;
static const char *
KeyboardKind(void)
{
static char kind[16];
KeyboardRead(kind, sizeof kind, NULL);
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;
char type[16], mods[4][16];
pid_t pid;
(void) w;
if (inputSyncing || !((XmToggleButtonCallbackStruct *) call)->set)
return;
KeyboardRead(type, sizeof type, mods);
KeyboardWrite(kind, mods);
kbKindState = strcmp(kind, "pc") == 0 ? 1 : strcmp(kind, "mac") == 0 ?
2 : 0;
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);
}
/* ---- the modifier keys: the dialog -------------------------------------- */
static Widget modShell, modMenu[4], modItem[4][5], modNote;
static const char *const modLabels[4] = { "Caps Lock:", "Control:",
"Option:", "Command:" };
static const char *const modFuncs[5] = { "Caps Lock", "Control", "Option",
"Command", "No Action" };
static const char *const modValues[5] = { "capslock", "control", "option",
"command", "none" };
static int
ModIndex(const char *v)
{
int i;
if (v == NULL)
return -1;
for (i = 0; i < 5; i++)
if (strcmp(v, modValues[i]) == 0)
return i;
return -1;
}
static int
ModChosen(int which)
{
Widget h = NULL;
int i;
XtVaGetValues(modMenu[which], XmNmenuHistory, &h, NULL);
for (i = 0; i < 5; i++)
if (modItem[which][i] == h)
return i;
return -1;
}
/* "dtkeys auto" now: it reads the same file (dtkeys.c, Modifiers) */
static int
RunDtkeys(void)
{
pid_t pid;
int status = 0;
if ((pid = fork()) == 0) {
execl(CDE_INSTALLATION_TOP "/bin/dtkeys", "dtkeys", "auto",
(char *) NULL);
_exit(127);
}
if (pid < 0)
return -1;
while (waitpid(pid, &status, 0) < 0)
;
return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
}
static void
ModifierApply(Widget w, XtPointer data, XtPointer call)
{
char type[16], mods[4][16];
int i, changed = 0;
(void) w;
(void) data;
(void) call;
KeyboardRead(type, sizeof type, mods);
for (i = 0; i < 4; i++) {
int v = ModChosen(i);
if (v < 0)
continue;
if (strcmp(mods[i], modValues[v]) != 0) {
(void)snprintf(mods[i], sizeof mods[i], "%s",
modValues[v]);
changed = 1;
}
}
if (!changed) {
SetText(modNote, "Nothing changed.");
return;
}
KeyboardWrite(type, mods);
if (RunDtkeys() != 0)
SetText(modNote, "The keyboard did not take it.");
else
SetText(modNote, "The keys have the new functions now, and "
"after the next login.");
}
static void
ModifierClose(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
XtPopdown(modShell);
}
/* what ~/.dt/keyboard says now into the menus */
static void
ModifierFill(void)
{
char type[16], mods[4][16];
int i;
KeyboardRead(type, sizeof type, mods);
for (i = 0; i < 4; i++) {
int v = ModIndex(mods[i][0] != '\0' ? mods[i] : modValues[i]);
if (v < 0)
v = i; /* its own function */
XtVaSetValues(modMenu[i], XmNmenuHistory, modItem[i][v], NULL);
}
SetText(modNote, "");
}
static void
ModifierDialog(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
if (modShell == NULL) {
Widget form, row, buttons;
XmString s;
int i, k;
modShell = XtVaCreatePopupShell("modifierKeys",
topLevelShellWidgetClass, XtParent(XtParent(w)),
XmNtitle, "Modifier Keys", XmNallowShellResize, True,
NULL);
form = XtVaCreateManagedWidget("modifierForm",
xmRowColumnWidgetClass, modShell, XmNorientation,
XmVERTICAL, XmNmarginWidth, 12, XmNmarginHeight, 12,
XmNspacing, 6, XmNwidth, 380, NULL);
for (i = 0; i < 4; i++) {
Widget pd;
Widget label;
row = XtVaCreateManagedWidget("modifierRow",
xmRowColumnWidgetClass, form, XmNorientation,
XmHORIZONTAL, XmNspacing, 8, XmNmarginWidth, 0,
NULL);
label = Text(row, "modifierLabel", modLabels[i]);
XtVaSetValues(label, XmNwidth, 110, XmNrecomputeSize,
False, XmNalignment, XmALIGNMENT_END, NULL);
modMenu[i] = XmCreateOptionMenu(row, "modifier",
NULL, 0);
pd = XmCreatePulldownMenu(modMenu[i], "modifierMenu",
NULL, 0);
for (k = 0; k < 5; k++) {
s = XmStringCreateLocalized((char *)
modFuncs[k]);
modItem[i][k] = XtVaCreateManagedWidget(
"modifierItem", xmPushButtonGadgetClass,
pd, XmNlabelString, s, NULL);
XmStringFree(s);
}
XtVaSetValues(modMenu[i], XmNsubMenuId, pd,
XmNmenuHistory, modItem[i][i], NULL);
XtManageChild(modMenu[i]);
}
modNote = Text(form, "modifierNote", "");
buttons = XtVaCreateManagedWidget("modifierButtons",
xmRowColumnWidgetClass, form, XmNorientation,
XmHORIZONTAL, XmNspacing, 8, XmNmarginWidth, 0, NULL);
{
Widget apply = Button(buttons, "Apply", ModifierApply,
NULL);
Widget close = Button(buttons, "Close", ModifierClose,
NULL);
XtVaSetValues(apply, XmNwidth, 90, NULL);
XtVaSetValues(close, XmNwidth, 90, NULL);
}
XtVaSetValues(modShell, XmNtransientFor,
XtParent(XtParent(w)), XmNwidth, 340, XmNheight, 330,
NULL);
}
ModifierFill();
XtPopup(modShell, XtGrabNone);
}
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);
kbRepeatState = ks.global_auto_repeat == AutoRepeatModeOn;
kbRepeat = XtVaCreateManagedWidget("Keys repeat when held down",
xmToggleButtonWidgetClass, box, NULL);
XmToggleButtonSetState(kbRepeat, kbRepeatState, False);
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) PaneButton(box, "Modifier Keys...", ModifierDialog, NULL);
(void) Text(box, "modNote", "Caps Lock, Control, Option and Command "
"can be given other functions.");
(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 Widget msScrollNormal, msScrollNatural;
static unsigned char msMap[MAX_BUTTONS];
static int msButtons;
static Boolean msWheelNatural;
static Boolean msWheelNaturalState;
static Boolean msLeftHandedState;
static Boolean wfPointerState, wfRaiseState;
/* "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;
if (inputSyncing)
return;
/* the radio pair: the one switched off does nothing */
if (w != NULL && call != NULL && XmIsToggleButton(w) &&
!((XmToggleButtonCallbackStruct *) call)->set)
return;
if (w == msRight || w == msLeft)
msLeftHandedState = left;
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);
}
/* the wheel's direction: buttons 4 and 5 change places (the reference's
* natural scrolling); how far one turn scrolls is the program's own step */
static void
ScrollApply(Widget w, XtPointer data, XtPointer call)
{
Display *dpy = XtDisplay(msScrollNatural);
unsigned char map[MAX_BUTTONS];
Boolean natural = XmToggleButtonGetState(msScrollNatural);
int n = XGetPointerMapping(dpy, map, MAX_BUTTONS), i;
(void) w;
(void) data;
(void) call;
if (inputSyncing)
return;
if (n >= 5 && natural != msWheelNatural) {
unsigned char t = map[3];
map[3] = map[4];
map[4] = t;
for (i = 0; i < 50; i++) {
int r = XSetPointerMapping(dpy, map, n);
if (r == MappingSuccess)
break;
if (r == MappingFailed)
return;
XSync(dpy, False);
}
msWheelNatural = natural;
msWheelNaturalState = natural;
msButtons = n;
memcpy(msMap, map, (size_t) (n < MAX_BUTTONS ? n : MAX_BUTTONS));
/* the map goes to dtsession with the rest (MouseApply) */
MouseApply(NULL, NULL, NULL);
}
}
/* ---- 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;
if (inputSyncing)
return;
wfPointerState = XmToggleButtonGetState(wfPointer);
wfRaiseState = XmToggleButtonGetState(wfRaise);
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, NULL);
msLeft = XtVaCreateManagedWidget("Right (left-handed)",
xmToggleButtonWidgetClass, radio, NULL);
msLeftHandedState = LeftHanded();
XmToggleButtonSetState(msLeftHandedState ? msLeft : msRight, True,
False);
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);
msWheelNatural = msButtons >= 5 && msMap[3] == 5 && msMap[4] == 4;
(void) Text(box, "scrollLabel", "The wheel:");
radio = XtVaCreateManagedWidget("wheel", xmRowColumnWidgetClass, box,
XmNorientation, XmHORIZONTAL, XmNradioBehavior, True, NULL);
msScrollNormal = XtVaCreateManagedWidget("Scrolls down when turned "
"away", xmToggleButtonWidgetClass, radio, NULL);
msScrollNatural = XtVaCreateManagedWidget("Scrolls down when turned "
"towards you", xmToggleButtonWidgetClass, radio, NULL);
msWheelNaturalState = msWheelNatural;
XmToggleButtonSetState(msWheelNaturalState ? msScrollNatural :
msScrollNormal, True, False);
XtAddCallback(msScrollNormal, XmNvalueChangedCallback, ScrollApply,
NULL);
XtAddCallback(msScrollNatural, XmNvalueChangedCallback, ScrollApply,
NULL);
(void) Text(box, "scrollNote", "How far one turn scrolls is the "
"program's own step (Motif's scroll bar).");
/* 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, NULL);
wfPointerState = DtwmResource(dpy, "dtwm.keyboardFocusPolicy",
"Dtwm.KeyboardFocusPolicy", "pointer", True);
XmToggleButtonSetState(wfPointer, wfPointerState, False);
wfRaise = XtVaCreateManagedWidget("The active window comes to the front",
xmToggleButtonWidgetClass, box, NULL);
wfRaiseState = DtwmResource(dpy, "dtwm.focusAutoRaise",
"Dtwm.FocusAutoRaise", "true", False);
XmToggleButtonSetState(wfRaise, wfRaiseState, False);
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;
}
/* ---- the shortcuts page ------------------------------------------------- */
static struct {
const char *name;
char key[64];
const char *where;
int editable;
} shortcuts[] = {
{ "Launcher", "Cmd+space", "Dtlaunch*hotkey", 1 },
{ "Next program", "Cmd+Tab", "dtwm", 0 },
{ "Previous program", "Shift+Cmd+Tab", "dtwm", 0 },
{ "Next window", "Alt+Tab", "dtwm", 0 },
{ "Next window of this program", "Cmd+`", "dtwm", 0 },
{ "Hide the program", "Cmd+H", "dtwm", 0 },
{ "Hide the others", "Option+Cmd+H", "dtwm", 0 },
{ "Minimize", "Cmd+M", "dtwm", 0 },
{ "Close the window", "Cmd+W", "app-defaults", 0 },
{ "Quit the program", "Cmd+Q", "app-defaults", 0 },
{ "Preferences", "Cmd+,", "app-defaults", 0 },
{ "Force Quit", "Option+Cmd+Esc", "dtwm/dtbar", 0 },
{ "Screen shot", "Shift+Cmd+3", "dtscreenshot", 0 },
{ "Screen shot of an area", "Shift+Cmd+4", "dtscreenshot", 0 },
};
#define NSHORTCUTS ((int) (sizeof shortcuts / sizeof shortcuts[0]))
static Widget scList, scField, scChange, scNote;
static int scSel = -1;
/* ~/.dt/shortcuts: "Name=key", one a line */
static void
ShortcutFile(char *buf, size_t len)
{
const char *home = getenv("HOME");
(void)snprintf(buf, len, "%s/.dt/shortcuts", home != NULL ? home : ".");
}
static void
ShortcutRead(void)
{
char path[1024], line[256];
FILE *f;
int i;
ShortcutFile(path, sizeof path);
if ((f = fopen(path, "r")) == NULL)
return;
while (fgets(line, sizeof line, f) != NULL) {
char *eq;
line[strcspn(line, "\r\n")] = '\0';
if (line[0] == '#' || (eq = strchr(line, '=')) == NULL)
continue;
*eq = '\0';
for (i = 0; i < NSHORTCUTS; i++)
if (strcmp(line, shortcuts[i].name) == 0) {
(void)snprintf(shortcuts[i].key,
sizeof shortcuts[i].key, "%s", eq + 1);
break;
}
}
fclose(f);
}
static void
ShortcutWrite(const char *name, const char *key)
{
char path[1024], keep[NSHORTCUTS][192], buf[256];
FILE *f;
int i, n = 0;
ShortcutFile(path, sizeof path);
{
char dir[1024], *slash;
(void)snprintf(dir, sizeof dir, "%s", path);
if ((slash = strrchr(dir, '/')) != NULL) {
*slash = '\0';
(void)mkdir(dir, 0755);
}
}
if ((f = fopen(path, "r")) != NULL) {
while (fgets(buf, sizeof buf, f) != NULL) {
char *eq;
buf[strcspn(buf, "\r\n")] = '\0';
if (buf[0] == '#' || buf[0] == '\0')
continue;
eq = strchr(buf, '=');
if (eq != NULL)
*eq = '\0';
if (strcmp(buf, name) == 0)
continue; /* replaced below */
(void)snprintf(keep[n], sizeof keep[n], "%s=%s", buf,
eq != NULL ? eq + 1 : "");
if (++n >= NSHORTCUTS)
break;
}
fclose(f);
}
if (n < NSHORTCUTS)
(void)snprintf(keep[n++], sizeof keep[0], "%s=%s", name, key);
if ((f = fopen(path, "w")) != NULL) {
fputs("# MCDE's keys, one a line: the name, =, the key.\n"
"# The launcher's key is read by dtlaunch through\n"
"# Dtlaunch*hotkey; the other rows are a record (their\n"
"# keys live in dtwm and the app-defaults).\n", f);
for (i = 0; i < n; i++)
fprintf(f, "%s\n", keep[i]);
fclose(f);
}
}
/* "Cmd+Shift+F5" as dtlaunch reads it (parse_hotkey, dtlaunch.c): modifiers
* and one key; the canonical order is Cmd, Option, Ctrl, Shift */
static int
ParseChord(const char *in, char *out, size_t len)
{
char buf[128], *tok, *save = NULL;
KeySym ks = NoSymbol;
int cmd = 0, opt = 0, ctrl = 0, shift = 0;
if (in == NULL)
return 0;
(void)snprintf(buf, sizeof buf, "%s", in);
for (tok = strtok_r(buf, "+", &save); tok != NULL;
tok = strtok_r(NULL, "+", &save)) {
if (strcmp(tok, "Cmd") == 0 || strcmp(tok, "Command") == 0 ||
strcmp(tok, "Mod4") == 0 || strcmp(tok, "Super") == 0)
cmd = 1;
else if (strcmp(tok, "Option") == 0 ||
strcmp(tok, "Alt") == 0 || strcmp(tok, "Meta") == 0)
opt = 1;
else if (strcmp(tok, "Ctrl") == 0 ||
strcmp(tok, "Control") == 0)
ctrl = 1;
else if (strcmp(tok, "Shift") == 0)
shift = 1;
else if ((ks = XStringToKeysym(tok)) == NoSymbol)
return 0;
}
if (ks == NoSymbol || XKeysymToKeycode(XtDisplay(scList), ks) == 0)
return 0;
out[0] = '\0';
if (cmd)
(void)snprintf(out + strlen(out), len - strlen(out), "Cmd+");
if (opt)
(void)snprintf(out + strlen(out), len - strlen(out),
"Option+");
if (ctrl)
(void)snprintf(out + strlen(out), len - strlen(out), "Ctrl+");
if (shift)
(void)snprintf(out + strlen(out), len - strlen(out), "Shift+");
(void)snprintf(out + strlen(out), len - strlen(out), "%s",
XKeysymToString(ks));
return out[0] != '\0';
}
static void
ShortcutFill(void)
{
char label[256];
int i;
XmListDeleteAllItems(scList);
for (i = 0; i < NSHORTCUTS; i++) {
XmString s;
(void)snprintf(label, sizeof label, "%-30s %-16s %s",
shortcuts[i].name, shortcuts[i].key,
shortcuts[i].editable ? "changeable here" :
shortcuts[i].where);
s = XmStringCreateLocalized(label);
XmListAddItemUnselected(scList, s, 0);
XmStringFree(s);
}
}
/* the launcher again with the key the resources now say (dtlaunch -quit
* waits until the old one has let go of its key) */
static void
RestartDtlaunch(void)
{
pid_t pid;
if ((pid = fork()) == 0) {
execl(CDE_INSTALLATION_TOP "/bin/dtlaunch", "dtlaunch", "-quit",
(char *) NULL);
_exit(127);
}
if (pid > 0)
while (waitpid(pid, NULL, 0) < 0)
;
if ((pid = fork()) == 0) {
setsid();
execl(CDE_INSTALLATION_TOP "/bin/dtlaunch", "dtlaunch",
(char *) NULL);
_exit(127);
}
}
static void
ShortcutApplyKey(const char *chord)
{
char res[200];
(void)snprintf(res, sizeof res, "Dtlaunch*hotkey: %s\n", chord);
_DtAddToResource(XtDisplay(scList), res);
(void)snprintf(shortcuts[0].key, sizeof shortcuts[0].key, "%s", chord);
ShortcutWrite(shortcuts[0].name, chord);
ShortcutFill();
XmListSelectPos(scList, 1, False);
RestartDtlaunch();
SetText(scNote, "The launcher has the new key now.");
}
static void
ShortcutChosen(Widget w, XtPointer data, XtPointer call)
{
XmListCallbackStruct *c = (XmListCallbackStruct *) call;
int i = c->item_position - 1;
(void) w;
(void) data;
if (i < 0 || i >= NSHORTCUTS)
return;
scSel = i;
XmTextFieldSetString(scField, shortcuts[i].key);
XtSetSensitive(scChange, shortcuts[i].editable != 0);
SetText(scNote, shortcuts[i].editable ?
"Type the new key (for example Cmd+j) and press Change; the "
"launcher takes it at once." :
"This key belongs to the window manager or a program's "
"app-defaults file; the row here is a record.");
}
static void
ShortcutChange(Widget w, XtPointer data, XtPointer call)
{
char *text = XmTextFieldGetString(scField);
char chord[128];
(void) w;
(void) data;
(void) call;
if (scSel != 0) {
SetText(scNote, "Only the launcher's key can be changed here.");
XtFree(text);
return;
}
if (text == NULL || !ParseChord(text, chord, sizeof chord)) {
SetText(scNote, "That is not a key: modifiers (Cmd, Option, "
"Ctrl, Shift) and one key, as in Cmd+space.");
XtFree(text);
return;
}
ShortcutApplyKey(chord);
XtFree(text);
}
static void
ShortcutRestore(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
ShortcutApplyKey("Cmd+space");
XmTextFieldSetString(scField, "Cmd+space");
}
static Widget
MakeShortcutsPage(Widget parent)
{
Widget box = XtVaCreateWidget("shortcuts", xmRowColumnWidgetClass,
parent, XmNspacing, 6, NULL);
Widget row;
Arg a[3];
(void) Text(box, "shortLabel", "The keys of the desktop:");
XtSetArg(a[0], XmNvisibleItemCount, 12);
XtSetArg(a[1], XmNselectionPolicy, XmBROWSE_SELECT);
XtSetArg(a[2], XmNwidth, 600);
scList = XmCreateScrolledList(box, "shortcuts", a, 3);
XtManageChild(scList);
XtAddCallback(scList, XmNbrowseSelectionCallback, ShortcutChosen, NULL);
row = XtVaCreateManagedWidget("shortRow", xmRowColumnWidgetClass, box,
XmNorientation, XmHORIZONTAL, XmNspacing, 8, XmNmarginWidth, 0,
NULL);
(void) Text(row, "keyLabel", "Key:");
scField = XtVaCreateManagedWidget("shortKey", xmTextFieldWidgetClass,
row, XmNcolumns, 18, NULL);
scChange = Button(row, "Change", ShortcutChange, NULL);
XtSetSensitive(scChange, False);
(void) Button(row, "Restore Defaults", ShortcutRestore, NULL);
scNote = Text(box, "shortNote", "");
ShortcutRead();
ShortcutFill();
return box;
}
/* ---- the pane: three tabs ----------------------------------------------- */
static Widget inputPage[3];
/* Motif's toggle indicators do not take a state given before the widget was
* managed (measured 2026-11-14: a radio pair showed the last child), so the
* state each toggle holds is put on it again once its page is up */
static void
InputSyncToggles(void)
{
Widget toggles[10];
Boolean state[10];
int i, n = 0;
toggles[n] = kbRepeat; n++;
toggles[n] = kbAuto; n++;
toggles[n] = kbPc; n++;
toggles[n] = kbMac; n++;
toggles[n] = msRight; n++;
toggles[n] = msLeft; n++;
toggles[n] = msScrollNormal; n++;
toggles[n] = msScrollNatural; n++;
toggles[n] = wfPointer; n++;
toggles[n] = wfRaise; n++;
state[0] = kbRepeatState;
state[1] = kbKindState == 0;
state[2] = kbKindState == 1;
state[3] = kbKindState == 2;
state[4] = !msLeftHandedState;
state[5] = msLeftHandedState;
state[6] = !msWheelNaturalState;
state[7] = msWheelNaturalState;
state[8] = wfPointerState;
state[9] = wfRaiseState;
/* off then on, with the notifications: the indicator redraws, and
* the pane's own callbacks stand aside (inputSyncing) */
inputSyncing = True;
for (i = 0; i < n; i++)
XmToggleButtonSetState(toggles[i], False, True);
for (i = 0; i < n; i++)
XmToggleButtonSetState(toggles[i], state[i], True);
inputSyncing = False;
}
static void
InputSyncToggles(void);
/* the pane is mapped only after ShowPane manages it: the toggles show their
* state from then on (before it, Motif's radio rows keep the last child) */
static void
InputMapped(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
InputSyncToggles();
}
static void
InputTabChosen(Widget w, XtPointer data, XtPointer call)
{
XmToggleButtonCallbackStruct *c = (XmToggleButtonCallbackStruct *) call;
int which = (int) (long) data, i;
(void) w;
if (!c->set)
return;
for (i = 0; i < 3; i++)
if (i == which)
XtManageChild(inputPage[i]);
else
XtUnmanageChild(inputPage[i]);
InputSyncToggles();
}
/*
* The pane: Keyboard | Mouse | Shortcuts. -pane keyboard or -pane mouse
* opens it on that page (ShowPane passes the id).
*/
Widget
MakeInput(Widget parent, const char *which)
{
Widget box = XtVaCreateWidget("keyboardmouse",
xmRowColumnWidgetClass, parent, XmNspacing, 8, NULL);
Widget tabs, pages;
static const char *const labels[3] = { "Keyboard", "Mouse",
"Shortcuts" };
int i, start = 0;
if (which != NULL && strcmp(which, "mouse") == 0)
start = 1;
else if (which != NULL && strcmp(which, "shortcuts") == 0)
start = 2;
tabs = XtVaCreateManagedWidget("inputTabs", xmRowColumnWidgetClass,
box, XmNorientation, XmHORIZONTAL, XmNspacing, 4,
XmNmarginWidth, 0, NULL);
pages = XtVaCreateManagedWidget("inputPages", xmRowColumnWidgetClass,
box, XmNspacing, 4, NULL);
for (i = 0; i < 3; i++) {
XmString s = XmStringCreateLocalized((char *) labels[i]);
Widget t = XtVaCreateManagedWidget("inputTab",
xmToggleButtonWidgetClass, tabs, XmNlabelString, s,
XmNindicatorOn, False, XmNshadowThickness, 2,
XmNfillOnSelect, True, XmNmarginWidth, 10,
XmNset, i == start ? XmSET : XmUNSET, NULL);
XmStringFree(s);
XtAddCallback(t, XmNvalueChangedCallback, InputTabChosen,
(XtPointer) (long) i);
}
inputPage[0] = MakeKeyboard(pages);
inputPage[1] = MakeMouse(pages);
inputPage[2] = MakeShortcutsPage(pages);
XtManageChild(inputPage[start]);
XtAddCallback(box, XmNmapCallback, InputMapped, NULL);
return box;
}