View | Details | Raw Unified | Return to bug 196678 | Differences between
and this patch

Collapse All | Expand All

(-)x11-servers/xorg-server/Makefile (-9 / +6 lines)
Lines 83-88 Link Here
83
CONFIGURE_ARGS+=	--disable-config-hal
83
CONFIGURE_ARGS+=	--disable-config-hal
84
.endif
84
.endif
85
85
86
.if ${PORT_OPTIONS:MDEVD}
87
CONFIGURE_ARGS+=	--enable-config-devd
88
.else
89
CONFIGURE_ARGS+=	--disable-config-devd
90
.endif
91
86
# We handle Xorg setuid in the plist. This allows to build xorg-server as a user.
92
# We handle Xorg setuid in the plist. This allows to build xorg-server as a user.
87
CONFIGURE_ARGS+=--disable-install-setuid
93
CONFIGURE_ARGS+=--disable-install-setuid
88
94
Lines 114-128 Link Here
114
		-e 's|^LTLIBRARIES = |LTLIBRARIES = libglx.la |g' \
120
		-e 's|^LTLIBRARIES = |LTLIBRARIES = libglx.la |g' \
115
		${WRKSRC}/hw/xfree86/dixmods/Makefile.in
121
		${WRKSRC}/hw/xfree86/dixmods/Makefile.in
116
122
117
post-configure:
118
.if ${PORT_OPTIONS:MDEVD}
119
	@${REINPLACE_CMD} -e 's|config\.c|config.c devd.c|g' \
120
		-e 's|config\.lo|config.lo devd.lo|g' \
121
		${WRKSRC}/config/Makefile
122
	@${REINPLACE_CMD} -e 's|^/\* #undef CONFIG_UDEV \*/|#define CONFIG_DEVD 1|' \
123
		${WRKSRC}/include/dix-config.h
124
.endif
125
126
.if ${SLAVE_PORT} == "no"
123
.if ${SLAVE_PORT} == "no"
127
post-install:
124
post-install:
128
# The .xorg dir because else the xorg-server might not load the correct
125
# The .xorg dir because else the xorg-server might not load the correct
(-)x11-servers/xorg-server/files/patch-config_Makefile.am (+19 lines)
Line 0 Link Here
1
--- config/Makefile.am.orig	2016-07-15 16:18:11 UTC
2
+++ config/Makefile.am
3
@@ -32,7 +32,15 @@ else
4
 
5
 if CONFIG_WSCONS
6
 libconfig_la_SOURCES += wscons.c
7
-endif # CONFIG_WSCONS
8
+
9
+else
10
+
11
+if CONFIG_DEVD
12
+libconfig_la_SOURCES += devd.c
13
+libconfig_la_LIBADD += -lusbhid
14
+endif # CONFIG_DEVD
15
+
16
+endif # !CONFIG_WSCONS
17
 
18
 endif # !CONFIG_HAL
19
 
