1 /*
2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
4 * Copyright © 2013-2015 Red Hat, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26 /*
27 * This list data structure is verbatim copy from wayland-util.h from the
28 * Wayland project; except that wl_ prefix has been removed.
29 */
30
31 #include "config.h"
32
33 #include <ctype.h>
34 #include <locale.h>
35 #include <stdarg.h>
36 #include <stdbool.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <libevdev/libevdev.h>
40
41 #include "libinput-util.h"
42 #include "libinput-private.h"
43
44 void
list_init(struct list * list)45 list_init(struct list *list)
46 {
47 list->prev = list;
48 list->next = list;
49 }
50
51 void
list_insert(struct list * list,struct list * elm)52 list_insert(struct list *list, struct list *elm)
53 {
54 assert((list->next != NULL && list->prev != NULL) ||
55 !"list->next|prev is NULL, possibly missing list_init()");
56 assert(((elm->next == NULL && elm->prev == NULL) || list_empty(elm)) ||
57 !"elm->next|prev is not NULL, list node used twice?");
58
59 elm->prev = list;
60 elm->next = list->next;
61 list->next = elm;
62 elm->next->prev = elm;
63 }
64
65 void
list_append(struct list * list,struct list * elm)66 list_append(struct list *list, struct list *elm)
67 {
68 assert((list->next != NULL && list->prev != NULL) ||
69 !"list->next|prev is NULL, possibly missing list_init()");
70 assert(((elm->next == NULL && elm->prev == NULL) || list_empty(elm)) ||
71 !"elm->next|prev is not NULL, list node used twice?");
72
73 elm->next = list;
74 elm->prev = list->prev;
75 list->prev = elm;
76 elm->prev->next = elm;
77 }
78
79 void
list_remove(struct list * elm)80 list_remove(struct list *elm)
81 {
82 assert((elm->next != NULL && elm->prev != NULL) ||
83 !"list->next|prev is NULL, possibly missing list_init()");
84
85 elm->prev->next = elm->next;
86 elm->next->prev = elm->prev;
87 elm->next = NULL;
88 elm->prev = NULL;
89 }
90
91 bool
list_empty(const struct list * list)92 list_empty(const struct list *list)
93 {
94 assert((list->next != NULL && list->prev != NULL) ||
95 !"list->next|prev is NULL, possibly missing list_init()");
96
97 return list->next == list;
98 }
99
100 void
ratelimit_init(struct ratelimit * r,uint64_t ival_us,unsigned int burst)101 ratelimit_init(struct ratelimit *r, uint64_t ival_us, unsigned int burst)
102 {
103 r->interval = ival_us;
104 r->begin = 0;
105 r->burst = burst;
106 r->num = 0;
107 }
108
109 /*
110 * Perform rate-limit test. Returns RATELIMIT_PASS if the rate-limited action
111 * is still allowed, RATELIMIT_THRESHOLD if the limit has been reached with
112 * this call, and RATELIMIT_EXCEEDED if you're beyond the threshold.
113 * It's safe to treat the return-value as boolean, if you're not interested in
114 * the exact state. It evaluates to "true" if the threshold hasn't been
115 * exceeded, yet.
116 *
117 * The ratelimit object must be initialized via ratelimit_init().
118 *
119 * Modelled after Linux' lib/ratelimit.c by Dave Young
120 * <hidave.darkstar@gmail.com>, which is licensed GPLv2.
121 */
122 enum ratelimit_state
ratelimit_test(struct ratelimit * r)123 ratelimit_test(struct ratelimit *r)
124 {
125 struct timespec ts;
126 uint64_t utime;
127
128 if (r->interval <= 0 || r->burst <= 0)
129 return RATELIMIT_PASS;
130
131 clock_gettime(CLOCK_MONOTONIC, &ts);
132 utime = s2us(ts.tv_sec) + ns2us(ts.tv_nsec);
133
134 if (r->begin <= 0 || r->begin + r->interval < utime) {
135 /* reset counter */
136 r->begin = utime;
137 r->num = 1;
138 return RATELIMIT_PASS;
139 } else if (r->num < r->burst) {
140 /* continue burst */
141 return (++r->num == r->burst) ? RATELIMIT_THRESHOLD
142 : RATELIMIT_PASS;
143 }
144
145 return RATELIMIT_EXCEEDED;
146 }
147
148 /* Helper function to parse the mouse DPI tag from udev.
149 * The tag is of the form:
150 * MOUSE_DPI=400 *1000 2000
151 * or
152 * MOUSE_DPI=400@125 *1000@125 2000@125
153 * Where the * indicates the default value and @number indicates device poll
154 * rate.
155 * Numbers should be in ascending order, and if rates are present they should
156 * be present for all entries.
157 *
158 * When parsing the mouse DPI property, if we find an error we just return 0
159 * since it's obviously invalid, the caller will treat that as an error and
160 * use a reasonable default instead. If the property contains multiple DPI
161 * settings but none flagged as default, we return the last because we're
162 * lazy and that's a silly way to set the property anyway.
163 *
164 * @param prop The value of the udev property (without the MOUSE_DPI=)
165 * @return The default dpi value on success, 0 on error
166 */
167 int
parse_mouse_dpi_property(const char * prop)168 parse_mouse_dpi_property(const char *prop)
169 {
170 bool is_default = false;
171 int nread, dpi = 0, rate;
172
173 if (!prop)
174 return 0;
175
176 while (*prop != 0) {
177 if (*prop == ' ') {
178 prop++;
179 continue;
180 }
181 if (*prop == '*') {
182 prop++;
183 is_default = true;
184 if (!isdigit(prop[0]))
185 return 0;
186 }
187
188 /* While we don't do anything with the rate right now we
189 * will validate that, if it's present, it is non-zero and
190 * positive
191 */
192 rate = 1;
193 nread = 0;
194 sscanf(prop, "%d@%d%n", &dpi, &rate, &nread);
195 if (!nread)
196 sscanf(prop, "%d%n", &dpi, &nread);
197 if (!nread || dpi <= 0 || rate <= 0 || prop[nread] == '@')
198 return 0;
199
200 if (is_default)
201 break;
202 prop += nread;
203 }
204 return dpi;
205 }
206
207 /**
208 * Helper function to parse the MOUSE_WHEEL_CLICK_COUNT property from udev.
209 * Property is of the form:
210 * MOUSE_WHEEL_CLICK_COUNT=<integer>
211 * Where the number indicates the number of wheel clicks per 360 deg
212 * rotation.
213 *
214 * @param prop The value of the udev property (without the MOUSE_WHEEL_CLICK_COUNT=)
215 * @return The click count of the wheel (may be negative) or 0 on error.
216 */
217 int
parse_mouse_wheel_click_count_property(const char * prop)218 parse_mouse_wheel_click_count_property(const char *prop)
219 {
220 int count = 0;
221
222 if (!prop)
223 return 0;
224
225 if (!safe_atoi(prop, &count) || abs(count) > 360)
226 return 0;
227
228 return count;
229 }
230
231 /**
232 *
233 * Helper function to parse the MOUSE_WHEEL_CLICK_ANGLE property from udev.
234 * Property is of the form:
235 * MOUSE_WHEEL_CLICK_ANGLE=<integer>
236 * Where the number indicates the degrees travelled for each click.
237 *
238 * @param prop The value of the udev property (without the MOUSE_WHEEL_CLICK_ANGLE=)
239 * @return The angle of the wheel (may be negative) or 0 on error.
240 */
241 int
parse_mouse_wheel_click_angle_property(const char * prop)242 parse_mouse_wheel_click_angle_property(const char *prop)
243 {
244 int angle = 0;
245
246 if (!prop)
247 return 0;
248
249 if (!safe_atoi(prop, &angle) || abs(angle) > 360)
250 return 0;
251
252 return angle;
253 }
254
255 /**
256 * Parses a simple dimension string in the form of "10x40". The two
257 * numbers must be positive integers in decimal notation.
258 * On success, the two numbers are stored in w and h. On failure, w and h
259 * are unmodified.
260 *
261 * @param prop The value of the property
262 * @param w Returns the first component of the dimension
263 * @param h Returns the second component of the dimension
264 * @return true on success, false otherwise
265 */
266 bool
parse_dimension_property(const char * prop,size_t * w,size_t * h)267 parse_dimension_property(const char *prop, size_t *w, size_t *h)
268 {
269 int x, y;
270
271 if (!prop)
272 return false;
273
274 if (sscanf(prop, "%dx%d", &x, &y) != 2)
275 return false;
276
277 if (x <= 0 || y <= 0)
278 return false;
279
280 *w = (size_t)x;
281 *h = (size_t)y;
282 return true;
283 }
284
285 /**
286 * Parses a set of 6 space-separated floats.
287 *
288 * @param prop The string value of the property
289 * @param calibration Returns the six components
290 * @return true on success, false otherwise
291 */
292 bool
parse_calibration_property(const char * prop,float calibration_out[6])293 parse_calibration_property(const char *prop, float calibration_out[6])
294 {
295 int idx;
296 char **strv;
297 float calibration[6];
298
299 if (!prop)
300 return false;
301
302 strv = strv_from_string(prop, " ");
303 if (!strv)
304 return false;
305
306 for (idx = 0; idx < 6; idx++) {
307 double v;
308 if (strv[idx] == NULL || !safe_atod(strv[idx], &v)) {
309 strv_free(strv);
310 return false;
311 }
312
313 calibration[idx] = v;
314 }
315
316 strv_free(strv);
317
318 memcpy(calibration_out, calibration, sizeof(calibration));
319
320 return true;
321 }
322
323 bool
parse_switch_reliability_property(const char * prop,enum switch_reliability * reliability)324 parse_switch_reliability_property(const char *prop,
325 enum switch_reliability *reliability)
326 {
327 if (!prop) {
328 *reliability = RELIABILITY_UNKNOWN;
329 return true;
330 }
331
332 if (streq(prop, "reliable"))
333 *reliability = RELIABILITY_RELIABLE;
334 else if (streq(prop, "write_open"))
335 *reliability = RELIABILITY_WRITE_OPEN;
336 else
337 return false;
338
339 return true;
340 }
341
342 /**
343 * Parses a string with the allowed values: "below"
344 * The value refers to the position of the touchpad (relative to the
345 * keyboard, i.e. your average laptop would be 'below')
346 *
347 * @param prop The value of the property
348 * @param layout The layout
349 * @return true on success, false otherwise
350 */
351 bool
parse_tpkbcombo_layout_poperty(const char * prop,enum tpkbcombo_layout * layout)352 parse_tpkbcombo_layout_poperty(const char *prop,
353 enum tpkbcombo_layout *layout)
354 {
355 if (!prop)
356 return false;
357
358 if (streq(prop, "below")) {
359 *layout = TPKBCOMBO_LAYOUT_BELOW;
360 return true;
361 }
362
363 return false;
364 }
365
366 /**
367 * Parses a string of the format "a:b" where both a and b must be integer
368 * numbers and a > b. Also allowed is the special string vaule "none" which
369 * amounts to unsetting the property.
370 *
371 * @param prop The value of the property
372 * @param hi Set to the first digit or 0 in case of 'none'
373 * @param lo Set to the second digit or 0 in case of 'none'
374 * @return true on success, false otherwise
375 */
376 bool
parse_range_property(const char * prop,int * hi,int * lo)377 parse_range_property(const char *prop, int *hi, int *lo)
378 {
379 int first, second;
380
381 if (!prop)
382 return false;
383
384 if (streq(prop, "none")) {
385 *hi = 0;
386 *lo = 0;
387 return true;
388 }
389
390 if (sscanf(prop, "%d:%d", &first, &second) != 2)
391 return false;
392
393 if (second >= first)
394 return false;
395
396 *hi = first;
397 *lo = second;
398
399 return true;
400 }
401
402 static bool
parse_evcode_string(const char * s,int * type_out,int * code_out)403 parse_evcode_string(const char *s, int *type_out, int *code_out)
404 {
405 int type, code;
406
407 if (strneq(s, "EV_", 3)) {
408 type = libevdev_event_type_from_name(s);
409 if (type == -1)
410 return false;
411
412 code = EVENT_CODE_UNDEFINED;
413 } else {
414 struct map {
415 const char *str;
416 int type;
417 } map[] = {
418 { "KEY_", EV_KEY },
419 { "BTN_", EV_KEY },
420 { "ABS_", EV_ABS },
421 { "REL_", EV_REL },
422 { "SW_", EV_SW },
423 };
424 struct map *m;
425 bool found = false;
426
427 ARRAY_FOR_EACH(map, m) {
428 if (!strneq(s, m->str, strlen(m->str)))
429 continue;
430
431 type = m->type;
432 code = libevdev_event_code_from_name(type, s);
433 if (code == -1)
434 return false;
435
436 found = true;
437 break;
438 }
439 if (!found)
440 return false;
441 }
442
443 *type_out = type;
444 *code_out = code;
445
446 return true;
447 }
448
449 /**
450 * Parses a string of the format "EV_ABS;KEY_A;BTN_TOOL_DOUBLETAP;ABS_X;"
451 * where each element must be a named event type OR a named event code OR a
452 * tuple in the form of EV_KEY:0x123, i.e. a named event type followed by a
453 * hex event code.
454 *
455 * events must point to an existing array of size nevents.
456 * nevents specifies the size of the array in events and returns the number
457 * of items, elements exceeding nevents are simply ignored, just make sure
458 * events is large enough for your use-case.
459 *
460 * The results are returned as input events with type and code set, all
461 * other fields undefined. Where only the event type is specified, the code
462 * is set to EVENT_CODE_UNDEFINED.
463 *
464 * On success, events contains nevents events.
465 */
466 bool
parse_evcode_property(const char * prop,struct input_event * events,size_t * nevents)467 parse_evcode_property(const char *prop, struct input_event *events, size_t *nevents)
468 {
469 char **strv = NULL;
470 bool rc = false;
471 size_t ncodes = 0;
472 size_t idx;
473 struct input_event evs[*nevents];
474
475 memset(evs, 0, sizeof evs);
476
477 strv = strv_from_string(prop, ";");
478 if (!strv)
479 goto out;
480
481 for (idx = 0; strv[idx]; idx++)
482 ncodes++;
483
484 /* A randomly chosen max so we avoid crazy quirks */
485 if (ncodes == 0 || ncodes > 32)
486 goto out;
487
488 ncodes = min(*nevents, ncodes);
489 for (idx = 0; strv[idx]; idx++) {
490 char *s = strv[idx];
491
492 int type, code;
493
494 if (strstr(s, ":") == NULL) {
495 if (!parse_evcode_string(s, &type, &code))
496 goto out;
497 } else {
498 int consumed;
499 char stype[13] = {0}; /* EV_FF_STATUS + '\0' */
500
501 if (sscanf(s, "%12[A-Z_]:%x%n", stype, &code, &consumed) != 2 ||
502 strlen(s) != (size_t)consumed ||
503 (type = libevdev_event_type_from_name(stype)) == -1 ||
504 code < 0 || code > libevdev_event_type_get_max(type))
505 goto out;
506 }
507
508 evs[idx].type = type;
509 evs[idx].code = code;
510 }
511
512 memcpy(events, evs, ncodes * sizeof *events);
513 *nevents = ncodes;
514 rc = true;
515
516 out:
517 strv_free(strv);
518 return rc;
519 }
520
521 /**
522 * Return the next word in a string pointed to by state before the first
523 * separator character. Call repeatedly to tokenize a whole string.
524 *
525 * @param state Current state
526 * @param len String length of the word returned
527 * @param separators List of separator characters
528 *
529 * @return The first word in *state, NOT null-terminated
530 */
531 static const char *
next_word(const char ** state,size_t * len,const char * separators)532 next_word(const char **state, size_t *len, const char *separators)
533 {
534 const char *next = *state;
535 size_t l;
536
537 if (!*next)
538 return NULL;
539
540 next += strspn(next, separators);
541 if (!*next) {
542 *state = next;
543 return NULL;
544 }
545
546 l = strcspn(next, separators);
547 *state = next + l;
548 *len = l;
549
550 return next;
551 }
552
553 /**
554 * Return a null-terminated string array with the tokens in the input
555 * string, e.g. "one two\tthree" with a separator list of " \t" will return
556 * an array [ "one", "two", "three", NULL ].
557 *
558 * Use strv_free() to free the array.
559 *
560 * @param in Input string
561 * @param separators List of separator characters
562 *
563 * @return A null-terminated string array or NULL on errors
564 */
565 char **
strv_from_string(const char * in,const char * separators)566 strv_from_string(const char *in, const char *separators)
567 {
568 const char *s, *word;
569 char **strv = NULL;
570 int nelems = 0, idx;
571 size_t l;
572
573 assert(in != NULL);
574
575 s = in;
576 while ((word = next_word(&s, &l, separators)) != NULL)
577 nelems++;
578
579 if (nelems == 0)
580 return NULL;
581
582 nelems++; /* NULL-terminated */
583 strv = zalloc(nelems * sizeof *strv);
584
585 idx = 0;
586
587 s = in;
588 while ((word = next_word(&s, &l, separators)) != NULL) {
589 char *copy = strndup(word, l);
590 if (!copy) {
591 strv_free(strv);
592 return NULL;
593 }
594
595 strv[idx++] = copy;
596 }
597
598 return strv;
599 }
600
601 /**
602 * Return a newly allocated string with all elements joined by the
603 * joiner, same as Python's string.join() basically.
604 * A strv of ["one", "two", "three", NULL] with a joiner of ", " results
605 * in "one, two, three".
606 *
607 * An empty strv ([NULL]) returns NULL, same for passing NULL as either
608 * argument.
609 *
610 * @param strv Input string arrray
611 * @param joiner Joiner between the elements in the final string
612 *
613 * @return A null-terminated string joining all elements
614 */
615 char *
strv_join(char ** strv,const char * joiner)616 strv_join(char **strv, const char *joiner)
617 {
618 char **s;
619 char *str;
620 size_t slen = 0;
621 size_t count = 0;
622
623 if (!strv || !joiner)
624 return NULL;
625
626 if (strv[0] == NULL)
627 return NULL;
628
629 for (s = strv, count = 0; *s; s++, count++) {
630 slen += strlen(*s);
631 }
632
633 assert(slen < 1000);
634 assert(strlen(joiner) < 1000);
635 assert(count > 0);
636 assert(count < 100);
637
638 slen += (count - 1) * strlen(joiner);
639
640 str = zalloc(slen + 1); /* trailing \0 */
641 for (s = strv; *s; s++) {
642 strcat(str, *s);
643 --count;
644 if (count > 0)
645 strcat(str, joiner);
646 }
647
648 return str;
649 }
650