shithub: mpl

Download patch

ref: 0a1d34375fe8c337100167df74058b0921b8cc03
parent: c0bc11f51c1220c8262499d6fccb8182a8803713
author: Jacob Moody <jsmoody@iastate.edu>
date: Tue Oct 8 16:27:15 EDT 2019

Add mouse events to change current song

--- a/dat.h
+++ b/dat.h
@@ -92,6 +92,13 @@
 	Album *start, *stop, *cur;
 };
 
+typedef struct Click Click;
+struct Click{
+	Rectangle r;
+	Album *a;
+	int songnum;
+};
+
 /*
  * Simple hashmap implementation.
  * Hnode key must be non nil.
--- a/draw.c
+++ b/draw.c
@@ -185,12 +185,13 @@
 }
 
 Point
-drawalbum(Album *a, Image *textcolor, Image *active, Point start, int cursong)
+drawalbum(Album *a, Image *textcolor, Image *active, Point start, int cursong, Channel *clickout)
 {
 	uint i;
 	Font *f = screen->display->defaultfont;
 	Rune *tracktitle = nil;
 	Point p = start;
+	Click c;
 
 	if(a->cover == nil)
 		a->cover = readcover(a->songs[0]);
@@ -216,6 +217,10 @@
 			break;
 		}
 		runestring(screen, p, i == cursong ? active : textcolor, ZP, f, tracktitle);
+		c.r = Rpt(p, Pt(p.x+runestrlen(tracktitle)*f->width,p.y+f->height));
+		c.a = a;
+		c.songnum = i;
+		send(clickout, &c);
 		p.y += f->height;
 	}
 	if(p.y > start.y+256)
@@ -226,7 +231,7 @@
 }
 
 void
-drawlibrary(Album *start, Album *stop, Album *cur, Image *textcolor, Image *active, int cursong)
+drawlibrary(Album *start, Album *stop, Album *cur, Image *textcolor, Image *active, int cursong, Channel *clickout)
 {
 	Point p = screen->r.min;
 	int height = screen->r.max.y - screen->r.min.y;
@@ -235,7 +240,7 @@
 	stop+=1;
 
 	for(;start!=stop;start++)
-		p = drawalbum(start, textcolor, active, p, start == cur ? cursong : -1);
+		p = drawalbum(start, textcolor, active, p, start == cur ? cursong : -1, clickout);
 }
 
 void
--- a/fncs.h
+++ b/fncs.h
@@ -23,10 +23,10 @@
 FlacMeta*	readflacmeta(int, int);
 
 /* draw.c */
-Point	drawalbum(Album*, Image*, Image*, Point, int);
+Point	drawalbum(Album*, Image*, Image*, Point, int, Channel*);
 Image*	convpic(int, char*);
 Image*	convpicbuf(uchar*, uvlong, char*);
-void	drawlibrary(Album*, Album*, Album*, Image*, Image*, int);
+void	drawlibrary(Album*, Album*, Album*, Image*, Image*, int, Channel*);
 void	drawvolume(int, Image*);
 
 /* dir.c */
@@ -39,7 +39,10 @@
 void*	mapget(Hmap*,char*);
 
 /* lib.c */
-void	spawnlib(Channel*,Channel*,char*);
+void	spawnlib(Channel*,Channel*,Channel*,Channel*,char*);
 
 /* vol.c */
-void	spawnvol(Channel*,Channel*);
\ No newline at end of file
+void	spawnvol(Channel*,Channel*);
+
+/* event.c */
+void	spawnevent(Channel*,Channel*,Channel*,Channel*);
\ No newline at end of file
--- a/lib.c
+++ b/lib.c
@@ -11,7 +11,9 @@
 
 enum{
 	LMSG,
-	QUEUE,
+	QUEUEPOP,
+	EIN,
+	OUT,
 };
 
 char*
@@ -58,34 +60,47 @@
 	Channel **chans = arg;
 	Channel *lctl = chans[0];
 	Channel *out = chans[1];
+	Channel *ein = chans[2];
+	Channel *resize = chans[3];
 	free(chans);
 
 	enum cmsg msg;
+	Click c;
 
 	Alt alts[] = {
 		{lctl, &msg, CHANRCV},
 		{queueout, nil, CHANRCV},
+		{ein, &c, CHANRCV},
 		{out, &lib, CHANSND},
 		{nil, nil, CHANEND},
 	};
