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