2011-05-14 10:47:12 -06:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2021-05-15 22:41:18 -06:00
|
|
|
|
|
|
|
/* I'm not a very good C programmer and this isn't the best I can do.
|
|
|
|
* There are a lot of fragments here from Suckless' dmenu. I would
|
|
|
|
* recommend hacking on that instead. This is itself a total hack and
|
|
|
|
* not a pretty one, but it works and I'm at least proud of that.
|
|
|
|
* ~ trinity */
|
|
|
|
|
2006-08-04 01:35:27 -06:00
|
|
|
#include <ctype.h>
|
2015-05-04 13:54:46 -06:00
|
|
|
#include <locale.h>
|
2006-08-04 01:35:27 -06:00
|
|
|
#include <stdio.h>
|
2008-06-13 04:46:50 -06:00
|
|
|
#include <stdlib.h>
|
2006-08-04 01:35:27 -06:00
|
|
|
#include <string.h>
|
2011-05-15 14:54:26 -06:00
|
|
|
#include <strings.h>
|
2015-09-27 16:06:05 -06:00
|
|
|
#include <time.h>
|
2018-05-25 05:03:25 -06:00
|
|
|
#include <unistd.h>
|
2015-09-27 16:38:17 -06:00
|
|
|
|
2007-09-16 12:14:09 -06:00
|
|
|
#include <X11/Xlib.h>
|
2010-08-10 06:38:49 -06:00
|
|
|
#include <X11/Xatom.h>
|
2006-08-04 01:35:27 -06:00
|
|
|
#include <X11/Xutil.h>
|
2010-07-30 06:40:56 -06:00
|
|
|
#ifdef XINERAMA
|
|
|
|
#include <X11/extensions/Xinerama.h>
|
|
|
|
#endif
|
2015-05-04 13:54:46 -06:00
|
|
|
#include <X11/Xft/Xft.h>
|
2010-07-30 06:40:56 -06:00
|
|
|
|
2015-05-04 13:54:46 -06:00
|
|
|
#include "drw.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2021-05-16 07:59:02 -06:00
|
|
|
#define BREAK_ON_EOF 0
|
|
|
|
|
2015-05-04 13:54:46 -06:00
|
|
|
/* macros */
|
2011-10-26 05:14:50 -06:00
|
|
|
#define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \
|
|
|
|
* MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org)))
|
2015-05-04 13:54:46 -06:00
|
|
|
#define LENGTH(X) (sizeof X / sizeof X[0])
|
2016-05-21 13:51:14 -06:00
|
|
|
#define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
|
2015-05-04 13:54:46 -06:00
|
|
|
|
|
|
|
/* enums */
|
2021-05-15 22:24:38 -06:00
|
|
|
enum { Scheme, SchemeLast }; /* color schemes */
|
2021-05-15 22:41:18 -06:00
|
|
|
/* enumerated because Suckless has SchemeNorm, SchemeSel, etc.
|
|
|
|
* should be made into like one char *Scheme[2] */
|
|
|
|
/* BTW SchemeLast is the size of an array of the schemes and it's used
|
|
|
|
* in this program for iteration. Assumes enums will be assigned
|
|
|
|
* 0,1,2,3... */
|
2007-01-11 07:52:37 -07:00
|
|
|
|
2016-10-08 06:08:28 -06:00
|
|
|
static char *embed;
|
2010-08-18 10:33:34 -06:00
|
|
|
static int bh, mw, mh;
|
2021-05-15 22:24:38 -06:00
|
|
|
static int inputw = 0;
|
2016-05-21 13:51:14 -06:00
|
|
|
static int lrpad; /* sum of left and right padding */
|
2015-09-27 15:57:39 -06:00
|
|
|
static int mon = -1, screen;
|
|
|
|
|
|
|
|
static Atom clip, utf8;
|
|
|
|
static Display *dpy;
|
2016-10-08 06:08:28 -06:00
|
|
|
static Window root, parentwin, win;
|
2011-10-16 10:21:33 -06:00
|
|
|
static XIC xic;
|
2010-07-30 06:40:56 -06:00
|
|
|
|
2015-05-04 13:54:46 -06:00
|
|
|
static Drw *drw;
|
2016-05-21 13:51:14 -06:00
|
|
|
static Clr *scheme[SchemeLast];
|
2015-05-04 13:54:46 -06:00
|
|
|
|
2013-04-17 13:04:05 -06:00
|
|
|
#include "config.h"
|
|
|
|
|
2010-06-16 08:36:17 -06:00
|
|
|
static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
|
2011-07-14 13:03:08 -06:00
|
|
|
static char *(*fstrstr)(const char *, const char *) = strstr;
|
2007-09-17 12:53:14 -06:00
|
|
|
|
2015-09-27 15:57:39 -06:00
|
|
|
static void
|
|
|
|
cleanup(void)
|
|
|
|
{
|
2015-10-18 10:37:20 -06:00
|
|
|
size_t i;
|
|
|
|
|
2016-05-21 13:51:14 -06:00
|
|
|
for (i = 0; i < SchemeLast; i++)
|
|
|
|
free(scheme[i]);
|
2015-05-04 13:54:46 -06:00
|
|
|
drw_free(drw);
|
|
|
|
XSync(dpy, False);
|
|
|
|
XCloseDisplay(dpy);
|
|
|
|
}
|
|
|
|
|
2015-09-27 15:57:39 -06:00
|
|
|
static void
|
2021-05-15 22:24:38 -06:00
|
|
|
drawmenu(char *text)
|
2015-09-27 15:57:39 -06:00
|
|
|
{
|
2016-07-26 15:02:34 -06:00
|
|
|
int x = 0, y = 0, w;
|
2010-08-03 10:10:29 -06:00
|
|
|
|
2021-05-15 22:24:38 -06:00
|
|
|
drw_setscheme(drw, scheme[Scheme]);
|
2016-05-21 13:51:14 -06:00
|
|
|
drw_rect(drw, 0, 0, mw, mh, 1, 1);
|
2010-08-09 04:54:46 -06:00
|
|
|
|
2021-05-15 22:24:38 -06:00
|
|
|
if (text && *text)
|
|
|
|
x = drw_text(drw, x, 0, textw(text), bh, lrpad / 2, text, 0);
|
2021-05-11 12:48:58 -06:00
|
|
|
|
2015-05-04 13:54:46 -06:00
|
|
|
drw_map(drw, win, 0, 0, mw, mh);
|
2010-08-11 07:24:25 -06:00
|
|
|
}
|
|
|
|
|
2021-05-15 22:24:38 -06:00
|
|
|
int
|
2021-05-15 22:41:18 -06:00
|
|
|
textw(char *text) /* figure out how to remove me */
|
2021-05-15 22:24:38 -06:00
|
|
|
{
|
|
|
|
return (text && *text) ? TEXTW(text) - lrpad / 4 : 0;
|
|
|
|
}
|
|
|
|
|
2021-05-15 22:41:18 -06:00
|
|
|
/* I don't know how anyting here works */
|
2015-09-27 15:57:39 -06:00
|
|
|
static void
|
|
|
|
setup(void)
|
|
|
|
{
|
2018-01-04 15:45:49 -07:00
|
|
|
int x, y, i, j;
|
2016-11-25 05:38:09 -07:00
|
|
|
unsigned int du;
|
2011-09-19 11:15:03 -06:00
|
|
|
XSetWindowAttributes swa;
|
2011-10-16 10:21:33 -06:00
|
|
|
XIM xim;
|
2016-11-25 05:38:09 -07:00
|
|
|
Window w, dw, *dws;
|
|
|
|
XWindowAttributes wa;
|
2021-05-16 07:59:02 -06:00
|
|
|
XClassHint ch = {"dmenubar", "dmenubar"};
|
2010-07-31 07:56:27 -06:00
|
|
|
#ifdef XINERAMA
|
|
|
|
XineramaScreenInfo *info;
|
2016-11-25 05:38:09 -07:00
|
|
|
Window pw;
|
2018-01-04 05:27:37 -07:00
|
|
|
int a, di, n, area = 0;
|
2010-07-30 06:40:56 -06:00
|
|
|
#endif
|
2015-05-04 13:54:46 -06:00
|
|
|
/* init appearance */
|
2017-11-03 14:05:29 -06:00
|
|
|
for (j = 0; j < SchemeLast; j++)
|
|
|
|
scheme[j] = drw_scm_create(drw, colors[j], 2);
|
2010-07-30 06:40:56 -06:00
|
|
|
|
2015-05-04 13:54:46 -06:00
|
|
|
clip = XInternAtom(dpy, "CLIPBOARD", False);
|
|
|
|
utf8 = XInternAtom(dpy, "UTF8_STRING", False);
|
2011-05-15 07:13:31 -06:00
|
|
|
|
2011-10-26 06:20:14 -06:00
|
|
|
/* calculate menu geometry */
|
2016-05-21 13:51:14 -06:00
|
|
|
bh = drw->fonts->h + 2;
|
2021-05-11 12:02:27 -06:00
|
|
|
mh = bh;
|
2010-07-31 07:56:27 -06:00
|
|
|
#ifdef XINERAMA
|
2018-01-04 15:45:49 -07:00
|
|
|
i = 0;
|
2016-10-08 06:08:28 -06:00
|
|
|
if (parentwin == root && (info = XineramaQueryScreens(dpy, &n))) {
|
2015-05-04 13:54:46 -06:00
|
|
|
XGetInputFocus(dpy, &w, &di);
|
2016-07-26 15:13:06 -06:00
|
|
|
if (mon >= 0 && mon < n)
|
2013-08-02 14:30:20 -06:00
|
|
|
i = mon;
|
2015-11-07 04:43:00 -07:00
|
|
|
else if (w != root && w != PointerRoot && w != None) {
|
2011-10-26 06:20:14 -06:00
|
|
|
/* find top-level window containing current input focus */
|
2011-10-26 05:14:50 -06:00
|
|
|
do {
|
2015-09-27 15:57:39 -06:00
|
|
|
if (XQueryTree(dpy, (pw = w), &dw, &w, &dws, &du) && dws)
|
2011-10-26 05:14:50 -06:00
|
|
|
XFree(dws);
|
2015-09-27 15:57:39 -06:00
|
|
|
} while (w != root && w != pw);
|
2011-10-26 06:20:14 -06:00
|
|
|
/* find xinerama screen with which the window intersects most */
|
2015-09-27 15:57:39 -06:00
|
|
|
if (XGetWindowAttributes(dpy, pw, &wa))
|
|
|
|
for (j = 0; j < n; j++)
|
|
|
|
if ((a = INTERSECT(wa.x, wa.y, wa.width, wa.height, info[j])) > area) {
|
2011-10-26 05:14:50 -06:00
|
|
|
area = a;
|
|
|
|
i = j;
|
|
|
|
}
|
|
|
|
}
|
2011-10-26 06:20:14 -06:00
|
|
|
/* no focused window is on screen, so use pointer location instead */
|
2016-07-26 15:13:06 -06:00
|
|
|
if (mon < 0 && !area && XQueryPointer(dpy, root, &dw, &dw, &x, &y, &di, &di, &du))
|
2015-09-27 15:57:39 -06:00
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
if (INTERSECT(x, y, 1, 1, info[i]))
|
2011-10-26 05:14:50 -06:00
|
|
|
break;
|
2011-10-26 06:20:14 -06:00
|
|
|
|
2011-05-12 06:17:41 -06:00
|
|
|
x = info[i].x_org;
|
|
|
|
y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
|
|
|
|
mw = info[i].width;
|
2010-07-30 06:40:56 -06:00
|
|
|
XFree(info);
|
2015-09-27 15:57:39 -06:00
|
|
|
} else
|
2010-07-30 06:40:56 -06:00
|
|
|
#endif
|
|
|
|
{
|
2016-10-08 06:08:28 -06:00
|
|
|
if (!XGetWindowAttributes(dpy, parentwin, &wa))
|
|
|
|
die("could not get embedding window attributes: 0x%lx",
|
|
|
|
parentwin);
|
2010-07-30 06:40:56 -06:00
|
|
|
x = 0;
|
2016-10-08 06:08:28 -06:00
|
|
|
y = topbar ? 0 : wa.height - mh;
|
|
|
|
mw = wa.width;
|
2010-07-30 06:40:56 -06:00
|
|
|
}
|
2011-05-18 09:20:03 -06:00
|
|
|
inputw = MIN(inputw, mw/3);
|
2011-05-15 07:21:00 -06:00
|
|
|
|
2011-10-26 06:20:14 -06:00
|
|
|
/* create menu window */
|
2011-09-19 11:15:03 -06:00
|
|
|
swa.override_redirect = True;
|
2021-05-15 22:24:38 -06:00
|
|
|
swa.background_pixel = scheme[Scheme][ColBg].pixel;
|
2011-09-19 11:15:03 -06:00
|
|
|
swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
|
2016-10-08 06:08:28 -06:00
|
|
|
win = XCreateWindow(dpy, parentwin, x, y, mw, mh, 0,
|
|
|
|
CopyFromParent, CopyFromParent, CopyFromParent,
|
2011-11-23 06:40:21 -07:00
|
|
|
CWOverrideRedirect | CWBackPixel | CWEventMask, &swa);
|
2017-11-03 10:49:10 -06:00
|
|
|
XSetClassHint(dpy, win, &ch);
|
2010-07-30 06:40:56 -06:00
|
|
|
|
2019-02-12 11:10:43 -07:00
|
|
|
|
|
|
|
/* input methods */
|
2019-03-03 05:08:54 -07:00
|
|
|
if ((xim = XOpenIM(dpy, NULL, NULL, NULL)) == NULL)
|
|
|
|
die("XOpenIM failed: could not open input device");
|
2019-02-12 11:10:43 -07:00
|
|
|
|
2011-10-16 10:21:33 -06:00
|
|
|
xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
|
2011-10-16 18:18:57 -06:00
|
|
|
XNClientWindow, win, XNFocusWindow, win, NULL);
|
2011-10-16 10:21:33 -06:00
|
|
|
|
2015-05-04 13:54:46 -06:00
|
|
|
XMapRaised(dpy, win);
|
2016-10-08 06:08:28 -06:00
|
|
|
if (embed) {
|
2019-02-03 16:29:26 -07:00
|
|
|
XSelectInput(dpy, parentwin, FocusChangeMask | SubstructureNotifyMask);
|
2016-10-08 06:08:28 -06:00
|
|
|
if (XQueryTree(dpy, parentwin, &dw, &w, &dws, &du) && dws) {
|
|
|
|
for (i = 0; i < du && dws[i] != win; ++i)
|
|
|
|
XSelectInput(dpy, dws[i], FocusChangeMask);
|
|
|
|
XFree(dws);
|
|
|
|
}
|
|
|
|
}
|
2015-05-04 13:54:46 -06:00
|
|
|
drw_resize(drw, mw, mh);
|
2010-07-31 07:56:27 -06:00
|
|
|
}
|
2011-05-18 09:20:03 -06:00
|
|
|
|
2015-09-27 15:57:39 -06:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2021-05-16 07:59:02 -06:00
|
|
|
fputs("usage: dmenubar [-bv] [-fn font] [-m monitor]\n"
|
|
|
|
" [-cb color] [-cf color] [-w windowid]\n", stderr);
|
2015-05-04 13:54:46 -06:00
|
|
|
exit(1);
|
2011-05-18 09:20:03 -06:00
|
|
|
}
|
2015-09-27 15:57:39 -06:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2016-10-08 06:08:28 -06:00
|
|
|
XWindowAttributes wa;
|
2021-05-11 12:19:56 -06:00
|
|
|
int i;
|
2021-05-15 22:24:38 -06:00
|
|
|
char text[text_s];
|
2021-05-16 07:59:02 -06:00
|
|
|
for(i = 0; i < text_s; ++i) /* initialize text[] */
|
2021-05-15 22:24:38 -06:00
|
|
|
text[i] = '\0';
|
2015-09-27 15:57:39 -06:00
|
|
|
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
/* these options take no arguments */
|
|
|
|
if (!strcmp(argv[i], "-v")) { /* prints version information */
|
2021-05-16 07:59:02 -06:00
|
|
|
puts("dmenuBAR-"VERSION);
|
2015-09-27 15:57:39 -06:00
|
|
|
exit(0);
|
2015-09-27 16:38:17 -06:00
|
|
|
} else if (!strcmp(argv[i], "-b")) /* appears at the bottom of the screen */
|
2015-11-08 15:03:34 -07:00
|
|
|
topbar = 0;
|
2021-05-11 12:14:33 -06:00
|
|
|
else if (i + 1 == argc)
|
2015-09-27 15:57:39 -06:00
|
|
|
usage();
|
|
|
|
/* these options take one argument */
|
|
|
|
else if (!strcmp(argv[i], "-m"))
|
|
|
|
mon = atoi(argv[++i]);
|
|
|
|
else if (!strcmp(argv[i], "-fn")) /* font or font set */
|
|
|
|
fonts[0] = argv[++i];
|
2021-05-16 07:59:02 -06:00
|
|
|
else if (!strcmp(argv[i], "-cb")) /* bg color */
|
2021-05-15 22:24:38 -06:00
|
|
|
colors[Scheme][ColBg] = argv[++i];
|
2021-05-16 07:59:02 -06:00
|
|
|
else if (!strcmp(argv[i], "-cf")) /* fg color */
|
2021-05-15 22:24:38 -06:00
|
|
|
colors[Scheme][ColFg] = argv[++i];
|
2016-10-08 06:08:28 -06:00
|
|
|
else if (!strcmp(argv[i], "-w")) /* embedding window id */
|
|
|
|
embed = argv[++i];
|
2015-09-27 15:57:39 -06:00
|
|
|
else
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
|
|
|
|
fputs("warning: no locale support\n", stderr);
|
|
|
|
if (!(dpy = XOpenDisplay(NULL)))
|
2016-08-12 06:39:30 -06:00
|
|
|
die("cannot open display");
|
2015-09-27 15:57:39 -06:00
|
|
|
screen = DefaultScreen(dpy);
|
|
|
|
root = RootWindow(dpy, screen);
|
2016-10-08 06:08:28 -06:00
|
|
|
if (!embed || !(parentwin = strtol(embed, NULL, 0)))
|
|
|
|
parentwin = root;
|
|
|
|
if (!XGetWindowAttributes(dpy, parentwin, &wa))
|
|
|
|
die("could not get embedding window attributes: 0x%lx",
|
|
|
|
parentwin);
|
|
|
|
drw = drw_create(dpy, screen, root, wa.width, wa.height);
|
2016-05-21 13:51:14 -06:00
|
|
|
if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
|
2016-08-12 06:39:30 -06:00
|
|
|
die("no fonts could be loaded.");
|
2016-05-21 13:51:14 -06:00
|
|
|
lrpad = drw->fonts->h;
|
2015-09-27 15:57:39 -06:00
|
|
|
|
2021-05-15 22:41:18 -06:00
|
|
|
/* ??? */
|
2018-05-25 05:03:25 -06:00
|
|
|
#ifdef __OpenBSD__
|
2018-05-25 05:07:17 -06:00
|
|
|
if (pledge("stdio rpath", NULL) == -1)
|
2018-05-25 04:04:22 -06:00
|
|
|
die("pledge");
|
2018-05-25 05:03:25 -06:00
|
|
|
#endif
|
2018-05-25 04:04:22 -06:00
|
|
|
|
2015-09-27 15:57:39 -06:00
|
|
|
setup();
|
2021-05-15 22:41:18 -06:00
|
|
|
|
2021-05-16 07:59:02 -06:00
|
|
|
do {
|
2021-05-15 22:24:38 -06:00
|
|
|
/* fgets adds a newline which will be printable in some fonts */
|
2021-05-16 07:59:02 -06:00
|
|
|
if (strlen(text) > 0)
|
2021-05-15 22:24:38 -06:00
|
|
|
text[strlen(text)-1] = '\0';
|
|
|
|
drawmenu(text);
|
2021-05-16 07:59:02 -06:00
|
|
|
} while (fgets(text, text_s, stdin) != NULL || !BREAK_ON_EOF);
|
2015-09-27 15:57:39 -06:00
|
|
|
|
2021-05-16 07:59:02 -06:00
|
|
|
cleanup();
|
|
|
|
return 0;
|
2015-09-27 15:57:39 -06:00
|
|
|
}
|