• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2013-2019 Red Hat, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "util-prop-parsers.h"
25 
26 #include <libevdev/libevdev.h>
27 #include <string.h>
28 
29 #include "util-macros.h"
30 #include "util-strings.h"
31 
32 /* Helper function to parse the mouse DPI tag from udev.
33  * The tag is of the form:
34  * MOUSE_DPI=400 *1000 2000
35  * or
36  * MOUSE_DPI=400@125 *1000@125 2000@125
37  * Where the * indicates the default value and @number indicates device poll
38  * rate.
39  * Numbers should be in ascending order, and if rates are present they should
40  * be present for all entries.
41  *
42  * When parsing the mouse DPI property, if we find an error we just return 0
43  * since it's obviously invalid, the caller will treat that as an error and
44  * use a reasonable default instead. If the property contains multiple DPI
45  * settings but none flagged as default, we return the last because we're
46  * lazy and that's a silly way to set the property anyway.
47  *
48  * @param prop The value of the udev property (without the MOUSE_DPI=)
49  * @return The default dpi value on success, 0 on error
50  */
51 int
parse_mouse_dpi_property(const char * prop)52 parse_mouse_dpi_property(const char *prop)
53 {
54 	bool is_default = false;
55 	int nread, dpi = 0, rate;
56 
57 	if (!prop)
58 		return 0;
59 
60 	while (*prop != 0) {
61 		if (*prop == ' ') {
62 			prop++;
63 			continue;
64 		}
65 		if (*prop == '*') {
66 			prop++;
67 			is_default = true;
68 			if (!isdigit(prop[0]))
69 				return 0;
70 		}
71 
72 		/* While we don't do anything with the rate right now we
73 		 * will validate that, if it's present, it is non-zero and
74 		 * positive
75 		 */
76 		rate = 1;
77 		nread = 0;
78 		sscanf(prop, "%d@%d%n", &dpi, &rate, &nread);
79 		if (!nread)
80 			sscanf(prop, "%d%n", &dpi, &nread);
81 		if (!nread || dpi <= 0 || rate <= 0 || prop[nread] == '@')
82 			return 0;
83 
84 		if (is_default)
85 			break;
86 		prop += nread;
87 	}
88 	return dpi;
89 }
90 
91 /**
92  * Helper function to parse the MOUSE_WHEEL_CLICK_COUNT property from udev.
93  * Property is of the form:
94  * MOUSE_WHEEL_CLICK_COUNT=<integer>
95  * Where the number indicates the number of wheel clicks per 360 deg
96  * rotation.
97  *
98  * @param prop The value of the udev property (without the MOUSE_WHEEL_CLICK_COUNT=)
99  * @return The click count of the wheel (may be negative) or 0 on error.
100  */
101 int
parse_mouse_wheel_click_count_property(const char * prop)102 parse_mouse_wheel_click_count_property(const char *prop)
103 {
104 	int count = 0;
105 
106 	if (!prop)
107 		return 0;
108 
109 	if (!safe_atoi(prop, &count) || abs(count) > 360)
110 		return 0;
111 
112         return count;
113 }
114 
115 /**
116  *
117  * Helper function to parse the MOUSE_WHEEL_CLICK_ANGLE property from udev.
118  * Property is of the form:
119  * MOUSE_WHEEL_CLICK_ANGLE=<integer>
120  * Where the number indicates the degrees travelled for each click.
121  *
122  * @param prop The value of the udev property (without the MOUSE_WHEEL_CLICK_ANGLE=)
123  * @return The angle of the wheel (may be negative) or 0 on error.
124  */
125 int
parse_mouse_wheel_click_angle_property(const char * prop)126 parse_mouse_wheel_click_angle_property(const char *prop)
127 {
128 	int angle = 0;
129 
130 	if (!prop)
131 		return 0;
132 
133 	if (!safe_atoi(prop, &angle) || abs(angle) > 360)
134 		return 0;
135 
136         return angle;
137 }
138 
139 /**
140  * Parses a simple dimension string in the form of "10x40". The two
141  * numbers must be positive integers in decimal notation.
142  * On success, the two numbers are stored in w and h. On failure, w and h
143  * are unmodified.
144  *
145  * @param prop The value of the property
146  * @param w Returns the first component of the dimension
147  * @param h Returns the second component of the dimension
148  * @return true on success, false otherwise
149  */
150 bool
parse_dimension_property(const char * prop,size_t * w,size_t * h)151 parse_dimension_property(const char *prop, size_t *w, size_t *h)
152 {
153 	int x, y;
154 
155 	if (!prop)
156 		return false;
157 
158 	if (sscanf(prop, "%dx%d", &x, &y) != 2)
159 		return false;
160 
161 	if (x <= 0 || y <= 0)
162 		return false;
163 
164 	*w = (size_t)x;
165 	*h = (size_t)y;
166 	return true;
167 }
168 
169 /**
170  * Parses a set of 6 space-separated floats.
171  *
172  * @param prop The string value of the property
173  * @param calibration Returns the six components
174  * @return true on success, false otherwise
175  */
176 bool
parse_calibration_property(const char * prop,float calibration_out[6])177 parse_calibration_property(const char *prop, float calibration_out[6])
178 {
179 	int idx;
180 	char **strv;
181 	float calibration[6];
182 
183 	if (!prop)
184 		return false;
185 
186 	strv = strv_from_string(prop, " ");
187 	if (!strv)
188 		return false;
189 
190 	for (idx = 0; idx < 6; idx++) {
191 		double v;
192 		if (strv[idx] == NULL || !safe_atod(strv[idx], &v)) {
193 			strv_free(strv);
194 			return false;
195 		}
196 
197 		calibration[idx] = v;
198 	}
199 
200 	strv_free(strv);
201 
202 	memcpy(calibration_out, calibration, sizeof(calibration));
203 
204 	return true;
205 }
206 
207 bool
parse_switch_reliability_property(const char * prop,enum switch_reliability * reliability)208 parse_switch_reliability_property(const char *prop,
209 				  enum switch_reliability *reliability)
210 {
211 	if (!prop) {
212 		*reliability = RELIABILITY_UNKNOWN;
213 		return true;
214 	}
215 
216 	if (streq(prop, "reliable"))
217 		*reliability = RELIABILITY_RELIABLE;
218 	else if (streq(prop, "write_open"))
219 		*reliability = RELIABILITY_WRITE_OPEN;
220 	else
221 		return false;
222 
223 	return true;
224 }
225 
226 /**
227  * Parses a string with the allowed values: "below"
228  * The value refers to the position of the touchpad (relative to the
229  * keyboard, i.e. your average laptop would be 'below')
230  *
231  * @param prop The value of the property
232  * @param layout The layout
233  * @return true on success, false otherwise
234  */
235 bool
parse_tpkbcombo_layout_poperty(const char * prop,enum tpkbcombo_layout * layout)236 parse_tpkbcombo_layout_poperty(const char *prop,
237 			       enum tpkbcombo_layout *layout)
238 {
239 	if (!prop)
240 		return false;
241 
242 	if (streq(prop, "below")) {
243 		*layout = TPKBCOMBO_LAYOUT_BELOW;
244 		return true;
245 	}
246 
247 	return false;
248 }
249 
250 /**
251  * Parses a string of the format "a:b" where both a and b must be integer
252  * numbers and a > b. Also allowed is the special string value "none" which
253  * amounts to unsetting the property.
254  *
255  * @param prop The value of the property
256  * @param hi Set to the first digit or 0 in case of 'none'
257  * @param lo Set to the second digit or 0 in case of 'none'
258  * @return true on success, false otherwise
259  */
260 bool
parse_range_property(const char * prop,int * hi,int * lo)261 parse_range_property(const char *prop, int *hi, int *lo)
262 {
263 	int first, second;
264 
265 	if (!prop)
266 		return false;
267 
268 	if (streq(prop, "none")) {
269 		*hi = 0;
270 		*lo = 0;
271 		return true;
272 	}
273 
274 	if (sscanf(prop, "%d:%d", &first, &second) != 2)
275 		return false;
276 
277 	if (second >= first)
278 		return false;
279 
280 	*hi = first;
281 	*lo = second;
282 
283 	return true;
284 }
285 
286 bool
parse_boolean_property(const char * prop,bool * b)287 parse_boolean_property(const char *prop, bool *b)
288 {
289 	if (!prop)
290 		return false;
291 
292 	if (streq(prop, "1"))
293 		*b = true;
294 	else if (streq(prop, "0"))
295 		*b = false;
296 	else
297 		return false;
298 
299 	return true;
300 }
301 
302 static bool
parse_evcode_string(const char * s,int * type_out,int * code_out)303 parse_evcode_string(const char *s, int *type_out, int *code_out)
304 {
305 	int type, code;
306 
307 	if (strneq(s, "EV_", 3)) {
308 		type = libevdev_event_type_from_name(s);
309 		if (type == -1)
310 			return false;
311 
312 		code = EVENT_CODE_UNDEFINED;
313 	} else {
314 		struct map {
315 			const char *str;
316 			int type;
317 		} map[] = {
318 			{ "KEY_", EV_KEY },
319 			{ "BTN_", EV_KEY },
320 			{ "ABS_", EV_ABS },
321 			{ "REL_", EV_REL },
322 			{ "SW_", EV_SW },
323 		};
324 		struct map *m;
325 		bool found = false;
326 
327 		ARRAY_FOR_EACH(map, m) {
328 			if (!strstartswith(s, m->str))
329 				continue;
330 
331 			type = m->type;
332 			code = libevdev_event_code_from_name(type, s);
333 			if (code == -1)
334 				return false;
335 
336 			found = true;
337 			break;
338 		}
339 		if (!found)
340 			return false;
341 	}
342 
343 	*type_out = type;
344 	*code_out = code;
345 
346 	return true;
347 }
348 
349 /**
350  * Parses a string of the format "EV_ABS;KEY_A;BTN_TOOL_DOUBLETAP;ABS_X;"
351  * where each element must be a named event type OR a named event code OR a
352  * tuple in the form of EV_KEY:0x123, i.e. a named event type followed by a
353  * hex event code.
354  *
355  * events must point to an existing array of size nevents.
356  * nevents specifies the size of the array in events and returns the number
357  * of items, elements exceeding nevents are simply ignored, just make sure
358  * events is large enough for your use-case.
359  *
360  * The results are returned as input events with type and code set, all
361  * other fields undefined. Where only the event type is specified, the code
362  * is set to EVENT_CODE_UNDEFINED.
363  *
364  * On success, events contains nevents events.
365  */
366 bool
parse_evcode_property(const char * prop,struct input_event * events,size_t * nevents)367 parse_evcode_property(const char *prop, struct input_event *events, size_t *nevents)
368 {
369 	char **strv = NULL;
370 	bool rc = false;
371 	size_t ncodes = 0;
372 	size_t idx;
373 	/* A randomly chosen max so we avoid crazy quirks */
374 	struct input_event evs[32];
375 
376 	memset(evs, 0, sizeof evs);
377 
378 	strv = strv_from_string(prop, ";");
379 	if (!strv)
380 		goto out;
381 
382 	for (idx = 0; strv[idx]; idx++)
383 		ncodes++;
384 
385 	if (ncodes == 0 || ncodes > ARRAY_LENGTH(evs))
386 		goto out;
387 
388 	ncodes = min(*nevents, ncodes);
389 	for (idx = 0; strv[idx]; idx++) {
390 		char *s = strv[idx];
391 
392 		int type, code;
393 
394 		if (strstr(s, ":") == NULL) {
395 			if (!parse_evcode_string(s, &type, &code))
396 				goto out;
397 		} else {
398 			int consumed;
399 			char stype[13] = {0}; /* EV_FF_STATUS + '\0' */
400 
401 			if (sscanf(s, "%12[A-Z_]:%x%n", stype, &code, &consumed) != 2 ||
402 			    strlen(s) != (size_t)consumed ||
403 			    (type = libevdev_event_type_from_name(stype)) == -1 ||
404 			    code < 0 || code > libevdev_event_type_get_max(type))
405 			    goto out;
406 		}
407 
408 		evs[idx].type = type;
409 		evs[idx].code = code;
410 	}
411 
412 	memcpy(events, evs, ncodes * sizeof *events);
413 	*nevents = ncodes;
414 	rc = true;
415 
416 out:
417 	strv_free(strv);
418 	return rc;
419 }
420 
421 /**
422  * Parses a string of the format "INPUT_PROP_BUTTONPAD;INPUT_PROP_POINTER;0x123;"
423  * where each element must be a named input prop OR a hexcode in the form
424  * 0x1234
425  *
426  * props must point to an existing array of size nprops.
427  * nprops specifies the size of the array in props and returns the number
428  * of elements, elements exceeding nprops are simply ignored, just make sure
429  * props is large enough for your use-case.
430  *
431  * On success, props contains nprops elements.
432  */
433 bool
parse_input_prop_property(const char * prop,unsigned int * props_out,size_t * nprops)434 parse_input_prop_property(const char *prop, unsigned int *props_out, size_t *nprops)
435 {
436 	char **strv = NULL;
437 	bool rc = false;
438 	size_t count = 0;
439 	size_t idx;
440 	unsigned int props[INPUT_PROP_CNT]; /* doubling up on quirks is a bug */
441 
442 	strv = strv_from_string(prop, ";");
443 	if (!strv)
444 		goto out;
445 
446 	for (idx = 0; strv[idx]; idx++)
447 		count++;
448 
449 	if (count == 0 || count > ARRAY_LENGTH(props))
450 		goto out;
451 
452 	count = min(*nprops, count);
453 	for (idx = 0; strv[idx]; idx++) {
454 		char *s = strv[idx];
455 		unsigned int prop;
456 
457 		if (safe_atou_base(s, &prop, 16)) {
458 			if (prop > INPUT_PROP_MAX)
459 				goto out;
460 		} else {
461 			int val = libevdev_property_from_name(s);
462 			if (val == -1)
463 				goto out;
464 			prop = (unsigned int)val;
465 		}
466 		props[idx] = prop;
467 	}
468 
469 	memcpy(props_out, props, count * sizeof *props);
470 	*nprops = count;
471 	rc = true;
472 
473 out:
474 	strv_free(strv);
475 	return rc;
476 }
477 
478 /**
479  * Parse the property value for the EVDEV_ABS_00 properties. Spec is
480  *  EVDEV_ABS_00=min:max:res:fuzz:flat
481  * where any element may be empty and subsequent elements may not be
482  * present. So we have to parse
483  *  EVDEV_ABS_00=min:max:res
484  *  EVDEV_ABS_00=::res
485  *  EVDEV_ABS_00=::res:fuzz:
486  *
487  * Returns a mask of the bits set and the absinfo struct with the values.
488  * The abs value for an unset bit is undefined.
489  */
490 uint32_t
parse_evdev_abs_prop(const char * prop,struct input_absinfo * abs)491 parse_evdev_abs_prop(const char *prop, struct input_absinfo *abs)
492 {
493 	char *str = safe_strdup(prop);
494 	char *current, *next;
495 	uint32_t mask = 0;
496 	int bit = ABS_MASK_MIN;
497 	int *val;
498 	int values[5];
499 
500 	/* basic sanity check: 5 digits for min/max, 3 for resolution, fuzz,
501 	 * flat and the colons. That's plenty, anything over is garbage */
502 	if (strlen(prop) > 24)
503 		goto out;
504 
505 	current = str;
506 	val = values;
507 	while (current && *current != '\0' && bit <= ABS_MASK_FLAT) {
508 		if (*current != ':') {
509 			int v;
510 			next = index(current, ':');
511 			if (next)
512 				*next = '\0';
513 
514 			if (!safe_atoi(current, &v)) {
515 				mask = 0;
516 				goto out;
517 			}
518 			*val = v;
519 			mask |= bit;
520 			current = next ? ++next : NULL;
521 		} else {
522 			current++;
523 		}
524 		bit <<= 1;
525 		val++;
526 	}
527 
528 	if (mask & ABS_MASK_MIN)
529 		abs->minimum = values[0];
530 	if (mask & ABS_MASK_MAX)
531 		abs->maximum = values[1];
532 	if (mask & ABS_MASK_RES)
533 		abs->resolution = values[2];
534 	if (mask & ABS_MASK_FUZZ)
535 		abs->fuzz = values[3];
536 	if (mask & ABS_MASK_FLAT)
537 		abs->flat = values[4];
538 
539 out:
540 	free(str);
541 
542 	return mask;
543 }
544