]> granicus.if.org Git - strace/blob - evdev.c
tests: factor out common shell code to functions
[strace] / evdev.c
1 /*
2  * Copyright (c) 2015 Etienne Gemsa <etienne.gemsa@lse.epita.fr>
3  * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "defs.h"
30
31 #ifdef HAVE_LINUX_INPUT_H
32 #include <linux/input.h>
33 #include "xlat/evdev_abs.h"
34 #include "xlat/evdev_autorepeat.h"
35 #include "xlat/evdev_ff_status.h"
36 #include "xlat/evdev_ff_types.h"
37 #include "xlat/evdev_keycode.h"
38 #include "xlat/evdev_leds.h"
39 #include "xlat/evdev_misc.h"
40 #include "xlat/evdev_mtslots.h"
41 #include "xlat/evdev_prop.h"
42 #include "xlat/evdev_relative_axes.h"
43 #include "xlat/evdev_snd.h"
44 #include "xlat/evdev_switch.h"
45 #include "xlat/evdev_sync.h"
46
47 #ifndef SYN_MAX
48 # define SYN_MAX 0xf
49 #endif
50
51 static void
52 decode_envelope(struct ff_envelope *envelope)
53 {
54         tprintf(", envelope={attack_length=%" PRIu16 ", attack_level=%" PRIu16
55                 ", fade_length=%" PRIu16 ", fade_level=%" PRIx32 "}",
56                 envelope->attack_length,
57                 envelope->attack_level,
58                 envelope->fade_length,
59                 envelope->fade_level);
60 }
61
62 static int
63 ff_effect_ioctl(struct tcb *tcp, long arg)
64 {
65         struct ff_effect ffe;
66
67         if (!verbose(tcp) || umove(tcp, arg, &ffe) < 0)
68                 return 0;
69
70         tprints(", {type=");
71         printxval(evdev_ff_types, ffe.type, "FF_???");
72         tprintf(", id=%" PRIu16 ", direction=%" PRIu16,
73                 ffe.id, ffe.direction);
74
75         if (!abbrev(tcp)) {
76                 tprintf(", trigger={button=%" PRIu16 ", interval=%" PRIu16 "}",
77                         ffe.trigger.button, ffe.trigger.interval);
78                 tprintf(", replay={lenght=%" PRIu16 ", delay=%" PRIu16 "}",
79                         ffe.replay.length, ffe.replay.delay);
80                 switch (ffe.type) {
81                         case FF_CONSTANT:
82                                 tprintf(", constant_ef={%" PRIi16,
83                                         ffe.u.constant.level);
84                                 decode_envelope(&ffe.u.constant.envelope);
85                                 tprints("}");
86                                 return 1;
87                         case FF_RAMP:
88                                 tprintf(", ramp={start_level=%" PRIi16
89                                         ", end_level=%" PRIi16,
90                                         ffe.u.ramp.start_level,
91                                         ffe.u.ramp.end_level);
92                                 decode_envelope(&ffe.u.ramp.envelope);
93                                 tprints("}");
94                                 return 1;
95                         case FF_PERIODIC:
96                                 tprintf(", periodic_ef={waveform=%" PRIu16
97                                         ", period=%" PRIu16
98                                         ", magnitude=%" PRIi16
99                                         ", offset=%" PRIi16
100                                         ", phase=%" PRIu16,
101                                         ffe.u.periodic.waveform,
102                                         ffe.u.periodic.period,
103                                         ffe.u.periodic.magnitude,
104                                         ffe.u.periodic.offset,
105                                         ffe.u.periodic.phase);
106                                 decode_envelope(&ffe.u.periodic.envelope);
107                                 tprintf(", custom_len=%" PRIu32
108                                         ", *custom_data=%#lx}",
109                                         ffe.u.periodic.custom_len,
110                                         (unsigned long)ffe.u.periodic.custom_data);
111                                 return 1;
112                         case FF_RUMBLE:
113                                 tprintf(", rumble={strong_magnitude=%" PRIu16
114                                         ", weak_magnitude=%" PRIu16 "}",
115                                         ffe.u.rumble.strong_magnitude,
116                                         ffe.u.rumble.weak_magnitude);
117                                 return 1;
118                         case FF_SPRING:
119                         case FF_FRICTION:
120                         case FF_DAMPER:
121                         case FF_INERTIA:
122                         case FF_CUSTOM:
123                                 break;
124                         default :
125                                 break;
126                 }
127         }
128
129         tprints(", ...}");
130         return 1;
131 }
132
133 static int
134 abs_ioctl(struct tcb *tcp, long arg)
135 {
136         struct input_absinfo absinfo;
137
138         if (!verbose(tcp) || umove(tcp, arg, &absinfo) < 0)
139                 return 0;
140
141         tprintf(", {value=%" PRIu32 ", minimum=%" PRIu32,
142                 absinfo.value, absinfo.minimum);
143         if (!abbrev(tcp)) {
144                 tprintf(", maximum=%" PRIu32 ", fuzz=%" PRIu32,
145                         absinfo.maximum, absinfo.fuzz);
146                 tprintf(", flat=%" PRIu32, absinfo.flat);
147 #ifdef HAVE_STRUCT_INPUT_ABSINFO_RESOLUTION
148                 tprintf(", resolution=%" PRIu32, absinfo.resolution);
149 #endif
150                 tprints("}");
151         } else {
152                 tprints(", ...}");
153         }
154         return 1;
155 }
156
157 static int
158 keycode_ioctl(struct tcb *tcp, long arg)
159 {
160         unsigned int keycode[2];
161
162         if (!arg) {
163                 tprints(", NULL");
164                 return 1;
165         }
166
167         if (!verbose(tcp) || umove(tcp, arg, &keycode) < 0)
168                 return 0;
169
170         tprintf(", [%u, ", keycode[0]);
171         printxval(evdev_keycode, keycode[1], "KEY_???");
172         tprints("]");
173         return 1;
174 }
175
176 #ifdef EVIOCGKEYCODE_V2
177 static int
178 keycode_V2_ioctl(struct tcb *tcp, long arg)
179 {
180         struct input_keymap_entry ike;
181
182         if (!arg) {
183                 tprints(", NULL");
184                 return 1;
185         }
186
187         if (!verbose(tcp) || umove(tcp, arg, &ike) < 0)
188                 return 0;
189
190         tprintf(", {flags=%" PRIu8 ", len=%" PRIu8, ike.flags, ike.len);
191         if (!abbrev(tcp)) {
192                 unsigned int i;
193
194                 tprintf(", index=%" PRIu16 ", keycode=", ike.index);
195                 printxval(evdev_keycode, ike.keycode, "KEY_???");
196                 tprints(", scancode=[");
197                 for (i = 0; i < ARRAY_SIZE(ike.scancode); i++) {
198                         if (i > 0)
199                                 tprints(", ");
200                         tprintf("%" PRIx8, ike.scancode[i]);
201                 }
202                 tprints("]}");
203         } else {
204                 tprints(", ...}");
205         }
206         return 1;
207 }
208 #endif /* EVIOCGKEYCODE_V2 */
209
210 static int
211 getid_ioctl(struct tcb *tcp, long arg)
212 {
213         struct input_id id;
214
215         if (!verbose(tcp) || umove(tcp, arg, &id) < 0)
216                 return 0;
217
218         tprintf(", {ID_BUS=%" PRIu16 ", ID_VENDOR=%" PRIu16,
219                 id.bustype, id.vendor);
220         if (!abbrev(tcp)) {
221                 tprintf(", ID_PRODUCT=%" PRIu16 ", ID_VERSION=%" PRIu16 "}",
222                         id.product, id.version);
223         } else {
224                 tprints(", ...}");
225         }
226         return 1;
227 }
228
229 static int
230 decode_bitset(struct tcb *tcp, long arg, const struct xlat decode_nr[],
231               const unsigned int max_nr, const char *dflt)
232 {
233         if (!verbose(tcp))
234                 return 0;
235
236         unsigned int size;
237         if ((unsigned long) tcp->u_rval > max_nr)
238                 size = max_nr;
239         else
240                 size = tcp->u_rval;
241         char decoded_arg[size];
242
243         if (umoven(tcp, arg, size, decoded_arg) < 0)
244                 return 0;
245
246         tprints(", [");
247
248         int bit_displayed = 0;
249         int i = next_set_bit(decoded_arg, 0, size);
250         if (i < 0) {
251                 tprints(" 0 ");
252         } else {
253                 printxval(decode_nr, i, dflt);
254
255                 while ((i = next_set_bit(decoded_arg, i + 1, size)) > 0) {
256                         if (abbrev(tcp) && bit_displayed >= 3) {
257                                 tprints(", ...");
258                                 break;
259                         }
260                         tprints(", ");
261                         printxval(decode_nr, i, dflt);
262                         bit_displayed++;
263                 }
264         }
265
266         tprints("]");
267
268         return 1;
269 }
270
271 #ifdef EVIOCGMTSLOTS
272 static int
273 mtslots_ioctl(struct tcb *tcp, const unsigned int code, long arg)
274 {
275         const size_t size = _IOC_SIZE(code) / sizeof(int32_t);
276         if (!size)
277                 return 0;
278
279         int32_t buffer[size];
280
281         if (!verbose(tcp) || umove(tcp, arg, &buffer) < 0)
282                 return 0;
283
284         tprints(", {code=");
285         printxval(evdev_mtslots, buffer[0], "ABS_MT_???");
286
287         unsigned int i;
288         tprints(", values=[");
289
290         for (i = 1; i < ARRAY_SIZE(buffer); i++)
291                 tprintf("%s%d", i > 1 ? ", " : "", buffer[i]);
292
293         tprints("]}");
294         return 1;
295 }
296 #endif /* EVIOCGMTSLOTS */
297
298 #ifdef EVIOCGREP
299 static int
300 repeat_ioctl(struct tcb *tcp, long arg)
301 {
302         unsigned int val[2];
303
304         if (!verbose(tcp) || umove(tcp, arg, &val) < 0)
305                 return 0;
306
307         tprintf(", [%" PRIu32 " %" PRIu32 "]", val[0], val[1]);
308         return 1;
309 }
310 #endif /* EVIOCGREP */
311
312 static int
313 evdev_read_ioctl(struct tcb *tcp, const unsigned int code, long arg)
314 {
315         if (entering(tcp))
316                 return 1;
317
318         if (syserror(tcp))
319                 return 0;
320
321         if ((_IOC_NR(code) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0))) {
322                 switch (_IOC_NR(code) - 0x20) {
323                         case EV_SYN:
324                                 return decode_bitset(tcp, arg, evdev_sync,
325                                                 SYN_MAX, "SYN_???");
326                         case EV_KEY:
327                                 return decode_bitset(tcp, arg, evdev_keycode,
328                                                 KEY_MAX, "KEY_???");
329                         case EV_REL:
330                                 return decode_bitset(tcp, arg, evdev_relative_axes,
331                                                 REL_MAX, "REL_???");
332                         case EV_ABS:
333                                 return decode_bitset(tcp, arg,
334                                                 evdev_abs, ABS_MAX, "ABS_???");
335                         case EV_MSC:
336                                 return decode_bitset(tcp, arg,
337                                                 evdev_misc, MSC_MAX, "MSC_???");
338 #ifdef EV_SW
339                         case EV_SW:
340                                 return decode_bitset(tcp, arg,
341                                                 evdev_switch, SW_MAX, "SW_???");
342 #endif
343                         case EV_LED:
344                                 return decode_bitset(tcp, arg,
345                                                 evdev_leds, LED_MAX, "LED_???");
346                         case EV_SND:
347                                 return decode_bitset(tcp, arg,
348                                                 evdev_snd, SND_MAX, "SND_???");
349                         case EV_REP:
350                                 return decode_bitset(tcp, arg, evdev_autorepeat,
351                                                 REP_MAX, "REP_???");
352                         case EV_FF:
353                                 return decode_bitset(tcp, arg, evdev_ff_types,
354                                                 FF_MAX, "FF_???");
355                         case EV_PWR:
356                                 printnum_int(tcp, arg, "%d");
357                                 return 1;
358                         case EV_FF_STATUS:
359                                 return decode_bitset(tcp, arg, evdev_ff_status,
360                                                 FF_STATUS_MAX, "FF_STATUS_???");
361                         default:
362                                 return 0;
363                 }
364         }
365
366         if ((_IOC_NR(code) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0)))
367                 return abs_ioctl(tcp, arg);
368
369         switch (code) {
370                 case EVIOCGVERSION:
371                         tprints(", ");
372                         printnum_int(tcp, arg, "%" PRIx32);
373                         return 1;
374                 case EVIOCGEFFECTS:
375                         tprints(", ");
376                         printnum_int(tcp, arg, "%" PRIu32);
377                         return 1;
378                 case EVIOCGID:
379                         return getid_ioctl(tcp, arg);
380 #ifdef EVIOCGREP
381                 case EVIOCGREP:
382                         return repeat_ioctl(tcp, arg);;
383 #endif
384                 case EVIOCGKEYCODE:
385                         return keycode_ioctl(tcp, arg);
386 #ifdef EVIOCGKEYCODE_V2
387                 case EVIOCGKEYCODE_V2:
388                         return keycode_V2_ioctl(tcp, arg);
389 #endif
390         }
391
392         switch (_IOC_NR(code)) {
393 #ifdef EVIOCGMTSLOTS
394                 case _IOC_NR(EVIOCGMTSLOTS(0)):
395                         return mtslots_ioctl(tcp, code, arg);
396 #endif
397                 case _IOC_NR(EVIOCGNAME(0)):
398                 case _IOC_NR(EVIOCGPHYS(0)):
399                 case _IOC_NR(EVIOCGUNIQ(0)):
400                         tprints(", ");
401                         printstr(tcp, arg, tcp->u_rval - 1);
402                         return 1;
403 #ifdef EVIOCGPROP
404                 case _IOC_NR(EVIOCGPROP(0)):
405                         return decode_bitset(tcp, arg,
406                                         evdev_prop, INPUT_PROP_MAX, "PROP_???");
407 #endif
408                 case _IOC_NR(EVIOCGSND(0)):
409                         return decode_bitset(tcp, arg,
410                                         evdev_snd, SND_MAX, "SND_???");
411 #ifdef EVIOCGSW
412                 case _IOC_NR(EVIOCGSW(0)):
413                         return decode_bitset(tcp, arg,
414                                         evdev_switch, SW_MAX, "SW_???");
415 #endif
416                 case _IOC_NR(EVIOCGKEY(0)):
417                         return decode_bitset(tcp, arg,
418                                         evdev_keycode, KEY_MAX, "KEY_???");
419                 case _IOC_NR(EVIOCGLED(0)):
420                         return decode_bitset(tcp, arg,
421                                         evdev_leds, LED_MAX, "LED_???");
422                 default:
423                         return 0;
424         }
425 }
426
427 static int
428 evdev_write_ioctl(struct tcb *tcp, const unsigned int code, long arg)
429 {
430         if (exiting(tcp))
431                 return 1;
432
433         if ((_IOC_NR(code) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0)))
434                 return abs_ioctl(tcp, arg);
435
436         switch (code) {
437 #ifdef EVIOCSREP
438                 case EVIOCSREP:
439                         return repeat_ioctl(tcp, arg);
440 #endif
441                 case EVIOCSKEYCODE:
442                         return keycode_ioctl(tcp, arg);
443 #ifdef EVIOCSKEYCODE_V2
444                 case EVIOCSKEYCODE_V2:
445                         return keycode_V2_ioctl(tcp, arg);
446 #endif
447                 case EVIOCSFF:
448                         return ff_effect_ioctl(tcp, arg);
449                 case EVIOCRMFF:
450 #ifdef EVIOCSCLOCKID
451                 case EVIOCSCLOCKID:
452 #endif
453                 case EVIOCGRAB:
454 #ifdef EVIOCREVOKE
455                 case EVIOCREVOKE:
456 #endif
457                         tprints(", ");
458                         printnum_int(tcp, arg, "%u");
459                         return 1;
460                 default:
461                         return 0;
462         }
463 }
464
465 int
466 evdev_ioctl(struct tcb *tcp, const unsigned int code, long arg)
467 {
468         switch(_IOC_DIR(code)) {
469                 case _IOC_READ:
470                         return evdev_read_ioctl(tcp, code, arg);
471                 case _IOC_WRITE:
472                         if (!evdev_write_ioctl(tcp, code, arg))
473                                 tprintf(", %lx", arg);
474                         return 1;
475                 default:
476                         return 0;
477         }
478 }
479
480 #endif /* HAVE_LINUX_INPUT_H */