ref: a93d7290f6d49317413fe60b7badb60cc7e97fe4
parent: 9d12fed5a69249baf09ae42781ce1c37e21f4968
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sat Mar 14 21:59:08 EDT 2020
plan9: run picker to modify the palette
--- a/plan9.c
+++ b/plan9.c
@@ -39,6 +39,7 @@
Menu3save,
Menu3dotstyle,
Menu3rulerstyle,
+ Menu3colors,
Menu3exit,
Nummenu3,
@@ -143,9 +144,10 @@
};
static char *menu3i[Nummenu3+1] = {
- [Menu3load] = "load",
- [Menu3save] = "save",
- [Menu3exit] = "exit",
+ [Menu3load] = "load",
+ [Menu3save] = "save",
+ [Menu3colors] = "colors",
+ [Menu3exit] = "exit",
};
static Menu menu3 = {
@@ -1144,7 +1146,7 @@
}
static void
-plumbthread(void *)
+plumbproc(void *)
{
int f;
Plumbmsg *m;
@@ -1162,6 +1164,84 @@
}
static void
+runpicker(void *x)
+{
+ int *p, i, a, wfd;
+ char *args[1+Numcolors+1], *wsys;
+ char colors[Numcolors][8];
+ char tmp[64];
+
+ if ((wsys = getenv("wsys")) == nil) {
+ fprint(2, "wsys not set\n");
+ goto err;
+ }
+ if ((wfd = open(wsys, ORDWR)) < 0) {
+ fprint(2, "can't open wsys: %r\n");
+ goto err;
+ }
+
+ snprint(tmp, sizeof(tmp), "new -pid %d -dx %d -dy %d", getpid(), 384, 320);
+
+ if (mount(wfd, -1, "/mnt/wsys", MREPL, tmp) < 0) {
+ fprint(2, "couldn't mount wsys: %r\n");
+ goto err;
+ }
+ if (bind("/mnt/wsys", "/dev", MBEFORE) < 0) {
+ fprint(2, "couldn't bind wsys: %r\n");
+ goto err;
+ }
+
+ a = 0;
+ args[a++] = "picker";
+ for (i = 0; i < Numcolors; i++) {
+ sprint(colors[i], "%06x", theme[i]>>8);
+ args[a++] = colors[i];
+ }
+ args[a] = nil;
+
+ p = x;
+ close(0);
+ dup(p[1], 1); close(p[1]); close(p[0]);
+ dup(open("/dev/null", OWRITE), 2);
+ procexec(nil, "/bin/picker", args);
+
+err:
+ threadexits("exec: %r");
+}
+
+static void
+colorsproc(void *)
+{
+ Biobuf *in;
+ char *s, *v[3];
+ int p[4], n, i;
+
+ threadsetname("colorsproc");
+ pipe(p);
+ procrfork(runpicker, p, 4096, RFFDG|RFNAMEG);
+ close(p[1]);
+ in = Bfdopen(p[0], OREAD);
+ for (;;) {
+ if ((s = Brdstr(in, '\n', 1)) == nil)
+ break;
+ if ((n = tokenize(s, v, nelem(v))) == 2 && (i = atoi(v[0])) >= 0 && i < Numcolors) {
+ lockdisplay(display);
+ freeimage(color[i]);
+ theme[i] = strtoul(v[1], nil, 16);
+ color[i] = allocimage(display, Rect(0, 0, 1, 1), RGB24, 1, theme[i]<<8 | 0xff);
+ unlockdisplay(display);
+ redraw(1);
+ }
+ free(s);
+ if (n != 2)
+ break;
+ }
+ Bterm(in);
+
+ threadexits(nil);
+}
+
+static void
kbdproc(void *cchan)
{
char buf[128], buf2[128], *s;
@@ -1413,7 +1493,7 @@
snaps = calloc(maxsnaps, sizeof(Snap));
- proccreate(plumbthread, nil, mainstacksize);
+ proccreate(plumbproc, nil, mainstacksize);
for (;;) {
redraw(complete);
@@ -1480,6 +1560,8 @@
dotstyle = ++dotstyle % Numstyles;
} else if (n == Menu3rulerstyle) {
rulerstyle = ++rulerstyle % Numstyles;
+ } else if (n == Menu3colors) {
+ proccreate(colorsproc, nil, mainstacksize);
} else if (n == Menu3exit) {
goto end;
}