|
| 1 | +#include "idle-inhibit.h" |
| 2 | + |
| 3 | +#include <gdk/gdk.h> |
| 4 | +#include <gdk/gdkwayland.h> |
| 5 | +#include <gio/gio.h> |
| 6 | +#include <glib-object.h> |
| 7 | +#include <glib.h> |
| 8 | +#include <gtk/gtk.h> |
| 9 | +#include <wayland-client-protocol.h> |
| 10 | +#include <wayland-client.h> |
| 11 | + |
| 12 | +#include "idle-inhibit-unstable-v1-client.h" |
| 13 | + |
| 14 | +struct _AstalInhibitManager { |
| 15 | + GObject parent_instance; |
| 16 | +}; |
| 17 | + |
| 18 | +typedef struct { |
| 19 | + gboolean init; |
| 20 | + struct wl_registry* wl_registry; |
| 21 | + struct wl_display* display; |
| 22 | + struct zwp_idle_inhibit_manager_v1* idle_inhibit_manager; |
| 23 | +} AstalInhibitManagerPrivate; |
| 24 | + |
| 25 | +G_DEFINE_TYPE_WITH_PRIVATE(AstalInhibitManager, astal_inhibit_manager, G_TYPE_OBJECT) |
| 26 | + |
| 27 | +AstalInhibitor* astal_inhibit_manager_inhibit(AstalInhibitManager* self, GtkWindow* window) { |
| 28 | + AstalInhibitManagerPrivate* priv = astal_inhibit_manager_get_instance_private(self); |
| 29 | + g_assert_true(priv->init); |
| 30 | + GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window)); |
| 31 | + struct wl_surface* surface = gdk_wayland_window_get_wl_surface(gdk_window); |
| 32 | + return zwp_idle_inhibit_manager_v1_create_inhibitor(priv->idle_inhibit_manager, surface); |
| 33 | +} |
| 34 | + |
| 35 | +static void global_registry_handler(void* data, struct wl_registry* registry, uint32_t id, |
| 36 | + const char* interface, uint32_t version) { |
| 37 | + AstalInhibitManager* self = ASTAL_INHIBIT_MANAGER(data); |
| 38 | + AstalInhibitManagerPrivate* priv = astal_inhibit_manager_get_instance_private(self); |
| 39 | + |
| 40 | + if (strcmp(interface, zwp_idle_inhibit_manager_v1_interface.name) == 0) { |
| 41 | + priv->idle_inhibit_manager = |
| 42 | + wl_registry_bind(registry, id, &zwp_idle_inhibit_manager_v1_interface, 1); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +static void global_registry_remover(void* data, struct wl_registry* registry, uint32_t id) { |
| 47 | + // neither inhibit_manager nor inhibitor is going to be removed by the compositor, so we don't |
| 48 | + // need do anything here. |
| 49 | +} |
| 50 | + |
| 51 | +static const struct wl_registry_listener registry_listener = {global_registry_handler, |
| 52 | + global_registry_remover}; |
| 53 | + |
| 54 | +static gboolean astal_inhibit_manager_wayland_init(AstalInhibitManager* self) { |
| 55 | + AstalInhibitManagerPrivate* priv = astal_inhibit_manager_get_instance_private(self); |
| 56 | + |
| 57 | + if (priv->init) return TRUE; |
| 58 | + |
| 59 | + GdkDisplay* gdk_display = gdk_display_get_default(); |
| 60 | + priv->display = gdk_wayland_display_get_wl_display(gdk_display); |
| 61 | + |
| 62 | + priv->wl_registry = wl_display_get_registry(priv->display); |
| 63 | + wl_registry_add_listener(priv->wl_registry, ®istry_listener, self); |
| 64 | + |
| 65 | + wl_display_roundtrip(priv->display); |
| 66 | + |
| 67 | + if (priv->idle_inhibit_manager == NULL) { |
| 68 | + g_critical("Can not connect idle inhibitor protocol"); |
| 69 | + return FALSE; |
| 70 | + } |
| 71 | + |
| 72 | + priv->init = TRUE; |
| 73 | + return TRUE; |
| 74 | +} |
| 75 | + |
| 76 | +AstalInhibitManager* astal_inhibit_manager_get_default() { |
| 77 | + static AstalInhibitManager* self = NULL; |
| 78 | + |
| 79 | + if (self == NULL) { |
| 80 | + self = g_object_new(ASTAL_TYPE_INHIBIT_MANAGER, NULL); |
| 81 | + if (!astal_inhibit_manager_wayland_init(self)) { |
| 82 | + g_object_unref(self); |
| 83 | + self = NULL; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return self; |
| 88 | +} |
| 89 | + |
| 90 | +static void astal_inhibit_manager_init(AstalInhibitManager* self) { |
| 91 | + AstalInhibitManagerPrivate* priv = astal_inhibit_manager_get_instance_private(self); |
| 92 | + priv->init = FALSE; |
| 93 | + priv->display = NULL; |
| 94 | + priv->wl_registry = NULL; |
| 95 | + priv->idle_inhibit_manager = NULL; |
| 96 | +} |
| 97 | + |
| 98 | +static void astal_inhibit_manager_finalize(GObject* object) { |
| 99 | + AstalInhibitManager* self = ASTAL_INHIBIT_MANAGER(object); |
| 100 | + AstalInhibitManagerPrivate* priv = astal_inhibit_manager_get_instance_private(self); |
| 101 | + |
| 102 | + if (priv->display != NULL) wl_display_roundtrip(priv->display); |
| 103 | + |
| 104 | + if (priv->wl_registry != NULL) wl_registry_destroy(priv->wl_registry); |
| 105 | + if (priv->idle_inhibit_manager != NULL) |
| 106 | + zwp_idle_inhibit_manager_v1_destroy(priv->idle_inhibit_manager); |
| 107 | + |
| 108 | + G_OBJECT_CLASS(astal_inhibit_manager_parent_class)->finalize(object); |
| 109 | +} |
| 110 | + |
| 111 | +static void astal_inhibit_manager_class_init(AstalInhibitManagerClass* class) { |
| 112 | + GObjectClass* object_class = G_OBJECT_CLASS(class); |
| 113 | + object_class->finalize = astal_inhibit_manager_finalize; |
| 114 | +} |
0 commit comments