From 8c3db10b5bf7b77ff959d7d0b6cc425cfb37ccda Mon Sep 17 00:00:00 2001 From: younix Date: Sun, 11 Jul 2010 12:59:27 +0200 Subject: wip: impl. management function --- interface.h | 1 + pa-sink-ctl.c | 73 ++++++++++++++++++++++++++++------------------------------- sink.c | 32 +++++++++++++++++++++++--- sink.h | 9 +++++--- 4 files changed, 71 insertions(+), 44 deletions(-) diff --git a/interface.h b/interface.h index 5979210..d152c7d 100644 --- a/interface.h +++ b/interface.h @@ -11,6 +11,7 @@ void print_sinks(void); void print_volume(pa_volume_t, int); void get_input(void); +void interface_init(void); void interface_clear(void); #endif diff --git a/pa-sink-ctl.c b/pa-sink-ctl.c index 2370505..2322b50 100644 --- a/pa-sink-ctl.c +++ b/pa-sink-ctl.c @@ -1,7 +1,8 @@ +#define _XOPEN_SOURCE 500 +#include #include #include #include -#include #include #include "sink_input.h" @@ -15,7 +16,7 @@ #define HEIGHT 10 sink_info** sink_list = NULL; -int sink_counter; +uint32_t sink_counter; uint32_t sink_max; pa_mainloop_api *mainloop_api = NULL; @@ -33,24 +34,11 @@ int main(int argc, char** argv) sink_max = 1; sink_list = sink_list_init(sink_max); - // ncurses - chooser = 0; - - initscr(); - clear(); - noecho(); - cbreak(); /* Line buffering disabled. pass on everything */ - startx = (80 - WIDTH) / 2; - starty = (24 - HEIGHT) / 2; - menu_win = newwin(HEIGHT, WIDTH, starty, startx); - keypad(menu_win, TRUE); - mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice"); - refresh(); - + interface_init(); pa_mainloop *m = NULL; int ret = 1; - if(!(m = pa_mainloop_new())) { + if (!(m = pa_mainloop_new())) { printf("error: pa_mainloop_new() failed.\n"); return -1; } @@ -61,9 +49,10 @@ int main(int argc, char** argv) printf("error: pa_context_new() failed.\n"); return -1; } - + + // define callback for connection init pa_context_set_state_callback(context, context_state_callback, NULL); - if (pa_context_connect(context, "tcp:127.0.0.1:4713", PA_CONTEXT_NOAUTOSPAWN, NULL)) { + if (pa_context_connect(context, "tcp:127.0.0.1:4712", PA_CONTEXT_NOAUTOSPAWN, NULL)) { printf("error: pa_context_connect() failed.\n"); } @@ -74,7 +63,9 @@ int main(int argc, char** argv) return ret; } - +/* + * is called after connection + */ void context_state_callback(pa_context *c, void *userdata) { switch (pa_context_get_state(c)) { @@ -101,6 +92,9 @@ void context_state_callback(pa_context *c, void *userdata) { } } +/* + * the begin of the callback loops + */ void get_sink_info_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) { if (is_last < 0) { @@ -112,21 +106,20 @@ void get_sink_info_callback(pa_context *c, const pa_sink_info *i, int is_last, v pa_operation_unref(pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL)); return; } + + sink_list_check(sink_list, &sink_max, sink_counter); + + sink_list[sink_counter] = (sink_info*) calloc(1, sizeof(sink_info)); + sink_list[sink_counter]->index = i->index; + sink_list[sink_counter]->mute = i->mute; + sink_list[sink_counter]->name = strdup(i->name); ++sink_counter; - if (sink_counter >= sink_max) { - sink_max *= 2; - sink_list = realloc(sink_list, sizeof(sink_info*) * sink_max); - } - - sink_list[sink_counter - 1] = (sink_info*) calloc(1, sizeof(sink_info)); - sink_list[sink_counter - 1]->index = i->index; - sink_list[sink_counter - 1]->mute = i->mute; - - sink_list[sink_counter - 1]->name = (char*) calloc(strlen(i->name) + 1, sizeof(char)); - strncpy(sink_list[sink_counter - 1]->name, i->name, strlen(i->name)); } +/* + * is called after sink-input + */ void get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) { char t[32], k[32]; //,cv[PA_CVOLUME_SNPRINT_MAX]; @@ -168,7 +161,7 @@ void get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, in int counter = sink_list[sink_num]->input_counter; // check the length of the list - sink_check_list(sink_list[sink_num]); + sink_check_input_list(sink_list[sink_num]); // check the current element of the list sink_input_check(&(sink_list[ sink_num ]->input_list[ counter ])); sink_input_info* input = sink_list[sink_num]->input_list[counter]; @@ -180,15 +173,19 @@ void get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, in ++sink_list[sink_num]->input_counter; } -void quit(void) -{ - clrtoeol(); - refresh(); - endwin(); +void quit(void) { + sink_list_clear(sink_list, &sink_max, &sink_counter); + interface_clear(); exit(0); } +/* + * is called, after user input + */ void change_callback(pa_context* c, int success, void* userdate) { - sink_input_counter = 0; + + sink_list_reset(sink_list, &sink_counter); + + // get information about sinks pa_operation_unref(pa_context_get_sink_input_info_list(context, get_sink_input_info_callback, NULL)); } diff --git a/sink.c b/sink.c index 5875638..5abff14 100644 --- a/sink.c +++ b/sink.c @@ -38,13 +38,30 @@ void sink_clear(sink_info* sink) { sink = NULL; } -void sink_check_list(sink_info* sink) { +void sink_check(sink_info** sink) { + + if ((*sink) == NULL) + (*sink) = (sink_info*) calloc(1, sizeof(sink_input_info)); +} +/* + * check the list length and resize the list, if current position = max + */ +void sink_list_check(sink_info** sink_list, uint32_t* max, uint32_t counter) { + if (counter >= (*max)) { + (*max) *= 2; + sink_list = (sink_info**) realloc(sink_list, (*max) * sizeof(sink_info*)); + for (int i = counter; i < (*max); ++i) + sink_list[i] = NULL; + } +} + +void sink_check_input_list(sink_info* sink) { if (sink->input_counter >= sink->input_max) sink_input_list_enlarge(sink->input_list, &sink->input_max, sink->input_counter); } -sink_info** sink_list_init(int max) { +sink_info** sink_list_init(uint32_t max) { sink_info** sink_list = (sink_info**) calloc(max, sizeof(sink_info*)); @@ -54,13 +71,22 @@ sink_info** sink_list_init(int max) { return sink_list; } -void sink_list_clear(sink_info** sink_list, int* max) { +void sink_list_reset(sink_info** sink_list, uint32_t* counter) { + + for (int i = 0; i < (*counter); ++i) + sink_list[i]->input_counter = 0; + + (*counter) = 0; +} + +void sink_list_clear(sink_info** sink_list, uint32_t* max, uint32_t* counter) { for (int i = 0; i < (*max); ++i) if (sink_list[i] != NULL) sink_clear(sink_list[i]); (*max) = 0; + (*counter) = 0; free(sink_list); sink_list = NULL; diff --git a/sink.h b/sink.h index 64b8267..78f5837 100644 --- a/sink.h +++ b/sink.h @@ -24,9 +24,12 @@ typedef struct _sink_info { sink_info* sink_init(void); void sink_clear(sink_info*); -void sink_check_list(sink_info*); +void sink_check(sink_info**); +void sink_list_check(sink_info**, uint32_t*, uint32_t); +void sink_check_input_list(sink_info*); -sink_info** sink_list_init(int); -void sink_list_clear(sink_info**, int*); +sink_info** sink_list_init(uint32_t); +void sink_list_reset(sink_info**, uint32_t*); +void sink_list_clear(sink_info**, uint32_t*, uint32_t*); #endif -- cgit