#!/usr/bin/env python # -*- coding: utf-8 -*- """vompilirc.py: Routes remote commands received through LIRC to vompclient via UDP.""" # Copyright (C) 2013 Alexander Koenig # # 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 # MERCHANTABILITY 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 __author__ = "Alexander König" __email__ = "alex@lisas.de" __license__ = "GPLv2" import pylirc, time, socket, os, signal vomp_host = '127.0.0.1' vomp_port = 2000 vomp_client = None # VOMP remote commands as defined in remote.h VOMP_NA_LEARN = 101 VOMP_NA_NONE = 98 VOMP_NA_UNKNOWN = 99 VOMP_NA_SIGNAL = 100 VOMP_DF_UP = 94 VOMP_DF_DOWN = 95 VOMP_DF_LEFT = 96 VOMP_DF_RIGHT = 97 VOMP_VOLUMEUP = 16 VOMP_VOLUMEDOWN = 17 VOMP_CHANNELUP = 32 VOMP_CHANNELDOWN = 33 VOMP_ZERO = 0 VOMP_ONE = 1 VOMP_TWO = 2 VOMP_THREE = 3 VOMP_FOUR = 4 VOMP_FIVE = 5 VOMP_SIX = 6 VOMP_SEVEN = 7 VOMP_EIGHT = 8 VOMP_NINE = 9 VOMP_POWER = 61 VOMP_GO = 59 VOMP_BACK = 31 VOMP_MENU = 13 VOMP_RED = 11 VOMP_GREEN = 46 VOMP_YELLOW = 56 VOMP_BLUE = 41 VOMP_MUTE = 15 VOMP_RADIO = 12 VOMP_REVERSE = 50 VOMP_PLAY = 53 VOMP_FORWARD = 52 VOMP_RECORD = 55 VOMP_STOP = 54 VOMP_PAUSE = 48 VOMP_SKIPBACK = 36 VOMP_SKIPFORWARD = 30 VOMP_OK = 37 VOMP_FULL = 60 VOMP_TV = 28 VOMP_VIDEOS = 24 VOMP_MUSIC = 25 VOMP_PICTURES = 26 VOMP_GUIDE = 27 VOMP_UP = 20 VOMP_DOWN = 21 VOMP_LEFT = 22 VOMP_RIGHT = 23 VOMP_PREVCHANNEL = 18 VOMP_STAR = 10 VOMP_HASH = 14 VOMP_PLAYPAUSE = 201 unhandled_map = { 'KEY_POWER': VOMP_POWER,'KEY_HOME': 0, 'KEY_TV': VOMP_TV, 'KEY_VIDEO': VOMP_VIDEOS, 'KEY_AUDIO': VOMP_MUSIC, 'KEY_CAMERA': VOMP_PICTURES } remote_map = { 'KEY_EPG': VOMP_GUIDE,'KEY_RADIO': VOMP_RADIO,'KEY_UP': VOMP_UP,'KEY_DOWN': VOMP_DOWN, 'KEY_LEFT': VOMP_LEFT,'KEY_RIGHT': VOMP_RIGHT,'KEY_OK': VOMP_OK,'KEY_EXIT': VOMP_BACK, 'KEY_MENU': VOMP_MENU,'KEY_PREVIOUS': VOMP_PREVCHANNEL,'KEY_MUTE': VOMP_MUTE, 'KEY_VOLUMEUP': VOMP_VOLUMEUP,'KEY_VOLUMEDOWN': VOMP_VOLUMEDOWN,'KEY_CHANNELUP': VOMP_CHANNELUP,'KEY_CHANNELDOWN': VOMP_CHANNELDOWN, 'KEY_RECORD': VOMP_RECORD,'KEY_STOP': VOMP_STOP,'KEY_PLAY': VOMP_PLAY,'KEY_REWIND': VOMP_REVERSE,'KEY_FORWARD': VOMP_FORWARD, 'KEY_FRAMEBACK': VOMP_SKIPBACK,'KEY_PAUSE': VOMP_PAUSE,'KEY_FRAMEFORWARD': VOMP_SKIPFORWARD, 'KEY_1': VOMP_ONE,'KEY_2': VOMP_TWO,'KEY_3': VOMP_THREE,'KEY_4': VOMP_FOUR,'KEY_5': VOMP_FIVE, 'KEY_6': VOMP_SIX,'KEY_7': VOMP_SEVEN,'KEY_8': VOMP_EIGHT,'KEY_9': VOMP_NINE, 'KEY_KPASTERISK': VOMP_STAR,'KEY_0': VOMP_ZERO,'KEY_KPPLUS': VOMP_HASH, 'KEY_RED': VOMP_RED,'KEY_GREEN': VOMP_GREEN,'KEY_YELLOW': VOMP_YELLOW,'KEY_BLUE': VOMP_BLUE } repeaters = [ VOMP_CHANNELUP, VOMP_CHANNELDOWN ] vomp_started = False debug = False def signal_handler(signum, frame): if debug: print 'Exiting...' pylirc.exit() os._exit(0) def process_lirc_event(): global vomp_started s = pylirc.nextcode(1) for code in s: vompmessage = '' if debug: print "Command: %s, Repeat: %d" % (code["config"], code["repeat"]) mapped_code = None try: 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 else: #os.system('/opt/vc/bin/tvservice -p') os.system('vompclient') vomp_started = True else: 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 __name__ == "__main__": try: vomp_client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) except socket.error, (value,message): sys.stderr.write('Failed to open UDP socket: "%s"\n' % message) sys.exit(1) if pylirc.init('vompilirc', os.path.join('/etc/lirc/lircrc')): pylirc.blocking(1) signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) while True: process_lirc_event() else: sys.stderr.write('Failed to init pylirc.\n') sys.exit(2)