Add a scale widget and command line options to set the output volume.

Bump version number 1.0
diff --git a/Tools/audiopy/README b/Tools/audiopy/README
index a49c75e..1cf3d56 100644
--- a/Tools/audiopy/README
+++ b/Tools/audiopy/README
@@ -6,10 +6,10 @@
 
 Introduction
 
-    Audiopy is a program to control the Solaris audio device, allowing 
-    you to set both the input and output devices.  It can be run
-    either as a standalone command-line script, or as a Tkinter based
-    GUI application.
+    Audiopy is a program to control the Solaris audio device, allowing
+    you to choose both the input and output devices, and to set the
+    output volume.  It can be run either as a standalone command-line
+    script, or as a Tkinter based GUI application.
 
     Note that your version of Python must have been built with the
     sunaudiodev module enabled.  It is not enabled by default however!
@@ -26,6 +26,8 @@
     devices: the headphone jack, the speakers, or the line-out jack.
     You can enable any combination of these three devices.
 
+    You can also set the output gain (volume) level.
+
 Running as a GUI
 
     Simply start audiopy with no arguments to start it as a Tkinter
@@ -43,11 +45,11 @@
     Alt-q is also an accelerator for selecting Quit from the File
     menu.
 
-    No unsupported devices will appear in the GUI.  When run as a GUI, 
-    audiopy monitors the audio device and automatically updates its
-    display if the state of the device is changed by some other
-    means.  In pre-Python 1.5.2 this is done by occasionally polling
-    the device, but in Python 1.5.2 no polling is necessary (you don't 
+    Unsupported devices will appear dimmed out in the GUI.  When run
+    as a GUI, audiopy monitors the audio device and automatically
+    updates its display if the state of the device is changed by some
+    other means.  In Python 1.5.2 this is done by occasionally polling
+    the device, but in Python 1.5.2 no polling is necessary (you don't
     really need to know this, but I thought I'd plug 1.5.2 :-).
     
 Running as a Command Line Program
@@ -85,7 +87,13 @@
 
     % ./audiopy -s=0 -p=1 -c
 
-    Audiopy understands two other command line options:
+    Audiopy understands these other command line options:
+
+    --gain volume
+    -g volume
+        Sets the output volume to the specified gain level.  This must 
+        be an integer between MIN_GAIN and MAX_GAIN (usually [0..255], 
+        but use the -h option to find the exact values).
 
     --version
     -v
diff --git a/Tools/audiopy/audiopy b/Tools/audiopy/audiopy
index 12994ab..1d6a641 100755
--- a/Tools/audiopy/audiopy
+++ b/Tools/audiopy/audiopy
@@ -7,10 +7,10 @@
 Version: %(__version__)s
 
 When no arguments are given, this pops up a graphical window which lets you
-choose the audio input and output devices.
+choose the audio input and output devices, and set the output volume.
 
 This program can be driven via the command line, and when done so, no window
-pops up.  Options have the general form:
+pops up.  Most options have the general form:
 
     --device[={0,1}]
     -d[={0,1}]
@@ -31,6 +31,11 @@
 
 Other options are:
 
+    --gain volume
+    -g volume
+        Sets the output gain to the specified volume, which must be an integer
+        in the range [%(MIN_GAIN)s..%(MAX_GAIN)s]
+
     --version
     -v
         Print the version number and exit.
@@ -49,7 +54,7 @@
 # Milliseconds between interrupt checks
 KEEPALIVE_TIMER = 500
 
-__version__ = '0.1'
+__version__ = '1.0'
 
 
 
@@ -213,6 +218,21 @@
         root.bind('<Alt-q>', self.__quit)
         root.bind('<Alt-Q>', self.__quit)
         #
+        # Volume
+        frame = Frame(root, bd=1, relief=RAISED)
+        frame.grid(row=3, column=0, sticky='NSEW')
+        label = Label(frame, text='Output Volume:')
+        label.grid(row=0, column=0, sticky=W)
+        self.__scalevar = IntVar()
+        self.__scale = Scale(frame,
+                             orient=HORIZONTAL,
+                             from_=MIN_GAIN,
+                             to=MAX_GAIN,
+                             length=200,
+                             variable=self.__scalevar,
+                             command=self.__volume)
+        self.__scale.grid(row=1, column=0, sticky=EW)
+        #
         # do we need to poll for changes?
         self.__needtopoll = 1
         try:
@@ -268,6 +288,8 @@
         self.__spkvar.set(info.o_port & SPEAKER)
         self.__headvar.set(info.o_port & HEADPHONE)
         self.__linevar.set(info.o_port & LINE_OUT)
+        # volume
+        self.__scalevar.set(info.o_gain)
 
     def __pushtodev(self, event=None):
         info = self.__devctl.getinfo()
@@ -275,6 +297,7 @@
                       self.__headvar.get() + \
                       self.__linevar.get()
         info.i_port = self.__inputvar.get()
+        info.o_gain = self.__scalevar.get()
         self.__devctl.setinfo(info)
 
     def __getset(self, var, onvalue):
@@ -306,6 +329,9 @@
     def __lineout(self, event=None):
         self.__getset(self.__linevar, LINE_OUT)
 
+    def __volume(self, event=None):
+        self.__pushtodev()
+
     def start(self):
         self.__keepalive()
         self.__tkroot.mainloop()
@@ -361,7 +387,7 @@
 
 
 
-def usage(msg='', code=1):
+def usage(code, msg=''):
     print __doc__ % globals()
     if msg:
         print msg
@@ -398,10 +424,23 @@
 
     info = device.getinfo()
     # first get the existing values
-    for arg in sys.argv[1:]:
+    i = 0
+    while i < len(sys.argv)-1:
+        i = i + 1
+        arg = sys.argv[i]
         if arg in ('-h', '--help'):
-            usage(code=0)
+            usage(0)
             # does not return
+        elif arg in ('-g', '--gain'):
+            gainspec = '<missing>'
+            try:
+                gainspec = sys.argv[i+1]
+                gain = int(gainspec)
+            except (ValueError, IndexError):
+                usage(1, 'Bad gain specification: ' + gainspec)
+            info.o_gain = gain
+            i = i + 1
+            continue
         elif arg in ('-v', '--version'):
             print '''\
 audiopy -- a program to control the Solaris audio device.
@@ -424,7 +463,7 @@
                 elif arg[:len(short)+1] == short+'=':
                     val = int(arg[len(short)+1:])
             except ValueError:
-                usage(msg='Invalid option: ' + arg)
+                usage(1, msg='Invalid option: ' + arg)
                 # does not return
             if val == 0:
                 if io == 0:
@@ -440,7 +479,7 @@
                 break
             # else keep trying next option
         else:
-            usage(msg='Invalid option: ' + arg)
+            usage(1, msg='Invalid option: ' + arg)
     # now set the values
     device.setinfo(info)
     device.close()