static void calcoffsetsv(void);
static char *cistrstr(const char *s, const char *sub);
static void cleanup(void);
+static void drawcursor(void);
static void drawmenu(void);
static void drawmenuh(void);
static void drawmenuv(void);
static Item *next = NULL;
static Item *prev = NULL;
static Item *curr = NULL;
-static Window root, win;
+static Window parent, win;
static int (*fstrncmp)(const char *, const char *, size_t n) = strncmp;
static char *(*fstrstr)(const char *, const char *) = strstr;
-static Bool vlist = False;
-static unsigned int lines = 5;
+static unsigned int lines = 0;
static void (*calcoffsets)(void) = calcoffsetsh;
void
return;
w = promptw + cmdw + 2 * spaceitem;
for(next = curr; next; next=next->right) {
- tw = textw(next->text);
- if(tw > mw / 3)
- tw = mw / 3;
+ tw = MIN(textw(next->text), mw / 3);
w += tw;
if(w > mw)
break;
}
w = promptw + cmdw + 2 * spaceitem;
for(prev = curr; prev && prev->left; prev=prev->left) {
- tw = textw(prev->left->text);
- if(tw > mw / 3)
- tw = mw / 3;
+ tw = MIN(textw(prev->left->text), mw / 3);
w += tw;
if(w > mw)
break;
void
calcoffsetsv(void) {
- static unsigned int w;
+ static unsigned int h;
if(!curr)
return;
- w = (dc.font.height + 2) * (lines + 1);
+ h = (dc.font.height + 2) * (lines + 1);
for(next = curr; next; next=next->right) {
- w -= dc.font.height + 2;
- if(w <= 0)
+ h -= dc.font.height + 2;
+ if(h <= 0)
break;
}
- w = (dc.font.height + 2) * (lines + 1);
+ h = (dc.font.height + 2) * (lines + 1);
for(prev = curr; prev && prev->left; prev=prev->left) {
- w -= dc.font.height + 2;
- if(w <= 0)
+ h -= dc.font.height + 2;
+ if(h <= 0)
break;
}
}
if(!sub)
return (char *)s;
- if((c = *sub++) != 0) {
+ if((c = *sub++) != '\0') {
c = tolower(c);
len = strlen(sub);
do {
do {
- if((csub = *s++) == 0)
+ if((csub = *s++) == '\0')
return NULL;
}
while(tolower(csub) != c);
dc.h = mh;
drawtext(NULL, dc.norm);
/* print prompt? */
- if(promptw) {
+ if(prompt) {
dc.w = promptw;
drawtext(prompt, dc.sel);
+ dc.x += dc.w;
}
- dc.x += promptw;
- dc.w = mw - promptw;
+ dc.w = mw - dc.x;
/* print command */
- if(cmdw && item)
+ if(cmdw && item && lines == 0)
dc.w = cmdw;
drawtext(text[0] ? text : NULL, dc.norm);
drawcursor();
- dc.x += cmdw;
if(curr) {
- if(vlist)
+ if(lines > 0)
drawmenuv();
else
drawmenuh();
drawmenuh(void) {
Item *i;
+ dc.x += cmdw;
dc.w = spaceitem;
- drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
+ drawtext(curr->left ? "<" : NULL, dc.norm);
dc.x += dc.w;
- /* determine maximum items */
for(i = curr; i != next; i=i->right) {
- dc.w = textw(i->text);
- if(dc.w > mw / 3)
- dc.w = mw / 3;
+ dc.w = MIN(textw(i->text), mw / 3);
drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
dc.x += dc.w;
}
- dc.x = mw - spaceitem;
dc.w = spaceitem;
+ dc.x = mw - dc.w;
drawtext(next ? ">" : NULL, dc.norm);
}
dc.x = 0;
dc.w = mw;
dc.y += dc.font.height + 2;
- /* determine maximum items */
for(i = curr; i != next; i=i->right) {
drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
dc.y += dc.font.height + 2;
unsigned int len;
for(len = 1000; len; len--) {
- if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
+ if(XGrabKeyboard(dpy, parent, True, GrabModeAsync, GrabModeAsync, CurrentTime)
== GrabSuccess)
break;
usleep(1000);
if(missing)
XFreeStringList(missing);
if(dc.font.set) {
- XFontSetExtents *font_extents;
XFontStruct **xfonts;
char **font_names;
dc.font.ascent = dc.font.descent = 0;
- font_extents = XExtentsOfFontSet(dc.font.set);
n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
- for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
- if(dc.font.ascent < (*xfonts)->ascent)
- dc.font.ascent = (*xfonts)->ascent;
- if(dc.font.descent < (*xfonts)->descent)
- dc.font.descent = (*xfonts)->descent;
+ for(i = 0; i < n; i++) {
+ dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
+ dc.font.descent = MAX(dc.font.descent, (*xfonts)->descent);
xfonts++;
}
}
KeySym ksym;
len = strlen(text);
- buf[0] = 0;
+ buf[0] = '\0';
num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
if(IsKeypadKey(ksym)) {
if(ksym == XK_KP_Enter)
switch (ksym) {
default: /* ignore other control sequences */
return;
- case XK_bracketleft:
+ case XK_a:
+ case XK_A:
+ ksym = XK_Home;
+ break;
+ case XK_c:
+ case XK_C:
ksym = XK_Escape;
break;
+ case XK_e:
+ case XK_E:
+ ksym = XK_End;
+ break;
case XK_h:
case XK_H:
ksym = XK_BackSpace;
break;
case XK_u:
case XK_U:
- text[0] = 0;
+ memmove(text, text + cursor, sizeof text - cursor + 1);
+ cursor = 0;
match(text);
- drawmenu();
break;
case XK_w:
case XK_W:
- if(len) {
- i = len - 1;
- while(i >= 0 && text[i] == ' ')
- text[i--] = 0;
- while(i >= 0 && text[i] != ' ')
- text[i--] = 0;
+ if(cursor > 0) {
+ i = cursor;
+ while(i-- > 0 && text[i] == ' ');
+ while(i-- > 0 && text[i] != ' ');
+ memmove(text + i + 1, text + cursor, sizeof text - cursor + 1);
+ cursor = i + 1;
match(text);
- drawmenu();
}
break;
}
case XK_p:
{
FILE *fp;
- char *c;
+ char *s;
if(!(fp = (FILE*)popen("sselp", "r")))
eprint("dmenu: Could not popen sselp\n");
- c = fgets(buf, sizeof buf, fp);
+ s = fgets(buf, sizeof buf, fp);
pclose(fp);
- if(c == NULL)
+ if(s == NULL)
return;
}
num = strlen(buf);
num = MIN(num, sizeof text - cursor);
if(num && !iscntrl((int) buf[0])) {
memmove(text + cursor + num, text + cursor, sizeof text - cursor - num);
- memmove(text + cursor, buf, num);
+ memcpy(text + cursor, buf, num);
cursor+=num;
match(text);
}
break;
case XK_BackSpace:
if(cursor > 0) {
- memmove(text + cursor + -1, text + cursor, sizeof text - cursor);
+ memmove(text + cursor - 1, text + cursor, sizeof text - cursor + 1);
cursor--;
match(text);
}
match(text);
break;
case XK_End:
- if(!item)
- return;
+ if(cursor < len) {
+ cursor = len;
+ break;
+ }
while(next) {
sel = curr = next;
calcoffsets();
running = False;
break;
case XK_Home:
- if(!item)
- return;
+ if(sel == item) {
+ cursor = 0;
+ break;
+ }
sel = curr = item;
calcoffsets();
break;
calcoffsets();
break;
case XK_Return:
- if((e->state & ShiftMask) && *text)
+ if((e->state & ShiftMask) || !sel)
fprintf(stdout, "%s", text);
- else if(sel)
+ else
fprintf(stdout, "%s", sel->text);
- else if(*text)
- fprintf(stdout, "%s", text);
fflush(stdout);
running = False;
break;
match(text);
break;
}
- len = strlen(text);
- cursor = MIN(cursor, len);
- cursor = MAX(cursor, 0);
drawmenu();
}
void
readstdin(void) {
- char *p, buf[1024];
+ char *p, buf[sizeof text];
unsigned int len = 0, max = 0;
Item *i, *new;
- i = 0;
+ i = NULL;
while(fgets(buf, sizeof buf, stdin)) {
len = strlen(buf);
- if (buf[len - 1] == '\n')
- buf[len - 1] = 0;
+ if(buf[len-1] == '\n')
+ buf[--len] = '\0';
if(!(p = strdup(buf)))
- eprint("fatal: could not strdup() %u bytes\n", strlen(buf));
- if(max < len) {
+ eprint("fatal: could not strdup() %u bytes\n", len);
+ if(max < len || !maxname) {
maxname = p;
max = len;
}
if(ev.xexpose.count == 0)
drawmenu();
break;
+ case VisibilityNotify:
+ if (ev.xvisibility.state != VisibilityUnobscured)
+ XRaiseWindow(dpy, win);
+ break;
}
}
#endif
XModifierKeymap *modmap;
XSetWindowAttributes wa;
+ XWindowAttributes pwa;
/* init modifier map */
modmap = XGetModifierMapping(dpy);
/* menu window */
wa.override_redirect = True;
wa.background_pixmap = ParentRelative;
- wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
+ wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask | VisibilityChangeMask;
/* menu window geometry */
- mh = dc.font.height + 2;
- mh = vlist ? mh * (lines+1) : mh;
+ mh = (dc.font.height + 2) * (lines + 1);
#if XINERAMA
- if(XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
+ if(parent == RootWindow(dpy, screen) && XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
i = 0;
if(n > 1) {
int di;
unsigned int dui;
Window dummy;
- if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui))
+ if(XQueryPointer(dpy, parent, &dummy, &dummy, &x, &y, &di, &di, &dui))
for(i = 0; i < n; i++)
if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
break;
else
#endif
{
+ XGetWindowAttributes(dpy, parent, &pwa);
x = 0;
- y = topbar ? 0 : DisplayHeight(dpy, screen) - mh;
- mw = DisplayWidth(dpy, screen);
+ y = topbar ? 0 : pwa.height - mh;
+ mw = pwa.width;
}
- win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
+ win = XCreateWindow(dpy, parent, x, y, mw, mh, 0,
DefaultDepth(dpy, screen), CopyFromParent,
DefaultVisual(dpy, screen),
CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
/* pixmap */
- dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
- dc.gc = XCreateGC(dpy, root, 0, NULL);
+ dc.drawable = XCreatePixmap(dpy, parent, mw, mh, DefaultDepth(dpy, screen));
+ dc.gc = XCreateGC(dpy, parent, 0, NULL);
XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
if(!dc.font.set)
XSetFont(dpy, dc.gc, dc.font.xfont->fid);
if(maxname)
- cmdw = textw(maxname);
- if(cmdw > mw / 3)
- cmdw = mw / 3;
+ cmdw = MIN(textw(maxname), mw / 3);
if(prompt)
- promptw = textw(prompt);
- if(promptw > mw / 5)
- promptw = mw / 5;
- text[0] = 0;
+ promptw = MIN(textw(prompt), mw / 5);
+ text[0] = '\0';
match(text);
XMapRaised(dpy, win);
}
}
else if(!strcmp(argv[i], "-b"))
topbar = False;
+ else if(!strcmp(argv[i], "-e")) {
+ if(++i < argc) parent = atoi(argv[i]);
+ }
else if(!strcmp(argv[i], "-l")) {
- vlist = True;
calcoffsets = calcoffsetsv;
if(++i < argc) lines = atoi(argv[i]);
}
if(++i < argc) selfgcolor = argv[i];
}
else if(!strcmp(argv[i], "-v"))
- eprint("dmenu-"VERSION", © 2006-2009 dmenu engineers, see LICENSE for details\n");
+ eprint("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n");
else
- eprint("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>] [-nf <color>]\n"
- " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
+ eprint("usage: dmenu [-i] [-b] [-e <xid>] [-l <lines>] [-fn <font>] [-nb <color>]\n"
+ " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
fprintf(stderr, "warning: no locale support\n");
if(!(dpy = XOpenDisplay(NULL)))
eprint("dmenu: cannot open display\n");
screen = DefaultScreen(dpy);
- root = RootWindow(dpy, screen);
+ if(!parent)
+ parent = RootWindow(dpy, screen);
- if(isatty(STDIN_FILENO)) {
- readstdin();
- running = grabkeyboard();
- }
- else { /* prevent keypress loss */
- running = grabkeyboard();
- readstdin();
- }
+ readstdin();
+ running = grabkeyboard();
setup(topbar);
drawmenu();