From c483a57e848aa966eec6dfcb9d31786d83050a33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Wed, 24 Jul 2019 17:10:49 +0200 Subject: [PATCH 28/28] cleanup: Unref GSource's once attached When a source is attached the main contaxt takes the ownership of it, so we can safely them. https://gitlab.gnome.org/GNOME/mutter/merge_requests/682 --- clutter/clutter/clutter-backend.c | 1 + clutter/clutter/clutter-master-clock-default.c | 2 +- clutter/clutter/evdev/clutter-device-manager-evdev.c | 2 +- src/backends/meta-screen-cast-stream-src.c | 1 + src/backends/x11/meta-backend-x11.c | 3 ++- src/wayland/meta-wayland.c | 2 +- 6 files changed, 7 insertions(+), 4 deletions(-) diff --git a/clutter/clutter/clutter-backend.c b/clutter/clutter/clutter-backend.c index 41ea3daed..df51464cc 100644 --- a/clutter/clutter/clutter-backend.c +++ b/clutter/clutter/clutter-backend.c @@ -381,60 +381,61 @@ clutter_backend_real_create_context (ClutterBackend *backend, if (clutter_backend_do_real_create_context (backend, all_known_drivers[j].driver_id, &internal_error)) break; if (internal_error) { CLUTTER_NOTE (BACKEND, "Unable to use the %s driver: %s", all_known_drivers[j].driver_desc, internal_error->message); g_clear_error (&internal_error); } } } } g_strfreev (known_drivers); if (backend->cogl_context == NULL) { if (internal_error != NULL) g_propagate_error (error, internal_error); else g_set_error_literal (error, CLUTTER_INIT_ERROR, CLUTTER_INIT_ERROR_BACKEND, _("Unable to initialize the Clutter backend: no available drivers found.")); return FALSE; } backend->cogl_source = cogl_glib_source_new (backend->cogl_context, G_PRIORITY_DEFAULT); g_source_attach (backend->cogl_source, NULL); + g_source_unref (backend->cogl_source); return TRUE; } static ClutterFeatureFlags clutter_backend_real_get_features (ClutterBackend *backend) { ClutterFeatureFlags flags = 0; if (cogl_clutter_winsys_has_feature (COGL_WINSYS_FEATURE_MULTIPLE_ONSCREEN)) { CLUTTER_NOTE (BACKEND, "Cogl supports multiple onscreen framebuffers"); flags |= CLUTTER_FEATURE_STAGE_MULTIPLE; } else { CLUTTER_NOTE (BACKEND, "Cogl only supports one onscreen framebuffer"); flags |= CLUTTER_FEATURE_STAGE_STATIC; } if (cogl_clutter_winsys_has_feature (COGL_WINSYS_FEATURE_SWAP_THROTTLE)) { CLUTTER_NOTE (BACKEND, "Cogl supports swap buffers throttling"); flags |= CLUTTER_FEATURE_SYNC_TO_VBLANK; } else CLUTTER_NOTE (BACKEND, "Cogl doesn't support swap buffers throttling"); if (cogl_clutter_winsys_has_feature (COGL_WINSYS_FEATURE_SWAP_BUFFERS_EVENT)) { diff --git a/clutter/clutter/clutter-master-clock-default.c b/clutter/clutter/clutter-master-clock-default.c index 7b2df0df3..83b1eadcb 100644 --- a/clutter/clutter/clutter-master-clock-default.c +++ b/clutter/clutter/clutter-master-clock-default.c @@ -577,61 +577,61 @@ clutter_clock_dispatch (GSource *source, g_slist_free (stages); master_clock->prev_tick = master_clock->cur_tick; _clutter_threads_release_lock (); return TRUE; } static void clutter_master_clock_default_finalize (GObject *gobject) { ClutterMasterClockDefault *master_clock = CLUTTER_MASTER_CLOCK_DEFAULT (gobject); g_slist_free (master_clock->timelines); G_OBJECT_CLASS (clutter_master_clock_default_parent_class)->finalize (gobject); } static void clutter_master_clock_default_class_init (ClutterMasterClockDefaultClass *klass) { GObjectClass *gobject_class = G_OBJECT_CLASS (klass); gobject_class->finalize = clutter_master_clock_default_finalize; } static void clutter_master_clock_default_init (ClutterMasterClockDefault *self) { - GSource *source; + g_autoptr (GSource) source = NULL; source = clutter_clock_source_new (self); self->source = source; self->idle = FALSE; self->ensure_next_iteration = FALSE; self->paused = FALSE; #ifdef CLUTTER_ENABLE_DEBUG self->frame_budget = G_USEC_PER_SEC / 60; #endif g_source_set_priority (source, CLUTTER_PRIORITY_REDRAW); g_source_set_can_recurse (source, FALSE); g_source_attach (source, NULL); } static void clutter_master_clock_default_add_timeline (ClutterMasterClock *clock, ClutterTimeline *timeline) { ClutterMasterClockDefault *master_clock = (ClutterMasterClockDefault *) clock; gboolean is_first; if (g_slist_find (master_clock->timelines, timeline)) return; is_first = master_clock->timelines == NULL; master_clock->timelines = g_slist_prepend (master_clock->timelines, diff --git a/clutter/clutter/evdev/clutter-device-manager-evdev.c b/clutter/clutter/evdev/clutter-device-manager-evdev.c index 11cb4781a..49566c233 100644 --- a/clutter/clutter/evdev/clutter-device-manager-evdev.c +++ b/clutter/clutter/evdev/clutter-device-manager-evdev.c @@ -706,61 +706,61 @@ clutter_event_dispatch (GSource *g_source, /* Drop events if we don't have any stage to forward them to */ if (!_clutter_input_device_get_stage (input_device)) goto out; /* forward the event into clutter for emission etc. */ _clutter_stage_queue_event (event->any.stage, event, FALSE); /* update the device states *after* the event */ event_state = seat->button_state | xkb_state_serialize_mods (seat->xkb, XKB_STATE_MODS_EFFECTIVE); _clutter_input_device_set_state (seat->core_pointer, event_state); _clutter_input_device_set_state (seat->core_keyboard, event_state); } out: _clutter_threads_release_lock (); return TRUE; } static GSourceFuncs event_funcs = { clutter_event_prepare, clutter_event_check, clutter_event_dispatch, NULL }; static ClutterEventSource * clutter_event_source_new (ClutterDeviceManagerEvdev *manager_evdev) { ClutterDeviceManagerEvdevPrivate *priv = manager_evdev->priv; - GSource *source; + g_autoptr (GSource) source = NULL; ClutterEventSource *event_source; gint fd; source = g_source_new (&event_funcs, sizeof (ClutterEventSource)); event_source = (ClutterEventSource *) source; /* setup the source */ event_source->manager_evdev = manager_evdev; fd = libinput_get_fd (priv->libinput); event_source->event_poll_fd.fd = fd; event_source->event_poll_fd.events = G_IO_IN; /* and finally configure and attach the GSource */ g_source_set_priority (source, CLUTTER_PRIORITY_EVENTS); g_source_add_poll (source, &event_source->event_poll_fd); g_source_set_can_recurse (source, TRUE); g_source_attach (source, NULL); return event_source; } static void clutter_event_source_free (ClutterEventSource *source) { GSource *g_source = (GSource *) source; CLUTTER_NOTE (EVENT, "Removing GSource for evdev device manager"); /* ignore the return value of close, it's not like we can do something diff --git a/src/backends/meta-screen-cast-stream-src.c b/src/backends/meta-screen-cast-stream-src.c index 457c0589e..254ef983d 100644 --- a/src/backends/meta-screen-cast-stream-src.c +++ b/src/backends/meta-screen-cast-stream-src.c @@ -465,60 +465,61 @@ static void init_spa_type (MetaSpaType *type, struct spa_type_map *map) { spa_type_media_type_map (map, &type->media_type); spa_type_media_subtype_map (map, &type->media_subtype); spa_type_format_video_map (map, &type->format_video); spa_type_video_format_map (map, &type->video_format); } static MetaPipeWireSource * create_pipewire_source (void) { MetaPipeWireSource *pipewire_source; pipewire_source = (MetaPipeWireSource *) g_source_new (&pipewire_source_funcs, sizeof (MetaPipeWireSource)); pipewire_source->pipewire_loop = pw_loop_new (NULL); if (!pipewire_source->pipewire_loop) { g_source_destroy ((GSource *) pipewire_source); return NULL; } g_source_add_unix_fd (&pipewire_source->base, pw_loop_get_fd (pipewire_source->pipewire_loop), G_IO_IN | G_IO_ERR); pw_loop_enter (pipewire_source->pipewire_loop); g_source_attach (&pipewire_source->base, NULL); + g_source_unref (&pipewire_source->base); return pipewire_source; } static const struct pw_remote_events remote_events = { PW_VERSION_REMOTE_EVENTS, .state_changed = on_state_changed, }; static gboolean meta_screen_cast_stream_src_initable_init (GInitable *initable, GCancellable *cancellable, GError **error) { MetaScreenCastStreamSrc *src = META_SCREEN_CAST_STREAM_SRC (initable); MetaScreenCastStreamSrcPrivate *priv = meta_screen_cast_stream_src_get_instance_private (src); priv->pipewire_source = create_pipewire_source (); if (!priv->pipewire_source) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Failed to create PipeWire source"); return FALSE; } priv->pipewire_core = pw_core_new (priv->pipewire_source->pipewire_loop, NULL); if (!priv->pipewire_core) { diff --git a/src/backends/x11/meta-backend-x11.c b/src/backends/x11/meta-backend-x11.c index 3f8645e93..b9acf48c0 100644 --- a/src/backends/x11/meta-backend-x11.c +++ b/src/backends/x11/meta-backend-x11.c @@ -449,71 +449,72 @@ x_event_source_dispatch (GSource *source, gpointer user_data) { XEventSource *x_source = (XEventSource *) source; MetaBackend *backend = x_source->backend; MetaBackendX11 *x11 = META_BACKEND_X11 (backend); MetaBackendX11Private *priv = meta_backend_x11_get_instance_private (x11); while (XPending (priv->xdisplay)) { XEvent event; XNextEvent (priv->xdisplay, &event); handle_host_xevent (backend, &event); } return TRUE; } static GSourceFuncs x_event_funcs = { x_event_source_prepare, x_event_source_check, x_event_source_dispatch, }; static GSource * x_event_source_new (MetaBackend *backend) { MetaBackendX11 *x11 = META_BACKEND_X11 (backend); MetaBackendX11Private *priv = meta_backend_x11_get_instance_private (x11); - GSource *source; + g_autoptr (GSource) source = NULL; XEventSource *x_source; source = g_source_new (&x_event_funcs, sizeof (XEventSource)); x_source = (XEventSource *) source; x_source->backend = backend; x_source->event_poll_fd.fd = ConnectionNumber (priv->xdisplay); x_source->event_poll_fd.events = G_IO_IN; g_source_add_poll (source, &x_source->event_poll_fd); g_source_attach (source, NULL); + return source; } static void on_monitors_changed (MetaMonitorManager *manager, MetaBackend *backend) { MetaBackendX11 *x11 = META_BACKEND_X11 (backend); MetaBackendX11Private *priv = meta_backend_x11_get_instance_private (x11); priv->cached_current_logical_monitor = NULL; } static void meta_backend_x11_post_init (MetaBackend *backend) { MetaBackendX11 *x11 = META_BACKEND_X11 (backend); MetaBackendX11Private *priv = meta_backend_x11_get_instance_private (x11); MetaMonitorManager *monitor_manager; int major, minor; gboolean has_xi = FALSE; priv->source = x_event_source_new (backend); if (!XSyncQueryExtension (priv->xdisplay, &priv->xsync_event_base, &priv->xsync_error_base) || !XSyncInitialize (priv->xdisplay, &major, &minor)) meta_fatal ("Could not initialize XSync"); priv->counter = find_idletime_counter (priv); if (priv->counter == None) diff --git a/src/wayland/meta-wayland.c b/src/wayland/meta-wayland.c index bab6b4ac4..44f48a0ee 100644 --- a/src/wayland/meta-wayland.c +++ b/src/wayland/meta-wayland.c @@ -325,61 +325,61 @@ meta_wayland_pre_clutter_init (void) } static bool meta_xwayland_global_filter (const struct wl_client *client, const struct wl_global *global, void *data) { MetaWaylandCompositor *compositor = (MetaWaylandCompositor *) data; MetaXWaylandManager *xwayland_manager = &compositor->xwayland_manager; /* Keyboard grabbing protocol is for Xwayland only */ if (client != xwayland_manager->client) return (wl_global_get_interface (global) != &zwp_xwayland_keyboard_grab_manager_v1_interface); /* All others are visible to all clients */ return true; } void meta_wayland_override_display_name (char *display_name) { g_clear_pointer (&_display_name_override, g_free); _display_name_override = g_strdup (display_name); } void meta_wayland_init (void) { MetaWaylandCompositor *compositor = meta_wayland_compositor_get_default (); - GSource *wayland_event_source; + g_autoptr (GSource) wayland_event_source = NULL; wayland_event_source = wayland_event_source_new (compositor->wayland_display); /* XXX: Here we are setting the wayland event source to have a * slightly lower priority than the X event source, because we are * much more likely to get confused being told about surface changes * relating to X clients when we don't know what's happened to them * according to the X protocol. */ g_source_set_priority (wayland_event_source, GDK_PRIORITY_EVENTS + 1); g_source_attach (wayland_event_source, NULL); if (!wl_global_create (compositor->wayland_display, &wl_compositor_interface, META_WL_COMPOSITOR_VERSION, compositor, compositor_bind)) g_error ("Failed to register the global wl_compositor"); wl_display_init_shm (compositor->wayland_display); meta_wayland_outputs_init (compositor); meta_wayland_data_device_manager_init (compositor); meta_wayland_subsurfaces_init (compositor); meta_wayland_shell_init (compositor); meta_wayland_pointer_gestures_init (compositor); meta_wayland_tablet_manager_init (compositor); meta_wayland_seat_init (compositor); meta_wayland_relative_pointer_init (compositor); meta_wayland_pointer_constraints_init (compositor); meta_wayland_xdg_foreign_init (compositor); -- 2.26.2