/* * Dropdown.c -- MCDE: a terminal that drops down from the top of the screen * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * dtterm -dropdown: the window is as wide as the screen, 40 % of its height, * and hidden. A key (the resource dropdownKey, F12 by default) brings it * down from the top edge to just below the top bar and gives it the * keyboard; the same key sends it back up. The key is grabbed on the root * window (XGrabKey(3)), so it works whatever program has the focus; the * window is override-redirect, so the window manager neither places nor * decorates it. The key reaches dtterm through its own event dispatcher * (XtSetEventDispatcher), not through a widget. * * C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md). */ #include #include #include #include #include #include #include #include "Dropdown.h" #define STEPS 8 /* the slide, in this many moves */ #define STEP_MS 12 static Widget shell; static Boolean shown; static int height, top, step; static XtIntervalId slide; static KeyCode code; /* the bottom edge of the top bar, if one runs (_MCDE_BAR_S) */ static int BarBottom(Display *dpy) { char name[32]; Window bar; XWindowAttributes a; snprintf(name, sizeof name, "_MCDE_BAR_S%d", DefaultScreen(dpy)); bar = XGetSelectionOwner(dpy, XInternAtom(dpy, name, False)); if (bar == None || !XGetWindowAttributes(dpy, bar, &a) || a.map_state != IsViewable) return 0; return a.y + a.height; } static void Place(int y) { XMoveWindow(XtDisplay(shell), XtWindow(shell), 0, y); } static void Slide(XtPointer data, XtIntervalId *id) { int y; (void) data; (void) id; slide = 0; step++; /* from -height to top when it comes, back up when it goes */ if (shown) y = -height + (height + top) * step / STEPS; else y = top - (height + top) * step / STEPS; Place(y); XFlush(XtDisplay(shell)); if (step < STEPS) slide = XtAppAddTimeOut(XtWidgetToApplicationContext(shell), STEP_MS, Slide, NULL); else if (!shown) XUnmapWindow(XtDisplay(shell), XtWindow(shell)); else { Widget term = XtNameToWidget(shell, "*dtTerm"); XSetInputFocus(XtDisplay(shell), XtWindow(shell), RevertToPointerRoot, CurrentTime); if (term != NULL) (void) XmProcessTraversal(term, XmTRAVERSE_CURRENT); } } static void Toggle(Time t) { Display *dpy = XtDisplay(shell); (void) t; if (slide != 0) { XtRemoveTimeOut(slide); slide = 0; } shown = !shown; step = 0; if (shown) { top = BarBottom(dpy); Place(-height); XMapRaised(dpy, XtWindow(shell)); } Slide(NULL, NULL); } static XtEventDispatchProc nextDispatcher; /* XtSetEventDispatcher(3): the grabbed key on the root window is taken here, * every other KeyPress goes on to Xt's own dispatcher. Xt would route a * key event through the keyboard focus of a shell, and the root window has * none; registering it with a widget did not bring the key (measured). */ static Boolean DispatchKey(XEvent *ev) { if (getenv("MCDE_DROPDOWN_DEBUG")) fprintf(stderr, "dropdown: KeyPress window 0x%lx keycode %u\n", (unsigned long) ev->xkey.window, ev->xkey.keycode); if (ev->type == KeyPress && ev->xkey.keycode == code && ev->xkey.window == DefaultRootWindow(ev->xkey.display)) { if (getenv("MCDE_DROPDOWN_DEBUG")) fprintf(stderr, "dropdown: key\n"); Toggle(ev->xkey.time); return True; } return (*nextDispatcher)(ev); } /* the key with every combination of the locks, so that NumLock or CapsLock * on does not take it away */ static int grabFailed; static int GrabError(Display *dpy, XErrorEvent *e) { (void) dpy; grabFailed = e->error_code; return 0; } static void Grab(Display *dpy, Window root) { static const unsigned int locks[] = { 0, LockMask, Mod2Mask, LockMask | Mod2Mask }; size_t i; XErrorHandler old; XSync(dpy, False); old = XSetErrorHandler(GrabError); for (i = 0; i < sizeof locks / sizeof locks[0]; i++) XGrabKey(dpy, code, locks[i], root, True, GrabModeAsync, GrabModeAsync); XSync(dpy, False); XSetErrorHandler(old); if (grabFailed) fprintf(stderr, "dtterm: the drop-down key is taken by another " "program (X error %d)\n", grabFailed); } void _McdeDropdownPrepare(Widget topShell) { Screen *scr = XtScreen(topShell); shell = topShell; height = HeightOfScreen(scr) * 2 / 5; XtVaSetValues(shell, XmNoverrideRedirect, True, XmNx, 0, XmNy, -height, XmNwidth, WidthOfScreen(scr), XmNheight, height, NULL); } int _McdeDropdownStart(Widget topShell, const char *key) { Display *dpy = XtDisplay(topShell); Window root = DefaultRootWindow(dpy); KeySym ks = XStringToKeysym(key != NULL && key[0] != '\0' ? key : "F12"); shell = topShell; if (ks == NoSymbol || (code = XKeysymToKeycode(dpy, ks)) == 0) { fprintf(stderr, "dtterm: no key \"%s\" for the drop-down\n", key); return 0; } nextDispatcher = XtSetEventDispatcher(dpy, KeyPress, DispatchKey); Grab(dpy, root); if (getenv("MCDE_DROPDOWN_DEBUG")) fprintf(stderr, "dropdown: grabbed keycode %u on 0x%lx\n", code, (unsigned long) root); return 1; }