(-)x11-servers/xorg-server/files/patch-config_devd.c (-319 / +746 lines)
Lines 1-11 Link Here
1
--- config/devd.c.orig	2017-01-19 15:20:42 UTC
1
--- config/devd.c.orig	2017-02-11 23:47:35 UTC
2
+++ config/devd.c
2
+++ config/devd.c
3
@@ -0,0 +1,532 @@
3
@@ -0,0 +1,959 @@
4
+/*
4
+/*
5
+ * Copyright (c) 2012 Baptiste Daroussin
5
+ * Copyright (c) 2012 Baptiste Daroussin
6
+ * Copyright (c) 2013, 2014 Alex Kozlov
6
+ * Copyright (c) 2013, 2014 Alex Kozlov
7
+ * Copyright (c) 2014 Robert Millan
7
+ * Copyright (c) 2014 Robert Millan
8
+ * Copyright (c) 2014 Jean-Sebastien Pedron
8
+ * Copyright (c) 2014 Jean-Sebastien Pedron
9
+ * Copyright (c) 2015 - 2017 Rozhuk Ivan
9
+ *
10
+ *
10
+ * Permission is hereby granted, free of charge, to any person obtaining a
11
+ * Permission is hereby granted, free of charge, to any person obtaining a
11
+ * copy of this software and associated documentation files (the "Software"),
12
+ * copy of this software and associated documentation files (the "Software"),
Lines 39-46 Link Here
39
+#include <sys/stat.h>
40
+#include <sys/stat.h>
40
+#include <sys/sysctl.h>
41
+#include <sys/sysctl.h>
41
+#include <sys/un.h>
42
+#include <sys/un.h>
43
+#include <sys/mouse.h>
44
+#include <sys/consio.h>
45
+#include <sys/ioctl.h>
46
+#include <dev/usb/usb_ioctl.h>
47
+#include <dev/usb/usbhid.h>
42
+
48
+
43
+#include <ctype.h>
49
+#include <ctype.h>
50
+#include <dirent.h>
44
+#include <errno.h>
51
+#include <errno.h>
45
+#include <fcntl.h>
52
+#include <fcntl.h>
46
+#include <stdlib.h>
53
+#include <stdlib.h>
Lines 47-52 Link Here
47
+#include <stdio.h>
54
+#include <stdio.h>
48
+#include <stdbool.h>
55
+#include <stdbool.h>
49
+#include <unistd.h>
56
+#include <unistd.h>
57
+#include <string.h>
58
+#include <paths.h>
59
+#include <usbhid.h>
50
+
60
+
51
+#include "input.h"
61
+#include "input.h"
52
+#include "inputstr.h"
62
+#include "inputstr.h"
Lines 54-480 Link Here
54
+#include "config-backends.h"
64
+#include "config-backends.h"
55
+#include "os.h"
65
+#include "os.h"
56
+
66
+
57
+#define DEVD_SOCK_PATH "/var/run/devd.pipe"
58
+
67
+
68
+/* from: <linux/input.h> */
69
+#define	_IOC_READ   IOC_OUT
70
+struct input_id {
71
+	uint16_t bustype;
72
+	uint16_t vendor;
73
+	uint16_t product;
74
+	uint16_t version;
75
+};
76
+
77
+#define	EVIOCGBIT(ev, len)	_IOC(_IOC_READ, 'E', 0x20 + (ev), (len))
78
+#define	EVIOCGID		_IOR('E', 0x02, struct input_id)
79
+#define	EVIOCGNAME(len)		_IOC(_IOC_READ, 'E', 0x06, (len))
80
+#define	EVIOCGPHYS(len)		_IOC(_IOC_READ, 'E', 0x07, (len))
81
+
82
+#define	EV_KEY			0x01
83
+#define	EV_REL			0x02
84
+#define	EV_ABS			0x03
85
+#define	BTN_MISC		0x100
86
+#define	BTN_LEFT		0x110
87
+#define	BTN_RIGHT		0x111
88
+#define	BTN_MIDDLE		0x112
89
+#define	BTN_JOYSTICK		0x120
90
+#define	BTN_TOOL_PEN		0x140
91
+#define	BTN_TOOL_FINGER		0x145
92
+#define	BTN_TOUCH		0x14a
93
+#define	BTN_STYLUS		0x14b
94
+#define	BTN_STYLUS2		0x14c
95
+#define	KEY_MAX			0x2ff
96
+#define	KEY_CNT			(KEY_MAX + 1)
97
+#define	REL_X			0x00
98
+#define	REL_Y			0x01
99
+#define	REL_MAX			0x0f
100
+#define	REL_CNT			(REL_MAX + 1)
101
+#define	ABS_X			0x00
102
+#define	ABS_Y			0x01
103
+#define	ABS_PRESSURE		0x18
104
+#define	ABS_MT_SLOT		0x2f
105
+#define	ABS_MAX			0x3f
106
+#define	ABS_CNT			(ABS_MAX + 1)
107
+
108
+#define	U32_BITS		(sizeof(uint32_t) * 8)
109
+#define	U32_CNT(__x)		(((__x) + U32_BITS - 1) / U32_BITS)
110
+#define U32_IS_BIT_SET(__x, __bit) (((((const uint32_t*)(__x))[((__bit) >> 5)] >> ((__bit) & 0x1f))) & 0x01)
111
+
112
+/* from: <linux/joystick.h> */
113
+#define JSIOCGNAME(len)		_IOC(_IOC_READ, 'j', 0x13, len)		/* get identifier string */
114
+
115
+/* WebCamD specific. */
116
+#define WEBCAMD_IOCTL_GET_USB_VENDOR_ID _IOR('q', 250, unsigned short)
117
+#define WEBCAMD_IOCTL_GET_USB_PRODUCT_ID _IOR('q', 251, unsigned short)
118
+#define WEBCAMD_IOCTL_GET_USB_SPEED	_IOR('q', 252, unsigned int)
119
+
120
+#ifdef COMPAT_32BIT
121
+#	define hid_pass_ptr(ptr)	((uint64_t)(uintptr_t)(ptr))
122
+#else
123
+#	define hid_pass_ptr(ptr)	(ptr)
124
+#endif
125
+
126
+#define _PATH_DEV_LEN		(sizeof(_PATH_DEV) - 1)
127
+#define DEVD_PATH_DEV		"devd:" _PATH_DEV
128
+#define DEVD_PATH_DEV_LEN	(sizeof(DEVD_PATH_DEV) - 1)
129
+
130
+#define DEVD_SOCK_PATH		_PATH_VARRUN "devd.pipe"
131
+
59
+#define DEVD_EVENT_ADD		'+'
132
+#define DEVD_EVENT_ADD		'+'
60
+#define DEVD_EVENT_REMOVE	'-'
133
+#define DEVD_EVENT_REMOVE	'-'
134
+#define DEVD_EVENT_NOTIFY	'!'
61
+
135
+
62
+#define RECONNECT_DELAY		5 * 1000
136
+#define RECONNECT_DELAY		(5 * 1000)
63
+
137
+
64
+static int sock_devd;
65
+static bool is_console_kbd = false;
66
+static bool is_kbdmux = false;
67
+static OsTimerPtr rtimer = NULL;
68
+
138
+
69
+struct hw_type {
139
+#define is_meuqual(__v1, __v1sz, __v2, __v2sz)				\
70
+	const char *driver;
140
+	((__v1sz) == (__v2sz) && NULL != (__v1) && NULL != (__v2) &&	\
71
+	int flag;
141
+	 0 == memcmp((__v1), (__v2), (__v1sz)))
72
+	const char *xdriver;
142
+
143
+#define is_meuqual_cstr(__cstr, __v, __vsz)				\
144
+	is_meuqual(__cstr, (sizeof(__cstr) - 1), __v, __vsz)
145
+
146
+#define is_de_euqual_cstr(__de, __cstr)					\
147
+	(NULL != (__de) &&						\
148
+	 is_meuqual((__de)->d_name, (__de)->d_namlen, __cstr, (sizeof(__cstr) - 1)))
149
+
150
+#define devd_get_val_cstr(__cstr, __buf, __bufsz, __valsz)		\
151
+	devd_get_val((__buf), (__bufsz), __cstr, (sizeof(__cstr) - 1),	\
152
+	(__valsz))
153
+
154
+
155
+static int devd_skt;
156
+static char devd_buf[4096];
157
+static size_t devd_buf_used = 0;
158
+static int is_kbdmux = 0;
159
+OsTimerPtr rtimer;
160
+
161
+
162
+/* Input devices. */
163
+typedef struct hw_type_s {
164
+	const char	*dev_name;
165
+	size_t		dev_name_size;
166
+	size_t		path_offset;
167
+	int		flags;
168
+	const char	*xdriver; 
169
+} hw_type_t, *hw_type_p;
170
+
171
+/* xdriver can be set via config "InputClass" section.
172
+ * Do not set xdriver name if device have more than one
173
+ * xf86-input-* drivers.
174
+ * "input/event" can be hadled by: xf86-input-evdev and
175
+ * xf86-input-wacom, let user chooses.
176
+ */
177
+static hw_type_t hw_types[] = {
178
+	{ "uhid",	4, 0, 0, NULL },
179
+	{ "ukbd",	4, 0, ATTR_KEYBOARD, "kbd" },
180
+	{ "atkbd",	5, 0, ATTR_KEYBOARD, "kbd" },
181
+	{ "kbdmux",	6, 0, ATTR_KEYBOARD, "kbd" },
182
+	{ "sysmouse",	8, 0, ATTR_POINTER, "mouse" },
183
+	{ "ums",	3, 0, ATTR_POINTER, "mouse" },
184
+	{ "psm",	3, 0, ATTR_POINTER, "mouse" },
185
+	{ "vboxguest",	9, 0, ATTR_POINTER, "vboxmouse" },
186
+	{ "joy",	3, 0, ATTR_JOYSTICK, NULL },
187
+	{ "atp",	3, 0, ATTR_TOUCHPAD, NULL },
188
+	{ "uep",	3, 0, ATTR_TOUCHSCREEN, NULL },
189
+	{ "input/event",5, 6, 0, NULL },
190
+	{ "input/js",	2, 6, ATTR_JOYSTICK, NULL },
191
+	{ NULL,		0, 0, 0, NULL },
73
+};
192
+};
74
+
193
+
75
+static struct hw_type hw_types[] = {
194
+/* Input devices paths. */
76
+	{ "ukbd", ATTR_KEYBOARD, "kbd" },
195
+static hw_type_t hw_type_path[] = {
77
+	{ "atkbd", ATTR_KEYBOARD, "kbd" },
196
+	{ "input/",	0, 6, 0, NULL },
78
+	{ "kbdmux", ATTR_KEYBOARD, "kbd" },
197
+	{ NULL,		0, 0, 0, NULL },
79
+	{ "sysmouse", ATTR_POINTER, "mouse" },
80
+	{ "ums", ATTR_POINTER, "mouse" },
81
+	{ "psm", ATTR_POINTER, "mouse" },
82
+	{ "vboxguest", ATTR_POINTER, "vboxmouse" },
83
+	{ "joy", ATTR_JOYSTICK, NULL },
84
+	{ "atp", ATTR_TOUCHPAD, NULL },
85
+	{ "uep", ATTR_TOUCHSCREEN, NULL },
86
+	{ NULL, -1, NULL },
87
+};
198
+};
88
+
199
+
89
+static bool
200
+
90
+sysctl_exists(const struct hw_type *device, int unit,
201
+static size_t
91
+	char *devname, size_t devname_len)
202
+bits_calc(const uint32_t *bits, size_t off_start, size_t off_stop)
92
+{
203
+{
93
+	char sysctlname[PATH_MAX];
204
+	size_t i, count = 0;
94
+	size_t len;
95
+	int ret;
96
+
205
+
97
+	if (device == NULL || device->driver == NULL)
206
+	for (i = off_start; i < off_stop; i ++) {
98
+		return false;
207
+		if (U32_IS_BIT_SET(bits, i)) {
208
+			count ++;
209
+		}
210
+	}
211
+	return (count);
212
+}
99
+
213
+
100
+	/* Check if a sysctl exists. */
214
+static hw_type_p
101
+	snprintf(sysctlname, sizeof(sysctlname), "dev.%s.%i.%%desc",
215
+get_dev_type_by_name(const char *dev_name, size_t dev_name_size)
102
+	    device->driver, unit);
216
+{
103
+	ret = sysctlbyname(sysctlname, NULL, &len, NULL, 0);
217
+	size_t i;
104
+
218
+
105
+	if (ret == 0 && len > 0) {
219
+	if (NULL == dev_name || 0 == dev_name_size)
106
+		snprintf(devname, devname_len, "%s%i", device->driver, unit);
220
+		return (NULL);
107
+		return true;
221
+	for (i = 0; NULL != hw_types[i].dev_name; i ++) {
222
+		if (dev_name_size >= (hw_types[i].dev_name_size + hw_types[i].path_offset) &&
223
+		    0 == memcmp(dev_name, hw_types[i].dev_name,
224
+		    (hw_types[i].path_offset + hw_types[i].dev_name_size))) {
225
+			return (&hw_types[i]);
226
+		}
108
+	}
227
+	}
228
+	return (NULL);
229
+}
109
+
230
+
110
+	return false;
231
+/* From: sys/dev/usb/usb_hid.c */
232
+static int
233
+hid_is_collection(report_desc_t s, uint32_t usage)
234
+{
235
+	struct hid_data *hd;
236
+	struct hid_item hi;
237
+	int rc;
238
+
239
+	hd = hid_start_parse(s, ~0, -1);
240
+	if (hd == NULL)
241
+		return (0);
242
+
243
+	while ((rc = hid_get_item(hd, &hi))) {
244
+		 if (hi.kind == hid_collection &&
245
+		     hi.usage == usage)
246
+			break;
247
+	}
248
+	hid_end_parse(hd);
249
+	return (rc);
111
+}
250
+}
112
+
251
+
113
+static bool
252
+static int
114
+devpath_exists(const struct hw_type *device,
253
+hid_is_mouse(report_desc_t s)
115
+	char *devname, size_t devname_len)
116
+{
254
+{
117
+	char *devpath;
255
+	struct hid_data *hd;
118
+	struct stat st;
256
+	struct hid_item hi;
119
+	int ret;
257
+	int mdepth;
258
+	int found;
120
+
259
+
121
+	if (device == NULL || device->driver == NULL)
260
+	hd = hid_start_parse(s, (1 << hid_input), -1);
122
+		return false;
261
+	if (hd == NULL)
262
+		return (0);
123
+
263
+
124
+	/* Check if /dev/$driver exists. */
264
+	mdepth = 0;
125
+	asprintf(&devpath, "/dev/%s", device->driver);
265
+	found = 0;
126
+	if (devpath == NULL)
127
+		return false;
128
+
266
+
129
+	ret = stat(devpath, &st);
267
+	while (hid_get_item(hd, &hi)) {
130
+	free(devpath);
268
+		switch (hi.kind) {
269
+		case hid_collection:
270
+			if (mdepth != 0)
271
+				mdepth ++;
272
+			else if (hi.collection == 1 &&
273
+			     hi.usage ==
274
+			      HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))
275
+				mdepth ++;
276
+			break;
277
+		case hid_endcollection:
278
+			if (mdepth != 0)
279
+				mdepth --;
280
+			break;
281
+		case hid_input:
282
+			if (mdepth == 0)
283
+				break;
284
+			if (hi.usage ==
285
+			     HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X) &&
286
+			    (hi.flags & (HIO_CONST|HIO_RELATIVE)) == HIO_RELATIVE)
287
+				found ++;
288
+			if (hi.usage ==
289
+			     HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y) &&
290
+			    (hi.flags & (HIO_CONST|HIO_RELATIVE)) == HIO_RELATIVE)
291
+				found ++;
292
+			break;
293
+		default:
294
+			break;
295
+		}
296
+	}
297
+	hid_end_parse(hd);
298
+	return (found);
299
+}
131
+
300
+
132
+	if (ret == 0) {
301
+
133
+		strncpy(devname, device->driver, devname_len);
302
+static hw_type_p
134
+		return true;
303
+get_dev_type_by_path(const char *dev_name, size_t dev_name_size,
304
+    hw_type_p hw_type_cust)
305
+{
306
+	size_t i;
307
+
308
+	if (NULL == dev_name || 0 == dev_name_size || NULL == hw_type_cust)
309
+		return (NULL);
310
+	for (i = 0; NULL != hw_type_path[i].dev_name; i ++) {
311
+		if (dev_name_size <= hw_type_path[i].path_offset ||
312
+		    0 != memcmp(dev_name, hw_type_path[i].dev_name,
313
+		    hw_type_path[i].path_offset))
314
+			continue;
315
+		/* Path in white list. */
316
+		hw_type_cust->dev_name = dev_name;
317
+		hw_type_cust->path_offset = hw_type_path[i].path_offset;
318
+		for (i = hw_type_cust->path_offset; i < dev_name_size; i ++) {
319
+			if (isdigit(dev_name[i]))
320
+				break;
321
+		}
322
+		hw_type_cust->dev_name_size = (i - hw_type_cust->path_offset);
323
+		hw_type_cust->flags = hw_type_path[i].flags;
324
+		hw_type_cust->xdriver = hw_type_path[i].xdriver;
325
+		return (hw_type_cust);
135
+	}
326
+	}
327
+	return (NULL);
328
+}
136
+
329
+
137
+	return false;
330
+static int
331
+is_kbdmux_enabled(void)
332
+{
333
+	/* Xorg uses /dev/ttyv0 as a console device */
334
+	/* const char device[]="/dev/console"; */
335
+	int fd;
336
+	const char *device = _PATH_TTY "v0";
337
+	keyboard_info_t info;
338
+
339
+	fd = open(device, O_RDONLY);
340
+	if (0 > fd)
341
+		return (0);
342
+	if (-1 == ioctl(fd, KDGKBINFO, &info) ||
343
+	    0 != memcmp(info.kb_name, "kbdmux", 6)) {
344
+		close(fd);
345
+		return (0);
346
+	}
347
+	close(fd);
348
+	return (1);
138
+}
349
+}
139
+
350
+
140
+static char *
351
+static char *
141
+sysctl_get_str(const char *sysctlname)
352
+sysctl_get_str(const char *sysctlname, size_t *size_ret)
142
+{
353
+{
143
+	char *dest = NULL;
354
+	char *dest;
144
+	size_t len;
355
+	size_t len;
145
+
356
+
146
+	if (sysctlname == NULL)
357
+	if (NULL == sysctlname)
147
+		return NULL;
358
+		return (NULL);
359
+	if (0 != sysctlbyname(sysctlname, NULL, &len, NULL, 0))
360
+		return (NULL);
361
+	dest = malloc(len + 4);
362
+	if (NULL == dest)
363
+		return (NULL);
364
+	if (0 != sysctlbyname(sysctlname, dest, &len, NULL, 0)) {
365
+		free(dest);
366
+		return (NULL);
367
+	}
368
+	dest[len] = 0;
369
+	if (NULL != size_ret)
370
+		(*size_ret) = len;
371
+	return (dest);
372
+}
148
+
373
+
149
+	if (sysctlbyname(sysctlname, NULL, &len, NULL, 0) == 0) {
374
+static char *
150
+		dest = malloc(len + 1);
375
+devd_get_val(char *buf, size_t buf_size, const char *val_name,
151
+		if (dest) {
376
+    size_t val_name_size, size_t *val_size)
152
+			if (sysctlbyname(sysctlname, dest, &len, NULL, 0) == 0)
377
+{
153
+				dest[len] = '\0';
378
+	char *ret, *buf_end, *ptr;
154
+			else {
379
+
155
+				free(dest);
380
+	buf_end = (buf + buf_size);
156
+				dest = NULL;
381
+	for (ret = buf; ret != NULL && ret < buf_end;) {
157
+			}
382
+		ret = memmem(ret, (buf_end - ret), val_name, val_name_size);
383
+		if (ret == NULL)
384
+			return (NULL);
385
+		/* Found. */
386
+		/* Check: space before or buf+1. */
387
+		if ((buf + 1) < ret && ret[-1] != ' ') {
388
+			ret += val_name_size;
389
+			continue;
158
+		}
390
+		}
391
+		/* Check: = after name and size for value. */
392
+		ret += val_name_size;
393
+		if ((ret + 1) >= buf_end)
394
+			return (NULL);
395
+		if ('=' != ret[0])
396
+			continue;
397
+		ret ++;
398
+		break;
159
+	}
399
+	}
160
+
400
+	if (ret == NULL || val_size == NULL)
161
+	return dest;
401
+		return (ret);
402
+	/* Calc value data size. */
403
+	ptr = memchr(ret, ' ', (buf_end - ret));
404
+	if (ptr == NULL) /* End of string/last value. */
405
+		ptr = buf_end;
406
+	(*val_size) = (ptr - ret);
407
+	return (ret);
162
+}
408
+}
163
+
409
+
164
+static void
410
+static void
165
+device_added(const char *devname)
411
+device_added(const char *dev_name, size_t dev_name_size, int allow_no_device)
166
+{
412
+{
167
+	char path[PATH_MAX];
413
+	int fd;
168
+	char sysctlname[PATH_MAX];
169
+	char *vendor;
170
+	char *product = NULL;
171
+	char *config_info = NULL;
172
+	char *walk;
173
+	InputOption *options = NULL;
414
+	InputOption *options = NULL;
174
+	InputAttributes attrs = { };
415
+	InputAttributes attrs;
175
+	DeviceIntPtr dev = NULL;
416
+	DeviceIntPtr dev_iptr = NULL;
176
+	int i;
417
+	keyboard_info_t kbdi;
177
+	int fd;
418
+	mousehw_t mshw;
419
+	struct input_id iid;
420
+	report_desc_t rep_desc;
421
+	size_t has_keys = 0, has_buttons = 0, has_lmr = 0;
422
+	size_t has_rel_axes = 0, has_abs_axes = 0, has_mt = 0;
423
+	uint32_t key_bits[U32_CNT(KEY_CNT)];
424
+	uint32_t rel_bits[U32_CNT(REL_CNT)];
425
+	uint32_t abs_bits[U32_CNT(ABS_CNT)];
426
+	char *dev_path, config_info[(PATH_MAX + 32)];
427
+	char sysctlname[PATH_MAX], *sdata, *ptr_pid, *ptr_vid;
428
+	char pnp_usb_id[PATH_MAX], vendor[PATH_MAX], product[PATH_MAX];
429
+	size_t sdata_size, pid_size, vid_size;
430
+	hw_type_t *hwtype, hwtype_cust;
431
+	unsigned short vid, pid;
178
+
432
+
179
+	for (i = 0; hw_types[i].driver != NULL; i++) {
180
+		size_t len;
181
+
433
+
182
+		len = strlen(hw_types[i].driver);
434
+	if (NULL == dev_name || 0 == dev_name_size || PATH_MAX < dev_name_size)
183
+		if (strcmp(devname, hw_types[i].driver) == 0 ||
435
+		return;
184
+			(strncmp(devname, hw_types[i].driver, len) == 0 &&
436
+	/* Make dev_name null ended string. */
185
+				isnumber(*(devname + len)))) {
437
+	snprintf(config_info, sizeof(config_info), DEVD_PATH_DEV"%.*s",
186
+			attrs.flags |= hw_types[i].flag;
438
+	    (int)dev_name_size, dev_name);
187
+			break;
439
+	/* Set / update pointers to dev_name and dev_path. */
188
+		}
440
+	dev_path = (config_info + 5); /* Skip: "devd:" */
441
+	dev_name = (dev_path + _PATH_DEV_LEN); /* Skip: "/dev/" */
442
+
443
+	/* Is known input device or path? */
444
+	hwtype = get_dev_type_by_name(dev_name, dev_name_size);
445
+	if (NULL == hwtype) {
446
+		hwtype = get_dev_type_by_path(dev_name, dev_name_size,
447
+		    &hwtype_cust);
189
+	}
448
+	}
190
+
449
+	if (NULL == hwtype) /* Not found in white list. */
191
+	if (hw_types[i].driver == NULL || hw_types[i].xdriver == NULL) {
192
+		LogMessage(X_INFO, "config/devd: ignoring device %s\n",
193
+				devname);
194
+		return;
450
+		return;
195
+	}
451
+	/* Init and set attributes. */
452
+	memset(&attrs, 0, sizeof(attrs));
453
+	attrs.device = dev_path;
454
+	attrs.flags = hwtype->flags;
196
+
455
+
197
+	/* Skip keyboard devices if kbdmux is enabled */
456
+	/* Skip keyboard devices if kbdmux is enabled */
198
+	if (is_kbdmux && is_console_kbd && hw_types[i].flag & ATTR_KEYBOARD) {
457
+	if (is_kbdmux && 0 == allow_no_device && (hwtype->flags & ATTR_KEYBOARD)) {
458
+skip_kbd_dev:
199
+		LogMessage(X_INFO, "config/devd: kbdmux is enabled, ignoring device %s\n",
459
+		LogMessage(X_INFO, "config/devd: kbdmux is enabled, ignoring device %s\n",
200
+				devname);
460
+		    dev_name);
201
+		return;
461
+		return;
202
+	}
462
+	}
203
+
463
+	/* Skip duplicate devices. */
204
+	snprintf(path, sizeof(path), "/dev/%s", devname);
464
+	if (device_is_duplicate(config_info)) {
205
+
465
+		LogMessage(X_WARNING, "config/devd: device %s already added. ignoring\n",
206
+	options = input_option_new(NULL, "_source", "server/devd");
466
+		    dev_path);
207
+	if (!options)
208
+		return;
467
+		return;
209
+
210
+	snprintf(sysctlname, sizeof(sysctlname), "dev.%s.%s.%%desc",
211
+	    hw_types[i].driver, devname + strlen(hw_types[i].driver));
212
+	vendor = sysctl_get_str(sysctlname);
213
+	if (vendor == NULL) {
214
+		options = input_option_new(options, "name", devname);
215
+	}
468
+	}
216
+	else {
217
+		if ((walk = strchr(vendor, ' ')) != NULL) {
218
+			walk[0] = '\0';
219
+			walk++;
220
+			product = walk;
221
+			if ((walk = strchr(product, ',')) != NULL)
222
+				walk[0] = '\0';
223
+		}
224
+
469
+
225
+		attrs.vendor = strdup(vendor);
470
+	/* Try to open device. */
226
+		if (product) {
471
+	fd = open(dev_path, O_RDONLY);
227
+			attrs.product = strdup(product);
472
+	if (0 > fd) {
228
+			options = input_option_new(options, "name", product);
473
+		if (0 == (hwtype->flags & ATTR_KEYBOARD)) {
229
+		}
230
+		else
231
+			options = input_option_new(options, "name", "(unnamed)");
232
+
233
+		free(vendor);
234
+	}
235
+
236
+	/* XXX implement usb_id */
237
+	attrs.usb_id = NULL;
238
+	attrs.device = strdup(path);
239
+	options = input_option_new(options, "driver", hw_types[i].xdriver);
240
+
241
+	fd = open(path, O_RDONLY);
242
+	if (fd > 0) {
243
+		close(fd);
244
+		options = input_option_new(options, "device", path);
245
+	}
246
+	else {
247
+		if (attrs.flags & ~ATTR_KEYBOARD) {
248
+			LogMessage(X_INFO, "config/devd: device %s already opened\n",
474
+			LogMessage(X_INFO, "config/devd: device %s already opened\n",
249
+					 path);
475
+			    dev_path);
250
+
251
+			/*
476
+			/*
252
+			 * Fail if cannot open device, it breaks AllowMouseOpenFail,
477
+			 * Fail if cannot open device, it breaks AllowMouseOpenFail,
253
+			 * but it should not matter when config/devd enabled
478
+			 * but it should not matter when config/devd enabled
254
+			 */
479
+			 */
255
+			goto unwind;
480
+			return;
256
+		}
481
+		}
257
+
482
+		if (0 == allow_no_device) {
258
+		if (is_console_kbd) {
259
+			/*
483
+			/*
260
+			 * There can be only one keyboard attached to console and
484
+			 * There can be only one keyboard attached to console and
261
+			 * it is already added.
485
+			 * it is already added.
262
+			 */
486
+			 */
263
+			LogMessage(X_WARNING, "config/devd: console keyboard is "
487
+			LogMessage(X_WARNING, "config/devd: console keyboard is "
264
+					"already added, ignoring %s (%s)\n",
488
+			    "already added, ignoring %s\n",
265
+					attrs.product, path);
489
+			    dev_path);
266
+			goto unwind;
490
+			return;
267
+		}
491
+		}
268
+		else
492
+		/*
269
+			/*
493
+		 * Don't pass "device" option if the keyboard is already
270
+			 * Don't pass "device" option if the keyboard is already
494
+		 * attached to the console (ie. open() fails).
271
+			 * attached to the console (ie. open() fails).
495
+		 * This would activate a special logic in xf86-input-keyboard.
272
+			 * This would activate a special logic in xf86-input-keyboard.
496
+		 * Prevent any other attached to console keyboards being
273
+			 * Prevent any other attached to console keyboards being
497
+		 * processed. There can be only one such device.
274
+			 * processed. There can be only one such device.
498
+		 */
275
+			 */
499
+		goto skip_ioctl;
276
+			is_console_kbd = true;
277
+	}
500
+	}
278
+
501
+
279
+	if (asprintf(&config_info, "devd:%s", devname) == -1) {
502
+	/* Try to get device info via ioctl(). */
280
+		config_info = NULL;
503
+	if (-1 != ioctl(fd, KDGKBINFO, &kbdi)) { /* Is this keyboard? */
281
+		goto unwind;
504
+		memcpy(product, kbdi.kb_name, sizeof(kbdi.kb_name));
505
+		attrs.product = product;
506
+		attrs.flags = ATTR_KEYBOARD;
507
+		LogMessage(X_INFO, "config/devd: detected keyboard: %s, kb_index=%i, kb_unit=%i, kb_type=%i, kb_config=%i\n",
508
+		    kbdi.kb_name, kbdi.kb_index, kbdi.kb_unit, kbdi.kb_type, kbdi.kb_config);
509
+	} else if (-1 != ioctl(fd, MOUSE_GETHWINFO, &mshw)) { /* Is this mouse? */
510
+#ifdef notyet /* FreeBSD mouse drivers does not return real vid+pid. */
511
+		/* construct USB ID in lowercase hex - "0000:ffff" */
512
+		snprintf(pnp_usb_id, sizeof(pnp_usb_id), "%04x:%04x",
513
+		    mshw.hwid, mshw.model);
514
+		attrs.usb_id = pnp_usb_id;
515
+#endif
516
+
517
+		switch (mshw.type) {
518
+		case MOUSE_MOUSE:
519
+		case MOUSE_TRACKBALL:
520
+		case MOUSE_STICK:
521
+			attrs.flags = ATTR_POINTER;
522
+			break;
523
+		case MOUSE_PAD:
524
+			attrs.flags = ATTR_TOUCHPAD;		
525
+			break;
526
+		}
527
+		LogMessage(X_INFO, "config/devd: detected mouse: hwid=%04x, model=%04x, type=%04x, iftype=%04x, buttons=%04x\n",
528
+		    mshw.hwid, mshw.model, mshw.type, mshw.iftype, mshw.buttons);
529
+	} else if (-1 != ioctl(fd, JSIOCGNAME((sizeof(product) - 1)), product)) { /* Is this joystick? */
530
+		attrs.product = product;
531
+		attrs.flags = ATTR_JOYSTICK;
532
+		LogMessage(X_INFO, "config/devd: detected joystick: %s\n",
533
+		    product);
534
+	} else if (-1 != ioctl(fd, EVIOCGID, &iid) &&
535
+	    -1 != ioctl(fd, EVIOCGNAME((sizeof(product) - 1)), product)) { /* Is this event? */
536
+		/* construct USB ID in lowercase hex - "0000:ffff" */
537
+		snprintf(pnp_usb_id, sizeof(pnp_usb_id), "%04x:%04x",
538
+		    iid.vendor, iid.product);
539
+		attrs.usb_id = pnp_usb_id;
540
+		attrs.product = product;
541
+
542
+		/* Detect device type. */
543
+		if (-1 != ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)), key_bits)) {
544
+			has_keys = bits_calc(key_bits, 0, BTN_MISC);
545
+			has_buttons = bits_calc(key_bits, BTN_MISC, BTN_JOYSTICK);
546
+			has_lmr = bits_calc(key_bits, BTN_LEFT, BTN_MIDDLE);
547
+		}
548
+		if (-1 != ioctl(fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)), rel_bits)) {
549
+			has_rel_axes = bits_calc(rel_bits, 0, REL_MAX);
550
+		}
551
+		if (-1 != ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)), abs_bits)) {
552
+			has_abs_axes = bits_calc(abs_bits, 0, ABS_MAX);
553
+			has_mt = bits_calc(abs_bits, ABS_MT_SLOT, ABS_MAX);
554
+		}
555
+
556
+		if (has_abs_axes) {
557
+			if (has_mt) {
558
+				if (0 == has_buttons) {
559
+					/*
560
+					 * XXX: I'm not sure that joystick detection is
561
+					 * done right. xf86-evdev does not support them.
562
+					 */
563
+					if (U32_IS_BIT_SET(key_bits, BTN_JOYSTICK)) {
564
+						attrs.flags = ATTR_JOYSTICK;
565
+						goto event_done;
566
+					} else {
567
+						has_buttons ++;
568
+					}
569
+				}
570
+			}
571
+			if (U32_IS_BIT_SET(abs_bits, ABS_X) &&
572
+			    U32_IS_BIT_SET(abs_bits, ABS_Y)) {
573
+				if (U32_IS_BIT_SET(key_bits, BTN_TOOL_PEN) ||
574
+				    U32_IS_BIT_SET(key_bits, BTN_STYLUS) ||
575
+				    U32_IS_BIT_SET(key_bits, BTN_STYLUS2)) {
576
+					attrs.flags = ATTR_TABLET;
577
+					goto event_done;
578
+				} else if (U32_IS_BIT_SET(abs_bits, ABS_PRESSURE) ||
579
+					   U32_IS_BIT_SET(key_bits, BTN_TOUCH)) {
580
+					if (has_lmr ||
581
+					    U32_IS_BIT_SET(key_bits, BTN_TOOL_FINGER)) {
582
+						attrs.flags = ATTR_TOUCHPAD;
583
+					} else {
584
+						attrs.flags = ATTR_TOUCHSCREEN;
585
+					}
586
+					goto event_done;
587
+				} else if (!(U32_IS_BIT_SET(rel_bits, REL_X) &&
588
+					     U32_IS_BIT_SET(rel_bits, REL_Y)) &&
589
+					    has_lmr) {
590
+					/* some touchscreens use BTN_LEFT rather than BTN_TOUCH */
591
+					attrs.flags = ATTR_TOUCHSCREEN;
592
+					goto event_done;
593
+				}
594
+			}
595
+		}
596
+		if (has_keys) {
597
+			if (is_kbdmux) {
598
+				close(fd);
599
+				goto skip_kbd_dev;
600
+			}
601
+			attrs.flags = ATTR_KEYBOARD;
602
+		} else if (has_rel_axes || has_abs_axes || has_buttons) {
603
+			attrs.flags = ATTR_POINTER;			
604
+		}
605
+event_done:
606
+		LogMessage(X_INFO, "config/devd: detected event: %s, bustype=%04x, vendor=%04x, product=%04x, version=%04x\n",
607
+		    product, iid.bustype, iid.vendor, iid.product, iid.version);
608
+	} else if (0 != (rep_desc = hid_get_report_desc(fd))) { /* Is USB HID? */
609
+		if (hid_is_mouse(rep_desc)) {
610
+			attrs.flags = ATTR_POINTER;
611
+		} else if (hid_is_collection(rep_desc, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) {
612
+			/* Skip keyboard devices if kbdmux is enabled */
613
+			if (is_kbdmux) {
614
+				hid_dispose_report_desc(rep_desc);
615
+				close(fd);
616
+				goto skip_kbd_dev;
617
+			}
618
+			attrs.flags = ATTR_KEYBOARD;
619
+		} else if (hid_is_collection(rep_desc, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_JOYSTICK)) ||
620
+		    hid_is_collection(rep_desc, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_GAME_PAD))) {
621
+			attrs.flags = ATTR_JOYSTICK;
622
+		}
623
+
624
+		hid_dispose_report_desc(rep_desc);
625
+		LogMessage(X_INFO, "config/devd: detected USB HID...\n");
282
+	}
626
+	}
283
+
627
+
284
+	if (device_is_duplicate(config_info)) {
628
+	if (NULL == attrs.usb_id) { /* Is this webcamd device? */
285
+		LogMessage(X_WARNING, "config/devd: device %s (%s) already added. "
629
+		if (-1 != ioctl(fd, WEBCAMD_IOCTL_GET_USB_VENDOR_ID, &vid) &&
286
+				"ignoring\n", attrs.product, path);
630
+		    -1 != ioctl(fd, WEBCAMD_IOCTL_GET_USB_PRODUCT_ID, &pid)) {
287
+		goto unwind;
631
+			snprintf(pnp_usb_id, sizeof(pnp_usb_id),
632
+			    "%04x:%04x", vid, pid);
633
+			attrs.usb_id = pnp_usb_id;
634
+			LogMessage(X_INFO, "config/devd: WebCamD device: %s\n", pnp_usb_id);
635
+		}
288
+	}
636
+	}
289
+
637
+
638
+	close(fd);
639
+
640
+skip_ioctl:
641
+	/* Try to get device info via sysctl(). */
642
+	if (NULL == attrs.usb_id && NULL == attrs.pnp_id) {
643
+		snprintf(sysctlname, sizeof(sysctlname), "dev.%.*s.%s.%%pnpinfo",
644
+		    (int)hwtype->dev_name_size, (hwtype->dev_name + hwtype->path_offset),
645
+		    (dev_name + hwtype->path_offset + hwtype->dev_name_size));
646
+		sdata = sysctl_get_str(sysctlname, &sdata_size);
647
+		if (NULL != sdata) {
648
+			ptr_vid = devd_get_val_cstr("vendor",
649
+			    sdata, sdata_size, &vid_size);
650
+			ptr_pid = devd_get_val_cstr("product",
651
+			    sdata, sdata_size, &pid_size);
652
+			if (NULL != ptr_vid && NULL != ptr_pid) { /* usb_id */
653
+				ptr_vid[vid_size] = 0;
654
+				ptr_pid[pid_size] = 0;
655
+				snprintf(pnp_usb_id, sizeof(pnp_usb_id),
656
+				    "%s:%s", ptr_vid, ptr_pid);
657
+				attrs.usb_id = pnp_usb_id;
658
+				LogMessage(X_INFO, "config/devd: [sysctl] usb_id: %s\n", pnp_usb_id);
659
+			} else { /* pnp_id */
660
+				strlcpy(pnp_usb_id, sdata, sizeof(pnp_usb_id));
661
+				attrs.pnp_id = pnp_usb_id;
662
+			}
663
+			free(sdata);
664
+		}
665
+	}
666
+	if (NULL == attrs.vendor || NULL == attrs.product) {
667
+		snprintf(sysctlname, sizeof(sysctlname), "dev.%.*s.%s.%%desc",
668
+		    (int)hwtype->dev_name_size, (hwtype->dev_name + hwtype->path_offset),
669
+		    (dev_name + hwtype->path_offset + hwtype->dev_name_size));
670
+		sdata = sysctl_get_str(sysctlname, &sdata_size);
671
+		if (NULL != sdata) {
672
+			/* Vendor. */
673
+			ptr_pid = memchr(sdata, ' ', sdata_size);
674
+			if (NULL != ptr_pid)
675
+				ptr_pid[0] = 0;
676
+			strlcpy(vendor, sdata, sizeof(vendor));
677
+			attrs.vendor = vendor;
678
+			/* Product. */
679
+			if (NULL == attrs.product && NULL != ptr_pid) {
680
+				ptr_pid ++;
681
+				ptr_vid = memchr(ptr_pid, ',',
682
+				    (sdata_size - (ptr_pid - sdata)));
683
+				if (NULL != ptr_vid)
684
+					ptr_vid[0] = 0;
685
+				strlcpy(product, ptr_pid, sizeof(product));
686
+				attrs.product = product;
687
+			} else {
688
+				product[0] = 0;
689
+			}
690
+			free(sdata);
691
+			LogMessage(X_INFO, "config/devd: [sysctl] vendor: %s, product: %s\n", vendor, product);
692
+		}
693
+	}
694
+
695
+	/* Init options. */
696
+	options = input_option_new(NULL, "_source", "server/devd");
697
+	if (NULL == options)
698
+		goto err_out;
699
+
290
+	options = input_option_new(options, "config_info", config_info);
700
+	options = input_option_new(options, "config_info", config_info);
291
+	LogMessage(X_INFO, "config/devd: adding input device %s (%s)\n",
701
+	if (NULL == options)
292
+			attrs.product, path);
702
+		goto err_out;
293
+
703
+
294
+	NewInputDeviceRequest(options, &attrs, &dev);
704
+	options = input_option_new(options, "name",
705
+	    ((NULL != attrs.product) ? attrs.product : dev_name));
706
+	if (NULL == options)
707
+		goto err_out;
295
+
708
+
296
+unwind:
709
+	if (fd >= 0) {
297
+	free(config_info);
710
+		options = input_option_new(options, "device", dev_path);
711
+		if (NULL == options)
712
+			goto err_out;
713
+	}
714
+
715
+	if (NULL != hwtype->xdriver) {
716
+		options = input_option_new(options, "driver", hwtype->xdriver);
717
+		if (NULL == options)
718
+			goto err_out;
719
+	}
720
+
721
+	LogMessage(X_INFO, "config/devd: adding input device %s\n", dev_path);
722
+	NewInputDeviceRequest(options, &attrs, &dev_iptr);
723
+	goto done;
724
+
725
+err_out:
726
+	LogMessage(X_INFO, "config/devd: error adding device '%s'\n",
727
+	    dev_path);
728
+done:
298
+	input_option_free_list(&options);
729
+	input_option_free_list(&options);
299
+	free(attrs.usb_id);
300
+	free(attrs.product);
301
+	free(attrs.device);
302
+	free(attrs.vendor);
303
+}
730
+}
304
+
731
+
305
+static void
732
+static void
306
+device_removed(char *devname)
733
+device_removed(const char *dev_name, size_t dev_name_size)
307
+{
734
+{
308
+	char *config_info;
735
+	char config_info[(PATH_MAX + 32)];
736
+	hw_type_t hwtype_cust;
309
+
737
+
310
+	if (asprintf(&config_info, "devd:%s", devname) == -1)
738
+	if (NULL == dev_name || 0 == dev_name_size || PATH_MAX < dev_name_size)
311
+		return;
739
+		return;
312
+
740
+	if (NULL == get_dev_type_by_name(dev_name, dev_name_size) &&
741
+	    NULL == get_dev_type_by_path(dev_name, dev_name_size, &hwtype_cust))
742
+		return; /* Device not in list - unknown. */
743
+	snprintf(config_info, sizeof(config_info), DEVD_PATH_DEV"%.*s",
744
+	    (int)dev_name_size, dev_name);
745
+	/* Skip non added devices. */
746
+	if (device_is_duplicate(config_info)) {
747
+		LogMessage(X_INFO, "config/devd: removing input device '%s'\n",
748
+		    (config_info + DEVD_PATH_DEV_LEN));
749
+	}
313
+	remove_devices("devd", config_info);
750
+	remove_devices("devd", config_info);
314
+
315
+	free(config_info);
316
+}
751
+}
317
+
752
+
318
+static bool is_kbdmux_enabled(void)
319
+{
320
+	/* Xorg uses /dev/ttyv0 as a console device */
321
+	/* const char device[]="/dev/console"; */
322
+	const char device[]="/dev/ttyv0";
323
+	keyboard_info_t info;
324
+	int fd;
325
+
753
+
326
+	fd = open(device, O_RDONLY);
327
+
328
+	if (fd < 0)
329
+		return false;
330
+
331
+	if (ioctl(fd, KDGKBINFO, &info) == -1) {
332
+		close(fd);
333
+		return false;
334
+	}
335
+
336
+	close(fd);
337
+
338
+	if (!strncmp(info.kb_name, "kbdmux", 6))
339
+		return true;
340
+
341
+	return false;
342
+}
343
+
344
+static void
754
+static void
345
+disconnect_devd(int sock)
755
+disconnect_devd(int sock)
346
+{
756
+{
347
+	if (sock >= 0) {
757
+	if (0 > sock)
348
+		RemoveGeneralSocket(sock);
758
+		return;
349
+		close(sock);
759
+	RemoveGeneralSocket(sock);
350
+	}
760
+	close(sock);
351
+}
761
+}
352
+
762
+
353
+static int
763
+static int
354
+connect_devd(void)
764
+connect_devd(void)
355
+{
765
+{
766
+	int sock;
356
+	struct sockaddr_un devd;
767
+	struct sockaddr_un devd;
357
+	int sock;
358
+
768
+
359
+	sock = socket(AF_UNIX, SOCK_STREAM, 0);
769
+	sock = socket(AF_UNIX, SOCK_STREAM, 0);
360
+	if (sock < 0) {
770
+	if (0 > sock) {
361
+		LogMessage(X_ERROR, "config/devd: fail opening stream socket\n");
771
+		LogMessage(X_ERROR, "config/devd: fail opening stream socket\n");
362
+		return -1;
772
+		return (-1);
363
+	}
773
+	}
364
+
774
+
365
+	devd.sun_family = AF_UNIX;
775
+	devd.sun_family = AF_UNIX;
366
+	strlcpy(devd.sun_path, DEVD_SOCK_PATH, sizeof(devd.sun_path));
776
+	memcpy(devd.sun_path, DEVD_SOCK_PATH, sizeof(DEVD_SOCK_PATH));
367
+
777
+	if (0 > connect(sock, (struct sockaddr*)&devd, sizeof(devd))) {
368
+	if (connect(sock, (struct sockaddr *) &devd, sizeof(devd)) < 0) {
369
+		close(sock);
778
+		close(sock);
370
+		LogMessage(X_ERROR, "config/devd: fail to connect to devd\n");
779
+		LogMessage(X_ERROR, "config/devd: fail to connect to devd\n");
371
+		return -1;
780
+		return (-1);
372
+	}
781
+	}
373
+
782
+
374
+	AddGeneralSocket(sock);
783
+	AddGeneralSocket(sock);
375
+
784
+
376
+	return	sock;
785
+	return (sock);
377
+}
786
+}
378
+
787
+
379
+static CARD32
788
+static CARD32
380
+reconnect_handler(OsTimerPtr timer, CARD32 time, void *arg)
789
+reconnect_handler(OsTimerPtr timer, CARD32 time, void *arg)
381
+{
790
+{
382
+	int newsock;
791
+	devd_buf_used = 0;
792
+	devd_skt = connect_devd();
793
+	if (-1 == devd_skt) /* Try again after RECONNECT_DELAY */
794
+		return (RECONNECT_DELAY);
795
+	TimerFree(rtimer);
796
+	rtimer = NULL;
797
+	LogMessage(X_INFO, "config/devd: reopening devd socket\n");
383
+
798
+
384
+	if ((newsock = connect_devd()) > 0) {
799
+	return (0);
385
+		sock_devd = newsock;
386
+		TimerFree(rtimer);
387
+		rtimer = NULL;
388
+		LogMessage(X_INFO, "config/devd: reopening devd socket\n");
389
+		return 0;
390
+	}
391
+
392
+	/* Try again after RECONNECT_DELAY */
393
+	return RECONNECT_DELAY;
394
+}
800
+}
395
+
801
+
396
+static ssize_t
802
+static void
397
+socket_getline(int fd, char **out)
803
+wakeup_handler(void *data, int err, void *read_mask)
398
+{
804
+{
399
+	char *buf, *newbuf;
805
+	int error;
400
+	ssize_t ret, cap, sz = 0;
806
+	char *ptr, *line, *val, *cdev;
401
+	char c;
807
+	size_t line_size, val_size, cdev_size;
808
+	ssize_t ios;
402
+
809
+
403
+	cap = 1024;
810
+	if (0 > err)
404
+	buf = malloc(cap * sizeof(char));
811
+		return;
405
+	if (!buf)
812
+	if (!FD_ISSET(devd_skt, (fd_set*)read_mask))
406
+		return -1;
813
+		return;
407
+
814
+	/* Read new data. */
408
+	for (;;) {
815
+	for (;;) {
409
+		ret = read(sock_devd, &c, 1);
816
+		ios = recv(devd_skt, (devd_buf + devd_buf_used), 
410
+		if (ret < 0) {
817
+		    (sizeof(devd_buf) - devd_buf_used), MSG_DONTWAIT);
411
+			if (errno == EINTR)
818
+		if (0 < ios) { /* Read OK. */
412
+				continue;
819
+			devd_buf_used += ios;
413
+			free(buf);
820
+			continue; /* Try to read more. */
414
+			return -1;
415
+		/* EOF - devd socket is lost */
416
+		} else if (ret == 0) {
417
+			disconnect_devd(sock_devd);
418
+			rtimer = TimerSet(NULL, 0, 1, reconnect_handler, NULL);
419
+			LogMessage(X_WARNING, "config/devd: devd socket is lost\n");
420
+			free(buf);
421
+			return -1;
422
+		}
821
+		}
423
+		if (c == '\n')
822
+		/* Something wrong. */
424
+			break;
823
+		error = errno;
425
+
824
+		if (error == EAGAIN)
426
+		if (sz + 1 >= cap) {
825
+			break; /* All avaible data readed. */
427
+			cap *= 2;
826
+		if (error == EINTR)
428
+			newbuf = realloc(buf, cap * sizeof(char));
827
+			continue;
429
+			if (!newbuf) {
828
+		if (sizeof(devd_buf) == devd_buf_used) { /* Message to long, reset buf. */
430
+				free(buf);
829
+			devd_buf_used = 0;
431
+				return -1;
830
+			continue;
432
+			}
433
+			buf = newbuf;
434
+		}
831
+		}
435
+		buf[sz] = c;
832
+		/* devd socket is lost */
436
+		sz++;
833
+		disconnect_devd(devd_skt);
834
+		rtimer = TimerSet(NULL, 0, 1, reconnect_handler, NULL);
835
+		LogMessage(X_WARNING, "config/devd: devd socket read error: %i - %s\n", error, strerror(error));
836
+		return;
437
+	}
837
+	}
838
+	ptr = memchr(devd_buf, '\n', devd_buf_used);
839
+	if (NULL == ptr)
840
+		return;
438
+
841
+
439
+	buf[sz] = '\0';
842
+	/* Process data. */
440
+	if (sz >= 0)
843
+	line = (devd_buf + 1);
441
+		*out = buf;
844
+	for (;;) {
442
+	else
845
+		line_size = (ptr - line);
443
+		free(buf);
846
+		if (DEVD_EVENT_NOTIFY != (*(line - 1)))
847
+			goto move_next_event; /* Handle only notify. */
848
+		/* Check: is system=DEVFS. */
849
+		val = devd_get_val_cstr("system", line, line_size, &val_size);
850
+		if (!is_meuqual_cstr("DEVFS", val, val_size))
851
+			goto move_next_event;
852
+		/* Check: is subsystem=CDEV. */
853
+		val = devd_get_val_cstr("subsystem", line, line_size, &val_size);
854
+		if (!is_meuqual_cstr("CDEV", val, val_size))
855
+			goto move_next_event;
856
+		/* Get device name. */
857
+		cdev = devd_get_val_cstr("cdev", line, line_size, &cdev_size);
858
+		if (NULL == cdev)
859
+			goto move_next_event;
860
+		/* Get event type. */
861
+		val = devd_get_val_cstr("type", line, line_size, &val_size);
862
+		if (is_meuqual_cstr("CREATE", val, val_size)) {
863
+			device_added(cdev, cdev_size, 0);
864
+		} else if (is_meuqual_cstr("DESTROY", val, val_size)) {
865
+			device_removed(cdev, cdev_size);
866
+		}
444
+
867
+
445
+	/* Number of bytes in the line, not counting the line break */
868
+move_next_event:
446
+	return sz;
869
+		line += (line_size + 2); /* Skip '\n' and event type byte. */
447
+}
870
+		line_size = (line - devd_buf);
448
+
871
+		if (devd_buf_used <= line_size) {
449
+static void
872
+			devd_buf_used = 0;
450
+wakeup_handler(void *data, int err, void *read_mask)
451
+{
452
+	char *line = NULL;
453
+	char *walk;
454
+
455
+	if (err < 0)
456
+		return;
457
+
458
+	if (FD_ISSET(sock_devd, (fd_set *) read_mask)) {
459
+		if (socket_getline(sock_devd, &line) < 0)
460
+			return;
873
+			return;
461
+
874
+		}
462
+		walk = strchr(line + 1, ' ');
875
+		ptr = memchr(line, '\n', (devd_buf_used - line_size));
463
+		if (walk != NULL)
876
+		if (NULL == ptr)
464
+			walk[0] = '\0';
465
+
466
+		switch (*line) {
467
+		case DEVD_EVENT_ADD:
468
+			device_added(line + 1);
469
+			break;
877
+			break;
470
+		case DEVD_EVENT_REMOVE:
471
+			device_removed(line + 1);
472
+			break;
473
+		default:
474
+			break;
475
+		}
476
+		free(line);
477
+	}
878
+	}
879
+	/* Save line without end marker. */
880
+	devd_buf_used -= (line_size - 1);
881
+	memmove(devd_buf, (line - 1), devd_buf_used);
478
+}
882
+}
479
+
883
+
480
+static void
884
+static void
Lines 485-492 Link Here
485
+int
889
+int
486
+config_devd_init(void)
890
+config_devd_init(void)
487
+{
891
+{
488
+	char devicename[1024];
892
+	size_t i, j, dir_cnt, sdir_cnt, tm;
489
+	int i, j;
893
+	char devicename[PATH_MAX];
894
+	struct dirent *de, **namelist, *sde, **snamelist;
490
+
895
+
491
+	LogMessage(X_INFO, "config/devd: probing input devices...\n");
896
+	LogMessage(X_INFO, "config/devd: probing input devices...\n");
492
+
897
+
Lines 494-520 Link Here
494
+	 * Add fake keyboard and give up on keyboards management
899
+	 * Add fake keyboard and give up on keyboards management
495
+	 * if kbdmux is enabled
900
+	 * if kbdmux is enabled
496
+	 */
901
+	 */
497
+	if ((is_kbdmux = is_kbdmux_enabled()) == true)
902
+	is_kbdmux = is_kbdmux_enabled();
498
+		device_added("kbdmux");
903
+	if (is_kbdmux)
904
+		device_added("kbdmux0", 7, 1);
499
+
905
+
500
+	for (i = 0; hw_types[i].driver != NULL; i++) {
906
+	/* Scan /dev/ for devices. */
501
+		/* First scan the sysctl to determine the hardware */
907
+	dir_cnt = scandir(_PATH_DEV, &namelist, 0, alphasort);
502
+		for (j = 0; j < 16; j++) {
908
+	for (i = 0; i < dir_cnt; i ++) {
503
+			if (sysctl_exists(&hw_types[i], j,
909
+		de = namelist[i];
504
+					devicename, sizeof(devicename)) != 0)
910
+		if (is_de_euqual_cstr(de, ".") ||
505
+				device_added(devicename);
911
+		    is_de_euqual_cstr(de, "..")) {
912
+			free(de);
913
+			continue;
506
+		}
914
+		}
507
+
915
+		if (DT_DIR != de->d_type) {
508
+		if (devpath_exists(&hw_types[i], devicename, sizeof(devicename)) != 0)
916
+			device_added(de->d_name, de->d_namlen, 0);
509
+			device_added(devicename);
917
+		} else { /* Sub folder. */
918
+			snprintf(devicename, sizeof(devicename),
919
+			    _PATH_DEV "%s", de->d_name);
920
+			sdir_cnt = scandir(devicename, &snamelist, 0, alphasort);
921
+			for (j = 0; j < sdir_cnt; j ++) {
922
+				sde = snamelist[j];
923
+				if (!is_de_euqual_cstr(sde, ".") &&
924
+				    !is_de_euqual_cstr(sde, "..") &&
925
+				    DT_DIR != sde->d_type) {
926
+					tm = snprintf(devicename, sizeof(devicename),
927
+					    "%s/%s", de->d_name, sde->d_name);
928
+					device_added(devicename, tm, 0);
929
+				}
930
+				free(sde);
931
+			}
932
+			free(snamelist);
933
+		}
934
+		free(de);
510
+	}
935
+	}
936
+	free(namelist);
511
+
937
+
512
+	if ((sock_devd = connect_devd()) < 0)
938
+	devd_buf_used = 0;
513
+		return 0;
939
+	devd_skt = connect_devd();
940
+	if (-1 == devd_skt)
941
+		return (0);
514
+
942
+
943
+	/* Register wakeup handler */
515
+	RegisterBlockAndWakeupHandlers(block_handler, wakeup_handler, NULL);
944
+	RegisterBlockAndWakeupHandlers(block_handler, wakeup_handler, NULL);
516
+
945
+
517
+	return 1;
946
+	return (1);
518
+}
947
+}
519
+
948
+
520
+void
949
+void
Lines 527-535 Link Here
527
+		rtimer = NULL;
956
+		rtimer = NULL;
528
+	}
957
+	}
529
+
958
+
530
+	disconnect_devd(sock_devd);
959
+	disconnect_devd(devd_skt);
531
+
960
+
532
+	RemoveBlockAndWakeupHandlers(block_handler, wakeup_handler, NULL);
961
+	RemoveBlockAndWakeupHandlers(block_handler, wakeup_handler, NULL);
533
+
534
+	is_console_kbd = false;
535
+}
962
+}
(-)x11-servers/xorg-server/files/patch-configure (-2 / +101 lines)
Lines 1-6 Link Here
1
--- configure.orig	2016-07-19 17:27:31 UTC
1
--- configure.orig	2016-07-19 17:27:31 UTC
2
+++ configure
2
+++ configure
3
@@ -23168,9 +23168,14 @@ $as_echo "#define USE_ALPHA_PIO 1" >>con
3
@@ -1030,6 +1030,8 @@ CONFIG_UDEV_FALSE
4
 CONFIG_UDEV_TRUE
