2 # asoundconf-gtk - GTK GUI to select the default sound card
5 # asoundconf parts (C) 2005 Canonical Ltd.
6 # gourmet parts (C) 2005 Thomas Hinkle (and any other gourmet developers)
7 # Both the asoundconf and gourmet projects are also licenced under the
8 # GPLv2 or any later version in exactly the same way as this project.
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # as published by the Free Software Foundation; either version 2
13 # of the License, or (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 import sys, re, os, pygtk, gtk, string
26 ######################################
27 #START small block of asoundconf code#
28 ######################################
31 cardspath = '/proc/asound/cards'
32 if not os.path.exists(cardspath):
34 procfile = open(cardspath, 'rb')
35 cardline = re.compile('^\s*\d+\s*\[')
37 lines = procfile.readlines()
40 card_lines.append(re.sub(r'^\s*\d+\s*\[(\w+)\s*\].+','\\1',l))
41 return card_lines # Snipped here
43 ####################################
44 #END small block of asoundconf code#
45 ####################################
47 ###################################
48 #START small block of gourmet code#
49 ###################################
51 def cb_set_active_text (combobox, text, col=0):
52 """Set the active text of combobox to text. We fail
53 if the text is not already in the model. Column is the column
54 of the model from which text is drawn."""
55 model = combobox.get_model()
59 combobox.set_active(n)
64 #################################
65 #END small block of gourmet code#
66 #################################
68 asoundconf = "/usr/bin/asoundconf"
71 '''Kill the application if it cannot run'''
72 if not os.path.exists("/proc/asound/cards"):
73 print "You need at least one ALSA sound card for this to work!"
75 if os.system(asoundconf + " is-active"):
76 print "You need to make sure asoundconf is active!"
77 print "By default, asoundconf's configuration file is ~/.asoundrc.asoundconf"
78 print "and must be included in ~/.asoundrc. Open this file to make sure it is!"
82 '''Get an asoundconf setting'''
83 cmd = asoundconf + " get " + setting
84 output = os.popen(cmd).readlines()
87 def get_default_card():
88 '''# Get the default PCM card (but assume this is the default card for everything)
89 This is just a very simple wrapper for get(...) as otherwise I'd have to repeat
90 a load (well, two lines) of code.'''
91 value_raw = get("defaults.pcm.card")
94 value = string.strip(value_raw[0])
97 def set_default_card(card):
98 '''Set the default card using asoundconf'''
99 call = asoundconf + " set-default-card " + card
100 return os.system(call)
102 def set_pulseaudio():
103 call = asoundconf + " set-pulseaudio"
104 return os.system(call)
106 def unset_pulseaudio():
107 call = asoundconf + " unset-pulseaudio"
108 return os.system(call)
110 class asoundconf_gtk:
111 def destroy(self, widget, data=None):
112 '''This is a stub function to allow for stuff to be done on close'''
115 def delete_event(self, widget, event, data=None):
116 '''Again, a stub to allow stuff to happen when widgets are deleted'''
119 def choose(self, widget, data=None):
120 '''Function to set thde default card on choice'''
121 card = widget.get_active_text()
122 if card == "PulseAudio":
126 set_default_card(card)
128 def pulse(self, widget, data=None):
129 '''Enables/Disables PulseAudio support'''
130 active = widget.get_active()
132 value = set_pulseaudio()
134 value = unset_pulseaudio()
137 def reset(self, widget, data=None):
138 '''Reset the default card to the current default'''
139 if self.combo.get_active_text() == "PulseAudio":
143 set_default_card(get_default_card())
146 # Initiate the window
147 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
148 self.window.set_title("Default Sound Card")
150 self.selectionbox = gtk.HBox(False, 0)
152 self.button = gtk.Button("Quit")
153 self.button.connect("clicked", self.reset, None)
154 self.button.connect_object("clicked", gtk.Widget.destroy, self.window)
156 self.combo = gtk.combo_box_new_text()
157 self.liststore = self.combo.get_model()
158 # Add cards to combobox liststore
163 self.combo.append_text(card.strip())
164 self.combo.connect("changed", self.choose, None)
166 self.label = gtk.Label("Select default card: ")
167 # Add contents to HBox "selectionbox"
168 self.selectionbox.pack_start(self.combo, True, True, 0)
169 self.selectionbox.pack_start(self.button, True, True, 0)
171 self.vbox = gtk.VBox(False, 0)
172 self.window.add(self.vbox)
173 self.vbox.pack_start(self.label, True, True, 0)
174 self.vbox.pack_start(self.selectionbox, True, True, 0)
175 # Create PulseAudio checkbox if ALSA PulseAudio plugin installed
176 if os.path.exists("/usr/lib/alsa-lib/libasound_module_pcm_pulse.so") and os.path.exists("/usr/lib/alsa-lib/libasound_module_ctl_pulse.so"):
177 #self.pulsecheck = gtk.CheckButton("Use _PulseAudio?")
178 self.combo.append_text("PulseAudio")
179 try: pcmDefault = get("pcm.!default")[0]
180 except: pcmDefault = ""
181 if pcmDefault == "{ type pulse }\n":
182 #self.pulsecheck.set_active(True)
183 cb_set_active_text(self.combo, "PulseAudio")
185 # Select current default
186 cb_set_active_text(self.combo, get_default_card())
187 #self.pulsecheck.connect("toggled", self.pulse, None)
188 #self.vbox.pack_start(self.pulsecheck, True, True, 0)
189 # Connect up window events
190 self.window.connect("destroy",self.destroy)
191 self.window.connect("delete_event",self.delete_event)
192 # Show window and contents
193 self.window.show_all()
199 if __name__ == "__main__":
201 aconf = asoundconf_gtk()
202 sys.exit(aconf.main())