From 09cd93673c24bb02ea6bea8abcb1b15287331acc Mon Sep 17 00:00:00 2001 From: lotherk Date: Wed, 11 Nov 2026 23:41:23 +0000 Subject: [PATCH] mcde: the monitors move into libDtSvc, for System Settings and dtwm alike include/Mcde/Displays.h (McdeDisp*): the arrangement arithmetic (DtUtil2/McdeDispConf.c, test_displays now in lib/DtSvc) and reading, setting and keeping the monitors through RandR (DtUtil2/McdeDisplays.c, the RandR ids in the structure instead of tables of the pane). libDtSvc links Xrandr when configure found it; dtwm links it for the coming plug and play. Tested: make check in lib/DtSvc (test_displays) and dtsystemsettings; the pane on the dummy Xorg as before (applied, back after 16 s, kept and written). Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 5 +- include/Makefile.am | 1 + include/Mcde/Displays.h | 138 +++++++ .../DtSvc/DtUtil2/McdeDispConf.c | 86 ++-- lib/DtSvc/DtUtil2/McdeDisplays.c | 389 ++++++++++++++++++ .../DtSvc/DtUtil2}/test_displays.c | 60 +-- lib/DtSvc/Makefile.am | 12 +- programs/dtsystemsettings/Makefile.am | 9 +- programs/dtsystemsettings/dispconf.h | 110 ----- programs/dtsystemsettings/displays.c | 383 ++--------------- programs/dtwm/Makefile.am | 3 +- 11 files changed, 656 insertions(+), 540 deletions(-) create mode 100644 include/Mcde/Displays.h rename programs/dtsystemsettings/dispconf.c => lib/DtSvc/DtUtil2/McdeDispConf.c (81%) create mode 100644 lib/DtSvc/DtUtil2/McdeDisplays.c rename {programs/dtsystemsettings => lib/DtSvc/DtUtil2}/test_displays.c (81%) delete mode 100644 programs/dtsystemsettings/dispconf.h diff --git a/AGENTS.md b/AGENTS.md index 75b2ae07..851de320 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -229,7 +229,10 @@ place of CDE; pkgsrc builds it on any platform from `pkgsrc/`. -- while a German menu shows "Strg+Einfg"; Command is "Cmd" everywhere. Xvfb starts afresh (keymap and properties gone) when its last client leaves: keep one (xprop -root -spy) in such a test. - Displays (System Settings > Displays, programs/dtsystemsettings/displays.c; - the arithmetic without X in dispconf.c, `test_displays`, make check): the + the monitors themselves in libDtSvc for the pane and dtwm alike: + `include/Mcde/Displays.h`, the arithmetic without X in + lib/DtSvc/DtUtil2/McdeDispConf.c with `test_displays` (make check), RandR + and the file in McdeDisplays.c; libDtSvc links Xrandr): the monitors as rectangles to drag, docked to each other; per monitor on/off, resolution, rotation, main display; mirror. RandR 1.3 (libXrandr, optional in configure: HAVE_XRANDR), set as xrandr(1) does: changing diff --git a/include/Makefile.am b/include/Makefile.am index 4c9e725e..0b6f967b 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -3,6 +3,7 @@ MAINTAINERCLEANFILES = Makefile.in # Install the header files nobase_include_HEADERS = Mcde/GlobalMenu.h \ + Mcde/Displays.h \ Mcde/Image.h \ Mcde/McdeSys.h \ Dt/Editor.h \ diff --git a/include/Mcde/Displays.h b/include/Mcde/Displays.h new file mode 100644 index 00000000..5ed36d5c --- /dev/null +++ b/include/Mcde/Displays.h @@ -0,0 +1,138 @@ +/* + * Mcde/Displays.h -- MCDE: the monitors, their arrangement and memory + * + * Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE. + * + * In libDtSvc. The outputs with their modes, where they are, how they are + * turned; the file ~/.dt/displays that keeps it; the arithmetic of + * arranging (bounds, docking a dragged display to the others, mirroring): + * plain C99 in lib/DtSvc/DtUtil2/McdeDispConf.c, tested by test_displays + * (make check). Reading and setting them through RandR: + * lib/DtSvc/DtUtil2/McdeDisplays.c. Used by System Settings > Displays + * and by dtwm (plug and play). + * + * MCDE remembers a monitor, not a socket (Konrad, 2026-11-11: "mcde muss + * sich ein display und dessen position/konfiguration merken"): its id is + * made from the EDID the monitor sends (maker, product, serial number: + * "DEL-a0b7-4c4a3132"), "output-NAME" for one without (a projector of old, + * a virtual screen). A monitor that is not plugged in keeps its line, so + * that plugging it in again brings back where it was. + * + * ~/.dt/displays, one line a monitor, English keys (configuration is + * English, Konrad 2026-11-11): + * + * mirror no + * display DEL-a0b7-4c4a3132 on 1280x1024 60.02 0 0 normal primary + * display output-VGA-0 on 1024x768 60.00 1280 0 left + * display SAM-0e12-00000000 off + */ + +#ifndef _Mcde_Displays_h +#define _Mcde_Displays_h + +#include + +#define MCDE_DISP_MAX 16 +#define MCDE_DISP_MODES 64 + +enum McdeDispRotation { MCDE_ROT_NORMAL, MCDE_ROT_LEFT, MCDE_ROT_INVERTED, MCDE_ROT_RIGHT }; + +typedef struct { + int w, h; + double rate; /* Hz */ +} McdeDispMode; + +typedef struct { + char name[32]; /* the RandR output: "DVI-0" */ + char id[48]; /* the monitor: EDID id or "output-NAME" */ + int connected; /* a monitor is plugged in */ + int on; + int primary; /* top bar and front panel are here */ + int w, h; /* the mode, not turned */ + double rate; + int x, y; /* in the screen */ + int rot; /* enum McdeDispRotation */ + McdeDispMode modes[MCDE_DISP_MODES]; + int nmodes; + /* RandR, filled by McdeDispLoad (XIDs) */ + unsigned long output; + unsigned long modeId[MCDE_DISP_MODES]; + unsigned short rotations; /* the rotations the CRTC can */ +} McdeDisp; + +typedef struct { + McdeDisp d[MCDE_DISP_MAX]; + int n; + int mirror; /* all displays show the same */ +} McdeDispSet; + +/* "normal", "left", "inverted", "right"; -1 if none */ +const char *McdeDispRotName(int rot); +int McdeDispRotParse(const char *name); + +/* the size on the screen, turned */ +void McdeDispSize(const McdeDisp *d, int *w, int *h); + +/* the screen around the displays that are on (0 0 if none) */ +void McdeDispBounds(const McdeDispSet *s, int *w, int *h); + +/* the displays that are on moved so that the leftmost and topmost start + * at 0 */ +void McdeDispNormalize(McdeDispSet *s); + +/* display I, dropped where it is, docks to the nearest edge of another + * display that is on, without overlapping any; then McdeDispNormalize */ +void McdeDispDock(McdeDispSet *s, int i); + +/* the largest size every display that is on has a mode for; 0 if none */ +int McdeDispCommonSize(const McdeDispSet *s, int *w, int *h); + +/* mirroring: every display that is on at 0 0 with the common size (and + * the mode with the rate closest to its own); -1 if there is none */ +int McdeDispMirror(McdeDispSet *s); + +/* the mode of D with W x H and the rate closest to RATE; -1 if none */ +int McdeDispFindMode(const McdeDisp *d, int w, int h, double rate); + +/* the first display that is on and primary, else the first that is on; + * -1 if none is on */ +int McdeDispPrimary(const McdeDispSet *s); + +/* the id of a monitor from its EDID (at least 16 bytes); "" if it is not + * one; into BUF */ +void McdeDispEdidId(const unsigned char *edid, int len, char *buf, size_t size); + +/* the file: read into FILE (monitors by id and setting, no modes); + * write S (the connected monitors) and after it what REMEMBERED says of + * the monitors that are not connected now (REMEMBERED may be NULL) */ +int McdeDispRead(FILE *f, McdeDispSet *file); +int McdeDispWrite(FILE *f, const McdeDispSet *s, const McdeDispSet *remembered); + +/* what FILE says, put on LIVE for the monitors that are connected now and + * have the mode; the number of monitors that took it. A monitor the file + * does not name keeps what it has. */ +int McdeDispMerge(McdeDispSet *live, const McdeDispSet *file); + +/* ---- through RandR 1.3 (McdeDisplays.c) ---- */ + +struct _XDisplay; + +/* ~/.dt/displays */ +const char *McdeDispConfigPath(void); + +/* the outputs as the server has them now (connected ones, and ones still + * on); -1 without RandR 1.3 */ +int McdeDispLoad(struct _XDisplay *dpy, McdeDispSet *s); + +/* S onto the server (as xrandr(1): changed CRTCs off, screen size, CRTCs + * on, the primary output); 0 on success */ +int McdeDispApply(struct _XDisplay *dpy, const McdeDispSet *s); + +/* ~/.dt/displays on what is plugged in: 1 applied, 0 nothing to do, + * -1 failed */ +int McdeDispApplyFile(struct _XDisplay *dpy); + +/* LIVE into ~/.dt/displays, the monitors not plugged in kept; 0 or -1 */ +int McdeDispSave(const McdeDispSet *live); + +#endif /* _Mcde_Displays_h */ diff --git a/programs/dtsystemsettings/dispconf.c b/lib/DtSvc/DtUtil2/McdeDispConf.c similarity index 81% rename from programs/dtsystemsettings/dispconf.c rename to lib/DtSvc/DtUtil2/McdeDispConf.c index 166ddcd1..a330405f 100644 --- a/programs/dtsystemsettings/dispconf.c +++ b/lib/DtSvc/DtUtil2/McdeDispConf.c @@ -9,20 +9,20 @@ #include #include -#include "dispconf.h" +#include #define DOCK_ALIGN 48 /* pixels: edges this close line up */ static const char *const rotNames[] = { "normal", "left", "inverted", "right" }; const char * -DispRotName(int rot) +McdeDispRotName(int rot) { return rot >= 0 && rot < 4 ? rotNames[rot] : NULL; } int -DispRotParse(const char *name) +McdeDispRotParse(const char *name) { int i; @@ -33,9 +33,9 @@ DispRotParse(const char *name) } void -DispSize(const Disp *d, int *w, int *h) +McdeDispSize(const McdeDisp *d, int *w, int *h) { - if (d->rot == ROT_LEFT || d->rot == ROT_RIGHT) { + if (d->rot == MCDE_ROT_LEFT || d->rot == MCDE_ROT_RIGHT) { *w = d->h; *h = d->w; } else { @@ -45,7 +45,7 @@ DispSize(const Disp *d, int *w, int *h) } void -DispBounds(const DispSet *s, int *w, int *h) +McdeDispBounds(const McdeDispSet *s, int *w, int *h) { int i, dw, dh; @@ -53,7 +53,7 @@ DispBounds(const DispSet *s, int *w, int *h) for (i = 0; i < s->n; i++) { if (!s->d[i].on) continue; - DispSize(&s->d[i], &dw, &dh); + McdeDispSize(&s->d[i], &dw, &dh); if (s->d[i].x + dw > *w) *w = s->d[i].x + dw; if (s->d[i].y + dh > *h) @@ -62,7 +62,7 @@ DispBounds(const DispSet *s, int *w, int *h) } void -DispNormalize(DispSet *s) +McdeDispNormalize(McdeDispSet *s) { int i, minx = 0, miny = 0, first = 1; @@ -107,20 +107,20 @@ Align(int v, int len, int j, int jlen) } void -DispDock(DispSet *s, int i) +McdeDispDock(McdeDispSet *s, int i) { - Disp *d = &s->d[i]; + McdeDisp *d = &s->d[i]; int w, h, j, k, c, best = -1, bx = d->x, by = d->y; long bestDist = 0; - DispSize(d, &w, &h); + McdeDispSize(d, &w, &h); for (j = 0; j < s->n; j++) { int jw, jh; - const Disp *o = &s->d[j]; + const McdeDisp *o = &s->d[j]; if (j == i || !o->on) continue; - DispSize(o, &jw, &jh); + McdeDispSize(o, &jw, &jh); for (c = 0; c < 4; c++) { int cx, cy, free = 1; long dist; @@ -144,7 +144,7 @@ DispDock(DispSet *s, int i) if (k == i || !s->d[k].on) continue; - DispSize(&s->d[k], &kw, &kh); + McdeDispSize(&s->d[k], &kw, &kh); if (Overlap(cx, cy, w, h, s->d[k].x, s->d[k].y, kw, kh)) free = 0; @@ -173,11 +173,11 @@ DispDock(DispSet *s, int i) } d->x = bx; d->y = by; - DispNormalize(s); + McdeDispNormalize(s); } int -DispFindMode(const Disp *d, int w, int h, double rate) +McdeDispFindMode(const McdeDisp *d, int w, int h, double rate) { int i, best = -1; double bestDiff = 0; @@ -199,11 +199,11 @@ DispFindMode(const Disp *d, int w, int h, double rate) } int -DispCommonSize(const DispSet *s, int *w, int *h) +McdeDispCommonSize(const McdeDispSet *s, int *w, int *h) { int i, m, j, any = 0; long area = 0; - const Disp *first = NULL; + const McdeDisp *first = NULL; *w = *h = 0; for (i = 0; i < s->n; i++) @@ -217,7 +217,7 @@ DispCommonSize(const DispSet *s, int *w, int *h) int mw = first->modes[m].w, mh = first->modes[m].h, all = 1; for (j = 0; j < s->n && all; j++) - if (s->d[j].on && DispFindMode(&s->d[j], mw, mh, 0) < 0) + if (s->d[j].on && McdeDispFindMode(&s->d[j], mw, mh, 0) < 0) all = 0; if (all && (long) mw * mh > area) { area = (long) mw * mh; @@ -230,31 +230,31 @@ DispCommonSize(const DispSet *s, int *w, int *h) } int -DispMirror(DispSet *s) +McdeDispMirror(McdeDispSet *s) { int i, w, h; - if (!DispCommonSize(s, &w, &h)) + if (!McdeDispCommonSize(s, &w, &h)) return -1; for (i = 0; i < s->n; i++) { - Disp *d = &s->d[i]; + McdeDisp *d = &s->d[i]; int m; if (!d->on) continue; - m = DispFindMode(d, w, h, d->rate); + m = McdeDispFindMode(d, w, h, d->rate); d->w = w; d->h = h; d->rate = d->modes[m].rate; d->x = d->y = 0; - d->rot = ROT_NORMAL; + d->rot = MCDE_ROT_NORMAL; } s->mirror = 1; return 0; } int -DispPrimary(const DispSet *s) +McdeDispPrimary(const McdeDispSet *s) { int i, firstOn = -1; @@ -270,7 +270,7 @@ DispPrimary(const DispSet *s) } void -DispEdidId(const unsigned char *e, int len, char *buf, size_t size) +McdeDispEdidId(const unsigned char *e, int len, char *buf, size_t size) { static const unsigned char head[8] = { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 @@ -291,7 +291,7 @@ DispEdidId(const unsigned char *e, int len, char *buf, size_t size) } int -DispRead(FILE *f, DispSet *file) +McdeDispRead(FILE *f, McdeDispSet *file) { char line[256]; @@ -300,13 +300,13 @@ DispRead(FILE *f, DispSet *file) char id[48], state[8], rot[16], prim[16]; int w, h, x, y, n; double rate; - Disp *d; + McdeDisp *d; if (sscanf(line, "mirror %7s", state) == 1) { file->mirror = strcmp(state, "yes") == 0; continue; } - if (file->n >= DISP_MAX || + if (file->n >= MCDE_DISP_MAX || sscanf(line, "display %47s %7s", id, state) != 2) continue; d = &file->d[file->n]; @@ -320,7 +320,7 @@ DispRead(FILE *f, DispSet *file) prim[0] = '\0'; n = sscanf(line, "display %*s on %dx%d %lf %d %d %15s %15s", &w, &h, &rate, &x, &y, rot, prim); - if (n < 6 || w <= 0 || h <= 0 || DispRotParse(rot) < 0) + if (n < 6 || w <= 0 || h <= 0 || McdeDispRotParse(rot) < 0) continue; d->on = 1; d->w = w; @@ -328,7 +328,7 @@ DispRead(FILE *f, DispSet *file) d->rate = rate; d->x = x; d->y = y; - d->rot = DispRotParse(rot); + d->rot = McdeDispRotParse(rot); d->primary = n == 7 && strcmp(prim, "primary") == 0; file->n++; } @@ -336,20 +336,20 @@ DispRead(FILE *f, DispSet *file) } static void -WriteOne(FILE *f, const Disp *d, int primary) +WriteOne(FILE *f, const McdeDisp *d, int primary) { if (!d->on) fprintf(f, "display %s off\n", d->id); else fprintf(f, "display %s on %dx%d %.2f %d %d %s%s\n", d->id, - d->w, d->h, d->rate, d->x, d->y, DispRotName(d->rot), + d->w, d->h, d->rate, d->x, d->y, McdeDispRotName(d->rot), primary ? " primary" : ""); } int -DispWrite(FILE *f, const DispSet *s, const DispSet *remembered) +McdeDispWrite(FILE *f, const McdeDispSet *s, const McdeDispSet *remembered) { - int i, j, p = DispPrimary(s); + int i, j, p = McdeDispPrimary(s); fprintf(f, "# MCDE displays: System Settings, Displays\n"); fprintf(f, "mirror %s\n", s->mirror ? "yes" : "no"); @@ -375,14 +375,14 @@ DispWrite(FILE *f, const DispSet *s, const DispSet *remembered) } int -DispMerge(DispSet *live, const DispSet *file) +McdeDispMerge(McdeDispSet *live, const McdeDispSet *file) { - DispSet s = *live; + McdeDispSet s = *live; int i, j, taken = 0, on = 0, p; for (i = 0; i < file->n; i++) { - const Disp *fd = &file->d[i]; - Disp *d = NULL; + const McdeDisp *fd = &file->d[i]; + McdeDisp *d = NULL; for (j = 0; j < s.n; j++) if (s.d[j].connected && @@ -396,7 +396,7 @@ DispMerge(DispSet *live, const DispSet *file) taken++; continue; } - j = DispFindMode(d, fd->w, fd->h, fd->rate); + j = McdeDispFindMode(d, fd->w, fd->h, fd->rate); if (j < 0) continue; d->on = 1; @@ -414,14 +414,14 @@ DispMerge(DispSet *live, const DispSet *file) /* never a computer without a screen */ if (on == 0) return 0; - if (file->mirror && DispMirror(&s) != 0) + if (file->mirror && McdeDispMirror(&s) != 0) s.mirror = 0; else if (!file->mirror) s.mirror = 0; - p = DispPrimary(&s); + p = McdeDispPrimary(&s); for (i = 0; i < s.n; i++) s.d[i].primary = i == p; - DispNormalize(&s); + McdeDispNormalize(&s); *live = s; return taken; } diff --git a/lib/DtSvc/DtUtil2/McdeDisplays.c b/lib/DtSvc/DtUtil2/McdeDisplays.c new file mode 100644 index 00000000..4deaaad9 --- /dev/null +++ b/lib/DtSvc/DtUtil2/McdeDisplays.c @@ -0,0 +1,389 @@ +/* + * McdeDisplays.c -- MCDE: the monitors through RandR (include/Mcde/Displays.h) + * + * Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE. + * + * Read the outputs, their modes and the monitor's id (EDID), set a whole + * arrangement as xrandr(1) does -- the CRTCs that change off, the screen's + * new size, the CRTCs on -- and keep it in ~/.dt/displays. System + * Settings > Displays (programs/dtsystemsettings/displays.c) and dtwm (a + * monitor plugged in or out, WmMcdeDisplays.c) both use it. Without + * libXrandr (HAVE_XRANDR) every function says it cannot. + */ + +#include + +#include +#include +#include +#include + +#include +#ifdef HAVE_XRANDR +#include +#endif + +#include + +#ifdef HAVE_XRANDR + +const char * +McdeDispConfigPath(void) +{ + static char path[1024]; + const char *home = getenv("HOME"); + + snprintf(path, sizeof path, "%s/.dt/displays", home ? home : "."); + return path; +} + +/* rates as xrandr computes them */ +static double +ModeRate(const XRRModeInfo *m) +{ + double v = (double) m->hTotal * m->vTotal; + + if (m->modeFlags & RR_DoubleScan) + v *= 2; + if (m->modeFlags & RR_Interlace) + v /= 2; + return v > 0 ? (double) m->dotClock / v : 0; +} + +static int +RotIndex(Rotation r) +{ + switch (r & 0xf) { + case RR_Rotate_90: + return MCDE_ROT_LEFT; + case RR_Rotate_180: + return MCDE_ROT_INVERTED; + case RR_Rotate_270: + return MCDE_ROT_RIGHT; + default: + return MCDE_ROT_NORMAL; + } +} + +static Rotation +RotValue(int rot) +{ + return (Rotation) (1 << rot); /* RR_Rotate_0, 90, 180, 270 */ +} + +/* the id of the monitor on OUT: from its EDID, else "output-NAME" */ +static void +MonitorId(Display *dpy, RROutput out, const char *name, char *buf, + size_t size) +{ + Atom edid = XInternAtom(dpy, RR_PROPERTY_RANDR_EDID, True), type; + unsigned char *data = NULL; + unsigned long n = 0, after; + int format; + + buf[0] = '\0'; + if (edid != None && XRRGetOutputProperty(dpy, out, edid, 0, 32, False, + False, AnyPropertyType, &type, &format, &n, &after, &data) == + Success && data != NULL) { + if (format == 8) + McdeDispEdidId(data, (int) n, buf, size); + XFree(data); + } + if (buf[0] == '\0') + snprintf(buf, size, "output-%s", name); +} + +int +McdeDispLoad(Display *dpy, McdeDispSet *s) +{ + Window root = DefaultRootWindow(dpy); + XRRScreenResources *res; + RROutput primary; + int ev, err, major = 0, minor = 0, o, m, k; + + memset(s, 0, sizeof *s); + if (!XRRQueryExtension(dpy, &ev, &err) || + !XRRQueryVersion(dpy, &major, &minor) || + major < 1 || (major == 1 && minor < 3)) + return -1; + res = XRRGetScreenResourcesCurrent(dpy, root); + if (res == NULL) + return -1; + primary = XRRGetOutputPrimary(dpy, root); + for (o = 0; o < res->noutput && s->n < MCDE_DISP_MAX; o++) { + XRROutputInfo *oi = XRRGetOutputInfo(dpy, res, res->outputs[o]); + McdeDisp *d; + int i = s->n; + + if (oi == NULL) + continue; + if (oi->connection != RR_Connected && oi->crtc == None) { + XRRFreeOutputInfo(oi); + continue; + } + d = &s->d[i]; + snprintf(d->name, sizeof d->name, "%s", oi->name); + MonitorId(dpy, res->outputs[o], oi->name, d->id, sizeof d->id); + d->connected = oi->connection == RR_Connected; + d->output = res->outputs[o]; + d->rotations = RR_Rotate_0; + for (k = 0; k < oi->nmode && d->nmodes < MCDE_DISP_MODES; k++) + for (m = 0; m < res->nmode; m++) { + XRRModeInfo *mi = &res->modes[m]; + int n = d->nmodes; + + if (mi->id != oi->modes[k]) + continue; + d->modes[n].w = (int) mi->width; + d->modes[n].h = (int) mi->height; + d->modes[n].rate = ModeRate(mi); + d->modeId[n] = mi->id; + d->nmodes++; + break; + } + if (oi->crtc != None || oi->ncrtc > 0) { + XRRCrtcInfo *ci = XRRGetCrtcInfo(dpy, res, + oi->crtc != None ? oi->crtc : oi->crtcs[0]); + + if (ci != NULL) { + d->rotations = ci->rotations; + if (oi->crtc != None && ci->mode != None) { + d->on = 1; + d->x = ci->x; + d->y = ci->y; + d->rot = RotIndex(ci->rotation); + for (m = 0; m < res->nmode; m++) + if (res->modes[m].id == ci->mode) { + d->w = (int) res->modes[m].width; + d->h = (int) res->modes[m].height; + d->rate = ModeRate(&res->modes[m]); + } + } + XRRFreeCrtcInfo(ci); + } + } + if (!d->on && d->nmodes > 0) { + /* the first mode is the one the monitor prefers */ + d->w = d->modes[0].w; + d->h = d->modes[0].h; + d->rate = d->modes[0].rate; + } + d->primary = res->outputs[o] == primary; + XRRFreeOutputInfo(oi); + s->n++; + } + XRRFreeScreenResources(res); + /* two monitors at the same place and size: mirrored */ + { + int a, b, on = 0, same = 1; + + for (a = 0; a < s->n; a++) + if (s->d[a].on) { + on++; + for (b = 0; b < a; b++) + if (s->d[b].on && (s->d[b].x != s->d[a].x || + s->d[b].y != s->d[a].y || + s->d[b].w != s->d[a].w)) + same = 0; + } + s->mirror = on > 1 && same; + } + return 0; +} + +int +McdeDispApply(Display *dpy, const McdeDispSet *s) +{ + Window root = DefaultRootWindow(dpy); + XRRScreenResources *res; + RRCrtc target[MCDE_DISP_MAX]; + RRMode mode[MCDE_DISP_MAX]; + Bool kept[MCDE_DISP_MAX]; + int W, H, minW, minH, maxW, maxH, i, c, p, rc = 0; + + McdeDispBounds(s, &W, &H); + if (W == 0 || H == 0) + return -1; + if (!XRRGetScreenSizeRange(dpy, root, &minW, &minH, &maxW, &maxH) || + W > maxW || H > maxH) + return -1; + if (W < minW) + W = minW; + if (H < minH) + H = minH; + res = XRRGetScreenResourcesCurrent(dpy, root); + if (res == NULL) + return -1; + + /* a CRTC for every display that is on: its own, else a free one */ + for (i = 0; i < s->n; i++) { + XRROutputInfo *oi; + int m; + + target[i] = None; + mode[i] = None; + kept[i] = False; + if (!s->d[i].on) + continue; + m = McdeDispFindMode(&s->d[i], s->d[i].w, s->d[i].h, s->d[i].rate); + if (m < 0) + continue; + mode[i] = (RRMode) s->d[i].modeId[m]; + oi = XRRGetOutputInfo(dpy, res, (RROutput) s->d[i].output); + if (oi == NULL) + continue; + for (c = -1; c < oi->ncrtc && target[i] == None; c++) { + RRCrtc cand = c < 0 ? oi->crtc : oi->crtcs[c]; + int j, used = 0; + + if (cand == None) + continue; + for (j = 0; j < i; j++) + if (target[j] == cand) + used = 1; + if (!used) + target[i] = cand; + } + XRRFreeOutputInfo(oi); + } + + XGrabServer(dpy); + /* off: every CRTC whose setting changes or that would not fit */ + for (c = 0; c < res->ncrtc; c++) { + XRRCrtcInfo *ci = XRRGetCrtcInfo(dpy, res, res->crtcs[c]); + int j = -1, dw, dh; + + if (ci == NULL) + continue; + for (i = 0; i < s->n; i++) + if (target[i] == res->crtcs[c]) + j = i; + if (ci->mode != None) { + Bool keep = False; + + if (j >= 0) { + McdeDispSize(&s->d[j], &dw, &dh); + keep = ci->mode == mode[j] && + ci->x == s->d[j].x && ci->y == s->d[j].y && + ci->rotation == RotValue(s->d[j].rot) && + ci->noutput == 1 && + ci->outputs[0] == (RROutput) s->d[j].output && + s->d[j].x + dw <= W && s->d[j].y + dh <= H && + (int) ci->x + (int) ci->width <= W && + (int) ci->y + (int) ci->height <= H; + kept[j] = keep; + } + if (!keep) + XRRSetCrtcConfig(dpy, res, res->crtcs[c], + CurrentTime, 0, 0, None, RR_Rotate_0, NULL, 0); + } + XRRFreeCrtcInfo(ci); + } + /* the screen: 96 dpi for its size in millimetres, as Xorg assumes */ + XRRSetScreenSize(dpy, root, W, H, (int) (W * 25.4 / 96), + (int) (H * 25.4 / 96)); + for (i = 0; i < s->n; i++) { + RROutput out = (RROutput) s->d[i].output; + + if (!s->d[i].on || kept[i]) + continue; + if (target[i] == None || mode[i] == None || + XRRSetCrtcConfig(dpy, res, target[i], CurrentTime, + s->d[i].x, s->d[i].y, mode[i], RotValue(s->d[i].rot), + &out, 1) != RRSetConfigSuccess) + rc = -1; + } + p = McdeDispPrimary(s); + if (p >= 0) + XRRSetOutputPrimary(dpy, root, (RROutput) s->d[p].output); + XUngrabServer(dpy); + XSync(dpy, False); + XRRFreeScreenResources(res); + return rc; +} + +int +McdeDispApplyFile(Display *dpy) +{ + static McdeDispSet file, live; /* large: not on the stack */ + FILE *f; + + if ((f = fopen(McdeDispConfigPath(), "r")) == NULL) + return 0; + McdeDispRead(f, &file); + fclose(f); + if (McdeDispLoad(dpy, &live) != 0) + return -1; + if (McdeDispMerge(&live, &file) == 0) + return 0; + return McdeDispApply(dpy, &live) == 0 ? 1 : -1; +} + +int +McdeDispSave(const McdeDispSet *live) +{ + static McdeDispSet remembered; /* large: not on the stack */ + char dir[1024]; + const char *home = getenv("HOME"); + FILE *f; + int rc; + + /* the monitors that are not plugged in stay in the file */ + memset(&remembered, 0, sizeof remembered); + if ((f = fopen(McdeDispConfigPath(), "r")) != NULL) { + McdeDispRead(f, &remembered); + fclose(f); + } + snprintf(dir, sizeof dir, "%s/.dt", home ? home : "."); + (void) mkdir(dir, 0755); + if ((f = fopen(McdeDispConfigPath(), "w")) == NULL) + return -1; + rc = McdeDispWrite(f, live, &remembered); + if (fclose(f) != 0) + rc = -1; + return rc; +} + +#else /* !HAVE_XRANDR */ + +const char * +McdeDispConfigPath(void) +{ + static char path[1024]; + const char *home = getenv("HOME"); + + snprintf(path, sizeof path, "%s/.dt/displays", home ? home : "."); + return path; +} + +int +McdeDispLoad(Display *dpy, McdeDispSet *s) +{ + (void) dpy; + memset(s, 0, sizeof *s); + return -1; +} + +int +McdeDispApply(Display *dpy, const McdeDispSet *s) +{ + (void) dpy; + (void) s; + return -1; +} + +int +McdeDispApplyFile(Display *dpy) +{ + (void) dpy; + return 0; +} + +int +McdeDispSave(const McdeDispSet *live) +{ + (void) live; + return -1; +} + +#endif /* HAVE_XRANDR */ diff --git a/programs/dtsystemsettings/test_displays.c b/lib/DtSvc/DtUtil2/test_displays.c similarity index 81% rename from programs/dtsystemsettings/test_displays.c rename to lib/DtSvc/DtUtil2/test_displays.c index 63a39716..69141c2d 100644 --- a/programs/dtsystemsettings/test_displays.c +++ b/lib/DtSvc/DtUtil2/test_displays.c @@ -8,7 +8,7 @@ #include #include -#include "dispconf.h" +#include static int failed; @@ -17,7 +17,7 @@ static int failed; } while (0) static void -AddMode(Disp *d, int w, int h, double rate) +AddMode(McdeDisp *d, int w, int h, double rate) { d->modes[d->nmodes].w = w; d->modes[d->nmodes].h = h; @@ -27,7 +27,7 @@ AddMode(Disp *d, int w, int h, double rate) /* two monitors: DVI-0 1280x1024 (and 1024x768), VGA-0 1024x768 (800x600) */ static void -Two(DispSet *s) +Two(McdeDispSet *s) { memset(s, 0, sizeof *s); s->n = 2; @@ -51,57 +51,57 @@ Two(DispSet *s) int main(void) { - DispSet s, f; + McdeDispSet s, f; int w, h; FILE *tmp; char buf[512]; size_t n; Two(&s); - DispBounds(&s, &w, &h); + McdeDispBounds(&s, &w, &h); CHECK(w == 2304 && h == 1024); /* turned left: VGA-0 is 768 wide, 1024 high */ - s.d[1].rot = ROT_LEFT; - DispBounds(&s, &w, &h); + s.d[1].rot = MCDE_ROT_LEFT; + McdeDispBounds(&s, &w, &h); CHECK(w == 2048 && h == 1024); - s.d[1].rot = ROT_NORMAL; + s.d[1].rot = MCDE_ROT_NORMAL; /* dragged below DVI-0, a little to the right: docks under it, the * left edges lined up (within 48 pixels) */ s.d[1].x = 30; s.d[1].y = 1100; - DispDock(&s, 1); + McdeDispDock(&s, 1); CHECK(s.d[1].x == 0 && s.d[1].y == 1024); /* dragged far to the left: docks left of DVI-0, tops lined up; after * normalizing VGA-0 starts at 0 and DVI-0 moved right */ s.d[1].x = -1500; s.d[1].y = 20; - DispDock(&s, 1); + McdeDispDock(&s, 1); CHECK(s.d[1].x == 0 && s.d[1].y == 0); CHECK(s.d[0].x == 1024 && s.d[0].y == 0); /* dropped on top of DVI-0: never overlapping */ s.d[1].x = s.d[0].x + 100; s.d[1].y = 100; - DispDock(&s, 1); + McdeDispDock(&s, 1); CHECK(s.d[1].x >= s.d[0].x + 1280 || s.d[1].x + 1024 <= s.d[0].x || s.d[1].y >= s.d[0].y + 1024 || s.d[1].y + 768 <= s.d[0].y); /* and touching it: one screen without a gap */ - DispBounds(&s, &w, &h); + McdeDispBounds(&s, &w, &h); CHECK((w == 2304 && h <= 1024 + 768) || (h == 1792 && w <= 2304)); /* a display that is off counts for nothing */ Two(&s); s.d[1].on = 0; - DispBounds(&s, &w, &h); + McdeDispBounds(&s, &w, &h); CHECK(w == 1280 && h == 1024); /* mirroring: the largest size both have, the rate nearest its own */ Two(&s); - CHECK(DispCommonSize(&s, &w, &h) && w == 1024 && h == 768); - CHECK(DispMirror(&s) == 0); + CHECK(McdeDispCommonSize(&s, &w, &h) && w == 1024 && h == 768); + CHECK(McdeDispMirror(&s) == 0); CHECK(s.mirror && s.d[0].x == 0 && s.d[1].x == 0); CHECK(s.d[0].w == 1024 && s.d[0].rate == 60.00); @@ -110,12 +110,12 @@ main(void) s.d[1].nmodes = 1; s.d[1].modes[0].w = 640; s.d[1].modes[0].h = 480; - CHECK(DispMirror(&s) == -1); + CHECK(McdeDispMirror(&s) == -1); /* the file: written and read back */ Two(&s); tmp = tmpfile(); - CHECK(DispWrite(tmp, &s, NULL) == 0); + CHECK(McdeDispWrite(tmp, &s, NULL) == 0); rewind(tmp); n = fread(buf, 1, sizeof buf - 1, tmp); buf[n] = '\0'; @@ -124,7 +124,7 @@ main(void) CHECK(strstr(buf, "display output-VGA-0 on 1024x768 60.00 1280 0 " "normal\n") != NULL); rewind(tmp); - DispRead(tmp, &f); + McdeDispRead(tmp, &f); fclose(tmp); CHECK(f.n == 2 && f.d[0].primary && !f.d[1].primary); CHECK(f.d[1].x == 1280 && f.d[1].w == 1024 && f.d[1].on); @@ -138,21 +138,21 @@ main(void) "display SAM-0e12-00000001 on 640x480 60 0 0 normal\n" "garbage line\n", tmp); rewind(tmp); - DispRead(tmp, &f); + McdeDispRead(tmp, &f); fclose(tmp); Two(&s); - CHECK(DispMerge(&s, &f) == 2); + CHECK(McdeDispMerge(&s, &f) == 2); CHECK(s.d[1].primary && !s.d[0].primary); CHECK(s.d[0].x == 1024 && s.d[1].x == 0); /* the same Dell on another socket: still its place */ Two(&s); snprintf(s.d[0].name, sizeof s.d[0].name, "HDMI-1"); - CHECK(DispMerge(&s, &f) == 2 && s.d[0].x == 1024); + CHECK(McdeDispMerge(&s, &f) == 2 && s.d[0].x == 1024); /* written again: the Samsung that is not plugged in is kept */ tmp = tmpfile(); - DispWrite(tmp, &s, &f); + McdeDispWrite(tmp, &s, &f); rewind(tmp); n = fread(buf, 1, sizeof buf - 1, tmp); buf[n] = '\0'; @@ -173,10 +173,10 @@ main(void) 0x4c }; char id[48]; - DispEdidId(e, 16, id, sizeof id); + McdeDispEdidId(e, 16, id, sizeof id); CHECK(strcmp(id, "DEL-a0b7-4c4a3132") == 0); e[0] = 1; - DispEdidId(e, 16, id, sizeof id); + McdeDispEdidId(e, 16, id, sizeof id); CHECK(id[0] == '\0'); } @@ -184,23 +184,23 @@ main(void) tmp = tmpfile(); fputs("display output-VGA-0 on 1920x1080 60 0 0 normal\n", tmp); rewind(tmp); - DispRead(tmp, &f); + McdeDispRead(tmp, &f); fclose(tmp); Two(&s); - CHECK(DispMerge(&s, &f) == 0 && s.d[1].w == 1024); + CHECK(McdeDispMerge(&s, &f) == 0 && s.d[1].w == 1024); /* every display off: refused, the screen stays */ tmp = tmpfile(); fputs("display output-VGA-0 off\ndisplay DEL-a0b7-4c4a3132 off\n", tmp); rewind(tmp); - DispRead(tmp, &f); + McdeDispRead(tmp, &f); fclose(tmp); Two(&s); - CHECK(DispMerge(&s, &f) == 0 && s.d[0].on && s.d[1].on); + CHECK(McdeDispMerge(&s, &f) == 0 && s.d[0].on && s.d[1].on); /* rotation names */ - CHECK(DispRotParse("left") == ROT_LEFT && DispRotParse("x") == -1); - CHECK(strcmp(DispRotName(ROT_INVERTED), "inverted") == 0); + CHECK(McdeDispRotParse("left") == MCDE_ROT_LEFT && McdeDispRotParse("x") == -1); + CHECK(strcmp(McdeDispRotName(MCDE_ROT_INVERTED), "inverted") == 0); if (failed) printf("dispconf: %d failed\n", failed); diff --git a/lib/DtSvc/Makefile.am b/lib/DtSvc/Makefile.am index 892afa65..119533dd 100644 --- a/lib/DtSvc/Makefile.am +++ b/lib/DtSvc/Makefile.am @@ -175,12 +175,15 @@ libDtSvc_la_SOURCES = DtCodelibs/buf.C \ DtUtil2/XmWrap.c \ DtUtil2/McdeMenuModel.c \ DtUtil2/McdeGlobalMenu.c \ + DtUtil2/McdeDispConf.c \ + DtUtil2/McdeDisplays.c \ DtUtil2/McdeImage.c \ DtUtil2/McdeWmHints.c # MCDE: libpng reads PNG pictures (DtUtil2/McdeImage.c) libDtSvc_la_CPPFLAGS += $(PNG_CFLAGS) -libDtSvc_la_LIBADD = $(PNG_LIBS) +# Xrandr: the monitors (DtUtil2/McdeDisplays.c), empty without it +libDtSvc_la_LIBADD = $(PNG_LIBS) $(XRANDRLIB) if HAS_PAM_LIBRARY libDtSvc_la_SOURCES += DtUtil2/SvcPam.c @@ -189,7 +192,10 @@ endif # MCDE: the text form of a menu bar (DtUtil2/McdeMenuModel.c) is plain C and # has its own tests (make check) -check_PROGRAMS = test_menumodel +check_PROGRAMS = test_menumodel test_displays test_menumodel_SOURCES = DtUtil2/test_menumodel.c DtUtil2/McdeMenuModel.c test_menumodel_CPPFLAGS = -I$(top_srcdir)/include -TESTS = test_menumodel +# the arrangement of the monitors (DtUtil2/McdeDispConf.c), plain C too +test_displays_SOURCES = DtUtil2/test_displays.c DtUtil2/McdeDispConf.c +test_displays_CPPFLAGS = -I$(top_srcdir)/include +TESTS = test_menumodel test_displays diff --git a/programs/dtsystemsettings/Makefile.am b/programs/dtsystemsettings/Makefile.am index 384aafe0..92af480b 100644 --- a/programs/dtsystemsettings/Makefile.am +++ b/programs/dtsystemsettings/Makefile.am @@ -6,17 +6,16 @@ bin_PROGRAMS = dtsystemsettings dtsystemsettings_SOURCES = main.c panes.c panes.h ui.h accounts.c update.c \ input.c appearance.c desktop.c timezone.c wifi.c \ - startup.c sharing.c backup.c displays.c dispconf.c dispconf.h + startup.c sharing.c backup.c displays.c dtsystemsettings_CPPFLAGS = -I$(top_srcdir)/include $(DT_INCDIR) \ -I$(top_builddir)/include $(XFT_CFLAGS) \ -DCDE_INSTALLATION_TOP=\"$(CDE_INSTALLATION_TOP)\" \ -DCDE_CONFIGURATION_TOP=\"$(CDE_CONFIGURATION_TOP)\" # Xft and fontconfig: the font families (appearance.c) -# Xrandr: the Displays pane (displays.c), when configure found it +# Xrandr: the monitors (libDtSvc's McdeDisplays.c), when configure found it dtsystemsettings_LDADD = $(LIBMCDESYS) $(DTCLIENTLIBS) $(XRANDRLIB) \ $(XTOOLLIB) $(XFTLIB) -check_PROGRAMS = test_panes test_displays +check_PROGRAMS = test_panes test_panes_SOURCES = test_panes.c panes.c panes.h -test_displays_SOURCES = test_displays.c dispconf.c dispconf.h -TESTS = test_panes test_displays +TESTS = test_panes diff --git a/programs/dtsystemsettings/dispconf.h b/programs/dtsystemsettings/dispconf.h deleted file mode 100644 index 3abefab1..00000000 --- a/programs/dtsystemsettings/dispconf.h +++ /dev/null @@ -1,110 +0,0 @@ -/* - * dispconf.h -- dtsystemsettings: the arrangement of the displays - * - * Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE. - * - * What the Displays pane (displays.c) shows and changes, without X: the - * outputs with their modes, where they are, how they are turned; the file - * ~/.dt/displays that keeps it; the arithmetic of arranging (bounds, - * docking a dragged display to the others, mirroring). C99 only, no X: - * test_displays.c tests it (make check). - * - * MCDE remembers a monitor, not a socket (Konrad, 2026-11-11: "mcde muss - * sich ein display und dessen position/konfiguration merken"): its id is - * made from the EDID the monitor sends (maker, product, serial number: - * "DEL-a0b7-4c4a3132"), "output-NAME" for one without (a projector of old, - * a virtual screen). A monitor that is not plugged in keeps its line, so - * that plugging it in again brings back where it was. - * - * ~/.dt/displays, one line a monitor, English keys (configuration is - * English, Konrad 2026-11-11): - * - * mirror no - * display DEL-a0b7-4c4a3132 on 1280x1024 60.02 0 0 normal primary - * display output-VGA-0 on 1024x768 60.00 1280 0 left - * display SAM-0e12-00000000 off - */ - -#ifndef _dtsystemsettings_dispconf_h -#define _dtsystemsettings_dispconf_h - -#include - -#define DISP_MAX 16 -#define DISP_MODES 64 - -enum DispRotation { ROT_NORMAL, ROT_LEFT, ROT_INVERTED, ROT_RIGHT }; - -typedef struct { - int w, h; - double rate; /* Hz */ -} DispMode; - -typedef struct { - char name[32]; /* the RandR output: "DVI-0" */ - char id[48]; /* the monitor: EDID id or "output-NAME" */ - int connected; /* a monitor is plugged in */ - int on; - int primary; /* top bar and front panel are here */ - int w, h; /* the mode, not turned */ - double rate; - int x, y; /* in the screen */ - int rot; /* enum DispRotation */ - DispMode modes[DISP_MODES]; - int nmodes; -} Disp; - -typedef struct { - Disp d[DISP_MAX]; - int n; - int mirror; /* all displays show the same */ -} DispSet; - -/* "normal", "left", "inverted", "right"; -1 if none */ -const char *DispRotName(int rot); -int DispRotParse(const char *name); - -/* the size on the screen, turned */ -void DispSize(const Disp *d, int *w, int *h); - -/* the screen around the displays that are on (0 0 if none) */ -void DispBounds(const DispSet *s, int *w, int *h); - -/* the displays that are on moved so that the leftmost and topmost start - * at 0 */ -void DispNormalize(DispSet *s); - -/* display I, dropped where it is, docks to the nearest edge of another - * display that is on, without overlapping any; then DispNormalize */ -void DispDock(DispSet *s, int i); - -/* the largest size every display that is on has a mode for; 0 if none */ -int DispCommonSize(const DispSet *s, int *w, int *h); - -/* mirroring: every display that is on at 0 0 with the common size (and - * the mode with the rate closest to its own); -1 if there is none */ -int DispMirror(DispSet *s); - -/* the mode of D with W x H and the rate closest to RATE; -1 if none */ -int DispFindMode(const Disp *d, int w, int h, double rate); - -/* the first display that is on and primary, else the first that is on; - * -1 if none is on */ -int DispPrimary(const DispSet *s); - -/* the id of a monitor from its EDID (at least 16 bytes); "" if it is not - * one; into BUF */ -void DispEdidId(const unsigned char *edid, int len, char *buf, size_t size); - -/* the file: read into FILE (monitors by id and setting, no modes); - * write S (the connected monitors) and after it what REMEMBERED says of - * the monitors that are not connected now (REMEMBERED may be NULL) */ -int DispRead(FILE *f, DispSet *file); -int DispWrite(FILE *f, const DispSet *s, const DispSet *remembered); - -/* what FILE says, put on LIVE for the monitors that are connected now and - * have the mode; the number of monitors that took it. A monitor the file - * does not name keeps what it has. */ -int DispMerge(DispSet *live, const DispSet *file); - -#endif /* _dtsystemsettings_dispconf_h */ diff --git a/programs/dtsystemsettings/displays.c b/programs/dtsystemsettings/displays.c index f4446e32..79269134 100644 --- a/programs/dtsystemsettings/displays.c +++ b/programs/dtsystemsettings/displays.c @@ -15,7 +15,8 @@ * * "Apply" asks "Keep these settings?" and goes back by itself after 15 * seconds (a monitor that shows nothing cannot be answered on). What is - * kept goes to ~/.dt/displays (dispconf.h); at login + * kept goes to ~/.dt/displays (include/Mcde/Displays.h, lib/DtSvc/DtUtil2/ + * McdeDisplays.c, which dtwm uses too); at login * `dtsystemsettings -apply-displays` (Xsession.d/0071.displays) puts it on * the outputs that are plugged in. The arrangement is drawn on Expose and * while dragging only. @@ -26,7 +27,6 @@ #include #include #include -#include #include #include @@ -40,18 +40,15 @@ #include #endif -#include "dispconf.h" +#include #include "ui.h" #define CONFIRM_SECONDS 15 #ifdef HAVE_XRANDR -static DispSet live; /* what the pane shows */ -static DispSet before; /* what was there before Apply */ -static RROutput outId[DISP_MAX]; -static RRMode modeId[DISP_MAX][DISP_MODES]; -static unsigned short rotMask[DISP_MAX]; +static McdeDispSet live; /* what the pane shows */ +static McdeDispSet before; /* what was there before Apply */ static int selected; static Widget area, nameLabel, onToggle, mainToggle, mirrorToggle; @@ -70,297 +67,11 @@ static int offX, offY; static void Draw(void); static void ShowSelected(void); -static const char * -ConfigPath(void) -{ - static char path[1024]; - const char *home = getenv("HOME"); - - snprintf(path, sizeof path, "%s/.dt/displays", home ? home : "."); - return path; -} - -/* rates as xrandr computes them */ -static double -ModeRate(const XRRModeInfo *m) -{ - double v = (double) m->hTotal * m->vTotal; - - if (m->modeFlags & RR_DoubleScan) - v *= 2; - if (m->modeFlags & RR_Interlace) - v /= 2; - return v > 0 ? (double) m->dotClock / v : 0; -} - -static int -RotIndex(Rotation r) -{ - switch (r & 0xf) { - case RR_Rotate_90: - return ROT_LEFT; - case RR_Rotate_180: - return ROT_INVERTED; - case RR_Rotate_270: - return ROT_RIGHT; - default: - return ROT_NORMAL; - } -} - -static Rotation -RotValue(int rot) -{ - return (Rotation) (1 << rot); /* RR_Rotate_0, 90, 180, 270 */ -} - -/* the id of the monitor on OUT: from its EDID, else "output-NAME" */ -static void -MonitorId(Display *dpy, RROutput out, const char *name, char *buf, - size_t size) -{ - Atom edid = XInternAtom(dpy, RR_PROPERTY_RANDR_EDID, True), type; - unsigned char *data = NULL; - unsigned long n = 0, after; - int format; - - buf[0] = '\0'; - if (edid != None && XRRGetOutputProperty(dpy, out, edid, 0, 32, False, - False, AnyPropertyType, &type, &format, &n, &after, &data) == - Success && data != NULL) { - if (format == 8) - DispEdidId(data, (int) n, buf, size); - XFree(data); - } - if (buf[0] == '\0') - snprintf(buf, size, "output-%s", name); -} - -/* the outputs as the server has them now; -1 without RandR 1.3 */ -static int -Load(Display *dpy, DispSet *s) -{ - Window root = DefaultRootWindow(dpy); - XRRScreenResources *res; - RROutput primary; - int ev, err, major = 0, minor = 0, o, m, k; - - memset(s, 0, sizeof *s); - if (!XRRQueryExtension(dpy, &ev, &err) || - !XRRQueryVersion(dpy, &major, &minor) || - major < 1 || (major == 1 && minor < 3)) - return -1; - res = XRRGetScreenResourcesCurrent(dpy, root); - if (res == NULL) - return -1; - primary = XRRGetOutputPrimary(dpy, root); - for (o = 0; o < res->noutput && s->n < DISP_MAX; o++) { - XRROutputInfo *oi = XRRGetOutputInfo(dpy, res, res->outputs[o]); - Disp *d; - int i = s->n; - - if (oi == NULL) - continue; - if (oi->connection != RR_Connected && oi->crtc == None) { - XRRFreeOutputInfo(oi); - continue; - } - d = &s->d[i]; - snprintf(d->name, sizeof d->name, "%s", oi->name); - MonitorId(dpy, res->outputs[o], oi->name, d->id, sizeof d->id); - d->connected = oi->connection == RR_Connected; - outId[i] = res->outputs[o]; - rotMask[i] = RR_Rotate_0; - for (k = 0; k < oi->nmode && d->nmodes < DISP_MODES; k++) - for (m = 0; m < res->nmode; m++) { - XRRModeInfo *mi = &res->modes[m]; - int n = d->nmodes; - - if (mi->id != oi->modes[k]) - continue; - d->modes[n].w = (int) mi->width; - d->modes[n].h = (int) mi->height; - d->modes[n].rate = ModeRate(mi); - modeId[i][n] = mi->id; - d->nmodes++; - break; - } - if (oi->crtc != None || oi->ncrtc > 0) { - XRRCrtcInfo *ci = XRRGetCrtcInfo(dpy, res, - oi->crtc != None ? oi->crtc : oi->crtcs[0]); - - if (ci != NULL) { - rotMask[i] = ci->rotations; - if (oi->crtc != None && ci->mode != None) { - d->on = 1; - d->x = ci->x; - d->y = ci->y; - d->rot = RotIndex(ci->rotation); - for (m = 0; m < res->nmode; m++) - if (res->modes[m].id == ci->mode) { - d->w = (int) res->modes[m].width; - d->h = (int) res->modes[m].height; - d->rate = ModeRate(&res->modes[m]); - } - } - XRRFreeCrtcInfo(ci); - } - } - if (!d->on && d->nmodes > 0) { - /* the first mode is the one the monitor prefers */ - d->w = d->modes[0].w; - d->h = d->modes[0].h; - d->rate = d->modes[0].rate; - } - d->primary = res->outputs[o] == primary; - XRRFreeOutputInfo(oi); - s->n++; - } - XRRFreeScreenResources(res); - /* two monitors at the same place and size: mirrored */ - { - int a, b, on = 0, same = 1; - - for (a = 0; a < s->n; a++) - if (s->d[a].on) { - on++; - for (b = 0; b < a; b++) - if (s->d[b].on && (s->d[b].x != s->d[a].x || - s->d[b].y != s->d[a].y || - s->d[b].w != s->d[a].w)) - same = 0; - } - s->mirror = on > 1 && same; - } - return 0; -} - -/* S onto the server; 0 on success */ -static int -ApplyX(Display *dpy, const DispSet *s) -{ - Window root = DefaultRootWindow(dpy); - XRRScreenResources *res; - RRCrtc target[DISP_MAX]; - RRMode mode[DISP_MAX]; - Boolean kept[DISP_MAX]; - int W, H, minW, minH, maxW, maxH, i, c, p, rc = 0; - - DispBounds(s, &W, &H); - if (W == 0 || H == 0) - return -1; - if (!XRRGetScreenSizeRange(dpy, root, &minW, &minH, &maxW, &maxH) || - W > maxW || H > maxH) - return -1; - if (W < minW) - W = minW; - if (H < minH) - H = minH; - res = XRRGetScreenResourcesCurrent(dpy, root); - if (res == NULL) - return -1; - - /* a CRTC for every display that is on: its own, else a free one */ - for (i = 0; i < s->n; i++) { - XRROutputInfo *oi; - int m; - - target[i] = None; - mode[i] = None; - kept[i] = False; - if (!s->d[i].on) - continue; - m = DispFindMode(&s->d[i], s->d[i].w, s->d[i].h, s->d[i].rate); - if (m < 0) - continue; - mode[i] = modeId[i][m]; - oi = XRRGetOutputInfo(dpy, res, outId[i]); - if (oi == NULL) - continue; - for (c = -1; c < oi->ncrtc && target[i] == None; c++) { - RRCrtc cand = c < 0 ? oi->crtc : oi->crtcs[c]; - int j, used = 0; - - if (cand == None) - continue; - for (j = 0; j < i; j++) - if (target[j] == cand) - used = 1; - if (!used) - target[i] = cand; - } - XRRFreeOutputInfo(oi); - } - - XGrabServer(dpy); - /* off: every CRTC whose setting changes or that would not fit */ - for (c = 0; c < res->ncrtc; c++) { - XRRCrtcInfo *ci = XRRGetCrtcInfo(dpy, res, res->crtcs[c]); - int j = -1, dw, dh; - - if (ci == NULL) - continue; - for (i = 0; i < s->n; i++) - if (target[i] == res->crtcs[c]) - j = i; - if (ci->mode != None) { - Boolean keep = False; - - if (j >= 0) { - DispSize(&s->d[j], &dw, &dh); - keep = ci->mode == mode[j] && - ci->x == s->d[j].x && ci->y == s->d[j].y && - ci->rotation == RotValue(s->d[j].rot) && - ci->noutput == 1 && - ci->outputs[0] == outId[j] && - s->d[j].x + dw <= W && s->d[j].y + dh <= H && - (int) ci->x + (int) ci->width <= W && - (int) ci->y + (int) ci->height <= H; - kept[j] = keep; - } - if (!keep) - XRRSetCrtcConfig(dpy, res, res->crtcs[c], - CurrentTime, 0, 0, None, RR_Rotate_0, NULL, 0); - } - XRRFreeCrtcInfo(ci); - } - /* the screen: 96 dpi for its size in millimetres, as Xorg assumes */ - XRRSetScreenSize(dpy, root, W, H, (int) (W * 25.4 / 96), - (int) (H * 25.4 / 96)); - for (i = 0; i < s->n; i++) { - if (!s->d[i].on || kept[i]) - continue; - if (target[i] == None || mode[i] == None || - XRRSetCrtcConfig(dpy, res, target[i], CurrentTime, - s->d[i].x, s->d[i].y, mode[i], RotValue(s->d[i].rot), - &outId[i], 1) != RRSetConfigSuccess) - rc = -1; - } - p = DispPrimary(s); - if (p >= 0) - XRRSetOutputPrimary(dpy, root, outId[p]); - XUngrabServer(dpy); - XSync(dpy, False); - XRRFreeScreenResources(res); - return rc; -} - /* dtsystemsettings -apply-displays: ~/.dt/displays on what is plugged in */ int ApplyDisplaysCommand(Display *dpy) { - static DispSet file; - FILE *f; - - if ((f = fopen(ConfigPath(), "r")) == NULL) - return 0; - DispRead(f, &file); - fclose(f); - if (Load(dpy, &live) != 0) - return 1; - if (DispMerge(&live, &file) == 0) - return 0; - return ApplyX(dpy, &live) == 0 ? 0 : 1; + return McdeDispApplyFile(dpy) < 0 ? 1 : 0; } /* ---- the drawing ---- */ @@ -377,12 +88,12 @@ Place(int i, int *x, int *y, int *w, int *h) { int dw, dh, bw, bh, j, off = 0; - DispSize(&live.d[i], &dw, &dh); + McdeDispSize(&live.d[i], &dw, &dh); if (live.d[i].on) { *x = offX + (int) (live.d[i].x * scale); *y = offY + (int) (live.d[i].y * scale); } else { - DispBounds(&live, &bw, &bh); + McdeDispBounds(&live, &bw, &bh); for (j = 0; j < i; j++) if (!live.d[j].on) off += (int) (live.d[j].w * scale) + 12; @@ -405,7 +116,7 @@ Layout(void) double sx, sy; AreaSize(&aw, &ah); - DispBounds(&live, &bw, &bh); + McdeDispBounds(&live, &bw, &bh); for (i = 0; i < live.n; i++) if (!live.d[i].on && live.d[i].connected) extra += live.d[i].w + 120; @@ -439,7 +150,7 @@ Draw(void) rt = PaneFont(area, 9, False); XClearWindow(dpy, win); Layout(); - p = DispPrimary(&live); + p = McdeDispPrimary(&live); for (i = 0; i < live.n; i++) { int x, y, w, h; XmString name; @@ -574,7 +285,7 @@ Mouse(Widget w, XtPointer data, XEvent *ev, Boolean *cont) case ButtonRelease: if (dragging < 0) return; - DispDock(&live, dragging); + McdeDispDock(&live, dragging); dragging = -1; Changed(); break; @@ -586,7 +297,7 @@ Mouse(Widget w, XtPointer data, XEvent *ev, Boolean *cont) static void ResolutionChosen(Widget w, XtPointer data, XtPointer call) { - Disp *d = &live.d[selected]; + McdeDisp *d = &live.d[selected]; int m = (int) (long) data; (void) w; @@ -600,7 +311,7 @@ ResolutionChosen(Widget w, XtPointer data, XtPointer call) if (!live.d[i].on) continue; - k = DispFindMode(&live.d[i], d->modes[m].w, + k = McdeDispFindMode(&live.d[i], d->modes[m].w, d->modes[m].h, d->modes[m].rate); if (k < 0) continue; @@ -612,7 +323,7 @@ ResolutionChosen(Widget w, XtPointer data, XtPointer call) d->w = d->modes[m].w; d->h = d->modes[m].h; d->rate = d->modes[m].rate; - DispDock(&live, selected); + McdeDispDock(&live, selected); } Changed(); } @@ -624,7 +335,7 @@ RotationChosen(Widget w, XtPointer data, XtPointer call) (void) call; live.d[selected].rot = (int) (long) data; if (!live.mirror) - DispDock(&live, selected); + McdeDispDock(&live, selected); Changed(); } @@ -632,7 +343,7 @@ RotationChosen(Widget w, XtPointer data, XtPointer call) static void MakeResolutionMenu(void) { - Disp *d = &live.d[selected]; + McdeDisp *d = &live.d[selected]; Widget pulldown, history = NULL; Arg a[3]; XmString label; @@ -684,20 +395,20 @@ MakeResolutionMenu(void) static void ShowSelected(void) { - Disp *d = &live.d[selected]; + McdeDisp *d = &live.d[selected]; char text[96]; int r; snprintf(text, sizeof text, "Display: %s", d->name); SetText(nameLabel, text); XmToggleButtonSetState(onToggle, d->on, False); - XmToggleButtonSetState(mainToggle, selected == DispPrimary(&live), + XmToggleButtonSetState(mainToggle, selected == McdeDispPrimary(&live), False); XtSetSensitive(mainToggle, d->on); MakeResolutionMenu(); for (r = 0; r < 4; r++) - XtSetSensitive(rotItem[r], (rotMask[selected] & - RotValue(r)) != 0 && !live.mirror); + XtSetSensitive(rotItem[r], (live.d[selected].rotations & + (1 << r)) != 0 && !live.mirror); XtVaSetValues(rotMenu, XmNmenuHistory, rotItem[d->rot], NULL); XtSetSensitive(rotMenu, d->on && !live.mirror); } @@ -705,7 +416,7 @@ ShowSelected(void) static void OnChanged(Widget w, XtPointer data, XtPointer call) { - Disp *d = &live.d[selected]; + McdeDisp *d = &live.d[selected]; int i, on = 0, bw, bh; (void) data; @@ -718,18 +429,18 @@ OnChanged(Widget w, XtPointer data, XtPointer call) return; } if (!d->on) { - DispBounds(&live, &bw, &bh); + McdeDispBounds(&live, &bw, &bh); d->on = 1; d->x = bw; d->y = 0; - if (live.mirror && DispMirror(&live) != 0) + if (live.mirror && McdeDispMirror(&live) != 0) live.mirror = 0; if (!live.mirror) - DispDock(&live, selected); + McdeDispDock(&live, selected); } else { d->on = 0; d->primary = 0; - DispNormalize(&live); + McdeDispNormalize(&live); } ShowSelected(); Changed(); @@ -760,7 +471,7 @@ MirrorChanged(Widget w, XtPointer data, XtPointer call) (void) data; (void) call; if (XmToggleButtonGetState(w)) { - if (DispMirror(&live) != 0) { + if (McdeDispMirror(&live) != 0) { XmToggleButtonSetState(w, False, False); Message(w, "These displays have no resolution in " "common.", 1); @@ -776,7 +487,7 @@ MirrorChanged(Widget w, XtPointer data, XtPointer call) continue; live.d[i].x = x; live.d[i].y = 0; - DispSize(&live.d[i], &dw, &dh); + McdeDispSize(&live.d[i], &dw, &dh); x += dw; } } @@ -786,36 +497,14 @@ MirrorChanged(Widget w, XtPointer data, XtPointer call) /* ---- Apply, and keep or go back ---- */ -static void -Save(void) -{ - char dir[1024]; - const char *home = getenv("HOME"); - static DispSet remembered; /* large: not on the stack */ - FILE *f; - - /* the monitors that are not plugged in stay in the file */ - memset(&remembered, 0, sizeof remembered); - if ((f = fopen(ConfigPath(), "r")) != NULL) { - DispRead(f, &remembered); - fclose(f); - } - snprintf(dir, sizeof dir, "%s/.dt", home ? home : "."); - (void) mkdir(dir, 0755); - if ((f = fopen(ConfigPath(), "w")) == NULL) - return; - DispWrite(f, &live, &remembered); - fclose(f); -} - static void GoBack(void) { if (tick != 0) XtRemoveTimeOut(tick); tick = 0; - (void) ApplyX(XtDisplay(area), &before); - (void) Load(XtDisplay(area), &live); + (void) McdeDispApply(XtDisplay(area), &before); + (void) McdeDispLoad(XtDisplay(area), &live); if (selected >= live.n) selected = 0; ShowSelected(); @@ -861,7 +550,7 @@ Keep(Widget w, XtPointer data, XtPointer call) if (tick != 0) XtRemoveTimeOut(tick); tick = 0; - Save(); + McdeDispSave(&live); XtSetSensitive(applyButton, False); } @@ -881,10 +570,10 @@ Apply(Widget w, XtPointer data, XtPointer call) (void) data; (void) call; - if (Load(dpy, &before) != 0) + if (McdeDispLoad(dpy, &before) != 0) return; - if (ApplyX(dpy, &live) != 0) { - (void) ApplyX(dpy, &before); + if (McdeDispApply(dpy, &live) != 0) { + (void) McdeDispApply(dpy, &before); Message(w, "The displays cannot be set this way; " "they stay as they were.", 1); GoBack(); @@ -923,7 +612,7 @@ Destroyed(Widget w, XtPointer data, XtPointer call) if (tick != 0) { XtRemoveTimeOut(tick); tick = 0; - (void) ApplyX(XtDisplay(area), &before); + (void) McdeDispApply(XtDisplay(area), &before); } area = NULL; resMenu = NULL; @@ -944,12 +633,12 @@ MakeDisplays(Widget parent) int r; XtAddCallback(box, XmNdestroyCallback, Destroyed, NULL); - if (Load(XtDisplay(parent), &live) != 0 || live.n == 0) { + if (McdeDispLoad(XtDisplay(parent), &live) != 0 || live.n == 0) { (void) Text(box, "none", "This X server cannot change its " "displays (it has no RandR 1.3)."); return box; } - selected = DispPrimary(&live) >= 0 ? DispPrimary(&live) : 0; + selected = McdeDispPrimary(&live) >= 0 ? McdeDispPrimary(&live) : 0; (void) Text(box, "hint", "Drag the displays to arrange them. The " "main display, with the bar along its top,\ncarries the top bar " "and the front panel; the others show the backdrop.\n" diff --git a/programs/dtwm/Makefile.am b/programs/dtwm/Makefile.am index 80249d5d..244ebb5b 100644 --- a/programs/dtwm/Makefile.am +++ b/programs/dtwm/Makefile.am @@ -12,7 +12,8 @@ AM_CPPFLAGS = -DLARGECURSORS -DR2_COMPAT -DOPAQUE -DSHAPE -DUNMAP_ON_RESTART \ -DBATCH_DRAG_REQUESTS -DCDE_INSTALLATION_TOP=\"$(CDE_INSTALLATION_TOP)\" \ -DCDE_CONFIGURATION_TOP=\"$(CDE_CONFIGURATION_TOP)\" -dtwm_LDADD = $(LIBMCDESYS) $(DTCLIENTLIBS) $(XTOOLLIB) +# Xrandr: monitors plugged in and out (WmMcdeDisplays.c) +dtwm_LDADD = $(LIBMCDESYS) $(DTCLIENTLIBS) $(XRANDRLIB) $(XTOOLLIB) dtfplist_LDADD = $(DTCLIENTLIBS) $(TIRPCLIB) $(XTOOLLIB) if SOLARIS