@@ -6,23 +6,26 @@ When no arguments are given, this pops up a graphical window which lets you
66choose which audio output device you want sound to go to.
77
88This program can be driven via the command line, and when done so, no window
9- pops up. Here are the options :
9+ pops up. Options have the general form :
1010
11- --headphones[={0,1}]
12- -p[={0,1}]
13- Set the headphones output device. With no value, it toggles the
14- headphone device. With a value, 0 turns the headphones off and 1
15- turns the headphones on.
11+ --device[={0,1}]
12+ -d[={0,1}]
13+ Set the I/O device. With no value, it toggles the specified device.
14+ With a value, 0 turns the device off and 1 turns the device on.
1615
17- --speaker[={0,1}]
18- -s[={0,1}]
19- Set the speaker output device. Value semantics are the same as
20- above.
16+ The list of devices and their short options are:
2117
22- --lineout[={0,1}]
23- -l[={0,1}]
24- Set the line out output device. Value semantics are the same as
25- above.
18+ (input)
19+ microphone -- m
20+ linein -- i
21+ cd -- c
22+
23+ (output)
24+ headphones -- p
25+ speaker -- s
26+ lineout -- o
27+
28+ Other options are:
2629
2730 --help
2831 -h
@@ -41,38 +44,20 @@ KEEPALIVE_TIMER = 500
4144
4245
4346class MainWindow :
44- def __init__ (self ):
47+ def __init__ (self , device ):
48+ self .__devctl = device
49+ info = device .getinfo ()
50+ #
4551 self .__tkroot = tkroot = Tk (className = 'Pynche' )
4652 tkroot .withdraw ()
47- # create the menubar
48- menubar = Menu (tkroot )
49- #
50- # File menu
51- #
52- filemenu = Menu (menubar , tearoff = 0 )
53- filemenu .add_command (label = 'Quit' ,
54- command = self .__quit ,
55- accelerator = 'Alt-Q' ,
56- underline = 0 )
57- #
58- # Tie them together
59- #
60- menubar .add_cascade (label = 'File' ,
61- menu = filemenu ,
62- underline = 0 )
6353 # now create the top level window
64- root = self .__root = Toplevel (tkroot , class_ = 'Audiopy' , menu = menubar )
54+ root = self .__root = Toplevel (tkroot , class_ = 'Audiopy' )
6555 root .protocol ('WM_DELETE_WINDOW' , self .__quit )
6656 root .title ('Audiopy' )
6757 root .iconname ('Audiopy' )
6858 root .tk .createtimerhandler (KEEPALIVE_TIMER , self .__keepalive )
69- root .bind ('<Alt-q>' , self .__quit )
70- root .bind ('<Alt-Q>' , self .__quit )
7159 #
72- # Open up the audio control device and query for the current output
73- # device
74- self .__devctl = sunaudiodev .open ('control' )
75- info = self .__devctl .getinfo ()
60+ buttons = []
7661 #
7762 # where does input come from?
7863 frame = Frame (root , bd = 1 , relief = RAISED )
@@ -89,6 +74,10 @@ class MainWindow:
8974 btn .grid (row = 0 , column = 1 , sticky = W )
9075 root .bind ('<Alt-m>' , self .__mic )
9176 root .bind ('<Alt-M>' , self .__mic )
77+ if not info .i_avail_ports & MICROPHONE :
78+ btn .configure (state = DISABLED )
79+ buttons .append (btn )
80+ ##
9281 self .__lineinvar = IntVar ()
9382 btn = Checkbutton (frame ,
9483 text = 'Line In' ,
@@ -99,6 +88,10 @@ class MainWindow:
9988 btn .grid (row = 1 , column = 1 , sticky = W )
10089 root .bind ('<Alt-i>' , self .__linein )
10190 root .bind ('<Alt-I>' , self .__linein )
91+ if not info .i_avail_ports & LINE_IN :
92+ btn .configure (state = DISABLED )
93+ buttons .append (btn )
94+ ##
10295 self .__cdvar = IntVar ()
10396 btn = Checkbutton (frame ,
10497 text = 'CD' ,
@@ -109,6 +102,9 @@ class MainWindow:
109102 btn .grid (row = 2 , column = 1 , sticky = W )
110103 root .bind ('<Alt-c>' , self .__cd )
111104 root .bind ('<Alt-C>' , self .__cd )
105+ if not info .i_avail_ports & CD :
106+ btn .configure (state = DISABLED )
107+ buttons .append (btn )
112108 #
113109 # where does output go to?
114110 frame = Frame (root , bd = 1 , relief = RAISED )
@@ -125,6 +121,10 @@ class MainWindow:
125121 btn .grid (row = 0 , column = 1 , sticky = W )
126122 root .bind ('<Alt-s>' , self .__speaker )
127123 root .bind ('<Alt-S>' , self .__speaker )
124+ if not info .o_avail_ports & SPEAKER :
125+ btn .configure (state = DISABLED )
126+ buttons .append (btn )
127+ ##
128128 self .__headvar = IntVar ()
129129 btn = Checkbutton (frame ,
130130 text = 'Headphones' ,
@@ -135,6 +135,10 @@ class MainWindow:
135135 btn .grid (row = 1 , column = 1 , sticky = W )
136136 root .bind ('<Alt-h>' , self .__headphones )
137137 root .bind ('<Alt-H>' , self .__headphones )
138+ if not info .o_avail_ports & HEADPHONE :
139+ btn .configure (state = DISABLED )
140+ buttons .append (btn )
141+ ##
138142 self .__linevar = IntVar ()
139143 btn = Checkbutton (frame ,
140144 variable = self .__linevar ,
@@ -145,6 +149,29 @@ class MainWindow:
145149 btn .grid (row = 2 , column = 1 , sticky = W )
146150 root .bind ('<Alt-l>' , self .__lineout )
147151 root .bind ('<Alt-L>' , self .__lineout )
152+ if not info .o_avail_ports & LINE_OUT :
153+ btn .configure (state = DISABLED )
154+ buttons .append (btn )
155+ #
156+ # Fix up widths
157+ widest = 0
158+ for b in buttons :
159+ width = b ['width' ]
160+ if width > widest :
161+ widest = width
162+ for b in buttons :
163+ b .configure (width = widest )
164+ #
165+ # Add quit button
166+ frame = Frame (root )
167+ frame .grid (row = 2 , column = 0 , sticky = 'EW' , ipady = 5 )
168+ btn = Button (frame ,
169+ text = 'Quit' ,
170+ command = self .__quit ,
171+ underline = 0 )
172+ btn .pack (expand = YES )
173+ root .bind ('<Alt-q>' , self .__quit )
174+ root .bind ('<Alt-Q>' , self .__quit )
148175 #
149176 # do we need to poll for changes?
150177 self .__needtopoll = 1
@@ -211,7 +238,9 @@ class MainWindow:
211238 self .__getset (self .__lineinvar , LINE_IN )
212239
213240 def __cd (self , event = None ):
241+ print 'pre:' , self .__cdvar .get ()
214242 self .__getset (self .__cdvar , CD )
243+ print 'post:' , self .__cdvar .get ()
215244
216245 def __speaker (self , event = None ):
217246 self .__getset (self .__spkvar , SPEAKER )
@@ -235,16 +264,38 @@ def usage(msg='', code=1):
235264
236265
237266def main ():
267+ #
268+ # Open up the audio control device and query for the current output
269+ # device
270+ device = sunaudiodev .open ('control' )
271+
238272 if len (sys .argv ) == 1 :
239273 # GUI
240- w = MainWindow ()
274+ w = MainWindow (device )
241275 try :
242276 w .start ()
243277 except KeyboardInterrupt :
244278 pass
245279 return
246280
247- # command line
281+ # spec: LONG OPT, SHORT OPT, 0=input,1=output, MASK
282+ options = (('--microphone' , '-m' , 0 , MICROPHONE ),
283+ ('--linein' , '-i' , 0 , LINE_IN ),
284+ ('--cd' , '-c' , 0 , CD ),
285+ ('--headphones' , '-p' , 1 , HEADPHONE ),
286+ ('--speaker' , '-s' , 1 , SPEAKER ),
287+ ('--lineout' , '-o' , 1 , LINE_OUT ),
288+ )
289+ values = []
290+ info = device .getinfo ()
291+ # first get the existing values
292+ for long , short , io , mask in options :
293+ if io == 0 :
294+ flags = info .i_port
295+ else :
296+ flags = info .o_port
297+ values .append (flags & mask )
298+
248299 sval = None
249300 hval = None
250301 lval = None
0 commit comments