mixer-stream: Improve GObject properties gunk a bit

Keep track of the `GParamSpec`s of the properties. This allows us to use
`g_object_notify_by_pspec()`, which is a bit more performant than
`g_object_notify()`(as it doesn't need to take a global lock to lookup
the property name). It also prevents accidental typos in the property
name at compile time.

Also always add `G_PARAM_STATIC_STRINGS`, to prevent some unnecessary
string duplications of property name, blurb and description.
This commit is contained in:
Niels De Graef 2022-05-29 13:45:38 +02:00 committed by Bastien Nocera
parent f6d6fe577f
commit ed28e761d5

View File

@ -83,7 +83,9 @@ enum
PROP_CARD_INDEX, PROP_CARD_INDEX,
PROP_PORT, PROP_PORT,
PROP_STATE, PROP_STATE,
N_PROPS
}; };
static GParamSpec *obj_props[N_PROPS] = { NULL, };
static void gvc_mixer_stream_finalize (GObject *object); static void gvc_mixer_stream_finalize (GObject *object);
@ -198,7 +200,7 @@ gvc_mixer_stream_set_volume (GvcMixerStream *stream,
if (!pa_cvolume_equal(gvc_channel_map_get_cvolume(stream->priv->channel_map), &cv)) { if (!pa_cvolume_equal(gvc_channel_map_get_cvolume(stream->priv->channel_map), &cv)) {
gvc_channel_map_volume_changed(stream->priv->channel_map, &cv, FALSE); gvc_channel_map_volume_changed(stream->priv->channel_map, &cv, FALSE);
g_object_notify (G_OBJECT (stream), "volume"); g_object_notify_by_pspec (G_OBJECT (stream), obj_props[PROP_VOLUME]);
return TRUE; return TRUE;
} }
@ -218,7 +220,7 @@ gvc_mixer_stream_set_decibel (GvcMixerStream *stream,
if (!pa_cvolume_equal(gvc_channel_map_get_cvolume(stream->priv->channel_map), &cv)) { if (!pa_cvolume_equal(gvc_channel_map_get_cvolume(stream->priv->channel_map), &cv)) {
gvc_channel_map_volume_changed(stream->priv->channel_map, &cv, FALSE); gvc_channel_map_volume_changed(stream->priv->channel_map, &cv, FALSE);
g_object_notify (G_OBJECT (stream), "volume"); g_object_notify_by_pspec (G_OBJECT (stream), obj_props[PROP_VOLUME]);
} }
return TRUE; return TRUE;
@ -246,7 +248,7 @@ gvc_mixer_stream_set_is_muted (GvcMixerStream *stream,
if (is_muted != stream->priv->is_muted) { if (is_muted != stream->priv->is_muted) {
stream->priv->is_muted = is_muted; stream->priv->is_muted = is_muted;
g_object_notify (G_OBJECT (stream), "is-muted"); g_object_notify_by_pspec (G_OBJECT (stream), obj_props[PROP_IS_MUTED]);
} }
return TRUE; return TRUE;
@ -260,7 +262,7 @@ gvc_mixer_stream_set_can_decibel (GvcMixerStream *stream,
if (can_decibel != stream->priv->can_decibel) { if (can_decibel != stream->priv->can_decibel) {
stream->priv->can_decibel = can_decibel; stream->priv->can_decibel = can_decibel;
g_object_notify (G_OBJECT (stream), "can-decibel"); g_object_notify_by_pspec (G_OBJECT (stream), obj_props[PROP_CAN_DECIBEL]);
} }
return TRUE; return TRUE;
@ -288,7 +290,7 @@ gvc_mixer_stream_set_name (GvcMixerStream *stream,
g_free (stream->priv->name); g_free (stream->priv->name);
stream->priv->name = g_strdup (name); stream->priv->name = g_strdup (name);
g_object_notify (G_OBJECT (stream), "name"); g_object_notify_by_pspec (G_OBJECT (stream), obj_props[PROP_NAME]);
return TRUE; return TRUE;
} }
@ -301,7 +303,7 @@ gvc_mixer_stream_set_description (GvcMixerStream *stream,
g_free (stream->priv->description); g_free (stream->priv->description);
stream->priv->description = g_strdup (description); stream->priv->description = g_strdup (description);
g_object_notify (G_OBJECT (stream), "description"); g_object_notify_by_pspec (G_OBJECT (stream), obj_props[PROP_DESCRIPTION]);
return TRUE; return TRUE;
} }
@ -321,7 +323,7 @@ gvc_mixer_stream_set_is_event_stream (GvcMixerStream *stream,
g_return_val_if_fail (GVC_IS_MIXER_STREAM (stream), FALSE); g_return_val_if_fail (GVC_IS_MIXER_STREAM (stream), FALSE);
stream->priv->is_event_stream = is_event_stream; stream->priv->is_event_stream = is_event_stream;
g_object_notify (G_OBJECT (stream), "is-event-stream"); g_object_notify_by_pspec (G_OBJECT (stream), obj_props[PROP_IS_EVENT_STREAM]);
return TRUE; return TRUE;
} }
@ -341,7 +343,7 @@ gvc_mixer_stream_set_is_virtual (GvcMixerStream *stream,
g_return_val_if_fail (GVC_IS_MIXER_STREAM (stream), FALSE); g_return_val_if_fail (GVC_IS_MIXER_STREAM (stream), FALSE);
stream->priv->is_virtual = is_virtual; stream->priv->is_virtual = is_virtual;
g_object_notify (G_OBJECT (stream), "is-virtual"); g_object_notify_by_pspec (G_OBJECT (stream), obj_props[PROP_IS_VIRTUAL]);
return TRUE; return TRUE;
} }
@ -361,7 +363,7 @@ gvc_mixer_stream_set_application_id (GvcMixerStream *stream,
g_free (stream->priv->application_id); g_free (stream->priv->application_id);
stream->priv->application_id = g_strdup (application_id); stream->priv->application_id = g_strdup (application_id);
g_object_notify (G_OBJECT (stream), "application-id"); g_object_notify_by_pspec (G_OBJECT (stream), obj_props[PROP_APPLICATION_ID]);
return TRUE; return TRUE;
} }
@ -374,7 +376,7 @@ on_channel_map_volume_changed (GvcChannelMap *channel_map,
if (set == TRUE) if (set == TRUE)
gvc_mixer_stream_push_volume (stream); gvc_mixer_stream_push_volume (stream);
g_object_notify (G_OBJECT (stream), "volume"); g_object_notify_by_pspec (G_OBJECT (stream), obj_props[PROP_VOLUME]);
} }
static gboolean static gboolean
@ -402,7 +404,7 @@ gvc_mixer_stream_set_channel_map (GvcMixerStream *stream,
G_CALLBACK (on_channel_map_volume_changed), G_CALLBACK (on_channel_map_volume_changed),
stream); stream);
g_object_notify (G_OBJECT (stream), "channel-map"); g_object_notify_by_pspec (G_OBJECT (stream), obj_props[PROP_CHANNEL_MAP]);
} }
return TRUE; return TRUE;
@ -452,7 +454,7 @@ gvc_mixer_stream_set_icon_name (GvcMixerStream *stream,
g_free (stream->priv->icon_name); g_free (stream->priv->icon_name);
stream->priv->icon_name = g_strdup (icon_name); stream->priv->icon_name = g_strdup (icon_name);
g_object_notify (G_OBJECT (stream), "icon-name"); g_object_notify_by_pspec (G_OBJECT (stream), obj_props[PROP_ICON_NAME]);
return TRUE; return TRUE;
} }
@ -465,7 +467,7 @@ gvc_mixer_stream_set_form_factor (GvcMixerStream *stream,
g_free (stream->priv->form_factor); g_free (stream->priv->form_factor);
stream->priv->form_factor = g_strdup (form_factor); stream->priv->form_factor = g_strdup (form_factor);
g_object_notify (G_OBJECT (stream), "form-factor"); g_object_notify_by_pspec (G_OBJECT (stream), obj_props[PROP_FORM_FACTOR]);
return TRUE; return TRUE;
} }
@ -478,7 +480,7 @@ gvc_mixer_stream_set_sysfs_path (GvcMixerStream *stream,
g_free (stream->priv->sysfs_path); g_free (stream->priv->sysfs_path);
stream->priv->sysfs_path = g_strdup (sysfs_path); stream->priv->sysfs_path = g_strdup (sysfs_path);
g_object_notify (G_OBJECT (stream), "sysfs-path"); g_object_notify_by_pspec (G_OBJECT (stream), obj_props[PROP_SYSFS_PATH]);
return TRUE; return TRUE;
} }
@ -558,7 +560,7 @@ gvc_mixer_stream_set_port (GvcMixerStream *stream,
} }
} }
g_object_notify (G_OBJECT (stream), "port"); g_object_notify_by_pspec (G_OBJECT (stream), obj_props[PROP_PORT]);
return TRUE; return TRUE;
} }
@ -591,7 +593,7 @@ gvc_mixer_stream_set_state (GvcMixerStream *stream,
if (stream->priv->state != state) { if (stream->priv->state != state) {
stream->priv->state = state; stream->priv->state = state;
g_object_notify (G_OBJECT (stream), "state"); g_object_notify_by_pspec (G_OBJECT (stream), obj_props[PROP_STATE]);
} }
return TRUE; return TRUE;
@ -645,7 +647,7 @@ gvc_mixer_stream_set_card_index (GvcMixerStream *stream,
g_return_val_if_fail (GVC_IS_MIXER_STREAM (stream), FALSE); g_return_val_if_fail (GVC_IS_MIXER_STREAM (stream), FALSE);
stream->priv->card_index = card_index; stream->priv->card_index = card_index;
g_object_notify (G_OBJECT (stream), "card-index"); g_object_notify_by_pspec (G_OBJECT (stream), obj_props[PROP_CARD_INDEX]);
return TRUE; return TRUE;
} }
@ -897,140 +899,103 @@ gvc_mixer_stream_class_init (GvcMixerStreamClass *klass)
klass->change_port = gvc_mixer_stream_real_change_port; klass->change_port = gvc_mixer_stream_real_change_port;
klass->change_is_muted = gvc_mixer_stream_real_change_is_muted; klass->change_is_muted = gvc_mixer_stream_real_change_is_muted;
g_object_class_install_property (gobject_class, obj_props[PROP_INDEX] = g_param_spec_ulong ("index",
PROP_INDEX, "Index",
g_param_spec_ulong ("index", "The index for this stream",
"Index", 0, G_MAXULONG, 0,
"The index for this stream", G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY|G_PARAM_STATIC_STRINGS);
0, G_MAXULONG, 0, obj_props[PROP_ID] = g_param_spec_ulong ("id",
G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)); "id",
g_object_class_install_property (gobject_class, "The id for this stream",
PROP_ID, 0, G_MAXULONG, 0,
g_param_spec_ulong ("id", G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY|G_PARAM_STATIC_STRINGS);
"id", obj_props[PROP_CHANNEL_MAP] = g_param_spec_object ("channel-map",
"The id for this stream", "channel map",
0, G_MAXULONG, 0, "The channel map for this stream",
G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)); GVC_TYPE_CHANNEL_MAP,
g_object_class_install_property (gobject_class, G_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_STATIC_STRINGS);
PROP_CHANNEL_MAP, obj_props[PROP_PA_CONTEXT] = g_param_spec_pointer ("pa-context",
g_param_spec_object ("channel-map", "PulseAudio context",
"channel map", "The PulseAudio context for this stream",
"The channel map for this stream", G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY|G_PARAM_STATIC_STRINGS);
GVC_TYPE_CHANNEL_MAP, obj_props[PROP_VOLUME] = g_param_spec_ulong ("volume",
G_PARAM_READWRITE|G_PARAM_CONSTRUCT)); "Volume",
g_object_class_install_property (gobject_class, "The volume for this stream",
PROP_PA_CONTEXT, 0, G_MAXULONG, 0,
g_param_spec_pointer ("pa-context", G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS);
"PulseAudio context", obj_props[PROP_DECIBEL] = g_param_spec_double ("decibel",
"The PulseAudio context for this stream", "Decibel",
G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)); "The decibel level for this stream",
g_object_class_install_property (gobject_class, -G_MAXDOUBLE, G_MAXDOUBLE, 0,
PROP_VOLUME, G_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_STATIC_STRINGS);
g_param_spec_ulong ("volume", obj_props[PROP_NAME] = g_param_spec_string ("name",
"Volume", "Name",
"The volume for this stream", "Name to display for this stream",
0, G_MAXULONG, 0, NULL,
G_PARAM_READWRITE)); G_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_STATIC_STRINGS);
g_object_class_install_property (gobject_class, obj_props[PROP_DESCRIPTION] = g_param_spec_string ("description",
PROP_DECIBEL, "Description",
g_param_spec_double ("decibel", "Description to display for this stream",
"Decibel", NULL,
"The decibel level for this stream", G_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_STATIC_STRINGS);
-G_MAXDOUBLE, G_MAXDOUBLE, 0, obj_props[PROP_APPLICATION_ID] = g_param_spec_string ("application-id",
G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
g_object_class_install_property (gobject_class,
PROP_NAME,
g_param_spec_string ("name",
"Name",
"Name to display for this stream",
NULL,
G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
g_object_class_install_property (gobject_class,
PROP_DESCRIPTION,
g_param_spec_string ("description",
"Description",
"Description to display for this stream",
NULL,
G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
g_object_class_install_property (gobject_class,
PROP_APPLICATION_ID,
g_param_spec_string ("application-id",
"Application identifier", "Application identifier",
"Application identifier for this stream", "Application identifier for this stream",
NULL, NULL,
G_PARAM_READWRITE|G_PARAM_CONSTRUCT)); G_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_STATIC_STRINGS);
g_object_class_install_property (gobject_class, obj_props[PROP_ICON_NAME] = g_param_spec_string ("icon-name",
PROP_ICON_NAME, "Icon Name",
g_param_spec_string ("icon-name", "Name of icon to display for this stream",
"Icon Name", NULL,
"Name of icon to display for this stream", G_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_STATIC_STRINGS);
NULL, obj_props[PROP_FORM_FACTOR] = g_param_spec_string ("form-factor",
G_PARAM_READWRITE|G_PARAM_CONSTRUCT)); "Form Factor",
g_object_class_install_property (gobject_class, "Device form factor for this stream, as reported by PulseAudio",
PROP_FORM_FACTOR, NULL,
g_param_spec_string ("form-factor", G_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_STATIC_STRINGS);
"Form Factor", obj_props[PROP_SYSFS_PATH] = g_param_spec_string ("sysfs-path",
"Device form factor for this stream, as reported by PulseAudio", "Sysfs path",
NULL, "Sysfs path for the device associated with this stream",
G_PARAM_READWRITE|G_PARAM_CONSTRUCT)); NULL,
g_object_class_install_property (gobject_class, G_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_STATIC_STRINGS);
PROP_SYSFS_PATH, obj_props[PROP_IS_MUTED] = g_param_spec_boolean ("is-muted",
g_param_spec_string ("sysfs-path", "is muted",
"Sysfs path", "Whether stream is muted",
"Sysfs path for the device associated with this stream", FALSE,
NULL, G_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_STATIC_STRINGS);
G_PARAM_READWRITE|G_PARAM_CONSTRUCT)); obj_props[PROP_CAN_DECIBEL] = g_param_spec_boolean ("can-decibel",
g_object_class_install_property (gobject_class, "can decibel",
PROP_IS_MUTED, "Whether stream volume can be converted to decibel units",
g_param_spec_boolean ("is-muted", FALSE,
"is muted", G_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_STATIC_STRINGS);
"Whether stream is muted", obj_props[PROP_IS_EVENT_STREAM] = g_param_spec_boolean ("is-event-stream",
FALSE, "is event stream",
G_PARAM_READWRITE|G_PARAM_CONSTRUCT)); "Whether stream's role is to play an event",
g_object_class_install_property (gobject_class, FALSE,
PROP_CAN_DECIBEL, G_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_STATIC_STRINGS);
g_param_spec_boolean ("can-decibel", obj_props[PROP_IS_VIRTUAL] = g_param_spec_boolean ("is-virtual",
"can decibel", "is virtual stream",
"Whether stream volume can be converted to decibel units", "Whether the stream is virtual",
FALSE, FALSE,
G_PARAM_READWRITE|G_PARAM_CONSTRUCT)); G_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_STATIC_STRINGS);
g_object_class_install_property (gobject_class, obj_props[PROP_PORT] = g_param_spec_string ("port",
PROP_IS_EVENT_STREAM, "Port",
g_param_spec_boolean ("is-event-stream", "The name of the current port for this stream",
"is event stream", NULL,
"Whether stream's role is to play an event", G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS);
FALSE, obj_props[PROP_STATE] = g_param_spec_enum ("state",
G_PARAM_READWRITE|G_PARAM_CONSTRUCT)); "State",
g_object_class_install_property (gobject_class, "The current state of this stream",
PROP_IS_VIRTUAL, GVC_TYPE_MIXER_STREAM_STATE,
g_param_spec_boolean ("is-virtual", GVC_STREAM_STATE_INVALID,
"is virtual stream", G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS);
"Whether the stream is virtual", obj_props[PROP_CARD_INDEX] = g_param_spec_long ("card-index",
FALSE, "Card index",
G_PARAM_READWRITE|G_PARAM_CONSTRUCT)); "The index of the card for this stream",
g_object_class_install_property (gobject_class, PA_INVALID_INDEX, G_MAXLONG, PA_INVALID_INDEX,
PROP_PORT, G_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_STATIC_STRINGS);
g_param_spec_string ("port",
"Port", g_object_class_install_properties (gobject_class, N_PROPS, obj_props);
"The name of the current port for this stream",
NULL,
G_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_STATE,
g_param_spec_enum ("state",
"State",
"The current state of this stream",
GVC_TYPE_MIXER_STREAM_STATE,
GVC_STREAM_STATE_INVALID,
G_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_CARD_INDEX,
g_param_spec_long ("card-index",
"Card index",
"The index of the card for this stream",
PA_INVALID_INDEX, G_MAXLONG, PA_INVALID_INDEX,
G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
} }
static void static void