Files
mcde/programs/dtbar/forcequit.c
T
lotherkandClaude Opus 5.5 2c7ca200b0 mcde: dtbar's M menu: About This Computer, Favourites, Recent Items, Force Quit, power
The M menu has About This Computer (logo, system and version, processor,
memory, startup disk, More Info with machine, kernel, host name, disk space
and the desktop's version, all from mcde_sys_info), Software Update (greyed
until the update exists), System Settings, Favourites (~/.dt/favourites, with
Edit Favourites...) and Recent Items (~/.dt/recent, written when an item is
opened, with Clear Menu) -- items.c with its test (PASS test_items) --, Force
Quit (the programs with a window, XKillClient), Sleep, Restart..., Shut
Down... (McdeSys, the last two ask first), Lock Screen and Log Out <name>.
The bar's dialogs are centred on the screen (Motif centred them on the bar).
Tested on Xvfb: About, a favourite started and listed under Recent Items,
Force Quit ended dtcalc, the Shut Down question shows (not confirmed: on the
development host it could shut the computer down).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 08:03:48 +00:00

165 lines
4.2 KiB
C

/*
* forcequit.c -- dtbar: Force Quit Applications
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* The programs with a window on the screen, by name; "Force Quit" ends the
* chosen one the X way, XKillClient(3): its connection is closed and the
* program stops, however it hangs. The windows are found as xprop(1) and
* xkill(1) find them: the top-level windows of the root, and in each the
* client window that carries WM_STATE (XmuClientWindow).
*
* C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md).
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <string.h>
#include <X11/Intrinsic.h>
#include <X11/Shell.h>
#include <X11/Xutil.h>
#include <X11/Xmu/WinUtil.h>
#include <Xm/Xm.h>
#include <Xm/List.h>
#include <Xm/MessageB.h>
#include "dtbar.h"
#define FQ_MAX 128
static Widget fqDialog, fqList;
static Window fqWindows[FQ_MAX];
static int fqCount;
/* the name a program goes by: its class (WM_CLASS), else its title */
static int
NameOf(Display *dpy, Window w, char *buf, size_t len)
{
XClassHint ch;
char *title = NULL;
if (XGetClassHint(dpy, w, &ch)) {
/* the bar and its own windows are not for quitting */
if (strcmp(ch.res_class, "Dtbar") == 0) {
XFree(ch.res_name);
XFree(ch.res_class);
return 0;
}
snprintf(buf, len, "%s", ch.res_class);
XFree(ch.res_name);
XFree(ch.res_class);
if (XFetchName(dpy, w, &title) && title != NULL) {
size_t at = strlen(buf);
snprintf(buf + at, len - at, " - %s", title);
XFree(title);
}
return 1;
}
if (XFetchName(dpy, w, &title) && title != NULL) {
snprintf(buf, len, "%s", title);
XFree(title);
return 1;
}
return 0;
}
static void
Fill(void)
{
Display *dpy = XtDisplay(fqDialog);
Window root, parent, *kids = NULL, mine = XtWindow(barShell);
unsigned int n = 0, i;
char name[256];
XmListDeleteAllItems(fqList);
fqCount = 0;
if (!XQueryTree(dpy, DefaultRootWindow(dpy), &root, &parent, &kids, &n))
return;
for (i = 0; i < n && fqCount < FQ_MAX; i++) {
XWindowAttributes a;
Window c;
XmString s;
if (!XGetWindowAttributes(dpy, kids[i], &a) ||
a.map_state != IsViewable || a.override_redirect)
continue;
c = XmuClientWindow(dpy, kids[i]);
if (c == mine || !NameOf(dpy, c, name, sizeof name))
continue;
fqWindows[fqCount++] = c;
s = XmStringCreateLocalized(name);
XmListAddItemUnselected(fqList, s, 0);
XmStringFree(s);
}
if (kids != NULL)
XFree(kids);
}
static void
Quit(Widget w, XtPointer data, XtPointer call)
{
int *pos = NULL, npos = 0;
(void) w;
(void) data;
(void) call;
if (XmListGetSelectedPos(fqList, &pos, &npos) && npos > 0 &&
pos[0] >= 1 && pos[0] <= fqCount) {
XKillClient(XtDisplay(fqList), fqWindows[pos[0] - 1]);
XFlush(XtDisplay(fqList));
}
if (pos != NULL)
XtFree((char *) pos);
Fill();
}
static void
Close(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
XtUnmanageChild(fqDialog);
}
void
ShowForceQuit(void)
{
XmString title, ok, cancel, msg;
Arg a[10];
int n = 0;
if (fqDialog == NULL) {
title = XmStringCreateLocalized("Force Quit Applications");
ok = XmStringCreateLocalized("Force Quit");
cancel = XmStringCreateLocalized("Cancel");
msg = XmStringCreateLocalized("If a program does not answer, "
"choose it and click Force Quit.\n"
"What it has not saved is lost.");
XtSetArg(a[n], XmNdialogTitle, title); n++;
XtSetArg(a[n], XmNokLabelString, ok); n++;
XtSetArg(a[n], XmNcancelLabelString, cancel); n++;
XtSetArg(a[n], XmNmessageString, msg); n++;
XtSetArg(a[n], XmNautoUnmanage, False); n++;
XtSetArg(a[n], XmNdefaultPosition, False); n++;
fqDialog = XmCreateTemplateDialog(barShell, "forceQuit", a, n);
XmStringFree(title);
XmStringFree(ok);
XmStringFree(cancel);
XmStringFree(msg);
fqList = XmCreateScrolledList(fqDialog, "programs", NULL, 0);
XtVaSetValues(fqList, XmNvisibleItemCount, 10,
XmNselectionPolicy, XmBROWSE_SELECT, XmNwidth, 360, NULL);
XtManageChild(fqList);
XtAddCallback(fqDialog, XmNokCallback, Quit, NULL);
XtAddCallback(fqList, XmNdefaultActionCallback, Quit, NULL);
XtAddCallback(fqDialog, XmNcancelCallback, Close, NULL);
}
XtManageChild(fqDialog);
Centre(fqDialog);
Fill();
}