Compare commits
108 Commits
Author | SHA1 | Date | |
---|---|---|---|
56f9e26b72 | |||
d094ebea96 | |||
ccf4d7166f | |||
06ae894434 | |||
b97783b07f | |||
11cb2e7dcc | |||
07239bbddd | |||
124bfd4a08 | |||
d27e3c1092 | |||
3a9f3a51ce | |||
53e92b5c17 | |||
d50ff5ca11 | |||
383e40dc21 | |||
8369e1736b | |||
c04b688cc0 | |||
4ebd7c4a21 | |||
dfe95cb546 | |||
8b633bf17d | |||
64697cdd0c | |||
5a3dfb1c40 | |||
4042a11e51 | |||
aa2f73fc88 | |||
f189781bbd | |||
0e19146d5f | |||
85a151c6a4 | |||
1fa4fa386a | |||
e1515a2fdc | |||
f555908cef | |||
73120414e2 | |||
9911455f5b | |||
2772c51e79 | |||
d57c873f10 | |||
724f72142f | |||
5b8dce15d3 | |||
063c3d26e9 | |||
03c2b05bb2 | |||
8a066fabd9 | |||
6c0e05eb0d | |||
900d34347b | |||
4d0cc17794 | |||
28b6ea0f67 | |||
598d72fba9 | |||
007be12f2b | |||
6b5b580aff | |||
d8b48d64e1 | |||
a71424ac0e | |||
f1ab687c62 | |||
68a24f5535 | |||
47b37834c4 | |||
5f436fdcb7 | |||
de34639883 | |||
18592825d2 | |||
3bc1f99e3c | |||
8a10b35c64 | |||
e4fc905317 | |||
a1913a6af7 | |||
66b2e8379f | |||
665488a6ee | |||
1e01ca0317 | |||
f3c12a7bff | |||
3ba8b71aba | |||
2bac5599b3 | |||
dbccf6fbff | |||
aea9bfec5e | |||
6175a39e8e | |||
2c7ff85f24 | |||
447046f7ae | |||
b76632b9e3 | |||
f8f5b27036 | |||
d6bf35caad | |||
507c030b5b | |||
0245394e4d | |||
b6cd6ed266 | |||
09813fcf2c | |||
64d68b7de9 | |||
eaf8909378 | |||
c0fcfb7827 | |||
a45d6983ab | |||
08420a935b | |||
deea1fb5ab | |||
771c0cb607 | |||
43a19425af | |||
bbfd5391ac | |||
0e96ae6f48 | |||
989649b5a5 | |||
f8d2a29a20 | |||
3438af001a | |||
201b056e39 | |||
3dcdbe2a57 | |||
5336608a86 | |||
d398a2e318 | |||
2829009138 | |||
cae2fbdf38 | |||
225cd764c3 | |||
279966a16f | |||
9bd80b3968 | |||
65912f2a96 | |||
4bd3466215 | |||
e19e42adbb | |||
796c4fb9e5 | |||
93c3f930c5 | |||
c69f397bb9 | |||
b9fbd59c8a | |||
da2b90ddd1 | |||
dd7ee0dae1 | |||
250aa199bb | |||
5d43e9243c | |||
1026eb8308 |
20
.hgtags
20
.hgtags
@ -12,3 +12,23 @@ e8c1e9733752db12f2dbd1fa93c46f5806242ba9 1.1
|
||||
bee7fe6d1189174d0204ca3195b83cdc1bb4f82e 1.2
|
||||
2eb9997be51cb1b11a8900728ccc0904f9371157 1.3
|
||||
df3fbb050004c544d14e43c36f6a94cca6ed4a69 1.4
|
||||
e071fb045bd9e8574947acff7196360bc0270e68 1.5
|
||||
dcc5427f99f51a978386a0dd770467cd911ac84b 1.6
|
||||
58dbef4aef3d45c7a3da6945e53c9667c0f02d5b 1.7
|
||||
3696d77aaf02f5d15728dde3b9e35abcaf291496 1.7.1
|
||||
d3e6fa22ae45b38b1bdb0d813390365e5930360b 1.8
|
||||
c7f5f4d543170f03d70468e98a3a0ec8d2c4161b 1.9
|
||||
1fce5c464fcd870b9f024aa1853d5cf3a3eb371b 2.0
|
||||
7656557298c954469a6a9564e6649b1fb5db663e 2.1
|
||||
90f0e34e7f118c9ad3227a1606211ee825942b1c 2.2
|
||||
b6e09682c8adcb6569656bee73c311f9ab457715 2.3
|
||||
9e9036cbfb4b7306c6fb366249e81dc0e65bdfde 2.4
|
||||
03e83e2788c83ddd63b45a667939d7ec783c98cb 2.4.1
|
||||
1ca5d430524e838c52ede912533cb90108c5cd66 2.4.2
|
||||
041143e9fc544c62edc58af52cae9ac5237e5945 2.5
|
||||
775f761a5647a05038e091d1c99fc35d3034cd68 2.6
|
||||
fbd9e9d63f202afe6834ccfdf890904f1897ec0b 2.7
|
||||
dd3d02b07cac44fbafc074a361c1002cebe7aae4 2.8
|
||||
59b3024854db49739c6d237fa9077f04a2da847a 3.0
|
||||
8f0f917ac988164e1b4446236e3a6ab6cfcb8c67 3.1
|
||||
e4c81a78ffbad6ba4d1ad119cc654da6eca63a4c 3.2
|
||||
|
4
LICENSE
4
LICENSE
@ -1,7 +1,7 @@
|
||||
MIT/X Consortium License
|
||||
|
||||
(C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
|
||||
(C)opyright MMVI Sander van Dijk <a dot h dot vandijk at gmail dot com>
|
||||
© 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
|
||||
© 2006-2007 Sander van Dijk <a dot h dot vandijk 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"),
|
||||
|
21
Makefile
21
Makefile
@ -1,9 +1,9 @@
|
||||
# dmenu - dynamic menu
|
||||
# (C)opyright MMVI Anselm R. Garbe
|
||||
# © 2006-2007 Anselm R. Garbe, Sander van Dijk
|
||||
|
||||
include config.mk
|
||||
|
||||
SRC = draw.c main.c util.c
|
||||
SRC = dmenu.c
|
||||
OBJ = ${SRC:.c=.o}
|
||||
|
||||
all: options dmenu
|
||||
@ -13,18 +13,16 @@ options:
|
||||
@echo "CFLAGS = ${CFLAGS}"
|
||||
@echo "LDFLAGS = ${LDFLAGS}"
|
||||
@echo "CC = ${CC}"
|
||||
@echo "LD = ${LD}"
|
||||
|
||||
.c.o:
|
||||
@echo CC $<
|
||||
@${CC} -c ${CFLAGS} $<
|
||||
|
||||
${OBJ}: dmenu.h config.mk
|
||||
${OBJ}: config.h config.mk
|
||||
|
||||
dmenu: ${OBJ}
|
||||
@echo LD $@
|
||||
@${LD} -o $@ ${OBJ} ${LDFLAGS}
|
||||
@strip $@
|
||||
@echo CC -o $@
|
||||
@${CC} -o $@ ${OBJ} ${LDFLAGS}
|
||||
|
||||
clean:
|
||||
@echo cleaning
|
||||
@ -33,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 ${SRC} dmenu-${VERSION}
|
||||
@cp -R LICENSE Makefile README config.mk dmenu.1 config.h dmenu_path ${SRC} dmenu-${VERSION}
|
||||
@tar -cf dmenu-${VERSION}.tar dmenu-${VERSION}
|
||||
@gzip dmenu-${VERSION}.tar
|
||||
@rm -rf dmenu-${VERSION}
|
||||
@ -41,16 +39,17 @@ dist: clean
|
||||
install: all
|
||||
@echo installing executable file to ${DESTDIR}${PREFIX}/bin
|
||||
@mkdir -p ${DESTDIR}${PREFIX}/bin
|
||||
@cp -f dmenu ${DESTDIR}${PREFIX}/bin
|
||||
@cp -f dmenu dmenu_path ${DESTDIR}${PREFIX}/bin
|
||||
@chmod 755 ${DESTDIR}${PREFIX}/bin/dmenu
|
||||
@chmod 755 ${DESTDIR}${PREFIX}/bin/dmenu_path
|
||||
@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
|
||||
@sed "s/VERSION/${VERSION}/g" < dmenu.1 > ${DESTDIR}${MANPREFIX}/man1/dmenu.1
|
||||
@chmod 644 ${DESTDIR}${MANPREFIX}/man1/dmenu.1
|
||||
|
||||
uninstall:
|
||||
@echo removing executable file from ${DESTDIR}${PREFIX}/bin
|
||||
@rm -f ${DESTDIR}${PREFIX}/bin/dmenu
|
||||
@rm -f ${DESTDIR}${PREFIX}/bin/dmenu ${DESTDIR}${PREFIX}/bin/dmenu_path
|
||||
@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-*-*-12-*-*-*-*-*-iso10646-*"
|
||||
#define NORMBGCOLOR "#000"
|
||||
#define NORMFGCOLOR "#ccc"
|
||||
#define SELBGCOLOR "#00f"
|
||||
#define SELFGCOLOR "#fff"
|
||||
/* next macro defines the space between menu items */
|
||||
#define SPACE 30 /* px */
|
10
config.mk
10
config.mk
@ -1,5 +1,5 @@
|
||||
# dmenu version
|
||||
VERSION = 1.5
|
||||
VERSION = 3.3
|
||||
|
||||
# Customize below to fit your system
|
||||
|
||||
@ -16,10 +16,14 @@ LIBS = -L/usr/lib -lc -L${X11LIB} -lX11
|
||||
|
||||
# flags
|
||||
CFLAGS = -Os ${INCS} -DVERSION=\"${VERSION}\"
|
||||
LDFLAGS = ${LIBS}
|
||||
LDFLAGS = -s ${LIBS}
|
||||
#CFLAGS = -g -Wall -O2 ${INCS} -DVERSION=\"${VERSION}\"
|
||||
#LDFLAGS = -g ${LIBS}
|
||||
|
||||
# Solaris
|
||||
#CFLAGS = -fast ${INCS} -DVERSION=\"${VERSION}\"
|
||||
#LDFLAGS = ${LIBS}
|
||||
#CFLAGS += -xtarget=ultra
|
||||
|
||||
# compiler and linker
|
||||
CC = cc
|
||||
LD = ${CC}
|
||||
|
64
dmenu.1
64
dmenu.1
@ -1,14 +1,15 @@
|
||||
.TH DMENU 1 dmenu-VERSION
|
||||
.TH DMENU 1 dmenu\-3.2
|
||||
.SH NAME
|
||||
dmenu \- dynamic menu
|
||||
.SH SYNOPSIS
|
||||
.B dmenu
|
||||
.RB [ \-font " <name>"]
|
||||
.RB [ \-normbg " <color>"]
|
||||
.RB [ \-normfg " <color>"]
|
||||
.RB [ \-selbg " <color>"]
|
||||
.RB [ \-selfg " <color>"]
|
||||
.RB [ \-t " <seconds>"]
|
||||
.RB [ \-b ]
|
||||
.RB [ \-fn " <font>"]
|
||||
.RB [ \-nb " <color>"]
|
||||
.RB [ \-nf " <color>"]
|
||||
.RB [ \-p " <prompt>"]
|
||||
.RB [ \-sb " <color>"]
|
||||
.RB [ \-sf " <color>"]
|
||||
.RB [ \-v ]
|
||||
.SH DESCRIPTION
|
||||
.SS Overview
|
||||
@ -18,29 +19,32 @@ It manages huge amounts (up to 10.000 and more) of user defined menu items
|
||||
efficiently.
|
||||
.SS Options
|
||||
.TP
|
||||
.B \-font <name>
|
||||
.B \-b
|
||||
makes dmenu appear at the screen bottom (by default it appears at the screen top).
|
||||
.TP
|
||||
.B \-fn <font>
|
||||
defines the font.
|
||||
.TP
|
||||
.B \-normbg <color>
|
||||
.B \-nb <color>
|
||||
defines the normal background color (#RGB, #RRGGBB, and color names are supported).
|
||||
.TP
|
||||
.B \-normfg <color>
|
||||
.B \-nf <color>
|
||||
defines the normal foreground color (#RGB, #RRGGBB, and color names are supported).
|
||||
.TP
|
||||
.B \-selbg <color>
|
||||
.B \-p <prompt>
|
||||
defines a prompt to be displayed before the input area.
|
||||
.TP
|
||||
.B \-sb <color>
|
||||
defines the selected background color (#RGB, #RRGGBB, and color names are supported).
|
||||
.TP
|
||||
.B \-selfg <color>
|
||||
.B \-sf <color>
|
||||
defines the selected foreground color (#RGB, #RRGGBB, and color names are supported).
|
||||
.TP
|
||||
.B \-t <seconds>
|
||||
defines the seconds to wait for standard input, before exiting (default is 3).
|
||||
.TP
|
||||
.B \-v
|
||||
prints version information to standard output, then exits.
|
||||
.SH USAGE
|
||||
dmenu reads a list of newline-separated items from standard input and creates a
|
||||
menu. When the user selects an item or enters any text and presses Return, his
|
||||
menu. When the user selects an item or enters any text and presses Return, his/her
|
||||
choice is printed to standard output and dmenu terminates.
|
||||
.P
|
||||
dmenu is completely controlled by the keyboard. The following keys are recognized:
|
||||
@ -49,32 +53,42 @@ dmenu is completely controlled by the keyboard. The following keys are recognize
|
||||
Appends the character to the text in the input field. This works as a filter:
|
||||
only items containing this text will be displayed.
|
||||
.TP
|
||||
.B Left/Right
|
||||
.B Left/Right (Mod1\-h/Mod1\-l)
|
||||
Select the previous/next item.
|
||||
.TP
|
||||
.B Tab
|
||||
.B PageUp/PageDown (Mod1\-k/Mod1\-j)
|
||||
Select the first item of the previous/next 'page' of items.
|
||||
.TP
|
||||
.B Home/End (Mod1\-g/Mod1\-G)
|
||||
Select the first/last item.
|
||||
.TP
|
||||
.B Tab (Control\-i)
|
||||
Copy the selected item to the input field.
|
||||
.TP
|
||||
.B Return
|
||||
.B Return (Control\-j)
|
||||
Confirm selection and quit (print the selected item to standard output). Returns
|
||||
.B 0
|
||||
on termination.
|
||||
.TP
|
||||
.B Shift-Return
|
||||
.B Shift\-Return (Control\-Shift\-j)
|
||||
Confirm selection and quit (print the text in the input field to standard output).
|
||||
Returns
|
||||
.B 0
|
||||
on termination.
|
||||
.TP
|
||||
.B Escape
|
||||
.B Escape (Control\-bracketleft)
|
||||
Quit without selecting an item. Returns
|
||||
.B 1
|
||||
on termination.
|
||||
.TP
|
||||
.B Backspace (Control-h)
|
||||
Remove enough characters from the input field to change its filtering effect.
|
||||
.B Backspace (Control\-h)
|
||||
Remove a character from the input field.
|
||||
.TP
|
||||
.B Control-u
|
||||
.B Control\-u
|
||||
Remove all characters from the input field.
|
||||
.TP
|
||||
.B Control\-w
|
||||
Remove all characters of current word from the input field.
|
||||
.SH SEE ALSO
|
||||
.BR dwm (1)
|
||||
.BR dwm (1),
|
||||
.BR wmii (1) .
|
||||
|
711
dmenu.c
Normal file
711
dmenu.c
Normal file
@ -0,0 +1,711 @@
|
||||
/* See LICENSE file for copyright and license details. */
|
||||
#include <ctype.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 {
|
||||
Item *next; /* traverses all items */
|
||||
Item *left, *right; /* traverses items matching current search pattern */
|
||||
char *text;
|
||||
};
|
||||
|
||||
/* forward declarations */
|
||||
void calcoffsets(void);
|
||||
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(Bool bottom);
|
||||
int strido(const char *text, const char *pattern);
|
||||
unsigned int textnw(const char *text, unsigned int len);
|
||||
unsigned int textw(const char *text);
|
||||
|
||||
#include "config.h"
|
||||
|
||||
/* 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 nitem = 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;
|
||||
|
||||
void
|
||||
calcoffsets(void) {
|
||||
unsigned int tw, w;
|
||||
|
||||
if(!curr)
|
||||
return;
|
||||
w = promptw + cmdw + 2 * SPACE;
|
||||
for(next = curr; next; next=next->right) {
|
||||
tw = textw(next->text);
|
||||
if(tw > mw / 3)
|
||||
tw = mw / 3;
|
||||
w += tw;
|
||||
if(w > mw)
|
||||
break;
|
||||
}
|
||||
w = promptw + cmdw + 2 * SPACE;
|
||||
for(prev = curr; prev && prev->left; prev=prev->left) {
|
||||
tw = textw(prev->left->text);
|
||||
if(tw > mw / 3)
|
||||
tw = mw / 3;
|
||||
w += tw;
|
||||
if(w > mw)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
dc.x = 0;
|
||||
dc.y = 0;
|
||||
dc.w = mw;
|
||||
dc.h = mh;
|
||||
drawtext(NULL, dc.norm);
|
||||
/* print prompt? */
|
||||
if(promptw) {
|
||||
dc.w = promptw;
|
||||
drawtext(prompt, dc.sel);
|
||||
}
|
||||
dc.x += promptw;
|
||||
dc.w = mw - promptw;
|
||||
/* print command */
|
||||
if(cmdw && item)
|
||||
dc.w = cmdw;
|
||||
drawtext(text[0] ? text : NULL, dc.norm);
|
||||
dc.x += cmdw;
|
||||
if(curr) {
|
||||
dc.w = SPACE;
|
||||
drawtext((curr && 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;
|
||||
drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
|
||||
dc.x += dc.w;
|
||||
}
|
||||
dc.x = mw - SPACE;
|
||||
dc.w = SPACE;
|
||||
drawtext(next ? ">" : NULL, dc.norm);
|
||||
}
|
||||
XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
|
||||
XFlush(dpy);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
for(len = 1000; len; len--) {
|
||||
if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
|
||||
== GrabSuccess)
|
||||
break;
|
||||
usleep(1000);
|
||||
}
|
||||
return len > 0;
|
||||
}
|
||||
|
||||
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);
|
||||
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))
|
||||
&& !(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;
|
||||
}
|
||||
dc.font.height = dc.font.ascent + dc.font.descent;
|
||||
}
|
||||
|
||||
void
|
||||
kpress(XKeyEvent * e) {
|
||||
char buf[32];
|
||||
int i, num;
|
||||
unsigned int len;
|
||||
KeySym ksym;
|
||||
|
||||
len = strlen(text);
|
||||
buf[0] = 0;
|
||||
num = XLookupString(e, buf, sizeof buf, &ksym, 0);
|
||||
if(IsKeypadKey(ksym)) {
|
||||
if(ksym == XK_KP_Enter) {
|
||||
ksym = XK_Return;
|
||||
} 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))
|
||||
return;
|
||||
/* first check if a control mask is omitted */
|
||||
if(e->state & ControlMask) {
|
||||
switch (ksym) {
|
||||
default: /* ignore other control sequences */
|
||||
return;
|
||||
case XK_bracketleft:
|
||||
ksym = XK_Escape;
|
||||
break;
|
||||
case XK_h:
|
||||
case XK_H:
|
||||
ksym = XK_BackSpace;
|
||||
break;
|
||||
case XK_i:
|
||||
case XK_I:
|
||||
ksym = XK_Tab;
|
||||
break;
|
||||
case XK_j:
|
||||
case XK_J:
|
||||
ksym = XK_Return;
|
||||
break;
|
||||
case XK_u:
|
||||
case XK_U:
|
||||
text[0] = 0;
|
||||
match(text);
|
||||
drawmenu();
|
||||
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) {
|
||||
switch(ksym) {
|
||||
default: return;
|
||||
case XK_h:
|
||||
ksym = XK_Left;
|
||||
break;
|
||||
case XK_l:
|
||||
ksym = XK_Right;
|
||||
break;
|
||||
case XK_j:
|
||||
ksym = XK_Next;
|
||||
break;
|
||||
case XK_k:
|
||||
ksym = XK_Prior;
|
||||
break;
|
||||
case XK_g:
|
||||
ksym = XK_Home;
|
||||
break;
|
||||
case XK_G:
|
||||
ksym = XK_End;
|
||||
break;
|
||||
}
|
||||
}
|
||||
switch(ksym) {
|
||||
default:
|
||||
if(num && !iscntrl((int) buf[0])) {
|
||||
buf[num] = 0;
|
||||
if(len > 0)
|
||||
strncat(text, buf, sizeof text);
|
||||
else
|
||||
strncpy(text, buf, sizeof text);
|
||||
match(text);
|
||||
}
|
||||
break;
|
||||
case XK_BackSpace:
|
||||
if(len) {
|
||||
text[--len] = 0;
|
||||
match(text);
|
||||
}
|
||||
break;
|
||||
case XK_End:
|
||||
if(!item)
|
||||
return;
|
||||
while(next) {
|
||||
sel = curr = next;
|
||||
calcoffsets();
|
||||
}
|
||||
while(sel && sel->right)
|
||||
sel = sel->right;
|
||||
break;
|
||||
case XK_Escape:
|
||||
ret = 1;
|
||||
running = False;
|
||||
break;
|
||||
case XK_Home:
|
||||
if(!item)
|
||||
return;
|
||||
sel = curr = item;
|
||||
calcoffsets();
|
||||
break;
|
||||
case XK_Left:
|
||||
if(!(sel && sel->left))
|
||||
return;
|
||||
sel=sel->left;
|
||||
if(sel->right == curr) {
|
||||
curr = prev;
|
||||
calcoffsets();
|
||||
}
|
||||
break;
|
||||
case XK_Next:
|
||||
if(!next)
|
||||
return;
|
||||
sel = curr = next;
|
||||
calcoffsets();
|
||||
break;
|
||||
case XK_Prior:
|
||||
if(!prev)
|
||||
return;
|
||||
sel = curr = prev;
|
||||
calcoffsets();
|
||||
break;
|
||||
case XK_Return:
|
||||
if((e->state & ShiftMask) && text)
|
||||
fprintf(stdout, "%s", text);
|
||||
else if(sel)
|
||||
fprintf(stdout, "%s", sel->text);
|
||||
else if(text)
|
||||
fprintf(stdout, "%s", text);
|
||||
fflush(stdout);
|
||||
running = False;
|
||||
break;
|
||||
case XK_Right:
|
||||
if(!(sel && sel->right))
|
||||
return;
|
||||
sel=sel->right;
|
||||
if(sel == next) {
|
||||
curr = next;
|
||||
calcoffsets();
|
||||
}
|
||||
break;
|
||||
case XK_Tab:
|
||||
if(!sel)
|
||||
return;
|
||||
strncpy(text, sel->text, sizeof text);
|
||||
match(text);
|
||||
break;
|
||||
}
|
||||
drawmenu();
|
||||
}
|
||||
|
||||
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++;
|
||||
}
|
||||
for(i = allitems; i; i=i->next)
|
||||
if(plen && strncmp(pattern, i->text, plen)
|
||||
&& !strstr(i->text, pattern)
|
||||
&& strido(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();
|
||||
}
|
||||
|
||||
void
|
||||
readstdin(void) {
|
||||
char *p, buf[1024];
|
||||
unsigned int len = 0, max = 0;
|
||||
Item *i, *new;
|
||||
|
||||
i = 0;
|
||||
while(fgets(buf, sizeof buf, stdin)) {
|
||||
len = strlen(buf);
|
||||
if (buf[len - 1] == '\n')
|
||||
buf[len - 1] = 0;
|
||||
p = estrdup(buf);
|
||||
if(max < len) {
|
||||
maxname = p;
|
||||
max = len;
|
||||
}
|
||||
new = emalloc(sizeof(Item));
|
||||
new->next = new->left = new->right = NULL;
|
||||
new->text = p;
|
||||
if(!i)
|
||||
allitems = new;
|
||||
else
|
||||
i->next = new;
|
||||
i = new;
|
||||
}
|
||||
}
|
||||
|
||||
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(Bool bottom) {
|
||||
unsigned int i, j;
|
||||
XModifierKeymap *modmap;
|
||||
XSetWindowAttributes wa;
|
||||
|
||||
/* init modifier map */
|
||||
modmap = XGetModifierMapping(dpy);
|
||||
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] = 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);
|
||||
mh = dc.font.height + 2;
|
||||
win = XCreateWindow(dpy, root, 0,
|
||||
bottom ? DisplayHeight(dpy, screen) - mh : 0, 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);
|
||||
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;
|
||||
if(prompt)
|
||||
promptw = textw(prompt);
|
||||
if(promptw > mw / 5)
|
||||
promptw = mw / 5;
|
||||
text[0] = 0;
|
||||
match(text);
|
||||
XMapRaised(dpy, win);
|
||||
}
|
||||
|
||||
int
|
||||
strido(const char *text, const char *pattern) {
|
||||
for(; *text && *pattern; text++)
|
||||
if (*text == *pattern)
|
||||
pattern++;
|
||||
return !*pattern;
|
||||
}
|
||||
|
||||
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[]) {
|
||||
Bool bottom = False;
|
||||
unsigned int i;
|
||||
|
||||
/* command line args */
|
||||
for(i = 1; i < argc; i++)
|
||||
if(!strcmp(argv[i], "-b")) {
|
||||
bottom = True;
|
||||
}
|
||||
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], "-v"))
|
||||
eprint("dmenu-"VERSION", © 2006-2007 Anselm R. Garbe, Sander van Dijk\n");
|
||||
else
|
||||
eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
|
||||
" [-p <prompt>] [-sb <color>] [-sf <color>] [-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(bottom);
|
||||
drawmenu();
|
||||
XSync(dpy, False);
|
||||
run();
|
||||
cleanup();
|
||||
XCloseDisplay(dpy);
|
||||
return ret;
|
||||
}
|
52
dmenu.h
52
dmenu.h
@ -1,52 +0,0 @@
|
||||
/* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
|
||||
* See LICENSE file for license details.
|
||||
*/
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xlocale.h>
|
||||
|
||||
#define FONT "fixed"
|
||||
#define NORMBGCOLOR "#333366"
|
||||
#define NORMFGCOLOR "#cccccc"
|
||||
#define SELBGCOLOR "#666699"
|
||||
#define SELFGCOLOR "#eeeeee"
|
||||
#define SPACE 30 /* px */
|
||||
|
||||
/* color */
|
||||
enum { ColFG, ColBG, ColLast };
|
||||
|
||||
typedef struct DC DC;
|
||||
typedef struct Fnt Fnt;
|
||||
|
||||
struct Fnt {
|
||||
XFontStruct *xfont;
|
||||
XFontSet set;
|
||||
int ascent;
|
||||
int descent;
|
||||
int height;
|
||||
};
|
||||
|
||||
struct DC {
|
||||
int x, y, w, h;
|
||||
unsigned long norm[ColLast];
|
||||
unsigned long sel[ColLast];
|
||||
Drawable drawable;
|
||||
Fnt font;
|
||||
GC gc;
|
||||
}; /* draw context */
|
||||
|
||||
extern int screen;
|
||||
extern Display *dpy;
|
||||
extern DC dc; /* global drawing context */
|
||||
|
||||
/* draw.c */
|
||||
extern void drawtext(const char *text,
|
||||
unsigned long col[ColLast]); /* draws text with the defined color tuple */
|
||||
extern unsigned long getcolor(const char *colstr); /* returns color of colstr */
|
||||
extern void setfont(const char *fontstr); /* sets global font */
|
||||
extern unsigned int textw(const char *text); /* returns width of text in px */
|
||||
|
||||
/* util.c */
|
||||
extern void *emalloc(unsigned int size); /* allocates memory, exits on error */
|
||||
extern void eprint(const char *errstr, ...); /* prints errstr and exits with 1 */
|
||||
extern char *estrdup(const char *str); /* duplicates str, exits on allocation error */
|
26
dmenu_path
Executable file
26
dmenu_path
Executable file
@ -0,0 +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 > $CACHE.$$
|
||||
mv $CACHE.$$ $CACHE
|
||||
fi
|
||||
|
||||
cat $CACHE
|
131
draw.c
131
draw.c
@ -1,131 +0,0 @@
|
||||
/* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
|
||||
* See LICENSE file for license details.
|
||||
*/
|
||||
#include "dmenu.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <X11/Xlocale.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 */
|
||||
|
||||
void
|
||||
drawtext(const char *text, unsigned long col[ColLast]) {
|
||||
int x, y, w, h;
|
||||
static char buf[256];
|
||||
unsigned int len, olen;
|
||||
XGCValues gcv;
|
||||
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 */
|
||||
gcv.foreground = col[ColFG];
|
||||
if(dc.font.set) {
|
||||
XChangeGC(dpy, dc.gc, GCForeground, &gcv);
|
||||
XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc,
|
||||
x, y, buf, len);
|
||||
}
|
||||
else {
|
||||
gcv.font = dc.font.xfont->fid;
|
||||
XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
|
||||
XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
|
||||
}
|
||||
}
|
||||
|
||||
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 **missing, *def;
|
||||
int i, n;
|
||||
|
||||
missing = NULL;
|
||||
setlocale(LC_ALL, "");
|
||||
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) {
|
||||
XFreeFontSet(dpy, dc.font.set);
|
||||
dc.font.set = NULL;
|
||||
}
|
||||
}
|
||||
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;
|
||||
dc.font.xfont = XLoadQueryFont(dpy, fontstr);
|
||||
if (!dc.font.xfont)
|
||||
dc.font.xfont = XLoadQueryFont(dpy, "fixed");
|
||||
if (!dc.font.xfont)
|
||||
eprint("error, cannot init 'fixed' font\n");
|
||||
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
|
||||
textw(const char *text) {
|
||||
return textnw(text, strlen(text)) + dc.font.height;
|
||||
}
|
394
main.c
394
main.c
@ -1,394 +0,0 @@
|
||||
/* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
|
||||
* (C)opyright MMVI Sander van Dijk <a dot h dot vandijk at gmail dot com>
|
||||
* See LICENSE file for license details.
|
||||
*/
|
||||
#include "dmenu.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/time.h>
|
||||
#include <X11/Xutil.h>
|
||||
#include <X11/keysym.h>
|
||||
|
||||
typedef struct Item Item;
|
||||
struct Item {
|
||||
Item *next; /* traverses all items */
|
||||
Item *left, *right; /* traverses items matching current search pattern */
|
||||
char *text;
|
||||
};
|
||||
|
||||
/* static */
|
||||
|
||||
static char text[4096];
|
||||
static int mx, my, mw, mh;
|
||||
static int ret = 0;
|
||||
static int nitem = 0;
|
||||
static unsigned int cmdw = 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;
|
||||
|
||||
static void
|
||||
calcoffsets(void) {
|
||||
unsigned int tw, w;
|
||||
|
||||
if(!curr)
|
||||
return;
|
||||
w = cmdw + 2 * SPACE;
|
||||
for(next = curr; next; next=next->right) {
|
||||
tw = textw(next->text);
|
||||
if(tw > mw / 3)
|
||||
tw = mw / 3;
|
||||
w += tw;
|
||||
if(w > mw)
|
||||
break;
|
||||
}
|
||||
w = cmdw + 2 * SPACE;
|
||||
for(prev = curr; prev && prev->left; prev=prev->left) {
|
||||
tw = textw(prev->left->text);
|
||||
if(tw > mw / 3)
|
||||
tw = mw / 3;
|
||||
w += tw;
|
||||
if(w > mw)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
drawmenu(void) {
|
||||
Item *i;
|
||||
|
||||
dc.x = 0;
|
||||
dc.y = 0;
|
||||
dc.w = mw;
|
||||
dc.h = mh;
|
||||
drawtext(NULL, dc.norm);
|
||||
/* print command */
|
||||
if(cmdw && item)
|
||||
dc.w = cmdw;
|
||||
drawtext(text[0] ? text : NULL, dc.norm);
|
||||
dc.x += cmdw;
|
||||
if(curr) {
|
||||
dc.w = SPACE;
|
||||
drawtext((curr && 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;
|
||||
drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
|
||||
dc.x += dc.w;
|
||||
}
|
||||
dc.x = mw - SPACE;
|
||||
dc.w = SPACE;
|
||||
drawtext(next ? ">" : NULL, dc.norm);
|
||||
}
|
||||
XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
|
||||
XFlush(dpy);
|
||||
}
|
||||
|
||||
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
|
||||
kpress(XKeyEvent * e) {
|
||||
char buf[32];
|
||||
int num, prev_nitem;
|
||||
unsigned int i, len;
|
||||
KeySym ksym;
|
||||
|
||||
len = strlen(text);
|
||||
buf[0] = 0;
|
||||
num = XLookupString(e, buf, sizeof buf, &ksym, 0);
|
||||
if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
|
||||
|| IsMiscFunctionKey(ksym) || IsPFKey(ksym)
|
||||
|| IsPrivateKeypadKey(ksym))
|
||||
return;
|
||||
/* first check if a control mask is omitted */
|
||||
if(e->state & ControlMask) {
|
||||
switch (ksym) {
|
||||
default: /* ignore other control sequences */
|
||||
return;
|
||||
break;
|
||||
case XK_h:
|
||||
case XK_H:
|
||||
ksym = XK_BackSpace;
|
||||
break;
|
||||
case XK_u:
|
||||
case XK_U:
|
||||
text[0] = 0;
|
||||
match(text);
|
||||
drawmenu();
|
||||
return;
|
||||
break;
|
||||
}
|
||||
}
|
||||
switch(ksym) {
|
||||
case XK_Left:
|
||||
if(!(sel && sel->left))
|
||||
return;
|
||||
sel=sel->left;
|
||||
if(sel->right == curr) {
|
||||
curr = prev;
|
||||
calcoffsets();
|
||||
}
|
||||
break;
|
||||
case XK_Tab:
|
||||
if(!sel)
|
||||
return;
|
||||
strncpy(text, sel->text, sizeof text);
|
||||
match(text);
|
||||
break;
|
||||
case XK_Right:
|
||||
if(!(sel && sel->right))
|
||||
return;
|
||||
sel=sel->right;
|
||||
if(sel == next) {
|
||||
curr = next;
|
||||
calcoffsets();
|
||||
}
|
||||
break;
|
||||
case XK_Return:
|
||||
if((e->state & ShiftMask) && text)
|
||||
fprintf(stdout, "%s", text);
|
||||
else if(sel)
|
||||
fprintf(stdout, "%s", sel->text);
|
||||
else if(text)
|
||||
fprintf(stdout, "%s", text);
|
||||
fflush(stdout);
|
||||
running = False;
|
||||
break;
|
||||
case XK_Escape:
|
||||
ret = 1;
|
||||
running = False;
|
||||
break;
|
||||
case XK_BackSpace:
|
||||
if((i = len)) {
|
||||
prev_nitem = nitem;
|
||||
do {
|
||||
text[--i] = 0;
|
||||
match(text);
|
||||
} while(i && nitem && prev_nitem == nitem);
|
||||
match(text);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if(num && !iscntrl((int) buf[0])) {
|
||||
buf[num] = 0;
|
||||
if(len > 0)
|
||||
strncat(text, buf, sizeof text);
|
||||
else
|
||||
strncpy(text, buf, sizeof text);
|
||||
match(text);
|
||||
}
|
||||
}
|
||||
drawmenu();
|
||||
}
|
||||
|
||||
static char *
|
||||
readstdin(void) {
|
||||
static char *maxname = NULL;
|
||||
char *p, buf[1024];
|
||||
unsigned int len = 0, max = 0;
|
||||
Item *i, *new;
|
||||
|
||||
i = 0;
|
||||
while(fgets(buf, sizeof buf, stdin)) {
|
||||
len = strlen(buf);
|
||||
if (buf[len - 1] == '\n')
|
||||
buf[len - 1] = 0;
|
||||
p = estrdup(buf);
|
||||
if(max < len) {
|
||||
maxname = p;
|
||||
max = len;
|
||||
}
|
||||
new = emalloc(sizeof(Item));
|
||||
new->next = new->left = new->right = NULL;
|
||||
new->text = p;
|
||||
if(!i)
|
||||
allitems = new;
|
||||
else
|
||||
i->next = new;
|
||||
i = new;
|
||||
}
|
||||
|
||||
return maxname;
|
||||
}
|
||||
|
||||
/* extern */
|
||||
|
||||
int screen;
|
||||
Display *dpy;
|
||||
DC dc = {0};
|
||||
|
||||
int
|
||||
main(int argc, char *argv[]) {
|
||||
char *font = FONT;
|
||||
char *maxname;
|
||||
char *normbg = NORMBGCOLOR;
|
||||
char *normfg = NORMFGCOLOR;
|
||||
char *selbg = SELBGCOLOR;
|
||||
char *selfg = SELFGCOLOR;
|
||||
fd_set rd;
|
||||
int i;
|
||||
struct timeval timeout;
|
||||
Item *itm;
|
||||
XEvent ev;
|
||||
XSetWindowAttributes wa;
|
||||
|
||||
timeout.tv_usec = 0;
|
||||
timeout.tv_sec = 3;
|
||||
/* command line args */
|
||||
for(i = 1; i < argc; i++)
|
||||
if(!strncmp(argv[i], "-font", 6)) {
|
||||
if(++i < argc) font = argv[i];
|
||||
}
|
||||
else if(!strncmp(argv[i], "-normbg", 8)) {
|
||||
if(++i < argc) normbg = argv[i];
|
||||
}
|
||||
else if(!strncmp(argv[i], "-normfg", 8)) {
|
||||
if(++i < argc) normfg = argv[i];
|
||||
}
|
||||
else if(!strncmp(argv[i], "-selbg", 7)) {
|
||||
if(++i < argc) selbg = argv[i];
|
||||
}
|
||||
else if(!strncmp(argv[i], "-selfg", 7)) {
|
||||
if(++i < argc) selfg = argv[i];
|
||||
}
|
||||
else if(!strncmp(argv[i], "-t", 3)) {
|
||||
if(++i < argc) timeout.tv_sec = atoi(argv[i]);
|
||||
}
|
||||
else if(!strncmp(argv[i], "-v", 3)) {
|
||||
fputs("dmenu-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
else
|
||||
eprint("usage: dmenu [-font <name>] [-{norm,sel}{bg,fg} <color>] [-t <seconds>] [-v]\n", stdout);
|
||||
dpy = XOpenDisplay(0);
|
||||
if(!dpy)
|
||||
eprint("dmenu: cannot open display\n");
|
||||
screen = DefaultScreen(dpy);
|
||||
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,
|
||||
GrabModeAsync, CurrentTime) != GrabSuccess)
|
||||
usleep(1000);
|
||||
FD_ZERO(&rd);
|
||||
FD_SET(STDIN_FILENO, &rd);
|
||||
if(select(ConnectionNumber(dpy) + 1, &rd, NULL, NULL, &timeout) < 1)
|
||||
goto UninitializedEnd;
|
||||
maxname = readstdin();
|
||||
/* style */
|
||||
dc.norm[ColBG] = getcolor(normbg);
|
||||
dc.norm[ColFG] = getcolor(normfg);
|
||||
dc.sel[ColBG] = getcolor(selbg);
|
||||
dc.sel[ColFG] = getcolor(selfg);
|
||||
setfont(font);
|
||||
/* menu window */
|
||||
wa.override_redirect = 1;
|
||||
wa.background_pixmap = ParentRelative;
|
||||
wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
|
||||
mx = my = 0;
|
||||
mw = DisplayWidth(dpy, screen);
|
||||
mh = dc.font.height + 2;
|
||||
win = XCreateWindow(dpy, root, mx, my, 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);
|
||||
XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
|
||||
if(maxname)
|
||||
cmdw = textw(maxname);
|
||||
if(cmdw > mw / 3)
|
||||
cmdw = mw / 3;
|
||||
text[0] = 0;
|
||||
match(text);
|
||||
XMapRaised(dpy, win);
|
||||
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);
|
||||
UninitializedEnd:
|
||||
XUngrabKeyboard(dpy, CurrentTime);
|
||||
XCloseDisplay(dpy);
|
||||
return ret;
|
||||
}
|
38
util.c
38
util.c
@ -1,38 +0,0 @@
|
||||
/* (C)opyright MMVI Anselm R. Garbe <garbeam 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>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.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