1 >From d96e2bd2a2b48ede527ad7071d3e0eeda9861b73 Mon Sep 17 00:00:00 2001
2 From: Robert Millan <rmh@debian.org>
3 Date: Mon, 24 Feb 2014 23:22:57 +0100
4 Subject: [PATCH] Add devd config backend for FreeBSD (and GNU/kFreeBSD)
6 Based on original code by Baptiste Daroussin, with some fixes made
7 by Koop Mast and myself.
9 Signed-off-by: Robert Millan <rmh@freebsd.org>
11 config/Makefile.am | 4 +
12 config/config-backends.h | 5 +
14 config/devd.c | 387 +++++++++++++++++++++++++++++++++++++++
16 hw/xfree86/common/xf86Config.c | 7 +-
17 hw/xfree86/common/xf86Globals.c | 3 +-
18 include/dix-config.h.in | 3 +
19 8 files changed, 427 insertions(+), 3 deletions(-)
20 create mode 100644 config/devd.c
22 Index: xorg-server/config/Makefile.am
23 ===================================================================
24 --- xorg-server.orig/config/Makefile.am
25 +++ xorg-server/config/Makefile.am
26 @@ -40,6 +40,10 @@ if CONFIG_WSCONS
27 libconfig_la_SOURCES += wscons.c
31 +libconfig_la_SOURCES += devd.c
34 endif # CONFIG_NEED_DBUS
37 Index: xorg-server/config/config-backends.h
38 ===================================================================
39 --- xorg-server.orig/config/config-backends.h
40 +++ xorg-server/config/config-backends.h
41 @@ -75,3 +75,8 @@ void config_hal_fini(void);
42 int config_wscons_init(void);
43 void config_wscons_fini(void);
47 +int config_devd_init(void);
48 +void config_devd_fini(void);
50 Index: xorg-server/config/config.c
51 ===================================================================
52 --- xorg-server.orig/config/config.c
53 +++ xorg-server/config/config.c
54 @@ -64,6 +64,9 @@ config_init(void)
55 #elif defined(CONFIG_WSCONS)
56 if (!config_wscons_init())
57 ErrorF("[config] failed to initialise wscons\n");
58 +#elif defined(CONFIG_DEVD)
59 + if (!config_devd_init())
60 + ErrorF("[config] failed to initialise devd\n");
64 @@ -82,6 +85,8 @@ config_fini(void)
65 config_dbus_core_fini();
66 #elif defined(CONFIG_WSCONS)
68 +#elif defined(CONFIG_DEVD)
73 Index: xorg-server/config/devd.c
74 ===================================================================
76 +++ xorg-server/config/devd.c
79 + * Copyright © 2012 Baptiste Daroussin
80 + * Copyright © 2014 Robert Millan
82 + * Permission is hereby granted, free of charge, to any person obtaining a
83 + * copy of this software and associated documentation files (the "Software"),
84 + * to deal in the Software without restriction, including without limitation
85 + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
86 + * and/or sell copies of the Software, and to permit persons to whom the
87 + * Software is furnished to do so, subject to the following conditions:
89 + * The above copyright notice and this permission notice (including the next
90 + * paragraph) shall be included in all copies or substantial portions of the
93 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
94 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
95 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
96 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
97 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
98 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
99 + * DEALINGS IN THE SOFTWARE.
101 + * Author: Baptiste Daroussin <bapt@FreeBSD.org>
104 +#ifdef HAVE_DIX_CONFIG_H
105 +#include <dix-config.h>
108 +#include <sys/types.h>
109 +#include <sys/socket.h>
110 +#include <sys/sysctl.h>
119 +#include <stdbool.h>
123 +#include "inputstr.h"
124 +#include "hotplug.h"
125 +#include "config-backends.h"
128 +#define DEVD_SOCK_PATH "/var/run/devd.pipe"
130 +#define DEVD_EVENT_ADD '+'
131 +#define DEVD_EVENT_REMOVE '-'
133 +static int sock_devd = -1;
136 + const char *driver;
138 + const char *xdriver;
141 +static struct hw_type hw_types[] = {
142 + {"ukbd", ATTR_KEYBOARD, "kbd"},
143 + {"atkbd", ATTR_KEYBOARD, "kbd"},
144 + {"ums", ATTR_POINTER, "mouse"},
145 + {"psm", ATTR_POINTER, "mouse"},
146 + {"uhid", ATTR_POINTER, "mouse"},
147 + {"joy", ATTR_JOYSTICK, NULL},
148 + {"atp", ATTR_TOUCHPAD, NULL},
149 + {"uep", ATTR_TOUCHSCREEN, NULL},
154 +sysctl_exists(const char *format, ...)
161 + if (format == NULL)
164 + va_start(args, format);
165 + vasprintf(&name, format, args);
168 + ret = sysctlbyname(name, NULL, &len, NULL, 0);
178 +sysctl_get_str(const char *format, ...)
185 + if (format == NULL)
188 + va_start(args, format);
189 + vasprintf(&name, format, args);
192 + if (sysctlbyname(name, NULL, &len, NULL, 0) == 0) {
193 + dest = malloc(len + 1);
196 + if (sysctlbyname(name, dest, &len, NULL, 0) == 0)
210 +device_added(char *devname)
212 + char path[PATH_MAX];
214 + char *product = NULL;
215 + char *config_info = NULL;
217 + InputOption *options = NULL;
218 + InputAttributes attrs = { };
219 + DeviceIntPtr dev = NULL;
223 + for (i = 0; hw_types[i].driver != NULL; i++) {
224 + if (strncmp(devname, hw_types[i].driver,
225 + strlen(hw_types[i].driver)) == 0 &&
226 + isdigit(*(devname + strlen(hw_types[i].driver)))) {
227 + attrs.flags |= hw_types[i].flag;
231 + if (hw_types[i].driver == NULL) {
232 + LogMessageVerb(X_INFO, 10, "config/devd: ignoring device %s\n",
236 + if (hw_types[i].xdriver == NULL) {
237 + LogMessageVerb(X_INFO, 10, "config/devd: ignoring device %s\n",
241 + snprintf(path, sizeof(path), "/dev/%s", devname);
243 + options = input_option_new(NULL, "_source", "server/devd");
248 + sysctl_get_str("dev.%s.%s.%%desc", hw_types[i].driver,
249 + devname + strlen(hw_types[i].driver));
250 + if (vendor == NULL) {
251 + attrs.vendor = strdup("(unnamed)");
252 + attrs.product = strdup("(unnamed)");
255 + if ((walk = strchr(vendor, ' ')) != NULL) {
259 + if ((walk = strchr(product, ',')) != NULL)
263 + attrs.vendor = strdup(vendor);
265 + attrs.product = strdup(product);
267 + attrs.product = strdup("(unnamed)");
269 + options = input_option_new(options, "name", xstrdup(attrs.product));
273 + attrs.usb_id = NULL;
274 + attrs.device = strdup(path);
275 + options = input_option_new(options, "driver", hw_types[i].xdriver);
276 + if (attrs.flags & ATTR_KEYBOARD) {
278 + * Don't pass device option if keyboard is attached to console (open fails),
279 + * thus activating special logic in xf86-input-keyboard.
281 + fd = open(path, O_RDONLY | O_NONBLOCK | O_EXCL);
284 + options = input_option_new(options, "device", xstrdup(path));
288 + options = input_option_new(options, "device", xstrdup(path));
291 + if (asprintf(&config_info, "devd:%s", devname) == -1) {
292 + config_info = NULL;
296 + if (device_is_duplicate(config_info)) {
297 + LogMessage(X_WARNING, "config/devd: device %s already added. "
298 + "Ignoring.\n", attrs.product);
302 + options = input_option_new(options, "config_info", config_info);
303 + LogMessage(X_INFO, "config/devd: adding input device %s (%s)\n",
304 + attrs.product, path);
306 + rc = NewInputDeviceRequest(options, &attrs, &dev);
313 + input_option_free_list(&options);
315 + free(attrs.usb_id);
316 + free(attrs.product);
317 + free(attrs.device);
318 + free(attrs.vendor);
322 +device_removed(char *devname)
326 + if (asprintf(&value, "devd:%s", devname) == -1)
329 + remove_devices("devd", value);
335 +socket_getline(int fd, char **out)
337 + char *buf, *newbuf;
338 + ssize_t ret, cap, sz = 0;
342 + buf = malloc(cap * sizeof(char));
347 + ret = read(sock_devd, &c, 1);
349 + if (errno == EINTR)
358 + if (sz + 1 >= cap) {
360 + newbuf = realloc(buf, cap * sizeof(char));
377 + return sz; /* number of bytes in the line, not counting the line break */
381 +wakeup_handler(void *data, int err, void *read_mask)
389 + if (FD_ISSET(sock_devd, (fd_set *) read_mask)) {
390 + if (socket_getline(sock_devd, &line) < 0)
393 + walk = strchr(line + 1, ' ');
398 + case DEVD_EVENT_ADD:
399 + device_added(line + 1);
401 + case DEVD_EVENT_REMOVE:
402 + device_removed(line + 1);
412 +block_handler(void *data, struct timeval **tv, void *read_mask)
417 +config_devd_init(void)
419 + struct sockaddr_un devd;
420 + char devicename[1024];
423 + /* first scan the sysctl to determine the hardware if needed */
425 + for (i = 0; hw_types[i].driver != NULL; i++) {
426 + for (j = 0; sysctl_exists("dev.%s.%i.%%desc", hw_types[i].driver, j);
428 + snprintf(devicename, sizeof(devicename), "%s%i", hw_types[i].driver,
430 + device_added(devicename);
434 + sock_devd = socket(AF_UNIX, SOCK_STREAM, 0);
435 + if (sock_devd < 0) {
436 + ErrorF("config/devd: Fail opening stream socket");
440 + devd.sun_family = AF_UNIX;
441 + strlcpy(devd.sun_path, DEVD_SOCK_PATH, sizeof(devd.sun_path));
443 + if (connect(sock_devd, (struct sockaddr *) &devd, sizeof(devd)) < 0) {
445 + ErrorF("config/devd: Fail to connect to devd");
449 + RegisterBlockAndWakeupHandlers(block_handler, wakeup_handler, NULL);
450 + AddGeneralSocket(sock_devd);
456 +config_devd_fini(void)
461 + RemoveGeneralSocket(sock_devd);
462 + RemoveBlockAndWakeupHandlers(block_handler, wakeup_handler, NULL);
465 Index: xorg-server/configure.ac
466 ===================================================================
467 --- xorg-server.orig/configure.ac
468 +++ xorg-server/configure.ac
469 @@ -618,6 +618,7 @@ AC_ARG_ENABLE(config-udev, AS_HELP_ST
470 AC_ARG_ENABLE(config-udev-kms, AS_HELP_STRING([--enable-config-udev-kms], [Build udev kms support (default: auto)]), [CONFIG_UDEV_KMS=$enableval], [CONFIG_UDEV_KMS=auto])
471 AC_ARG_ENABLE(config-dbus, AS_HELP_STRING([--enable-config-dbus], [Build D-BUS API support (default: no)]), [CONFIG_DBUS_API=$enableval], [CONFIG_DBUS_API=no])
472 AC_ARG_ENABLE(config-hal, AS_HELP_STRING([--disable-config-hal], [Build HAL support (default: auto)]), [CONFIG_HAL=$enableval], [CONFIG_HAL=auto])
473 +AC_ARG_ENABLE(config-devd, AS_HELP_STRING([--disable-config-devd], [Build devd support (default: auto)]), [CONFIG_DEVD=$enableval], [CONFIG_DEVD=auto])
474 AC_ARG_ENABLE(config-wscons, AS_HELP_STRING([--enable-config-wscons], [Build wscons config support (default: auto)]), [CONFIG_WSCONS=$enableval], [CONFIG_WSCONS=auto])
475 AC_ARG_ENABLE(xfree86-utils, AS_HELP_STRING([--enable-xfree86-utils], [Build xfree86 DDX utilities (default: enabled)]), [XF86UTILS=$enableval], [XF86UTILS=yes])
476 AC_ARG_ENABLE(vgahw, AS_HELP_STRING([--enable-vgahw], [Build Xorg with vga access (default: enabled)]), [VGAHW=$enableval], [VGAHW=yes])
477 @@ -922,6 +923,21 @@ if test "x$CONFIG_WSCONS" = xyes; then
478 AC_DEFINE(CONFIG_WSCONS, 1, [Use wscons for input auto configuration])
481 +if test "x$CONFIG_DEVD" = xauto; then
483 + freebsd* | kfreebsd*-gnu)
491 +AM_CONDITIONAL(CONFIG_DEVD, [test "x$CONFIG_DEVD" = xyes])
492 +if test "x$CONFIG_DEVD" = xyes; then
493 + AC_DEFINE(CONFIG_DEVD, 1, [Use devd for input auto configuration])
496 if test "x$USE_SIGIO_BY_DEFAULT" = xyes; then
497 USE_SIGIO_BY_DEFAULT_VALUE=TRUE
499 Index: xorg-server/hw/xfree86/common/xf86Config.c
500 ===================================================================
501 --- xorg-server.orig/hw/xfree86/common/xf86Config.c
502 +++ xorg-server/hw/xfree86/common/xf86Config.c
503 @@ -1377,15 +1377,18 @@ checkCoreInputDevices(serverLayoutPtr se
506 if (!xf86Info.forceInputDevices && !(foundPointer && foundKeyboard)) {
507 -#if defined(CONFIG_HAL) || defined(CONFIG_UDEV) || defined(CONFIG_WSCONS)
508 +#if defined(CONFIG_HAL) || defined(CONFIG_UDEV) || defined(CONFIG_WSCONS) || \
509 + defined(CONFIG_DEVD)
510 const char *config_backend;
512 #if defined(CONFIG_HAL)
513 config_backend = "HAL";
514 #elif defined(CONFIG_UDEV)
515 config_backend = "udev";
517 +#elif defined(CONFIG_WSCONS)
518 config_backend = "wscons";
519 +#elif defined(CONFIG_DEVD)
520 + config_backend = "devd";
522 xf86Msg(X_INFO, "The server relies on %s to provide the list of "
523 "input devices.\n\tIf no devices become available, "
524 Index: xorg-server/hw/xfree86/common/xf86Globals.c
525 ===================================================================
526 --- xorg-server.orig/hw/xfree86/common/xf86Globals.c
527 +++ xorg-server/hw/xfree86/common/xf86Globals.c
528 @@ -123,7 +123,8 @@ xf86InfoRec xf86Info = {
530 .disableRandR = FALSE,
531 .randRFrom = X_DEFAULT,
532 -#if defined(CONFIG_HAL) || defined(CONFIG_UDEV) || defined(CONFIG_WSCONS)
533 +#if defined(CONFIG_HAL) || defined(CONFIG_UDEV) || defined(CONFIG_WSCONS) || \
534 + defined(CONFIG_DEVD)
535 .forceInputDevices = FALSE,
536 .autoAddDevices = TRUE,
537 .autoEnableDevices = TRUE,
538 Index: xorg-server/include/dix-config.h.in
539 ===================================================================
540 --- xorg-server.orig/include/dix-config.h.in
541 +++ xorg-server/include/dix-config.h.in
543 /* Support HAL for hotplug */
546 +/* Support devd for hotplug */
549 /* Have a monotonic clock from clock_gettime() */
550 #undef MONOTONIC_CLOCK