Skip to content

Commit

Permalink
refactor(media): refactor ValentMedia to implement GListModel
Browse files Browse the repository at this point in the history
This should make it easier to work with in GTK and reduces the API
surface somewhat.

* remove `::player-added` and `::player-removed` signals
* remove `valent_media_get_players()` method
* remove `valent_media_get_player_by_name()` method
  • Loading branch information
andyholmes committed Oct 2, 2022
1 parent 43f7130 commit 2e40b4d
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 198 deletions.
177 changes: 60 additions & 117 deletions src/libvalent/media/valent-media.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ struct _ValentMedia
GPtrArray *paused;
};

G_DEFINE_TYPE (ValentMedia, valent_media, VALENT_TYPE_COMPONENT)
static void g_list_model_iface_init (GListModelInterface *iface);

G_DEFINE_TYPE_EXTENDED (ValentMedia, valent_media, VALENT_TYPE_COMPONENT,
0,
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, g_list_model_iface_init))

enum {
PLAYER_ADDED,
PLAYER_CHANGED,
PLAYER_REMOVED,
PLAYER_SEEKED,
N_SIGNALS
};
Expand All @@ -54,6 +56,45 @@ static guint signals[N_SIGNALS] = { 0, };
static ValentMedia *default_media = NULL;


/*
* GListModel
*/
static gpointer
valent_media_get_item (GListModel *list,
unsigned int position)
{
ValentMedia *self = VALENT_MEDIA (list);

g_assert (VALENT_IS_MEDIA (self));
g_assert (position < self->players->len);

return g_object_ref (g_ptr_array_index (self->players, position));
}

static GType
valent_media_get_item_type (GListModel *list)
{
return VALENT_TYPE_MEDIA_PLAYER;
}

static unsigned int
valent_media_get_n_items (GListModel *list)
{
ValentMedia *self = VALENT_MEDIA (list);

g_assert (VALENT_IS_MEDIA (self));

return self->players->len;
}

static void
g_list_model_iface_init (GListModelInterface *iface)
{
iface->get_item = valent_media_get_item;
iface->get_item_type = valent_media_get_item_type;
iface->get_n_items = valent_media_get_n_items;
}