5
 UDEV_LIBS
6
 UDEV_CFLAGS
7
+CONFIG_DEVD_FALSE
8
+CONFIG_DEVD_TRUE
9
 HAVE_SYSTEMD_DAEMON_FALSE
10
 HAVE_SYSTEMD_DAEMON_TRUE
11
 SYSTEMD_DAEMON_LIBS
12
@@ -1364,6 +1366,7 @@ enable_config_udev
13
 enable_config_udev_kms
14
 enable_config_hal
15
 enable_config_wscons
16
+enable_config_devd
17
 enable_xfree86_utils
18
 enable_vgahw
19
 enable_vbe
20
@@ -2189,6 +2192,7 @@ Optional Features:
21
                           Build udev kms support (default: auto)
22
   --disable-config-hal    Build HAL support (default: auto)
23
   --enable-config-wscons  Build wscons config support (default: auto)
24
+  --enable-config-devd    Build devd support (default: auto)
25
   --enable-xfree86-utils  Build xfree86 DDX utilities (default: enabled)
26
   --enable-vgahw          Build Xorg with vga access (default: enabled)
27
   --enable-vbe            Build Xorg with VBE module (default: enabled)
28
@@ -23168,9 +23172,14 @@ $as_echo "#define USE_ALPHA_PIO 1" >>con
4
 	esac
