root/trunk/libffado/support/mixer-qt4/ffado/panelmanager.py

Revision 1692, 13.3 kB (checked in by arnonym, 11 years ago)

Fix the dummy after last commit.

  • Property svn:mergeinfo set to
Line 
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2005-2008 by Pieter Palmers
4 #               2007-2008 by Arnold Krille
5 #
6 # This file is part of FFADO
7 # FFADO = Free Firewire (pro-)audio drivers for linux
8 #
9 # FFADO is based upon FreeBoB.
10 #
11 # This program is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation, either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 #
24
25 from ffado.config import * #FFADO_VERSION, FFADO_DBUS_SERVER, FFADO_DBUS_BASEPATH
26
27 from PyQt4.QtGui import QFrame, QWidget, QTabWidget, QVBoxLayout, QMainWindow, QIcon, QAction, qApp, QStyleOptionTabWidgetFrame
28 from PyQt4.QtCore import QTimer
29
30 from ffado.dbus_util import *
31 from ffado.registration import *
32
33 from ffado.configuration import *
34
35 from ffado.mixer.globalmixer import GlobalMixer
36 from ffado.mixer.dummy import Dummy
37
38 import time
39
40 import logging
41 log = logging.getLogger('panelmanager')
42
43 use_generic = False
44 try:
45     from mixer_generic import *
46     log.info("The generic mixer is found, seems to be a developer using ffadomixer...")
47 except ImportError:
48     pass
49 else:
50     use_generic = True
51
52 # pseudo-guid
53 GUID_GENERIC_MIXER = 0
54
55
56 class HLine( QFrame ):
57     def __init__( self, parent ):
58         QFrame.__init__( self, parent )
59         self.setFrameShape( QFrame.HLine )
60         self.setLineWidth( 2 )
61         self.setMinimumHeight( 10 )
62
63 class PanelManagerStatus(QWidget):
64     def __init__(self, parent):
65         QWidget.__init__(self,parent)
66         uicLoad("ffado/panelmanagerstatus", self)
67
68 class OwnTabWidget(QTabWidget):
69     def __init__(self,parent):
70         QTabWidget.__init__(self,parent)
71
72     def tabInserted(self,index):
73         self.checkTabBar()
74
75     def tabRemoved(self,index):
76         self.checkTabBar()
77
78     def checkTabBar(self):
79         if self.count()<2:
80             self.tabBar().hide()
81         else:
82             self.tabBar().show()
83
84 class PanelManager(QWidget):
85     def __init__(self, parent, devmgr=None):
86         QWidget.__init__(self, parent)
87         self.setObjectName("PanelManager")
88
89         # maps a device GUID to a QT panel
90         self.panels = {}
91
92         # a layout for ourselves
93         self.layout = QVBoxLayout(self)
94
95         # the tabs
96         self.tabs = OwnTabWidget(self)
97         self.tabs.hide()
98         self.layout.addWidget(self.tabs)
99
100         # a dialog that is shown during update
101         self.status = PanelManagerStatus(self)
102         self.layout.addWidget(self.status)
103         self.status.show()
104
105         self.devices = DeviceList( SYSTEM_CONFIG_FILE )
106         self.devices.updateFromFile( USER_CONFIG_FILE )
107
108         if devmgr is not None:
109             self.setManager(devmgr)
110
111     def setManager(self,devmgr):
112         self.devmgr = devmgr
113         self.devmgr.registerPreUpdateCallback(self.devlistPreUpdate)
114         self.devmgr.registerPostUpdateCallback(self.devlistPostUpdate)
115         self.devmgr.registerUpdateCallback(self.devlistUpdate)
116         self.devmgr.registerDestroyedCallback(self.devmgrDestroyed)
117         # create a timer to poll the panels
118         self.polltimer = QTimer()
119         self.connect( self.polltimer, SIGNAL('timeout()'), self.pollPanels )
120         self.polltimer.start( POLL_SLEEP_TIME_MSEC )
121
122         # create a timer to initialize the panel after the main form is shown
123         # since initialization can take a while
124         QTimer.singleShot( POLL_SLEEP_TIME_MSEC, self.updatePanels )
125
126         # live check timer
127         self.alivetimer = QTimer()
128         QObject.connect( self.alivetimer, SIGNAL('timeout()'), self.commCheck )
129         self.alivetimer.start( 2000 )
130
131     def count(self):
132         return self.tabs.count()
133
134     def pollPanels(self):
135         #log.debug("PanelManager::pollPanels()")
136         # only when not modifying the tabs
137         if self.tabs.isEnabled():
138             for guid in self.panels.keys():
139                 w = self.panels[guid]
140                 for child in w.children():
141                     #log.debug("poll child %s,%s" % (guid,child))
142                     if 'polledUpdate' in dir(child):
143                         try:
144                             child.polledUpdate()
145                         except:
146                             log.error("error in polled update")
147                             raise
148
149     def devlistPreUpdate(self):
150         log.debug("devlistPreUpdate")
151         self.tabs.setEnabled(False)
152         self.tabs.hide()
153         self.status.lblMessage.setText("Bus reconfiguration in progress, please wait...")
154         self.status.show()
155         #self.statusBar().showMessage("bus reconfiguration in progress...", 5000)
156
157     def devlistPostUpdate(self):
158         log.debug("devlistPostUpdate")
159         # this can fail if multiple busresets happen in fast succession
160         ntries = 10
161         while ntries > 0:
162             try:
163                 self.updatePanels()
164                 return
165             except:
166                 log.debug("devlistPostUpdate failed (%d)" % ntries)
167                 for guid in self.panels.keys():
168                     w = self.panels[guid]
169                     del self.panels[guid] # remove from the list
170                     idx = self.tabs.indexOf(w)
171                     self.tabs.removeTab(idx)
172                     del w # GC might also take care of that
173
174                 ntries = ntries - 1
175                 time.sleep(2) # sleep a few seconds
176
177         log.debug("devlistPostUpdate failed completely")
178         self.tabs.setEnabled(False)
179         self.tabs.hide()
180         self.status.lblMessage.setText("Error while reconfiguring. Please restart ffadomixer.")
181         self.status.show()
182
183
184     def devlistUpdate(self):
185         log.debug("devlistUpdate")
186
187     def devmgrDestroyed(self):
188         log.debug("devmgrDestroyed")
189         self.alivetimer.stop()
190         self.tabs.setEnabled(False)
191         self.tabs.hide()
192         self.status.lblMessage.setText("DBUS server was shut down, please restart it and restart ffadomixer...")
193         self.status.show()
194
195     def commCheck(self):
196         try:
197             nbDevices = self.devmgr.getNbDevices()
198         except:
199             log.debug("comms lost")
200             self.tabs.setEnabled(False)
201             self.tabs.hide()
202             self.status.lblMessage.setText("Failed to communicate with DBUS server. Please restart it and restart ffadomixer...")
203             self.status.show()
204             self.alivetimer.stop()
205
206     def updatePanels(self):
207         log.debug("PanelManager::updatePanels()")
208         nbDevices = self.devmgr.getNbDevices()
209         #self.statusBar().showMessage("Reconfiguring the mixer panels...")
210
211         # list of panels present
212         guids_with_tabs = self.panels.keys()
213
214         # build list of guids on the bus now
215         guids_present = []
216         guid_indexes = {}
217         for idx in range(nbDevices):
218             path = self.devmgr.getDeviceName(idx)
219             cfgrom = ConfigRomInterface(FFADO_DBUS_SERVER, FFADO_DBUS_BASEPATH+'/DeviceManager/'+path)
220             guid = cfgrom.getGUID()
221             guids_present.append(guid)
222             guid_indexes[guid] = idx
223
224         # figure out what to remove
225         # the special panel (generic)
226         # that has (pseudo-)GUID 0
227         # is also automatically removed
228         to_remove = []
229         for guid in guids_with_tabs:
230             if not guid in guids_present:
231                 to_remove.append(guid)
232                 log.debug("going to remove %s" % str(guid))
233             else:
234                 log.debug("going to keep %s" % str(guid))
235
236         # figure out what to add
237         to_add = []
238         for guid in guids_present:
239             if not guid in guids_with_tabs:
240                 to_add.append(guid)
241                 log.debug("going to add %s" % str(guid))
242
243         # update the widget
244         for guid in to_remove:
245             w = self.panels[guid]
246             del self.panels[guid] # remove from the list
247             idx = self.tabs.indexOf(w)
248             self.tabs.removeTab(idx)
249             del w # GC might also take care of that
250
251         for guid in to_add:
252             # retrieve the device manager index
253             idx = guid_indexes[guid]
254             path = self.devmgr.getDeviceName(idx)
255             log.debug("Adding device %d: %s" % (idx, path))
256
257             cfgrom = ConfigRomInterface(FFADO_DBUS_SERVER, FFADO_DBUS_BASEPATH+'/DeviceManager/'+path)
258             vendorId = cfgrom.getVendorId()
259             modelId = cfgrom.getModelId()
260             unitVersion = cfgrom.getUnitVersion()
261             guid = cfgrom.getGUID()
262             vendorName = cfgrom.getVendorName()
263             modelName = cfgrom.getModelName()
264             log.debug(" Found (%s, %X, %X) %s %s" % (str(guid), vendorId, modelId, vendorName, modelName))
265
266             # check whether this has already been registered at ffado.org
267             reg = ffado_registration(FFADO_VERSION, int(guid, 16),
268                                      vendorId, modelId,
269                                      vendorName, modelName)
270             reg.check_for_registration()
271
272             # The MOTU devices use unitVersion to differentiate models.  For the
273             # moment though we don't need to know precisely which model we're
274             # using.
275             if vendorId == 0x1f2:
276                 modelId = 0x00000000
277
278             # The RME devices use unitVersion to differentiate models.
279             # Therefore in the configuration file we use the config file's
280             # modelid field to store the unit version.  As a result we must
281             # override the modelId with the unit version here so the correct
282             # configuration file entry (and hense mixer widget) is identified.
283             if vendorId == 0xa35:
284                 modelId = unitVersion;
285
286             dev = self.devices.getDeviceById( vendorId, modelId )
287
288             w = QWidget( )
289             l = QVBoxLayout( w )
290
291             # create a control object
292             hw = ControlInterface(FFADO_DBUS_SERVER, FFADO_DBUS_BASEPATH+'/DeviceManager/'+path)
293             clockselect = ClockSelectInterface( FFADO_DBUS_SERVER, FFADO_DBUS_BASEPATH+"/DeviceManager/"+path )
294             samplerateselect = SamplerateSelectInterface( FFADO_DBUS_SERVER, FFADO_DBUS_BASEPATH+"/DeviceManager/"+path )
295             streamingstatus = StreamingStatusInterface( FFADO_DBUS_SERVER, FFADO_DBUS_BASEPATH+"/DeviceManager/"+path )
296             nickname = TextInterface( FFADO_DBUS_SERVER, FFADO_DBUS_BASEPATH+"/DeviceManager/"+path+"/Generic/Nickname" )
297
298             #
299             # Generic elements for all mixers follow here:
300             #
301             globalmixer = GlobalMixer( w )
302             globalmixer.configrom = cfgrom
303             globalmixer.clockselect = clockselect
304             globalmixer.samplerateselect = samplerateselect
305             globalmixer.streamingstatus = streamingstatus
306             globalmixer.nickname = nickname
307             globalmixer.hw = hw
308             globalmixer.initValues()
309             l.addWidget( globalmixer, 1 )
310
311             #
312             # Line to separate
313             #
314             l.addWidget( HLine( w ) )
315
316             #
317             # Specific (or dummy) mixer widgets get loaded in the following
318             #
319             if 'mixer' in dev and dev['mixer'] != None:
320                 mixerapp = dev['mixer']
321                 exec( """
322 import ffado.mixer.%s
323 mixerwidget = ffado.mixer.%s.%s( w )
324 """ % (mixerapp.lower(), mixerapp.lower(), mixerapp) )
325             else:
326                 mixerwidget = Dummy( w )
327                 mixerapp = modelName+" (Dummy)"
328
329             #
330             # The same for all mixers
331             #
332             l.addWidget( mixerwidget, 10 )
333             mixerwidget.configrom = cfgrom
334             mixerwidget.clockselect = clockselect
335             mixerwidget.samplerateselect = samplerateselect
336             mixerwidget.streamingstatus = streamingstatus
337             mixerwidget.nickname = nickname
338             mixerwidget.hw = hw
339             if 'buildMixer' in dir(mixerwidget):
340                 mixerwidget.buildMixer()
341             if 'initValues' in dir(mixerwidget):
342                 mixerwidget.initValues()
343             if 'getDisplayTitle' in dir(mixerwidget):
344                 title = mixerwidget.getDisplayTitle()
345             else:
346                 title = mixerapp
347
348             globalmixer.setName(title)
349             self.tabs.addTab( w, title )
350             self.panels[guid] = w
351
352         # if there is no panel, add the no-device message
353         # else make sure it is not present
354         if self.count() == 0:
355             self.tabs.hide()
356             self.tabs.setEnabled(False)
357             self.status.lblMessage.setText("No supported device found.")
358             self.status.show()
359             #self.statusBar().showMessage("No supported device found.", 5000)
360         else:
361             self.tabs.show()
362             self.tabs.setEnabled(True)
363             self.status.hide()
364             #self.statusBar().showMessage("Configured the mixer for %i devices." % self.tabs.count())
365             if use_generic:
366                 #
367                 # Show the generic (development) mixer if it is available
368                 #
369                 w = GenericMixer( devmgr.bus, FFADO_DBUS_SERVER, mw )
370                 self.tabs.addTab( w, "Generic Mixer" )
371                 self.panels[GUID_GENERIC_MIXER] = w
372
373 # vim: et
Note: See TracBrowser for help on using the browser.