summaryrefslogtreecommitdiff
path: root/src/backends/x11/meta-idle-monitor-xsync.c
blob: de25312ad588c91a623e7671a40da14b43aa57e9 (plain)
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
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */

/*
 * Copyright 2013 Red Hat, Inc.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 * Adapted from gnome-session/gnome-session/gs-idle-monitor.c and
 *         from gnome-desktop/libgnome-desktop/gnome-idle-monitor.c
 */

#include "config.h"

#include "meta-idle-monitor-xsync.h"
#include "meta-idle-monitor-private.h"

#include "meta-backend-x11.h"

#include <string.h>

struct _MetaIdleMonitorXSync
{
  MetaIdleMonitor parent;

  GHashTable  *alarms;
  Display     *display;
  XSyncCounter counter;
  XSyncAlarm   user_active_alarm;
};

struct _MetaIdleMonitorXSyncClass
{
  MetaIdleMonitorClass parent_class;
};

typedef struct {
  MetaIdleMonitorWatch base;

  XSyncAlarm xalarm;
} MetaIdleMonitorWatchXSync;

G_DEFINE_TYPE (MetaIdleMonitorXSync, meta_idle_monitor_xsync, META_TYPE_IDLE_MONITOR)

static gint64
_xsyncvalue_to_int64 (XSyncValue value)
{
  return ((guint64) XSyncValueHigh32 (value)) << 32
    | (guint64) XSyncValueLow32 (value);
}

#define GUINT64_TO_XSYNCVALUE(value, ret) XSyncIntsToValue (ret, (value) & 0xFFFFFFFF, ((guint64)(value)) >> 32)

static XSyncAlarm
_xsync_alarm_set (MetaIdleMonitorXSync	*monitor_xsync,
		  XSyncTestType          test_type,
		  guint64                interval,
		  gboolean               want_events)
{
  XSyncAlarmAttributes attr;
  XSyncValue	     delta;
  guint		     flags;

  flags = XSyncCACounter | XSyncCAValueType | XSyncCATestType |
    XSyncCAValue | XSyncCADelta | XSyncCAEvents;

  XSyncIntToValue (&delta, 0);
  attr.trigger.counter = monitor_xsync->counter;
  attr.trigger.value_type = XSyncAbsolute;
  attr.delta = delta;
  attr.events = want_events;

  GUINT64_TO_XSYNCVALUE (interval, &attr.trigger.wait_value);
  attr.trigger.test_type = test_type;
  return XSyncCreateAlarm (monitor_xsync->display, flags, &attr);
}

static void
ensure_alarm_rescheduled (Display    *dpy,
			  XSyncAlarm  alarm)
{
  XSyncAlarmAttributes attr;

  /* Some versions of Xorg have an issue where alarms aren't
   * always rescheduled. Calling XSyncChangeAlarm, even
   * without any attributes, will reschedule the alarm. */
  XSyncChangeAlarm (dpy, alarm, 0, &attr);
}

static void
set_alarm_enabled (Display    *dpy,
		   XSyncAlarm  alarm,
		   gboolean    enabled)
{
  XSyncAlarmAttributes attr;
  attr.events = enabled;
  XSyncChangeAlarm (dpy, alarm, XSyncCAEvents, &attr);
}

static char *
counter_name_for_device (int device_id)
{
  if (device_id > 0)
    return g_strdup_printf ("DEVICEIDLETIME %d", device_id);

  return g_strdup ("IDLETIME");
}

static XSyncCounter
find_idletime_counter (MetaIdleMonitorXSync *monitor_xsync)
{
  MetaIdleMonitor *monitor = META_IDLE_MONITOR (monitor_xsync);
  int		      i;
  int		      ncounters;
  XSyncSystemCounter *counters;
  XSyncCounter        counter = None;
  char               *counter_name;

  counter_name = counter_name_for_device (monitor->device_id);
  counters = XSyncListSystemCounters (monitor_xsync->display, &ncounters);
  for (i = 0; i < ncounters; i++)
    {
      if (counters[i].name != NULL && strcmp (counters[i].name, counter_name) == 0)
        {
          counter = counters[i].counter;
          break;
        }
    }
  XSyncFreeSystemCounterList (counters);
  g_free (counter_name);

  return counter;
}

