aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaheman Vaiya <r.vaiya@gmail.com>2021-12-26 18:57:04 +0100
committerRomain Gonçalves <me@rgoncalves.se>2024-05-12 21:20:50 +0200
commit5dceed2bfa9c57c9e230b86e3834f7cba89bb0d1 (patch)
treef90baa6a48e28db70ef9e41bde7220a73efaf8f5
parent7ad409ac2e8d4e9f9cae5f6984f7121cae292b14 (diff)
downloadst-5dceed2bfa9c57c9e230b86e3834f7cba89bb0d1.tar.gz
Add support for OSC color sequences
-rw-r--r--config.def.h8
-rw-r--r--st.c80
-rw-r--r--st.h3
-rw-r--r--x.c13
4 files changed, 100 insertions, 4 deletions
diff --git a/config.def.h b/config.def.h
index e86bbb4..717b2f0 100644
--- a/config.def.h
+++ b/config.def.h
@@ -126,6 +126,8 @@ static const char *colorname[] = {
/* more colors can be added after 255 to use with DefaultXX */
"#cccccc",
"#555555",
+ "gray90", /* default foreground colour */
+ "black", /* default background colour */
};
@@ -133,9 +135,9 @@ 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;
+unsigned int defaultfg = 258;
+unsigned int defaultbg = 259;
+unsigned int defaultcs = 256;
static unsigned int defaultrcs = 257;
/*
diff --git a/st.c b/st.c
index a9338e1..781dbf2 100644
--- a/st.c
+++ b/st.c
@@ -1843,6 +1843,42 @@ csireset(void)
}
void
+osc4_color_response(int num)
+{
+ int n;
+ char buf[32];
+ unsigned char r, g, b;
+
+ if (xgetcolor(num, &r, &g, &b)) {
+ fprintf(stderr, "erresc: failed to fetch osc4 color %d\n", num);
+ return;
+ }
+
+ n = snprintf(buf, sizeof buf, "\033]4;%d;rgb:%02x%02x/%02x%02x/%02x%02x\007",
+ num, r, r, g, g, b, b);
+
+ ttywrite(buf, n, 1);
+}
+
+void
+osc_color_response(int index, int num)
+{
+ int n;
+ char buf[32];
+ unsigned char r, g, b;
+
+ if (xgetcolor(index, &r, &g, &b)) {
+ fprintf(stderr, "erresc: failed to fetch osc color %d\n", index);
+ return;
+ }
+
+ n = snprintf(buf, sizeof buf, "\033]%d;rgb:%02x%02x/%02x%02x/%02x%02x\007",
+ num, r, r, g, g, b, b);
+
+ ttywrite(buf, n, 1);
+}
+
+void
strhandle(void)
{
char *p = NULL, *dec;
@@ -1880,6 +1916,45 @@ strhandle(void)
}
}
return;
+ case 10:
+ if (narg < 2)
+ break;
+
+ p = strescseq.args[1];
+
+ if (!strcmp(p, "?"))
+ osc_color_response(defaultfg, 10);
+ else if (xsetcolorname(defaultfg, p))
+ fprintf(stderr, "erresc: invalid foreground color: %s\n", p);
+ else
+ redraw();
+ break;
+ case 11:
+ if (narg < 2)
+ break;
+
+ p = strescseq.args[1];
+
+ if (!strcmp(p, "?"))
+ osc_color_response(defaultbg, 11);
+ else if (xsetcolorname(defaultbg, p))
+ fprintf(stderr, "erresc: invalid background color: %s\n", p);
+ else
+ redraw();
+ break;
+ case 12:
+ if (narg < 2)
+ break;
+
+ p = strescseq.args[1];
+
+ if (!strcmp(p, "?"))
+ osc_color_response(defaultcs, 12);
+ else if (xsetcolorname(defaultcs, p))
+ fprintf(stderr, "erresc: invalid cursor color: %s\n", p);
+ else
+ redraw();
+ break;
case 4: /* color set */
if (narg < 3)
break;
@@ -1887,7 +1962,10 @@ strhandle(void)
/* FALLTHROUGH */
case 104: /* color reset, here p = NULL */
j = (narg > 1) ? atoi(strescseq.args[1]) : -1;
- if (xsetcolorname(j, p)) {
+
+ if (!strcmp(p, "?"))
+ osc4_color_response(j);
+ else if (xsetcolorname(j, p)) {
if (par == 104 && narg <= 1)
return; /* color reset without parameter */
fprintf(stderr, "erresc: invalid color j=%d, p=%s\n",
diff --git a/st.h b/st.h
index fa2eddf..519b9bd 100644
--- a/st.h
+++ b/st.h
@@ -111,6 +111,8 @@ void *xmalloc(size_t);
void *xrealloc(void *, size_t);
char *xstrdup(const char *);
+int xgetcolor(int x, unsigned char *r, unsigned char *g, unsigned char *b);
+
/* config.h globals */
extern char *utmp;
extern char *scroll;
@@ -123,3 +125,4 @@ extern char *termname;
extern unsigned int tabspaces;
extern unsigned int defaultfg;
extern unsigned int defaultbg;
+extern unsigned int defaultcs;
diff --git a/x.c b/x.c
index fb38406..12008ef 100644
--- a/x.c
+++ b/x.c
@@ -803,6 +803,19 @@ xloadcols(void)
}
int
+xgetcolor(int x, unsigned char *r, unsigned char *g, unsigned char *b)
+{
+ if (!BETWEEN(x, 0, dc.collen))
+ return 1;
+
+ *r = dc.col[x].color.red >> 8;
+ *g = dc.col[x].color.green >> 8;
+ *b = dc.col[x].color.blue >> 8;
+
+ return 0;
+}
+
+int
xsetcolorname(int x, const char *name)
{
Color ncolor;
remember that computers suck.