~ubuntu-branches/ubuntu/vivid/gnome-flashback/vivid

« back to all changes in this revision

Viewing changes to gnome-flashback/libidle-monitor/meta-idle-monitor.c

  • Committer: Package Import Robot
  • Author(s): Dmitry Shachnev
  • Date: 2014-09-19 17:09:43 UTC
  • Revision ID: package-import@ubuntu.com-20140919170943-oboafsedi6z69951
Tags: upstream-3.10.0
ImportĀ upstreamĀ versionĀ 3.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2013 Red Hat, Inc.
 
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
 * Adapted from gnome-session/gnome-session/gs-idle-monitor.c and
 
18
 *         from gnome-desktop/libgnome-desktop/gnome-idle-monitor.c
 
19
 */
 
20
 
 
21
/**
 
22
 * SECTION:idle-monitor
 
23
 * @title: MetaIdleMonitor
 
24
 * @short_description: Mutter idle counter (similar to X's IDLETIME)
 
25
 */
 
26
 
 
27
#include "config.h"
 
28
 
 
29
#include <string.h>
 
30
 
 
31
#include "meta-idle-monitor.h"
 
32
#include "meta-idle-monitor-xsync.h"
 
33
#include "meta-idle-monitor-dbus.h"
 
34
#include "meta-backend.h"
 
35
 
 
36
G_STATIC_ASSERT(sizeof(unsigned long) == sizeof(gpointer));
 
37
 
 
38
enum
 
39
{
 
40
  PROP_0,
 
41
  PROP_DEVICE_ID,
 
42
  PROP_LAST,
 
43
};
 
44
 
 
45
static GParamSpec *obj_props[PROP_LAST];
 
46
 
 
47
G_DEFINE_TYPE (MetaIdleMonitor, meta_idle_monitor, G_TYPE_OBJECT)
 
48
 
 
49
void
 
50
_meta_idle_monitor_watch_fire (MetaIdleMonitorWatch *watch)
 
51
{
 
52
  MetaIdleMonitor *monitor;
 
53
  guint id;
 
54
  gboolean is_user_active_watch;
 
55
 
 
56
  monitor = watch->monitor;
 
57
  g_object_ref (monitor);
 
58
 
 
59
  if (watch->idle_source_id)
 
60
    {
 
61
      g_source_remove (watch->idle_source_id);
 
62
      watch->idle_source_id = 0;
 
63
    }
 
64
 
 
65
  id = watch->id;
 
66
  is_user_active_watch = (watch->timeout_msec == 0);
 
67
 
 
68
  if (watch->callback)
 
69
    watch->callback (monitor, id, watch->user_data);
 
70
 
 
71
  if (is_user_active_watch)
 
72
    meta_idle_monitor_remove_watch (monitor, id);
 
73
 
 
74
  g_object_unref (monitor);
 
75
}
 
76
 
 
77
static void
 
78
meta_idle_monitor_dispose (GObject *object)
 
79
{
 
80
  MetaIdleMonitor *monitor = META_IDLE_MONITOR (object);
 
81
 
 
82
  g_clear_pointer (&monitor->watches, g_hash_table_destroy);
 
83
 
 
84
  G_OBJECT_CLASS (meta_idle_monitor_parent_class)->dispose (object);
 
85
}
 
86
 
 
87
static void
 
88
meta_idle_monitor_get_property (GObject    *object,
 
89
                                guint       prop_id,
 
90
                                GValue     *value,
 
91
                                GParamSpec *pspec)
 
92
{
 
93
  MetaIdleMonitor *monitor = META_IDLE_MONITOR (object);
 
94
 
 
95
  switch (prop_id)
 
96
    {
 
97
    case PROP_DEVICE_ID:
 
98
      g_value_set_int (value, monitor->device_id);
 
99
      break;
 
100
    default:
 
101
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
102
      break;
 
103
    }
 
104
}
 
105
 
 
106
static void
 
107
meta_idle_monitor_set_property (GObject      *object,
 
108
                                guint         prop_id,
 
109
                                const GValue *value,
 
110
                                GParamSpec   *pspec)
 
111
{
 
112
  MetaIdleMonitor *monitor = META_IDLE_MONITOR (object);
 
113
  switch (prop_id)
 
114
    {
 
115
    case PROP_DEVICE_ID:
 
116
      monitor->device_id = g_value_get_int (value);
 
117
      break;
 
118
    default:
 
119
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
120
      break;
 
121
    }
 
122
}
 
123
 
 
124
static void
 
125
meta_idle_monitor_class_init (MetaIdleMonitorClass *klass)
 
126
{
 
127
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
128
 
 
129
  object_class->dispose = meta_idle_monitor_dispose;
 
130
  object_class->get_property = meta_idle_monitor_get_property;
 
131
  object_class->set_property = meta_idle_monitor_set_property;
 
132
 
 
133
  /**
 
134
   * MetaIdleMonitor:device_id:
 
135
   *
 
136
   * The device to listen to idletime on.
 
137
   */
 
138
  obj_props[PROP_DEVICE_ID] =
 
139
    g_param_spec_int ("device-id",
 
140
                      "Device ID",
 
141
                      "The device to listen to idletime on",
 
142
                      0, 255, 0,
 
143
                      G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
 
144
  g_object_class_install_property (object_class, PROP_DEVICE_ID, obj_props[PROP_DEVICE_ID]);
 
145
}
 
146
 
 
147
static void
 
148
meta_idle_monitor_init (MetaIdleMonitor *monitor)
 
149
{
 
150
}
 
151
 
 
152
static MetaIdleMonitor *device_monitors[256];
 
153
 
 
154
/**
 
155
 * meta_idle_monitor_get_core:
 
156
 *
 
157
 * Returns: (transfer none): the #MetaIdleMonitor that tracks the server-global
 
158
 * idletime for all devices. To track device-specific idletime,
 
159
 * use meta_idle_monitor_get_for_device().
 
160
 */
 
161
MetaIdleMonitor *
 
162
meta_idle_monitor_get_core (void)
 
163
{
 
164
  MetaBackend *backend = meta_get_backend ();
 
165
  return meta_backend_get_idle_monitor (backend, 0);
 
166
}
 
167
 
 
168
/**
 
169
 * meta_idle_monitor_get_for_device:
 
170
 * @device_id: the device to get the idle time for.
 
171
 *
 
172
 * Returns: (transfer none): a new #MetaIdleMonitor that tracks the
 
173
 * device-specific idletime for @device. To track server-global idletime
 
174
 * for all devices, use meta_idle_monitor_get_core().
 
175
 */
 
176
MetaIdleMonitor *
 
177
meta_idle_monitor_get_for_device (gint device_id)
 
178
{
 
179
  MetaBackend *backend = meta_get_backend ();
 
180
  return meta_backend_get_idle_monitor (backend, device_id);
 
181
}
 
182
 
 
183
static MetaIdleMonitorWatch *
 
184
make_watch (MetaIdleMonitor           *monitor,
 
185
            guint64                    timeout_msec,
 
186
            MetaIdleMonitorWatchFunc   callback,
 
187
            gpointer                   user_data,
 
188
            GDestroyNotify             notify)
 
189
{
 
190
  MetaIdleMonitorWatch *watch;
 
191
 
 
192
  watch = META_IDLE_MONITOR_GET_CLASS (monitor)->make_watch (monitor,
 
193
                                                             timeout_msec,
 
194
                                                             callback,
 
195
                                                             user_data,
 
196
                                                             notify);
 
197
 
 
198
  g_hash_table_insert (monitor->watches,
 
199
                       GUINT_TO_POINTER (watch->id),
 
200
                       watch);
 
201
  return watch;
 
202
}
 
203
 
 
204
/**
 
205
 * meta_idle_monitor_add_idle_watch:
 
206
 * @monitor: A #MetaIdleMonitor
 
207
 * @interval_msec: The idletime interval, in milliseconds
 
208
 * @callback: (nullable): The callback to call when the user has
 
209
 *     accumulated @interval_msec milliseconds of idle time.
 
210
 * @user_data: (nullable): The user data to pass to the callback
 
211
 * @notify: A #GDestroyNotify
 
212
 *
 
213
 * Returns: a watch id
 
214
 *
 
215
 * Adds a watch for a specific idle time. The callback will be called
 
216
 * when the user has accumulated @interval_msec milliseconds of idle time.
 
217
 * This function will return an ID that can either be passed to
 
218
 * meta_idle_monitor_remove_watch(), or can be used to tell idle time
 
219
 * watches apart if you have more than one.
 
220
 *
 
221
 * Also note that this function will only care about positive transitions
 
222
 * (user's idle time exceeding a certain time). If you want to know about
 
223
 * when the user has become active, use
 
224
 * meta_idle_monitor_add_user_active_watch().
 
225
 */
 
226
guint
 
227
meta_idle_monitor_add_idle_watch (MetaIdleMonitor              *monitor,
 
228
                                  guint64                       interval_msec,
 
229
                                  MetaIdleMonitorWatchFunc      callback,
 
230
                                  gpointer                      user_data,
 
231
                                  GDestroyNotify                notify)
 
232
{
 
233
  MetaIdleMonitorWatch *watch;
 
234
 
 
235
  g_return_val_if_fail (META_IS_IDLE_MONITOR (monitor), 0);
 
236
  g_return_val_if_fail (interval_msec > 0, 0);
 
237
 
 
238
  watch = make_watch (monitor,
 
239
                      interval_msec,
 
240
                      callback,
 
241
                      user_data,
 
242
                      notify);
 
243
 
 
244
  return watch->id;
 
245
}
 
246
 
 
247
/**
 
248
 * meta_idle_monitor_add_user_active_watch:
 
249
 * @monitor: A #MetaIdleMonitor
 
250
 * @callback: (nullable): The callback to call when the user is
 
251
 *     active again.
 
252
 * @user_data: (nullable): The user data to pass to the callback
 
253
 * @notify: A #GDestroyNotify
 
254
 *
 
255
 * Returns: a watch id
 
256
 *
 
257
 * Add a one-time watch to know when the user is active again.
 
258
 * Note that this watch is one-time and will de-activate after the
 
259
 * function is called, for efficiency purposes. It's most convenient
 
260
 * to call this when an idle watch, as added by
 
261
 * meta_idle_monitor_add_idle_watch(), has triggered.
 
262
 */
 
263
guint
 
264
meta_idle_monitor_add_user_active_watch (MetaIdleMonitor          *monitor,
 
265
                                         MetaIdleMonitorWatchFunc  callback,
 
266
                                         gpointer                  user_data,
 
267
                                         GDestroyNotify            notify)
 
268
{
 
269
  MetaIdleMonitorWatch *watch;
 
270
 
 
271
  g_return_val_if_fail (META_IS_IDLE_MONITOR (monitor), 0);
 
272
 
 
273
  watch = make_watch (monitor,
 
274
                      0,
 
275
                      callback,
 
276
                      user_data,
 
277
                      notify);
 
278
 
 
279
  return watch->id;
 
280
}
 
281
 
 
282
/**
 
283
 * meta_idle_monitor_remove_watch:
 
284
 * @monitor: A #MetaIdleMonitor
 
285
 * @id: A watch ID
 
286
 *
 
287
 * Removes an idle time watcher, previously added by
 
288
 * meta_idle_monitor_add_idle_watch() or
 
289
 * meta_idle_monitor_add_user_active_watch().
 
290
 */
 
291
void
 
292
meta_idle_monitor_remove_watch (MetaIdleMonitor *monitor,
 
293
                                guint            id)
 
294
{
 
295
  g_return_if_fail (META_IS_IDLE_MONITOR (monitor));
 
296
 
 
297
  g_object_ref (monitor);
 
298
  g_hash_table_remove (monitor->watches,
 
299
                       GUINT_TO_POINTER (id));
 
300
  g_object_unref (monitor);
 
301
}
 
302
 
 
303
/**
 
304
 * meta_idle_monitor_get_idletime:
 
305
 * @monitor: A #MetaIdleMonitor
 
306
 *
 
307
 * Returns: The current idle time, in milliseconds, or -1 for not supported
 
308
 */
 
309
gint64
 
310
meta_idle_monitor_get_idletime (MetaIdleMonitor *monitor)
 
311
{
 
312
  return META_IDLE_MONITOR_GET_CLASS (monitor)->get_idletime (monitor);
 
313
}