/* * 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 #include #include #include #include #include #include #include #include #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(); }