History

plan9: update theming logic

Sigrid Haflínudóttir committed 6 years ago

Commit b390d011 parent b70bc704 files · +220 −131Browse files

Unified · Split

4 files
  1. mkfile +1 −0
  2. plan9.c +30 −131
  3. theme.c +164 −0
  4. theme.h +25 −0
mkfile+1−0
@@ -17,6 +17,7 @@ OFILES=\ · 16 unmodified lines
1717 gbuffer.$O\
1818 plan9.$O\
1919 sim.$O\
20+ theme.$O\
2021 vmio.$O\
2122
2223 default:V: all
plan9.c+30−131
@@ -10,6 +10,7 @@ · 9 unmodified lines
1010 #include <plumb.h>
1111 #include <cursor.h>
1212 #include <tos.h>
13+#include "theme.h"
1314
1415 #define MIN(x,y) ((x)<=(y)?(x):(y))
1516 #define MAX(x,y) ((x)>=(y)?(x):(y))
@@ -45,17 +46,6 @@ enum { · 29 unmodified lines
4546 Menu3exit,
4647 Nummenu3,
4748
48− Dback = 0,
49− Dfhigh,
50− Dfmed,
51− Dflow,
52− Dfinv,
53− Dbhigh,
54− Dbmed,
55− Dblow,
56− Dbinv,
57− Numcolors,
58−
5949 /* this might become a bad idea in the future */
6050 Mark_flag_group_highlight = 1<<6,
6151 Mark_flag_selected = 1<<7,
@@ -143,23 +133,6 @@ static struct { · 81 unmodified lines
143133 },
144134 };
145135
146−static struct {
147− char *id;
148− u32int color;
149−}theme[Numcolors] = {
150− [Dback] = {"background", 0x000000},
151− [Dfhigh] = {"f_high", 0xffffff},
152− [Dfmed] = {"f_med", 0x777777},
153− [Dflow] = {"f_low", 0x444444},
154− [Dfinv] = {"f_inv", 0x000000},
155− [Dbhigh] = {"b_high", 0xdddddd},
156− [Dbmed] = {"b_med", 0x72dec2},
157− [Dblow] = {"b_low", 0x222222},
158− [Dbinv] = {"b_inv", 0xffb545},
159−};
160−
161−static Image *color[Numcolors];
162−
163136 static char *modes[Nummodes] = {
164137 [Minsert] = "insert",
165138 [Mappend] = "append",
@@ -596,10 +569,10 @@ redraw(int complete) · 430 unmodified lines
596569 if (complete) {
597570 r = screen->r;
598571 r.max.y = r.min.y + txtoff.y;
599− draw(screen, r, color[Dback], nil, ZP);
572+ draw(screen, r, colors[Dback].im, nil, ZP);
600573 r = screen->r;
601574 r.max.x = r.min.x + txtoff.x;
602− draw(screen, r, color[Dback], nil, ZP);
575+ draw(screen, r, colors[Dback].im, nil, ZP);
603576 }
604577
605578 off = field.width*cur.y + cur.x;
@@ -636,7 +609,7 @@ redraw(int complete) · 30 unmodified lines
636609
637610 if (!grouphl && !complete && c == fscr.buffer[off] && attr == mscr.buffer[off]) {
638611 if (i > 0) {
639− p = runestringnbg(screen, p, color[oldfg], ZP, font, linebuf, i, color[oldbg], ZP);
612+ p = runestringnbg(screen, p, colors[oldfg].im, ZP, font, linebuf, i, colors[oldbg].im, ZP);
640613 i = 0;
641614 }
642615 p.x += glyphsz.x;
@@ -687,43 +660,43 @@ redraw(int complete) · 44 unmodified lines
687660 }
688661
689662 if (bg != oldbg || fg != oldfg) {
690− p = runestringnbg(screen, p, color[oldfg], ZP, font, linebuf, i, color[oldbg], ZP);
663+ p = runestringnbg(screen, p, colors[oldfg].im, ZP, font, linebuf, i, colors[oldbg].im, ZP);
691664 i = 0;
692665 }
693666 linebuf[i++] = c;
694667 }
695− runestringnbg(screen, p, color[fg], ZP, font, linebuf, i, color[bg], ZP);
668+ runestringnbg(screen, p, colors[fg].im, ZP, font, linebuf, i, colors[bg].im, ZP);
696669 p.y += glyphsz.y;
697670 }
698671
699672 r = screen->r;
700673 r.min.x += txtoff.x + max.x*glyphsz.x;
701− draw(screen, r, color[Dback], nil, ZP);
674+ draw(screen, r, colors[Dback].im, nil, ZP);
702675
703676 r = screen->r;
704677 r.min.y += txtoff.y + max.y*glyphsz.y;
705− draw(screen, r, color[Dback], nil, ZP);
678+ draw(screen, r, colors[Dback].im, nil, ZP);
706679
707680 p = top;
708681 p.y += glyphsz.y*(max.y-1)/2;
709682 if (scroll.x > 0) {
710683 p.x = top.x - txtoff.x;
711− string(screen, p, color[Dfmed], ZP, font, "←");
684+ string(screen, p, colors[Dfmed].im, ZP, font, "←");
712685 }
713686 if (max.x+scroll.x < field.width) {
714687 p.x = top.x + max.x*glyphsz.x + txtoff.x - glyphsz.x;
715− string(screen, p, color[Dfmed], ZP, font, "→");
688+ string(screen, p, colors[Dfmed].im, ZP, font, "→");
716689 }
717690
718691 p = top;
719692 p.x += glyphsz.x*(max.x-1)/2;
720693 if (scroll.y > 0) {
721694 p.y = screen->r.min.y;
722− string(screen, p, color[Dfmed], ZP, font, "↑");
695+ string(screen, p, colors[Dfmed].im, ZP, font, "↑");
723696 }
724697 if (max.y+scroll.y < field.height) {
725698 p.y = top.y + max.y*glyphsz.y;
726− string(screen, p, color[Dfmed], ZP, font, "↓");
699+ string(screen, p, colors[Dfmed].im, ZP, font, "↓");
727700 }
728701
729702 i = 0;
@@ -741,7 +714,7 @@ redraw(int complete) · 11 unmodified lines
741714 i += runesprint(linebuf+i, "%-9s", s);
742715 sprint(s, "%ldms", labs(framedev));
743716 i += runesprint(linebuf+i, "%-8s", s);
744− runestringn(screen, bot, color[Dfhigh], ZP, font, linebuf, i);
717+ runestringn(screen, bot, colors[Dfhigh].im, ZP, font, linebuf, i);
745718 bot.y += glyphsz.y;
746719
747720 i = 0;
@@ -752,12 +725,18 @@ redraw(int complete) · 4 unmodified lines
752725 i += runesprint(linebuf+i, "%-9s", s);
753726 i += runesprint(linebuf+i, "%-9s", modes[altdown ? Mslide : mode]);
754727 i += runesprint(linebuf+i, "%s", filename[0] ? filename : "unnamed");
755− runestringn(screen, bot, color[Dfhigh], ZP, font, linebuf, i);
728+ runestringn(screen, bot, colors[Dfhigh].im, ZP, font, linebuf, i);
756729
757730 flushimage(display, 1);
758731 unlockdisplay(display);
759732 }
760733
734+void
735+themechanged(void)
736+{
737+ redraw(1);
738+}
739+
761740 static void
762741 command(char *s, void (*snapshot)(void))
763742 {
@@ -815,7 +794,7 @@ command(char *s, void (*snapshot)(void)) · 51 unmodified lines
815794 } else if (s[0] == 'c' && s[1] == 'o') { /* color ffffff 111111 232323 ... */
816795 if (argc < 2) {
817796 for (i = 0; i < Numcolors; i++)
818− print("%06x ", theme[i].color);
797+ print("%06x ", colors[i].rgb);
819798 print("\n");
820799 fflush(stdout);
821800 } else {
@@ -825,9 +804,9 @@ command(char *s, void (*snapshot)(void)) · 3 unmodified lines
825804 continue;
826805 if ((im = allocimage(display, Rect(0, 0, 1, 1), RGB24, 1, c<<8 | 0xff)) == nil)
827806 continue;
828− freeimage(color[i-1]);
829− theme[i-1].color = c;
830− color[i-1] = im;
807+ freeimage(colors[i-1].im);
808+ colors[i-1].rgb = c;
809+ colors[i-1].im = im;
831810 }
832811 unlockdisplay(display);
833812 redraw(1);
@@ -1168,88 +1147,6 @@ plumbproc(void *) · 334 unmodified lines
11681147 threadexits(nil);
11691148 }
11701149
1171−static void
1172−runpicker(void *x)
1173−{
1174− int *p, wfd;
1175− char *wsys;
1176− char tmp[64];
1177−
1178− if ((wsys = getenv("wsys")) == nil) {
1179− fprint(2, "wsys not set\n");
1180− goto err;
1181− }
1182− if ((wfd = open(wsys, ORDWR)) < 0) {
1183− fprint(2, "can't open wsys: %r\n");
1184− goto err;
1185− }
1186−
1187− snprint(tmp, sizeof(tmp), "new -pid %d -dx %d -dy %d", getpid(), 384, 320);
1188−
1189− if (mount(wfd, -1, "/mnt/wsys", MREPL, tmp) < 0) {
1190− fprint(2, "couldn't mount wsys: %r\n");
1191− goto err;
1192− }
1193− if (bind("/mnt/wsys", "/dev", MBEFORE) < 0) {
1194− fprint(2, "couldn't bind wsys: %r\n");
1195− goto err;
1196− }
1197−
1198− p = x;
1199− dup(p[0], 0); close(p[0]); close(p[1]);
1200− dup(p[3], 1); close(p[3]); close(p[2]);
1201− dup(open("/dev/null", OWRITE), 2);
1202− procexecl(nil, "/bin/picker", "picker", nil);
1203−
1204−err:
1205− threadexits("exec: %r");
1206−}
1207−
1208−static void
1209−colorsproc(void *)
1210−{
1211− Biobuf *in, *out;
1212− char *s, *v[3];
1213− int p[4], n, i;
1214−
1215− threadsetname("colorsproc");
1216− pipe(p);
1217− pipe(p+2);
1218− procrfork(runpicker, p, 4096, RFFDG|RFNAMEG);
1219− close(p[0]);
1220− close(p[3]);
1221− out = Bfdopen(p[1], OWRITE);
1222− in = Bfdopen(p[2], OREAD);
1223−
1224− for (i = 0; i < nelem(theme); i++)
1225− Bprint(out, "%s\t%06ux\n", theme[i].id, theme[i].color);
1226− Bterm(out);
1227−
1228− for (;;) {
1229− if ((s = Brdstr(in, '\n', 1)) == nil)
1230− break;
1231− if ((n = tokenize(s, v, nelem(v))) == 2) {
1232− for (i = 0; i < nelem(theme); i++) {
1233− if (strcmp(theme[i].id, v[0]) == 0) {
1234− lockdisplay(display);
1235− freeimage(color[i]);
1236− theme[i].color = strtoul(v[1], nil, 16);
1237− color[i] = allocimage(display, Rect(0, 0, 1, 1), RGB24, 1, theme[i].color<<8 | 0xff);
1238− unlockdisplay(display);
1239− redraw(1);
1240− break;
1241− }
1242− }
1243− }
1244− free(s);
1245− if (n != 2)
1246− break;
1247− }
1248− Bterm(in);
1249−
1250− threadexits(nil);
1251−}
1252−
12531150 static void
12541151 kbdproc(void *cchan)
12551152 {
@@ -1357,7 +1254,7 @@ threadmain(int argc, char **argv) · 101 unmodified lines
13571254 Point p;
13581255 char tmp[256];
13591256 char cmd[256];
1360− int oldw, oldh, w, h, n, oldbuttons;
1257+ int oldw, oldh, w, h, n, oldbuttons, themetid;
13611258 long seed;
13621259 bool complete;
13631260 Alt a[Numchan+1] = {
@@ -1464,8 +1361,8 @@ threadmain(int argc, char **argv) · 100 unmodified lines
14641361
14651362 proccreate(stdinproc, nil, mainstacksize);
14661363
1467− for (n = 0; n < Numcolors; n++)
1468− color[n] = allocimage(display, Rect(0, 0, 1, 1), RGB24, 1, theme[n].color<<8 | 0xff);
1364+ themeinit();
1365+
14691366 glyphsz.x = stringwidth(font, "@");
14701367 glyphsz.y = font->height;
14711368
@@ -1496,6 +1393,7 @@ threadmain(int argc, char **argv) · 24 unmodified lines
14961393 move.y = 1;
14971394 oldbuttons = 0;
14981395 cmd[0] = 0;
1396+ themetid = -1;
14991397
15001398 netdial(nil, nil);
15011399 midiopen(nil);
@@ -1570,7 +1468,7 @@ noredraw: · 68 unmodified lines
15701468 } else if (n == Menu3rulerstyle) {
15711469 rulerstyle = ++rulerstyle % Numstyles;
15721470 } else if (n == Menu3colors) {
1573− proccreate(colorsproc, nil, mainstacksize);
1471+ themetid = proccreate(themeproc, nil, 4096);
15741472 } else if (n == Menu3exit) {
15751473 setcursor(mctl, &yousure);
15761474 for (;;) {
@@ -1840,6 +1738,7 @@ noredraw: · 263 unmodified lines
18401738 }
18411739
18421740 end:
1741+ threadint(themetid);
18431742 chanclose(a[Ckey].c);
18441743 chanclose(a[Credraw].c);
18451744 mbuf_reusable_deinit(&mscr);
theme.c+164−0
@@ -0,0 +1,164 @@
1+#include <u.h>
2+#include <libc.h>
3+#include <plumb.h>
4+#include <draw.h>
5+#include <bio.h>
6+#include <thread.h>
7+#include "theme.h"
8+
9+ThemeColor colors[Numcolors] = {
10+ [Dback] = {"background", 0x000000},
11+ [Dfhigh] = {"f_high", 0xffffff},
12+ [Dfmed] = {"f_med", 0x777777},
13+ [Dflow] = {"f_low", 0x444444},
14+ [Dfinv] = {"f_inv", 0x000000},
15+ [Dbhigh] = {"b_high", 0xdddddd},
16+ [Dbmed] = {"b_med", 0x72dec2},
17+ [Dblow] = {"b_low", 0x222222},
18+ [Dbinv] = {"b_inv", 0xffb545},
19+};
20+
21+void themechanged(void);
22+
23+static char *themeplumb;
24+
25+static void
26+runpicker(void *x)
27+{
28+ int *p, f;
29+ char tmp[32];
30+
31+ snprint(tmp, sizeof(tmp), "-pid %d -dx %d -dy %d", getpid(), 384, 320);
32+ newwindow(tmp);
33+
34+ p = x;
35+ dup(*p, 0); dup(*p, 1); close(*p);
36+ close(p[1]);
37+ close(p[2]);
38+ dup(f = open("/dev/null", OWRITE), 2); close(f);
39+ execl("/bin/picker", "picker", nil);
40+
41+ threadexits("exec: %r");
42+}
43+
44+void
45+themeproc(void *fd)
46+{
47+ Biobuf *b;
48+ char *s, *v[3];
49+ int p[3], n, i;
50+ static int pid;
51+
52+ threadsetname("themeproc");
53+ pipe(p);
54+ p[2] = fd != nil ? *(int*)fd : -1;
55+ postnote(PNGROUP, pid, "interrupt");
56+ pid = threadpid(procrfork(runpicker, p, 4096, RFFDG|RFNAMEG|RFNOTEG));
57+ close(p[0]);
58+ b = Bfdopen(p[1], OREAD);
59+
60+ for(i = 0; i < nelem(colors); i++)
61+ fprint(p[1], "%s\t%06ux\n", colors[i].id, colors[i].rgb);
62+
63+ for(;;){
64+ if((s = Brdstr(b, '\n', 1)) == nil)
65+ break;
66+ if((n = tokenize(s, v, nelem(v))) >= 2){
67+ for(i = 0; i < nelem(colors); i++){
68+ if(strcmp(colors[i].id, v[0]) == 0){
69+ if(display->locking)
70+ lockdisplay(display);
71+ freeimage(colors[i].im);
72+ colors[i].rgb = strtoul(v[1], nil, 16);
73+ colors[i].im = allocimage(display, Rect(0,0,1,1), RGB24, 1, colors[i].rgb<<8 | 0xff);
74+ if(display->locking)
75+ unlockdisplay(display);
76+ themechanged();
77+ break;
78+ }
79+ }
80+ }
81+ free(s);
82+ if(n != 2)
83+ break;
84+ }
85+ Bterm(b);
86+ postnote(PNGROUP, pid, "interrupt");
87+
88+ threadexits(nil);
89+}
90+
91+static int
92+loadtheme(char *filename, int init)
93+{
94+ Biobuf *in;
95+ char *s, *v[3];
96+ int i, n;
97+
98+ if ((in = Bopen(filename, OREAD)) != nil) {
99+ if(display->locking && !init)
100+ lockdisplay(display);
101+ for(;;){
102+ if((s = Brdstr(in, '\n', 1)) == nil)
103+ break;
104+ if((n = tokenize(s, v, nelem(v))) == 2){
105+ for(i = 0; i < nelem(colors); i++){
106+ if(strcmp(colors[i].id, v[0]) == 0){
107+ freeimage(colors[i].im);
108+ colors[i].rgb = strtoul(v[1], nil, 16);
109+ colors[i].im = allocimage(display, Rect(0,0,1,1), RGB24, 1, colors[i].rgb<<8 | 0xff);
110+ break;
111+ }
112+ }
113+ }
114+ free(s);
115+ if(n != 2)
116+ break;
117+ }
118+ if(display->locking && !init)
119+ unlockdisplay(display);
120+ Bterm(in);
121+ if(!init)
122+ themechanged();
123+ return 0;
124+ }
125+
126+ return -1;
127+}
128+
129+static void
130+plumbproc(void *)
131+{
132+ int f;
133+ Plumbmsg *m;
134+
135+ threadsetname("theme/plumb");
136+ if ((f = plumbopen(themeplumb, OREAD)) >= 0) {
137+ while ((m = plumbrecv(f)) != nil) {
138+ loadtheme(m->data, 0);
139+ themechanged();
140+ plumbfree(m);
141+ }
142+ }
143+
144+ threadexits(nil);
145+}
146+
147+void
148+themeinit(void)
149+{
150+ char *s;
151+ int i;
152+
153+ if((s = getenv("theme")) != nil){
154+ if(loadtheme(s, 1) != 0)
155+ sysfatal("theme load failed: %r");
156+ free(s);
157+ }
158+ for(i = 0; i < Numcolors; i++){
159+ if(colors[i].im == nil)
160+ colors[i].im = allocimage(display, Rect(0,0,1,1), RGB24, 1, colors[i].rgb<<8 | 0xff);
161+ }
162+ if((themeplumb = getenv("themeplumb")) != nil)
163+ proccreate(plumbproc, nil, 4096);
164+}
theme.h+25−0
@@ -0,0 +1,25 @@
1+enum {
2+ Dback = 0,
3+ Dfhigh,
4+ Dfmed,
5+ Dflow,
6+ Dfinv,
7+ Dbhigh,
8+ Dbmed,
9+ Dblow,
10+ Dbinv,
11+ Numcolors,
12+};
13+
14+typedef struct ThemeColor ThemeColor;
15+
16+struct ThemeColor {
17+ char *id;
18+ u32int rgb;
19+ Image *im;
20+};
21+
22+extern ThemeColor colors[Numcolors];
23+
24+void themeproc(void *fd);
25+void themeinit(void);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t