|
Server : Apache/2.2.2 (Fedora) System : Linux App1.pathumtani.go.th 2.6.20-1.2320.fc5smp #1 SMP Tue Jun 12 19:40:16 EDT 2007 i686 User : apache ( 48) PHP Version : 5.2.9 Disable Function : NONE Directory : /usr/share/system-config-soundcard/ |
Upload File : |
## soundcardBackend.py - Contains the backend code needed for system-config-soundcard
## Copyright (C) 2002, 2003 Red Hat, Inc.
## Copyright (C) 2002, 2003 Brent Fox <bfox@redhat.com>
## Copyright (C) 2004, 2005 Bastien Nocera <hadess@hadess.net>
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
import string
import kudzu
import os
import getopt
import sys
import commands
import signal
import timeit
import gtk
##
## I18N
##
from rhpl.translate import _, N_
import rhpl.translate as translate
translate.textdomain ("system-config-soundcard")
##
## Get position
##
def position(list, name):
for line in list:
if line[1] == name:
return line[0]
return 0
##
## Load list of drivers
##
def read_driver_list():
try:
fd = open('/proc/asound/modules', 'r')
except:
return []
list = fd.readlines()
fd.close()
drivers = []
for line in list:
tmp = line.split()
drivers.append([int(tmp[0]), string.replace(tmp[1],'_','-')])
return drivers
##
## Sort fnc for box
##
def card_sort(card1, card2):
return cmp(card1.position, card2.position)
##
## Usage
##
def usage():
print \
_("""Usage: system-config-soundcard [OPTIONS]
Options:
-h, --help display this help and exit
-k, --kernel= which kernel version to assume:
2.4 (modules.conf)
2.6 (modprobe.conf, default)
""")
##
## Options
##
global kernel_type
kver = os.uname()[2]
kernel_type = string.join(kver.split('.')[0:2], '.')
def options():
try:
(opts, rest_args) = getopt.getopt(sys.argv[1:],
"hk:",
["driver=", "help", "kernel="])
except (getopt.GetoptError), e:
print e
print
usage()
sys.exit(1)
for (opt, value) in opts:
if opt == "-h" or opt == "--help":
usage()
sys.exit(0)
if opt == "-k" or opt == "--kernel":
kernel_type = value
if kernel_type != '2.4' and kernel_type != '2.6':
print "ERROR: Invalid kernel version:", kernel_type
usage()
sys.exit(0)
class soundcardBackend:
def __init__(self):
self.defaultModule = None
self.newFile = []
self.readFile()
self.cardList = []
self.dspList = []
self.hardware_device = 0
self.default_device = 0
self.default_card = 0
self.dynamic_keys = 0
self.volume = 60
self.play_end = 0
self.aplay_pid = 0
self.readConfig()
self.setVolume(0, self.volume)
def destroy(self, args):
self.stopTestSound()
def probeCards(self):
driverList = read_driver_list()
list = kudzu.probe(kudzu.CLASS_AUDIO, kudzu.BUS_PCI | kudzu.BUS_MACIO,
kudzu.PROBE_ALL)
print "----------- Kudzu -------------"
for card in list:
print card
print "----------- **** -------------"
for card in list:
#Let's go through the list of cards. If the driver is 'unknown',
#don't put the card in the list.
if card.driver != "unknown":
card.position = position(driverList,card.driver)
self.cardList.append(card)
# Same thing with ISA cards
list = kudzu.probe(kudzu.CLASS_AUDIO, kudzu.BUS_ISAPNP, kudzu.PROBE_ALL)
for card in list:
if card.driver != "unknown":
card.position = position(driverList,card.driver)
self.cardList.append(card)
# Only add USB audio devices that have snd-usb-audio as the driver
list = kudzu.probe(kudzu.CLASS_AUDIO, kudzu.BUS_USB, kudzu.PROBE_ALL)
for card in list:
if card.driver == "snd-usb-audio":
card.position = position(driverList,card.driver)
self.cardList.append(card)
# Same with Mac sound devices
list = kudzu.probe(kudzu.CLASS_AUDIO, kudzu.BUS_MACIO, kudzu.PROBE_ALL)
for card in list:
if card.driver == "snd_powermac":
card.position = position(driverList,card.driver)
self.cardList.append(card)
self.cardList.sort(card_sort)
if self.cardList != []:
self.defaultModule = self.cardList[0].driver
return self.cardList
def getData(self, card):
device, module, description = card
if (string.count(description,"|") == 1):
maker, model = string.split(description, "|")
elif (string.count(description," ") > 0):
maker, model = string.split(description, " ", 1)
else:
maker = description
model = description
return device, module, maker, model
def playTestSound(self, module, index):
if self.aplay_pid != 0:
return
if kernel_type == '2.6':
device = "plughw:%d,0"% index
path = "/usr/bin/aplay"
args = [path, "-D", device, "-N", "/usr/share/system-config-soundcard/sound-sample.wav"]
if kernel_type == "2.4":
if self.dspList == []:
device = "/dev/dsp"
else:
device = "/dev/dsp%d" % self.dspList.index(module)
path = "/usr/bin/play"
args = [path, "-d", device, "/usr/share/system-config-soundcard/sound-sample.wav", "-v", "2"]
self.aplay_pid = os.fork()
if (not self.aplay_pid):
os.execv(path, args)
def stopTestSound(self):
# Kill the child process
if self.aplay_pid != 0:
os.kill(self.aplay_pid,15)
def checkTestSound(self):
# Check if test sound is being played
finished = False
if (self.aplay_pid != 0):
status = os.waitpid(self.aplay_pid, os.WNOHANG)
if (status[0] != 0):
self.aplay_pid = 0
finished = True
return (self.aplay_pid, finished)
def readFile(self):
if kernel_type == '2.6':
fd = open('/etc/modprobe.conf', 'r')
if kernel_type == '2.4':
fd = open('/etc/modules.conf', 'r')
lines = fd.readlines()
fd.close()
self.newFile = []
for line in lines:
#strip out all lines dealing with sound
if (string.find(line, "sound-slot-") < 0 and string.find(line,"snd-card-") < 0) or string.strip(line)[0] == "#":
self.newFile.append(line)
def writeFile(self, selectedCard):
self.selectedCard = selectedCard
aliasList = []
aumixList = []
self.dspList = []
device, self.defaultModule, maker, model = self.getData(self.cardList[selectedCard])
if kernel_type == '2.6':
aliasList.append("alias snd-card-0 %s\n" % self.defaultModule)
aumixList.append("install %s /sbin/modprobe --ignore-install %s && /usr/sbin/alsactl restore >/dev/null 2>&1 ||:\n" % (self.defaultModule, self.defaultModule))
aumixList.append("remove %s { /usr/sbin/alsactl store >/dev/null 2>&1 || : ; }; /sbin/modprobe -r --ignore-remove %s\n" % (self.defaultModule, self.defaultModule))
if kernel_type == '2.4':
aliasList.append("alias sound-slot-0 %s\n" % self.defaultModule)
aumixList.append("post-install sound-slot-0 /bin/aumix-minimal -f /etc/.aumixrc -L >/dev/null 2>&1 || :\n")
aumixList.append("pre-remove sound-slot-0 /bin/aumix-minimal -f /etc/.aumixrc -S >/dev/null 2>&1 || :\n")
self.dspList.append(self.defaultModule)
count = 1
for card in self.cardList:
device, module, maker, model = self.getData(card)
if module == self.defaultModule:
#We've already written this line, so skip
pass
else:
if kernel_type == '2.6':
aliasList.append("alias snd-card-%d %s\n" % (count, module))
aumixList.append("install %s /sbin/modprobe --ignore-install %s && /usr/sbin/alsactl restore >/dev/null 2>&1 ||:\n" % (module, module))
aumixList.append("remove %s { /usr/sbin/alsactl store >/dev/null 2>&1 || : ; }; /sbin/modprobe -r --ignore-remove %s\n" % (module, module))
if kernel_type == '2.4':
aliasList.append("alias sound-slot-%d %s\n" % (count, module))
aumixList.append("post-install sound-slot-%d /bin/aumix-minimal -f /etc/.aumixrc -L >/dev/null 2>&1 || :\n" % count)
aumixList.append("pre-remove sound-slot-%d /bin/aumix-minimal -f /etc/.aumixrc -S >/dev/null 2>&1 || :\n" % count)
self.dspList.append(module)
count = count + 1
#Let's re-read the file again to make sure we're starting from a clean base
self.readFile()
self.newFile = self.newFile + aliasList + aumixList
if kernel_type == '2.6':
fd = open('/etc/modprobe.conf', 'w')
if kernel_type == '2.4':
fd = open('/etc/modules.conf', 'w')
for line in self.newFile:
fd.write(line)
fd.close()
def setVolume(self, index, value):
if kernel_type == '2.4':
aumix_path = "/bin/aumix-minimal"
aumix_pid = os.fork()
aumix_args = [aumix_path, "-f", "/etc/.aumixrc", "-L"]
if (not aumix_pid):
try:
os.execv(aumix_path, aumix_args)
except:
sys.exit(0)
else:
os.waitpid(aumix_pid,0)
#alsa drivers load muted. we need to set the volume for them.
self.volume = value
if kernel_type == '2.6':
amixer_path = "/bin/alsaunmute"
amixer_args = [amixer_path, `index`, "-s", `value`]
amixer_pid = os.fork()
if (not amixer_pid):
try:
os.execv(amixer_path, amixer_args)
except:
sys.exit(0)
else:
os.waitpid(amixer_pid,0)
def getVolume(self):
return self.volume
def setDefaultCard(self, index):
self.default_card = index
def getDefaultCard(self):
return self.cardList[self.default_card]
def getDefaultCardNum(self):
return self.default_card
def setDefaultDevice(self, index):
self.default_device = index
def getDefaultDevice(self):
return self.default_device
def setHardwareDevice(self, state):
self.hardware_device = state;
def getHardwareDevice(self):
return self.hardware_device
def setDynamicKeys(self, state):
self.dynamic_keys = state;
def getDynamicKeys(self):
return self.dynamic_keys
def reconfigureDynamicKeys(self):
if self.dynamic_keys :
if os.path.exists("/var/run/console/console.lock"):
os.system("/usr/bin/ainit `cat /var/run/console/console.lock` start")
else:
if os.path.exists("/var/run/console/console.lock"):
os.system("/usr/bin/ainit `cat /var/run/console/console.lock` stop")
def writeConfig(self):
# Writing to asound.conf
index = self.default_card
device = self.default_device
if kernel_type == '2.6':
lines = []
lines.append("#Generated by system-config-soundcard, do not edit by hand\n")
if self.hardware_device:
lines.append("#HWCONF\n")
lines.append("#DEV %d\n" % device)
lines.append("pcm.!default { type hw card %d device %d } \n" % (index, device))
lines.append("ctl.!default { type hw card %d }\n" % index)
else:
lines.append("#SWCONF\n")
lines.append("#DEV %d\n" % device)
lines.append("defaults.pcm.card %d \n" % index)
lines.append("defaults.pcm.device %d \n" % device)
lines.append("defaults.ctl.card %d \n" % index)
fd = open('/etc/asound.conf', 'w')
for line in lines:
fd.write(line)
fd.close()
# re-writing ainit.conf
fd = open('/etc/alsa/ainit.conf', 'r')
lines = fd.readlines()
fd.close()
lines_out = []
for line in lines:
line_tmp = string.split(string.strip(line))
if line_tmp != [] and cmp(string.lower(line_tmp[0]),"enable") == 0:
if self.dynamic_keys:
lines_out.append("enable = yes\n")
else:
lines_out.append("enable = no\n")
else:
lines_out.append(line)
fd = open('/etc/alsa/ainit.conf', 'w')
fd.writelines(lines_out)
fd.close()
def readConfig(self):
# Check alsa configuration
try:
fd = open('/etc/asound.conf', 'r')
line = fd.readline()
line = fd.readline()
self.hardware_device = (cmp(line[:7],'#HWCONF') != 1)
line = fd.readline()
if not cmp(line[:4],'#DEV'):
self.default_device = int(line[4:])
fd.close()
except:
self.hardware_device = 0
self.default_device = 0
# Check ainit configuration
try:
fd = open('/etc/alsa/ainit.conf', 'r')
lines = fd.readlines()
fd.close()
for line in lines:
line = string.split(string.strip(line))
if line != [] and cmp(string.lower(line[0]),"enable") == 0:
self.dynamic_keys = (cmp(string.lower(line[2]),'yes') == 0)
break;
except:
self.dynamic_keys = 0
try:
self.default_card = int(commands.getoutput('/bin/alsacard'))
except:
self.default_card = 0
print "Read config:\ncard %d\ndevice %d\nHW %d" % (self.default_card, self.default_device, self.hardware_device)
def unloadModules(self):
for card in self.cardList:
device, module, maker, model = self.getData(card)
retval = os.system("/sbin/modprobe -r %s" % module)
def loadModules(self):
retval = os.system("/sbin/modprobe %s" % self.defaultModule)
if retval == 0:
#Modprobe worked
pass
else:
#Modprobe failed
return self.defaultModule
for card in self.cardList:
device, module, maker, model = self.getData(card)
if module == self.defaultModule:
#We've already inserted this one, so don't try again
pass
else:
retval = os.system("/sbin/modprobe %s" % module)
if retval == 0:
#Modprobe worked
pass
else:
#Modprobe failed
return self.defaultModule
return 0