diff options
author | ben <benjaminfranzke@googlemail.com> | 2010-07-15 00:56:50 +0200 |
---|---|---|
committer | ben <benjaminfranzke@googlemail.com> | 2010-07-15 00:56:50 +0200 |
commit | c0aa6a50753ab4bfbe7e1f1fe7e823d65453279a (patch) | |
tree | d7f2c01a56fa340b87f36beb811c930233f1f896 /src | |
parent | 8b28bf9e4c0dc7ee03b0187a65a02783a92b4b2a (diff) | |
download | pa-sink-ctl-c0aa6a50753ab4bfbe7e1f1fe7e823d65453279a.tar.gz pa-sink-ctl-c0aa6a50753ab4bfbe7e1f1fe7e823d65453279a.tar.bz2 pa-sink-ctl-c0aa6a50753ab4bfbe7e1f1fe7e823d65453279a.zip |
replace sink_input_list implementation with GArray
Diffstat (limited to 'src')
-rw-r--r-- | src/interface.c | 28 | ||||
-rw-r--r-- | src/pa-sink-ctl.c | 34 | ||||
-rw-r--r-- | src/sink.c | 13 | ||||
-rw-r--r-- | src/sink.h | 10 | ||||
-rw-r--r-- | src/sink_input.c | 72 | ||||
-rw-r--r-- | src/sink_input.h | 13 |
6 files changed, 44 insertions, 126 deletions
diff --git a/src/interface.c b/src/interface.c index 002a09a..96de386 100644 --- a/src/interface.c +++ b/src/interface.c @@ -58,8 +58,8 @@ void print_sink_list(void) { if (chooser_input == -2) { chooser_input = -1; /* if index is going to be not found, select the sink itself */ /* step through inputs for current sink and find the selected */ - for (int i = 0; i < g_array_index(sink_list, sink_info, chooser_sink).input_counter; ++i) { - if (selected_index == g_array_index(sink_list, sink_info, chooser_sink).input_list[i]->index) { + for (int i = 0; i < g_array_index(sink_list, sink_info, chooser_sink).input_list->len; ++i) { + if (selected_index == g_array_index(g_array_index(sink_list, sink_info, chooser_sink).input_list, sink_input_info, i).index) { chooser_input = i; break; } @@ -81,7 +81,7 @@ void print_sink_list(void) { print_input_list(i); - offset += g_array_index(sink_list, sink_info, i).input_counter; + offset += g_array_index(sink_list, sink_info, i).input_list->len; } wrefresh(menu_win); } @@ -89,20 +89,20 @@ void print_sink_list(void) { void print_input_list(int sink_num) { int offset = sink_num + 1 + 2; for (int i = 0; i < sink_num; ++i) - offset += g_array_index(sink_list, sink_info, i).input_counter; + offset += g_array_index(sink_list, sink_info, i).input_list->len; - for (int i = 0; i < g_array_index(sink_list, sink_info, sink_num).input_counter; ++i) { + for (int i = 0; i < g_array_index(sink_list, sink_info, sink_num).input_list->len; ++i) { if (chooser_sink == sink_num && chooser_input == i) wattron(menu_win, A_REVERSE); mvwprintw(menu_win, offset + i, 2, "%*s%-*s", 2+1+1, "", 13 - 1, - g_array_index(sink_list, sink_info, sink_num).input_list[i]->name); + g_array_index(g_array_index(sink_list, sink_info, sink_num).input_list, sink_input_info, i).name); if (chooser_sink == sink_num && chooser_input == i) wattroff(menu_win, A_REVERSE); - print_volume(g_array_index(sink_list, sink_info, sink_num).input_list[i]->vol, - g_array_index(sink_list, sink_info, sink_num).input_list[i]->mute, offset + i); + print_volume(g_array_index(g_array_index(sink_list, sink_info, sink_num).input_list, sink_input_info, i).vol, + g_array_index(g_array_index(sink_list, sink_info, sink_num).input_list, sink_input_info, i).mute, offset + i); } } @@ -139,7 +139,7 @@ void get_input(void) case KEY_UP: if (chooser_input == -1 && chooser_sink > 0) { --chooser_sink; - chooser_input = g_array_index(sink_list, sink_info, chooser_sink).input_counter - 1; + chooser_input = (gint)g_array_index(sink_list, sink_info, chooser_sink).input_list->len - 1; } else if (chooser_input >= 0) @@ -148,11 +148,11 @@ void get_input(void) case 'j': case KEY_DOWN: - if (chooser_input == g_array_index(sink_list, sink_info, chooser_sink).input_counter - 1 && chooser_sink < sink_list->len - 1) { + if (chooser_input == ((gint)g_array_index(sink_list, sink_info, chooser_sink).input_list->len - 1) && chooser_sink < sink_list->len - 1) { ++chooser_sink; chooser_input = -1; } - else if (chooser_input < g_array_index(sink_list, sink_info, chooser_sink).input_counter - 1) + else if (chooser_input < ((gint)g_array_index(sink_list, sink_info, chooser_sink).input_list->len - 1)) ++chooser_input; break; @@ -169,7 +169,7 @@ void get_input(void) pa_operation* (*volume_set) (pa_context*,uint32_t,const pa_cvolume*,pa_context_success_cb_t,void*); if (chooser_input >= 0) { - sink_input_info *input = g_array_index(sink_list, sink_info, chooser_sink).input_list[chooser_input]; + sink_input_info *input = &g_array_index(g_array_index(sink_list, sink_info, chooser_sink).input_list, sink_input_info, chooser_input); index = input->index; volume.channels = input->channels; tmp_vol = input->vol; @@ -201,7 +201,7 @@ void get_input(void) case 'M': { pa_operation* (*mute_set) (pa_context*,uint32_t,int,pa_context_success_cb_t,void*); if (chooser_input >= 0) { - sink_input_info *input = g_array_index(sink_list, sink_info, chooser_sink).input_list[chooser_input]; + sink_input_info *input = &g_array_index(g_array_index(sink_list, sink_info, chooser_sink).input_list, sink_input_info, chooser_input); index = input->index; mute = input->mute; mute_set = pa_context_set_sink_input_mute; @@ -224,7 +224,7 @@ void get_input(void) case ' ': if (chooser_input == -1) break; - selected_index = g_array_index(sink_list, sink_info, chooser_sink).input_list[chooser_input]->index; + selected_index = g_array_index(g_array_index(sink_list, sink_info, chooser_sink).input_list, sink_input_info, chooser_input).index; if (chooser_sink < sink_list->len - 1) chooser_sink++;//sink = chooser_sink + 1; else diff --git a/src/pa-sink-ctl.c b/src/pa-sink-ctl.c index 9dd0610..45d3022 100644 --- a/src/pa-sink-ctl.c +++ b/src/pa-sink-ctl.c @@ -31,7 +31,7 @@ int main(int argc, char** argv) GMainLoop *g_loop = NULL; pa_glib_mainloop *m = NULL; - sink_list_alloc(&sink_list); + sink_list = sink_list_alloc(); interface_init(); @@ -118,10 +118,7 @@ void get_sink_info_callback(pa_context *c, const pa_sink_info *i, int is_last, v .device = pa_proplist_contains(i->proplist, "device.product.name") ? strdup(pa_proplist_gets(i->proplist, "device.product.name")) : NULL, - .input_counter = 0, - .input_max = 1, - .input_list = sink_input_list_init(1) - + .input_list = sink_input_list_alloc() })); } @@ -147,22 +144,15 @@ void get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, in snprintf(t, sizeof(t), "%u", i->owner_module); snprintf(k, sizeof(k), "%u", i->client); - int sink_num = i->sink; - int counter = g_array_index(sink_list, sink_info, sink_num).input_counter; - // check the length of the list - sink_check_input_list(&g_array_index(sink_list, sink_info, sink_num)); - - // check the current element of the list - sink_input_check(&(g_array_index(sink_list, sink_info, sink_num).input_list[counter])); - - sink_input_info* input = g_array_index(sink_list, sink_info, sink_num).input_list[counter]; - input->name = strdup(pa_proplist_gets(i->proplist, "application.name")); - input->index = i->index; - input->channels = i->volume.channels; - input->vol = pa_cvolume_avg(&i->volume); - input->mute = i->mute; - - ++(g_array_index(sink_list, sink_info, sink_num).input_counter); + g_array_append_val(g_array_index(sink_list, sink_info, i->sink).input_list, ((sink_input_info) { + .index = i->index, + .sink = i->sink, + .name = strdup(pa_proplist_gets(i->proplist, "application.name")), + .mute = i->mute, + .channels = i->volume.channels, + .vol = pa_cvolume_avg(&i->volume), + .pid = NULL /* maybe obsolete */ + })); } void quit(void) { @@ -182,6 +172,6 @@ void change_callback(pa_context* c, int success, void* userdate) { void collect_all_info(void) { sink_list_free(sink_list); - sink_list_alloc(&sink_list); + sink_list = sink_list_alloc(); pa_operation_unref(pa_context_get_sink_info_list(context, get_sink_info_callback, NULL)); } @@ -8,31 +8,24 @@ #include "sink_input.h" #include "sink.h" -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); -} - /* * init a sink list */ -void sink_list_alloc(GArray **sink_list) { - *sink_list = g_array_sized_new(false, false, sizeof(sink_info), 16); +GArray *sink_list_alloc(void) { + return g_array_sized_new(false, false, sizeof(sink_info), 16); } /* * frees all dynamic allocated components of a sink */ static void sink_clear(sink_info* sink) { - if (sink->name != NULL) free(sink->name); if (sink->device != NULL) free(sink->device); - sink_input_list_clear(sink->input_list, &sink->input_max); + sink_input_list_free(sink->input_list); } /* @@ -17,16 +17,10 @@ typedef struct _sink_info { int mute; uint8_t channels; pa_volume_t vol; - - // input list - int input_counter; - int input_max; - sink_input_info** input_list; + GArray *input_list; } sink_info; -void sink_check_input_list(sink_info*); - -void sink_list_alloc(GArray **sink_list); +GArray *sink_list_alloc(void); void sink_list_free(GArray *sink_list); #endif diff --git a/src/sink_input.c b/src/sink_input.c index a1440df..ae5ad83 100644 --- a/src/sink_input.c +++ b/src/sink_input.c @@ -1,4 +1,5 @@ #include <stdio.h> +#include <glib.h> #include <pulse/pulseaudio.h> #include <ncurses.h> #include <string.h> @@ -6,77 +7,20 @@ #include "sink_input.h" -sink_input_info* sink_input_init(void) { - - sink_input_info* sink_input = (sink_input_info*) calloc(1, sizeof(sink_input_info)); - sink_input->name = NULL; - sink_input->pid = NULL; - - return sink_input; +GArray *sink_input_list_alloc(void) { + return g_array_sized_new(false, false, sizeof(sink_input_info), 8); } -void sink_input_clear(sink_input_info* sink_input) { - +static void sink_input_clear(sink_input_info* sink_input) { if (sink_input->name != NULL) free(sink_input->name); if (sink_input->pid != NULL) free(sink_input->pid); - - free(sink_input); - // sink_input = NULL; } -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[i] = NULL; - - return sink_input_list; +void sink_input_list_free(GArray *sink_input_list) { + for (int i = 0; i < sink_input_list->len; ++i) + sink_input_clear(&g_array_index(sink_input_list, sink_input_info, i)); + g_array_free(sink_input_list, true); } - -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) { - - for (int i = 0; i < (*max); ++i) - if (sink_input_list[i] != NULL) - sink_input_clear(sink_input_list[i]); - - (*max) = 0; - - free(sink_input_list); - // sink_input_list = NULL; -} - -void sink_input_check(sink_input_info** sink_input) { - - if (sink_input == NULL) - printf("Error: NULL\n"); - - if ((*sink_input) == NULL) - (*sink_input) = sink_input_init(); -} - -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); - - if (sinka->sink < sinkb->sink) - return -1; - else if (sinka->sink > sinkb->sink) - return 1; - else - return 0; -} - diff --git a/src/sink_input.h b/src/sink_input.h index 5a65992..2691afc 100644 --- a/src/sink_input.h +++ b/src/sink_input.h @@ -1,6 +1,9 @@ #ifndef SINK_INPUT_H #define SINK_INPUT_H +#include <stdint.h> + +#include <glib.h> #include <pulse/pulseaudio.h> // TODO: change this with the given define from pulselib #define VOLUME_MAX UINT16_MAX @@ -15,13 +18,7 @@ typedef struct _sink_input_info { pa_volume_t vol; // TOTO: exchange with the channel-list } sink_input_info; -sink_input_info* sink_input_init(void); -void sink_input_clear(sink_input_info*); - -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 *); +GArray *sink_input_list_alloc(void); +void sink_input_list_free(GArray *sink_input_list); #endif |