2d61122fd91cfe08d988bab76080f93a0124a0c6
1 /*
2 * $Id: screen.c,v 1.10 2004/03/17 14:50:12 kalle Exp $
3 *
4 */
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <stdarg.h>
9 #include <string.h>
10 #include <time.h>
11 #include <glib.h>
12 #include <ncurses.h>
14 #include "libmpdclient.h"
15 #include "mpc.h"
16 #include "command.h"
17 #include "screen.h"
18 #include "screen_play.h"
19 #include "screen_file.h"
20 #include "screen_help.h"
21 #include "screen_search.h"
23 #define STATUS_MESSAGE_TIMEOUT 3
24 #define STATUS_LINE_MAX_SIZE 512
26 static screen_t *screen = NULL;
28 static void
29 switch_screen_mode(screen_mode_t new_mode, mpd_client_t *c)
30 {
31 if( new_mode == screen->mode )
32 return;
34 switch(screen->mode)
35 {
36 case SCREEN_PLAY_WINDOW:
37 play_close(screen, c);
38 break;
39 case SCREEN_FILE_WINDOW:
40 file_close(screen, c);
41 break;
42 case SCREEN_SEARCH_WINDOW:
43 search_close(screen, c);
44 break;
45 case SCREEN_HELP_WINDOW:
46 help_close(screen, c);
47 break;
48 }
50 screen->mode = new_mode;
51 screen->painted = 0;
53 switch(screen->mode)
54 {
55 case SCREEN_PLAY_WINDOW:
56 play_open(screen, c);
57 break;
58 case SCREEN_FILE_WINDOW:
59 file_open(screen, c);
60 break;
61 case SCREEN_SEARCH_WINDOW:
62 search_open(screen, c);
63 break;
64 case SCREEN_HELP_WINDOW:
65 help_open(screen, c);
66 break;
67 }
68 }
70 static void
71 paint_top_window(char *header, int volume, int clear)
72 {
73 static int prev_volume = -1;
74 WINDOW *w = screen->top_window.w;
76 if(clear)
77 {
78 wclear(w);
79 }
81 if(prev_volume!=volume || clear)
82 {
83 char buf[12];
85 wattron(w, A_BOLD);
86 mvwaddstr(w, 0, 0, header );
87 wattroff(w, A_BOLD);
88 if( volume==MPD_STATUS_NO_VOLUME )
89 {
90 snprintf(buf, 12, "Volume n/a ");
91 }
92 else
93 {
94 snprintf(buf, 12, "Volume %3d%%", volume);
95 }
96 mvwaddstr(w, 0, screen->top_window.cols-12, buf);
98 mvwhline(w, 1, 0, ACS_HLINE, screen->top_window.cols);
100 wrefresh(w);
101 }
102 }
104 static void
105 paint_progress_window(mpd_client_t *c)
106 {
107 double p;
108 int width;
110 if( c->status==NULL || !IS_PLAYING(c->status->state) )
111 {
112 mvwhline(screen->progress_window.w, 0, 0, ACS_HLINE,
113 screen->progress_window.cols);
114 wrefresh(screen->progress_window.w);
115 return;
116 }
118 p = ((double) c->status->elapsedTime) / ((double) c->status->totalTime);
120 width = (int) (p * (double) screen->progress_window.cols);
122 mvwhline(screen->progress_window.w,
123 0, 0,
124 ACS_HLINE,
125 screen->progress_window.cols);
127 whline(screen->progress_window.w, '=', width-1);
129 mvwaddch(screen->progress_window.w, 0, width-1, 'O');
130 wrefresh(screen->progress_window.w);
131 }
133 static void
134 paint_status_window(mpd_client_t *c)
135 {
136 WINDOW *w = screen->status_window.w;
137 mpd_Status *status = c->status;
138 mpd_Song *song = c->song;
139 int x = 0;
141 if( time(NULL) - screen->status_timestamp <= STATUS_MESSAGE_TIMEOUT )
142 return;
144 wmove(w, 0, 0);
145 wclrtoeol(w);
147 switch(status->state)
148 {
149 case MPD_STATUS_STATE_STOP:
150 wattron(w, A_BOLD);
151 waddstr(w, "Stopped! ");
152 wattroff(w, A_BOLD);
153 break;
154 case MPD_STATUS_STATE_PLAY:
155 waddstr(w, "Playing:");
156 break;
157 case MPD_STATUS_STATE_PAUSE:
158 wattron(w, A_BOLD);
159 waddstr(w, "Paused:");
160 wattroff(w, A_BOLD);
161 break;
162 default:
163 waddstr(w, "Warning: Music Player Daemon in unknown state!");
164 break;
165 }
166 x += 10;
168 if( IS_PLAYING(status->state) && song )
169 {
170 mvwaddstr(w, 0, x, mpc_get_song_name(song));
171 }
174 /* time */
175 if( IS_PLAYING(status->state) )
176 {
177 x = screen->status_window.cols - strlen(screen->buf);
179 if( c->status->repeat )
180 mvwaddstr(w, 0, x-3, "<R>");
181 else
182 mvwaddstr(w, 0, x-3, " ");
184 snprintf(screen->buf, screen->buf_size,
185 " [%i:%02i/%i:%02i] ",
186 status->elapsedTime/60, status->elapsedTime%60,
187 status->totalTime/60, status->totalTime%60 );
188 mvwaddstr(w, 0, x, screen->buf);
190 }
193 wrefresh(w);
194 }
198 int
199 screen_exit(void)
200 {
201 endwin();
202 if( screen )
203 {
204 screen->playlist = list_window_free(screen->playlist);
205 screen->filelist = list_window_free(screen->filelist);
206 screen->helplist = list_window_free(screen->helplist);
207 free(screen->buf);
208 free(screen->findbuf);
209 free(screen);
210 screen = NULL;
211 }
212 return 0;
213 }
215 void
216 screen_resized(int sig)
217 {
218 screen_exit();
219 if( COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS )
220 {
221 fprintf(stderr, "Error: Screen to small!\n");
222 exit(EXIT_FAILURE);
223 }
224 screen_init();
225 }
227 void
228 screen_status_message(mpd_client_t *c, char *msg)
229 {
230 WINDOW *w = screen->status_window.w;
232 wmove(w, 0, 0);
233 wclrtoeol(w);
234 wattron(w, A_BOLD);
235 waddstr(w, msg);
236 wattroff(w, A_BOLD);
237 wrefresh(w);
238 screen->status_timestamp = time(NULL);
239 }
241 void
242 screen_status_printf(mpd_client_t *c, char *format, ...)
243 {
244 char buffer[STATUS_LINE_MAX_SIZE];
245 va_list ap;
247 va_start(ap,format);
248 vsnprintf(buffer,sizeof(buffer),format,ap);
249 va_end(ap);
250 screen_status_message(c, buffer);
251 }
253 int
254 screen_init(void)
255 {
256 /* initialize the curses library */
257 initscr();
258 start_color();
259 use_default_colors();
260 /* tell curses not to do NL->CR/NL on output */
261 nonl();
262 /* take input chars one at a time, no wait for \n */
263 cbreak();
264 /* don't echo input */
265 noecho();
266 /* set cursor invisible */
267 curs_set(0);
268 /* return from getch() without blocking */
269 // nodelay(stdscr, TRUE);
270 keypad(stdscr, TRUE);
271 timeout(100); /*void wtimeout(WINDOW *win, int delay);*/
274 if( COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS )
275 {
276 fprintf(stderr, "Error: Screen to small!\n");
277 exit(EXIT_FAILURE);
278 }
280 screen = malloc(sizeof(screen_t));
281 memset(screen, 0, sizeof(screen_t));
282 screen->mode = SCREEN_PLAY_WINDOW;
283 screen->cols = COLS;
284 screen->rows = LINES;
285 screen->buf = malloc(screen->cols);
286 screen->buf_size = screen->cols;
287 screen->findbuf = NULL;
288 screen->painted = 0;
290 /* create top window */
291 screen->top_window.rows = 2;
292 screen->top_window.cols = screen->cols;
293 screen->top_window.w = newwin(screen->top_window.rows,
294 screen->top_window.cols,
295 0, 0);
296 leaveok(screen->top_window.w, TRUE);
297 keypad(screen->top_window.w, TRUE);
299 /* create main window */
300 screen->main_window.rows = screen->rows-4;
301 screen->main_window.cols = screen->cols;
302 screen->main_window.w = newwin(screen->main_window.rows,
303 screen->main_window.cols,
304 2,
305 0);
306 screen->playlist = list_window_init( screen->main_window.w,
307 screen->main_window.cols,
308 screen->main_window.rows );
309 screen->filelist = list_window_init( screen->main_window.w,
310 screen->main_window.cols,
311 screen->main_window.rows );
312 screen->helplist = list_window_init( screen->main_window.w,
313 screen->main_window.cols,
314 screen->main_window.rows );
315 leaveok(screen->main_window.w, TRUE);
316 keypad(screen->main_window.w, TRUE);
318 /* create progress window */
319 screen->progress_window.rows = 1;
320 screen->progress_window.cols = screen->cols;
321 screen->progress_window.w = newwin(screen->progress_window.rows,
322 screen->progress_window.cols,
323 screen->rows-2,
324 0);
325 leaveok(screen->progress_window.w, TRUE);
327 /* create status window */
328 screen->status_window.rows = 1;
329 screen->status_window.cols = screen->cols;
330 screen->status_window.w = newwin(screen->status_window.rows,
331 screen->status_window.cols,
332 screen->rows-1,
333 0);
334 leaveok(screen->status_window.w, FALSE);
335 keypad(screen->status_window.w, TRUE);
337 return 0;
338 }
340 void
341 screen_paint(mpd_client_t *c)
342 {
343 switch(screen->mode)
344 {
345 case SCREEN_PLAY_WINDOW:
346 paint_top_window(TOP_HEADER_PLAY, c->status->volume, 1);
347 play_paint(screen, c);
348 break;
349 case SCREEN_FILE_WINDOW:
350 paint_top_window(file_get_header(c), c->status->volume, 1);
351 file_paint(screen, c);
352 break;
353 case SCREEN_SEARCH_WINDOW:
354 paint_top_window(TOP_HEADER_SEARCH, c->status->volume, 1);
355 search_paint(screen, c);
356 break;
357 case SCREEN_HELP_WINDOW:
358 paint_top_window(TOP_HEADER_PLAY, c->status->volume, 1);
359 help_paint(screen, c);
360 break;
361 }
363 paint_progress_window(c);
364 paint_status_window(c);
365 screen->painted = 1;
366 }
368 void
369 screen_update(mpd_client_t *c)
370 {
371 if( !screen->painted )
372 return screen_paint(c);
374 switch(screen->mode)
375 {
376 case SCREEN_PLAY_WINDOW:
377 paint_top_window(TOP_HEADER_PLAY, c->status->volume, 0);
378 play_update(screen, c);
379 break;
380 case SCREEN_FILE_WINDOW:
381 paint_top_window(file_get_header(c), c->status->volume, 0);
382 file_update(screen, c);
383 break;
384 case SCREEN_SEARCH_WINDOW:
385 paint_top_window(TOP_HEADER_SEARCH, c->status->volume, 0);
386 search_update(screen, c);
387 break;
388 case SCREEN_HELP_WINDOW:
389 paint_top_window(TOP_HEADER_HELP, c->status->volume, 0);
390 help_update(screen, c);
391 break;
392 }
393 paint_progress_window(c);
394 paint_status_window(c);
395 }
397 void
398 screen_cmd(mpd_client_t *c, command_t cmd)
399 {
400 int n;
401 char buf[256];
402 screen_mode_t new_mode = screen->mode;
404 switch(screen->mode)
405 {
406 case SCREEN_PLAY_WINDOW:
407 if( play_cmd(screen, c, cmd) )
408 return;
409 break;
410 case SCREEN_FILE_WINDOW:
411 if( file_cmd(screen, c, cmd) )
412 return;
413 break;
414 case SCREEN_SEARCH_WINDOW:
415 if( search_cmd(screen, c, cmd) )
416 return;
417 break;
418 case SCREEN_HELP_WINDOW:
419 if( help_cmd(screen, c, cmd) )
420 return;
421 break;
422 }
424 switch(cmd)
425 {
426 case CMD_PLAY:
427 mpd_sendPlayCommand(c->connection, screen->playlist->selected);
428 mpd_finishCommand(c->connection);
429 break;
430 case CMD_PAUSE:
431 mpd_sendPauseCommand(c->connection);
432 mpd_finishCommand(c->connection);
433 break;
434 case CMD_STOP:
435 mpd_sendStopCommand(c->connection);
436 mpd_finishCommand(c->connection);
437 break;
438 case CMD_TRACK_NEXT:
439 if( IS_PLAYING(c->status->state) )
440 {
441 mpd_sendNextCommand(c->connection);
442 mpd_finishCommand(c->connection);
443 }
444 break;
445 case CMD_TRACK_PREVIOUS:
446 if( IS_PLAYING(c->status->state) )
447 {
448 mpd_sendPrevCommand(c->connection);
449 mpd_finishCommand(c->connection);
450 }
451 break;
452 case CMD_SHUFFLE:
453 mpd_sendShuffleCommand(c->connection);
454 mpd_finishCommand(c->connection);
455 screen_status_message(c, "Shuffled playlist!");
456 break;
457 case CMD_CLEAR:
458 mpd_sendClearCommand(c->connection);
459 mpd_finishCommand(c->connection);
460 file_clear_highlights(c);
461 screen_status_message(c, "Cleared playlist!");
462 break;
463 case CMD_REPEAT:
464 n = !c->status->repeat;
465 mpd_sendRepeatCommand(c->connection, n);
466 mpd_finishCommand(c->connection);
467 snprintf(buf, 256, "Repeat is %s", n ? "On" : "Off");
468 screen_status_message(c, buf);
469 break;
470 case CMD_RANDOM:
471 n = !c->status->random;
472 mpd_sendRandomCommand(c->connection, n);
473 mpd_finishCommand(c->connection);
474 snprintf(buf, 256, "Random is %s", n ? "On" : "Off");
475 screen_status_message(c, buf);
476 break;
477 case CMD_VOLUME_UP:
478 mpd_sendSetvolCommand(c->connection, 1);
479 mpd_finishCommand(c->connection);
480 if( c->status->volume!=MPD_STATUS_NO_VOLUME )
481 {
482 snprintf(buf, 256, "Volume %d%%", c->status->volume+1);
483 screen_status_message(c, buf);
484 }
485 break;
486 case CMD_VOLUME_DOWN:
487 mpd_sendSetvolCommand(c->connection, -1);
488 mpd_finishCommand(c->connection);
489 if( c->status->volume!=MPD_STATUS_NO_VOLUME )
490 {
491 snprintf(buf, 256, "Volume %d%%", c->status->volume-1);
492 screen_status_message(c, buf);
493 }
494 break;
495 case CMD_SCREEN_PREVIOUS:
496 if( screen->mode > SCREEN_PLAY_WINDOW )
497 new_mode = screen->mode - 1;
498 else
499 new_mode = SCREEN_HELP_WINDOW-1;
500 switch_screen_mode(new_mode, c);
501 break;
502 case CMD_SCREEN_NEXT:
503 new_mode = screen->mode + 1;
504 if( new_mode >= SCREEN_HELP_WINDOW )
505 new_mode = SCREEN_PLAY_WINDOW;
506 switch_screen_mode(new_mode, c);
507 break;
508 case CMD_SCREEN_PLAY:
509 switch_screen_mode(SCREEN_PLAY_WINDOW, c);
510 break;
511 case CMD_SCREEN_FILE:
512 switch_screen_mode(SCREEN_FILE_WINDOW, c);
513 break;
514 case CMD_SCREEN_SEARCH:
515 switch_screen_mode(SCREEN_SEARCH_WINDOW, c);
516 break;
517 case CMD_SCREEN_HELP:
518 switch_screen_mode(SCREEN_HELP_WINDOW, c);
519 break;
520 case CMD_QUIT:
521 exit(EXIT_SUCCESS);
522 case CMD_LIST_FIND:
523 case CMD_LIST_FIND_NEXT:
524 case CMD_NONE:
525 case CMD_DELETE:
526 case CMD_SELECT:
527 case CMD_LIST_PREVIOUS:
528 case CMD_LIST_NEXT:
529 case CMD_LIST_FIRST:
530 case CMD_LIST_LAST:
531 case CMD_LIST_NEXT_PAGE:
532 case CMD_LIST_PREVIOUS_PAGE:
533 break;
534 }
536 }