Files
lotherk f8a45ae56c dtbar/dtwm: the bar is no focus target, so its menu can be used with the mouse
Moving the pointer from a program to the bar took the keyboard away from that
program: dtbar set the input focus to PointerRoot on EnterNotify and dtwm
cleared the active client, so the bar showed the M menu instead of the
program's and a click could not act on it (t0255/t0257).

Now dtbar marks its window _MCDE_NO_FOCUS and leaves the focus alone, and
dtwm's focus-follows-pointer path (HandleWsFocusIn) skips a client that
carries the property: the last program stays focused while the pointer crosses
the bar, the bar keeps showing its menu, and an item acts on the client the
menu came from (ItemActivated sends the ClientMessage to that window, not to
whoever has the X focus).

Measured: make -C programs/dtwm rc=0 and make -C programs/dtbar rc=0, 0
errors each.
2026-11-22 09:59:49 +01:00

1634 lines
44 KiB
C

/*
* dtbar.c -- MCDE: the bar at the top of the screen
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* Left the M menu (the system: settings, lock, log out), then the menus of
* the program that has the focus, right the clock. The menus are not this
* program's own: the program writes its menu bar to its window and keeps the
* buttons, this draws the bar from the text and sends a click back (the
* protocol: include/Mcde/GlobalMenu.h; the program's side is
* lib/DtSvc/DtUtil2/McdeGlobalMenu.c, which every CDE program has through
* DtInitialize). One bar a screen: it owns _MCDE_BAR_S<screen>.
*
* C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md).
*/
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <pwd.h>
#include <ctype.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/wait.h>
#include <X11/Intrinsic.h>
#include <X11/Shell.h>
#include <X11/Xatom.h>
#include <X11/Xmu/WinUtil.h>
#include <nl_types.h>
#include <Dt/MsgCatP.h>
#include <Xm/Xm.h>
#include <Xm/CascadeBG.h>
#include <Xm/Form.h>
#include <Xm/LabelG.h>
#include <Xm/PushBG.h>
#include <Xm/RowColumn.h>
#include <Xm/SeparatoG.h>
#include <Xm/ToggleBG.h>
#include <Xm/MessageB.h>
#include <Dt/SessionM.h>
#include <Dt/WsmP.h>
#include <Mcde/GlobalMenu.h>
#include <Mcde/McdeSys.h>
#include "dtbar.h"
#include "items.h"
static Display *dpy;
static Widget shell, form, bar, clockLabel;
static Widget mcdeMenuButton; /* the M cascade, first in the bar (Ctrl+F2) */
static Widget sink; /* the root's and the client's events go here */
static Atom aBar, aMenu, aActive, aActivate, aRefresh, aUtf8, aManager;
static Atom aNetActive, aLeader, aCommand;
static Widget forceQuitItem;
static char appName[64]; /* the class of the program shown */
static Window client; /* the window whose menus are shown */
static Boolean focused = True; /* client's program has the focus */
static McdeMenuItem items[MCDE_MENU_MAX];
static int nitems;
static Widget appWidgets[2 * MCDE_MENU_MAX]; /* what Rebuild made */
static int nappWidgets;
static int debug; /* MCDE_GLOBAL_MENU_DEBUG */
static int posted; /* menus of the bar open now */
static Boolean pending; /* a new _MCDE_MENU came meanwhile */
static void ReadMenu(void);
/* an open menu is never rebuilt under the pointer: a new menu text waits
* until the last menu of the bar closes */
static void
Mapped(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
posted++;
}
static void
Unmapped(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
if (posted > 0)
posted--;
if (posted == 0 && pending) {
pending = False;
ReadMenu();
}
}
static Widget
Pulldown(Widget parent, const char *name)
{
Widget m = XmCreatePulldownMenu(parent, (char *) name, NULL, 0);
XtAddCallback(m, XmNmapCallback, Mapped, NULL);
XtAddCallback(m, XmNunmapCallback, Unmapped, NULL);
return m;
}
static const char *fallback[] = {
"Dtbar*shadowThickness: 1",
"Dtbar*marginHeight: 1",
"Dtbar*mbar.marginHeight: 0",
"Dtbar*mbar.shadowThickness: 0",
"Dtbar*mcdeM.labelString: M",
"Dtbar*clock.marginWidth: 10",
/* the bar has the colours of the programs' windows (colour set 5,
* primary), not those of a menu bar (6, secondary): it is the
* desktop's, as the front panel is (Konrad, 2026-11-13) */
"Dtbar*secondaryColorSetId: 5",
NULL
};
static int
IgnoreErrors(Display *d, XErrorEvent *e)
{
/* a window that went away under our feet (BadWindow) is no error */
(void) d;
(void) e;
return 0;
}
/* ---- the M menu --------------------------------------------------------- */
Widget barShell;
/* start ARGV in a session of its own, without waiting and without a
* zombie: the child forks the program and ends, the program becomes init's
* (SIGCHLD stays default: McdeSys waits for its tools) */
static void
Spawn(char *const argv[])
{
pid_t pid = fork();
if (pid == 0) {
if (fork() == 0) {
setsid();
execvp(argv[0], argv);
_exit(127);
}
_exit(0);
}
if (pid > 0)
while (waitpid(pid, NULL, 0) < 0 && errno == EINTR)
;
}
void
RunCommand(const char *command)
{
char *argv[] = { "/bin/sh", "-c", NULL, NULL };
argv[2] = (char *) command;
Spawn(argv);
}
static void
Run(const char *action)
{
char *argv[] = { "dtaction", NULL, NULL };
argv[1] = (char *) action;
Spawn(argv);
}
static void
MAction(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) call;
Run((const char *) data);
}
void
Centre(Widget dialog)
{
Widget sh = XtParent(dialog);
Screen *scr = XtScreen(dialog);
Dimension w = 0, h = 0;
XtVaGetValues(sh, XmNwidth, &w, XmNheight, &h, NULL);
XtVaSetValues(sh, XmNx, (Position) ((WidthOfScreen(scr) - w) / 2),
XmNy, (Position) ((HeightOfScreen(scr) - h) / 3), NULL);
}
static void
MessageClosed(Widget w, XtPointer data, XtPointer call)
{
(void) data;
(void) call;
XtDestroyWidget(XtParent(w));
}
void
ShowMessage(const char *title, const char *text, int error)
{
XmString t = XmStringCreateLocalized((char *) title);
XmString m = XmStringCreateLocalized((char *) text);
Arg a[4];
int n = 0;
Widget d;
XtSetArg(a[n], XmNdialogTitle, t); n++;
XtSetArg(a[n], XmNmessageString, m); n++;
XtSetArg(a[n], XmNdefaultPosition, False); n++;
d = error ? XmCreateErrorDialog(barShell, "message", a, n) :
XmCreateInformationDialog(barShell, "message", a, n);
XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_CANCEL_BUTTON));
XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_HELP_BUTTON));
XtAddCallback(d, XmNokCallback, MessageClosed, NULL);
XtManageChild(d);
Centre(d);
XmStringFree(t);
XmStringFree(m);
}
static Widget
Push(Widget menu, const char *label, char mn, XtCallbackProc cb,
XtPointer data)
{
XmString s = XmStringCreateLocalized((char *) label);
Widget b = XtVaCreateManagedWidget("item", xmPushButtonGadgetClass,
menu, XmNlabelString, s, XmNmnemonic, (KeySym) mn, NULL);
XmStringFree(s);
if (cb != NULL)
XtAddCallback(b, XmNactivateCallback, cb, data);
else
XtSetSensitive(b, False);
return b;
}
static void
Separator(Widget menu)
{
XtVaCreateManagedWidget("sep", xmSeparatorGadgetClass, menu, NULL);
}
static Widget
Cascade(Widget menu, const char *label, char mn, Widget sub)
{
XmString s = XmStringCreateLocalized((char *) label);
Widget b = XtVaCreateManagedWidget("cascade", xmCascadeButtonGadgetClass,
menu, XmNlabelString, s, XmNmnemonic, (KeySym) mn,
XmNsubMenuId, sub, NULL);
XmStringFree(s);
return b;
}
/* a command line of the menu (RunCommand) */
static void
RunAction(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) call;
RunCommand((const char *) data);
}
static void
About(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
ShowAbout();
}
static void
ForceQuit(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
ShowForceQuit();
}
/* a program of the session, started directly (no shell, no foreign text):
* dtsystemsettings with the pane as its one argument */
static void
RunProgram(Widget w, XtPointer data, XtPointer call)
{
char *argv[] = { "dtsystemsettings", "-pane", (char *) data, NULL };
(void) w;
(void) call;
Spawn(argv);
}
static void
SetAccel(Widget item, const char *text)
{
XmString s = XmStringCreateLocalized((char *) text);
XtVaSetValues(item, XmNacceleratorText, s, NULL);
XmStringFree(s);
}
/* the program of a window: the WM_CLASS class string, else the window name */
static void
ProgramOf(Window w, char *buf, size_t len)
{
XClassHint ch;
XTextProperty tp;
buf[0] = '\0';
if (w == None)
return;
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)
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' && XGetWMName(dpy, w, &tp) && tp.value != NULL) {
snprintf(buf, len, "%.*s", (int) tp.nitems, (char *) tp.value);
XFree(tp.value);
}
}
/* the program of the focused window (MCDE: the reference names it in the
* Force Quit item, "Force Quit Finder"). _NET_ACTIVE_WINDOW first; a window
* manager that does not set it leaves the keyboard focus, and with the bar
* focused the program whose menus it carries. */
static void
ActiveProgram(char *buf, size_t len)
{
Atom type;
int format;
unsigned long n, after;
unsigned char *data = NULL;
Window active = None;
int revert;
Window focus;
buf[0] = '\0';
if (XGetWindowProperty(dpy, DefaultRootWindow(dpy), aNetActive, 0, 1,
False, XA_WINDOW, &type, &format, &n, &after, &data) == Success &&
data != NULL && n == 1 && format == 32)
active = (Window) *(long *) data;
if (data != NULL)
XFree(data);
if (active != None)
ProgramOf(XmuClientWindow(dpy, active), buf, len);
if (buf[0] == '\0') {
XGetInputFocus(dpy, &focus, &revert);
if (focus != None && focus != PointerRoot)
ProgramOf(XmuClientWindow(dpy, focus), buf, len);
}
if (buf[0] == '\0' && client != None)
ProgramOf(XmuClientWindow(dpy, client), buf, len);
}
/* the Force Quit item names the program when the M menu opens */
static void
FillForceQuit(Widget w, XtPointer data, XtPointer call)
{
char name[128], label[192];
XmString s;
(void) w;
(void) data;
(void) call;
ActiveProgram(name, sizeof name);
if (name[0] != '\0')
snprintf(label, sizeof label, "Force Quit %s", name);
else
snprintf(label, sizeof label, "Force Quit...");
s = XmStringCreateLocalized(label);
XtVaSetValues(forceQuitItem, XmNlabelString, s, NULL);
XmStringFree(s);
}
/* Favourites (~/.dt/favourites, by hand) and Recent Items (~/.dt/recent,
* by dtbar): items.c */
static Widget favouritesMenu, recentMenu;
static Item favourites[ITEMS_MAX], recent[ITEMS_MAX];
static void
DtFile(const char *name, char *buf, size_t len)
{
const char *home = getenv("HOME");
snprintf(buf, len, "%s/.dt/%s", home != NULL ? home : ".", name);
}
/* how many Recent Items System Settings > Appearance asks for
* (~/.dt/recent.conf, "documents=N"); 10 when the file does not say */
static int
RecentCount(void)
{
char path[1024], line[256];
const char *home = getenv("HOME");
FILE *fp;
int n = 10;
if (home == NULL || (size_t) snprintf(path, sizeof path,
"%s/.dt/recent.conf", home) >= sizeof path)
return 10;
if ((fp = fopen(path, "r")) == NULL)
return 10;
while (fgets(line, sizeof line, fp) != NULL) {
line[strcspn(line, "\r\n")] = '\0';
if (strncmp(line, "documents=", 10) == 0)
n = atoi(line + 10);
}
fclose(fp);
if (n < 0)
n = 0;
if (n > ITEMS_MAX)
n = ITEMS_MAX;
return n;
}
static void
Open(const Item *it)
{
char file[1024];
int n, max;
RunCommand(it->command);
max = RecentCount();
if (max <= 0)
return;
DtFile("recent", file, sizeof file);
n = ItemsRead(file, recent, ITEMS_MAX);
n = ItemsPush(recent, n, max, it);
(void) ItemsWrite(file, recent, n);
}
static void
OpenFavourite(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) call;
Open(&favourites[(int) (size_t) data]);
}
static void
OpenRecent(Widget w, XtPointer data, XtPointer call)
{
Item it;
(void) w;
(void) call;
it = recent[(int) (size_t) data]; /* Open rewrites recent[] */
Open(&it);
}
static void
EditFavourites(Widget w, XtPointer data, XtPointer call)
{
char file[1024], cmd[1200];
FILE *fp;
(void) w;
(void) data;
(void) call;
DtFile("favourites", file, sizeof file);
if ((fp = fopen(file, "a")) != NULL) {
if (ftell(fp) == 0)
fputs("# Favourites: one a line, the name, a tab, the "
"command.\n# Terminal\txterm\n", fp);
fclose(fp);
}
snprintf(cmd, sizeof cmd, "dtpad '%s'", file);
RunCommand(cmd);
}
static void
ClearRecent(Widget w, XtPointer data, XtPointer call)
{
char file[1024];
(void) w;
(void) data;
(void) call;
DtFile("recent", file, sizeof file);
(void) remove(file);
}
static void
Empty(Widget menu)
{
WidgetList kids;
Cardinal n, i;
XtVaGetValues(menu, XmNchildren, &kids, XmNnumChildren, &n, NULL);
for (i = 0; i < n; i++)
XtDestroyWidget(kids[i]);
}
/* the two lists are read when the M menu opens, so they are always new */
static void
FillFavourites(Widget w, XtPointer data, XtPointer call)
{
char file[1024];
int n, i;
(void) w;
(void) data;
(void) call;
Empty(favouritesMenu);
DtFile("favourites", file, sizeof file);
n = ItemsRead(file, favourites, ITEMS_MAX);
for (i = 0; i < n; i++)
Push(favouritesMenu, favourites[i].label, 0, OpenFavourite,
(XtPointer) (size_t) i);
if (n == 0)
Push(favouritesMenu, "No Favourites", 0, NULL, NULL);
Separator(favouritesMenu);
Push(favouritesMenu, "Edit Favourites...", 'E', EditFavourites, NULL);
}
static void
FillRecent(Widget w, XtPointer data, XtPointer call)
{
char file[1024];
int n, i;
(void) w;
(void) data;
(void) call;
Empty(recentMenu);
DtFile("recent", file, sizeof file);
n = ItemsRead(file, recent, ITEMS_MAX);
{
int max = RecentCount();
if (n > max)
n = max;
}
for (i = 0; i < n; i++)
Push(recentMenu, recent[i].label, 0, OpenRecent,
(XtPointer) (size_t) i);
if (n == 0)
Push(recentMenu, "No Recent Items", 0, NULL, NULL);
Separator(recentMenu);
Push(recentMenu, "Clear Menu", 'C', n > 0 ? ClearRecent : NULL, NULL);
}
/* sleep, restart, shut down: McdeSys does it the system's way; restart and
* shut down ask first */
static void
DoPower(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) call;
XFlush(XtDisplay(barShell));
if (mcde_sys_power((McdeSysPower) (size_t) data) != MCDE_SYS_OK)
ShowMessage("Power", mcde_sys_error(), 1);
}
static void
AskPower(Widget w, XtPointer data, XtPointer call)
{
McdeSysPower what = (McdeSysPower) (size_t) data;
const char *q = what == MCDE_POWER_RESTART ?
"Are you sure you want to restart the computer now?" :
"Are you sure you want to shut down the computer now?";
XmString m = XmStringCreateLocalized((char *) q);
XmString ok = XmStringCreateLocalized(what == MCDE_POWER_RESTART ?
"Restart" : "Shut Down");
XmString t = XmStringCreateLocalized(what == MCDE_POWER_RESTART ?
"Restart" : "Shut Down");
Arg a[4];
int n = 0;
Widget d;
(void) w;
(void) call;
XtSetArg(a[n], XmNmessageString, m); n++;
XtSetArg(a[n], XmNokLabelString, ok); n++;
XtSetArg(a[n], XmNdialogTitle, t); n++;
XtSetArg(a[n], XmNdefaultPosition, False); n++;
d = XmCreateQuestionDialog(barShell, "power", a, n);
XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_HELP_BUTTON));
XtAddCallback(d, XmNokCallback, DoPower, data);
XtAddCallback(d, XmNokCallback, MessageClosed, NULL);
XtAddCallback(d, XmNcancelCallback, MessageClosed, NULL);
XtManageChild(d);
Centre(d);
XmStringFree(m);
XmStringFree(ok);
XmStringFree(t);
}
/* "Log Out Konrad Lother...": the name from the password file */
static void
LogOutLabel(char *buf, size_t len)
{
struct passwd *pw = getpwuid(getuid());
char name[128] = "";
if (pw != NULL) {
snprintf(name, sizeof name, "%s", pw->pw_gecos != NULL &&
pw->pw_gecos[0] != '\0' && pw->pw_gecos[0] != ',' ?
pw->pw_gecos : pw->pw_name);
name[strcspn(name, ",")] = '\0';
}
if (name[0] != '\0')
snprintf(buf, len, "Log Out %s...", name);
else
snprintf(buf, len, "Log Out...");
}
/* dtbar's message catalog (programs/dtbar/dtbar.msg, the locale's copy in
* programs/localized/<locale>/msg, installed under lib/nls/msg) */
static nl_catd barCatalog = (nl_catd) -1;
static const char *
BarMsg(int n, const char *text)
{
if (barCatalog == (nl_catd) -1) {
if (getenv("NLSPATH") == NULL)
(void) setenv("NLSPATH", CDE_INSTALLATION_TOP
"/lib/nls/msg/%L/%N.cat:" CDE_INSTALLATION_TOP
"/lib/nls/msg/C/%N.cat", 0);
barCatalog = CATOPEN("dtbar", NL_CAT_LOCALE);
}
if (barCatalog == (nl_catd) -1)
return text;
return CATGETS(barCatalog, 1, n, text);
}
static void
MakeMMenu(void)
{
Widget menu = Pulldown(bar, "mcdeMMenu");
char logout[200];
Push(menu, BarMsg(1, "About This Computer"), 'A', About, NULL);
Push(menu, BarMsg(2, "Software Update..."), 'U', RunProgram, "update");
/* the package catalogue is a task of its own (t0056): insensitive
* until it exists, as the reference's "Mac OS X Software..." */
Push(menu, BarMsg(3, "Makronix Software..."), 'w', NULL, NULL);
Separator(menu);
Push(menu, BarMsg(4, "System Settings..."), 'S', RunAction,
"dtsystemsettings");
Separator(menu);
/* the lists are filled when the M menu opens, not when their own menu
* does: a pulldown emptied while Motif posts it closes again */
favouritesMenu = Pulldown(menu, "favourites");
(void) Cascade(menu, BarMsg(5, "Favourites"), 'F', favouritesMenu);
recentMenu = Pulldown(menu, "recent");
(void) Cascade(menu, BarMsg(6, "Recent Items"), 'R', recentMenu);
XtAddCallback(menu, XmNmapCallback, FillFavourites, NULL);
XtAddCallback(menu, XmNmapCallback, FillRecent, NULL);
Separator(menu);
forceQuitItem = Push(menu, BarMsg(7, "Force Quit..."), 'Q', ForceQuit,
NULL);
SetAccel(forceQuitItem, "Cmd+Option+Esc");
XtAddCallback(menu, XmNmapCallback, FillForceQuit, NULL);
Separator(menu);
{
Widget panelMenu = Pulldown(menu, "frontPanel");
Widget item;
(void) Cascade(menu, BarMsg(8, "Front Panel"), 'F', panelMenu);
item = Push(panelMenu, "Show or Hide", 'H', MAction,
"f.toggle_frontpanel");
SetAccel(item, "Cmd+Option+D");
/* the pane of the bar comes with t0112 */
Push(panelMenu, "Settings...", 'E', NULL, NULL);
}
Separator(menu);
Push(menu, BarMsg(9, "Sleep"), 'l', DoPower,
(XtPointer) (size_t) MCDE_POWER_SLEEP);
Push(menu, BarMsg(10, "Restart..."), 't', AskPower,
(XtPointer) (size_t) MCDE_POWER_RESTART);
Push(menu, BarMsg(11, "Shut Down..."), 'D', AskPower,
(XtPointer) (size_t) MCDE_POWER_SHUTDOWN);
Separator(menu);
Push(menu, BarMsg(12, "Lock Screen"), 'k', MAction, "LockDisplay");
LogOutLabel(logout, sizeof logout);
{
Widget item = Push(menu, logout, 'O', MAction, "ExitSession");
SetAccel(item, "Shift+Cmd+Q");
}
/* the Makronix logo, 16 pixels (icons/MakronixDtlogo.t.pm), given when
* the button is made so that Motif sizes it; the letter M where the
* icon is missing (the resource mcdeM.labelString) */
{
char path[512];
Pixel fg, bg;
Pixmap pm;
XtVaGetValues(bar, XmNforeground, &fg, XmNbackground, &bg, NULL);
snprintf(path, sizeof path,
"%s/appconfig/icons/C/MakronixDtlogo.t.pm",
CDE_INSTALLATION_TOP);
pm = XmGetPixmap(XtScreen(bar), path, fg, bg);
if (pm != XmUNSPECIFIED_PIXMAP)
mcdeMenuButton = XtVaCreateManagedWidget("mcdeM",
xmCascadeButtonGadgetClass, bar, XmNsubMenuId, menu,
XmNlabelType, XmPIXMAP, XmNlabelPixmap, pm, NULL);
else
mcdeMenuButton = XtVaCreateManagedWidget("mcdeM",
xmCascadeButtonGadgetClass, bar, XmNsubMenuId, menu,
NULL);
}
}
/* ---- the program's menus ------------------------------------------------ */
static void
Send(Atom type, long a, long b)
{
XEvent ev;
if (client == None)
return;
memset(&ev, 0, sizeof ev);
ev.xclient.type = ClientMessage;
ev.xclient.window = client;
ev.xclient.message_type = type;
ev.xclient.format = 32;
ev.xclient.data.l[0] = a;
ev.xclient.data.l[1] = b;
XSendEvent(dpy, client, False, NoEventMask, &ev);
XFlush(dpy);
}
static void
ItemActivated(Widget w, XtPointer data, XtPointer call)
{
XmAnyCallbackStruct *cbs = (XmAnyCallbackStruct *) call;
Time t = CurrentTime;
(void) w;
if (cbs != NULL && cbs->event != NULL &&
(cbs->event->type == ButtonRelease || cbs->event->type == ButtonPress))
t = cbs->event->xbutton.time;
else if (cbs != NULL && cbs->event != NULL &&
(cbs->event->type == KeyPress || cbs->event->type == KeyRelease))
t = cbs->event->xkey.time;
if (debug)
fprintf(stderr, "dtbar: activate %d\n", (int) (size_t) data);
Send(aActivate, (long) (size_t) data, (long) t);
}
/* ---- the program's own menu (MCDE: the first, fat cascade) ------------ */
/* the program a menu item label names, case-insensitively: -1 if none; the
* first match at the shallowest depth (About in Help, Options in File) */
static int
FindItem(const char *what)
{
int i, best = -1, depth = 0;
for (i = 0; i < nitems; i++) {
const char *l = items[i].label;
size_t k;
if (items[i].kind != MCDE_MENU_PUSH &&
items[i].kind != MCDE_MENU_CASCADE)
continue;
for (k = 0; l[k] != '\0'; k++) {
size_t j = 0;
while (what[j] != '\0' && l[k + j] != '\0' &&
tolower((unsigned char) l[k + j]) ==
(unsigned char) what[j])
j++;
if (what[j] == '\0') {
if (best < 0 || items[i].depth < depth) {
best = i;
depth = items[i].depth;
}
break;
}
}
}
return best;
}
/* the class of the client's window, else its title: the program's name */
static void
ClientName(char *buf, size_t len)
{
XClassHint ch;
buf[0] = '\0';
if (client != None && XGetClassHint(dpy, client, &ch)) {
const char *n = ch.res_class != NULL && ch.res_class[0] != '\0' ?
ch.res_class : ch.res_name;
if (n != NULL)
(void)snprintf(buf, len, "%s", n);
if (ch.res_name != NULL)
XFree(ch.res_name);
if (ch.res_class != NULL)
XFree(ch.res_class);
}
if (buf[0] == '\0' && client != None) {
char *title = NULL;
if (XFetchName(dpy, client, &title) && title != NULL) {
(void)snprintf(buf, len, "%s", title);
XFree(title);
}
}
}
static void
AppAbout(Widget w, XtPointer data, XtPointer call)
{
char title[128], text[320];
int i = FindItem("About");
(void)w;
(void)data;
(void)call;
if (i >= 0) {
Send(aActivate, (long) i, (long) CurrentTime);
return;
}
(void)snprintf(title, sizeof title, "About %s", appName);
(void)snprintf(text, sizeof text, "%s\nMCDE (CDE 2.5.3)", appName);
ShowMessage(title, text, 0);
}
static void
AppPreferences(Widget w, XtPointer data, XtPointer call)
{
int i;
(void)w;
(void)data;
(void)call;
i = FindItem("Options");
if (i < 0)
i = FindItem("Preferences");
if (i < 0)
i = FindItem("Properties");
if (i >= 0)
Send(aActivate, (long) i, (long) CurrentTime);
else
XBell(dpy, 0);
}
/* WM_CHANGE_STATE IconicState, the ICCCM way to ask the window manager to
* iconify a window (dtwm's f.mcde_app is its own, not for clients) */
static void
ChangeState(Window w, long state)
{
Atom a = XInternAtom(dpy, "WM_CHANGE_STATE", False);
XEvent ev;
memset(&ev, 0, sizeof ev);
ev.xclient.type = ClientMessage;
ev.xclient.window = w;
ev.xclient.message_type = a;
ev.xclient.format = 32;
ev.xclient.data.l[0] = state;
/* the ICCCM sends WM_CHANGE_STATE to the root, naming the window */
XSendEvent(dpy, DefaultRootWindow(dpy), False,
SubstructureRedirectMask | SubstructureNotifyMask, &ev);
}
/* the client windows of the screen, each with its class */
static int
ClientWindows(Window *out, char classes[][64], int max)
{
Window root, parent, *kids = NULL;
unsigned int n = 0, i;
int count = 0;
if (!XQueryTree(dpy, DefaultRootWindow(dpy), &root, &parent, &kids, &n))
return 0;
for (i = 0; i < n && count < max; i++) {
XWindowAttributes a;
Window c;
char cls[64];
if (!XGetWindowAttributes(dpy, kids[i], &a) ||
a.map_state == IsUnmapped || a.override_redirect)
continue;
c = XmuClientWindow(dpy, kids[i]);
if (c == None || c == XtWindow(barShell))
continue;
cls[0] = '\0';
{
XClassHint ch;
if (XGetClassHint(dpy, c, &ch)) {
const char *nm = ch.res_class != NULL ?
ch.res_class : ch.res_name;
if (nm != NULL)
(void)snprintf(cls, sizeof cls, "%s", nm);
if (ch.res_name != NULL)
XFree(ch.res_name);
if (ch.res_class != NULL)
XFree(ch.res_class);
}
}
if (cls[0] == '\0')
continue;
out[count] = c;
(void)snprintf(classes[count], 64, "%s", cls);
count++;
}
if (kids != NULL)
XFree(kids);
return count;
}
/* hide the program's windows, or every other program's */
static void
AppHide(Widget w, XtPointer data, XtPointer call)
{
Window wins[128];
char classes[128][64];
int n, i, others = data != NULL;
(void)w;
(void)call;
n = ClientWindows(wins, classes, 128);
for (i = 0; i < n; i++) {
Boolean mine = strcmp(classes[i], appName) == 0;
if (others ? !mine : mine)
ChangeState(wins[i], IconicState);
}
XFlush(dpy);
}
/* every hidden program comes back: WM_CHANGE_STATE NormalState, the
* ICCCM's way, to the client of each frame -- iconified windows are
* unmapped, so they are not in ClientWindows() (measured: dtwm restores the
* window on this message) */
static void
AppShowAll(Widget w, XtPointer data, XtPointer call)
{
Window root, parent, *kids = NULL;
unsigned int n = 0, i;
(void) w;
(void) data;
(void) call;
if (!XQueryTree(dpy, DefaultRootWindow(dpy), &root, &parent, &kids,
&n))
return;
for (i = 0; i < n; i++) {
XWindowAttributes a;
Window c;
if (!XGetWindowAttributes(dpy, kids[i], &a) ||
a.override_redirect)
continue;
c = XmuClientWindow(dpy, kids[i]);
if (c == None || c == XtWindow(barShell))
continue;
ChangeState(c, NormalState);
}
if (kids != NULL)
XFree(kids);
XFlush(dpy);
}
static void
AppQuit(Widget w, XtPointer data, XtPointer call)
{
int i;
(void)w;
(void)data;
(void)call;
i = FindItem("Exit");
if (i < 0)
i = FindItem("Quit");
if (i < 0)
i = FindItem("Close");
if (i >= 0)
Send(aActivate, (long) i, (long) CurrentTime);
else if (client != None) {
/* no Exit entry: ask the window to close (WM_DELETE_WINDOW) */
Atom prot = XInternAtom(dpy, "WM_PROTOCOLS", False);
Atom del = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
XEvent ev;
memset(&ev, 0, sizeof ev);
ev.xclient.type = ClientMessage;
ev.xclient.window = client;
ev.xclient.message_type = prot;
ev.xclient.format = 32;
ev.xclient.data.l[0] = (long) del;
ev.xclient.data.l[1] = (long) CurrentTime;
XSendEvent(dpy, client, False, NoEventMask, &ev);
XFlush(dpy);
}
}
/* the cascade that goes before the program's own menus */
static Widget
MakeAppMenu(void)
{
Widget menu = Pulldown(bar, "appMenu");
Widget item;
char label[160];
ClientName(appName, sizeof appName);
if (appName[0] == '\0')
(void)snprintf(appName, sizeof appName, "Program");
(void)snprintf(label, sizeof label, "About %s", appName);
Push(menu, label, 'A', AppAbout, NULL);
Separator(menu);
item = Push(menu, "Preferences...", 'P', AppPreferences, NULL);
SetAccel(item, "Cmd+,");
Separator(menu);
(void)snprintf(label, sizeof label, "Hide %s", appName);
item = Push(menu, label, 'H', AppHide, NULL);
SetAccel(item, "Cmd+H");
item = Push(menu, "Hide Others", 'O', AppHide, (XtPointer) 1);
SetAccel(item, "Option+Cmd+H");
Push(menu, "Show All", 'S', AppShowAll, NULL);
Separator(menu);
(void)snprintf(label, sizeof label, "Quit %s", appName);
item = Push(menu, label, 'Q', AppQuit, NULL);
SetAccel(item, "Cmd+Q");
return Cascade(bar, appName, appName[0], menu);
}
/* the entries of items[from..to) of depth DEPTH into MENU */
static void
Build(Widget menu, int from, int to, int depth)
{
int i;
for (i = from; i < to; i = McdeMenuEnd(items, to, i)) {
McdeMenuItem *m = &items[i];
XmString s = XmStringCreateLocalized(m->label);
XmString acc = m->accelerator[0] != '\0' ?
XmStringCreateLocalized(m->accelerator) : NULL;
Widget w = NULL, sub;
Arg a[8];
int n = 0;
if (m->depth != depth)
continue;
XtSetArg(a[n], XmNlabelString, s); n++;
if (m->mnemonic != '\0') {
XtSetArg(a[n], XmNmnemonic, (KeySym) m->mnemonic); n++;
}
if (acc != NULL) {
XtSetArg(a[n], XmNacceleratorText, acc); n++;
}
switch (m->kind) {
case MCDE_MENU_CASCADE:
sub = Pulldown(menu, "appMenu");
if (menu == bar)
appWidgets[nappWidgets++] = sub;
Build(sub, i + 1, McdeMenuEnd(items, to, i), depth + 1);
XtSetArg(a[n], XmNsubMenuId, sub); n++;
w = XtCreateManagedWidget("appCascade",
xmCascadeButtonGadgetClass, menu, a, n);
break;
case MCDE_MENU_PUSH:
w = XtCreateManagedWidget("appItem",
xmPushButtonGadgetClass, menu, a, n);
XtAddCallback(w, XmNactivateCallback, ItemActivated,
(XtPointer) (size_t) i);
break;
case MCDE_MENU_TOGGLE:
XtSetArg(a[n], XmNset, (m->flags & MCDE_MENU_CHECKED) ?
XmSET : XmUNSET); n++;
XtSetArg(a[n], XmNindicatorType,
(m->flags & MCDE_MENU_RADIO) ? XmONE_OF_MANY :
XmN_OF_MANY); n++;
XtSetArg(a[n], XmNvisibleWhenOff, True); n++;
w = XtCreateManagedWidget("appItem",
xmToggleButtonGadgetClass, menu, a, n);
/* the state shown is the program's: it sets it
* itself, and the next _MCDE_MENU shows it */
XtAddCallback(w, XmNvalueChangedCallback, ItemActivated,
(XtPointer) (size_t) i);
break;
case MCDE_MENU_SEPARATOR:
w = XtCreateManagedWidget("sep",
xmSeparatorGadgetClass, menu, NULL, 0);
break;
default:
w = XtCreateManagedWidget("appLabel",
xmLabelGadgetClass, menu, a, n);
break;
}
if (w != NULL && !(m->flags & MCDE_MENU_SENSITIVE) &&
m->kind != MCDE_MENU_SEPARATOR)
XtSetSensitive(w, False);
if (w != NULL && menu == bar)
appWidgets[nappWidgets++] = w;
XmStringFree(s);
if (acc != NULL)
XmStringFree(acc);
}
}
static void
Rebuild(void)
{
int i;
if (debug)
fprintf(stderr, "dtbar: rebuild, %d items of 0x%lx\n", nitems,
(unsigned long) client);
for (i = 0; i < nappWidgets; i++)
XtDestroyWidget(appWidgets[i]);
nappWidgets = 0;
if (client != None && focused) {
Widget c = MakeAppMenu();
appWidgets[nappWidgets++] = c;
Build(bar, 0, nitems, 0);
}
}
/* the _MCDE_MENU of the client into items[] */
static void
ReadMenu(void)
{
Atom type;
int format;
unsigned long n, after;
unsigned char *data = NULL;
static char *shown; /* the text the bar shows now */
static Window shownFor;
static Boolean shownFocused;
const char *text = "";
if (posted > 0) {
pending = True;
return;
}
if (client != None &&
XGetWindowProperty(dpy, client, aMenu, 0, 1 << 20, False, aUtf8,
&type, &format, &n, &after, &data) == Success && data != NULL)
text = (const char *) data;
/* the same menus again (a refresh that changed nothing): an open menu
* must not be pulled away under the pointer */
if (shown != NULL && shownFor == client && shownFocused == focused &&
strcmp(shown, text) == 0) {
if (data != NULL)
XFree(data);
return;
}
free(shown);
shown = strdup(text);
shownFor = client;
shownFocused = focused;
nitems = McdeMenuParse(text, items, MCDE_MENU_MAX);
if (nitems < 0)
nitems = 0;
if (data != NULL)
XFree(data);
Rebuild();
}
/* a WINDOW property of W, or None */
static Window
WindowProperty(Window w, Atom name)
{
Atom type;
int format;
unsigned long n, after;
unsigned char *data = NULL;
Window v = None;
if (w != None && XGetWindowProperty(dpy, w, name, 0, 1, False,
XA_WINDOW, &type, &format, &n, &after, &data) == Success &&
data != NULL && n == 1 && format == 32)
v = (Window) *(long *) data;
if (data != NULL)
XFree(data);
return v;
}
/* is ACTIVE a window of the program whose menus the bar has: the window
* itself, one of its dialogs (WM_TRANSIENT_FOR, ICCCM 4.1.2.6), or another
* window of the same program (WM_CLIENT_LEADER, ICCCM 5.1) */
static Boolean
OfClient(Window active)
{
Window w, leader;
int depth;
if (client == None || active == None)
return False;
for (w = active, depth = 0; w != None && depth < 8; depth++) {
if (w == client)
return True;
w = WindowProperty(w, XA_WM_TRANSIENT_FOR);
}
leader = WindowProperty(client, aLeader);
return leader != None && WindowProperty(active, aLeader) == leader;
}
/* the window manager's _NET_ACTIVE_WINDOW (EWMH): a program that does not
* give its menus to the bar has the focus -- the bar shows none, not the
* menus of the last program that did. Without the property (a window
* manager that does not set it) the bar keeps the last menus, as before. */
static void
UpdateFocused(void)
{
Atom type;
int format;
unsigned long n, after;
unsigned char *data = NULL;
if (XGetWindowProperty(dpy, DefaultRootWindow(dpy), aNetActive, 0, 1,
False, XA_WINDOW, &type, &format, &n, &after, &data) == Success &&
data != NULL && n == 1 && format == 32)
focused = OfClient((Window) *(long *) data);
else
focused = True;
if (data != NULL)
XFree(data);
}
static void
Follow(Window w)
{
if (w == client)
return;
if (client != None)
XtUnregisterDrawable(dpy, client);
client = w;
if (client != None) {
XtRegisterDrawable(dpy, client, sink);
XSelectInput(dpy, client, PropertyChangeMask | StructureNotifyMask);
}
UpdateFocused();
ReadMenu();
}
/* _MCDE_ACTIVE_MENU of the root */
static void
ReadActive(void)
{
Atom type;
int format;
unsigned long n, after;
unsigned char *data = NULL;
Window w = None;
if (XGetWindowProperty(dpy, DefaultRootWindow(dpy), aActive, 0, 1,
False, XA_WINDOW, &type, &format, &n, &after, &data) == Success &&
data != NULL && n == 1 && format == 32)
w = (Window) *(long *) data;
if (data != NULL)
XFree(data);
Follow(w);
}
/* ---- dtbar -command NAME: the desktop's keys (dtwm's sys.dtwmrc) ------- */
/* "log out now": dtsession's protocol message _MCDE_EXIT_SESSION_NOW
* (programs/dtsession/SmCommun.c), sent to its window as dtwm sends
* _DT_WM_EXIT_SESSION */
static void
LogOutNow(void)
{
Window sm;
XEvent ev;
if (_DtGetSmWindow(dpy, DefaultRootWindow(dpy), &sm) != Success)
return;
memset(&ev, 0, sizeof ev);
ev.xclient.type = ClientMessage;
ev.xclient.window = sm;
ev.xclient.message_type = XInternAtom(dpy, _XA_DT_SM_WM_PROTOCOL,
False);
ev.xclient.format = 32;
ev.xclient.data.l[0] = (long) XInternAtom(dpy,
"_MCDE_EXIT_SESSION_NOW", False);
ev.xclient.data.l[1] = CurrentTime;
XSendEvent(dpy, sm, False, NoEventMask, &ev);
XFlush(dpy);
}
static void
PowerChosen(Widget w, XtPointer data, XtPointer call)
{
XmAnyCallbackStruct *cbs = (XmAnyCallbackStruct *) call;
(void) data;
XtDestroyWidget(w);
XFlush(dpy);
if (cbs->reason == XmCR_CANCEL)
return;
if (mcde_sys_power(cbs->reason == XmCR_HELP ? MCDE_POWER_RESTART :
MCDE_POWER_SHUTDOWN) != MCDE_SYS_OK)
ShowMessage("Power", mcde_sys_error(), 1);
}
/* Ctrl+Eject: one question for restart and shut down (the help button is
* Restart) */
static void
AskShutDown(void)
{
XmString m = XmStringCreateLocalized("Are you sure you want to shut "
"down your computer now?");
XmString ok = XmStringCreateLocalized("Shut Down");
XmString re = XmStringCreateLocalized("Restart");
XmString t = XmStringCreateLocalized("Shut Down");
Arg a[5];
int n = 0;
Widget d;
XtSetArg(a[n], XmNmessageString, m); n++;
XtSetArg(a[n], XmNokLabelString, ok); n++;
XtSetArg(a[n], XmNhelpLabelString, re); n++;
XtSetArg(a[n], XmNdialogTitle, t); n++;
XtSetArg(a[n], XmNdefaultPosition, False); n++;
d = XmCreateQuestionDialog(barShell, "shutDown", a, n);
XtAddCallback(d, XmNokCallback, PowerChosen, NULL);
XtAddCallback(d, XmNhelpCallback, PowerChosen, NULL);
XtAddCallback(d, XmNcancelCallback, PowerChosen, NULL);
XtManageChild(d);
Centre(d);
XmStringFree(m);
XmStringFree(ok);
XmStringFree(re);
XmStringFree(t);
}
static void
Command(const char *name)
{
if (debug)
fprintf(stderr, "dtbar: command %s\n", name);
if (strcmp(name, "forcequit") == 0)
ShowForceQuit();
else if (strcmp(name, "logout") == 0)
Run("ExitSession");
else if (strcmp(name, "logout-now") == 0)
LogOutNow();
else if (strcmp(name, "power") == 0)
AskShutDown();
else if (strcmp(name, "showall") == 0)
AppShowAll(NULL, NULL, NULL);
else if (strcmp(name, "menu") == 0) {
/* Ctrl+F2: the keyboard in the menu bar. The focus goes to the
* bar's shell and the M menu opens under its button, so that the
* arrows and Return work at once. XmProcessTraversal alone was
* not enough (measured 2026-11-15): the bar carries the client's
* exported menu bar as well, Down and Return did nothing. */
XRaiseWindow(dpy, XtWindow(shell));
XSetInputFocus(dpy, XtWindow(shell), RevertToPointerRoot,
CurrentTime);
if (mcdeMenuButton != NULL)
XmProcessTraversal(mcdeMenuButton, XmTRAVERSE_CURRENT);
else
XmProcessTraversal(bar, XmTRAVERSE_HOME);
XFlush(dpy);
}
}
static void
Events(Widget w, XtPointer data, XEvent *ev, Boolean *cont)
{
(void) w;
(void) data;
(void) cont;
switch (ev->type) {
case PropertyNotify:
if (ev->xproperty.window == DefaultRootWindow(dpy) &&
ev->xproperty.atom == aActive)
ReadActive();
else if (ev->xproperty.window == DefaultRootWindow(dpy) &&
ev->xproperty.atom == aNetActive) {
UpdateFocused();
ReadMenu();
}
else if (ev->xproperty.window == client &&
ev->xproperty.atom == aMenu)
ReadMenu();
break;
case DestroyNotify:
if (ev->xdestroywindow.window == client)
Follow(None);
break;
case ClientMessage:
if (ev->xclient.message_type == aCommand &&
ev->xclient.format == 8) {
char cmd[sizeof ev->xclient.data.b + 1];
memcpy(cmd, ev->xclient.data.b, sizeof ev->xclient.data.b);
cmd[sizeof cmd - 1] = '\0';
Command(cmd);
}
break;
case EnterNotify:
/* the program brings its menus up to date before one opens */
if (ev->xcrossing.window == XtWindow(shell))
Send(aRefresh, 0, 0);
/* The bar is no focus target (t0255, t0257): moving the
* pointer onto it must not take the keyboard away from the
* program whose menu the bar shows. The bar says so on its
* own window (_MCDE_NO_FOCUS) and leaves the focus alone; its
* menus open with the pointer, and an item acts on the client
* the menu came from, not on whatever has the X focus. */
break;
case MapNotify:
/* tell dtwm not to give the keyboard to the bar (see
* EnterNotify): focus follows the pointer elsewhere, and the
* bar keeps the last program's menu */
if (ev->xmap.window == XtWindow(shell)) {
Atom a = XInternAtom(dpy, "_MCDE_NO_FOCUS", False);
long one = 1;
XChangeProperty(dpy, XtWindow(shell), a, XA_CARDINAL,
32, PropModeReplace, (unsigned char *) &one, 1);
}
break;
case VisibilityNotify:
if (ev->xvisibility.window == XtWindow(shell) &&
ev->xvisibility.state != VisibilityUnobscured)
XRaiseWindow(dpy, XtWindow(shell));
break;
}
}
/* ---- the clock ---------------------------------------------------------- */
/* ~/.dt/clock, written by the Clock tab of dtsystemsettings: "show=0|1"
* and "format=" with the strftime format of the bar's clock */
static void
ClockSettings(char *format, size_t len, Boolean *show)
{
char path[512], line[256];
const char *home = getenv("HOME");
FILE *fp;
(void)snprintf(format, len, "%%a %%e %%b %%H:%%M");
*show = True;
if (home == NULL || (size_t) snprintf(path, sizeof path, "%s/.dt/clock",
home) >= sizeof path)
return;
if ((fp = fopen(path, "r")) == NULL)
return;
while (fgets(line, sizeof line, fp) != NULL) {
line[strcspn(line, "\r\n")] = '\0';
if (strncmp(line, "show=", 5) == 0)
*show = atoi(line + 5) != 0;
else if (strncmp(line, "format=", 7) == 0 && line[7] != '\0')
(void)snprintf(format, len, "%s", line + 7);
}
fclose(fp);
}
static void
Tick(XtPointer data, XtIntervalId *id)
{
XtAppContext app = (XtAppContext) data;
char buf[128], format[128];
time_t now = time(NULL);
struct tm tm;
XmString s;
Boolean show;
(void) id;
ClockSettings(format, sizeof format, &show);
if (!show) {
/* a gadget has no window to unmap: an empty label, and the
* file may be switched on again (looked at once a minute) */
XmString e = XmStringCreateLocalized((char *) "");
XtVaSetValues(clockLabel, XmNlabelString, e, NULL);
XmStringFree(e);
localtime_r(&now, &tm);
XtAppAddTimeOut(app, (unsigned long) (60 - tm.tm_sec) * 1000 +
50, Tick, data);
return;
}
localtime_r(&now, &tm);
strftime(buf, sizeof buf, format, &tm);
s = XmStringCreateLocalized(buf);
XtVaSetValues(clockLabel, XmNlabelString, s, NULL);
XmStringFree(s);
/* the next full minute */
XtAppAddTimeOut(app, (unsigned long) (60 - tm.tm_sec) * 1000 + 50,
Tick, data);
}
int
main(int argc, char **argv)
{
XtAppContext app;
char name[32];
const char *command = NULL;
Window owner, root;
XEvent ev;
int width, i;
XtSetLanguageProc(NULL, NULL, NULL);
shell = XtVaAppInitialize(&app, "Dtbar", NULL, 0, &argc, argv,
(String *) fallback, XmNoverrideRedirect, True, NULL);
dpy = XtDisplay(shell);
barShell = shell;
XSetErrorHandler(IgnoreErrors);
debug = getenv("MCDE_GLOBAL_MENU_DEBUG") != NULL;
snprintf(name, sizeof name, "_MCDE_BAR_S%d", DefaultScreen(dpy));
aBar = XInternAtom(dpy, name, False);
aMenu = XInternAtom(dpy, "_MCDE_MENU", False);
aActive = XInternAtom(dpy, "_MCDE_ACTIVE_MENU", False);
aActivate = XInternAtom(dpy, "_MCDE_MENU_ACTIVATE", False);
aRefresh = XInternAtom(dpy, "_MCDE_MENU_REFRESH", False);
aUtf8 = XInternAtom(dpy, "UTF8_STRING", False);
aManager = XInternAtom(dpy, "MANAGER", False);
aNetActive = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
aLeader = XInternAtom(dpy, "WM_CLIENT_LEADER", False);
aCommand = XInternAtom(dpy, "_MCDE_BAR_COMMAND", False);
for (i = 1; i < argc; i++)
if (strcmp(argv[i], "-command") == 0 && i + 1 < argc)
command = argv[++i];
owner = XGetSelectionOwner(dpy, aBar);
/* dtbar -command NAME (dtwm's keys): NAME to the running bar, as a
* ClientMessage _MCDE_BAR_COMMAND to the owner of _MCDE_BAR_S<n> */
if (command != NULL) {
if (owner == None || strlen(command) >= 20) {
fprintf(stderr, "dtbar: no bar runs for -command %s\n",
command);
return 1;
}
memset(&ev, 0, sizeof ev);
ev.xclient.type = ClientMessage;
ev.xclient.window = owner;
ev.xclient.message_type = aCommand;
ev.xclient.format = 8;
memcpy(ev.xclient.data.b, command, strlen(command));
XSendEvent(dpy, owner, False, NoEventMask, &ev);
XFlush(dpy);
return 0;
}
if (owner != None) {
fprintf(stderr, "dtbar: a bar runs on this screen already\n");
return 0;
}
width = DisplayWidth(dpy, DefaultScreen(dpy));
form = XtVaCreateManagedWidget("form", xmFormWidgetClass, shell,
XmNwidth, width, XmNshadowThickness, 1, NULL);
bar = XmCreateMenuBar(form, "mbar", NULL, 0);
XtVaSetValues(bar, XmNleftAttachment, XmATTACH_FORM,
XmNtopAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM, NULL);
clockLabel = XtVaCreateManagedWidget("clock", xmLabelGadgetClass, form,
XmNrightAttachment, XmATTACH_FORM, XmNtopAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM, NULL);
{
Widget status = MakeStatus(form);
XtVaSetValues(status, XmNrightAttachment, XmATTACH_WIDGET,
XmNrightWidget, clockLabel, XmNtopAttachment,
XmATTACH_FORM, XmNbottomAttachment, XmATTACH_FORM, NULL);
XtVaSetValues(bar, XmNrightAttachment, XmATTACH_WIDGET,
XmNrightWidget, status, NULL);
}
MakeMMenu();
XtManageChild(bar);
Tick((XtPointer) app, NULL);
/* the whole width, the height the menu bar wants; an override-redirect
* shell is not sized by a window manager, so it is sized here */
{
XtWidgetGeometry pref;
Dimension h;
memset(&pref, 0, sizeof pref);
(void) XtQueryGeometry(form, NULL, &pref);
h = (pref.request_mode & CWHeight) && pref.height > 0 ?
pref.height : 24;
XtVaSetValues(shell, XmNx, 0, XmNy, 0, XmNwidth, width,
XmNheight, h, NULL);
}
XtRealizeWidget(shell);
/* the bar of this screen; programs find it by the selection, and one
* that started before learns of it from MANAGER (ICCCM 2.8) */
root = DefaultRootWindow(dpy);
XSetSelectionOwner(dpy, aBar, XtWindow(shell), CurrentTime);
if (XGetSelectionOwner(dpy, aBar) != XtWindow(shell)) {
fprintf(stderr, "dtbar: cannot own %s\n", name);
return 1;
}
memset(&ev, 0, sizeof ev);
ev.xclient.type = ClientMessage;
ev.xclient.window = root;
ev.xclient.message_type = aManager;
ev.xclient.format = 32;
ev.xclient.data.l[0] = CurrentTime;
ev.xclient.data.l[1] = (long) aBar;
ev.xclient.data.l[2] = (long) XtWindow(shell);
XSendEvent(dpy, root, False, StructureNotifyMask, &ev);
/* the root and the client's window are registered with a widget, so
* their events come to Events; their masks are selected by hand (raw) */
/* not the shell: it stops on a StructureNotify of another window
* ("Event with wrong window", Shell.c) */
sink = XtVaCreateWidget("sink", widgetClass, XtCreatePopupShell(
"sinkShell", overrideShellWidgetClass, shell, NULL, 0),
XmNwidth, 1, XmNheight, 1, NULL);
XtRegisterDrawable(dpy, root, sink);
XSelectInput(dpy, root, PropertyChangeMask);
XtAddRawEventHandler(sink, PropertyChangeMask | StructureNotifyMask,
False, Events, NULL);
XtAddEventHandler(shell, EnterWindowMask | VisibilityChangeMask, True,
Events, NULL);
ReadActive();
StartStatus(app);
XtAppMainLoop(app);
return 0;
}