chiark / gitweb /
extras/keymap/findkeyboards: beautify shell code and get rid of grep
[elogind.git] / extras / keymap / findkeyboards
1 #!/bin/sh -e
2 # Find "real" keyboard devices and print their device path.
3 # Author: Martin Pitt <martin.pitt@ubuntu.com>
4 #
5 # Copyright (C) 2009, Canonical Ltd.
6 #
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16
17 # returns OK if $1 contains $2
18 strstr() {
19     [ "${1#*$2*}" != "$1" ]
20 }
21
22 # returns OK if $1 contains $2 at the beginning
23 str_starts() {
24     [ "${1#$2*}" != "$1" ]
25 }
26
27 str_line_starts() {
28     while read a; do str_starts "$a" "$1" && return 0;done
29     return 1;
30 }
31
32 # print a list of input devices which are keyboard-like
33 keyboard_devices() {
34     # standard AT keyboard
35     for dev in `udevadm trigger --dry-run --verbose --property-match=ID_INPUT_KEYBOARD=1`; do
36         walk=`udevadm info --attribute-walk --path=$dev`
37         env=`udevadm info --query=env --path=$dev`
38         # filter out non-event devices, such as the parent input devices which
39         # have no devnode
40         if ! echo "$env" | str_line_starts 'DEVNAME='; then
41             continue
42         fi
43         if strstr "$walk" 'DRIVERS=="atkbd"'; then
44             echo -n 'AT keyboard: '
45         elif echo "$env" | str_line_starts 'ID_USB_DRIVER=usbhid'; then
46             echo -n 'USB keyboard: '
47         else
48             echo -n 'Unknown type: '
49         fi
50         udevadm info --query=name --path=$dev
51     done
52
53     # modules
54     module=$(udevadm trigger --verbose --dry-run --subsystem-match=input --attr-match=name='*Extra Buttons')
55     module="$module
56 $(udevadm trigger --verbose --dry-run --subsystem-match=input --attr-match=name='*extra buttons')"
57     module="$module
58 $(udevadm trigger --verbose --dry-run --subsystem-match=input --attr-match=name='Sony Vaio Keys')"
59     for m in $module; do
60         for evdev in $m/event*/dev; do
61             if [ -e "$evdev" ]; then
62                 echo -n 'module: '
63                 udevadm info --query=name --path=${evdev%%/dev}
64             fi
65         done
66     done
67 }
68
69 keyboard_devices