29
 	esac
5
 	GLX_ARCH_DEFINES="-D__GLX_ALIGN64 -mieee"
30
 	GLX_ARCH_DEFINES="-D__GLX_ALIGN64 -mieee"
6
 	;;
31
 	;;
Lines 16-22 Link Here
16
 	;;
41
 	;;
17
   i*86)
42
   i*86)
18
 	I386_VIDEO=yes
43
 	I386_VIDEO=yes
19
@@ -26057,7 +26062,7 @@ fi
44
@@ -23291,6 +23300,9 @@ $as_echo "#define PCVT_SUPPORT 1" >>conf
45
 
46
 $as_echo "#define SYSCONS_SUPPORT 1" >>confdefs.h
47
 
48
+
49
+$as_echo "#define CONFIG_DEVD 1" >>confdefs.h
50
+
51
 	DRI=yes
52
 	;;
53
   *netbsd*)
54
@@ -23968,6 +23980,13 @@ else
55
   CONFIG_WSCONS=auto
56
 fi
57
 
58
+# Check whether --enable-config-devd was given.
59
+if test "${enable_config_devd+set}" = set; then :
60
+  enableval=$enable_config_devd; CONFIG_DEVD=$enableval
61
+else
62
+  CONFIG_DEVD=auto
63
+fi
64
+
65
 # Check whether --enable-xfree86-utils was given.
