|
| 1 | +// Copyright (C) 2024 Rob Caelers <[email protected]> |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +// |
| 17 | + |
| 18 | +#ifdef HAVE_CONFIG_H |
| 19 | +# include "config.h" |
| 20 | +#endif |
| 21 | + |
| 22 | +#include "QtWaylandInputMonitor.hh" |
| 23 | + |
| 24 | +#include <memory> |
| 25 | +#include <wayland-client-protocol.h> |
| 26 | +#include <QGuiApplication> |
| 27 | + |
| 28 | +#include "wayland-ext-idle-notify-v1-client-protocol.h" |
| 29 | +#include "debug.hh" |
| 30 | + |
| 31 | +static const struct wl_registry_listener registry_listener = { |
| 32 | + .global = QtWaylandInputMonitor::registry_global, |
| 33 | + .global_remove = QtWaylandInputMonitor::registry_global_remove, |
| 34 | +}; |
| 35 | + |
| 36 | +static const struct ext_idle_notification_v1_listener idle_notification_listener = { |
| 37 | + .idled = QtWaylandInputMonitor::notification_idled, |
| 38 | + .resumed = QtWaylandInputMonitor::notification_resumed, |
| 39 | +}; |
| 40 | + |
| 41 | +QtWaylandInputMonitor::~QtWaylandInputMonitor() |
| 42 | +{ |
| 43 | + TRACE_ENTRY(); |
| 44 | + if (monitor_thread) |
| 45 | + { |
| 46 | + monitor_thread->join(); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +bool |
| 51 | +QtWaylandInputMonitor::init() |
| 52 | +{ |
| 53 | + TRACE_ENTRY(); |
| 54 | + |
| 55 | + auto *app = qGuiApp->nativeInterface<QNativeInterface::QWaylandApplication>(); |
| 56 | + auto *wl_display = app->display(); |
| 57 | + auto *wl_seat = app->seat(); |
| 58 | + |
| 59 | + wl_registry = wl_display_get_registry(wl_display); |
| 60 | + |
| 61 | + wl_registry_add_listener(wl_registry, ®istry_listener, this); |
| 62 | + wl_display_roundtrip(wl_display); |
| 63 | + |
| 64 | + if (wl_notifier == nullptr) |
| 65 | + { |
| 66 | + TRACE_MSG("ext-idle-notify-v1 protocol unsupported"); |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + auto *wl_notification = ext_idle_notifier_v1_get_idle_notification(wl_notifier, timeout, wl_seat); |
| 71 | + |
| 72 | + ext_idle_notification_v1_add_listener(wl_notification, &idle_notification_listener, this); |
| 73 | + monitor_thread = std::make_shared<std::thread>([this] { run(); }); |
| 74 | + |
| 75 | + TRACE_MSG("ext-idle-notify-v1 protocol supported"); |
| 76 | + return true; |
| 77 | +} |
| 78 | + |
| 79 | +void |
| 80 | +QtWaylandInputMonitor::terminate() |
| 81 | +{ |
| 82 | + TRACE_ENTRY(); |
| 83 | + mutex.lock(); |
| 84 | + abort = true; |
| 85 | + cond.notify_all(); |
| 86 | + mutex.unlock(); |
| 87 | + |
| 88 | + if (monitor_thread) |
| 89 | + { |
| 90 | + monitor_thread->join(); |
| 91 | + } |
| 92 | + |
| 93 | + if (wl_notifier != nullptr) |
| 94 | + { |
| 95 | + ext_idle_notifier_v1_destroy(wl_notifier); |
| 96 | + } |
| 97 | + wl_registry_destroy(wl_registry); |
| 98 | +} |
| 99 | + |
| 100 | +void |
| 101 | +QtWaylandInputMonitor::registry_global(void *data, |
| 102 | + struct wl_registry *registry, |
| 103 | + uint32_t id, |
| 104 | + const char *interface, |
| 105 | + uint32_t version) |
| 106 | +{ |
| 107 | + TRACE_ENTRY(); |
| 108 | + auto *self = static_cast<QtWaylandInputMonitor *>(data); |
| 109 | + if (strcmp(ext_idle_notifier_v1_interface.name, interface) == 0) |
| 110 | + { |
| 111 | + if (self->wl_notifier != nullptr) |
| 112 | + { |
| 113 | + ext_idle_notifier_v1_destroy(self->wl_notifier); |
| 114 | + } |
| 115 | + |
| 116 | + self->wl_notifier = static_cast<ext_idle_notifier_v1 *>( |
| 117 | + wl_registry_bind(self->wl_registry, |
| 118 | + id, |
| 119 | + &ext_idle_notifier_v1_interface, |
| 120 | + std::min((uint32_t)ext_idle_notifier_v1_interface.version, version))); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +void |
| 125 | +QtWaylandInputMonitor::registry_global_remove(void *data, struct wl_registry *registry, uint32_t id) |
| 126 | +{ |
| 127 | +} |
| 128 | + |
| 129 | +void |
| 130 | +QtWaylandInputMonitor::notification_idled(void *data, struct ext_idle_notification_v1 *notification) |
| 131 | +{ |
| 132 | + auto *self = static_cast<QtWaylandInputMonitor *>(data); |
| 133 | + self->idle = true; |
| 134 | +} |
| 135 | + |
| 136 | +void |
| 137 | +QtWaylandInputMonitor::notification_resumed(void *data, struct ext_idle_notification_v1 *notification) |
| 138 | +{ |
| 139 | + auto *self = static_cast<QtWaylandInputMonitor *>(data); |
| 140 | + self->idle = false; |
| 141 | +} |
| 142 | + |
| 143 | +void |
| 144 | +QtWaylandInputMonitor::run() |
| 145 | +{ |
| 146 | + TRACE_ENTRY(); |
| 147 | + { |
| 148 | + std::unique_lock lock(mutex); |
| 149 | + while (!abort) |
| 150 | + { |
| 151 | + if (!idle) |
| 152 | + { |
| 153 | + fire_action(); |
| 154 | + } |
| 155 | + |
| 156 | + cond.wait_for(lock, std::chrono::milliseconds(timeout)); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments