summaryrefslogtreecommitdiff
path: root/src/ck-inhibit-manager.c
blob: 20316437989cb3b9baf5223a876dae4a94efaf41 (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
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2014 Eric Koegel <eric.koegel@gmail.com>
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>

#include <glib.h>
#include <glib-object.h>
#include <glib-unix.h>
#include <glib/gstdio.h>

#include "ck-inhibit.h"
#include "ck-inhibit-manager.h"
#include "ck-marshal.h"


#define CK_INHIBIT_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CK_TYPE_INHIBIT_MANAGER, CkInhibitManagerPrivate))

struct CkInhibitManagerPrivate
{
         /* We could use something more complicated but
         * it's doubtful there will be more than a dozen items in the list.
         */
        GList *inhibit_list;
        /* inhibitors is an 2-dimensional array of which events to suppress
         * and how they are suppressed.
         * The CkInhibitEvent value is used to indicate how many different
         * inhibits are suppressing that event. The CkInhibitMode indicates
         * if it's a block or delay event */
        gint inhibitors[CK_INHIBIT_MODE_LAST][CK_INHIBIT_EVENT_LAST];
};

typedef enum {
        SIG_CHANGED_EVENT,
        SIG_N_SIGNALS,
} INHIBIT_SIGNALS;

static guint __signals[SIG_N_SIGNALS] = { 0, };


static void     ck_inhibit_manager_class_init  (CkInhibitManagerClass *klass);
static void     ck_inhibit_manager_init        (CkInhibitManager      *manager);
static void     ck_inhibit_manager_finalize    (GObject               *object);

G_DEFINE_TYPE (CkInhibitManager, ck_inhibit_manager, G_TYPE_OBJECT)


static void
ck_inhibit_manager_class_init (CkInhibitManagerClass *klass)
{
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);

        object_class->finalize = ck_inhibit_manager_finalize;

        g_type_class_add_private (klass, sizeof (CkInhibitManagerPrivate));

        __signals[SIG_CHANGED_EVENT] = g_signal_new("changed-event",
                                                    G_OBJECT_CLASS_TYPE (object_class),
                                                    G_SIGNAL_RUN_LAST,
                                                    G_STRUCT_OFFSET (CkInhibitManagerClass, changed_event),
                                                    NULL, NULL,
                                                    ck_marshal_VOID__INT_INT_BOOLEAN,
                                                    G_TYPE_NONE,
                                                    3, G_TYPE_INT, G_TYPE_INT, G_TYPE_BOOLEAN);
}

static void
ck_inhibit_manager_init (CkInhibitManager *manager)
{
        manager->priv = CK_INHIBIT_MANAGER_GET_PRIVATE (manager);
}

static void
ck_inhibit_manager_finalize (GObject *object)
{
        G_OBJECT_CLASS (ck_inhibit_manager_parent_class)->finalize (object);
}

static void
cb_changed_event (CkInhibit *inhibit,
                  gint       inhibit_mode,
                  gint       event,
                  gboolean   enabled,
                  gpointer   user_data)
{
        CkInhibitManager        *manager;
        CkInhibitManagerPrivate *priv;

        g_return_if_fail (CK_IS_INHIBIT_MANAGER (user_data));

        manager = CK_INHIBIT_MANAGER (user_data);

        priv = CK_INHIBIT_MANAGER_GET_PRIVATE (manager);

        if (event < 0 || event >= CK_INHIBIT_EVENT_LAST) {
                g_warning ("invalid event id");
                return;
        }

        if (enabled) {
                priv->inhibitors[inhibit_mode][event]++;

                if (priv->inhibitors[inhibit_mode][event] == 1) {
                        /* event is now inhibited, send a notification */
                        g_signal_emit(G_OBJECT (manager),
                                      __signals[SIG_CHANGED_EVENT],
                                      0,
                                      inhibit_mode,
                                      event,
                                      TRUE);
                }
        } else {
                priv->inhibitors[inhibit_mode][event]--;
                if (priv->inhibitors[inhibit_mode][event] < 0) {
                        g_warning ("cb_changed_event: priv->inhibitors[%d][%d] "
                                   "is negative, that's not supposed to happen",
                                   inhibit_mode, event);
                }

                if (priv->inhibitors[inhibit_mode][event] == 0) {
                        /* event is no longer inhibited, send a notification */
                        g_signal_emit(G_OBJECT (manager),
                                      __signals[SIG_CHANGED_EVENT],
                                      0,
                                      inhibit_mode,
                                      event,
                                      FALSE);
                }
        }
}

