Files
mcde/programs/dtwm/WmMcdeDisplays.c
T
lotherkandClaude Opus 5.5 9fd4818051 MCDE: the review's open points -- sound through McdeSys, dtwm plug and play, the rest
From the review of 2026-11-12's commits (the points left open in
1b524eb94):

Sound behind McdeSys (ext/src/mcde/AGENTS.md, philosophy 3): libMcdeSound
named /usr/bin/paplay, aplay, /usr/pkg/bin/audioplay and ffplay itself and
waited for the player, so mcdesoundd heard nothing while a sound played.
mcde_sys_sound_play (sound.c) takes paplay where a PulseAudio server
answers, else the backend's player (mcde_sys_player: audioplay(1) on
NetBSD, aplay(1) on Linux), started in a grandchild: no wait, no zombie.
Measured against a private PulseAudio with a null sink: OpenFolder and
TerminalBell sent back to back play at the same time (pactl list short
sink-inputs: two streams), mcdesoundd -selftest PASS, a second mcdesoundd
says "a server runs already".  The /usr/pkg/dt fallbacks of Sound.c and
dtphoto.c are gone (CDE_INSTALLATION_TOP is the prefix);
McdeSoundFile's dead buffer is gone; mcdesoundd ends the 20 bytes of a
message itself; Sound.h is installed (include/Makefile.am, PLIST).

Linking: LIBMCDESOUND and LIBTERM carry the libraries below them
(configure.ac).  Debian's libtool hands no indirect library to the
linker, which then took the installed, older libMcdeSys through
libMcdeSound's RUNPATH and failed (dtterm: undefined
mcde_sys_sound_play).

dtwm plug and play (WmMcdeDisplays.c, written 2026-11-11, uncommitted and
not compiling): the ClientData fields it uses, McdeHeadsChanged (Xinerama's
list read anew, its XineramaQueryScreens memory given back with XFree),
McdePanelMonitorsChanged (the panel centres on the main display), the
RandR events taken in WmMain.c's loop, McdeDisplaysInit after the EWMH
setup, the file in Makefile.am.  Measured on Xvfb: dtwm starts with it,
the check at login leaves the outputs as they were (xrandr before and
after: screen connected 1024x700+0+0), dtwm runs on.  Plugging itself was
not measured: Xvfb cannot change its outputs, and Xorg's dummy driver
refuses a user who is not at the console.

Smaller: dttoolbox ignores SIGCHLD from the start (set after the first
fork it missed the first child) and lost its test hook
/tmp/dttoolbox-no-home; dtfile plays OpenFolder once per folder
(FileMgr.c), not twice; dtpaint's unused variable; dttoolbox frees
XGetAtomName's string without a cast-away const.  AGENTS.md describes
McdeSound, mcdesoundd, dtphoto, dtpaint, dttoolbox and the plug and play;
the WAV tones' maker was not kept, which it says.  The two stale citations
(53b852bb2, 945da0f4c) are corrected in git notes.

Built with make mcde-host, exit 0 (build/logs/mcde-host-2026-11-13q.log).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 06:41:53 +00:00

370 lines
8.9 KiB
C

