Compare commits

...

7 Commits
2.2 ... 2.3

8 changed files with 104 additions and 111 deletions

View File

@ -20,3 +20,4 @@ d3e6fa22ae45b38b1bdb0d813390365e5930360b 1.8
c7f5f4d543170f03d70468e98a3a0ec8d2c4161b 1.9 c7f5f4d543170f03d70468e98a3a0ec8d2c4161b 1.9
1fce5c464fcd870b9f024aa1853d5cf3a3eb371b 2.0 1fce5c464fcd870b9f024aa1853d5cf3a3eb371b 2.0
7656557298c954469a6a9564e6649b1fb5db663e 2.1 7656557298c954469a6a9564e6649b1fb5db663e 2.1
90f0e34e7f118c9ad3227a1606211ee825942b1c 2.2

View File

@ -44,7 +44,7 @@ install: all
@chmod 755 ${DESTDIR}${PREFIX}/bin/dmenu @chmod 755 ${DESTDIR}${PREFIX}/bin/dmenu
@echo installing manual page to ${DESTDIR}${MANPREFIX}/man1 @echo installing manual page to ${DESTDIR}${MANPREFIX}/man1
@mkdir -p ${DESTDIR}${MANPREFIX}/man1 @mkdir -p ${DESTDIR}${MANPREFIX}/man1
@sed 's/VERSION/${VERSION}/g' < dmenu.1 > ${DESTDIR}${MANPREFIX}/man1/dmenu.1 @sed "s/VERSION/${VERSION}/g" < dmenu.1 > ${DESTDIR}${MANPREFIX}/man1/dmenu.1
@chmod 644 ${DESTDIR}${MANPREFIX}/man1/dmenu.1 @chmod 644 ${DESTDIR}${MANPREFIX}/man1/dmenu.1
uninstall: uninstall:

View File

@ -1,5 +1,5 @@
# dmenu version # dmenu version
VERSION = 2.2 VERSION = 2.3
# Customize below to fit your system # Customize below to fit your system

View File

@ -10,7 +10,6 @@ dmenu \- dynamic menu
.RB [ \-p " <prompt>"] .RB [ \-p " <prompt>"]
.RB [ \-sb " <color>"] .RB [ \-sb " <color>"]
.RB [ \-sf " <color>"] .RB [ \-sf " <color>"]
.RB [ \-t " <seconds>"]
.RB [ \-v ] .RB [ \-v ]
.SH DESCRIPTION .SH DESCRIPTION
.SS Overview .SS Overview
@ -41,9 +40,6 @@ defines the selected background color (#RGB, #RRGGBB, and color names are suppor
.B \-sf <color> .B \-sf <color>
defines the selected foreground color (#RGB, #RRGGBB, and color names are supported). defines the selected foreground color (#RGB, #RRGGBB, and color names are supported).
.TP .TP
.B \-t <seconds>
defines the seconds to wait for standard input, before exiting (default is 3).
.TP
.B \-v .B \-v
prints version information to standard output, then exits. prints version information to standard output, then exits.
.SH USAGE .SH USAGE
@ -90,6 +86,9 @@ Remove enough characters from the input field to change its filtering effect.
.TP .TP
.B Control-u .B Control-u
Remove all characters from the input field. Remove all characters from the input field.
.TP
.B Control-w
Remove all characters of current word from the input field.
.SH SEE ALSO .SH SEE ALSO
.BR dwm (1), .BR dwm (1),
.BR wmii (1) . .BR wmii (1) .

View File

@ -39,11 +39,9 @@ extern Display *dpy;
extern DC dc; /* global drawing context */ extern DC dc; /* global drawing context */
/* draw.c */ /* draw.c */
extern void drawtext(const char *text, extern void drawtext(const char *text, unsigned long col[ColLast]);
unsigned long col[ColLast]); /* draws text with the defined color tuple */ extern unsigned int textw(const char *text);
extern unsigned long getcolor(const char *colstr); /* returns color of colstr */ extern unsigned int textnw(const char *text, unsigned int len);
extern void setfont(const char *fontstr); /* sets global font */
extern unsigned int textw(const char *text); /* returns width of text in px */
/* util.c */ /* util.c */
extern void *emalloc(unsigned int size); /* allocates memory, exits on error */ extern void *emalloc(unsigned int size); /* allocates memory, exits on error */

76
draw.c
View File

@ -1,23 +1,10 @@
/* (C)opyright MMIV-MMVII Anselm R. Garbe <garbeam at gmail dot com> /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
* (C)opyright MMVI-MMVII Sander van Dijk <a dot h dot vandijk at gmail dot com>
* See LICENSE file for license details. * See LICENSE file for license details.
*/ */
#include "dmenu.h" #include "dmenu.h"
#include <stdio.h>
#include <string.h> #include <string.h>
/* static */
static 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);
}
/* extern */ /* extern */
void void
@ -67,55 +54,18 @@ drawtext(const char *text, unsigned long col[ColLast]) {
} }
} }
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;
}
void
setfont(const char *fontstr) {
char *def, **missing;
int i, n;
missing = NULL;
if(dc.font.set)
XFreeFontSet(dpy, dc.font.set);
dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
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;
xfonts++;
}
}
else {
if(dc.font.xfont)
XFreeFont(dpy, dc.font.xfont);
dc.font.xfont = NULL;
if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
eprint("error, cannot load font: '%s'\n", fontstr);
dc.font.ascent = dc.font.xfont->ascent;
dc.font.descent = dc.font.xfont->descent;
}
dc.font.height = dc.font.ascent + dc.font.descent;
}
unsigned int unsigned int
textw(const char *text) { textw(const char *text) {
return textnw(text, strlen(text)) + dc.font.height; 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);
}

