shithub: vdir

Download patch

ref: 95b7d1f07e94e97e2ee60f1036f3189a7d0ebed4
parent: 692a7dd95ec710318bc5400e97b3ebbf85e89a3e
author: phil9 <telephil9@gmail.com>
date: Mon Aug 30 18:20:03 EDT 2021

replace libevent by libthread

--- a/alert.c
+++ b/alert.c
@@ -1,7 +1,8 @@
 #include <u.h>
 #include <libc.h>
 #include <draw.h>
-#include <event.h>
+#include <thread.h>
+#include <mouse.h>
 #include <keyboard.h>
 
 enum { Padding = 12, };
@@ -15,17 +16,28 @@
 }
 
 void
-alert(const char *title, const char *message)
+alert(const char *title, const char *message, Mousectl *mctl, Keyboardctl *kctl)
 {
+	Alt alts[3];
 	Rectangle r, sc;
 	Point o, p;
 	Image *b, *save, *bg, *fg;
 	Font *tf, *mf;
-	int done, i, h, w, tw, mw;
-	Event ev;
+	int done, h, w, tw, mw;
 	Mouse m;
 	Rune k;
 
+	alts[0].op = CHANRCV;
+	alts[0].c  = mctl->c;
+	alts[0].v  = &m;
+	alts[1].op = CHANRCV;
+	alts[1].c  = kctl->c;
+	alts[1].v  = &k;
+	alts[2].op = CHANEND;
+	alts[2].c  = nil;
+	alts[2].v  = nil;
+	while(nbrecv(kctl->c, nil)==1)
+		;
 	bg = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xf8d7daff);
 	fg = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x721c24ff);
 	tf = openfont(display, Titlefont);
@@ -59,8 +71,6 @@
 		p.y += tf->height;
 		string(b, p, fg, ZP, mf, message);
 		flushimage(display, 1);
-		i = Ekeyboard|Emouse;
-		i = eread(i, &ev);
 		if(b!=screen || !eqrect(screen->clipr, sc)){
 			freeimage(save);
 			save = nil;
@@ -68,16 +78,14 @@
 		b = screen;
 		sc = b->clipr;
 		replclipr(b, 0, b->r);
-		switch(i){
+		switch(alt(alts)){
 		default:
 			continue;
 			break;
-		case Ekeyboard:
-			k = ev.kbdc;
+		case 1:
 			done = (k=='\n' || k==Kesc);
 			break;
-		case Emouse:
-			m = ev.mouse;
+		case 0:
 			done = m.buttons&1 && ptinrect(m.xy, r);
 			break;
 		}
--- a/vdir.c
+++ b/vdir.c
@@ -1,13 +1,14 @@
 #include <u.h>
 #include <libc.h>
 #include <draw.h>
-#include <event.h>
+#include <thread.h>
+#include <mouse.h>
 #include <keyboard.h>
 #include <plumb.h>
 #include <bio.h>
 #include "icons.h"
 
-extern void alert(const char *title, const char *message);
+extern void alert(const char *title, const char *message, Mousectl *mctl, Keyboardctl *kctl);
 
 enum
 {
@@ -17,10 +18,19 @@
 	Slowscroll = 10,
 };
 
+enum
+{
+	Emouse,
+	Eresize,
+	Ekeyboard,
+};
+
 char *home;
 char path[256];
 Dir* dirs;
 long ndirs;
+Mousectl *mctl;
+Keyboardctl *kctl;
 Rectangle toolr;
 Rectangle homer;
 Rectangle upr;
@@ -57,7 +67,7 @@
 	char errbuf[ERRMAX];
 
 	errstr(errbuf, ERRMAX-1);
-	alert("Error", errbuf);
+	alert("Error", errbuf, mctl, kctl);
 }
 
 void
@@ -131,7 +141,7 @@
 	else
 		snprint(newpath, sizeof newpath, "%s/%s", path, dir);
 	if(access(newpath, 0)<0){
-		alert("Error", "Directory does not exist");
+		alert("Error", "Directory does not exist", mctl, kctl);
 		return;
 	}
 	snprint(path, sizeof path, abspath(path, newpath));
@@ -146,7 +156,7 @@
 
 	p = smprint("%s/%s", path, name);
 	if(access(p, 0)>=0){
-		alert("Error", "Directory already exists");
+		alert("Error", "Directory already exists", mctl, kctl);
 		goto cleanup;
 	}
 	fd = create(p, OREAD, DMDIR|0755);
@@ -168,7 +178,7 @@
 
 	p = smprint("%s/%s", path, name);
 	if(access(p, 0)>=0){
-		alert("Error", "File already exists");
+		alert("Error", "File already exists", mctl, kctl);
 		goto cleanup;
 	}
 	fd = create(p, OREAD, 0644);
@@ -281,6 +291,7 @@
 	Point p;
 	int i, h, y;
 
+	lockdisplay(display);
 	draw(screen, screen->r, display->white, nil, ZP);
 	p = addpt(screen->r.min, Pt(0, Toolpadding));
 	draw(screen, toolr, toolbg, nil, ZP);
@@ -310,6 +321,8 @@
 		p.x = viewr.min.x+Toolpadding;
 		p.y += lineh;
 	}
