1 /*
2 * Copyright © 2007 Daniel Stone
3 * Copyright © 2007 Red Hat, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Author: Daniel Stone <daniel@fooishbar.org>
25 */
26
27 #ifdef HAVE_DIX_CONFIG_H
28 #include <dix-config.h>
29 #endif
30
31 #include <dbus/dbus.h>
32 #include <hal/libhal.h>
33 #include <string.h>
34 #include <sys/select.h>
35
36 #include "dbus-core.h"
37 #include "input.h"
38 #include "inputstr.h"
39 #include "hotplug.h"
40 #include "config-backends.h"
41 #include "os.h"
42
43 #define LIBHAL_PROP_KEY "input.x11_options."
44 #define LIBHAL_XKB_PROP_KEY "input.xkb."
45
46 struct config_hal_info {
47 DBusConnection *system_bus;
48 LibHalContext *hal_ctx;
49 };
50
51 /* Used for special handling of xkb options. */
52 struct xkb_options {
53 char *layout;
54 char *model;
55 char *rules;
56 char *variant;
57 char *options;
58 };
59
60 static void
device_removed(LibHalContext * ctx,const char * udi)61 device_removed(LibHalContext * ctx, const char *udi)
62 {
63 char *value;
64
65 if (asprintf(&value, "hal:%s", udi) == -1)
66 return;
67
68 remove_devices("hal", value);
69
70 free(value);
71 }
72
73 static char *
get_prop_string(LibHalContext * hal_ctx,const char * udi,const char * name)74 get_prop_string(LibHalContext * hal_ctx, const char *udi, const char *name)
75 {
76 char *prop, *ret;
77
78 prop = libhal_device_get_property_string(hal_ctx, udi, name, NULL);
79 LogMessageVerb(X_INFO, 10, "config/hal: getting %s on %s returned %s\n",
80 name, udi, prop ? prop : "(null)");
81 if (prop) {
82 ret = strdup(prop);
83 libhal_free_string(prop);
84 }
85 else {
86 return NULL;
87 }
88
89 return ret;
90 }
91
92 static char *
get_prop_string_array(LibHalContext * hal_ctx,const char * udi,const char * prop)93 get_prop_string_array(LibHalContext * hal_ctx, const char *udi,
94 const char *prop)
95 {
96 char **props, *ret, *str;
97 int i, len = 0;
98
99 props = libhal_device_get_property_strlist(hal_ctx, udi, prop, NULL);
100 if (props) {
101 for (i = 0; props[i]; i++)
102 len += strlen(props[i]);
103
104 ret = calloc(sizeof(char), len + i); /* i - 1 commas, 1 NULL */
105 if (!ret) {
106 libhal_free_string_array(props);
107 return NULL;
108 }
109
110 str = ret;
111 for (i = 0; props[i]; i++) {
112 strcpy(str, props[i]);
113 str += strlen(props[i]);
114 *str++ = ',';
115 }
116 *(str - 1) = '\0';
117
118 libhal_free_string_array(props);
119 }
120 else {
121 return NULL;
122 }
123
124 return ret;
125 }
126
127 static void
device_added(LibHalContext * hal_ctx,const char * udi)128 device_added(LibHalContext * hal_ctx, const char *udi)
129 {
130 char *path = NULL, *driver = NULL, *name = NULL, *config_info = NULL;
131 char *hal_tags, *parent;
132 InputOption *input_options = NULL;
133 InputAttributes attrs = { 0 };
134 DeviceIntPtr dev = NULL;
135 DBusError error;
136 struct xkb_options xkb_opts = { 0 };
137 int rc;
138
139 LibHalPropertySet *set = NULL;
140 LibHalPropertySetIterator set_iter;
141 char *psi_key = NULL, *tmp_val;
142
143 dbus_error_init(&error);
144
145 driver = get_prop_string(hal_ctx, udi, "input.x11_driver");
146 if (!driver) {
147 /* verbose, don't tell the user unless they _want_ to see it */
148 LogMessageVerb(X_INFO, 7,
149 "config/hal: no driver specified for device %s\n", udi);
150 goto unwind;
151 }
152
153 path = get_prop_string(hal_ctx, udi, "input.device");
154 if (!path) {
155 LogMessage(X_WARNING,
156 "config/hal: no driver or path specified for %s\n", udi);
157 goto unwind;
158 }
159 attrs.device = strdup(path);
160
161 name = get_prop_string(hal_ctx, udi, "info.product");
162 if (!name)
163 name = strdup("(unnamed)");
164 else
165 attrs.product = strdup(name);
166
167 attrs.vendor = get_prop_string(hal_ctx, udi, "info.vendor");
168 hal_tags = get_prop_string(hal_ctx, udi, "input.tags");
169 attrs.tags = xstrtokenize(hal_tags, ",");
170 free(hal_tags);
171
172 if (libhal_device_query_capability(hal_ctx, udi, "input.keys", NULL))
173 attrs.flags |= ATTR_KEY | ATTR_KEYBOARD;
174 if (libhal_device_query_capability(hal_ctx, udi, "input.mouse", NULL))
175 attrs.flags |= ATTR_POINTER;
176 if (libhal_device_query_capability(hal_ctx, udi, "input.joystick", NULL))
177 attrs.flags |= ATTR_JOYSTICK;
178 if (libhal_device_query_capability(hal_ctx, udi, "input.tablet", NULL))
179 attrs.flags |= ATTR_TABLET;
180 if (libhal_device_query_capability(hal_ctx, udi, "input.tablet_pad", NULL))
181 attrs.flags |= ATTR_TABLET_PAD;
182 if (libhal_device_query_capability(hal_ctx, udi, "input.touchpad", NULL))
183 attrs.flags |= ATTR_TOUCHPAD;
184 if (libhal_device_query_capability(hal_ctx, udi, "input.touchscreen", NULL))
185 attrs.flags |= ATTR_TOUCHSCREEN;
186
187 parent = get_prop_string(hal_ctx, udi, "info.parent");
188 if (parent) {
189 int usb_vendor, usb_product;
190 char *old_parent;
191
192 /* construct USB ID in lowercase - "0000:ffff" */
193 usb_vendor = libhal_device_get_property_int(hal_ctx, parent,
194 "usb.vendor_id", NULL);
195 LogMessageVerb(X_INFO, 10,
196 "config/hal: getting usb.vendor_id on %s "
197 "returned %04x\n", parent, usb_vendor);
198 usb_product = libhal_device_get_property_int(hal_ctx, parent,
199 "usb.product_id", NULL);
200 LogMessageVerb(X_INFO, 10,
201 "config/hal: getting usb.product_id on %s "
202 "returned %04x\n", parent, usb_product);
203 if (usb_vendor && usb_product)
204 if (asprintf(&attrs.usb_id, "%04x:%04x", usb_vendor, usb_product)
205 == -1)
206 attrs.usb_id = NULL;
207
208 attrs.pnp_id = get_prop_string(hal_ctx, parent, "pnp.id");
209 old_parent = parent;
210
211 while (!attrs.pnp_id &&
212 (parent = get_prop_string(hal_ctx, parent, "info.parent"))) {
213 attrs.pnp_id = get_prop_string(hal_ctx, parent, "pnp.id");
214
215 free(old_parent);
216 old_parent = parent;
217 }
218
219 free(old_parent);
220 }
221
222 input_options = input_option_new(NULL, "_source", "server/hal");
223 if (!input_options) {
224 LogMessage(X_ERROR,
225 "config/hal: couldn't allocate first key/value pair\n");
226 goto unwind;
227 }
228
229 /* most drivers use device.. not path. evdev uses both however, but the
230 * path version isn't documented apparently. support both for now. */
231 input_options = input_option_new(input_options, "path", path);
232 input_options = input_option_new(input_options, "device", path);
233
234 input_options = input_option_new(input_options, "driver", driver);
235 input_options = input_option_new(input_options, "name", name);
236
237 if (asprintf(&config_info, "hal:%s", udi) == -1) {
238 config_info = NULL;
239 LogMessage(X_ERROR, "config/hal: couldn't allocate name\n");
240 goto unwind;
241 }
242
243 /* Check for duplicate devices */
244 if (device_is_duplicate(config_info)) {
245 LogMessage(X_WARNING,
246 "config/hal: device %s already added. Ignoring.\n", name);
247 goto unwind;
248 }
249
250 /* ok, grab options from hal.. iterate through all properties
251 * and lets see if any of them are options that we can add */
252 set = libhal_device_get_all_properties(hal_ctx, udi, &error);
253
254 if (!set) {
255 LogMessage(X_ERROR,
256 "config/hal: couldn't get property list for %s: %s (%s)\n",
257 udi, error.name, error.message);
258 goto unwind;
259 }
260
261 libhal_psi_init(&set_iter, set);
262 while (libhal_psi_has_more(&set_iter)) {
263 /* we are looking for supported keys.. extract and add to options */
264 psi_key = libhal_psi_get_key(&set_iter);
265
266 if (psi_key) {
267
268 /* normal options first (input.x11_options.<propname>) */
269 if (!strncasecmp
270 (psi_key, LIBHAL_PROP_KEY, sizeof(LIBHAL_PROP_KEY) - 1)) {
271 char *tmp;
272
273 /* only support strings for all values */
274 tmp_val = get_prop_string(hal_ctx, udi, psi_key);
275
276 if (tmp_val) {
277
278 /* xkb needs special handling. HAL specs include
279 * input.xkb.xyz options, but the x11-input.fdi specifies
280 * input.x11_options.Xkbxyz options. By default, we use
281 * the former, unless the specific X11 ones are specified.
282 * Since we can't predict the order in which the keys
283 * arrive, we need to store them.
284 */
285 if ((tmp = strcasestr(psi_key, "xkb")) && strlen(tmp) >= 4) {
286 if (!strcasecmp(&tmp[3], "layout")) {
287 free(xkb_opts.layout);
288 xkb_opts.layout = strdup(tmp_val);
289 }
290 else if (!strcasecmp(&tmp[3], "model")) {
291 free(xkb_opts.model);
292 xkb_opts.model = strdup(tmp_val);
293 }
294 else if (!strcasecmp(&tmp[3], "rules")) {
295 free(xkb_opts.rules);
296 xkb_opts.rules = strdup(tmp_val);
297 }
298 else if (!strcasecmp(&tmp[3], "variant")) {
299 free(xkb_opts.variant);
300 xkb_opts.variant = strdup(tmp_val);
301 }
302 else if (!strcasecmp(&tmp[3], "options")) {
303 free(xkb_opts.options);
304 xkb_opts.options = strdup(tmp_val);
305 }
306 }
307 else {
308 /* all others */
309 input_options =
310 input_option_new(input_options,
311 psi_key + sizeof(LIBHAL_PROP_KEY) -
312 1, tmp_val);
313 free(tmp_val);
314 }
315 }
316 else {
317 /* server 1.4 had xkb_options as strlist. */
318 if ((tmp = strcasestr(psi_key, "xkb")) &&
319 (strlen(tmp) >= 4) &&
320 (!strcasecmp(&tmp[3], "options")) &&
321 (tmp_val =
322 get_prop_string_array(hal_ctx, udi, psi_key))) {
323 free(xkb_opts.options);
324 xkb_opts.options = strdup(tmp_val);
325 }
326 }
327 }
328 else if (!strncasecmp
329 (psi_key, LIBHAL_XKB_PROP_KEY,
330 sizeof(LIBHAL_XKB_PROP_KEY) - 1)) {
331 char *tmp;
332
333 /* only support strings for all values */
334 tmp_val = get_prop_string(hal_ctx, udi, psi_key);
335
336 if (tmp_val && strlen(psi_key) >= sizeof(LIBHAL_XKB_PROP_KEY)) {
337
338 tmp = &psi_key[sizeof(LIBHAL_XKB_PROP_KEY) - 1];
339
340 if (!strcasecmp(tmp, "layout")) {
341 if (!xkb_opts.layout)
342 xkb_opts.layout = strdup(tmp_val);
343 }
344 else if (!strcasecmp(tmp, "rules")) {
345 if (!xkb_opts.rules)
346 xkb_opts.rules = strdup(tmp_val);
347 }
348 else if (!strcasecmp(tmp, "variant")) {
349 if (!xkb_opts.variant)
350 xkb_opts.variant = strdup(tmp_val);
351 }
352 else if (!strcasecmp(tmp, "model")) {
353 if (!xkb_opts.model)
354 xkb_opts.model = strdup(tmp_val);
355 }
356 else if (!strcasecmp(tmp, "options")) {
357 if (!xkb_opts.options)
358 xkb_opts.options = strdup(tmp_val);
359 }
360 free(tmp_val);
361 }
362 else {
363 /* server 1.4 had xkb options as strlist */
364 tmp_val = get_prop_string_array(hal_ctx, udi, psi_key);
365 if (tmp_val &&
366 strlen(psi_key) >= sizeof(LIBHAL_XKB_PROP_KEY)) {
367 tmp = &psi_key[sizeof(LIBHAL_XKB_PROP_KEY) - 1];
368 if (!strcasecmp(tmp, ".options") && (!xkb_opts.options))
369 xkb_opts.options = strdup(tmp_val);
370 }
371 free(tmp_val);
372 }
373 }
374 }
375
376 /* psi_key doesn't need to be freed */
377 libhal_psi_next(&set_iter);
378 }
379
380 /* Now add xkb options */
381 if (xkb_opts.layout)
382 input_options =
383 input_option_new(input_options, "xkb_layout", xkb_opts.layout);
384 if (xkb_opts.rules)
385 input_options =
386 input_option_new(input_options, "xkb_rules", xkb_opts.rules);
387 if (xkb_opts.variant)
388 input_options =
389 input_option_new(input_options, "xkb_variant", xkb_opts.variant);
390 if (xkb_opts.model)
391 input_options =
392 input_option_new(input_options, "xkb_model", xkb_opts.model);
393 if (xkb_opts.options)
394 input_options =
395 input_option_new(input_options, "xkb_options", xkb_opts.options);
396 input_options = input_option_new(input_options, "config_info", config_info);
397
398 /* this isn't an error, but how else do you output something that the user can see? */
399 LogMessage(X_INFO, "config/hal: Adding input device %s\n", name);
400 if ((rc = NewInputDeviceRequest(input_options, &attrs, &dev)) != Success) {
401 LogMessage(X_ERROR, "config/hal: NewInputDeviceRequest failed (%d)\n",
402 rc);
403 dev = NULL;
404 goto unwind;
405 }
406
407 unwind:
408 if (set)
409 libhal_free_property_set(set);
410 free(path);
411 free(driver);
412 free(name);
413 free(config_info);
414 input_option_free_list(&input_options);
415
416 free(attrs.product);
417 free(attrs.vendor);
418 free(attrs.device);
419 free(attrs.pnp_id);
420 free(attrs.usb_id);
421 if (attrs.tags) {
422 char **tag = attrs.tags;
423
424 while (*tag) {
425 free(*tag);
426 tag++;
427 }
428 free(attrs.tags);
429 }
430
431 free(xkb_opts.layout);
432 free(xkb_opts.rules);
433 free(xkb_opts.model);
434 free(xkb_opts.variant);
435 free(xkb_opts.options);
436
437 dbus_error_free(&error);
438
439 return;
440 }
441
442 static void
disconnect_hook(void * data)443 disconnect_hook(void *data)
444 {
445 DBusError error;
446 struct config_hal_info *info = data;
447
448 if (info->hal_ctx) {
449 if (dbus_connection_get_is_connected(info->system_bus)) {
450 dbus_error_init(&error);
451 if (!libhal_ctx_shutdown(info->hal_ctx, &error))
452 LogMessage(X_WARNING,
453 "config/hal: disconnect_hook couldn't shut down context: %s (%s)\n",
454 error.name, error.message);
455 dbus_error_free(&error);
456 }
457 libhal_ctx_free(info->hal_ctx);
458 }
459
460 info->hal_ctx = NULL;
461 info->system_bus = NULL;
462 }
463
464 static BOOL
connect_and_register(DBusConnection * connection,struct config_hal_info * info)465 connect_and_register(DBusConnection * connection, struct config_hal_info *info)
466 {
467 DBusError error;
468 char **devices;
469 int num_devices, i;
470
471 if (info->hal_ctx)
472 return TRUE; /* already registered, pretend we did something */
473
474 info->system_bus = connection;
475
476 dbus_error_init(&error);
477
478 info->hal_ctx = libhal_ctx_new();
479 if (!info->hal_ctx) {
480 LogMessage(X_ERROR, "config/hal: couldn't create HAL context\n");
481 goto out_err;
482 }
483
484 if (!libhal_ctx_set_dbus_connection(info->hal_ctx, info->system_bus)) {
485 LogMessage(X_ERROR,
486 "config/hal: couldn't associate HAL context with bus\n");
487 goto out_err;
488 }
489 if (!libhal_ctx_init(info->hal_ctx, &error)) {
490 LogMessage(X_ERROR,
491 "config/hal: couldn't initialise context: %s (%s)\n",
492 error.name ? error.name : "unknown error",
493 error.message ? error.message : "null");
494 goto out_err;
495 }
496 if (!libhal_device_property_watch_all(info->hal_ctx, &error)) {
497 LogMessage(X_ERROR,
498 "config/hal: couldn't watch all properties: %s (%s)\n",
499 error.name ? error.name : "unknown error",
500 error.message ? error.message : "null");
501 goto out_ctx;
502 }
503 libhal_ctx_set_device_added(info->hal_ctx, device_added);
504 libhal_ctx_set_device_removed(info->hal_ctx, device_removed);
505
506 devices = libhal_find_device_by_capability(info->hal_ctx, "input",
507 &num_devices, &error);
508 /* FIXME: Get default devices if error is set. */
509 if (dbus_error_is_set(&error)) {
510 LogMessage(X_ERROR, "config/hal: couldn't find input device: %s (%s)\n",
511 error.name ? error.name : "unknown error",
512 error.message ? error.message : "null");
513 goto out_ctx;
514 }
515 for (i = 0; i < num_devices; i++)
516 device_added(info->hal_ctx, devices[i]);
517 libhal_free_string_array(devices);
518
519 dbus_error_free(&error);
520
521 return TRUE;
522
523 out_ctx:
524 dbus_error_free(&error);
525
526 if (!libhal_ctx_shutdown(info->hal_ctx, &error)) {
527 LogMessage(X_WARNING,
528 "config/hal: couldn't shut down context: %s (%s)\n",
529 error.name ? error.name : "unknown error",
530 error.message ? error.message : "null");
531 dbus_error_free(&error);
532 }
533
534 out_err:
535 dbus_error_free(&error);
536
537 if (info->hal_ctx) {
538 libhal_ctx_free(info->hal_ctx);
539 }
540
541 info->hal_ctx = NULL;
542 info->system_bus = NULL;
543
544 return FALSE;
545 }
546
547 /**
548 * Handle NewOwnerChanged signals to deal with HAL startup at X server runtime.
549 *
550 * NewOwnerChanged is send once when HAL shuts down, and once again when it
551 * comes back up. Message has three arguments, first is the name
552 * (org.freedesktop.Hal), the second one is the old owner, third one is new
553 * owner.
554 */
555 static DBusHandlerResult
ownerchanged_handler(DBusConnection * connection,DBusMessage * message,void * data)556 ownerchanged_handler(DBusConnection * connection, DBusMessage * message,
557 void *data)
558 {
559 int ret = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
560
561 if (dbus_message_is_signal(message,
562 "org.freedesktop.DBus", "NameOwnerChanged")) {
563 DBusError error;
564 char *name, *old_owner, *new_owner;
565
566 dbus_error_init(&error);
567 dbus_message_get_args(message, &error,
568 DBUS_TYPE_STRING, &name,
569 DBUS_TYPE_STRING, &old_owner,
570 DBUS_TYPE_STRING, &new_owner, DBUS_TYPE_INVALID);
571
572 if (dbus_error_is_set(&error)) {
573 ErrorF
574 ("[config/hal] failed to get NameOwnerChanged args: %s (%s)\n",
575 error.name, error.message);
576 }
577 else if (name && strcmp(name, "org.freedesktop.Hal") == 0) {
578
579 if (!old_owner || !strlen(old_owner)) {
580 DebugF("[config/hal] HAL startup detected.\n");
581 if (connect_and_register
582 (connection, (struct config_hal_info *) data))
583 dbus_connection_unregister_object_path(connection,
584 "/org/freedesktop/DBus");
585 else
586 ErrorF("[config/hal] Failed to connect to HAL bus.\n");
587 }
588
589 ret = DBUS_HANDLER_RESULT_HANDLED;
590 }
591 dbus_error_free(&error);
592 }
593
594 return ret;
595 }
596
597 /**
598 * Register a handler for the NameOwnerChanged signal.
599 */
600 static BOOL
listen_for_startup(DBusConnection * connection,void * data)601 listen_for_startup(DBusConnection * connection, void *data)
602 {
603 DBusObjectPathVTable vtable = {.message_function = ownerchanged_handler, };
604 DBusError error;
605 const char MATCH_RULE[] = "sender='org.freedesktop.DBus',"
606 "interface='org.freedesktop.DBus',"
607 "type='signal',"
608 "path='/org/freedesktop/DBus'," "member='NameOwnerChanged'";
609 int rc = FALSE;
610
611 dbus_error_init(&error);
612 dbus_bus_add_match(connection, MATCH_RULE, &error);
613 if (!dbus_error_is_set(&error)) {
614 if (dbus_connection_register_object_path(connection,
615 "/org/freedesktop/DBus",
616 &vtable, data))
617 rc = TRUE;
618 else
619 ErrorF("[config/hal] cannot register object path.\n");
620 }
621 else {
622 ErrorF("[config/hal] couldn't add match rule: %s (%s)\n", error.name,
623 error.message);
624 ErrorF("[config/hal] cannot detect a HAL startup.\n");
625 }
626
627 dbus_error_free(&error);
628
629 return rc;
630 }
631
632 static void
connect_hook(DBusConnection * connection,void * data)633 connect_hook(DBusConnection * connection, void *data)
634 {
635 struct config_hal_info *info = data;
636
637 if (listen_for_startup(connection, data) &&
638 connect_and_register(connection, info))
639 dbus_connection_unregister_object_path(connection,
640 "/org/freedesktop/DBus");
641
642 return;
643 }
644
645 static struct config_hal_info hal_info;
646
647 static struct dbus_core_hook hook = {
648 .connect = connect_hook,
649 .disconnect = disconnect_hook,
650 .data = &hal_info,
651 };
652
653 int
config_hal_init(void)654 config_hal_init(void)
655 {
656 memset(&hal_info, 0, sizeof(hal_info));
657 hal_info.system_bus = NULL;
658 hal_info.hal_ctx = NULL;
659
660 if (!dbus_core_add_hook(&hook)) {
661 LogMessage(X_ERROR, "config/hal: failed to add D-Bus hook\n");
662 return 0;
663 }
664
665 /* verbose message */
666 LogMessageVerb(X_INFO, 7, "config/hal: initialized\n");
667
668 return 1;
669 }
670
671 void
config_hal_fini(void)672 config_hal_fini(void)
673 {
674 dbus_core_remove_hook(&hook);
675 }
676