From ae614ee5125891a4d75fac97c754207f97834a7d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Leonardo=20Hern=C3=A1ndez=20Hern=C3=A1ndez?=
 <leohdz172@protonmail.com>
Date: Mon, 12 Jul 2021 19:58:55 -0500
Subject: implement idle-inhibitor protocol

This allows clients to disable idle monitoring
---
 README.md |  2 +-
 dwl.c     | 27 +++++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 13a0000..22172d5 100644
--- a/README.md
+++ b/README.md
@@ -14,6 +14,7 @@ dwl is not meant to provide every feature under the sun. Instead, like dwm, it s
 - Any features provided by dwm/Xlib: simple window borders, tags, keybindings, client rules, mouse move/resize. Providing a built-in status bar is an exception to this goal, to avoid dependencies on font rendering and/or drawing libraries when an external bar could work well.
 - Configurable multi-monitor layout support, including position and rotation
 - Configurable HiDPI/multi-DPI support
+- Idle-inhibit protocol which lets applications such as mpv disable idle monitoring
 - Provide information to external status bars via stdout/stdin
 - Urgency hints via xdg-activate protocol
 - Various Wayland protocols
@@ -26,7 +27,6 @@ Features under consideration (possibly as patches) are:
 
 - Protocols made trivial by wlroots
 - Implement the input-inhibitor protocol to support screen lockers (see https://github.com/djpohly/dwl/pull/132)
-- Implement the idle-inhibit protocol which lets applications such as mpv disable idle monitoring (see https://github.com/djpohly/dwl/pull/133)
 - Implement the text-input and input-method protocols to support IME once ibus implements input-method v2 (see https://github.com/ibus/ibus/pull/2256 and https://github.com/djpohly/dwl/pull/12)
 
 Feature *non-goals* for the main codebase include:
diff --git a/dwl.c b/dwl.c
index f8c2264..01d165b 100644
--- a/dwl.c
+++ b/dwl.c
@@ -23,6 +23,7 @@
 #include <wlr/types/wlr_gamma_control_v1.h>
 #include <wlr/types/wlr_input_device.h>
 #include <wlr/types/wlr_idle.h>
+#include <wlr/types/wlr_idle_inhibit_v1.h>
 #include <wlr/types/wlr_layer_shell_v1.h>
 #include <wlr/types/wlr_keyboard.h>
 #include <wlr/types/wlr_matrix.h>
@@ -219,12 +220,14 @@ static void cleanupmon(struct wl_listener *listener, void *data);
 static void closemon(Monitor *m);
 static void commitlayersurfacenotify(struct wl_listener *listener, void *data);
 static void commitnotify(struct wl_listener *listener, void *data);
+static void createidleinhibitor(struct wl_listener *listener, void *data);
 static void createkeyboard(struct wlr_input_device *device);
 static void createmon(struct wl_listener *listener, void *data);
 static void createnotify(struct wl_listener *listener, void *data);
 static void createlayersurface(struct wl_listener *listener, void *data);
 static void createpointer(struct wlr_input_device *device);
 static void cursorframe(struct wl_listener *listener, void *data);
+static void destroyidleinhibitor(struct wl_listener *listener, void *data);
 static void destroylayersurfacenotify(struct wl_listener *listener, void *data);
 static void destroynotify(struct wl_listener *listener, void *data);
 static Monitor *dirtomon(enum wlr_direction dir);
@@ -307,6 +310,7 @@ static struct wlr_xdg_activation_v1 *activation;
 static struct wl_list clients; /* tiling order */
 static struct wl_list fstack;  /* focus order */
 static struct wlr_idle *idle;
+static struct wlr_idle_inhibit_manager_v1 *idle_inhibit_mgr;
 static struct wlr_layer_shell_v1 *layer_shell;
 static struct wlr_output_manager_v1 *output_mgr;
 static struct wlr_virtual_keyboard_manager_v1 *virtual_keyboard_mgr;
@@ -331,6 +335,8 @@ static struct wl_listener cursor_button = {.notify = buttonpress};
 static struct wl_listener cursor_frame = {.notify = cursorframe};
 static struct wl_listener cursor_motion = {.notify = motionrelative};
 static struct wl_listener cursor_motion_absolute = {.notify = motionabsolute};
+static struct wl_listener idle_inhibitor_create = {.notify = createidleinhibitor};
+static struct wl_listener idle_inhibitor_destroy = {.notify = destroyidleinhibitor};
 static struct wl_listener layout_change = {.notify = updatemons};
 static struct wl_listener new_input = {.notify = inputdevice};
 static struct wl_listener new_virtual_keyboard = {.notify = virtualkeyboard};
@@ -769,6 +775,15 @@ commitnotify(struct wl_listener *listener, void *data)
 		c->resize = 0;
 }
 
+void
+createidleinhibitor(struct wl_listener *listener, void *data)
+{
+	struct wlr_idle_inhibitor_v1 *idle_inhibitor = data;
+	wl_signal_add(&idle_inhibitor->events.destroy, &idle_inhibitor_destroy);
+
+	wlr_idle_set_enabled(idle, seat, 0);
+}
+
 void
 createkeyboard(struct wlr_input_device *device)
 {
@@ -988,6 +1003,14 @@ cursorframe(struct wl_listener *listener, void *data)
 	wlr_seat_pointer_notify_frame(seat);
 }
 
+void
+destroyidleinhibitor(struct wl_listener *listener, void *data)
+{
+	/* I've been testing and at this point the inhibitor has not yet been
+	 * removed from the list, checking if it has at least one item. */
+	wlr_idle_set_enabled(idle, seat, wl_list_length(&idle_inhibit_mgr->inhibitors) <= 1);
+}
+
 void
 destroylayersurfacenotify(struct wl_listener *listener, void *data)
 {
@@ -1147,6 +1170,7 @@ focusclient(Client *c, int lift)
 	}
 
 	printstatus();
+	wlr_idle_set_enabled(idle, seat, wl_list_empty(&idle_inhibit_mgr->inhibitors));
 
 	if (!c) {
 		/* With no client, all we have left is to clear focus */
@@ -1963,6 +1987,9 @@ setup(void)
 
 	idle = wlr_idle_create(dpy);
 
+	idle_inhibit_mgr = wlr_idle_inhibit_v1_create(dpy);
+	wl_signal_add(&idle_inhibit_mgr->events.new_inhibitor, &idle_inhibitor_create);
+
 	layer_shell = wlr_layer_shell_v1_create(dpy);
 	wl_signal_add(&layer_shell->events.new_surface, &new_layer_shell_surface);
 
-- 
cgit v1.2.3