/**
 * ck_inhibit_manager_create_lock:
 * @manager: The @CkInhibitManager object
 * @who:  A human-readable, descriptive string of who is taking
 *        the lock. Example: "Xfburn"
 * @what: What is a colon-separated list of lock types.
 *        The list of lock types are: shutdown, sleep, idle,
 *        handle-power-key, handle-suspend-key, handle-hibernate-key.
 *        Example: "shutdown:idle"
 * @why:  A human-readable, descriptive string of why the program
 *        is taking the lock. Example: "Burning a DVD, interrupting now
 *        will ruin the DVD."
 * @mode: Must either be block or delay. block prevents the operation
 *        from happening and will cause a call to perform that action
 *        to fail. delay temporarly prevents the operation from happening
 *        until either the lock is released or a timeout is reached.
 *
 * Initializes an inhibit lock with the supplied paramters and returns
 * the named pipe. An application can only hold one lock at a time, multiple
 * calls will fail.
 *
 * Return value: The named pipe (a file descriptor) on success.
 *               This is a value of 0 or greater.
 *               Returns a CkInhbitError on failure.
 **/
gint
ck_inhibit_manager_create_lock (CkInhibitManager *manager,
                                const gchar *who,
                                const gchar *what,
                                const gchar *why,
                                const gchar *mode)
{
        CkInhibitManagerPrivate *priv;
        CkInhibit               *inhibit;
        gint                     fd, signal_id;

        g_return_val_if_fail (CK_IS_INHIBIT_MANAGER (manager), CK_INHIBIT_ERROR_GENERAL);

        priv = CK_INHIBIT_MANAGER_GET_PRIVATE (manager);

        inhibit = ck_inhibit_new ();

        if (inhibit == NULL) {
                g_error ("error creating new inhibit object");
                return CK_INHIBIT_ERROR_OOM;
        }

        /* add our signal handler before we create the lock so we get
         * the inhibit enable signals.
         */
        signal_id = g_signal_connect (inhibit, "changed-event", G_CALLBACK (cb_changed_event), manager);

        fd = ck_inhibit_create_lock (inhibit, who, what, why, mode);

        if (fd == -1) {
                g_error ("error creating inhibit lock");
                /* ensure we disconnect the signal handler and
                 * unref the inhibit object we won't be using */
                g_signal_handler_disconnect (inhibit, signal_id);
                g_object_unref (inhibit);
                return CK_INHIBIT_ERROR_GENERAL;
        }

        /* Add it to our list */
        priv->inhibit_list = g_list_append (priv->inhibit_list,
                                            inhibit);

        return fd;
}

/**
 * ck_inhibit_manager_remove_lock:
 * @manager: The @CkInhibitManager object
 * @who:  A human-readable, descriptive string of who has taken
 *        the lock. Example: "Xfburn"
 *
 * Finds the inhibit lock @who and removes it.
 *
 * Return value: TRUE on successful removal.
 **/