66
 if test "${enable_xfree86_utils+set}" = set; then :
67
   enableval=$enable_xfree86_utils; XF86UTILS=$enableval
68
@@ -24681,6 +24700,7 @@ case $host_os in
69
 		CONFIG_HAL=no
70
 		CONFIG_UDEV=no
71
 		CONFIG_UDEV_KMS=no
72
+		CONFIG_DEVD=no
73
 		DGA=no
74
 		DRM=no
75
 		DRI2=no
76
@@ -25157,6 +25177,30 @@ else
77
 fi
78
 
79
 
80
+if test "x$CONFIG_DEVD" = xauto; then
81
+	case $host_os in
82
+		*freebsd*)
83
+			CONFIG_DEVD=yes;
84
+			;;
85
+		*)
86
+			CONFIG_DEVD=no;
87
+			;;
88
+	esac
89
+fi
90
+ if test "x$CONFIG_DEVD" = xyes; then
91
+  CONFIG_DEVD_TRUE=
92
+  CONFIG_DEVD_FALSE='#'
93
+else
94
+  CONFIG_DEVD_TRUE='#'
95
+  CONFIG_DEVD_FALSE=
96
+fi
97
+
98
+if test "x$CONFIG_DEVD" = xyes; then
99
+
100
+$as_echo "#define CONFIG_DEVD 1" >>confdefs.h
101
+
102
+fi
103
+
104
 if test "x$CONFIG_UDEV" = xyes && test "x$CONFIG_HAL" = xyes; then
