diff options
author | Benjamin Franzke <benjaminfranzke@googlemail.com> | 2011-10-21 12:52:21 +0200 |
---|---|---|
committer | Benjamin Franzke <benjaminfranzke@googlemail.com> | 2011-10-21 23:13:39 +0200 |
commit | badb9650df460fa3999ac937958ac05f97eaad88 (patch) | |
tree | cf1e1f6c40bda9e9baa05d08ad5a032d58519422 /src/pa-sink-ctl.c | |
parent | b96d01ced40a2fe3df47dc4690bc50c6466e3d12 (diff) | |
download | pa-sink-ctl-badb9650df460fa3999ac937958ac05f97eaad88.tar.gz pa-sink-ctl-badb9650df460fa3999ac937958ac05f97eaad88.tar.bz2 pa-sink-ctl-badb9650df460fa3999ac937958ac05f97eaad88.zip |
Stop using global variables.
Rather store it in a context, thats used everywhere as parameter,
or as userdata.
Diffstat (limited to 'src/pa-sink-ctl.c')
-rw-r--r-- | src/pa-sink-ctl.c | 131 |
1 files changed, 65 insertions, 66 deletions
diff --git a/src/pa-sink-ctl.c b/src/pa-sink-ctl.c index a2d8afe..32d7873 100644 --- a/src/pa-sink-ctl.c +++ b/src/pa-sink-ctl.c @@ -6,18 +6,6 @@ #include "interface.h" #include "pa-sink-ctl.h" -pa_context *context = NULL; -gboolean context_ready = FALSE; - -static gboolean info_callbacks_finished = TRUE; -static gboolean info_callbacks_blocked = FALSE; - -struct fetch_ctx { - GList *sinks; -}; - -GList *sink_list; - #define list_append_struct(list, data) \ do { \ (list) = g_list_append((list), \ @@ -27,43 +15,48 @@ GList *sink_list; int main(int argc, char** argv) { + struct context *ctx = g_new0(struct context, 1); pa_mainloop_api *mainloop_api = NULL; pa_glib_mainloop *m = NULL; - sink_list = NULL; + ctx->info_callbacks_finished = TRUE; + ctx->info_callbacks_blocked = FALSE; + ctx->sink_list = NULL; + ctx->max_name_len = 0; + ctx->context_ready = FALSE; - GMainLoop *g_loop = g_main_loop_new(NULL, FALSE); + ctx->loop = g_main_loop_new(NULL, FALSE); - interface_init(); + interface_init(ctx); if (!(m = pa_glib_mainloop_new(NULL))) { - interface_clear(); + interface_clear(ctx); g_printerr("error: pa_glib_mainloop_new() failed.\n"); return -1; } mainloop_api = pa_glib_mainloop_get_api(m); - if (!(context = pa_context_new(mainloop_api, "pa-sink-ctl"))) { - interface_clear(); + if (!(ctx->context = pa_context_new(mainloop_api, "pa-sink-ctl"))) { + interface_clear(ctx); g_printerr("error: pa_context_new() failed.\n"); return -1; } // define callback for connection init - pa_context_set_state_callback(context, context_state_callback, g_loop); - if (pa_context_connect(context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL)) { - interface_clear(); + pa_context_set_state_callback(ctx->context, context_state_callback, ctx); + if (pa_context_connect(ctx->context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL)) { + interface_clear(ctx); g_printerr("error: pa_context_connect() failed.\n"); } - g_main_loop_run(g_loop); + g_main_loop_run(ctx->loop); - interface_clear(); - g_list_free(sink_list); + interface_clear(ctx); + g_list_free(ctx->sink_list); pa_glib_mainloop_free(m); - g_main_loop_unref(g_loop); + g_main_loop_unref(ctx->loop); return 0; } @@ -71,10 +64,12 @@ main(int argc, char** argv) static void subscribe_cb(pa_context *c, pa_subscription_event_type_t t, guint32 idx, gpointer userdata) { - if (!info_callbacks_finished) - info_callbacks_blocked = TRUE; + struct context *ctx = userdata; + + if (!ctx->info_callbacks_finished) + ctx->info_callbacks_blocked = TRUE; else - collect_all_info(); + collect_all_info(ctx); } /* @@ -83,42 +78,43 @@ subscribe_cb(pa_context *c, pa_subscription_event_type_t t, guint32 idx, gpointe void context_state_callback(pa_context *c, gpointer userdata) { - static pa_operation *o = NULL; - context_ready = FALSE; + struct context *ctx = userdata; + + ctx->context_ready = FALSE; switch (pa_context_get_state(c)) { case PA_CONTEXT_CONNECTING: - interface_set_status("connecting..."); + interface_set_status(ctx, "connecting..."); break; case PA_CONTEXT_AUTHORIZING: - interface_set_status("authorizing..."); + interface_set_status(ctx, "authorizing..."); break; case PA_CONTEXT_SETTING_NAME: - interface_set_status("setting name..."); + interface_set_status(ctx, "setting name..."); break; case PA_CONTEXT_READY: - collect_all_info(); - pa_context_set_subscribe_callback(context, subscribe_cb, NULL); - g_assert((o = pa_context_subscribe(c, (pa_subscription_mask_t) ( + collect_all_info(ctx); + pa_context_set_subscribe_callback(c, subscribe_cb, ctx); + g_assert((ctx->op = pa_context_subscribe(c, (pa_subscription_mask_t) ( PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SINK_INPUT ), NULL, NULL))); - context_ready = TRUE; - interface_set_status("ready to process events."); + ctx->context_ready = TRUE; + interface_set_status(ctx, "ready to process events."); break; case PA_CONTEXT_FAILED: - interface_set_status("cannot connect!"); + interface_set_status(ctx, "cannot connect!"); break; case PA_CONTEXT_TERMINATED: - g_assert(o != NULL); - pa_operation_cancel(o); - pa_operation_unref(o); - o = NULL; - interface_set_status("connection terminated."); - g_main_loop_quit((GMainLoop *)userdata); + g_assert(ctx->op != NULL); + pa_operation_cancel(ctx->op); + pa_operation_unref(ctx->op); + ctx->op = NULL; + interface_set_status(ctx, "connection terminated."); + g_main_loop_quit(ctx->loop); break; default: - interface_set_status("unknown state"); + interface_set_status(ctx, "unknown state"); break; } } @@ -131,15 +127,15 @@ void get_sink_info_callback(pa_context *c, const pa_sink_info *i, gint is_last, gpointer userdata) { g_assert(userdata != NULL); - struct fetch_ctx *fetch_ctx = userdata; + struct context *ctx = userdata; if (is_last < 0) { g_printerr("Failed to get sink information: %s\n", pa_strerror(pa_context_errno(c))); - quit(); + quit(ctx); } if (is_last) { - pa_operation_unref(pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, fetch_ctx)); + pa_operation_unref(pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, ctx)); return; } @@ -154,7 +150,7 @@ get_sink_info_callback(pa_context *c, const pa_sink_info *i, gint is_last, gpoin .input_list = NULL }; - list_append_struct(fetch_ctx->sinks, sink); + list_append_struct(ctx->tmp_sinks, sink); } /* @@ -164,7 +160,7 @@ void get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, gint is_last, gpointer userdata) { g_assert(userdata != NULL); - struct fetch_ctx *fetch_ctx = userdata; + struct context *ctx = userdata; if (is_last < 0) { g_printerr("Failed to get sink input information: %s\n", pa_strerror(pa_context_errno(c))); @@ -172,16 +168,15 @@ get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, gint is } if (is_last) { - info_callbacks_finished = TRUE; - g_list_free_full(sink_list, g_free); - sink_list = fetch_ctx->sinks; - g_free(fetch_ctx); + ctx->info_callbacks_finished = TRUE; + g_list_free_full(ctx->sink_list, g_free); + ctx->sink_list = ctx->tmp_sinks; - print_sink_list(); + print_sink_list(ctx); - if (info_callbacks_blocked) { - info_callbacks_blocked = FALSE; - collect_all_info(); + if (ctx->info_callbacks_blocked) { + ctx->info_callbacks_blocked = FALSE; + collect_all_info(ctx); } return; } @@ -200,14 +195,14 @@ get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, gint is .pid = NULL /* maybe obsolete */ }; - sink_info *sink = g_list_nth_data(fetch_ctx->sinks, i->sink); + sink_info *sink = g_list_nth_data(ctx->tmp_sinks, i->sink); list_append_struct(sink->input_list, sink_input); } void -quit(void) +quit(struct context *ctx) { - pa_context_disconnect(context); + pa_context_disconnect(ctx->context); } /* @@ -216,14 +211,18 @@ quit(void) void change_callback(pa_context* c, gint success, gpointer userdata) { +#if 0 + struct context *ctx = userdata; +#endif return; } void -collect_all_info(void) +collect_all_info(struct context *ctx) { - if (!info_callbacks_finished) + if (!ctx->info_callbacks_finished) return; - info_callbacks_finished = FALSE; - pa_operation_unref(pa_context_get_sink_info_list(context, get_sink_info_callback, g_new0(struct fetch_ctx, 1))); + ctx->info_callbacks_finished = FALSE; + ctx->tmp_sinks = NULL; + pa_operation_unref(pa_context_get_sink_info_list(ctx->context, get_sink_info_callback, ctx)); } |