/*
* WmMcdeDisplays.c -- MCDE: monitors plugged in and out (plug and play)
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* Konrad, 2026-11-11: "wenn display an, direkt anschalten und benutzen.
* wenn display ab alle fenster auf main sammeln. wenn display wieder
* dran, desktop wie vorher wiederherstellen."
*
* dtwm listens for RandR's output and screen changes on the root windows.
*
* An output change (a monitor plugged in or out; gathered for half a
* second, a plug bounces): a monitor that is connected and off is switched
* on -- as ~/.dt/displays remembers it (include/Mcde/Displays.h: the
* monitor by its EDID id, whatever socket it is in), else right of the
* others in the mode it prefers; one the user switched off in System
* Settings stays off. An output that is on with nothing plugged in is
* switched off. McdeDispApply sets it as xrandr(1) would.
*
* A screen change (that, or System Settings > Displays): before, every
* window was on a monitor at a place; each window keeps that monitor's id
* and its place on it. A window whose monitor is gone goes to the main
* display at the same place (clamped into it) and stays marked; when its
* monitor comes back it goes back there. Maximized and full-screen
* windows are made so again on the monitor they are on now. The front
* panel follows the main display (WmMcdePanel.c, Center); the top bar
* (dtbar) moves itself.
*
* Nothing runs while nothing is plugged: no polling, events only.
*/
#include <cde_config.h>
#include <stdlib.h>
#include <string.h>
#include "WmGlobal.h"
#include "WmCDInfo.h"
#include "WmEvent.h"
#include "WmFunction.h"
#include "WmMultiHead.h"
#include "WmWinConf.h"
#include "WmWinState.h"
#include "WmMcdeDisplays.h"
#include "WmMcdePanel.h"
#include "WmMcdeSnap.h"
#include <Mcde/Displays.h>
#ifdef HAVE_XRANDR
#include <X11/extensions/Xrandr.h>
#define HOTPLUG_DELAY 500 /* ms: a plug bounces */
#define REARRANGE_DELAY 200 /* ms: one screen change brings several */
typedef struct {
char id[48];
int x, y, w, h;
} McdeMon;
static McdeMon mons[MCDE_DISP_MAX];
static int nmons, mainMon = -1;
static int rrEventBase = -1;
static XtIntervalId hotplugTimer, rearrangeTimer;
static McdeDispSet live, file; /* large: not on the stack */
/* the monitors that are on now, and which is the main one */
static void
Snapshot(void)
{
int i, p;
nmons = 0;
mainMon = -1;
if (McdeDispLoad(DISPLAY, &live) != 0)
return;
p = McdeDispPrimary(&live);
for (i = 0; i < live.n && nmons < MCDE_DISP_MAX; i++) {
McdeDisp *d = &live.d[i];
if (!d->on)
continue;
if (i == p)
mainMon = nmons;
snprintf(mons[nmons].id, sizeof mons[nmons].id, "%s", d->id);
mons[nmons].x = d->x;
mons[nmons].y = d->y;
McdeDispSize(d, &mons[nmons].w, &mons[nmons].h);
nmons++;
}
if (mainMon < 0 && nmons > 0)
mainMon = 0;
}
Boolean
McdeMainMonitor(int *x, int *y, int *w, int *h)
{
if (mainMon < 0)
return False;
*x = mons[mainMon].x;
*y = mons[mainMon].y;
*w = mons[mainMon].w;
*h = mons[mainMon].h;
return True;
}
/* ---- plugged in or out: switch it on or off ---- */
static const McdeDisp *
Remembered(const char *id)
{
int i;
for (i = 0; i < file.n; i++)
if (strcmp(file.d[i].id, id) == 0)
return &file.d[i];
return NULL;
}
static void
Hotplug(XtPointer data, XtIntervalId *id)
{
FILE *f;
int i, j, changed = 0, bw, bh;
(void) data;
(void) id;
hotplugTimer = 0;
if (McdeDispLoad(DISPLAY, &live) != 0)
return;
memset(&file, 0, sizeof file);
if ((f = fopen(McdeDispConfigPath(), "r")) != NULL) {
McdeDispRead(f, &file);
fclose(f);
}
/* unplugged first: they leave room */
for (i = 0; i < live.n; i++)
if (!live.d[i].connected && live.d[i].on) {
live.d[i].on = 0;
live.d[i].primary = 0;
changed = 1;
}
for (i = 0; i < live.n; i++) {
McdeDisp *d = &live.d[i];
const McdeDisp *r;
int m = -1;
if (!d->connected || d->on || d->nmodes == 0)
continue;
r = Remembered(d->id);
if (r != NULL && !r->on)
continue; /* switched off on purpose */
if (r != NULL)
m = McdeDispFindMode(d, r->w, r->h, r->rate);
if (m < 0)
m = 0; /* the one the monitor prefers */
d->w = d->modes[m].w;
d->h = d->modes[m].h;
d->rate = d->modes[m].rate;
d->on = 1;
if (r != NULL && McdeDispFindMode(d, r->w, r->h, r->rate) >= 0) {
d->x = r->x;
d->y = r->y;
d->rot = r->rot;
if (r->primary) {
for (j = 0; j < live.n; j++)
live.d[j].primary = 0;
d->primary = 1;
}
} else {
/* new: right of the others, docked */
d->on = 0;
McdeDispBounds(&live, &bw, &bh);
d->on = 1;
d->x = bw;
d->y = 0;
d->rot = MCDE_ROT_NORMAL;
}
McdeDispDock(&live, i);
changed = 1;
}
if (!changed)
return;
j = McdeDispPrimary(&live);
for (i = 0; i < live.n; i++)
live.d[i].primary = i == j;
McdeDispNormalize(&live);
(void) McdeDispApply(DISPLAY, &live);
}
/* ---- the screen changed: windows to their monitors ---- */
static int
FindMonitor(const McdeMon *list, int n, const char *id)
{
int i;
for (i = 0; i < n; i++)
if (strcmp(list[i].id, id) == 0)
return i;
return -1;
}
static int
MonitorAt(const McdeMon *list, int n, int x, int y)
{
int i;
for (i = 0; i < n; i++)
if (x >= list[i].x && y >= list[i].y &&
x < list[i].x + list[i].w && y < list[i].y + list[i].h)
return i;
return -1;
}
static int
Clamp(int v, int lo, int hi)
{
return hi < lo ? lo : v < lo ? lo : v > hi ? hi : v;
}
static void
MoveClient(ClientData *pCD, int fx, int fy)
{
int state = pCD->clientState;
Boolean full = pCD->fullscreen;
if (full)
McdeToggleFullscreen(pCD);
if (state == MAXIMIZED_STATE)
SetClientState(pCD, NORMAL_STATE, GetTimestamp());
ProcessNewConfiguration(pCD, fx, fy, (unsigned int) pCD->clientWidth,
(unsigned int) pCD->clientHeight, False);
/* made so again on the monitor it is on now (GetHeadInfo) */
if (state == MAXIMIZED_STATE)
SetClientState(pCD, MAXIMIZED_STATE, GetTimestamp());
if (full)
McdeToggleFullscreen(pCD);
}
static void
Rearrange(XtPointer data, XtIntervalId *id)
{
McdeMon old[MCDE_DISP_MAX];
int oldN = nmons, oldMain = mainMon, s;
(void) data;
(void) id;
rearrangeTimer = 0;
memcpy(old, mons, sizeof old);
Snapshot();
McdeHeadsChanged();
if (nmons == 0)
return;
for (s = 0; s < wmGD.numScreens; s++) {
WmScreenData *pSD = &wmGD.Screens[s];
ClientListEntry *e;
if (!pSD->managed)
continue;
for (e = pSD->clientList; e != NULL; e = e->nextSibling) {
ClientData *pCD = e->pCD;
int fx, fy, fw, fh, m, nx, ny;
if (e->type != NORMAL_STATE || pCD == NULL ||
(pCD->dtwmBehaviors & (DtWM_BEHAVIOR_PANEL |
DtWM_BEHAVIOR_SUBPANEL)) ||
pCD->clientState == WITHDRAWN_STATE)
continue;
fx = pCD->clientX - pCD->clientOffset.x;
fy = pCD->clientY - pCD->clientOffset.y;
fw = pCD->clientWidth + 2 * pCD->clientOffset.x;
fh = pCD->clientHeight + pCD->clientOffset.y +
pCD->clientOffset.x;
/* where it was, unless it waits for its monitor */
if (!pCD->mcdeRescued) {
m = MonitorAt(old, oldN, fx + fw / 2, fy + fh / 2);
if (m < 0)
m = MonitorAt(old, oldN, fx, fy);
if (m < 0)
m = oldMain;
if (m < 0)
continue;
snprintf(pCD->mcdeMonitor, sizeof pCD->mcdeMonitor,
"%s", old[m].id);
pCD->mcdeMonX = fx - old[m].x;
pCD->mcdeMonY = fy - old[m].y;
}
m = FindMonitor(mons, nmons, pCD->mcdeMonitor);
pCD->mcdeRescued = m < 0;
if (m < 0)
m = mainMon;
nx = mons[m].x + Clamp(pCD->mcdeMonX, 0, mons[m].w - fw);
ny = mons[m].y + Clamp(pCD->mcdeMonY, 0, mons[m].h - fh);
if (nx != fx || ny != fy)
MoveClient(pCD, nx, ny);
}
}
McdePanelMonitorsChanged();
}
void
McdeDisplaysInit(void)
{
int ev, err, s;
if (!XRRQueryExtension(DISPLAY, &ev, &err))
return;
rrEventBase = ev;
for (s = 0; s < wmGD.numScreens; s++)
if (wmGD.Screens[s].managed)
XRRSelectInput(DISPLAY, wmGD.Screens[s].rootWindow,
RRScreenChangeNotifyMask | RROutputChangeNotifyMask);
Snapshot();
/* what was plugged in while nobody listened (before the login) */
hotplugTimer = XtAppAddTimeOut(wmGD.mwmAppContext, HOTPLUG_DELAY,
Hotplug, NULL);
}
Boolean
McdeDisplaysEvent(XEvent *event)
{
if (rrEventBase < 0)
return False;
if (event->type == rrEventBase + RRScreenChangeNotify) {
XRRUpdateConfiguration(event);
if (rearrangeTimer != 0)
XtRemoveTimeOut(rearrangeTimer);
rearrangeTimer = XtAppAddTimeOut(wmGD.mwmAppContext,
REARRANGE_DELAY, Rearrange, NULL);
return True;
}
if (event->type == rrEventBase + RRNotify) {
if (((XRRNotifyEvent *) event)->subtype == RRNotify_OutputChange) {
if (hotplugTimer != 0)
XtRemoveTimeOut(hotplugTimer);
hotplugTimer = XtAppAddTimeOut(wmGD.mwmAppContext,
HOTPLUG_DELAY, Hotplug, NULL);
}
return True;
}
return False;
}
#else /* !HAVE_XRANDR */
void
McdeDisplaysInit(void)
{
}
Boolean
McdeDisplaysEvent(XEvent *event)
{
(void) event;
return False;
}
Boolean
McdeMainMonitor(int *x, int *y, int *w, int *h)
{
(void) x;
(void) y;
(void) w;
(void) h;
return False;
}
#endif /* HAVE_XRANDR */