static void
init_xsync (MetaIdleMonitorXSync *monitor_xsync)
{
  monitor_xsync->counter = find_idletime_counter (monitor_xsync);
  /* IDLETIME counter not found? */
  if (monitor_xsync->counter == None)
    {
      g_warning ("IDLETIME counter not found\n");
      return;
    }

  monitor_xsync->user_active_alarm = _xsync_alarm_set (monitor_xsync, XSyncNegativeTransition, 1, FALSE);
}

static void
meta_idle_monitor_xsync_dispose (GObject *object)
{
  MetaIdleMonitorXSync *monitor_xsync = META_IDLE_MONITOR_XSYNC (object);

  if (monitor_xsync->user_active_alarm != None)
    {
      XSyncDestroyAlarm (monitor_xsync->display, monitor_xsync->user_active_alarm);
      monitor_xsync->user_active_alarm = None;
    }

  g_clear_pointer (&monitor_xsync->alarms, g_hash_table_destroy);

  G_OBJECT_CLASS (meta_idle_monitor_xsync_parent_class)->dispose (object);
}

static void
meta_idle_monitor_xsync_constructed (GObject *object)
{
  MetaIdleMonitorXSync *monitor_xsync = META_IDLE_MONITOR_XSYNC (object);
  MetaBackendX11 *backend = META_BACKEND_X11 (meta_get_backend ());

  monitor_xsync->display = meta_backend_x11_get_xdisplay (backend);
  init_xsync (monitor_xsync);

  G_OBJECT_CLASS (meta_idle_monitor_xsync_parent_class)->constructed (object);
}

static gint64
meta_idle_monitor_xsync_get_idletime (MetaIdleMonitor *monitor)
{
  MetaIdleMonitorXSync *monitor_xsync = META_IDLE_MONITOR_XSYNC (monitor);
  XSyncValue value;

  if (!XSyncQueryCounter (monitor_xsync->display, monitor_xsync->counter, &value))
    return -1;

  return _xsyncvalue_to_int64 (value);
}

static gboolean
fire_watch_idle (gpointer data)
{
  MetaIdleMonitorWatch *watch = data;

  watch->idle_source_id = 0;
  _meta_idle_monitor_watch_fire (watch);

  return FALSE;
}

static guint32
get_next_watch_serial (void)
{
  static guint32 serial = 0;
  g_atomic_int_inc (&serial);
  return serial;
}

static void
free_watch (gpointer data)
{
  MetaIdleMonitorWatchXSync *watch_xsync = data;
  MetaIdleMonitorWatch *watch = (MetaIdleMonitorWatch *) watch_xsync;
  MetaIdleMonitor *monitor = watch->monitor;
  MetaIdleMonitorXSync *monitor_xsync = META_IDLE_MONITOR_XSYNC (monitor);

  g_object_ref (monitor);

  if (watch->idle_source_id)
    {
      g_source_remove (watch->idle_source_id);
      watch->idle_source_id = 0;
    }

  if (watch->notify != NULL)
    watch->notify (watch->user_data);

  if (watch_xsync->xalarm != monitor_xsync->user_active_alarm &&
      watch_xsync->xalarm != None)
    {
      XSyncDestroyAlarm (monitor_xsync->display, watch_xsync->xalarm);
      g_hash_table_remove (monitor_xsync->alarms, (gpointer) watch_xsync->xalarm);
    }

  g_object_unref (monitor);
  g_slice_free (MetaIdleMonitorWatchXSync, watch_xsync);
}

