Revert "Import translations from Launchpad.", imported into the wrong branch
[laditools.git] / g15ladi
blob5ef0de3d5a81d93cc0b8c30348baa1636d1fa0ae
1 #!/usr/bin/python
3 # LADITools - Linux Audio Desktop Integration Tools
4 # g15ladi - A jack monitor for the g15 keyboard that uses the dbus interface
5 # Copyright (C) 2012 Alessio Treglia <quadrispro@ubuntu.com>
6 # Copyright (C) 2007-2010, Marc-Olivier Barre <marco@marcochapeau.org>
7 # Copyright (C) 2007-2009, Nedko Arnaudov <nedko@arnaudov.name>
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 import sys
23 import os
24 import time
25 import signal
26 import argparse
28 import laditools
30 pipe_filename = "/dev/shm/g15log.pipe"
32 class g15composer (object):
33 text_justify_left = 0
34 text_justify_center = 1
35 text_justify_right = 2
37 text_size_small = 0
38 #text_size_medium = 1
39 text_size_large = 2
41 width = 160
42 height = 43
44 def __init__(self, pipe_filename, large):
45 if large:
46 self.size = self.text_size_large
47 self.max_lines = 5 # 7 for small, 5 for large
48 self.max_chars = 20
49 else:
50 self.size = self.text_size_small
51 self.max_lines = 7
52 self.max_chars = 40
54 self.pipe_filename = pipe_filename
55 self.pid = os.spawnlp(os.P_NOWAIT, 'g15composer', 'g15composer', self.pipe_filename)
56 #print "g15composer started"
57 while not os.access(self.pipe_filename, os.F_OK):
58 #print "Waiting pipe to appear..."
59 time.sleep(1)
60 self.pipe = file(self.pipe_filename, "w")
61 os.remove(self.pipe_filename)
62 #print "pipe unlinked"
64 def __del__(self):
65 self.pipe.close()
66 #print "stopping g15composer..."
67 os.kill(self.pid, signal.SIGTERM)
68 os.waitpid(self.pid, 0)
69 #print "g15composer stopped"
71 def send_message(self, message):
72 #print message,
73 self.pipe.write(message)
74 self.pipe.flush()
76 def draw_lines(self, lines, x, y, justify):
77 message = 'TO %u %u %u %u' % (x, y, self.size, justify)
78 for line in lines:
79 message += ' "%s "' % line.replace('"', "'").strip("\n")
80 message += "\n"
81 self.send_message(message)
83 def draw_lines(self, lines, x, y, justify):
84 message = 'TO %u %u %u %u' % (x, y, self.size, justify)
85 for line in lines:
86 message += ' "%s "' % line.replace('"', "'").strip("\n")
87 message += "\n"
88 self.send_message(message)
90 def clear(self, ink):
91 if ink:
92 message = 'PC 1\n'
93 else:
94 message = 'PC 0\n'
95 self.send_message(message)
97 def draw_bar(self, x, y, width, height, ink, fill):
98 if width < 0:
99 width = self.width - width - x - 1
100 else:
101 if width < 5:
102 width = 5
103 if height < 3:
104 height = 3
105 width -= 1
106 height -= 3
107 y += 1
108 if ink:
109 ink = 1
110 else:
111 ink = 0
112 self.send_message("DB %u %u %u %u %u %u %u 1\n" % (x, y, x + width, y + height, ink, fill * 1000, 1000))
114 class jackctl_g15(object):
115 def __init__(self):
116 self.jack = None
117 self.g15 = g15composer(pipe_filename, False)
118 self.started = False
119 self.ladish = None
121 def get_jack_info(self):
122 if not self.jack:
123 self.jack = laditools.JackController()
125 if not self.jack.is_started():
126 return False, None, None, None, None, None
128 realtime = self.jack.is_realtime()
129 load = self.jack.get_load()
131 xruns = self.jack.get_xruns()
132 if xruns == 0:
133 xruns_line = "no xruns"
134 elif xruns == 1:
135 xruns_line = "1 xrun"
136 else:
137 xruns_line = "%u xruns" % xruns
139 rate = self.jack.get_sample_rate()
140 if rate % 1000.0 == 0:
141 rate_line = "Sample rate: %.0f Khz" % (rate / 1000.0)
142 else:
143 rate_line = "Sample rate: %.1f Khz" % (rate / 1000.0)
144 latency_line = "Latency: %.1f ms" % self.jack.get_latency()
146 return True, realtime, load, xruns_line, rate_line, latency_line
148 def update(self):
149 try:
150 studio_name = None
151 if not self.ladish:
152 try:
153 self.ladish = laditools.LadishProxy()
154 except:
155 pass
156 try:
157 studio_name = self.ladish.studio_name()
158 except:
159 pass
161 started, realtime, load, xruns_line, rate_line, latency_line = self.get_jack_info()
163 if started:
164 self.started = True
165 elif self.started:
166 self.g15.clear(False)
167 self.started = False
169 if self.ladish:
170 if studio_name:
171 status_line = '"' + studio_name + '"'
172 if started:
173 status_line += " started"
174 if realtime:
175 status_line += " (RT)"
176 else:
177 status_line += " stopped"
178 else:
179 if started:
180 status_line = "ladish is sick, jack started w/o studio"
181 else:
182 status_line = "No studio loaded"
183 else:
184 status_line = "JACK"
185 if started:
186 if realtime:
187 status_line += " started in realtime mode"
188 else:
189 status_line += " started in non-realtime mode"
190 else:
191 status_line += " stopped"
192 except Exception, e:
193 print repr(e)
194 status_line = "JACK cannot be connected"
195 if self.jack:
196 self.g15.clear(False)
197 self.jack = None
198 self.started = False
200 self.g15.draw_lines([status_line], 0, 0, self.g15.text_justify_left)
201 if self.started:
202 self.g15.draw_lines(["DSP load"], 0, 12, self.g15.text_justify_left)
203 self.g15.draw_bar(35, 12, 95, 5, True, load / 100.0)
204 self.g15.draw_lines(["%.3f%%" % load], 135, 12, self.g15.text_justify_left)
206 self.g15.draw_lines([xruns_line], 0, 22, self.g15.text_justify_left)
207 self.g15.draw_lines([rate_line, latency_line], 0, 31, self.g15.text_justify_left)
209 def run(self):
210 while True:
211 self.update()
212 time.sleep(1)
214 del self.g15
216 if __name__ == '__main__':
217 parser = argparse.ArgumentParser(description='JACK monitor for g15 keyboards',
218 epilog='This program is part of the LADITools suite.')
219 parser.add_argument('--version', action='version', version="%(prog)s " + laditools.get_version_string())
221 parser.parse_args()
222 jackctl_g15().run()
223 sys.exit(0)