From: Bjørn Rustad Date: Wed, 30 Jan 2013 20:23:00 +0000 (+0100) Subject: Some more ncurses hacking X-Git-Url: http://git.rustad.me/?a=commitdiff_plain;ds=inline;p=spots Some more ncurses hacking --- diff --git a/spots.c b/spots.c index 5413e0d..01eb1c1 100644 --- a/spots.c +++ b/spots.c @@ -2,6 +2,7 @@ #include #include #include +#include #include @@ -25,8 +26,12 @@ pthread_cond_t notify_main_thread_cond; pthread_mutex_t input_output_mutex; pthread_cond_t input_cond; +pthread_mutex_t ui_mutex; +pthread_cond_t ui_cond; + int main_thread_notified; int show_prompt; +int show_ui; int log_out; char *cmd_line; @@ -137,10 +142,10 @@ void cb_logged_in (sp_session *session, sp_error error) exit(3); } - pthread_mutex_lock(&input_output_mutex); - show_prompt = 1; - pthread_cond_signal(&input_cond); - pthread_mutex_unlock(&input_output_mutex); + pthread_mutex_lock(&ui_mutex); + show_ui = 1; + pthread_cond_signal(&ui_cond); + pthread_mutex_unlock(&ui_mutex); } void cb_logged_out (sp_session *session) @@ -167,8 +172,6 @@ int cb_music_delivery (sp_session *session, const sp_audioformat *format, int pa_err; struct audio_q_element *aqe; - - fprintf(stderr, "music delivery\n"); aqe = malloc(sizeof(struct audio_q_element)); aqe->num_samples = num_frames; @@ -193,6 +196,62 @@ static void finish (int sig) exit(0); } +void *ui_thread_function (void *attr) +{ + WINDOW *main_win, *playlists_win; + + if ((main_win = initscr()) == NULL) { + fprintf(stderr, "Error initializing ncurses.\n"); + exit(4); + } + fprintf(stderr, "DEBUG: UI started, ncurses initialized.\n"); + + cbreak(); + noecho(); + keypad(main_win, TRUE); + + playlists_win = subwin(main_win, LINES - 2, COLS, 2, 0); + box(playlists_win, 0, 0); + + int i; + + i = 0; + pthread_mutex_lock(&ui_mutex); + while (1) { + while (!show_ui) + pthread_cond_wait(&ui_cond, &ui_mutex); + + pthread_mutex_unlock(&ui_mutex); + + mvwprintw(playlists_win, i + 2, 2, "LOLLLOL"); + + wrefresh(playlists_win); + + getch(); + + ++i; + pthread_mutex_lock(&ui_mutex); + } + + delwin(playlists_win); + delwin(main_win); + endwin(); + + finish(0); +} + +void start_ui_thread (void) +{ + int ui_thread_id; + pthread_t ui_thread; + + ui_thread_id = pthread_create( + &ui_thread, + NULL, + &ui_thread_function, + NULL); +} + int main (int argc, char *argv[]) { sp_error err; @@ -246,9 +305,11 @@ int main (int argc, char *argv[]) pthread_mutex_init(¬ify_main_thread_mutex, NULL); pthread_mutex_init(&input_output_mutex, NULL); + pthread_mutex_init(&ui_mutex, NULL); pthread_mutex_init(&audio_q_mutex, NULL); pthread_cond_init(¬ify_main_thread_cond, NULL); pthread_cond_init(&input_cond, NULL); + pthread_cond_init(&ui_cond, NULL); pthread_cond_init(&audio_q_cond, NULL); pthread_mutex_lock(¬ify_main_thread_mutex); @@ -259,6 +320,10 @@ int main (int argc, char *argv[]) show_prompt = 0; pthread_mutex_unlock(&input_output_mutex); + pthread_mutex_lock(&ui_mutex); + show_ui = 0; + pthread_mutex_unlock(&ui_mutex); + pthread_mutex_lock(&audio_q_mutex); play = 0; pthread_mutex_unlock(&audio_q_mutex); @@ -279,12 +344,15 @@ int main (int argc, char *argv[]) err = sp_session_login(session, username, password, 0, NULL); if (err != SP_ERROR_OK) { - fprintf(stderr, "Could not log in: %s.", + fprintf(stderr, "Could not log in: %s.\n", sp_error_message(err)); - return 2; + exit(2); } - start_input_thread(); + signal(SIGINT, finish); + + //start_input_thread(); + start_ui_thread(); start_audio_thread(); pthread_mutex_lock(&audio_q_mutex);