static MetaIdleMonitorWatch *
meta_idle_monitor_xsync_make_watch (MetaIdleMonitor           *monitor,
                                    guint64                    timeout_msec,
                                    MetaIdleMonitorWatchFunc   callback,
                                    gpointer                   user_data,
                                    GDestroyNotify             notify)
{
  MetaIdleMonitorXSync *monitor_xsync = META_IDLE_MONITOR_XSYNC (monitor);
  MetaIdleMonitorWatchXSync *watch_xsync;
  MetaIdleMonitorWatch *watch;

  watch_xsync = g_slice_new0 (MetaIdleMonitorWatchXSync);
  watch = (MetaIdleMonitorWatch *) watch_xsync;

  watch->monitor = monitor;
  watch->id = get_next_watch_serial ();
  watch->callback = callback;
  watch->user_data = user_data;
  watch->notify = notify;
  watch->timeout_msec = timeout_msec;

  if (monitor_xsync->user_active_alarm != None)
    {
      if (timeout_msec != 0)
        {
          watch_xsync->xalarm = _xsync_alarm_set (monitor_xsync, XSyncPositiveTransition, timeout_msec, TRUE);

          g_hash_table_add (monitor_xsync->alarms, (gpointer) watch_xsync->xalarm);

          if (meta_idle_monitor_get_idletime (monitor) > (gint64)timeout_msec)
            {
              watch->idle_source_id = g_idle_add (fire_watch_idle, watch);
              g_source_set_name_by_id (watch->idle_source_id, "[mutter] fire_watch_idle");
            }
        }
      else
        {
          watch_xsync->xalarm = monitor_xsync->user_active_alarm;

          set_alarm_enabled (monitor_xsync->display, monitor_xsync->user_active_alarm, TRUE);
        }
    }

  return watch;
}

static void
meta_idle_monitor_xsync_class_init (MetaIdleMonitorXSyncClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  MetaIdleMonitorClass *idle_monitor_class = META_IDLE_MONITOR_CLASS (klass);

  object_class->dispose = meta_idle_monitor_xsync_dispose;
  object_class->constructed = meta_idle_monitor_xsync_constructed;

  idle_monitor_class->get_idletime = meta_idle_monitor_xsync_get_idletime;
  idle_monitor_class->make_watch = meta_idle_monitor_xsync_make_watch;
}

static void
meta_idle_monitor_xsync_init (MetaIdleMonitorXSync *monitor_xsync)
{
  MetaIdleMonitor *monitor = META_IDLE_MONITOR (monitor_xsync);

  monitor->watches = g_hash_table_new_full (NULL, NULL, NULL, free_watch);
  monitor_xsync->alarms = g_hash_table_new (NULL, NULL);
}

static void
check_x11_watches (MetaIdleMonitor *monitor,
                   XSyncAlarm       alarm)
{
  GList *node, *watch_ids;

  /* we get the keys and do explicit look ups in case
   * an early iteration of the loop ends up leading
   * to watches from later iterations getting invalidated
   */
  watch_ids = g_hash_table_get_keys (monitor->watches);

  for (node = watch_ids; node != NULL; node = node->next)
    {
      guint watch_id = GPOINTER_TO_UINT (node->data);
      MetaIdleMonitorWatchXSync *watch;

      watch = g_hash_table_lookup (monitor->watches, GUINT_TO_POINTER (watch_id));

      if (watch && watch->xalarm == alarm)
        _meta_idle_monitor_watch_fire ((MetaIdleMonitorWatch *) watch);
    }

  g_list_free (watch_ids);
}

void
meta_idle_monitor_xsync_handle_xevent (MetaIdleMonitor       *monitor,
                                       XSyncAlarmNotifyEvent *alarm_event)
{
  MetaIdleMonitorXSync *monitor_xsync = META_IDLE_MONITOR_XSYNC (monitor);
  XSyncAlarm alarm;
  gboolean has_alarm;

  if (alarm_event->state != XSyncAlarmActive)
    return;

  alarm = alarm_event->alarm;

  has_alarm = FALSE;

  if (alarm == monitor_xsync->user_active_alarm)
    {
      set_alarm_enabled (monitor_xsync->display,
                         alarm,
                         FALSE);
      has_alarm = TRUE;
    }
  else if (g_hash_table_contains (monitor_xsync->alarms, (gpointer) alarm))
    {
      ensure_alarm_rescheduled (monitor_xsync->display,
                                alarm);
      has_alarm = TRUE;
    }

  if (has_alarm)
    check_x11_watches (monitor, alarm);
}