summaryrefslogtreecommitdiff
path: root/tools/hal-setup-keymap.c
blob: 24c51f94f8be668d1b330ecabd0f02d866b11672 (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
/*
 * Licensed under the GNU General Public License Version 2
 *
 * Copyright (C) 2007 Richard Hughes <richard@hughsie.com>
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

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

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>

#ifdef HAL_LINUX_INPUT_HEADER_H
  #include HAL_LINUX_INPUT_HEADER_H
#else
  #include <linux/input.h>
#endif

#include "libhal/libhal.h"
#include "hal-setup-keymap-hash-name.h"

static LibHalContext *ctx = NULL;
static char* udi;

static int
evdev_open (const char *dev)
{
	int fd;

	/* requires root privs */
	fd = open (dev, O_RDWR);
	if (fd < 0) {
		fprintf (stderr, "hal-setup-keymap: failed to open('%s'): %s\n",
			 dev, strerror (errno));
		return -1;
	}

	return fd;
}

static int
evdev_set_keycode (int fd, int scancode, int keycode)
{
	int ret;
	int codes[2];

	codes[0] = scancode;
	codes[1] = keycode;

	ret = ioctl (fd, EVIOCSKEYCODE, codes);
	if (ret < 0) {
		fprintf (stderr, "hal-setup-keymap: failed to set scancode %x to keycode %d: %s\n",
			 scancode, keycode, strerror(errno));
		return -1;
	}

	return 0;
}

int
main (int argc, char **argv)
{
	char **keymap_list;
	int i = 0;
	int fd;
	unsigned int scancode = 0;
	int keycode = 0;
	char keyname[128];
	int values;
	char *device;
	DBusError error;
	const struct key *name;

	udi = getenv ("UDI");
	if (udi == NULL) {
		fprintf (stderr, "hal-setup-keymap: Failed to get UDI\n");
		return 1;
	}
	dbus_error_init (&error);
	if ((ctx = libhal_ctx_init_direct (&error)) == NULL) {
		fprintf (stderr, "hal-setup-keymap: Unable to initialise libhal context: %s\n", error.message);
		return 1;
	}

	dbus_error_init (&error);
	if (!libhal_device_addon_is_ready (ctx, udi, &error)) {
		return 1;
	}

	/* get the string list data */
	dbus_error_init (&error);
	keymap_list = libhal_device_get_property_strlist (ctx, udi, "input.keymap.data", &error);
	if (dbus_error_is_set (&error) == TRUE) {
		fprintf (stderr, "hal-setup-keymap: Failed to get keymap list: '%s'\n", error.message);
		return 1;
	}

	/* get the device */
	device = libhal_device_get_property_string (ctx, udi, "input.device", &error);
	if (dbus_error_is_set (&error) == TRUE) {
		fprintf (stderr, "hal-setup-keymap: Failed to get input device list: '%s'\n", error.message);
		return 1;
	}

	/* get a file descriptor to the device */
	fprintf (stderr, "hal-setup-keymap: Using device %s\n", device);
	fd = evdev_open (device);
	if (fd < 0) {
		fprintf (stderr, "hal-setup-keymap: Could not open device\n");
		return 1;
	}

	/* add each of the keys */
	do {
		values = sscanf (keymap_list[i], "%x:%s", &scancode, keyname);
		if (values == 2) {
			/* fix for high scancodes on i8042 KBD - we do this here
			 * and not in the fdi as the high value is displayed in
			 * dmesg and we don't want the user to do more work */
			if (scancode > 127) {
				scancode -= 0xe000;
				scancode += 128;
			}

			/* use gperf to convert as it's really quick */
			name = lookup_key (keyname, strlen(keyname));
			if (name != NULL) {
				keycode = name->id;
				fprintf (stderr, "hal-setup-keymap: parsed %s to (0x%x, %i)\n",
					 keymap_list[i], scancode, keycode);
				evdev_set_keycode (fd, scancode, keycode);
			} else {
				fprintf (stderr, "hal-setup-keymap: failed to find key '%s'\n", keyname);
			}
		} else {
			fprintf (stderr, "hal-setup-keymap: Failed to parse %s\n", keymap_list[i]);
		}
	} while (keymap_list[++i] != NULL);

	libhal_free_string_array (keymap_list);
	close (fd);

	/* we do not have to poll anything, so exit */
	return 0;
}