blob: b817c5cc05fca7c53652cd50e01e5c2ad7d8f3e7 [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
Barry Warsawd8d179d1999-09-22 15:45:51 +000050import errno
Barry Warsaw2a060841998-10-30 18:21:52 +000051import sunaudiodev
52from SUNAUDIODEV import *
53
54# Milliseconds between interrupt checks
55KEEPALIVE_TIMER = 500
56
Barry Warsaw772d6961999-08-18 20:17:42 +000057__version__ = '1.1'
Barry Warsaw3febc241998-11-01 07:10:51 +000058
Barry Warsaw2a060841998-10-30 18:21:52 +000059
60
61class MainWindow:
Barry Warsaw2c8b35b1998-10-31 00:25:14 +000062 def __init__(self, device):
Barry Warsaw9ade9dd1998-11-01 07:13:14 +000063 from Tkinter import *
Barry Warsawf622a331998-12-03 19:32:38 +000064 self.__helpwin = None
Barry Warsaw2c8b35b1998-10-31 00:25:14 +000065 self.__devctl = device
66 info = device.getinfo()
67 #
Barry Warsaw3febc241998-11-01 07:10:51 +000068 self.__tkroot = tkroot = Tk(className='Audiopy')
Barry Warsaw2a060841998-10-30 18:21:52 +000069 tkroot.withdraw()
Barry Warsawf622a331998-12-03 19:32:38 +000070 # create the menubar
71 menubar = Menu(tkroot)
72 filemenu = Menu(menubar, tearoff=0)
73 filemenu.add_command(label='Quit',
74 command=self.__quit,
75 accelerator='Alt-Q',
76 underline=0)
77 helpmenu = Menu(menubar, name='help', tearoff=0)
78 helpmenu.add_command(label='About Audiopy...',
79 command=self.__popup_about,
80 underline=0)
81 helpmenu.add_command(label='Help...',
82 command=self.__popup_using,
83 underline=0)
84 menubar.add_cascade(label='File',
85 menu=filemenu,
86 underline=0)
87 menubar.add_cascade(label='Help',
88 menu=helpmenu,
89 underline=0)
Barry Warsaw2a060841998-10-30 18:21:52 +000090 # now create the top level window
Barry Warsawf622a331998-12-03 19:32:38 +000091 root = self.__root = Toplevel(tkroot, class_='Audiopy', menu=menubar)
Barry Warsaw2a060841998-10-30 18:21:52 +000092 root.protocol('WM_DELETE_WINDOW', self.__quit)
Barry Warsaw3febc241998-11-01 07:10:51 +000093 root.title('audiopy ' + __version__)
94 root.iconname('audiopy ' + __version__)
Barry Warsaw2a060841998-10-30 18:21:52 +000095 root.tk.createtimerhandler(KEEPALIVE_TIMER, self.__keepalive)
Barry Warsaw2a060841998-10-30 18:21:52 +000096 #
Barry Warsaw2c8b35b1998-10-31 00:25:14 +000097 buttons = []
Barry Warsaw2a060841998-10-30 18:21:52 +000098 #
Barry Warsawb7d1d631998-10-30 23:45:41 +000099 # where does input come from?
100 frame = Frame(root, bd=1, relief=RAISED)
Barry Warsawf622a331998-12-03 19:32:38 +0000101 frame.grid(row=1, column=0, sticky='NSEW')
Barry Warsawb7d1d631998-10-30 23:45:41 +0000102 label = Label(frame, text='Input From:')
103 label.grid(row=0, column=0, sticky=E)
Barry Warsawe77ec171998-11-01 07:03:55 +0000104 self.__inputvar = IntVar()
Barry Warsaw441abb41998-11-02 22:48:56 +0000105 ##
106 btn = Radiobutton(frame,
107 text='None',
108 variable=self.__inputvar,
109 value=0,
110 command=self.__pushtodev,
111 underline=0)
112 btn.grid(row=0, column=1, sticky=W)
113 root.bind('<Alt-n>', self.__none)
114 root.bind('<Alt-N>', self.__none)
115 if not info.i_avail_ports & MICROPHONE:
116 btn.configure(state=DISABLED)
117 buttons.append(btn)
118 ##
Barry Warsawe77ec171998-11-01 07:03:55 +0000119 btn = Radiobutton(frame,
Barry Warsawb7d1d631998-10-30 23:45:41 +0000120 text='Microphone',
Barry Warsawe77ec171998-11-01 07:03:55 +0000121 variable=self.__inputvar,
122 value=MICROPHONE,
Barry Warsawb7d1d631998-10-30 23:45:41 +0000123 command=self.__pushtodev,
124 underline=0)
Barry Warsaw441abb41998-11-02 22:48:56 +0000125 btn.grid(row=1, column=1, sticky=W)
Barry Warsawb7d1d631998-10-30 23:45:41 +0000126 root.bind('<Alt-m>', self.__mic)
127 root.bind('<Alt-M>', self.__mic)
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000128 if not info.i_avail_ports & MICROPHONE:
129 btn.configure(state=DISABLED)
130 buttons.append(btn)
131 ##
Barry Warsawe77ec171998-11-01 07:03:55 +0000132 btn = Radiobutton(frame,
Barry Warsawb7d1d631998-10-30 23:45:41 +0000133 text='Line In',
Barry Warsawe77ec171998-11-01 07:03:55 +0000134 variable=self.__inputvar,
135 value=LINE_IN,
Barry Warsawb7d1d631998-10-30 23:45:41 +0000136 command=self.__pushtodev,
137 underline=5)
Barry Warsaw441abb41998-11-02 22:48:56 +0000138 btn.grid(row=2, column=1, sticky=W)
Barry Warsawb7d1d631998-10-30 23:45:41 +0000139 root.bind('<Alt-i>', self.__linein)
140 root.bind('<Alt-I>', self.__linein)
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000141 if not info.i_avail_ports & LINE_IN:
142 btn.configure(state=DISABLED)
143 buttons.append(btn)
Barry Warsawd55049a1998-11-01 07:17:24 +0000144 ## if SUNAUDIODEV was built on an older version of Solaris, the CD
145 ## input device won't exist
146 try:
147 btn = Radiobutton(frame,
148 text='CD',
149 variable=self.__inputvar,
150 value=CD,
151 command=self.__pushtodev,
152 underline=0)
Barry Warsaw441abb41998-11-02 22:48:56 +0000153 btn.grid(row=3, column=1, sticky=W)
Barry Warsawd55049a1998-11-01 07:17:24 +0000154 root.bind('<Alt-c>', self.__cd)
155 root.bind('<Alt-C>', self.__cd)
156 if not info.i_avail_ports & CD:
157 btn.configure(state=DISABLED)
158 buttons.append(btn)
159 except NameError:
160 pass
Barry Warsawb7d1d631998-10-30 23:45:41 +0000161 #
162 # where does output go to?
163 frame = Frame(root, bd=1, relief=RAISED)
Barry Warsawf622a331998-12-03 19:32:38 +0000164 frame.grid(row=2, column=0, sticky='NSEW')
Barry Warsawb7d1d631998-10-30 23:45:41 +0000165 label = Label(frame, text='Output To:')
Barry Warsaw2a060841998-10-30 18:21:52 +0000166 label.grid(row=0, column=0, sticky=E)
167 self.__spkvar = IntVar()
Barry Warsawb7d1d631998-10-30 23:45:41 +0000168 btn = Checkbutton(frame,
Barry Warsaw2a060841998-10-30 18:21:52 +0000169 text='Speaker',
170 variable=self.__spkvar,
171 onvalue=SPEAKER,
Barry Warsawb7d1d631998-10-30 23:45:41 +0000172 command=self.__pushtodev,
Barry Warsaw2a060841998-10-30 18:21:52 +0000173 underline=0)
174 btn.grid(row=0, column=1, sticky=W)
175 root.bind('<Alt-s>', self.__speaker)
176 root.bind('<Alt-S>', self.__speaker)
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000177 if not info.o_avail_ports & SPEAKER:
178 btn.configure(state=DISABLED)
179 buttons.append(btn)
180 ##
Barry Warsaw2a060841998-10-30 18:21:52 +0000181 self.__headvar = IntVar()
Barry Warsawb7d1d631998-10-30 23:45:41 +0000182 btn = Checkbutton(frame,
Barry Warsaw2a060841998-10-30 18:21:52 +0000183 text='Headphones',
184 variable=self.__headvar,
185 onvalue=HEADPHONE,
Barry Warsawb7d1d631998-10-30 23:45:41 +0000186 command=self.__pushtodev,
Barry Warsawf622a331998-12-03 19:32:38 +0000187 underline=4)
Barry Warsaw2a060841998-10-30 18:21:52 +0000188 btn.grid(row=1, column=1, sticky=W)
Barry Warsawf622a331998-12-03 19:32:38 +0000189 root.bind('<Alt-p>', self.__headphones)
190 root.bind('<Alt-P>', self.__headphones)
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000191 if not info.o_avail_ports & HEADPHONE:
192 btn.configure(state=DISABLED)
193 buttons.append(btn)
194 ##
Barry Warsaw2a060841998-10-30 18:21:52 +0000195 self.__linevar = IntVar()
Barry Warsawb7d1d631998-10-30 23:45:41 +0000196 btn = Checkbutton(frame,
Barry Warsaw2a060841998-10-30 18:21:52 +0000197 variable=self.__linevar,
198 onvalue=LINE_OUT,
Barry Warsawb7d1d631998-10-30 23:45:41 +0000199 text='Line Out',
200 command=self.__pushtodev,
Barry Warsaw2a060841998-10-30 18:21:52 +0000201 underline=0)
202 btn.grid(row=2, column=1, sticky=W)
203 root.bind('<Alt-l>', self.__lineout)
204 root.bind('<Alt-L>', self.__lineout)
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000205 if not info.o_avail_ports & LINE_OUT:
206 btn.configure(state=DISABLED)
207 buttons.append(btn)
208 #
209 # Fix up widths
210 widest = 0
211 for b in buttons:
212 width = b['width']
213 if width > widest:
214 widest = width
215 for b in buttons:
216 b.configure(width=widest)
Barry Warsawf622a331998-12-03 19:32:38 +0000217 # root bindings
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000218 root.bind('<Alt-q>', self.__quit)
219 root.bind('<Alt-Q>', self.__quit)
Barry Warsawb7d1d631998-10-30 23:45:41 +0000220 #
Barry Warsawa4b055f1999-04-28 19:32:46 +0000221 # Volume
222 frame = Frame(root, bd=1, relief=RAISED)
223 frame.grid(row=3, column=0, sticky='NSEW')
224 label = Label(frame, text='Output Volume:')
225 label.grid(row=0, column=0, sticky=W)
226 self.__scalevar = IntVar()
227 self.__scale = Scale(frame,
228 orient=HORIZONTAL,
229 from_=MIN_GAIN,
230 to=MAX_GAIN,
231 length=200,
232 variable=self.__scalevar,
233 command=self.__volume)
234 self.__scale.grid(row=1, column=0, sticky=EW)
235 #
Barry Warsawb7d1d631998-10-30 23:45:41 +0000236 # do we need to poll for changes?
237 self.__needtopoll = 1
238 try:
239 fd = self.__devctl.fileno()
240 self.__needtopoll = 0
241 except AttributeError:
242 pass
243 else:
Barry Warsawb7d1d631998-10-30 23:45:41 +0000244 import fcntl
245 import signal
Barry Warsawb7d1d631998-10-30 23:45:41 +0000246 import STROPTS
247 # set up the signal handler
248 signal.signal(signal.SIGPOLL, self.__update)
249 fcntl.ioctl(fd, STROPTS.I_SETSIG, STROPTS.S_MSG)
250 self.__update()
251
Barry Warsaw2a060841998-10-30 18:21:52 +0000252 def __quit(self, event=None):
253 self.__devctl.close()
254 self.__root.quit()
255
Barry Warsawf622a331998-12-03 19:32:38 +0000256 def __popup_about(self, event=None):
257 import tkMessageBox
258 tkMessageBox.showinfo('About Audiopy ' + __version__,
259 '''\
260Audiopy %s
261Control the Solaris audio device
262
Barry Warsaw88604051998-12-14 21:36:40 +0000263For information
264Contact: Barry A. Warsaw
265Email: bwarsaw@python.org''' % __version__)
Barry Warsawf622a331998-12-03 19:32:38 +0000266
267 def __popup_using(self, event=None):
268 if not self.__helpwin:
269 self.__helpwin = Helpwin(self.__tkroot, self.__quit)
270 self.__helpwin.deiconify()
271
272
Barry Warsaw2a060841998-10-30 18:21:52 +0000273 def __keepalive(self):
274 # Exercise the Python interpreter regularly so keyboard interrupts get
275 # through.
276 self.__tkroot.tk.createtimerhandler(KEEPALIVE_TIMER, self.__keepalive)
Barry Warsawb7d1d631998-10-30 23:45:41 +0000277 if self.__needtopoll:
278 self.__update()
279
280 def __update(self, num=None, frame=None):
Barry Warsaw772d6961999-08-18 20:17:42 +0000281 # It's possible (although I have never seen it) to get an interrupted
282 # system call during the getinfo() call. If so, and we're polling,
283 # don't sweat it because we'll come around again later. Otherwise,
284 # we'll give it a couple of tries and then give up until next time.
285 tries = 0
286 while 1:
287 try:
288 info = self.__devctl.getinfo()
289 break
290 except sunaudiodev.error:
291 if self.__needtopoll or tries > 3:
292 return
293 tries = tries + 1
Barry Warsawe77ec171998-11-01 07:03:55 +0000294 # input
295 self.__inputvar.set(info.i_port)
296 # output
Barry Warsaw2a060841998-10-30 18:21:52 +0000297 self.__spkvar.set(info.o_port & SPEAKER)
298 self.__headvar.set(info.o_port & HEADPHONE)
299 self.__linevar.set(info.o_port & LINE_OUT)
Barry Warsawa4b055f1999-04-28 19:32:46 +0000300 # volume
301 self.__scalevar.set(info.o_gain)
Barry Warsaw2a060841998-10-30 18:21:52 +0000302
Barry Warsawb7d1d631998-10-30 23:45:41 +0000303 def __pushtodev(self, event=None):
Barry Warsaw2a060841998-10-30 18:21:52 +0000304 info = self.__devctl.getinfo()
305 info.o_port = self.__spkvar.get() + \
306 self.__headvar.get() + \
307 self.__linevar.get()
Barry Warsawe77ec171998-11-01 07:03:55 +0000308 info.i_port = self.__inputvar.get()
Barry Warsawa4b055f1999-04-28 19:32:46 +0000309 info.o_gain = self.__scalevar.get()
Barry Warsawbc9476e2000-03-29 21:05:53 +0000310 try:
311 self.__devctl.setinfo(info)
312 except sunaudiodev.error, msg:
313 # TBD: what to do? it's probably temporary.
314 pass
Barry Warsaw2a060841998-10-30 18:21:52 +0000315
316 def __getset(self, var, onvalue):
Barry Warsaw441abb41998-11-02 22:48:56 +0000317 if var.get() == onvalue:
Barry Warsaw2a060841998-10-30 18:21:52 +0000318 var.set(0)
319 else:
320 var.set(onvalue)
Barry Warsawb7d1d631998-10-30 23:45:41 +0000321 self.__pushtodev()
322
Barry Warsaw441abb41998-11-02 22:48:56 +0000323 def __none(self, event=None):
324 self.__inputvar.set(0)
325 self.__pushtodev()
326
Barry Warsawb7d1d631998-10-30 23:45:41 +0000327 def __mic(self, event=None):
Barry Warsawe77ec171998-11-01 07:03:55 +0000328 self.__getset(self.__inputvar, MICROPHONE)
Barry Warsawb7d1d631998-10-30 23:45:41 +0000329
330 def __linein(self, event=None):
Barry Warsawe77ec171998-11-01 07:03:55 +0000331 self.__getset(self.__inputvar, LINE_IN)
Barry Warsawb7d1d631998-10-30 23:45:41 +0000332
333 def __cd(self, event=None):
Barry Warsawe77ec171998-11-01 07:03:55 +0000334 self.__getset(self.__inputvar, CD)
Barry Warsaw2a060841998-10-30 18:21:52 +0000335
336 def __speaker(self, event=None):
337 self.__getset(self.__spkvar, SPEAKER)
338
339 def __headphones(self, event=None):
340 self.__getset(self.__headvar, HEADPHONE)
341
342 def __lineout(self, event=None):
343 self.__getset(self.__linevar, LINE_OUT)
344
Barry Warsawa4b055f1999-04-28 19:32:46 +0000345 def __volume(self, event=None):
346 self.__pushtodev()
347
Barry Warsaw2a060841998-10-30 18:21:52 +0000348 def start(self):
349 self.__keepalive()
350 self.__tkroot.mainloop()
351
352
353
Barry Warsawf622a331998-12-03 19:32:38 +0000354class Helpwin:
355 def __init__(self, master, quitfunc):
356 from Tkinter import *
357 self.__root = root = Toplevel(master, class_='Audiopy')
358 root.protocol('WM_DELETE_WINDOW', self.__withdraw)
359 root.title('Audiopy Help Window')
360 root.iconname('Audiopy Help Window')
361 root.bind('<Alt-q>', quitfunc)
362 root.bind('<Alt-Q>', quitfunc)
363 root.bind('<Alt-w>', self.__withdraw)
364 root.bind('<Alt-W>', self.__withdraw)
365
366 # more elaborate help is available in the README file
367 readmefile = os.path.join(sys.path[0], 'README')
368 try:
369 fp = None
370 try:
371 fp = open(readmefile)
372 contents = fp.read()
373 # wax the last page, it contains Emacs cruft
Walter Dörwaldaaab30e2002-09-11 20:36:02 +0000374 i = contents.rfind('\f')
Barry Warsawf622a331998-12-03 19:32:38 +0000375 if i > 0:
Walter Dörwaldaaab30e2002-09-11 20:36:02 +0000376 contents = contents[:i].rstrip()
Barry Warsawf622a331998-12-03 19:32:38 +0000377 finally:
378 if fp:
379 fp.close()
380 except IOError:
381 sys.stderr.write("Couldn't open audiopy's README, "
382 'using docstring instead.\n')
383 contents = __doc__ % globals()
384
385 self.__text = text = Text(root, relief=SUNKEN,
386 width=80, height=24)
387 text.insert(0.0, contents)
388 scrollbar = Scrollbar(root)
389 scrollbar.pack(fill=Y, side=RIGHT)
390 text.pack(fill=BOTH, expand=YES)
391 text.configure(yscrollcommand=(scrollbar, 'set'))
392 scrollbar.configure(command=(text, 'yview'))
393
394 def __withdraw(self, event=None):
395 self.__root.withdraw()
396
397 def deiconify(self):
398 self.__root.deiconify()
399
400
401
402
Barry Warsawa4b055f1999-04-28 19:32:46 +0000403def usage(code, msg=''):
Barry Warsawd81d5341998-11-18 03:28:45 +0000404 print __doc__ % globals()
Barry Warsaw2858b431998-11-18 03:32:43 +0000405 if msg:
406 print msg
Barry Warsaw2a060841998-10-30 18:21:52 +0000407 sys.exit(code)
408
409
410def main():
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000411 #
412 # Open up the audio control device and query for the current output
413 # device
414 device = sunaudiodev.open('control')
415
Barry Warsaw2a060841998-10-30 18:21:52 +0000416 if len(sys.argv) == 1:
417 # GUI
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000418 w = MainWindow(device)
Barry Warsawb7d1d631998-10-30 23:45:41 +0000419 try:
420 w.start()
421 except KeyboardInterrupt:
422 pass
Barry Warsaw2a060841998-10-30 18:21:52 +0000423 return
424
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000425 # spec: LONG OPT, SHORT OPT, 0=input,1=output, MASK
Barry Warsawd55049a1998-11-01 07:17:24 +0000426 options = [('--microphone', '-m', 0, MICROPHONE),
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000427 ('--linein', '-i', 0, LINE_IN),
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000428 ('--headphones', '-p', 1, HEADPHONE),
429 ('--speaker', '-s', 1, SPEAKER),
430 ('--lineout', '-o', 1, LINE_OUT),
Barry Warsawd55049a1998-11-01 07:17:24 +0000431 ]
432 # See the comment above about `CD'
433 try:
434 options.append(('--cd', '-c', 0, CD))
435 except NameError:
436 pass
437
Barry Warsaw2c8b35b1998-10-31 00:25:14 +0000438 info = device.getinfo()
439 # first get the existing values
Barry Warsawa4b055f1999-04-28 19:32:46 +0000440 i = 0
441 while i < len(sys.argv)-1:
442 i = i + 1
443 arg = sys.argv[i]
Barry Warsaw2a060841998-10-30 18:21:52 +0000444 if arg in ('-h', '--help'):
Barry Warsawa4b055f1999-04-28 19:32:46 +0000445 usage(0)
Barry Warsawe77ec171998-11-01 07:03:55 +0000446 # does not return
Barry Warsawa4b055f1999-04-28 19:32:46 +0000447 elif arg in ('-g', '--gain'):
448 gainspec = '<missing>'
449 try:
450 gainspec = sys.argv[i+1]
451 gain = int(gainspec)
452 except (ValueError, IndexError):
453 usage(1, 'Bad gain specification: ' + gainspec)
454 info.o_gain = gain
455 i = i + 1
456 continue
Barry Warsaw3febc241998-11-01 07:10:51 +0000457 elif arg in ('-v', '--version'):
Barry Warsawd81d5341998-11-18 03:28:45 +0000458 print '''\
459audiopy -- a program to control the Solaris audio device.
Barry Warsaw88604051998-12-14 21:36:40 +0000460Contact: Barry Warsaw
Barry Warsawd81d5341998-11-18 03:28:45 +0000461Email: bwarsaw@python.org
462Version: %s''' % __version__
Barry Warsaw3febc241998-11-01 07:10:51 +0000463 sys.exit(0)
Barry Warsawe77ec171998-11-01 07:03:55 +0000464 for long, short, io, mask in options:
465 if arg in (long, short):
466 # toggle the option
467 if io == 0:
468 info.i_port = info.i_port ^ mask
469 else:
470 info.o_port = info.o_port ^ mask
471 break
472 val = None
Barry Warsaw2a060841998-10-30 18:21:52 +0000473 try:
Barry Warsawe77ec171998-11-01 07:03:55 +0000474 if arg[:len(long)+1] == long+'=':
475 val = int(arg[len(long)+1:])
476 elif arg[:len(short)+1] == short+'=':
477 val = int(arg[len(short)+1:])
Barry Warsaw2a060841998-10-30 18:21:52 +0000478 except ValueError:
Barry Warsawa4b055f1999-04-28 19:32:46 +0000479 usage(1, msg='Invalid option: ' + arg)
Barry Warsawe77ec171998-11-01 07:03:55 +0000480 # does not return
481 if val == 0:
482 if io == 0:
483 info.i_port = info.i_port & ~mask
484 else:
485 info.o_port = info.o_port & ~mask
486 break
487 elif val == 1:
488 if io == 0:
489 info.i_port = info.i_port | mask
490 else:
491 info.o_port = info.o_port | mask
492 break
493 # else keep trying next option
Barry Warsaw2a060841998-10-30 18:21:52 +0000494 else:
Barry Warsawa4b055f1999-04-28 19:32:46 +0000495 usage(1, msg='Invalid option: ' + arg)
Barry Warsaw2a060841998-10-30 18:21:52 +0000496 # now set the values
Barry Warsawd8d179d1999-09-22 15:45:51 +0000497 try:
498 device.setinfo(info)
499 except sunaudiodev.error, (code, msg):
Barry Warsawc4eb6a61999-10-07 20:00:01 +0000500 if code <> errno.EINVAL:
501 raise
Barry Warsawe77ec171998-11-01 07:03:55 +0000502 device.close()
Barry Warsaw2a060841998-10-30 18:21:52 +0000503
504
505
506if __name__ == '__main__':
507 main()