Files
makronix/ext/src/mcde/programs/dtkeys/dtkeys.c
T
lotherkandClaude Sonnet 5.5 abd4c853de 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

355 lines
9.7 KiB
C

/*
* dtkeys -- MCDE: the key left of the space bar is the Command key
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* MCDE's shortcuts go by the place of a key, not by what is printed on it
* (Konrad, 2026-11-11): the key left of the space bar is Command, the one
* left of it Option, the one left of that Ctrl. Command is the modifier
* Mod4 (Super) everywhere -- Motif's text fields, dtterm, dtwm and the
* programs' menus use it -- and Option is Alt (Mod1), where Motif's menu
* mnemonics stay.
*
* On a Macintosh keyboard that is so already (Command is Super_L, Option is
* Alt_L). On a PC keyboard the key left of the space bar is Alt and the one
* left of it the Windows key, so dtkeys exchanges what the two produce: the
* left Alt key gives Super_L in Mod4, the left Windows key Alt_L in Mod1.
* Xlib only (XChangeKeyboardMapping, XSetModifierMapping): no setxkbmap.
*
* The modifier keys can be given other functions (System Settings >
* Keyboard & Mouse > Modifier Keys...): "capslock: control" and its three
* neighbours in ~/.dt/keyboard say what the physical Caps Lock, Control,
* Option and Command keys should be. XChangeKeyboardMapping +
* XSetModifierMapping, the same way as the exchange above.
*
* usage: dtkeys [auto | pc | mac | off]
* auto (the default) the kind from ~/.dt/keyboard ("type: pc|mac|auto"),
* else mcde_sys_keyboard_is_mac
* off the keyboard as the X server gave it (undo)
* Run at login (Xsession.d/0072.dtkeys) and by System Settings, Keyboard.
* The root window's _MCDE_KEYS property ("pc" or "mac") says what was done,
* so a second run never exchanges the keys back.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <Mcde/McdeSys.h>
/* the kind from ~/.dt/keyboard, NULL if it says nothing */
static const char *
Configured(void)
{
static char kind[16];
const char *home = getenv("HOME");
char path[1024], line[256];
FILE *f;
if (home == NULL)
return NULL;
snprintf(path, sizeof path, "%s/.dt/keyboard", home);
if ((f = fopen(path, "r")) == NULL)
return NULL;
kind[0] = '\0';
while (fgets(line, sizeof line, f) != NULL)
if (sscanf(line, "type: %15s", kind) == 1)
break;
fclose(f);
return kind[0] != '\0' ? kind : NULL;
}
/* the first keycode whose first keysym is SYM, 0 if none */
static KeyCode
CodeOf(Display *d, KeySym sym)
{
int min, max, per, k;
KeySym *map;
KeyCode found = 0;
XDisplayKeycodes(d, &min, &max);
map = XGetKeyboardMapping(d, (KeyCode) min, max - min + 1, &per);
if (map == NULL)
return 0;
for (k = 0; k <= max - min && found == 0; k++)
if (map[k * per] == sym)
found = (KeyCode) (min + k);
XFree(map);
return found;
}
/* KC produces SYM (all its keysyms) */
static void
SetSym(Display *d, KeyCode kc, KeySym sym)
{
XChangeKeyboardMapping(d, kc, 1, &sym, 1);
}
/* KC is in modifier MOD only (of Mod1 and Mod4) */
static void
SetModifier(XModifierKeymap **mm, KeyCode kc, int mod)
{
*mm = XDeleteModifiermapEntry(*mm, kc, Mod1MapIndex);
*mm = XDeleteModifiermapEntry(*mm, kc, Mod4MapIndex);
*mm = XInsertModifiermapEntry(*mm, kc, mod);
}
/* exchange left Alt and left Windows key (PC), or put them back */
static int
Exchange(Display *d, int toCommand)
{
/* before: Alt_L on the key left of the space bar, Super_L left of it;
* after: the other way round */
KeySym first = toCommand ? XK_Alt_L : XK_Super_L;
KeySym second = toCommand ? XK_Super_L : XK_Alt_L;
KeyCode left = CodeOf(d, first), win = CodeOf(d, second);
XModifierKeymap *mm;
int i;
if (left == 0)
return -1;
if (win == 0) {
/* an old keyboard without a Windows key: only the key left of
* the space bar, which becomes Command; Option is not there */
SetSym(d, left, second);
} else {
SetSym(d, left, second);
SetSym(d, win, first);
}
mm = XGetModifierMapping(d);
SetModifier(&mm, left, toCommand ? Mod4MapIndex : Mod1MapIndex);
if (win != 0)
SetModifier(&mm, win, toCommand ? Mod1MapIndex : Mod4MapIndex);
/* the server answers MappingBusy while a key is held: try a while */
for (i = 0; i < 50; i++) {
int r = XSetModifierMapping(d, mm);
if (r == MappingSuccess)
break;
if (r == MappingFailed) {
XFreeModifiermap(mm);
return -1;
}
XSync(d, False);
}
XFreeModifiermap(mm);
return 0;
}
/* ---- the modifier keys (System Settings > Keyboard & Mouse) ------------- */
/* what ~/.dt/keyboard says a physical modifier key should be; NULL if the
* file says nothing about it */
static const char *
Wanted(const char *key)
{
static char values[4][16];
static int read_done;
static const char *const names[] = { "capslock", "control", "option",
"command" };
const char *home;
char path[1024], line[256], *p;
FILE *f;
int i;
if (!read_done) {
read_done = 1;
if ((home = getenv("HOME")) == NULL)
return NULL;
snprintf(path, sizeof path, "%s/.dt/keyboard", home);
if ((f = fopen(path, "r")) != NULL) {
while (fgets(line, sizeof line, f) != NULL) {
for (i = 0; i < 4; i++) {
size_t l = strlen(names[i]);
if (strncmp(line, names[i], l) != 0 ||
line[l] != ':')
continue;
p = line + l + 1;
while (*p == ' ' || *p == '\t')
p++;
(void)sscanf(p, "%15s", values[i]);
}
}
fclose(f);
}
}
for (i = 0; i < 4; i++)
if (strcmp(key, names[i]) == 0 && values[i][0] != '\0')
return values[i];
return NULL;
}
/* the keysym a function's key gives */
static KeySym
FunctionSym(const char *what)
{
if (what == NULL)
return NoSymbol;
if (strcmp(what, "capslock") == 0)
return XK_Caps_Lock;
if (strcmp(what, "control") == 0)
return XK_Control_L;
if (strcmp(what, "option") == 0)
return XK_Alt_L;
if (strcmp(what, "command") == 0)
return XK_Super_L;
if (strcmp(what, "none") == 0)
return XK_VoidSymbol;
return NoSymbol;
}
/* the modifier index a function's key belongs to, -1 for none */
static int
FunctionMod(const char *what)
{
if (what == NULL)
return -1;
if (strcmp(what, "control") == 0)
return ControlMapIndex;
if (strcmp(what, "option") == 0)
return Mod1MapIndex;
if (strcmp(what, "command") == 0)
return Mod4MapIndex;
return -1;
}
/* the first keycode in modifier MOD, 0 if there is none */
static KeyCode
FirstInMod(XModifierKeymap *mm, int mod)
{
int i;
for (i = 0; i < mm->max_keypermod; i++)
if (mm->modifiermap[mod * mm->max_keypermod + i] != 0)
return mm->modifiermap[mod * mm->max_keypermod + i];
return 0;
}
/* the four physical keys, as ~/.dt/keyboard names them, get their function:
* first every one of them out of Lock, Control, Mod1 and Mod4, then into the
* modifier its function asks for (Xlib grows the map if it has to) */
static int
Modifiers(Display *d)
{
static const char *const names[] = { "capslock", "control", "option",
"command" };
static const int mods[] = { LockMapIndex, ControlMapIndex,
Mod1MapIndex, Mod4MapIndex };
KeyCode code[4];
KeySym sym, want_sym[4];
XModifierKeymap *mm;
int want_mod[4], i, m, tries;
int any = 0;
mm = XGetModifierMapping(d);
if (mm == NULL)
return -1;
code[0] = CodeOf(d, XK_Caps_Lock);
code[1] = CodeOf(d, XK_Control_L);
code[2] = FirstInMod(mm, Mod1MapIndex);
code[3] = FirstInMod(mm, Mod4MapIndex);
for (i = 0; i < 4; i++) {
if ((sym = FunctionSym(Wanted(names[i]))) != NoSymbol)
any = 1;
want_sym[i] = sym;
want_mod[i] = FunctionMod(Wanted(names[i]));
}
if (!any) {
XFreeModifiermap(mm);
return 0;
}
for (i = 0; i < 4; i++) {
if (code[i] == 0 || want_sym[i] == NoSymbol)
continue;
for (m = 0; m < 4; m++)
mm = XDeleteModifiermapEntry(mm, code[i], mods[m]);
}
for (i = 0; i < 4; i++) {
if (code[i] == 0 || want_sym[i] == NoSymbol)
continue;
XChangeKeyboardMapping(d, code[i], 1, &want_sym[i], 1);
if (want_mod[i] >= 0)
mm = XInsertModifiermapEntry(mm, code[i], want_mod[i]);
}
for (tries = 0; tries < 50; tries++) {
int r = XSetModifierMapping(d, mm);
if (r == MappingSuccess)
break;
if (r == MappingFailed) {
XFreeModifiermap(mm);
return -1;
}
XSync(d, False);
}
XFreeModifiermap(mm);
return 0;
}
int
main(int argc, char **argv)
{
const char *want = argc > 1 ? argv[1] : "auto";
Display *d;
Atom prop, type;
int format;
unsigned long n, after;
unsigned char *data = NULL;
char done[16] = "";
const char *kind;
int rc = 0;
if (strcmp(want, "auto") != 0 && strcmp(want, "pc") != 0 &&
strcmp(want, "mac") != 0 && strcmp(want, "off") != 0) {
fprintf(stderr, "usage: dtkeys [auto | pc | mac | off]\n");
return 2;
}
if ((d = XOpenDisplay(NULL)) == NULL) {
fprintf(stderr, "dtkeys: cannot open the display\n");
return 1;
}
prop = XInternAtom(d, "_MCDE_KEYS", False);
if (XGetWindowProperty(d, DefaultRootWindow(d), prop, 0, 4, False,
XA_STRING, &type, &format, &n, &after, &data) == Success &&
data != NULL) {
snprintf(done, sizeof done, "%.*s", (int) n, (char *) data);
XFree(data);
}
kind = want;
if (strcmp(kind, "auto") == 0) {
const char *c = Configured();
kind = c != NULL && strcmp(c, "auto") != 0 ? c :
(mcde_sys_keyboard_is_mac() ? "mac" : "pc");
}
/* undo what was done if the kind changed */
if (strcmp(done, "pc") == 0 && strcmp(kind, "pc") != 0) {
if (Exchange(d, 0) != 0)
rc = 1;
done[0] = '\0';
}
if (strcmp(kind, "pc") == 0 && strcmp(done, "pc") != 0 &&
Exchange(d, 1) != 0) {
fprintf(stderr, "dtkeys: no left Alt key to make Command\n");
rc = 1;
}
if (Modifiers(d) != 0)
fprintf(stderr, "dtkeys: the modifier keys were not changed\n");
if (strcmp(kind, "off") == 0)
XDeleteProperty(d, DefaultRootWindow(d), prop);
else
XChangeProperty(d, DefaultRootWindow(d), prop, XA_STRING, 8,
PropModeReplace, (const unsigned char *) kind,
(int) strlen(kind));
XCloseDisplay(d);
return rc;
}