Skip to content

Commit

Permalink
Merge pull request #164 from altdesktop/playerctld
Browse files Browse the repository at this point in the history
add the playerctl daemon
  • Loading branch information
Tony Crisci authored Jan 23, 2020
2 parents 2b162bb + 2636163 commit 3b11dec
Show file tree
Hide file tree
Showing 5 changed files with 449 additions and 57 deletions.
8 changes: 8 additions & 0 deletions playerctl/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ playerctl_executable = executable(
install: true,
)

playerctld_executable = executable(
'playerctld',
'playerctl-daemon.c', enums,
dependencies: deps,
include_directories: configuration_inc,
install: true,
)

install_headers(
headers,
install_dir: join_paths(get_option('includedir'), 'playerctl'),
Expand Down
54 changes: 54 additions & 0 deletions playerctl/playerctl-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,57 @@ GList *pctl_player_name_find_instance(GList *list, gchar *player_id, PlayerctlSo
void pctl_player_name_list_destroy(GList *list) {
g_list_free_full(list, (GDestroyNotify)playerctl_player_name_free);
}

GList *pctl_list_player_names_on_bus(GBusType bus_type, GError **err) {
GError *tmp_error = NULL;
GList *players = NULL;

GDBusProxy *proxy = g_dbus_proxy_new_for_bus_sync(
bus_type, G_DBUS_PROXY_FLAGS_NONE, NULL, "org.freedesktop.DBus", "/org/freedesktop/DBus",
"org.freedesktop.DBus", NULL, &tmp_error);

if (tmp_error != NULL) {
if (tmp_error->domain == G_IO_ERROR && tmp_error->code == G_IO_ERROR_NOT_FOUND) {
// XXX: This means the dbus socket address is not found which may
// mean that the bus is not running or the address was set
// incorrectly. I think we can pass through here because it is true
// that there are no names on the bus that is supposed to be at
// this socket path. But we need a better way of dealing with this case.
g_warning("D-Bus socket address not found, unable to list player names");
g_clear_error(&tmp_error);
return NULL;
}
g_propagate_error(err, tmp_error);
return NULL;
}

g_debug("Getting list of player names from D-Bus");
GVariant *reply = g_dbus_proxy_call_sync(proxy, "ListNames", NULL, G_DBUS_CALL_FLAGS_NONE, -1,
NULL, &tmp_error);

if (tmp_error != NULL) {
g_propagate_error(err, tmp_error);
g_object_unref(proxy);
return NULL;
}

GVariant *reply_child = g_variant_get_child_value(reply, 0);
gsize reply_count;
const gchar **names = g_variant_get_strv(reply_child, &reply_count);

size_t offset = strlen(MPRIS_PREFIX);
for (gsize i = 0; i < reply_count; i += 1) {
if (g_str_has_prefix(names[i], MPRIS_PREFIX)) {
PlayerctlPlayerName *player_name =
pctl_player_name_new(names[i] + offset, pctl_bus_type_to_source(bus_type));
players = g_list_append(players, player_name);
}
}

g_object_unref(proxy);
g_variant_unref(reply);
g_variant_unref(reply_child);
g_free(names);

return players;
}
2 changes: 2 additions & 0 deletions playerctl/playerctl-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,7 @@ GList *pctl_player_name_find_instance(GList *list, gchar *player_id, PlayerctlSo

void pctl_player_name_list_destroy(GList *list);

GList *pctl_list_player_names_on_bus(GBusType bus_type, GError **err);

#undef __PLAYERCTL_COMMON_H__
#endif
Loading

0 comments on commit 3b11dec

Please sign in to comment.