+	flushimage(display, 1);
+	unlockdisplay(display);
 }
 
 int
@@ -349,9 +362,9 @@
 }
 
 void
-eresized(int new)
+evtresize(void)
 {
-	if(new && getwindow(display, Refnone)<0)
+	if(getwindow(display, Refnone)<0)
 		sysfatal("cannot reattach: %r");
 	lineh = Padding+font->height+Padding;
 	toolr = screen->r;
@@ -373,7 +386,7 @@
 	switch(k){
 	case 'q':
 	case Kdel:
-		exits(nil);
+		threadexitsall(nil);
 		break;
 	case Kpgup:
 		scrollup(nlines);
@@ -442,7 +455,7 @@
 			redraw();
 		}else if(ptinrect(m.xy, cdr)){
 			m.xy = cept("Go to directory");
-			if(eenter("Go to directory", buf, sizeof buf, &m)>0){
+			if(enter("Go to directory", buf, sizeof buf, mctl, kctl, nil)>0){
 				cd(buf);
 				redraw();
 			}
@@ -450,13 +463,13 @@
 			plumbsendtext(plumbfd, "vdir", nil, nil, path);
 		}else if(ptinrect(m.xy, newdirr)){
 			m.xy = cept("Create directory");
-			if(eenter("Create directory", buf, sizeof buf, &m)>0){
+			if(enter("Create directory", buf, sizeof buf, mctl, kctl, nil)>0){
 				mkdir(buf);
 				redraw();
 			}
 		}else if(ptinrect(m.xy, newfiler)){
 			m.xy = cept("Create file");
-			if(eenter("Create file", buf, sizeof buf, &m)>0){
+			if(enter("Create file", buf, sizeof buf, mctl, kctl, nil)>0){
 				touch(buf);
 				redraw();
 			}
@@ -480,9 +493,16 @@
 }
 
 void
-main(int argc, char *argv[])
+threadmain(int argc, char *argv[])
 {
-	Event e;
+	Mouse m;
+	Rune k;
+	Alt alts[] = {
+		{ nil, &m,  CHANRCV },
+		{ nil, nil, CHANRCV },	
+		{ nil, &k,  CHANRCV },
+		{ nil, nil, CHANEND },
+	};
 
 	offset = 0;
 	scrolling = 0;
@@ -497,17 +517,30 @@
 	loaddirs();
 	if(initdraw(nil, nil, "vdir")<0)
 		sysfatal("initdraw: %r");
+	unlockdisplay(display);
+	display->locking = 1;
+	mctl = initmouse(nil, screen);
+	if(mctl==nil)
+		sysfatal("initmouse: %r");
+	kctl = initkeyboard(nil);
+	if(kctl==nil)
+		sysfatal("initkeyboard: %r");
+	alts[Emouse].c = mctl->c;
+	alts[Eresize].c = mctl->resizec;
+	alts[Ekeyboard].c = kctl->c;
 	initcolors();
 	initimages();
-	einit(Emouse|Ekeyboard);
-	eresized(0);
+	evtresize();
 	for(;;){
-		switch(event(&e)){
-		case Ekeyboard:
-			evtkey(e.kbdc);
-			break;
+		switch(alt(alts)){
 		case Emouse:
-			evtmouse(e.mouse);
+			evtmouse(m);
+			break;
+		case Eresize:
+			evtresize();
+			break;
+		case Ekeyboard:
+			evtkey(k);
 			break;
 		}
 	}