-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontend.c
318 lines (256 loc) · 8.93 KB
/
frontend.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
/* frontend.c all of the code for the frontends
*
* messy crap resides here.
*/
#include "bnxc.h"
#include "config.h"
#ifdef FRONTEND_NCURSES
typedef struct menu_parameters_str {
char** list;
unsigned int list_len;
char *title;
char** sections;
unsigned int num_sections;
unsigned int curr_section;
char *commands;
unsigned int entered;
unsigned int select;
unsigned int scroll;
unsigned int height;
unsigned int width;
} *menu_parameters;
static int nmenu(menu_parameters params); /* menu display and input system */
static void destroy_menu_params(menu_parameters params);
/* the menu logic for the ncurses interface, much simpler than it looks...
*/
void ninterface()
{
menu_parameters artists_menu, albums_menu, tracks_menu;
char *sections[] = {"Artists", "Albums", "Tracks" }; /* Section titles, used in the menu */
artist art;
album alb;
track song;
list np;
int art_choice, alb_choice, trak_choice;
/* ---===[ ARTISTS MENU ]===--- */
artists_menu = (menu_parameters)malloc(sizeof(struct menu_parameters_str));
artists_menu->list = get_artist_list();
artists_menu->list_len = num_artists;
artists_menu->title = "Artists:";
artists_menu->sections = sections;
artists_menu->num_sections = 3;
artists_menu->curr_section = 1;
artists_menu->commands = COMMANDS;
artists_menu->select = 0;
artists_menu->scroll = 0;
artists_menu->height = LINES;
artists_menu->width = COLS;
while(1) {
art_choice = nmenu(artists_menu); /* draw artist menu */
artists_menu->select = art_choice;
if(artists_menu->entered == QUIT_BACK) /* exit artist menu (and function) */
break;
else if(artists_menu->entered == APPEND_PLAYLIST || artists_menu->entered == REPLACE_PLAYLIST) { /* play an artist (XMMS2) */
if(artists_menu->entered == REPLACE_PLAYLIST) {
player_stop();
player_clear_playlist();
}
art = ((artist)(lookup(artists_menu->list[art_choice])->contents));
player_add_artist(art);
player_play();
} else if(artists_menu->entered == TOGGLE_PLAY_PAUSE) {
player_toggle();
} else if(artists_menu->entered == NOWPLAYING) {
now_playing();
} else if(artists_menu->entered == NEXT) {
player_next();
} else if(artists_menu->entered == PREVIOUS) {
player_previous();
}
/* ---===[ ALBUMS MENU ]===--- */
else if(artists_menu->entered == '\n') { /* enter this menu */
art = ((artist)(lookup(artists_menu->list[art_choice])->contents));
albums_menu = (menu_parameters)malloc(sizeof(struct menu_parameters_str));
albums_menu->list = get_album_list(art);
albums_menu->list_len = art->num_albums;
albums_menu->title = art->name;
albums_menu->sections = sections;
albums_menu->num_sections = 3;
albums_menu->curr_section = 2;
albums_menu->commands = COMMANDS;
albums_menu->select = 0;
albums_menu->scroll = 0;
albums_menu->height = LINES;
albums_menu->width = COLS;
while(1) {
alb_choice = nmenu(albums_menu); /* draw album menu */
if(albums_menu->entered == KEY_LEFT || albums_menu->entered == QUIT_BACK) /* exit albums menu */
break;
else if(albums_menu->entered == APPEND_PLAYLIST || albums_menu->entered == REPLACE_PLAYLIST) { /* play an album (XMMS2) */
if(albums_menu->entered == REPLACE_PLAYLIST) {
player_stop();
player_clear_playlist();
}
for(np = art->albums; np != NULL; np = np->next)
if(strcmp(albums_menu->list[alb_choice], np->name) == 0)
break;
alb = (album)np->contents;
player_add_album(alb);
player_play();
} else if(albums_menu->entered == TOGGLE_PLAY_PAUSE) {
player_toggle();
} else if(albums_menu->entered == NOWPLAYING) {
now_playing();
} else if(albums_menu->entered == NEXT) {
player_next();
} else if(albums_menu->entered == PREVIOUS) {
player_previous();
}
/* ---===[ TRACKS MENU ]===--- */
else if(albums_menu->entered == '\n') { /* enter this menu */
for(np = art->albums; np != NULL; np = np->next)
if(strcmp(albums_menu->list[alb_choice], np->name) == 0)
break;
alb = (album)np->contents;
tracks_menu = (menu_parameters)malloc(sizeof(struct menu_parameters_str));
tracks_menu->list = get_track_list(alb);
tracks_menu->list_len = alb->num_songs;
tracks_menu->title = alb->name;
tracks_menu->sections = sections;
tracks_menu->num_sections = 3;
tracks_menu->curr_section = 3;
tracks_menu->commands = COMMANDS;
tracks_menu->select = 0;
tracks_menu->scroll = 0;
tracks_menu->height = LINES;
tracks_menu->width = COLS;
while(1) {
trak_choice = nmenu(tracks_menu); /* draw track menu */
/* play a track (XMMS2) */
if(tracks_menu->entered == APPEND_PLAYLIST || tracks_menu->entered == REPLACE_PLAYLIST) { /* play a track (XMMS2) */
if(tracks_menu->entered == REPLACE_PLAYLIST) {
player_stop();
player_clear_playlist();
}
for(song = alb->songs; song != NULL; song = song->next)
if(strcmp(tracks_menu->list[trak_choice], song->name) == 0)
break;
player_add_track(song);
player_play();
} else if(tracks_menu->entered == TOGGLE_PLAY_PAUSE) {
player_toggle();
} else if(tracks_menu->entered == NOWPLAYING) {
now_playing();
} else if(tracks_menu->entered == KEY_LEFT || tracks_menu->entered == QUIT_BACK) { /* exit tracks menu */
break;
} else if(tracks_menu->entered == NEXT) {
player_next();
} else if(tracks_menu->entered == PREVIOUS) {
player_previous();
}
} /* drop out of tracks here */
destroy_menu_params(tracks_menu);
}
} /* drop out of albums here */
destroy_menu_params(albums_menu);
}
} /* drop out of artists here */
destroy_menu_params(artists_menu);
return;
}
/* menu drawing routine for the ncurses version, designed to be relatively re-usable.
* breif explaination of arguments follows:
*/
static int nmenu(menu_parameters params)
{
unsigned int list_height;
unsigned int key=0;
unsigned int i;
void *dumb = NULL; /* needed for mvchgat, (argument reserved for future expansion...) */
WINDOW *menu_win;
params->height = LINES;
params->width = COLS;
if(params->height-1 <= params->list_len)
list_height = params->height-1;
else
list_height = params->list_len;
menu_win = newwin(params->height, params->width, 0, 0); /* create the ncurses window */
while(1) {
/* =====DRAW THE MENU===== */
werase(menu_win);
mvwprintw(menu_win, 0, params->curr_section, "%s", params->title);
mvwchgat(menu_win, 0, 0, COLS, COLOR_PAIR(3)|A_BOLD, 3, dumb);
for(i=0; i < list_height; i++)
mvwprintw(menu_win, 1+i, params->curr_section+1, "%s", params->list[i+params->scroll]);
for(i=params->curr_section; i<(unsigned)COLS; i++)
mvwchgat(menu_win, params->select-params->scroll+1, i, 1, COLOR_PAIR(1)|A_BOLD, 1, dumb);
for(i=0; i<params->curr_section; i++) {
unsigned int y = 0;
int offset = (params->height / 2) - (strlen(params->sections[i]) / 2);
for(y=0; y<strlen(params->sections[i]); y++)
mvwprintw(menu_win, y+offset, i, "%c", params->sections[i][y]);
}
for(i=0; i<params->curr_section; i++) {
int y;
for(y=0; y<LINES; y++) {
if(i == params->curr_section-1)
mvwchgat(menu_win, y, i, 1, COLOR_PAIR(3)|A_BOLD, 3, dumb);
else
mvwchgat(menu_win, y, i, 1, COLOR_PAIR(2), 2, dumb);
}
}
for(i=params->curr_section; i<params->num_sections; i++) {
unsigned int y = 0;
int offset = (params->height / 2) - (strlen(params->sections[i]) / 2);
for(y=0; y<strlen(params->sections[i]); y++)
mvwprintw(menu_win, y+offset, COLS-(params->num_sections-i), "%c", params->sections[i][y]);
}
for(i=params->curr_section; i<params->num_sections; i++) {
int y;
for(y=0; y<LINES; y++)
mvwchgat(menu_win, y, COLS-(params->num_sections-i), 1, COLOR_PAIR(2), 2, dumb);
}
refresh();
wrefresh(menu_win);
/* =====DONE DRAWING MENU===== */
/* =====HANDLE INPUT===== */
key = getch();
switch(key) {
case QUIT_BACK:
params->entered = QUIT_BACK; /* q *always* quits, not a custom command */
return 0;
case KEY_UP: case 'k': /* move selection up */
if(params->select > 0)
params->select--;
if(params->select < params->scroll)
params->scroll--;
break;
case KEY_DOWN: case 'j': /* move selection down */
if(params->select < params->list_len-1) {
params->select++;
if(params->select > params->scroll+params->height-2 && params->scroll+params->height-1 < params->list_len)
params->scroll++;
}
break;
case '\n': case KEY_RIGHT: case 'l': /* select/chose something */
params->entered = '\n';
return params->select;
break;
case KEY_LEFT: case 'h': /* go back */
params->entered = KEY_LEFT;
return params->select;
break;
}
/* see if we hit a custom command */
if(strchr(params->commands, key) != NULL) {
params->entered = key;
return params->select;
}
}
}
static void destroy_menu_params(menu_parameters params)
{
free(params->list);
free(params);
}
#endif