From 4de19331ab61fa2f0b270f6f6040acac80edecbf Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 26 May 2009 04:13:46 +0200 Subject: canberra-gtk-play: implement --property= --- src/canberra-gtk-play.c | 60 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/src/canberra-gtk-play.c b/src/canberra-gtk-play.c index 8445b72..a870b58 100644 --- a/src/canberra-gtk-play.c +++ b/src/canberra-gtk-play.c @@ -22,9 +22,11 @@ #include #endif +#include +#include + #include #include -#include static int ret = 0; static ca_proplist *proplist = NULL; @@ -78,20 +80,52 @@ static void callback(ca_context *c, uint32_t id, int error, void *userdata) { g_idle_add(idle_quit, NULL); } +static GQuark error_domain(void) { + return g_quark_from_static_string("canberra-error-quark"); +} + +static gboolean property_callback( + const gchar *option_name, + const gchar *value, + gpointer data, + GError **error) { + + const char *equal; + char *t; + + if (!(equal = strchr(value, '='))) { + g_set_error(error, error_domain(), 0, "Property lacks '='."); + return FALSE; + } + + t = g_strndup(value, equal - value); + + if (ca_proplist_sets(proplist, t, equal + 1) < 0) { + g_set_error(error, error_domain(), 0, "Invalid property."); + g_free(t); + return FALSE; + } + + g_free(t); + return TRUE; +} + int main (int argc, char *argv[]) { GOptionContext *oc; static gchar *event_id = NULL, *filename = NULL, *event_description = NULL, *cache_control = NULL, *volume = NULL; int r; static gboolean version = FALSE; + GError *error = NULL; static const GOptionEntry options[] = { - { "id", 'i', 0, G_OPTION_ARG_STRING, &event_id, "Event sound identifier", "STRING" }, - { "file", 'f', 0, G_OPTION_ARG_STRING, &filename, "Play file", "PATH" }, - { "description", 'd', 0, G_OPTION_ARG_STRING, &event_description, "Event sound description", "STRING" }, - { "cache-control", 'c', 0, G_OPTION_ARG_STRING, &cache_control, "Cache control (permanent, volatile, never)", "STRING" }, - { "loop", 'l', 0, G_OPTION_ARG_INT, &n_loops, "Loop how many times (detault: 1)", "INTEGER" }, - { "volume", 'V', 0, G_OPTION_ARG_STRING, &volume, "A floating point dB value for the sample volume (ex: 0.0)", "STRING" }, - { "version", 'v', 0, G_OPTION_ARG_NONE, &version, "Display version number and quit", NULL }, + { "version", 'v', 0, G_OPTION_ARG_NONE, &version, "Display version number and quit", NULL }, + { "id", 'i', 0, G_OPTION_ARG_STRING, &event_id, "Event sound identifier", "STRING" }, + { "file", 'f', 0, G_OPTION_ARG_STRING, &filename, "Play file", "PATH" }, + { "description", 'd', 0, G_OPTION_ARG_STRING, &event_description, "Event sound description", "STRING" }, + { "cache-control", 'c', 0, G_OPTION_ARG_STRING, &cache_control, "Cache control (permanent, volatile, never)", "STRING" }, + { "loop", 'l', 0, G_OPTION_ARG_INT, &n_loops, "Loop how many times (detault: 1)", "INTEGER" }, + { "volume", 'V', 0, G_OPTION_ARG_STRING, &volume, "A floating point dB value for the sample volume (ex: 0.0)", "STRING" }, + { "property", 0, 0, G_OPTION_ARG_CALLBACK, (void*) property_callback, "An arbitrary property", "STRING" }, { NULL, 0, 0, 0, NULL, NULL, NULL } }; @@ -100,11 +134,17 @@ int main (int argc, char *argv[]) { g_type_init(); g_thread_init(NULL); + ca_proplist_create(&proplist); + oc = g_option_context_new("- canberra-gtk-play"); g_option_context_add_main_entries(oc, options, NULL); g_option_context_add_group(oc, gtk_get_option_group(TRUE)); g_option_context_set_help_enabled(oc, TRUE); - g_option_context_parse(oc, &argc, &argv, NULL); + + if (!(g_option_context_parse(oc, &argc, &argv, &error))) { + g_print("Option parsing failed: %s\n", error->message); + return 1; + } g_option_context_free(oc); if (version) { @@ -122,8 +162,6 @@ int main (int argc, char *argv[]) { CA_PROP_APPLICATION_ID, "org.freedesktop.libcanberra.GtkPlay", NULL); - ca_proplist_create(&proplist); - if (event_id) ca_proplist_sets(proplist, CA_PROP_EVENT_ID, event_id); -- cgit