/*
* Signal Relays
*/
Expand Down Expand Up @@ -92,8 +133,12 @@ on_player_added (ValentMediaAdapter *adapter,
ValentMediaPlayer *player,
ValentMedia *self)
{
unsigned int position = 0;

VALENT_ENTRY;

g_assert (VALENT_IS_MEDIA_ADAPTER (adapter));
g_assert (VALENT_IS_MEDIA_PLAYER (player));
g_assert (VALENT_IS_MEDIA (self));

VALENT_NOTE ("%s: %s",
Expand All @@ -104,14 +149,14 @@ on_player_added (ValentMediaAdapter *adapter,
"changed",
G_CALLBACK (on_player_changed),
self, 0);

g_signal_connect_object (player,
"notify::position",
G_CALLBACK (on_player_seeked),
self, 0);

position = self->players->len;
g_ptr_array_add (self->players, g_object_ref (player));
g_signal_emit (G_OBJECT (self), signals [PLAYER_ADDED], 0, player);
g_list_model_items_changed (G_LIST_MODEL (self), position, 0, 1);

VALENT_EXIT;
}
Expand All @@ -121,8 +166,12 @@ on_player_removed (ValentMediaAdapter *adapter,
ValentMediaPlayer *player,
ValentMedia *self)
{
unsigned int position = 0;

VALENT_ENTRY;

g_assert (VALENT_IS_MEDIA_ADAPTER (adapter));
g_assert (VALENT_IS_MEDIA_PLAYER (player));
g_assert (VALENT_IS_MEDIA (self));

VALENT_NOTE ("%s: %s",
Expand All @@ -131,11 +180,13 @@ on_player_removed (ValentMediaAdapter *adapter,

g_signal_handlers_disconnect_by_func (player, on_player_changed, self);
g_signal_handlers_disconnect_by_func (player, on_player_seeked, self);

g_ptr_array_remove (self->players, player);
g_ptr_array_remove (self->paused, player);

g_signal_emit (G_OBJECT (self), signals [PLAYER_REMOVED], 0, player);
if (g_ptr_array_find (self->players, player, &position))
{
g_ptr_array_remove_index (self->players, position);
g_list_model_items_changed (G_LIST_MODEL (self), position, 1, 0);
}

VALENT_EXIT;
}
Expand Down Expand Up @@ -175,13 +226,12 @@ valent_media_enable_extension (ValentComponent *component,
g_assert (VALENT_IS_MEDIA (self));
g_assert (VALENT_IS_MEDIA_ADAPTER (adapter));

g_ptr_array_add (self->adapters, g_object_ref (extension));
g_ptr_array_add (self->adapters, g_object_ref (adapter));
g_signal_connect_object (adapter,
"player-added",
G_CALLBACK (on_player_added),
self,
0);

g_signal_connect_object (adapter,
"player-removed",
G_CALLBACK (on_player_removed),
Expand Down Expand Up @@ -221,7 +271,6 @@ valent_media_disable_extension (ValentComponent *component,
VALENT_EXIT;
}


/*
* GObject
*/
Expand Down Expand Up @@ -261,51 +310,6 @@ valent_media_class_init (ValentMediaClass *klass)
component_class->enable_extension = valent_media_enable_extension;
component_class->disable_extension = valent_media_disable_extension;


/**
* ValentMedia::player-added:
* @media: an #ValentMedia
* @player: an #ValentMediaPlayer
*
* Emitted when a [class@Valent.MediaPlayer] has been added to a
* [class@Valent.MediaAdapter] implementation.
*
* Since: 1.0
*/
signals [PLAYER_ADDED] =
g_signal_new ("player-added",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST,
0,
NULL, NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1, VALENT_TYPE_MEDIA_PLAYER);
g_signal_set_va_marshaller (signals [PLAYER_ADDED],
G_TYPE_FROM_CLASS (klass),
g_cclosure_marshal_VOID__OBJECTv);

/**
* ValentMedia::player-removed:
* @media: an #ValentMedia
* @player: an #ValentMediaPlayer
*
* Emitted when a [class@Valent.MediaPlayer] has been removed from a
* [class@Valent.MediaAdapter] implementation.
*
* Since: 1.0
*/
signals [PLAYER_REMOVED] =
g_signal_new ("player-removed",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST,
0,
NULL, NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1, VALENT_TYPE_MEDIA_PLAYER);
g_signal_set_va_marshaller (signals [PLAYER_REMOVED],
G_TYPE_FROM_CLASS (klass),
g_cclosure_marshal_VOID__OBJECTv);

/**
* ValentMedia::player-changed:
* @media: an #ValentMedia
Expand Down Expand Up @@ -383,67 +387,6 @@ valent_media_get_default (void)
return default_media;
}

/**
* valent_media_get_players:
* @media: a #ValentMedia
*
* Get a list of all the #ValentMediaPlayer instances currently known to @media.
*
* Returns: (transfer container) (element-type Valent.MediaPlayer) (not nullable):
* a #GPtrArray of #ValentMediaPlayer
*
* Since: 1.0
*/
GPtrArray *
valent_media_get_players (ValentMedia *media)
{
GPtrArray *ret;

VALENT_ENTRY;

g_return_val_if_fail (VALENT_IS_MEDIA (media), NULL);

ret = g_ptr_array_new_with_free_func (g_object_unref);

for (unsigned int i = 0; i < media->players->len; i++)
g_ptr_array_add (ret, g_object_ref (g_ptr_array_index (media->players, i)));

VALENT_RETURN (ret);
}

/**
* valent_media_get_player_by_name:
* @media: a #ValentMedia
* @name: player name
*
* Get the #ValentMediaPlayer with the identity @name.
*
* Returns: (transfer none) (nullable): a #ValentMediaPlayer
*
* Since: 1.0
*/
ValentMediaPlayer *
valent_media_get_player_by_name (ValentMedia *media,
const char *name)
{
ValentMediaPlayer *player;

VALENT_ENTRY;

g_return_val_if_fail (VALENT_IS_MEDIA (media), NULL);
g_return_val_if_fail (name != NULL, NULL);

for (unsigned int i = 0; i < media->players->len; i++)
{
player = g_ptr_array_index (media->players, i);

if (g_strcmp0 (valent_media_player_get_name (player), name) == 0)
VALENT_RETURN (player);
}

VALENT_RETURN (NULL);
}

/**
* valent_media_export_player:
* @media: a #ValentMedia
Expand Down
20 changes: 7 additions & 13 deletions src/libvalent/media/valent-media.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,17 @@ VALENT_AVAILABLE_IN_1_0
G_DECLARE_FINAL_TYPE (ValentMedia, valent_media, VALENT, MEDIA, ValentComponent)

VALENT_AVAILABLE_IN_1_0
ValentMedia * valent_media_get_default (void);

VALENT_AVAILABLE_IN_1_0
GPtrArray * valent_media_get_players (ValentMedia *media);
VALENT_AVAILABLE_IN_1_0
ValentMediaPlayer * valent_media_get_player_by_name (ValentMedia *media,
const char *name);
ValentMedia * valent_media_get_default (void);
VALENT_AVAILABLE_IN_1_0
void valent_media_export_player (ValentMedia *media,
ValentMediaPlayer *player);
void valent_media_export_player (ValentMedia *media,
ValentMediaPlayer *player);
VALENT_AVAILABLE_IN_1_0
void valent_media_unexport_player (ValentMedia *media,
ValentMediaPlayer *player);
void valent_media_unexport_player (ValentMedia *media,
ValentMediaPlayer *player);
VALENT_AVAILABLE_IN_1_0
void valent_media_pause (ValentMedia *media);
void valent_media_pause (ValentMedia *media);
VALENT_AVAILABLE_IN_1_0
void valent_media_unpause (ValentMedia *media);
void valent_media_unpause (ValentMedia *media);

G_END_DECLS

46 changes: 33 additions & 13 deletions src/plugins/mpris/valent-mpris-plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ static void valent_mpris_plugin_send_player_list (ValentMprisPlugin *self);
/*
* Local Players
*/
static ValentMediaPlayer *
valent_mpris_plugin_lookup_player (ValentMprisPlugin *self,
const char *name)
{
unsigned int n_players = 0;

n_players = g_list_model_get_n_items (G_LIST_MODEL (self->media));

for (unsigned int i = 0; i < n_players; i++)
{
g_autoptr (ValentMediaPlayer) player = NULL;

player = g_list_model_get_item (G_LIST_MODEL (self->media), i);

if (g_strcmp0 (valent_media_player_get_name (player), name) == 0)
return player;
}

return NULL;
}

static void
send_album_art_cb (ValentTransfer *transfer,
GAsyncResult *result,
Expand Down Expand Up @@ -157,7 +178,9 @@ on_player_seeked (ValentMedia *media,

static void
on_players_changed (ValentMedia *media,
ValentMediaPlayer *player,
unsigned int position,
unsigned int removed,
unsigned int added,
ValentMprisPlugin *self)
{
valent_mpris_plugin_send_player_list (self);
Expand Down Expand Up @@ -215,7 +238,7 @@ valent_mpris_plugin_handle_mpris_request (ValentMprisPlugin *self,

/* Start by checking for a player */
if (valent_packet_get_string (packet, "player", &name))
player = valent_media_get_player_by_name (self->media, name);
player = valent_mpris_plugin_lookup_player (self, name);

if (player == NULL || valent_packet_check_field (packet, "requestPlayerList"))
{
Expand Down Expand Up @@ -413,7 +436,7 @@ valent_mpris_plugin_send_player_list (ValentMprisPlugin *self)
{
JsonBuilder *builder;
g_autoptr (JsonNode) packet = NULL;
g_autoptr (GPtrArray) players = NULL;
unsigned int n_players = 0;

g_assert (VALENT_IS_MPRIS_PLUGIN (self));

Expand All @@ -423,16 +446,17 @@ valent_mpris_plugin_send_player_list (ValentMprisPlugin *self)
json_builder_set_member_name (builder, "playerList");
json_builder_begin_array (builder);

players = valent_media_get_players (self->media);
n_players = g_list_model_get_n_items (G_LIST_MODEL (self->media));

for (unsigned int i = 0; i < players->len; i++)
for (unsigned int i = 0; i < n_players; i++)
{
ValentMediaPlayer *player;
g_autoptr (ValentMediaPlayer) player = NULL;
const char *name;

player = g_ptr_array_index (players, i);
player = g_list_model_get_item (G_LIST_MODEL (self->media), i);
name = valent_media_player_get_name (player);

if ((name = valent_media_player_get_name (player)) != NULL)
if (name != NULL)
json_builder_add_string_value (builder, name);
}

Expand All @@ -458,11 +482,7 @@ valent_mpris_plugin_watch_media (ValentMprisPlugin *self,
if (connect)
{
g_signal_connect_object (self->media,
"player-added",
G_CALLBACK (on_players_changed),
self, 0);
g_signal_connect_object (self->media,
"player-removed",
"items-changed",
G_CALLBACK (on_players_changed),
self, 0);
g_signal_connect_object (self->media,
Expand Down
Loading

0 comments on commit 2e40b4d

Please sign in to comment.