The dialog is the reference's: the sentence at the top, one entry per program, the hint "You can open this window by pressing Command+Option+Escape." at the bottom left, and the button at the right -- "Force Quit", or "Relaunch" when the chosen program is the file manager. A question comes first: "Do you want to force <program> to quit? You will lose any unsaved changes." A program is its class (WM_CLASS), so its windows are one entry (dtfile showed up three times before). Whether it answers is the process from _NET_WM_PID: a stopped (SIGSTOP, a debugger) or zombie process says "(not responding)". That test is new in McdeSys -- mcde_sys_process_stopped(), /proc/PID/stat on Linux and KERN_PROC2/p_stat on NetBSD -- because a program never reads /proc itself (ext/src/mcde/AGENTS.md, "the operating system is behind one layer"). The pid is looked for three levels down from the root's children: dtpad's main shell carries the class but not the pid, the file manager's main window carries both (measured 2026-11-14), so the class grouping takes the pid from whichever window of the program has one. Ending a program: XKillClient closes its connection (it ends however it hangs), then a local pid gets SIGTERM and, three seconds later, SIGKILL -- a stopped process cannot see the closed connection, so the signal is what ends it. The file manager is started again a second later. Measured under Xvfb with xdotool (dtbar + dtfile, dtfile SIGSTOPped): "Dtfile (not responding)" in the list, the button says "Relaunch" (docs/screenshots/leopard/force-quit-mcde.png); Return opens the question, Return again confirms; the old process (pid 3692926) is gone after six seconds and a new dtfile runs (pid 3696883). Open: the list cannot show the program's icon or colour it red -- XmList takes strings (the reference has icons and red "not responding"); the dialog is placed by dtwm at the corner, the picture moves it aside with xdotool.
484 lines
13 KiB
C
484 lines
13 KiB
C
/*
|
|
* forcequit.c -- dtbar: Force Quit Applications
|
|
*
|
|
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
|
|
*
|
|
* The reference's window: a sentence at the top, one entry per program, the
|
|
* hint about Command+Option+Escape at the bottom, "Force Quit" at the right.
|
|
* A program is its windows' class (WM_CLASS), so a program with three
|
|
* windows is one entry; a program whose process is stopped (SIGSTOP, a
|
|
* debugger) or a zombie says "(not responding)" -- the process from
|
|
* _NET_WM_PID, the state from McdeSys (mcde_sys_process_stopped). The
|
|
* file manager is the desktop: for it the button says "Relaunch" and starts
|
|
* it again.
|
|
*
|
|
* "Force Quit" ends the program the X way first, XKillClient(3): its
|
|
* connection is closed and the program stops, however it hangs; a local
|
|
* process from _NET_WM_PID gets SIGTERM and, three seconds later, SIGKILL.
|
|
* What it has not saved is lost, so the question comes first.
|
|
*
|
|
* C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md).
|
|
*/
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <signal.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include <X11/Intrinsic.h>
|
|
#include <X11/Shell.h>
|
|
#include <X11/Xatom.h>
|
|
#include <X11/Xutil.h>
|
|
#include <X11/Xmu/WinUtil.h>
|
|
#include <Xm/DialogS.h> /* XmNdeleteResponse */
|
|
#include <Xm/Form.h>
|
|
#include <Xm/Label.h>
|
|
#include <Xm/List.h>
|
|
#include <Xm/MessageB.h>
|
|
#include <Xm/PushB.h>
|
|
#include <Xm/Protocols.h>
|
|
#include <Xm/Xm.h>
|
|
|
|
#include <Mcde/McdeSys.h>
|
|
|
|
#include "dtbar.h"
|
|
|
|
#define FQ_MAX 128
|
|
|
|
typedef struct {
|
|
Window client; /* the window whose connection is killed */
|
|
pid_t pid; /* from _NET_WM_PID, 0 when unknown */
|
|
char class[64]; /* WM_CLASS' class, or the title */
|
|
char name[96]; /* what the list shows */
|
|
Boolean stopped;
|
|
} FQProgram;
|
|
|
|
static Widget fqShell, fqList, fqOk;
|
|
static Arg fqArgs[4];
|
|
static void Fill(void);
|
|
static void FillLater(XtPointer client, XtIntervalId *id);
|
|
static void QuitCallback(Widget w, XtPointer client, XtPointer call);
|
|
static FQProgram fqPrograms[FQ_MAX];
|
|
static int fqCount;
|
|
static int fqChosen = -1;
|
|
static int fqPending = -1;
|
|
|
|
/* the class of a window: WM_CLASS' class, else its title; 0 when neither */
|
|
static int
|
|
ClassOf(Display *dpy, Window w, char *buf, size_t len)
|
|
{
|
|
XClassHint ch;
|
|
char *title = NULL;
|
|
|
|
buf[0] = '\0';
|
|
if (XGetClassHint(dpy, w, &ch)) {
|
|
const char *name = ch.res_class != NULL && ch.res_class[0] != '\0' ?
|
|
ch.res_class : ch.res_name;
|
|
|
|
if (name != NULL)
|
|
(void)snprintf(buf, len, "%s", name);
|
|
if (ch.res_name != NULL)
|
|
XFree(ch.res_name);
|
|
if (ch.res_class != NULL)
|
|
XFree(ch.res_class);
|
|
}
|
|
if (buf[0] == '\0' && XFetchName(dpy, w, &title) && title != NULL) {
|
|
(void)snprintf(buf, len, "%s", title);
|
|
XFree(title);
|
|
}
|
|
return buf[0] != '\0';
|
|
}
|
|
|
|
/* _NET_WM_PID of a window, or 0 */
|
|
static pid_t
|
|
PidOf(Display *dpy, Window w)
|
|
{
|
|
Atom a = XInternAtom(dpy, "_NET_WM_PID", False), type;
|
|
int format;
|
|
unsigned long n, after;
|
|
unsigned char *data = NULL;
|
|
pid_t pid = 0;
|
|
|
|
if (XGetWindowProperty(dpy, w, a, 0, 1, False, XA_CARDINAL, &type,
|
|
&format, &n, &after, &data) == Success && data != NULL &&
|
|
n == 1 && format == 32)
|
|
pid = (pid_t) *(long *) data;
|
|
if (data != NULL)
|
|
XFree(data);
|
|
return pid;
|
|
}
|
|
|
|
static void
|
|
SetOkLabel(const char *label)
|
|
{
|
|
XmString s = XmStringCreateLocalized((char *) label);
|
|
|
|
XtVaSetValues(fqOk, XmNlabelString, s, NULL);
|
|
XmStringFree(s);
|
|
}
|
|
|
|
typedef struct {
|
|
Window w;
|
|
char cls[64];
|
|
pid_t pid;
|
|
} FQWindow;
|
|
|
|
/* every viewable window with a class, three levels deep from the root's
|
|
* children: the frame, the client window, and the shells a program makes
|
|
* besides its main one (measured 2026-11-14: dtpad's main shell carries the
|
|
* class, another window of the same program the _NET_WM_PID) */
|
|
static void
|
|
Collect(Display *dpy, Window w, int depth, FQWindow *list, int *n, int max)
|
|
{
|
|
XWindowAttributes a;
|
|
Window root, parent, *kids = NULL;
|
|
unsigned int nk = 0, i;
|
|
char cls[64];
|
|
|
|
if (*n >= max || depth > 3)
|
|
return;
|
|
if (!XGetWindowAttributes(dpy, w, &a) || a.map_state != IsViewable ||
|
|
a.override_redirect)
|
|
return;
|
|
if (ClassOf(dpy, w, cls, sizeof cls) && strcmp(cls, "Dtbar") != 0) {
|
|
list[*n].w = w;
|
|
(void)snprintf(list[*n].cls, sizeof list[*n].cls, "%s", cls);
|
|
list[*n].pid = PidOf(dpy, w);
|
|
(*n)++;
|
|
}
|
|
if (XQueryTree(dpy, w, &root, &parent, &kids, &nk)) {
|
|
for (i = 0; i < nk && *n < max; i++)
|
|
Collect(dpy, kids[i], depth + 1, list, n, max);
|
|
if (kids != NULL)
|
|
XFree(kids);
|
|
}
|
|
}
|
|
|
|
/* one entry per program, "(not responding)" for a stopped one */
|
|
static void
|
|
Fill(void)
|
|
{
|
|
Display *dpy = XtDisplay(fqShell);
|
|
Window root, parent, *kids = NULL;
|
|
unsigned int n = 0, i;
|
|
FQWindow wins[FQ_MAX * 2];
|
|
int nw = 0, k;
|
|
|
|
XmListDeleteAllItems(fqList);
|
|
fqCount = 0;
|
|
fqChosen = -1;
|
|
SetOkLabel("Force Quit");
|
|
if (!XQueryTree(dpy, DefaultRootWindow(dpy), &root, &parent, &kids, &n))
|
|
return;
|
|
for (i = 0; i < n; i++)
|
|
Collect(dpy, kids[i], 0, wins, &nw, FQ_MAX * 2);
|
|
if (kids != NULL)
|
|
XFree(kids);
|
|
|
|
for (k = 0; k < nw && fqCount < FQ_MAX; k++) {
|
|
XmString s;
|
|
int j, have = 0;
|
|
pid_t pid = wins[k].pid;
|
|
|
|
for (j = 0; j < fqCount; j++)
|
|
if (strcmp(fqPrograms[j].class, wins[k].cls) == 0) {
|
|
have = 1;
|
|
break;
|
|
}
|
|
if (have) {
|
|
/* another window of a program already listed: its
|
|
* pid may be the one we have */
|
|
if (fqPrograms[j].pid == 0 && pid != 0)
|
|
fqPrograms[j].pid = pid;
|
|
continue;
|
|
}
|
|
fqPrograms[fqCount].client = wins[k].w;
|
|
fqPrograms[fqCount].pid = pid;
|
|
(void)snprintf(fqPrograms[fqCount].class,
|
|
sizeof fqPrograms[fqCount].class, "%s", wins[k].cls);
|
|
fqPrograms[fqCount].stopped = pid > 0 &&
|
|
mcde_sys_process_stopped(pid) == 1;
|
|
(void)snprintf(fqPrograms[fqCount].name,
|
|
sizeof fqPrograms[fqCount].name, "%s%s",
|
|
fqPrograms[fqCount].class,
|
|
fqPrograms[fqCount].stopped ? " (not responding)" : "");
|
|
s = XmStringCreateLocalized(fqPrograms[fqCount].name);
|
|
XmListAddItemUnselected(fqList, s, 0);
|
|
XmStringFree(s);
|
|
fqCount++;
|
|
}
|
|
/* a program found first without its pid may get one later */
|
|
for (k = 0; k < fqCount; k++) {
|
|
if (fqPrograms[k].pid > 0 && !fqPrograms[k].stopped &&
|
|
mcde_sys_process_stopped(fqPrograms[k].pid) == 1) {
|
|
XmString s;
|
|
int pos;
|
|
|
|
fqPrograms[k].stopped = True;
|
|
(void)snprintf(fqPrograms[k].name,
|
|
sizeof fqPrograms[k].name,
|
|
"%s (not responding)", fqPrograms[k].class);
|
|
s = XmStringCreateLocalized(fqPrograms[k].name);
|
|
pos = k + 1;
|
|
XmListReplaceItemsPosUnselected(fqList, &s, 1, pos);
|
|
XmStringFree(s);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void
|
|
FillLater(XtPointer client, XtIntervalId *id)
|
|
{
|
|
(void)client;
|
|
(void)id;
|
|
Fill();
|
|
}
|
|
|
|
/* the button says what it will do with the chosen program */
|
|
static void
|
|
Chosen(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
XmListCallbackStruct *c = (XmListCallbackStruct *) call;
|
|
|
|
(void)w;
|
|
(void)client;
|
|
fqChosen = c->item_position - 1;
|
|
if (fqChosen >= 0 && fqChosen < fqCount &&
|
|
strcmp(fqPrograms[fqChosen].class, "Dtfile") == 0)
|
|
SetOkLabel("Relaunch");
|
|
else
|
|
SetOkLabel("Force Quit");
|
|
}
|
|
|
|
/* ---- the end of a program ---------------------------------------------- */
|
|
|
|
typedef struct {
|
|
pid_t pid;
|
|
int relaunch;
|
|
Display *dpy;
|
|
} FQLater;
|
|
|
|
static void
|
|
KillLater(XtPointer client, XtIntervalId *id)
|
|
{
|
|
FQLater *s = client;
|
|
|
|
(void)id;
|
|
if (s->pid > 0 && kill(s->pid, 0) == 0)
|
|
(void)kill(s->pid, SIGKILL);
|
|
XtFree((char *) s);
|
|
}
|
|
|
|
static void
|
|
RelaunchLater(XtPointer client, XtIntervalId *id)
|
|
{
|
|
char *argv[] = { (char *) "dtfile", NULL };
|
|
|
|
(void)client;
|
|
(void)id;
|
|
/* the old file manager has to be gone before its successor asks for
|
|
* the same windows and session */
|
|
if (fork() == 0) {
|
|
setsid();
|
|
execvp(argv[0], argv);
|
|
_exit(127);
|
|
}
|
|
}
|
|
|
|
static void
|
|
DoQuit(int i)
|
|
{
|
|
Display *dpy;
|
|
FQLater *s;
|
|
Boolean relaunch;
|
|
|
|
if (i < 0 || i >= fqCount)
|
|
return;
|
|
dpy = XtDisplay(fqShell);
|
|
relaunch = strcmp(fqPrograms[i].class, "Dtfile") == 0;
|
|
XKillClient(dpy, fqPrograms[i].client);
|
|
XFlush(dpy);
|
|
if (fqPrograms[i].pid > 0) {
|
|
(void)kill(fqPrograms[i].pid, SIGTERM);
|
|
s = XtNew(FQLater);
|
|
s->pid = fqPrograms[i].pid;
|
|
s->relaunch = 0;
|
|
s->dpy = dpy;
|
|
XtAppAddTimeOut(XtWidgetToApplicationContext(fqShell), 3000,
|
|
KillLater, s);
|
|
}
|
|
if (relaunch)
|
|
XtAppAddTimeOut(XtWidgetToApplicationContext(fqShell), 1000,
|
|
RelaunchLater, NULL);
|
|
/* the list again, once the window is gone */
|
|
XtAppAddTimeOut(XtWidgetToApplicationContext(fqShell), 500,
|
|
FillLater, NULL);
|
|
}
|
|
|
|
/* the question the reference asks before it throws work away */
|
|
static void
|
|
ConfirmQuit(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
Arg a[8];
|
|
int n = 0;
|
|
Widget d;
|
|
char text[256];
|
|
XmString m, ok, cancel, title;
|
|
|
|
(void)w;
|
|
(void)client;
|
|
(void)call;
|
|
fqPending = fqChosen;
|
|
if (fqPending < 0 || fqPending >= fqCount) {
|
|
XBell(XtDisplay(fqShell), 0);
|
|
return;
|
|
}
|
|
(void)snprintf(text, sizeof text,
|
|
"Do you want to force %s to quit?\nYou will lose any unsaved "
|
|
"changes.", fqPrograms[fqPending].class);
|
|
m = XmStringCreateLocalized(text);
|
|
ok = XmStringCreateLocalized((char *) "Force Quit");
|
|
cancel = XmStringCreateLocalized((char *) "Cancel");
|
|
title = XmStringCreateLocalized((char *) "Force Quit Applications");
|
|
XtSetArg(a[n], XmNmessageString, m); n++;
|
|
XtSetArg(a[n], XmNokLabelString, ok); n++;
|
|
XtSetArg(a[n], XmNcancelLabelString, cancel); n++;
|
|
XtSetArg(a[n], XmNdialogTitle, title); n++;
|
|
XtSetArg(a[n], XmNautoUnmanage, False); n++;
|
|
d = XmCreateQuestionDialog(fqShell, "confirmQuit", a, n);
|
|
XmStringFree(m);
|
|
XmStringFree(ok);
|
|
XmStringFree(cancel);
|
|
XmStringFree(title);
|
|
XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_HELP_BUTTON));
|
|
XtAddCallback(d, XmNokCallback, QuitCallback, (XtPointer) d);
|
|
XtAddCallback(d, XmNcancelCallback, QuitCallback, (XtPointer) d);
|
|
XtManageChild(d);
|
|
Centre(d);
|
|
}
|
|
|
|
/* both the OK and the cancel of the question come here */
|
|
static void
|
|
QuitCallback(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
XmAnyCallbackStruct *c = (XmAnyCallbackStruct *) call;
|
|
Widget d = (Widget) client;
|
|
|
|
(void)w;
|
|
if (c != NULL && c->reason == XmCR_OK) {
|
|
DoQuit(fqPending);
|
|
fqPending = -1;
|
|
}
|
|
XtDestroyWidget(d);
|
|
}
|
|
|
|
/* ---- the window --------------------------------------------------------- */
|
|
|
|
static void
|
|
Close(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
(void)w;
|
|
(void)client;
|
|
(void)call;
|
|
XtUnmanageChild(fqShell);
|
|
}
|
|
|
|
static void
|
|
CloseCB(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
(void)w;
|
|
(void)client;
|
|
(void)call;
|
|
XtUnmanageChild(fqShell);
|
|
}
|
|
|
|
static void
|
|
KeyHandler(Widget w, XtPointer client, XEvent *event, Boolean *cont)
|
|
{
|
|
(void)client;
|
|
if (XLookupKeysym(&event->xkey, 0) == XK_Escape) {
|
|
XtUnmanageChild(fqShell);
|
|
*cont = False;
|
|
}
|
|
}
|
|
|
|
static void
|
|
Build(void)
|
|
{
|
|
Widget form, text, buttons, hint;
|
|
XmString s;
|
|
|
|
fqShell = XtVaCreatePopupShell("forceQuit", topLevelShellWidgetClass,
|
|
barShell, XmNtitle, "Force Quit Applications",
|
|
XmNallowShellResize, True, XmNtransientFor, barShell, NULL);
|
|
form = XtVaCreateManagedWidget("fqForm", xmFormWidgetClass, fqShell,
|
|
XmNmarginWidth, 12, XmNmarginHeight, 12,
|
|
XmNwidth, 470, XmNheight, 290, NULL);
|
|
|
|
text = XtVaCreateManagedWidget("fqText", xmLabelWidgetClass, form,
|
|
XmNalignment, XmALIGNMENT_BEGINNING,
|
|
XmNtopAttachment, XmATTACH_FORM,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM, NULL);
|
|
s = XmStringCreateLocalized("If an application doesn't respond for a "
|
|
"while, select its\nname and click Force Quit.");
|
|
XtVaSetValues(text, XmNlabelString, s, NULL);
|
|
XmStringFree(s);
|
|
|
|
XtSetArg(fqArgs[0], XmNvisibleItemCount, 8);
|
|
XtSetArg(fqArgs[1], XmNselectionPolicy, XmBROWSE_SELECT);
|
|
fqList = XmCreateScrolledList(form, "programs", fqArgs, 2);
|
|
XtVaSetValues(XtParent(fqList),
|
|
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, text,
|
|
XmNtopOffset, 10,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNbottomAttachment, XmATTACH_WIDGET,
|
|
XmNbottomWidget, NULL, NULL);
|
|
|
|
/* the hint bottom left, the button bottom right */
|
|
hint = XtVaCreateManagedWidget("fqHint", xmLabelWidgetClass, form,
|
|
XmNalignment, XmALIGNMENT_BEGINNING,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNbottomAttachment, XmATTACH_FORM, NULL);
|
|
s = XmStringCreateLocalized("You can open this window by pressing\n"
|
|
"Command+Option+Escape.");
|
|
XtVaSetValues(hint, XmNlabelString, s, NULL);
|
|
XmStringFree(s);
|
|
|
|
s = XmStringCreateLocalized((char *) "Force Quit");
|
|
fqOk = XtVaCreateManagedWidget("fqForce", xmPushButtonWidgetClass,
|
|
form, XmNlabelString, s,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNbottomAttachment, XmATTACH_FORM, NULL);
|
|
XmStringFree(s);
|
|
XtVaSetValues(XtParent(fqList),
|
|
XmNbottomAttachment, XmATTACH_WIDGET, XmNbottomWidget, hint,
|
|
XmNbottomOffset, 10, NULL);
|
|
/* the list inside its scrolled window: the window comes with it */
|
|
XtManageChild(fqList);
|
|
|
|
XtAddCallback(fqOk, XmNactivateCallback, ConfirmQuit, NULL);
|
|
XtAddCallback(fqList, XmNbrowseSelectionCallback, Chosen, NULL);
|
|
XtAddCallback(fqList, XmNdefaultActionCallback, ConfirmQuit, NULL);
|
|
XtAddEventHandler(fqShell, KeyPressMask, False, KeyHandler, NULL);
|
|
/* the close button of the window only hides it */
|
|
XmAddWMProtocolCallback(fqShell,
|
|
XmInternAtom(XtDisplay(fqShell), "WM_DELETE_WINDOW", False),
|
|
CloseCB, NULL);
|
|
}
|
|
|
|
void
|
|
ShowForceQuit(void)
|
|
{
|
|
if (fqShell == NULL)
|
|
Build();
|
|
fqPending = -1;
|
|
XtManageChild(fqShell);
|
|
XtPopup(fqShell, XtGrabNone);
|
|
Centre(fqShell);
|
|
Fill();
|
|
}
|