/usr/share/pyshared/freevo/evdev.py is in python-freevo 1.9.2b2-4.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | # -*- coding: iso-8859-1 -*-
# -----------------------------------------------------------------------
# Linux /dev/input/event interface library
# -----------------------------------------------------------------------
# $Id: evdev.py 11149 2008-11-06 20:16:47Z duncan $
#
# Notes:
# Todo:
#
# -----------------------------------------------------------------------
# Freevo - A Home Theater PC framework
# Copyright (C) 2002 Krister Lagerstrom, et al.
# Please see the file freevo/Docs/CREDITS for a complete list of authors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# -----------------------------------------------------------------------
"""
Linux /dev/input/event interface library
"""
import sys
import os
import re
from fcntl import ioctl
import struct
_types = {}
_events = {}
_ids = {}
_buses = {}
# Copied from asm-generic/ioctl.h
_IOC_NRBITS = 8
_IOC_TYPEBITS = 8
_IOC_SIZEBITS = 14
_IOC_DIRBITS = 2
_IOC_NRMASK = ((1 << _IOC_NRBITS)-1)
_IOC_TYPEMASK = ((1 << _IOC_TYPEBITS)-1)
_IOC_SIZEMASK = ((1 << _IOC_SIZEBITS)-1)
_IOC_DIRMASK = ((1 << _IOC_DIRBITS)-1)
_IOC_NRSHIFT = 0
_IOC_TYPESHIFT = (_IOC_NRSHIFT+_IOC_NRBITS)
_IOC_SIZESHIFT = (_IOC_TYPESHIFT+_IOC_TYPEBITS)
_IOC_DIRSHIFT = (_IOC_SIZESHIFT+_IOC_SIZEBITS)
_IOC_NONE = 0
_IOC_WRITE = 1
_IOC_READ = 2
def _IOC(dir,type,nr,size):
ioc = (((dir) << _IOC_DIRSHIFT) | \
(ord(type) << _IOC_TYPESHIFT) | \
((nr) << _IOC_NRSHIFT) | \
((size) << _IOC_SIZESHIFT))
if ioc >= 2**31:
ioc = int(ioc - 2**32)
return ioc
def _IO(type,nr):
return _IOC(_IOC_NONE,(type),(nr),0)
def _IOR(type,nr,size):
return _IOC(_IOC_READ,(type),(nr),(size))
def _IOW(type,nr,size):
return _IOC(_IOC_WRITE,(type),(nr),(size))
def _IOWR(type,nr,size):
return _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),(size))
EVIOCGVERSION = _IOR('E', 0x01, 4) # get driver version
EVIOCGID = _IOR('E', 0x02, 8) # get device ID
def EVIOCGNAME(len):
return _IOR('E', 0x06, len) # get device name
def EVIOCGPHYS(len):
return _IOR('E', 0x07, len) # get physical location
def EVIOCGUNIQ(len):
return _IOR('E', 0x08, len) # get unique identifier
def EVIOCGBIT(ev,len):
return _IOR('E', 0x20 + ev, len) # get event bits
def EVIOCGABS(abs):
return _IOR('E', 0x40 + abs, 20) # get abs value/limits
def EVIOCSABS(abs):
return _IOW('E', 0xc0 + abs, 20) # set abs value/limits
class evdev:
"""
Interface to the Linux event device
@ivar _fd: file descriptor of the event device
@type _fd: open file descriptor
"""
def __init__(self, dev, blocking=False):
"""
Initialize an instance of evdev.
"""
#print 'evdev.__init__(dev, blocking=False)'
self._fd = None
if blocking:
self._fd = os.open(dev, os.O_RDONLY)
else:
self._fd = os.open(dev, os.O_RDONLY | os.O_NDELAY)
self.get_events()
def __del__(self):
"""
Delete an instance of evdev.
"""
#print '__del__(self=%r)' % (self.__dict__)
try:
self.close()
except Exception, why:
pass
def close(self):
"""
Close the open resouces of an instance of evdev.
"""
#print 'close(self=%r)' % (self,)
if self._fd is not None:
os.close(self._fd)
self._fd = None
def print_info(self):
"""
Print the information about an event device
"""
#print 'print_info()'
print 'Input driver version %d.%d.%d' % self.get_version()
devid = self.get_id()
print 'Device ID: bus %(bus)s vendor 0x%(vendor)04x product 0x%(product)04x version 0x%(version)04x' % devid
print 'Device name: "' + self.get_name() + '"'
print 'Device location: "' + self.get_location() + '"'
def print_events(self):
"""
"""
#print 'print_events()'
print 'Supported events:'
keys = self._events.keys()
keys.sort()
for key in keys:
print ' Event type %s (%d):' % (_types[key], key)
self._events[key].sort()
for event in self._events[key]:
try:
print ' Event %s (%d)' % (_events[key][event], event)
except KeyError:
print ' Event ??? (%d)' % event
def get_version(self):
"""
"""
#print 'get_version()'
buf = ioctl(self._fd, EVIOCGVERSION, ' ')
l, = struct.unpack('I', buf)
return (l >> 16, (l >> 8) & 0xff, l & 0xff)
def get_id(self):
"""
"""
#print 'get_id()'
buf = ioctl(self._fd, EVIOCGID, ' ' * 8)
bus, vendor, product, version = struct.unpack('HHHH', buf)
return { 'bus':_buses[bus], 'vendor':vendor, 'product':product, 'version':version }
def get_name(self):
"""
"""
#print 'get_name()'
buf = ioctl(self._fd, EVIOCGNAME(1024), ' ' * 1024)
null = buf.find('\0')
return buf[:null]
def get_location(self):
"""
"""
#print 'get_location()'
buf = ioctl(self._fd, EVIOCGPHYS(1024), ' ' * 1024)
null = buf.find('\0')
return buf[:null]
def get_events(self):
"""
"""
#print 'get_events()'
keys = _types.keys()
keys.sort()
# We need one bit per type, rounded up to even 4 bytes
l = ((keys[-1] + 7) / 8 + 3) & ~0x3
buf = ioctl(self._fd, EVIOCGBIT(0, l), ' ' * l)
array = struct.unpack('I' * (l/4), buf)
self._events = {}
for i in xrange(l * 8):
if not array[i / 32] & (1 << i % 32):
continue
self._events[i] = []
subkeys = _events[i].keys()
subkeys.sort()
# We need one bit per type, rounded up to even 4 bytes
sl = ((subkeys[-1] + 7) / 8 + 3) & ~0x3
try:
buf = ioctl(self._fd, EVIOCGBIT(i, sl), ' ' * sl)
except IOError:
# No events for a type results in Errno 22 (EINVAL)
break
subarray = struct.unpack('I' * (sl/4), buf)
for j in xrange(sl * 8):
if not subarray[j / 32] & (1 << j % 32):
continue
self._events[i].append(j)
def has_event(self, test_event):
"""
"""
#print 'has_event(test_event)'
for type in self._events.keys():
for event in self._events[type]:
if _events[type][event] == test_event:
return True
return False
def read(self):
"""
"""
#print 'read()'
try:
buf = os.read(self._fd, struct.calcsize('LLHHi'))
except OSError, (errno, str):
if errno in (11, 19):
return None
raise
sec, usec, type, code, value = struct.unpack('LLHHi', buf)
return (float(sec) + float(usec)/1000000.0, _types[type], _events[type][code], value)
if __name__ == '__main__':
def _convert_value(s):
#print '_convert_value(s)'
if s.startswith('0x'):
return int(s, 16)
return int(s, 10)
def parse_input_h(path):
#print 'parse_input_h(path)'
global _types, _events, _ids, _buses
f = file(path)
types = {}
events = {}
ids = {}
buses = {}
for line in f.readlines():
m = re.search('#define (?P<name>EV_[A-Za-z0-9_]+)\s+(?P<value>(0x)?[0-9A-Fa-f]+)', line)
if m:
if m.group('name') != 'EV_VERSION':
types[_convert_value(m.group('value'))] = m.group('name')
continue
m = re.search('#define (?P<name>ID_[A-Za-z0-9_]+)\s+(?P<value>(0x)?[0-9A-Fa-f]+)', line)
if m:
ids[_convert_value(m.group('value'))] = m.group('name')
continue
m = re.search('#define (?P<name>BUS_[A-Za-z0-9_]+)\s+(?P<value>(0x)?[0-9A-Fa-f]+)', line)
if m:
buses[_convert_value(m.group('value'))] = m.group('name')
continue
m = re.search('#define (?P<name>(?P<type>[A-Za-z0-9]+)_[A-Za-z0-9_]+)\s+(?P<value>(0x)?[0-9A-Fa-f]+)', line)
if m:
t = m.group('type')
# The naming is a bit off in input.h
if t == 'BTN':
t = 'KEY'
for k in types.keys():
if types[k] == 'EV_' + t:
break
else:
raise Exception('Invalid type: %s' % m.group('type'))
if not events.has_key(k):
events[k] = {}
events[k][_convert_value(m.group('value'))] = m.group('name')
_types = types
_events = events
_ids = ids
_buses = buses
def _print_tables():
#print '_print_tables()'
print '_types = {'
keys = _types.keys()
keys.sort()
for key in keys:
print ' %2d:%s,' % (key, repr(_types[key]))
print ' }'
print ''
print '_events = {'
keys = _events.keys()
keys.sort()
for key in keys:
print ' %2d:{ # %s' % (key, _types[key])
subkeys = _events[key].keys()
for subkey in subkeys:
print ' %3d:%s,' % (subkey, repr(_events[key][subkey]))
print ' },'
print ' }'
print ''
print '_ids = {'
keys = _ids.keys()
keys.sort()
for key in keys:
print ' %2d:%s,' % (key, repr(_ids[key]))
print ' }'
print ''
print '_buses = {'
keys = _buses.keys()
keys.sort()
for key in keys:
print ' %2d:%s,' % (key, repr(_buses[key]))
print ' }'
# Main starts here
e = evdev(sys.argv[1], True)
e.print_info()
e.print_events()
try:
while True:
print e.read()
except KeyboardInterrupt:
pass
try:
from linuxevent import _types, _events, _ids, _buses
except ImportError:
from evfallback import _types, _events, _ids, _buses
except StandardError, e:
print 'evfallback:', e
|