st-noxz

[fork] suckless st - personal fork
git clone https://noxz.tech/git/st-noxz.git
st-noxz

commit: 35cb81e24c6d58b6412f614bdef6915785175b05
parent: d64877b26ec2287b5d1b8176db81a15735159cec
author: Chris Noxz <chris@noxz.tech>
date:   Wed, 14 Aug 2019 13:41:26 +0200
get colors from 'vt' (get_vt_colors)
Mconfig.def.h50++++++++------------
Mx.c50++++++++++++++++++--
2 files changed, 68 insertions(+), 32 deletions(-)
diff --git a/config.def.h b/config.def.h
@@ -98,32 +98,24 @@ char *termname = "st-256color";
 unsigned int tabspaces = 8;
 
 /* Terminal colors (16 first used in escape sequence) */
-static const char *colorname[] = {
-	/* 8 normal colors */
-	"black",
-	"red3",
-	"green3",
-	"yellow3",
-	"blue2",
-	"magenta3",
-	"cyan3",
-	"gray90",
-
-	/* 8 bright colors */
-	"gray50",
-	"red",
-	"green",
-	"yellow",
-	"#5c5cff",
-	"magenta",
-	"cyan",
-	"white",
-
+static char colorname[][8] = {
+	"#000000",
+	"#000000",
+	"#000000",
+	"#000000",
+	"#000000",
+	"#000000",
+	"#000000",
+	"#000000",
+	"#000000",
+	"#000000",
+	"#000000",
+	"#000000",
+	"#000000",
+	"#000000",
+	"#000000",
+	"#000000",
 	[255] = 0,
-
-	/* more colors can be added after 255 to use with DefaultXX */
-	"#cccccc",
-	"#555555",
 };
 
 
@@ -131,10 +123,10 @@ static const char *colorname[] = {
  * Default colors (colorname index)
  * foreground, background, cursor, reverse cursor
  */
-unsigned int defaultfg = 7;
-unsigned int defaultbg = 0;
-static unsigned int defaultcs = 256;
-static unsigned int defaultrcs = 257;
+unsigned int defaultbg  = 0;
+unsigned int defaultfg  = 7;
+unsigned int defaultcs  = 7;
+unsigned int defaultrcs = 0;
 
 /*
  * Default shape of cursor
diff --git a/x.c b/x.c
@@ -173,6 +173,7 @@ static void mousesel(XEvent *, int);
 static void mousereport(XEvent *);
 static char *kmap(KeySym, uint);
 static int match(uint, uint);
+static void get_vt_colors(void);
 
 static void run(void);
 static void usage(void);
@@ -745,13 +746,15 @@ xloadcols(void)
 	Color *cp;
 
 	signal(SIGREDRW, &redraw_signalhandler);
+	get_vt_colors();
+
+	dc.collen = 256;
+	dc.collen = MAX(LEN(colorname), dc.collen);
+	dc.col = xmalloc(dc.collen * sizeof(Color));
 
 	if (loaded) {
 		for (cp = dc.col; cp < &dc.col[dc.collen]; ++cp)
 			XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
-	} else {
-		dc.collen = MAX(LEN(colorname), 256);
-		dc.col = xmalloc(dc.collen * sizeof(Color));
 	}
 
 	for (i = 0; i < dc.collen; i++)
@@ -1895,6 +1898,47 @@ run(void)
 	}
 }
 
+void
+get_vt_colors(void)
+{
+	char *cfs[3] = {
+		"/sys/module/vt/parameters/default_red",
+		"/sys/module/vt/parameters/default_grn",
+		"/sys/module/vt/parameters/default_blu",
+	};
+	char vtcs[16][8];
+	char tk[] = ",";
+	char cl[64];
+	char *tp = NULL;
+	FILE *fp;
+	size_t r, l;
+	int i, c, n;
+
+	for (i = 0; i < 16; i++)
+		strcpy(vtcs[i], "#000000");
+
+	for (i = 0, r = 0; i < 3; i++) {
+		if ((fp = fopen(cfs[i], "r")) == NULL)
+			continue;
+		while ((cl[r] = fgetc(fp)) != EOF && cl[r] != '\n')
+			r++;
+		cl[r] = '\0';
+		for (c = 0, tp = cl, n = 0; c < 16; c++, tp++) {
+			if ((r = strcspn(tp, tk)) == -1)
+				break;
+			for (n = 0; r && *tp >= 48 && *tp < 58; r--, tp++)
+				n = n * 10 - 48 + *tp;
+			vtcs[c][i * 2 + 1] = n / 16 < 10 ? n / 16 + 48 : n / 16 + 87;
+			vtcs[c][i * 2 + 2] = n % 16 < 10 ? n % 16 + 48 : n % 16 + 87;
+		}
+		fclose(fp);
+	}
+	for (i = 0; i < 16; i++) {
+		if (strlen(colorname[i]) >= strlen(vtcs[i]))
+			memcpy(colorname[i], vtcs[i], 7);
+	}
+}
+
 void
 usage(void)
 {