2 # -*- coding: utf-8 -*-
4 """vompilirc.py: Routes remote commands received through LIRC to vompclient via UDP."""
6 # Copyright (C) 2013 Alexander Koenig <alex@lisas.de>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 __author__ = "Alexander König"
23 __email__ = "alex@lisas.de"
26 import pylirc, time, socket, os, signal, subprocess
28 vomp_host = '127.0.0.1'
32 # VOMP remote commands as defined in remote.h
96 'KEY_POWER': VOMP_POWER,'KEY_HOME': 0, 'KEY_TV': VOMP_TV, 'KEY_VIDEO': VOMP_VIDEOS, 'KEY_AUDIO': VOMP_MUSIC, 'KEY_CAMERA': VOMP_PICTURES
100 'KEY_EPG': VOMP_GUIDE,'KEY_RADIO': VOMP_RADIO,'KEY_UP': VOMP_UP,'KEY_DOWN': VOMP_DOWN,
101 'KEY_LEFT': VOMP_LEFT,'KEY_RIGHT': VOMP_RIGHT,'KEY_OK': VOMP_OK,'KEY_EXIT': VOMP_BACK,
102 'KEY_MENU': VOMP_MENU,'KEY_PREVIOUS': VOMP_PREVCHANNEL,'KEY_MUTE': VOMP_MUTE,
103 'KEY_VOLUMEUP': VOMP_VOLUMEUP,'KEY_VOLUMEDOWN': VOMP_VOLUMEDOWN,'KEY_CHANNELUP': VOMP_CHANNELUP,'KEY_CHANNELDOWN': VOMP_CHANNELDOWN,
104 'KEY_RECORD': VOMP_RECORD,'KEY_STOP': VOMP_STOP,'KEY_PLAY': VOMP_PLAY,'KEY_REWIND': VOMP_REVERSE,'KEY_FORWARD': VOMP_FORWARD,
105 'KEY_FRAMEBACK': VOMP_SKIPBACK,'KEY_PAUSE': VOMP_PAUSE,'KEY_FRAMEFORWARD': VOMP_SKIPFORWARD,
106 'KEY_1': VOMP_ONE,'KEY_2': VOMP_TWO,'KEY_3': VOMP_THREE,'KEY_4': VOMP_FOUR,'KEY_5': VOMP_FIVE,
107 'KEY_6': VOMP_SIX,'KEY_7': VOMP_SEVEN,'KEY_8': VOMP_EIGHT,'KEY_9': VOMP_NINE,
108 'KEY_KPASTERISK': VOMP_STAR,'KEY_0': VOMP_ZERO,'KEY_KPPLUS': VOMP_HASH,
109 'KEY_RED': VOMP_RED,'KEY_GREEN': VOMP_GREEN,'KEY_YELLOW': VOMP_YELLOW,'KEY_BLUE': VOMP_BLUE
112 (VOMPILIRC_MODE_OFF, VOMPILIRC_MODE_VOMP, VOMPILIRC_MODE_XBMC) = (0, 1, 2)
114 mode = VOMPILIRC_MODE_OFF
117 repeaters = [ VOMP_CHANNELUP, VOMP_CHANNELDOWN ]
125 print 'Starting vompclient'
127 mode = VOMPILIRC_MODE_VOMP
128 child_pipe = subprocess.Popen(['vompclient','-n'])
135 print 'Starting XBMC'
137 mode = VOMPILIRC_MODE_XBMC
138 child_pipe = subprocess.Popen(['/usr/lib/xbmc/xbmc.bin', '--standalone'])
141 def tv_power(off=False):
142 pipe = subprocess.Popen(['cec-client', '-s'], stdin=subprocess.PIPE)
145 pipe.communicate('standby 0')
147 pipe.communicate('on 0')
149 def stop_child(force_off=False):
154 print 'Terminating child process'
160 print 'Did not terminate within timeout, sending kill signal.'
169 mode = VOMPILIRC_MODE_OFF
171 def signal_handler(signum, frame):
172 if debug: print 'Exiting...'
174 if child_pipe != None:
180 def process_lirc_event():
184 s = pylirc.nextcode(1)
188 if debug: print 'Command: %s, Repeat: %d' % (code['config'], code['repeat'])
191 mapped_code = '%i' % remote_map[code['config']]
195 if mapped_code != None:
196 vompmessage = mapped_code
197 elif code['config'] == 'KEY_POWER':
198 if code['repeat'] == 0:
199 if mode == VOMPILIRC_MODE_OFF:
202 stop_child(force_off=True)
203 elif code['config'] == 'KEY_TV':
204 if code['repeat'] == 0:
205 if mode == VOMPILIRC_MODE_OFF:
207 elif mode == VOMPILIRC_MODE_XBMC:
208 stop_child(force_off=False)
210 elif code['config'] == 'KEY_VIDEO':
211 if code['repeat'] == 0:
212 if mode == VOMPILIRC_MODE_OFF:
214 elif mode == VOMPILIRC_MODE_VOMP:
215 stop_child(force_off=False)
218 sys.stderr.write('Unknown key: %s\n' % code['config'])
220 if mode == VOMPILIRC_MODE_VOMP:
221 if (len(vompmessage) > 0) and ((code['repeat'] == 0) or (vompmessage in repeaters)):
222 vomp_client.sendto(vompmessage, (vomp_host, vomp_port))
224 if __name__ == '__main__':
226 vomp_client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
227 except socket.error, (value,message):
228 sys.stderr.write('Failed to open UDP socket: "%s"\n' % message)
231 if pylirc.init('vompilirc', os.path.join('/etc/lirc/lircrc')):
234 signal.signal(signal.SIGINT, signal_handler)
235 signal.signal(signal.SIGTERM, signal_handler)
240 sys.stderr.write('Failed to init pylirc.\n')