suckless-patches / dmenu-xrdb.diff

diff -r 2b9683c50723 -r ebcc0d8213e0 dmenu.c
--- a/dmenu.c	Wed Dec 01 20:25:10 2010 +0000
+++ b/dmenu.c	Tue Dec 14 15:53:44 2010 +0300
@@ -7,6 +7,7 @@
 #include <X11/Xlib.h>
 #include <X11/Xatom.h>
 #include <X11/Xutil.h>
+#include <X11/Xresource.h>
 #ifdef XINERAMA
 #include <X11/extensions/Xinerama.h>
 #endif
@@ -37,6 +38,7 @@
 static void run(void);
 static void setup(void);
 static void usage(void);
+static void read_resourses(void);
 
 static char text[BUFSIZ];
 static int bh, mw, mh;
@@ -47,10 +49,10 @@
 static size_t cursor = 0;
 static const char *font = NULL;
 static const char *prompt = NULL;
-static const char *normbgcolor = "#cccccc";
-static const char *normfgcolor = "#000000";
-static const char *selbgcolor  = "#0066ff";
-static const char *selfgcolor  = "#ffffff";
+static const char *normbgcolor = NULL;
+static const char *normfgcolor = NULL;
+static const char *selbgcolor  = NULL;
+static const char *selfgcolor  = NULL;
 static unsigned long normcol[ColLast];
 static unsigned long selcol[ColLast];
 static Atom utf8;
@@ -101,6 +103,7 @@
 			usage();
 
 	dc = initdc();
+	read_resourses();
 	initfont(dc, font);
 	readstdin();
 	setup();
@@ -109,6 +112,42 @@
 	return EXIT_FAILURE;  /* should not reach */
 }
 
+/* Set font and colors from X resources database if they are not set
+ * from command line */
+void
+read_resourses(void) {
+	XrmDatabase xdb;
+	char* xrm;
+	char* datatype[20];
+	XrmValue xvalue;
+
+	XrmInitialize();
+	xrm = XResourceManagerString(dc->dpy);
+	if( xrm != NULL ) {
+		xdb = XrmGetStringDatabase(xrm);
+		if( font == NULL && XrmGetResource(xdb, "dmenu.font", "*", datatype, &xvalue) == True )
+			font = strdup(xvalue.addr);
+		if( normfgcolor == NULL && XrmGetResource(xdb, "dmenu.foreground", "*", datatype, &xvalue) == True )
+			normfgcolor = strdup(xvalue.addr);
+		if( normbgcolor == NULL && XrmGetResource(xdb, "dmenu.background", "*", datatype, &xvalue) == True )
+			normbgcolor = strdup(xvalue.addr);
+		if( selfgcolor == NULL && XrmGetResource(xdb, "dmenu.selforeground", "*", datatype, &xvalue) == True )
+			selfgcolor = strdup(xvalue.addr);
+		if( selbgcolor == NULL && XrmGetResource(xdb, "dmenu.selbackground", "*", datatype, &xvalue) == True )
+			selbgcolor = strdup(xvalue.addr);
+		XrmDestroyDatabase(xdb);
+	}
+	/* Set default colors if they are not set */
+	if( normbgcolor == NULL )
+		normbgcolor = "#cccccc";
+	if( normfgcolor == NULL )
+		normfgcolor = "#000000";
+	if( selbgcolor == NULL )
+		selbgcolor  = "#0066ff";
+	if( selfgcolor == NULL )
+		selfgcolor  = "#ffffff";
+}
+
 void
 appenditem(Item *item, Item **list, Item **last) {
 	if(!*last)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.