From e6c49319e1b84287f8fa07559b820cdf4cb5bc7b Mon Sep 17 00:00:00 2001 From: Tim Blechmann Date: Fri, 9 Dec 2005 12:50:06 +0000 Subject: improved find_hid searching capabilities svn path=/trunk/abstractions/tb/; revision=4179 --- find_hid-help.pd | 8 +++++--- find_hid.py | 56 ++++++++++++++++++++++++++++++++++++++++++++------------ le3dp.pd | 22 +++++++++++----------- 3 files changed, 60 insertions(+), 26 deletions(-) diff --git a/find_hid-help.pd b/find_hid-help.pd index 11f31c7..a219d68 100644 --- a/find_hid-help.pd +++ b/find_hid-help.pd @@ -2,9 +2,11 @@ #X obj 51 100 py find_hid find; #X obj 51 122 print; #X msg 51 65 reload; -#X msg 158 69 find USB-PS/2 Optical Mouse; -#X text 53 239 $Id: find_hid-help.pd 192 2005-11-20 00:54:05Z tim $; +#X text 53 239 $Id: find_hid-help.pd 209 2005-12-09 12:50:36Z tim $ +; #X text 49 153 find device in linux udevfs; +#X msg 158 69 "manufacturer=Logitech" "product=USB-PS/2 Optical Mouse" +; #X connect 0 0 1 0; #X connect 2 0 0 0; -#X connect 3 0 0 1; +#X connect 5 0 0 1; diff --git a/find_hid.py b/find_hid.py index 15b56ff..99f474e 100644 --- a/find_hid.py +++ b/find_hid.py @@ -16,28 +16,60 @@ # the Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA. # -# $Id: find_hid.py,v 1.1 2005-12-04 18:11:08 timblech Exp $ +# $Id: find_hid.py,v 1.2 2005-12-09 12:50:06 timblech Exp $ # from os import popen, listdir +def parse_device(event, rules): + pipe = popen('udevinfo -a -p /sys/class/input/%s' % event) + + line = pipe.readline() + while line: + line.strip() + if '==' in line: + setting, value = line.split('==') + setting = setting.strip() + if '{' in setting: + setting = setting.split('{')[1].split('}')[0] + value = value.strip().strip('"') + + if setting in rules: + if rules [setting] == value: + # we matched one rule, so we remove it from the pending rules + del rules[setting] + + line = pipe.readline() + + if len(rules) == 0: + pipe.close() + print event + return float(event.strip("event")) + + pipe.close() + return -1 + def find (*args): name = "" for token in args: name+=" " + str(token) + name = name.strip() + rules = name.split('" "') + rules = map (lambda x: x.strip('"'), rules) - events = filter(lambda x: "event" in x, listdir('/sys/class/input/')) - - for event in events: - pipe = popen('udevinfo -a -p /sys/class/input/%s' % event) + ruledict = dict() - line = pipe.readline() - while line: - if name in line: - print event - return float(event.strip("event")) + for rule in rules: + setting, value = rule.split('=',1) + ruledict[setting] = value + rules = ruledict - line = pipe.readline() - pipe.close() + events = filter(lambda x: "event" in x, listdir('/sys/class/input/')) + + for event in events: + ret = parse_device(event, dict(rules)) + if ret != -1: + return int(ret) + return -1 diff --git a/le3dp.pd b/le3dp.pd index 918d0fd..1c025ab 100644 --- a/le3dp.pd +++ b/le3dp.pd @@ -109,7 +109,6 @@ btn_8 btn_9 btn_10 btn_11; #X connect 1 11 3 0; #X connect 1 12 14 0; #X restore 51 266 pd buttons; -#X msg 38 60 find Logitech Extreme 3D; #X obj 38 172 prepend open; #X obj 38 84 py find_hid; #X obj 8 39 t f b; @@ -120,16 +119,17 @@ btn_8 btn_9 btn_10 btn_11; #X text 12 294 $Id: le3dp.pd 194 2005-11-20 01:17:05Z tim $; #X text 116 220 abstraction to use a Logitech Extreme 3D Joystick with hid under linux; -#X connect 0 0 7 0; +#X msg 38 60 find "product=Logitech Extreme 3D"; +#X connect 0 0 6 0; #X connect 2 0 1 0; #X connect 2 1 3 0; -#X connect 4 0 6 1; +#X connect 4 0 7 0; #X connect 5 0 8 0; -#X connect 6 0 9 0; -#X connect 7 0 11 0; -#X connect 7 1 4 0; -#X connect 8 0 2 0; -#X connect 9 0 10 0; -#X connect 9 1 5 0; -#X connect 10 0 11 1; -#X connect 11 0 8 0; +#X connect 6 0 10 0; +#X connect 6 1 13 0; +#X connect 7 0 2 0; +#X connect 8 0 9 0; +#X connect 8 1 4 0; +#X connect 9 0 10 1; +#X connect 10 0 7 0; +#X connect 13 0 5 1; -- cgit v1.2.1