| 1 | /* |
| 2 | * This file is part of DisOrder. |
| 3 | * Copyright (C) 2006-2009 Richard Kettlewell |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | /** @file disobedience/control.c |
| 19 | * @brief Volume control and buttons |
| 20 | */ |
| 21 | |
| 22 | #include "disobedience.h" |
| 23 | |
| 24 | /* Forward declarations ---------------------------------------------------- */ |
| 25 | |
| 26 | struct icon; |
| 27 | |
| 28 | static void clicked_icon(GtkToolButton *, gpointer); |
| 29 | static void toggled_icon(GtkToggleToolButton *button, |
| 30 | gpointer user_data); |
| 31 | static void clicked_menu(GtkMenuItem *, gpointer userdata); |
| 32 | static void toggled_menu(GtkCheckMenuItem *, gpointer userdata); |
| 33 | |
| 34 | static int enable_playing(disorder_eclient *c, |
| 35 | disorder_eclient_no_response *completed, |
| 36 | void *v); |
| 37 | static int disable_playing(disorder_eclient *c, |
| 38 | disorder_eclient_no_response *completed, |
| 39 | void *v); |
| 40 | static int enable_random(disorder_eclient *c, |
| 41 | disorder_eclient_no_response *completed, |
| 42 | void *v); |
| 43 | static int disable_random(disorder_eclient *c, |
| 44 | disorder_eclient_no_response *completed, |
| 45 | void *v); |
| 46 | static int pause_track(disorder_eclient *c, |
| 47 | disorder_eclient_no_response *completed, |
| 48 | void *v); |
| 49 | static int resume_track(disorder_eclient *c, |
| 50 | disorder_eclient_no_response *completed, |
| 51 | void *v); |
| 52 | static int enable_rtp(disorder_eclient *c, |
| 53 | disorder_eclient_no_response *completed, |
| 54 | void *v); |
| 55 | static int disable_rtp(disorder_eclient *c, |
| 56 | disorder_eclient_no_response *completed, |
| 57 | void *v); |
| 58 | |
| 59 | static double left(double v, double b); |
| 60 | static double right(double v, double b); |
| 61 | static double volume(double l, double r); |
| 62 | static double balance(double l, double r); |
| 63 | |
| 64 | static void volume_adjusted(GtkAdjustment *a, gpointer user_data); |
| 65 | static gchar *format_volume(GtkScale *scale, gdouble value); |
| 66 | static gchar *format_balance(GtkScale *scale, gdouble value); |
| 67 | |
| 68 | static void icon_changed(const char *event, |
| 69 | void *evendata, |
| 70 | void *callbackdata); |
| 71 | static void volume_changed(const char *event, |
| 72 | void *eventdata, |
| 73 | void *callbackdata); |
| 74 | static void control_minimode(const char *event, |
| 75 | void *eventdata, |
| 76 | void *callbackdata); |
| 77 | |
| 78 | /* Control bar ------------------------------------------------------------- */ |
| 79 | |
| 80 | /** @brief Guard against feedback */ |
| 81 | int suppress_actions = 1; |
| 82 | |
| 83 | /** @brief Toolbar widget */ |
| 84 | static GtkWidget *toolbar; |
| 85 | |
| 86 | /** @brief Old state to restore on error |
| 87 | * |
| 88 | * Here's the problem we're trying to solve. Suppose the user clicks the |
| 89 | * `pause' button. Immediately, Gtk toggles the button's state. There's also |
| 90 | * a menu item which is @i not yet updated, so we hack @c last_state and call |
| 91 | * icon_changed() by hand. We send a `pause' command to the server. If all is |
| 92 | * well, then we're done: we'll get a state update in the server log which will |
| 93 | * match what we're already doing and everything is fine. If there's an error, |
| 94 | * though, we must put @c last_state back the way we found it since there will |
| 95 | * be no change reported in the server log to correct the wrong state |
| 96 | * information. Also, we must untoggle the icon and menu items. |
| 97 | */ |
| 98 | static unsigned long restore_state; |
| 99 | |
| 100 | /** @brief Definition of an icon |
| 101 | * |
| 102 | * We have two kinds of icon: |
| 103 | * - action icons, which just do something but don't have a state as such |
| 104 | * - toggle icons, which toggle between two states ("on" and "off"). |
| 105 | * |
| 106 | * The scratch button is an action icon; currently all the others are toggle |
| 107 | * icons. |
| 108 | * |
| 109 | * (All icons can be sensitive or insensitive, separately to the above.) |
| 110 | */ |
| 111 | struct icon { |
| 112 | /** @brief TRUE to use GTK+ stock icons instead of filenames */ |
| 113 | gboolean stock; |
| 114 | |
| 115 | /** @brief TRUE for toggle buttons, FALSE for action buttons */ |
| 116 | gboolean toggle; |
| 117 | |
| 118 | /** @brief Filename for image or stock string */ |
| 119 | const char *icon; |
| 120 | |
| 121 | /** @brief Text for 'on' tooltip */ |
| 122 | const char *tip_on; |
| 123 | |
| 124 | /** @brief Text for 'off' tooltip */ |
| 125 | const char *tip_off; |
| 126 | |
| 127 | /** @brief Associated menu item or NULL */ |
| 128 | const char *menuitem; |
| 129 | |
| 130 | /** @brief Label text */ |
| 131 | const char *label; |
| 132 | |
| 133 | /** @brief Events that change this icon, separated by spaces */ |
| 134 | const char *events; |
| 135 | |
| 136 | /** @brief @ref eclient.h function to call to go from off to on |
| 137 | * |
| 138 | * For action buttons, this should be NULL. |
| 139 | */ |
| 140 | int (*action_go_on)(disorder_eclient *c, |
| 141 | disorder_eclient_no_response *completed, |
| 142 | void *v); |
| 143 | |
| 144 | /** @brief @ref eclient.h function to call to go from on to off |
| 145 | * |
| 146 | * For action buttons, this action is used. |
| 147 | */ |
| 148 | int (*action_go_off)(disorder_eclient *c, |
| 149 | disorder_eclient_no_response *completed, |
| 150 | void *v); |
| 151 | |
| 152 | /** @brief Get button state |
| 153 | * @return 1 for on, 0 for off |
| 154 | */ |
| 155 | int (*on)(void); |
| 156 | |
| 157 | /** @brief Get button sensitivity |
| 158 | * @return 1 for sensitive, 0 for insensitive |
| 159 | * |
| 160 | * Can be NULL for always sensitive. |
| 161 | */ |
| 162 | int (*sensitive)(void); |
| 163 | |
| 164 | /** @brief True if the menu item has inverse sense to the button */ |
| 165 | gboolean menu_invert; |
| 166 | |
| 167 | /** @brief Pointer to button */ |
| 168 | GtkWidget *button; |
| 169 | |
| 170 | /** @brief Pointer to menu item */ |
| 171 | GtkWidget *item; |
| 172 | |
| 173 | GtkWidget *image; |
| 174 | }; |
| 175 | |
| 176 | static int pause_resume_on(void) { |
| 177 | return !!(last_state & DISORDER_TRACK_PAUSED); |
| 178 | } |
| 179 | |
| 180 | static int pause_resume_sensitive(void) { |
| 181 | return playing_track |
| 182 | && !!(last_state & DISORDER_PLAYING) |
| 183 | && (last_rights & RIGHT_PAUSE); |
| 184 | } |
| 185 | |
| 186 | static int scratch_sensitive(void) { |
| 187 | return !!(last_state & DISORDER_PLAYING) |
| 188 | && right_scratchable(last_rights, config->username, playing_track); |
| 189 | } |
| 190 | |
| 191 | static int random_sensitive(void) { |
| 192 | return !!(last_rights & RIGHT_GLOBAL_PREFS); |
| 193 | } |
| 194 | |
| 195 | static int random_enabled(void) { |
| 196 | return !!(last_state & DISORDER_RANDOM_ENABLED); |
| 197 | } |
| 198 | |
| 199 | static int playing_sensitive(void) { |
| 200 | return !!(last_rights & RIGHT_GLOBAL_PREFS); |
| 201 | } |
| 202 | |
| 203 | static int playing_enabled(void) { |
| 204 | return !!(last_state & DISORDER_PLAYING_ENABLED); |
| 205 | } |
| 206 | |
| 207 | static int rtp_enabled(void) { |
| 208 | return rtp_is_running; |
| 209 | } |
| 210 | |
| 211 | static int rtp_sensitive(void) { |
| 212 | return rtp_supported; |
| 213 | } |
| 214 | |
| 215 | /** @brief Table of all icons */ |
| 216 | static struct icon icons[] = { |
| 217 | { |
| 218 | .toggle = TRUE, |
| 219 | .stock = TRUE, |
| 220 | .icon = GTK_STOCK_MEDIA_PAUSE, |
| 221 | .label = "Pause", |
| 222 | .tip_on = "Resume playing track", |
| 223 | .tip_off = "Pause playing track", |
| 224 | .menuitem = "<GdisorderMain>/Control/Playing", |
| 225 | .on = pause_resume_on, |
| 226 | .sensitive = pause_resume_sensitive, |
| 227 | .action_go_on = pause_track, |
| 228 | .action_go_off = resume_track, |
| 229 | .events = "pause-changed playing-changed rights-changed playing-track-changed", |
| 230 | .menu_invert = TRUE, |
| 231 | }, |
| 232 | { |
| 233 | .stock = TRUE, |
| 234 | .icon = GTK_STOCK_STOP, |
| 235 | .label = "Scratch", |
| 236 | .tip_on = "Cancel playing track", |
| 237 | .menuitem = "<GdisorderMain>/Control/Scratch", |
| 238 | .sensitive = scratch_sensitive, |
| 239 | .action_go_off = disorder_eclient_scratch_playing, |
| 240 | .events = "playing-track-changed rights-changed", |
| 241 | }, |
| 242 | { |
| 243 | .toggle = TRUE, |
| 244 | .stock = FALSE, |
| 245 | .icon = "cards24.png", |
| 246 | .label = "Random", |
| 247 | .tip_on = "Disable random play", |
| 248 | .tip_off = "Enable random play", |
| 249 | .menuitem = "<GdisorderMain>/Control/Random play", |
| 250 | .on = random_enabled, |
| 251 | .sensitive = random_sensitive, |
| 252 | .action_go_on = enable_random, |
| 253 | .action_go_off = disable_random, |
| 254 | .events = "random-changed rights-changed", |
| 255 | }, |
| 256 | { |
| 257 | .toggle = TRUE, |
| 258 | .stock = TRUE, |
| 259 | .icon = GTK_STOCK_MEDIA_PLAY, |
| 260 | .label = "Play", |
| 261 | .tip_on = "Disable play", |
| 262 | .tip_off = "Enable play", |
| 263 | .on = playing_enabled, |
| 264 | .sensitive = playing_sensitive, |
| 265 | .action_go_on = enable_playing, |
| 266 | .action_go_off = disable_playing, |
| 267 | .events = "enabled-changed rights-changed", |
| 268 | }, |
| 269 | { |
| 270 | .toggle = TRUE, |
| 271 | .stock = TRUE, |
| 272 | .icon = GTK_STOCK_CONNECT, |
| 273 | .label = "RTP", |
| 274 | .tip_on = "Stop playing network stream", |
| 275 | .tip_off = "Play network stream", |
| 276 | .menuitem = "<GdisorderMain>/Control/Network player", |
| 277 | .on = rtp_enabled, |
| 278 | .sensitive = rtp_sensitive, |
| 279 | .action_go_on = enable_rtp, |
| 280 | .action_go_off = disable_rtp, |
| 281 | .events = "rtp-changed", |
| 282 | }, |
| 283 | }; |
| 284 | |
| 285 | /** @brief Count of icons */ |
| 286 | #define NICONS (int)(sizeof icons / sizeof *icons) |
| 287 | |
| 288 | static GtkAdjustment *volume_adj; |
| 289 | static GtkAdjustment *balance_adj; |
| 290 | static GtkWidget *volume_widget; |
| 291 | static GtkWidget *balance_widget; |
| 292 | |
| 293 | /** @brief Create the control bar */ |
| 294 | GtkWidget *control_widget(void) { |
| 295 | GtkWidget *hbox = gtk_hbox_new(FALSE, 1); |
| 296 | int n; |
| 297 | |
| 298 | D(("control_widget")); |
| 299 | assert(mainmenufactory); /* ordering must be right */ |
| 300 | toolbar = gtk_toolbar_new(); |
| 301 | /* Don't permit overflow arrow as otherwise the toolbar isn't greedy enough |
| 302 | * in asking for space. The ideal is probably to make the volume and balance |
| 303 | * sliders hang down from the toolbar so it unavoidably gets the whole width |
| 304 | * of the window to play with. */ |
| 305 | gtk_toolbar_set_show_arrow(GTK_TOOLBAR(toolbar), FALSE); |
| 306 | if(full_mode) gtk_toolbar_unset_style(GTK_TOOLBAR(toolbar)); |
| 307 | else gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS); |
| 308 | for(n = 0; n < NICONS; ++n) { |
| 309 | struct icon *const icon = &icons[n]; |
| 310 | icon->button = (icon->toggle |
| 311 | ? GTK_WIDGET(gtk_toggle_tool_button_new()) |
| 312 | : GTK_WIDGET(gtk_tool_button_new(NULL, NULL))); |
| 313 | gtk_widget_set_style(icons[n].button, tool_style); |
| 314 | if(icons[n].stock) { |
| 315 | /* We'll use the stock icons for this one */ |
| 316 | icon->image = gtk_image_new_from_stock(icons[n].icon, |
| 317 | GTK_ICON_SIZE_LARGE_TOOLBAR); |
| 318 | } else { |
| 319 | /* Create the 'on' image */ |
| 320 | icon->image = gtk_image_new_from_pixbuf(find_image(icons[n].icon)); |
| 321 | } |
| 322 | assert(icon->image); |
| 323 | gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(icon->button), |
| 324 | icon->image); |
| 325 | gtk_tool_button_set_label(GTK_TOOL_BUTTON(icon->button), |
| 326 | icon->label); |
| 327 | if(icon->toggle) |
| 328 | g_signal_connect(G_OBJECT(icon->button), "toggled", |
| 329 | G_CALLBACK(toggled_icon), icon); |
| 330 | else |
| 331 | g_signal_connect(G_OBJECT(icon->button), "clicked", |
| 332 | G_CALLBACK(clicked_icon), icon); |
| 333 | gtk_toolbar_insert(GTK_TOOLBAR(toolbar), |
| 334 | GTK_TOOL_ITEM(icon->button), |
| 335 | -1); |
| 336 | if(icons[n].menuitem) { |
| 337 | /* Find the menu item */ |
| 338 | icons[n].item = gtk_item_factory_get_widget(mainmenufactory, |
| 339 | icons[n].menuitem); |
| 340 | if(icon->toggle) |
| 341 | g_signal_connect(G_OBJECT(icons[n].item), "toggled", |
| 342 | G_CALLBACK(toggled_menu), &icons[n]); |
| 343 | else |
| 344 | g_signal_connect(G_OBJECT(icons[n].item), "activate", |
| 345 | G_CALLBACK(clicked_menu), &icons[n]); |
| 346 | } |
| 347 | /* Make sure the icon is updated when relevant things changed */ |
| 348 | char **events = split(icons[n].events, 0, 0, 0, 0); |
| 349 | while(*events) |
| 350 | event_register(*events++, icon_changed, &icons[n]); |
| 351 | event_register("connected-changed", icon_changed, &icons[n]); |
| 352 | } |
| 353 | /* create the adjustments for the volume control */ |
| 354 | volume_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, goesupto, |
| 355 | goesupto / 20, goesupto / 20, |
| 356 | 0)); |
| 357 | balance_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, -1, 1, |
| 358 | 0.2, 0.2, 0)); |
| 359 | /* the volume control */ |
| 360 | volume_widget = gtk_hscale_new(volume_adj); |
| 361 | balance_widget = gtk_hscale_new(balance_adj); |
| 362 | gtk_widget_set_style(volume_widget, tool_style); |
| 363 | gtk_widget_set_style(balance_widget, tool_style); |
| 364 | gtk_scale_set_digits(GTK_SCALE(volume_widget), 10); |
| 365 | gtk_scale_set_digits(GTK_SCALE(balance_widget), 10); |
| 366 | gtk_widget_set_size_request(volume_widget, 128, -1); |
| 367 | gtk_widget_set_size_request(balance_widget, 128, -1); |
| 368 | gtk_widget_set_tooltip_text(volume_widget, "Volume"); |
| 369 | gtk_widget_set_tooltip_text(balance_widget, "Balance"); |
| 370 | gtk_box_pack_start(GTK_BOX(hbox), toolbar, |
| 371 | FALSE/*expand*/, TRUE/*fill*/, 0); |
| 372 | gtk_box_pack_start(GTK_BOX(hbox), volume_widget, |
| 373 | FALSE/*expand*/, TRUE/*fill*/, 0); |
| 374 | gtk_box_pack_start(GTK_BOX(hbox), balance_widget, |
| 375 | FALSE/*expand*/, TRUE/*fill*/, 0); |
| 376 | /* space updates rather than hammering the server */ |
| 377 | gtk_range_set_update_policy(GTK_RANGE(volume_widget), GTK_UPDATE_DELAYED); |
| 378 | gtk_range_set_update_policy(GTK_RANGE(balance_widget), GTK_UPDATE_DELAYED); |
| 379 | /* notice when the adjustments are changed */ |
| 380 | g_signal_connect(G_OBJECT(volume_adj), "value-changed", |
| 381 | G_CALLBACK(volume_adjusted), 0); |
| 382 | g_signal_connect(G_OBJECT(balance_adj), "value-changed", |
| 383 | G_CALLBACK(volume_adjusted), 0); |
| 384 | /* format the volume/balance values ourselves */ |
| 385 | g_signal_connect(G_OBJECT(volume_widget), "format-value", |
| 386 | G_CALLBACK(format_volume), 0); |
| 387 | g_signal_connect(G_OBJECT(balance_widget), "format-value", |
| 388 | G_CALLBACK(format_balance), 0); |
| 389 | event_register("volume-changed", volume_changed, 0); |
| 390 | event_register("rtp-changed", volume_changed, 0); |
| 391 | event_register("mini-mode-changed", control_minimode, 0); |
| 392 | return hbox; |
| 393 | } |
| 394 | |
| 395 | /** @brief Return TRUE if volume setting is supported */ |
| 396 | static int volume_supported(void) { |
| 397 | /* TODO: if the server doesn't know how to set the volume [but isn't using |
| 398 | * network play] then we should have volume_supported = FALSE */ |
| 399 | return 1; |
| 400 | } |
| 401 | |
| 402 | /** @brief Update the volume control when it changes */ |
| 403 | static void volume_changed(const char attribute((unused)) *event, |
| 404 | void attribute((unused)) *eventdata, |
| 405 | void attribute((unused)) *callbackdata) { |
| 406 | double l, r; |
| 407 | |
| 408 | D(("volume_changed")); |
| 409 | ++suppress_actions; |
| 410 | /* Only display volume/balance controls if they will work */ |
| 411 | if(volume_supported()) { |
| 412 | gtk_widget_show(volume_widget); |
| 413 | if(full_mode) |
| 414 | gtk_widget_show(balance_widget); |
| 415 | l = volume_l / 100.0; |
| 416 | r = volume_r / 100.0; |
| 417 | gtk_adjustment_set_value(volume_adj, volume(l, r) * goesupto); |
| 418 | gtk_adjustment_set_value(balance_adj, balance(l, r)); |
| 419 | } else { |
| 420 | gtk_widget_hide(volume_widget); |
| 421 | gtk_widget_hide(balance_widget); |
| 422 | } |
| 423 | --suppress_actions; |
| 424 | } |
| 425 | |
| 426 | /** @brief Update the state of one of the control icons |
| 427 | */ |
| 428 | static void icon_changed(const char attribute((unused)) *event, |
| 429 | void attribute((unused)) *evendata, |
| 430 | void *callbackdata) { |
| 431 | //fprintf(stderr, "icon_changed (%s)\n", event); |
| 432 | const struct icon *const icon = callbackdata; |
| 433 | int on = icon->on ? icon->on() : 1; |
| 434 | int sensitive = icon->sensitive ? icon->sensitive() : 1; |
| 435 | //fprintf(stderr, "sensitive->%d\n", sensitive); |
| 436 | |
| 437 | ++suppress_actions; |
| 438 | /* If the connection is down nothing is ever usable */ |
| 439 | if(!(last_state & DISORDER_CONNECTED)) |
| 440 | sensitive = 0; |
| 441 | if(icon->toggle) |
| 442 | gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(icon->button), |
| 443 | on); |
| 444 | /* If you disable play or random play NOT via the icon (for instance, via the |
| 445 | * edit menu or via a completely separate command line invocation) then the |
| 446 | * icon shows up as insensitive. Hover the mouse over it and the correct |
| 447 | * state is immediately displayed. sensitive and GTK_WIDGET_SENSITIVE show |
| 448 | * it to be in the correct state, so I think this is may be a GTK+ bug. */ |
| 449 | if(icon->tip_on) |
| 450 | gtk_widget_set_tooltip_text(icon->button, |
| 451 | on ? icon->tip_on : icon->tip_off); |
| 452 | gtk_widget_set_sensitive(icon->button, sensitive); |
| 453 | /* Icons with an associated menu item */ |
| 454 | if(icon->item) { |
| 455 | if(icon->toggle) |
| 456 | gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(icon->item), |
| 457 | !!icon->menu_invert ^ !!on); |
| 458 | gtk_widget_set_sensitive(icon->item, sensitive); |
| 459 | } |
| 460 | --suppress_actions; |
| 461 | } |
| 462 | |
| 463 | static void icon_action_completed(void *v, const char *err) { |
| 464 | if(err) { |
| 465 | last_state = restore_state; |
| 466 | popup_protocol_error(0, err); |
| 467 | icon_changed(0, 0, v); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | static void clicked_icon(GtkToolButton attribute((unused)) *button, |
| 472 | gpointer userdata) { |
| 473 | const struct icon *icon = userdata; |
| 474 | |
| 475 | if(suppress_actions) |
| 476 | return; |
| 477 | icon->action_go_off(client, icon_action_completed, 0); |
| 478 | } |
| 479 | |
| 480 | static void toggled_icon(GtkToggleToolButton attribute((unused)) *button, |
| 481 | gpointer user_data) { |
| 482 | const struct icon *icon = user_data; |
| 483 | |
| 484 | if(suppress_actions) |
| 485 | return; |
| 486 | restore_state = last_state; |
| 487 | if(icon->on()) |
| 488 | icon->action_go_off(client, icon_action_completed, 0); |
| 489 | else |
| 490 | icon->action_go_on(client, icon_action_completed, 0); |
| 491 | icon_changed(0, 0, user_data); |
| 492 | } |
| 493 | |
| 494 | static void clicked_menu(GtkMenuItem attribute((unused)) *menuitem, |
| 495 | gpointer userdata) { |
| 496 | clicked_icon(NULL, userdata); |
| 497 | } |
| 498 | |
| 499 | static void toggled_menu(GtkCheckMenuItem attribute((unused)) *menuitem, |
| 500 | gpointer userdata) { |
| 501 | toggled_icon(NULL, userdata); |
| 502 | } |
| 503 | |
| 504 | /** @brief Called when a volume command completes */ |
| 505 | static void volume_completed(void attribute((unused)) *v, |
| 506 | const char *err) { |
| 507 | if(err) |
| 508 | popup_protocol_error(0, err); |
| 509 | /* We don't set the UI's notion of the volume here, it is set from the log |
| 510 | * regardless of the reason it changed */ |
| 511 | } |
| 512 | |
| 513 | /** @brief Called when the volume has been adjusted */ |
| 514 | static void volume_adjusted(GtkAdjustment attribute((unused)) *a, |
| 515 | gpointer attribute((unused)) user_data) { |
| 516 | double v = gtk_adjustment_get_value(volume_adj) / goesupto; |
| 517 | double b = gtk_adjustment_get_value(balance_adj); |
| 518 | |
| 519 | if(suppress_actions) |
| 520 | /* This is the result of an update from the server, not a change from the |
| 521 | * user. Don't feedback! */ |
| 522 | return; |
| 523 | D(("volume_adjusted")); |
| 524 | /* force to 'stereotypical' values */ |
| 525 | v = nearbyint(100 * v) / 100; |
| 526 | b = nearbyint(5 * b) / 5; |
| 527 | /* Set the volume. We don't want a reply, we'll get the actual new volume |
| 528 | * from the log. */ |
| 529 | if(rtp_supported) { |
| 530 | int l = nearbyint(left(v, b) * 100), r = nearbyint(right(v, b) * 100); |
| 531 | rtp_setvol(&l, &r); |
| 532 | } else |
| 533 | disorder_eclient_set_volume(client, volume_completed, |
| 534 | nearbyint(left(v, b) * 100), |
| 535 | nearbyint(right(v, b) * 100), |
| 536 | 0); |
| 537 | } |
| 538 | |
| 539 | /** @brief Formats the volume value */ |
| 540 | static gchar *format_volume(GtkScale attribute((unused)) *scale, |
| 541 | gdouble value) { |
| 542 | char s[32]; |
| 543 | |
| 544 | snprintf(s, sizeof s, "%.1f", (double)value); |
| 545 | return g_strdup(s); |
| 546 | } |
| 547 | |
| 548 | /** @brief Formats the balance value */ |
| 549 | static gchar *format_balance(GtkScale attribute((unused)) *scale, |
| 550 | gdouble value) { |
| 551 | char s[32]; |
| 552 | |
| 553 | if(fabs(value) < 0.1) |
| 554 | return g_strdup("0"); |
| 555 | snprintf(s, sizeof s, "%+.1f", (double)value); |
| 556 | return g_strdup(s); |
| 557 | } |
| 558 | |
| 559 | /* Volume mapping. We consider left, right, volume to be in [0,1] |
| 560 | * and balance to be in [-1,1]. |
| 561 | * |
| 562 | * First, we just have volume = max(left, right). |
| 563 | * |
| 564 | * Balance we consider to linearly represent the amount by which the quieter |
| 565 | * channel differs from the louder. In detail: |
| 566 | * |
| 567 | * if right > left then balance > 0: |
| 568 | * balance = 0 => left = right (as an endpoint, not an instance) |
| 569 | * balance = 1 => left = 0 |
| 570 | * fitting to linear, left = right * (1 - balance) |
| 571 | * so balance = 1 - left / right |
| 572 | * (right > left => right > 0 so no division by 0.) |
| 573 | * |
| 574 | * if left > right then balance < 0: |
| 575 | * balance = 0 => right = left (same caveat as above) |
| 576 | * balance = -1 => right = 0 |
| 577 | * again fitting to linear, right = left * (1 + balance) |
| 578 | * so balance = right / left - 1 |
| 579 | * (left > right => left > 0 so no division by 0.) |
| 580 | * |
| 581 | * if left = right then we just have balance = 0. |
| 582 | * |
| 583 | * Thanks to Clive and Andrew. |
| 584 | */ |
| 585 | |
| 586 | /** @brief Return the greater of @p x and @p y */ |
| 587 | static double max(double x, double y) { |
| 588 | return x > y ? x : y; |
| 589 | } |
| 590 | |
| 591 | /** @brief Compute the left channel volume */ |
| 592 | static double left(double v, double b) { |
| 593 | if(b > 0) /* volume = right */ |
| 594 | return v * (1 - b); |
| 595 | else /* volume = left */ |
| 596 | return v; |
| 597 | } |
| 598 | |
| 599 | /** @brief Compute the right channel volume */ |
| 600 | static double right(double v, double b) { |
| 601 | if(b > 0) /* volume = right */ |
| 602 | return v; |
| 603 | else /* volume = left */ |
| 604 | return v * (1 + b); |
| 605 | } |
| 606 | |
| 607 | /** @brief Compute the overall volume */ |
| 608 | static double volume(double l, double r) { |
| 609 | return max(l, r); |
| 610 | } |
| 611 | |
| 612 | /** @brief Compute the balance */ |
| 613 | static double balance(double l, double r) { |
| 614 | if(l > r) |
| 615 | return r / l - 1; |
| 616 | else if(r > l) |
| 617 | return 1 - l / r; |
| 618 | else /* left = right */ |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | /** @brief Called to enable jukebox playback */ |
| 623 | static int enable_playing(disorder_eclient *c, |
| 624 | disorder_eclient_no_response *completed, void *v) { |
| 625 | last_state |= DISORDER_PLAYING; |
| 626 | return disorder_eclient_enable(c, completed, v); |
| 627 | } |
| 628 | |
| 629 | /** @brief Called to disable jukebox playback */ |
| 630 | static int disable_playing(disorder_eclient *c, |
| 631 | disorder_eclient_no_response *completed, void *v) { |
| 632 | last_state &= ~DISORDER_PLAYING; |
| 633 | return disorder_eclient_disable(c, completed, v); |
| 634 | } |
| 635 | |
| 636 | /** @brief Called to enable random selection */ |
| 637 | static int enable_random(disorder_eclient *c, |
| 638 | disorder_eclient_no_response *completed, void *v) { |
| 639 | last_state |= DISORDER_RANDOM_ENABLED; |
| 640 | return disorder_eclient_random_enable(c, completed, v); |
| 641 | } |
| 642 | |
| 643 | /** @brief Called to disable random selection */ |
| 644 | static int disable_random(disorder_eclient *c, |
| 645 | disorder_eclient_no_response *completed, void *v) { |
| 646 | last_state &= ~DISORDER_RANDOM_ENABLED; |
| 647 | return disorder_eclient_random_disable(c, completed, v); |
| 648 | } |
| 649 | |
| 650 | /** @brief Called to pause the current track */ |
| 651 | static int pause_track(disorder_eclient *c, |
| 652 | disorder_eclient_no_response *completed, void *v) { |
| 653 | last_state |= DISORDER_TRACK_PAUSED; |
| 654 | return disorder_eclient_pause(c, completed, v); |
| 655 | } |
| 656 | |
| 657 | /** @brief Called to resume the current track */ |
| 658 | static int resume_track(disorder_eclient *c, |
| 659 | disorder_eclient_no_response *completed, void *v) { |
| 660 | last_state &= ~DISORDER_TRACK_PAUSED; |
| 661 | return disorder_eclient_resume(c, completed, v); |
| 662 | } |
| 663 | |
| 664 | /** @brief Called to enable RTP play |
| 665 | * |
| 666 | * Rather odd signature is to fit in with the other icons which all call @ref |
| 667 | * lib/eclient.h functions. |
| 668 | */ |
| 669 | static int enable_rtp(disorder_eclient attribute((unused)) *c, |
| 670 | disorder_eclient_no_response attribute((unused)) *completed, |
| 671 | void attribute((unused)) *v) { |
| 672 | start_rtp(); |
| 673 | rtp_is_running = 1; |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | /** @brief Called to disable RTP play |
| 678 | * |
| 679 | * Rather odd signature is to fit in with the other icons which all call @ref |
| 680 | * lib/eclient.h functions. |
| 681 | */ |
| 682 | static int disable_rtp(disorder_eclient attribute((unused)) *c, |
| 683 | disorder_eclient_no_response attribute((unused)) *completed, |
| 684 | void attribute((unused)) *v) { |
| 685 | stop_rtp(); |
| 686 | rtp_is_running = 0; |
| 687 | return 0; |
| 688 | } |
| 689 | |
| 690 | static void control_minimode(const char attribute((unused)) *event, |
| 691 | void attribute((unused)) *evendata, |
| 692 | void attribute((unused)) *callbackdata) { |
| 693 | if(full_mode && volume_supported()) { |
| 694 | gtk_widget_show(balance_widget); |
| 695 | gtk_scale_set_value_pos(GTK_SCALE(volume_widget), GTK_POS_TOP); |
| 696 | } else { |
| 697 | gtk_widget_hide(balance_widget); |
| 698 | gtk_scale_set_value_pos(GTK_SCALE(volume_widget), GTK_POS_RIGHT); |
| 699 | } |
| 700 | if(full_mode) gtk_toolbar_unset_style(GTK_TOOLBAR(toolbar)); |
| 701 | else gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS); |
| 702 | } |
| 703 | |
| 704 | /* |
| 705 | Local Variables: |
| 706 | c-basic-offset:2 |
| 707 | comment-column:40 |
| 708 | fill-column:79 |
| 709 | indent-tabs-mode:nil |
| 710 | End: |
| 711 | */ |