Package elisa :: Package plugins :: Package good :: Package lirc_plugin :: Module lirc_input
[hide private]
[frames] | no frames]

Source Code for Module elisa.plugins.good.lirc_plugin.lirc_input

  1  # -*- coding: utf-8 -*- 
  2  # Elisa - Home multimedia server 
  3  # Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com). 
  4  # All rights reserved. 
  5  # 
  6  # This file is available under one of two license agreements. 
  7  # 
  8  # This file is licensed under the GPL version 3. 
  9  # See "LICENSE.GPL" in the root of this distribution including a special 
 10  # exception to use Elisa with Fluendo's plugins. 
 11  # 
 12  # The GPL part of Elisa is also available under a commercial licensing 
 13  # agreement from Fluendo. 
 14  # See "LICENSE.Elisa" in the root directory of this distribution package 
 15  # for details on that license. 
 16   
 17  """ 
 18  LircInputProvider component class 
 19  """ 
 20   
 21   
 22  __maintainer__ = 'Florian Boucault <florian@fluendo.com>' 
 23   
 24  from elisa.base_components.input_provider import PollInputProvider 
 25  from elisa.core.input_event import * 
 26  from elisa.core.component import InitializeFailure 
 27   
 28  import os 
 29  import pkg_resources 
 30  from twisted.internet import reactor 
 31  from tempfile import mkstemp 
 32  import pylirc 
 33   
34 -class LircInput(PollInputProvider):
35 """ 36 This class implements LIRC remote control support 37 """ 38 39 config_doc = {'lirc_rc': 'filename of the LIRC config map to use', 40 'delay' : 'how many keys to wait until triggering a repeat', 41 'repeat' : 'every which repeat should be used, zero means' 42 ' no repeating' 43 } 44 default_config = {'lirc_rc': 'streamzap.lirc', 45 'delay' : '4', 46 'repeat' : '1' 47 } 48 49 50 convert_table = { 51 'move_up_key' : EventAction.GO_UP, 52 'move_down_key' : EventAction.GO_DOWN, 53 'move_left_key' : EventAction.GO_LEFT, 54 'move_right_key': EventAction.GO_RIGHT, 55 'toggle_menu_key': EventAction.MENU, 56 'activate_key' : EventAction.OK, 57 'close_key' : EventAction.EXIT, 58 'toggle_play_pause_key': EventAction.PLAY, 59 'pause_key': EventAction.PAUSE, 60 'stop_key': EventAction.STOP, 61 'increment_playback_speed' : EventAction.INC_PLAYBACK_SPEED, 62 'decrement_playback_speed' : EventAction.DEC_PLAYBACK_SPEED, 63 'seek_forward_key' : EventAction.SEEK_FORWARD, 64 'seek_backward_key': EventAction.SEEK_BACKWARD, 65 'next_key': EventAction.NEXT, 66 'previous_key': EventAction.PREVIOUS, 67 'toggle_fullscreen_key' : EventAction.TOGGLE_FULLSCREEN, 68 'toggle_mute_key' : EventAction.MUTE, 69 'increment_volume_key' : EventAction.VOL_UP, 70 'decrement_volume_key' : EventAction.VOL_DOWN 71 } 72 73
74 - def __init__(self):
75 PollInputProvider.__init__(self) 76 self.sock = None 77 self._temp_path = None
78
79 - def initialize(self):
80 rc_file = self.get_lirc_config() 81 try: 82 self.sock = pylirc.init('elisa', rc_file) 83 except Exception, error: 84 self.clean() 85 raise InitializeFailure(self.name, str(error)) 86 else: 87 self.info("Using LIRC config file: %s" % rc_file) 88 pylirc.blocking(False)
89
90 - def clean(self):
91 if self._temp_path: 92 if os.path.exists(self._temp_path): 93 os.remove(self._temp_path) 94 95 PollInputProvider.clean(self)
96
97 - def get_lirc_config(self):
98 delay = int(self.config.get('delay', '0')) 99 repeat = int(self.config.get('repeat', '0')) 100 lirc_config = self.config.get('lirc_rc','') 101 102 if not os.path.exists(lirc_config): 103 path = 'data/lirc/%s' % lirc_config 104 lirc_config = pkg_resources.resource_filename(self.__module__, 105 path) 106 107 reader = open(lirc_config, 'r') 108 write_id, path = mkstemp('.lirc', 'elisa_tmp_lirc_') 109 writer = os.fdopen(write_id, 'w') 110 111 # replace all delays and repeats 112 rep_count = 0 113 del_count = 0 114 for line in reader: 115 if line.find('delay') != -1: 116 del_count += 1 117 writer.write("delay = %s\n" % delay) 118 elif line.find('repeat') != -1: 119 rep_count += 1 120 writer.write("repeat = %s\n" % repeat) 121 else: 122 writer.write("%s" % line) 123 124 self.debug("Written %s delays and %s repeats to %s" % (del_count, 125 rep_count, 126 path)) 127 reader.close() 128 writer.close() 129 self._temp_path = path 130 return path
131
132 - def get_input_events(self):
133 ret = [] 134 135 ev = pylirc.nextcode() 136 if not ev: 137 return [] 138 139 if len(ev): 140 self.debug("Received: %r", ev) 141 142 for i in ev: 143 e = self.create_input_event(i) 144 if e: 145 ret.append(e) 146 return ret
147
148 - def create_input_event(self, data):
149 evt = None 150 if data and data in self.convert_table: 151 evt = InputEvent(EventSource.REMOTE, EventType.OTHER, 152 self.convert_table[data]) 153 evt.origin = self.origin 154 else: 155 self.info('Unrecognized key received: %s', data) 156 return evt
157 158 159 if __name__ == "__main__": 160 161 import time 162 from elisa.core import config 163 164 _config = config.Config('elisa.conf') 165 166 _section = {'lirc_rc': 'streamzap.lirc'} 167 _config.set_section('lirc_input', _section) 168 169 l = LircInput() 170 l.load_config(_config) 171 l.initialize() 172 173 while True: 174 events = l.get_input_events() 175 for event in events: 176 print event 177 time.sleep(0.01) 178