1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
*
* Copyright (C) 2008 Red Hat, Inc.
* Copyright (C) 2012-2021 MATE Developers
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Authors: William Jon McCann <mccann@jhu.edu>
*
*/
#include "config.h"
#include <time.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/extensions/sync.h>
#ifdef HAVE_XTEST
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
#endif /* HAVE_XTEST */
#include <glib.h>
#include <gdk/gdkx.h>
#include <gdk/gdk.h>
#include "gs-idle-monitor.h"
static void gs_idle_monitor_finalize (GObject *object);
struct _GSIdleMonitor {
GObject parent;
GHashTable *watches;
int sync_event_base;
XSyncCounter counter;
/* For use with XTest */
int *keycode;
int keycode1;
int keycode2;
gboolean have_xtest;
};
typedef struct
{
guint id;
XSyncValue interval;
GSIdleMonitorWatchFunc callback;
gpointer user_data;
XSyncAlarm xalarm_positive;
XSyncAlarm xalarm_negative;
} GSIdleMonitorWatch;
static guint32 watch_serial = 1;
G_DEFINE_TYPE (GSIdleMonitor, gs_idle_monitor, G_TYPE_OBJECT)
static gint64
_xsyncvalue_to_int64 (XSyncValue value)
{
return ((guint64) XSyncValueHigh32 (value)) << 32
| (guint64) XSyncValueLow32 (value);
}
static XSyncValue
_int64_to_xsyncvalue (gint64 value)
{
XSyncValue ret;
XSyncIntsToValue (&ret, value, ((guint64)value) >> 32);
return ret;
}
static void
gs_idle_monitor_dispose (GObject *object)
{
GSIdleMonitor *monitor;
g_return_if_fail (GS_IS_IDLE_MONITOR (object));
monitor = GS_IDLE_MONITOR (object);
if (monitor->watches != NULL) {
g_hash_table_destroy (monitor->watches);
monitor->watches = NULL;
}
G_OBJECT_CLASS (gs_idle_monitor_parent_class)->dispose (object);
}
static gboolean
_find_alarm (gpointer key,
GSIdleMonitorWatch *watch,
XSyncAlarm *alarm)
{
g_debug ("Searching for %d in %d,%d", (int)*alarm, (int)watch->xalarm_positive, (int)watch->xalarm_negative);
if (watch->xalarm_positive == *alarm
|| watch->xalarm_negative == *alarm) {
return TRUE;
}
return FALSE;
}
static GSIdleMonitorWatch *
find_watch_for_alarm (GSIdleMonitor *monitor,
XSyncAlarm alarm)
{
GSIdleMonitorWatch *watch;
watch = g_hash_table_find (monitor->watches,
(GHRFunc)_find_alarm,
&alarm);
return watch;
}
#ifdef HAVE_XTEST
static gboolean
send_fake_event (GSIdleMonitor *monitor)
{
if (! monitor->have_xtest) {
return FALSE;
}
g_debug ("GSIdleMonitor: sending fake key");
XLockDisplay (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()));
XTestFakeKeyEvent (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
*monitor->keycode,
True,
CurrentTime);
XTestFakeKeyEvent (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
*monitor->keycode,
False,
CurrentTime);
XUnlockDisplay (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()));
/* Swap the keycode */
if (monitor->keycode == &monitor->keycode1) {
monitor->keycode = &monitor->keycode2;
} else {
monitor->keycode = &monitor->keycode1;
}
return TRUE;
}
#endif /* HAVE_XTEST */
void
gs_idle_monitor_reset (GSIdleMonitor *monitor)
{
g_return_if_fail (GS_IS_IDLE_MONITOR (monitor));
#ifdef HAVE_XTEST
/* FIXME: is there a better way to reset the IDLETIME? */
send_fake_event (monitor);
#endif
}
static void
handle_alarm_notify_event (GSIdleMonitor *monitor,
XSyncAlarmNotifyEvent *alarm_event)
{
GSIdleMonitorWatch *watch;
gboolean res;
gboolean condition;
if (alarm_event->state == XSyncAlarmDestroyed) {
return;
}
watch = find_watch_for_alarm (monitor, alarm_event->alarm);
if (watch == NULL) {
g_debug ("Unable to find watch for alarm %d", (int)alarm_event->alarm);
return;
}
g_debug ("Watch %d fired, idle time = %" G_GINT64_FORMAT,
watch->id,
_xsyncvalue_to_int64 (alarm_event->counter_value));
if (alarm_event->alarm == watch->xalarm_positive) {
condition = TRUE;
} else {
condition = FALSE;
}
res = TRUE;
if (watch->callback != NULL) {
res = watch->callback (monitor,
watch->id,
condition,
watch->user_data);
}
if (! res) {
/* reset all timers */
g_debug ("GSIdleMonitor: callback returned FALSE; resetting idle time");
gs_idle_monitor_reset (monitor);
}
}
static GdkFilterReturn
xevent_filter (GdkXEvent *xevent,
GdkEvent *event,
GSIdleMonitor *monitor)
{
XEvent *ev;
XSyncAlarmNotifyEvent *alarm_event;
ev = xevent;
if (ev->xany.type != monitor->sync_event_base + XSyncAlarmNotify) {
return GDK_FILTER_CONTINUE;
}
alarm_event = xevent;
handle_alarm_notify_event (monitor, alarm_event);
return GDK_FILTER_CONTINUE;
}
static gboolean
init_xsync (GSIdleMonitor *monitor)
{
int sync_error_base;
int res;
int major;
int minor;
int i;
int ncounters;
XSyncSystemCounter *counters;
res = XSyncQueryExtension (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
&monitor->sync_event_base,
&sync_error_base);
if (! res) {
g_warning ("GSIdleMonitor: Sync extension not present");
return FALSE;
}
res = XSyncInitialize (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), &major, &minor);
if (! res) {
g_warning ("GSIdleMonitor: Unable to initialize Sync extension");
return FALSE;
}
counters = XSyncListSystemCounters (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), &ncounters);
for (i = 0; i < ncounters; i++) {
if (counters[i].name != NULL
&& strcmp (counters[i].name, "IDLETIME") == 0) {
monitor->counter = counters[i].counter;
break;
}
}
XSyncFreeSystemCounterList (counters);
if (monitor->counter == None) {
g_warning ("GSIdleMonitor: IDLETIME counter not found");
return FALSE;
}
gdk_window_add_filter (NULL, (GdkFilterFunc)xevent_filter, monitor);
return TRUE;
}
static void
_init_xtest (GSIdleMonitor *monitor)
{
#ifdef HAVE_XTEST
int a, b, c, d;
XLockDisplay (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()));
monitor->have_xtest = (XTestQueryExtension (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), &a, &b, &c, &d) == True);
if (monitor->have_xtest) {
monitor->keycode1 = XKeysymToKeycode (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), XK_Alt_L);
if (monitor->keycode1 == 0) {
g_warning ("keycode1 not existent");
}
monitor->keycode2 = XKeysymToKeycode (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), XK_Alt_R);
if (monitor->keycode2 == 0) {
monitor->keycode2 = XKeysymToKeycode (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), XK_Alt_L);
if (monitor->keycode2 == 0) {
g_warning ("keycode2 not existent");
}
}
monitor->keycode = &monitor->keycode1;
}
XUnlockDisplay (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()));
#endif /* HAVE_XTEST */
}
static GObject *
gs_idle_monitor_constructor (GType type,
guint n_construct_properties,
GObjectConstructParam *construct_properties)
{
GSIdleMonitor *monitor;
monitor = GS_IDLE_MONITOR (G_OBJECT_CLASS (gs_idle_monitor_parent_class)->constructor (type,
n_construct_properties,
construct_properties));
_init_xtest (monitor);
if (! init_xsync (monitor)) {
g_object_unref (monitor);
return NULL;
}
return G_OBJECT (monitor);
}
static void
gs_idle_monitor_class_init (GSIdleMonitorClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gs_idle_monitor_finalize;
object_class->dispose = gs_idle_monitor_dispose;
object_class->constructor = gs_idle_monitor_constructor;
}
static guint32
get_next_watch_serial (void)
{
guint32 serial;
serial = watch_serial++;
if ((gint32)watch_serial < 0) {
watch_serial = 1;
}
/* FIXME: make sure it isn't in the hash */
return serial;
}
static GSIdleMonitorWatch *
idle_monitor_watch_new (guint interval)
{
GSIdleMonitorWatch *watch;
watch = g_slice_new0 (GSIdleMonitorWatch);
watch->interval = _int64_to_xsyncvalue ((gint64)interval);
watch->id = get_next_watch_serial ();
watch->xalarm_positive = None;
watch->xalarm_negative = None;
return watch;
}
static void
idle_monitor_watch_free (GSIdleMonitorWatch *watch)
{
if (watch == NULL) {
return;
}
if (watch->xalarm_positive != None) {
XSyncDestroyAlarm (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), watch->xalarm_positive);
}
if (watch->xalarm_negative != None) {
XSyncDestroyAlarm (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), watch->xalarm_negative);
}
g_slice_free (GSIdleMonitorWatch, watch);
}
static void
gs_idle_monitor_init (GSIdleMonitor *monitor)
{
monitor->watches = g_hash_table_new_full (NULL,
NULL,
NULL,
(GDestroyNotify)idle_monitor_watch_free);
monitor->counter = None;
}
static void
gs_idle_monitor_finalize (GObject *object)
{
GSIdleMonitor *idle_monitor;
g_return_if_fail (object != NULL);
g_return_if_fail (GS_IS_IDLE_MONITOR (object));
idle_monitor = GS_IDLE_MONITOR (object);
g_return_if_fail (idle_monitor != NULL);
G_OBJECT_CLASS (gs_idle_monitor_parent_class)->finalize (object);
}
GSIdleMonitor *
gs_idle_monitor_new (void)
{
GObject *idle_monitor;
idle_monitor = g_object_new (GS_TYPE_IDLE_MONITOR,
NULL);
return GS_IDLE_MONITOR (idle_monitor);
}
static gboolean
_xsync_alarm_set (GSIdleMonitor *monitor,
GSIdleMonitorWatch *watch)
{
XSyncAlarmAttributes attr;
XSyncValue delta;
guint flags;
flags = XSyncCACounter
| XSyncCAValueType
| XSyncCATestType
| XSyncCAValue
| XSyncCADelta
| XSyncCAEvents;
XSyncIntToValue (&delta, 0);
attr.trigger.counter = monitor->counter;
attr.trigger.value_type = XSyncAbsolute;
attr.trigger.wait_value = watch->interval;
attr.delta = delta;
attr.events = TRUE;
attr.trigger.wait_value = _int64_to_xsyncvalue (_xsyncvalue_to_int64 (watch->interval) - 1);
attr.trigger.test_type = XSyncPositiveTransition;
if (watch->xalarm_positive != None) {
g_debug ("GSIdleMonitor: updating alarm for positive transition wait=%" G_GINT64_FORMAT,
_xsyncvalue_to_int64 (attr.trigger.wait_value));
XSyncChangeAlarm (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), watch->xalarm_positive, flags, &attr);
} else {
g_debug ("GSIdleMonitor: creating new alarm for positive transition wait=%" G_GINT64_FORMAT,
_xsyncvalue_to_int64 (attr.trigger.wait_value));
watch->xalarm_positive = XSyncCreateAlarm (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), flags, &attr);
}
attr.trigger.test_type = XSyncNegativeTransition;
if (watch->xalarm_negative != None) {
g_debug ("GSIdleMonitor: updating alarm for negative transition wait=%" G_GINT64_FORMAT,
_xsyncvalue_to_int64 (attr.trigger.wait_value));
XSyncChangeAlarm (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), watch->xalarm_negative, flags, &attr);
} else {
g_debug ("GSIdleMonitor: creating new alarm for negative transition wait=%" G_GINT64_FORMAT,
_xsyncvalue_to_int64 (attr.trigger.wait_value));
watch->xalarm_negative = XSyncCreateAlarm (GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), flags, &attr);
}
return TRUE;
}
guint
gs_idle_monitor_add_watch (GSIdleMonitor *monitor,
guint interval,
GSIdleMonitorWatchFunc callback,
gpointer user_data)
{
GSIdleMonitorWatch *watch;
g_return_val_if_fail (GS_IS_IDLE_MONITOR (monitor), 0);
g_return_val_if_fail (callback != NULL, 0);
watch = idle_monitor_watch_new (interval);
watch->callback = callback;
watch->user_data = user_data;
_xsync_alarm_set (monitor, watch);
g_hash_table_insert (monitor->watches,
GUINT_TO_POINTER (watch->id),
watch);
return watch->id;
}
void
gs_idle_monitor_remove_watch (GSIdleMonitor *monitor,
guint id)
{
g_return_if_fail (GS_IS_IDLE_MONITOR (monitor));
g_hash_table_remove (monitor->watches,
GUINT_TO_POINTER (id));
}
|