Compare commits
104 Commits
Author | SHA1 | Date | |
---|---|---|---|
698ec259af | |||
88efbf3dd1 | |||
4d8a646958 | |||
b89a9501c7 | |||
65d655b371 | |||
2cef0ddb01 | |||
3f244b1d52 | |||
a6945d5094 | |||
8911aa1060 | |||
d2305e4b13 | |||
fcf26a38f1 | |||
4fc6cbe608 | |||
a45f106d3c | |||
596033b781 | |||
357558798c | |||
1f6af5e78f | |||
f3617bd7ca | |||
096fb3723b | |||
6423288394 | |||
7195e941c1 | |||
12805b69ac | |||
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 | |||
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 |
16
.hgtags
16
.hgtags
@ -21,3 +21,19 @@ 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
|
||||
709df5a4bad7015a346b2b44b1b3b573ea3088ff 3.3
|
||||
9ab649b3b3e5bfccf1c8f352c59e5361e070a25f 3.4
|
||||
05e5bd706b3b3e61399d57c4bb43df296a20112d 3.5
|
||||
0bc2751d06e8b95e0138854c7815e154c5c3d990 3.6
|
||||
0508a3a6ee106f36d9b8ff07bb5b28584edfa89c 3.7
|
||||
|
23
LICENSE
23
LICENSE
@ -1,22 +1,23 @@
|
||||
MIT/X Consortium License
|
||||
|
||||
(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>
|
||||
© 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"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
|
16
Makefile
16
Makefile
@ -1,9 +1,9 @@
|
||||
# dmenu - dynamic menu
|
||||
# (C)opyright MMVI-MMVII Anselm R. Garbe
|
||||
# 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 ${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,8 +39,10 @@ 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 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
|
||||
@ -49,7 +50,8 @@ install: all
|
||||
|
||||
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
|
||||
@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
|
||||
|
||||
|
9
config.h
Normal file
9
config.h
Normal file
@ -0,0 +1,9 @@
|
||||
/* See LICENSE file for copyright and license details. */
|
||||
|
||||
/* appearance */
|
||||
static const char *font = "-*-terminus-medium-r-normal-*-14-*-*-*-*-*-*-*";
|
||||
static const char *normbgcolor = "#cccccc";
|
||||
static const char *normfgcolor = "#000000";
|
||||
static const char *selbgcolor = "#0066ff";
|
||||
static const char *selfgcolor = "#ffffff";
|
||||
static unsigned int spaceitem = 30; /* px between menu items */
|
16
config.mk
16
config.mk
@ -1,5 +1,5 @@
|
||||
# dmenu version
|
||||
VERSION = 2.3
|
||||
VERSION = 3.8
|
||||
|
||||
# Customize below to fit your system
|
||||
|
||||
@ -10,20 +10,22 @@ MANPREFIX = ${PREFIX}/share/man
|
||||
X11INC = /usr/X11R6/include
|
||||
X11LIB = /usr/X11R6/lib
|
||||
|
||||
# Xinerama, comment if you don't want it
|
||||
XINERAMALIBS = -L${X11LIB} -lXinerama
|
||||
XINERAMAFLAGS = -DXINERAMA
|
||||
|
||||
# includes and libs
|
||||
INCS = -I. -I/usr/include -I${X11INC}
|
||||
LIBS = -L/usr/lib -lc -L${X11LIB} -lX11
|
||||
LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 ${XINERAMALIBS}
|
||||
|
||||
# flags
|
||||
CFLAGS = -Os ${INCS} -DVERSION=\"${VERSION}\"
|
||||
LDFLAGS = ${LIBS}
|
||||
#CFLAGS = -g -Wall -O2 ${INCS} -DVERSION=\"${VERSION}\"
|
||||
#LDFLAGS = -g ${LIBS}
|
||||
CPPFLAGS = -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS}
|
||||
CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
|
||||
LDFLAGS = -s ${LIBS}
|
||||
|
||||
# Solaris
|
||||
#CFLAGS = -fast ${INCS} -DVERSION=\"${VERSION}\"
|
||||
#LDFLAGS = ${LIBS}
|
||||
#CFLAGS += -xtarget=ultra
|
||||
|
||||
# compiler and linker
|
||||
CC = cc
|
||||
|
30
dmenu.1
30
dmenu.1
@ -1,8 +1,9 @@
|
||||
.TH DMENU 1 dmenu-VERSION
|
||||
.TH DMENU 1 dmenu\-VERSION
|
||||
.SH NAME
|
||||
dmenu \- dynamic menu
|
||||
.SH SYNOPSIS
|
||||
.B dmenu
|
||||
.RB [ \-i ]
|
||||
.RB [ \-b ]
|
||||
.RB [ \-fn " <font>"]
|
||||
.RB [ \-nb " <color>"]
|
||||
@ -19,8 +20,11 @@ It manages huge amounts (up to 10.000 and more) of user defined menu items
|
||||
efficiently.
|
||||
.SS Options
|
||||
.TP
|
||||
.B \-i
|
||||
makes dmenu match menu entries case insensitively.
|
||||
.TP
|
||||
.B \-b
|
||||
makes dmenu appear at the screen bottom (by default it appears at the screen top).
|
||||
defines that dmenu appears at the bottom.
|
||||
.TP
|
||||
.B \-fn <font>
|
||||
defines the font.
|
||||
@ -53,41 +57,41 @@ 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 (Mod1-h/Mod1-l)
|
||||
.B Left/Right (Mod1\-h/Mod1\-l)
|
||||
Select the previous/next item.
|
||||
.TP
|
||||
.B PageUp/PageDown (Mod1-k/Mod1-j)
|
||||
.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)
|
||||
.B Home/End (Mod1\-g/Mod1\-G)
|
||||
Select the first/last item.
|
||||
.TP
|
||||
.B Tab (Control-i)
|
||||
.B Tab (Control\-i)
|
||||
Copy the selected item to the input field.
|
||||
.TP
|
||||
.B Return (Control-j)
|
||||
.B Return (Control\-j)
|
||||
Confirm selection and quit (print the selected item to standard output). Returns
|
||||
.B 0
|
||||
on termination.
|
||||
.TP
|
||||
.B Shift-Return (Control-Shift-j)
|
||||
.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 (Control-bracketleft)
|
||||
.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
|
||||
.B Control\-w
|
||||
Remove all characters of current word from the input field.
|
||||
.SH SEE ALSO
|
||||
.BR dwm (1),
|
||||
|
623
main.c → dmenu.c
623
main.c → dmenu.c
@ -1,53 +1,113 @@
|
||||
/* (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.
|
||||
*/
|
||||
#include "dmenu.h"
|
||||
/* See LICENSE file for copyright and license details. */
|
||||
#define _BSD_SOURCE
|
||||
#include <ctype.h>
|
||||
#include <locale.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <unistd.h>
|
||||
#include <X11/Xutil.h>
|
||||
#include <X11/keysym.h>
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xutil.h>
|
||||
#ifdef XINERAMA
|
||||
#include <X11/extensions/Xinerama.h>
|
||||
#endif
|
||||
|
||||
#define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
|
||||
/* macros */
|
||||
#define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
|
||||
#define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
|
||||
|
||||
/* 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 */
|
||||
static void appenditem(Item *i, Item **list, Item **last);
|
||||
static void calcoffsets(void);
|
||||
static char *cistrstr(const char *s, const char *sub);
|
||||
static void cleanup(void);
|
||||
static void drawmenu(void);
|
||||
static void drawtext(const char *text, unsigned long col[ColLast]);
|
||||
static void eprint(const char *errstr, ...);
|
||||
static unsigned long getcolor(const char *colstr);
|
||||
static Bool grabkeyboard(void);
|
||||
static void initfont(const char *fontstr);
|
||||
static void kpress(XKeyEvent * e);
|
||||
static void match(char *pattern);
|
||||
static void readstdin(void);
|
||||
static void run(void);
|
||||
static void setup(Bool topbar);
|
||||
static int textnw(const char *text, unsigned int len);
|
||||
static int textw(const char *text);
|
||||
|
||||
static char text[4096];
|
||||
#include "config.h"
|
||||
|
||||
/* variables */
|
||||
static char *maxname = NULL;
|
||||
static char *prompt = NULL;
|
||||
static int mw, mh;
|
||||
static char text[4096];
|
||||
static int cmdw = 0;
|
||||
static int promptw = 0;
|
||||
static int ret = 0;
|
||||
static int nitem = 0;
|
||||
static unsigned int cmdw = 0;
|
||||
static unsigned int promptw = 0;
|
||||
static int screen;
|
||||
static unsigned int mw, mh;
|
||||
static unsigned int numlockmask = 0;
|
||||
static Bool running = True;
|
||||
static Display *dpy;
|
||||
static DC dc = {0};
|
||||
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 Window root, win;
|
||||
static int (*fstrncmp)(const char *, const char *, size_t n) = strncmp;
|
||||
static char *(*fstrstr)(const char *, const char *) = strstr;
|
||||
|
||||
static void
|
||||
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;
|
||||
int tw;
|
||||
unsigned int w;
|
||||
|
||||
if(!curr)
|
||||
return;
|
||||
w = promptw + cmdw + 2 * SPACE;
|
||||
w = promptw + cmdw + 2 * spaceitem;
|
||||
for(next = curr; next; next=next->right) {
|
||||
tw = textw(next->text);
|
||||
if(tw > mw / 3)
|
||||
@ -56,7 +116,7 @@ calcoffsets(void) {
|
||||
if(w > mw)
|
||||
break;
|
||||
}
|
||||
w = promptw + cmdw + 2 * SPACE;
|
||||
w = promptw + cmdw + 2 * spaceitem;
|
||||
for(prev = curr; prev && prev->left; prev=prev->left) {
|
||||
tw = textw(prev->left->text);
|
||||
if(tw > mw / 3)
|
||||
@ -67,7 +127,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;
|
||||
|
||||
@ -89,7 +192,7 @@ drawmenu(void) {
|
||||
drawtext(text[0] ? text : NULL, dc.norm);
|
||||
dc.x += cmdw;
|
||||
if(curr) {
|
||||
dc.w = SPACE;
|
||||
dc.w = spaceitem;
|
||||
drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
|
||||
dc.x += dc.w;
|
||||
/* determine maximum items */
|
||||
@ -100,16 +203,66 @@ drawmenu(void) {
|
||||
drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
|
||||
dc.x += dc.w;
|
||||
}
|
||||
dc.x = mw - SPACE;
|
||||
dc.w = SPACE;
|
||||
dc.x = mw - spaceitem;
|
||||
dc.w = spaceitem;
|
||||
drawtext(next ? ">" : NULL, dc.norm);
|
||||
}
|
||||
XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
|
||||
XFlush(dpy);
|
||||
}
|
||||
|
||||
static unsigned long
|
||||
initcolor(const char *colstr) {
|
||||
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
|
||||
eprint(const char *errstr, ...) {
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, errstr);
|
||||
vfprintf(stderr, errstr, ap);
|
||||
va_end(ap);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
unsigned long
|
||||
getcolor(const char *colstr) {
|
||||
Colormap cmap = DefaultColormap(dpy, screen);
|
||||
XColor color;
|
||||
|
||||
@ -118,11 +271,26 @@ initcolor(const char *colstr) {
|
||||
return color.pixel;
|
||||
}
|
||||
|
||||
static void
|
||||
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);
|
||||
@ -148,7 +316,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;
|
||||
@ -156,56 +325,25 @@ 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, prev_nitem;
|
||||
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))
|
||||
|| IsMiscFunctionKey(ksym) || IsPFKey(ksym)
|
||||
|| IsPrivateKeypadKey(ksym))
|
||||
return;
|
||||
/* first check if a control mask is omitted */
|
||||
if(e->state & ControlMask) {
|
||||
@ -282,12 +420,8 @@ kpress(XKeyEvent * e) {
|
||||
}
|
||||
break;
|
||||
case XK_BackSpace:
|
||||
if((i = len)) {
|
||||
prev_nitem = nitem;
|
||||
do {
|
||||
text[--i] = 0;
|
||||
match(text);
|
||||
} while(i && nitem && prev_nitem == nitem);
|
||||
if(len) {
|
||||
text[--len] = 0;
|
||||
match(text);
|
||||
}
|
||||
break;
|
||||
@ -333,11 +467,11 @@ kpress(XKeyEvent * e) {
|
||||
calcoffsets();
|
||||
break;
|
||||
case XK_Return:
|
||||
if((e->state & ShiftMask) && text)
|
||||
if((e->state & ShiftMask) && *text)
|
||||
fprintf(stdout, "%s", text);
|
||||
else if(sel)
|
||||
fprintf(stdout, "%s", sel->text);
|
||||
else if(text)
|
||||
else if(*text)
|
||||
fprintf(stdout, "%s", text);
|
||||
fflush(stdout);
|
||||
running = False;
|
||||
@ -361,9 +495,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;
|
||||
@ -373,12 +547,14 @@ readstdin(void) {
|
||||
len = strlen(buf);
|
||||
if (buf[len - 1] == '\n')
|
||||
buf[len - 1] = 0;
|
||||
p = estrdup(buf);
|
||||
if(!(p = strdup(buf)))
|
||||
eprint("fatal: could not strdup() %u bytes\n", strlen(buf));
|
||||
if(max < len) {
|
||||
maxname = p;
|
||||
max = len;
|
||||
}
|
||||
new = emalloc(sizeof(Item));
|
||||
if((new = (Item *)malloc(sizeof(Item))) == NULL)
|
||||
eprint("fatal: could not malloc() %u bytes\n", sizeof(Item));
|
||||
new->next = new->left = new->right = NULL;
|
||||
new->text = p;
|
||||
if(!i)
|
||||
@ -387,122 +563,11 @@ 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;
|
||||
XModifierKeymap *modmap;
|
||||
XSetWindowAttributes wa;
|
||||
|
||||
if(isatty(STDIN_FILENO)) {
|
||||
fputs("error: dmenu can't run in an interactive shell\n", stdout);
|
||||
usage();
|
||||
}
|
||||
/* 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", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
|
||||
else
|
||||
usage();
|
||||
setlocale(LC_CTYPE, "");
|
||||
dpy = XOpenDisplay(0);
|
||||
if(!dpy)
|
||||
eprint("dmenu: cannot open display\n");
|
||||
screen = DefaultScreen(dpy);
|
||||
root = RootWindow(dpy, screen);
|
||||
while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
|
||||
GrabModeAsync, CurrentTime) != GrabSuccess)
|
||||
usleep(1000);
|
||||
maxname = readstdin();
|
||||
/* 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] = initcolor(normbg);
|
||||
dc.norm[ColFG] = initcolor(normfg);
|
||||
dc.sel[ColBG] = initcolor(selbg);
|
||||
dc.sel[ColFG] = initcolor(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(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);
|
||||
drawmenu();
|
||||
XSync(dpy, False);
|
||||
|
||||
/* main event loop */
|
||||
while(running && !XNextEvent(dpy, &ev))
|
||||
@ -517,22 +582,164 @@ main(int argc, char *argv[]) {
|
||||
drawmenu();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* cleanup */
|
||||
while(allitems) {
|
||||
itm = allitems->next;
|
||||
free(allitems->text);
|
||||
free(allitems);
|
||||
allitems = itm;
|
||||
void
|
||||
setup(Bool topbar) {
|
||||
int i, j, x, y;
|
||||
#if XINERAMA
|
||||
int n;
|
||||
XineramaScreenInfo *info = NULL;
|
||||
#endif
|
||||
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(normbgcolor);
|
||||
dc.norm[ColFG] = getcolor(normfgcolor);
|
||||
dc.sel[ColBG] = getcolor(selbgcolor);
|
||||
dc.sel[ColFG] = getcolor(selfgcolor);
|
||||
initfont(font);
|
||||
|
||||
/* menu window */
|
||||
wa.override_redirect = 1;
|
||||
wa.background_pixmap = ParentRelative;
|
||||
wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
|
||||
|
||||
/* menu window geometry */
|
||||
mh = dc.font.height + 2;
|
||||
#if XINERAMA
|
||||
if(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))
|
||||
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;
|
||||
}
|
||||
x = info[i].x_org;
|
||||
y = topbar ? info[i].y_org : info[i].y_org + info[i].height - mh;
|
||||
mw = info[i].width;
|
||||
XFree(info);
|
||||
}
|
||||
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);
|
||||
#endif
|
||||
{
|
||||
x = 0;
|
||||
y = topbar ? 0 : DisplayHeight(dpy, screen) - mh;
|
||||
mw = DisplayWidth(dpy, screen);
|
||||
}
|
||||
|
||||
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);
|
||||
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
|
||||
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);
|
||||
}
|
||||
|
||||
int
|
||||
textw(const char *text) {
|
||||
return textnw(text, strlen(text)) + dc.font.height;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[]) {
|
||||
unsigned int i;
|
||||
Bool topbar = True;
|
||||
|
||||
/* command line args */
|
||||
for(i = 1; i < argc; i++)
|
||||
if(!strcmp(argv[i], "-i")) {
|
||||
fstrncmp = strncasecmp;
|
||||
fstrstr = cistrstr;
|
||||
}
|
||||
else if(!strcmp(argv[i], "-b"))
|
||||
topbar = False;
|
||||
else if(!strcmp(argv[i], "-fn")) {
|
||||
if(++i < argc) font = argv[i];
|
||||
}
|
||||
else if(!strcmp(argv[i], "-nb")) {
|
||||
if(++i < argc) normbgcolor = argv[i];
|
||||
}
|
||||
else if(!strcmp(argv[i], "-nf")) {
|
||||
if(++i < argc) normfgcolor = argv[i];
|
||||
}
|
||||
else if(!strcmp(argv[i], "-p")) {
|
||||
if(++i < argc) prompt = argv[i];
|
||||
}
|
||||
else if(!strcmp(argv[i], "-sb")) {
|
||||
if(++i < argc) selbgcolor = argv[i];
|
||||
}
|
||||
else if(!strcmp(argv[i], "-sf")) {
|
||||
if(++i < argc) selfgcolor = 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] [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
|
||||
" [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
|
||||
if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
|
||||
fprintf(stderr, "warning: no locale support\n");
|
||||
if(!(dpy = XOpenDisplay(0)))
|
||||
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(topbar);
|
||||
drawmenu();
|
||||
XSync(dpy, False);
|
||||
run();
|
||||
cleanup();
|
||||
XCloseDisplay(dpy);
|
||||
return ret;
|
||||
}
|
49
dmenu.h
49
dmenu.h
@ -1,49 +0,0 @@
|
||||
/* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam 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 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]);
|
||||
extern unsigned int textw(const char *text);
|
||||
extern unsigned int textnw(const char *text, unsigned int len);
|
||||
|
||||
/* 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" &&
|
||||
for dir in $PATH
|
||||
do
|
||||
test ! $dir -nt "$CACHE" || return 1
|
||||
done
|
||||
}
|
||||
|
||||
if ! uptodate
|
||||
then
|
||||
for dir in $PATH
|
||||
do
|
||||
cd "$dir" &&
|
||||
for file in *
|
||||
do
|
||||
test -x "$file" && echo "$file"
|
||||
done
|
||||
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 ${1+"$@"}` && exec $exe
|
71
draw.c
71
draw.c
@ -1,71 +0,0 @@
|
||||
/* (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.
|
||||
*/
|
||||
#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;
|
||||
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 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 @@
|
||||
/* (C)opyright MMVI-MMVII 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>
|
||||
|
||||
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