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 vaule "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 static bool
parse_evcode_string(const char * s,int * type_out,int * code_out)287 parse_evcode_string(const char *s, int *type_out, int *code_out)
288 {
289 int type, code;
290
291 if (strneq(s, "EV_", 3)) {
292 type = libevdev_event_type_from_name(s);
293 if (type == -1)
294 return false;
295
296 code = EVENT_CODE_UNDEFINED;
297 } else {
298 struct map {
299 const char *str;
300 int type;
301 } map[] = {
302 { "KEY_", EV_KEY },
303 { "BTN_", EV_KEY },
304 { "ABS_", EV_ABS },
305 { "REL_", EV_REL },
306 { "SW_", EV_SW },
307 };
308 struct map *m;
309 bool found = false;
310
311 ARRAY_FOR_EACH(map, m) {
312 if (!strstartswith(s, m->str))
313 continue;
314
315 type = m->type;
316 code = libevdev_event_code_from_name(type, s);
317 if (code == -1)
318 return false;
319
320 found = true;
321 break;
322 }
323 if (!found)
324 return false;
325 }
326
327 *type_out = type;
328 *code_out = code;
329
330 return true;
331 }
332
333 /**
334 * Parses a string of the format "EV_ABS;KEY_A;BTN_TOOL_DOUBLETAP;ABS_X;"
335 * where each element must be a named event type OR a named event code OR a
336 * tuple in the form of EV_KEY:0x123, i.e. a named event type followed by a
337 * hex event code.
338 *
339 * events must point to an existing array of size nevents.
340 * nevents specifies the size of the array in events and returns the number
341 * of items, elements exceeding nevents are simply ignored, just make sure
342 * events is large enough for your use-case.
343 *
344 * The results are returned as input events with type and code set, all
345 * other fields undefined. Where only the event type is specified, the code
346 * is set to EVENT_CODE_UNDEFINED.
347 *
348 * On success, events contains nevents events.
349 */
350 bool
parse_evcode_property(const char * prop,struct input_event * events,size_t * nevents)351 parse_evcode_property(const char *prop, struct input_event *events, size_t *nevents)
352 {
353 char **strv = NULL;
354 bool rc = false;
355 size_t ncodes = 0;
356 size_t idx;
357 /* A randomly chosen max so we avoid crazy quirks */
358 struct input_event evs[32];
359
360 memset(evs, 0, sizeof evs);
361
362 strv = strv_from_string(prop, ";");
363 if (!strv)
364 goto out;
365
366 for (idx = 0; strv[idx]; idx++)
367 ncodes++;
368
369 if (ncodes == 0 || ncodes > ARRAY_LENGTH(evs))
370 goto out;
371
372 ncodes = min(*nevents, ncodes);
373 for (idx = 0; strv[idx]; idx++) {
374 char *s = strv[idx];
375
376 int type, code;
377
378 if (strstr(s, ":") == NULL) {
379 if (!parse_evcode_string(s, &type, &code))
380 goto out;
381 } else {
382 int consumed;
383 char stype[13] = {0}; /* EV_FF_STATUS + '\0' */
384
385 if (sscanf(s, "%12[A-Z_]:%x%n", stype, &code, &consumed) != 2 ||
386 strlen(s) != (size_t)consumed ||
387 (type = libevdev_event_type_from_name(stype)) == -1 ||
388 code < 0 || code > libevdev_event_type_get_max(type))
389 goto out;
390 }
391
392 evs[idx].type = type;
393 evs[idx].code = code;
394 }
395
396 memcpy(events, evs, ncodes * sizeof *events);
397 *nevents = ncodes;
398 rc = true;
399
400 out:
401 strv_free(strv);
402 return rc;
403 }
404
405 /**
406 * Parse the property value for the EVDEV_ABS_00 properties. Spec is
407 * EVDEV_ABS_00=min:max:res:fuzz:flat
408 * where any element may be empty and subsequent elements may not be
409 * present. So we have to parse
410 * EVDEV_ABS_00=min:max:res
411 * EVDEV_ABS_00=::res
412 * EVDEV_ABS_00=::res:fuzz:
413 *
414 * Returns a mask of the bits set and the absinfo struct with the values.
415 * The abs value for an unset bit is undefined.
416 */
417 uint32_t
parse_evdev_abs_prop(const char * prop,struct input_absinfo * abs)418 parse_evdev_abs_prop(const char *prop, struct input_absinfo *abs)
419 {
420 char *str = strdup(prop);
421 char *current, *next;
422 uint32_t mask = 0;
423 int bit = ABS_MASK_MIN;
424 int *val;
425 int values[5];
426
427 /* basic sanity check: 5 digits for min/max, 3 for resolution, fuzz,
428 * flat and the colons. That's plenty, anything over is garbage */
429 if (strlen(prop) > 24)
430 goto out;
431
432 current = str;
433 val = values;
434 while (current && *current != '\0' && bit <= ABS_MASK_FLAT) {
435 if (*current != ':') {
436 int v;
437 next = index(current, ':');
438 if (next)
439 *next = '\0';
440
441 if (!safe_atoi(current, &v)) {
442 mask = 0;
443 goto out;
444 }
445 *val = v;
446 mask |= bit;
447 current = next ? ++next : NULL;
448 } else {
449 current++;
450 }
451 bit <<= 1;
452 val++;
453 }
454
455 if (mask & ABS_MASK_MIN)
456 abs->minimum = values[0];
457 if (mask & ABS_MASK_MAX)
458 abs->maximum = values[1];
459 if (mask & ABS_MASK_RES)
460 abs->resolution = values[2];
461 if (mask & ABS_MASK_FUZZ)
462 abs->fuzz = values[3];
463 if (mask & ABS_MASK_FLAT)
464 abs->flat = values[4];
465
466 out:
467 free(str);
468
469 return mask;
470 }
471