~ubuntu-branches/ubuntu/maverick/devicekit-power/maverick

« back to all changes in this revision

Viewing changes to src/dkp-input.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Biebl
  • Date: 2009-06-03 13:47:15 UTC
  • mfrom: (1.2.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090603134715-2jrfcf9jtoqs3ohd
Tags: 008-1
* New upstream release.
* debian/copyright
  - Add Name, Maintainer and Source field as recommended by DEP-5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2009 Richard Hughes <richard@hughsie.com>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 *
 
19
 */
 
20
 
 
21
#ifdef HAVE_CONFIG_H
 
22
#  include "config.h"
 
23
#endif
 
24
 
 
25
#include <string.h>
 
26
#include <math.h>
 
27
#include <errno.h>
 
28
#include <fcntl.h>
 
29
#include <stdint.h>
 
30
#include <stdio.h>
 
31
#include <stdlib.h>
 
32
#include <string.h>
 
33
#include <unistd.h>
 
34
#include <linux/input.h>
 
35
 
 
36
#include <glib.h>
 
37
#include <glib/gstdio.h>
 
38
#include <glib/gi18n-lib.h>
 
39
#include <glib-object.h>
 
40
#include <devkit-gobject/devkit-gobject.h>
 
41
 
 
42
#include "sysfs-utils.h"
 
43
#include "egg-debug.h"
 
44
 
 
45
#include "dkp-enum.h"
 
46
#include "dkp-daemon.h"
 
47
#include "dkp-input.h"
 
48
#include "dkp-daemon.h"
 
49
 
 
50
struct DkpInputPrivate
 
51
{
 
52
        int                      eventfp;
 
53
        struct input_event       event;
 
54
        gsize                    offset;
 
55
        GIOChannel              *channel;
 
56
        DkpDaemon               *daemon;
 
57
};
 
58
 
 
59
static void     dkp_input_class_init    (DkpInputClass  *klass);
 
60
 
 
61
G_DEFINE_TYPE (DkpInput, dkp_input, G_TYPE_OBJECT)
 
62
#define DKP_INPUT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), DKP_TYPE_INPUT, DkpInputPrivate))
 
63
 
 
64
/* we must use this kernel-compatible implementation */
 
65
#define BITS_PER_LONG (sizeof(long) * 8)
 
66
#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
 
67
#define OFF(x)  ((x)%BITS_PER_LONG)
 
68
#define BIT(x)  (1UL<<OFF(x))
 
69
#define LONG(x) ((x)/BITS_PER_LONG)
 
70
#define test_bit(bit, array)    ((array[LONG(bit)] >> OFF(bit)) & 1)
 
71
 
 
72
/**
 
73
 * dkp_input_str_to_bitmask:
 
74
 **/
 
75
static gint
 
76
dkp_input_str_to_bitmask (const gchar *s, glong *bitmask, size_t max_size)
 
77
{
 
78
        gint i, j;
 
79
        gchar **v;
 
80
        gint num_bits_set = 0;
 
81
 
 
82
        memset (bitmask, 0, max_size);
 
83
        v = g_strsplit (s, " ", max_size);
 
84
        for (i = g_strv_length (v) - 1, j = 0; i >= 0; i--, j++) {
 
85
                gulong val;
 
86
 
 
87
                val = strtoul (v[i], NULL, 16);
 
88
                bitmask[j] = val;
 
89
 
 
90
                while (val != 0) {
 
91
                        num_bits_set++;
 
92
                        val &= (val - 1);
 
93
                }
 
94
        }
 
95
        g_strfreev(v);
 
96
 
 
97
        return num_bits_set;
 
98
}
 
99
 
 
100
/**
 
101
 * dkp_input_event_io:
 
102
 **/
 
103
static gboolean
 
104
dkp_input_event_io (GIOChannel *channel, GIOCondition condition, gpointer data)
 
105
{
 
106
        DkpInput *input = (DkpInput*) data;
 
107
        GError *error = NULL;
 
108
        gsize read_bytes;
 
109
        glong bitmask[NBITS(SW_MAX)];
 
110
        gboolean ret;
 
111
 
 
112
        /* uninteresting */
 
113
        if (condition & (G_IO_HUP | G_IO_ERR | G_IO_NVAL))
 
114
                return FALSE;
 
115
 
 
116
        /* read event */
 
117
        while (g_io_channel_read_chars (channel,
 
118
                ((gchar*)&input->priv->event) + input->priv->offset,
 
119
                sizeof(struct input_event) - input->priv->offset,
 
120
                &read_bytes, &error) == G_IO_STATUS_NORMAL) {
 
121
 
 
122
                /* not enough data */
 
123
                if (input->priv->offset + read_bytes < sizeof (struct input_event)) {
 
124
                        input->priv->offset = input->priv->offset + read_bytes;
 
125
                        egg_debug ("incomplete read");
 
126
                        goto out;
 
127
                }
 
128
 
 
129
                /* we have all the data */
 
130
                input->priv->offset = 0;
 
131
 
 
132
                egg_debug ("event.value=%d ; event.code=%d (0x%02x)",
 
133
                           input->priv->event.value,
 
134
                           input->priv->event.code,
 
135
                           input->priv->event.code);
 
136
 
 
137
                /* switch? */
 
138
                if (input->priv->event.type != EV_SW) {
 
139
                        egg_debug ("not a switch event");
 
140
                        continue;
 
141
                }
 
142
 
 
143
                /* is not lid */
 
144
                if (input->priv->event.code != SW_LID) {
 
145
                        egg_debug ("not a lid");
 
146
                        continue;
 
147
                }
 
148
 
 
149
                /* check switch state */
 
150
                if (ioctl (g_io_channel_unix_get_fd(channel), EVIOCGSW(sizeof (bitmask)), bitmask) < 0) {
 
151
                        egg_debug ("ioctl EVIOCGSW failed");
 
152
                        continue;
 
153
                }
 
154
 
 
155
                /* are we set */
 
156
                ret = test_bit (input->priv->event.code, bitmask);
 
157
                dkp_daemon_set_lid_is_closed (input->priv->daemon, ret);
 
158
        }
 
159
out:
 
160
        return TRUE;
 
161
}
 
162
 
 
163
/**
 
164
 * dkp_input_coldplug:
 
165
 **/
 
166
gboolean
 
167
dkp_input_coldplug (DkpInput *input, DkpDaemon *daemon, DevkitDevice *d)
 
168
{
 
169
        gboolean ret = FALSE;
 
170
        gchar *path;
 
171
        gchar *contents = NULL;
 
172
        const gchar *native_path;
 
173
        const gchar *device_file;
 
174
        GError *error = NULL;
 
175
        glong bitmask[NBITS(SW_MAX)];
 
176
        gint num_bits;
 
177
        GIOStatus status;
 
178
 
 
179
        /* get sysfs path */
 
180
        native_path = devkit_device_get_native_path (d);
 
181
 
 
182
        /* is a switch */
 
183
        path = g_build_filename (native_path, "../capabilities/sw", NULL);
 
184
        if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
 
185
                egg_debug ("not a switch [%s]", path);
 
186
                goto out;
 
187
        }
 
188
 
 
189
        /* get caps */
 
190
        ret = g_file_get_contents (path, &contents, NULL, &error);
 
191
        if (!ret) {
 
192
                egg_debug ("failed to get contents for [%s]: %s", path, error->message);
 
193
                g_error_free (error);
 
194
                goto out;
 
195
        }
 
196
 
 
197
        /* convert to a bitmask */
 
198
        num_bits = dkp_input_str_to_bitmask (contents, bitmask, sizeof (bitmask));
 
199
        if (num_bits != 1) {
 
200
                egg_debug ("not one bitmask entry for %s", native_path);
 
201
                ret = FALSE;
 
202
                goto out;
 
203
        }
 
204
 
 
205
        /* is this a lid? */
 
206
        if (!test_bit (SW_LID, bitmask)) {
 
207
                egg_debug ("not a lid: %s", native_path);
 
208
                ret = FALSE;
 
209
                goto out;
 
210
        }
 
211
 
 
212
        /* get device file */
 
213
        device_file = devkit_device_get_device_file (d);
 
214
        if (device_file == NULL || device_file[0] == '\0') {
 
215
                egg_warning ("no device file");
 
216
                ret = FALSE;
 
217
                goto out;
 
218
        }
 
219
 
 
220
        /* open device file */
 
221
        input->priv->eventfp = open (device_file, O_RDONLY | O_NONBLOCK);
 
222
        if (input->priv->eventfp <= 0) {
 
223
                egg_warning ("cannot open '%s': %s", device_file, strerror (errno));
 
224
                ret = FALSE;
 
225
                goto out;
 
226
        }
 
227
 
 
228
        /* get initial state */
 
229
        if (ioctl (input->priv->eventfp, EVIOCGSW(sizeof (bitmask)), bitmask) < 0) {
 
230
                egg_warning ("ioctl EVIOCGSW on %s failed", native_path);
 
231
                ret = FALSE;
 
232
                goto out;
 
233
        }
 
234
 
 
235
        /* create channel */
 
236
        egg_debug ("watching %s (%i)", device_file, input->priv->eventfp);
 
237
        input->priv->channel = g_io_channel_unix_new (input->priv->eventfp);
 
238
 
 
239
        /* set binary encoding */
 
240
        status = g_io_channel_set_encoding (input->priv->channel, NULL, &error);
 
241
        if (status != G_IO_STATUS_NORMAL) {
 
242
                egg_warning ("failed to set encoding: %s", error->message);
 
243
                g_error_free (error);
 
244
                ret = FALSE;
 
245
                goto out;
 
246
        }
 
247
 
 
248
        /* save daemon */
 
249
        input->priv->daemon = g_object_ref (daemon);
 
250
 
 
251
        /* watch this */
 
252
        g_io_add_watch (input->priv->channel, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL, dkp_input_event_io, input);
 
253
 
 
254
        /* set if we are closed */
 
255
        egg_debug ("using %s for lid event", native_path);
 
256
        dkp_daemon_set_lid_is_closed (daemon, test_bit (SW_LID, bitmask));
 
257
 
 
258
out:
 
259
        g_free (path);
 
260
        g_free (contents);
 
261
        return ret;
 
262
}
 
