Compare commits
54 Commits
Author | SHA1 | Date | |
---|---|---|---|
1538bb7426 | |||
cbe5e88e4f | |||
2fe3ccdf58 | |||
07d82c0c2a | |||
e7423f8fc6 | |||
61976c292b | |||
63c9e30a76 | |||
015f51ac46 | |||
542c58d8d0 | |||
745c46d8fa | |||
d058e83570 | |||
301b4e5591 | |||
38b866ba34 | |||
540a78761b | |||
7804354106 | |||
6d8e68dcfd | |||
d8688f7a4f | |||
09d0926bb9 | |||
adc4ec02c0 | |||
8b2f132973 | |||
1c488e6dac | |||
03c546c6b3 | |||
fdc1dba7ce | |||
3439470a47 | |||
6674bac1d3 | |||
72a8eb412f | |||
975dfb4163 | |||
2e898a308f | |||
6514b07ad2 | |||
724fe3cf7f | |||
70cb32b021 | |||
56f9e26b72 | |||
d094ebea96 | |||
ccf4d7166f | |||
06ae894434 | |||
b97783b07f | |||
11cb2e7dcc | |||
07239bbddd | |||
124bfd4a08 | |||
d27e3c1092 | |||
3a9f3a51ce | |||
53e92b5c17 | |||
d50ff5ca11 | |||
383e40dc21 | |||
8369e1736b | |||
c04b688cc0 | |||
4ebd7c4a21 | |||
dfe95cb546 | |||
8b633bf17d | |||
64697cdd0c | |||
5a3dfb1c40 | |||
4042a11e51 | |||
aa2f73fc88 | |||
f189781bbd |
6
.hgtags
6
.hgtags
@ -29,3 +29,9 @@ b6e09682c8adcb6569656bee73c311f9ab457715 2.3
|
||||
775f761a5647a05038e091d1c99fc35d3034cd68 2.6
|
||||
fbd9e9d63f202afe6834ccfdf890904f1897ec0b 2.7
|
||||
dd3d02b07cac44fbafc074a361c1002cebe7aae4 2.8
|
||||
59b3024854db49739c6d237fa9077f04a2da847a 3.0
|
||||
8f0f917ac988164e1b4446236e3a6ab6cfcb8c67 3.1
|
||||
e4c81a78ffbad6ba4d1ad119cc654da6eca63a4c 3.2
|
||||
709df5a4bad7015a346b2b44b1b3b573ea3088ff 3.3
|
||||
9ab649b3b3e5bfccf1c8f352c59e5361e070a25f 3.4
|
||||
05e5bd706b3b3e61399d57c4bb43df296a20112d 3.5
|
||||
|
5
LICENSE
5
LICENSE
@ -1,7 +1,8 @@
|
||||
MIT/X Consortium License
|
||||
|
||||
© 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
|
||||
© 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
|
||||
© 2006-2008 Anselm R. Garbe <garbeam at gmail dot com>
|
||||
© 2006-2008 Sander van Dijk <a dot h dot vandijk at gmail dot com>
|
||||
© 2006-2007 Michał Janeczek <janeczek at gmail dot com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
|
13
Makefile
13
Makefile
@ -1,9 +1,9 @@
|
||||
# dmenu - dynamic menu
|
||||
# © 2006-2007 Anselm R. Garbe, Sander van Dijk
|
||||
# See LICENSE file for copyright and license details.
|
||||
|
||||
include config.mk
|
||||
|
||||
SRC = draw.c main.c util.c
|
||||
SRC = dmenu.c
|
||||
OBJ = ${SRC:.c=.o}
|
||||
|
||||
all: options dmenu
|
||||
@ -18,12 +18,11 @@ options:
|
||||
@echo CC $<
|
||||
@${CC} -c ${CFLAGS} $<
|
||||
|
||||
${OBJ}: dmenu.h config.mk
|
||||
${OBJ}: config.h config.mk
|
||||
|
||||
dmenu: ${OBJ}
|
||||
@echo CC -o $@
|
||||
@${CC} -o $@ ${OBJ} ${LDFLAGS}
|
||||
@strip $@
|
||||
|
||||
clean:
|
||||
@echo cleaning
|
||||
@ -32,7 +31,7 @@ clean:
|
||||
dist: clean
|
||||
@echo creating dist tarball
|
||||
@mkdir -p dmenu-${VERSION}
|
||||
@cp -R LICENSE Makefile README config.mk dmenu.1 dmenu.h dmenu_path ${SRC} dmenu-${VERSION}
|
||||
@cp -R LICENSE Makefile README config.mk dmenu.1 config.h dmenu_path dmenu_run ${SRC} dmenu-${VERSION}
|
||||
@tar -cf dmenu-${VERSION}.tar dmenu-${VERSION}
|
||||
@gzip dmenu-${VERSION}.tar
|
||||
@rm -rf dmenu-${VERSION}
|
||||
@ -40,9 +39,10 @@ dist: clean
|
||||
install: all
|
||||
@echo installing executable file to ${DESTDIR}${PREFIX}/bin
|
||||
@mkdir -p ${DESTDIR}${PREFIX}/bin
|
||||
@cp -f dmenu dmenu_path ${DESTDIR}${PREFIX}/bin
|
||||
@cp -f dmenu dmenu_path dmenu_run ${DESTDIR}${PREFIX}/bin
|
||||
@chmod 755 ${DESTDIR}${PREFIX}/bin/dmenu
|
||||
@chmod 755 ${DESTDIR}${PREFIX}/bin/dmenu_path
|
||||
@chmod 755 ${DESTDIR}${PREFIX}/bin/dmenu_run
|
||||
@echo installing manual page to ${DESTDIR}${MANPREFIX}/man1
|
||||
@mkdir -p ${DESTDIR}${MANPREFIX}/man1
|
||||
@sed "s/VERSION/${VERSION}/g" < dmenu.1 > ${DESTDIR}${MANPREFIX}/man1/dmenu.1
|
||||
@ -51,6 +51,7 @@ install: all
|
||||
uninstall:
|
||||
@echo removing executable file from ${DESTDIR}${PREFIX}/bin
|
||||
@rm -f ${DESTDIR}${PREFIX}/bin/dmenu ${DESTDIR}${PREFIX}/bin/dmenu_path
|
||||
@rm -f ${DESTDIR}${PREFIX}/bin/dmenu ${DESTDIR}${PREFIX}/bin/dmenu_run
|
||||
@echo removing manual page from ${DESTDIR}${MANPREFIX}/man1
|
||||
@rm -f ${DESTDIR}${MANPREFIX}/man1/dmenu.1
|
||||
|
||||
|
10
config.h
Normal file
10
config.h
Normal file
@ -0,0 +1,10 @@
|
||||
/* See LICENSE file for copyright and license details. */
|
||||
|
||||
/* appearance */
|
||||
#define FONT "-*-terminus-medium-r-normal-*-14-*-*-*-*-*-*-*"
|
||||
#define NORMBGCOLOR "#cccccc"
|
||||
#define NORMFGCOLOR "#000000"
|
||||
#define SELBGCOLOR "#0066ff"
|
||||
#define SELFGCOLOR "#ffffff"
|
||||
/* next macro defines the space between menu items */
|
||||
#define SPACE 30 /* px */
|
@ -1,5 +1,5 @@
|
||||
# dmenu version
|
||||
VERSION = 3.0
|
||||
VERSION = 3.6
|
||||
|
||||
# Customize below to fit your system
|
||||
|
||||
@ -16,8 +16,8 @@ LIBS = -L/usr/lib -lc -L${X11LIB} -lX11
|
||||
|
||||
# flags
|
||||
CFLAGS = -Os ${INCS} -DVERSION=\"${VERSION}\"
|
||||
LDFLAGS = ${LIBS}
|
||||
#CFLAGS = -g -Wall -O2 ${INCS} -DVERSION=\"${VERSION}\"
|
||||
LDFLAGS = -s ${LIBS}
|
||||
#CFLAGS = -g -std=c99 -pedantic -Wall -O2 ${INCS} -DVERSION=\"${VERSION}\"
|
||||
#LDFLAGS = -g ${LIBS}
|
||||
|
||||
# Solaris
|
||||
|
20
dmenu.1
20
dmenu.1
@ -3,7 +3,10 @@
|
||||
dmenu \- dynamic menu
|
||||
.SH SYNOPSIS
|
||||
.B dmenu
|
||||
.RB [ \-b ]
|
||||
.RB [ \-i ]
|
||||
.RB [ \-x " <x>"]
|
||||
.RB [ \-y " <y>"]
|
||||
.RB [ \-w " <width>"]
|
||||
.RB [ \-fn " <font>"]
|
||||
.RB [ \-nb " <color>"]
|
||||
.RB [ \-nf " <color>"]
|
||||
@ -19,8 +22,19 @@ It manages huge amounts (up to 10.000 and more) of user defined menu items
|
||||
efficiently.
|
||||
.SS Options
|
||||
.TP
|
||||
.B \-b
|
||||
makes dmenu appear at the screen bottom (by default it appears at the screen top).
|
||||
.B \-x
|
||||
defines the x coordinate dmenu appears at (0 by default).
|
||||
.TP
|
||||
.B \-y
|
||||
defines the y coordinate dmenu appears at (0 by default). If it is negative,
|
||||
dmenu will appear with the bottom at the given positive coordinate. If it is
|
||||
-0, dmenu appears at the screen bottom.
|
||||
.TP
|
||||
.B \-w
|
||||
defines the width of the dmenu window (screen width by default).
|
||||
.TP
|
||||
.B \-i
|
||||
makes dmenu match menu entries case insensitively.
|
||||
.TP
|
||||
.B \-fn <font>
|
||||
defines the font.
|
||||
|
570
main.c → dmenu.c
570
main.c → dmenu.c
@ -1,46 +1,108 @@
|
||||
/* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
|
||||
* © 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
|
||||
* See LICENSE file for license details. */
|
||||
#include "dmenu.h"
|
||||
/* See LICENSE file for copyright and license details. */
|
||||
#include <ctype.h>
|
||||
#include <limits.h>
|
||||
#include <locale.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xutil.h>
|
||||
#include <X11/keysym.h>
|
||||
|
||||
/* macros */
|
||||
#define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
|
||||
|
||||
/* enums */
|
||||
enum { ColFG, ColBG, ColLast };
|
||||
|
||||
/* typedefs */
|
||||
typedef struct {
|
||||
int x, y, w, h;
|
||||
unsigned long norm[ColLast];
|
||||
unsigned long sel[ColLast];
|
||||
Drawable drawable;
|
||||
GC gc;
|
||||
struct {
|
||||
XFontStruct *xfont;
|
||||
XFontSet set;
|
||||
int ascent;
|
||||
int descent;
|
||||
int height;
|
||||
} font;
|
||||
} DC; /* draw context */
|
||||
|
||||
typedef struct Item Item;
|
||||
struct Item {
|
||||
char *text;
|
||||
Item *next; /* traverses all items */
|
||||
Item *left, *right; /* traverses items matching current search pattern */
|
||||
char *text;
|
||||
};
|
||||
|
||||
/* static */
|
||||
/* forward declarations */
|
||||
void appenditem(Item *i, Item **list, Item **last);
|
||||
void calcoffsets(void);
|
||||
char *cistrstr(const char *s, const char *sub);
|
||||
void cleanup(void);
|
||||
void drawmenu(void);
|
||||
void drawtext(const char *text, unsigned long col[ColLast]);
|
||||
void *emalloc(unsigned int size);
|
||||
void eprint(const char *errstr, ...);
|
||||
char *estrdup(const char *str);
|
||||
unsigned long getcolor(const char *colstr);
|
||||
Bool grabkeyboard(void);
|
||||
void initfont(const char *fontstr);
|
||||
void kpress(XKeyEvent * e);
|
||||
void match(char *pattern);
|
||||
void readstdin(void);
|
||||
void run(void);
|
||||
void setup(int x, int y, int w);
|
||||
unsigned int textnw(const char *text, unsigned int len);
|
||||
unsigned int textw(const char *text);
|
||||
|
||||
static char text[4096];
|
||||
static char *prompt = NULL;
|
||||
static int mw, mh;
|
||||
static int ret = 0;
|
||||
static int nitem = 0;
|
||||
static unsigned int cmdw = 0;
|
||||
static unsigned int promptw = 0;
|
||||
static unsigned int numlockmask = 0;
|
||||
static Bool running = True;
|
||||
static Item *allitems = NULL; /* first of all items */
|
||||
static Item *item = NULL; /* first of pattern matching items */
|
||||
static Item *sel = NULL;
|
||||
static Item *next = NULL;
|
||||
static Item *prev = NULL;
|
||||
static Item *curr = NULL;
|
||||
static Window root;
|
||||
static Window win;
|
||||
#include "config.h"
|
||||
|
||||
static void
|
||||
/* variables */
|
||||
char *font = FONT;
|
||||
char *maxname = NULL;
|
||||
char *normbg = NORMBGCOLOR;
|
||||
char *normfg = NORMFGCOLOR;
|
||||
char *prompt = NULL;
|
||||
char *selbg = SELBGCOLOR;
|
||||
char *selfg = SELFGCOLOR;
|
||||
char text[4096];
|
||||
int screen;
|
||||
int ret = 0;
|
||||
unsigned int cmdw = 0;
|
||||
unsigned int mw, mh;
|
||||
unsigned int promptw = 0;
|
||||
unsigned int numlockmask = 0;
|
||||
Bool running = True;
|
||||
Display *dpy;
|
||||
DC dc = {0};
|
||||
Item *allitems = NULL; /* first of all items */
|
||||
Item *item = NULL; /* first of pattern matching items */
|
||||
Item *sel = NULL;
|
||||
Item *next = NULL;
|
||||
Item *prev = NULL;
|
||||
Item *curr = NULL;
|
||||
Window root, win;
|
||||
int (*fstrncmp)(const char *, const char *, size_t n) = strncmp;
|
||||
char *(*fstrstr)(const char *, const char *) = strstr;
|
||||
|
||||
void
|
||||
appenditem(Item *i, Item **list, Item **last) {
|
||||
if(!(*last))
|
||||
*list = i;
|
||||
else
|
||||
(*last)->right = i;
|
||||
i->left = *last;
|
||||
i->right = NULL;
|
||||
*last = i;
|
||||
}
|
||||
|
||||
void
|
||||
calcoffsets(void) {
|
||||
unsigned int tw, w;
|
||||
|
||||
@ -66,7 +128,50 @@ calcoffsets(void) {
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
char *
|
||||
cistrstr(const char *s, const char *sub) {
|
||||
int c, csub;
|
||||
unsigned int len;
|
||||
|
||||
if(!sub)
|
||||
return (char *)s;
|
||||
if((c = *sub++) != 0) {
|
||||
c = tolower(c);
|
||||
len = strlen(sub);
|
||||
do {
|
||||
do {
|
||||
if((csub = *s++) == 0)
|
||||
return (NULL);
|
||||
}
|
||||
while(tolower(csub) != c);
|
||||
}
|
||||
while(strncasecmp(s, sub, len) != 0);
|
||||
s--;
|
||||
}
|
||||
return (char *)s;
|
||||
}
|
||||
|
||||
void
|
||||
cleanup(void) {
|
||||
Item *itm;
|
||||
|
||||
while(allitems) {
|
||||
itm = allitems->next;
|
||||
free(allitems->text);
|
||||
free(allitems);
|
||||
allitems = itm;
|
||||
}
|
||||
if(dc.font.set)
|
||||
XFreeFontSet(dpy, dc.font.set);
|
||||
else
|
||||
XFreeFont(dpy, dc.font.xfont);
|
||||
XFreePixmap(dpy, dc.drawable);
|
||||
XFreeGC(dpy, dc.gc);
|
||||
XDestroyWindow(dpy, win);
|
||||
XUngrabKeyboard(dpy, CurrentTime);
|
||||
}
|
||||
|
||||
void
|
||||
drawmenu(void) {
|
||||
Item *i;
|
||||
|
||||
@ -107,7 +212,85 @@ drawmenu(void) {
|
||||
XFlush(dpy);
|
||||
}
|
||||
|
||||
static Bool
|
||||
void
|
||||
drawtext(const char *text, unsigned long col[ColLast]) {
|
||||
int x, y, w, h;
|
||||
static char buf[256];
|
||||
unsigned int len, olen;
|
||||
XRectangle r = { dc.x, dc.y, dc.w, dc.h };
|
||||
|
||||
XSetForeground(dpy, dc.gc, col[ColBG]);
|
||||
XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
|
||||
if(!text)
|
||||
return;
|
||||
w = 0;
|
||||
olen = len = strlen(text);
|
||||
if(len >= sizeof buf)
|
||||
len = sizeof buf - 1;
|
||||
memcpy(buf, text, len);
|
||||
buf[len] = 0;
|
||||
h = dc.font.ascent + dc.font.descent;
|
||||
y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
|
||||
x = dc.x + (h / 2);
|
||||
/* shorten text if necessary */
|
||||
while(len && (w = textnw(buf, len)) > dc.w - h)
|
||||
buf[--len] = 0;
|
||||
if(len < olen) {
|
||||
if(len > 1)
|
||||
buf[len - 1] = '.';
|
||||
if(len > 2)
|
||||
buf[len - 2] = '.';
|
||||
if(len > 3)
|
||||
buf[len - 3] = '.';
|
||||
}
|
||||
if(w > dc.w)
|
||||
return; /* too long */
|
||||
XSetForeground(dpy, dc.gc, col[ColFG]);
|
||||
if(dc.font.set)
|
||||
XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
|
||||
else
|
||||
XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
|
||||
}
|
||||
|
||||
void *
|
||||
emalloc(unsigned int size) {
|
||||
void *res = malloc(size);
|
||||
|
||||
if(!res)
|
||||
eprint("fatal: could not malloc() %u bytes\n", size);
|
||||
return res;
|
||||
}
|
||||
|
||||
void
|
||||
eprint(const char *errstr, ...) {
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, errstr);
|
||||
vfprintf(stderr, errstr, ap);
|
||||
va_end(ap);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
char *
|
||||
estrdup(const char *str) {
|
||||
void *res = strdup(str);
|
||||
|
||||
if(!res)
|
||||
eprint("fatal: could not malloc() %u bytes\n", strlen(str));
|
||||
return res;
|
||||
}
|
||||
|
||||
unsigned long
|
||||
getcolor(const char *colstr) {
|
||||
Colormap cmap = DefaultColormap(dpy, screen);
|
||||
XColor color;
|
||||
|
||||
if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
|
||||
eprint("error, cannot allocate color '%s'\n", colstr);
|
||||
return color.pixel;
|
||||
}
|
||||
|
||||
Bool
|
||||
grabkeyboard(void) {
|
||||
unsigned int len;
|
||||
|
||||
@ -120,21 +303,13 @@ grabkeyboard(void) {
|
||||
return len > 0;
|
||||
}
|
||||
|
||||
static unsigned long
|
||||
initcolor(const char *colstr) {
|
||||
Colormap cmap = DefaultColormap(dpy, screen);
|
||||
XColor color;
|
||||
|
||||
if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
|
||||
eprint("error, cannot allocate color '%s'\n", colstr);
|
||||
return color.pixel;
|
||||
}
|
||||
|
||||
static void
|
||||
void
|
||||
initfont(const char *fontstr) {
|
||||
char *def, **missing;
|
||||
int i, n;
|
||||
|
||||
if(!fontstr || fontstr[0] == '\0')
|
||||
eprint("error, cannot load font: '%s'\n", fontstr);
|
||||
missing = NULL;
|
||||
if(dc.font.set)
|
||||
XFreeFontSet(dpy, dc.font.set);
|
||||
@ -160,7 +335,8 @@ initfont(const char *fontstr) {
|
||||
if(dc.font.xfont)
|
||||
XFreeFont(dpy, dc.font.xfont);
|
||||
dc.font.xfont = NULL;
|
||||
if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
|
||||
if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
|
||||
&& !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
|
||||
eprint("error, cannot load font: '%s'\n", fontstr);
|
||||
dc.font.ascent = dc.font.xfont->ascent;
|
||||
dc.font.descent = dc.font.xfont->descent;
|
||||
@ -168,44 +344,7 @@ initfont(const char *fontstr) {
|
||||
dc.font.height = dc.font.ascent + dc.font.descent;
|
||||
}
|
||||
|
||||
static void
|
||||
match(char *pattern) {
|
||||
unsigned int plen;
|
||||
Item *i, *j;
|
||||
|
||||
if(!pattern)
|
||||
return;
|
||||
plen = strlen(pattern);
|
||||
item = j = NULL;
|
||||
nitem = 0;
|
||||
for(i = allitems; i; i=i->next)
|
||||
if(!plen || !strncmp(pattern, i->text, plen)) {
|
||||
if(!j)
|
||||
item = i;
|
||||
else
|
||||
j->right = i;
|
||||
i->left = j;
|
||||
i->right = NULL;
|
||||
j = i;
|
||||
nitem++;
|
||||
}
|
||||
for(i = allitems; i; i=i->next)
|
||||
if(plen && strncmp(pattern, i->text, plen)
|
||||
&& strstr(i->text, pattern)) {
|
||||
if(!j)
|
||||
item = i;
|
||||
else
|
||||
j->right = i;
|
||||
i->left = j;
|
||||
i->right = NULL;
|
||||
j = i;
|
||||
nitem++;
|
||||
}
|
||||
curr = prev = next = sel = item;
|
||||
calcoffsets();
|
||||
}
|
||||
|
||||
static void
|
||||
void
|
||||
kpress(XKeyEvent * e) {
|
||||
char buf[32];
|
||||
int i, num;
|
||||
@ -215,13 +354,11 @@ kpress(XKeyEvent * e) {
|
||||
len = strlen(text);
|
||||
buf[0] = 0;
|
||||
num = XLookupString(e, buf, sizeof buf, &ksym, 0);
|
||||
if(IsKeypadKey(ksym)) {
|
||||
if(ksym == XK_KP_Enter) {
|
||||
if(IsKeypadKey(ksym))
|
||||
if(ksym == XK_KP_Enter)
|
||||
ksym = XK_Return;
|
||||
} else if(ksym >= XK_KP_0 && ksym <= XK_KP_9) {
|
||||
else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
|
||||
ksym = (ksym - XK_KP_0) + XK_0;
|
||||
}
|
||||
}
|
||||
if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
|
||||
|| IsMiscFunctionKey(ksym) || IsPFKey(ksym)
|
||||
|| IsPrivateKeypadKey(ksym))
|
||||
@ -376,9 +513,49 @@ kpress(XKeyEvent * e) {
|
||||
drawmenu();
|
||||
}
|
||||
|
||||
static char *
|
||||
void
|
||||
match(char *pattern) {
|
||||
unsigned int plen;
|
||||
Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
|
||||
|
||||
if(!pattern)
|
||||
return;
|
||||
plen = strlen(pattern);
|
||||
item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
|
||||
for(i = allitems; i; i = i->next)
|
||||
if(!fstrncmp(pattern, i->text, plen + 1))
|
||||
appenditem(i, &lexact, &exactend);
|
||||
else if(!fstrncmp(pattern, i->text, plen))
|
||||
appenditem(i, &lprefix, &prefixend);
|
||||
else if(fstrstr(i->text, pattern))
|
||||
appenditem(i, &lsubstr, &substrend);
|
||||
if(lexact) {
|
||||
item = lexact;
|
||||
itemend = exactend;
|
||||
}
|
||||
if(lprefix) {
|
||||
if(itemend) {
|
||||
itemend->right = lprefix;
|
||||
lprefix->left = itemend;
|
||||
}
|
||||
else
|
||||
item = lprefix;
|
||||
itemend = prefixend;
|
||||
}
|
||||
if(lsubstr) {
|
||||
if(itemend) {
|
||||
itemend->right = lsubstr;
|
||||
lsubstr->left = itemend;
|
||||
}
|
||||
else
|
||||
item = lsubstr;
|
||||
}
|
||||
curr = prev = next = sel = item;
|
||||
calcoffsets();
|
||||
}
|
||||
|
||||
void
|
||||
readstdin(void) {
|
||||
static char *maxname = NULL;
|
||||
char *p, buf[1024];
|
||||
unsigned int len = 0, max = 0;
|
||||
Item *i, *new;
|
||||
@ -402,105 +579,67 @@ readstdin(void) {
|
||||
i->next = new;
|
||||
i = new;
|
||||
}
|
||||
|
||||
return maxname;
|
||||
}
|
||||
|
||||
static void
|
||||
usage(void) {
|
||||
eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
|
||||
" [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
|
||||
}
|
||||
|
||||
/* extern */
|
||||
|
||||
int screen;
|
||||
Display *dpy;
|
||||
DC dc = {0};
|
||||
|
||||
int
|
||||
main(int argc, char *argv[]) {
|
||||
Bool bottom = False;
|
||||
char *font = FONT;
|
||||
char *maxname;
|
||||
char *normbg = NORMBGCOLOR;
|
||||
char *normfg = NORMFGCOLOR;
|
||||
char *selbg = SELBGCOLOR;
|
||||
char *selfg = SELFGCOLOR;
|
||||
int i, j;
|
||||
Item *itm;
|
||||
void
|
||||
run(void) {
|
||||
XEvent ev;
|
||||
|
||||
/* main event loop */
|
||||
while(running && !XNextEvent(dpy, &ev))
|
||||
switch (ev.type) {
|
||||
default: /* ignore all crap */
|
||||
break;
|
||||
case KeyPress:
|
||||
kpress(&ev.xkey);
|
||||
break;
|
||||
case Expose:
|
||||
if(ev.xexpose.count == 0)
|
||||
drawmenu();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
setup(int x, int y, int w) {
|
||||
unsigned int i, j;
|
||||
XModifierKeymap *modmap;
|
||||
XSetWindowAttributes wa;
|
||||
|
||||
/* command line args */
|
||||
for(i = 1; i < argc; i++)
|
||||
if(!strncmp(argv[i], "-b", 3)) {
|
||||
bottom = True;
|
||||
}
|
||||
else if(!strncmp(argv[i], "-fn", 4)) {
|
||||
if(++i < argc) font = argv[i];
|
||||
}
|
||||
else if(!strncmp(argv[i], "-nb", 4)) {
|
||||
if(++i < argc) normbg = argv[i];
|
||||
}
|
||||
else if(!strncmp(argv[i], "-nf", 4)) {
|
||||
if(++i < argc) normfg = argv[i];
|
||||
}
|
||||
else if(!strncmp(argv[i], "-p", 3)) {
|
||||
if(++i < argc) prompt = argv[i];
|
||||
}
|
||||
else if(!strncmp(argv[i], "-sb", 4)) {
|
||||
if(++i < argc) selbg = argv[i];
|
||||
}
|
||||
else if(!strncmp(argv[i], "-sf", 4)) {
|
||||
if(++i < argc) selfg = argv[i];
|
||||
}
|
||||
else if(!strncmp(argv[i], "-v", 3))
|
||||
eprint("dmenu-"VERSION", © 2006-2007 Anselm R. Garbe, Sander van Dijk\n");
|
||||
else
|
||||
usage();
|
||||
setlocale(LC_CTYPE, "");
|
||||
dpy = XOpenDisplay(0);
|
||||
if(!dpy)
|
||||
eprint("dmenu: cannot open display\n");
|
||||
screen = DefaultScreen(dpy);
|
||||
root = RootWindow(dpy, screen);
|
||||
if(isatty(STDIN_FILENO)) {
|
||||
maxname = readstdin();
|
||||
running = grabkeyboard();
|
||||
}
|
||||
else { /* prevent keypress loss */
|
||||
running = grabkeyboard();
|
||||
maxname = readstdin();
|
||||
}
|
||||
/* init modifier map */
|
||||
modmap = XGetModifierMapping(dpy);
|
||||
for (i = 0; i < 8; i++) {
|
||||
for(i = 0; i < 8; i++)
|
||||
for(j = 0; j < modmap->max_keypermod; j++) {
|
||||
if(modmap->modifiermap[i * modmap->max_keypermod + j]
|
||||
== XKeysymToKeycode(dpy, XK_Num_Lock))
|
||||
numlockmask = (1 << i);
|
||||
}
|
||||
}
|
||||
XFreeModifiermap(modmap);
|
||||
|
||||
/* style */
|
||||
dc.norm[ColBG] = initcolor(normbg);
|
||||
dc.norm[ColFG] = initcolor(normfg);
|
||||
dc.sel[ColBG] = initcolor(selbg);
|
||||
dc.sel[ColFG] = initcolor(selfg);
|
||||
dc.norm[ColBG] = getcolor(normbg);
|
||||
dc.norm[ColFG] = getcolor(normfg);
|
||||
dc.sel[ColBG] = getcolor(selbg);
|
||||
dc.sel[ColFG] = getcolor(selfg);
|
||||
initfont(font);
|
||||
|
||||
/* menu window */
|
||||
wa.override_redirect = 1;
|
||||
wa.background_pixmap = ParentRelative;
|
||||
wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
|
||||
mw = DisplayWidth(dpy, screen);
|
||||
mw = w ? w : DisplayWidth(dpy, screen);
|
||||
mh = dc.font.height + 2;
|
||||
win = XCreateWindow(dpy, root, 0,
|
||||
bottom ? DisplayHeight(dpy, screen) - mh : 0, mw, mh, 0,
|
||||
if(y < 0) {
|
||||
if(y == INT_MIN)
|
||||
y = DisplayHeight(dpy, screen) - mh;
|
||||
else
|
||||
y = (-1 * y) - mh;
|
||||
}
|
||||
win = XCreateWindow(dpy, root, 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, 0);
|
||||
@ -518,38 +657,93 @@ main(int argc, char *argv[]) {
|
||||
text[0] = 0;
|
||||
match(text);
|
||||
XMapRaised(dpy, win);
|
||||
}
|
||||
|
||||
unsigned int
|
||||
textnw(const char *text, unsigned int len) {
|
||||
XRectangle r;
|
||||
|
||||
if(dc.font.set) {
|
||||
XmbTextExtents(dc.font.set, text, len, NULL, &r);
|
||||
return r.width;
|
||||
}
|
||||
return XTextWidth(dc.font.xfont, text, len);
|
||||
}
|
||||
|
||||
unsigned int
|
||||
textw(const char *text) {
|
||||
return textnw(text, strlen(text)) + dc.font.height;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[]) {
|
||||
int x = 0, y = 0, w = 0;
|
||||
unsigned int i;
|
||||
|
||||
/* command line args */
|
||||
for(i = 1; i < argc; i++)
|
||||
if(!strcmp(argv[i], "-i")) {
|
||||
fstrncmp = strncasecmp;
|
||||
fstrstr = cistrstr;
|
||||
}
|
||||
else if(!strcmp(argv[i], "-fn")) {
|
||||
if(++i < argc) font = argv[i];
|
||||
}
|
||||
else if(!strcmp(argv[i], "-nb")) {
|
||||
if(++i < argc) normbg = argv[i];
|
||||
}
|
||||
else if(!strcmp(argv[i], "-nf")) {
|
||||
if(++i < argc) normfg = argv[i];
|
||||
}
|
||||
else if(!strcmp(argv[i], "-p")) {
|
||||
if(++i < argc) prompt = argv[i];
|
||||
}
|
||||
else if(!strcmp(argv[i], "-sb")) {
|
||||
if(++i < argc) selbg = argv[i];
|
||||
}
|
||||
else if(!strcmp(argv[i], "-sf")) {
|
||||
if(++i < argc) selfg = argv[i];
|
||||
}
|
||||
else if(!strcmp(argv[i], "-x")) {
|
||||
if(++i < argc) x = atoi(argv[i]);
|
||||
}
|
||||
else if(!strcmp(argv[i], "-y")) {
|
||||
if(++i < argc)
|
||||
if(!strcmp(argv[i], "-0"))
|
||||
y = INT_MIN;
|
||||
else
|
||||
y = atoi(argv[i]);
|
||||
}
|
||||
else if(!strcmp(argv[i], "-w")) {
|
||||
if(++i < argc) w = atoi(argv[i]);
|
||||
}
|
||||
else if(!strcmp(argv[i], "-v"))
|
||||
eprint("dmenu-"VERSION", © 2006-2008 dmenu engineers, see LICENSE for details\n");
|
||||
else
|
||||
eprint("usage: dmenu [-i] [-fn <font>] [-nb <color>] [-nf <color>]\n"
|
||||
" [-p <prompt>] [-sb <color>] [-sf <color>]\n"
|
||||
" [-x <x>] [-y <y>] [-w <w>] [-v]\n");
|
||||
setlocale(LC_CTYPE, "");
|
||||
dpy = XOpenDisplay(0);
|
||||
if(!dpy)
|
||||
eprint("dmenu: cannot open display\n");
|
||||
screen = DefaultScreen(dpy);
|
||||
root = RootWindow(dpy, screen);
|
||||
|
||||
if(isatty(STDIN_FILENO)) {
|
||||
readstdin();
|
||||
running = grabkeyboard();
|
||||
}
|
||||
else { /* prevent keypress loss */
|
||||
running = grabkeyboard();
|
||||
readstdin();
|
||||
}
|
||||
|
||||
setup(x, y, w);
|
||||
drawmenu();
|
||||
XSync(dpy, False);
|
||||
|
||||
/* main event loop */
|
||||
while(running && !XNextEvent(dpy, &ev))
|
||||
switch (ev.type) {
|
||||
default: /* ignore all crap */
|
||||
break;
|
||||
case KeyPress:
|
||||
kpress(&ev.xkey);
|
||||
break;
|
||||
case Expose:
|
||||
if(ev.xexpose.count == 0)
|
||||
drawmenu();
|
||||
break;
|
||||
}
|
||||
|
||||
/* cleanup */
|
||||
while(allitems) {
|
||||
itm = allitems->next;
|
||||
free(allitems->text);
|
||||
free(allitems);
|
||||
allitems = itm;
|
||||
}
|
||||
if(dc.font.set)
|
||||
XFreeFontSet(dpy, dc.font.set);
|
||||
else
|
||||
XFreeFont(dpy, dc.font.xfont);
|
||||
XFreePixmap(dpy, dc.drawable);
|
||||
XFreeGC(dpy, dc.gc);
|
||||
XDestroyWindow(dpy, win);
|
||||
XUngrabKeyboard(dpy, CurrentTime);
|
||||
run();
|
||||
cleanup();
|
||||
XCloseDisplay(dpy);
|
||||
return ret;
|
||||
}
|
43
dmenu.h
43
dmenu.h
@ -1,43 +0,0 @@
|
||||
/* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
|
||||
* © 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
|
||||
* See LICENSE file for license details. */
|
||||
#include <X11/Xlib.h>
|
||||
|
||||
#define FONT "-*-fixed-medium-r-normal-*-13-*-*-*-*-*-*-*"
|
||||
#define NORMBGCOLOR "#eeeeee"
|
||||
#define NORMFGCOLOR "#222222"
|
||||
#define SELBGCOLOR "#006699"
|
||||
#define SELFGCOLOR "#ffffff"
|
||||
#define SPACE 30 /* px */
|
||||
|
||||
/* color */
|
||||
enum { ColFG, ColBG, ColLast };
|
||||
|
||||
typedef struct {
|
||||
int x, y, w, h;
|
||||
unsigned long norm[ColLast];
|
||||
unsigned long sel[ColLast];
|
||||
Drawable drawable;
|
||||
GC gc;
|
||||
struct {
|
||||
XFontStruct *xfont;
|
||||
XFontSet set;
|
||||
int ascent;
|
||||
int descent;
|
||||
int height;
|
||||
} font;
|
||||
} DC; /* draw context */
|
||||
|
||||
int screen;
|
||||
Display *dpy;
|
||||
DC dc; /* global drawing context */
|
||||
|
||||
/* draw.c */
|
||||
void drawtext(const char *text, unsigned long col[ColLast]);
|
||||
unsigned int textw(const char *text);
|
||||
unsigned int textnw(const char *text, unsigned int len);
|
||||
|
||||
/* util.c */
|
||||
void *emalloc(unsigned int size); /* allocates memory, exits on error */
|
||||
void eprint(const char *errstr, ...); /* prints errstr and exits with 1 */
|
||||
char *estrdup(const char *str); /* duplicates str, exits on allocation error */
|
19
dmenu_path
19
dmenu_path
@ -1,9 +1,26 @@
|
||||
#!/bin/sh
|
||||
CACHE=$HOME/.dmenu_cache
|
||||
IFS=:
|
||||
|
||||
uptodate() {
|
||||
test ! -f $CACHE && return 1
|
||||
for dir in $PATH
|
||||
do
|
||||
test $dir -nt $CACHE && return 1
|
||||
done
|
||||
return 0
|
||||
}
|
||||
|
||||
if ! uptodate
|
||||
then
|
||||
for dir in $PATH
|
||||
do
|
||||
for file in "$dir"/*
|
||||
do
|
||||
test -x "$file" && echo "${file##*/}"
|
||||
done
|
||||
done | sort | uniq
|
||||
done | sort | uniq > $CACHE.$$
|
||||
mv $CACHE.$$ $CACHE
|
||||
fi
|
||||
|
||||
cat $CACHE
|
||||
|
2
dmenu_run
Executable file
2
dmenu_run
Executable file
@ -0,0 +1,2 @@
|
||||
#!/bin/sh
|
||||
exe=`dmenu_path | dmenu $*` && exec $exe
|
63
draw.c
63
draw.c
@ -1,63 +0,0 @@
|
||||
/* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
|
||||
* © 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
|
||||
* See LICENSE file for license details. */
|
||||
#include "dmenu.h"
|
||||
#include <string.h>
|
||||
|
||||
/* extern */
|
||||
|
||||
void
|
||||
drawtext(const char *text, unsigned long col[ColLast]) {
|
||||
int x, y, w, h;
|
||||
static char buf[256];
|
||||
unsigned int len, olen;
|
||||
XRectangle r = { dc.x, dc.y, dc.w, dc.h };
|
||||
|
||||
XSetForeground(dpy, dc.gc, col[ColBG]);
|
||||
XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
|
||||
if(!text)
|
||||
return;
|
||||
w = 0;
|
||||
olen = len = strlen(text);
|
||||
if(len >= sizeof buf)
|
||||
len = sizeof buf - 1;
|
||||
memcpy(buf, text, len);
|
||||
buf[len] = 0;
|
||||
h = dc.font.ascent + dc.font.descent;
|
||||
y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
|
||||
x = dc.x + (h / 2);
|
||||
/* shorten text if necessary */
|
||||
while(len && (w = textnw(buf, len)) > dc.w - h)
|
||||
buf[--len] = 0;
|
||||
if(len < olen) {
|
||||
if(len > 1)
|
||||
buf[len - 1] = '.';
|
||||
if(len > 2)
|
||||
buf[len - 2] = '.';
|
||||
if(len > 3)
|
||||
buf[len - 3] = '.';
|
||||
}
|
||||
if(w > dc.w)
|
||||
return; /* too long */
|
||||
XSetForeground(dpy, dc.gc, col[ColFG]);
|
||||
if(dc.font.set)
|
||||
XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
|
||||
else
|
||||
XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
|
||||
}
|
||||
|
||||
unsigned int
|
||||
textw(const char *text) {
|
||||
return textnw(text, strlen(text)) + dc.font.height;
|
||||
}
|
||||
|
||||
unsigned int
|
||||
textnw(const char *text, unsigned int len) {
|
||||
XRectangle r;
|
||||
|
||||
if(dc.font.set) {
|
||||
XmbTextExtents(dc.font.set, text, len, NULL, &r);
|
||||
return r.width;
|
||||
}
|
||||
return XTextWidth(dc.font.xfont, text, len);
|
||||
}
|
36
util.c
36
util.c
@ -1,36 +0,0 @@
|
||||
/* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
|
||||
* © 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
|
||||
* See LICENSE file for license details. */
|
||||
#include "dmenu.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void *
|
||||
emalloc(unsigned int size) {
|
||||
void *res = malloc(size);
|
||||
|
||||
if(!res)
|
||||
eprint("fatal: could not malloc() %u bytes\n", size);
|
||||
return res;
|
||||
}
|
||||
|
||||
void
|
||||
eprint(const char *errstr, ...) {
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, errstr);
|
||||
vfprintf(stderr, errstr, ap);
|
||||
va_end(ap);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
char *
|
||||
estrdup(const char *str) {
|
||||
void *res = strdup(str);
|
||||
|
||||
if(!res)
|
||||
eprint("fatal: could not malloc() %u bytes\n", strlen(str));
|
||||
return res;
|
||||
}
|
Reference in New Issue
Block a user