105
 	as_fn_error $? "Hotplugging through both libudev and hal not allowed" "$LINENO" 5
106
 fi
107
@@ -26057,7 +26101,7 @@ fi
20
 case "x$XTRANS_SEND_FDS" in
108
 case "x$XTRANS_SEND_FDS" in
21
 xauto)
109
 xauto)
22
 	case "$host_os" in
110
 	case "$host_os" in
Lines 25-27 Link Here
25
 		XTRANS_SEND_FDS=yes
113
 		XTRANS_SEND_FDS=yes
26
 		;;
114
 		;;
27
 	*)
115
 	*)
116
@@ -32398,6 +32442,10 @@ if test -z "${HAVE_SYSTEMD_DAEMON_TRUE}"
117
   as_fn_error $? "conditional \"HAVE_SYSTEMD_DAEMON\" was never defined.
118
 Usually this means the macro was only invoked conditionally." "$LINENO" 5
119
 fi
120
+if test -z "${CONFIG_DEVD_TRUE}" && test -z "${CONFIG_DEVD_FALSE}"; then
121
+  as_fn_error $? "conditional \"CONFIG_DEVD\" was never defined.
122
+Usually this means the macro was only invoked conditionally." "$LINENO" 5
123
+fi
124
 if test -z "${CONFIG_UDEV_TRUE}" && test -z "${CONFIG_UDEV_FALSE}"; then
