sites

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

dmenu-xresources-20200302-db6093f.diff (2744B)


      1 From 916c4921c4e870f77d91d816272375f90dc49867 Mon Sep 17 00:00:00 2001
      2 From: Nihal Jere <nihal@nihaljere.xyz>
      3 Date: Mon, 2 Mar 2020 15:56:12 -0600
      4 Subject: [PATCH] xresources patch frees memory properly and no longer
      5  segfaults
      6 
      7 ---
      8  dmenu.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++---
      9  1 file changed, 47 insertions(+), 3 deletions(-)
     10 
     11 diff --git a/dmenu.c b/dmenu.c
     12 index 65f25ce..d444e8c 100644
     13 --- a/dmenu.c
     14 +++ b/dmenu.c
     15 @@ -15,6 +15,7 @@
     16  #include <X11/extensions/Xinerama.h>
     17  #endif
     18  #include <X11/Xft/Xft.h>
     19 +#include <X11/Xresource.h>
     20  
     21  #include "drw.h"
     22  #include "util.h"
     23 @@ -601,8 +602,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 @@ -694,6 +700,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 @@ -748,8 +789,11 @@ main(int argc, char *argv[])
     82  		die("could not get embedding window attributes: 0x%lx",
     83  		    parentwin);
     84  	drw = drw_create(dpy, screen, root, wa.width, wa.height);
     85 -	if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
     86 +	readxresources();
     87 +	if (!drw_fontset_create(drw, (const char**)fonts, LENGTH(fonts)))
     88  		die("no fonts could be loaded.");
     89 +
     90 +	free(fonts[0]);
     91  	lrpad = drw->fonts->h;
     92  
     93  #ifdef __OpenBSD__
     94 -- 
     95 2.25.1
     96