summaryrefslogtreecommitdiff
path: root/src/mcd-slacker.c
blob: b3ffbef2fa0456f29b41640c6ccd22deed675cc6 (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
/*
 * slacker.c - Maemo device state monitor
 * Copyright ©2010 Collabora Ltd.
 * Copyright ©2008-2010 Nokia Corporation
 *
 * Derived from code in e-book-backend-tp.c in eds-backend-telepathy; thanks!
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "mcd-slacker.h"
#include "config.h"

#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>

#include <telepathy-glib/telepathy-glib.h>

#ifdef HAVE_MCE
#include <mce/dbus-names.h>
#else /* HAVE_MCE */

/* Use some dummy interfaces etc. for the test suite.
 *
 * In a perfect world of sweetness and light these would not be enabled for the
 * real build, but we do not live in a perfect world of sweetness and light: we
 * live below a dark cloud of bitter ash, the charred remains of a defunct
 * economy and cygnine clothing.
 */
#define MCE_SERVICE "org.freedesktop.Telepathy.MissionControl.Tests.MCE"

#define MCE_SIGNAL_IF "org.freedesktop.Telepathy.MissionControl.Tests.MCE"
#define MCE_INACTIVITY_SIG "InactivityChanged"

#define MCE_REQUEST_IF "org.freedesktop.Telepathy.MissionControl.Tests.MCE"
#define MCE_REQUEST_PATH "/org/freedesktop/Telepathy/MissionControl/Tests/MCE"
#define MCE_INACTIVITY_STATUS_GET "GetInactivity"

#endif /* HAVE_MCE */

#include "mcd-debug.h"
#include "mcd-signals-marshal.h"

struct _McdSlackerPrivate {
    DBusGConnection *bus;
    DBusGProxy *mce_request_proxy;

    gboolean is_inactive;
};

G_DEFINE_TYPE (McdSlacker, mcd_slacker, G_TYPE_OBJECT)

enum {
    SIG_INACTIVITY_CHANGED = 0,
    N_SIGNALS
};

static guint signals[N_SIGNALS];

/**
 * mcd_slacker_is_inactive:
 * @self: do some work!
 *
 * <!-- -->
 *
 * Returns: %TRUE if the device is known to be inactive; false otherwise.
 */
gboolean
mcd_slacker_is_inactive (McdSlacker *self)
{
  g_return_val_if_fail (MCD_IS_SLACKER (self), FALSE);

  return self->priv->is_inactive;
}

static void
slacker_inactivity_changed (
    McdSlacker *self,
    gboolean is_inactive)
{
  McdSlackerPrivate *priv = self->priv;
  gboolean old = priv->is_inactive;

  priv->is_inactive = is_inactive;

  if (!!old != !!is_inactive)
    {
      DEBUG ("device became %s", (is_inactive ? "inactive" : "active"));
      g_signal_emit (self, signals[SIG_INACTIVITY_CHANGED], 0, is_inactive);
    }
}

static GQuark mce_signal_interface_quark = 0;
static GQuark mce_inactivity_signal_quark = 0;

#define INACTIVITY_MATCH_RULE \
  "type='signal',interface='" MCE_SIGNAL_IF "',member='" MCE_INACTIVITY_SIG "'"

static DBusHandlerResult
slacker_message_filter (
    DBusConnection *connection,
    DBusMessage *message,
    gpointer user_data)
{
  McdSlacker *self = MCD_SLACKER (user_data);
  GQuark interface, member;
  const gchar *interface_name = NULL;
  const gchar *member_name = NULL;

  if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

  interface_name = dbus_message_get_interface (message);
  if (interface_name == NULL)
    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

  member_name = dbus_message_get_member (message);
  if (member_name == NULL)
    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

  interface = g_quark_try_string (interface_name);
  member = g_quark_try_string (member_name);

  if (interface == mce_signal_interface_quark &&
      member == mce_inactivity_signal_quark)
    {
      gboolean is_inactive;

      if (dbus_message_get_args (message, NULL, DBUS_TYPE_BOOLEAN, &is_inactive,
              DBUS_TYPE_INVALID))
        slacker_inactivity_changed (self, is_inactive);
      else
        DEBUG (MCE_INACTIVITY_SIG " without a boolean argument, ignoring");
    }

  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

static void
get_inactivity_status_cb (
    DBusGProxy *proxy,
    DBusGProxyCall *call,
    gpointer user_data)
{
  McdSlacker *self = MCD_SLACKER (user_data);
  gboolean is_inactive;
  GError *error = NULL;

  if (!dbus_g_proxy_end_call (proxy, call, &error /* ignore errors */,
          G_TYPE_BOOLEAN, &is_inactive, G_TYPE_INVALID))
    {
      DEBUG ("error getting inactivity status: %s", error->message);
      g_error_free (error);
    }
    else
    {
      slacker_inactivity_changed (self, is_inactive);
    }

  tp_clear_object (&self->priv->mce_request_proxy);
}

static void
slacker_add_filter (McdSlacker *self)
{
  McdSlackerPrivate *priv = self->priv;
  DBusConnection *c = dbus_g_connection_get_connection (self->priv->bus);

  dbus_connection_add_filter (c, slacker_message_filter, self, NULL);
  dbus_bus_add_match (c, INACTIVITY_MATCH_RULE, NULL);

  priv->mce_request_proxy = dbus_g_proxy_new_for_name (priv->bus,
      MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF);
  dbus_g_proxy_begin_call (priv->mce_request_proxy, MCE_INACTIVITY_STATUS_GET,
      get_inactivity_status_cb, self, NULL, G_TYPE_INVALID);
}

static void
slacker_remove_filter (McdSlacker *self)
{
  DBusConnection *c = dbus_g_connection_get_connection (self->priv->bus);

  dbus_connection_remove_filter (c, slacker_message_filter, self);
  dbus_bus_remove_match (c, INACTIVITY_MATCH_RULE, NULL);
}

/* GObject boilerplate */

static void
mcd_slacker_init (McdSlacker *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, MCD_TYPE_SLACKER,
      McdSlackerPrivate);
}

static gpointer slacker = NULL;

static GObject *
mcd_slacker_constructor (
    GType type,
    guint n_construct_properties,
    GObjectConstructParam *construct_properties)
{
  GObject *retval;

  if (slacker == NULL)
    {
      slacker = G_OBJECT_CLASS (mcd_slacker_parent_class)->constructor (
        type, n_construct_properties, construct_properties);
      retval = slacker;
      g_object_add_weak_pointer (retval, &slacker);
    }
  else
    {
      retval = g_object_ref (slacker);
    }

  return retval;
}

static void
mcd_slacker_constructed (GObject *object)
{
  McdSlacker *self = MCD_SLACKER (object);
  GError *error = NULL;

#ifdef HAVE_MCE
  self->priv->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
#else
  self->priv->bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
#endif

  if (self->priv->bus == NULL)
    {
      g_warning ("help! where did my system bus go? %s", error->message);
      g_clear_error (&error);
    }
  else
    {
      slacker_add_filter (self);
    }
}

static void
mcd_slacker_dispose (GObject *object)
{
  McdSlacker *self = MCD_SLACKER (object);
  McdSlackerPrivate *priv = self->priv;

  tp_clear_object (&priv->mce_request_proxy); /* this cancels pending calls */

  if (priv->bus != NULL)
    slacker_remove_filter (self);

  tp_clear_pointer (&priv->bus, dbus_g_connection_unref);

  ((GObjectClass *) mcd_slacker_parent_class)->dispose (object);
}

static void
mcd_slacker_class_init (McdSlackerClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructor = mcd_slacker_constructor;
  object_class->constructed = mcd_slacker_constructed;
  object_class->dispose = mcd_slacker_dispose;

  g_type_class_add_private (klass, sizeof (McdSlackerPrivate));

  /**
   * McdSlacker::inactivity-changed:
   * @self: what a slacker
   * @inactive: %TRUE if the device is inactive.
   *
   * The ::inactivity-changed is emitted whenever MCE declares that the device
   * has become active or inactive. Note that there is a lag (of around 30
   * seconds, at the time of writing) between the screen blanking and MCE
   * declaring the device inactive.
   */
  signals[SIG_INACTIVITY_CHANGED] = g_signal_new ("inactivity-changed",
      MCD_TYPE_SLACKER, G_SIGNAL_RUN_LAST, 0, NULL, NULL,
      _mcd_marshal_VOID__BOOLEAN,
      G_TYPE_NONE, 1, G_TYPE_BOOLEAN);

  if (!mce_signal_interface_quark)
    {
      mce_signal_interface_quark = g_quark_from_static_string (MCE_SIGNAL_IF);
      mce_inactivity_signal_quark = g_quark_from_static_string (
          MCE_INACTIVITY_SIG);
    }
}

McdSlacker *
mcd_slacker_new ()
{
  return g_object_new (MCD_TYPE_SLACKER, NULL);
}