shithub: mez

Download patch

ref: d78b63812f0bb408f250e574a4a041890b38c9e1
parent: 41b28a4595a25d2f1eb076d009ef06460b3276db
author: spew <spew@palas>
date: Sat Feb 1 18:02:17 EST 2025

make window creation a function

--- a/guitest.c
+++ b/guitest.c
@@ -8,6 +8,10 @@
 #include <frame.h>
 #include "mez.h"
 
+Image *nicksscreen, *cmdscreen, *nickscols[NCOL], *cols[NCOL], *cmdcols[NCOL];
+char *nickswsys = "/mnt/nickswsys", *cmdwsys = "/mnt/cmdwsys";
+Screen *_nicksscreen, *_cmdscreen;
+
 char buf[32000];
 Rune lorem[32000];
 
@@ -39,7 +43,7 @@
 	void *p;
 
 	if((p = mallocz(sz, clr)) == nil)
-		error("Out of memory");
+		error("Out of memory here");
 	setmalloctag(p, getcallerpc(&sz));
 	return p;
 }
@@ -50,7 +54,7 @@
 	void *p;
 
 	if((p = realloc(ptr, size)) == nil)
-		error("Out of memory");
+		error("Out of memory there");
 	setmalloctag(p, getcallerpc(&ptr));
 	return p;
 }
@@ -118,6 +122,32 @@
 }
 
 void
+createwindow(char *wsys, Image **screen, Screen **_screen, Rectangle r)
+{
+	static char s[512];
+	char *wsysv;
+	int fd;
+
+	if((wsysv = getenv("wsys")) == nil)
+		sysfatal("cannot find $wsys: %r");
+	if((fd = open(wsysv, ORDWR)) < 0)
+		sysfatal("cannot open $wsys: %r");
+	free(wsysv);
+	snprint(s, sizeof(s), "new -r %d %d %d %d", r.min.x, r.min.y, r.max.x, r.max.y);
+	if(mount(fd, -1, wsys, MREPL, s) < 0)
+		sysfatal("cannot create new window: %r");
+	close(fd);
+	snprint(s, sizeof(s), "%s/label", wsys);
+	if((fd = open(s, OWRITE)) < 0)
+		sysfatal("cannot open label: %r");
+	write(fd, "guitest", sizeof("guitest"));
+	close(fd);
+	snprint(s, sizeof(s), "%s/winname", wsys);
+	if(gengetwindow(display, s, screen, _screen, Refnone) < 0)
+		sysfatal("cannot get window: %r");
+}
+
+void
 textsetline(Text *t, uint sol)
 {
 	if(t->nlines == t->linescap){
@@ -139,7 +169,7 @@
 		t->textcap *= 2;
 		t->text = erealloc(t->text, t->textcap*sizeof(*t->text));
 	}
-	runestrecpy(t->text + t->textlen, t->text+t->textcap, s);
+	runestrecpy(t->text+t->textlen, t->text+t->textcap, s);
 	sol = t->textlen;
 	for(rp = s; rp = runestrchr(rp, L'\n'); rp++){
 		textsetline(t, sol);
@@ -162,8 +192,8 @@
 	Rune r, *rs;
 	Mouse m;
 	Chan *chan;
-	char *s, *wsys, *mntstr;
-	int len, fd;
+	char *s;
+	int len;
 
 	ARGBEGIN{}ARGEND
 
@@ -175,19 +205,29 @@
 
 	if(initdraw(nil, nil, "guitest") < 0)
 		sysfatal("initdraw: %r");
-	if((mc = initmouse(nil, screen)) == nil)
-		sysfatal("initmouse failed: %r");
-	if((kc = initkeyboard(nil)) == nil)
-		sysfatal("initkeyboard failed: %r");
 
+	cols[BACK] = allocimagemix(display, DPurpleblue, DWhite);
+	cols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkyellow);
+	cols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPurpleblue);
+	cols[TEXT] = display->black;
+	cols[HTEXT] = display->black;
+	nickscols[BACK] = allocimagemix(display, DCyan, DWhite);
+	nickscols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkyellow);
+	nickscols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPurpleblue);
+	nickscols[TEXT] = display->black;
+	nickscols[HTEXT] = display->black;
+	cmdcols[BACK] = allocimagemix(display, DPalebluegreen, DWhite);
+	cmdcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPalegreygreen);
+	cmdcols[BORD] = allocimage(display, Rect(0,0,2,2), screen->chan, 1, DPurpleblue);
+	cmdcols[TEXT] = display->black;
+	cmdcols[HTEXT] = display->black;
+
 	chan = emallocz(sizeof(*chan), 1);
 