-	for(;;)
+	for(;;){
 		switch(alt(alts)){
 		case LMSG:
 			handlemsg(msg);
 			break;
-		case QUEUE:
+		case QUEUEPOP:
 			handlemsg(NEXT);
 			break;
+		case EIN:
+			lib.cur = c.a;
+			lib.cursong = c.songnum;
+			sendp(queuein, nextsong(&lib));
+			break;
+		case OUT:
+			continue;
 		}
+		send(resize, nil);
+	}
 }
 
 void
-spawnlib(Channel *ctl, Channel *out, char *path)
+spawnlib(Channel *ctl, Channel *out, Channel *ein, Channel *resize, char *path)
 {
 	Channel **chans;
 
 	queuein = queueout = decctl = nil;
-	spawndec(&queuein, &queueout, &decctl);
+	spawndec(&queuein, &decctl, &queueout);
 
 	lib.cursong = 0;
 	lib.nalbum = parselibrary(&(lib.start), path);
@@ -94,9 +109,11 @@
 	lib.cur = lib.start;
 	lib.stop = lib.start+(lib.nalbum-1);
 
-	chans = emalloc(sizeof(Channel*)*2);
+	chans = emalloc(sizeof(Channel*)*4);
 	chans[0] = ctl;
 	chans[1] = out;
+	chans[2] = ein;
+	chans[3] = resize;
 
 	sendp(queuein, nextsong(&lib));
 	threadcreate(libproc, chans, 8192);
--- a/mkfile
+++ b/mkfile
@@ -13,6 +13,7 @@
 	dat.$O \
 	lib.$O \
 	vol.$O \
+	event.$O \
 
 
 </sys/src/cmd/mkone
\ No newline at end of file
--- a/mpl.c
+++ b/mpl.c
@@ -10,7 +10,6 @@
 #include "fncs.h"
 
 enum {
-	MOUSEC,
 	RESIZEC,
 	KEYC,
 	NONE
@@ -20,6 +19,7 @@
 Keyboardctl *kctl;
 Channel		*ctl, *lout;
 Channel		*vctl, *vlevel;
+Channel		*clickin, *clickreset;
 int			decpid;
 
 Image *black;
@@ -53,9 +53,10 @@
 	if(isnew && getwindow(display, Refnone) < 0)
 		quit("eresized: Can't reattach to window");
 
+	send(clickreset, nil);
 	draw(screen, screen->r, background, nil, ZP);
 	recv(lout, &lib);
-	drawlibrary(lib.cur, lib.stop, lib.cur, black, red, lib.cursong);
+	drawlibrary(lib.cur, lib.stop, lib.cur, black, red, lib.cursong, clickin);
 	recv(vlevel, &level);
 	drawvolume(level, black);
 	flushimage(display, Refnone);
@@ -113,8 +114,8 @@
 void
 threadmain(int argc, char *argv[])
 {
-	Mouse mouse;
 	Rune kbd;
+	Channel *clickout;
 	int resize[2];
 	ctl = vctl = vlevel = nil;
 
@@ -132,9 +133,14 @@
 	if((kctl = initkeyboard(nil)) == nil)
 		sysfatal("initkeyboard: %r");
 
+	clickin = chancreate(sizeof(Click), 0);
+	clickout = chancreate(sizeof(Click), 0);
+	clickreset = chancreate(1, 0);
+	spawnevent(mctl->c, clickin, clickout, clickreset);
+
 	ctl = chancreate(sizeof(enum cmsg), 0);
 	lout = chancreate(sizeof(Lib), 0);
-	spawnlib(ctl, lout, argv[1]);
+	spawnlib(ctl, lout, clickout, mctl->resizec, argv[1]);
 
 	vctl = chancreate(sizeof(enum volmsg), 0);
 	vlevel = chancreate(sizeof(int), 0);
@@ -147,7 +153,6 @@
 	eresized(0);
 
 	Alt alts[] = {
-		{mctl->c, &mouse, CHANRCV},
 		{mctl->resizec, resize, CHANRCV},
 		{kctl->c, &kbd, CHANRCV},
 		{nil, nil, CHANEND},