~moody/mpl

mpl/mpl.c -rw-r--r-- 4.1 KiB
7309e0f2 — Jacob Moody Archive and point to zuke 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include <keyboard.h>
#include <cursor.h>
#include <mouse.h>

#include "dat.h"
#include "fncs.h"

enum {
	RESIZEC,
	KEYC,
	REDRAW,
	NONE
};

Mousectl 	*mctl;
Keyboardctl *kctl;
Channel		*ctl, *lout, *loadc;
Channel		*vctl, *vlevel;
Channel		*clickin, *clickreset;
int			decpid;
int 		showlists;

int mflag, sflag, pflag, rflag, fflag;

Image *black;
Image *red;
Image *background;
Image *listbackground;

int
cleanup(void*,char*)
{
	postnote(PNGROUP, decpid, "kill");
	closedisplay(display);
	closemouse(mctl);
	closekeyboard(kctl);
	return 0;
}

void
quit(char *err)
{
	cleanup(nil, nil);
	threadexitsall(err);
}

void
eresized(int isnew)
{
	int level;
	Lib lib;
	Point p;
	if(isnew && getwindow(display, Refnone) < 0)
		quit("eresized: Can't reattach to window");

	p = screen->r.min;
	send(clickreset, nil);
	draw(screen, screen->r, background, nil, ZP);
	recv(lout, &lib);
	if(showlists) {
		drawlists(p, black, listbackground, clickin);
		p.x+=256;
	}
	drawlibrary(&lib, p, black, red, clickin);
	recv(vlevel, &level);
	drawvolume(level, black);
	flushimage(display, Refnone);
}

void
handleaction(Rune kbd)
{
	enum volmsg vmsg;
	enum cmsg msg;
	char buf[512] = {0};
	switch(kbd){
		case Kbs:
		case Kdel:
			postnote(PNGROUP, decpid, "kill");
			quit(nil);
			return;
		case 'w':
			break;
		case 's':
			showlists = !showlists;
			break;
		case 'p':
			msg = PAUSE;
			send(ctl, &msg);
			return;
		case 'l':
			msg = START;
			send(ctl, &msg);
			return;
		case 'n':
			msg = NEXT;
			send(ctl, &msg);
			break;
		case 'm':
			msg = PREV;
			send(ctl, &msg);
			break;
		case '9':
			vmsg = DOWN;
			send(vctl, &vmsg);
			break;
		case '0':
			vmsg = UP;
			send(vctl, &vmsg);
			break;
		case 'd':
			msg = DUMP;
			send(ctl, &msg);
			/* lib proc is waiting for us to give it a name */
			enter("name?", buf, sizeof buf, mctl, kctl, nil);
			sendp(loadc, buf);
			break;
		case 'o':
			enter("Playlist?", buf, sizeof buf, mctl, kctl, nil);
			sendp(loadc, buf);
			break;
	}
	eresized(0);
}

void
usage(void)
{
	fprint(2, "Usage: -mspr %s file", argv0);
	sysfatal("usage");
}

void
threadmain(int argc, char *argv[])
{
	Rune kbd;
	Channel *clickout;
	int resize[2];
	ctl = vctl = vlevel = nil;
	mflag = sflag = pflag = rflag = fflag = 0;
	showlists = 0;

	/*
	 * This shouldn't need to be buffered,
	 * but for some reason it likes to still be
	 * blocked by the time the libproc is asked
	 * to produce the Lib struct. TODO: Investigate.
	 */
	Channel *redraw = chancreate(1, 1);

	ARGBEGIN{
	case 'm': mflag++; break;
	case 's': sflag++; break;
	case 'p': pflag++; break;
	case 'r': rflag++; break;
	case 'f': fflag++; break;
	default: usage();
	}ARGEND

	if(mflag+sflag+pflag+rflag+fflag < 1){
		fprint(2, "Please specify a playlist flag(m, s, r, f, or p)\n");
		threadexits(nil);
	}

	threadnotify(cleanup, 1);

	if(argc != 1)
		usage();

	if(initdraw(nil, nil, "mpl") < 0)
		sysfatal("%s: Failed to init screen %r", argv0);
	if((mctl = initmouse(nil, screen)) == nil)
		sysfatal("initmouse: %r");
	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);
	loadc = chancreate(sizeof(char*), 0);
	spawnlib(ctl, lout, clickout, redraw, loadc, argv[0]);

	vctl = chancreate(sizeof(enum volmsg), 0);
	vlevel = chancreate(sizeof(int), 0);
	spawnvol(vctl, vlevel);

	red = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DBlue);
	black = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DBlack);
	listbackground = allocimagemix(display, DPalebluegreen, DPalegreen);
	background = allocimagemix(display, DYellow, DPalegreyblue);

	eresized(0);

	Alt alts[] = {
		{mctl->resizec, resize, CHANRCV},
		{kctl->c, &kbd, CHANRCV},
		{redraw, nil, CHANRCV},
		{nil, nil, CHANEND},
	};

	for(;;){
		switch(alt(alts)){
			case KEYC:
				handleaction(kbd);
				break;
			case RESIZEC:
				eresized(1);
				break;
			case REDRAW:
				eresized(0);
				break;
		}
	}
}