Add keyboard shortcuts.
[laditools.git] / wmladi
blob6c4376ec15a3d39478860769868f2dcf4ff5385a
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
40 from gi.repository import Gtk
41 from gi.repository import GObject
43 from laditools.gtk import LadiManager
45 # Default configuration
46 autostart_default = 0
47 debug = False
49 class wmladi (wmoo.Application, LadiManager):
50 def __init__ (self):
51 # Handle the configuration
52 self.global_config = LadiConfiguration ()
53 self.param_dict = self.global_config.get_config_section ('wmladi')
54 if self.param_dict != None:
55 if 'autostart' not in self.param_dict:
56 self.param_dict['autostart'] = str (autostart_default)
57 else:
58 self.param_dict = {}
59 self.param_dict['autostart'] = str (autostart_default)
60 autostart = self.param_dict['autostart']
61 wmoo.Application.__init__ (
62 self,
63 #background = os.path.dirname(sys.argv[0]) + os.sep + "wmjackctl.xpm",
64 margin = 2,
65 debug = False)
67 LadiManager.__init__(self, self.global_config.get_config_section ('ladimenu'), int(autostart))
69 self.addCallback (self.on_button_release, 'buttonrelease', area=(0,0,64,64))
71 self.lines = []
72 for i in range (6):
73 self.lines.append ("")
74 self.clear ()
75 self.started = False
77 def set_starting_status (self):
78 self.set_line (0, "JACK")
79 self.set_line (1, "Starting")
80 self.clear_line (2)
81 self.clear_line (3)
82 self.clear_line (4)
83 self.clear_line (5)
85 def on_button_release (self, event):
86 if event['button'] == 3:
87 self.menu_activate ()
89 def set_line (self, line, text):
90 self.lines[line] = text
91 while len (self.lines[line]) < 9:
92 self.lines[line] += ' '
94 def clear_line (self, line):
95 self.set_line (line, "")
97 def clear (self):
98 for i in range (6):
99 self.clear_line (i)
101 def update (self):
102 try:
103 if self.jack_is_started():
104 self.started = True
106 line0 = "JACK"
107 if self.jack_is_realtime():
108 line0 += " RT"
109 else:
110 line0 += " "
112 if self.a2j_is_available():
113 try:
114 if self.a2j_is_started():
115 line0 += ' A'
116 else:
117 line0 += ' a'
118 except:
119 pass
121 self.set_line(0, line0)
122 self.set_line(1, "started")
123 self.set_line(2, "%.3f %%" % self.jack_get_load())
125 xruns = self.jack_get_xruns()
126 if xruns == 0:
127 self.set_line(3, "no xruns")
128 elif xruns == 1:
129 self.set_line(3, "1 xrun")
130 elif xruns > 999:
131 self.set_line(3, "lot xruns")
132 else:
133 self.set_line(3, "%s xruns" % xruns)
135 rate = self.jack_get_sample_rate()
136 if rate % 1000.0 == 0:
137 self.set_line(4, "%.0f Khz" % (rate / 1000.0))
138 else:
139 self.set_line(4, "%.1f Khz" % (rate / 1000.0))
141 self.set_line(5, "%.1f ms" % self.jack_get_latency())
142 else:
143 self.set_line(0, "JACK")
144 self.set_line(1, "stopped")
145 if self.started:
146 self.clear_line(2)
147 self.clear_line(3)
148 self.clear_line(4)
149 self.clear_line(5)
150 self.started = False
151 self.clear_diagnose_text()
152 except Exception, e:
153 self.set_diagnose_text(repr(e))
154 if debug:
155 print repr (e)
156 self.set_line(0, "JACK")
157 self.set_line(1, "is sick")
158 self.clear_line(2)
159 self.clear_line(3)
160 self.clear_line(4)
161 self.clear_line(5)
162 self.clear_jack_proxies()
164 self.put_lines (self.lines)
165 wmoo.Application.update (self)
166 # Take a look at the processes we've started so we don't get any zombies
167 for i in self.proc_list:
168 i.poll ()
170 def put_lines (self, lines):
171 x = 3
172 y = 2
173 for line in lines:
174 self.putString (x, y, line)
175 y += 9
177 def do_dockapp (self, user_data = None):
178 """this is called from event loop. events are examined and if a
179 callback has been registered, it is called, passing it the event as
180 argument.
182 event = pywmhelpers.getEvent ()
183 while not event is None:
184 if event['type'] == 'destroynotify':
185 sys.exit (0)
187 for evtype, key, area, callback in self._events:
188 if evtype is not None and evtype != event['type']: continue
189 if key is not None and key != event['button']: continue
190 if area is not None:
191 if not area[0] <= event['x'] <= area[2]: continue
192 if not area[1] <= event['y'] <= area[3]: continue
194 callback (event)
196 event = pywmhelpers.getEvent ()
197 self.redraw ()
198 #print "tick"
199 return True
201 def run_sleep (self):
202 self.go = True
203 while self.go:
204 while Gtk.events_pending ():
205 Gtk.main_iteration ()
206 self.do_dockapp ()
207 while Gtk.events_pending ():
208 Gtk.main_iteration ()
209 time.sleep (self._sleep)
211 def run_gtk (self):
212 #print self._sleep
213 GObject.timeout_add (int(self._sleep * 1000), self.do_dockapp, None)
214 Gtk.main ()
216 def run (self):
217 self.run_gtk ()
218 self.global_config.set_config_section ('wmladi', self.param_dict)
219 self.global_config.set_config_section ('ladimenu', self.menu_array)
220 self.global_config.save ()
222 def position_menu(self, menu, data):
223 _, x, y, _ = Gdk.Display.get_default().get_pointer()
224 return x, y, True
226 def menu_activate(self):
227 menu = self.create_menu()
228 try:
229 menu.popup(parent_menu_shell=None,
230 parent_menu_item=None,
231 func=self.position_menu,
232 data=None,
233 button=3,
234 activate_time=0)
235 except Exception, e:
236 print repr(e)
237 pass
239 if __name__ == '__main__':
240 parser = argparse.ArgumentParser(description=_('Window maker dockapp for jackdbus'),
241 epilog=_('This program is part of the LADITools suite.'))
242 parser.add_argument('--version', action='version', version="%(prog)s " + get_version_string())
244 parser.parse_args()
245 wmladi().run ()
246 sys.exit(0)