sites

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

tabbed-xresources-20210317-dabf6a2.diff (4725B)


      1 From 8c48f1564c555bbd21758a3a70a9984e61c34a35 Mon Sep 17 00:00:00 2001
      2 From: 6d6f7274686f6e <4648531+6d6f7274686f6e@users.noreply.github.com>
      3 Date: Wed, 17 Mar 2021 10:59:18 +0100
      4 Subject: [PATCH] xresources support
      5 
      6 ---
      7  config.def.h | 27 ++++++++++++++------
      8  tabbed.c     | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++
      9  2 files changed, 89 insertions(+), 7 deletions(-)
     10 
     11 diff --git a/config.def.h b/config.def.h
     12 index defa426..244e288 100644
     13 --- a/config.def.h
     14 +++ b/config.def.h
     15 @@ -1,13 +1,13 @@
     16  /* See LICENSE file for copyright and license details. */
     17  
     18  /* appearance */
     19 -static const char font[]        = "monospace:size=9";
     20 -static const char* normbgcolor  = "#222222";
     21 -static const char* normfgcolor  = "#cccccc";
     22 -static const char* selbgcolor   = "#555555";
     23 -static const char* selfgcolor   = "#ffffff";
     24 -static const char* urgbgcolor   = "#111111";
     25 -static const char* urgfgcolor   = "#cc0000";
     26 +static char font[]        = "monospace:size=9";
     27 +static char* normbgcolor  = "#222222";
     28 +static char* normfgcolor  = "#cccccc";
     29 +static char* selbgcolor   = "#555555";
     30 +static char* selfgcolor   = "#ffffff";
     31 +static char* urgbgcolor   = "#111111";
     32 +static char* urgfgcolor   = "#cc0000";
     33  static const char before[]      = "<";
     34  static const char after[]       = ">";
     35  static const char titletrim[]   = "...";
     36 @@ -33,6 +33,19 @@ static Bool npisrelative  = False;
     37          } \
     38  }
     39  
     40 +/*
     41 + * Xresources preferences to load at startup
     42 + */
     43 +ResourcePref resources[] = {
     44 +		{ "font",         STRING,  &font },
     45 +		{ "color0",       STRING,  &normbgcolor },
     46 +		{ "color4",       STRING,  &normfgcolor },
     47 +		{ "color4",       STRING,  &selbgcolor },
     48 +		{ "color7",       STRING,  &selfgcolor },
     49 +		{ "color2",       STRING,  &urgbgcolor },
     50 +		{ "color3",       STRING,  &urgfgcolor },
     51 +};
     52 +
     53  #define MODKEY ControlMask
     54  static Key keys[] = {
     55  	/* modifier             key        function     argument */
     56 diff --git a/tabbed.c b/tabbed.c
     57 index eafe28a..c5bffc7 100644
     58 --- a/tabbed.c
     59 +++ b/tabbed.c
     60 @@ -13,6 +13,7 @@
     61  #include <X11/Xatom.h>
     62  #include <X11/Xlib.h>
     63  #include <X11/Xproto.h>
     64 +#include <X11/Xresource.h>
     65  #include <X11/Xutil.h>
     66  #include <X11/XKBlib.h>
     67  #include <X11/Xft/Xft.h>
     68 @@ -85,11 +86,26 @@ typedef struct {
     69  	Bool urgent;
     70  	Bool closed;
     71  } Client;
     72 + 
     73 +/* Xresources preferences */
     74 +enum resource_type {
     75 +	STRING = 0,
     76 +	INTEGER = 1,
     77 +	FLOAT = 2
     78 +};
     79 +
     80 +typedef struct {
     81 +	char *name;
     82 +	enum resource_type type;
     83 +	void *dst;
     84 +} ResourcePref;
     85 + 
     86  
     87  /* function declarations */
     88  static void buttonpress(const XEvent *e);
     89  static void cleanup(void);
     90  static void clientmessage(const XEvent *e);
     91 +static void config_init(void);
     92  static void configurenotify(const XEvent *e);
     93  static void configurerequest(const XEvent *e);
     94  static void createnotify(const XEvent *e);
     95 @@ -120,6 +136,7 @@ static void move(const Arg *arg);
     96  static void movetab(const Arg *arg);
     97  static void propertynotify(const XEvent *e);
     98  static void resize(int c, int w, int h);
     99 +static int resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst);
    100  static void rotate(const Arg *arg);
    101  static void run(void);
    102  static void sendxembed(int c, long msg, long detail, long d1, long d2);
    103 @@ -245,6 +262,23 @@ clientmessage(const XEvent *e)
    104  	}
    105  }
    106  
    107 +void
    108 +config_init(void)
    109 +{
    110 +	char *resm;
    111 +	XrmDatabase db;
    112 +	ResourcePref *p;
    113 +
    114 +	XrmInitialize();
    115 +	resm = XResourceManagerString(dpy);
    116 +	if (!resm)
    117 +		return;
    118 +
    119 +	db = XrmGetStringDatabase(resm);
    120 +	for (p = resources; p < resources + LENGTH(resources); p++)
    121 +		resource_load(db, p->name, p->type, p->dst);
    122 +}
    123 +
    124  void
    125  configurenotify(const XEvent *e)
    126  {
    127 @@ -897,6 +931,40 @@ resize(int c, int w, int h)
    128  	           (XEvent *)&ce);
    129  }
    130  
    131 +int
    132 +resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst)
    133 +{
    134 +	char **sdst = dst;
    135 +	int *idst = dst;
    136 +	float *fdst = dst;
    137 +
    138 +	char fullname[256];
    139 +	char fullclass[256];
    140 +	char *type;
    141 +	XrmValue ret;
    142 +
    143 +	snprintf(fullname, sizeof(fullname), "%s.%s", "tabbed", name);
    144 +	snprintf(fullclass, sizeof(fullclass), "%s.%s", "tabbed", name);
    145 +	fullname[sizeof(fullname) - 1] = fullclass[sizeof(fullclass) - 1] = '\0';
    146 +
    147 +	XrmGetResource(db, fullname, fullclass, &type, &ret);
    148 +	if (ret.addr == NULL || strncmp("String", type, 64))
    149 +		return 1;
    150 +
    151 +	switch (rtype) {
    152 +	case STRING:
    153 +		*sdst = ret.addr;
    154 +		break;
    155 +	case INTEGER:
    156 +		*idst = strtoul(ret.addr, NULL, 10);
    157 +		break;
    158 +	case FLOAT:
    159 +		*fdst = strtof(ret.addr, NULL);
    160 +		break;
    161 +	}
    162 +	return 0;
    163 +}
    164 +
    165  void
    166  rotate(const Arg *arg)
    167  {
    168 @@ -1354,6 +1422,7 @@ main(int argc, char *argv[])
    169  	if (!(dpy = XOpenDisplay(NULL)))
    170  		die("%s: cannot open display\n", argv0);
    171  
    172 +	config_init();
    173  	setup();
    174  	printf("0x%lx\n", win);
    175  	fflush(NULL);
    176 -- 
    177 2.30.2
    178