-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.c
471 lines (406 loc) · 9.82 KB
/
draw.c
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* See LICENSE file for copyright and license details. */
#include "draw.h"
static void drawheader(struct info *);
static void drawbody(struct info *);
static void drawstart(struct info *);
static void drawsearch(struct info *);
static void drawsearching(void);
static void drawselect(struct info *);
static void drawplay(struct info *);
static void drawfooter(struct info *);
static void drawbodyfill(int);
static int nlprintw(int, int, int *, const char*, ...);
void
draw_error(char *msg)
{
int i;
if (LINES < 6)
return;
/* Clear footer line */
for (i = 1; i < COLS-1; i++)
(void)mvprintw(LINES-2, i, " ");
(void)mvprintw(LINES-2, 2, "ERROR: %.*s", COLS-11, msg);
(void)refresh();
(void)sleep(3); /* Display error for 3 seconds */
}
void
draw_exit(void)
{
(void)endwin();
}
void
draw_init(void)
{
int ret;
if (initscr() == NULL)
goto error;
/* Set up ncurses environment */
/* Hide cursor */
ret = curs_set(0);
if (ret == ERR)
goto error;
/* Set half-blocking mode and time */
ret = halfdelay(HALFDELAY);
if (ret == ERR)
goto error;
/* Make function keys available */
ret = keypad(stdscr, true);
if (ret == ERR)
goto error;
/* Disable echoing keys on screen */
ret = noecho();
if (ret == ERR)
goto error;
/* Prevent enter from creating a newline */
ret = nonl();
if (ret == ERR)
goto error;
/* Set short timeout for ESC */
ret = set_escdelay(DELAYESC);
if (ret == ERR)
goto error;
/* Clear the screen */
(void)clear();
return;
error:
draw_exit();
errx(1, "Failed to initialize ncurses.");
}
void
draw_redraw(struct info *data)
{
(void)erase();
drawheader(data);
drawbody(data);
drawfooter(data);
(void)refresh();
}
static void
drawheader(struct info *data)
{
char *mix, *track;
int index, i;
size_t len;
if (data == NULL)
return;
if (LINES < 4)
return;
/* Get mix and track name */
mix = NULL;
track = NULL;
if (data->m) {
if (data->m->name)
mix = data->m->name;
index = data->m->track_count;
if (index > 0) {
len = strlen(data->m->track[index-1]->name) +
strlen(" - ") +
strlen(data->m->track[index-1]->performer) + 1;
track = malloc(len * sizeof(char));
if (track == NULL)
err(1, NULL);
track[0] = '\0';
(void)snprintf(track, len, "%s - %s",
data->m->track[index-1]->performer,
data->m->track[index-1]->name);
}
}
/* First line */
(void)mvprintw(0, 0, "%lc", L'\u250C');
for (i = 1; i < COLS-1; i++)
(void)printw("%lc", L'\u2500');
(void)mvaddstr(0, 3, " 8p ");
(void)mvprintw(0, COLS-1, "%lc", L'\u2510');
/* Second line */
(void)mvprintw(1, 0, "%lc", L'\u2502');
(void)mvprintw(1, COLS-1, "%lc", L'\u2502');
if (mix)
(void)mvprintw(1, 2, "Mix: %.*s", COLS-4-7, mix);
else
(void)mvprintw(1, 2, "Mix:");
/* Third line */
(void)mvprintw(2, 0, "%lc", L'\u2502');
(void)mvprintw(2, COLS-1, "%lc", L'\u2502');
if (track)
(void)mvprintw(2, 2, "Track: %.*s", COLS-4-7, track);
else
(void)mvprintw(2, 2, "Track:");
/* Fourth line */
(void)mvprintw(3, 0, "%lc", L'\u2514');
for (i = 1; i < COLS-1; i++)
(void)printw("%lc", L'\u2500');
(void)mvprintw(3, COLS-1, "%lc", L'\u2518');
if (track)
free(track);
}
static void
drawbody(struct info *data)
{
if (data == NULL)
return;
/* Header and footer get priority */
if (LINES <= 6)
return;
(void)mvprintw(3, 0, "%lc", L'\u251C');
(void)mvprintw(3, COLS-1, "%lc", L'\u2524');
switch (data->state) {
case START: drawstart(data); break;
case SEARCH: drawsearch(data); break;
case SEARCHING: drawsearching(); break;
case SELECT: drawselect(data); break;
case PLAY: drawplay(data); break;
default: break;
}
}
static void
drawbodyfill(int ln)
{
for (; ln < LINES-3; ln++) {
(void)mvprintw(ln, 0, "%lc", L'\u2502');
(void)mvprintw(ln, COLS-1, "%lc", L'\u2502');
}
}
static void
drawstart(struct info *data)
{
int y;
int scroll;
scroll = data->scroll;
y = nlprintw(4, FALSE, &scroll, "Welcome to 8p.");
y = nlprintw(y, FALSE, &scroll,
"Press \"s\" to start searching for 8tracks.com mixes.");
drawbodyfill(y);
}
static void
drawsearch(struct info *data)
{
char *txt[] = {
"Search Help",
"-----------",
"\n",
"Search by using Smart ID, for example:",
"\n",
"Smart ID Result",
"all:popular all mixes",
"tags:chill mixes tagged \"chill\"",
"tags:chill+hip_hop:recent new mixes tagged \"chill\" & "
"\"hip hop\"",
"artist:Radiohead mixes including songs by Radiohead",
"keyword:ocarina mixes including the text \"ocarina\"",
"dj:1 mixes published by the user with id=1",
"liked:1 mixes liked by the user with id=1",
"similar:14 mixes similar to mix with id=14",
"\n",
"Note: no spaces allowed."};
int i;
int y;
int scroll;
scroll = data->scroll;
y = nlprintw(4, FALSE, &scroll, "%s", txt[0]);
for (i = 1; i < (int)(sizeof(txt)/sizeof(txt[0])); i++)
y = nlprintw(y, FALSE, &scroll, "%s", txt[i]);
drawbodyfill(y);
}
static void
drawsearching(void)
{
int scroll, y;
scroll = 0;
y = nlprintw(4, FALSE, &scroll, "Searching...");
drawbodyfill(y);
}
static void
drawselect(struct info *data)
{
int scroll, y;
int i, j;
scroll = data->scroll;
if (data->mlist == NULL)
goto error;
if (data->mlist_size == 0)
goto error;
y = 4;
for (j = 0, i = data->select_pos; j < (int)data->mlist_size; j++, i++) {
i = mod(i, data->mlist_size);
if (data->mlist[i] == NULL) {
y = nlprintw(y, FALSE, &scroll, "-. Error");
continue;
}
if (i == data->select_pos) {
y = nlprintw(y, TRUE, &scroll, "%d. %s", i+1,
data->mlist[i]->name);
y = nlprintw(y, FALSE, &scroll, "");
} else {
y = nlprintw(y, FALSE, &scroll, "%d. %s", i+1,
data->mlist[i]->name);
}
y = nlprintw(y, FALSE, &scroll, "\nDescription:\n%s",
data->mlist[i]->description);
y = nlprintw(y, FALSE, &scroll, "Tags: %s",
data->mlist[i]->tags);
y = nlprintw(y, FALSE, &scroll, "Number of plays: %d",
data->mlist[i]->plays_count);
y = nlprintw(y, FALSE, &scroll, "Number of likes: %d",
data->mlist[i]->likes_count);
y = nlprintw(y, FALSE, &scroll, "\n---\n\n");
}
drawbodyfill(y);
return;
error:
y = nlprintw(4, FALSE, &scroll, "Search returned no results.");
drawbodyfill(y);
}
static void
drawplay(struct info *data)
{
int scroll, y, i;
scroll = data->scroll;
y = nlprintw(4, FALSE, &scroll, "Playlist");
y = nlprintw(y, FALSE, &scroll, "--------");
for (i = 0; i < data->m->track_count; i++) {
y = nlprintw(y, FALSE, &scroll, "%d. %s - %s", i+1,
data->m->track[i]->performer,
data->m->track[i]->name);
}
drawbodyfill(y);
}
static void
drawfooter(struct info *data)
{
char playing[] = "q Quit s Search p Play/Pause n Next track"
" N Next mix";
char search[] = "ESC Exit | Search: ";
char select[] = "ESC Exit Enter Select";
char start[] = "q Quit s Search";
struct search_node *it;
int cp, i;
if (data == NULL)
return;
if (LINES < 6)
return;
(void)mvprintw(LINES-3, 0, "%lc", L'\u251C');
for (i = 1; i < COLS-1; i++)
(void)printw("%lc", L'\u2500');
(void)printw("%lc", L'\u2524');
(void)mvprintw(LINES-2, 0, "%lc", L'\u2502');
(void)mvprintw(LINES-2, COLS-1, "%lc", L'\u2502');
(void)mvprintw(LINES-1, 0, "%lc", L'\u2514');
for (i = 1; i < COLS-1; i++)
(void)printw("%lc", L'\u2500');
(void)mvprintw(LINES-1, COLS-1, "%lc", L'\u2518');
switch (data->state) {
case PLAY:
(void)mvprintw(LINES-2, 2, "%.*s", COLS-4, playing);
(void)curs_set(0);
break;
case START:
(void)mvprintw(LINES-2, 2, "%.*s", COLS-4, start);
(void)curs_set(0);
break;
case SEARCH:
(void)mvprintw(LINES-2, 2, "%.*s", COLS-4, search);
for (it = data->slist_head; it != NULL; it = it->next)
(void)printw("%lc", it->c);
(void)curs_set(1);
cp = strlen(search) + 2;
it = data->slist_head;
for (i = 0; i < data->cursor_pos; i++, it = it->next)
cp += wcwidth(it->c);
(void)move(LINES-2, cp);
break;
case SELECT:
(void)mvprintw(LINES-2, 2, "%.*s", COLS-4, select);
(void)curs_set(0);
break;
default:
(void)curs_set(0);
break;
}
}
/* Print a line only if it is visible and wrap if the line
* length is too long to fit.
* NOTE: only usable for drawing in the body section.
*/
static int
nlprintw(int ln, int sel, int *scroll, const char *fmt, ...)
{
va_list ap;
char *str;
wchar_t *wstr;
int errn, i, slen;
size_t wlen;
/* Check if start line is visible */
if (ln > LINES - 4) {
return ln;
}
str = NULL;
wstr = NULL;
/* Get string length */
va_start(ap, fmt);
slen = vsnprintf(NULL, 0, fmt, ap);
if (slen < 0)
return ln;
slen++;
va_end(ap);
/* Set string */
str = malloc(slen * sizeof(char));
if (str == NULL)
err(1, NULL);
va_start(ap, fmt);
errn = vsnprintf(str, slen, fmt, ap);
if (errn < 0)
goto error;
va_end(ap);
/* Convert string to a wide-character string */
wstr = malloc(slen * sizeof(wchar_t));
if (wstr == NULL)
err(1, NULL);
wlen = mbstowcs(wstr, str, slen);
if (wlen == (size_t)-1)
goto error;
/* Apply strict width rules to the string */
errn = wwrap(&wstr, COLS-4);
if (errn == ERROR)
goto error;
/* Print string */
i = 0;
if (*scroll > 0) {
while(wstr[i] != L'\0' && *scroll != 0) {
if (wstr[i] == L'\n')
(*scroll)--;
i++;
}
if (*scroll > 0) {
(*scroll)--;
goto error;
}
}
while (ln < LINES - 3 && wstr[i] != L'\0') {
(void)mvprintw(ln, 0, "%lc", L'\u2502');
(void)move(ln, 2);
if (sel == TRUE)
(void)attron(A_REVERSE);
while(wstr[i] != L'\0' && wstr[i] != L'\n') {
(void)printw("%lc", wstr[i]);
i++;
}
if (sel == TRUE)
(void)attroff(A_REVERSE);
if (wstr[i] == L'\n') {
(void)addch(wstr[i]);
i++;
}
(void)mvprintw(ln, COLS-1, "%lc", L'\u2502');
ln++;
}
/* Cleanup */
free(str);
free(wstr);
return ln;
error:
if (str)
free(str);
if (wstr)
free(wstr);
return ln;
}