125
   as_fn_error $? "conditional \"CONFIG_UDEV\" was never defined.
126
 Usually this means the macro was only invoked conditionally." "$LINENO" 5
(-)x11-servers/xorg-server/files/patch-configure.ac (+61 lines)
Line 0 Link Here
1
--- configure.ac.orig	2016-07-19 17:27:07 UTC
2
+++ configure.ac
3
@@ -325,6 +325,11 @@ case $host_cpu in
4
   arm*)
5
 	ARM_VIDEO=yes
6
 	DEFAULT_INT10="stub"
7
+	case $host_os in
8
+		*freebsd*)
9
+			$as_echo "#define USE_DEV_IO 1" >>confdefs.h
10
+		 ;;
11
+	esac
12
 	;;
13
   i*86)
14
 	I386_VIDEO=yes
15
@@ -618,6 +623,7 @@ AC_ARG_ENABLE(config-udev,    AS_HELP_ST
16
 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])
17
 AC_ARG_ENABLE(config-hal,     AS_HELP_STRING([--disable-config-hal], [Build HAL support (default: auto)]), [CONFIG_HAL=$enableval], [CONFIG_HAL=auto])
18
 AC_ARG_ENABLE(config-wscons,  AS_HELP_STRING([--enable-config-wscons], [Build wscons config support (default: auto)]), [CONFIG_WSCONS=$enableval], [CONFIG_WSCONS=auto])
