Update soundtest
[grml-scripts.git] / usr_bin / osd_server.py
1 #!/usr/bin/python
2 # -*- coding: iso-8859-15 -*-
3 # Copyright (C) 2005 2006 Alexander Bernauer <alex@copton.net>
4 # Copyright (C) 2005 2006 Rico Schiekel <fire@donwgra.de>
5 # Copyright (C) 2005 2006 Ulrich Dangel <uli@spamt.net>
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation version 2
10 # of the License.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA # 02111-1307, USA.
20 #
21
22 import sys, getopt, logging
23 from socket import *
24 from os import popen
25 from select import select
26
27 host='localhost'
28 port=1234
29 osdcmd = "/usr/bin/osd_cat"
30
31 def syntax():
32     print "osd_server.py [options]"
33     print "   options:"
34     print "     -h --help       print this message"
35     print "     -H --host       host of the osd server (def: " + host + ")"
36     print "     -P --port       port of the osd server (def: " + str(port) + ")"
37     print "     -o --osd        set new osd parameter string"
38     print "     -l --log        log file ('-' logs to stdout)"
39
40 def get_osd_paramstr(def_params, user_params):
41     ret = user_params
42     if user_params == '':
43         for n, v in def_params.iteritems():
44             if v != '':
45                 ret += n + "=" + v + " "
46     return ret
47
48
49
50 osd_params={ '--pos': 'middle',
51              '--offset': '100',
52              '--align': 'center',
53              '--indent': '100',
54              '--font':
55              '\-\*\-helvetica\-\*\-r\-\*\-\*\-34\-\*\-\*\-\*\-\*\-\*\-iso8859\-15',
56              '--colour': 'green',
57              '--shadow': '0',
58              '--shadowcolour': '',
59              '--outline': '1',
60              '--outlinecolour': 'black',
61              '--age': '4',
62              '--lines': '5',
63              '--delay': '4' }
64
65 logfile_name = ''
66 user_osd_params = ''
67
68 try:
69     opts, args = getopt.getopt(sys.argv[1:], "hH:P:o:l:", ["help", "host=", "port=", "osd=", "log="])
70 except getopt.GetoptError:
71     syntax()
72     sys.exit(2)
73
74 for opt, arg in opts:
75     if opt in ("-h", "--help"):
76         syntax()
77         sys.exit(3)
78     elif opt in ("-H", "--host"):
79         host = arg
80     elif opt in ("-P", "--port"):
81         port = int(arg)
82     elif opt in ("-o", "--osd"):
83         user_osd_params = arg
84     elif opt in ("-l", "--log"):
85         logfile_name = arg
86
87 l = socket(AF_INET, SOCK_STREAM)
88 l.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
89 l.bind((host, port))
90 l.listen(5)
91
92 logger = logging.getLogger('osd_server')
93 lformatter = logging.Formatter('%(asctime)s %(message)s')
94 if logfile_name not in ('', '-'):
95     lfh = logging.FileHandler(logfile_name)
96     lfh.setFormatter(lformatter)
97     logger.addHandler(lfh)
98 else:
99     lout = logging.StreamHandler(sys.stdout)
100     lout.setFormatter(lformatter)
101     logger.addHandler(lout)
102 logger.setLevel(logging.INFO)
103
104 logger.info("osd_server running on [%s] port [%d]" % (host, port))
105 osdpipe = popen("%s %s" % (osdcmd, get_osd_paramstr(osd_params, user_osd_params)), 'w')
106
107 r = range(32,127)
108 r.extend([ord(umlaut) for umlaut in "äöüßÄÖܤ" ])
109
110 while 1:
111     (con, addr) = l.accept()
112     message = con.recv(60).strip()
113     con.close()
114
115     message = message.replace("\n", "")
116     message = ''.join(map(lambda a: not a in r \
117                         and '[%.2d]' % a \
118                         or chr(a), \
119                         [ord(c) for c in message]))[:60]
120
121     logger.info(message)
122     osdpipe.write(message)
123     osdpipe.write("\n")
124     osdpipe.flush()
125
126 osdpipe.close()