blob: f5a10218d2b16d78488c1ba20c8076de79a7005e [file] [log] [blame]
Barry Warsaw2a060841998-10-30 18:21:52 +00001#! /usr/bin/env python
2
Barry Warsawd81d5341998-11-18 03:28:45 +00003"""audiopy -- a program to control the Solaris audio device.
Barry Warsaw2a060841998-10-30 18:21:52 +00004
Barry Warsaw88604051998-12-14 21:36:40 +00005Contact: Barry Warsaw
Barry Warsawd81d5341998-11-18 03:28:45 +00006Email: bwarsaw@python.org
7Version: %(__version__)s
Barry Warsawf79b7e21998-11-18 00:27:14 +00008
Barry Warsaw2a060841998-10-30 18:21:52 +00009When no arguments are given, this pops up a graphical window which lets you
Barry Warsawa4b055f1999-04-28 19:32:46 +000010choose the audio input and output devices, and set the output volume.
Barry Warsaw2a060841998-10-30 18:21:52 +000011
12This program can be driven via the command line, and when done so, no window
Barry Warsawa4b055f1999-04-28 19:32:46 +000013pops up. Most options have the general form:
Barry Warsaw2a060841998-10-30 18:21:52 +000014
Barry Warsaw2c8b35b1998-10-31 00:25:14 +000015 --device[={0,1}]
16 -d[={0,1}]
17 Set the I/O device. With no value, it toggles the specified device.
18 With a value, 0 turns the device off and 1 turns the device on.
Barry Warsaw2a060841998-10-30 18:21:52 +000019
Barry Warsaw2c8b35b1998-10-31 00:25:14 +000020The list of devices and their short options are:
Barry Warsaw2a060841998-10-30 18:21:52 +000021
Barry Warsaw2c8b35b1998-10-31 00:25:14 +000022 (input)
23 microphone -- m
24 linein -- i
25 cd -- c
26
27 (output)
28 headphones -- p
29 speaker -- s
30 lineout -- o
31
32Other options are:
Barry Warsaw2a060841998-10-30 18:21:52 +000033
Barry Warsawa4b055f1999-04-28 19:32:46 +000034 --gain volume
35 -g volume
36 Sets the output gain to the specified volume, which must be an integer
37 in the range [%(MIN_GAIN)s..%(MAX_GAIN)s]
38
Barry Warsaw3febc241998-11-01 07:10:51 +000039 --version
40 -v
41 Print the version number and exit.
42
Barry Warsaw2a060841998-10-30 18:21:52 +000043 --help
44 -h
45 Print this message and exit.
Barry Warsaw2a060841998-10-30 18:21:52 +000046"""
47
48import sys
Barry Warsawf622a331998-12-03 19:32:38 +000049import os
50import string
Barry Warsawd8d179d1999-09-22 15:45:51 +000051import errno
Barry Warsaw2a060841998-10-30 18:21:52 +000052import sunaudiodev
53from SUNAUDIODEV import *
54
55# Milliseconds between interrupt checks
56KEEPALIVE_TIMER = 500
57
Barry Warsaw772d6961999-08-18 20:17:42 +000058__version__ = '1.1'
Barry Warsaw3febc241998-11-01 07:10:51 +000059
Barry Warsaw2a060841998-10-30 18:21:52 +000060
61
62class MainWindow:
Barry Warsaw2c8b35b1998-10-31 00:25:14 +000063 def __init__(self, device):
Barry Warsaw9ade9dd1998-11-01 07:13:14 +000064 from Tkinter import *
Barry Warsawf622a331998-12-03 19:32:38 +000065 self.__helpwin = None
Barry Warsaw2c8b35b1998-10-31 00:25:14 +000066 self.__devctl = device
67 info = device.getinfo()
68 #
Barry Warsaw3febc241998-11-01 07:10:51 +000069 self.__tkroot = tkroot = Tk(className='Audiopy')
Barry Warsaw2a060841998-10-30 18:21:52 +000070 tkroot.withdraw()
Barry Warsawf622a331998-12-03 19:32:38 +000071 # create the menubar
72 menubar = Menu(tkroot)
73 filemenu = Menu(menubar, tearoff=0)
74 filemenu.add_command(label='Quit',
75 command=self.__quit,
76 accelerator='Alt-Q',
77 underline=0)
78 helpmenu = Menu(menubar, name='help', tearoff=0)
79 helpmenu.add_command(label='About Audiopy...',
80 command=self.__popup_about,
81 underline=0)
82 helpmenu.add_command(label='Help...',
83 command=self.__popup_using,
84 underline=0)
85 menubar.add_cascade(label='File',
86 menu=filemenu,
87 underline=0)
88 menubar.add_cascade(label='Help',
89 menu=helpmenu,
90 underline=0)
Barry Warsaw2a060841998-10-30 18:21:52 +000091 # now create the top level window
Barry Warsawf622a331998-12-03 19:32:38 +000092 root = self.__root = Toplevel(tkroot, class_='Audiopy', menu=menubar)
Barry Warsaw2a060841998-10-30 18:21:52 +000093 root.protocol('WM_DELETE_WINDOW', self.__quit)
Barry Warsaw3febc241998-11-01 07:10:51 +000094 root.title('audiopy ' + __version__)
95 root.iconname('audiopy ' + __version__)
Barry Warsaw2a060841998-10-30 18:21:52 +000096 root.tk.createtimerhandler(KEEPALIVE_TIMER, self.__keepalive)
Barry Warsaw2a060841998-10-30 18:21:52 +000097 #
Barry Warsaw2c8b35b1998-10-31 00:25:14 +000098 buttons = []
Barry Warsaw2a060841998-10-30 18:21:52 +000099 #
Barry Warsawb7d1d631998-10-30 23:45:41 +0000100 # where does input come from?
101 frame = Frame(root, bd=1, relief=RAISED)
Barry Warsawf622a331998-12-03 19:32:38 +0000102 frame.grid(row=1, column=0, sticky='NSEW')
Barry Warsawb7d1d631998-10-30 23:45:41 +0000103 label = Label(frame, text='Input From:')
104 label.grid(row=0, column=0, sticky=E)
Barry Warsawe77ec171998-11-01 07:03:55 +0000105 self.__inputvar = IntVar()
Barry Warsaw441abb41998-11-02 22:48:56 +0000106 ##
107 btn = Radiobutton(frame,
108 text='None',
109 variable=self.__inputvar,
110 value=0,
111 command=self.__pushtodev,
112 underline=0)
113 btn.grid(row=0, column=1, sticky=W)
114 root.bind('<Alt-n>', self.__none)
115 root.bind('<Alt-N>', self.__none)
116 if not info.i_avail_ports & MICROPHONE:
117 btn.configure(state=DISABLED)
118 buttons.append(btn)
119 ##
Barry Warsawe77ec171998-11-01 07:03:55 +0000120 btn = Radiobutton(frame,
Barry Warsawb7d1d631998-10-30 23:45:41 +0000121 text='Microphone',
Barry Warsawe77ec171998-11-01 07:03:55 +0000122 variable=self.__inputvar,
123 value=MICROPHONE,
Barry Warsawb7d1d631998-10-30 23:45:41 +0000124 command=self.__pushtodev,
125 underline=0)
Barry Warsaw441abb41998-11-02 22:48:56 +0000126 btn.grid(row=1, column=1, sticky=W)
Barry Warsawb7d1d631998-10-30 23:45:41 +0000127 root.bind('<Alt-m>', self.__mic)
128 root.bind('<Alt-M>', self.__mic)
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000129 if not info.i_avail_ports & MICROPHONE:
130 btn.configure(state=DISABLED)
131 buttons.append(btn)
132 ##
Barry Warsawe77ec171998-11-01 07:03:55 +0000133 btn = Radiobutton(frame,
Barry Warsawb7d1d631998-10-30 23:45:41 +0000134 text='Line In',
Barry Warsawe77ec171998-11-01 07:03:55 +0000135 variable=self.__inputvar,
136 value=LINE_IN,
Barry Warsawb7d1d631998-10-30 23:45:41 +0000137 command=self.__pushtodev,
138 underline=5)
Barry Warsaw441abb41998-11-02 22:48:56 +0000139 btn.grid(row=2, column=1, sticky=W)
Barry Warsawb7d1d631998-10-30 23:45:41 +0000140 root.bind('<Alt-i>', self.__linein)
141 root.bind('<Alt-I>', self.__linein)
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000142 if not info.i_avail_ports & LINE_IN:
143 btn.configure(state=DISABLED)
144 buttons.append(btn)
Barry Warsawd55049a1998-11-01 07:17:24 +0000145 ## if SUNAUDIODEV was built on an older version of Solaris, the CD
146 ## input device won't exist
147 try:
148 btn = Radiobutton(frame,
149 text='CD',
150 variable=self.__inputvar,
151 value=CD,
152 command=self.__pushtodev,
153 underline=0)
Barry Warsaw441abb41998-11-02 22:48:56 +0000154 btn.grid(row=3, column=1, sticky=W)
Barry Warsawd55049a1998-11-01 07:17:24 +0000155 root.bind('<Alt-c>', self.__cd)
156 root.bind('<Alt-C>', self.__cd)
157 if not info.i_avail_ports & CD:
158 btn.configure(state=DISABLED)
159 buttons.append(btn)
160 except NameError:
161 pass
Barry Warsawb7d1d631998-10-30 23:45:41 +0000162 #
163 # where does output go to?
164 frame = Frame(root, bd=1, relief=RAISED)
Barry Warsawf622a331998-12-03 19:32:38 +0000165 frame.grid(row=2, column=0, sticky='NSEW')
Barry Warsawb7d1d631998-10-30 23:45:41 +0000166 label = Label(frame, text='Output To:')
Barry Warsaw2a060841998-10-30 18:21:52 +0000167 label.grid(row=0, column=0, sticky=E)
168 self.__spkvar = IntVar()
Barry Warsawb7d1d631998-10-30 23:45:41 +0000169 btn = Checkbutton(frame,
Barry Warsaw2a060841998-10-30 18:21:52 +0000170 text='Speaker',
171 variable=self.__spkvar,
172 onvalue=SPEAKER,
Barry Warsawb7d1d631998-10-30 23:45:41 +0000173 command=self.__pushtodev,
Barry Warsaw2a060841998-10-30 18:21:52 +0000174 underline=0)
175 btn.grid(row=0, column=1, sticky=W)
176 root.bind('<Alt-s>', self.__speaker)
177 root.bind('<Alt-S>', self.__speaker)
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000178 if not info.o_avail_ports & SPEAKER:
179 btn.configure(state=DISABLED)
180 buttons.append(btn)
181 ##
Barry Warsaw2a060841998-10-30 18:21:52 +0000182 self.__headvar = IntVar()
Barry Warsawb7d1d631998-10-30 23:45:41 +0000183 btn = Checkbutton(frame,
Barry Warsaw2a060841998-10-30 18:21:52 +0000184 text='Headphones',
185 variable=self.__headvar,
186 onvalue=HEADPHONE,
Barry Warsawb7d1d631998-10-30 23:45:41 +0000187 command=self.__pushtodev,
Barry Warsawf622a331998-12-03 19:32:38 +0000188 underline=4)
Barry Warsaw2a060841998-10-30 18:21:52 +0000189 btn.grid(row=1, column=1, sticky=W)
Barry Warsawf622a331998-12-03 19:32:38 +0000190 root.bind('<Alt-p>', self.__headphones)
191 root.bind('<Alt-P>', self.__headphones)
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000192 if not info.o_avail_ports & HEADPHONE:
193 btn.configure(state=DISABLED)
194 buttons.append(btn)
195 ##
Barry Warsaw2a060841998-10-30 18:21:52 +0000196 self.__linevar = IntVar()
Barry Warsawb7d1d631998-10-30 23:45:41 +0000197 btn = Checkbutton(frame,
Barry Warsaw2a060841998-10-30 18:21:52 +0000198 variable=self.__linevar,
199 onvalue=LINE_OUT,
Barry Warsawb7d1d631998-10-30 23:45:41 +0000200 text='Line Out',
201 command=self.__pushtodev,
Barry Warsaw2a060841998-10-30 18:21:52 +0000202 underline=0)
203 btn.grid(row=2, column=1, sticky=W)
204 root.bind('<Alt-l>', self.__lineout)
205 root.bind('<Alt-L>', self.__lineout)
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000206 if not info.o_avail_ports & LINE_OUT:
207 btn.configure(state=DISABLED)
208 buttons.append(btn)
209 #
210 # Fix up widths
211 widest = 0
212 for b in buttons:
213 width = b['width']
214 if width > widest:
215 widest = width
216 for b in buttons:
217 b.configure(width=widest)
Barry Warsawf622a331998-12-03 19:32:38 +0000218 # root bindings
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000219 root.bind('<Alt-q>', self.__quit)
220 root.bind('<Alt-Q>', self.__quit)
Barry Warsawb7d1d631998-10-30 23:45:41 +0000221 #
Barry Warsawa4b055f1999-04-28 19:32:46 +0000222 # Volume
223 frame = Frame(root, bd=1, relief=RAISED)
224 frame.grid(row=3, column=0, sticky='NSEW')
225 label = Label(frame, text='Output Volume:')
226 label.grid(row=0, column=0, sticky=W)
227 self.__scalevar = IntVar()
228 self.__scale = Scale(frame,
229 orient=HORIZONTAL,
230 from_=MIN_GAIN,
231 to=MAX_GAIN,
232 length=200,
233 variable=self.__scalevar,
234 command=self.__volume)
235 self.__scale.grid(row=1, column=0, sticky=EW)
236 #
Barry Warsawb7d1d631998-10-30 23:45:41 +0000237 # do we need to poll for changes?
238 self.__needtopoll = 1
239 try:
240 fd = self.__devctl.fileno()
241 self.__needtopoll = 0
242 except AttributeError:
243 pass
244 else:
Barry Warsawb7d1d631998-10-30 23:45:41 +0000245 import fcntl
246 import signal
Barry Warsawb7d1d631998-10-30 23:45:41 +0000247 import STROPTS
248 # set up the signal handler
249 signal.signal(signal.SIGPOLL, self.__update)
250 fcntl.ioctl(fd, STROPTS.I_SETSIG, STROPTS.S_MSG)
251 self.__update()
252
Barry Warsaw2a060841998-10-30 18:21:52 +0000253 def __quit(self, event=None):
254 self.__devctl.close()
255 self.__root.quit()
256
Barry Warsawf622a331998-12-03 19:32:38 +0000257 def __popup_about(self, event=None):
258 import tkMessageBox
259 tkMessageBox.showinfo('About Audiopy ' + __version__,
260 '''\
261Audiopy %s
262Control the Solaris audio device
263
Barry Warsaw88604051998-12-14 21:36:40 +0000264For information
265Contact: Barry A. Warsaw
266Email: bwarsaw@python.org''' % __version__)
Barry Warsawf622a331998-12-03 19:32:38 +0000267
268 def __popup_using(self, event=None):
269 if not self.__helpwin:
270 self.__helpwin = Helpwin(self.__tkroot, self.__quit)
271 self.__helpwin.deiconify()
272
273
Barry Warsaw2a060841998-10-30 18:21:52 +0000274 def __keepalive(self):
275 # Exercise the Python interpreter regularly so keyboard interrupts get
276 # through.
277 self.__tkroot.tk.createtimerhandler(KEEPALIVE_TIMER, self.__keepalive)
Barry Warsawb7d1d631998-10-30 23:45:41 +0000278 if self.__needtopoll:
279 self.__update()
280
281 def __update(self, num=None, frame=None):
Barry Warsaw772d6961999-08-18 20:17:42 +0000282 # It's possible (although I have never seen it) to get an interrupted
283 # system call during the getinfo() call. If so, and we're polling,
284 # don't sweat it because we'll come around again later. Otherwise,
285 # we'll give it a couple of tries and then give up until next time.
286 tries = 0
287 while 1:
288 try:
289 info = self.__devctl.getinfo()
290 break
291 except sunaudiodev.error:
292 if self.__needtopoll or tries > 3:
293 return
294 tries = tries + 1
Barry Warsawe77ec171998-11-01 07:03:55 +0000295 # input
296 self.__inputvar.set(info.i_port)
297 # output
Barry Warsaw2a060841998-10-30 18:21:52 +0000298 self.__spkvar.set(info.o_port & SPEAKER)
299 self.__headvar.set(info.o_port & HEADPHONE)
300 self.__linevar.set(info.o_port & LINE_OUT)
Barry Warsawa4b055f1999-04-28 19:32:46 +0000301 # volume
302 self.__scalevar.set(info.o_gain)
Barry Warsaw2a060841998-10-30 18:21:52 +0000303
Barry Warsawb7d1d631998-10-30 23:45:41 +0000304 def __pushtodev(self, event=None):
Barry Warsaw2a060841998-10-30 18:21:52 +0000305 info = self.__devctl.getinfo()
306 info.o_port = self.__spkvar.get() + \
307 self.__headvar.get() + \
308 self.__linevar.get()
Barry Warsawe77ec171998-11-01 07:03:55 +0000309 info.i_port = self.__inputvar.get()
Barry Warsawa4b055f1999-04-28 19:32:46 +0000310 info.o_gain = self.__scalevar.get()
Barry Warsawbc9476e2000-03-29 21:05:53 +0000311 try:
312 self.__devctl.setinfo(info)
313 except sunaudiodev.error, msg:
314 # TBD: what to do? it's probably temporary.
315 pass
Barry Warsaw2a060841998-10-30 18:21:52 +0000316
317 def __getset(self, var, onvalue):
Barry Warsaw441abb41998-11-02 22:48:56 +0000318 if var.get() == onvalue:
Barry Warsaw2a060841998-10-30 18:21:52 +0000319 var.set(0)
320 else:
321 var.set(onvalue)
Barry Warsawb7d1d631998-10-30 23:45:41 +0000322 self.__pushtodev()
323
Barry Warsaw441abb41998-11-02 22:48:56 +0000324 def __none(self, event=None):
325 self.__inputvar.set(0)
326 self.__pushtodev()
327
Barry Warsawb7d1d631998-10-30 23:45:41 +0000328 def __mic(self, event=None):
Barry Warsawe77ec171998-11-01 07:03:55 +0000329 self.__getset(self.__inputvar, MICROPHONE)
Barry Warsawb7d1d631998-10-30 23:45:41 +0000330
331 def __linein(self, event=None):
Barry Warsawe77ec171998-11-01 07:03:55 +0000332 self.__getset(self.__inputvar, LINE_IN)
Barry Warsawb7d1d631998-10-30 23:45:41 +0000333
334 def __cd(self, event=None):
Barry Warsawe77ec171998-11-01 07:03:55 +0000335 self.__getset(self.__inputvar, CD)
Barry Warsaw2a060841998-10-30 18:21:52 +0000336
337 def __speaker(self, event=None):
338 self.__getset(self.__spkvar, SPEAKER)
339
340 def __headphones(self, event=None):
341 self.__getset(self.__headvar, HEADPHONE)
342
343 def __lineout(self, event=None):
344 self.__getset(self.__linevar, LINE_OUT)
345
Barry Warsawa4b055f1999-04-28 19:32:46 +0000346 def __volume(self, event=None):
347 self.__pushtodev()
348
Barry Warsaw2a060841998-10-30 18:21:52 +0000349 def start(self):
350 self.__keepalive()
351 self.__tkroot.mainloop()
352
353
354
Barry Warsawf622a331998-12-03 19:32:38 +0000355class Helpwin:
356 def __init__(self, master, quitfunc):
357 from Tkinter import *
358 self.__root = root = Toplevel(master, class_='Audiopy')
359 root.protocol('WM_DELETE_WINDOW', self.__withdraw)
360 root.title('Audiopy Help Window')
361 root.iconname('Audiopy Help Window')
362 root.bind('<Alt-q>', quitfunc)
363 root.bind('<Alt-Q>', quitfunc)
364 root.bind('<Alt-w>', self.__withdraw)
365 root.bind('<Alt-W>', self.__withdraw)
366
367 # more elaborate help is available in the README file
368 readmefile = os.path.join(sys.path[0], 'README')
369 try:
370 fp = None
371 try:
372 fp = open(readmefile)
373 contents = fp.read()
374 # wax the last page, it contains Emacs cruft
375 i = string.rfind(contents, '\f')
376 if i > 0:
377 contents = string.rstrip(contents[:i])
378 finally:
379 if fp:
380 fp.close()
381 except IOError:
382 sys.stderr.write("Couldn't open audiopy's README, "
383 'using docstring instead.\n')
384 contents = __doc__ % globals()
385
386 self.__text = text = Text(root, relief=SUNKEN,
387 width=80, height=24)
388 text.insert(0.0, contents)
389 scrollbar = Scrollbar(root)
390 scrollbar.pack(fill=Y, side=RIGHT)
391 text.pack(fill=BOTH, expand=YES)
392 text.configure(yscrollcommand=(scrollbar, 'set'))
393 scrollbar.configure(command=(text, 'yview'))
394
395 def __withdraw(self, event=None):
396 self.__root.withdraw()
397
398 def deiconify(self):
399 self.__root.deiconify()
400
401
402
403
Barry Warsawa4b055f1999-04-28 19:32:46 +0000404def usage(code, msg=''):
Barry Warsawd81d5341998-11-18 03:28:45 +0000405 print __doc__ % globals()
Barry Warsaw2858b431998-11-18 03:32:43 +0000406 if msg:
407 print msg
Barry Warsaw2a060841998-10-30 18:21:52 +0000408 sys.exit(code)
409
410
411def main():
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000412 #
413 # Open up the audio control device and query for the current output
414 # device
415 device = sunaudiodev.open('control')
416
Barry Warsaw2a060841998-10-30 18:21:52 +0000417 if len(sys.argv) == 1:
418 # GUI
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000419 w = MainWindow(device)
Barry Warsawb7d1d631998-10-30 23:45:41 +0000420 try:
421 w.start()
422 except KeyboardInterrupt:
423 pass
Barry Warsaw2a060841998-10-30 18:21:52 +0000424 return
425
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000426 # spec: LONG OPT, SHORT OPT, 0=input,1=output, MASK
Barry Warsawd55049a1998-11-01 07:17:24 +0000427 options = [('--microphone', '-m', 0, MICROPHONE),
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000428 ('--linein', '-i', 0, LINE_IN),
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000429 ('--headphones', '-p', 1, HEADPHONE),
430 ('--speaker', '-s', 1, SPEAKER),
431 ('--lineout', '-o', 1, LINE_OUT),
Barry Warsawd55049a1998-11-01 07:17:24 +0000432 ]
433 # See the comment above about `CD'
434 try:
435 options.append(('--cd', '-c', 0, CD))
436 except NameError:
437 pass
438
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000439 info = device.getinfo()
440 # first get the existing values
Barry Warsawa4b055f1999-04-28 19:32:46 +0000441 i = 0
442 while i < len(sys.argv)-1:
443 i = i + 1
444 arg = sys.argv[i]
Barry Warsaw2a060841998-10-30 18:21:52 +0000445 if arg in ('-h', '--help'):
Barry Warsawa4b055f1999-04-28 19:32:46 +0000446 usage(0)
Barry Warsawe77ec171998-11-01 07:03:55 +0000447 # does not return
Barry Warsawa4b055f1999-04-28 19:32:46 +0000448 elif arg in ('-g', '--gain'):
449 gainspec = '<missing>'
450 try:
451 gainspec = sys.argv[i+1]
452 gain = int(gainspec)
453 except (ValueError, IndexError):
454 usage(1, 'Bad gain specification: ' + gainspec)
455 info.o_gain = gain
456 i = i + 1
457 continue
Barry Warsaw3febc241998-11-01 07:10:51 +0000458 elif arg in ('-v', '--version'):
Barry Warsawd81d5341998-11-18 03:28:45 +0000459 print '''\
460audiopy -- a program to control the Solaris audio device.
Barry Warsaw88604051998-12-14 21:36:40 +0000461Contact: Barry Warsaw
Barry Warsawd81d5341998-11-18 03:28:45 +0000462Email: bwarsaw@python.org
463Version: %s''' % __version__
Barry Warsaw3febc241998-11-01 07:10:51 +0000464 sys.exit(0)
Barry Warsawe77ec171998-11-01 07:03:55 +0000465 for long, short, io, mask in options:
466 if arg in (long, short):
467 # toggle the option
468 if io == 0:
469 info.i_port = info.i_port ^ mask
470 else:
471 info.o_port = info.o_port ^ mask
472 break
473 val = None
Barry Warsaw2a060841998-10-30 18:21:52 +0000474 try:
Barry Warsawe77ec171998-11-01 07:03:55 +0000475 if arg[:len(long)+1] == long+'=':
476 val = int(arg[len(long)+1:])
477 elif arg[:len(short)+1] == short+'=':
478 val = int(arg[len(short)+1:])
Barry Warsaw2a060841998-10-30 18:21:52 +0000479 except ValueError:
Barry Warsawa4b055f1999-04-28 19:32:46 +0000480 usage(1, msg='Invalid option: ' + arg)
Barry Warsawe77ec171998-11-01 07:03:55 +0000481 # does not return
482 if val == 0:
483 if io == 0:
484 info.i_port = info.i_port & ~mask
485 else:
486 info.o_port = info.o_port & ~mask
487 break
488 elif val == 1:
489 if io == 0:
490 info.i_port = info.i_port | mask
491 else:
492 info.o_port = info.o_port | mask
493 break
494 # else keep trying next option
Barry Warsaw2a060841998-10-30 18:21:52 +0000495 else:
Barry Warsawa4b055f1999-04-28 19:32:46 +0000496 usage(1, msg='Invalid option: ' + arg)
Barry Warsaw2a060841998-10-30 18:21:52 +0000497 # now set the values
Barry Warsawd8d179d1999-09-22 15:45:51 +0000498 try:
499 device.setinfo(info)
500 except sunaudiodev.error, (code, msg):
Barry Warsawc4eb6a61999-10-07 20:00:01 +0000501 if code <> errno.EINVAL:
502 raise
Barry Warsawe77ec171998-11-01 07:03:55 +0000503 device.close()
Barry Warsaw2a060841998-10-30 18:21:52 +0000504
505
506
507if __name__ == '__main__':
508 main()