117
main.c
View File

@ -3,15 +3,12 @@
* See LICENSE file for license details. * See LICENSE file for license details.
*/ */
#include "dmenu.h" #include "dmenu.h"
#include <ctype.h> #include <ctype.h>
#include <locale.h> #include <locale.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <X11/Xutil.h> #include <X11/Xutil.h>
#include <X11/keysym.h> #include <X11/keysym.h>
@ -111,6 +108,54 @@ drawmenu(void) {
XFlush(dpy); XFlush(dpy);
} }
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
initfont(const char *fontstr) {
char *def, **missing;
int i, n;
missing = NULL;
if(dc.font.set)
XFreeFontSet(dpy, dc.font.set);
dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
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;
xfonts++;
}
}
else {
if(dc.font.xfont)
XFreeFont(dpy, dc.font.xfont);
dc.font.xfont = NULL;
if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
eprint("error, cannot load font: '%s'\n", fontstr);
dc.font.ascent = dc.font.xfont->ascent;
dc.font.descent = dc.font.xfont->descent;
}
dc.font.height = dc.font.ascent + dc.font.descent;
}
static void static void
match(char *pattern) { match(char *pattern) {
unsigned int plen; unsigned int plen;
@ -151,8 +196,8 @@ match(char *pattern) {
static void static void
kpress(XKeyEvent * e) { kpress(XKeyEvent * e) {
char buf[32]; char buf[32];
int num, prev_nitem; int i, num, prev_nitem;
unsigned int i, len; unsigned int len;
KeySym ksym; KeySym ksym;
len = strlen(text); len = strlen(text);
@ -188,6 +233,18 @@ kpress(XKeyEvent * e) {
match(text); match(text);
drawmenu(); drawmenu();
return; return;
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;
match(text);
drawmenu();
}
return;
} }
} }
if(CLEANMASK(e->state) & Mod1Mask) { if(CLEANMASK(e->state) & Mod1Mask) {
@ -334,6 +391,12 @@ readstdin(void) {
return maxname; 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 */ /* extern */
int screen; int screen;
@ -349,16 +412,16 @@ main(int argc, char *argv[]) {
char *normfg = NORMFGCOLOR; char *normfg = NORMFGCOLOR;
char *selbg = SELBGCOLOR; char *selbg = SELBGCOLOR;
char *selfg = SELFGCOLOR; char *selfg = SELFGCOLOR;
fd_set rd;
int i, j; int i, j;
struct timeval timeout;
Item *itm; Item *itm;
XEvent ev; XEvent ev;
XModifierKeymap *modmap; XModifierKeymap *modmap;
XSetWindowAttributes wa; XSetWindowAttributes wa;
timeout.tv_usec = 0; if(isatty(STDIN_FILENO)) {
timeout.tv_sec = 3; fputs("error: dmenu can't run in an interactive shell\n", stdout);
usage();
}
/* command line args */ /* command line args */
for(i = 1; i < argc; i++) for(i = 1; i < argc; i++)
if(!strncmp(argv[i], "-b", 3)) { if(!strncmp(argv[i], "-b", 3)) {
@ -382,51 +445,36 @@ main(int argc, char *argv[]) {
else if(!strncmp(argv[i], "-sf", 4)) { else if(!strncmp(argv[i], "-sf", 4)) {
if(++i < argc) selfg = argv[i]; if(++i < argc) selfg = argv[i];
} }
else if(!strncmp(argv[i], "-t", 3)) { else if(!strncmp(argv[i], "-v", 3))
if(++i < argc) timeout.tv_sec = atoi(argv[i]); eprint("dmenu-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
}
else if(!strncmp(argv[i], "-v", 3)) {
fputs("dmenu-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
exit(EXIT_SUCCESS);
}
else else
eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>] [-p <prompt>]\n" usage();
" [-sb <color>] [-sf <color>] [-t <seconds>] [-v]\n", stdout);
setlocale(LC_CTYPE, ""); setlocale(LC_CTYPE, "");
dpy = XOpenDisplay(0); dpy = XOpenDisplay(0);
if(!dpy) if(!dpy)
eprint("dmenu: cannot open display\n"); eprint("dmenu: cannot open display\n");
screen = DefaultScreen(dpy); screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen); root = RootWindow(dpy, screen);
/* Note, the select() construction allows to grab all keypresses as
* early as possible, to not loose them. But if there is no standard
* input supplied, we will make sure to exit after MAX_WAIT_STDIN
* seconds. This is convenience behavior for rapid typers.
*/
while(XGrabKeyboard(dpy, root, True, GrabModeAsync, while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
GrabModeAsync, CurrentTime) != GrabSuccess) GrabModeAsync, CurrentTime) != GrabSuccess)
usleep(1000); usleep(1000);
FD_ZERO(&rd);
FD_SET(STDIN_FILENO, &rd);
if(select(ConnectionNumber(dpy) + 1, &rd, NULL, NULL, &timeout) < 1)
goto UninitializedEnd;
maxname = readstdin(); maxname = readstdin();
/* init modifier map */ /* init modifier map */
modmap = XGetModifierMapping(dpy); modmap = XGetModifierMapping(dpy);
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
for (j = 0; j < modmap->max_keypermod; j++) { for (j = 0; j < modmap->max_keypermod; j++) {
if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock)) if(modmap->modifiermap[i * modmap->max_keypermod + j]
== XKeysymToKeycode(dpy, XK_Num_Lock))
numlockmask = (1 << i); numlockmask = (1 << i);
} }
} }
XFreeModifiermap(modmap); XFreeModifiermap(modmap);
/* style */ /* style */
dc.norm[ColBG] = getcolor(normbg); dc.norm[ColBG] = initcolor(normbg);
dc.norm[ColFG] = getcolor(normfg); dc.norm[ColFG] = initcolor(normfg);
dc.sel[ColBG] = getcolor(selbg); dc.sel[ColBG] = initcolor(selbg);
dc.sel[ColFG] = getcolor(selfg); dc.sel[ColFG] = initcolor(selfg);
setfont(font); initfont(font);
/* menu window */ /* menu window */
wa.override_redirect = 1; wa.override_redirect = 1;
wa.background_pixmap = ParentRelative; wa.background_pixmap = ParentRelative;
@ -484,7 +532,6 @@ main(int argc, char *argv[]) {
XFreePixmap(dpy, dc.drawable); XFreePixmap(dpy, dc.drawable);
XFreeGC(dpy, dc.gc); XFreeGC(dpy, dc.gc);
XDestroyWindow(dpy, win); XDestroyWindow(dpy, win);
UninitializedEnd:
XUngrabKeyboard(dpy, CurrentTime); XUngrabKeyboard(dpy, CurrentTime);
XCloseDisplay(dpy); XCloseDisplay(dpy);
return ret; return ret;

2
util.c
View File

@ -6,8 +6,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/wait.h>
#include <unistd.h>
void * void *
emalloc(unsigned int size) { emalloc(unsigned int size) {