263
 
 
264
/**
 
265
 * dkp_input_init:
 
266
 **/
 
267
static void
 
268
dkp_input_init (DkpInput *input)
 
269
{
 
270
        input->priv = DKP_INPUT_GET_PRIVATE (input);
 
271
        input->priv->eventfp = -1;
 
272
        input->priv->channel = NULL;
 
273
        input->priv->daemon = NULL;
 
274
}
 
275
 
 
276
/**
 
277
 * dkp_input_finalize:
 
278
 **/
 
279
static void
 
280
dkp_input_finalize (GObject *object)
 
281
{
 
282
        DkpInput *input;
 
283
 
 
284
        g_return_if_fail (object != NULL);
 
285
        g_return_if_fail (DKP_IS_INPUT (object));
 
286
 
 
287
        input = DKP_INPUT (object);
 
288
        g_return_if_fail (input->priv != NULL);
 
289
 
 
290
        if (input->priv->daemon != NULL)
 
291
                g_object_unref (input->priv->daemon);
 
292
        if (input->priv->eventfp >= 0)
 
293
                close (input->priv->eventfp);
 
294
        if (input->priv->channel) {
 
295
                g_io_channel_shutdown (input->priv->channel, FALSE, NULL);
 
296
                g_io_channel_unref (input->priv->channel);
 
297
                egg_error ("moooo");
 
298
        }
 
299
        G_OBJECT_CLASS (dkp_input_parent_class)->finalize (object);
 
300
}
 
301
 
 
302
/**
 
303
 * dkp_input_class_init:
 
304
 **/
 
305
static void
 
306
dkp_input_class_init (DkpInputClass *klass)
 
307
{
 
308
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
309
        object_class->finalize = dkp_input_finalize;
 
310
        g_type_class_add_private (klass, sizeof (DkpInputPrivate));
 
311
}
 
312
 
 
313
/**
 
314
 * dkp_input_new:
 
315
 **/
 
316
DkpInput *
 
317
dkp_input_new (void)
 
318
{
 
319
        return g_object_new (DKP_TYPE_INPUT, NULL);
 
320
}
 
321