19
+AC_ARG_ENABLE(config-devd,    AS_HELP_STRING([--enable-config-devd], [Build devd support (default: auto)]), [CONFIG_DEVD=$enableval], [CONFIG_DEVD=auto])
20
 AC_ARG_ENABLE(xfree86-utils,     AS_HELP_STRING([--enable-xfree86-utils], [Build xfree86 DDX utilities (default: enabled)]), [XF86UTILS=$enableval], [XF86UTILS=yes])
21
 AC_ARG_ENABLE(vgahw,          AS_HELP_STRING([--enable-vgahw], [Build Xorg with vga access (default: enabled)]), [VGAHW=$enableval], [VGAHW=yes])
22
 AC_ARG_ENABLE(vbe,            AS_HELP_STRING([--enable-vbe], [Build Xorg with VBE module (default: enabled)]), [VBE=$enableval], [VBE=yes])
23
@@ -708,6 +714,7 @@ case $host_os in
24
 		CONFIG_HAL=no
25
 		CONFIG_UDEV=no
26
 		CONFIG_UDEV_KMS=no
27
+		CONFIG_DEVD=no
28
 		DGA=no
29
 		DRM=no
30
 		DRI2=no
31
@@ -866,6 +873,21 @@ if test "x$WITH_SYSTEMD_DAEMON" = "xyes"
32
 fi
33
 AM_CONDITIONAL([HAVE_SYSTEMD_DAEMON], [test "x$HAVE_SYSTEMD_DAEMON" = "xyes"])
34
 
35
+if test "x$CONFIG_DEVD" = xauto; then
36
+	case $host_os in
37
+		*freebsd*)
38
+			CONFIG_DEVD=yes;
39
+			;;
40
+		*)
41
+			CONFIG_DEVD=no;
42
+			;;
43
+	esac
44
+fi
45
+AM_CONDITIONAL(CONFIG_DEVD, [test "x$CONFIG_DEVD" = xyes])
46
+if test "x$CONFIG_DEVD" = xyes; then
47
+	AC_DEFINE(CONFIG_DEVD, 1, [Use devd for input auto configuration])
48
+fi
49
+
50
 if test "x$CONFIG_UDEV" = xyes && test "x$CONFIG_HAL" = xyes; then
51
 	AC_MSG_ERROR([Hotplugging through both libudev and hal not allowed])
52
 fi
53
@@ -1184,7 +1206,7 @@ AC_ARG_ENABLE(xtrans-send-fds,	AS_HELP_S
54
 case "x$XTRANS_SEND_FDS" in
55
 xauto)
56
 	case "$host_os" in
57
-	linux*|solaris*)
58
+	linux*|solaris*|freebsd*|dragonfly*)
59
 		XTRANS_SEND_FDS=yes
60
 		;;
61
 	*)
(-)x11-servers/xorg-server/files/patch-include_dix-config.h.in (+12 lines)
Line 0 Link Here
1
--- include/dix-config.h.in.orig	2015-10-27 23:12:01.000000000 +0300
2
+++ include/dix-config.h.in	2016-10-10 03:51:05.267830000 +0300
3
@@ -430,6 +430,9 @@
4
 /* Use udev_enumerate_add_match_tag() */
5
 #undef HAVE_UDEV_ENUMERATE_ADD_MATCH_TAG
6
 
7
+/* Use devd for input hotplug */
8
+#undef CONFIG_DEVD
9
+
10
 /* Enable D-Bus core */
11
 #undef NEED_DBUS
12
 

Return to bug 196678