From 21b1027832ff5e53edc37dd9f5c153217a65b6fd Mon Sep 17 00:00:00 2001 From: Jan Klemkow Date: Fri, 9 Jul 2010 00:55:02 +0200 Subject: wip: cleanup sink & co. --- pa-sink-ctl.c | 40 ++++++++++++++-------------------------- sink.c | 36 ++++++++++++++++++++++++++++++++++-- sink.h | 5 +++++ sink_input.c | 28 ++++++++++++++++++++++++++-- sink_input.h | 4 +++- 5 files changed, 82 insertions(+), 31 deletions(-) diff --git a/pa-sink-ctl.c b/pa-sink-ctl.c index b024d60..2370505 100644 --- a/pa-sink-ctl.c +++ b/pa-sink-ctl.c @@ -18,10 +18,6 @@ sink_info** sink_list = NULL; int sink_counter; uint32_t sink_max; -sink_input_info** sink_input_list = NULL; -int sink_input_counter; -int sink_input_max; - pa_mainloop_api *mainloop_api = NULL; pa_context *context = NULL; @@ -33,14 +29,9 @@ int starty; int main(int argc, char** argv) { - // pulseaudio - sink_input_counter = 0; - sink_input_max = 1; - sink_input_list = (sink_input_info**) calloc(sink_input_max, sizeof(sink_input_info*)); - sink_counter = 0; sink_max = 1; - sink_list = (sink_info**) calloc(sink_max, sizeof(sink_info*)); + sink_list = sink_list_init(sink_max); // ncurses chooser = 0; @@ -146,7 +137,7 @@ void get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, in if (is_last) { print_sinks(); - get_input(); + get_input(); return; } @@ -171,25 +162,22 @@ void get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, in pa_proplist_gets(i->proplist, "application.process.id")); */ - const char *name = pa_proplist_gets(i->proplist, "application.name"); +// const char *name = pa_proplist_gets(i->proplist, "application.name"); -// if (i->sink > sink_max) -// sink_max = i->sink; + int sink_num = i->sink; + int counter = sink_list[sink_num]->input_counter; - ++sink_input_counter; - - if (sink_input_counter >= sink_input_max) { - sink_input_max*=2; - sink_input_list = (sink_input_info**) realloc(sink_input_list, sizeof(sink_input_info*) * sink_input_max); - } + // check the length of the list + sink_check_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]; - sink_input_list[sink_input_counter-1] = (sink_input_info*) calloc(1, sizeof(sink_input_info)); - sink_input_list[sink_input_counter-1]->name = (char*) calloc(strlen(name) + 1, sizeof(char)); + input->name = strdup(pa_proplist_gets(i->proplist, "application.name")); + input->index = i->index; + input->vol = pa_cvolume_avg(&i->volume); - sink_input_list[sink_input_counter-1]->index = i->index; - sink_input_list[sink_input_counter-1]->sink = i->sink; - strncpy(sink_input_list[sink_input_counter-1]->name, name, strlen(name)); - sink_input_list[sink_input_counter-1]->vol = pa_cvolume_avg(&i->volume); + ++sink_list[sink_num]->input_counter; } void quit(void) diff --git a/sink.c b/sink.c index b237d32..5875638 100644 --- a/sink.c +++ b/sink.c @@ -7,6 +7,9 @@ #include "sink_input.h" #include "sink.h" +/* + * return an initilized sink + */ sink_info* sink_init(void) { sink_info* sink = (sink_info*) calloc(1, sizeof(sink_info)); @@ -16,13 +19,13 @@ sink_info* sink_init(void) { sink->input_max = 1; sink->input_list = NULL; - sink_input_list_init(sink->input_list, sink->input_max); + sink->input_list = sink_input_list_init(sink->input_max); return sink; } /* - * free's sink + * frees all components of a sink */ void sink_clear(sink_info* sink) { @@ -32,4 +35,33 @@ void sink_clear(sink_info* sink) { sink_input_list_clear(sink->input_list, &sink->input_max); free(sink); + sink = NULL; +} + +void sink_check_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 = (sink_info**) calloc(max, sizeof(sink_info*)); + + for (int i = 0; i < max; ++i) + sink_list[i] = NULL; + + return sink_list; +} + +void sink_list_clear(sink_info** sink_list, int* max) { + + for (int i = 0; i < (*max); ++i) + if (sink_list[i] != NULL) + sink_clear(sink_list[i]); + + (*max) = 0; + + free(sink_list); + sink_list = NULL; } diff --git a/sink.h b/sink.h index abdb731..64b8267 100644 --- a/sink.h +++ b/sink.h @@ -24,4 +24,9 @@ typedef struct _sink_info { sink_info* sink_init(void); void sink_clear(sink_info*); +void sink_check_list(sink_info*); + +sink_info** sink_list_init(int); +void sink_list_clear(sink_info**, int*); + #endif diff --git a/sink_input.c b/sink_input.c index 1a6eda2..54f7c04 100644 --- a/sink_input.c +++ b/sink_input.c @@ -24,10 +24,26 @@ void sink_input_clear(sink_input_info* sink_input) { free(sink_input->pid); free(sink_input); + sink_input = NULL; } -void sink_input_list_init(sink_input_info** sink_input_list, int max) { - sink_input_list = (sink_input_info**) calloc(max, sizeof(sink_input_info*)); +sink_input_info** sink_input_list_init(int max) { + + sink_input_info** sink_input_list = (sink_input_info**) calloc(max, sizeof(sink_input_info*)); + + for (int i = 0; i < max; ++i) + sink_input_list = NULL; + + return sink_input_list; +} + +void sink_input_list_enlarge(sink_input_info** sink_input_list, int* max, int counter) { + + (*max) *= 2; + sink_input_list = (sink_input_info**) realloc(sink_input_list, (*max) * sizeof(sink_input_info*)); + + for (int i = counter; i < (*max); ++i) + sink_input_list[i] = NULL; } void sink_input_list_clear(sink_input_info** sink_input_list, int *max) { @@ -38,9 +54,17 @@ void sink_input_list_clear(sink_input_info** sink_input_list, int *max) { (*max) = 0; free(sink_input_list); + sink_input_list = NULL; +} + +void sink_input_check(sink_input_info** sink_input) { + + if ((*sink_input) == NULL) + (*sink_input) = (sink_input_info*) calloc(1, sizeof(sink_input_info)); } int cmp_sink_input_list(const void *a, const void *b) { + sink_input_info* sinka = *((sink_input_info**) a); sink_input_info* sinkb = *((sink_input_info**) b); diff --git a/sink_input.h b/sink_input.h index e426cf7..d16924c 100644 --- a/sink_input.h +++ b/sink_input.h @@ -16,8 +16,10 @@ typedef struct _sink_input_info { sink_input_info* sink_input_init(); void sink_input_clear(sink_input_info*); -void sink_input_list_init(sink_input_info**, int); +sink_input_info** sink_input_list_init(int); +void sink_input_list_enlarge(sink_input_info**, int*, int); void sink_input_list_clear(sink_input_info**, int*); +void sink_input_check(sink_input_info**); int cmp_sink_input_list(const void *, const void *); #endif -- cgit