From a35e1d48e54ac59aecbf61aaba5e9dfb0cf151e0 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Wed, 21 Jan 2015 17:10:22 -0600 Subject: Checkpoint, nothing works --- tests/pa-mock.c | 299 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 299 insertions(+) create mode 100644 tests/pa-mock.c diff --git a/tests/pa-mock.c b/tests/pa-mock.c new file mode 100644 index 0000000..6f4e6ad --- /dev/null +++ b/tests/pa-mock.c @@ -0,0 +1,299 @@ + +#include + +#ifdef G_LOG_DOMAIN +#undef G_LOG_DOMAIN +#endif +#define G_LOG_DOMAIN "PA-Mock" + +G_DEFINE_QUARK("pa-mock-state-cb-list", state_cb); +typedef struct { + pa_context_notify_cb_t cb; + gpointer user_data; +} state_cb_t; + +/* ******************************* + * context.h + * *******************************/ + +static void +context_weak_cb (gpointer user_data, GObject * oldobj) +{ + g_debug("Finalizing context: %p", oldobj); +} + +pa_context * +pa_context_new_with_proplist (pa_mainloop_api *mainloop, const char *name, pa_proplist *proplist) +{ + GObject * gctx = g_object_new(G_TYPE_OBJECT); + pa_context * ctx = (pa_context *)gctx; + + g_debug("Creating new context: %p", ctx); + g_object_weak_ref(gctx, context_weak_cb, NULL); + + return ctx; +} + +void +pa_context_unref (pa_context *c) { + g_return_if_fail(G_IS_OBJECT(c)); + g_object_unref(G_OBJECT(c)); +} + +void +pa_context_ref (pa_context *c) { + g_return_if_fail(G_IS_OBJECT(c)); + g_object_ref(G_OBJECT(c)); +} + +int +pa_context_connect (pa_context *c, const char *server, pa_context_flags_t flags, const pa_spawn_api *api) +{ + g_return_if_fail(G_IS_OBJECT(c)); + g_debug("Context Connect"); + return 0; +} + +void +pa_context_disconnect (pa_context *c) +{ + g_return_if_fail(G_IS_OBJECT(c)); + g_debug("Context Disconnect"); +} + +int +pa_context_errno (pa_context *c) +{ + g_return_val_if_fail(G_IS_OBJECT(c), -1); + + return 0; +} + +static void +state_cb_list_destroy (gpointer data) +{ + GList * statelist = (GList *)data; + g_list_free_full(statelist, g_free); +} + +void +pa_context_set_state_callback (pa_context *c, pa_context_notify_cb_t cb, void *userdata) +{ + g_return_if_fail(G_IS_OBJECT(c)); + g_return_if_fail(cb != NULL); + + state_cb_t * state_cb = g_new0(state_cb_t, 1); + state_cb->cb = cb; + state_cb->userdata = userdata; + + GList * statelist = g_object_get_qdata(G_OBJECT(c), state_cb_quark); + statelist = g_list_append(statelist, state_cb); + g_object_set_qdata_full(G_OBJECT(c), state_cb_quark, state_cb, state_cb_list_destroy); +} + +pa_context_state_t +pa_context_get_state (pa_context *c) +{ + g_return_if_fail(G_IS_OBJECT(c)); + + return PA_CONTEXT_READY; +} + +/* ******************************* + * introspect.h + * *******************************/ + +typedef struct { + pa_server_info_cb_t cb; + gpointer userdata; + pa_context * context; +} get_server_info_t; + +static void +get_server_info_free (gpointer data) +{ + get_server_info_t * info = (get_server_info_t *)data; + g_object_unref(info->context); + g_free(info); +} + +static gboolean +get_server_info_cb (gpointer data) +{ + pa_server_info server = { + .user_name = "user", + .host_name = "host", + .server_version = "1.2.3", + .server_name = "server", + .sample_spec = , + .default_sink_name = "default-sink", + .default_source_name = "default-source", + .cookie = 1234, + .channel_map = { + .channels = 0 + } + }; + get_server_info_t * info = (get_server_info_t *)data; + + info->cb(info->context, &server, info->userdata); + + return G_SOURCE_REMOVE; +} + +pa_operation* +pa_context_get_server_info (pa_context *c, pa_server_info_cb_t cb, void *userdata) +{ + g_return_val_if_fail(G_IS_OBJECT(c), NULL); + g_return_val_if_fail(cb != NULL, NULL); + + get_server_info_t * info = g_new(get_server_info_t, 1); + info->cb = cb; + info->userdata = userdata; + info->context = g_objet_ref(c); + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + get_server_info_cb, + info, + get_server_info_free); + + GObject * goper = g_object_new(G_TYPE_OBJECT); + pa_operation * oper = (pa_operation *)goper; + return oper; +} + +typedef struct { + pa_sink_info_cb_t cb; + gpointer userdata; + pa_context * context; + uint32_t index; +} get_sink_info_t; + +static void +get_sink_info_free (gpointer data) +{ + get_sink_info_t * info = (get_sink_info_t *)data; + g_object_unref(info->context); + g_free(info); +} + +static gboolean +get_info_info_cb (gpointer data) +{ + pa_sink_info sink = { + .name = "default-sink", + .index = 0, + .description = "Default Sink", + .channel_map = { + .channels = 0 + } + }; + get_sink_info_t * info = (get_sink_info_t *)data; + + info->cb(info->context, &sink, 1, info->userdata); + + return G_SOURCE_REMOVE; +} + +pa_operation* +pa_context_get_sink_info_by_name (pa_context *c, const gchar * name, pa_sink_info_cb_t cb, void *userdata) +{ + g_return_val_if_fail(G_IS_OBJECT(c), NULL); + g_return_val_if_fail(g_strcmp0(name, "default-sink") == 0, NULL); + g_return_val_if_fail(cb != NULL, NULL); + + get_sink_info_t * info = g_new(get_sink_info_t, 1); + info->cb = cb; + info->userdata = userdata; + info->context = g_objet_ref(c); + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + get_sink_info_cb, + info, + get_sink_info_free); + + GObject * goper = g_object_new(G_TYPE_OBJECT); + pa_operation * oper = (pa_operation *)goper; + return oper; +} + +pa_operation* +pa_context_get_sink_info_list (pa_context *c, pa_sink_info_cb_t cb, void *userdata) +{ + /* Only have one today, so this is the same */ + return pa_context_get_sink_info_by_name(c, "default-sink", cb, userdata); +} + + +pa_context_get_sink_input_info +pa_context_get_source_info_by_name +pa_context_get_source_output_info +pa_context_get_state + +pa_context_set_sink_mute_by_index +pa_context_set_sink_volume_by_index +pa_context_set_source_volume_by_name +pa_context_set_subscribe_callback +pa_context_subscribe + +pa_cvolume_init +pa_cvolume_max +pa_cvolume_scale +pa_cvolume_set + +/* ******************************* + * glib-mainloop.h + * *******************************/ + +struct pa_glib_mainloop { + GMainContext * context; +}; + +struct pa_mainloop_api mock_mainloop = { 0 }; + +pa_mainloop_api * +pa_glib_mainloop_get_api (pa_glib_mainloop * g) +{ + return &mock_mainloop; +} + +pa_glib_mainloop * +pa_glib_mainloop_new (GMainContext * c) +{ + pa_glib_mainloop * loop = g_new0(pa_glib_mainloop, 1); + + if (c == NULL) + loop->context = g_main_context_default(); + else + loop->context = c; + + g_main_context_ref(loop->context); + return loop; +} + +void +pa_glib_mainloop_free (pa_glib_mainloop * g) +{ + g_main_context_unref(g->context); + g_free(g); +} + +/* ******************************* + * operation.h + * *******************************/ + +void +pa_operation_unref (pa_operation * operation) +{ + g_return_if_fail(G_IS_OBJECT(operation)); + g_object_unref(G_OBJECT(operation)); +} + +pa_proplist_free +pa_proplist_gets +pa_proplist_new +pa_proplist_sets + +pa_strerror + +pa_sw_volume_from_dB + -- cgit v1.2.3 From b7b3a850299741e61c88e0471fdb797ae77c70cc Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 26 Jan 2015 15:26:28 -0600 Subject: Checkpoint, need some refactoring --- tests/pa-mock.c | 253 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 235 insertions(+), 18 deletions(-) diff --git a/tests/pa-mock.c b/tests/pa-mock.c index 6f4e6ad..906e3de 100644 --- a/tests/pa-mock.c +++ b/tests/pa-mock.c @@ -7,15 +7,18 @@ #define G_LOG_DOMAIN "PA-Mock" G_DEFINE_QUARK("pa-mock-state-cb-list", state_cb); -typedef struct { - pa_context_notify_cb_t cb; - gpointer user_data; -} state_cb_t; +G_DEFINE_QUARK("pa-mock-subscribe-callback", subscribe_cb); +G_DEFINE_QUARK("pa-mock-subscribe-mask", subscribe_mask); /* ******************************* * context.h * *******************************/ +typedef struct { + pa_context_notify_cb_t cb; + gpointer user_data; +} state_cb_t; + static void context_weak_cb (gpointer user_data, GObject * oldobj) { @@ -223,22 +226,128 @@ pa_context_get_sink_info_list (pa_context *c, pa_sink_info_cb_t cb, void *userda return pa_context_get_sink_info_by_name(c, "default-sink", cb, userdata); } +typedef struct { + pa_sink_info_cb_t cb; + gpointer userdata; + pa_context * context; +} get_sink_input_info_t; + +static void +get_sink_info_free (gpointer data) +{ + get_sink_input_info_t * info = (get_sink_info_t *)data; + pa_context_unref(info->context); + g_free(info); +} + +static gboolean +get_sink_info_cb (gpointer data) +{ + pa_sink_input_info sink = { + .name = "default-sink" + }; + get_sink_input_info_t * info = (get_sink_input_info_t *)data; + + info->cb(info->context, &sink, info->userdata); + + return G_SOURCE_REMOVE; +} + +pa_operation * +pa_context_get_sink_input_info (pa_context *c, pa_sink_input_info_cb_t cb, void * userdata) +{ + g_return_val_if_fail(G_IS_OBJECT(c), NULL); + g_return_val_if_fail(cb != NULL, NULL); + + get_sink_input_info_t * info = g_new(get_sink_input_info_t, 1); + info->cb = cb; + info->userdata = userdata; + info->context = g_objet_ref(c); + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + get_sink_input_info_cb, + info, + get_sink_input_info_free); + + GObject * goper = g_object_new(G_TYPE_OBJECT); + pa_operation * oper = (pa_operation *)goper; + return oper; +} -pa_context_get_sink_input_info +#if 0 pa_context_get_source_info_by_name pa_context_get_source_output_info -pa_context_get_state pa_context_set_sink_mute_by_index pa_context_set_sink_volume_by_index pa_context_set_source_volume_by_name -pa_context_set_subscribe_callback -pa_context_subscribe +#endif + +/* ******************************* + * subscribe.h + * *******************************/ + +typedef struct { + pa_context_subscribe_cb_t cb; + gointer userdata; + GObject * context; + pa_subscription_mask_t mask; +} subscribe_mask_t; + +static void +subscribe_mask_free (gpointer data) +{ + subscribe_mask_t * mask_data = (subscribe_mask_t *)data; + g_object_unref(mask_data->context); + g_free(mask_data); +} + +static gboolean +subscribe_mask_cb (gpointer data) +{ + subscribe_mask_t * mask_data = (subscribe_mask_t *)data; + g_object_set_qdata(mask_data->context, subscribe_mask_quark, GINT_TO_POINTER(mask_data->mask)); + mask_data->cb(mask_data->context, 1, mask_data->userdata); + return G_SOURCE_REMOVE; +} + +pa_operation * +pa_context_subscribe (pa_context * c, pa_subscription_mask_t mask, pa_context_success_cb_t callback, void * userdata) +{ + g_return_if_fail(G_IS_OBJECT(c)); + + subscribe_mask_t * data = g_new0(subscribe_mask_t, 1); + data->cb = callback; + data->userdata = userdata; + data->context = g_object_ref(G_OBJECT(c)); + data->mask = mask; -pa_cvolume_init -pa_cvolume_max -pa_cvolume_scale -pa_cvolume_set + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + subscribe_mask_cb, + data, + subscribe_mask_free); + + GObject * goper = g_object_new(G_TYPE_OBJECT); + pa_operation * oper = (pa_operation *)goper; + return oper; +} + +typedef struct { + pa_context_subscribe_cb_t cb; + gointer userdata; +} subscribe_cb_t; + +void +pa_context_set_subscribe_callback (pa_context * c, pa_context_subscribe_cb_t callback, void * userdata) +{ + g_return_if_fail(G_IS_OBJECT(c)); + + subscribe_cb_t * sub = g_new0(subscribe_cb_t, 1); + sub->cb = callback; + sub->userdata = userdata; + + g_object_set_qdata_full(c, subscribe_cb_quark, sub, g_free); +} /* ******************************* * glib-mainloop.h @@ -288,12 +397,120 @@ pa_operation_unref (pa_operation * operation) g_object_unref(G_OBJECT(operation)); } -pa_proplist_free -pa_proplist_gets -pa_proplist_new -pa_proplist_sets +/* ******************************* + * proplist.h + * *******************************/ + +pa_proplist * +pa_proplist_new (void) +{ + return (pa_proplist *)g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); +} + +void +pa_proplist_free (pa_proplist * p) +{ + g_return_if_fail(p != NULL); + g_hash_table_destroy((GHashTable *)p); +} + +const char * +pa_proplist_gets (pa_proplist * p, const char * key) +{ + g_return_val_if_fail(p != NULL, NULL); + g_return_val_if_fail(key != NULL, NULL); + return g_hash_table_lookup((GHashTable *)p, key); +} + +int +pa_proplist_sets (pa_proplist *p, const char * key, const char * value) +{ + g_return_val_if_fail(p != NULL, -1); + g_return_val_if_fail(key != NULL, -1); + g_return_val_if_fail(value != NULL, -1); -pa_strerror + g_hash_table_insert((GHashTable *)p, g_strdup(key), g_strdup(value)); + return 0; +} -pa_sw_volume_from_dB +/* ******************************* + * error.h + * *******************************/ + +const char * +pa_strerror (int error) +{ + return "This is error text"; +} + +/* ******************************* + * volume.h + * *******************************/ + +pa_volume_t +pa_sw_volume_from_dB (double f) +{ + double linear = pow(10.0, f / 20.0); + return (pa_volume_t) PA_CLAMP_VOLUME((uint64_t) lround(cbrt(linear) * PA_VOLUME_NORM)); +} + +pa_cvolume * +pa_cvolume_init (pa_cvolume * cvol) +{ + g_return_val_if_fail(cvol != NULL, NULL); + + cvol->channels = 0; + + unsigned int i; + for (i = 0; i < PA_CHANNELS_MAX; i++) + cvol->values[i] = PA_VOLUME_INVALID; + + return cvol; +} + +pa_cvolume * +pa_cvolume_set (pa_cvolume * cvol, unsigned channels, pa_volume_t volume) +{ + g_return_val_if_fail(cvol != NULL, NULL); + g_return_val_if_fail(channels > 0, NULL); + g_return_val_if_fail(channels <= PA_CHANNELS_MAX, NULL); + + cvol->channels = channels; + + unsigned int i; + for (i = 0; i < channels; i++) + cvol->values[i] = PA_CLAMP_VOLUME(volume); + + return cvol; +} + +pa_volume_t +pa_cvolume_max (const pa_cvolume * cvol) +{ + g_return_val_if_fail(cvol != NULL, NULL); + pa_volume_t max = PA_VOLUME_MUTED; + + unsigned int i; + for (i = 0; i < cvol->channels; i++) + max = MAX(max, cvol->values[i]); + + return max; +} + +pa_cvolume * +pa_cvolume_scale (pa_cvolume * cvol, pa_volume_t max) +{ + g_return_val_if_fail(cvol != NULL, NULL); + + pa_volume_t originalmax = pa_cvolume_max(cvol); + + if (originalmax <= PA_VOLUME_MUTED) + return pa_cvolume_set(cvol, cvol->channels, max); + + unsigned int i; + for (i = 0; i < channels; i++) + cvol->values[i] = PA_CLAMP_VOLUME( (cvol->values[i] * max) / originalmax ); + + return cvol; +} -- cgit v1.2.3 From 7200c1ba22c4c8155af58784b1dacdc0a06cf93a Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 26 Jan 2015 16:16:41 -0600 Subject: All the code 'written' --- tests/pa-mock.c | 247 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 240 insertions(+), 7 deletions(-) diff --git a/tests/pa-mock.c b/tests/pa-mock.c index 906e3de..20fced9 100644 --- a/tests/pa-mock.c +++ b/tests/pa-mock.c @@ -274,14 +274,247 @@ pa_context_get_sink_input_info (pa_context *c, pa_sink_input_info_cb_t cb, void return oper; } -#if 0 -pa_context_get_source_info_by_name -pa_context_get_source_output_info +typedef struct { + pa_source_info_cb_t cb; + gpointer userdata; + pa_context * context; +} get_source_info_t; -pa_context_set_sink_mute_by_index -pa_context_set_sink_volume_by_index -pa_context_set_source_volume_by_name -#endif +static void +get_source_info_free (gpointer data) +{ + get_source_info_t * info = (get_source_info_t *)data; + g_object_unref(info->context); + g_free(info); +} + +static gboolean +get_source_info_cb (gpointer data) +{ + pa_source_info source = { + .name = "default-source" + }; + get_source_info_t * info = (get_source_info_t *)data; + + info->cb(info->context, &source, info->userdata); + + return G_SOURCE_REMOVE; +} + +pa_operation* +pa_context_get_source_info_by_name (pa_context *c, const char * name, pa_source_info_cb_t cb, void *userdata) +{ + g_return_val_if_fail(G_IS_OBJECT(c), NULL); + g_return_val_if_fail(cb != NULL, NULL); + + get_source_info_t * info = g_new(get_source_info_t, 1); + info->cb = cb; + info->userdata = userdata; + info->context = g_objet_ref(c); + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + get_source_info_cb, + info, + get_source_info_free); + + GObject * goper = g_object_new(G_TYPE_OBJECT); + pa_operation * oper = (pa_operation *)goper; + return oper; +} + +typedef struct { + pa_source_output_info_cb_t cb; + gpointer userdata; + pa_context * context; +} get_source_output_t; + +static void +get_source_output_free (gpointer data) +{ + get_source_output_t * info = (get_source_output_t *)data; + g_object_unref(info->context); + g_free(info); +} + +static gboolean +get_source_output_cb (gpointer data) +{ + pa_source_output_info source = { + .name = "default-source" + }; + get_source_output_t * info = (get_source_output_t *)data; + + info->cb(info->context, &source, info->userdata); + + return G_SOURCE_REMOVE; +} + +pa_operation* +pa_context_get_source_output_info (pa_context *c, uint32_t idx, pa_source_output_info_cb_t cb, void *userdata) +{ + g_return_val_if_fail(G_IS_OBJECT(c), NULL); + g_return_val_if_fail(cb != NULL, NULL); + + get_source_output_t * info = g_new(get_source_output_t, 1); + info->cb = cb; + info->userdata = userdata; + info->context = g_objet_ref(c); + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + get_source_output_cb, + info, + get_source_output_free); + + GObject * goper = g_object_new(G_TYPE_OBJECT); + pa_operation * oper = (pa_operation *)goper; + return oper; +} + +typedef struct { + pa_context_success_cb_t cb; + gpointer userdata; + int mute; +} set_sink_mute_t; + +static void +set_sink_mute_free (gpointer data) +{ + set_sink_mute_t * mute = (set_sink_mute_t *)data; + g_object_unref(mute->context); + g_free(mute); +} + +static gboolean +set_sink_mute_cb (gpointer data) +{ + set_sink_mute_t * mute = (set_sink_mute_t *)data; + + mute->cb(mute->context, 1, mute->userdata); + + return G_SOURCE_REMOVE; +} + +pa_operation* +pa_context_set_sink_mute_by_index (pa_context *c, uint32_t idx, int mute, pa_context_success_cb_t cb, void *userdata) +{ + g_return_val_if_fail(G_IS_OBJECT(c), NULL); + g_return_val_if_fail(cb != NULL, NULL); + + set_sink_mute_t * data = g_new(set_sink_mute_t, 1); + data->cb = cb; + data->userdata = userdata; + data->context = g_objet_ref(c); + data->mute = mute; + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + set_sink_mute_cb, + data, + set_sink_mute_free); + + GObject * goper = g_object_new(G_TYPE_OBJECT); + pa_operation * oper = (pa_operation *)goper; + return oper; +} + +typedef struct { + pa_context_success_cb_t cb; + gpointer userdata; + pa_cvolume cvol; +} set_sink_volume_t; + +static void +set_sink_volume_free (gpointer data) +{ + set_sink_volume_t * vol = (set_sink_volume_t *)data; + g_object_unref(vol->context); + g_free(vol); +} + +static gboolean +set_sink_volume_cb (gpointer data) +{ + set_sink_volume_t * vol = (set_sink_volume_t *)data; + + vol->cb(vol->context, 1, vol->userdata); + + return G_SOURCE_REMOVE; +} + +pa_operation* +pa_context_set_sink_volume_by_index (pa_context *c, uint32_t idx, const pa_cvolume * cvol, pa_context_success_cb_t cb, void *userdata) +{ + g_return_val_if_fail(G_IS_OBJECT(c), NULL); + g_return_val_if_fail(cb != NULL, NULL); + + set_sink_volume_t * data = g_new(set_sink_volume_t, 1); + data->cb = cb; + data->userdata = userdata; + data->context = g_objet_ref(c); + data->cvol.channels = cvol->channels; + + int i; + for (i = 0; i < cvol->channels; i++) + data->cvol.values[i] = cvol->values[i]; + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + set_sink_volume_cb, + data, + set_sink_volume_free); + + GObject * goper = g_object_new(G_TYPE_OBJECT); + pa_operation * oper = (pa_operation *)goper; + return oper; +} + +typedef struct { + pa_context_success_cb_t cb; + gpointer userdata; + pa_cvolume cvol; +} set_source_volume_t; + +static void +set_source_volume_free (gpointer data) +{ + set_source_volume_t * vol = (set_source_volume_t *)data; + g_object_unref(vol->context); + g_free(vol); +} + +static gboolean +set_source_volume_cb (gpointer data) +{ + set_source_volume_t * vol = (set_source_volume_t *)data; + + vol->cb(vol->context, 1, vol->userdata); + + return G_SOURCE_REMOVE; +} + +pa_operation* +pa_context_set_source_volume_by_name (pa_context *c, const char * name, const pa_cvolume * cvol, pa_context_success_cb_t cb, void *userdata) +{ + g_return_val_if_fail(G_IS_OBJECT(c), NULL); + g_return_val_if_fail(cb != NULL, NULL); + + set_source_volume_t * data = g_new(set_source_volume_t, 1); + data->cb = cb; + data->userdata = userdata; + data->context = g_objet_ref(c); + data->cvol.channels = cvol->channels; + + int i; + for (i = 0; i < cvol->channels; i++) + data->cvol.values[i] = cvol->values[i]; + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + set_source_volume_cb, + data, + set_source_volume_free); + + GObject * goper = g_object_new(G_TYPE_OBJECT); + pa_operation * oper = (pa_operation *)goper; + return oper; +} /* ******************************* * subscribe.h -- cgit v1.2.3 From d788cd8f6910a8ff17454cc54175815a122f0c63 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 26 Jan 2015 16:53:28 -0600 Subject: IT COMPILES! --- tests/CMakeLists.txt | 15 +++++++ tests/pa-mock.c | 119 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 90 insertions(+), 44 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ae68c45..5c37ea3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -82,6 +82,21 @@ target_link_libraries( include_directories(${CMAKE_CURRENT_BINARY_DIR}) +########################### +# Pulse Mock +########################### + +add_library( + pulse-mock + SHARED + pa-mock.c +) + +target_link_libraries( + pulse-mock + ${SOUNDSERVICE_LIBRARIES} +) + ########################### # Name Watch Test ########################### diff --git a/tests/pa-mock.c b/tests/pa-mock.c index 20fced9..b995712 100644 --- a/tests/pa-mock.c +++ b/tests/pa-mock.c @@ -1,5 +1,8 @@ -#include +#include +#include +#include +#include #ifdef G_LOG_DOMAIN #undef G_LOG_DOMAIN @@ -16,7 +19,7 @@ G_DEFINE_QUARK("pa-mock-subscribe-mask", subscribe_mask); typedef struct { pa_context_notify_cb_t cb; - gpointer user_data; + gpointer userdata; } state_cb_t; static void @@ -28,7 +31,7 @@ context_weak_cb (gpointer user_data, GObject * oldobj) pa_context * pa_context_new_with_proplist (pa_mainloop_api *mainloop, const char *name, pa_proplist *proplist) { - GObject * gctx = g_object_new(G_TYPE_OBJECT); + GObject * gctx = g_object_new(G_TYPE_OBJECT, NULL); pa_context * ctx = (pa_context *)gctx; g_debug("Creating new context: %p", ctx); @@ -43,10 +46,11 @@ pa_context_unref (pa_context *c) { g_object_unref(G_OBJECT(c)); } -void +pa_context * pa_context_ref (pa_context *c) { g_return_if_fail(G_IS_OBJECT(c)); g_object_ref(G_OBJECT(c)); + return c; } int @@ -89,9 +93,9 @@ pa_context_set_state_callback (pa_context *c, pa_context_notify_cb_t cb, void *u state_cb->cb = cb; state_cb->userdata = userdata; - GList * statelist = g_object_get_qdata(G_OBJECT(c), state_cb_quark); + GList * statelist = g_object_get_qdata(G_OBJECT(c), state_cb_quark()); statelist = g_list_append(statelist, state_cb); - g_object_set_qdata_full(G_OBJECT(c), state_cb_quark, state_cb, state_cb_list_destroy); + g_object_set_qdata_full(G_OBJECT(c), state_cb_quark(), state_cb, state_cb_list_destroy); } pa_context_state_t @@ -128,7 +132,6 @@ get_server_info_cb (gpointer data) .host_name = "host", .server_version = "1.2.3", .server_name = "server", - .sample_spec = , .default_sink_name = "default-sink", .default_source_name = "default-source", .cookie = 1234, @@ -152,14 +155,14 @@ pa_context_get_server_info (pa_context *c, pa_server_info_cb_t cb, void *userdat get_server_info_t * info = g_new(get_server_info_t, 1); info->cb = cb; info->userdata = userdata; - info->context = g_objet_ref(c); + info->context = g_object_ref(c); g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, get_server_info_cb, info, get_server_info_free); - GObject * goper = g_object_new(G_TYPE_OBJECT); + GObject * goper = g_object_new(G_TYPE_OBJECT, NULL); pa_operation * oper = (pa_operation *)goper; return oper; } @@ -180,7 +183,7 @@ get_sink_info_free (gpointer data) } static gboolean -get_info_info_cb (gpointer data) +get_sink_info_cb (gpointer data) { pa_sink_info sink = { .name = "default-sink", @@ -207,14 +210,14 @@ pa_context_get_sink_info_by_name (pa_context *c, const gchar * name, pa_sink_inf get_sink_info_t * info = g_new(get_sink_info_t, 1); info->cb = cb; info->userdata = userdata; - info->context = g_objet_ref(c); + info->context = g_object_ref(c); g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, get_sink_info_cb, info, get_sink_info_free); - GObject * goper = g_object_new(G_TYPE_OBJECT); + GObject * goper = g_object_new(G_TYPE_OBJECT, NULL); pa_operation * oper = (pa_operation *)goper; return oper; } @@ -227,34 +230,34 @@ pa_context_get_sink_info_list (pa_context *c, pa_sink_info_cb_t cb, void *userda } typedef struct { - pa_sink_info_cb_t cb; + pa_sink_input_info_cb_t cb; gpointer userdata; pa_context * context; } get_sink_input_info_t; static void -get_sink_info_free (gpointer data) +get_sink_input_info_free (gpointer data) { - get_sink_input_info_t * info = (get_sink_info_t *)data; + get_sink_input_info_t * info = (get_sink_input_info_t *)data; pa_context_unref(info->context); g_free(info); } static gboolean -get_sink_info_cb (gpointer data) +get_sink_input_info_cb (gpointer data) { pa_sink_input_info sink = { .name = "default-sink" }; get_sink_input_info_t * info = (get_sink_input_info_t *)data; - info->cb(info->context, &sink, info->userdata); + info->cb(info->context, &sink, 0, info->userdata); return G_SOURCE_REMOVE; } pa_operation * -pa_context_get_sink_input_info (pa_context *c, pa_sink_input_info_cb_t cb, void * userdata) +pa_context_get_sink_input_info (pa_context *c, uint32_t idx, pa_sink_input_info_cb_t cb, void * userdata) { g_return_val_if_fail(G_IS_OBJECT(c), NULL); g_return_val_if_fail(cb != NULL, NULL); @@ -262,14 +265,14 @@ pa_context_get_sink_input_info (pa_context *c, pa_sink_input_info_cb_t cb, void get_sink_input_info_t * info = g_new(get_sink_input_info_t, 1); info->cb = cb; info->userdata = userdata; - info->context = g_objet_ref(c); + info->context = g_object_ref(c); g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, get_sink_input_info_cb, info, get_sink_input_info_free); - GObject * goper = g_object_new(G_TYPE_OBJECT); + GObject * goper = g_object_new(G_TYPE_OBJECT, NULL); pa_operation * oper = (pa_operation *)goper; return oper; } @@ -296,7 +299,7 @@ get_source_info_cb (gpointer data) }; get_source_info_t * info = (get_source_info_t *)data; - info->cb(info->context, &source, info->userdata); + info->cb(info->context, &source, 0, info->userdata); return G_SOURCE_REMOVE; } @@ -310,14 +313,14 @@ pa_context_get_source_info_by_name (pa_context *c, const char * name, pa_source_ get_source_info_t * info = g_new(get_source_info_t, 1); info->cb = cb; info->userdata = userdata; - info->context = g_objet_ref(c); + info->context = g_object_ref(c); g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, get_source_info_cb, info, get_source_info_free); - GObject * goper = g_object_new(G_TYPE_OBJECT); + GObject * goper = g_object_new(G_TYPE_OBJECT, NULL); pa_operation * oper = (pa_operation *)goper; return oper; } @@ -344,7 +347,7 @@ get_source_output_cb (gpointer data) }; get_source_output_t * info = (get_source_output_t *)data; - info->cb(info->context, &source, info->userdata); + info->cb(info->context, &source, 0, info->userdata); return G_SOURCE_REMOVE; } @@ -358,14 +361,14 @@ pa_context_get_source_output_info (pa_context *c, uint32_t idx, pa_source_output get_source_output_t * info = g_new(get_source_output_t, 1); info->cb = cb; info->userdata = userdata; - info->context = g_objet_ref(c); + info->context = g_object_ref(c); g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, get_source_output_cb, info, get_source_output_free); - GObject * goper = g_object_new(G_TYPE_OBJECT); + GObject * goper = g_object_new(G_TYPE_OBJECT, NULL); pa_operation * oper = (pa_operation *)goper; return oper; } @@ -373,6 +376,7 @@ pa_context_get_source_output_info (pa_context *c, uint32_t idx, pa_source_output typedef struct { pa_context_success_cb_t cb; gpointer userdata; + pa_context * context; int mute; } set_sink_mute_t; @@ -403,7 +407,7 @@ pa_context_set_sink_mute_by_index (pa_context *c, uint32_t idx, int mute, pa_con set_sink_mute_t * data = g_new(set_sink_mute_t, 1); data->cb = cb; data->userdata = userdata; - data->context = g_objet_ref(c); + data->context = g_object_ref(c); data->mute = mute; g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, @@ -411,7 +415,7 @@ pa_context_set_sink_mute_by_index (pa_context *c, uint32_t idx, int mute, pa_con data, set_sink_mute_free); - GObject * goper = g_object_new(G_TYPE_OBJECT); + GObject * goper = g_object_new(G_TYPE_OBJECT, NULL); pa_operation * oper = (pa_operation *)goper; return oper; } @@ -419,6 +423,7 @@ pa_context_set_sink_mute_by_index (pa_context *c, uint32_t idx, int mute, pa_con typedef struct { pa_context_success_cb_t cb; gpointer userdata; + pa_context * context; pa_cvolume cvol; } set_sink_volume_t; @@ -449,7 +454,7 @@ pa_context_set_sink_volume_by_index (pa_context *c, uint32_t idx, const pa_cvolu set_sink_volume_t * data = g_new(set_sink_volume_t, 1); data->cb = cb; data->userdata = userdata; - data->context = g_objet_ref(c); + data->context = g_object_ref(c); data->cvol.channels = cvol->channels; int i; @@ -461,7 +466,7 @@ pa_context_set_sink_volume_by_index (pa_context *c, uint32_t idx, const pa_cvolu data, set_sink_volume_free); - GObject * goper = g_object_new(G_TYPE_OBJECT); + GObject * goper = g_object_new(G_TYPE_OBJECT, NULL); pa_operation * oper = (pa_operation *)goper; return oper; } @@ -469,6 +474,7 @@ pa_context_set_sink_volume_by_index (pa_context *c, uint32_t idx, const pa_cvolu typedef struct { pa_context_success_cb_t cb; gpointer userdata; + pa_context * context; pa_cvolume cvol; } set_source_volume_t; @@ -499,7 +505,7 @@ pa_context_set_source_volume_by_name (pa_context *c, const char * name, const pa set_source_volume_t * data = g_new(set_source_volume_t, 1); data->cb = cb; data->userdata = userdata; - data->context = g_objet_ref(c); + data->context = g_object_ref(c); data->cvol.channels = cvol->channels; int i; @@ -511,7 +517,7 @@ pa_context_set_source_volume_by_name (pa_context *c, const char * name, const pa data, set_source_volume_free); - GObject * goper = g_object_new(G_TYPE_OBJECT); + GObject * goper = g_object_new(G_TYPE_OBJECT, NULL); pa_operation * oper = (pa_operation *)goper; return oper; } @@ -521,9 +527,9 @@ pa_context_set_source_volume_by_name (pa_context *c, const char * name, const pa * *******************************/ typedef struct { - pa_context_subscribe_cb_t cb; - gointer userdata; - GObject * context; + pa_context_success_cb_t cb; + gpointer userdata; + pa_context * context; pa_subscription_mask_t mask; } subscribe_mask_t; @@ -539,7 +545,7 @@ static gboolean subscribe_mask_cb (gpointer data) { subscribe_mask_t * mask_data = (subscribe_mask_t *)data; - g_object_set_qdata(mask_data->context, subscribe_mask_quark, GINT_TO_POINTER(mask_data->mask)); + g_object_set_qdata(G_OBJECT(mask_data->context), subscribe_mask_quark(), GINT_TO_POINTER(mask_data->mask)); mask_data->cb(mask_data->context, 1, mask_data->userdata); return G_SOURCE_REMOVE; } @@ -560,14 +566,14 @@ pa_context_subscribe (pa_context * c, pa_subscription_mask_t mask, pa_context_su data, subscribe_mask_free); - GObject * goper = g_object_new(G_TYPE_OBJECT); + GObject * goper = g_object_new(G_TYPE_OBJECT, NULL); pa_operation * oper = (pa_operation *)goper; return oper; } typedef struct { pa_context_subscribe_cb_t cb; - gointer userdata; + gpointer userdata; } subscribe_cb_t; void @@ -579,7 +585,7 @@ pa_context_set_subscribe_callback (pa_context * c, pa_context_subscribe_cb_t cal sub->cb = callback; sub->userdata = userdata; - g_object_set_qdata_full(c, subscribe_cb_quark, sub, g_free); + g_object_set_qdata_full(G_OBJECT(c), subscribe_cb_quark(), sub, g_free); } /* ******************************* @@ -684,7 +690,16 @@ pa_volume_t pa_sw_volume_from_dB (double f) { double linear = pow(10.0, f / 20.0); - return (pa_volume_t) PA_CLAMP_VOLUME((uint64_t) lround(cbrt(linear) * PA_VOLUME_NORM)); + + pa_volume_t calculated = lround(cbrt(linear) * PA_VOLUME_NORM); + + if (G_UNLIKELY(calculated > PA_VOLUME_MAX)) { + return PA_VOLUME_MAX; + } else if (G_UNLIKELY(calculated < PA_VOLUME_MUTED)) { + return PA_VOLUME_MUTED; + } else { + return calculated; + } } pa_cvolume * @@ -711,8 +726,15 @@ pa_cvolume_set (pa_cvolume * cvol, unsigned channels, pa_volume_t volume) cvol->channels = channels; unsigned int i; - for (i = 0; i < channels; i++) - cvol->values[i] = PA_CLAMP_VOLUME(volume); + for (i = 0; i < channels; i++) { + if (G_UNLIKELY(volume > PA_VOLUME_MAX)) { + cvol->values[i] = PA_VOLUME_MAX; + } else if (G_UNLIKELY(volume < PA_VOLUME_MUTED)) { + cvol->values[i] = PA_VOLUME_MUTED; + } else { + cvol->values[i] = volume; + } + } return cvol; } @@ -741,8 +763,17 @@ pa_cvolume_scale (pa_cvolume * cvol, pa_volume_t max) return pa_cvolume_set(cvol, cvol->channels, max); unsigned int i; - for (i = 0; i < channels; i++) - cvol->values[i] = PA_CLAMP_VOLUME( (cvol->values[i] * max) / originalmax ); + for (i = 0; i < cvol->channels; i++) { + pa_volume_t calculated = (cvol->values[i] * max) / originalmax; + + if (G_UNLIKELY(calculated > PA_VOLUME_MAX)) { + cvol->values[i] = PA_VOLUME_MAX; + } else if (G_UNLIKELY(calculated < PA_VOLUME_MUTED)) { + cvol->values[i] = PA_VOLUME_MUTED; + } else { + cvol->values[i] = calculated; + } + } return cvol; } -- cgit v1.2.3 From 26c4b845af442fd7968863929f4f62c8721fb687 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 27 Jan 2015 11:42:26 -0600 Subject: Linking and starting and failing! --- src/CMakeLists.txt | 2 +- tests/CMakeLists.txt | 21 ++++++++--- tests/volume-control-test.cc | 83 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 tests/volume-control-test.cc diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c18726c..b6f006a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -163,7 +163,6 @@ add_library( target_link_libraries( indicator-sound-service-lib - ${PULSEAUDIO_LIBRARIES} ${SOUNDSERVICE_LIBRARIES} ) @@ -187,6 +186,7 @@ set_target_properties( target_link_libraries( indicator-sound-service-bin indicator-sound-service-lib + ${PULSEAUDIO_LIBRARIES} ) ########################### diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5c37ea3..a9ec204 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -92,11 +92,6 @@ add_library( pa-mock.c ) -target_link_libraries( - pulse-mock - ${SOUNDSERVICE_LIBRARIES} -) - ########################### # Name Watch Test ########################### @@ -130,6 +125,22 @@ add_test(accounts-service-user-test-player accounts-service-user-test --gtest_filter=AccountsServiceUserTest.SetMediaPlayer ) +########################### +# Volume Control +########################### + +include_directories(${CMAKE_SOURCE_DIR}/src) +add_executable (volume-control-test volume-control-test.cc) +target_link_libraries ( + volume-control-test + indicator-sound-service-lib + pulse-mock + gtest + ${TEST_LIBRARIES} +) + +add_test(volume-control-test volume-control-test) + ########################### # Sound Menu ########################### diff --git a/tests/volume-control-test.cc b/tests/volume-control-test.cc new file mode 100644 index 0000000..9970241 --- /dev/null +++ b/tests/volume-control-test.cc @@ -0,0 +1,83 @@ +/* + * Copyright © 2014 Canonical Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * Authors: + * Ted Gould + */ + +#include +#include +#include + +extern "C" { +#include "indicator-sound-service.h" +} + +class VolumeControlTest : public ::testing::Test +{ + + protected: + DbusTestService * service = NULL; + GDBusConnection * session = NULL; + + virtual void SetUp() { + service = dbus_test_service_new(NULL); + dbus_test_service_start_tasks(service); + + session = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL); + ASSERT_NE(nullptr, session); + g_dbus_connection_set_exit_on_close(session, FALSE); + g_object_add_weak_pointer(G_OBJECT(session), (gpointer *)&session); + } + + virtual void TearDown() { + g_clear_object(&service); + + g_object_unref(session); + + unsigned int cleartry = 0; + while (session != NULL && cleartry < 100) { + loop(100); + cleartry++; + } + + ASSERT_EQ(nullptr, session); + } + + static gboolean timeout_cb (gpointer user_data) { + GMainLoop * loop = static_cast(user_data); + g_main_loop_quit(loop); + return G_SOURCE_REMOVE; + } + + void loop (unsigned int ms) { + GMainLoop * loop = g_main_loop_new(NULL, FALSE); + g_timeout_add(ms, timeout_cb, loop); + g_main_loop_run(loop); + g_main_loop_unref(loop); + } +}; + +TEST_F(VolumeControlTest, BasicObject) { + VolumeControl * control = volume_control_new(); + + /* Setup the PA backend */ + loop(100); + + /* Ready */ + EXPECT_TRUE(volume_control_get_ready(control)); + + g_clear_object(&control); +} -- cgit v1.2.3 From 4b73db41e3a3507f67ba2ead8da004b63d2e995a Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 27 Jan 2015 14:29:12 -0600 Subject: Enough state logic to get is 'going' --- tests/pa-mock.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 7 deletions(-) diff --git a/tests/pa-mock.c b/tests/pa-mock.c index b995712..bc4fb95 100644 --- a/tests/pa-mock.c +++ b/tests/pa-mock.c @@ -12,6 +12,15 @@ G_DEFINE_QUARK("pa-mock-state-cb-list", state_cb); G_DEFINE_QUARK("pa-mock-subscribe-callback", subscribe_cb); G_DEFINE_QUARK("pa-mock-subscribe-mask", subscribe_mask); +G_DEFINE_QUARK("pa-mock-current-state", state); +G_DEFINE_QUARK("pa-mock-future-state", state_future); + +/* ******************************* + * Prototypes + * *******************************/ + +static void set_state (pa_context * c, pa_context_state_t state); +static void queue_state (pa_context * context, pa_context_state_t state); /* ******************************* * context.h @@ -37,6 +46,8 @@ pa_context_new_with_proplist (pa_mainloop_api *mainloop, const char *name, pa_pr g_debug("Creating new context: %p", ctx); g_object_weak_ref(gctx, context_weak_cb, NULL); + set_state(ctx, PA_CONTEXT_UNCONNECTED); + return ctx; } @@ -58,6 +69,7 @@ pa_context_connect (pa_context *c, const char *server, pa_context_flags_t flags, { g_return_if_fail(G_IS_OBJECT(c)); g_debug("Context Connect"); + queue_state(c, PA_CONTEXT_READY); return 0; } @@ -66,6 +78,7 @@ pa_context_disconnect (pa_context *c) { g_return_if_fail(G_IS_OBJECT(c)); g_debug("Context Disconnect"); + queue_state(c, PA_CONTEXT_UNCONNECTED); } int @@ -76,6 +89,49 @@ pa_context_errno (pa_context *c) return 0; } +static void +set_state_cb (gpointer pcbdata, gpointer pcontext) +{ + pa_context * context = (pa_context *)pcontext; + state_cb_t * cbdata = (state_cb_t *)pcbdata; + + cbdata->cb(context, cbdata->userdata); +} + +static void +set_state (pa_context * c, pa_context_state_t state) +{ + pa_context_state_t oldstate = pa_context_get_state(c); + if (oldstate == state) + return; + + g_object_set_qdata(G_OBJECT(c), state_quark(), GINT_TO_POINTER(state)); + GList * statelist = g_object_get_qdata(G_OBJECT(c), state_cb_quark()); + g_list_foreach(statelist, set_state_cb, c); +} + +static gboolean +queue_state_cb (gpointer pcontext) +{ + pa_context * context = (pa_context *)pcontext; + pa_context_state_t state = GPOINTER_TO_INT(g_object_get_qdata(G_OBJECT(context), state_future_quark())); + + set_state(context, state); + + return G_SOURCE_REMOVE; +} + +static void +queue_state (pa_context * context, pa_context_state_t state) +{ + g_object_set_qdata(G_OBJECT(context), state_future_quark(), GINT_TO_POINTER(state)); + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + queue_state_cb, + g_object_ref(context), + g_object_unref); +} + static void state_cb_list_destroy (gpointer data) { @@ -95,7 +151,7 @@ pa_context_set_state_callback (pa_context *c, pa_context_notify_cb_t cb, void *u GList * statelist = g_object_get_qdata(G_OBJECT(c), state_cb_quark()); statelist = g_list_append(statelist, state_cb); - g_object_set_qdata_full(G_OBJECT(c), state_cb_quark(), state_cb, state_cb_list_destroy); + g_object_set_qdata_full(G_OBJECT(c), state_cb_quark(), statelist, state_cb_list_destroy); } pa_context_state_t @@ -103,7 +159,7 @@ pa_context_get_state (pa_context *c) { g_return_if_fail(G_IS_OBJECT(c)); - return PA_CONTEXT_READY; + return GPOINTER_TO_INT(g_object_get_qdata(G_OBJECT(c), state_quark())); } /* ******************************* @@ -185,13 +241,17 @@ get_sink_info_free (gpointer data) static gboolean get_sink_info_cb (gpointer data) { + pa_sink_port_info active_port = { + .name = "speaker" + }; pa_sink_info sink = { .name = "default-sink", .index = 0, .description = "Default Sink", .channel_map = { .channels = 0 - } + }, + .active_port = &active_port }; get_sink_info_t * info = (get_sink_info_t *)data; @@ -393,7 +453,8 @@ set_sink_mute_cb (gpointer data) { set_sink_mute_t * mute = (set_sink_mute_t *)data; - mute->cb(mute->context, 1, mute->userdata); + if (mute->cb != NULL) + mute->cb(mute->context, 1, mute->userdata); return G_SOURCE_REMOVE; } @@ -402,7 +463,6 @@ pa_operation* pa_context_set_sink_mute_by_index (pa_context *c, uint32_t idx, int mute, pa_context_success_cb_t cb, void *userdata) { g_return_val_if_fail(G_IS_OBJECT(c), NULL); - g_return_val_if_fail(cb != NULL, NULL); set_sink_mute_t * data = g_new(set_sink_mute_t, 1); data->cb = cb; @@ -546,7 +606,8 @@ subscribe_mask_cb (gpointer data) { subscribe_mask_t * mask_data = (subscribe_mask_t *)data; g_object_set_qdata(G_OBJECT(mask_data->context), subscribe_mask_quark(), GINT_TO_POINTER(mask_data->mask)); - mask_data->cb(mask_data->context, 1, mask_data->userdata); + if (mask_data->cb != NULL) + mask_data->cb(mask_data->context, 1, mask_data->userdata); return G_SOURCE_REMOVE; } @@ -720,7 +781,7 @@ pa_cvolume * pa_cvolume_set (pa_cvolume * cvol, unsigned channels, pa_volume_t volume) { g_return_val_if_fail(cvol != NULL, NULL); - g_return_val_if_fail(channels > 0, NULL); + g_warn_if_fail(channels > 0); g_return_val_if_fail(channels <= PA_CHANNELS_MAX, NULL); cvol->channels = channels; -- cgit v1.2.3 From ed54228025cc86f6affca2eafe61d83d234351d4 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Wed, 28 Jan 2015 09:41:22 -0600 Subject: Create a base object and move the state stuff over to it --- tests/CMakeLists.txt | 2 +- tests/pa-mock.c | 841 -------------------------------------------------- tests/pa-mock.cpp | 857 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 858 insertions(+), 842 deletions(-) delete mode 100644 tests/pa-mock.c create mode 100644 tests/pa-mock.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a9ec204..fb93aca 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -89,7 +89,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}) add_library( pulse-mock SHARED - pa-mock.c + pa-mock.cpp ) ########################### diff --git a/tests/pa-mock.c b/tests/pa-mock.c deleted file mode 100644 index bc4fb95..0000000 --- a/tests/pa-mock.c +++ /dev/null @@ -1,841 +0,0 @@ - -#include -#include -#include -#include - -#ifdef G_LOG_DOMAIN -#undef G_LOG_DOMAIN -#endif -#define G_LOG_DOMAIN "PA-Mock" - -G_DEFINE_QUARK("pa-mock-state-cb-list", state_cb); -G_DEFINE_QUARK("pa-mock-subscribe-callback", subscribe_cb); -G_DEFINE_QUARK("pa-mock-subscribe-mask", subscribe_mask); -G_DEFINE_QUARK("pa-mock-current-state", state); -G_DEFINE_QUARK("pa-mock-future-state", state_future); - -/* ******************************* - * Prototypes - * *******************************/ - -static void set_state (pa_context * c, pa_context_state_t state); -static void queue_state (pa_context * context, pa_context_state_t state); - -/* ******************************* - * context.h - * *******************************/ - -typedef struct { - pa_context_notify_cb_t cb; - gpointer userdata; -} state_cb_t; - -static void -context_weak_cb (gpointer user_data, GObject * oldobj) -{ - g_debug("Finalizing context: %p", oldobj); -} - -pa_context * -pa_context_new_with_proplist (pa_mainloop_api *mainloop, const char *name, pa_proplist *proplist) -{ - GObject * gctx = g_object_new(G_TYPE_OBJECT, NULL); - pa_context * ctx = (pa_context *)gctx; - - g_debug("Creating new context: %p", ctx); - g_object_weak_ref(gctx, context_weak_cb, NULL); - - set_state(ctx, PA_CONTEXT_UNCONNECTED); - - return ctx; -} - -void -pa_context_unref (pa_context *c) { - g_return_if_fail(G_IS_OBJECT(c)); - g_object_unref(G_OBJECT(c)); -} - -pa_context * -pa_context_ref (pa_context *c) { - g_return_if_fail(G_IS_OBJECT(c)); - g_object_ref(G_OBJECT(c)); - return c; -} - -int -pa_context_connect (pa_context *c, const char *server, pa_context_flags_t flags, const pa_spawn_api *api) -{ - g_return_if_fail(G_IS_OBJECT(c)); - g_debug("Context Connect"); - queue_state(c, PA_CONTEXT_READY); - return 0; -} - -void -pa_context_disconnect (pa_context *c) -{ - g_return_if_fail(G_IS_OBJECT(c)); - g_debug("Context Disconnect"); - queue_state(c, PA_CONTEXT_UNCONNECTED); -} - -int -pa_context_errno (pa_context *c) -{ - g_return_val_if_fail(G_IS_OBJECT(c), -1); - - return 0; -} - -static void -set_state_cb (gpointer pcbdata, gpointer pcontext) -{ - pa_context * context = (pa_context *)pcontext; - state_cb_t * cbdata = (state_cb_t *)pcbdata; - - cbdata->cb(context, cbdata->userdata); -} - -static void -set_state (pa_context * c, pa_context_state_t state) -{ - pa_context_state_t oldstate = pa_context_get_state(c); - if (oldstate == state) - return; - - g_object_set_qdata(G_OBJECT(c), state_quark(), GINT_TO_POINTER(state)); - GList * statelist = g_object_get_qdata(G_OBJECT(c), state_cb_quark()); - g_list_foreach(statelist, set_state_cb, c); -} - -static gboolean -queue_state_cb (gpointer pcontext) -{ - pa_context * context = (pa_context *)pcontext; - pa_context_state_t state = GPOINTER_TO_INT(g_object_get_qdata(G_OBJECT(context), state_future_quark())); - - set_state(context, state); - - return G_SOURCE_REMOVE; -} - -static void -queue_state (pa_context * context, pa_context_state_t state) -{ - g_object_set_qdata(G_OBJECT(context), state_future_quark(), GINT_TO_POINTER(state)); - - g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, - queue_state_cb, - g_object_ref(context), - g_object_unref); -} - -static void -state_cb_list_destroy (gpointer data) -{ - GList * statelist = (GList *)data; - g_list_free_full(statelist, g_free); -} - -void -pa_context_set_state_callback (pa_context *c, pa_context_notify_cb_t cb, void *userdata) -{ - g_return_if_fail(G_IS_OBJECT(c)); - g_return_if_fail(cb != NULL); - - state_cb_t * state_cb = g_new0(state_cb_t, 1); - state_cb->cb = cb; - state_cb->userdata = userdata; - - GList * statelist = g_object_get_qdata(G_OBJECT(c), state_cb_quark()); - statelist = g_list_append(statelist, state_cb); - g_object_set_qdata_full(G_OBJECT(c), state_cb_quark(), statelist, state_cb_list_destroy); -} - -pa_context_state_t -pa_context_get_state (pa_context *c) -{ - g_return_if_fail(G_IS_OBJECT(c)); - - return GPOINTER_TO_INT(g_object_get_qdata(G_OBJECT(c), state_quark())); -} - -/* ******************************* - * introspect.h - * *******************************/ - -typedef struct { - pa_server_info_cb_t cb; - gpointer userdata; - pa_context * context; -} get_server_info_t; - -static void -get_server_info_free (gpointer data) -{ - get_server_info_t * info = (get_server_info_t *)data; - g_object_unref(info->context); - g_free(info); -} - -static gboolean -get_server_info_cb (gpointer data) -{ - pa_server_info server = { - .user_name = "user", - .host_name = "host", - .server_version = "1.2.3", - .server_name = "server", - .default_sink_name = "default-sink", - .default_source_name = "default-source", - .cookie = 1234, - .channel_map = { - .channels = 0 - } - }; - get_server_info_t * info = (get_server_info_t *)data; - - info->cb(info->context, &server, info->userdata); - - return G_SOURCE_REMOVE; -} - -pa_operation* -pa_context_get_server_info (pa_context *c, pa_server_info_cb_t cb, void *userdata) -{ - g_return_val_if_fail(G_IS_OBJECT(c), NULL); - g_return_val_if_fail(cb != NULL, NULL); - - get_server_info_t * info = g_new(get_server_info_t, 1); - info->cb = cb; - info->userdata = userdata; - info->context = g_object_ref(c); - - g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, - get_server_info_cb, - info, - get_server_info_free); - - GObject * goper = g_object_new(G_TYPE_OBJECT, NULL); - pa_operation * oper = (pa_operation *)goper; - return oper; -} - -typedef struct { - pa_sink_info_cb_t cb; - gpointer userdata; - pa_context * context; - uint32_t index; -} get_sink_info_t; - -static void -get_sink_info_free (gpointer data) -{ - get_sink_info_t * info = (get_sink_info_t *)data; - g_object_unref(info->context); - g_free(info); -} - -static gboolean -get_sink_info_cb (gpointer data) -{ - pa_sink_port_info active_port = { - .name = "speaker" - }; - pa_sink_info sink = { - .name = "default-sink", - .index = 0, - .description = "Default Sink", - .channel_map = { - .channels = 0 - }, - .active_port = &active_port - }; - get_sink_info_t * info = (get_sink_info_t *)data; - - info->cb(info->context, &sink, 1, info->userdata); - - return G_SOURCE_REMOVE; -} - -pa_operation* -pa_context_get_sink_info_by_name (pa_context *c, const gchar * name, pa_sink_info_cb_t cb, void *userdata) -{ - g_return_val_if_fail(G_IS_OBJECT(c), NULL); - g_return_val_if_fail(g_strcmp0(name, "default-sink") == 0, NULL); - g_return_val_if_fail(cb != NULL, NULL); - - get_sink_info_t * info = g_new(get_sink_info_t, 1); - info->cb = cb; - info->userdata = userdata; - info->context = g_object_ref(c); - - g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, - get_sink_info_cb, - info, - get_sink_info_free); - - GObject * goper = g_object_new(G_TYPE_OBJECT, NULL); - pa_operation * oper = (pa_operation *)goper; - return oper; -} - -pa_operation* -pa_context_get_sink_info_list (pa_context *c, pa_sink_info_cb_t cb, void *userdata) -{ - /* Only have one today, so this is the same */ - return pa_context_get_sink_info_by_name(c, "default-sink", cb, userdata); -} - -typedef struct { - pa_sink_input_info_cb_t cb; - gpointer userdata; - pa_context * context; -} get_sink_input_info_t; - -static void -get_sink_input_info_free (gpointer data) -{ - get_sink_input_info_t * info = (get_sink_input_info_t *)data; - pa_context_unref(info->context); - g_free(info); -} - -static gboolean -get_sink_input_info_cb (gpointer data) -{ - pa_sink_input_info sink = { - .name = "default-sink" - }; - get_sink_input_info_t * info = (get_sink_input_info_t *)data; - - info->cb(info->context, &sink, 0, info->userdata); - - return G_SOURCE_REMOVE; -} - -pa_operation * -pa_context_get_sink_input_info (pa_context *c, uint32_t idx, pa_sink_input_info_cb_t cb, void * userdata) -{ - g_return_val_if_fail(G_IS_OBJECT(c), NULL); - g_return_val_if_fail(cb != NULL, NULL); - - get_sink_input_info_t * info = g_new(get_sink_input_info_t, 1); - info->cb = cb; - info->userdata = userdata; - info->context = g_object_ref(c); - - g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, - get_sink_input_info_cb, - info, - get_sink_input_info_free); - - GObject * goper = g_object_new(G_TYPE_OBJECT, NULL); - pa_operation * oper = (pa_operation *)goper; - return oper; -} - -typedef struct { - pa_source_info_cb_t cb; - gpointer userdata; - pa_context * context; -} get_source_info_t; - -static void -get_source_info_free (gpointer data) -{ - get_source_info_t * info = (get_source_info_t *)data; - g_object_unref(info->context); - g_free(info); -} - -static gboolean -get_source_info_cb (gpointer data) -{ - pa_source_info source = { - .name = "default-source" - }; - get_source_info_t * info = (get_source_info_t *)data; - - info->cb(info->context, &source, 0, info->userdata); - - return G_SOURCE_REMOVE; -} - -pa_operation* -pa_context_get_source_info_by_name (pa_context *c, const char * name, pa_source_info_cb_t cb, void *userdata) -{ - g_return_val_if_fail(G_IS_OBJECT(c), NULL); - g_return_val_if_fail(cb != NULL, NULL); - - get_source_info_t * info = g_new(get_source_info_t, 1); - info->cb = cb; - info->userdata = userdata; - info->context = g_object_ref(c); - - g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, - get_source_info_cb, - info, - get_source_info_free); - - GObject * goper = g_object_new(G_TYPE_OBJECT, NULL); - pa_operation * oper = (pa_operation *)goper; - return oper; -} - -typedef struct { - pa_source_output_info_cb_t cb; - gpointer userdata; - pa_context * context; -} get_source_output_t; - -static void -get_source_output_free (gpointer data) -{ - get_source_output_t * info = (get_source_output_t *)data; - g_object_unref(info->context); - g_free(info); -} - -static gboolean -get_source_output_cb (gpointer data) -{ - pa_source_output_info source = { - .name = "default-source" - }; - get_source_output_t * info = (get_source_output_t *)data; - - info->cb(info->context, &source, 0, info->userdata); - - return G_SOURCE_REMOVE; -} - -pa_operation* -pa_context_get_source_output_info (pa_context *c, uint32_t idx, pa_source_output_info_cb_t cb, void *userdata) -{ - g_return_val_if_fail(G_IS_OBJECT(c), NULL); - g_return_val_if_fail(cb != NULL, NULL); - - get_source_output_t * info = g_new(get_source_output_t, 1); - info->cb = cb; - info->userdata = userdata; - info->context = g_object_ref(c); - - g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, - get_source_output_cb, - info, - get_source_output_free); - - GObject * goper = g_object_new(G_TYPE_OBJECT, NULL); - pa_operation * oper = (pa_operation *)goper; - return oper; -} - -typedef struct { - pa_context_success_cb_t cb; - gpointer userdata; - pa_context * context; - int mute; -} set_sink_mute_t; - -static void -set_sink_mute_free (gpointer data) -{ - set_sink_mute_t * mute = (set_sink_mute_t *)data; - g_object_unref(mute->context); - g_free(mute); -} - -static gboolean -set_sink_mute_cb (gpointer data) -{ - set_sink_mute_t * mute = (set_sink_mute_t *)data; - - if (mute->cb != NULL) - mute->cb(mute->context, 1, mute->userdata); - - return G_SOURCE_REMOVE; -} - -pa_operation* -pa_context_set_sink_mute_by_index (pa_context *c, uint32_t idx, int mute, pa_context_success_cb_t cb, void *userdata) -{ - g_return_val_if_fail(G_IS_OBJECT(c), NULL); - - set_sink_mute_t * data = g_new(set_sink_mute_t, 1); - data->cb = cb; - data->userdata = userdata; - data->context = g_object_ref(c); - data->mute = mute; - - g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, - set_sink_mute_cb, - data, - set_sink_mute_free); - - GObject * goper = g_object_new(G_TYPE_OBJECT, NULL); - pa_operation * oper = (pa_operation *)goper; - return oper; -} - -typedef struct { - pa_context_success_cb_t cb; - gpointer userdata; - pa_context * context; - pa_cvolume cvol; -} set_sink_volume_t; - -static void -set_sink_volume_free (gpointer data) -{ - set_sink_volume_t * vol = (set_sink_volume_t *)data; - g_object_unref(vol->context); - g_free(vol); -} - -static gboolean -set_sink_volume_cb (gpointer data) -{ - set_sink_volume_t * vol = (set_sink_volume_t *)data; - - vol->cb(vol->context, 1, vol->userdata); - - return G_SOURCE_REMOVE; -} - -pa_operation* -pa_context_set_sink_volume_by_index (pa_context *c, uint32_t idx, const pa_cvolume * cvol, pa_context_success_cb_t cb, void *userdata) -{ - g_return_val_if_fail(G_IS_OBJECT(c), NULL); - g_return_val_if_fail(cb != NULL, NULL); - - set_sink_volume_t * data = g_new(set_sink_volume_t, 1); - data->cb = cb; - data->userdata = userdata; - data->context = g_object_ref(c); - data->cvol.channels = cvol->channels; - - int i; - for (i = 0; i < cvol->channels; i++) - data->cvol.values[i] = cvol->values[i]; - - g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, - set_sink_volume_cb, - data, - set_sink_volume_free); - - GObject * goper = g_object_new(G_TYPE_OBJECT, NULL); - pa_operation * oper = (pa_operation *)goper; - return oper; -} - -typedef struct { - pa_context_success_cb_t cb; - gpointer userdata; - pa_context * context; - pa_cvolume cvol; -} set_source_volume_t; - -static void -set_source_volume_free (gpointer data) -{ - set_source_volume_t * vol = (set_source_volume_t *)data; - g_object_unref(vol->context); - g_free(vol); -} - -static gboolean -set_source_volume_cb (gpointer data) -{ - set_source_volume_t * vol = (set_source_volume_t *)data; - - vol->cb(vol->context, 1, vol->userdata); - - return G_SOURCE_REMOVE; -} - -pa_operation* -pa_context_set_source_volume_by_name (pa_context *c, const char * name, const pa_cvolume * cvol, pa_context_success_cb_t cb, void *userdata) -{ - g_return_val_if_fail(G_IS_OBJECT(c), NULL); - g_return_val_if_fail(cb != NULL, NULL); - - set_source_volume_t * data = g_new(set_source_volume_t, 1); - data->cb = cb; - data->userdata = userdata; - data->context = g_object_ref(c); - data->cvol.channels = cvol->channels; - - int i; - for (i = 0; i < cvol->channels; i++) - data->cvol.values[i] = cvol->values[i]; - - g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, - set_source_volume_cb, - data, - set_source_volume_free); - - GObject * goper = g_object_new(G_TYPE_OBJECT, NULL); - pa_operation * oper = (pa_operation *)goper; - return oper; -} - -/* ******************************* - * subscribe.h - * *******************************/ - -typedef struct { - pa_context_success_cb_t cb; - gpointer userdata; - pa_context * context; - pa_subscription_mask_t mask; -} subscribe_mask_t; - -static void -subscribe_mask_free (gpointer data) -{ - subscribe_mask_t * mask_data = (subscribe_mask_t *)data; - g_object_unref(mask_data->context); - g_free(mask_data); -} - -static gboolean -subscribe_mask_cb (gpointer data) -{ - subscribe_mask_t * mask_data = (subscribe_mask_t *)data; - g_object_set_qdata(G_OBJECT(mask_data->context), subscribe_mask_quark(), GINT_TO_POINTER(mask_data->mask)); - if (mask_data->cb != NULL) - mask_data->cb(mask_data->context, 1, mask_data->userdata); - return G_SOURCE_REMOVE; -} - -pa_operation * -pa_context_subscribe (pa_context * c, pa_subscription_mask_t mask, pa_context_success_cb_t callback, void * userdata) -{ - g_return_if_fail(G_IS_OBJECT(c)); - - subscribe_mask_t * data = g_new0(subscribe_mask_t, 1); - data->cb = callback; - data->userdata = userdata; - data->context = g_object_ref(G_OBJECT(c)); - data->mask = mask; - - g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, - subscribe_mask_cb, - data, - subscribe_mask_free); - - GObject * goper = g_object_new(G_TYPE_OBJECT, NULL); - pa_operation * oper = (pa_operation *)goper; - return oper; -} - -typedef struct { - pa_context_subscribe_cb_t cb; - gpointer userdata; -} subscribe_cb_t; - -void -pa_context_set_subscribe_callback (pa_context * c, pa_context_subscribe_cb_t callback, void * userdata) -{ - g_return_if_fail(G_IS_OBJECT(c)); - - subscribe_cb_t * sub = g_new0(subscribe_cb_t, 1); - sub->cb = callback; - sub->userdata = userdata; - - g_object_set_qdata_full(G_OBJECT(c), subscribe_cb_quark(), sub, g_free); -} - -/* ******************************* - * glib-mainloop.h - * *******************************/ - -struct pa_glib_mainloop { - GMainContext * context; -}; - -struct pa_mainloop_api mock_mainloop = { 0 }; - -pa_mainloop_api * -pa_glib_mainloop_get_api (pa_glib_mainloop * g) -{ - return &mock_mainloop; -} - -pa_glib_mainloop * -pa_glib_mainloop_new (GMainContext * c) -{ - pa_glib_mainloop * loop = g_new0(pa_glib_mainloop, 1); - - if (c == NULL) - loop->context = g_main_context_default(); - else - loop->context = c; - - g_main_context_ref(loop->context); - return loop; -} - -void -pa_glib_mainloop_free (pa_glib_mainloop * g) -{ - g_main_context_unref(g->context); - g_free(g); -} - -/* ******************************* - * operation.h - * *******************************/ - -void -pa_operation_unref (pa_operation * operation) -{ - g_return_if_fail(G_IS_OBJECT(operation)); - g_object_unref(G_OBJECT(operation)); -} - -/* ******************************* - * proplist.h - * *******************************/ - -pa_proplist * -pa_proplist_new (void) -{ - return (pa_proplist *)g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); -} - -void -pa_proplist_free (pa_proplist * p) -{ - g_return_if_fail(p != NULL); - g_hash_table_destroy((GHashTable *)p); -} - -const char * -pa_proplist_gets (pa_proplist * p, const char * key) -{ - g_return_val_if_fail(p != NULL, NULL); - g_return_val_if_fail(key != NULL, NULL); - return g_hash_table_lookup((GHashTable *)p, key); -} - -int -pa_proplist_sets (pa_proplist *p, const char * key, const char * value) -{ - g_return_val_if_fail(p != NULL, -1); - g_return_val_if_fail(key != NULL, -1); - g_return_val_if_fail(value != NULL, -1); - - g_hash_table_insert((GHashTable *)p, g_strdup(key), g_strdup(value)); - return 0; -} - -/* ******************************* - * error.h - * *******************************/ - -const char * -pa_strerror (int error) -{ - return "This is error text"; -} - -/* ******************************* - * volume.h - * *******************************/ - -pa_volume_t -pa_sw_volume_from_dB (double f) -{ - double linear = pow(10.0, f / 20.0); - - pa_volume_t calculated = lround(cbrt(linear) * PA_VOLUME_NORM); - - if (G_UNLIKELY(calculated > PA_VOLUME_MAX)) { - return PA_VOLUME_MAX; - } else if (G_UNLIKELY(calculated < PA_VOLUME_MUTED)) { - return PA_VOLUME_MUTED; - } else { - return calculated; - } -} - -pa_cvolume * -pa_cvolume_init (pa_cvolume * cvol) -{ - g_return_val_if_fail(cvol != NULL, NULL); - - cvol->channels = 0; - - unsigned int i; - for (i = 0; i < PA_CHANNELS_MAX; i++) - cvol->values[i] = PA_VOLUME_INVALID; - - return cvol; -} - -pa_cvolume * -pa_cvolume_set (pa_cvolume * cvol, unsigned channels, pa_volume_t volume) -{ - g_return_val_if_fail(cvol != NULL, NULL); - g_warn_if_fail(channels > 0); - g_return_val_if_fail(channels <= PA_CHANNELS_MAX, NULL); - - cvol->channels = channels; - - unsigned int i; - for (i = 0; i < channels; i++) { - if (G_UNLIKELY(volume > PA_VOLUME_MAX)) { - cvol->values[i] = PA_VOLUME_MAX; - } else if (G_UNLIKELY(volume < PA_VOLUME_MUTED)) { - cvol->values[i] = PA_VOLUME_MUTED; - } else { - cvol->values[i] = volume; - } - } - - return cvol; -} - -pa_volume_t -pa_cvolume_max (const pa_cvolume * cvol) -{ - g_return_val_if_fail(cvol != NULL, NULL); - pa_volume_t max = PA_VOLUME_MUTED; - - unsigned int i; - for (i = 0; i < cvol->channels; i++) - max = MAX(max, cvol->values[i]); - - return max; -} - -pa_cvolume * -pa_cvolume_scale (pa_cvolume * cvol, pa_volume_t max) -{ - g_return_val_if_fail(cvol != NULL, NULL); - - pa_volume_t originalmax = pa_cvolume_max(cvol); - - if (originalmax <= PA_VOLUME_MUTED) - return pa_cvolume_set(cvol, cvol->channels, max); - - unsigned int i; - for (i = 0; i < cvol->channels; i++) { - pa_volume_t calculated = (cvol->values[i] * max) / originalmax; - - if (G_UNLIKELY(calculated > PA_VOLUME_MAX)) { - cvol->values[i] = PA_VOLUME_MAX; - } else if (G_UNLIKELY(calculated < PA_VOLUME_MUTED)) { - cvol->values[i] = PA_VOLUME_MUTED; - } else { - cvol->values[i] = calculated; - } - } - - return cvol; -} - diff --git a/tests/pa-mock.cpp b/tests/pa-mock.cpp new file mode 100644 index 0000000..5b433fe --- /dev/null +++ b/tests/pa-mock.cpp @@ -0,0 +1,857 @@ + +#include +#include +#include + +#include +#include +#include +#include + +#ifdef G_LOG_DOMAIN +#undef G_LOG_DOMAIN +#endif +#define G_LOG_DOMAIN "PA-Mock" + +G_DEFINE_QUARK("pa-mock-state-cb-list", state_cb); +G_DEFINE_QUARK("pa-mock-subscribe-callback", subscribe_cb); +G_DEFINE_QUARK("pa-mock-subscribe-mask", subscribe_mask); +G_DEFINE_QUARK("pa-mock-current-state", state); +G_DEFINE_QUARK("pa-mock-future-state", state_future); + +/* Core class of the PA Mock state */ +class PAMockContext { +public: + /* Accounting */ + std::atomic refcnt; + + /* State stuff */ + std::vector> stateCallbacks; + pa_context_state_t currentState; + pa_context_state_t futureState; + + /* Event stuff */ + std::vector> eventCallbacks; + pa_subscription_mask_t mask; + + PAMockContext () + : refcnt(1) + , currentState(PA_CONTEXT_UNCONNECTED) + , futureState(PA_CONTEXT_UNCONNECTED) + , mask(PA_SUBSCRIPTION_MASK_NULL) + { + g_debug("Creating Context: %p", this); + } + + ~PAMockContext () { + g_debug("Destroying Context: %p", this); + } + + /* Ref counting */ + void ref () { + refcnt++; + } + + void unref () { + refcnt--; + if (refcnt == 0) + delete this; + } + + /* C/C++ boundry */ + operator pa_context *() { + return reinterpret_cast(this); + } + + /* State Stuff */ + void setState (pa_context_state_t state) + { + if (state == currentState) + return; + + currentState = state; + for (auto callback : stateCallbacks) { + callback(); + } + } + + void idleOnce (std::function idleFunc) { + auto allocated = new std::function(idleFunc); + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + [](gpointer data) -> gboolean { + std::function * pidleFunc = reinterpret_cast *>(data); + (*pidleFunc)(); + return G_SOURCE_REMOVE; + }, + allocated, + [](gpointer data) -> void { + std::function * pidleFunc = reinterpret_cast *>(data); + delete pidleFunc; + }); + } + + void queueState (pa_context_state_t state) + { + idleOnce([this, state](){ + setState(state); + }); + } + + pa_context_state_t getState (void) + { + return currentState; + } + + void addStateCallback (std::function &callback) + { + stateCallbacks.push_back(callback); + } +}; + +struct pa_context { + operator PAMockContext * () { + return reinterpret_cast(this); + } +}; + +/* ******************************* + * context.h + * *******************************/ + +pa_context * +pa_context_new_with_proplist (pa_mainloop_api *mainloop, const char *name, pa_proplist *proplist) +{ + return *(new PAMockContext()); +} + +void +pa_context_unref (pa_context *c) { + reinterpret_cast(c)->unref(); +} + +pa_context * +pa_context_ref (pa_context *c) { + reinterpret_cast(c)->ref(); + return c; +} + +int +pa_context_connect (pa_context *c, const char *server, pa_context_flags_t flags, const pa_spawn_api *api) +{ + reinterpret_cast(c)->queueState(PA_CONTEXT_READY); + return 0; +} + +void +pa_context_disconnect (pa_context *c) +{ + reinterpret_cast(c)->queueState(PA_CONTEXT_UNCONNECTED); +} + +int +pa_context_errno (pa_context *c) +{ + return 0; +} + +void +pa_context_set_state_callback (pa_context *c, pa_context_notify_cb_t cb, void *userdata) +{ + std::function cppcb([c, cb, userdata]() { + cb(c, userdata); + }); + reinterpret_cast(c)->addStateCallback(cppcb); +} + +pa_context_state_t +pa_context_get_state (pa_context *c) +{ + return reinterpret_cast(c)->getState(); +} + +/* ******************************* + * introspect.h + * *******************************/ + +typedef struct { + pa_server_info_cb_t cb; + gpointer userdata; + pa_context * context; +} get_server_info_t; + +static void +get_server_info_free (gpointer data) +{ + get_server_info_t * info = (get_server_info_t *)data; + g_object_unref(info->context); + g_free(info); +} + +static gboolean +get_server_info_cb (gpointer data) +{ + pa_server_info server{ + .user_name = "user", + .host_name = "host", + .server_version = "1.2.3", + .server_name = "server", + .sample_spec = { + .format = PA_SAMPLE_U8, + .rate = 44100, + .channels = 1 + }, + .default_sink_name = "default-sink", + .default_source_name = "default-source", + .cookie = 1234, + .channel_map = { + .channels = 0 + } + }; + get_server_info_t * info = (get_server_info_t *)data; + + info->cb(info->context, &server, info->userdata); + + return G_SOURCE_REMOVE; +} + +pa_operation* +pa_context_get_server_info (pa_context *c, pa_server_info_cb_t cb, void *userdata) +{ + g_return_val_if_fail(G_IS_OBJECT(c), nullptr); + g_return_val_if_fail(cb != nullptr, nullptr); + + get_server_info_t * info = g_new(get_server_info_t, 1); + info->cb = cb; + info->userdata = userdata; + info->context = (pa_context *)g_object_ref(c); + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + get_server_info_cb, + info, + get_server_info_free); + + GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr); + pa_operation * oper = (pa_operation *)goper; + return oper; +} + +typedef struct { + pa_sink_info_cb_t cb; + gpointer userdata; + pa_context * context; + uint32_t index; +} get_sink_info_t; + +static void +get_sink_info_free (gpointer data) +{ + get_sink_info_t * info = (get_sink_info_t *)data; + g_object_unref(info->context); + g_free(info); +} + +static gboolean +get_sink_info_cb (gpointer data) +{ + #if 0 + pa_sink_port_info active_port = { + .name = "speaker" + }; + #endif + pa_sink_info sink = {0}; + #if 0 + .name = "default-sink", + .index = 0, + .description = "Default Sink", + .channel_map = { + .channels = 0 + }, + .active_port = &active_port + }; + #endif + get_sink_info_t * info = (get_sink_info_t *)data; + + info->cb(info->context, &sink, 1, info->userdata); + + return G_SOURCE_REMOVE; +} + +pa_operation* +pa_context_get_sink_info_by_name (pa_context *c, const gchar * name, pa_sink_info_cb_t cb, void *userdata) +{ + g_return_val_if_fail(G_IS_OBJECT(c), nullptr); + g_return_val_if_fail(g_strcmp0(name, "default-sink") == 0, nullptr); + g_return_val_if_fail(cb != nullptr, nullptr); + + get_sink_info_t * info = g_new(get_sink_info_t, 1); + info->cb = cb; + info->userdata = userdata; + info->context = (pa_context *)g_object_ref(c); + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + get_sink_info_cb, + info, + get_sink_info_free); + + GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr); + pa_operation * oper = (pa_operation *)goper; + return oper; +} + +pa_operation* +pa_context_get_sink_info_list (pa_context *c, pa_sink_info_cb_t cb, void *userdata) +{ + /* Only have one today, so this is the same */ + return pa_context_get_sink_info_by_name(c, "default-sink", cb, userdata); +} + +typedef struct { + pa_sink_input_info_cb_t cb; + gpointer userdata; + pa_context * context; +} get_sink_input_info_t; + +static void +get_sink_input_info_free (gpointer data) +{ + get_sink_input_info_t * info = (get_sink_input_info_t *)data; + pa_context_unref(info->context); + g_free(info); +} + +static gboolean +get_sink_input_info_cb (gpointer data) +{ + pa_sink_input_info sink = { 0 }; + + get_sink_input_info_t * info = (get_sink_input_info_t *)data; + + info->cb(info->context, &sink, 0, info->userdata); + + return G_SOURCE_REMOVE; +} + +pa_operation * +pa_context_get_sink_input_info (pa_context *c, uint32_t idx, pa_sink_input_info_cb_t cb, void * userdata) +{ + g_return_val_if_fail(G_IS_OBJECT(c), nullptr); + g_return_val_if_fail(cb != nullptr, nullptr); + + get_sink_input_info_t * info = g_new(get_sink_input_info_t, 1); + info->cb = cb; + info->userdata = userdata; + info->context = (pa_context *)g_object_ref(c); + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + get_sink_input_info_cb, + info, + get_sink_input_info_free); + + GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr); + pa_operation * oper = (pa_operation *)goper; + return oper; +} + +typedef struct { + pa_source_info_cb_t cb; + gpointer userdata; + pa_context * context; +} get_source_info_t; + +static void +get_source_info_free (gpointer data) +{ + get_source_info_t * info = (get_source_info_t *)data; + g_object_unref(info->context); + g_free(info); +} + +static gboolean +get_source_info_cb (gpointer data) +{ + pa_source_info source = { + .name = "default-source" + }; + get_source_info_t * info = (get_source_info_t *)data; + + info->cb(info->context, &source, 0, info->userdata); + + return G_SOURCE_REMOVE; +} + +pa_operation* +pa_context_get_source_info_by_name (pa_context *c, const char * name, pa_source_info_cb_t cb, void *userdata) +{ + g_return_val_if_fail(G_IS_OBJECT(c), nullptr); + g_return_val_if_fail(cb != nullptr, nullptr); + + get_source_info_t * info = g_new(get_source_info_t, 1); + info->cb = cb; + info->userdata = userdata; + info->context = (pa_context *)g_object_ref(c); + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + get_source_info_cb, + info, + get_source_info_free); + + GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr); + pa_operation * oper = (pa_operation *)goper; + return oper; +} + +typedef struct { + pa_source_output_info_cb_t cb; + gpointer userdata; + pa_context * context; +} get_source_output_t; + +static void +get_source_output_free (gpointer data) +{ + get_source_output_t * info = (get_source_output_t *)data; + g_object_unref(info->context); + g_free(info); +} + +static gboolean +get_source_output_cb (gpointer data) +{ + pa_source_output_info source = {0}; + source.name = "default source"; + + get_source_output_t * info = (get_source_output_t *)data; + + info->cb(info->context, &source, 0, info->userdata); + + return G_SOURCE_REMOVE; +} + +pa_operation* +pa_context_get_source_output_info (pa_context *c, uint32_t idx, pa_source_output_info_cb_t cb, void *userdata) +{ + g_return_val_if_fail(G_IS_OBJECT(c), nullptr); + g_return_val_if_fail(cb != nullptr, nullptr); + + get_source_output_t * info = g_new(get_source_output_t, 1); + info->cb = cb; + info->userdata = userdata; + info->context = (pa_context *)g_object_ref(c); + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + get_source_output_cb, + info, + get_source_output_free); + + GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr); + pa_operation * oper = (pa_operation *)goper; + return oper; +} + +typedef struct { + pa_context_success_cb_t cb; + gpointer userdata; + pa_context * context; + int mute; +} set_sink_mute_t; + +static void +set_sink_mute_free (gpointer data) +{ + set_sink_mute_t * mute = (set_sink_mute_t *)data; + g_object_unref(mute->context); + g_free(mute); +} + +static gboolean +set_sink_mute_cb (gpointer data) +{ + set_sink_mute_t * mute = (set_sink_mute_t *)data; + + if (mute->cb != nullptr) + mute->cb(mute->context, 1, mute->userdata); + + return G_SOURCE_REMOVE; +} + +pa_operation* +pa_context_set_sink_mute_by_index (pa_context *c, uint32_t idx, int mute, pa_context_success_cb_t cb, void *userdata) +{ + g_return_val_if_fail(G_IS_OBJECT(c), nullptr); + + set_sink_mute_t * data = g_new(set_sink_mute_t, 1); + data->cb = cb; + data->userdata = userdata; + data->context = pa_context_ref(c); + data->mute = mute; + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + set_sink_mute_cb, + data, + set_sink_mute_free); + + GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr); + pa_operation * oper = (pa_operation *)goper; + return oper; +} + +typedef struct { + pa_context_success_cb_t cb; + gpointer userdata; + pa_context * context; + pa_cvolume cvol; +} set_sink_volume_t; + +static void +set_sink_volume_free (gpointer data) +{ + set_sink_volume_t * vol = (set_sink_volume_t *)data; + g_object_unref(vol->context); + g_free(vol); +} + +static gboolean +set_sink_volume_cb (gpointer data) +{ + set_sink_volume_t * vol = (set_sink_volume_t *)data; + + vol->cb(vol->context, 1, vol->userdata); + + return G_SOURCE_REMOVE; +} + +pa_operation* +pa_context_set_sink_volume_by_index (pa_context *c, uint32_t idx, const pa_cvolume * cvol, pa_context_success_cb_t cb, void *userdata) +{ + g_return_val_if_fail(G_IS_OBJECT(c), nullptr); + g_return_val_if_fail(cb != nullptr, nullptr); + + set_sink_volume_t * data = g_new(set_sink_volume_t, 1); + data->cb = cb; + data->userdata = userdata; + data->context = pa_context_ref(c); + data->cvol.channels = cvol->channels; + + int i; + for (i = 0; i < cvol->channels; i++) + data->cvol.values[i] = cvol->values[i]; + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + set_sink_volume_cb, + data, + set_sink_volume_free); + + GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr); + pa_operation * oper = (pa_operation *)goper; + return oper; +} + +typedef struct { + pa_context_success_cb_t cb; + gpointer userdata; + pa_context * context; + pa_cvolume cvol; +} set_source_volume_t; + +static void +set_source_volume_free (gpointer data) +{ + set_source_volume_t * vol = (set_source_volume_t *)data; + g_object_unref(vol->context); + g_free(vol); +} + +static gboolean +set_source_volume_cb (gpointer data) +{ + set_source_volume_t * vol = (set_source_volume_t *)data; + + vol->cb(vol->context, 1, vol->userdata); + + return G_SOURCE_REMOVE; +} + +pa_operation* +pa_context_set_source_volume_by_name (pa_context *c, const char * name, const pa_cvolume * cvol, pa_context_success_cb_t cb, void *userdata) +{ + g_return_val_if_fail(G_IS_OBJECT(c), nullptr); + g_return_val_if_fail(cb != nullptr, nullptr); + + set_source_volume_t * data = g_new(set_source_volume_t, 1); + data->cb = cb; + data->userdata = userdata; + data->context = pa_context_ref(c); + data->cvol.channels = cvol->channels; + + int i; + for (i = 0; i < cvol->channels; i++) + data->cvol.values[i] = cvol->values[i]; + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + set_source_volume_cb, + data, + set_source_volume_free); + + GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr); + pa_operation * oper = (pa_operation *)goper; + return oper; +} + +/* ******************************* + * subscribe.h + * *******************************/ + +typedef struct { + pa_context_success_cb_t cb; + gpointer userdata; + pa_context * context; + pa_subscription_mask_t mask; +} subscribe_mask_t; + +static void +subscribe_mask_free (gpointer data) +{ + subscribe_mask_t * mask_data = (subscribe_mask_t *)data; + g_object_unref(mask_data->context); + g_free(mask_data); +} + +static gboolean +subscribe_mask_cb (gpointer data) +{ + subscribe_mask_t * mask_data = (subscribe_mask_t *)data; + g_object_set_qdata(G_OBJECT(mask_data->context), subscribe_mask_quark(), GINT_TO_POINTER(mask_data->mask)); + if (mask_data->cb != nullptr) + mask_data->cb(mask_data->context, 1, mask_data->userdata); + return G_SOURCE_REMOVE; +} + +pa_operation * +pa_context_subscribe (pa_context * c, pa_subscription_mask_t mask, pa_context_success_cb_t callback, void * userdata) +{ + g_return_val_if_fail(G_IS_OBJECT(c), nullptr); + + subscribe_mask_t * data = g_new0(subscribe_mask_t, 1); + data->cb = callback; + data->userdata = userdata; + data->context = pa_context_ref(c); + data->mask = mask; + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + subscribe_mask_cb, + data, + subscribe_mask_free); + + GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr); + pa_operation * oper = (pa_operation *)goper; + return oper; +} + +typedef struct { + pa_context_subscribe_cb_t cb; + gpointer userdata; +} subscribe_cb_t; + +void +pa_context_set_subscribe_callback (pa_context * c, pa_context_subscribe_cb_t callback, void * userdata) +{ + g_return_if_fail(G_IS_OBJECT(c)); + + subscribe_cb_t * sub = g_new0(subscribe_cb_t, 1); + sub->cb = callback; + sub->userdata = userdata; + + g_object_set_qdata_full(G_OBJECT(c), subscribe_cb_quark(), sub, g_free); +} + +/* ******************************* + * glib-mainloop.h + * *******************************/ + +struct pa_glib_mainloop { + GMainContext * context; +}; + +struct pa_mainloop_api mock_mainloop = { 0 }; + +pa_mainloop_api * +pa_glib_mainloop_get_api (pa_glib_mainloop * g) +{ + return &mock_mainloop; +} + +pa_glib_mainloop * +pa_glib_mainloop_new (GMainContext * c) +{ + pa_glib_mainloop * loop = g_new0(pa_glib_mainloop, 1); + + if (c == nullptr) + loop->context = g_main_context_default(); + else + loop->context = c; + + g_main_context_ref(loop->context); + return loop; +} + +void +pa_glib_mainloop_free (pa_glib_mainloop * g) +{ + g_main_context_unref(g->context); + g_free(g); +} + +/* ******************************* + * operation.h + * *******************************/ + +void +pa_operation_unref (pa_operation * operation) +{ + g_return_if_fail(G_IS_OBJECT(operation)); + g_object_unref(G_OBJECT(operation)); +} + +/* ******************************* + * proplist.h + * *******************************/ + +pa_proplist * +pa_proplist_new (void) +{ + return (pa_proplist *)g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); +} + +void +pa_proplist_free (pa_proplist * p) +{ + g_return_if_fail(p != nullptr); + g_hash_table_destroy((GHashTable *)p); +} + +const char * +pa_proplist_gets (pa_proplist * p, const char * key) +{ + g_return_val_if_fail(p != nullptr, nullptr); + g_return_val_if_fail(key != nullptr, nullptr); + return (const char *)g_hash_table_lookup((GHashTable *)p, key); +} + +int +pa_proplist_sets (pa_proplist *p, const char * key, const char * value) +{ + g_return_val_if_fail(p != nullptr, -1); + g_return_val_if_fail(key != nullptr, -1); + g_return_val_if_fail(value != nullptr, -1); + + g_hash_table_insert((GHashTable *)p, g_strdup(key), g_strdup(value)); + return 0; +} + +/* ******************************* + * error.h + * *******************************/ + +const char * +pa_strerror (int error) +{ + return "This is error text"; +} + +/* ******************************* + * volume.h + * *******************************/ + +pa_volume_t +pa_sw_volume_from_dB (double f) +{ + double linear = pow(10.0, f / 20.0); + + pa_volume_t calculated = lround(cbrt(linear) * PA_VOLUME_NORM); + + if (G_UNLIKELY(calculated > PA_VOLUME_MAX)) { + return PA_VOLUME_MAX; + } else if (G_UNLIKELY(calculated < PA_VOLUME_MUTED)) { + return PA_VOLUME_MUTED; + } else { + return calculated; + } +} + +pa_cvolume * +pa_cvolume_init (pa_cvolume * cvol) +{ + g_return_val_if_fail(cvol != nullptr, nullptr); + + cvol->channels = 0; + + unsigned int i; + for (i = 0; i < PA_CHANNELS_MAX; i++) + cvol->values[i] = PA_VOLUME_INVALID; + + return cvol; +} + +pa_cvolume * +pa_cvolume_set (pa_cvolume * cvol, unsigned channels, pa_volume_t volume) +{ + g_return_val_if_fail(cvol != nullptr, nullptr); + g_warn_if_fail(channels > 0); + g_return_val_if_fail(channels <= PA_CHANNELS_MAX, nullptr); + + cvol->channels = channels; + + unsigned int i; + for (i = 0; i < channels; i++) { + if (G_UNLIKELY(volume > PA_VOLUME_MAX)) { + cvol->values[i] = PA_VOLUME_MAX; + } else if (G_UNLIKELY(volume < PA_VOLUME_MUTED)) { + cvol->values[i] = PA_VOLUME_MUTED; + } else { + cvol->values[i] = volume; + } + } + + return cvol; +} + +pa_volume_t +pa_cvolume_max (const pa_cvolume * cvol) +{ + g_return_val_if_fail(cvol != nullptr, PA_VOLUME_MUTED); + pa_volume_t max = PA_VOLUME_MUTED; + + unsigned int i; + for (i = 0; i < cvol->channels; i++) + max = MAX(max, cvol->values[i]); + + return max; +} + +pa_cvolume * +pa_cvolume_scale (pa_cvolume * cvol, pa_volume_t max) +{ + g_return_val_if_fail(cvol != nullptr, nullptr); + + pa_volume_t originalmax = pa_cvolume_max(cvol); + + if (originalmax <= PA_VOLUME_MUTED) + return pa_cvolume_set(cvol, cvol->channels, max); + + unsigned int i; + for (i = 0; i < cvol->channels; i++) { + pa_volume_t calculated = (cvol->values[i] * max) / originalmax; + + if (G_UNLIKELY(calculated > PA_VOLUME_MAX)) { + cvol->values[i] = PA_VOLUME_MAX; + } else if (G_UNLIKELY(calculated < PA_VOLUME_MUTED)) { + cvol->values[i] = PA_VOLUME_MUTED; + } else { + cvol->values[i] = calculated; + } + } + + return cvol; +} + -- cgit v1.2.3 From 701b0a3df9c394814d8144e720cb63d9aa3efb0c Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Wed, 28 Jan 2015 16:22:19 -0600 Subject: Refactor out common code --- tests/pa-mock.cpp | 532 ++++++++++++------------------------------------------ 1 file changed, 113 insertions(+), 419 deletions(-) diff --git a/tests/pa-mock.cpp b/tests/pa-mock.cpp index 5b433fe..990b10f 100644 --- a/tests/pa-mock.cpp +++ b/tests/pa-mock.cpp @@ -13,12 +13,6 @@ #endif #define G_LOG_DOMAIN "PA-Mock" -G_DEFINE_QUARK("pa-mock-state-cb-list", state_cb); -G_DEFINE_QUARK("pa-mock-subscribe-callback", subscribe_cb); -G_DEFINE_QUARK("pa-mock-subscribe-mask", subscribe_mask); -G_DEFINE_QUARK("pa-mock-current-state", state); -G_DEFINE_QUARK("pa-mock-future-state", state_future); - /* Core class of the PA Mock state */ class PAMockContext { public: @@ -32,13 +26,13 @@ public: /* Event stuff */ std::vector> eventCallbacks; - pa_subscription_mask_t mask; + pa_subscription_mask_t eventMask; PAMockContext () : refcnt(1) , currentState(PA_CONTEXT_UNCONNECTED) , futureState(PA_CONTEXT_UNCONNECTED) - , mask(PA_SUBSCRIPTION_MASK_NULL) + , eventMask(PA_SUBSCRIPTION_MASK_NULL) { g_debug("Creating Context: %p", this); } @@ -107,11 +101,16 @@ public: { stateCallbacks.push_back(callback); } -}; -struct pa_context { - operator PAMockContext * () { - return reinterpret_cast(this); + /* Event Stuff */ + void setMask (pa_subscription_mask_t mask) + { + eventMask = mask; + } + + void addEventCallback (std::function &callback) + { + eventCallbacks.push_back(callback); } }; @@ -174,129 +173,68 @@ pa_context_get_state (pa_context *c) * introspect.h * *******************************/ -typedef struct { - pa_server_info_cb_t cb; - gpointer userdata; - pa_context * context; -} get_server_info_t; - -static void -get_server_info_free (gpointer data) -{ - get_server_info_t * info = (get_server_info_t *)data; - g_object_unref(info->context); - g_free(info); -} - -static gboolean -get_server_info_cb (gpointer data) -{ - pa_server_info server{ - .user_name = "user", - .host_name = "host", - .server_version = "1.2.3", - .server_name = "server", - .sample_spec = { - .format = PA_SAMPLE_U8, - .rate = 44100, - .channels = 1 - }, - .default_sink_name = "default-sink", - .default_source_name = "default-source", - .cookie = 1234, - .channel_map = { - .channels = 0 - } - }; - get_server_info_t * info = (get_server_info_t *)data; - - info->cb(info->context, &server, info->userdata); - - return G_SOURCE_REMOVE; -} - -pa_operation* -pa_context_get_server_info (pa_context *c, pa_server_info_cb_t cb, void *userdata) +static pa_operation * +dummy_operation (void) { - g_return_val_if_fail(G_IS_OBJECT(c), nullptr); - g_return_val_if_fail(cb != nullptr, nullptr); - - get_server_info_t * info = g_new(get_server_info_t, 1); - info->cb = cb; - info->userdata = userdata; - info->context = (pa_context *)g_object_ref(c); - - g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, - get_server_info_cb, - info, - get_server_info_free); - GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr); pa_operation * oper = (pa_operation *)goper; return oper; } -typedef struct { - pa_sink_info_cb_t cb; - gpointer userdata; - pa_context * context; - uint32_t index; -} get_sink_info_t; - -static void -get_sink_info_free (gpointer data) -{ - get_sink_info_t * info = (get_sink_info_t *)data; - g_object_unref(info->context); - g_free(info); -} - -static gboolean -get_sink_info_cb (gpointer data) +pa_operation* +pa_context_get_server_info (pa_context *c, pa_server_info_cb_t cb, void *userdata) { - #if 0 - pa_sink_port_info active_port = { - .name = "speaker" - }; - #endif - pa_sink_info sink = {0}; - #if 0 - .name = "default-sink", - .index = 0, - .description = "Default Sink", - .channel_map = { - .channels = 0 - }, - .active_port = &active_port - }; - #endif - get_sink_info_t * info = (get_sink_info_t *)data; + reinterpret_cast(c)->idleOnce( + [c, cb, userdata]() { + if (cb == nullptr) + return; - info->cb(info->context, &sink, 1, info->userdata); + pa_server_info server{ + .user_name = "user", + .host_name = "host", + .server_version = "1.2.3", + .server_name = "server", + .sample_spec = { + .format = PA_SAMPLE_U8, + .rate = 44100, + .channels = 1 + }, + .default_sink_name = "default-sink", + .default_source_name = "default-source", + .cookie = 1234, + .channel_map = { + .channels = 0 + } + }; + + cb(c, &server, userdata); + }); - return G_SOURCE_REMOVE; + return dummy_operation(); } pa_operation* pa_context_get_sink_info_by_name (pa_context *c, const gchar * name, pa_sink_info_cb_t cb, void *userdata) { - g_return_val_if_fail(G_IS_OBJECT(c), nullptr); - g_return_val_if_fail(g_strcmp0(name, "default-sink") == 0, nullptr); - g_return_val_if_fail(cb != nullptr, nullptr); + reinterpret_cast(c)->idleOnce( + [c, name, cb, userdata]() { + if (cb == nullptr) + return; - get_sink_info_t * info = g_new(get_sink_info_t, 1); - info->cb = cb; - info->userdata = userdata; - info->context = (pa_context *)g_object_ref(c); + pa_sink_port_info active_port = {0}; + active_port.name = "speaker"; - g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, - get_sink_info_cb, - info, - get_sink_info_free); + pa_sink_info sink = {0}; + sink.name = "default-sink"; + sink.index = 0; + sink.description = "Default Sink"; + sink.channel_map.channels = 0; + sink.active_port = &active_port; - GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr); - pa_operation * oper = (pa_operation *)goper; - return oper; + cb(c, &sink, 1, userdata); + }); + + return dummy_operation(); } pa_operation* @@ -306,363 +244,119 @@ pa_context_get_sink_info_list (pa_context *c, pa_sink_info_cb_t cb, void *userda return pa_context_get_sink_info_by_name(c, "default-sink", cb, userdata); } -typedef struct { - pa_sink_input_info_cb_t cb; - gpointer userdata; - pa_context * context; -} get_sink_input_info_t; - -static void -get_sink_input_info_free (gpointer data) -{ - get_sink_input_info_t * info = (get_sink_input_info_t *)data; - pa_context_unref(info->context); - g_free(info); -} - -static gboolean -get_sink_input_info_cb (gpointer data) -{ - pa_sink_input_info sink = { 0 }; - - get_sink_input_info_t * info = (get_sink_input_info_t *)data; - - info->cb(info->context, &sink, 0, info->userdata); - - return G_SOURCE_REMOVE; -} - pa_operation * pa_context_get_sink_input_info (pa_context *c, uint32_t idx, pa_sink_input_info_cb_t cb, void * userdata) { - g_return_val_if_fail(G_IS_OBJECT(c), nullptr); - g_return_val_if_fail(cb != nullptr, nullptr); - - get_sink_input_info_t * info = g_new(get_sink_input_info_t, 1); - info->cb = cb; - info->userdata = userdata; - info->context = (pa_context *)g_object_ref(c); - - g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, - get_sink_input_info_cb, - info, - get_sink_input_info_free); - - GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr); - pa_operation * oper = (pa_operation *)goper; - return oper; -} - -typedef struct { - pa_source_info_cb_t cb; - gpointer userdata; - pa_context * context; -} get_source_info_t; + reinterpret_cast(c)->idleOnce( + [c, idx, cb, userdata]() { + if (cb == nullptr) + return; -static void -get_source_info_free (gpointer data) -{ - get_source_info_t * info = (get_source_info_t *)data; - g_object_unref(info->context); - g_free(info); -} + pa_sink_input_info sink = { 0 }; -static gboolean -get_source_info_cb (gpointer data) -{ - pa_source_info source = { - .name = "default-source" - }; - get_source_info_t * info = (get_source_info_t *)data; - - info->cb(info->context, &source, 0, info->userdata); + cb(c, &sink, 1, userdata); + }); - return G_SOURCE_REMOVE; + return dummy_operation(); } pa_operation* pa_context_get_source_info_by_name (pa_context *c, const char * name, pa_source_info_cb_t cb, void *userdata) { - g_return_val_if_fail(G_IS_OBJECT(c), nullptr); - g_return_val_if_fail(cb != nullptr, nullptr); - - get_source_info_t * info = g_new(get_source_info_t, 1); - info->cb = cb; - info->userdata = userdata; - info->context = (pa_context *)g_object_ref(c); - - g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, - get_source_info_cb, - info, - get_source_info_free); - - GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr); - pa_operation * oper = (pa_operation *)goper; - return oper; -} - -typedef struct { - pa_source_output_info_cb_t cb; - gpointer userdata; - pa_context * context; -} get_source_output_t; - -static void -get_source_output_free (gpointer data) -{ - get_source_output_t * info = (get_source_output_t *)data; - g_object_unref(info->context); - g_free(info); -} - -static gboolean -get_source_output_cb (gpointer data) -{ - pa_source_output_info source = {0}; - source.name = "default source"; + reinterpret_cast(c)->idleOnce( + [c, name, cb, userdata]() { + if (cb == nullptr) + return; - get_source_output_t * info = (get_source_output_t *)data; + pa_source_info source = { + .name = "default-source" + }; - info->cb(info->context, &source, 0, info->userdata); + cb(c, &source, 1, userdata); + }); - return G_SOURCE_REMOVE; + return dummy_operation(); } pa_operation* pa_context_get_source_output_info (pa_context *c, uint32_t idx, pa_source_output_info_cb_t cb, void *userdata) { - g_return_val_if_fail(G_IS_OBJECT(c), nullptr); - g_return_val_if_fail(cb != nullptr, nullptr); - - get_source_output_t * info = g_new(get_source_output_t, 1); - info->cb = cb; - info->userdata = userdata; - info->context = (pa_context *)g_object_ref(c); - - g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, - get_source_output_cb, - info, - get_source_output_free); - - GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr); - pa_operation * oper = (pa_operation *)goper; - return oper; -} - -typedef struct { - pa_context_success_cb_t cb; - gpointer userdata; - pa_context * context; - int mute; -} set_sink_mute_t; - -static void -set_sink_mute_free (gpointer data) -{ - set_sink_mute_t * mute = (set_sink_mute_t *)data; - g_object_unref(mute->context); - g_free(mute); -} + reinterpret_cast(c)->idleOnce( + [c, idx, cb, userdata]() { + if (cb == nullptr) + return; -static gboolean -set_sink_mute_cb (gpointer data) -{ - set_sink_mute_t * mute = (set_sink_mute_t *)data; + pa_source_output_info source = {0}; + source.name = "default source"; - if (mute->cb != nullptr) - mute->cb(mute->context, 1, mute->userdata); + cb(c, &source, 1, userdata); + }); - return G_SOURCE_REMOVE; + return dummy_operation(); } pa_operation* pa_context_set_sink_mute_by_index (pa_context *c, uint32_t idx, int mute, pa_context_success_cb_t cb, void *userdata) { - g_return_val_if_fail(G_IS_OBJECT(c), nullptr); - - set_sink_mute_t * data = g_new(set_sink_mute_t, 1); - data->cb = cb; - data->userdata = userdata; - data->context = pa_context_ref(c); - data->mute = mute; - - g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, - set_sink_mute_cb, - data, - set_sink_mute_free); - - GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr); - pa_operation * oper = (pa_operation *)goper; - return oper; -} - -typedef struct { - pa_context_success_cb_t cb; - gpointer userdata; - pa_context * context; - pa_cvolume cvol; -} set_sink_volume_t; - -static void -set_sink_volume_free (gpointer data) -{ - set_sink_volume_t * vol = (set_sink_volume_t *)data; - g_object_unref(vol->context); - g_free(vol); -} - -static gboolean -set_sink_volume_cb (gpointer data) -{ - set_sink_volume_t * vol = (set_sink_volume_t *)data; - - vol->cb(vol->context, 1, vol->userdata); + reinterpret_cast(c)->idleOnce( + [c, idx, mute, cb, userdata]() { + if (cb != nullptr) + cb(c, 1, userdata); + }); - return G_SOURCE_REMOVE; + return dummy_operation(); } pa_operation* pa_context_set_sink_volume_by_index (pa_context *c, uint32_t idx, const pa_cvolume * cvol, pa_context_success_cb_t cb, void *userdata) { - g_return_val_if_fail(G_IS_OBJECT(c), nullptr); - g_return_val_if_fail(cb != nullptr, nullptr); - - set_sink_volume_t * data = g_new(set_sink_volume_t, 1); - data->cb = cb; - data->userdata = userdata; - data->context = pa_context_ref(c); - data->cvol.channels = cvol->channels; - - int i; - for (i = 0; i < cvol->channels; i++) - data->cvol.values[i] = cvol->values[i]; - - g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, - set_sink_volume_cb, - data, - set_sink_volume_free); - - GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr); - pa_operation * oper = (pa_operation *)goper; - return oper; -} - -typedef struct { - pa_context_success_cb_t cb; - gpointer userdata; - pa_context * context; - pa_cvolume cvol; -} set_source_volume_t; - -static void -set_source_volume_free (gpointer data) -{ - set_source_volume_t * vol = (set_source_volume_t *)data; - g_object_unref(vol->context); - g_free(vol); -} - -static gboolean -set_source_volume_cb (gpointer data) -{ - set_source_volume_t * vol = (set_source_volume_t *)data; - - vol->cb(vol->context, 1, vol->userdata); + reinterpret_cast(c)->idleOnce( + [c, idx, cvol, cb, userdata]() { + if (cb != nullptr) + cb(c, 1, userdata); + }); - return G_SOURCE_REMOVE; + return dummy_operation(); } pa_operation* pa_context_set_source_volume_by_name (pa_context *c, const char * name, const pa_cvolume * cvol, pa_context_success_cb_t cb, void *userdata) { - g_return_val_if_fail(G_IS_OBJECT(c), nullptr); - g_return_val_if_fail(cb != nullptr, nullptr); - - set_source_volume_t * data = g_new(set_source_volume_t, 1); - data->cb = cb; - data->userdata = userdata; - data->context = pa_context_ref(c); - data->cvol.channels = cvol->channels; - - int i; - for (i = 0; i < cvol->channels; i++) - data->cvol.values[i] = cvol->values[i]; - - g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, - set_source_volume_cb, - data, - set_source_volume_free); + reinterpret_cast(c)->idleOnce( + [c, name, cvol, cb, userdata]() { + if (cb != nullptr) + cb(c, 1, userdata); + }); - GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr); - pa_operation * oper = (pa_operation *)goper; - return oper; + return dummy_operation(); } /* ******************************* * subscribe.h * *******************************/ -typedef struct { - pa_context_success_cb_t cb; - gpointer userdata; - pa_context * context; - pa_subscription_mask_t mask; -} subscribe_mask_t; - -static void -subscribe_mask_free (gpointer data) -{ - subscribe_mask_t * mask_data = (subscribe_mask_t *)data; - g_object_unref(mask_data->context); - g_free(mask_data); -} - -static gboolean -subscribe_mask_cb (gpointer data) -{ - subscribe_mask_t * mask_data = (subscribe_mask_t *)data; - g_object_set_qdata(G_OBJECT(mask_data->context), subscribe_mask_quark(), GINT_TO_POINTER(mask_data->mask)); - if (mask_data->cb != nullptr) - mask_data->cb(mask_data->context, 1, mask_data->userdata); - return G_SOURCE_REMOVE; -} - pa_operation * pa_context_subscribe (pa_context * c, pa_subscription_mask_t mask, pa_context_success_cb_t callback, void * userdata) { - g_return_val_if_fail(G_IS_OBJECT(c), nullptr); - - subscribe_mask_t * data = g_new0(subscribe_mask_t, 1); - data->cb = callback; - data->userdata = userdata; - data->context = pa_context_ref(c); - data->mask = mask; - - g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, - subscribe_mask_cb, - data, - subscribe_mask_free); + reinterpret_cast(c)->idleOnce( + [c, mask, callback, userdata]() { + reinterpret_cast(c)->setMask(mask); + if (callback != nullptr) + callback(c, 1, userdata); + }); - GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr); - pa_operation * oper = (pa_operation *)goper; - return oper; + return dummy_operation(); } -typedef struct { - pa_context_subscribe_cb_t cb; - gpointer userdata; -} subscribe_cb_t; - void pa_context_set_subscribe_callback (pa_context * c, pa_context_subscribe_cb_t callback, void * userdata) { - g_return_if_fail(G_IS_OBJECT(c)); - - subscribe_cb_t * sub = g_new0(subscribe_cb_t, 1); - sub->cb = callback; - sub->userdata = userdata; + std::function cppcb([c, callback, userdata](pa_subscription_event_type_t event, uint32_t index) { + if (callback != nullptr) + callback(c, event, index, userdata); + }); - g_object_set_qdata_full(G_OBJECT(c), subscribe_cb_quark(), sub, g_free); + reinterpret_cast(c)->addEventCallback(cppcb); } /* ******************************* -- cgit v1.2.3 From d744a701bebec0b95b7640db1cbe6444632770b2 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Wed, 28 Jan 2015 16:24:05 -0600 Subject: Adding a copyright header --- tests/pa-mock.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/pa-mock.cpp b/tests/pa-mock.cpp index 990b10f..8ca2374 100644 --- a/tests/pa-mock.cpp +++ b/tests/pa-mock.cpp @@ -1,3 +1,21 @@ +/* + * Copyright © 2015 Canonical Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * Authors: + * Ted Gould + */ #include #include -- cgit v1.2.3 From 7f8f6bf4c2da5056fec32c82986c4f5af20113a9 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Thu, 29 Jan 2015 08:34:50 -0600 Subject: Make the destructor private to protect from accidental deleting --- tests/pa-mock.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/pa-mock.cpp b/tests/pa-mock.cpp index 8ca2374..5dd5c9b 100644 --- a/tests/pa-mock.cpp +++ b/tests/pa-mock.cpp @@ -55,10 +55,12 @@ public: g_debug("Creating Context: %p", this); } +private: /* To ensure we're the only ones who can delete it */ ~PAMockContext () { g_debug("Destroying Context: %p", this); } +public: /* Ref counting */ void ref () { refcnt++; -- cgit v1.2.3