ref: ccb2fe62a80e247e5cb6ae83addab916ec0d9696
parent: d78b63812f0bb408f250e574a4a041890b38c9e1
author: spew <spew@palas>
date: Sun Feb 2 00:55:24 EST 2025
refactoring
--- a/guitest.c
+++ b/guitest.c
@@ -78,9 +78,11 @@
textdraw(Text *t)
{
Rectangle scrpos;
+ Image *screen;
- draw(t->screen, t->screen->r, t->cols[BACK], nil, ZP);
- draw(t->screen, t->scrollr, t->cols[BORD], nil, ZP);
+ screen = *t->screen;
+ draw(screen, screen->r, t->cols[BACK], nil, ZP);
+ draw(screen, t->scrollr, t->cols[BORD], nil, ZP);
scrpos = t->scrollr;
if(t->nlines > 0){
@@ -88,7 +90,7 @@
scrpos.max.y = scrpos.min.y+Dy(t->scrollr)*t->Frame.maxlines/t->nlines;
}
scrpos = insetrect(scrpos, 1);
- draw(t->screen, scrpos, t->cols[BACK], nil, ZP);
+ draw(screen, scrpos, t->cols[BACK], nil, ZP);
frinsert(t, t->text + t->lines[t->topline], t->text + t->textlen, 0);
flushimage(display, 1);
@@ -97,13 +99,16 @@
void
textinit(Text *t)
{
- t->scrollr = t->screen->r;
- t->scrollr.max.x = t->screen->r.min.x + Scrollwid + 1;
+ Image *screen;
- t->bodyr.min.x = t->screen->r.min.x + Scrollwid + Scrollgap + Margin;
- t->bodyr.min.y = t->screen->r.min.y;
- t->bodyr.max = t->screen->r.max;
+ screen = *t->screen;
+ t->scrollr = screen->r;
+ t->scrollr.max.x = screen->r.min.x + Scrollwid + 1;
+ t->bodyr.min.x = screen->r.min.x + Scrollwid + Scrollgap + Margin;
+ t->bodyr.min.y = screen->r.min.y;
+ t->bodyr.max = screen->r.max;
+
if(t->text == nil){
t->textcap = 8192;
t->text = emallocz(t->textcap*sizeof(*t->text), 1);
@@ -118,7 +123,7 @@
}
frclear(t, 0);
- frinit(t, t->bodyr, display->defaultfont, t->screen, t->cols);
+ frinit(t, t->bodyr, display->defaultfont, screen, t->cols);
}
void
@@ -185,10 +190,35 @@
}
void
+textresize(Text *t)
+{
+ static char s[512];
+
+ snprint(s, sizeof(s), "%s/winname", t->wsys);
+ if(gengetwindow(display, s, t->screen, t->_screen, Refnone) < 0)
+ sysfatal("%s: %r", argv0);
+ textinit(t);
+ textdraw(t);
+}
+
+void
+initmousekbd(char *wsys, Image *screen, Mousectl **mc, Keyboardctl **kc)
+{
+ static char s[512];
+
+ snprint(s, sizeof(s), "%s/mouse", wsys);
+ if((*mc = initmouse(s, screen)) == nil)
+ sysfatal("initmouse failed: %r");
+ snprint(s, sizeof(s), "%s/cons", wsys);
+ if((*kc = initkeyboard(s)) == nil)
+ sysfatal("initkeyboard failed: %r");
+}
+
+void
threadmain(int argc, char **argv)
{
- Keyboardctl *kc, *kcnicks;
- Mousectl *mc, *mcnicks;
+ Keyboardctl *kc, *nickskc;
+ Mousectl *mc, *nicksmc;
Rune r, *rs;
Mouse m;
Chan *chan;
@@ -225,8 +255,8 @@
chan = emallocz(sizeof(*chan), 1);
memmove(chan->body.cols, cols, sizeof(cols));
- chan->body.screen = screen;
- chan->body._screen = _screen;
+ chan->body.screen = &screen;
+ chan->body._screen = &_screen;
chan->body.wsys = "/dev";
textinit(&chan->body);
textaddlines(&chan->body, lorem);
@@ -240,8 +270,8 @@
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.screen = &nicksscreen;
+ chan->nicks._screen = &_nicksscreen;
chan->nicks.wsys = nickswsys;
textinit(&chan->nicks);
textaddlines(&chan->nicks, lorem);
@@ -250,22 +280,16 @@
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((kcnicks = initkeyboard("/mnt/nickswsys/cons")) == nil)
- sysfatal("initkeyboard failed: %r");
- if((mcnicks = initmouse("/mnt/nickswsys/mouse", chan->nicks.screen)) == nil)
- sysfatal("initmouse failed: %r");
+ initmousekbd("/dev", screen, &mc, &kc);
+ initmousekbd(nickswsys, nicksscreen, &nicksmc, &nickskc);
Alt a[NCHAN+1] = {
[MOUSE] = {mc->c, &m, CHANRCV},
- [MOUSENICKS] = {mcnicks->c, &m, CHANRCV},
+ [MOUSENICKS] = {nicksmc->c, &m, CHANRCV},
[RESIZE] = {mc->resizec, nil, CHANRCV},
- [RESIZENICKS] = {mcnicks->resizec, nil, CHANRCV},
+ [RESIZENICKS] = {nicksmc->resizec, nil, CHANRCV},
[KEYBD] = {kc->c, &r, CHANRCV},
- [KEYBDNICKS] = {kcnicks->c, &r, CHANRCV},
+ [KEYBDNICKS] = {nickskc->c, &r, CHANRCV},
[NCHAN] = {nil, nil, CHANEND},
};
for(;;)switch(alt(a)){
@@ -292,21 +316,15 @@
while(m.buttons == 2){
texttopline(&chan->nicks, m.xy.y);
textdraw(&chan->nicks);
- readmouse(mcnicks);
- m = *mcnicks;
+ readmouse(nicksmc);
+ m = *nicksmc;
}
break;
case RESIZE:
- if(getwindow(display, Refnone) < 0)
- sysfatal("%s: %r", argv0);
- textinit(&chan->body);
- textdraw(&chan->body);
+ textresize(&chan->body);
break;
case RESIZENICKS:
- if(gengetwindow(display, "/mnt/nickswsys/winname", &chan->nicks.screen, &chan->nicks._screen, Refnone) < 0)
- sysfatal("%s: %r", argv0);
- textinit(&chan->nicks);
- textdraw(&chan->nicks);
+ textresize(&chan->nicks);
break;
}
end:
--- a/mez.h
+++ b/mez.h
@@ -7,13 +7,15 @@
char *host, *user, *passwd;
};
+// The multiple indirection for the screen is because
+// multiple texts may share the same screen.
struct Text {
Frame;
Rune *text;
uint textlen, topline, nlines, linescap, textcap, *lines;
Rectangle scrollr, bodyr;
- Image *screen, *cols[NCOL];
- Screen *_screen;
+ Image **screen, *cols[NCOL];
+ Screen **_screen;
char *wsys;
};
--
⑨