~moody/mpl

mpl/lib.c -rw-r--r-- 4.5 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <thread.h>
#include <draw.h>

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

Channel *queuein, *queueout, *decctl;
Channel *radiochan;
Lib lib;

char*
nextsong(Lib *lib)
{
	if(lib->cursong < 0){
		lib->cur--;
		if(lib->cur < lib->start)
			lib->cur = lib->stop;
		lib->cursong = lib->cur->nsong-1;
	}
	if(lib->cursong > lib->cur->nsong-1){
		lib->cur++;
		if(lib->cur > lib->stop)
			lib->cur = lib->start;
		lib->cursong = 0;
	}
	return (lib->cur->songs+(lib->cursong))->path;
}

void
handlemsg(enum cmsg msg)
{
	switch(msg){
	case NEXT:
		lib.cursong++;
		sendp(queuein, nextsong(&lib));
		break;
	case PREV:
		lib.cursong--;
		sendp(queuein, nextsong(&lib));
		break;
	case STOP:
	case START:
	case PAUSE:
		send(decctl, &msg);
		break;	
	}
}

void
libproc(void *arg)
{
	Channel **chans = arg;
	Channel *lctl = chans[0];
	Channel *out = chans[1];
	Channel *ein = chans[2];
	Channel *redraw = chans[3];
	Channel *loadc = chans[4];
	free(chans);

	enum cmsg msg;
	Click c;

	char *radiotitle = nil;
	char *toload;

	Alt alts[] = {
		{lctl, &msg, CHANRCV},
		{queueout, nil, CHANRCV},
		{ein, &c, CHANRCV},
		{out, &lib, CHANSND},
		{radiochan, &radiotitle, CHANRCV},
		{loadc,	&toload, CHANRCV},
		{nil, nil, CHANEND},
	};
	enum{
		LMSG,
		QUEUEPOP,
		EIN,
		OUT,
		RADIOIN,
		LOADC,
	};

	for(;;){
		switch(alt(alts)){
		case LMSG:
			if(msg == DUMP){
				/* TODO: rename chan to imply this use */
				toload = recvp(loadc);
				free(lib.name);
				lib.name = strdup(toload);
				dumplib(&lib);
				break;
			}
			handlemsg(msg);
			break;
		case QUEUEPOP:
			handlemsg(NEXT);
			break;
		case EIN:
			switch(c.type){
			case CSONG:
				lib.cur = c.a;
				lib.cursong = c.songnum;
				sendp(queuein, nextsong(&lib));
				break;
			case CLIST:
				//TODO clean more
				free(lib.name);
				lib.name = strdup(c.list);
				goto Load;
			}
			break;
		case OUT:
			continue;
		case RADIOIN:
			/* Eat everything in the channel;
			 * This leaks memory,
			 * but fixes a race.
			 * TODO: Find a better fix.
			 */
			while(nbrecv(radiochan, &radiotitle))
				;
			if(lib.cur->songs->title != nil)
				free(lib.cur->songs->title);
			lib.cur->songs->title = runesmprint("%s", radiotitle);
			free(radiotitle);
			break;
		case LOADC:
			//TODO: clean more
			free(lib.name);
			lib.name = strdup(toload);
Load:
			loadlib(&lib);
			lib.stop = lib.start+(lib.nalbum-1);
			lib.cursong = 0;
			lib.cur = lib.start;
			break;
		}
		send(redraw, nil);
	}
}

void
radioproc(void *arg)
{
	char *path = arg;
	char buf[512];
	char *dot, *end;
	Biobuf *b;

	dot = strrchr(path, '/');
	if(dot == nil)
		sysfatal("readradiosong: bad song path");
	end = buf+(dot-path)+1;
	if(end - buf > sizeof buf)
		sysfatal("readradiosong: buffer too small");
	seprint(buf, end, "%s", path);
	snprint(buf, 512, "%s/playing", buf);
	free(path);
	b = Bopen(buf, OREAD);
	if(b == nil)
		sysfatal("readradiosong: Bopen %r");
	for(;;){
		path = Brdstr(b, '\n', 1);
		sendp(radiochan, path);
	}
}

void
spawnlib(Channel *ctl, Channel *out, Channel *ein, Channel *redraw, Channel *loadc, char *path)
{
	Channel **chans;
	char *name;

	queuein = queueout = decctl = nil;
	radiochan = nil;

	spawndec(&queuein, &decctl, &queueout);
	radiochan = chancreate(sizeof(char*), 1024);

	name = strrchr(path, '/');
	lib.name = strdup(name == nil ? path : name);

	extern int mflag;
	extern int sflag;
	extern int rflag;
	extern int pflag;
	extern int fflag;

	if(mflag == 1){
		chanclose(radiochan);
		lib.nalbum = parselibrary(&(lib.start), path);
		if(lib.nalbum == 0)
			sysfatal("no songs found");
		lib.stop = lib.start+(lib.nalbum-1);
	}else if(sflag == 1){
		chanclose(radiochan);
		lib.nalbum = 1;
		lib.start = emalloc(sizeof(Album));
		if(!dir2album(lib.start, path))
			sysfatal("no songs found");
		lib.stop = lib.start;
	}else if(rflag == 1){
		lib.nalbum = 1;
		lib.start = emalloc(sizeof(Album));
		lib.stop = lib.start;
		radio2album(lib.start, path);
		proccreate(radioproc, strdup(path), 8192);
	}else if(pflag == 1){
		chanclose(radiochan);
		loadlib(&lib);
		lib.stop = lib.start+(lib.nalbum-1);
	}else if(fflag == 1){
		lib.nalbum = 1;
		lib.start = emalloc(sizeof(Album));
		lib.stop = lib.start;
		file2album(lib.start, L"", path);
	}

	lib.cursong = 0;
	lib.cur = lib.start;
	if(lib.cur->nsong == 0)
		sysfatal("No songs found");

	chans = emalloc(sizeof(Channel*)*5);
	chans[0] = ctl;
	chans[1] = out;
	chans[2] = ein;
	chans[3] = redraw;
	chans[4] = loadc;

	sendp(queuein, nextsong(&lib));
	threadcreate(libproc, chans, 8192);
}