updated on Sat Jan 14 12:12:45 UTC 2012
[aur-mirror.git] / asoundconf / asoundconf-gtk
blob6637f6752838160c64691c1945d3c263671f1432
1 #!/usr/bin/env python2
2 # asoundconf-gtk - GTK GUI to select the default sound card
4 # (C) 2006 Toby Smithe
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.
14
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 ######################################
30 def getcards():
31         cardspath = '/proc/asound/cards'
32         if not os.path.exists(cardspath):
33                 return False
34         procfile = open(cardspath, 'rb')
35         cardline = re.compile('^\s*\d+\s*\[')
36         card_lines = []
37         lines = procfile.readlines()
38         for l in lines:
39                 if cardline.match(l):
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()
56     n = 0
57     for rw in model:
58         if rw[col]==text:
59             combobox.set_active(n)
60             return n
61         n += 1
62     return None
64 #################################
65 #END small block of gourmet code#
66 #################################
68 asoundconf = "/usr/bin/asoundconf"
70 def die_on_error():
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!"
74                 sys.exit(-1)
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!"
79                 sys.exit(-2)
81 def get(setting):
82         '''Get an asoundconf setting'''
83         cmd = asoundconf + " get " + setting
84         output = os.popen(cmd).readlines()
85         return output
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")
92         if not value_raw:
93                 return 0
94         value = string.strip(value_raw[0])
95         return value
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)
109         
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'''
113                 gtk.main_quit()
115         def delete_event(self, widget, event, data=None):
116                 '''Again, a stub to allow stuff to happen when widgets are deleted'''
117                 return False
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":
123                         set_pulseaudio()
124                 else:
125                         unset_pulseaudio()
126                         set_default_card(card)
128         def pulse(self, widget, data=None):
129                 '''Enables/Disables PulseAudio support'''
130                 active = widget.get_active()
131                 if active:
132                         value = set_pulseaudio()
133                 else:
134                         value = unset_pulseaudio()
135                 return value
137         def reset(self, widget, data=None):
138                 '''Reset the default card to the current default'''
139                 if self.combo.get_active_text() == "PulseAudio":
140                         set_pulseaudio()
141                 else:
142                         unset_pulseaudio()                      
143                         set_default_card(get_default_card())
144         
145         def __init__(self):
146                 # Initiate the window
147                 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
148                 self.window.set_title("Default Sound Card")
149                 # Create an HBox box
150                 self.selectionbox = gtk.HBox(False, 0)
151                 # Create a button
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)
155                 # Create combobox
156                 self.combo = gtk.combo_box_new_text()
157                 self.liststore = self.combo.get_model()
158                 # Add cards to combobox liststore
159                 cards = getcards()
160                 if not cards:
161                         return False
162                 for card in cards:
163                         self.combo.append_text(card.strip())
164                 self.combo.connect("changed", self.choose, None)
165                 # Create a label
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)
170                 # Create a VBox
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")
184                         else:
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()
195         def main(self):
196                 '''Do the stuffs'''
197                 gtk.main()
199 if __name__ == "__main__":
200         die_on_error()
201         aconf = asoundconf_gtk()
202         sys.exit(aconf.main())