X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/851bbc586215d48acb7575e784d09d0e0ce559df..fe0a1c48c648f683b3691132fb2b12b01d1ace32:/disobedience/control.c diff --git a/disobedience/control.c b/disobedience/control.c index 7d96aa2..9153f33 100644 --- a/disobedience/control.c +++ b/disobedience/control.c @@ -25,10 +25,30 @@ struct icon; -static void clicked_icon(GtkButton *, gpointer); +static void clicked_icon(GtkToolButton *, gpointer); +static void toggled_icon(GtkToggleToolButton *button, + gpointer user_data); static void clicked_menu(GtkMenuItem *, gpointer userdata); static void toggled_menu(GtkCheckMenuItem *, gpointer userdata); +static int enable_playing(disorder_eclient *c, + disorder_eclient_no_response *completed, + void *v); +static int disable_playing(disorder_eclient *c, + disorder_eclient_no_response *completed, + void *v); +static int enable_random(disorder_eclient *c, + disorder_eclient_no_response *completed, + void *v); +static int disable_random(disorder_eclient *c, + disorder_eclient_no_response *completed, + void *v); +static int pause_track(disorder_eclient *c, + disorder_eclient_no_response *completed, + void *v); +static int resume_track(disorder_eclient *c, + disorder_eclient_no_response *completed, + void *v); static int enable_rtp(disorder_eclient *c, disorder_eclient_no_response *completed, void *v); @@ -60,6 +80,23 @@ static void control_minimode(const char *event, /** @brief Guard against feedback */ int suppress_actions = 1; +/** @brief Toolbar widget */ +static GtkWidget *toolbar; + +/** @brief Old state to restore on error + * + * Here's the problem we're trying to solve. Suppose the user clicks the + * `pause' button. Immediately, Gtk toggles the button's state. There's also + * a menu item which is @i not yet updated, so we hack @c last_state and call + * icon_changed() by hand. We send a `pause' command to the server. If all is + * well, then we're done: we'll get a state update in the server log which will + * match what we're already doing and everything is fine. If there's an error, + * though, we must put @c last_state back the way we found it since there will + * be no change reported in the server log to correct the wrong state + * information. Also, we must untoggle the icon and menu items. + */ +static unsigned long restore_state; + /** @brief Definition of an icon * * We have two kinds of icon: @@ -72,24 +109,27 @@ int suppress_actions = 1; * (All icons can be sensitive or insensitive, separately to the above.) */ struct icon { - /** @brief True to use GTK+ stock icons instead of filenames */ + /** @brief TRUE to use GTK+ stock icons instead of filenames */ gboolean stock; - /** @brief Filename for 'on' image */ - const char *icon_on; + /** @brief TRUE for toggle buttons, FALSE for action buttons */ + gboolean toggle; + + /** @brief Filename for image or stock string */ + const char *icon; /** @brief Text for 'on' tooltip */ const char *tip_on; - /** @brief Filename for 'off' image or NULL for an action icon */ - const char *icon_off; - /** @brief Text for 'off' tooltip */ const char *tip_off; /** @brief Associated menu item or NULL */ const char *menuitem; + /** @brief Label text */ + const char *label; + /** @brief Events that change this icon, separated by spaces */ const char *events; @@ -120,6 +160,9 @@ struct icon { * Can be NULL for always sensitive. */ int (*sensitive)(void); + + /** @brief True if the menu item has inverse sense to the button */ + gboolean menu_invert; /** @brief Pointer to button */ GtkWidget *button; @@ -127,12 +170,11 @@ struct icon { /** @brief Pointer to menu item */ GtkWidget *item; - GtkWidget *image_on; - GtkWidget *image_off; + GtkWidget *image; }; static int pause_resume_on(void) { - return !(last_state & DISORDER_TRACK_PAUSED); + return !!(last_state & DISORDER_TRACK_PAUSED); } static int pause_resume_sensitive(void) { @@ -154,7 +196,6 @@ static int random_enabled(void) { return !!(last_state & DISORDER_RANDOM_ENABLED); } -#if 0 static int playing_sensitive(void) { return !!(last_rights & RIGHT_GLOBAL_PREFS); } @@ -162,7 +203,6 @@ static int playing_sensitive(void) { static int playing_enabled(void) { return !!(last_state & DISORDER_PLAYING_ENABLED); } -#endif static int rtp_enabled(void) { return rtp_is_running; @@ -175,66 +215,70 @@ static int rtp_sensitive(void) { /** @brief Table of all icons */ static struct icon icons[] = { { - stock: TRUE, - icon_on: GTK_STOCK_MEDIA_PAUSE, - tip_on: "Pause playing track", - icon_off: GTK_STOCK_MEDIA_PLAY, - tip_off: "Resume playing track", - menuitem: "/Control/Playing", - on: pause_resume_on, - sensitive: pause_resume_sensitive, - action_go_on: disorder_eclient_resume, - action_go_off: disorder_eclient_pause, - events: "pause-changed playing-changed rights-changed playing-track-changed", + .toggle = TRUE, + .stock = TRUE, + .icon = GTK_STOCK_MEDIA_PAUSE, + .label = "Pause", + .tip_on = "Resume playing track", + .tip_off = "Pause playing track", + .menuitem = "/Control/Playing", + .on = pause_resume_on, + .sensitive = pause_resume_sensitive, + .action_go_on = pause_track, + .action_go_off = resume_track, + .events = "pause-changed playing-changed rights-changed playing-track-changed", + .menu_invert = TRUE, }, { - stock: TRUE, - icon_on: GTK_STOCK_STOP, - tip_on: "Cancel playing track", - menuitem: "/Control/Scratch", - sensitive: scratch_sensitive, - action_go_off: disorder_eclient_scratch_playing, - events: "playing-track-changed rights-changed", + .stock = TRUE, + .icon = GTK_STOCK_STOP, + .label = "Scratch", + .tip_on = "Cancel playing track", + .menuitem = "/Control/Scratch", + .sensitive = scratch_sensitive, + .action_go_off = disorder_eclient_scratch_playing, + .events = "playing-track-changed rights-changed", }, { - stock: TRUE, - icon_on: GTK_STOCK_YES, - tip_on: "Disable random play", - icon_off: GTK_STOCK_NO, - tip_off: "Enable random play", - menuitem: "/Control/Random play", - on: random_enabled, - sensitive: random_sensitive, - action_go_on: disorder_eclient_random_enable, - action_go_off: disorder_eclient_random_disable, - events: "random-changed rights-changed", + .toggle = TRUE, + .stock = FALSE, + .icon = "cards24.png", + .label = "Random", + .tip_on = "Disable random play", + .tip_off = "Enable random play", + .menuitem = "/Control/Random play", + .on = random_enabled, + .sensitive = random_sensitive, + .action_go_on = enable_random, + .action_go_off = disable_random, + .events = "random-changed rights-changed", }, -#if 0 { - stock: TRUE, - icon_on: GTK_STOCK_YES, - tip_on: "Disable play", - icon_off: GTK_STOCK_NO, - tip_off: "Enable play", - on: playing_enabled, - sensitive: playing_sensitive, - action_go_on: disorder_eclient_enable, - action_go_off: disorder_eclient_disable, - events: "enabled-changed rights-changed", + .toggle = TRUE, + .stock = TRUE, + .icon = GTK_STOCK_MEDIA_PLAY, + .label = "Play", + .tip_on = "Disable play", + .tip_off = "Enable play", + .on = playing_enabled, + .sensitive = playing_sensitive, + .action_go_on = enable_playing, + .action_go_off = disable_playing, + .events = "enabled-changed rights-changed", }, -#endif { - stock: TRUE, - icon_on: GTK_STOCK_CONNECT, - tip_on: "Stop playing network stream", - icon_off: GTK_STOCK_DISCONNECT, - tip_off: "Play network stream", - menuitem: "/Control/Network player", - on: rtp_enabled, - sensitive: rtp_sensitive, - action_go_on: enable_rtp, - action_go_off: disable_rtp, - events: "rtp-changed", + .toggle = TRUE, + .stock = TRUE, + .icon = GTK_STOCK_CONNECT, + .label = "RTP", + .tip_on = "Stop playing network stream", + .tip_off = "Play network stream", + .menuitem = "/Control/Network player", + .on = rtp_enabled, + .sensitive = rtp_sensitive, + .action_go_on = enable_rtp, + .action_go_off = disable_rtp, + .events = "rtp-changed", }, }; @@ -248,48 +292,52 @@ static GtkWidget *balance_widget; /** @brief Create the control bar */ GtkWidget *control_widget(void) { - GtkWidget *hbox = gtk_hbox_new(FALSE, 1), *vbox; + GtkWidget *hbox = gtk_hbox_new(FALSE, 1); int n; D(("control_widget")); assert(mainmenufactory); /* ordering must be right */ + toolbar = gtk_toolbar_new(); + /* Don't permit overflow arrow as otherwise the toolbar isn't greedy enough + * in asking for space. The ideal is probably to make the volume and balance + * sliders hang down from the toolbar so it unavoidably gets the whole width + * of the window to play with. */ + gtk_toolbar_set_show_arrow(GTK_TOOLBAR(toolbar), FALSE); + if(full_mode) gtk_toolbar_unset_style(GTK_TOOLBAR(toolbar)); + else gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS); for(n = 0; n < NICONS; ++n) { - /* Create the button */ - icons[n].button = gtk_button_new(); + struct icon *const icon = &icons[n]; + icon->button = (icon->toggle + ? GTK_WIDGET(gtk_toggle_tool_button_new()) + : GTK_WIDGET(gtk_tool_button_new(NULL, NULL))); gtk_widget_set_style(icons[n].button, tool_style); if(icons[n].stock) { /* We'll use the stock icons for this one */ - gtk_button_set_use_stock(GTK_BUTTON(icons[n].button), TRUE); - icons[n].image_on = gtk_image_new_from_stock(icons[n].icon_on, - GTK_ICON_SIZE_LARGE_TOOLBAR); - if(icons[n].icon_off) - icons[n].image_off = gtk_image_new_from_stock(icons[n].icon_off, - GTK_ICON_SIZE_LARGE_TOOLBAR); + icon->image = gtk_image_new_from_stock(icons[n].icon, + GTK_ICON_SIZE_LARGE_TOOLBAR); } else { /* Create the 'on' image */ - icons[n].image_on = gtk_image_new_from_pixbuf(find_image(icons[n].icon_on)); - gtk_widget_set_style(icons[n].image_on, tool_style); - /* If it's a toggle icon, create the 'off' half too */ - if(icons[n].icon_off) { - icons[n].image_off = gtk_image_new_from_pixbuf(find_image(icons[n].icon_off)); - gtk_widget_set_style(icons[n].image_off, tool_style); - } + icon->image = gtk_image_new_from_pixbuf(find_image(icons[n].icon)); } - g_object_ref(icons[n].image_on); - if(icons[n].image_off) - g_object_ref(icons[n].image_off); - g_signal_connect(G_OBJECT(icons[n].button), "clicked", - G_CALLBACK(clicked_icon), &icons[n]); - /* pop the icon in a vbox so it doesn't get vertically stretch if there are - * taller things in the control bar */ - vbox = gtk_vbox_new(FALSE, 0); - gtk_box_pack_start(GTK_BOX(vbox), icons[n].button, TRUE, FALSE, 0); - gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0); + assert(icon->image); + gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(icon->button), + icon->image); + gtk_tool_button_set_label(GTK_TOOL_BUTTON(icon->button), + icon->label); + if(icon->toggle) + g_signal_connect(G_OBJECT(icon->button), "toggled", + G_CALLBACK(toggled_icon), icon); + else + g_signal_connect(G_OBJECT(icon->button), "clicked", + G_CALLBACK(clicked_icon), icon); + gtk_toolbar_insert(GTK_TOOLBAR(toolbar), + GTK_TOOL_ITEM(icon->button), + -1); if(icons[n].menuitem) { /* Find the menu item */ icons[n].item = gtk_item_factory_get_widget(mainmenufactory, icons[n].menuitem); - if(icons[n].icon_off) + if(icon->toggle) g_signal_connect(G_OBJECT(icons[n].item), "toggled", G_CALLBACK(toggled_menu), &icons[n]); else @@ -315,12 +363,16 @@ GtkWidget *control_widget(void) { gtk_widget_set_style(balance_widget, tool_style); gtk_scale_set_digits(GTK_SCALE(volume_widget), 10); gtk_scale_set_digits(GTK_SCALE(balance_widget), 10); - gtk_widget_set_size_request(volume_widget, 192, -1); - gtk_widget_set_size_request(balance_widget, 192, -1); + gtk_widget_set_size_request(volume_widget, 128, -1); + gtk_widget_set_size_request(balance_widget, 128, -1); gtk_widget_set_tooltip_text(volume_widget, "Volume"); gtk_widget_set_tooltip_text(balance_widget, "Balance"); - gtk_box_pack_start(GTK_BOX(hbox), volume_widget, FALSE, TRUE, 0); - gtk_box_pack_start(GTK_BOX(hbox), balance_widget, FALSE, TRUE, 0); + gtk_box_pack_start(GTK_BOX(hbox), toolbar, + FALSE/*expand*/, TRUE/*fill*/, 0); + gtk_box_pack_start(GTK_BOX(hbox), volume_widget, + FALSE/*expand*/, TRUE/*fill*/, 0); + gtk_box_pack_start(GTK_BOX(hbox), balance_widget, + FALSE/*expand*/, TRUE/*fill*/, 0); /* space updates rather than hammering the server */ gtk_range_set_update_policy(GTK_RANGE(volume_widget), GTK_UPDATE_DELAYED); gtk_range_set_update_policy(GTK_RANGE(balance_widget), GTK_UPDATE_DELAYED); @@ -344,8 +396,7 @@ GtkWidget *control_widget(void) { static int volume_supported(void) { /* TODO: if the server doesn't know how to set the volume [but isn't using * network play] then we should have volume_supported = FALSE */ - return (!rtp_supported - || (rtp_supported && backend && backend->set_volume)); + return 1; } /** @brief Update the volume control when it changes */ @@ -382,22 +433,14 @@ static void icon_changed(const char attribute((unused)) *event, int on = icon->on ? icon->on() : 1; int sensitive = icon->sensitive ? icon->sensitive() : 1; //fprintf(stderr, "sensitive->%d\n", sensitive); - GtkWidget *child, *newchild; ++suppress_actions; /* If the connection is down nothing is ever usable */ if(!(last_state & DISORDER_CONNECTED)) sensitive = 0; - //fprintf(stderr, "(checked connected) sensitive->%d\n", sensitive); - /* Replace the child */ - newchild = on ? icon->image_on : icon->image_off; - child = gtk_bin_get_child(GTK_BIN(icon->button)); - if(child != newchild) { - if(child) - gtk_container_remove(GTK_CONTAINER(icon->button), child); - gtk_container_add(GTK_CONTAINER(icon->button), newchild); - gtk_widget_show(newchild); - } + if(icon->toggle) + gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(icon->button), + on); /* If you disable play or random play NOT via the icon (for instance, via the * edit menu or via a completely separate command line invocation) then the * icon shows up as insensitive. Hover the mouse over it and the correct @@ -409,29 +452,43 @@ static void icon_changed(const char attribute((unused)) *event, gtk_widget_set_sensitive(icon->button, sensitive); /* Icons with an associated menu item */ if(icon->item) { - if(icon->icon_off) - gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(icon->item), on); + if(icon->toggle) + gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(icon->item), + !!icon->menu_invert ^ !!on); gtk_widget_set_sensitive(icon->item, sensitive); } --suppress_actions; } -static void icon_action_completed(void attribute((unused)) *v, - const char *err) { - if(err) +static void icon_action_completed(void *v, const char *err) { + if(err) { + last_state = restore_state; popup_protocol_error(0, err); + icon_changed(0, 0, v); + } } -static void clicked_icon(GtkButton attribute((unused)) *button, +static void clicked_icon(GtkToolButton attribute((unused)) *button, gpointer userdata) { const struct icon *icon = userdata; if(suppress_actions) return; - if(!icon->on || icon->on()) + icon->action_go_off(client, icon_action_completed, 0); +} + +static void toggled_icon(GtkToggleToolButton attribute((unused)) *button, + gpointer user_data) { + const struct icon *icon = user_data; + + if(suppress_actions) + return; + restore_state = last_state; + if(icon->on()) icon->action_go_off(client, icon_action_completed, 0); else icon->action_go_on(client, icon_action_completed, 0); + icon_changed(0, 0, user_data); } static void clicked_menu(GtkMenuItem attribute((unused)) *menuitem, @@ -441,14 +498,12 @@ static void clicked_menu(GtkMenuItem attribute((unused)) *menuitem, static void toggled_menu(GtkCheckMenuItem attribute((unused)) *menuitem, gpointer userdata) { - clicked_icon(NULL, userdata); + toggled_icon(NULL, userdata); } /** @brief Called when a volume command completes */ static void volume_completed(void attribute((unused)) *v, - const char *err, - int attribute((unused)) l, - int attribute((unused)) r) { + const char *err) { if(err) popup_protocol_error(0, err); /* We don't set the UI's notion of the volume here, it is set from the log @@ -473,13 +528,12 @@ static void volume_adjusted(GtkAdjustment attribute((unused)) *a, * from the log. */ if(rtp_supported) { int l = nearbyint(left(v, b) * 100), r = nearbyint(right(v, b) * 100); - if(backend && backend->set_volume) - backend->set_volume(&l, &r); + rtp_setvol(&l, &r); } else - disorder_eclient_volume(client, volume_completed, - nearbyint(left(v, b) * 100), - nearbyint(right(v, b) * 100), - 0); + disorder_eclient_set_volume(client, volume_completed, + nearbyint(left(v, b) * 100), + nearbyint(right(v, b) * 100), + 0); } /** @brief Formats the volume value */ @@ -565,6 +619,48 @@ static double balance(double l, double r) { return 0; } +/** @brief Called to enable jukebox playback */ +static int enable_playing(disorder_eclient *c, + disorder_eclient_no_response *completed, void *v) { + last_state |= DISORDER_PLAYING; + return disorder_eclient_enable(c, completed, v); +} + +/** @brief Called to disable jukebox playback */ +static int disable_playing(disorder_eclient *c, + disorder_eclient_no_response *completed, void *v) { + last_state &= ~DISORDER_PLAYING; + return disorder_eclient_disable(c, completed, v); +} + +/** @brief Called to enable random selection */ +static int enable_random(disorder_eclient *c, + disorder_eclient_no_response *completed, void *v) { + last_state |= DISORDER_RANDOM_ENABLED; + return disorder_eclient_random_enable(c, completed, v); +} + +/** @brief Called to disable random selection */ +static int disable_random(disorder_eclient *c, + disorder_eclient_no_response *completed, void *v) { + last_state &= ~DISORDER_RANDOM_ENABLED; + return disorder_eclient_random_disable(c, completed, v); +} + +/** @brief Called to pause the current track */ +static int pause_track(disorder_eclient *c, + disorder_eclient_no_response *completed, void *v) { + last_state |= DISORDER_TRACK_PAUSED; + return disorder_eclient_pause(c, completed, v); +} + +/** @brief Called to resume the current track */ +static int resume_track(disorder_eclient *c, + disorder_eclient_no_response *completed, void *v) { + last_state &= ~DISORDER_TRACK_PAUSED; + return disorder_eclient_resume(c, completed, v); +} + /** @brief Called to enable RTP play * * Rather odd signature is to fit in with the other icons which all call @ref @@ -574,6 +670,7 @@ static int enable_rtp(disorder_eclient attribute((unused)) *c, disorder_eclient_no_response attribute((unused)) *completed, void attribute((unused)) *v) { start_rtp(); + rtp_is_running = 1; return 0; } @@ -586,6 +683,7 @@ static int disable_rtp(disorder_eclient attribute((unused)) *c, disorder_eclient_no_response attribute((unused)) *completed, void attribute((unused)) *v) { stop_rtp(); + rtp_is_running = 0; return 0; } @@ -599,6 +697,8 @@ static void control_minimode(const char attribute((unused)) *event, gtk_widget_hide(balance_widget); gtk_scale_set_value_pos(GTK_SCALE(volume_widget), GTK_POS_RIGHT); } + if(full_mode) gtk_toolbar_unset_style(GTK_TOOLBAR(toolbar)); + else gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS); } /*