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>
370 lines
8.9 KiB
C
370 lines
8.9 KiB
C
/*
|
|
* WmMcdeDisplays.c -- MCDE: monitors plugged in and out (plug and play)
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* 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 */
|