-	chan->body.cols[BACK] = allocimagemix(display, DPurpleblue, DWhite);
-	chan->body.cols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkyellow);
-	chan->body.cols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPurpleblue);
-	chan->body.cols[TEXT] = display->black;
-	chan->body.cols[HTEXT] = display->black;
+	memmove(chan->body.cols, cols, sizeof(cols));
 	chan->body.screen = screen;
+	chan->body._screen = _screen;
+	chan->body.wsys = "/dev";
 	textinit(&chan->body);
 	textaddlines(&chan->body, lorem);
 
@@ -197,29 +237,26 @@
 		len = chartorune(rs, s);
 	*rs = L'\0';
 
-	if((wsys = getenv("wsys")) == nil)
-		sysfatal("cannot find $wsys: %r");
-	if((fd = open(wsys, ORDWR)) < 0)
-		sysfatal("cannot open $wsys: %r");
-	mntstr = smprint("new -r %d %d %d %d", screen->r.min.x - Dx(screen->r)/3, screen->r.min.y-4, screen->r.min.x-4, screen->r.max.y+4);
-	if(mount(fd, -1, "/mnt/wsysnicks", MREPL, mntstr) < 0)
-		sysfatal("cannot create new window: %r");
-	close(fd);
-	fd = open("/mnt/wsysnicks/label", OWRITE);
-	write(fd, "nicks", sizeof("nicks"));
-	close(fd);
-	if(gengetwindow(display, "/mnt/wsysnicks/winname", &chan->nicks.screen, &chan->nicks._screen, Refnone) < 0)
-		sysfatal("cannot get window: %r");
-	chan->nicks.cols[BACK] = allocimagemix(display, DCyan, DWhite);
-	chan->nicks.cols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkyellow);
-	chan->nicks.cols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPurpleblue);
-	chan->nicks.cols[TEXT] = display->black;
-	chan->nicks.cols[HTEXT] = display->black;
+	createwindow(nickswsys, &nicksscreen, &_nicksscreen, (Rectangle){(Point){screen->r.min.x - Dx(screen->r)/3, screen->r.min.y-4}, (Point){screen->r.min.x-4, screen->r.max.y+4}});
+
+	memmove(chan->nicks.cols, nickscols, sizeof(nickscols));
+	chan->nicks.screen = nicksscreen;
+	chan->nicks._screen = _nicksscreen;
+	chan->nicks.wsys = nickswsys;
 	textinit(&chan->nicks);
 	textaddlines(&chan->nicks, lorem);
-	if((kcnicks = initkeyboard("/mnt/wsysnicks/cons")) == nil)
+
+	textdraw(&chan->body);
+	textdraw(&chan->nicks);
+	flushimage(display, 1);
+
+	if((mc = initmouse(nil, screen)) == nil)
+		sysfatal("initmouse failed: %r");
+	if((kc = initkeyboard(nil)) == nil)
 		sysfatal("initkeyboard failed: %r");
-	if((mcnicks = initmouse("/mnt/wsysnicks/mouse", chan->nicks.screen)) == nil)
+	if((kcnicks = initkeyboard("/mnt/nickswsys/cons")) == nil)
+		sysfatal("initkeyboard failed: %r");
+	if((mcnicks = initmouse("/mnt/nickswsys/mouse", chan->nicks.screen)) == nil)
 		sysfatal("initmouse failed: %r");
 
 	Alt a[NCHAN+1] = {
@@ -231,9 +268,6 @@
 		[KEYBDNICKS] = {kcnicks->c, &r, CHANRCV},
 		[NCHAN] = {nil, nil, CHANEND},
 	};
-	textdraw(&chan->body);
-	textdraw(&chan->nicks);
-	flushimage(display, 1);
 	for(;;)switch(alt(a)){
 	default:
 		break;
@@ -269,7 +303,7 @@
 		textdraw(&chan->body);
 		break;
 	case RESIZENICKS:
-		if(gengetwindow(display, "/mnt/wsysnicks/winname", &chan->nicks.screen, &chan->nicks._screen, Refnone) < 0)
+		if(gengetwindow(display, "/mnt/nickswsys/winname", &chan->nicks.screen, &chan->nicks._screen, Refnone) < 0)
 			sysfatal("%s: %r", argv0);
 		textinit(&chan->nicks);
 		textdraw(&chan->nicks);
@@ -276,6 +310,6 @@
 		break;
 	}
 end:
-	unmount(nil, "/mnt/wsysnicks");
+	unmount(nil, nickswsys);
 	threadexitsall(0);
 }
--- a/mez.h
+++ b/mez.h
@@ -14,6 +14,7 @@
 	Rectangle scrollr, bodyr;
 	Image *screen, *cols[NCOL];
 	Screen *_screen;
+	char *wsys;
 };
 
 struct Chan {
@@ -26,4 +27,5 @@
 	char *name, *host, *state, *nick, *user, *real;
 	Chan *channels;
 	int fd, id;
+	char buf[1024];
 };