gboolean
ck_inhibit_manager_remove_lock (CkInhibitManager *manager,
                                const gchar      *who)
{
        CkInhibitManagerPrivate *priv;
        GList                   *l;

        g_return_val_if_fail (CK_IS_INHIBIT_MANAGER (manager), FALSE);

        priv = CK_INHIBIT_MANAGER_GET_PRIVATE (manager);

        for (l = g_list_first (priv->inhibit_list); l != NULL; l = g_list_next (priv->inhibit_list)) {
                if (l->data && g_strcmp0 (ck_inhibit_get_who (l->data), who) == 0) {
                        CkInhibit *inhibit = l->data;

                        /* Found it! Remove it from the list and unref the object */
                        priv->inhibit_list = g_list_remove (priv->inhibit_list, inhibit);
                        ck_inhibit_remove_lock (inhibit);
                        g_signal_handlers_disconnect_by_func (inhibit,
                                                              G_CALLBACK (cb_changed_event),
                                                              manager);
                        g_object_unref (inhibit);
                        return TRUE;
                }
        }

        return FALSE;
}

/**
 * ck_inhibit_manager_get:
 *
 * Increases the reference count of the @CkInhibitManager object.
 *
 * Return value:  Returns a reference to the CkInhibitManager object or
 *                NULL on failure.
 **/
CkInhibitManager*
ck_inhibit_manager_get (void)
{
        static GObject *manager = NULL;

        if (manager != NULL) {
                g_object_ref (manager);
        } else {
                manager = g_object_new (CK_TYPE_INHIBIT_MANAGER, NULL);

                g_object_add_weak_pointer (manager,
                                           (gpointer *) &manager);
        }

        return CK_INHIBIT_MANAGER (manager);
}

static gboolean
get_inhibit_status (CkInhibitManager *manager, CkInhibitEvent event, CkInhibitMode mode)
{
        g_return_val_if_fail (CK_IS_INHIBIT_MANAGER (manager), FALSE);

        return manager->priv->inhibitors[mode][event] > 0 ? TRUE : FALSE;
}

/**
 * ck_inhibit_manager_is_shutdown_delayed:
 * @manager: The @CkInhibitManager object
 *
 * Return value: TRUE is delayed.
 **/
gboolean
ck_inhibit_manager_is_shutdown_delayed (CkInhibitManager *manager)
{
        return get_inhibit_status (manager, CK_INHIBIT_EVENT_SHUTDOWN, CK_INHIBIT_MODE_DELAY);
}

/**
 * ck_inhibit_manager_is_shutdown_blocked:
 * @manager: The @CkInhibitManager object
 *
 * Return value: TRUE is blocked.
 **/
gboolean
ck_inhibit_manager_is_shutdown_blocked (CkInhibitManager *manager)
{
        return get_inhibit_status (manager, CK_INHIBIT_EVENT_SHUTDOWN, CK_INHIBIT_MODE_BLOCK);
}

/**
 * ck_inhibit_manager_is_suspend_delayed:
 * @manager: The @CkInhibitManager object
 *
 * Return value: TRUE is delayed.
 **/
gboolean
ck_inhibit_manager_is_suspend_delayed (CkInhibitManager *manager)
{
        return get_inhibit_status (manager, CK_INHIBIT_EVENT_SUSPEND, CK_INHIBIT_MODE_DELAY);
}

/**
 * ck_inhibit_manager_is_suspend_blocked:
 * @manager: The @CkInhibitManager object
 *
 * Return value: TRUE is blocked.
 **/
gboolean
ck_inhibit_manager_is_suspend_blocked (CkInhibitManager *manager)
{
        return get_inhibit_status (manager, CK_INHIBIT_EVENT_SUSPEND, CK_INHIBIT_MODE_BLOCK);
}

/**
 * ck_inhibit_manager_is_idle_delayed:
 * @manager: The @CkInhibitManager object
 *
 * Return value: TRUE is delayed.
 **/
gboolean
ck_inhibit_manager_is_idle_delayed (CkInhibitManager *manager)
{
        return get_inhibit_status (manager, CK_INHIBIT_EVENT_IDLE, CK_INHIBIT_MODE_DELAY);
}

