From 77f9dda5ab6065619cee95282755806dff401d6c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sat, 20 Feb 2010 02:45:12 +0100 Subject: cmdline: add command line parser --- gnome-speaker-setup.vala | 151 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 130 insertions(+), 21 deletions(-) diff --git a/gnome-speaker-setup.vala b/gnome-speaker-setup.vala index d9df5f5..fea5eeb 100644 --- a/gnome-speaker-setup.vala +++ b/gnome-speaker-setup.vala @@ -24,6 +24,10 @@ using PulseAudio; using Gee; using Canberra; +int64 default_card_index; +int64 default_device_index; +bool lock_selection; + public class BoldLabel : Label { public BoldLabel(string? text = null) { if (text != null) @@ -346,17 +350,19 @@ public class SpeakerSetupWindow : Window { context = new PulseAudio.Context(main_loop.get_api(), "Speaker Test"); if (context == null) { - stderr.printf("Couldn't allocate connection.\n"); + stderr.printf("Cannot allocate connection.\n"); main_quit(); } else { context.set_state_callback(context_state_cb); context.set_subscribe_callback(context_subscribe_cb); if (context.connect() < 0) { - stderr.printf("Couldn't initiate connection: %s\n", PulseAudio.strerror(context.errno())); + stderr.printf("Cannot initiate connection: %s\n", PulseAudio.strerror(context.errno())); main_quit(); } } + + vbox.show_all(); } private const int position_table[] = { @@ -406,7 +412,7 @@ public class SpeakerSetupWindow : Window { PulseAudio.Context.State state = c.get_state(); if (!state.IS_GOOD()) { - stderr.printf("Couldn't establish connection: %s\n", PulseAudio.strerror(c.errno())); + stderr.printf("Cannot establish connection: %s\n", PulseAudio.strerror(c.errno())); Gtk.main_quit(); return; }; @@ -414,6 +420,7 @@ public class SpeakerSetupWindow : Window { if (state == PulseAudio.Context.State.READY) { /* Connection is ready, let's query cards and sinks */ context.subscribe(PulseAudio.Context.SubscriptionMask.SINK | PulseAudio.Context.SubscriptionMask.CARD); + c.get_card_info_list(context_card_info_cb); c.get_sink_info_list(context_sink_info_cb); } @@ -513,29 +520,110 @@ public class SpeakerSetupWindow : Window { } } - void pick_initial_card() { + bool select_card(uint32 index) { + TreeIter iter; + if (!cards.get_iter_first(out iter)) + return false; + + do { + uint32 j; + cards.get(iter, 1, out j); + + if (j == index) { + card_combo_box.set_active_iter(iter); + return true; + } + + } while (cards.iter_next(ref iter)); + + return false; + } + + bool select_device(uint32 index) { + + if (!(index in device_lookup)) + return false; + + DeviceData *d = device_lookup[index]; + ListStore *devices; + + if (d->card != null) { + + if (!select_card(d->card->index)) + return false; + + devices = d->card->devices; + } else { + card_combo_box.set_active(0); /* that's the independant devices entry */ + devices = unowned_devices; + } + + TreeIter iter; + if (!devices->get_iter_first(out iter)) + return false; + + do { + uint32 j; + devices->get(iter, 1, out j); + + if (j == index) { + device_combo_box.set_active_iter(iter); + return true; + } + + } while (devices->iter_next(ref iter)); + + return false; + } + + bool pick_initial_selection() { + if (finished_startup) - return; + return true; finished_startup = true; - if (card_combo_box.get_active() >= 0) - return; + if (default_device_index != INVALID_INDEX) { - TreeIter i; - if (cards.get_iter_first(out i) && - cards.iter_next(ref i)) - card_combo_box.set_active(1); - else - card_combo_box.set_active(0); + if (!select_device((uint32) default_device_index)) { + stderr.printf("Cannot find device.\n"); + main_quit(); + return false; + } + + profile_combo_box.set_sensitive(!lock_selection); + card_combo_box.set_sensitive(!lock_selection); + device_combo_box.set_sensitive(!lock_selection); + + } else if (default_card_index != INVALID_INDEX) { + + if (!select_card((uint32) default_card_index)) { + stderr.printf("Cannot find card.\n"); + main_quit(); + return false; + } + + card_combo_box.set_sensitive(!lock_selection); + } else { + if (card_combo_box.get_active() >= 0) + return true; + + TreeIter i; + if (cards.get_iter_first(out i) && + cards.iter_next(ref i)) + card_combo_box.set_active_iter(i); + else + card_combo_box.set_active(0); + } + + return true; } void context_card_info_cb(PulseAudio.Context c, CardInfo? info, int eol) { - if (info == null) { - pick_initial_card(); + if (info == null) return; - } + CardData *d; TreeIter iter; @@ -581,8 +669,11 @@ public class SpeakerSetupWindow : Window { void context_sink_info_cb(PulseAudio.Context c, SinkInfo? info, int eol) { - if (info == null) + if (info == null) { + if (pick_initial_selection()) + show(); return; + } DeviceData *d; TreeIter iter; @@ -599,7 +690,6 @@ public class SpeakerSetupWindow : Window { devices = d->card->devices; find_device_iter(devices, d->index, out iter); - } else { d = new DeviceData(); @@ -858,12 +948,31 @@ public class SpeakerSetupWindow : Window { } } +static const OptionEntry entries[] = { + { "card", 0, 0, OptionArg.INT64, out default_card_index, "Show this card by default", "INDEX" }, + { "device", 0, 0, OptionArg.INT64, out default_device_index, "Show this device by default", "INDEX" }, + { "lock", 0, 0, OptionArg.NONE, out lock_selection, "Don't allow other selection", null }, + { null } +}; + int main (string[] args) { - Gtk.init (ref args); + Gtk.init(ref args); + + default_card_index = INVALID_INDEX; + default_device_index = INVALID_INDEX; + lock_selection = false; - var window = new SpeakerSetupWindow(); - window.show_all(); + OptionContext context = new OptionContext("- Speaker Setup Tool"); + context.add_main_entries(entries, null); + context.add_group(Gtk.get_option_group(false)); + + try { + context.parse(ref args); + } catch (GLib.OptionError e) { + message("Failed to parse command line: %s".printf(e.message)); + } + new SpeakerSetupWindow(); Gtk.main(); return 0; -- cgit