Remove trailing 'parameters' word from command description
[laditools.git] / ladi-player
blob24fdf34a65ada00a940ec77d6fc6c3d0b5ca7e2a
1 #!/usr/bin/python
2 # LADITools - Linux Audio Desktop Integration Tools
3 # laditray - System tray integration for LADI
4 # Copyright (C) 2012 Alessio Treglia <quadrispro@ubuntu.com>
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 import os
20 import sys
21 import time
22 import signal
23 import gettext
24 import argparse
26 sig_handler = signal.getsignal(signal.SIGTERM)
27 signal.signal(signal.SIGINT, sig_handler)
29 from laditools import _gettext_domain
30 gettext.install(_gettext_domain)
32 from laditools import get_version_string
33 from laditools import LadiConfiguration
34 from laditools import LadiApp
36 from gi.repository import Gtk
37 from gi.repository import GObject
38 from laditools import LadiManager
39 from laditools.gtk import LadiManagerGtk
40 from laditools.gtk import find_data_file
42 timeout_add = GObject.timeout_add
44 class LadiPlayer(LadiManagerGtk, LadiApp):
46 _appname = 'ladi-player'
47 _appname_long = _("LADI Player")
48 _appid = 'org.linuxaudio.ladi.player'
50 # Default configuration
51 _default_config = {
52 'autostart' : False,
55 def on_about(self, *args):
56 LadiManagerGtk.on_about(self, parent=self.window_main, version=get_version_string())
58 def on_quit(self, *args, **kwargs):
59 self.global_config.set_config_section (self.appname, self.config_dict)
60 self.global_config.save()
61 Gtk.main_quit()
63 def _run_select_studio_dialog(self, title, *args):
64 studio_list = self.get_ladish_controller().studio_list()
65 if not studio_list:
66 dlg = Gtk.MessageDialog(None,Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
67 Gtk.MessageType.INFO,
68 Gtk.ButtonsType.CLOSE,
69 _('No studio is available.'))
70 dlg.set_transient_for(self.window_main)
71 dlg.set_title(title)
72 dlg.run()
73 dlg.destroy()
74 return None
76 dlg = Gtk.Dialog()
77 treeview = Gtk.TreeView()
78 model = Gtk.ListStore(GObject.TYPE_STRING)
79 renderer = Gtk.CellRendererText()
80 column = Gtk.TreeViewColumn('Available studios', renderer, text=0)
82 treeview.set_model(model)
83 treeview.append_column(column)
84 treeview.set_cursor(Gtk.TreePath(path=0),
85 focus_column=column,
86 start_editing=False)
88 for studio in studio_list:
89 model.append((studio,))
91 dlg.set_transient_for(self.window_main)
92 dlg.set_modal(True)
93 dlg.set_destroy_with_parent(True)
94 dlg.set_title(title)
95 dlg.get_content_area().pack_start(child=treeview,
96 expand=True,
97 fill=True,
98 padding=10)
99 dlg.add_button(Gtk.STOCK_OK, Gtk.ResponseType.OK)
100 dlg.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL)
101 dlg.show_all()
103 response = dlg.run()
104 ret = None
105 if response == Gtk.ResponseType.OK:
106 path, column = treeview.get_cursor()
107 ret = model[path][0]
108 dlg.hide()
109 return ret
111 def _toolbuttons_set_active(self, group, **kwargs):
112 buttons = getattr(self, "%s_status_buttons" % group)
113 if 'all' in kwargs:
114 status = kwargs['all']
115 for button in buttons:
116 buttons[button].set_sensitive(status)
117 else:
118 for button in kwargs:
119 buttons[button].set_sensitive(kwargs[button])
121 def jack_is_started(self, *args, **kwargs):
122 while True:
123 try:
124 return LadiManagerGtk.jack_is_started(self, *args, **kwargs)
125 except:
126 time.sleep(0.5)
127 continue
129 def ladish_toolbuttons_set_active(self, **kwargs): self._toolbuttons_set_active('ladish', **kwargs)
130 def jack_toolbuttons_set_active(self, **kwargs): self._toolbuttons_set_active('jack', **kwargs)
131 def a2j_toolbuttons_set_active(self, **kwargs): self._toolbuttons_set_active('a2j', **kwargs)
133 def ladish_status_buttons_update(self):
134 buttons = self.ladish_status_buttons
136 # Setup ladish controls
137 if self.ladish_is_available():
138 # Buttons "rename" and "unload"
139 if self.studio_is_loaded():
140 self.ladish_toolbuttons_set_active(unload=True, rename=True)
141 # Buttons "start" and "stop"
142 if self.studio_is_started():
143 self.ladish_toolbuttons_set_active(start=False, stop=True, save=True)
144 else:
145 self.ladish_toolbuttons_set_active(start=True, stop=False, save=False)
146 else:
147 self.ladish_toolbuttons_set_active(start=True,
148 stop=False,
149 save=False,
150 unload=False,
151 rename=False)
152 else:
153 self.ladish_toolbuttons_set_active(all=False)
155 def jack_status_buttons_update(self):
156 if not self.jack_is_available():
157 return
159 buttons = self.jack_status_buttons
160 ladish_available = self.ladish_is_available()
161 jack_started = self.jack_is_started()
163 if jack_started:
164 self.jack_toolbuttons_set_active(jack_start=False,
165 jack_stop=(not ladish_available),
166 jack_reset_xruns=True,
167 jack_reactivate=True)
168 else:
169 self.jack_toolbuttons_set_active(jack_start=True,
170 jack_stop=False,
171 jack_reset_xruns=False,
172 jack_reactivate=True)
175 def a2j_status_buttons_update(self):
176 if not self.a2j_is_available():
177 self.a2j_toolbuttons_set_active(all=False)
178 return
180 buttons = self.a2j_status_buttons
181 ladish_available = self.ladish_is_available()
182 a2j_started = self.a2j_is_started()
184 if not ladish_available:
185 if a2j_started:
186 self.a2j_toolbuttons_set_active(a2j_start=False,
187 a2j_stop=True,
188 a2j_reactivate=True)
189 else:
190 self.a2j_toolbuttons_set_active(a2j_start=True,
191 a2j_stop=False,
192 a2j_reactivate=True)
193 else:
194 self.a2j_toolbuttons_set_active(a2j_start=False,
195 a2j_stop=False,
196 a2j_reactivate=True)
198 def update_status_buttons(self):
200 # Update widgets
201 self.ladish_status_buttons_update()
202 self.jack_status_buttons_update()
203 self.a2j_status_buttons_update()
205 # Update status label and window icon
206 if self.jack_is_started():
207 self.window_main.set_icon_name("ladi-started")
208 if self.jack_is_realtime():
209 status_text = "RT | "
210 else:
211 status_text = ""
212 # Get DSP Load
213 status_text += str (round (float (self.jack_get_load()),1)) + "% | "
214 # Get Xruns
215 status_text += str (self.jack_get_xruns())
216 # Set a started status
217 self.status_label.set_label (status_text)
218 else:
219 self.window_main.set_icon_name("ladi-stopped")
220 self.status_label.set_label(_('<i>Stopped</i>'))
222 def update(self, *args, **kwargs):
223 self.update_status_buttons()
224 LadiManagerGtk.update(self, *args, **kwargs)
225 return True
227 def _set_starting_status(self):
228 self.window_main.set_icon_name("ladi-starting")
230 def action_ladish_new(self, action, *args):
231 self.studio_new()
233 def action_ladish_start(self, action, *args):
234 if self.studio_is_loaded():
235 if not self.studio_is_started():
236 self._set_starting_status()
237 self.studio_start()
238 self.update_status_buttons()
239 else:
240 self._set_starting_status()
241 self.jack_start()
242 self.update_status_buttons()
244 def action_ladish_save(self, action, *args):
245 self.studio_save()
247 def action_ladish_stop(self, action, *args):
248 if self.jack_is_started() and self.studio_is_started():
249 self.studio_stop()
250 self.update_status_buttons()
252 def action_ladish_rename(self, action, *args):
253 self.studio_rename()
254 self.update_status_buttons()
256 def action_ladish_load(self, action, *args):
257 selection = self._run_select_studio_dialog(_('Load studio'))
258 if selection:
259 self.studio_load(studio=selection)
260 return selection
262 def action_ladish_delete(self, action, *args):
263 selection = self._run_select_studio_dialog(_('Delete studio'))
264 if selection:
265 LadiManager.studio_delete(self, studio=selection)
266 return selection
268 def action_ladish_unload(self, action, *args):
269 while True:
270 try:
271 if self.studio_is_loaded():
272 self.studio_unload()
273 self.update_status_buttons()
274 break
275 except:
276 time.sleep(0.5)
277 continue
279 def action_ladish_reactivate(self, action, *args):
280 self.ladish_reactivate()
282 def action_jack_start(self, action, *args):
283 self._set_starting_status()
284 self.jack_start()
285 def action_jack_stop(self, action, *args):
286 self.jack_stop()
287 def action_jack_reset_xruns(self, action, *args):
288 self.jack_reset_xruns()
289 def action_jack_reactivate(self, action, *args):
290 self.jack_reactivate()
292 def action_a2j_start(self, action, *args):
293 self.a2j_start()
294 def action_a2j_stop(self, action, *args):
295 self.a2j_stop()
296 def action_a2j_reactivate(self, action, *args):
297 self.a2j_reactivate()
299 def action_launcher_launch(self, action, *args):
300 self.launcher_exec(command=self._launcher_which(action.get_name()))
302 def __init__(self):
303 # Handle the configuration
304 self.global_config = LadiConfiguration(self.appname, self._default_config)
305 self.config_dict = self.global_config.get_config_section (self.appname)
306 autostart = bool(eval(self.config_dict['autostart']))
308 LadiManagerGtk.__init__(self, autostart)
310 # Build the UI
311 builder = Gtk.Builder()
312 ui_path = find_data_file("ladi-player.ui")
313 builder.add_from_file(ui_path)
314 sys.stderr.write( _("Loading interface from %s\n") % ui_path)
315 sys.stderr.flush()
317 # Retrieve objects
318 self.window_main = builder.get_object("window_main")
319 actiongroup_ladish = builder.get_object("actiongroup_ladish")
320 actiongroup_jack = builder.get_object("actiongroup_jack")
321 actiongroup_a2j = builder.get_object("actiongroup_a2j")
322 actiongroup_launcher = builder.get_object("actiongroup_launcher")
323 self.status_label = builder.get_object('toolbutton_label_status')
325 # Setup status buttons
326 self.ladish_status_buttons = ladish_status_buttons = {}
327 self.jack_status_buttons = jack_status_buttons = {}
328 self.a2j_status_buttons = a2j_status_buttons = {}
329 self.launcher_status_buttons = launcher_status_buttons = {}
331 for action in actiongroup_ladish.list_actions():
332 ladish_status_buttons[action.get_name()] = action
333 for action in actiongroup_jack.list_actions():
334 jack_status_buttons[action.get_name()] = action
335 for action in actiongroup_a2j.list_actions():
336 a2j_status_buttons[action.get_name()] = action
337 for action in actiongroup_launcher.list_actions():
338 launcher_status_buttons[action.get_name()] = action
340 # Remove launchers for unavailable commands
341 for command in launcher_status_buttons:
342 if not self._launcher_which(command):
343 launcher_status_buttons[command].set_active(False)
345 # Accelerators
346 # accelgroup = builder.get_object("accelgroup1")
347 # action = actiongroup_launcher.get_action("gladish")
348 # action.set_accel_group(accelgroup)
349 # action.set_accel_path("menu_studio")
351 # Get the initial status
352 self.update ()
353 # Add the auto update callback
354 self.auto_updater = timeout_add (250, self.update, None)
356 builder.connect_signals(self)
358 def run(self):
359 self.window_main.show_all()
361 if __name__ == "__main__":
362 parser = argparse.ArgumentParser(description=_('graphical front-end that allows users to start, stop and'
363 'monitor JACK, as well as start some JACK related applications'),
364 epilog=_('This program is part of the LADITools suite.'))
365 parser.add_argument('--version', action='version', version="%(prog)s " + get_version_string())
367 parser.parse_args()
368 GObject.threads_init()
370 LadiPlayer().run()
371 Gtk.main()
372 sys.exit(0)