aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile6
-rw-r--r--dinput.c23
-rw-r--r--dmenu.c22
-rw-r--r--draw.c57
-rw-r--r--draw.h45
5 files changed, 81 insertions, 72 deletions
diff --git a/Makefile b/Makefile
index d5884da..2f1cada 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@ include config.mk
6SRC = dinput.c dmenu.c draw.c 6SRC = dinput.c dmenu.c draw.c
7OBJ = ${SRC:.c=.o} 7OBJ = ${SRC:.c=.o}
8 8
9all: options dinput dmenu 9all: options draw.o dinput dmenu
10 10
11options: 11options:
12 @echo dmenu build options: 12 @echo dmenu build options:
@@ -18,7 +18,7 @@ options:
18 @echo CC $< 18 @echo CC $<
19 @${CC} -c ${CFLAGS} $< 19 @${CC} -c ${CFLAGS} $<
20 20
21${OBJ}: config.h config.mk draw.c 21${OBJ}: config.h config.mk draw.h
22 22
23config.h: 23config.h:
24 @echo creating $@ from config.def.h 24 @echo creating $@ from config.def.h
@@ -26,7 +26,7 @@ config.h:
26 26
27.o: 27.o:
28 @echo CC -o $@ 28 @echo CC -o $@
29 @${CC} -o $@ $< ${LDFLAGS} 29 @${CC} -o $@ $< draw.o ${LDFLAGS}
30 30
31clean: 31clean:
32 @echo cleaning 32 @echo cleaning
diff --git a/dinput.c b/dinput.c
index 52118e5..f2b504a 100644
--- a/dinput.c
+++ b/dinput.c
@@ -25,32 +25,27 @@
25static void cleanup(void); 25static void cleanup(void);
26static void drawcursor(void); 26static void drawcursor(void);
27static void drawinput(void); 27static void drawinput(void);
28static void eprint(const char *errstr, ...);
29static Bool grabkeyboard(void); 28static Bool grabkeyboard(void);
30static void kpress(XKeyEvent * e); 29static void kpress(XKeyEvent * e);
31static void run(void); 30static void run(void);
32static void setup(Bool topbar); 31static void setup(Bool topbar);
33 32
34#include "config.h" 33#include "config.h"
34#include "draw.h"
35 35
36/* variables */ 36/* variables */
37static char *prompt = NULL; 37static char *prompt = NULL;
38static char text[4096]; 38static char text[4096];
39static int promptw = 0; 39static int promptw = 0;
40static int ret = 0; 40static int ret = 0;
41static int screen;
42static unsigned int mw, mh;
43static unsigned int cursor = 0; 41static unsigned int cursor = 0;
44static unsigned int numlockmask = 0; 42static unsigned int numlockmask = 0;
45static Bool running = True; 43static Bool running = True;
46static Display *dpy; 44static Window win;
47static Window parent, win;
48
49#include "draw.c"
50 45
51void 46void
52cleanup(void) { 47cleanup(void) {
53 dccleanup(); 48 drawcleanup();
54 XDestroyWindow(dpy, win); 49 XDestroyWindow(dpy, win);
55 XUngrabKeyboard(dpy, CurrentTime); 50 XUngrabKeyboard(dpy, CurrentTime);
56} 51}
@@ -86,16 +81,6 @@ drawinput(void)
86 XFlush(dpy); 81 XFlush(dpy);
87} 82}
88 83
89void
90eprint(const char *errstr, ...) {
91 va_list ap;
92
93 va_start(ap, errstr);
94 vfprintf(stderr, errstr, ap);
95 va_end(ap);
96 exit(EXIT_FAILURE);
97}
98
99Bool 84Bool
100grabkeyboard(void) { 85grabkeyboard(void) {
101 unsigned int len; 86 unsigned int len;
@@ -318,7 +303,7 @@ setup(Bool topbar) {
318 DefaultVisual(dpy, screen), 303 DefaultVisual(dpy, screen),
319 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); 304 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
320 305
321 dcsetup(); 306 drawsetup();
322 if(prompt) 307 if(prompt)
323 promptw = MIN(textw(prompt), mw / 5); 308 promptw = MIN(textw(prompt), mw / 5);
324 cursor = strlen(text); 309 cursor = strlen(text);
diff --git a/dmenu.c b/dmenu.c
index 7672eac..0d89474 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -46,6 +46,7 @@ static void run(void);
46static void setup(Bool topbar); 46static void setup(Bool topbar);
47 47
48#include "config.h" 48#include "config.h"
49#include "draw.h"
49 50
50/* variables */ 51/* variables */
51static char *maxname = NULL; 52static char *maxname = NULL;
@@ -54,25 +55,20 @@ static char text[4096];
54static int cmdw = 0; 55static int cmdw = 0;
55static int promptw = 0; 56static int promptw = 0;
56static int ret = 0; 57static int ret = 0;
57static int screen;
58static unsigned int mw, mh;
59static unsigned int numlockmask = 0; 58static unsigned int numlockmask = 0;
60static Bool running = True; 59static Bool running = True;
61static Display *dpy;
62static Item *allitems = NULL; /* first of all items */ 60static Item *allitems = NULL; /* first of all items */
63static Item *item = NULL; /* first of pattern matching items */ 61static Item *item = NULL; /* first of pattern matching items */
64static Item *sel = NULL; 62static Item *sel = NULL;
65static Item *next = NULL; 63static Item *next = NULL;
66static Item *prev = NULL; 64static Item *prev = NULL;
67static Item *curr = NULL; 65static Item *curr = NULL;
68static Window parent, win; 66static Window win;
69static int (*fstrncmp)(const char *, const char *, size_t) = strncmp; 67static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
70static char *(*fstrstr)(const char *, const char *) = strstr; 68static char *(*fstrstr)(const char *, const char *) = strstr;
71static unsigned int lines = 0; 69static unsigned int lines = 0;
72static void (*calcoffsets)(void) = calcoffsetsh; 70static void (*calcoffsets)(void) = calcoffsetsh;
73 71
74#include "draw.c"
75
76void 72void
77appenditem(Item *i, Item **list, Item **last) { 73appenditem(Item *i, Item **list, Item **last) {
78 if(!(*last)) 74 if(!(*last))
@@ -136,7 +132,7 @@ cistrstr(const char *s, const char *sub) {
136 132
137void 133void
138cleanup(void) { 134cleanup(void) {
139 dccleanup(); 135 drawcleanup();
140 XDestroyWindow(dpy, win); 136 XDestroyWindow(dpy, win);
141 XUngrabKeyboard(dpy, CurrentTime); 137 XUngrabKeyboard(dpy, CurrentTime);
142} 138}
@@ -202,16 +198,6 @@ drawmenuv(void) {
202 drawtext(NULL, dc.norm); 198 drawtext(NULL, dc.norm);
203} 199}
204 200
205void
206eprint(const char *errstr, ...) {
207 va_list ap;
208
209 va_start(ap, errstr);
210 vfprintf(stderr, errstr, ap);
211 va_end(ap);
212 exit(EXIT_FAILURE);
213}
214
215Bool 201Bool
216grabkeyboard(void) { 202grabkeyboard(void) {
217 unsigned int len; 203 unsigned int len;
@@ -529,7 +515,7 @@ setup(Bool topbar) {
529 DefaultVisual(dpy, screen), 515 DefaultVisual(dpy, screen),
530 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); 516 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
531 517
532 dcsetup(); 518 drawsetup();
533 if(maxname) 519 if(maxname)
534 cmdw = MIN(textw(maxname), mw / 3); 520 cmdw = MIN(textw(maxname), mw / 3);
535 if(prompt) 521 if(prompt)
diff --git a/draw.c b/draw.c
index 932b2f6..688bd69 100644
--- a/draw.c
+++ b/draw.c
@@ -1,37 +1,20 @@
1/* See LICENSE file for copyright and license details. */ 1/* See LICENSE file for copyright and license details. */
2 2#include <ctype.h>
3/* enums */ 3#include <locale.h>
4enum { ColFG, ColBG, ColLast }; 4#include <stdarg.h>
5 5#include <stdio.h>
6/* typedefs */ 6#include <stdlib.h>
7typedef struct { 7#include <string.h>
8 int x, y, w, h; 8#include <strings.h>
9 unsigned long norm[ColLast]; 9#include <X11/Xlib.h>
10 unsigned long sel[ColLast]; 10#include "draw.h"
11 Drawable drawable; 11
12 GC gc; 12/* macros */
13 struct { 13#define MIN(a, b) ((a) < (b) ? (a) : (b))
14 XFontStruct *xfont; 14#define MAX(a, b) ((a) > (b) ? (a) : (b))
15 XFontSet set;
16 int ascent;
17 int descent;
18 int height;
19 } font;
20} DC; /* draw context */
21
22/* forward declarations */
23static void dccleanup(void);
24static void dcsetup(void);
25static void drawtext(const char *text, unsigned long col[ColLast]);
26static unsigned long getcolor(const char *colstr);
27static void initfont(const char *fontstr);
28static int textnw(const char *text, unsigned int len);
29static int textw(const char *text);
30
31static DC dc;
32 15
33void 16void
34dccleanup(void) { 17drawcleanup(void) {
35 if(dc.font.set) 18 if(dc.font.set)
36 XFreeFontSet(dpy, dc.font.set); 19 XFreeFontSet(dpy, dc.font.set);
37 else 20 else
@@ -41,7 +24,7 @@ dccleanup(void) {
41} 24}
42 25
43void 26void
44dcsetup(void) { 27drawsetup(void) {
45 /* style */ 28 /* style */
46 dc.norm[ColBG] = getcolor(normbgcolor); 29 dc.norm[ColBG] = getcolor(normbgcolor);
47 dc.norm[ColFG] = getcolor(normfgcolor); 30 dc.norm[ColFG] = getcolor(normfgcolor);
@@ -84,6 +67,16 @@ drawtext(const char *text, unsigned long col[ColLast]) {
84 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len); 67 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
85} 68}
86 69
70void
71eprint(const char *errstr, ...) {
72 va_list ap;
73
74 va_start(ap, errstr);
75 vfprintf(stderr, errstr, ap);
76 va_end(ap);
77 exit(EXIT_FAILURE);
78}
79
87unsigned long 80unsigned long
88getcolor(const char *colstr) { 81getcolor(const char *colstr) {
89 Colormap cmap = DefaultColormap(dpy, screen); 82 Colormap cmap = DefaultColormap(dpy, screen);
diff --git a/draw.h b/draw.h
new file mode 100644
index 0000000..79db37e
--- /dev/null
+++ b/draw.h
@@ -0,0 +1,45 @@
1/* See LICENSE file for copyright and license details. */
2
3/* enums */
4enum { ColFG, ColBG, ColLast };
5
6/* typedefs */
7typedef struct {
8 int x, y, w, h;
9 unsigned long norm[ColLast];
10 unsigned long sel[ColLast];
11 Drawable drawable;
12 GC gc;
13 struct {
14 XFontStruct *xfont;
15 XFontSet set;
16 int ascent;
17 int descent;
18 int height;
19 } font;
20} DC; /* draw context */
21
22/* forward declarations */
23void drawcleanup(void);
24void drawsetup(void);
25void drawtext(const char *text, unsigned long col[ColLast]);
26void eprint(const char *errstr, ...);
27unsigned long getcolor(const char *colstr);
28void initfont(const char *fontstr);
29int textnw(const char *text, unsigned int len);
30int textw(const char *text);
31
32/* variables */
33Display *dpy;
34DC dc;
35int screen;
36unsigned int mw, mh;
37unsigned int spaceitem;
38Window parent;
39
40/* style */
41const char *font;
42const char *normbgcolor;
43const char *normfgcolor;
44const char *selbgcolor;
45const char *selfgcolor;