1 /*
2  * Copyright 1995-1999 by Frederic Lepied, France. <Lepied@XFree86.org>
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is  hereby granted without fee, provided that
6  * the  above copyright   notice appear  in   all  copies and  that both  that
7  * copyright  notice   and   this  permission   notice  appear  in  supporting
8  * documentation, and that   the  name of  Frederic   Lepied not  be  used  in
9  * advertising or publicity pertaining to distribution of the software without
10  * specific,  written      prior  permission.     Frederic  Lepied   makes  no
11  * representations about the suitability of this software for any purpose.  It
12  * is provided "as is" without express or implied warranty.
13  *
14  * FREDERIC  LEPIED DISCLAIMS ALL   WARRANTIES WITH REGARD  TO  THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED   WARRANTIES OF MERCHANTABILITY  AND   FITNESS, IN NO
16  * EVENT  SHALL FREDERIC  LEPIED BE   LIABLE   FOR ANY  SPECIAL, INDIRECT   OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA  OR PROFITS, WHETHER  IN  AN ACTION OF  CONTRACT,  NEGLIGENCE OR OTHER
19  * TORTIOUS  ACTION, ARISING    OUT OF OR   IN  CONNECTION  WITH THE USE    OR
20  * PERFORMANCE OF THIS SOFTWARE.
21  *
22  */
23 /*
24  * Copyright (c) 2000-2002 by The XFree86 Project, Inc.
25  *
26  * Permission is hereby granted, free of charge, to any person obtaining a
27  * copy of this software and associated documentation files (the "Software"),
28  * to deal in the Software without restriction, including without limitation
29  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
30  * and/or sell copies of the Software, and to permit persons to whom the
31  * Software is furnished to do so, subject to the following conditions:
32  *
33  * The above copyright notice and this permission notice shall be included in
34  * all copies or substantial portions of the Software.
35  *
36  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
39  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
40  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
41  * OTHER DEALINGS IN THE SOFTWARE.
42  *
43  * Except as contained in this notice, the name of the copyright holder(s)
44  * and author(s) shall not be used in advertising or otherwise to promote
45  * the sale, use or other dealings in this Software without prior written
46  * authorization from the copyright holder(s) and author(s).
47  */
48 
49 #ifdef HAVE_XORG_CONFIG_H
50 #include <xorg-config.h>
51 #endif
52 
53 #include <X11/Xfuncproto.h>
54 #include <X11/Xmd.h>
55 #include <X11/extensions/XI.h>
56 #include <X11/extensions/XIproto.h>
57 #include <X11/Xatom.h>
58 #include "xf86.h"
59 #include "xf86Priv.h"
60 #include "xf86Config.h"
61 #include "xf86Xinput.h"
62 #include "xf86Optrec.h"
63 #include "mipointer.h"
64 #include "extinit.h"
65 #include "loaderProcs.h"
66 #include "systemd-logind.h"
67 
68 #include "exevents.h"           /* AddInputDevice */
69 #include "exglobals.h"
70 #include "eventstr.h"
71 #include "inpututils.h"
72 #include "optionstr.h"
73 
74 #include <string.h>             /* InputClassMatches */
75 #ifdef HAVE_FNMATCH_H
76 #include <fnmatch.h>
77 #endif
78 #ifdef HAVE_SYS_UTSNAME_H
79 #include <sys/utsname.h>
80 #endif
81 
82 #include <stdarg.h>
83 #include <stdint.h>             /* for int64_t */
84 #include <sys/types.h>
85 #include <sys/stat.h>
86 #include <unistd.h>
87 #ifdef HAVE_SYS_SYSMACROS_H
88 #include <sys/sysmacros.h>
89 #endif
90 #ifdef HAVE_SYS_MKDEV_H
91 #include <sys/mkdev.h>          /* for major() & minor() on Solaris */
92 #endif
93 
94 #include "mi.h"
95 
96 #include <ptrveloc.h>           /* dix pointer acceleration */
97 #include <xserver-properties.h>
98 
99 #ifdef XFreeXDGA
100 #include "dgaproc.h"
101 #endif
102 
103 #include "xkbsrv.h"
104 
105 /* Valuator verification macro */
106 #define XI_VERIFY_VALUATORS(num_valuators)					\
107 	if (num_valuators > MAX_VALUATORS) {					\
108 		xf86Msg(X_ERROR, "%s: num_valuator %d is greater than"		\
109 			" MAX_VALUATORS\n", __FUNCTION__, num_valuators);	\
110 		return;								\
111 	}
112 
113 static int
114  xf86InputDevicePostInit(DeviceIntPtr dev);
115 
116 typedef struct {
117     struct xorg_list node;
118     InputInfoPtr pInfo;
119 } PausedInputDeviceRec;
120 typedef PausedInputDeviceRec *PausedInputDevicePtr;
121 
122 static struct xorg_list new_input_devices_list = {
123     .next = &new_input_devices_list,
124     .prev = &new_input_devices_list,
125 };
126 
127 /**
128  * Eval config and modify DeviceVelocityRec accordingly
129  */
130 static void
ProcessVelocityConfiguration(DeviceIntPtr pDev,const char * devname,void * list,DeviceVelocityPtr s)131 ProcessVelocityConfiguration(DeviceIntPtr pDev, const char *devname, void *list,
132                              DeviceVelocityPtr s)
133 {
134     int tempi;
135     float tempf;
136     Atom float_prop = XIGetKnownProperty(XATOM_FLOAT);
137     Atom prop;
138 
139     if (!s)
140         return;
141 
142     /* common settings (available via device properties) */
143     tempf = xf86SetRealOption(list, "ConstantDeceleration", 1.0);
144     if (tempf != 1.0) {
145         xf86Msg(X_CONFIG, "%s: (accel) constant deceleration by %.1f\n",
146                 devname, tempf);
147         prop = XIGetKnownProperty(ACCEL_PROP_CONSTANT_DECELERATION);
148         XIChangeDeviceProperty(pDev, prop, float_prop, 32,
149                                PropModeReplace, 1, &tempf, FALSE);
150     }
151 
152     tempf = xf86SetRealOption(list, "AdaptiveDeceleration", 1.0);
153     if (tempf > 1.0) {
154         xf86Msg(X_CONFIG, "%s: (accel) adaptive deceleration by %.1f\n",
155                 devname, tempf);
156         prop = XIGetKnownProperty(ACCEL_PROP_ADAPTIVE_DECELERATION);
157         XIChangeDeviceProperty(pDev, prop, float_prop, 32,
158                                PropModeReplace, 1, &tempf, FALSE);
159     }
160 
161     /* select profile by number */
162     tempi = xf86SetIntOption(list, "AccelerationProfile",
163                              s->statistics.profile_number);
164 
165     prop = XIGetKnownProperty(ACCEL_PROP_PROFILE_NUMBER);
166     if (XIChangeDeviceProperty(pDev, prop, XA_INTEGER, 32,
167                                PropModeReplace, 1, &tempi, FALSE) == Success) {
168         xf86Msg(X_CONFIG, "%s: (accel) acceleration profile %i\n", devname,
169                 tempi);
170     }
171     else {
172         xf86Msg(X_CONFIG, "%s: (accel) acceleration profile %i is unknown\n",
173                 devname, tempi);
174     }
175 
176     /* set scaling */
177     tempf = xf86SetRealOption(list, "ExpectedRate", 0);
178     prop = XIGetKnownProperty(ACCEL_PROP_VELOCITY_SCALING);
179     if (tempf > 0) {
180         tempf = 1000.0 / tempf;
181         XIChangeDeviceProperty(pDev, prop, float_prop, 32,
182                                PropModeReplace, 1, &tempf, FALSE);
183     }
184     else {
185         tempf = xf86SetRealOption(list, "VelocityScale", s->corr_mul);
186         XIChangeDeviceProperty(pDev, prop, float_prop, 32,
187                                PropModeReplace, 1, &tempf, FALSE);
188     }
189 
190     tempi = xf86SetIntOption(list, "VelocityTrackerCount", -1);
191     if (tempi > 1)
192         InitTrackers(s, tempi);
193 
194     s->initial_range = xf86SetIntOption(list, "VelocityInitialRange",
195                                         s->initial_range);
196 
197     s->max_diff = xf86SetRealOption(list, "VelocityAbsDiff", s->max_diff);
198 
199     tempf = xf86SetRealOption(list, "VelocityRelDiff", -1);
200     if (tempf >= 0) {
201         xf86Msg(X_CONFIG, "%s: (accel) max rel. velocity difference: %.1f%%\n",
202                 devname, tempf * 100.0);
203         s->max_rel_diff = tempf;
204     }
205 
206     /*  Configure softening. If const deceleration is used, this is expected
207      *  to provide better subpixel information so we enable
208      *  softening by default only if ConstantDeceleration is not used
209      */
210     s->use_softening = xf86SetBoolOption(list, "Softening",
211                                          s->const_acceleration == 1.0);
212 
213     s->average_accel = xf86SetBoolOption(list, "AccelerationProfileAveraging",
214                                          s->average_accel);
215 
216     s->reset_time = xf86SetIntOption(list, "VelocityReset", s->reset_time);
217 }
218 
219 static void
ApplyAccelerationSettings(DeviceIntPtr dev)220 ApplyAccelerationSettings(DeviceIntPtr dev)
221 {
222     int scheme, i;
223     DeviceVelocityPtr pVel;
224     InputInfoPtr pInfo = (InputInfoPtr) dev->public.devicePrivate;
225     char *schemeStr;
226 
227     if (dev->valuator && dev->ptrfeed) {
228         schemeStr = xf86SetStrOption(pInfo->options, "AccelerationScheme", "");
229 
230         scheme = dev->valuator->accelScheme.number;
231 
232         if (!xf86NameCmp(schemeStr, "predictable"))
233             scheme = PtrAccelPredictable;
234 
235         if (!xf86NameCmp(schemeStr, "lightweight"))
236             scheme = PtrAccelLightweight;
237 
238         if (!xf86NameCmp(schemeStr, "none"))
239             scheme = PtrAccelNoOp;
240 
241         /* reinit scheme if needed */
242         if (dev->valuator->accelScheme.number != scheme) {
243             if (dev->valuator->accelScheme.AccelCleanupProc) {
244                 dev->valuator->accelScheme.AccelCleanupProc(dev);
245             }
246 
247             if (InitPointerAccelerationScheme(dev, scheme)) {
248                 xf86Msg(X_CONFIG, "%s: (accel) selected scheme %s/%i\n",
249                         pInfo->name, schemeStr, scheme);
250             }
251             else {
252                 xf86Msg(X_CONFIG, "%s: (accel) could not init scheme %s\n",
253                         pInfo->name, schemeStr);
254                 scheme = dev->valuator->accelScheme.number;
255             }
256         }
257         else {
258             xf86Msg(X_CONFIG, "%s: (accel) keeping acceleration scheme %i\n",
259                     pInfo->name, scheme);
260         }
261 
262         free(schemeStr);
263 
264         /* process special configuration */
265         switch (scheme) {
266         case PtrAccelPredictable:
267             pVel = GetDevicePredictableAccelData(dev);
268             ProcessVelocityConfiguration(dev, pInfo->name, pInfo->options,
269                                          pVel);
270             break;
271         }
272 
273         i = xf86SetIntOption(pInfo->options, "AccelerationNumerator",
274                              dev->ptrfeed->ctrl.num);
275         if (i >= 0)
276             dev->ptrfeed->ctrl.num = i;
277 
278         i = xf86SetIntOption(pInfo->options, "AccelerationDenominator",
279                              dev->ptrfeed->ctrl.den);
280         if (i > 0)
281             dev->ptrfeed->ctrl.den = i;
282 
283         i = xf86SetIntOption(pInfo->options, "AccelerationThreshold",
284                              dev->ptrfeed->ctrl.threshold);
285         if (i >= 0)
286             dev->ptrfeed->ctrl.threshold = i;
287 
288         xf86Msg(X_CONFIG, "%s: (accel) acceleration factor: %.3f\n",
289                 pInfo->name, ((float) dev->ptrfeed->ctrl.num) /
290                 ((float) dev->ptrfeed->ctrl.den));
291         xf86Msg(X_CONFIG, "%s: (accel) acceleration threshold: %i\n",
292                 pInfo->name, dev->ptrfeed->ctrl.threshold);
293     }
294 }
295 
296 static void
ApplyTransformationMatrix(DeviceIntPtr dev)297 ApplyTransformationMatrix(DeviceIntPtr dev)
298 {
299     InputInfoPtr pInfo = (InputInfoPtr) dev->public.devicePrivate;
300     char *str;
301     int rc;
302     float matrix[9] = { 0 };
303 
304     if (!dev->valuator)
305         return;
306 
307     str = xf86SetStrOption(pInfo->options, "TransformationMatrix", NULL);
308     if (!str)
309         return;
310 
311     rc = sscanf(str, "%f %f %f %f %f %f %f %f %f", &matrix[0], &matrix[1],
312                 &matrix[2], &matrix[3], &matrix[4], &matrix[5], &matrix[6],
313                 &matrix[7], &matrix[8]);
314     if (rc != 9) {
315         xf86Msg(X_ERROR,
316                 "%s: invalid format for transformation matrix. Ignoring configuration.\n",
317                 pInfo->name);
318         return;
319     }
320 
321     XIChangeDeviceProperty(dev, XIGetKnownProperty(XI_PROP_TRANSFORM),
322                            XIGetKnownProperty(XATOM_FLOAT), 32,
323                            PropModeReplace, 9, matrix, FALSE);
324 }
325 
326 /***********************************************************************
327  *
328  * xf86ProcessCommonOptions --
329  *
330  *	Process global options.
331  *
332  ***********************************************************************
333  */
334 void
xf86ProcessCommonOptions(InputInfoPtr pInfo,XF86OptionPtr list)335 xf86ProcessCommonOptions(InputInfoPtr pInfo, XF86OptionPtr list)
336 {
337     if (xf86SetBoolOption(list, "Floating", 0) ||
338         !xf86SetBoolOption(list, "AlwaysCore", 1) ||
339         !xf86SetBoolOption(list, "SendCoreEvents", 1) ||
340         !xf86SetBoolOption(list, "CorePointer", 1) ||
341         !xf86SetBoolOption(list, "CoreKeyboard", 1)) {
342         xf86Msg(X_CONFIG, "%s: doesn't report core events\n", pInfo->name);
343     }
344     else {
345         pInfo->flags |= XI86_ALWAYS_CORE;
346         xf86Msg(X_CONFIG, "%s: always reports core events\n", pInfo->name);
347     }
348 }
349 
350 /***********************************************************************
351  *
352  * xf86ActivateDevice --
353  *
354  *	Initialize an input device.
355  *
356  * Returns TRUE on success, or FALSE otherwise.
357  ***********************************************************************
358  */
359 static DeviceIntPtr
xf86ActivateDevice(InputInfoPtr pInfo)360 xf86ActivateDevice(InputInfoPtr pInfo)
361 {
362     DeviceIntPtr dev;
363     Atom atom;
364 
365     dev = AddInputDevice(serverClient, pInfo->device_control, TRUE);
366 
367     if (dev == NULL) {
368         xf86Msg(X_ERROR, "Too many input devices. Ignoring %s\n", pInfo->name);
369         pInfo->dev = NULL;
370         return NULL;
371     }
372 
373     atom = MakeAtom(pInfo->type_name, strlen(pInfo->type_name), TRUE);
374     AssignTypeAndName(dev, atom, pInfo->name);
375     dev->public.devicePrivate = pInfo;
376     pInfo->dev = dev;
377 
378     dev->coreEvents = pInfo->flags & XI86_ALWAYS_CORE;
379     dev->type = SLAVE;
380     dev->spriteInfo->spriteOwner = FALSE;
381 
382     dev->config_info = xf86SetStrOption(pInfo->options, "config_info", NULL);
383 
384     if (serverGeneration == 1)
385         xf86Msg(X_INFO,
386                 "XINPUT: Adding extended input device \"%s\" (type: %s, id %d)\n",
387                 pInfo->name, pInfo->type_name, dev->id);
388 
389     return dev;
390 }
391 
392 /****************************************************************************
393  *
394  * Caller:	ProcXSetDeviceMode
395  *
396  * Change the mode of an extension device.
397  * This function is used to change the mode of a device from reporting
398  * relative motion to reporting absolute positional information, and
399  * vice versa.
400  * The default implementation below is that no such devices are supported.
401  *
402  ***********************************************************************
403  */
404 
405 int
SetDeviceMode(ClientPtr client,DeviceIntPtr dev,int mode)406 SetDeviceMode(ClientPtr client, DeviceIntPtr dev, int mode)
407 {
408     InputInfoPtr pInfo = (InputInfoPtr) dev->public.devicePrivate;
409 
410     if (pInfo->switch_mode) {
411         return (*pInfo->switch_mode) (client, dev, mode);
412     }
413     else
414         return BadMatch;
415 }
416 
417 /***********************************************************************
418  *
419  * Caller:	ProcXSetDeviceValuators
420  *
421  * Set the value of valuators on an extension input device.
422  * This function is used to set the initial value of valuators on
423  * those input devices that are capable of reporting either relative
424  * motion or an absolute position, and allow an initial position to be set.
425  * The default implementation below is that no such devices are supported.
426  *
427  ***********************************************************************
428  */
429 
430 int
SetDeviceValuators(ClientPtr client,DeviceIntPtr dev,int * valuators,int first_valuator,int num_valuators)431 SetDeviceValuators(ClientPtr client, DeviceIntPtr dev, int *valuators,
432                    int first_valuator, int num_valuators)
433 {
434     InputInfoPtr pInfo = (InputInfoPtr) dev->public.devicePrivate;
435 
436     if (pInfo->set_device_valuators)
437         return (*pInfo->set_device_valuators) (pInfo, valuators, first_valuator,
438                                                num_valuators);
439 
440     return BadMatch;
441 }
442 
443 /***********************************************************************
444  *
445  * Caller:	ProcXChangeDeviceControl
446  *
447  * Change the specified device controls on an extension input device.
448  *
449  ***********************************************************************
450  */
451 
452 int
ChangeDeviceControl(ClientPtr client,DeviceIntPtr dev,xDeviceCtl * control)453 ChangeDeviceControl(ClientPtr client, DeviceIntPtr dev, xDeviceCtl * control)
454 {
455     InputInfoPtr pInfo = (InputInfoPtr) dev->public.devicePrivate;
456 
457     if (!pInfo->control_proc) {
458         switch (control->control) {
459         case DEVICE_CORE:
460         case DEVICE_ABS_CALIB:
461         case DEVICE_ABS_AREA:
462             return BadMatch;
463         case DEVICE_RESOLUTION:
464         case DEVICE_ENABLE:
465             return Success;
466         default:
467             return BadMatch;
468         }
469     }
470     else {
471         return (*pInfo->control_proc) (pInfo, control);
472     }
473 }
474 
475 /*
476  * Get the operating system name from uname and store it statically to avoid
477  * repeating the system call each time MatchOS is checked.
478  */
479 static const char *
HostOS(void)480 HostOS(void)
481 {
482 #ifdef HAVE_SYS_UTSNAME_H
483     struct utsname name;
484     static char host_os[sizeof(name.sysname)] = "";
485 
486     if (*host_os == '\0') {
487         if (uname(&name) >= 0)
488             strlcpy(host_os, name.sysname, sizeof(host_os));
489         else {
490             strlcpy(host_os, "unknown", sizeof(host_os));
491         }
492     }
493     return host_os;
494 #else
495     return "";
496 #endif
497 }
498 
499 static int
match_substring(const char * attr,const char * pattern)500 match_substring(const char *attr, const char *pattern)
501 {
502     return (strstr(attr, pattern)) ? 0 : -1;
503 }
504 
505 #ifdef HAVE_FNMATCH_H
506 static int
match_pattern(const char * attr,const char * pattern)507 match_pattern(const char *attr, const char *pattern)
508 {
509     return fnmatch(pattern, attr, 0);
510 }
511 #else
512 #define match_pattern match_substring
513 #endif
514 
515 #ifdef HAVE_FNMATCH_H
516 static int
match_path_pattern(const char * attr,const char * pattern)517 match_path_pattern(const char *attr, const char *pattern)
518 {
519     return fnmatch(pattern, attr, FNM_PATHNAME);
520 }
521 #else
522 #define match_path_pattern match_substring
523 #endif
524 
525 /*
526  * If no Layout section is found, xf86ServerLayout.id becomes "(implicit)"
527  * It is convenient that "" in patterns means "no explicit layout"
528  */
529 static int
match_string_implicit(const char * attr,const char * pattern)530 match_string_implicit(const char *attr, const char *pattern)
531 {
532     if (strlen(pattern)) {
533         return strcmp(attr, pattern);
534     }
535     else {
536         return strcmp(attr, "(implicit)");
537     }
538 }
539 
540 /*
541  * Match an attribute against a list of NULL terminated arrays of patterns.
542  * If a pattern in each list entry is matched, return TRUE.
543  */
544 static Bool
MatchAttrToken(const char * attr,struct xorg_list * patterns,int (* compare)(const char * attr,const char * pattern))545 MatchAttrToken(const char *attr, struct xorg_list *patterns,
546                int (*compare) (const char *attr, const char *pattern))
547 {
548     const xf86MatchGroup *group;
549 
550     /* If there are no patterns, accept the match */
551     if (xorg_list_is_empty(patterns))
552         return TRUE;
553 
554     /*
555      * Iterate the list of patterns ensuring each entry has a
556      * match. Each list entry is a separate Match line of the same type.
557      */
558     xorg_list_for_each_entry(group, patterns, entry) {
559         char *const *cur;
560         Bool is_negated = group->is_negated;
561         Bool match = is_negated;
562 
563         /* If there's a pattern but no attribute, we reject the match for a
564          * MatchFoo directive, and accept it for a NoMatchFoo directive
565          */
566         if (!attr)
567             return is_negated;
568 
569         for (cur = group->values; *cur; cur++)
570             if ((*compare) (attr, *cur) == 0) {
571                 match = !is_negated;
572                 break;
573             }
574         if (!match)
575             return FALSE;
576     }
577 
578     /* All the entries in the list matched the attribute */
579     return TRUE;
580 }
581 
582 /*
583  * Classes without any Match statements match all devices. Otherwise, all
584  * statements must match.
585  */
586 static Bool
InputClassMatches(const XF86ConfInputClassPtr iclass,const InputInfoPtr idev,const InputAttributes * attrs)587 InputClassMatches(const XF86ConfInputClassPtr iclass, const InputInfoPtr idev,
588                   const InputAttributes * attrs)
589 {
590     /* MatchProduct substring */
591     if (!MatchAttrToken
592         (attrs->product, &iclass->match_product, match_substring))
593         return FALSE;
594 
595     /* MatchVendor substring */
596     if (!MatchAttrToken(attrs->vendor, &iclass->match_vendor, match_substring))
597         return FALSE;
598 
599     /* MatchDevicePath pattern */
600     if (!MatchAttrToken
601         (attrs->device, &iclass->match_device, match_path_pattern))
602         return FALSE;
603 
604     /* MatchOS case-insensitive string */
605     if (!MatchAttrToken(HostOS(), &iclass->match_os, strcasecmp))
606         return FALSE;
607 
608     /* MatchPnPID pattern */
609     if (!MatchAttrToken(attrs->pnp_id, &iclass->match_pnpid, match_pattern))
610         return FALSE;
611 
612     /* MatchUSBID pattern */
613     if (!MatchAttrToken(attrs->usb_id, &iclass->match_usbid, match_pattern))
614         return FALSE;
615 
616     /* MatchDriver string */
617     if (!MatchAttrToken(idev->driver, &iclass->match_driver, strcmp))
618         return FALSE;
619 
620     /*
621      * MatchTag string
622      * See if any of the device's tags match any of the MatchTag tokens.
623      */
624     if (!xorg_list_is_empty(&iclass->match_tag)) {
625         char *const *tag;
626         Bool match;
627 
628         if (!attrs->tags)
629             return FALSE;
630         for (tag = attrs->tags, match = FALSE; *tag; tag++) {
631             if (MatchAttrToken(*tag, &iclass->match_tag, strcmp)) {
632                 match = TRUE;
633                 break;
634             }
635         }
636         if (!match)
637             return FALSE;
638     }
639 
640     /* MatchLayout string */
641     if (!xorg_list_is_empty(&iclass->match_layout)) {
642         if (!MatchAttrToken(xf86ConfigLayout.id,
643                             &iclass->match_layout, match_string_implicit))
644             return FALSE;
645     }
646 
647     /* MatchIs* booleans */
648     if (iclass->is_keyboard.set &&
649         iclass->is_keyboard.val != ! !(attrs->flags & (ATTR_KEY|ATTR_KEYBOARD)))
650         return FALSE;
651     if (iclass->is_pointer.set &&
652         iclass->is_pointer.val != ! !(attrs->flags & ATTR_POINTER))
653         return FALSE;
654     if (iclass->is_joystick.set &&
655         iclass->is_joystick.val != ! !(attrs->flags & ATTR_JOYSTICK))
656         return FALSE;
657     if (iclass->is_tablet.set &&
658         iclass->is_tablet.val != ! !(attrs->flags & ATTR_TABLET))
659         return FALSE;
660     if (iclass->is_tablet_pad.set &&
661         iclass->is_tablet_pad.val != ! !(attrs->flags & ATTR_TABLET_PAD))
662         return FALSE;
663     if (iclass->is_touchpad.set &&
664         iclass->is_touchpad.val != ! !(attrs->flags & ATTR_TOUCHPAD))
665         return FALSE;
666     if (iclass->is_touchscreen.set &&
667         iclass->is_touchscreen.val != ! !(attrs->flags & ATTR_TOUCHSCREEN))
668         return FALSE;
669 
670     return TRUE;
671 }
672 
673 /*
674  * Merge in any InputClass configurations. Options in each InputClass
675  * section have more priority than the original device configuration as
676  * well as any previous InputClass sections.
677  */
678 static int
MergeInputClasses(const InputInfoPtr idev,const InputAttributes * attrs)679 MergeInputClasses(const InputInfoPtr idev, const InputAttributes * attrs)
680 {
681     XF86ConfInputClassPtr cl;
682     XF86OptionPtr classopts;
683 
684     for (cl = xf86configptr->conf_inputclass_lst; cl; cl = cl->list.next) {
685         if (!InputClassMatches(cl, idev, attrs))
686             continue;
687 
688         /* Collect class options and driver settings */
689         classopts = xf86optionListDup(cl->option_lst);
690         if (cl->driver) {
691             free((void *) idev->driver);
692             idev->driver = xstrdup(cl->driver);
693             if (!idev->driver) {
694                 xf86Msg(X_ERROR, "Failed to allocate memory while merging "
695                         "InputClass configuration");
696                 return BadAlloc;
697             }
698             classopts = xf86ReplaceStrOption(classopts, "driver", idev->driver);
699         }
700 
701         /* Apply options to device with InputClass settings preferred. */
702         xf86Msg(X_CONFIG, "%s: Applying InputClass \"%s\"\n",
703                 idev->name, cl->identifier);
704         idev->options = xf86optionListMerge(idev->options, classopts);
705     }
706 
707     return Success;
708 }
709 
710 /*
711  * Iterate the list of classes and look for Option "Ignore". Return the
712  * value of the last matching class and holler when returning TRUE.
713  */
714 static Bool
IgnoreInputClass(const InputInfoPtr idev,const InputAttributes * attrs)715 IgnoreInputClass(const InputInfoPtr idev, const InputAttributes * attrs)
716 {
717     XF86ConfInputClassPtr cl;
718     Bool ignore = FALSE;
719     const char *ignore_class;
720 
721     for (cl = xf86configptr->conf_inputclass_lst; cl; cl = cl->list.next) {
722         if (!InputClassMatches(cl, idev, attrs))
723             continue;
724         if (xf86findOption(cl->option_lst, "Ignore")) {
725             ignore = xf86CheckBoolOption(cl->option_lst, "Ignore", FALSE);
726             ignore_class = cl->identifier;
727         }
728     }
729 
730     if (ignore)
731         xf86Msg(X_CONFIG, "%s: Ignoring device from InputClass \"%s\"\n",
732                 idev->name, ignore_class);
733     return ignore;
734 }
735 
736 InputInfoPtr
xf86AllocateInput(void)737 xf86AllocateInput(void)
738 {
739     InputInfoPtr pInfo;
740 
741     pInfo = calloc(sizeof(*pInfo), 1);
742     if (!pInfo)
743         return NULL;
744 
745     pInfo->fd = -1;
746     pInfo->type_name = "UNKNOWN";
747 
748     return pInfo;
749 }
750 
751 /* Append InputInfoRec to the tail of xf86InputDevs. */
752 static void
xf86AddInput(InputDriverPtr drv,InputInfoPtr pInfo)753 xf86AddInput(InputDriverPtr drv, InputInfoPtr pInfo)
754 {
755     InputInfoPtr *prev = NULL;
756 
757     pInfo->drv = drv;
758     pInfo->module = DuplicateModule(drv->module, NULL);
759 
760     for (prev = &xf86InputDevs; *prev; prev = &(*prev)->next);
761 
762     *prev = pInfo;
763     pInfo->next = NULL;
764 
765     xf86CollectInputOptions(pInfo, (const char **) drv->default_options);
766     xf86OptionListReport(pInfo->options);
767     xf86ProcessCommonOptions(pInfo, pInfo->options);
768 }
769 
770 /*
771  * Remove an entry from xf86InputDevs and free all the device's information.
772  */
773 void
xf86DeleteInput(InputInfoPtr pInp,int flags)774 xf86DeleteInput(InputInfoPtr pInp, int flags)
775 {
776     /* First check if the inputdev is valid. */
777     if (pInp == NULL)
778         return;
779 
780     if (pInp->module)
781         UnloadModule(pInp->module);
782 
783     /* This should *really* be handled in drv->UnInit(dev) call instead, but
784      * if the driver forgets about it make sure we free it or at least crash
785      * with flying colors */
786     free(pInp->private);
787 
788     FreeInputAttributes(pInp->attrs);
789 
790     if (pInp->flags & XI86_SERVER_FD)
791         systemd_logind_release_fd(pInp->major, pInp->minor, pInp->fd);
792 
793     /* Remove the entry from the list. */
794     if (pInp == xf86InputDevs)
795         xf86InputDevs = pInp->next;
796     else {
797         InputInfoPtr p = xf86InputDevs;
798 
799         while (p && p->next != pInp)
800             p = p->next;
801         if (p)
802             p->next = pInp->next;
803         /* Else the entry wasn't in the xf86InputDevs list (ignore this). */
804     }
805 
806     free((void *) pInp->driver);
807     free((void *) pInp->name);
808     xf86optionListFree(pInp->options);
809     free(pInp);
810 }
811 
812 /*
813  * Apply backend-specific initialization. Invoked after ActivateDevice(),
814  * i.e. after the driver successfully completed DEVICE_INIT and the device
815  * is advertised.
816  * @param dev the device
817  * @return Success or an error code
818  */
819 static int
xf86InputDevicePostInit(DeviceIntPtr dev)820 xf86InputDevicePostInit(DeviceIntPtr dev)
821 {
822     ApplyAccelerationSettings(dev);
823     ApplyTransformationMatrix(dev);
824     return Success;
825 }
826 
827 static void
xf86stat(const char * path,int * maj,int * min)828 xf86stat(const char *path, int *maj, int *min)
829 {
830     struct stat st;
831 
832     if (stat(path, &st) == -1)
833         return;
834 
835     *maj = major(st.st_rdev);
836     *min = minor(st.st_rdev);
837 }
838 
839 static inline InputDriverPtr
xf86LoadInputDriver(const char * driver_name)840 xf86LoadInputDriver(const char *driver_name)
841 {
842     InputDriverPtr drv = NULL;
843 
844     /* Memory leak for every attached device if we don't
845      * test if the module is already loaded first */
846     drv = xf86LookupInputDriver(driver_name);
847     if (!drv) {
848         if (xf86LoadOneModule(driver_name, NULL))
849             drv = xf86LookupInputDriver(driver_name);
850     }
851 
852     return drv;
853 }
854 
855 /**
856  * Create a new input device, activate and enable it.
857  *
858  * Possible return codes:
859  *    BadName .. a bad driver name was supplied.
860  *    BadImplementation ... The driver does not have a PreInit function. This
861  *                          is a driver bug.
862  *    BadMatch .. device initialization failed.
863  *    BadAlloc .. too many input devices
864  *
865  * @param idev The device, already set up with identifier, driver, and the
866  * options.
867  * @param pdev Pointer to the new device, if Success was reported.
868  * @param enable Enable the device after activating it.
869  *
870  * @return Success or an error code
871  */
872 _X_INTERNAL int
xf86NewInputDevice(InputInfoPtr pInfo,DeviceIntPtr * pdev,BOOL enable)873 xf86NewInputDevice(InputInfoPtr pInfo, DeviceIntPtr *pdev, BOOL enable)
874 {
875     InputDriverPtr drv = NULL;
876     DeviceIntPtr dev = NULL;
877     Bool paused = 0;
878     int rval;
879     char *path = NULL;
880 
881     drv = xf86LoadInputDriver(pInfo->driver);
882     if (!drv) {
883         xf86Msg(X_ERROR, "No input driver matching `%s'\n", pInfo->driver);
884 
885         if (strlen(FALLBACK_INPUT_DRIVER) > 0) {
886             xf86Msg(X_INFO, "Falling back to input driver `%s'\n",
887                     FALLBACK_INPUT_DRIVER);
888             drv = xf86LoadInputDriver(FALLBACK_INPUT_DRIVER);
889             if (drv) {
890                 free(pInfo->driver);
891                 pInfo->driver = strdup(FALLBACK_INPUT_DRIVER);
892             }
893         }
894         if (!drv) {
895             rval = BadName;
896             goto unwind;
897         }
898     }
899 
900     xf86Msg(X_INFO, "Using input driver '%s' for '%s'\n", drv->driverName,
901             pInfo->name);
902 
903     if (!drv->PreInit) {
904         xf86Msg(X_ERROR,
905                 "Input driver `%s' has no PreInit function (ignoring)\n",
906                 drv->driverName);
907         rval = BadImplementation;
908         goto unwind;
909     }
910 
911     path = xf86CheckStrOption(pInfo->options, "Device", NULL);
912     if (path && pInfo->major == 0 && pInfo->minor == 0)
913         xf86stat(path, &pInfo->major, &pInfo->minor);
914 
915     if (path && (drv->capabilities & XI86_DRV_CAP_SERVER_FD)){
916         int fd = systemd_logind_take_fd(pInfo->major, pInfo->minor,
917                                         path, &paused);
918         if (fd != -1) {
919             if (paused) {
920                 /* Put on new_input_devices list for delayed probe */
921                 PausedInputDevicePtr new_device = xnfalloc(sizeof *new_device);
922                 new_device->pInfo = pInfo;
923 
924                 xorg_list_append(&new_device->node, &new_input_devices_list);
925                 systemd_logind_release_fd(pInfo->major, pInfo->minor, fd);
926                 free(path);
927                 return BadMatch;
928             }
929             pInfo->fd = fd;
930             pInfo->flags |= XI86_SERVER_FD;
931             pInfo->options = xf86ReplaceIntOption(pInfo->options, "fd", fd);
932         }
933     }
934 
935     free(path);
936 
937     xf86AddInput(drv, pInfo);
938 
939     input_lock();
940     rval = drv->PreInit(drv, pInfo, 0);
941     input_unlock();
942 
943     if (rval != Success) {
944         xf86Msg(X_ERROR, "PreInit returned %d for \"%s\"\n", rval, pInfo->name);
945         goto unwind;
946     }
947 
948     if (!(dev = xf86ActivateDevice(pInfo))) {
949         rval = BadAlloc;
950         goto unwind;
951     }
952 
953     rval = ActivateDevice(dev, TRUE);
954     if (rval != Success) {
955         xf86Msg(X_ERROR, "Couldn't init device \"%s\"\n", pInfo->name);
956         RemoveDevice(dev, TRUE);
957         goto unwind;
958     }
959 
960     rval = xf86InputDevicePostInit(dev);
961     if (rval != Success) {
962         xf86Msg(X_ERROR, "Couldn't post-init device \"%s\"\n", pInfo->name);
963         RemoveDevice(dev, TRUE);
964         goto unwind;
965     }
966 
967     /* Enable it if it's properly initialised and we're currently in the VT */
968     if (enable && dev->inited && dev->startup && xf86VTOwner()) {
969         input_lock();
970         EnableDevice(dev, TRUE);
971         if (!dev->enabled) {
972             xf86Msg(X_ERROR, "Couldn't init device \"%s\"\n", pInfo->name);
973             RemoveDevice(dev, TRUE);
974             rval = BadMatch;
975             input_unlock();
976             goto unwind;
977         }
978         /* send enter/leave event, update sprite window */
979         CheckMotion(NULL, dev);
980         input_unlock();
981     }
982 
983     *pdev = dev;
984     return Success;
985 
986  unwind:
987     if (pInfo) {
988         if (drv && drv->UnInit)
989             drv->UnInit(drv, pInfo, 0);
990         else
991             xf86DeleteInput(pInfo, 0);
992     }
993     return rval;
994 }
995 
996 int
NewInputDeviceRequest(InputOption * options,InputAttributes * attrs,DeviceIntPtr * pdev)997 NewInputDeviceRequest(InputOption *options, InputAttributes * attrs,
998                       DeviceIntPtr *pdev)
999 {
1000     InputInfoPtr pInfo = NULL;
1001     InputOption *option = NULL;
1002     int rval = Success;
1003     int is_auto = 0;
1004 
1005     pInfo = xf86AllocateInput();
1006     if (!pInfo)
1007         return BadAlloc;
1008 
1009     nt_list_for_each_entry(option, options, list.next) {
1010         const char *key = input_option_get_key(option);
1011         const char *value = input_option_get_value(option);
1012 
1013         if (strcasecmp(key, "driver") == 0) {
1014             if (pInfo->driver) {
1015                 rval = BadRequest;
1016                 goto unwind;
1017             }
1018             pInfo->driver = xstrdup(value);
1019             if (!pInfo->driver) {
1020                 rval = BadAlloc;
1021                 goto unwind;
1022             }
1023         }
1024 
1025         if (strcasecmp(key, "name") == 0 || strcasecmp(key, "identifier") == 0) {
1026             if (pInfo->name) {
1027                 rval = BadRequest;
1028                 goto unwind;
1029             }
1030             pInfo->name = xstrdup(value);
1031             if (!pInfo->name) {
1032                 rval = BadAlloc;
1033                 goto unwind;
1034             }
1035         }
1036 
1037         if (strcmp(key, "_source") == 0 &&
1038             (strcmp(value, "server/hal") == 0 ||
1039              strcmp(value, "server/udev") == 0 ||
1040              strcmp(value, "server/devd") == 0 ||
1041              strcmp(value, "server/wscons") == 0)) {
1042             is_auto = 1;
1043             if (!xf86Info.autoAddDevices) {
1044                 rval = BadMatch;
1045                 goto unwind;
1046             }
1047         }
1048 
1049         if (strcmp(key, "major") == 0)
1050             pInfo->major = atoi(value);
1051 
1052         if (strcmp(key, "minor") == 0)
1053             pInfo->minor = atoi(value);
1054     }
1055 
1056     nt_list_for_each_entry(option, options, list.next) {
1057         /* Copy option key/value strings from the provided list */
1058         pInfo->options = xf86AddNewOption(pInfo->options,
1059                                           input_option_get_key(option),
1060                                           input_option_get_value(option));
1061     }
1062 
1063     /* Apply InputClass settings */
1064     if (attrs) {
1065         if (IgnoreInputClass(pInfo, attrs)) {
1066             rval = BadIDChoice;
1067             goto unwind;
1068         }
1069 
1070         rval = MergeInputClasses(pInfo, attrs);
1071         if (rval != Success)
1072             goto unwind;
1073 
1074         pInfo->attrs = DuplicateInputAttributes(attrs);
1075     }
1076 
1077     if (!pInfo->name) {
1078         xf86Msg(X_INFO, "No identifier specified, ignoring this device.\n");
1079         rval = BadRequest;
1080         goto unwind;
1081     }
1082 
1083     if (!pInfo->driver) {
1084         xf86Msg(X_INFO, "No input driver specified, ignoring this device.\n");
1085         xf86Msg(X_INFO,
1086                 "This device may have been added with another device file.\n");
1087         rval = BadRequest;
1088         goto unwind;
1089     }
1090 
1091     rval = xf86NewInputDevice(pInfo, pdev,
1092                               (!is_auto ||
1093                                (is_auto && xf86Info.autoEnableDevices)));
1094 
1095     return rval;
1096 
1097  unwind:
1098     if (is_auto && !xf86Info.autoAddDevices)
1099         xf86Msg(X_INFO, "AutoAddDevices is off - not adding device.\n");
1100     xf86DeleteInput(pInfo, 0);
1101     return rval;
1102 }
1103 
1104 void
DeleteInputDeviceRequest(DeviceIntPtr pDev)1105 DeleteInputDeviceRequest(DeviceIntPtr pDev)
1106 {
1107     InputInfoPtr pInfo = (InputInfoPtr) pDev->public.devicePrivate;
1108     InputDriverPtr drv = NULL;
1109     Bool isMaster = IsMaster(pDev);
1110 
1111     if (pInfo)                  /* need to get these before RemoveDevice */
1112         drv = pInfo->drv;
1113 
1114     input_lock();
1115     RemoveDevice(pDev, TRUE);
1116 
1117     if (!isMaster && pInfo != NULL) {
1118         if (drv->UnInit)
1119             drv->UnInit(drv, pInfo, 0);
1120         else
1121             xf86DeleteInput(pInfo, 0);
1122     }
1123     input_unlock();
1124 }
1125 
1126 void
RemoveInputDeviceTraces(const char * config_info)1127 RemoveInputDeviceTraces(const char *config_info)
1128 {
1129     PausedInputDevicePtr d, tmp;
1130 
1131     xorg_list_for_each_entry_safe(d, tmp, &new_input_devices_list, node) {
1132         const char *ci = xf86findOptionValue(d->pInfo->options, "config_info");
1133         if (!ci || strcmp(ci, config_info) != 0)
1134             continue;
1135 
1136         xorg_list_del(&d->node);
1137         free(d);
1138     }
1139 }
1140 
1141 /*
1142  * convenient functions to post events
1143  */
1144 
1145 void
xf86PostMotionEvent(DeviceIntPtr device,int is_absolute,int first_valuator,int num_valuators,...)1146 xf86PostMotionEvent(DeviceIntPtr device,
1147                     int is_absolute, int first_valuator, int num_valuators, ...)
1148 {
1149     va_list var;
1150     int i = 0;
1151     ValuatorMask mask;
1152 
1153     XI_VERIFY_VALUATORS(num_valuators);
1154 
1155     valuator_mask_zero(&mask);
1156     va_start(var, num_valuators);
1157     for (i = 0; i < num_valuators; i++)
1158         valuator_mask_set(&mask, first_valuator + i, va_arg(var, int));
1159 
1160     va_end(var);
1161 
1162     xf86PostMotionEventM(device, is_absolute, &mask);
1163 }
1164 
1165 void
xf86PostMotionEventP(DeviceIntPtr device,int is_absolute,int first_valuator,int num_valuators,const int * valuators)1166 xf86PostMotionEventP(DeviceIntPtr device,
1167                      int is_absolute,
1168                      int first_valuator,
1169                      int num_valuators, const int *valuators)
1170 {
1171     ValuatorMask mask;
1172 
1173     XI_VERIFY_VALUATORS(num_valuators);
1174 
1175     valuator_mask_set_range(&mask, first_valuator, num_valuators, valuators);
1176     xf86PostMotionEventM(device, is_absolute, &mask);
1177 }
1178 
1179 static int
xf86CheckMotionEvent4DGA(DeviceIntPtr device,int is_absolute,const ValuatorMask * mask)1180 xf86CheckMotionEvent4DGA(DeviceIntPtr device, int is_absolute,
1181                          const ValuatorMask *mask)
1182 {
1183     int stolen = 0;
1184 
1185 #ifdef XFreeXDGA
1186     ScreenPtr scr = NULL;
1187     int idx = 0, i;
1188 
1189     /* The evdev driver may not always send all axes across. */
1190     if (valuator_mask_isset(mask, 0) || valuator_mask_isset(mask, 1)) {
1191         scr = miPointerGetScreen(device);
1192         if (scr) {
1193             int dx = 0, dy = 0;
1194 
1195             idx = scr->myNum;
1196 
1197             if (valuator_mask_isset(mask, 0)) {
1198                 dx = valuator_mask_get(mask, 0);
1199                 if (is_absolute)
1200                     dx -= device->last.valuators[0];
1201                 else if (valuator_mask_has_unaccelerated(mask))
1202                     dx = valuator_mask_get_unaccelerated(mask, 0);
1203             }
1204 
1205             if (valuator_mask_isset(mask, 1)) {
1206                 dy = valuator_mask_get(mask, 1);
1207                 if (is_absolute)
1208                     dy -= device->last.valuators[1];
1209                 else if (valuator_mask_has_unaccelerated(mask))
1210                     dy = valuator_mask_get_unaccelerated(mask, 1);
1211             }
1212 
1213             if (DGAStealMotionEvent(device, idx, dx, dy))
1214                 stolen = 1;
1215         }
1216     }
1217 
1218     for (i = 2; i < valuator_mask_size(mask); i++) {
1219         AxisInfoPtr ax;
1220         double incr;
1221         int val, button;
1222 
1223         if (i >= device->valuator->numAxes)
1224             break;
1225 
1226         if (!valuator_mask_isset(mask, i))
1227             continue;
1228 
1229         ax = &device->valuator->axes[i];
1230 
1231         if (ax->scroll.type == SCROLL_TYPE_NONE)
1232             continue;
1233 
1234         if (!scr) {
1235             scr = miPointerGetScreen(device);
1236             if (!scr)
1237                 break;
1238             idx = scr->myNum;
1239         }
1240 
1241         incr = ax->scroll.increment;
1242         val = valuator_mask_get(mask, i);
1243 
1244         if (ax->scroll.type == SCROLL_TYPE_VERTICAL) {
1245             if (incr * val < 0)
1246                 button = 4; /* up */
1247             else
1248                 button = 5; /* down */
1249         } else { /* SCROLL_TYPE_HORIZONTAL */
1250             if (incr * val < 0)
1251                 button = 6; /* left */
1252             else
1253                 button = 7; /* right */
1254         }
1255 
1256         if (DGAStealButtonEvent(device, idx, button, 1) &&
1257                 DGAStealButtonEvent(device, idx, button, 0))
1258             stolen = 1;
1259     }
1260 
1261 #endif
1262 
1263     return stolen;
1264 }
1265 
1266 void
xf86PostMotionEventM(DeviceIntPtr device,int is_absolute,const ValuatorMask * mask)1267 xf86PostMotionEventM(DeviceIntPtr device,
1268                      int is_absolute, const ValuatorMask *mask)
1269 {
1270     int flags = 0;
1271 
1272     if (xf86CheckMotionEvent4DGA(device, is_absolute, mask))
1273         return;
1274 
1275     if (valuator_mask_num_valuators(mask) > 0) {
1276         if (is_absolute)
1277             flags = POINTER_ABSOLUTE;
1278         else
1279             flags = POINTER_RELATIVE | POINTER_ACCELERATE;
1280     }
1281 
1282     QueuePointerEvents(device, MotionNotify, 0, flags, mask);
1283 }
1284 
1285 void
xf86PostProximityEvent(DeviceIntPtr device,int is_in,int first_valuator,int num_valuators,...)1286 xf86PostProximityEvent(DeviceIntPtr device,
1287                        int is_in, int first_valuator, int num_valuators, ...)
1288 {
1289     va_list var;
1290     int i;
1291     ValuatorMask mask;
1292 
1293     XI_VERIFY_VALUATORS(num_valuators);
1294 
1295     valuator_mask_zero(&mask);
1296     va_start(var, num_valuators);
1297     for (i = 0; i < num_valuators; i++)
1298         valuator_mask_set(&mask, first_valuator + i, va_arg(var, int));
1299 
1300     va_end(var);
1301 
1302     xf86PostProximityEventM(device, is_in, &mask);
1303 }
1304 
1305 void
xf86PostProximityEventP(DeviceIntPtr device,int is_in,int first_valuator,int num_valuators,const int * valuators)1306 xf86PostProximityEventP(DeviceIntPtr device,
1307                         int is_in,
1308                         int first_valuator,
1309                         int num_valuators, const int *valuators)
1310 {
1311     ValuatorMask mask;
1312 
1313     XI_VERIFY_VALUATORS(num_valuators);
1314 
1315     valuator_mask_set_range(&mask, first_valuator, num_valuators, valuators);
1316     xf86PostProximityEventM(device, is_in, &mask);
1317 }
1318 
1319 void
xf86PostProximityEventM(DeviceIntPtr device,int is_in,const ValuatorMask * mask)1320 xf86PostProximityEventM(DeviceIntPtr device,
1321                         int is_in, const ValuatorMask *mask)
1322 {
1323     QueueProximityEvents(device, is_in ? ProximityIn : ProximityOut, mask);
1324 }
1325 
1326 void
xf86PostButtonEvent(DeviceIntPtr device,int is_absolute,int button,int is_down,int first_valuator,int num_valuators,...)1327 xf86PostButtonEvent(DeviceIntPtr device,
1328                     int is_absolute,
1329                     int button,
1330                     int is_down, int first_valuator, int num_valuators, ...)
1331 {
1332     va_list var;
1333     ValuatorMask mask;
1334     int i = 0;
1335 
1336     XI_VERIFY_VALUATORS(num_valuators);
1337 
1338     valuator_mask_zero(&mask);
1339 
1340     va_start(var, num_valuators);
1341     for (i = 0; i < num_valuators; i++)
1342         valuator_mask_set(&mask, first_valuator + i, va_arg(var, int));
1343 
1344     va_end(var);
1345 
1346     xf86PostButtonEventM(device, is_absolute, button, is_down, &mask);
1347 }
1348 
1349 void
xf86PostButtonEventP(DeviceIntPtr device,int is_absolute,int button,int is_down,int first_valuator,int num_valuators,const int * valuators)1350 xf86PostButtonEventP(DeviceIntPtr device,
1351                      int is_absolute,
1352                      int button,
1353                      int is_down,
1354                      int first_valuator,
1355                      int num_valuators, const int *valuators)
1356 {
1357     ValuatorMask mask;
1358 
1359     XI_VERIFY_VALUATORS(num_valuators);
1360 
1361     valuator_mask_set_range(&mask, first_valuator, num_valuators, valuators);
1362     xf86PostButtonEventM(device, is_absolute, button, is_down, &mask);
1363 }
1364 
1365 void
xf86PostButtonEventM(DeviceIntPtr device,int is_absolute,int button,int is_down,const ValuatorMask * mask)1366 xf86PostButtonEventM(DeviceIntPtr device,
1367                      int is_absolute,
1368                      int button, int is_down, const ValuatorMask *mask)
1369 {
1370     int flags = 0;
1371 
1372     if (valuator_mask_num_valuators(mask) > 0) {
1373         if (is_absolute)
1374             flags = POINTER_ABSOLUTE;
1375         else
1376             flags = POINTER_RELATIVE | POINTER_ACCELERATE;
1377     }
1378 
1379 #ifdef XFreeXDGA
1380     if (miPointerGetScreen(device)) {
1381         int index = miPointerGetScreen(device)->myNum;
1382 
1383         if (DGAStealButtonEvent(device, index, button, is_down))
1384             return;
1385     }
1386 #endif
1387 
1388     QueuePointerEvents(device,
1389                        is_down ? ButtonPress : ButtonRelease, button,
1390                        flags, mask);
1391 }
1392 
1393 void
xf86PostKeyEvent(DeviceIntPtr device,unsigned int key_code,int is_down)1394 xf86PostKeyEvent(DeviceIntPtr device, unsigned int key_code, int is_down)
1395 {
1396     xf86PostKeyEventM(device, key_code, is_down);
1397 }
1398 
1399 void
xf86PostKeyEventP(DeviceIntPtr device,unsigned int key_code,int is_down)1400 xf86PostKeyEventP(DeviceIntPtr device,
1401                   unsigned int key_code,
1402                   int is_down)
1403 {
1404     xf86PostKeyEventM(device, key_code, is_down);
1405 }
1406 
1407 void
xf86PostKeyEventM(DeviceIntPtr device,unsigned int key_code,int is_down)1408 xf86PostKeyEventM(DeviceIntPtr device, unsigned int key_code, int is_down)
1409 {
1410 #ifdef XFreeXDGA
1411     DeviceIntPtr pointer;
1412 
1413     /* Some pointers send key events, paired device is wrong then. */
1414     pointer = GetMaster(device, POINTER_OR_FLOAT);
1415 
1416     if (miPointerGetScreen(pointer)) {
1417         int index = miPointerGetScreen(pointer)->myNum;
1418 
1419         if (DGAStealKeyEvent(device, index, key_code, is_down))
1420             return;
1421     }
1422 #endif
1423 
1424     QueueKeyboardEvents(device, is_down ? KeyPress : KeyRelease, key_code);
1425 }
1426 
1427 void
xf86PostKeyboardEvent(DeviceIntPtr device,unsigned int key_code,int is_down)1428 xf86PostKeyboardEvent(DeviceIntPtr device, unsigned int key_code, int is_down)
1429 {
1430     ValuatorMask mask;
1431 
1432     valuator_mask_zero(&mask);
1433     xf86PostKeyEventM(device, key_code, is_down);
1434 }
1435 
1436 InputInfoPtr
xf86FirstLocalDevice(void)1437 xf86FirstLocalDevice(void)
1438 {
1439     return xf86InputDevs;
1440 }
1441 
1442 /*
1443  * Cx     - raw data from touch screen
1444  * to_max - scaled highest dimension
1445  *          (remember, this is of rows - 1 because of 0 origin)
1446  * to_min  - scaled lowest dimension
1447  * from_max - highest raw value from touch screen calibration
1448  * from_min  - lowest raw value from touch screen calibration
1449  *
1450  * This function is the same for X or Y coordinates.
1451  * You may have to reverse the high and low values to compensate for
1452  * different orgins on the touch screen vs X.
1453  *
1454  * e.g. to scale from device coordinates into screen coordinates, call
1455  * xf86ScaleAxis(x, 0, screen_width, dev_min, dev_max);
1456  */
1457 
1458 int
xf86ScaleAxis(int Cx,int to_max,int to_min,int from_max,int from_min)1459 xf86ScaleAxis(int Cx, int to_max, int to_min, int from_max, int from_min)
1460 {
1461     int X;
1462     int64_t to_width = to_max - to_min;
1463     int64_t from_width = from_max - from_min;
1464 
1465     if (from_width) {
1466         X = (int) (((to_width * (Cx - from_min)) / from_width) + to_min);
1467     }
1468     else {
1469         X = 0;
1470         ErrorF("Divide by Zero in xf86ScaleAxis\n");
1471     }
1472 
1473     if (X > to_max)
1474         X = to_max;
1475     if (X < to_min)
1476         X = to_min;
1477 
1478     return X;
1479 }
1480 
1481 Bool
xf86InitValuatorAxisStruct(DeviceIntPtr dev,int axnum,Atom label,int minval,int maxval,int resolution,int min_res,int max_res,int mode)1482 xf86InitValuatorAxisStruct(DeviceIntPtr dev, int axnum, Atom label, int minval,
1483                            int maxval, int resolution, int min_res, int max_res,
1484                            int mode)
1485 {
1486     if (!dev || !dev->valuator)
1487         return FALSE;
1488 
1489     return InitValuatorAxisStruct(dev, axnum, label, minval, maxval, resolution,
1490                                   min_res, max_res, mode);
1491 }
1492 
1493 /*
1494  * Set the valuator values to be in sync with dix/event.c
1495  * DefineInitialRootWindow().
1496  */
1497 void
xf86InitValuatorDefaults(DeviceIntPtr dev,int axnum)1498 xf86InitValuatorDefaults(DeviceIntPtr dev, int axnum)
1499 {
1500     if (axnum == 0) {
1501         dev->valuator->axisVal[0] = screenInfo.screens[0]->width / 2;
1502         dev->last.valuators[0] = dev->valuator->axisVal[0];
1503     }
1504     else if (axnum == 1) {
1505         dev->valuator->axisVal[1] = screenInfo.screens[0]->height / 2;
1506         dev->last.valuators[1] = dev->valuator->axisVal[1];
1507     }
1508 }
1509 
1510 /**
1511  * Deactivate a device. Call this function from the driver if you receive a
1512  * read error or something else that spoils your day.
1513  * Device will be moved to the off_devices list, but it will still be there
1514  * until you really clean up after it.
1515  * Notifies the client about an inactive device.
1516  *
1517  * @param panic True if device is unrecoverable and needs to be removed.
1518  */
1519 void
xf86DisableDevice(DeviceIntPtr dev,Bool panic)1520 xf86DisableDevice(DeviceIntPtr dev, Bool panic)
1521 {
1522     if (!panic) {
1523         DisableDevice(dev, TRUE);
1524     }
1525     else {
1526         SendDevicePresenceEvent(dev->id, DeviceUnrecoverable);
1527         DeleteInputDeviceRequest(dev);
1528     }
1529 }
1530 
1531 /**
1532  * Reactivate a device. Call this function from the driver if you just found
1533  * out that the read error wasn't quite that bad after all.
1534  * Device will be re-activated, and an event sent to the client.
1535  */
1536 void
xf86EnableDevice(DeviceIntPtr dev)1537 xf86EnableDevice(DeviceIntPtr dev)
1538 {
1539     EnableDevice(dev, TRUE);
1540 }
1541 
1542 /**
1543  * Post a touch event with optional valuators.  If this is the first touch in
1544  * the sequence, at least x & y valuators must be provided. The driver is
1545  * responsible for maintaining the correct event sequence (TouchBegin, TouchUpdate,
1546  * TouchEnd). Submitting an update or end event for a unregistered touchid will
1547  * result in errors.
1548  * Touch IDs may be reused by the driver but only after a TouchEnd has been
1549  * submitted for that touch ID.
1550  *
1551  * @param dev The device to post the event for
1552  * @param touchid The touchid of the current touch event. Must be an
1553  * existing ID for TouchUpdate or TouchEnd events
1554  * @param type One of XI_TouchBegin, XI_TouchUpdate, XI_TouchEnd
1555  * @param flags Flags for this event
1556  * @param The valuator mask with all valuators set for this event.
1557  */
1558 void
xf86PostTouchEvent(DeviceIntPtr dev,uint32_t touchid,uint16_t type,uint32_t flags,const ValuatorMask * mask)1559 xf86PostTouchEvent(DeviceIntPtr dev, uint32_t touchid, uint16_t type,
1560                    uint32_t flags, const ValuatorMask *mask)
1561 {
1562 
1563     QueueTouchEvents(dev, type, touchid, flags, mask);
1564 }
1565 
1566 void
xf86InputEnableVTProbe(void)1567 xf86InputEnableVTProbe(void)
1568 {
1569     int is_auto = 0;
1570     DeviceIntPtr pdev;
1571     PausedInputDevicePtr d, tmp;
1572 
1573     xorg_list_for_each_entry_safe(d, tmp, &new_input_devices_list, node) {
1574         InputInfoPtr pInfo = d->pInfo;
1575         const char *value = xf86findOptionValue(pInfo->options, "_source");
1576 
1577         is_auto = 0;
1578         if (value &&
1579             (strcmp(value, "server/hal") == 0 ||
1580              strcmp(value, "server/udev") == 0 ||
1581              strcmp(value, "server/wscons") == 0))
1582             is_auto = 1;
1583 
1584         xf86NewInputDevice(pInfo, &pdev,
1585                                   (!is_auto ||
1586                                    (is_auto && xf86Info.autoEnableDevices)));
1587         xorg_list_del(&d->node);
1588         free(d);
1589     }
1590 }
1591 
1592 /* end of xf86Xinput.c */
1593