Files
lotherkandClaude Sonnet 5.5 dbe6350447 dtwm: F8 shows the workspaces as a grid of small screens with their windows; arrows, digit, Return or click switch (t0183, first part)
WmMcdeSpaces.c: an override-redirect window like the window overview, drawn
once on demand, nothing runs while it is closed.  Reached as f.mcde_app spaces
(a new function name is not bound by a key, as measured for the overview).
Seen on Xvfb: F8 opens it, Right + Return goes to Two, a click on Four goes
there, F8 twice closes, 1 goes back to One; docs/screenshots/leopard/Spaces-mcde.png.

The 'F9 does nothing' of t0058/t0216 was the test sender: xdotool 3.20160805
sends Alt_L before an F-key in this keymap, so the key arrives as Alt+F9.  With
plain XTest keycodes F8 reaches the root grab (ext/src/mcde/AGENTS.md).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 05:45:20 +00:00

341 lines
8.9 KiB
C

/*
* WmMcdeSpaces.c -- MCDE: the overview of the workspaces (F8), without a
* compositor (t0183)
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* F8 opens an override-redirect window over the screen: the workspaces in a
* grid (as square as it can be: 4 are 2 x 2, 6 are 3 x 2), each a small
* screen with its name above it and its windows drawn in it as outlines at
* their places and sizes. The workspace you are in has a white frame, the
* one the keys have chosen a blue one. A click on a workspace, Return, or a
* digit key goes there and closes the view; the arrow keys choose; Escape or
* F8 again closes it. No live pictures, no animation, no dimming: drawn once,
* on demand, and nothing runs while it is closed.
*
* Not yet: the backdrop of each workspace in its miniature, dragging a window
* from one workspace to another, the assignment of programs to workspaces,
* and the settings pane (the rest of t0183).
*
* C99, POSIX, Xt and Motif (ext/src/mcde/AGENTS.md).
*/
#include <stdio.h>
#include <string.h>
#include <X11/Intrinsic.h>
#include <X11/keysym.h>
#include <Xm/Xm.h>
#include "WmGlobal.h"
#include "WmFunction.h"
#include "WmEvent.h"
#include "WmResNames.h"
#include "WmWrkspace.h"
#define SP_MAX 16
#define SP_GAP 32 /* between the miniatures and at the edges */
#define SP_LABEL 18 /* room for a name above a miniature */
static Window spWin = None;
static int spActive;
static int spN, spSel, spCols, spRows;
static int spCellW, spCellH; /* a cell: the miniature and its label */
static int spScrW, spScrH; /* the screen */
/* the text of an XmString whatever tag it has (see WmMcdeOverview.c) */
static char *
TitleText(XmString xs)
{
XmStringContext ctx;
XmStringComponentType t, unknown;
XmStringDirection dir;
char *text = NULL, *tag = NULL;
unsigned char *value = NULL;
unsigned short len = 0;
char buf[256];
buf[0] = '\0';
if (xs == NULL || !XmStringInitContext(&ctx, xs))
return XtNewString("");
while ((t = XmStringGetNextComponent(ctx, &text, &tag, &dir, &unknown,
&len, &value)) != XmSTRING_COMPONENT_END) {
if (t == XmSTRING_COMPONENT_TEXT && text != NULL) {
size_t have = strlen(buf);
(void)snprintf(buf + have, sizeof buf - have, "%s",
text);
}
}
XmStringFreeContext(ctx);
return XtNewString(buf);
}
/* what is drawn of a window: not the panel, not an icon or a hidden one */
static Boolean
AWindow(ClientData *c)
{
return c != NULL && c->clientState != WITHDRAWN_STATE &&
c->clientState != MINIMIZED_STATE &&
!(c->dtwmBehaviors & (DtWM_BEHAVIOR_PANEL |
DtWM_BEHAVIOR_SUBPANEL)) && c->frameInfo.width > 1;
}
/* the miniature of the workspace I: its place in the window, and the scale of
* the screen into it */
static void
Mini(int i, int *x, int *y, int *w, int *h)
{
int col = i % spCols, row = i / spCols;
int bw = spCellW - 2 * SP_GAP / 2, bh = spCellH - SP_LABEL - SP_GAP / 2;
int mw, mh;
if (bw < 20)
bw = 20;
if (bh < 20)
bh = 20;
/* the screen's proportions */
mw = bw;
mh = (int) ((long) mw * spScrH / spScrW);
if (mh > bh) {
mh = bh;
mw = (int) ((long) mh * spScrW / spScrH);
}
*w = mw;
*h = mh;
*x = SP_GAP + col * (spCellW + SP_GAP) + (spCellW - mw) / 2;
*y = SP_GAP + row * (spCellH + SP_GAP) + SP_LABEL;
}
static void
Draw(void)
{
Display *dpy = DISPLAY;
WmScreenData *pSD = ACTIVE_PSD;
GC gc;
int i, k;
if (spWin == None)
return;
gc = XCreateGC(dpy, spWin, 0, NULL);
XSetForeground(dpy, gc, BlackPixel(dpy, pSD->screen));
XFillRectangle(dpy, spWin, gc, 0, 0, (unsigned) spScrW,
(unsigned) spScrH);
for (i = 0; i < spN; i++) {
WmWorkspaceData *pWS = &pSD->pWS[i];
int x, y, w, h;
char *name;
Mini(i, &x, &y, &w, &h);
/* its name, above it */
name = TitleText(pWS->title);
if (name[0] == '\0') {
XtFree(name);
name = XtNewString(pWS->name != NULL ? pWS->name : "");
}
XSetForeground(dpy, gc, WhitePixel(dpy, pSD->screen));
XDrawString(dpy, spWin, gc, x, y - 13, name, (int) strlen(name));
XtFree(name);
/* the miniature: the screen, small */
XSetForeground(dpy, gc, (Pixel) 0x44525e);
XFillRectangle(dpy, spWin, gc, x, y, (unsigned) w, (unsigned) h);
/* its windows, outlines at their places */
for (k = 0; k < (int) pWS->numClients; k++) {
ClientData *c = pWS->ppClients[k];
int wx, wy, ww, wh;
if (!AWindow(c))
continue;
wx = x + (int) ((long) c->frameInfo.x * w / spScrW);
wy = y + (int) ((long) c->frameInfo.y * h / spScrH);
ww = (int) ((long) c->frameInfo.width * w / spScrW);
wh = (int) ((long) c->frameInfo.height * h / spScrH);
if (wx < x)
wx = x;
if (wy < y)
wy = y;
if (ww < 4)
ww = 4;
if (wh < 4)
wh = 4;
if (wx + ww > x + w)
ww = x + w - wx;
if (wy + wh > y + h)
wh = y + h - wy;
if (ww < 2 || wh < 2)
continue;
XSetForeground(dpy, gc, (Pixel) 0xc8c8c8);
XFillRectangle(dpy, spWin, gc, wx, wy, (unsigned) ww,
(unsigned) wh);
XSetForeground(dpy, gc, BlackPixel(dpy, pSD->screen));
XDrawRectangle(dpy, spWin, gc, wx, wy,
(unsigned) (ww - 1), (unsigned) (wh - 1));
}
/* the frame: white for the workspace we are in, blue for the
* one the keys have chosen (both, if it is the same) */
if (pWS == pSD->pActiveWS) {
XSetForeground(dpy, gc, WhitePixel(dpy, pSD->screen));
XSetLineAttributes(dpy, gc, 3, LineSolid, CapButt,
JoinMiter);
XDrawRectangle(dpy, spWin, gc, x - 3, y - 3,
(unsigned) (w + 5), (unsigned) (h + 5));
}
if (i == spSel) {
XSetForeground(dpy, gc, (Pixel) 0x5599ff);
XSetLineAttributes(dpy, gc, 3, LineSolid, CapButt,
JoinMiter);
XDrawRectangle(dpy, spWin, gc, x - 8, y - 8,
(unsigned) (w + 15), (unsigned) (h + 15));
}
XSetLineAttributes(dpy, gc, 0, LineSolid, CapButt, JoinMiter);
}
XFreeGC(dpy, gc);
XFlush(dpy);
}
static void
Close(void)
{
if (spWin == None)
return;
XUngrabKeyboard(DISPLAY, CurrentTime);
XUngrabPointer(DISPLAY, CurrentTime);
XDestroyWindow(DISPLAY, spWin);
spWin = None;
spActive = 0;
}
/* go to workspace I and close the view */
static void
Go(int i)
{
WmScreenData *pSD = ACTIVE_PSD;
Close();
if (i >= 0 && i < spN && &pSD->pWS[i] != pSD->pActiveWS)
ChangeToWorkspace(&pSD->pWS[i]);
}
/* the workspace under the pointer, or -1 */
static int
At(int px, int py)
{
int i, x, y, w, h;
for (i = 0; i < spN; i++) {
Mini(i, &x, &y, &w, &h);
if (px >= x && px < x + w && py >= y && py < y + h)
return i;
}
return -1;
}
Boolean
McdeSpacesEvent(XEvent *event)
{
int i;
if (!spActive || spWin == None || event->xany.window != spWin)
return False;
switch (event->type) {
case Expose:
if (event->xexpose.count == 0)
Draw();
return True;
case KeyPress: {
KeySym key = XLookupKeysym(&event->xkey, 0);
if (key == XK_Escape || key == XK_F8) {
Close();
} else if (key == XK_Return || key == XK_KP_Enter) {
Go(spSel);
} else if (key == XK_Left) {
if (spSel > 0)
spSel--;
Draw();
} else if (key == XK_Right) {
if (spSel < spN - 1)
spSel++;
Draw();
} else if (key == XK_Up) {
if (spSel - spCols >= 0)
spSel -= spCols;
Draw();
} else if (key == XK_Down) {
if (spSel + spCols < spN)
spSel += spCols;
Draw();
} else if (key >= XK_1 && key <= XK_9) {
Go((int) (key - XK_1));
}
return True;
}
case MotionNotify:
case ButtonPress:
i = At(event->xbutton.x, event->xbutton.y);
if (i >= 0) {
if (event->type == ButtonPress)
Go(i);
else if (i != spSel) {
spSel = i;
Draw();
}
}
return True;
default:
return True; /* nothing else reaches the view */
}
}
/* f.mcde_app spaces: the overview of the workspaces (F8) */
Boolean
F_McdeSpaces(String args, ClientData *pCD, XEvent *event)
{
Display *dpy = DISPLAY;
WmScreenData *pSD = ACTIVE_PSD;
XSetWindowAttributes a;
int i;
(void) args;
(void) pCD;
(void) event;
if (spActive) {
Close();
return True;
}
spN = pSD->numWorkspaces;
if (spN > SP_MAX)
spN = SP_MAX;
if (spN < 1)
return True;
spScrW = DisplayWidth(dpy, pSD->screen);
spScrH = DisplayHeight(dpy, pSD->screen);
spCols = 1;
while (spCols * spCols < spN)
spCols++;
spRows = (spN + spCols - 1) / spCols;
spCellW = (spScrW - (spCols + 1) * SP_GAP) / spCols;
spCellH = (spScrH - (spRows + 1) * SP_GAP) / spRows;
spSel = 0;
for (i = 0; i < spN; i++)
if (&pSD->pWS[i] == pSD->pActiveWS)
spSel = i;
memset(&a, 0, sizeof a);
a.override_redirect = True;
a.background_pixel = BlackPixel(dpy, pSD->screen);
a.event_mask = ExposureMask | KeyPressMask | ButtonPressMask |
PointerMotionMask;
spWin = XCreateWindow(dpy, pSD->rootWindow, 0, 0, (unsigned) spScrW,
(unsigned) spScrH, 0, CopyFromParent, InputOutput, CopyFromParent,
CWOverrideRedirect | CWBackPixel | CWEventMask, &a);
XMapRaised(dpy, spWin);
XGrabKeyboard(dpy, spWin, False, GrabModeAsync, GrabModeAsync,
CurrentTime);
XGrabPointer(dpy, spWin, False, ButtonPressMask | PointerMotionMask,
GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
spActive = 1;
XSync(dpy, False);
Draw();
return True;
}