diff options
author | Raheman Vaiya <r.vaiya@gmail.com> | 2021-12-26 18:57:04 +0100 |
---|---|---|
committer | Hiltjo Posthuma <hiltjo@codemadness.org> | 2021-12-26 18:57:44 +0100 |
commit | 8e310303903792c010d03c046ba75f8b18f7d3a7 (patch) | |
tree | f634e71e556b312e64ed1cc9ddd5b1e3fb3ced71 | |
parent | 2f6e597ed871cff91c627850d03152cae5f45779 (diff) | |
download | st-patched-8e310303903792c010d03c046ba75f8b18f7d3a7.tar.bz2 st-patched-8e310303903792c010d03c046ba75f8b18f7d3a7.tar.xz st-patched-8e310303903792c010d03c046ba75f8b18f7d3a7.zip |
Add support for OSC color sequences
-rw-r--r-- | config.def.h | 8 | ||||
-rw-r--r-- | st.c | 80 | ||||
-rw-r--r-- | st.h | 3 | ||||
-rw-r--r-- | x.c | 13 |
4 files changed, 100 insertions, 4 deletions
diff --git a/config.def.h b/config.def.h index 6f05dce..91ab8ca 100644 --- a/config.def.h +++ b/config.def.h | |||
@@ -120,6 +120,8 @@ static const char *colorname[] = { | |||
120 | /* more colors can be added after 255 to use with DefaultXX */ | 120 | /* more colors can be added after 255 to use with DefaultXX */ |
121 | "#cccccc", | 121 | "#cccccc", |
122 | "#555555", | 122 | "#555555", |
123 | "gray90", /* default foreground colour */ | ||
124 | "black", /* default background colour */ | ||
123 | }; | 125 | }; |
124 | 126 | ||
125 | 127 | ||
@@ -127,9 +129,9 @@ static const char *colorname[] = { | |||
127 | * Default colors (colorname index) | 129 | * Default colors (colorname index) |
128 | * foreground, background, cursor, reverse cursor | 130 | * foreground, background, cursor, reverse cursor |
129 | */ | 131 | */ |
130 | unsigned int defaultfg = 7; | 132 | unsigned int defaultfg = 258; |
131 | unsigned int defaultbg = 0; | 133 | unsigned int defaultbg = 259; |
132 | static unsigned int defaultcs = 256; | 134 | unsigned int defaultcs = 256; |
133 | static unsigned int defaultrcs = 257; | 135 | static unsigned int defaultrcs = 257; |
134 | 136 | ||
135 | /* | 137 | /* |
@@ -1843,6 +1843,42 @@ csireset(void) | |||
1843 | } | 1843 | } |
1844 | 1844 | ||
1845 | void | 1845 | void |
1846 | osc4_color_response(int num) | ||
1847 | { | ||
1848 | int n; | ||
1849 | char buf[32]; | ||
1850 | unsigned char r, g, b; | ||
1851 | |||
1852 | if (xgetcolor(num, &r, &g, &b)) { | ||
1853 | fprintf(stderr, "erresc: failed to fetch osc4 color %d\n", num); | ||
1854 | return; | ||
1855 | } | ||
1856 | |||
1857 | n = snprintf(buf, sizeof buf, "\033]4;%d;rgb:%02x%02x/%02x%02x/%02x%02x\007", | ||
1858 | num, r, r, g, g, b, b); | ||
1859 | |||
1860 | ttywrite(buf, n, 1); | ||
1861 | } | ||
1862 | |||
1863 | void | ||
1864 | osc_color_response(int index, int num) | ||
1865 | { | ||
1866 | int n; | ||
1867 | char buf[32]; | ||
1868 | unsigned char r, g, b; | ||
1869 | |||
1870 | if (xgetcolor(index, &r, &g, &b)) { | ||
1871 | fprintf(stderr, "erresc: failed to fetch osc color %d\n", index); | ||
1872 | return; | ||
1873 | } | ||
1874 | |||
1875 | n = snprintf(buf, sizeof buf, "\033]%d;rgb:%02x%02x/%02x%02x/%02x%02x\007", | ||
1876 | num, r, r, g, g, b, b); | ||
1877 | |||
1878 | ttywrite(buf, n, 1); | ||
1879 | } | ||
1880 | |||
1881 | void | ||
1846 | strhandle(void) | 1882 | strhandle(void) |
1847 | { | 1883 | { |
1848 | char *p = NULL, *dec; | 1884 | char *p = NULL, *dec; |
@@ -1880,6 +1916,45 @@ strhandle(void) | |||
1880 | } | 1916 | } |
1881 | } | 1917 | } |
1882 | return; | 1918 | return; |
1919 | case 10: | ||
1920 | if (narg < 2) | ||
1921 | break; | ||
1922 | |||
1923 | p = strescseq.args[1]; | ||
1924 | |||
1925 | if (!strcmp(p, "?")) | ||
1926 | osc_color_response(defaultfg, 10); | ||
1927 | else if (xsetcolorname(defaultfg, p)) | ||
1928 | fprintf(stderr, "erresc: invalid foreground color: %s\n", p); | ||
1929 | else | ||
1930 | redraw(); | ||
1931 | break; | ||
1932 | case 11: | ||
1933 | if (narg < 2) | ||
1934 | break; | ||
1935 | |||
1936 | p = strescseq.args[1]; | ||
1937 | |||
1938 | if (!strcmp(p, "?")) | ||
1939 | osc_color_response(defaultbg, 11); | ||
1940 | else if (xsetcolorname(defaultbg, p)) | ||
1941 | fprintf(stderr, "erresc: invalid background color: %s\n", p); | ||
1942 | else | ||
1943 | redraw(); | ||
1944 | break; | ||
1945 | case 12: | ||
1946 | if (narg < 2) | ||
1947 | break; | ||
1948 | |||
1949 | p = strescseq.args[1]; | ||
1950 | |||
1951 | if (!strcmp(p, "?")) | ||
1952 | osc_color_response(defaultcs, 12); | ||
1953 | else if (xsetcolorname(defaultcs, p)) | ||
1954 | fprintf(stderr, "erresc: invalid cursor color: %s\n", p); | ||
1955 | else | ||
1956 | redraw(); | ||
1957 | break; | ||
1883 | case 4: /* color set */ | 1958 | case 4: /* color set */ |
1884 | if (narg < 3) | 1959 | if (narg < 3) |
1885 | break; | 1960 | break; |
@@ -1887,7 +1962,10 @@ strhandle(void) | |||
1887 | /* FALLTHROUGH */ | 1962 | /* FALLTHROUGH */ |
1888 | case 104: /* color reset, here p = NULL */ | 1963 | case 104: /* color reset, here p = NULL */ |
1889 | j = (narg > 1) ? atoi(strescseq.args[1]) : -1; | 1964 | j = (narg > 1) ? atoi(strescseq.args[1]) : -1; |
1890 | if (xsetcolorname(j, p)) { | 1965 | |
1966 | if (!strcmp(p, "?")) | ||
1967 | osc4_color_response(j); | ||
1968 | else if (xsetcolorname(j, p)) { | ||
1891 | if (par == 104 && narg <= 1) | 1969 | if (par == 104 && narg <= 1) |
1892 | return; /* color reset without parameter */ | 1970 | return; /* color reset without parameter */ |
1893 | fprintf(stderr, "erresc: invalid color j=%d, p=%s\n", | 1971 | fprintf(stderr, "erresc: invalid color j=%d, p=%s\n", |
@@ -111,6 +111,8 @@ void *xmalloc(size_t); | |||
111 | void *xrealloc(void *, size_t); | 111 | void *xrealloc(void *, size_t); |
112 | char *xstrdup(const char *); | 112 | char *xstrdup(const char *); |
113 | 113 | ||
114 | int xgetcolor(int x, unsigned char *r, unsigned char *g, unsigned char *b); | ||
115 | |||
114 | /* config.h globals */ | 116 | /* config.h globals */ |
115 | extern char *utmp; | 117 | extern char *utmp; |
116 | extern char *scroll; | 118 | extern char *scroll; |
@@ -123,3 +125,4 @@ extern char *termname; | |||
123 | extern unsigned int tabspaces; | 125 | extern unsigned int tabspaces; |
124 | extern unsigned int defaultfg; | 126 | extern unsigned int defaultfg; |
125 | extern unsigned int defaultbg; | 127 | extern unsigned int defaultbg; |
128 | extern unsigned int defaultcs; | ||
@@ -800,6 +800,19 @@ xloadcols(void) | |||
800 | } | 800 | } |
801 | 801 | ||
802 | int | 802 | int |
803 | xgetcolor(int x, unsigned char *r, unsigned char *g, unsigned char *b) | ||
804 | { | ||
805 | if (!BETWEEN(x, 0, dc.collen)) | ||
806 | return 1; | ||
807 | |||
808 | *r = dc.col[x].color.red >> 8; | ||
809 | *g = dc.col[x].color.green >> 8; | ||
810 | *b = dc.col[x].color.blue >> 8; | ||
811 | |||
812 | return 0; | ||
813 | } | ||
814 | |||
815 | int | ||
803 | xsetcolorname(int x, const char *name) | 816 | xsetcolorname(int x, const char *name) |
804 | { | 817 | { |
805 | Color ncolor; | 818 | Color ncolor; |