Refactoring.
[laditools.git] / wmladi
blob9b27cb75fe81174b87965809724ee54fdf86c8b5
1 #!/usr/bin/python
3 # LADITools - Linux Audio Desktop Integration Tools
4 # wmladi - Window maker dockapp for jackdbus
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 signal
25 from wmdocklib import wmoo, pywmhelpers
26 import time
27 import argparse
28 import gettext
29 from gi.repository import Gdk
31 sig_handler = signal.getsignal(signal.SIGTERM)
32 signal.signal(signal.SIGINT, sig_handler)
34 from laditools import _gettext_domain
35 gettext.install(_gettext_domain)
37 from laditools import get_version_string
38 from laditools import LadiConfiguration
39 from laditools import LadiApp
41 from gi.repository import Gtk
42 from gi.repository import GObject
44 from laditools.gtk import LadiMenu
46 # Default configuration
47 autostart_default = 0
48 debug = False
50 class wmladi (wmoo.Application, LadiMenu, LadiApp):
52 _appname = 'wmladi'
53 _appname_long = _("LADI WindowMaker dockapp")
54 _appid = 'org.linuxaudio.ladi.wmlami'
56 # Default configuration
57 _default_config = {
58 'autostart' : False,
61 def __init__ (self):
62 # Handle the configuration
63 self.global_config = LadiConfiguration (self.appname,
64 self._default_config)
65 self.config_dict = self.global_config.get_config_section (self.appname)
66 autostart = bool(eval(self.config_dict['autostart']))
67 wmoo.Application.__init__ (
68 self,
69 #background = os.path.dirname(sys.argv[0]) + os.sep + "wmjackctl.xpm",
70 margin = 2,
71 debug = False)
73 LadiMenu.__init__(self, autostart)
75 self.addCallback (self.on_button_release, 'buttonrelease', area=(0,0,64,64))
77 self.lines = []
78 for i in range (6):
79 self.lines.append ("")
80 self.clear ()
81 self.started = False
83 def set_starting_status (self):
84 self.set_line (0, "JACK")
85 self.set_line (1, "Starting")
86 self.clear_line (2)
87 self.clear_line (3)
88 self.clear_line (4)
89 self.clear_line (5)
91 def on_button_release (self, event):
92 if event['button'] == 3:
93 self.menu_activate ()
95 def set_line (self, line, text):
96 self.lines[line] = text
97 while len (self.lines[line]) < 9:
98 self.lines[line] += ' '
100 def clear_line (self, line):
101 self.set_line (line, "")
103 def clear (self):
104 for i in range (6):
105 self.clear_line (i)
107 def update (self):
108 try:
109 if self.jack_is_started():
110 self.started = True
112 line0 = "JACK"
113 if self.jack_is_realtime():
114 line0 += " RT"
115 else:
116 line0 += " "
118 if self.a2j_is_available():
119 try:
120 if self.a2j_is_started():
121 line0 += ' A'
122 else:
123 line0 += ' a'
124 except:
125 pass
127 self.set_line(0, line0)
128 self.set_line(1, "started")
129 self.set_line(2, "%.3f %%" % self.jack_get_load())
131 xruns = self.jack_get_xruns()
132 if xruns == 0:
133 self.set_line(3, "no xruns")
134 elif xruns == 1:
135 self.set_line(3, "1 xrun")
136 elif xruns > 999:
137 self.set_line(3, "lot xruns")
138 else:
139 self.set_line(3, "%s xruns" % xruns)
141 rate = self.jack_get_sample_rate()
142 if rate % 1000.0 == 0:
143 self.set_line(4, "%.0f Khz" % (rate / 1000.0))
144 else:
145 self.set_line(4, "%.1f Khz" % (rate / 1000.0))
147 self.set_line(5, "%.1f ms" % self.jack_get_latency())
148 else:
149 self.set_line(0, "JACK")
150 self.set_line(1, "stopped")
151 if self.started:
152 self.clear_line(2)
153 self.clear_line(3)
154 self.clear_line(4)
155 self.clear_line(5)
156 self.started = False
157 self.clear_diagnose_text()
158 except Exception, e:
159 self.set_diagnose_text(repr(e))
160 if debug:
161 print repr (e)
162 self.set_line(0, "JACK")
163 self.set_line(1, "is sick")
164 self.clear_line(2)
165 self.clear_line(3)
166 self.clear_line(4)
167 self.clear_line(5)
168 self.clear_jack_proxies()
170 self.put_lines (self.lines)
171 wmoo.Application.update (self)
172 LadiMenu.update(self)
174 def put_lines (self, lines):
175 x = 3
176 y = 2
177 for line in lines:
178 self.putString (x, y, line)
179 y += 9
181 def do_dockapp (self, user_data = None):
182 """this is called from event loop. events are examined and if a
183 callback has been registered, it is called, passing it the event as
184 argument.
186 event = pywmhelpers.getEvent ()
187 while not event is None:
188 if event['type'] == 'destroynotify':
189 sys.exit (0)
191 for evtype, key, area, callback in self._events:
192 if evtype is not None and evtype != event['type']: continue
193 if key is not None and key != event['button']: continue
194 if area is not None:
195 if not area[0] <= event['x'] <= area[2]: continue
196 if not area[1] <= event['y'] <= area[3]: continue
198 callback (event)
200 event = pywmhelpers.getEvent ()
201 self.redraw ()
202 #print "tick"
203 return True
205 def run_sleep (self):
206 self.go = True
207 while self.go:
208 while Gtk.events_pending ():
209 Gtk.main_iteration ()
210 self.do_dockapp ()
211 while Gtk.events_pending ():
212 Gtk.main_iteration ()
213 time.sleep (self._sleep)
215 def run_gtk (self):
216 #print self._sleep
217 GObject.timeout_add (int(self._sleep * 1000), self.do_dockapp, None)
218 Gtk.main ()
220 def run (self):
221 self.run_gtk ()
222 self.global_config.set_config_section (self.appname, self.config_dict)
223 self.global_config.save ()
225 def position_menu(self, menu, data):
226 _, x, y, _ = Gdk.Display.get_default().get_pointer()
227 return x, y, True
229 def menu_activate(self):
230 menu = self.create_menu()
231 try:
232 menu.popup(parent_menu_shell=None,
233 parent_menu_item=None,
234 func=self.position_menu,
235 data=None,
236 button=3,
237 activate_time=0)
238 except Exception, e:
239 print repr(e)
240 pass
242 if __name__ == '__main__':
243 parser = argparse.ArgumentParser(description=_('Window maker dockapp for jackdbus'),
244 epilog=_('This program is part of the LADITools suite.'))
245 parser.add_argument('--version', action='version', version="%(prog)s " + get_version_string())
247 parser.parse_args()
248 wmladi().run ()
249 sys.exit(0)