123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- import xf86config
- import ixf86config
- import string
- import os
- import sys
- if os.uname()[4] == "x86_64" :
- TOP_MOD_DIR = "/usr/lib64/xorg/modules"
- else:
- TOP_MOD_DIR = "/usr/lib/xorg/modules"
- def addModulePath(files, newPathEntry):
- prevModPath = []
-
- if (files.module != None):
- prevModPath = string.split(files.module, ",")
-
-
- newModPath = [TOP_MOD_DIR]
-
- for i in range(len(prevModPath)):
- mp = prevModPath[i]
-
-
- if mp[len(mp) - 1] == "/":
- mp = mp[:(len(mp) - 1)]
-
- if not mp in newModPath and mp != (TOP_MOD_DIR + "/extensions"):
- newModPath.append(mp)
-
- if not (TOP_MOD_DIR + newPathEntry) in newModPath:
- newModPath.append(TOP_MOD_DIR + newPathEntry)
-
- newModPath.reverse()
- files.module = string.join(newModPath, ",")
- def removeModulePath(files, modulePath):
- prevModPath = []
-
- if (files.module != None):
- prevModPath = string.split(files.module, ",")
- if (len(prevModPath) < 1):
-
- return
- newModPath = []
- for i in range(len(prevModPath)):
- mp = prevModPath[i]
-
-
- if mp[len(mp) - 1] == "/":
- mp = mp[:(len(mp) - 1)]
- if mp != (TOP_MOD_DIR + modulePath) and mp != (TOP_MOD_DIR + "/extensions"):
- newModPath.append(mp)
- files.module = string.join(newModPath, ",")
- def toggleDriver(device, oldDriver, newDriver):
- for dev in device:
- if (dev.driver.lower() == oldDriver.lower()):
- dev.driver = newDriver
- def printError(err):
- print "Error:", err
- def printUsage():
- print "Usage: nvidia-config-display [enable|disable]"
- xorgconf = "/etc/X11/xorg.conf"
- if os.access(xorgconf, os.F_OK):
- try:
- (xconfig, xconfigpath) = ixf86config.readConfigFile()
- except:
- pass
- elif os.access(xorgconf + ".dist", os.F_OK):
- import shutil
- shutil.copy(xorgconf + ".dist", xorgconf)
- try:
- (xconfig, xconfigpath) = ixf86config.readConfigFile()
- except:
- pass
- else:
- printError("Could not read X config file")
- sys.exit(1)
- if (len(sys.argv) == 2):
- arg = sys.argv[1]
- else:
- printError("Wrong number of arguments")
- printUsage()
- sys.exit(1)
- if arg != "enable" and arg != "disable":
- printError("Invalid command")
- printUsage()
- sys.exit(1)
- backup_file = None
- output_file = xconfigpath
- driver_dir = TOP_MOD_DIR + "/drivers/"
- if output_file != None and os.access(output_file, os.F_OK):
- backup_file = output_file + ".backup-nvidia"
- try:
- os.rename(output_file, backup_file)
- except:
- printError("Cannot write backup file")
- sys.exit(1)
- else:
- printError("Cannot open X config file (missing or malformed)")
- sys.exit(1)
- try:
- if (arg == "enable"):
-
-
- addModulePath(xconfig.files, "/extensions/nvidia")
- toggleDriver(xconfig.device, "nv", "nvidia")
- toggleDriver(xconfig.device, "nouveau", "nvidia")
- elif (arg == "disable"):
-
-
- removeModulePath(xconfig.files, "/extensions/nvidia")
- if os.access(driver_dir+"nouveau_drv.so", os.F_OK):
- toggleDriver(xconfig.device, "nvidia", "nouveau")
- elif os.access(driver_dir+"nv_drv.so", os.F_OK):
- toggleDriver(xconfig.device, "nvidia", "nv")
- else:
- toggleDriver(xconfig.device, "nvidia", "vesa")
- else:
-
- raise
-
- xconfig.write(output_file)
- except:
- printError("Editing failed, restoring backup")
- try:
-
- os.rename(backup_file, output_file)
- except:
- printError("Failed to restore backup")
|