sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

dmenu-xresources-4.9.diff (4122B)


      1 diff '--color=auto' -up ../dmenu-4.9/dmenu.c ./dmenu.c
      2 --- ../dmenu-4.9/dmenu.c	2019-02-02 13:55:02.000000000 +0100
      3 +++ ./dmenu.c	2020-05-24 00:27:58.038586112 +0200
      4 @@ -15,6 +15,7 @@
      5  #include <X11/extensions/Xinerama.h>
      6  #endif
      7  #include <X11/Xft/Xft.h>
      8 +#include <X11/Xresource.h>
      9 
     10  #include "drw.h"
     11  #include "util.h"
     12 @@ -53,6 +54,10 @@ static XIC xic;
     13  static Drw *drw;
     14  static Clr *scheme[SchemeLast];
     15 
     16 +/* Temporary arrays to allow overriding xresources values */
     17 +static char *colortemp[4];
     18 +static char *tempfonts;
     19 +
     20  #include "config.h"
     21 
     22  static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
     23 @@ -596,8 +601,13 @@ setup(void)
     24  	int a, di, n, area = 0;
     25  #endif
     26  	/* init appearance */
     27 -	for (j = 0; j < SchemeLast; j++)
     28 -		scheme[j] = drw_scm_create(drw, colors[j], 2);
     29 +	for (j = 0; j < SchemeLast; j++) {
     30 +		scheme[j] = drw_scm_create(drw, (const char**)colors[j], 2);
     31 +	}
     32 +	for (j = 0; j < SchemeOut; ++j) {
     33 +		for (i = 0; i < 2; ++i)
     34 +			free(colors[j][i]);
     35 +	}
     36 
     37  	clip = XInternAtom(dpy, "CLIPBOARD",   False);
     38  	utf8 = XInternAtom(dpy, "UTF8_STRING", False);
     39 @@ -687,6 +697,41 @@ usage(void)
     40  	exit(1);
     41  }
     42 
     43 +void
     44 +readxresources(void) {
     45 +	XrmInitialize();
     46 +
     47 +	char* xrm;
     48 +	if ((xrm = XResourceManagerString(drw->dpy))) {
     49 +		char *type;
     50 +		XrmDatabase xdb = XrmGetStringDatabase(xrm);
     51 +		XrmValue xval;
     52 +
     53 +		if (XrmGetResource(xdb, "dmenu.font", "*", &type, &xval))
     54 +			fonts[0] = strdup(xval.addr);
     55 +		else
     56 +			fonts[0] = strdup(fonts[0]);
     57 +		if (XrmGetResource(xdb, "dmenu.background", "*", &type, &xval))
     58 +			colors[SchemeNorm][ColBg] = strdup(xval.addr);
     59 +		else
     60 +			colors[SchemeNorm][ColBg] = strdup(colors[SchemeNorm][ColBg]);
     61 +		if (XrmGetResource(xdb, "dmenu.foreground", "*", &type, &xval))
     62 +			colors[SchemeNorm][ColFg] = strdup(xval.addr);
     63 +		else
     64 +			colors[SchemeNorm][ColFg] = strdup(colors[SchemeNorm][ColFg]);
     65 +		if (XrmGetResource(xdb, "dmenu.selbackground", "*", &type, &xval))
     66 +			colors[SchemeSel][ColBg] = strdup(xval.addr);
     67 +		else
     68 +			colors[SchemeSel][ColBg] = strdup(colors[SchemeSel][ColBg]);
     69 +		if (XrmGetResource(xdb, "dmenu.selforeground", "*", &type, &xval))
     70 +			colors[SchemeSel][ColFg] = strdup(xval.addr);
     71 +		else
     72 +			colors[SchemeSel][ColFg] = strdup(colors[SchemeSel][ColFg]);
     73 +
     74 +		XrmDestroyDatabase(xdb);
     75 +	}
     76 +}
     77 +
     78  int
     79  main(int argc, char *argv[])
     80  {
     81 @@ -715,15 +760,15 @@ main(int argc, char *argv[])
     82  		else if (!strcmp(argv[i], "-p"))   /* adds prompt to left of input field */
     83  			prompt = argv[++i];
     84  		else if (!strcmp(argv[i], "-fn"))  /* font or font set */
     85 -			fonts[0] = argv[++i];
     86 +			tempfonts = argv[++i];
     87  		else if (!strcmp(argv[i], "-nb"))  /* normal background color */
     88 -			colors[SchemeNorm][ColBg] = argv[++i];
     89 +			colortemp[0] = argv[++i];
     90  		else if (!strcmp(argv[i], "-nf"))  /* normal foreground color */
     91 -			colors[SchemeNorm][ColFg] = argv[++i];
     92 +			colortemp[1] = argv[++i];
     93  		else if (!strcmp(argv[i], "-sb"))  /* selected background color */
     94 -			colors[SchemeSel][ColBg] = argv[++i];
     95 +			colortemp[2] = argv[++i];
     96  		else if (!strcmp(argv[i], "-sf"))  /* selected foreground color */
     97 -			colors[SchemeSel][ColFg] = argv[++i];
     98 +			colortemp[3] = argv[++i];
     99  		else if (!strcmp(argv[i], "-w"))   /* embedding window id */
    100  			embed = argv[++i];
    101  		else
    102 @@ -743,8 +788,23 @@ main(int argc, char *argv[])
    103  		die("could not get embedding window attributes: 0x%lx",
    104  		    parentwin);
    105  	drw = drw_create(dpy, screen, root, wa.width, wa.height);
    106 -	if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
    107 +	readxresources();
    108 +	/* Now we check whether to override xresources with commandline parameters */
    109 +	if ( tempfonts )
    110 +	   fonts[0] = strdup(tempfonts);
    111 +	if ( colortemp[0])
    112 +	   colors[SchemeNorm][ColBg] = strdup(colortemp[0]);
    113 +	if ( colortemp[1])
    114 +	   colors[SchemeNorm][ColFg] = strdup(colortemp[1]);
    115 +	if ( colortemp[2])
    116 +	   colors[SchemeSel][ColBg]  = strdup(colortemp[2]);
    117 +	if ( colortemp[3])
    118 +	   colors[SchemeSel][ColFg]  = strdup(colortemp[3]);
    119 +
    120 +	if (!drw_fontset_create(drw, (const char**)fonts, LENGTH(fonts)))
    121  		die("no fonts could be loaded.");
    122 +
    123 +	free(fonts[0]);
    124  	lrpad = drw->fonts->h;
    125 
    126  #ifdef __OpenBSD__