summaryrefslogtreecommitdiff
path: root/src/sj-inhibit.c
blob: 67582aead4cfdab653b882346091824813a74259 (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
/*
 * Copyright (C) 2007 Carl-Anton Ingmarsson <ca.ingmarsson@gmail.com>
 * Copyright (C) 2006-2007 Richard Hughes <richard@hughsie.com>
 *
 * Sound Juicer - sj-inhibit.c
 *
 * 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.
 *
 * Authors: Carl-Anton Ingmarsson <ca.ingmarsson@gmail.com>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gio/gio.h>

#include "sj-inhibit.h"

/* PowerManagent defines */
#define	PM_DBUS_SERVICE    "org.gnome.SessionManager"
#define	PM_DBUS_INHIBIT_PATH   "/org/gnome/SessionManager"
#define	PM_DBUS_INHIBIT_INTERFACE    "org.gnome.SessionManager"

/** cookie is returned as an unsigned integer */
guint
sj_inhibit (const gchar * appname, const gchar * reason, guint xid)
{
  guint cookie;
  GError *error = NULL;
  GDBusProxy *proxy = NULL;
  GVariant *variant;


  proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
                                         G_DBUS_PROXY_FLAGS_NONE,
                                         NULL,
                                         PM_DBUS_SERVICE,
                                         PM_DBUS_INHIBIT_PATH,
                                         PM_DBUS_INHIBIT_INTERFACE,
                                         NULL,
                                         &error);

  if (!proxy)
  {
    g_warning ("Could not get DBUS proxy: %s", error->message);
    g_clear_error (&error);
    return 0;
  }


  variant = g_dbus_proxy_call_sync (proxy,
                                    "Inhibit",
                                    g_variant_new ("(susu)",
                                                   appname,
                                                   xid,
                                                   reason,
                                                   4+8),
                                    G_DBUS_CALL_FLAGS_NONE, -1, NULL,
                                    &error);
  if (variant)
    {
      g_variant_get (variant, "(u)", &cookie);
      g_variant_unref (variant);
    }
  else
    {
      g_warning ("Problem calling inhibit %s", error->message);
    }

  g_object_unref (proxy);

  return cookie;
}

void
sj_uninhibit (guint cookie)
{
  GError *error = NULL;
  GDBusProxy *proxy = NULL;

  /* check if the cookie is valid */
  if (cookie <= 0)
    {
      g_warning ("Invalid cookie");
      return;
    }


  proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
                                         G_DBUS_PROXY_FLAGS_NONE,
                                         NULL,
                                         PM_DBUS_SERVICE,
                                         PM_DBUS_INHIBIT_PATH,
                                         PM_DBUS_INHIBIT_INTERFACE,
                                         NULL,
                                         &error);

  if (!proxy)
    {
      g_warning ("Could not get DBUS proxy: %s", error->message);
      g_clear_error (&error);
      return;
    }

  g_dbus_proxy_call_sync (proxy, "Uninhibit",
                          g_variant_new ("(u)", cookie),
                          G_DBUS_CALL_FLAGS_NONE, -1, NULL,
                          &error);

  if (error)
    {
      g_warning ("Problem uninhibiting: %s", error->message);
      g_clear_error (&error);
    }

  g_object_unref (proxy);
}