/**
 * ck_inhibit_manager_is_idle_blocked:
 * @manager: The @CkInhibitManager object
 *
 * Return value: TRUE is blocked.
 **/
gboolean
ck_inhibit_manager_is_idle_blocked (CkInhibitManager *manager)
{
        return get_inhibit_status (manager, CK_INHIBIT_EVENT_IDLE, CK_INHIBIT_MODE_BLOCK);
}

/**
 * ck_inhibit_manager_is_power_key_delayed:
 * @manager: The @CkInhibitManager object
 *
 * Return value: TRUE is delayed.
 **/
gboolean
ck_inhibit_manager_is_power_key_delayed (CkInhibitManager *manager)
{
        return get_inhibit_status (manager, CK_INHIBIT_EVENT_POWER_KEY, CK_INHIBIT_MODE_DELAY);
}

/**
 * ck_inhibit_manager_is_power_key_blocked:
 * @manager: The @CkInhibitManager object
 *
 * Return value: TRUE is blocked.
 **/
gboolean
ck_inhibit_manager_is_power_key_blocked (CkInhibitManager *manager)
{
        return get_inhibit_status (manager, CK_INHIBIT_EVENT_POWER_KEY, CK_INHIBIT_MODE_BLOCK);
}

/**
 * ck_inhibit_manager_is_suspend_key_delayed:
 * @manager: The @CkInhibitManager object
 *
 * Return value: TRUE is delayed.
 **/
gboolean
ck_inhibit_manager_is_suspend_key_delayed (CkInhibitManager *manager)
{
        return get_inhibit_status (manager, CK_INHIBIT_EVENT_SUSPEND_KEY, CK_INHIBIT_MODE_DELAY);
}

/**
 * ck_inhibit_manager_is_suspend_key_blocked:
 * @manager: The @CkInhibitManager object
 *
 * Return value: TRUE is blocked.
 **/
gboolean
ck_inhibit_manager_is_suspend_key_blocked (CkInhibitManager *manager)
{
        return get_inhibit_status (manager, CK_INHIBIT_EVENT_SUSPEND_KEY, CK_INHIBIT_MODE_BLOCK);
}

/**
 * ck_inhibit_manager_is_hibernate_key_delayed:
 * @manager: The @CkInhibitManager object
 *
 * Return value: TRUE is delayed.
 **/
gboolean
ck_inhibit_manager_is_hibernate_key_delayed (CkInhibitManager *manager)
{
        return get_inhibit_status (manager, CK_INHIBIT_EVENT_HIBERNATE_KEY, CK_INHIBIT_MODE_DELAY);
}

/**
 * ck_inhibit_manager_is_hibernate_key_blocked:
 * @manager: The @CkInhibitManager object
 *
 * Return value: TRUE is blocked.
 **/
gboolean
ck_inhibit_manager_is_hibernate_key_blocked (CkInhibitManager *manager)
{
        return get_inhibit_status (manager, CK_INHIBIT_EVENT_HIBERNATE_KEY, CK_INHIBIT_MODE_BLOCK);
}

/**
 * ck_inhibit_manager_is_lid_switch_delayed:
 * @manager: The @CkInhibitManager object
 *
 * Return value: TRUE is delayed.
 **/
gboolean
ck_inhibit_manager_is_lid_switch_delayed (CkInhibitManager *manager)
{
        return get_inhibit_status (manager, CK_INHIBIT_EVENT_LID_SWITCH, CK_INHIBIT_MODE_DELAY);
}

/**
 * ck_inhibit_manager_is_lid_switch_blocked:
 * @manager: The @CkInhibitManager object
 *
 * Return value: TRUE is blocked.
 **/
gboolean
ck_inhibit_manager_is_lid_switch_blocked (CkInhibitManager *manager)
{
        return get_inhibit_status (manager, CK_INHIBIT_EVENT_LID_SWITCH, CK_INHIBIT_MODE_BLOCK);
}