added draw.h
This commit is contained in:
		
							
								
								
									
										6
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								Makefile
									
									
									
									
									
								
							@@ -6,7 +6,7 @@ include config.mk
 | 
				
			|||||||
SRC = dinput.c dmenu.c draw.c
 | 
					SRC = dinput.c dmenu.c draw.c
 | 
				
			||||||
OBJ = ${SRC:.c=.o}
 | 
					OBJ = ${SRC:.c=.o}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
all: options dinput dmenu
 | 
					all: options draw.o dinput dmenu
 | 
				
			||||||
 | 
					
 | 
				
			||||||
options:
 | 
					options:
 | 
				
			||||||
	@echo dmenu build options:
 | 
						@echo dmenu build options:
 | 
				
			||||||
@@ -18,7 +18,7 @@ options:
 | 
				
			|||||||
	@echo CC $<
 | 
						@echo CC $<
 | 
				
			||||||
	@${CC} -c ${CFLAGS} $<
 | 
						@${CC} -c ${CFLAGS} $<
 | 
				
			||||||
 | 
					
 | 
				
			||||||
${OBJ}: config.h config.mk draw.c
 | 
					${OBJ}: config.h config.mk draw.h
 | 
				
			||||||
 | 
					
 | 
				
			||||||
config.h:
 | 
					config.h:
 | 
				
			||||||
	@echo creating $@ from config.def.h
 | 
						@echo creating $@ from config.def.h
 | 
				
			||||||
@@ -26,7 +26,7 @@ config.h:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
.o:
 | 
					.o:
 | 
				
			||||||
	@echo CC -o $@
 | 
						@echo CC -o $@
 | 
				
			||||||
	@${CC} -o $@ $< ${LDFLAGS}
 | 
						@${CC} -o $@ $< draw.o ${LDFLAGS}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
clean:
 | 
					clean:
 | 
				
			||||||
	@echo cleaning
 | 
						@echo cleaning
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										23
									
								
								dinput.c
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								dinput.c
									
									
									
									
									
								
							@@ -25,32 +25,27 @@
 | 
				
			|||||||
static void cleanup(void);
 | 
					static void cleanup(void);
 | 
				
			||||||
static void drawcursor(void);
 | 
					static void drawcursor(void);
 | 
				
			||||||
static void drawinput(void);
 | 
					static void drawinput(void);
 | 
				
			||||||
static void eprint(const char *errstr, ...);
 | 
					 | 
				
			||||||
static Bool grabkeyboard(void);
 | 
					static Bool grabkeyboard(void);
 | 
				
			||||||
static void kpress(XKeyEvent * e);
 | 
					static void kpress(XKeyEvent * e);
 | 
				
			||||||
static void run(void);
 | 
					static void run(void);
 | 
				
			||||||
static void setup(Bool topbar);
 | 
					static void setup(Bool topbar);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "config.h"
 | 
					#include "config.h"
 | 
				
			||||||
 | 
					#include "draw.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* variables */
 | 
					/* variables */
 | 
				
			||||||
static char *prompt = NULL;
 | 
					static char *prompt = NULL;
 | 
				
			||||||
static char text[4096];
 | 
					static char text[4096];
 | 
				
			||||||
static int promptw = 0;
 | 
					static int promptw = 0;
 | 
				
			||||||
static int ret = 0;
 | 
					static int ret = 0;
 | 
				
			||||||
static int screen;
 | 
					 | 
				
			||||||
static unsigned int mw, mh;
 | 
					 | 
				
			||||||
static unsigned int cursor = 0;
 | 
					static unsigned int cursor = 0;
 | 
				
			||||||
static unsigned int numlockmask = 0;
 | 
					static unsigned int numlockmask = 0;
 | 
				
			||||||
static Bool running = True;
 | 
					static Bool running = True;
 | 
				
			||||||
static Display *dpy;
 | 
					static Window win;
 | 
				
			||||||
