2011-05-14 10:47:12 -06:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
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"
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */
|
2007-01-11 07:52:37 -07:00
|
|
|
|
2015-09-27 16:15:03 -06:00
|
|
|
struct item {
|
2008-03-18 10:52:51 -06:00
|
|
|
char *text;
|
2015-09-27 16:15:03 -06:00
|
|
|
struct item *left, *right;
|
2015-11-08 15:03:34 -07:00
|
|
|
int out;
|
2006-08-04 01:35:27 -06:00
|
|
|
};
|
|
|
|
|
2011-05-05 08:46:48 -06:00
|
|
|
static char text[BUFSIZ] = "";
|
2016-10-08 06:08:28 -06:00
|
|
|
static char *embed;
|
2010-08-18 10:33:34 -06:00
|
|
|
static int bh, mw, mh;
|
2016-05-21 13:51:14 -06:00
|
|
|
static int inputw = 0, promptw;
|
|
|
|
static int lrpad; /* sum of left and right padding */
|
2015-09-27 15:57:39 -06:00
|
|
|
static size_t cursor;
|
2015-09-27 16:15:03 -06:00
|
|
|
static struct item *items = NULL;
|
|
|
|
static struct item *matches, *matchend;
|
|
|
|
static struct item *prev, *curr, *next, *sel;
|
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
|
2015-09-27 16:15:03 -06:00
|
|
|
appenditem(struct item *item, struct item **list, struct item **last)
|
2015-09-27 15:57:39 -06:00
|
|
|
{
|
|
|
|
if (*last)
|
2010-07-31 07:56:27 -06:00
|
|
|
(*last)->right = item;
|
2011-09-19 03:40:56 -06:00
|
|
|
else
|
|
|
|
*list = item;
|
2011-07-14 13:03:08 -06:00
|
|
|
|
2010-07-31 07:56:27 -06:00
|
|
|
item->left = *last;
|
|
|
|
item->right = NULL;
|
|
|
|
*last = item;
|
2007-09-23 10:26:41 -06:00
|
|
|
}
|
|
|
|
|
2015-09-27 15:57:39 -06:00
|
|
|
static void
|
|
|
|
calcoffsets(void)
|
|
|
|
{
|
2011-05-16 05:59:31 -06:00
|
|
|
int i, n;
|
2006-08-04 01:35:27 -06:00
|
|
|
|
2021-05-11 12:02:27 -06:00
|
|
|
n = mw - (promptw + inputw + TEXTW("<") + TEXTW(">"));
|
2011-10-26 06:20:14 -06:00
|
|
|
/* calculate which items will begin the next page and previous page */
|
2015-09-27 15:57:39 -06:00
|
|
|
for (i = 0, next = curr; next; next = next->right)
|
2021-05-11 12:02:27 -06:00
|
|
|
if ((i ? bh : MIN(TEXTW(next->text), n)) > n)
|
2010-08-10 07:14:37 -06:00
|
|
|
break;
|
2015-09-27 15:57:39 -06:00
|
|
|
for (i = 0, prev = curr; prev && prev->left; prev = prev->left)
|
2021-05-11 12:02:27 -06:00
|
|
|
if ((i ? bh : MIN(TEXTW(prev->left->text), n)) > n)
|
2010-08-10 07:14:37 -06:00
|
|
|
break;
|
2006-08-04 01:35:27 -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;
|
|
|
|
|
2015-05-04 13:54:46 -06:00
|
|
|
XUngrabKey(dpy, AnyKey, AnyModifier, root);
|
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);
|
|
|
|
}
|
|
|
|
|
2016-05-21 13:51:14 -06:00
|
|
|
static int
|
|
|
|
drawitem(struct item *item, int x, int y, int w)
|
|
|
|
{
|
|
|
|
if (item == sel)
|
|
|
|
drw_setscheme(drw, scheme[SchemeSel]);
|
|
|
|
else if (item->out)
|
|
|
|
drw_setscheme(drw, scheme[SchemeOut]);
|
|
|
|
else
|
|
|
|
drw_setscheme(drw, scheme[SchemeNorm]);
|
|
|
|
|
|
|
|
return drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0);
|
|
|
|
}
|
|
|
|
|
2015-09-27 15:57:39 -06:00
|
|
|
static void
|
|
|
|
drawmenu(void)
|
|
|
|
{
|
2015-09-27 16:15:03 -06:00
|
|
|
struct item *item;
|
2016-07-26 15:02:34 -06:00
|
|
|
int x = 0, y = 0, w;
|
2010-08-03 10:10:29 -06:00
|
|
|
|
2016-05-21 13:51:14 -06:00
|
|
|
drw_setscheme(drw, scheme[SchemeNorm]);
|
|
|
|
drw_rect(drw, 0, 0, mw, mh, 1, 1);
|
2010-08-09 04:54:46 -06:00
|
|
|
|
2015-09-27 15:57:39 -06:00
|
|
|
if (prompt && *prompt) {
|
2016-05-21 13:51:14 -06:00
|
|
|
drw_setscheme(drw, scheme[SchemeSel]);
|
|
|
|
x = drw_text(drw, x, 0, promptw, bh, lrpad / 2, prompt, 0);
|
2006-12-13 06:14:41 -07:00
|
|
|
}
|
2011-10-26 06:20:14 -06:00
|
|
|
/* draw input field */
|
2021-05-11 12:02:27 -06:00
|
|
|
w = (!matches) ? mw - x : inputw;
|
2016-07-26 15:02:34 -06:00
|
|
|
drw_setscheme(drw, scheme[SchemeNorm]);
|
2016-05-21 13:51:14 -06:00
|
|
|
drw_text(drw, x, 0, w, bh, lrpad / 2, text, 0);
|
2015-05-04 13:54:46 -06:00
|
|
|
|
2021-05-11 12:02:27 -06:00
|
|
|
if (matches) {
|
2011-10-26 06:20:14 -06:00
|
|
|
/* draw horizontal list */
|
2015-05-04 13:54:46 -06:00
|
|
|
x += inputw;
|
|
|
|
w = TEXTW("<");
|
2015-09-27 15:57:39 -06:00
|
|
|
if (curr->left) {
|
2016-05-21 13:51:14 -06:00
|
|
|
drw_setscheme(drw, scheme[SchemeNorm]);
|
|
|
|
drw_text(drw, x, 0, w, bh, lrpad / 2, "<", 0);
|
2015-05-04 13:54:46 -06:00
|
|
|
}
|
2016-05-21 13:51:14 -06:00
|
|
|
x += w;
|
|
|
|
for (item = curr; item != next; item = item->right)
|
|
|
|
x = drawitem(item, x, 0, MIN(TEXTW(item->text), mw - x - TEXTW(">")));
|
2015-09-27 15:57:39 -06:00
|
|
|
if (next) {
|
2016-05-21 13:51:14 -06:00
|
|
|
w = TEXTW(">");
|
|
|
|
drw_setscheme(drw, scheme[SchemeNorm]);
|
|
|
|
drw_text(drw, mw - w, 0, w, bh, lrpad / 2, ">", 0);
|
2010-08-03 10:10:29 -06:00
|
|
|
}
|
2009-11-28 05:28:15 -07: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
|
|
|
}
|
|
|
|
|
2015-09-27 16:19:36 -06:00
|
|
|
static void
|
|
|
|
match(void)
|
|
|
|
{
|
|
|
|
static char **tokv = NULL;
|
|
|
|
static int tokn = 0;
|
|
|
|
|
|
|
|
char buf[sizeof text], *s;
|
|
|
|
int i, tokc = 0;
|
2015-10-04 06:01:22 -06:00
|
|
|
size_t len, textsize;
|
2015-09-27 16:19:36 -06:00
|
|
|
struct item *item, *lprefix, *lsubstr, *prefixend, *substrend;
|
|
|
|
|
|
|
|
strcpy(buf, text);
|
|
|
|
/* separate input text into tokens to be matched individually */
|
|
|
|
for (s = strtok(buf, " "); s; tokv[tokc - 1] = s, s = strtok(NULL, " "))
|
|
|
|
if (++tokc > tokn && !(tokv = realloc(tokv, ++tokn * sizeof *tokv)))
|
2016-08-12 06:39:30 -06:00
|
|
|
die("cannot realloc %u bytes:", tokn * sizeof *tokv);
|
2015-09-27 16:19:36 -06:00
|
|
|
len = tokc ? strlen(tokv[0]) : 0;
|
|
|
|
|
|
|
|
matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
|
2016-12-07 07:45:01 -07:00
|
|
|
textsize = strlen(text) + 1;
|
2015-09-27 16:19:36 -06:00
|
|
|
for (item = items; item && item->text; item++) {
|
|
|
|
for (i = 0; i < tokc; i++)
|
|
|
|
if (!fstrstr(item->text, tokv[i]))
|
|
|
|
break;
|
|
|
|
if (i != tokc) /* not all tokens match */
|
|
|
|
continue;
|
|
|
|
/* exact matches go first, then prefixes, then substrings */
|
2015-10-04 06:01:22 -06:00
|
|
|
if (!tokc || !fstrncmp(text, item->text, textsize))
|
2015-09-27 16:19:36 -06:00
|
|
|
appenditem(item, &matches, &matchend);
|
|
|
|
else if (!fstrncmp(tokv[0], item->text, len))
|
|
|
|
appenditem(item, &lprefix, &prefixend);
|
|
|
|
else
|
|
|
|
appenditem(item, &lsubstr, &substrend);
|
|
|
|
}
|
|
|
|
if (lprefix) {
|
|
|
|
if (matches) {
|
|
|
|
matchend->right = lprefix;
|
|
|
|
lprefix->left = matchend;
|
|
|
|
} else
|
|
|
|
matches = lprefix;
|
|
|
|
matchend = prefixend;
|
|
|
|
}
|
|
|
|
if (lsubstr) {
|
|
|
|
if (matches) {
|
|
|
|
matchend->right = lsubstr;
|
|
|
|
lsubstr->left = matchend;
|
|
|
|
} else
|
|
|
|
matches = lsubstr;
|
|
|
|
matchend = substrend;
|
|
|
|
}
|
|
|
|
curr = sel = matches;
|
|
|
|
calcoffsets();
|
|
|
|
}
|
|
|
|
|
2015-09-27 15:57:39 -06:00
|
|
|
static void
|
|
|
|
insert(const char *str, ssize_t n)
|
|
|
|
{
|
|
|
|
if (strlen(text) + n > sizeof text - 1)
|
2010-08-12 08:35:51 -06:00
|
|
|
return;
|
2011-10-26 06:20:14 -06:00
|
|
|
/* move existing text out of the way, insert new text, and update cursor */
|
2011-05-15 06:58:54 -06:00
|
|
|
memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0));
|
2015-09-27 15:57:39 -06:00
|
|
|
if (n > 0)
|
2011-05-15 09:05:32 -06:00
|
|
|
memcpy(&text[cursor], str, n);
|
2010-07-31 07:56:27 -06:00
|
|
|
cursor += n;
|
2011-09-19 03:40:56 -06:00
|
|
|
match();
|
2010-07-30 06:40:56 -06:00
|
|
|
}
|
|
|
|
|
2015-09-27 15:57:39 -06:00
|
|
|
static void
|
|
|
|
readstdin(void)
|
|
|
|
{
|
2016-05-21 13:51:14 -06:00
|
|
|
char buf[sizeof text], *p;
|
|
|
|
size_t i, imax = 0, size = 0;
|
|
|
|
unsigned int tmpmax = 0;
|
2006-08-04 01:35:27 -06:00
|
|
|
|
2011-10-26 06:20:14 -06:00
|
|
|
/* read each line from stdin and add it to the item list */
|
2015-09-27 15:57:39 -06:00
|
|
|
for (i = 0; fgets(buf, sizeof buf, stdin); i++) {
|
|
|
|
if (i + 1 >= size / sizeof *items)
|
|
|
|
if (!(items = realloc(items, (size += BUFSIZ))))
|
2015-05-04 13:54:46 -06:00
|
|
|
die("cannot realloc %u bytes:", size);
|
2015-09-27 15:57:39 -06:00
|
|
|
if ((p = strchr(buf, '\n')))
|
2010-08-02 07:22:54 -06:00
|
|
|
*p = '\0';
|
2015-09-27 15:57:39 -06:00
|
|
|
if (!(items[i].text = strdup(buf)))
|
|
|
|
die("cannot strdup %u bytes:", strlen(buf) + 1);
|
2015-11-08 15:03:34 -07:00
|
|
|
items[i].out = 0;
|
2016-05-21 13:51:14 -06:00
|
|
|
drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL);
|
|
|
|
if (tmpmax > inputw) {
|
|
|
|
inputw = tmpmax;
|
|
|
|
imax = i;
|
|
|
|
}
|
2006-08-04 01:35:27 -06:00
|
|
|
}
|
2015-09-27 15:57:39 -06:00
|
|
|
if (items)
|
2011-05-18 09:20:03 -06:00
|
|
|
items[i].text = NULL;
|
2016-06-27 23:11:50 -06:00
|
|
|
inputw = items ? TEXTW(items[imax].text) : 0;
|
2006-08-04 01:35:27 -06:00
|
|
|
}
|
|
|
|
|
2015-09-27 15:57:39 -06:00
|
|
|
static void
|
|
|
|
run(void)
|
|
|
|
{
|
2010-07-30 06:40:56 -06:00
|
|
|
XEvent ev;
|
|
|
|
|
2015-09-27 15:57:39 -06:00
|
|
|
while (!XNextEvent(dpy, &ev)) {
|
2019-03-03 05:08:54 -07:00
|
|
|
if (XFilterEvent(&ev, win))
|
2011-10-16 10:21:33 -06:00
|
|
|
continue;
|
2010-07-30 06:40:56 -06:00
|
|
|
switch(ev.type) {
|
2019-02-03 16:29:26 -07:00
|
|
|
case DestroyNotify:
|
|
|
|
if (ev.xdestroywindow.window != win)
|
|
|
|
break;
|
|
|
|
cleanup();
|
|
|
|
exit(1);
|
2010-07-30 06:40:56 -06:00
|
|
|
case Expose:
|
2015-09-27 15:57:39 -06:00
|
|
|
if (ev.xexpose.count == 0)
|
2015-05-04 13:54:46 -06:00
|
|
|
drw_map(drw, win, 0, 0, mw, mh);
|
2010-07-30 06:40:56 -06:00
|
|
|
break;
|
|
|
|
case VisibilityNotify:
|
2015-09-27 15:57:39 -06:00
|
|
|
if (ev.xvisibility.state != VisibilityUnobscured)
|
2015-05-04 13:54:46 -06:00
|
|
|
XRaiseWindow(dpy, win);
|
2010-07-30 06:40:56 -06:00
|
|
|
break;
|
|
|
|
}
|
2011-10-16 10:21:33 -06:00
|
|
|
}
|
2010-07-30 06:40:56 -06:00
|
|
|
}
|
|
|
|
|
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;
|
2017-11-03 10:49:10 -06:00
|
|
|
XClassHint ch = {"dmenu", "dmenu"};
|
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
|
|
|
}
|
2016-05-21 13:51:14 -06:00
|
|
|
promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0;
|
2011-05-18 09:20:03 -06:00
|
|
|
inputw = MIN(inputw, mw/3);
|
2011-09-19 03:40:56 -06:00
|
|
|
match();
|
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;
|
2016-05-21 13:51:14 -06:00
|
|
|
swa.background_pixel = scheme[SchemeNorm][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);
|
2011-05-15 07:13:31 -06:00
|
|
|
drawmenu();
|
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-11 12:02:27 -06:00
|
|
|
fputs("usage: dmenu [-bfiv] [-p prompt] [-fn font] [-m monitor]\n"
|
2016-10-08 06:08:28 -06:00
|
|
|
" [-nb color] [-nf color] [-sb color] [-sf 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;
|
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 */
|
|
|
|
puts("dmenu-"VERSION);
|
|
|
|
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], "-p")) /* adds prompt to left of input field */
|
|
|
|
prompt = argv[++i];
|
|
|
|
else if (!strcmp(argv[i], "-fn")) /* font or font set */
|
|
|
|
fonts[0] = argv[++i];
|
|
|
|
else if (!strcmp(argv[i], "-nb")) /* normal background color */
|
2016-05-21 13:51:14 -06:00
|
|
|
colors[SchemeNorm][ColBg] = argv[++i];
|
2015-09-27 15:57:39 -06:00
|
|
|
else if (!strcmp(argv[i], "-nf")) /* normal foreground color */
|
2016-05-21 13:51:14 -06:00
|
|
|
colors[SchemeNorm][ColFg] = argv[++i];
|
2015-09-27 15:57:39 -06:00
|
|
|
else if (!strcmp(argv[i], "-sb")) /* selected background color */
|
2016-05-21 13:51:14 -06:00
|
|
|
colors[SchemeSel][ColBg] = argv[++i];
|
2015-09-27 15:57:39 -06:00
|
|
|
else if (!strcmp(argv[i], "-sf")) /* selected foreground color */
|
2016-05-21 13:51:14 -06:00
|
|
|
colors[SchemeSel][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
|
|
|
|
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
|
|
|
|
2021-05-11 12:18:07 -06:00
|
|
|
readstdin();
|
|
|
|
|
2015-09-27 15:57:39 -06:00
|
|
|
setup();
|
|
|
|
run();
|
|
|
|
|
|
|
|
return 1; /* unreachable */
|
|
|
|
}
|