Allow switching to XBMC for media playback.
authorAlexander Koenig <alex@lisas.de>
Sun, 15 Dec 2013 22:02:52 +0000 (22:02 +0000)
committerAlexander Koenig <alex@lisas.de>
Sun, 15 Dec 2013 22:02:52 +0000 (22:02 +0000)
Lircmap.xml
vompilirc.py

index fd56d485e53dd3960e1649a5fc4cdce0cbfdec8a..a71b01eee0506aeb74c735ddec078d542e3f076c 100644 (file)
                <skipplus>KEY_FRAMEFORWARD</skipplus>
                <skipminus>KEY_FRAMEBACK</skipminus>
                <mute>KEY_MUTE</mute>
-               <power>KEY_POWER</power>
-               <myvideo>KEY_VIDEO</myvideo>
+               <!-- <power>KEY_POWER</power> -->
+               <!-- <myvideo>KEY_VIDEO</myvideo>  -->
                <mymusic>KEY_AUDIO</mymusic>
-               <mytv>LiveTV</mytv>
+               <!-- <mytv>LiveTV</mytv> -->
                <one>KEY_1</one>
                <two>KEY_2</two>
                <three>KEY_3</three>
index 1f6fea19c63c1045d31ceccecc047790546662ac..9e1a48d20ecf24071c2f3dc928a5aeb3ccf5c4c5 100755 (executable)
@@ -23,7 +23,7 @@ __author__ = "Alexander K├Ânig"
 __email__ = "alex@lisas.de"
 __license__ = "GPLv2"
 
-import pylirc, time, socket, os, signal
+import pylirc, time, socket, os, signal, subprocess
 
 vomp_host = '127.0.0.1'
 vomp_port = 2000
@@ -109,50 +109,119 @@ remote_map = {
        'KEY_RED': VOMP_RED,'KEY_GREEN': VOMP_GREEN,'KEY_YELLOW': VOMP_YELLOW,'KEY_BLUE': VOMP_BLUE
 }
 
+(VOMPILIRC_MODE_OFF, VOMPILIRC_MODE_VOMP, VOMPILIRC_MODE_XBMC) = (0, 1, 2)
+
+mode = VOMPILIRC_MODE_OFF
+child_pipe = None
+
 repeaters = [ VOMP_CHANNELUP, VOMP_CHANNELDOWN ]
-vomp_started = False
 debug = False
 
+def start_vomp():
+       global mode
+       global child_pipe
+       
+       if debug:
+               print 'Starting vompclient'
+       
+       mode = VOMPILIRC_MODE_VOMP
+       child_pipe = subprocess.Popen(['vompclient','-n'])
+
+def start_xbmc():
+       global mode
+       global child_pipe
+       
+       if debug:
+               print 'Starting XBMC'
+       
+       mode = VOMPILIRC_MODE_XBMC
+       child_pipe = subprocess.Popen(['/usr/lib/xbmc/xbmc.bin', '--standalone'])
+
+
+def tv_power(off=False):
+       pipe = subprocess.Popen(['cec-client', '-s'], stdin=subprocess.PIPE)
+       
+       if off:
+               pipe.communicate('standby 0')
+       else:
+               pipe.communicate('on 0')
+
+def stop_child(force_off=False):
+       global mode
+       global child_pipe
+       
+       if debug:
+               print 'Terminating child process'
+       
+       try:
+               child_pipe.wait(5)
+       except:
+               if debug:
+                       print 'Did not terminate within timeout, sending kill signal.'
+                       
+               child_pipe.kill()
+               child_pipe.wait()       
+       
+       if force_off:
+               tv_power(off=True)
+               
+       child_pipe = None
+       mode = VOMPILIRC_MODE_OFF
+
 def signal_handler(signum, frame):
        if debug: print 'Exiting...'
+       
+       if child_pipe != None:
+               stop_child()            
+               
        pylirc.exit()
        os._exit(0)
 
 def process_lirc_event():
-       global vomp_started
+       global mode
+       global child_pipe
        
        s = pylirc.nextcode(1)
                        
        for code in s:
                vompmessage = ''
-               if debug: print "Command: %s, Repeat: %d" % (code["config"], code["repeat"])
+               if debug: print 'Command: %s, Repeat: %d' % (code['config'], code['repeat'])
                mapped_code = None
                try:
-                       mapped_code = '%i' % remote_map[code["config"]]
+                       mapped_code = '%i' % remote_map[code['config']]
                except:
                        pass
 
                if mapped_code != None:
                        vompmessage = mapped_code
-               elif code["config"] == 'KEY_POWER':
-                       if code["repeat"] == 0:
-                               if vomp_started:
-                                       os.system('killall vompclient')
-                                       time.sleep(1)
-                                       os.system('killall -9 vompclient')
-                                       #os.system('/opt/vc/bin/tvservice -o')
-                                       vomp_started = False
+               elif code['config'] == 'KEY_POWER':
+                       if code['repeat'] == 0:
+                               if mode == VOMPILIRC_MODE_OFF:
+                                       start_vomp()
                                else:
-                                       #os.system('/opt/vc/bin/tvservice -p')
-                                       os.system('vompclient')
-                                       vomp_started = True
+                                       stop_child(force_off=True)
+               elif code['config'] == 'KEY_TV':
+                       if code['repeat'] == 0:
+                               if mode == VOMPILIRC_MODE_OFF:
+                                       start_vomp()
+                               elif mode == VOMPILIRC_MODE_XBMC:
+                                       stop_child(force_off=False)
+                                       start_vomp()
+               elif code['config'] == 'KEY_VIDEO':
+                       if code['repeat'] == 0:
+                               if mode == VOMPILIRC_MODE_OFF:
+                                       start_xbmc()
+                               elif mode == VOMPILIRC_MODE_VOMP:
+                                       stop_child(force_off=False)
+                                       start_xbmc()
                else:
-                       sys.stderr.write('Unknown key: %s\n' % code["config"])
+                       sys.stderr.write('Unknown key: %s\n' % code['config'])
 
-               if (len(vompmessage) > 0) and ((code["repeat"] == 0) or (vompmessage in repeaters)):
-                       vomp_client.sendto(vompmessage, (vomp_host, vomp_port))
+               if mode == VOMPILIRC_MODE_VOMP:
+                       if (len(vompmessage) > 0) and ((code['repeat'] == 0) or (vompmessage in repeaters)):
+                               vomp_client.sendto(vompmessage, (vomp_host, vomp_port))
                                        
-if __name__ == "__main__":
+if __name__ == '__main__':
        try:
                vomp_client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        except socket.error, (value,message):