static Window parent, win;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#include "draw.c"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
cleanup(void) {
 | 
					cleanup(void) {
 | 
				
			||||||
	dccleanup();
 | 
						drawcleanup();
 | 
				
			||||||
	XDestroyWindow(dpy, win);
 | 
						XDestroyWindow(dpy, win);
 | 
				
			||||||
	XUngrabKeyboard(dpy, CurrentTime);
 | 
						XUngrabKeyboard(dpy, CurrentTime);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -86,16 +81,6 @@ drawinput(void)
 | 
				
			|||||||
	XFlush(dpy);
 | 
						XFlush(dpy);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					 | 
				
			||||||
eprint(const char *errstr, ...) {
 | 
					 | 
				
			||||||
	va_list ap;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	va_start(ap, errstr);
 | 
					 | 
				
			||||||
	vfprintf(stderr, errstr, ap);
 | 
					 | 
				
			||||||
	va_end(ap);
 | 
					 | 
				
			||||||
	exit(EXIT_FAILURE);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
Bool
 | 
					Bool
 | 
				
			||||||
grabkeyboard(void) {
 | 
					grabkeyboard(void) {
 | 
				
			||||||
	unsigned int len;
 | 
						unsigned int len;
 | 
				
			||||||
@@ -318,7 +303,7 @@ setup(Bool topbar) {
 | 
				
			|||||||
			DefaultVisual(dpy, screen),
 | 
								DefaultVisual(dpy, screen),
 | 
				
			||||||
			CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
 | 
								CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	dcsetup();
 | 
						drawsetup();
 | 
				
			||||||
	if(prompt)
 | 
						if(prompt)
 | 
				
			||||||
		promptw = MIN(textw(prompt), mw / 5);
 | 
							promptw = MIN(textw(prompt), mw / 5);
 | 
				
			||||||
	cursor = strlen(text);
 | 
						cursor = strlen(text);
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										22
									
								
								dmenu.c
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								dmenu.c
									
									
									
									
									
								
							@@ -46,6 +46,7 @@ static void run(void);
 | 
				
			|||||||
static void setup(Bool topbar);
 | 
					static void setup(Bool topbar);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "config.h"
 | 
					#include "config.h"
 | 
				
			||||||
 | 
					#include "draw.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* variables */
 | 
					/* variables */
 | 
				
			||||||
static char *maxname = NULL;
 | 
					static char *maxname = NULL;
 | 
				
			||||||
@@ -54,25 +55,20 @@ static char text[4096];
 | 
				
			|||||||
static int cmdw = 0;
 | 
					static int cmdw = 0;
 | 
				
			||||||
static int promptw = 0;
 | 
					static int promptw = 0;
 | 
				
			||||||
static int ret = 0;
 | 
					static int ret = 0;
 | 
				
			||||||
static int screen;
 | 
					 | 
				
			||||||
static unsigned int mw, mh;
 | 
					 | 
				
			||||||
static unsigned int numlockmask = 0;
 | 
					static unsigned int numlockmask = 0;
 | 
				
			||||||
static Bool running = True;
 | 
					static Bool running = True;
 | 
				
			||||||
static Display *dpy;
 | 
					 | 
				
			||||||
static Item *allitems = NULL;  /* first of all items */
 | 
					static Item *allitems = NULL;  /* first of all items */
 | 
				
			||||||
static Item *item = NULL;      /* first of pattern matching items */
 | 
					static Item *item = NULL;      /* first of pattern matching items */
 | 
				
			||||||
static Item *sel = NULL;
 | 
					static Item *sel = NULL;
 | 
				
			||||||
static Item *next = NULL;
 | 
					static Item *next = NULL;
 | 
				
			||||||
static Item *prev = NULL;
 | 
					static Item *prev = NULL;
 | 
				
			||||||
static Item *curr = NULL;
 | 
					static Item *curr = NULL;
 | 
				
			||||||
static Window parent, win;
 | 
					static Window win;
 | 
				
			||||||
static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
 | 
					static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
 | 
				
			||||||
static char *(*fstrstr)(const char *, const char *) = strstr;
 | 
					static char *(*fstrstr)(const char *, const char *) = strstr;
 | 
				
			||||||
static unsigned int lines = 0;
 | 
					static unsigned int lines = 0;
 | 
				
			||||||
static void (*calcoffsets)(void) = calcoffsetsh;
 | 
					static void (*calcoffsets)(void) = calcoffsetsh;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "draw.c"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
appenditem(Item *i, Item **list, Item **last) {
 | 
					appenditem(Item *i, Item **list, Item **last) {
 | 
				
			||||||
	if(!(*last))
 | 
						if(!(*last))
 | 
				
			||||||
@@ -136,7 +132,7 @@ cistrstr(const char *s, const char *sub) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
cleanup(void) {
 | 
					cleanup(void) {
 | 
				
			||||||
	dccleanup();
 | 
						drawcleanup();
 | 
				
			||||||
	XDestroyWindow(dpy, win);
 | 
						XDestroyWindow(dpy, win);
 | 
				
			||||||
	XUngrabKeyboard(dpy, CurrentTime);
 | 
						XUngrabKeyboard(dpy, CurrentTime);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -202,16 +198,6 @@ drawmenuv(void) {
 | 
				
			|||||||
	drawtext(NULL, dc.norm);
 | 
						drawtext(NULL, dc.norm);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					 | 
				
			||||||
eprint(const char *errstr, ...) {
 | 
					 | 
				
			||||||
	va_list ap;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	va_start(ap, errstr);
 | 
					 | 
				
			||||||
	vfprintf(stderr, errstr, ap);
 | 
					 | 
				
			||||||
	va_end(ap);
 | 
					 | 
				
			||||||
	exit(EXIT_FAILURE);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
Bool
 | 
					Bool
 | 
				
			||||||
grabkeyboard(void) {
 | 
					grabkeyboard(void) {
 | 
				
			||||||
	unsigned int len;
 | 
						unsigned int len;
 | 
				
			||||||
@@ -529,7 +515,7 @@ setup(Bool topbar) {
 | 
				
			|||||||
			DefaultVisual(dpy, screen),
 | 
								DefaultVisual(dpy, screen),
 | 
				
			||||||
			CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
 | 
								CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	dcsetup();
 | 
						drawsetup();
 | 
				
			||||||
	if(maxname)
 | 
						if(maxname)
 | 
				
			||||||
		cmdw = MIN(textw(maxname), mw / 3);
 | 
							cmdw = MIN(textw(maxname), mw / 3);
 | 
				
			||||||
	if(prompt)
 | 
						if(prompt)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										55
									
								
								draw.c
									
									
									
									
									
								
							
							
						
						
									
										55
									
								
								draw.c
									
									
									
									
									
								
							@@ -1,37 +1,20 @@
 | 
				
			|||||||
/* See LICENSE file for copyright and license details. */
 | 
					/* See LICENSE file for copyright and license details. */
 | 
				
			||||||
 | 
					#include <ctype.h>
 | 
				
			||||||
 | 
					#include <locale.h>
 | 
				
			||||||
 | 
					#include <stdarg.h>
 | 
				
			||||||
 | 
					#include <stdio.h>
 | 
				
			||||||
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					#include <strings.h>
 | 
				
			||||||
 | 
					#include <X11/Xlib.h>
 | 
				
			||||||
 | 
					#include "draw.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* enums */
 | 
					/* macros */
 | 
				
			||||||
enum { ColFG, ColBG, ColLast };
 | 
					#define MIN(a, b)               ((a) < (b) ? (a) : (b))
 | 
				
			||||||
 | 
					#define MAX(a, b)               ((a) > (b) ? (a) : (b))
 | 
				
			||||||
/* 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 */
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/* forward declarations */
 | 
					 | 
				
			||||||
static void dccleanup(void);
 | 
					 | 
				
			||||||
static void dcsetup(void);
 | 
					 | 
				
			||||||
static void drawtext(const char *text, unsigned long col[ColLast]);
 | 
					 | 
				
			||||||
static unsigned long getcolor(const char *colstr);
 | 
					 | 
				
			||||||
static void initfont(const char *fontstr);
 | 
					 | 
				
			||||||
static int textnw(const char *text, unsigned int len);
 | 
					 | 
				
			||||||
static int textw(const char *text);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
static DC dc;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
dccleanup(void) {
 | 
					drawcleanup(void) {
 | 
				
			||||||
	if(dc.font.set)
 | 
						if(dc.font.set)
 | 
				
			||||||
		XFreeFontSet(dpy, dc.font.set);
 | 
							XFreeFontSet(dpy, dc.font.set);
 | 
				
			||||||
	else
 | 
						else
 | 
				
			||||||
@@ -41,7 +24,7 @@ dccleanup(void) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
dcsetup(void) {
 | 
					drawsetup(void) {
 | 
				
			||||||
	/* style */
 | 
						/* style */
 | 
				
			||||||
	dc.norm[ColBG] = getcolor(normbgcolor);
 | 
						dc.norm[ColBG] = getcolor(normbgcolor);
 | 
				
			||||||
	dc.norm[ColFG] = getcolor(normfgcolor);
 | 
						dc.norm[ColFG] = getcolor(normfgcolor);
 | 
				
			||||||
@@ -84,6 +67,16 @@ drawtext(const char *text, unsigned long col[ColLast]) {
 | 
				
			|||||||
		XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
 | 
							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
 | 
					unsigned long
 | 
				
			||||||
getcolor(const char *colstr) {
 | 
					getcolor(const char *colstr) {
 | 
				
			||||||
	Colormap cmap = DefaultColormap(dpy, screen);
 | 
						Colormap cmap = DefaultColormap(dpy, screen);
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										45
									
								
								draw.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								draw.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,45 @@
 | 
				
			|||||||
 | 
					/* See LICENSE file for copyright and license details. */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* 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 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* forward declarations */
 | 
				
			||||||
 | 
					void drawcleanup(void);
 | 
				
			||||||
 | 
					void drawsetup(void);
 | 
				
			||||||
 | 
					void drawtext(const char *text, unsigned long col[ColLast]);
 | 
				
			||||||
 | 
					void eprint(const char *errstr, ...);
 | 
				
			||||||
 | 
					unsigned long getcolor(const char *colstr);
 | 
				
			||||||
 | 
					void initfont(const char *fontstr);
 | 
				
			||||||
 | 
					int textnw(const char *text, unsigned int len);
 | 
				
			||||||
 | 
					int textw(const char *text);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* variables */
 | 
				
			||||||
 | 
					Display *dpy;
 | 
				
			||||||
 | 
					DC dc;
 | 
				
			||||||
 | 
					int screen;
 | 
				
			||||||
 | 
					unsigned int mw, mh;
 | 
				
			||||||
 | 
					unsigned int spaceitem;
 | 
				
			||||||
 | 
					Window parent;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* style */
 | 
				
			||||||
 | 
					const char *font;
 | 
				
			||||||
 | 
					const char *normbgcolor;
 | 
				
			||||||
 | 
					const char *normfgcolor;
 | 
				
			||||||
 | 
					const char *selbgcolor;
 | 
				
			||||||
 | 
					const char *selfgcolor;
 | 
				
			||||||
		Reference in New Issue
	
	Block a user