MakronixDtlogo.t.pm: the M with its star, drawn by hand -- the 240-pixel logo scaled to 16 was a dark square. dtbar gives it when the button is made (so Motif sizes it) and keeps the letter M where the icon is missing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
883 lines
23 KiB
C
883 lines
23 KiB
C
/*
|
|
* dtbar.c -- MCDE: the bar at the top of the screen
|
|
*
|
|
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
|
|
*
|
|
* 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 <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 <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 <Mcde/GlobalMenu.h>
|
|
#include <Mcde/McdeSys.h>
|
|
|
|
#include "dtbar.h"
|
|
#include "items.h"
|
|
|
|
static Display *dpy;
|
|
static Widget shell, form, bar, clockLabel;
|
|
static Widget sink; /* the root's and the client's events go here */
|
|
static Atom aBar, aMenu, aActive, aActivate, aRefresh, aUtf8, aManager;
|
|
static Window client; /* the window whose menus are shown */
|
|
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",
|
|
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();
|
|
}
|
|
|
|
/* 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);
|
|
}
|
|
|
|
static void
|
|
Open(const Item *it)
|
|
{
|
|
char file[1024];
|
|
int n;
|
|
|
|
RunCommand(it->command);
|
|
DtFile("recent", file, sizeof file);
|
|
n = ItemsRead(file, recent, ITEMS_MAX);
|
|
n = ItemsPush(recent, n, 10, 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\tdtterm\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);
|
|
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...");
|
|
}
|
|
|
|
static void
|
|
MakeMMenu(void)
|
|
{
|
|
Widget menu = Pulldown(bar, "mcdeMMenu");
|
|
char logout[200];
|
|
|
|
Push(menu, "About This Computer", 'A', About, NULL);
|
|
Push(menu, "Software Update...", 'U', NULL, NULL);
|
|
Separator(menu);
|
|
Push(menu, "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, "Favourites", 'F', favouritesMenu);
|
|
recentMenu = Pulldown(menu, "recent");
|
|
(void) Cascade(menu, "Recent Items", 'R', recentMenu);
|
|
XtAddCallback(menu, XmNmapCallback, FillFavourites, NULL);
|
|
XtAddCallback(menu, XmNmapCallback, FillRecent, NULL);
|
|
Separator(menu);
|
|
Push(menu, "Force Quit...", 'Q', ForceQuit, NULL);
|
|
Separator(menu);
|
|
Push(menu, "Sleep", 'l', DoPower, (XtPointer) (size_t) MCDE_POWER_SLEEP);
|
|
Push(menu, "Restart...", 't', AskPower,
|
|
(XtPointer) (size_t) MCDE_POWER_RESTART);
|
|
Push(menu, "Shut Down...", 'D', AskPower,
|
|
(XtPointer) (size_t) MCDE_POWER_SHUTDOWN);
|
|
Separator(menu);
|
|
Push(menu, "Lock Screen", 'k', MAction, "LockDisplay");
|
|
LogOutLabel(logout, sizeof logout);
|
|
Push(menu, logout, 'O', MAction, "ExitSession");
|
|
/* 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)
|
|
XtVaCreateManagedWidget("mcdeM", xmCascadeButtonGadgetClass,
|
|
bar, XmNsubMenuId, menu, XmNlabelType, XmPIXMAP,
|
|
XmNlabelPixmap, pm, NULL);
|
|
else
|
|
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 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)
|
|
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;
|
|
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 && strcmp(shown, text) == 0) {
|
|
if (data != NULL)
|
|
XFree(data);
|
|
return;
|
|
}
|
|
free(shown);
|
|
shown = strdup(text);
|
|
shownFor = client;
|
|
nitems = McdeMenuParse(text, items, MCDE_MENU_MAX);
|
|
if (nitems < 0)
|
|
nitems = 0;
|
|
if (data != NULL)
|
|
XFree(data);
|
|
Rebuild();
|
|
}
|
|
|
|
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);
|
|
}
|
|
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);
|
|
}
|
|
|
|
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 == client &&
|
|
ev->xproperty.atom == aMenu)
|
|
ReadMenu();
|
|
break;
|
|
case DestroyNotify:
|
|
if (ev->xdestroywindow.window == client)
|
|
Follow(None);
|
|
break;
|
|
case EnterNotify:
|
|
/* the program brings its menus up to date before one opens */
|
|
if (ev->xcrossing.window == XtWindow(shell))
|
|
Send(aRefresh, 0, 0);
|
|
break;
|
|
case VisibilityNotify:
|
|
if (ev->xvisibility.window == XtWindow(shell) &&
|
|
ev->xvisibility.state != VisibilityUnobscured)
|
|
XRaiseWindow(dpy, XtWindow(shell));
|
|
break;
|
|
}
|
|
}
|
|
|
|
/* ---- the clock ---------------------------------------------------------- */
|
|
|
|
static void
|
|
Tick(XtPointer data, XtIntervalId *id)
|
|
{
|
|
XtAppContext app = (XtAppContext) data;
|
|
char buf[64];
|
|
time_t now = time(NULL);
|
|
struct tm tm;
|
|
XmString s;
|
|
|
|
(void) id;
|
|
localtime_r(&now, &tm);
|
|
strftime(buf, sizeof buf, "%a %e %b %H:%M", &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];
|
|
Window owner, root;
|
|
XEvent ev;
|
|
int width;
|
|
|
|
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);
|
|
|
|
owner = XGetSelectionOwner(dpy, aBar);
|
|
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, False,
|
|
Events, NULL);
|
|
ReadActive();
|
|
StartStatus(app);
|
|
XtAppMainLoop(app);
|
|
return 0;
|
|
}
|