/* * displays.c -- dtsystemsettings: the Displays pane * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * The monitors as rectangles to drag into place, and for the chosen one: * on or off, resolution, rotation, main display; all of them mirrored. * Through RandR 1.3 (libXrandr), as xrandr(1) does it: the CRTCs that * change are switched off, the screen gets its new size, then they are set. * * Konrad, 2026-11-11: the main display carries the front panel and the top * bar; the others show the backdrop only, the same one; the workspaces are * the same on all of them. So there is nothing to choose per display but * where it is and how it shows. * * "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 (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. */ #include #include #include #include #include #include #include #include #include #include #include #ifdef HAVE_XRANDR #include #endif #include #include "ui.h" #define CONFIRM_SECONDS 15 #ifdef HAVE_XRANDR 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; static Widget resRow, resMenu, rotMenu, applyButton; static Widget rotItem[4]; static Widget confirm; static XtIntervalId tick; static int secondsLeft; /* the dragged display and where in it the pointer took it */ static int dragging = -1, grabDx, grabDy; /* the screen -> area scale and offset of the last drawing */ static double scale = 0.1; static int offX, offY; static void Draw(void); static void ShowSelected(void); /* dtsystemsettings -apply-displays: ~/.dt/displays on what is plugged in */ int ApplyDisplaysCommand(Display *dpy) { return McdeDispApplyFile(dpy) < 0 ? 1 : 0; } /* ---- the drawing ---- */ static void AreaSize(Dimension *w, Dimension *h) { XtVaGetValues(area, XmNwidth, w, XmNheight, h, NULL); } /* where display I is drawn (an off display right of the others) */ static void Place(int i, int *x, int *y, int *w, int *h) { int dw, dh, bw, bh, j, off = 0; 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 { McdeDispBounds(&live, &bw, &bh); for (j = 0; j < i; j++) if (!live.d[j].on) off += (int) (live.d[j].w * scale) + 12; *x = offX + (int) (bw * scale) + 24 + off; *y = offY; } *w = (int) (dw * scale); *h = (int) (dh * scale); if (*w < 8) *w = 8; if (*h < 8) *h = 8; } static void Layout(void) { Dimension aw, ah; int bw, bh, extra = 0, i; double sx, sy; AreaSize(&aw, &ah); 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; if (bw + extra <= 0 || bh <= 0) return; sx = (aw - 24) / (double) (bw + extra); sy = (ah - 24) / (double) bh; scale = sx < sy ? sx : sy; offX = ((int) aw - (int) ((bw + extra) * scale)) / 2; offY = ((int) ah - (int) (bh * scale)) / 2; } static void Draw(void) { Display *dpy; Window win; Pixel bg, fg, top, bottom, sel; Colormap cmap; GC gc; XmRenderTable rt; int i, p; if (area == NULL || !XtIsRealized(area)) return; dpy = XtDisplay(area); win = XtWindow(area); XtVaGetValues(area, XmNbackground, &bg, XmNcolormap, &cmap, NULL); XmGetColors(XtScreen(area), cmap, bg, &fg, &top, &bottom, &sel); gc = XCreateGC(dpy, win, 0, NULL); rt = PaneFont(area, 9, False); XClearWindow(dpy, win); Layout(); p = McdeDispPrimary(&live); for (i = 0; i < live.n; i++) { int x, y, w, h; XmString name; Dimension tw, th; if (!live.d[i].connected) continue; Place(i, &x, &y, &w, &h); XSetForeground(dpy, gc, i == selected ? sel : top); if (live.d[i].on) XFillRectangle(dpy, win, gc, x, y, (unsigned) w, (unsigned) h); XSetForeground(dpy, gc, fg); XSetLineAttributes(dpy, gc, i == selected ? 2 : 1, live.d[i].on ? LineSolid : LineOnOffDash, CapButt, JoinMiter); XDrawRectangle(dpy, win, gc, x, y, (unsigned) w, (unsigned) h); /* the main display: the top bar along its upper edge */ if (i == p && live.d[i].on) XFillRectangle(dpy, win, gc, x, y, (unsigned) w, (unsigned) (h / 14 > 3 ? h / 14 : 3)); name = XmStringCreateLocalized(live.d[i].name); XmStringExtent(rt, name, &tw, &th); XmStringDraw(dpy, win, rt, name, gc, (Position) x, (Position) (y + (h - (int) th) / 2), (Dimension) w, XmALIGNMENT_CENTER, XmSTRING_DIRECTION_L_TO_R, NULL); XmStringFree(name); } XmRenderTableFree(rt); XFreeGC(dpy, gc); } static void Changed(void) { XtSetSensitive(applyButton, True); Draw(); } static void Exposed(Widget w, XtPointer data, XtPointer call) { XmDrawingAreaCallbackStruct *cb = (XmDrawingAreaCallbackStruct *) call; (void) w; (void) data; if (cb->event != NULL && cb->event->xexpose.count == 0) Draw(); } static int Hit(int px, int py) { int i, x, y, w, h, found = -1; for (i = 0; i < live.n; i++) { if (!live.d[i].connected) continue; Place(i, &x, &y, &w, &h); if (px >= x && py >= y && px < x + w && py < y + h) found = i; } return found; } static void Mouse(Widget w, XtPointer data, XEvent *ev, Boolean *cont) { (void) w; (void) data; (void) cont; switch (ev->type) { case ButtonPress: if (ev->xbutton.button != Button1) return; { int i = Hit(ev->xbutton.x, ev->xbutton.y), x, y, ww, hh; if (i < 0) return; selected = i; ShowSelected(); if (live.d[i].on && !live.mirror) { Place(i, &x, &y, &ww, &hh); dragging = i; grabDx = ev->xbutton.x - x; grabDy = ev->xbutton.y - y; } Draw(); } break; case MotionNotify: if (dragging < 0) return; live.d[dragging].x = (int) ((ev->xmotion.x - grabDx - offX) / scale); live.d[dragging].y = (int) ((ev->xmotion.y - grabDy - offY) / scale); /* no Layout while dragging: the scale stays put */ { Display *dpy = XtDisplay(area); XEvent next; /* only the last of the queued motions */ while (XCheckTypedWindowEvent(dpy, XtWindow(area), MotionNotify, &next)) live.d[dragging].x = (int) ((next.xmotion.x - grabDx - offX) / scale), live.d[dragging].y = (int) ((next.xmotion.y - grabDy - offY) / scale); } { int i, x, y, ww, hh; Display *dpy = XtDisplay(area); Window win = XtWindow(area); GC gc = XCreateGC(dpy, win, 0, NULL); Pixel bg; XtVaGetValues(area, XmNforeground, &bg, NULL); XClearWindow(dpy, win); XSetForeground(dpy, gc, bg); for (i = 0; i < live.n; i++) { if (!live.d[i].connected || !live.d[i].on) continue; Place(i, &x, &y, &ww, &hh); XDrawRectangle(dpy, win, gc, x, y, (unsigned) ww, (unsigned) hh); } XFreeGC(dpy, gc); } break; case ButtonRelease: if (dragging < 0) return; McdeDispDock(&live, dragging); dragging = -1; Changed(); break; } } /* ---- the controls of the chosen display ---- */ static void ResolutionChosen(Widget w, XtPointer data, XtPointer call) { McdeDisp *d = &live.d[selected]; int m = (int) (long) data; (void) w; (void) call; if (live.mirror) { /* every display the same size */ int i; for (i = 0; i < live.n; i++) { int k; if (!live.d[i].on) continue; k = McdeDispFindMode(&live.d[i], d->modes[m].w, d->modes[m].h, d->modes[m].rate); if (k < 0) continue; live.d[i].w = live.d[i].modes[k].w; live.d[i].h = live.d[i].modes[k].h; live.d[i].rate = live.d[i].modes[k].rate; } } else { d->w = d->modes[m].w; d->h = d->modes[m].h; d->rate = d->modes[m].rate; McdeDispDock(&live, selected); } Changed(); } static void RotationChosen(Widget w, XtPointer data, XtPointer call) { (void) w; (void) call; live.d[selected].rot = (int) (long) data; if (!live.mirror) McdeDispDock(&live, selected); Changed(); } /* the resolutions of the chosen display, as an option menu */ static void MakeResolutionMenu(void) { McdeDisp *d = &live.d[selected]; Widget pulldown, history = NULL; Arg a[3]; XmString label; int m; if (resMenu != NULL) XtDestroyWidget(resMenu); pulldown = XmCreatePulldownMenu(resRow, "resolutions", NULL, 0); for (m = 0; m < d->nmodes; m++) { char text[64]; XmString s; Widget b; int k; /* one entry for a size and rate (the server may list both * variants of a mode) */ for (k = 0; k < m; k++) if (d->modes[k].w == d->modes[m].w && d->modes[k].h == d->modes[m].h && (int) (d->modes[k].rate + .5) == (int) (d->modes[m].rate + .5)) break; if (k < m) continue; snprintf(text, sizeof text, "%d x %d, %.0f Hz", d->modes[m].w, d->modes[m].h, d->modes[m].rate); s = XmStringCreateLocalized(text); b = XtVaCreateManagedWidget("mode", xmPushButtonWidgetClass, pulldown, XmNlabelString, s, NULL); XmStringFree(s); XtAddCallback(b, XmNactivateCallback, ResolutionChosen, (XtPointer) (long) m); if (history == NULL && d->modes[m].w == d->w && d->modes[m].h == d->h && (int) (d->modes[m].rate + .5) == (int) (d->rate + .5)) history = b; } label = XmStringCreateLocalized("Resolution:"); XtSetArg(a[0], XmNsubMenuId, pulldown); XtSetArg(a[1], XmNlabelString, label); resMenu = XmCreateOptionMenu(resRow, "resolution", a, 2); XmStringFree(label); if (history != NULL) XtVaSetValues(resMenu, XmNmenuHistory, history, NULL); XtManageChild(resMenu); XtSetSensitive(resMenu, d->on && d->nmodes > 0); } static void ShowSelected(void) { 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 == McdeDispPrimary(&live), False); XtSetSensitive(mainToggle, d->on); MakeResolutionMenu(); for (r = 0; r < 4; r++) 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); } static void OnChanged(Widget w, XtPointer data, XtPointer call) { McdeDisp *d = &live.d[selected]; int i, on = 0, bw, bh; (void) data; (void) call; for (i = 0; i < live.n; i++) on += live.d[i].on; if (d->on && on == 1) { /* never a computer without a screen */ XmToggleButtonSetState(w, True, False); return; } if (!d->on) { McdeDispBounds(&live, &bw, &bh); d->on = 1; d->x = bw; d->y = 0; if (live.mirror && McdeDispMirror(&live) != 0) live.mirror = 0; if (!live.mirror) McdeDispDock(&live, selected); } else { d->on = 0; d->primary = 0; McdeDispNormalize(&live); } ShowSelected(); Changed(); } static void MainChanged(Widget w, XtPointer data, XtPointer call) { int i; (void) data; (void) call; if (!XmToggleButtonGetState(w)) { /* there is always one: choose another to change it */ XmToggleButtonSetState(w, True, False); return; } for (i = 0; i < live.n; i++) live.d[i].primary = i == selected; Changed(); } static void MirrorChanged(Widget w, XtPointer data, XtPointer call) { int i, x = 0; (void) data; (void) call; if (XmToggleButtonGetState(w)) { if (McdeDispMirror(&live) != 0) { XmToggleButtonSetState(w, False, False); Message(w, "These displays have no resolution in " "common.", 1); return; } } else { /* side by side, in the order of the outputs */ live.mirror = 0; for (i = 0; i < live.n; i++) { int dw, dh; if (!live.d[i].on) continue; live.d[i].x = x; live.d[i].y = 0; McdeDispSize(&live.d[i], &dw, &dh); x += dw; } } ShowSelected(); Changed(); } /* ---- Apply, and keep or go back ---- */ static void GoBack(void) { if (tick != 0) XtRemoveTimeOut(tick); tick = 0; (void) McdeDispApply(XtDisplay(area), &before); (void) McdeDispLoad(XtDisplay(area), &live); if (selected >= live.n) selected = 0; ShowSelected(); XtSetSensitive(applyButton, False); Draw(); } static void ConfirmText(void) { char text[160]; XmString s; snprintf(text, sizeof text, "Keep these display settings?\n\n" "The displays go back to how they were in %d seconds.", secondsLeft); s = XmStringCreateLocalized(text); XtVaSetValues(confirm, XmNmessageString, s, NULL); XmStringFree(s); } static void Tick(XtPointer data, XtIntervalId *id) { (void) data; (void) id; tick = 0; if (--secondsLeft <= 0) { XtUnmanageChild(confirm); GoBack(); return; } ConfirmText(); tick = XtAppAddTimeOut(app, 1000, Tick, NULL); } static void Keep(Widget w, XtPointer data, XtPointer call) { (void) w; (void) data; (void) call; if (tick != 0) XtRemoveTimeOut(tick); tick = 0; McdeDispSave(&live); XtSetSensitive(applyButton, False); } static void Back(Widget w, XtPointer data, XtPointer call) { (void) w; (void) data; (void) call; GoBack(); } static void Apply(Widget w, XtPointer data, XtPointer call) { Display *dpy = XtDisplay(w); (void) data; (void) call; if (McdeDispLoad(dpy, &before) != 0) return; 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(); return; } if (confirm == NULL) { XmString ok = XmStringCreateLocalized("Keep"); XmString back = XmStringCreateLocalized("Go Back"); Arg a[3]; XtSetArg(a[0], XmNokLabelString, ok); XtSetArg(a[1], XmNcancelLabelString, back); XtSetArg(a[2], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL); confirm = XmCreateQuestionDialog(XtParent(area), "keepDisplays", a, 3); XmStringFree(ok); XmStringFree(back); XtUnmanageChild(XmMessageBoxGetChild(confirm, XmDIALOG_HELP_BUTTON)); XtAddCallback(confirm, XmNokCallback, Keep, NULL); XtAddCallback(confirm, XmNcancelCallback, Back, NULL); } secondsLeft = CONFIRM_SECONDS; ConfirmText(); XtManageChild(confirm); tick = XtAppAddTimeOut(app, 1000, Tick, NULL); } static void Destroyed(Widget w, XtPointer data, XtPointer call) { (void) w; (void) data; (void) call; /* the pane goes; a question still open answers itself */ if (tick != 0) { XtRemoveTimeOut(tick); tick = 0; (void) McdeDispApply(XtDisplay(area), &before); } area = NULL; resMenu = NULL; confirm = NULL; } Widget MakeDisplays(Widget parent) { static const char *const rotLabels[4] = { "Normal", "Turned left", "Upside down", "Turned right" }; Widget box = XtVaCreateWidget("displays", xmRowColumnWidgetClass, parent, XmNspacing, 6, NULL); Widget pulldown; XmString label; Arg a[2]; int r; XtAddCallback(box, XmNdestroyCallback, Destroyed, NULL); 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 = 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" "Workspaces change on all displays together."); area = XtVaCreateManagedWidget("arrangement", xmDrawingAreaWidgetClass, box, XmNwidth, 440, XmNheight, 200, XmNresizePolicy, XmRESIZE_NONE, NULL); XtAddCallback(area, XmNexposeCallback, Exposed, NULL); XtAddEventHandler(area, ButtonPressMask | ButtonReleaseMask | Button1MotionMask, False, Mouse, NULL); nameLabel = Text(box, "name", ""); XtVaSetValues(nameLabel, XmNrenderTable, PaneFont(box, 10, True), NULL); onToggle = XtVaCreateManagedWidget("Use this display", xmToggleButtonWidgetClass, box, NULL); XtAddCallback(onToggle, XmNvalueChangedCallback, OnChanged, NULL); mainToggle = XtVaCreateManagedWidget( "Main display (top bar and front panel)", xmToggleButtonWidgetClass, box, NULL); XtAddCallback(mainToggle, XmNvalueChangedCallback, MainChanged, NULL); resRow = XtVaCreateManagedWidget("resolutionRow", xmRowColumnWidgetClass, box, XmNorientation, XmHORIZONTAL, XmNmarginWidth, 0, NULL); pulldown = XmCreatePulldownMenu(box, "rotations", NULL, 0); for (r = 0; r < 4; r++) { XmString s = XmStringCreateLocalized((char *) rotLabels[r]); rotItem[r] = XtVaCreateManagedWidget("rotation", xmPushButtonWidgetClass, pulldown, XmNlabelString, s, NULL); XmStringFree(s); XtAddCallback(rotItem[r], XmNactivateCallback, RotationChosen, (XtPointer) (long) r); } label = XmStringCreateLocalized("Rotation:"); XtSetArg(a[0], XmNsubMenuId, pulldown); XtSetArg(a[1], XmNlabelString, label); rotMenu = XmCreateOptionMenu(box, "rotation", a, 2); XmStringFree(label); XtManageChild(rotMenu); mirrorToggle = XtVaCreateManagedWidget("Mirror displays", xmToggleButtonWidgetClass, box, XmNset, live.mirror, NULL); XtAddCallback(mirrorToggle, XmNvalueChangedCallback, MirrorChanged, NULL); applyButton = PaneButton(box, "Apply", Apply, NULL); XtSetSensitive(applyButton, False); ShowSelected(); return box; } #else /* !HAVE_XRANDR */ int ApplyDisplaysCommand(Display *dpy) { (void) dpy; return 0; } Widget MakeDisplays(Widget parent) { Widget box = XtVaCreateWidget("displays", xmRowColumnWidgetClass, parent, NULL); (void) Text(box, "none", "MCDE was built without RandR (libXrandr): " "the displays cannot be changed here."); return box; } #endif /* HAVE_XRANDR */