summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorben <benjaminfranzke@googlemail.com>2010-07-15 00:10:48 +0200
committerben <benjaminfranzke@googlemail.com>2010-07-15 00:12:49 +0200
commit25edb3357129ebb184fe24de18b8b73e8f35b6fc (patch)
tree06c084347f488f056fe097c65fc211dfd017fa4b
parent9ab2dc0b904c5b684c7b408c479253cb51de52de (diff)
downloadpa-sink-ctl-25edb3357129ebb184fe24de18b8b73e8f35b6fc.tar.gz
pa-sink-ctl-25edb3357129ebb184fe24de18b8b73e8f35b6fc.tar.bz2
pa-sink-ctl-25edb3357129ebb184fe24de18b8b73e8f35b6fc.zip
replace own sink-list implementation with GArray
-rw-r--r--src/interface.c52
-rw-r--r--src/pa-sink-ctl.c56
-rw-r--r--src/sink.c102
-rw-r--r--src/sink.h12
4 files changed, 74 insertions, 148 deletions
diff --git a/src/interface.c b/src/interface.c
index 450315b..002a09a 100644
--- a/src/interface.c
+++ b/src/interface.c
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <glib.h>
#include <pulse/pulseaudio.h>
#include <ncurses.h>
#include <string.h>
@@ -22,9 +23,7 @@ int starty;
int selected_index;
-extern int sink_counter;
-extern int sink_max;
-extern sink_info** sink_list;
+extern GArray *sink_list;
extern pa_context* context;
@@ -59,51 +58,51 @@ 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 < sink_list[chooser_sink]->input_counter; ++i) {
- if (selected_index == sink_list[chooser_sink]->input_list[i]->index) {
+ 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) {
chooser_input = i;
break;
}
}
}
- for (i = 0; i < sink_counter; ++i) {
+ for (i = 0; i < sink_list->len; ++i) {
if (i == chooser_sink && chooser_input == -1)
wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y+i+offset, x, "%2d %-13s",
- sink_list[i]->index,
- sink_list[i]->device != NULL ? sink_list[i]->device : sink_list[i]->name);
+ g_array_index(sink_list, sink_info, i).index,
+ g_array_index(sink_list, sink_info, i).device != NULL ? g_array_index(sink_list, sink_info, i).device : g_array_index(sink_list, sink_info, i).name);
if (i == chooser_sink && chooser_input == -1)
wattroff(menu_win, A_REVERSE);
- print_volume(sink_list[i]->vol, sink_list[i]->mute, y+i+offset);
+ print_volume(g_array_index(sink_list, sink_info, i).vol, g_array_index(sink_list, sink_info, i).mute, y+i+offset);
print_input_list(i);
- offset += sink_list[i]->input_counter;
+ offset += g_array_index(sink_list, sink_info, i).input_counter;
}
+ wrefresh(menu_win);
}
void print_input_list(int sink_num) {
int offset = sink_num + 1 + 2;
for (int i = 0; i < sink_num; ++i)
- offset += sink_list[i]->input_counter;
-
- for (int i = 0; i < sink_list[sink_num]->input_counter; ++i) {
+ offset += g_array_index(sink_list, sink_info, i).input_counter;
+ for (int i = 0; i < g_array_index(sink_list, sink_info, sink_num).input_counter; ++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,
- sink_list[sink_num]->input_list[i]->name);
+ g_array_index(sink_list, sink_info, sink_num).input_list[i]->name);
if (chooser_sink == sink_num && chooser_input == i)
wattroff(menu_win, A_REVERSE);
- print_volume(sink_list[sink_num]->input_list[i]->vol,
- sink_list[sink_num]->input_list[i]->mute, offset + i);
+ 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);
}
}
@@ -125,7 +124,6 @@ void print_volume(pa_volume_t volume, int mute, int y) {
mvwprintw(menu_win, y, x + VOLUME_BAR_LEN, "]");
}
-
void get_input(void)
{
int c;
@@ -141,7 +139,7 @@ void get_input(void)
case KEY_UP:
if (chooser_input == -1 && chooser_sink > 0) {
--chooser_sink;
- chooser_input = sink_list[chooser_sink]->input_counter - 1;
+ chooser_input = g_array_index(sink_list, sink_info, chooser_sink).input_counter - 1;
}
else if (chooser_input >= 0)
@@ -150,11 +148,11 @@ void get_input(void)
case 'j':
case KEY_DOWN:
- if (chooser_input == sink_list[chooser_sink]->input_counter - 1 && chooser_sink < sink_counter - 1) {
+ if (chooser_input == g_array_index(sink_list, sink_info, chooser_sink).input_counter - 1 && chooser_sink < sink_list->len - 1) {
++chooser_sink;
chooser_input = -1;
}
- else if (chooser_input < sink_list[chooser_sink]->input_counter - 1)
+ else if (chooser_input < g_array_index(sink_list, sink_info, chooser_sink).input_counter - 1)
++chooser_input;
break;
@@ -171,13 +169,13 @@ 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 = sink_list[chooser_sink]->input_list[chooser_input];
+ sink_input_info *input = g_array_index(sink_list, sink_info, chooser_sink).input_list[chooser_input];
index = input->index;
volume.channels = input->channels;
tmp_vol = input->vol;
volume_set = pa_context_set_sink_input_volume;
} else if (chooser_input == -1) {
- sink_info *sink = sink_list[chooser_sink];
+ sink_info *sink = &g_array_index(sink_list, sink_info, chooser_sink);
index = sink->index;
volume.channels = sink->channels;
tmp_vol = sink->vol;
@@ -203,12 +201,12 @@ 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 = sink_list[chooser_sink]->input_list[chooser_input];
+ sink_input_info *input = g_array_index(sink_list, sink_info, chooser_sink).input_list[chooser_input];
index = input->index;
mute = input->mute;
mute_set = pa_context_set_sink_input_mute;
} else if (chooser_input == -1) {
- sink_info *sink = sink_list[chooser_sink];
+ sink_info *sink = &g_array_index(sink_list, sink_info, chooser_sink);
index = sink->index;
mute = sink->mute;
mute_set = pa_context_set_sink_mute_by_index;
@@ -226,8 +224,8 @@ void get_input(void)
case ' ':
if (chooser_input == -1)
break;
- selected_index = sink_list[chooser_sink]->input_list[chooser_input]->index;
- if (chooser_sink < sink_counter - 1)
+ selected_index = g_array_index(sink_list, sink_info, chooser_sink).input_list[chooser_input]->index;
+ if (chooser_sink < sink_list->len - 1)
chooser_sink++;//sink = chooser_sink + 1;
else
chooser_sink = 0;
@@ -237,7 +235,7 @@ void get_input(void)
pa_context_move_sink_input_by_index(
context,
selected_index,
- sink_list[chooser_sink]->index,
+ g_array_index(sink_list, sink_info, chooser_sink).index,
change_callback,
NULL));
return;
diff --git a/src/pa-sink-ctl.c b/src/pa-sink-ctl.c
index 44a6fcc..9dd0610 100644
--- a/src/pa-sink-ctl.c
+++ b/src/pa-sink-ctl.c
@@ -20,28 +20,18 @@
#define WIDTH 80
#define HEIGHT 10
-sink_info** sink_list = NULL;
-uint32_t sink_counter;
-uint32_t sink_max;
+GArray *sink_list = NULL;
pa_mainloop_api *mainloop_api = NULL;
pa_context *context = NULL;
-// ncurses
-WINDOW *menu_win;
-int chooser;
-int startx;
-int starty;
-
int main(int argc, char** argv)
{
GMainContext *g_context = NULL;
GMainLoop *g_loop = NULL;
pa_glib_mainloop *m = NULL;
- sink_counter = 0;
- sink_max = 1;
- sink_list = sink_list_init(sink_max);
+ sink_list_alloc(&sink_list);
interface_init();
@@ -119,17 +109,20 @@ void get_sink_info_callback(pa_context *c, const pa_sink_info *i, int is_last, v
return;
}
- sink_list_check(&sink_list, &sink_max, sink_counter);
- sink_check(&(sink_list[sink_counter]));
- sink_list[sink_counter]->index = i->index;
- sink_list[sink_counter]->mute = i->mute;
- sink_list[sink_counter]->vol = pa_cvolume_avg(&i->volume);
- sink_list[sink_counter]->channels = i->volume.channels;
- sink_list[sink_counter]->name = strdup(i->name);
- const char *tmp = pa_proplist_gets(i->proplist, "device.product.name");
- sink_list[sink_counter]->device = (tmp == NULL) ? NULL : strdup(tmp);
-
- ++sink_counter;
+ g_array_append_val(sink_list, ((sink_info) {
+ .index = i->index,
+ .mute = i->mute,
+ .vol = pa_cvolume_avg(&i->volume),
+ .channels = i->volume.channels,
+ .name = strdup(i->name),
+ .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)
+
+ }));
}
/*
@@ -155,23 +148,25 @@ void get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, in
snprintf(k, sizeof(k), "%u", i->client);
int sink_num = i->sink;
- int counter = sink_list[sink_num]->input_counter;
+ int counter = g_array_index(sink_list, sink_info, sink_num).input_counter;
// check the length of the list
- sink_check_input_list(sink_list[sink_num]);
+ sink_check_input_list(&g_array_index(sink_list, sink_info, 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_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;
- ++(sink_list[sink_num]->input_counter);
+ ++(g_array_index(sink_list, sink_info, sink_num).input_counter);
}
void quit(void) {
- sink_list_clear(sink_list, &sink_max, &sink_counter);
+ sink_list_free(sink_list);
interface_clear();
exit(0);
}
@@ -186,6 +181,7 @@ void change_callback(pa_context* c, int success, void* userdate) {
}
void collect_all_info(void) {
- sink_list_reset(sink_list, &sink_counter);
+ sink_list_free(sink_list);
+ sink_list_alloc(&sink_list);
pa_operation_unref(pa_context_get_sink_info_list(context, get_sink_info_callback, NULL));
}
diff --git a/src/sink.c b/src/sink.c
index 8f6b13b..e086a64 100644
--- a/src/sink.c
+++ b/src/sink.c
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <glib.h>
#include <pulse/pulseaudio.h>
#include <ncurses.h>
#include <string.h>
@@ -7,26 +8,30 @@
#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));
+void sink_check_input_list(sink_info* sink) {
- sink->name = NULL;
- sink->device = NULL;
- sink->input_counter = 0;
- sink->input_max = 1;
- sink->input_list = NULL;
+ if (sink->input_counter >= sink->input_max)
+ sink_input_list_enlarge(&sink->input_list, &sink->input_max, sink->input_counter);
+}
- sink->input_list = sink_input_list_init(sink->input_max);
+/*
+ * init a sink list
+ */
+void sink_list_alloc(GArray **sink_list) {
+ *sink_list = g_array_sized_new(false, false, sizeof(sink_info), 16);
+}
- return sink;
+/*
+ * frees a complete sink array
+ */
+void sink_list_free(GArray *sink_list) {
+ for (int i = 0; i < sink_list->len; ++i)
+ sink_clear(&g_array_index(sink_list, sink_info, i));
+ g_array_free(sink_list, true);
}
/*
- * frees all components of a sink
+ * frees all dynamic allocated components of a sink
*/
void sink_clear(sink_info* sink) {
@@ -37,74 +42,5 @@ void sink_clear(sink_info* sink) {
free(sink->device);
sink_input_list_clear(sink->input_list, &sink->input_max);
-
- free(sink);
- //sink = NULL;
-}
-
-void sink_check(sink_info** sink) {
-
- if (sink == NULL)
- printf("Error: sink = NULL\n");
-
- if ((*sink) == NULL)
- (*sink) = sink_init();
}
-/*
- * 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)
- return;
-
- *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(uint32_t 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_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;
-
- /* TODO: for all *_clear:
- * setting local parameter to NULL doesnt do want is wanted
- * pointer to sink_list would be needed here...
- */
-}
diff --git a/src/sink.h b/src/sink.h
index e47b8ff..fbc47ee 100644
--- a/src/sink.h
+++ b/src/sink.h
@@ -2,6 +2,7 @@
#define SINK_H
#include <stdio.h>
+#include <glib.h>
#include <pulse/pulseaudio.h>
#include <ncurses.h>
#include <string.h>
@@ -23,15 +24,10 @@ typedef struct _sink_info {
sink_input_info** input_list;
} sink_info;
-sink_info* sink_init(void);
-void sink_clear(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(uint32_t);
-void sink_list_reset(sink_info**, uint32_t*);
-void sink_list_clear(sink_info**, uint32_t*, uint32_t*);
+void sink_list_alloc(GArray **sink_list);
+void sink_list_free(GArray *sink_list);
+void sink_clear(sink_info*);
#endif