clang-format the project

This commit is contained in:
Tony Crisci
2018-09-26 10:36:54 -04:00
parent a3694bfa79
commit 2e3b24f36e
6 changed files with 972 additions and 973 deletions

11
.clang-format Normal file
View File

@@ -0,0 +1,11 @@
BasedOnStyle: google
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortBlocksOnASingleLine: false
AlwaysBreakBeforeMultilineStrings: false
IndentWidth: 4
PointerBindsToType: false
ColumnLimit: 100
SpaceBeforeParens: ControlStatements
IndentCaseLabels: false

View File

@@ -1,5 +1,4 @@
/* vim:ts=2:sw=2:expandtab /*
*
* This file is part of playerctl. * This file is part of playerctl.
* *
* playerctl is free software: you can redistribute it and/or modify it under * playerctl is free software: you can redistribute it and/or modify it under
@@ -18,12 +17,12 @@
* Copyright © 2014 - 2016, Tony Crisci and contributors. * Copyright © 2014 - 2016, Tony Crisci and contributors.
*/ */
#include "playerctl.h"
#include <stdlib.h>
#include <stdio.h>
#include <gio/gio.h> #include <gio/gio.h>
#include <locale.h> #include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "playerctl.h"
#define LENGTH(array) (sizeof array / sizeof array[0]) #define LENGTH(array) (sizeof array / sizeof array[0])
@@ -40,32 +39,20 @@ static gboolean print_version_and_exit;
/* The commands passed on the command line, filled in via G_OPTION_REMAINING. */ /* The commands passed on the command line, filled in via G_OPTION_REMAINING. */
static gchar **command = NULL; static gchar **command = NULL;
static GString *list_player_names_on_bus(GError **err, GBusType busType) static GString *list_player_names_on_bus(GError **err, GBusType busType) {
{
GError *tmp_error = NULL; GError *tmp_error = NULL;
GDBusProxy *proxy = g_dbus_proxy_new_for_bus_sync( GDBusProxy *proxy = g_dbus_proxy_new_for_bus_sync(
busType, busType, G_DBUS_PROXY_FLAGS_NONE, NULL, "org.freedesktop.DBus",
G_DBUS_PROXY_FLAGS_NONE, "/org/freedesktop/DBus", "org.freedesktop.DBus", NULL, &tmp_error);
NULL,
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
NULL,
&tmp_error);
if (tmp_error != NULL) { if (tmp_error != NULL) {
g_propagate_error(err, tmp_error); g_propagate_error(err, tmp_error);
return NULL; return NULL;
} }
GVariant *reply = g_dbus_proxy_call_sync(proxy, GVariant *reply = g_dbus_proxy_call_sync(
"ListNames", proxy, "ListNames", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &tmp_error);
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&tmp_error);
if (tmp_error != NULL) { if (tmp_error != NULL) {
g_propagate_error(err, tmp_error); g_propagate_error(err, tmp_error);
@@ -78,7 +65,7 @@ static GString *list_player_names_on_bus(GError **err, GBusType busType)
gsize reply_count; gsize reply_count;
const gchar **names = g_variant_get_strv(reply_child, &reply_count); const gchar **names = g_variant_get_strv(reply_child, &reply_count);
size_t offset = strlen ("org.mpris.MediaPlayer2."); size_t offset = strlen("org.mpris.MediaPlayer2.");
for (int i = 0; i < reply_count; i += 1) { for (int i = 0; i < reply_count; i += 1) {
if (g_str_has_prefix(names[i], "org.mpris.MediaPlayer2.")) { if (g_str_has_prefix(names[i], "org.mpris.MediaPlayer2.")) {
g_string_append_printf(names_str, "%s\n", names[i] + offset); g_string_append_printf(names_str, "%s\n", names[i] + offset);
@@ -110,7 +97,6 @@ static gchar *list_player_names(GError **err) {
return g_string_free(sessionPlayers, FALSE); return g_string_free(sessionPlayers, FALSE);
} }
#define PLAYER_COMMAND_FUNC(COMMAND) \ #define PLAYER_COMMAND_FUNC(COMMAND) \
GError *tmp_error = NULL; \ GError *tmp_error = NULL; \
\ \
@@ -121,45 +107,47 @@ static gchar *list_player_names(GError **err) {
} \ } \
return TRUE; return TRUE;
static gboolean play (PlayerctlPlayer *player, gchar **arguments, GError **error) static gboolean play(PlayerctlPlayer *player, gchar **arguments,
{ GError **error) {
PLAYER_COMMAND_FUNC(play); PLAYER_COMMAND_FUNC(play);
} }
/* Pause is defined in unistd.h */ /* Pause is defined in unistd.h */
static gboolean paus (PlayerctlPlayer *player, gchar **arguments, GError **error) static gboolean paus(PlayerctlPlayer *player, gchar **arguments,
{ GError **error) {
PLAYER_COMMAND_FUNC(pause); PLAYER_COMMAND_FUNC(pause);
} }
static gboolean play_pause (PlayerctlPlayer *player, gchar **arguments, GError **error) static gboolean play_pause(PlayerctlPlayer *player, gchar **arguments,
{ GError **error) {
PLAYER_COMMAND_FUNC(play_pause); PLAYER_COMMAND_FUNC(play_pause);
} }
static gboolean stop (PlayerctlPlayer *player, gchar **arguments, GError **error) static gboolean stop(PlayerctlPlayer *player, gchar **arguments,
{ GError **error) {
PLAYER_COMMAND_FUNC(stop); PLAYER_COMMAND_FUNC(stop);
} }
static gboolean next (PlayerctlPlayer *player, gchar **arguments, GError **error) static gboolean next(PlayerctlPlayer *player, gchar **arguments,
{ GError **error) {
PLAYER_COMMAND_FUNC(next); PLAYER_COMMAND_FUNC(next);
} }
static gboolean previous (PlayerctlPlayer *player, gchar **arguments, GError **error) static gboolean previous(PlayerctlPlayer *player, gchar **arguments,
{ GError **error) {
PLAYER_COMMAND_FUNC(previous); PLAYER_COMMAND_FUNC(previous);
} }
#undef PLAYER_COMMAND_FUNC #undef PLAYER_COMMAND_FUNC
static gboolean open (PlayerctlPlayer *player, gchar **arguments, GError **error) static gboolean open(PlayerctlPlayer *player, gchar **arguments,
{ GError **error) {
const gchar *uri = *arguments; const gchar *uri = *arguments;
GError *tmp_error = NULL; GError *tmp_error = NULL;
if (uri) { if (uri) {
playerctl_player_open(player, g_file_get_uri(g_file_new_for_commandline_arg(uri)), &tmp_error); playerctl_player_open(player,
g_file_get_uri(g_file_new_for_commandline_arg(uri)),
&tmp_error);
if (tmp_error != NULL) { if (tmp_error != NULL) {
g_propagate_error(error, tmp_error); g_propagate_error(error, tmp_error);
return FALSE; return FALSE;
@@ -168,8 +156,8 @@ static gboolean open (PlayerctlPlayer *player, gchar **arguments, GError **error
return TRUE; return TRUE;
} }
static gboolean position (PlayerctlPlayer *player, gchar **arguments, GError **error) static gboolean position(PlayerctlPlayer *player, gchar **arguments,
{ GError **error) {
const gchar *position = *arguments; const gchar *position = *arguments;
gint64 offset; gint64 offset;
GError *tmp_error = NULL; GError *tmp_error = NULL;
@@ -179,7 +167,8 @@ static gboolean position (PlayerctlPlayer *player, gchar **arguments, GError **e
offset = 1000000.0 * strtod(position, &endptr); offset = 1000000.0 * strtod(position, &endptr);
if (position == endptr) { if (position == endptr) {
g_set_error(error, playerctl_cli_error_quark (), 1, "Could not parse position as a number: %s\n", position); g_set_error(error, playerctl_cli_error_quark(), 1,
"Could not parse position as a number: %s\n", position);
return FALSE; return FALSE;
} }
@@ -209,8 +198,8 @@ static gboolean position (PlayerctlPlayer *player, gchar **arguments, GError **e
return TRUE; return TRUE;
} }
static gboolean set_or_get_volume (PlayerctlPlayer *player, gchar **arguments, GError **error) static gboolean set_or_get_volume(PlayerctlPlayer *player, gchar **arguments,
{ GError **error) {
const gchar *volume = *arguments; const gchar *volume = *arguments;
gdouble level; gdouble level;
@@ -218,15 +207,16 @@ static gboolean set_or_get_volume (PlayerctlPlayer *player, gchar **arguments, G
char *endptr = NULL; char *endptr = NULL;
size_t last = strlen(volume) - 1; size_t last = strlen(volume) - 1;
if(volume[last] == '+' || volume[last] == '-') { if (volume[last] == '+' || volume[last] == '-') {
gdouble adjustment = strtod(volume, &endptr); gdouble adjustment = strtod(volume, &endptr);
if (volume == endptr) { if (volume == endptr) {
g_set_error(error, playerctl_cli_error_quark (), 1, "Could not parse volume as a number: %s\n", volume); g_set_error(error, playerctl_cli_error_quark(), 1,
"Could not parse volume as a number: %s\n", volume);
return FALSE; return FALSE;
} }
if(volume[last] == '-') { if (volume[last] == '-') {
adjustment *= -1; adjustment *= -1;
} }
@@ -235,10 +225,10 @@ static gboolean set_or_get_volume (PlayerctlPlayer *player, gchar **arguments, G
} else { } else {
level = strtod(volume, &endptr); level = strtod(volume, &endptr);
if (volume == endptr) { if (volume == endptr) {
g_set_error(error, playerctl_cli_error_quark (), 1, "Could not parse volume as a number: %s\n", volume); g_set_error(error, playerctl_cli_error_quark(), 1,
"Could not parse volume as a number: %s\n", volume);
return FALSE; return FALSE;
} }
} }
g_object_set(player, "volume", level, NULL); g_object_set(player, "volume", level, NULL);
} else { } else {
@@ -249,8 +239,8 @@ static gboolean set_or_get_volume (PlayerctlPlayer *player, gchar **arguments, G
return TRUE; return TRUE;
} }
static gboolean status (PlayerctlPlayer *player, gchar **arguments, GError **error) static gboolean status(PlayerctlPlayer *player, gchar **arguments,
{ GError **error) {
gchar *state = NULL; gchar *state = NULL;
g_object_get(player, "status", &state, NULL); g_object_get(player, "status", &state, NULL);
@@ -260,8 +250,8 @@ static gboolean status (PlayerctlPlayer *player, gchar **arguments, GError **err
return TRUE; return TRUE;
} }
static gboolean get_metadata (PlayerctlPlayer *player, gchar **arguments, GError **error) static gboolean get_metadata(PlayerctlPlayer *player, gchar **arguments,
{ GError **error) {
const gchar *type = *arguments; const gchar *type = *arguments;
GError *tmp_error = NULL; GError *tmp_error = NULL;
gchar *data; gchar *data;
@@ -288,63 +278,76 @@ static gboolean get_metadata (PlayerctlPlayer *player, gchar **arguments, GError
struct PlayerCommand { struct PlayerCommand {
const gchar *name; const gchar *name;
gboolean (*func) (PlayerctlPlayer *player, gchar **arguments, GError **error); gboolean (*func)(PlayerctlPlayer *player, gchar **arguments, GError **error);
} commands[] = { } commands[] = {
{ "open", &open }, {"open", &open},
{ "play", &play }, {"play", &play},
{ "pause", &paus }, {"pause", &paus},
{ "play-pause", &play_pause }, {"play-pause", &play_pause},
{ "stop", &stop }, {"stop", &stop},
{ "next", &next }, {"next", &next},
{ "previous", &previous }, {"previous", &previous},
{ "position", &position }, {"position", &position},
{ "volume", &set_or_get_volume }, {"volume", &set_or_get_volume},
{ "status", &status }, {"status", &status},
{ "metadata", &get_metadata }, {"metadata", &get_metadata},
}; };
static gboolean handle_player_command (PlayerctlPlayer *player, gchar **command, GError **error) static gboolean handle_player_command(PlayerctlPlayer *player, gchar **command,
{ GError **error) {
for (int i = 0; i < LENGTH(commands); i++) { for (int i = 0; i < LENGTH(commands); i++) {
if (g_strcmp0(commands[i].name, command[0]) == 0) { if (g_strcmp0(commands[i].name, command[0]) == 0) {
// Do not pass the command's name to the function that processes it. // Do not pass the command's name to the function that processes it.
return commands[i].func(player, command + 1, error); return commands[i].func(player, command + 1, error);
} }
} }
g_set_error(error, playerctl_cli_error_quark (), 1, "Command not recognized: %s", command[0]); g_set_error(error, playerctl_cli_error_quark(), 1,
"Command not recognized: %s", command[0]);
return FALSE; return FALSE;
} }
static const GOptionEntry entries[] = { static const GOptionEntry entries[] = {
{ "player", 'p', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &player_name_list, {"player", 'p', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &player_name_list,
"A comma separated list of names of players to control (default: the first available player)", "NAME" }, "A comma separated list of names of players to control (default: the "
{ "all-players", 'a', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &select_all_players, "first available player)",
"Select all available players to be controlled", NULL }, "NAME"},
{ "list-all", 'l', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &list_all_players_and_exit, {"all-players", 'a', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE,
"List the names of running players that can be controlled and exit", NULL }, &select_all_players, "Select all available players to be controlled",
{ "version", 'v', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &print_version_and_exit, NULL},
"Print version information and exit", NULL }, {"list-all", 'l', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE,
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &command, NULL, "COMMAND" }, &list_all_players_and_exit,
{ NULL } "List the names of running players that can be controlled and exit", NULL},
}; {"version", 'v', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE,
&print_version_and_exit, "Print version information and exit", NULL},
{G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &command, NULL,
"COMMAND"},
{NULL}};
static gboolean parse_setup_options (int argc, char *argv[], GError **error) static gboolean parse_setup_options(int argc, char *argv[], GError **error) {
{ static const gchar *description =
static const gchar *description = "Available Commands:" "Available Commands:"
"\n open [URI] Command for the player to open given URI." "\n open [URI] Command for the player to open given URI."
"\n URI can be either file path or remote URL with mandatory scheme, like http://..." "\n URI can be either file path or remote URL "
"with mandatory scheme, like http://..."
"\n play Command the player to play" "\n play Command the player to play"
"\n pause Command the player to pause" "\n pause Command the player to pause"
"\n play-pause Command the player to toggle between play/pause" "\n play-pause Command the player to toggle between "
"play/pause"
"\n stop Command the player to stop" "\n stop Command the player to stop"
"\n next Command the player to skip to the next track" "\n next Command the player to skip to the next track"
"\n previous Command the player to skip to the previous track" "\n previous Command the player to skip to the previous "
"\n position [OFFSET][+/-] Command the player to go to the position or seek forward/backward OFFSET in seconds" "track"
"\n volume [LEVEL][+/-] Print or set the volume to LEVEL from 0.0 to 1.0" "\n position [OFFSET][+/-] Command the player to go to the position or "
"seek forward/backward OFFSET in seconds"
"\n volume [LEVEL][+/-] Print or set the volume to LEVEL from 0.0 "
"to 1.0"
"\n status Get the play status of the player" "\n status Get the play status of the player"
"\n metadata [KEY] Print metadata information for the current track. If KEY is passed," "\n metadata [KEY] Print metadata information for the current "
"\n print only that value. KEY may be one of artist, title or album"; "track. If KEY is passed,"
static const gchar *summary = " Only for players supporting the MPRIS D-Bus specification"; "\n print only that value. KEY may be one of "
"artist, title or album";
static const gchar *summary =
" Only for players supporting the MPRIS D-Bus specification";
GOptionContext *context = NULL; GOptionContext *context = NULL;
gboolean success; gboolean success;
@@ -360,9 +363,11 @@ static gboolean parse_setup_options (int argc, char *argv[], GError **error)
return FALSE; return FALSE;
} }
if (command == NULL && !print_version_and_exit && !list_all_players_and_exit) { if (command == NULL && !print_version_and_exit &&
!list_all_players_and_exit) {
gchar *help = g_option_context_get_help(context, TRUE, NULL); gchar *help = g_option_context_get_help(context, TRUE, NULL);
g_set_error (error, playerctl_cli_error_quark(), 1, "No command entered\n\n%s", help); g_set_error(error, playerctl_cli_error_quark(), 1,
"No command entered\n\n%s", help);
g_option_context_free(context); g_option_context_free(context);
g_free(help); g_free(help);
return FALSE; return FALSE;
@@ -372,8 +377,7 @@ static gboolean parse_setup_options (int argc, char *argv[], GError **error)
return TRUE; return TRUE;
} }
int main (int argc, char *argv[]) int main(int argc, char *argv[]) {
{
PlayerctlPlayer *player; PlayerctlPlayer *player;
GError *error = NULL; GError *error = NULL;
int exit_status = 0; int exit_status = 0;
@@ -441,7 +445,7 @@ int main (int argc, char *argv[])
exit_status = 1; exit_status = 1;
} }
loopend: loopend:
if (player != NULL) { if (player != NULL) {
g_object_unref(player); g_object_unref(player);
} }
@@ -461,4 +465,3 @@ end:
return exit_status; return exit_status;
} }

View File

@@ -1,5 +1,4 @@
/* vim:ts=2:sw=2:expandtab /*
*
* This file is part of playerctl. * This file is part of playerctl.
* *
* playerctl is free software: you can redistribute it and/or modify it under * playerctl is free software: you can redistribute it and/or modify it under
@@ -18,12 +17,12 @@
* Copyright © 2014 - 2016, Tony Crisci and contributors. * Copyright © 2014 - 2016, Tony Crisci and contributors.
*/ */
#include <string.h>
#include <gio/gio.h> #include <gio/gio.h>
#include <glib-object.h> #include <glib-object.h>
#include <string.h>
#include "playerctl-player.h"
#include "playerctl-generated.h" #include "playerctl-generated.h"
#include "playerctl-player.h"
#include <stdint.h> #include <stdint.h>
@@ -40,7 +39,7 @@ enum {
}; };
enum { enum {
//PROPERTIES_CHANGED, // PROPERTIES_CHANGED,
PLAY, PLAY,
PAUSE, PAUSE,
STOP, STOP,
@@ -49,12 +48,13 @@ enum {
LAST_SIGNAL LAST_SIGNAL
}; };
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, }; static GParamSpec *obj_properties[N_PROPERTIES] = {
NULL,
};
static guint connection_signals[LAST_SIGNAL] = {0}; static guint connection_signals[LAST_SIGNAL] = {0};
struct _PlayerctlPlayerPrivate struct _PlayerctlPlayerPrivate {
{
OrgMprisMediaPlayer2Player *proxy; OrgMprisMediaPlayer2Player *proxy;
gchar *player_name; gchar *player_name;
gchar *bus_name; gchar *bus_name;
@@ -63,12 +63,15 @@ struct _PlayerctlPlayerPrivate
GVariant *metadata; GVariant *metadata;
}; };
static void playerctl_player_properties_changed_callback (GDBusProxy *_proxy, GVariant *changed_properties, const gchar *const *invalidated_properties, gpointer user_data) static void playerctl_player_properties_changed_callback(
{ GDBusProxy *_proxy, GVariant *changed_properties,
const gchar *const *invalidated_properties, gpointer user_data) {
PlayerctlPlayer *self = user_data; PlayerctlPlayer *self = user_data;
GVariant *metadata = g_variant_lookup_value(changed_properties, "Metadata", NULL); GVariant *metadata =
GVariant *playback_status = g_variant_lookup_value(changed_properties, "PlaybackStatus", NULL); g_variant_lookup_value(changed_properties, "Metadata", NULL);
GVariant *playback_status =
g_variant_lookup_value(changed_properties, "PlaybackStatus", NULL);
if (metadata) { if (metadata) {
g_signal_emit(self, connection_signals[METADATA], 0, metadata); g_signal_emit(self, connection_signals[METADATA], 0, metadata);
@@ -95,13 +98,15 @@ static void playerctl_player_properties_changed_callback (GDBusProxy *_proxy, GV
static void playerctl_player_initable_iface_init(GInitableIface *iface); static void playerctl_player_initable_iface_init(GInitableIface *iface);
G_DEFINE_TYPE_WITH_CODE (PlayerctlPlayer, playerctl_player, G_TYPE_OBJECT, G_DEFINE_TYPE_WITH_CODE(PlayerctlPlayer, playerctl_player, G_TYPE_OBJECT,
G_ADD_PRIVATE(PlayerctlPlayer) G_IMPLEMENT_INTERFACE(G_TYPE_INITABLE, playerctl_player_initable_iface_init)); G_ADD_PRIVATE(PlayerctlPlayer) G_IMPLEMENT_INTERFACE(
G_TYPE_INITABLE,
playerctl_player_initable_iface_init));
G_DEFINE_QUARK(playerctl-player-error-quark, playerctl_player_error); G_DEFINE_QUARK(playerctl-player-error-quark, playerctl_player_error);
static GVariant *playerctl_player_get_metadata(PlayerctlPlayer *self, GError **err) static GVariant *playerctl_player_get_metadata(PlayerctlPlayer *self,
{ GError **err) {
GVariant *metadata; GVariant *metadata;
GError *tmp_error = NULL; GError *tmp_error = NULL;
@@ -111,15 +116,10 @@ static GVariant *playerctl_player_get_metadata(PlayerctlPlayer *self, GError **e
if (!metadata) { if (!metadata) {
// XXX: Ugly spotify workaround. Spotify does not seem to use the property // XXX: Ugly spotify workaround. Spotify does not seem to use the property
// cache. We have to get the properties directly. // cache. We have to get the properties directly.
GVariant *call_reply = g_dbus_proxy_call_sync (G_DBUS_PROXY(self->priv->proxy), GVariant *call_reply = g_dbus_proxy_call_sync(
"org.freedesktop.DBus.Properties.Get", G_DBUS_PROXY(self->priv->proxy), "org.freedesktop.DBus.Properties.Get",
g_variant_new ("(ss)", g_variant_new("(ss)", "org.mpris.MediaPlayer2.Player", "Metadata"),
"org.mpris.MediaPlayer2.Player", G_DBUS_CALL_FLAGS_NONE, -1, NULL, &tmp_error);
"Metadata"),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&tmp_error);
if (tmp_error != NULL) { if (tmp_error != NULL) {
g_propagate_error(err, tmp_error); g_propagate_error(err, tmp_error);
@@ -137,18 +137,20 @@ static GVariant *playerctl_player_get_metadata(PlayerctlPlayer *self, GError **e
return metadata; return metadata;
} }
static void playerctl_player_set_property(GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { static void playerctl_player_set_property(GObject *object, guint property_id,
const GValue *value,
GParamSpec *pspec) {
PlayerctlPlayer *self = PLAYERCTL_PLAYER(object); PlayerctlPlayer *self = PLAYERCTL_PLAYER(object);
switch (property_id) switch (property_id) {
{
case PROP_PLAYER_NAME: case PROP_PLAYER_NAME:
g_free(self->priv->player_name); g_free(self->priv->player_name);
self->priv->player_name = g_strdup(g_value_get_string(value)); self->priv->player_name = g_strdup(g_value_get_string(value));
break; break;
case PROP_VOLUME: case PROP_VOLUME:
org_mpris_media_player2_player_set_volume(self->priv->proxy, g_value_get_double(value)); org_mpris_media_player2_player_set_volume(self->priv->proxy,
g_value_get_double(value));
break; break;
default: default:
@@ -157,11 +159,11 @@ static void playerctl_player_set_property(GObject *object, guint property_id, co
} }
} }
static void playerctl_player_get_property(GObject *object, guint property_id, GValue *value, GParamSpec *pspec) { static void playerctl_player_get_property(GObject *object, guint property_id,
GValue *value, GParamSpec *pspec) {
PlayerctlPlayer *self = PLAYERCTL_PLAYER(object); PlayerctlPlayer *self = PLAYERCTL_PLAYER(object);
switch (property_id) switch (property_id) {
{
case PROP_PLAYER_NAME: case PROP_PLAYER_NAME:
g_value_set_string(value, self->priv->player_name); g_value_set_string(value, self->priv->player_name);
break; break;
@@ -169,14 +171,15 @@ static void playerctl_player_get_property(GObject *object, guint property_id, GV
case PROP_STATUS: case PROP_STATUS:
if (self->priv->proxy) { if (self->priv->proxy) {
g_main_context_iteration(NULL, FALSE); g_main_context_iteration(NULL, FALSE);
g_value_set_string(value, org_mpris_media_player2_player_get_playback_status(self->priv->proxy)); g_value_set_string(value,
org_mpris_media_player2_player_get_playback_status(
self->priv->proxy));
} else { } else {
g_value_set_string(value, ""); g_value_set_string(value, "");
} }
break; break;
case PROP_METADATA: case PROP_METADATA: {
{
GVariant *metadata = NULL; GVariant *metadata = NULL;
if (self->priv->proxy) if (self->priv->proxy)
@@ -193,7 +196,8 @@ static void playerctl_player_get_property(GObject *object, guint property_id, GV
case PROP_VOLUME: case PROP_VOLUME:
if (self->priv->proxy) { if (self->priv->proxy) {
g_main_context_iteration(NULL, FALSE); g_main_context_iteration(NULL, FALSE);
g_value_set_double(value, org_mpris_media_player2_player_get_volume(self->priv->proxy)); g_value_set_double(value, org_mpris_media_player2_player_get_volume(
self->priv->proxy));
} else { } else {
g_value_set_double(value, 0); g_value_set_double(value, 0);
} }
@@ -205,25 +209,25 @@ static void playerctl_player_get_property(GObject *object, guint property_id, GV
// connection is made so we have to call the method directly here or it // connection is made so we have to call the method directly here or it
// won't work. // won't work.
GError *tmp_error = NULL; GError *tmp_error = NULL;
GVariant *call_reply = g_dbus_proxy_call_sync (G_DBUS_PROXY(self->priv->proxy), GVariant *call_reply = g_dbus_proxy_call_sync(
G_DBUS_PROXY(self->priv->proxy),
"org.freedesktop.DBus.Properties.Get", "org.freedesktop.DBus.Properties.Get",
g_variant_new ("(ss)", g_variant_new("(ss)", "org.mpris.MediaPlayer2.Player", "Position"),
"org.mpris.MediaPlayer2.Player", G_DBUS_CALL_FLAGS_NONE, -1, NULL, &tmp_error);
"Position"),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&tmp_error);
if (tmp_error) { if (tmp_error) {
g_warning("Get position property failed. Using cache. (%s)", tmp_error->message); g_warning("Get position property failed. Using cache. (%s)",
gint64 cached_position = org_mpris_media_player2_player_get_position(self->priv->proxy); tmp_error->message);
gint64 cached_position =
org_mpris_media_player2_player_get_position(self->priv->proxy);
g_value_set_int64(value, cached_position); g_value_set_int64(value, cached_position);
break; break;
} }
GVariant *call_reply_properties = g_variant_get_child_value(call_reply, 0); GVariant *call_reply_properties =
GVariant *call_reply_unboxed = g_variant_get_variant(call_reply_properties); g_variant_get_child_value(call_reply, 0);
GVariant *call_reply_unboxed =
g_variant_get_variant(call_reply_properties);
gint64 position = g_variant_get_int64(call_reply_unboxed); gint64 position = g_variant_get_int64(call_reply_unboxed);
g_value_set_int64(value, position); g_value_set_int64(value, position);
@@ -242,8 +246,7 @@ static void playerctl_player_get_property(GObject *object, guint property_id, GV
} }
} }
static void playerctl_player_constructed(GObject *gobject) static void playerctl_player_constructed(GObject *gobject) {
{
PlayerctlPlayer *self = PLAYERCTL_PLAYER(gobject); PlayerctlPlayer *self = PLAYERCTL_PLAYER(gobject);
self->priv->init_error = NULL; self->priv->init_error = NULL;
@@ -253,8 +256,7 @@ static void playerctl_player_constructed(GObject *gobject)
G_OBJECT_CLASS(playerctl_player_parent_class)->constructed(gobject); G_OBJECT_CLASS(playerctl_player_parent_class)->constructed(gobject);
} }
static void playerctl_player_dispose(GObject *gobject) static void playerctl_player_dispose(GObject *gobject) {
{
PlayerctlPlayer *self = PLAYERCTL_PLAYER(gobject); PlayerctlPlayer *self = PLAYERCTL_PLAYER(gobject);
g_clear_error(&self->priv->init_error); g_clear_error(&self->priv->init_error);
@@ -263,8 +265,7 @@ static void playerctl_player_dispose(GObject *gobject)
G_OBJECT_CLASS(playerctl_player_parent_class)->dispose(gobject); G_OBJECT_CLASS(playerctl_player_parent_class)->dispose(gobject);
} }
static void playerctl_player_finalize(GObject *gobject) static void playerctl_player_finalize(GObject *gobject) {
{
PlayerctlPlayer *self = PLAYERCTL_PLAYER(gobject); PlayerctlPlayer *self = PLAYERCTL_PLAYER(gobject);
g_free(self->priv->player_name); g_free(self->priv->player_name);
@@ -273,7 +274,7 @@ static void playerctl_player_finalize(GObject *gobject)
G_OBJECT_CLASS(playerctl_player_parent_class)->finalize(gobject); G_OBJECT_CLASS(playerctl_player_parent_class)->finalize(gobject);
} }
static void playerctl_player_class_init (PlayerctlPlayerClass *klass) { static void playerctl_player_class_init(PlayerctlPlayerClass *klass) {
GObjectClass *gobject_class = G_OBJECT_CLASS(klass); GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
gobject_class->set_property = playerctl_player_set_property; gobject_class->set_property = playerctl_player_set_property;
@@ -282,47 +283,32 @@ static void playerctl_player_class_init (PlayerctlPlayerClass *klass) {
gobject_class->dispose = playerctl_player_dispose; gobject_class->dispose = playerctl_player_dispose;
gobject_class->finalize = playerctl_player_finalize; gobject_class->finalize = playerctl_player_finalize;
obj_properties[PROP_PLAYER_NAME] = obj_properties[PROP_PLAYER_NAME] = g_param_spec_string(
g_param_spec_string("player-name", "player-name", "Player name", "The name of the player mpris player",
"Player name",
"The name of the player mpris player",
NULL, /* default */ NULL, /* default */
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY| G_PARAM_STATIC_STRINGS); G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
obj_properties[PROP_STATUS] = obj_properties[PROP_STATUS] =
g_param_spec_string("status", g_param_spec_string("status", "Player status",
"Player status", "The play status of the player", NULL, /* default */
"The play status of the player",
NULL, /* default */
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
obj_properties[PROP_VOLUME] = obj_properties[PROP_VOLUME] = g_param_spec_double(
g_param_spec_double("volume", "volume", "Player volume", "The volume level of the player", 0, 100, 0,
"Player volume",
"The volume level of the player",
0,
100,
0,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
obj_properties[PROP_POSITION] = obj_properties[PROP_POSITION] = g_param_spec_int64(
g_param_spec_int64("position", "position", "Player position",
"Player position", "The position in the current track of the player", 0, INT64_MAX, 0,
"The position in the current track of the player",
0,
INT64_MAX,
0,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
obj_properties[PROP_METADATA] = obj_properties[PROP_METADATA] = g_param_spec_variant(
g_param_spec_variant("metadata", "metadata", "Player metadata",
"Player metadata", "The metadata of the currently playing track", G_VARIANT_TYPE_VARIANT,
"The metadata of the currently playing track", NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
G_VARIANT_TYPE_VARIANT,
NULL,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties(gobject_class, N_PROPERTIES, obj_properties); g_object_class_install_properties(gobject_class, N_PROPERTIES,
obj_properties);
#if 0 #if 0
/* not implemented yet */ /* not implemented yet */
@@ -339,8 +325,8 @@ static void playerctl_player_class_init (PlayerctlPlayerClass *klass) {
G_TYPE_VARIANT); G_TYPE_VARIANT);
#endif #endif
connection_signals[PLAY] = g_signal_new( connection_signals[PLAY] =
"play", /* signal_name */ g_signal_new("play", /* signal_name */
PLAYERCTL_TYPE_PLAYER, /* itype */ PLAYERCTL_TYPE_PLAYER, /* itype */
G_SIGNAL_RUN_FIRST, /* signal_flags */ G_SIGNAL_RUN_FIRST, /* signal_flags */
0, /* class_offset */ 0, /* class_offset */
@@ -350,8 +336,8 @@ static void playerctl_player_class_init (PlayerctlPlayerClass *klass) {
G_TYPE_NONE, /* return_type */ G_TYPE_NONE, /* return_type */
0); /* n_params */ 0); /* n_params */
connection_signals[PAUSE] = g_signal_new( connection_signals[PAUSE] =
"pause", /* signal_name */ g_signal_new("pause", /* signal_name */
PLAYERCTL_TYPE_PLAYER, /* itype */ PLAYERCTL_TYPE_PLAYER, /* itype */
G_SIGNAL_RUN_FIRST, /* signal_flags */ G_SIGNAL_RUN_FIRST, /* signal_flags */
0, /* class_offset */ 0, /* class_offset */
@@ -361,8 +347,8 @@ static void playerctl_player_class_init (PlayerctlPlayerClass *klass) {
G_TYPE_NONE, /* return_type */ G_TYPE_NONE, /* return_type */
0); /* n_params */ 0); /* n_params */
connection_signals[STOP] = g_signal_new( connection_signals[STOP] =
"stop", /* signal_name */ g_signal_new("stop", /* signal_name */
PLAYERCTL_TYPE_PLAYER, /* itype */ PLAYERCTL_TYPE_PLAYER, /* itype */
G_SIGNAL_RUN_FIRST, /* signal_flags */ G_SIGNAL_RUN_FIRST, /* signal_flags */
0, /* class_offset */ 0, /* class_offset */
@@ -372,8 +358,8 @@ static void playerctl_player_class_init (PlayerctlPlayerClass *klass) {
G_TYPE_NONE, /* return_type */ G_TYPE_NONE, /* return_type */
0); /* n_params */ 0); /* n_params */
connection_signals[METADATA] = g_signal_new( connection_signals[METADATA] =
"metadata", /* signal_name */ g_signal_new("metadata", /* signal_name */
PLAYERCTL_TYPE_PLAYER, /* itype */ PLAYERCTL_TYPE_PLAYER, /* itype */
G_SIGNAL_RUN_FIRST, /* signal_flags */ G_SIGNAL_RUN_FIRST, /* signal_flags */
0, /* class_offset */ 0, /* class_offset */
@@ -384,8 +370,8 @@ static void playerctl_player_class_init (PlayerctlPlayerClass *klass) {
1, /* n_params */ 1, /* n_params */
G_TYPE_VARIANT); G_TYPE_VARIANT);
connection_signals[EXIT] = g_signal_new( connection_signals[EXIT] =
"exit", /* signal_name */ g_signal_new("exit", /* signal_name */
PLAYERCTL_TYPE_PLAYER, /* itype */ PLAYERCTL_TYPE_PLAYER, /* itype */
G_SIGNAL_RUN_FIRST, /* signal_flags */ G_SIGNAL_RUN_FIRST, /* signal_flags */
0, /* class_offset */ 0, /* class_offset */
@@ -396,13 +382,12 @@ static void playerctl_player_class_init (PlayerctlPlayerClass *klass) {
0); /* n_params */ 0); /* n_params */
} }
static void playerctl_player_init (PlayerctlPlayer *self) static void playerctl_player_init(PlayerctlPlayer *self) {
{
self->priv = playerctl_player_get_instance_private(self); self->priv = playerctl_player_get_instance_private(self);
} }
static gchar *playerctl_player_get_bus_name(PlayerctlPlayer *self, GError **err, GBusType busType) static gchar *playerctl_player_get_bus_name(PlayerctlPlayer *self, GError **err,
{ GBusType busType) {
gchar *bus_name = NULL; gchar *bus_name = NULL;
GError *tmp_error = NULL; GError *tmp_error = NULL;
@@ -413,31 +398,21 @@ static gchar *playerctl_player_get_bus_name(PlayerctlPlayer *self, GError **err,
} }
if (self->priv->player_name != NULL) { if (self->priv->player_name != NULL) {
return g_strjoin(".", "org.mpris.MediaPlayer2", self->priv->player_name, NULL); return g_strjoin(".", "org.mpris.MediaPlayer2", self->priv->player_name,
NULL);
} }
GDBusProxy *proxy = g_dbus_proxy_new_for_bus_sync( GDBusProxy *proxy = g_dbus_proxy_new_for_bus_sync(
busType, busType, G_DBUS_PROXY_FLAGS_NONE, NULL, "org.freedesktop.DBus",
G_DBUS_PROXY_FLAGS_NONE, "/org/freedesktop/DBus", "org.freedesktop.DBus", NULL, &tmp_error);
NULL,
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
NULL,
&tmp_error);
if (tmp_error != NULL) { if (tmp_error != NULL) {
g_propagate_error(err, tmp_error); g_propagate_error(err, tmp_error);
return NULL; return NULL;
} }
GVariant *reply = g_dbus_proxy_call_sync(proxy, GVariant *reply = g_dbus_proxy_call_sync(
"ListNames", proxy, "ListNames", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &tmp_error);
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&tmp_error);
if (tmp_error != NULL) { if (tmp_error != NULL) {
g_propagate_error(err, tmp_error); g_propagate_error(err, tmp_error);
@@ -462,7 +437,8 @@ static gchar *playerctl_player_get_bus_name(PlayerctlPlayer *self, GError **err,
g_free(names); g_free(names);
if (bus_name == NULL) { if (bus_name == NULL) {
tmp_error = g_error_new(playerctl_player_error_quark(), 1, "No players found"); tmp_error =
g_error_new(playerctl_player_error_quark(), 1, "No players found");
g_propagate_error(err, tmp_error); g_propagate_error(err, tmp_error);
return NULL; return NULL;
} }
@@ -470,8 +446,9 @@ static gchar *playerctl_player_get_bus_name(PlayerctlPlayer *self, GError **err,
return bus_name; return bus_name;
} }
static gboolean playerctl_player_initable_init(GInitable *initable, GCancellable *cancellable, GError **err) static gboolean playerctl_player_initable_init(GInitable *initable,
{ GCancellable *cancellable,
GError **err) {
GError *tmp_error = NULL; GError *tmp_error = NULL;
PlayerctlPlayer *player = PLAYERCTL_PLAYER(initable); PlayerctlPlayer *player = PLAYERCTL_PLAYER(initable);
GBusType busType = G_BUS_TYPE_SESSION; GBusType busType = G_BUS_TYPE_SESSION;
@@ -481,11 +458,13 @@ static gboolean playerctl_player_initable_init(GInitable *initable, GCancellable
g_return_val_if_fail(err == NULL || *err == NULL, FALSE); g_return_val_if_fail(err == NULL || *err == NULL, FALSE);
player->priv->bus_name = playerctl_player_get_bus_name(player, &tmp_error, busType); player->priv->bus_name =
playerctl_player_get_bus_name(player, &tmp_error, busType);
if (tmp_error) { if (tmp_error) {
g_clear_error(&tmp_error); g_clear_error(&tmp_error);
busType = G_BUS_TYPE_SYSTEM; busType = G_BUS_TYPE_SYSTEM;
player->priv->bus_name = playerctl_player_get_bus_name(player, &tmp_error, busType); player->priv->bus_name =
playerctl_player_get_bus_name(player, &tmp_error, busType);
} }
if (tmp_error != NULL) { if (tmp_error != NULL) {
@@ -495,31 +474,28 @@ static gboolean playerctl_player_initable_init(GInitable *initable, GCancellable
if (player->priv->player_name == NULL) { if (player->priv->player_name == NULL) {
/* org.mpris.MediaPlayer2.[NAME] */ /* org.mpris.MediaPlayer2.[NAME] */
size_t offset = strlen ("org.mpris.MediaPlayer2"); size_t offset = strlen("org.mpris.MediaPlayer2");
player->priv->player_name = g_strdup(player->priv->bus_name + offset); player->priv->player_name = g_strdup(player->priv->bus_name + offset);
} }
player->priv->proxy = org_mpris_media_player2_player_proxy_new_for_bus_sync( player->priv->proxy = org_mpris_media_player2_player_proxy_new_for_bus_sync(
busType, busType, G_DBUS_PROXY_FLAGS_NONE, player->priv->bus_name,
G_DBUS_PROXY_FLAGS_NONE, "/org/mpris/MediaPlayer2", NULL, &tmp_error);
player->priv->bus_name,
"/org/mpris/MediaPlayer2",
NULL,
&tmp_error);
if (tmp_error != NULL) { if (tmp_error != NULL) {
g_propagate_error(err, tmp_error); g_propagate_error(err, tmp_error);
return FALSE; return FALSE;
} }
g_signal_connect(player->priv->proxy, "g-properties-changed", G_CALLBACK(playerctl_player_properties_changed_callback), player); g_signal_connect(player->priv->proxy, "g-properties-changed",
G_CALLBACK(playerctl_player_properties_changed_callback),
player);
player->priv->initted = TRUE; player->priv->initted = TRUE;
return TRUE; return TRUE;
} }
static void playerctl_player_initable_iface_init(GInitableIface *iface) static void playerctl_player_initable_iface_init(GInitableIface *iface) {
{
iface->init = playerctl_player_initable_init; iface->init = playerctl_player_initable_init;
} }
@@ -534,12 +510,12 @@ static void playerctl_player_initable_iface_init(GInitableIface *iface)
* Returns:(transfer full): A new #PlayerctlPlayer connected to the bus name or * Returns:(transfer full): A new #PlayerctlPlayer connected to the bus name or
* NULL if an error occurred * NULL if an error occurred
*/ */
PlayerctlPlayer *playerctl_player_new(const gchar *name, GError **err) PlayerctlPlayer *playerctl_player_new(const gchar *name, GError **err) {
{
GError *tmp_error = NULL; GError *tmp_error = NULL;
PlayerctlPlayer *player; PlayerctlPlayer *player;
player = g_initable_new(PLAYERCTL_TYPE_PLAYER, NULL, &tmp_error, "player-name", name, NULL); player = g_initable_new(PLAYERCTL_TYPE_PLAYER, NULL, &tmp_error,
"player-name", name, NULL);
if (tmp_error != NULL) { if (tmp_error != NULL) {
g_propagate_error(err, tmp_error); g_propagate_error(err, tmp_error);
@@ -558,8 +534,8 @@ PlayerctlPlayer *playerctl_player_new(const gchar *name, GError **err)
* *
* Returns: (transfer none): the #PlayerctlPlayer for chaining * Returns: (transfer none): the #PlayerctlPlayer for chaining
*/ */
PlayerctlPlayer *playerctl_player_on(PlayerctlPlayer *self, const gchar *event, GClosure *callback, GError **err) PlayerctlPlayer *playerctl_player_on(PlayerctlPlayer *self, const gchar *event,
{ GClosure *callback, GError **err) {
g_return_val_if_fail(self != NULL, NULL); g_return_val_if_fail(self != NULL, NULL);
g_return_val_if_fail(event != NULL, NULL); g_return_val_if_fail(event != NULL, NULL);
g_return_val_if_fail(callback != NULL, NULL); g_return_val_if_fail(callback != NULL, NULL);
@@ -589,7 +565,8 @@ PlayerctlPlayer *playerctl_player_on(PlayerctlPlayer *self, const gchar *event,
return self; \ return self; \
} \ } \
\ \
org_mpris_media_player2_player_call_##COMMAND##_sync(self->priv->proxy, NULL, &tmp_error); \ org_mpris_media_player2_player_call_##COMMAND##_sync(self->priv->proxy, \
NULL, &tmp_error); \
\ \
if (tmp_error != NULL) { \ if (tmp_error != NULL) { \
g_propagate_error(err, tmp_error); \ g_propagate_error(err, tmp_error); \
@@ -606,8 +583,8 @@ PlayerctlPlayer *playerctl_player_on(PlayerctlPlayer *self, const gchar *event,
* *
* Returns: (transfer none): the #PlayerctlPlayer for chaining * Returns: (transfer none): the #PlayerctlPlayer for chaining
*/ */
PlayerctlPlayer *playerctl_player_play_pause(PlayerctlPlayer *self, GError **err) PlayerctlPlayer *playerctl_player_play_pause(PlayerctlPlayer *self,
{ GError **err) {
PLAYER_COMMAND_FUNC(play_pause); PLAYER_COMMAND_FUNC(play_pause);
} }
@@ -620,8 +597,8 @@ PlayerctlPlayer *playerctl_player_play_pause(PlayerctlPlayer *self, GError **err
* *
* Returns: (transfer none): the #PlayerctlPlayer for chaining * Returns: (transfer none): the #PlayerctlPlayer for chaining
*/ */
PlayerctlPlayer *playerctl_player_open(PlayerctlPlayer *self, gchar *uri, GError **err) PlayerctlPlayer *playerctl_player_open(PlayerctlPlayer *self, gchar *uri,
{ GError **err) {
GError *tmp_error = NULL; GError *tmp_error = NULL;
g_return_val_if_fail(self != NULL, NULL); g_return_val_if_fail(self != NULL, NULL);
@@ -631,7 +608,8 @@ PlayerctlPlayer *playerctl_player_open(PlayerctlPlayer *self, gchar *uri, GError
g_propagate_error(err, g_error_copy(self->priv->init_error)); g_propagate_error(err, g_error_copy(self->priv->init_error));
return self; return self;
} }
org_mpris_media_player2_player_call_open_uri_sync(self->priv->proxy, uri, NULL, &tmp_error); org_mpris_media_player2_player_call_open_uri_sync(self->priv->proxy, uri,
NULL, &tmp_error);
if (tmp_error != NULL) { if (tmp_error != NULL) {
g_propagate_error(err, tmp_error); g_propagate_error(err, tmp_error);
@@ -650,8 +628,7 @@ PlayerctlPlayer *playerctl_player_open(PlayerctlPlayer *self, gchar *uri, GError
* *
* Returns: (transfer none): the #PlayerctlPlayer for chaining * Returns: (transfer none): the #PlayerctlPlayer for chaining
*/ */
PlayerctlPlayer *playerctl_player_play(PlayerctlPlayer *self, GError **err) PlayerctlPlayer *playerctl_player_play(PlayerctlPlayer *self, GError **err) {
{
PLAYER_COMMAND_FUNC(play); PLAYER_COMMAND_FUNC(play);
} }
@@ -664,8 +641,7 @@ PlayerctlPlayer *playerctl_player_play(PlayerctlPlayer *self, GError **err)
* *
* Returns: (transfer none): the #PlayerctlPlayer for chaining * Returns: (transfer none): the #PlayerctlPlayer for chaining
*/ */
PlayerctlPlayer *playerctl_player_pause(PlayerctlPlayer *self, GError **err) PlayerctlPlayer *playerctl_player_pause(PlayerctlPlayer *self, GError **err) {
{
PLAYER_COMMAND_FUNC(pause); PLAYER_COMMAND_FUNC(pause);
} }
@@ -678,8 +654,7 @@ PlayerctlPlayer *playerctl_player_pause(PlayerctlPlayer *self, GError **err)
* *
* Returns: (transfer none): the #PlayerctlPlayer for chaining * Returns: (transfer none): the #PlayerctlPlayer for chaining
*/ */
PlayerctlPlayer *playerctl_player_stop(PlayerctlPlayer *self, GError **err) PlayerctlPlayer *playerctl_player_stop(PlayerctlPlayer *self, GError **err) {
{
PLAYER_COMMAND_FUNC(stop); PLAYER_COMMAND_FUNC(stop);
} }
@@ -692,8 +667,8 @@ PlayerctlPlayer *playerctl_player_stop(PlayerctlPlayer *self, GError **err)
* *
* Returns: (transfer none): the #PlayerctlPlayer for chaining * Returns: (transfer none): the #PlayerctlPlayer for chaining
*/ */
PlayerctlPlayer *playerctl_player_seek(PlayerctlPlayer *self, gint64 offset, GError **err) PlayerctlPlayer *playerctl_player_seek(PlayerctlPlayer *self, gint64 offset,
{ GError **err) {
GError *tmp_error = NULL; GError *tmp_error = NULL;
g_return_val_if_fail(self != NULL, NULL); g_return_val_if_fail(self != NULL, NULL);
@@ -704,7 +679,8 @@ PlayerctlPlayer *playerctl_player_seek(PlayerctlPlayer *self, gint64 offset, GEr
return self; return self;
} }
org_mpris_media_player2_player_call_seek_sync(self->priv->proxy, offset, NULL, &tmp_error); org_mpris_media_player2_player_call_seek_sync(self->priv->proxy, offset, NULL,
&tmp_error);
if (tmp_error != NULL) { if (tmp_error != NULL) {
g_propagate_error(err, tmp_error); g_propagate_error(err, tmp_error);
@@ -723,8 +699,7 @@ PlayerctlPlayer *playerctl_player_seek(PlayerctlPlayer *self, gint64 offset, GEr
* *
* Returns: (transfer none): the #PlayerctlPlayer for chaining * Returns: (transfer none): the #PlayerctlPlayer for chaining
*/ */
PlayerctlPlayer *playerctl_player_next(PlayerctlPlayer *self, GError **err) PlayerctlPlayer *playerctl_player_next(PlayerctlPlayer *self, GError **err) {
{
PLAYER_COMMAND_FUNC(next); PLAYER_COMMAND_FUNC(next);
} }
@@ -737,8 +712,8 @@ PlayerctlPlayer *playerctl_player_next(PlayerctlPlayer *self, GError **err)
* *
* Returns: (transfer none): the #PlayerctlPlayer for chaining * Returns: (transfer none): the #PlayerctlPlayer for chaining
*/ */
PlayerctlPlayer *playerctl_player_previous(PlayerctlPlayer *self, GError **err) PlayerctlPlayer *playerctl_player_previous(PlayerctlPlayer *self,
{ GError **err) {
PLAYER_COMMAND_FUNC(previous); PLAYER_COMMAND_FUNC(previous);
} }
@@ -753,8 +728,9 @@ PlayerctlPlayer *playerctl_player_previous(PlayerctlPlayer *self, GError **err)
* *
* Returns: (transfer full): The artist from the metadata of the current track * Returns: (transfer full): The artist from the metadata of the current track
*/ */
gchar *playerctl_player_print_metadata_prop(PlayerctlPlayer *self, const gchar *property, GError **err) gchar *playerctl_player_print_metadata_prop(PlayerctlPlayer *self,
{ const gchar *property,
GError **err) {
GError *tmp_error = NULL; GError *tmp_error = NULL;
g_return_val_if_fail(self != NULL, NULL); g_return_val_if_fail(self != NULL, NULL);
@@ -818,13 +794,12 @@ gchar *playerctl_player_print_metadata_prop(PlayerctlPlayer *self, const gchar *
* @self: a #PlayerctlPlayer * @self: a #PlayerctlPlayer
* @err: (allow-none): the location of a GError or NULL * @err: (allow-none): the location of a GError or NULL
* *
* Gets the artist from the metadata of the current track, or the empty string if * Gets the artist from the metadata of the current track, or the empty string
* no track is playing. * if no track is playing.
* *
* Returns: (transfer full): The artist from the metadata of the current track * Returns: (transfer full): The artist from the metadata of the current track
*/ */
gchar *playerctl_player_get_artist(PlayerctlPlayer *self, GError **err) gchar *playerctl_player_get_artist(PlayerctlPlayer *self, GError **err) {
{
g_return_val_if_fail(self != NULL, NULL); g_return_val_if_fail(self != NULL, NULL);
g_return_val_if_fail(err == NULL || *err == NULL, NULL); g_return_val_if_fail(err == NULL || *err == NULL, NULL);
@@ -846,8 +821,7 @@ gchar *playerctl_player_get_artist(PlayerctlPlayer *self, GError **err)
* *
* Returns: (transfer full): The title from the metadata of the current track * Returns: (transfer full): The title from the metadata of the current track
*/ */
gchar *playerctl_player_get_title(PlayerctlPlayer *self, GError **err) gchar *playerctl_player_get_title(PlayerctlPlayer *self, GError **err) {
{
g_return_val_if_fail(self != NULL, NULL); g_return_val_if_fail(self != NULL, NULL);
g_return_val_if_fail(err == NULL || *err == NULL, NULL); g_return_val_if_fail(err == NULL || *err == NULL, NULL);
@@ -869,8 +843,7 @@ gchar *playerctl_player_get_title(PlayerctlPlayer *self, GError **err)
* *
* Returns: (transfer full): The album from the metadata of the current track * Returns: (transfer full): The album from the metadata of the current track
*/ */
gchar *playerctl_player_get_album(PlayerctlPlayer *self, GError **err) gchar *playerctl_player_get_album(PlayerctlPlayer *self, GError **err) {
{
g_return_val_if_fail(self != NULL, NULL); g_return_val_if_fail(self != NULL, NULL);
g_return_val_if_fail(err == NULL || *err == NULL, NULL); g_return_val_if_fail(err == NULL || *err == NULL, NULL);
@@ -889,8 +862,8 @@ gchar *playerctl_player_get_album(PlayerctlPlayer *self, GError **err)
* *
* Sets the position of the current track to the given position in microseconds. * Sets the position of the current track to the given position in microseconds.
*/ */
void playerctl_player_set_position(PlayerctlPlayer *self, gint64 position, GError **err) void playerctl_player_set_position(PlayerctlPlayer *self, gint64 position,
{ GError **err) {
GError *tmp_error = NULL; GError *tmp_error = NULL;
g_return_if_fail(self != NULL); g_return_if_fail(self != NULL);
@@ -907,28 +880,31 @@ void playerctl_player_set_position(PlayerctlPlayer *self, gint64 position, GErro
return; return;
} }
GVariant *track_id_variant = g_variant_lookup_value(metadata, "mpris:trackid", G_VARIANT_TYPE_OBJECT_PATH); GVariant *track_id_variant = g_variant_lookup_value(
metadata, "mpris:trackid", G_VARIANT_TYPE_OBJECT_PATH);
if (track_id_variant == NULL) { if (track_id_variant == NULL) {
// XXX some players set this as a string, which is against the protocol, // XXX some players set this as a string, which is against the protocol,
// but a lot of them do it and I don't feel like fixing it on all the // but a lot of them do it and I don't feel like fixing it on all the
// players in the world. // players in the world.
track_id_variant = g_variant_lookup_value(metadata, "mpris:trackid", G_VARIANT_TYPE_STRING); track_id_variant = g_variant_lookup_value(metadata, "mpris:trackid",
G_VARIANT_TYPE_STRING);
} }
g_variant_unref(metadata); g_variant_unref(metadata);
if (track_id_variant == NULL) { if (track_id_variant == NULL) {
tmp_error = g_error_new(playerctl_player_error_quark(), 1, "Could not get track id to set position"); tmp_error = g_error_new(playerctl_player_error_quark(), 1,
"Could not get track id to set position");
g_propagate_error(err, tmp_error); g_propagate_error(err, tmp_error);
return; return;
} }
const gchar *track_id = g_variant_get_string(track_id_variant, NULL); const gchar *track_id = g_variant_get_string(track_id_variant, NULL);
org_mpris_media_player2_player_call_set_position_sync(self->priv->proxy, track_id, position, NULL, &tmp_error); org_mpris_media_player2_player_call_set_position_sync(
self->priv->proxy, track_id, position, NULL, &tmp_error);
g_variant_unref(track_id_variant); g_variant_unref(track_id_variant);
if (tmp_error != NULL) { if (tmp_error != NULL) {
g_propagate_error(err, tmp_error); g_propagate_error(err, tmp_error);
} }
} }

View File

@@ -1,5 +1,4 @@
/* vim:ts=2:sw=2:expandtab /*
*
* This file is part of playerctl. * This file is part of playerctl.
* *
* playerctl is free software: you can redistribute it and/or modify it under * playerctl is free software: you can redistribute it and/or modify it under
@@ -31,19 +30,25 @@
* SECTION: playerctl-player * SECTION: playerctl-player
* @short_description: A class to control an MPRIS player * @short_description: A class to control an MPRIS player
*/ */
#define PLAYERCTL_TYPE_PLAYER (playerctl_player_get_type ()) #define PLAYERCTL_TYPE_PLAYER (playerctl_player_get_type())
#define PLAYERCTL_PLAYER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PLAYERCTL_TYPE_PLAYER, PlayerctlPlayer)) #define PLAYERCTL_PLAYER(obj) \
#define PLAYERCTL_IS_PLAYER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PLAYERCTL_TYPE_PLAYER)) (G_TYPE_CHECK_INSTANCE_CAST((obj), PLAYERCTL_TYPE_PLAYER, PlayerctlPlayer))
#define PLAYERCTL_PLAYER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PLAYERCTL_TYPE_PLAYER, PlayerctlPlayerClass)) #define PLAYERCTL_IS_PLAYER(obj) \
#define PLAYERCTL_IS_PLAYER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PLAYERCTL_TYPE_PLAYER)) (G_TYPE_CHECK_INSTANCE_TYPE((obj), PLAYERCTL_TYPE_PLAYER))
#define PLAYERCTL_PLAYER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PLAYERCTL_TYPE_PLAYER, PlayerctlPlayerClass)) #define PLAYERCTL_PLAYER_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass), PLAYERCTL_TYPE_PLAYER, \
PlayerctlPlayerClass))
#define PLAYERCTL_IS_PLAYER_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass), PLAYERCTL_TYPE_PLAYER))
#define PLAYERCTL_PLAYER_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj), PLAYERCTL_TYPE_PLAYER, \
PlayerctlPlayerClass))
typedef struct _PlayerctlPlayer PlayerctlPlayer; typedef struct _PlayerctlPlayer PlayerctlPlayer;
typedef struct _PlayerctlPlayerClass PlayerctlPlayerClass; typedef struct _PlayerctlPlayerClass PlayerctlPlayerClass;
typedef struct _PlayerctlPlayerPrivate PlayerctlPlayerPrivate; typedef struct _PlayerctlPlayerPrivate PlayerctlPlayerPrivate;
struct _PlayerctlPlayer struct _PlayerctlPlayer {
{
/* Parent instance structure */ /* Parent instance structure */
GObject parent_instance; GObject parent_instance;
@@ -51,31 +56,34 @@ struct _PlayerctlPlayer
PlayerctlPlayerPrivate *priv; PlayerctlPlayerPrivate *priv;
}; };
struct _PlayerctlPlayerClass struct _PlayerctlPlayerClass {
{
/* Parent class structure */ /* Parent class structure */
GObjectClass parent_class; GObjectClass parent_class;
}; };
GType playerctl_player_get_type (void); GType playerctl_player_get_type(void);
PlayerctlPlayer *playerctl_player_new (const gchar *name, GError **err); PlayerctlPlayer *playerctl_player_new(const gchar *name, GError **err);
/* /*
* Method definitions. * Method definitions.
*/ */
PlayerctlPlayer *playerctl_player_on(PlayerctlPlayer *self, const gchar *event, GClosure *callback, GError **err); PlayerctlPlayer *playerctl_player_on(PlayerctlPlayer *self, const gchar *event,
GClosure *callback, GError **err);
PlayerctlPlayer *playerctl_player_open(PlayerctlPlayer *self, gchar *uri, GError **err); PlayerctlPlayer *playerctl_player_open(PlayerctlPlayer *self, gchar *uri,
GError **err);
PlayerctlPlayer *playerctl_player_play_pause(PlayerctlPlayer *self, GError **err); PlayerctlPlayer *playerctl_player_play_pause(PlayerctlPlayer *self,
GError **err);
PlayerctlPlayer *playerctl_player_play(PlayerctlPlayer *self, GError **err); PlayerctlPlayer *playerctl_player_play(PlayerctlPlayer *self, GError **err);
PlayerctlPlayer *playerctl_player_stop(PlayerctlPlayer *self, GError **err); PlayerctlPlayer *playerctl_player_stop(PlayerctlPlayer *self, GError **err);
PlayerctlPlayer *playerctl_player_seek(PlayerctlPlayer *self, gint64 offset, GError **err); PlayerctlPlayer *playerctl_player_seek(PlayerctlPlayer *self, gint64 offset,
GError **err);
PlayerctlPlayer *playerctl_player_pause(PlayerctlPlayer *self, GError **err); PlayerctlPlayer *playerctl_player_pause(PlayerctlPlayer *self, GError **err);
@@ -83,7 +91,9 @@ PlayerctlPlayer *playerctl_player_next(PlayerctlPlayer *self, GError **err);
PlayerctlPlayer *playerctl_player_previous(PlayerctlPlayer *self, GError **err); PlayerctlPlayer *playerctl_player_previous(PlayerctlPlayer *self, GError **err);
gchar *playerctl_player_print_metadata_prop(PlayerctlPlayer *self, const gchar *property, GError **err); gchar *playerctl_player_print_metadata_prop(PlayerctlPlayer *self,
const gchar *property,
GError **err);
gchar *playerctl_player_get_artist(PlayerctlPlayer *self, GError **err); gchar *playerctl_player_get_artist(PlayerctlPlayer *self, GError **err);
@@ -91,6 +101,7 @@ gchar *playerctl_player_get_title(PlayerctlPlayer *self, GError **err);
gchar *playerctl_player_get_album(PlayerctlPlayer *self, GError **err); gchar *playerctl_player_get_album(PlayerctlPlayer *self, GError **err);
void playerctl_player_set_position(PlayerctlPlayer *self, gint64 position, GError **err); void playerctl_player_set_position(PlayerctlPlayer *self, gint64 position,
GError **err);
#endif /* __PLAYERCTL_PLAYER_H__ */ #endif /* __PLAYERCTL_PLAYER_H__ */

View File

@@ -1,5 +1,4 @@
/* vim:ts=2:sw=2:expandtab /*
*
* This file is part of playerctl. * This file is part of playerctl.
* *
* playerctl is free software: you can redistribute it and/or modify it under * playerctl is free software: you can redistribute it and/or modify it under

View File

@@ -1,5 +1,4 @@
/* vim:ts=2:sw=2:expandtab /*
*
* This file is part of playerctl. * This file is part of playerctl.
* *
* playerctl is free software: you can redistribute it and/or modify it under * playerctl is free software: you can redistribute it and/or modify it under
@@ -23,8 +22,8 @@
#define __PLAYERCTL_INSIDE__ #define __PLAYERCTL_INSIDE__
#include <playerctl/playerctl-version.h>
#include <playerctl/playerctl-player.h> #include <playerctl/playerctl-player.h>
#include <playerctl/playerctl-version.h>
#undef __PLAYERCTL_INSIDE__ #undef __PLAYERCTL_INSIDE__