static void print_playing_song_default (DBusGProxy *shell_proxy, DBusGProxy *player_proxy) { gboolean errored; GHashTable *properties; char *string; GError *error = NULL; properties = get_playing_song_info (shell_proxy, player_proxy, &error); if (annoy (&error)) { return; } if (properties == NULL) { g_print ("%s\n", _("Not playing")); return; } if (g_hash_table_lookup (properties, "rb:stream-song-title") != NULL) { string = parse_pattern ("%st (%tt)", properties, 0); } else { string = parse_pattern ("%ta - %tt", properties, 0); } g_print ("%s\n", string); g_hash_table_destroy (properties); g_free (string); }
static void print_playing_song (DBusGProxy *shell_proxy, DBusGProxy *player_proxy, const char *format) { gboolean errored; GHashTable *properties; guint elapsed = 0; GError *error = NULL; char *string; properties = get_playing_song_info (shell_proxy, player_proxy, &error); if (annoy (&error)) { return; } if (properties == NULL) { g_print ("%s\n", _("Not playing")); return; } org_gnome_Rhythmbox_Player_get_elapsed (player_proxy, &elapsed, &error); annoy (&error); string = parse_pattern (format, properties, elapsed); g_print ("%s\n", string); g_hash_table_destroy (properties); g_free (string); }
static void print_playing_song (GDBusProxy *mpris, const char *format) { GHashTable *properties; GVariant *v; gint64 elapsed = 0; char *string; properties = get_playing_song_info (mpris); if (properties == NULL) { g_print ("%s\n", _("Not playing")); return; } v = g_dbus_proxy_get_cached_property (mpris, "Position"); if (v != NULL) { elapsed = g_variant_get_int64 (v); g_variant_unref (v); } string = parse_pattern (format, properties, elapsed); g_print ("%s\n", string); g_hash_table_destroy (properties); g_free (string); }
static void rate_song (GDBusProxy *mpris, gdouble song_rating) { GHashTable *properties; GVariantBuilder props; GVariant *v; GError *error = NULL; properties = get_playing_song_info (mpris); if (properties == NULL) { rb_debug ("can't set rating when not playing"); return; } v = g_hash_table_lookup (properties, "xesam:url"); if (v == NULL) { rb_debug ("can't set rating, no url"); return; } g_variant_builder_init (&props, G_VARIANT_TYPE ("a{sv}")); g_variant_builder_add (&props, "{sv}", "rating", g_variant_new_double (song_rating)); g_dbus_connection_call_sync (g_dbus_proxy_get_connection (mpris), "org.gnome.Rhythmbox3", "/org/gnome/Rhythmbox3/RhythmDB", "org.gnome.Rhythmbox3.RhythmDB", "SetEntryProperties", g_variant_new ("(sa{sv})", g_variant_get_string (v, NULL), &props), NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); if (error != NULL) { g_warning ("Error setting rating on %s: %s", g_variant_get_string (v, NULL), error->message); g_clear_error (&error); } g_hash_table_destroy (properties); }
static void print_playing_song_default (GDBusProxy *mpris) { GHashTable *properties; char *string; properties = get_playing_song_info (mpris); if (properties == NULL) { g_print ("%s\n", _("Not playing")); return; } if (g_hash_table_lookup (properties, "rhythmbox:streamTitle") != NULL) { string = parse_pattern ("%st (%tt)", properties, 0); } else { string = parse_pattern ("%ta - %tt", properties, 0); } g_print ("%s\n", string); g_hash_table_destroy (properties); g_free (string); }
int main (int argc, char **argv) { GOptionContext *context; GError *error = NULL; GDBusConnection *bus; GDBusProxy *mpris; GDBusProxy *queue; GApplication *app; gboolean loaded; gboolean scanned; GVariant *state; #ifdef ENABLE_NLS /* initialize i18n */ bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); #endif /* setup */ setlocale (LC_ALL, ""); g_type_init (); g_set_prgname ("rhythmbox-client"); /* parse arguments */ context = g_option_context_new (NULL); g_option_context_add_main_entries (context, args, NULL); g_option_context_parse (context, &argc, &argv, &error); if (annoy (&error)) exit (1); rb_debug_init (debug); app = g_application_new ("org.gnome.Rhythmbox3", G_APPLICATION_IS_LAUNCHER); if (g_application_register (app, NULL, &error) == FALSE) { if (check_running) { rb_debug ("no running instance found"); exit (2); } else if (quit) { rb_debug ("no existing instance to quit"); exit (0); } rb_debug ("uh.. what?"); exit (0); } /* are we just checking if it's running? */ if (check_running) { rb_debug ("running instance found"); exit (0); } /* wait until it's ready to accept control */ state = g_action_group_get_action_state (G_ACTION_GROUP (app), "LoadURI"); if (state == NULL) { rb_debug ("couldn't get app startup state"); exit (0); } bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL); g_variant_get (state, "(bb)", &loaded, &scanned); if ((loaded && scanned) == FALSE) { GMainLoop *loop; GDBusProxy *app_proxy; rb_debug ("waiting for app startup"); loop = g_main_loop_new (NULL, FALSE); g_signal_connect (app, "action-state-changed", G_CALLBACK (state_changed_cb), loop); /* dbus implementation of GApplication doesn't do action state updates yet */ app_proxy = g_dbus_proxy_new_sync (bus, G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, NULL, "org.gnome.Rhythmbox3", "/org/gnome/Rhythmbox3", "org.gtk.Actions", NULL, &error); if (app_proxy == NULL || proxy_has_name_owner (app_proxy) == FALSE) { g_warning ("unable to wait for app startup: %s", error->message); g_clear_error (&error); } else { g_object_set_data (G_OBJECT (app_proxy), "actual-app", app); g_signal_connect (app_proxy, "g-signal", G_CALLBACK (state_changed_signal_cb), loop); g_main_loop_run (loop); rb_debug ("app is now started enough"); } } /* create proxies */ mpris = g_dbus_proxy_new_sync (bus, G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, NULL, "org.mpris.MediaPlayer2.rhythmbox", "/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2.Player", NULL, &error); if (mpris == NULL || proxy_has_name_owner (mpris) == FALSE) { g_warning ("MPRIS D-Bus interface not available, some things won't work"); if (next || previous || (seek != 0) || play || do_pause || play_pause || stop || volume_up || volume_down || (set_volume > -0.01)) { exit (1); } } queue = g_dbus_proxy_new_sync (bus, G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, NULL, "org.gnome.Rhythmbox3", "/org/gnome/Rhythmbox3/PlayQueue", "org.gnome.Rhythmbox3.PlayQueue", NULL, &error); if (queue == NULL || proxy_has_name_owner (queue) == FALSE) { g_warning ("Play queue interface not available, some things won't work"); if (enqueue || clear_queue) { exit (1); } } /* activate or quit */ if (quit) { rb_debug ("quitting existing instance"); g_action_group_activate_action (G_ACTION_GROUP (app), "Quit", NULL); exit (0); } /* don't present if we're doing something else */ if (next || previous || (seek != 0) || clear_queue || play_uri || other_stuff || play || do_pause || play_pause || stop || toggle_shuffle || print_playing || print_playing_format || (set_volume > -0.01) || volume_up || volume_down || print_volume /*|| mute || unmute*/ || (set_rating > -0.01)) no_present = TRUE; /* present */ if (!no_present) { g_application_activate (app); } /* set song rating */ if (set_rating >= 0.0 && set_rating <= 5.0) { rb_debug ("rate song"); rate_song (mpris, set_rating); } if (toggle_shuffle) { rb_debug("toggle shuffle"); g_action_group_activate_action (G_ACTION_GROUP (app), "ToggleShuffle", NULL); annoy(&error); } /* skip to next or previous track */ if (next) { rb_debug ("next track"); g_dbus_proxy_call_sync (mpris, "Next", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); annoy (&error); } else if (previous) { rb_debug ("previous track"); g_dbus_proxy_call_sync (mpris, "Previous", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); annoy (&error); } /* seek in track */ if (seek != 0) { GHashTable *properties; rb_debug ("seek"); properties = get_playing_song_info (mpris); if (properties != NULL) { GVariant *v = g_hash_table_lookup (properties, "mpris:trackid"); if (v != NULL) { g_dbus_proxy_call_sync (mpris, "SetPosition", g_variant_new ("(ox)", g_variant_get_string (v, NULL), seek), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); annoy (&error); } } } /* add/enqueue */ if (clear_queue) { g_dbus_proxy_call_sync (queue, "ClearQueue", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); annoy (&error); } if (other_stuff) { int i; for (i = 0; other_stuff[i] != NULL; i++) { GFile *file; char *fileuri; file = g_file_new_for_commandline_arg (other_stuff[i]); fileuri = g_file_get_uri (file); if (fileuri == NULL) { g_warning ("couldn't convert \"%s\" to a URI", other_stuff[i]); continue; } if (enqueue) { rb_debug ("enqueueing %s", fileuri); g_dbus_proxy_call_sync (queue, "AddToQueue", g_variant_new ("(s)", fileuri), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); annoy (&error); } else { rb_debug ("importing %s", fileuri); g_action_group_activate_action (G_ACTION_GROUP (app), "LoadURI", g_variant_new ("(sb)", fileuri, FALSE)); } g_free (fileuri); g_object_unref (file); } } /* select/activate/play source */ if (select_source) { rb_debug ("selecting source %s", select_source); g_action_group_activate_action (G_ACTION_GROUP (app), "ActivateSource", g_variant_new ("(su)", select_source, 0)); } else if (activate_source) { rb_debug ("activating source %s", activate_source); g_action_group_activate_action (G_ACTION_GROUP (app), "ActivateSource", g_variant_new ("(su)", activate_source, 1)); } else if (play_source) { rb_debug ("playing source %s", play_source); g_action_group_activate_action (G_ACTION_GROUP (app), "ActivateSource", g_variant_new ("(su)", play_source, 2)); } /* play uri */ if (play_uri) { GFile *file; char *fileuri; file = g_file_new_for_commandline_arg (play_uri); fileuri = g_file_get_uri (file); if (fileuri == NULL) { g_warning ("couldn't convert \"%s\" to a URI", play_uri); } else { rb_debug ("loading and playing %s", fileuri); g_action_group_activate_action (G_ACTION_GROUP (app), "LoadURI", g_variant_new ("(sb)", fileuri, TRUE)); annoy (&error); } g_free (fileuri); g_object_unref (file); } /* play/pause/stop */ if (mpris) { GVariant *v; gboolean is_playing = FALSE; v = g_dbus_proxy_get_cached_property (mpris, "PlaybackStatus"); if (v != NULL) { is_playing = (g_strcmp0 (g_variant_get_string (v, NULL), "Playing") == 0); g_variant_unref (v); } if (play || do_pause || play_pause) { if (is_playing != play || play_pause) { rb_debug ("calling PlayPause to change playback state"); g_dbus_proxy_call_sync (mpris, "PlayPause", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); annoy (&error); } else { rb_debug ("no need to change playback state"); } } else if (stop) { g_warning ("not implemented yet"); } } /* get/set volume, mute/unmute */ if (set_volume > -0.01) { g_dbus_proxy_call_sync (mpris, "org.freedesktop.DBus.Properties.Set", g_variant_new ("(ssv)", "org.mpris.MediaPlayer2.Player", "Volume", g_variant_new_double (set_volume)), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); annoy (&error); } else if (volume_up || volume_down) { GVariant *v; v = g_dbus_proxy_get_cached_property (mpris, "Volume"); if (v != NULL) { set_volume = g_variant_get_double (v) + (volume_up ? 0.1 : -0.1); g_dbus_proxy_call_sync (mpris, "org.freedesktop.DBus.Properties.Set", g_variant_new ("(ssv)", "org.mpris.MediaPlayer2.Player", "Volume", g_variant_new_double (set_volume)), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); annoy (&error); g_variant_unref (v); } } /* no mute for now? */ /* } else if (unmute || mute) { org_gnome_Rhythmbox_Player_set_mute (player_proxy, unmute ? FALSE : TRUE, &error); annoy (&error); } */ if (print_volume) { gdouble volume = 1.0; GVariant *v = g_dbus_proxy_get_cached_property (mpris, "Volume"); if (v != NULL) { volume = g_variant_get_double (v); g_variant_unref (v); } g_print (_("Playback volume is %f.\n"), volume); } /* print playing song */ if (print_playing_format) { print_playing_song (mpris, print_playing_format); } else if (print_playing) { print_playing_song_default (mpris); } if (mpris) { g_object_unref (mpris); } g_dbus_connection_flush_sync (bus, NULL, NULL); g_option_context_free (context); return 0; }