tests/screen-cast: Avoid undefined behavior with GSource

Follow the existing convention in Mutter and avoid downcasting
custom GSource structs.

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/2406>
This commit is contained in:
Dor Askayo 2022-05-07 23:59:06 +03:00 committed by Marge Bot
parent 61c9e344e4
commit 40edfbcbeb

View File

@ -83,18 +83,18 @@ typedef struct _ScreenCast
typedef struct _PipeWireSource typedef struct _PipeWireSource
{ {
GSource base; GSource source;
struct pw_loop *pipewire_loop; struct pw_loop *pipewire_loop;
} PipeWireSource; } PipeWireSource;
static PipeWireSource *_pipewire_source; static GSource *_pipewire_source;
static struct pw_context *_pipewire_context; static struct pw_context *_pipewire_context;
static struct pw_core *_pipewire_core; static struct pw_core *_pipewire_core;
static struct spa_hook _pipewire_core_listener; static struct spa_hook _pipewire_core_listener;
static gboolean static gboolean
pipewire_loop_source_prepare (GSource *base, pipewire_loop_source_prepare (GSource *source,
int *timeout) int *timeout)
{ {
*timeout = -1; *timeout = -1;
@ -133,24 +133,26 @@ static GSourceFuncs pipewire_source_funcs =
pipewire_loop_source_finalize pipewire_loop_source_finalize
}; };
static PipeWireSource * static GSource *
create_pipewire_source (void) create_pipewire_source (struct pw_loop *pipewire_loop)
{ {
GSource *source;
PipeWireSource *pipewire_source; PipeWireSource *pipewire_source;
pipewire_source = source = g_source_new (&pipewire_source_funcs,
(PipeWireSource *) g_source_new (&pipewire_source_funcs, sizeof (PipeWireSource));
sizeof (PipeWireSource));
pipewire_source->pipewire_loop = pw_loop_new (NULL); pipewire_source = (PipeWireSource *) source;
g_assert_nonnull (pipewire_source->pipewire_loop); pipewire_source->pipewire_loop = pipewire_loop;
g_source_add_unix_fd (&pipewire_source->base,
g_source_add_unix_fd (source,
pw_loop_get_fd (pipewire_source->pipewire_loop), pw_loop_get_fd (pipewire_source->pipewire_loop),
G_IO_IN | G_IO_ERR); G_IO_IN | G_IO_ERR);
pw_loop_enter (pipewire_source->pipewire_loop); pw_loop_enter (pipewire_source->pipewire_loop);
g_source_attach (&pipewire_source->base, NULL); g_source_attach (source, NULL);
return pipewire_source; return source;
} }
static void static void
@ -171,9 +173,15 @@ static const struct pw_core_events core_events = {
static void static void
init_pipewire (void) init_pipewire (void)
{ {
struct pw_loop *pipewire_loop;
pw_init (NULL, NULL); pw_init (NULL, NULL);
_pipewire_source = create_pipewire_source ();
_pipewire_context = pw_context_new (_pipewire_source->pipewire_loop, pipewire_loop = pw_loop_new (NULL);
g_assert_nonnull (pipewire_loop);
_pipewire_source = create_pipewire_source (pipewire_loop);
_pipewire_context = pw_context_new (pipewire_loop,
NULL, 0); NULL, 0);
g_assert_nonnull (_pipewire_context); g_assert_nonnull (_pipewire_context);
_pipewire_core = pw_context_connect (_pipewire_context, NULL, 0); _pipewire_core = pw_context_connect (_pipewire_context, NULL, 0);
@ -192,8 +200,8 @@ release_pipewire (void)
g_clear_pointer (&_pipewire_context, pw_context_destroy); g_clear_pointer (&_pipewire_context, pw_context_destroy);
if (_pipewire_source) if (_pipewire_source)
{ {
g_source_destroy ((GSource *) _pipewire_source); g_source_destroy (_pipewire_source);
g_source_unref ((GSource *) _pipewire_source); g_source_unref (_pipewire_source);
_pipewire_source = NULL; _pipewire_source = NULL;
} }
} }