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
17 gbuffer.$O\17 gbuffer.$O\
18 plan9.$O\18 plan9.$O\
19 sim.$O\19 sim.$O\
20 theme.$O\
20 vmio.$O\21 vmio.$O\
21 22
22default:V: all23default:V: all
plan9.c+30−131
@@ -10,6 +10,7 @@ · 9 unmodified lines
10#include <plumb.h>10#include <plumb.h>
11#include <cursor.h>11#include <cursor.h>
12#include <tos.h>12#include <tos.h>
13#include "theme.h"
13 14
14#define MIN(x,y) ((x)<=(y)?(x):(y))15#define MIN(x,y) ((x)<=(y)?(x):(y))
15#define MAX(x,y) ((x)>=(y)?(x):(y))16#define MAX(x,y) ((x)>=(y)?(x):(y))
@@ -45,17 +46,6 @@ enum { · 29 unmodified lines
45 Menu3exit,46 Menu3exit,
46 Nummenu3,47 Nummenu3,
47 48
48 Dback = 0,
49 Dfhigh,
50 Dfmed,
51 Dflow,
52 Dfinv,
53 Dbhigh,
54 Dbmed,
55 Dblow,
56 Dbinv,
57 Numcolors,
58
59 /* this might become a bad idea in the future */49 /* this might become a bad idea in the future */
60 Mark_flag_group_highlight = 1<<6,50 Mark_flag_group_highlight = 1<<6,
61 Mark_flag_selected = 1<<7,51 Mark_flag_selected = 1<<7,
@@ -143,23 +133,6 @@ static struct { · 81 unmodified lines
143 },133 },
144};134};
145 135
146static 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
161static Image *color[Numcolors];
162
163static char *modes[Nummodes] = {136static char *modes[Nummodes] = {
164 [Minsert] = "insert",137 [Minsert] = "insert",
165 [Mappend] = "append",138 [Mappend] = "append",
@@ -596,10 +569,10 @@ redraw(int complete) · 430 unmodified lines
596 if (complete) {569 if (complete) {
597 r = screen->r;570 r = screen->r;
598 r.max.y = r.min.y + txtoff.y;571 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);
600 r = screen->r;573 r = screen->r;
601 r.max.x = r.min.x + txtoff.x;574 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);
603 }576 }
604 577
605 off = field.width*cur.y + cur.x;578 off = field.width*cur.y + cur.x;
@@ -636,7 +609,7 @@ redraw(int complete) · 30 unmodified lines
636 609
637 if (!grouphl && !complete && c == fscr.buffer[off] && attr == mscr.buffer[off]) {610 if (!grouphl && !complete && c == fscr.buffer[off] && attr == mscr.buffer[off]) {
638 if (i > 0) {611 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);
640 i = 0;613 i = 0;
641 }614 }
642 p.x += glyphsz.x;615 p.x += glyphsz.x;
@@ -687,43 +660,43 @@ redraw(int complete) · 44 unmodified lines
687 }660 }
688 661
689 if (bg != oldbg || fg != oldfg) {662 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);
691 i = 0;664 i = 0;
692 }665 }
693 linebuf[i++] = c;666 linebuf[i++] = c;
694 }667 }
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);
696 p.y += glyphsz.y;669 p.y += glyphsz.y;
697 }670 }
698 671
699 r = screen->r;672 r = screen->r;
700 r.min.x += txtoff.x + max.x*glyphsz.x;673 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);
702 675
703 r = screen->r;676 r = screen->r;
704 r.min.y += txtoff.y + max.y*glyphsz.y;677 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);
706 679
707 p = top;680 p = top;
708 p.y += glyphsz.y*(max.y-1)/2;681 p.y += glyphsz.y*(max.y-1)/2;
709 if (scroll.x > 0) {682 if (scroll.x > 0) {
710 p.x = top.x - txtoff.x;683 p.x = top.x - txtoff.x;
711 string(screen, p, color[Dfmed], ZP, font, "←");684 string(screen, p, colors[Dfmed].im, ZP, font, "←");
712 }685 }
713 if (max.x+scroll.x < field.width) {686 if (max.x+scroll.x < field.width) {
714 p.x = top.x + max.x*glyphsz.x + txtoff.x - glyphsz.x;687 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, "→");
716 }689 }
717 690
718 p = top;691 p = top;
719 p.x += glyphsz.x*(max.x-1)/2;692 p.x += glyphsz.x*(max.x-1)/2;
720 if (scroll.y > 0) {693 if (scroll.y > 0) {
721 p.y = screen->r.min.y;694 p.y = screen->r.min.y;
722 string(screen, p, color[Dfmed], ZP, font, "↑");695 string(screen, p, colors[Dfmed].im, ZP, font, "↑");
723 }696 }
724 if (max.y+scroll.y < field.height) {697 if (max.y+scroll.y < field.height) {
725 p.y = top.y + max.y*glyphsz.y;698 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, "↓");
727 }700 }
728 701
729 i = 0;702 i = 0;
@@ -741,7 +714,7 @@ redraw(int complete) · 11 unmodified lines
741 i += runesprint(linebuf+i, "%-9s", s);714 i += runesprint(linebuf+i, "%-9s", s);
742 sprint(s, "%ldms", labs(framedev));715 sprint(s, "%ldms", labs(framedev));
743 i += runesprint(linebuf+i, "%-8s", s);716 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);
745 bot.y += glyphsz.y;718 bot.y += glyphsz.y;
746 719
747 i = 0;720 i = 0;
@@ -752,12 +725,18 @@ redraw(int complete) · 4 unmodified lines
752 i += runesprint(linebuf+i, "%-9s", s);725 i += runesprint(linebuf+i, "%-9s", s);
753 i += runesprint(linebuf+i, "%-9s", modes[altdown ? Mslide : mode]);726 i += runesprint(linebuf+i, "%-9s", modes[altdown ? Mslide : mode]);
754 i += runesprint(linebuf+i, "%s", filename[0] ? filename : "unnamed");727 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);
756 729
757 flushimage(display, 1);730 flushimage(display, 1);
758 unlockdisplay(display);731 unlockdisplay(display);
759}732}
760 733
734void
735themechanged(void)
736{
737 redraw(1);
738}
739
761static void740static void
762command(char *s, void (*snapshot)(void))741command(char *s, void (*snapshot)(void))
763{742{
@@ -815,7 +794,7 @@ command(char *s, void (*snapshot)(void)) · 51 unmodified lines
815 } else if (s[0] == 'c' && s[1] == 'o') { /* color ffffff 111111 232323 ... */794 } else if (s[0] == 'c' && s[1] == 'o') { /* color ffffff 111111 232323 ... */
816 if (argc < 2) {795 if (argc < 2) {
817 for (i = 0; i < Numcolors; i++)796 for (i = 0; i < Numcolors; i++)
818 print("%06x ", theme[i].color);797 print("%06x ", colors[i].rgb);
819 print("\n");798 print("\n");
820 fflush(stdout);799 fflush(stdout);
821 } else {800 } else {
@@ -825,9 +804,9 @@ command(char *s, void (*snapshot)(void)) · 3 unmodified lines
825 continue;804 continue;
826 if ((im = allocimage(display, Rect(0, 0, 1, 1), RGB24, 1, c<<8 | 0xff)) == nil)805 if ((im = allocimage(display, Rect(0, 0, 1, 1), RGB24, 1, c<<8 | 0xff)) == nil)
827 continue;806 continue;
828 freeimage(color[i-1]);807 freeimage(colors[i-1].im);
829 theme[i-1].color = c;808 colors[i-1].rgb = c;
830 color[i-1] = im;809 colors[i-1].im = im;
831 }810 }
832 unlockdisplay(display);811 unlockdisplay(display);
833 redraw(1);812 redraw(1);
@@ -1168,88 +1147,6 @@ plumbproc(void *) · 334 unmodified lines
1168 threadexits(nil);1147 threadexits(nil);
1169}1148}
1170 1149
1171static void
1172runpicker(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
1204err:
1205 threadexits("exec: %r");
1206}
1207
1208static void
1209colorsproc(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
1253static void1150static void
1254kbdproc(void *cchan)1151kbdproc(void *cchan)
1255{1152{
@@ -1357,7 +1254,7 @@ threadmain(int argc, char **argv) · 101 unmodified lines
1357 Point p;1254 Point p;
1358 char tmp[256];1255 char tmp[256];
1359 char cmd[256];1256 char cmd[256];
1360 int oldw, oldh, w, h, n, oldbuttons;1257 int oldw, oldh, w, h, n, oldbuttons, themetid;
1361 long seed;1258 long seed;
1362 bool complete;1259 bool complete;
1363 Alt a[Numchan+1] = {1260 Alt a[Numchan+1] = {
@@ -1464,8 +1361,8 @@ threadmain(int argc, char **argv) · 100 unmodified lines
1464 1361
1465 proccreate(stdinproc, nil, mainstacksize);1362 proccreate(stdinproc, nil, mainstacksize);
1466 1363
1467 for (n = 0; n < Numcolors; n++)1364 themeinit();
1468 color[n] = allocimage(display, Rect(0, 0, 1, 1), RGB24, 1, theme[n].color<<8 | 0xff);1365
1469 glyphsz.x = stringwidth(font, "@");1366 glyphsz.x = stringwidth(font, "@");
1470 glyphsz.y = font->height;1367 glyphsz.y = font->height;
1471 1368
@@ -1496,6 +1393,7 @@ threadmain(int argc, char **argv) · 24 unmodified lines
1496 move.y = 1;1393 move.y = 1;
1497 oldbuttons = 0;1394 oldbuttons = 0;
1498 cmd[0] = 0;1395 cmd[0] = 0;
1396 themetid = -1;
1499 1397
1500 netdial(nil, nil);1398 netdial(nil, nil);
1501 midiopen(nil);1399 midiopen(nil);
@@ -1570,7 +1468,7 @@ noredraw: · 68 unmodified lines
1570 } else if (n == Menu3rulerstyle) {1468 } else if (n == Menu3rulerstyle) {
1571 rulerstyle = ++rulerstyle % Numstyles;1469 rulerstyle = ++rulerstyle % Numstyles;
1572 } else if (n == Menu3colors) {1470 } else if (n == Menu3colors) {
1573 proccreate(colorsproc, nil, mainstacksize);1471 themetid = proccreate(themeproc, nil, 4096);
1574 } else if (n == Menu3exit) {1472 } else if (n == Menu3exit) {
1575 setcursor(mctl, &yousure);1473 setcursor(mctl, &yousure);
1576 for (;;) {1474 for (;;) {
@@ -1840,6 +1738,7 @@ noredraw: · 263 unmodified lines
1840 }1738 }
1841 1739
1842end:1740end:
1741 threadint(themetid);
1843 chanclose(a[Ckey].c);1742 chanclose(a[Ckey].c);
1844 chanclose(a[Credraw].c);1743 chanclose(a[Credraw].c);
1845 mbuf_reusable_deinit(&mscr);1744 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
9ThemeColor 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
21void themechanged(void);
22
23static char *themeplumb;
24
25static void
26runpicker(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
44void
45themeproc(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
91static int
92loadtheme(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
129static void
130plumbproc(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
147void
148themeinit(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 @@
1enum {
2 Dback = 0,
3 Dfhigh,
4 Dfmed,
5 Dflow,
6 Dfinv,
7 Dbhigh,
8 Dbmed,
9 Dblow,
10 Dbinv,
11 Numcolors,
12};
13
14typedef struct ThemeColor ThemeColor;
15
16struct ThemeColor {
17 char *id;
18 u32int rgb;
19 Image *im;
20};
21
22extern ThemeColor colors[Numcolors];
23
24void themeproc(void *fd);
25void 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