Ticket #6839: mythremctl.py

File mythremctl.py, 2.5 KB (added by raymond@…, 15 years ago)
Line 
1#!/usr/local/bin/python
2#
3#
4import os
5import sys
6import curses
7from MythTV import Frontend, MythTV
8 
9from curses.wrapper import wrapper
10 
11PORT = 6546
12FE = None
13 
14try:
15        FE = Frontend(sys.argv[1],PORT)
16except:
17        print "polling frontends..."
18        mythtv = MythTV()
19        fes = mythtv.getFrontends()
20        if len(fes) == 0:
21                print "No accessible frontends found"
22                sys.exit(1)
23        print "Select a frontend:"
24        print "  0. quit"
25        for i in range(0,len(fes)):
26                print "  %d. %s:%d" % (i+1,fes[i].host,fes[i].port)
27        i = None
28        while not i:
29                try:
30                        i = int(raw_input("> "))
31                except KeyboardInterrupt:
32                        sys.exit(0)
33                except:
34                        print "Invalid input... Please choose between 0 and %d" %len(fes)
35                        continue
36                print i
37                if i == 0:
38                        sys.exit(0)
39                if (i < 1) | (i > len(fes)):
40                        print "Invalid input... Please choose between 0 and %d" %len(fes)
41                        i = None
42                        continue
43                FE = fes[i-1]
44
45keys = {
46        9: 'tab',
47        10: 'enter',
48        27: 'escape',
49        32: 'space',
50        127: 'backspace',
51        258: 'down',
52        259: 'up',
53        260: 'left',
54        261: 'right',
55        262: 'home',
56        263: 'backspace',
57        330: 'delete',
58        331: 'insert',
59        338: 'pagedown',
60        339: 'pageup',
61        353: 'backtab',
62        360: 'end',
63}
64 
65def main(screen):
66        try:
67                while True:
68                        try:
69                                screen.addstr(0, 1, '-= MythTV console remote  =-\n')
70                                c = screen.getch()
71                                if c == 274:
72                                        os.system('tvoff')
73                                if c == 275:
74                                        os.system('tvon')
75                                elif c in keys:
76                                        s = 'key %s\n' % keys[c]
77                                        screen.addstr(1, 3, s)
78                                        FE.sendKey(keys[c])
79                                else:
80                                        if c <= 256:
81                                                s = 'key %s\n' % chr(c)
82                                                FE.sendKey(chr(c))
83                                        else:
84                                                s = 'key #%s\n' % c
85                                                FE.sendKey('#%s' %c)
86                                        screen.addstr(1, 3, s)
87                        except EOFError:
88                                FE.disconnect()
89                                FE.connect()
90        except KeyboardInterrupt:
91                screen.addstr("Exiting...")
92        curses.endwin()
93 
94wrapper(main)