1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3
* Copyright (C) 2009 Richard Hughes <richard@hughsie.com>
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.
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.
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
34
#include <linux/input.h>
37
#include <glib/gstdio.h>
38
#include <glib/gi18n-lib.h>
39
#include <glib-object.h>
40
#include <devkit-gobject/devkit-gobject.h>
42
#include "sysfs-utils.h"
43
#include "egg-debug.h"
46
#include "dkp-daemon.h"
47
#include "dkp-input.h"
48
#include "dkp-daemon.h"
50
struct DkpInputPrivate
53
struct input_event event;
59
static void dkp_input_class_init (DkpInputClass *klass);
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))
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)
73
* dkp_input_str_to_bitmask:
76
dkp_input_str_to_bitmask (const gchar *s, glong *bitmask, size_t max_size)
80
gint num_bits_set = 0;
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++) {
87
val = strtoul (v[i], NULL, 16);
101
* dkp_input_event_io:
104
dkp_input_event_io (GIOChannel *channel, GIOCondition condition, gpointer data)
106
DkpInput *input = (DkpInput*) data;
107
GError *error = NULL;
109
glong bitmask[NBITS(SW_MAX)];
113
if (condition & (G_IO_HUP | G_IO_ERR | G_IO_NVAL))
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) {
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");
129
/* we have all the data */
130
input->priv->offset = 0;
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);
138
if (input->priv->event.type != EV_SW) {
139
egg_debug ("not a switch event");
144
if (input->priv->event.code != SW_LID) {
145
egg_debug ("not a lid");
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");
156
ret = test_bit (input->priv->event.code, bitmask);
157
dkp_daemon_set_lid_is_closed (input->priv->daemon, ret);
164
* dkp_input_coldplug:
167
dkp_input_coldplug (DkpInput *input, DkpDaemon *daemon, DevkitDevice *d)
169
gboolean ret = FALSE;
171
gchar *contents = NULL;
172
const gchar *native_path;
173
const gchar *device_file;
174
GError *error = NULL;
175
glong bitmask[NBITS(SW_MAX)];
180
native_path = devkit_device_get_native_path (d);
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);
190
ret = g_file_get_contents (path, &contents, NULL, &error);
192
egg_debug ("failed to get contents for [%s]: %s", path, error->message);
193
g_error_free (error);
197
/* convert to a bitmask */
198
num_bits = dkp_input_str_to_bitmask (contents, bitmask, sizeof (bitmask));
200
egg_debug ("not one bitmask entry for %s", native_path);
206
if (!test_bit (SW_LID, bitmask)) {
207
egg_debug ("not a lid: %s", native_path);
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");
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));
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);
236
egg_debug ("watching %s (%i)", device_file, input->priv->eventfp);
237
input->priv->channel = g_io_channel_unix_new (input->priv->eventfp);
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);
249
input->priv->daemon = g_object_ref (daemon);
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);
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));
268
dkp_input_init (DkpInput *input)
270
input->priv = DKP_INPUT_GET_PRIVATE (input);
271
input->priv->eventfp = -1;
272
input->priv->channel = NULL;
273
input->priv->daemon = NULL;
277
* dkp_input_finalize:
280
dkp_input_finalize (GObject *object)
284
g_return_if_fail (object != NULL);
285
g_return_if_fail (DKP_IS_INPUT (object));
287
input = DKP_INPUT (object);
288
g_return_if_fail (input->priv != NULL);
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);
299
G_OBJECT_CLASS (dkp_input_parent_class)->finalize (object);
303
* dkp_input_class_init:
306
dkp_input_class_init (DkpInputClass *klass)
308
GObjectClass *object_class = G_OBJECT_CLASS (klass);
309
object_class->finalize = dkp_input_finalize;
310
g_type_class_add_private (klass, sizeof (DkpInputPrivate));
319
return g_object_new (DKP_TYPE_INPUT, NULL);