/usr/share/pyshared/freevo/helpers/inputhelper.py is in python-freevo 1.9.2b2-4.2.
This file is owned by root:root, with mode 0o755.
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 | #! /usr/bin/python
# -*- coding: iso-8859-1 -*-
# -----------------------------------------------------------------------
# Input helper to process LIRC and other input devices.
# -----------------------------------------------------------------------
# $Id: imdb.py 11565 2009-05-25 18:36:59Z 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
#
# -----------------------------------------------------------------------
import logging
logger = logging.getLogger("freevo.helpers.inputhelper")
import fcntl
import os
import sys
import struct
import time
import kaa
import config
wire_format = struct.Struct('d30p')
def post_key(key):
"""
Send key to main process.
"""
os.write(fd, wire_format.pack(time.time(), key))
class Lirc:
"""
Class to handle lirc events
"""
def __init__(self):
_debug_('Lirc.__init__()', 2)
try:
global pylirc
import pylirc
except ImportError:
_debug_('PyLirc not found, lirc remote control disabled!', DWARNING)
raise
try:
if os.path.isfile(config.LIRCRC):
self.resume()
else:
raise IOError
except RuntimeError:
_debug_('Could not initialize PyLirc!', DWARNING)
raise
except IOError:
_debug_('%r not found!' % (config.LIRCRC), DWARNING)
raise
global PYLIRC
PYLIRC = True
self.last_code = [None, 0, False]
def __process_key(self, code):
now = time.time()
if self.last_code[0] == code:
diff = now - self.last_code[1]
if diff > config.LIRC_KEY_REPEAT[0] and self.last_code[2]:
post_key(code)
self.last_code = [code, now, False]
return
if diff > config.LIRC_KEY_REPEAT[1] and not self.last_code[2]:
post_key(code)
self.last_code = [code, now, False]
return
else:
post_key(code)
self.last_code = [code, now, True]
def resume(self):
"""
(re-)initialize pylirc, e.g. after calling close()
"""
_debug_('Lirc.resume()', 2)
fd = pylirc.init('freevo', config.LIRCRC)
self.dispatcher = kaa.IOMonitor(self._handle_lirc_input)
self.dispatcher.register(fd)
def _handle_lirc_input(self):
codes = pylirc.nextcode()
if codes:
for code in codes:
self.__process_key(code)
def suspend(self):
"""
cleanup pylirc, close devices
"""
_debug_('Lirc.suspend()', 2)
self.dispatcher.unregister()
pylirc.exit()
class Evdev:
"""
Class to handle evdev events
"""
def __init__(self):
"""
init all specified devices
"""
_debug_('Evdev.__init__()', 2)
import evdev
self._devs = []
for dev in config.EVENT_DEVS:
e = None
if os.path.exists(dev):
try:
e = evdev.evdev(dev)
except:
print "Problem opening event device '%s'" % dev
else:
names = []
name = dev
for dev in os.listdir('/dev/input'):
if not dev.startswith('event'):
continue
try:
dev = '/dev/input/' + dev
e = evdev.evdev(dev)
except:
continue
names.append(e.get_name())
if e.get_name() == name:
break
else:
e = None
_debug_("Could not find device named '%s', possible are:\n - %s" % \
(name, '\n - '.join(names)), DWARNING)
if e is not None:
_debug_("Added input device '%s': %s" % (dev, e.get_name()), DINFO)
m = kaa.IOMonitor(self.__handle_event, e)
m.register(e._fd)
self._devs.append(m)
self._movements = {}
def __handle_event(self, dev):
"""
Handle evdev events
"""
event = dev.read()
if event is None:
return
time, type, code, value = event
if type == 'EV_KEY':
self._movements = {}
if config.EVENTMAP.has_key(code):
# 0 = release, 1 = press, 2 = repeat
if value > 0:
post_key(config.EVENTMAP[code])
elif type == 'EV_REL':
if config.EVENTMAP.has_key(code):
if self._movements.has_key(code):
self._movements[code] += value
else:
self._movements[code] = value
if self._movements[code] < -10:
self._movements = {}
post_key(config.EVENTMAP[code][0])
elif self._movements[code] > 10:
self._movements = {}
post_key(config.EVENTMAP[code][1])
def handle_stdin():
"""
Handle commands sent via stdin
"""
cmd = sys.stdin.readline().strip()
if cmd == 'suspend':
for i in inputs:
if hasattr(i, 'suspend'):
i.suspend()
elif cmd == 'resume':
for i in inputs:
if hasattr(i, 'resume'):
i.resume()
elif cmd == 'quit' or cmd == '':
sys.exit(0)
if len(sys.argv) < 2:
sys.stderr.write('No fd specified!')
sys.exit(1)
fd = int(sys.argv[1])
_debug_('Using pipe fd %d' % fd)
# Put fd in non-blocking mode
flag = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, flag | os.O_NDELAY)
inputs = [Lirc()]
if config.EVENT_DEVS:
try:
inputs.append(Evdev())
except:
pass
stdin_dispatcher = kaa.IOMonitor(handle_stdin)
stdin_dispatcher.register(sys.stdin)
kaa.main.run()
|