~ubuntu-branches/debian/sid/hal/sid

« back to all changes in this revision

Viewing changes to tools/hal-setup-keymap.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2007-10-23 12:33:58 UTC
  • Revision ID: james.westby@ubuntu.com-20071023123358-xaf8mjc5n84d5gtz
Tags: upstream-0.5.10
ImportĀ upstreamĀ versionĀ 0.5.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Licensed under the GNU General Public License Version 2
 
3
 *
 
4
 * Copyright (C) 2007 Richard Hughes <richard@hughsie.com>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; either version 2
 
9
 * of the License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
19
 * 02110-1301, USA.
 
20
 */
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
#  include <config.h>
 
24
#endif
 
25
 
 
26
#include <stdio.h>
 
27
#include <stdlib.h>
 
28
#include <errno.h>
 
29
#include <string.h>
 
30
#include <fcntl.h>
 
31
#include <unistd.h>
 
32
#include <sys/ioctl.h>
 
33
 
 
34
#include <linux/input.h>
 
35
 
 
36
#include "libhal/libhal.h"
 
37
#include "hal-setup-keymap-hash-name.h"
 
38
 
 
39
static LibHalContext *ctx = NULL;
 
40
static char* udi;
 
41
 
 
42
static int
 
43
evdev_open (const char *dev)
 
44
{
 
45
        int fd;
 
46
 
 
47
        /* requires root privs */
 
48
        fd = open (dev, O_RDWR);
 
49
        if (fd < 0) {
 
50
                fprintf (stderr, "hal-setup-keymap: failed to open('%s'): %s\n",
 
51
                         dev, strerror (errno));
 
52
                return -1;
 
53
        }
 
54
 
 
55
        return fd;
 
56
}
 
57
 
 
58
static int
 
59
evdev_set_keycode (int fd, int scancode, int keycode)
 
60
{
 
61
        int ret;
 
62
        int codes[2];
 
63
 
 
64
        codes[0] = scancode;
 
65
        codes[1] = keycode;
 
66
 
 
67
        ret = ioctl (fd, EVIOCSKEYCODE, codes);
 
68
        if (ret < 0) {
 
69
                fprintf (stderr, "hal-setup-keymap: failed to set scancode %x to keycode %d: %s\n",
 
70
                         scancode, keycode, strerror(errno));
 
71
                return -1;
 
72
        }
 
73
 
 
74
        return 0;
 
75
}
 
76
 
 
77
int
 
78
main (int argc, char **argv)
 
79
{
 
80
        char **keymap_list;
 
81
        int i = 0;
 
82
        int fd;
 
83
        unsigned int scancode = 0;
 
84
        int keycode = 0;
 
85
        char keyname[128];
 
86
        int values;
 
87
        char *device;
 
88
        DBusError error;
 
89
        const struct key *name;
 
90
 
 
91
        udi = getenv ("UDI");
 
92
        if (udi == NULL) {
 
93
                fprintf (stderr, "hal-setup-keymap: Failed to get UDI\n");
 
94
                return 1;
 
95
        }
 
96
        dbus_error_init (&error);
 
97
        if ((ctx = libhal_ctx_init_direct (&error)) == NULL) {
 
98
                fprintf (stderr, "hal-setup-keymap: Unable to initialise libhal context: %s\n", error.message);
 
99
                return 1;
 
100
        }
 
101
 
 
102
        dbus_error_init (&error);
 
103
        if (!libhal_device_addon_is_ready (ctx, udi, &error)) {
 
104
                return 1;
 
105
        }
 
106
 
 
107
        /* get the string list data */
 
108
        dbus_error_init (&error);
 
109
        keymap_list = libhal_device_get_property_strlist (ctx, udi, "input.keymap.data", &error);
 
110
        if (dbus_error_is_set (&error) == TRUE) {
 
111
                fprintf (stderr, "hal-setup-keymap: Failed to get keymap list: '%s'\n", error.message);
 
112
                return 1;
 
113
        }
 
114
 
 
115
        /* get the device */
 
116
        device = libhal_device_get_property_string (ctx, udi, "input.device", &error);
 
117
        if (dbus_error_is_set (&error) == TRUE) {
 
118
                fprintf (stderr, "hal-setup-keymap: Failed to get input device list: '%s'\n", error.message);
 
119
                return 1;
 
120
        }
 
121
 
 
122
        /* get a file descriptor to the device */
 
123
        fprintf (stderr, "hal-setup-keymap: Using device %s\n", device);
 
124
        fd = evdev_open (device);
 
125
        if (fd < 0) {
 
126
                fprintf (stderr, "hal-setup-keymap: Could not open device\n");
 
127
                return 1;
 
128
        }
 
129
 
 
130
        /* add each of the keys */
 
131
        do {
 
132
                values = sscanf (keymap_list[i], "%x:%s", &scancode, keyname);
 
133
                if (values == 2) {
 
134
                        /* fix for high scancodes on i8042 KBD - we do this here
 
135
                         * and not in the fdi as the high value is displayed in
 
136
                         * dmesg and we don't want the user to do more work */
 
137
                        if (scancode > 127) {
 
138
                                scancode -= 0xe000;
 
139
                                scancode += 128;
 
140
                        }
 
141
 
 
142
                        /* use gperf to convert as it's really quick */
 
143
                        name = lookup_key (keyname, strlen(keyname));
 
144
                        if (name != NULL) {
 
145
                                keycode = name->id;
 
146
                                fprintf (stderr, "hal-setup-keymap: parsed %s to (0x%x, %i)\n",
 
147
                                         keymap_list[i], scancode, keycode);
 
148
                                evdev_set_keycode (fd, scancode, keycode);
 
149
                        } else {
 
150
                                fprintf (stderr, "hal-setup-keymap: failed to find key '%s'\n", keyname);
 
151
                        }
 
152
                } else {
 
153
                        fprintf (stderr, "hal-setup-keymap: Failed to parse %s\n", keymap_list[i]);
 
154
                }
 
155
        } while (keymap_list[++i] != NULL);
 
156
 
 
157
        libhal_free_string_array (keymap_list);
 
158
        close (fd);
 
159
 
 
160
        /* we do not have to poll anything, so exit */
 
161
        return 0;
 
162
}
 
163