dtkeys makes it Mod4 at login (a PC keyboard's left Alt and Windows keys are exchanged, a Macintosh keyboard is so already; ~/.dt/keyboard chooses, McdeSys tells a Macintosh keyboard). Motif's text fields copy, cut, paste and select all with Command+C/X/V/A; dtterm pastes, copies, opens and closes tabs and switches them with Command; dtwm's switching, minimizing, zones and full screen go by Command too. Option is Alt, where Motif's menu mnemonics stay. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
195 lines
5.5 KiB
C
195 lines
5.5 KiB
C
/*
|
|
* dtkeys -- MCDE: the key left of the space bar is the Command key
|
|
*
|
|
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
|
|
*
|
|
* 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.
|
|
*
|
|
* 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;
|
|
}
|
|
|
|
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 (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;
|
|
}
|