nvidia-config-display 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/python
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU Library General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. #
  17. # Copyright 2003, 2004 Peter Backlund
  18. import xf86config
  19. import ixf86config
  20. import string
  21. import os
  22. import sys
  23. if os.uname()[4] == "x86_64" :
  24. TOP_MOD_DIR = "/usr/lib64/xorg/modules"
  25. else:
  26. TOP_MOD_DIR = "/usr/lib/xorg/modules"
  27. # This will add an entry to ModulePath section,
  28. # with previous entries untouched.
  29. def addModulePath(files, newPathEntry):
  30. prevModPath = []
  31. # Check for existing ModulePath
  32. if (files.module != None):
  33. prevModPath = string.split(files.module, ",")
  34. # First, add the default module dirs. We add the dirs in
  35. # reversed order, and reverse the list at the end.
  36. newModPath = [TOP_MOD_DIR]
  37. #newModPath.append(TOP_MOD_DIR + "/extensions")
  38. for i in range(len(prevModPath)):
  39. mp = prevModPath[i]
  40. # Remove trailing "/", in case the config file
  41. # has been hand-edited
  42. if mp[len(mp) - 1] == "/":
  43. mp = mp[:(len(mp) - 1)]
  44. # Add to new module path
  45. if not mp in newModPath and mp != (TOP_MOD_DIR + "/extensions"):
  46. newModPath.append(mp)
  47. # Add new path entry
  48. if not (TOP_MOD_DIR + newPathEntry) in newModPath:
  49. newModPath.append(TOP_MOD_DIR + newPathEntry)
  50. # Reverse list
  51. newModPath.reverse()
  52. files.module = string.join(newModPath, ",")
  53. #
  54. # Removes an entry in the ModulePath list.
  55. #
  56. def removeModulePath(files, modulePath):
  57. prevModPath = []
  58. # Check for existing ModulePath
  59. if (files.module != None):
  60. prevModPath = string.split(files.module, ",")
  61. if (len(prevModPath) < 1):
  62. # ModulePath empty, do nothing.
  63. return
  64. newModPath = []
  65. for i in range(len(prevModPath)):
  66. mp = prevModPath[i]
  67. # Remove trailing "/", in case the config file
  68. # has been hand-edited
  69. if mp[len(mp) - 1] == "/":
  70. mp = mp[:(len(mp) - 1)]
  71. if mp != (TOP_MOD_DIR + modulePath) and mp != (TOP_MOD_DIR + "/extensions"):
  72. newModPath.append(mp)
  73. files.module = string.join(newModPath, ",")
  74. #
  75. # Set driver to newDriver where
  76. # if driver is oldDriver
  77. #
  78. def toggleDriver(device, oldDriver, newDriver):
  79. for dev in device:
  80. if (dev.driver.lower() == oldDriver.lower()):
  81. dev.driver = newDriver
  82. def printError(err):
  83. print "Error:", err
  84. def printUsage():
  85. print "Usage: nvidia-config-display [enable|disable]"
  86. # ------------
  87. # Main section
  88. # ------------
  89. xorgconf = "/etc/X11/xorg.conf"
  90. if os.access(xorgconf, os.F_OK):
  91. try:
  92. (xconfig, xconfigpath) = ixf86config.readConfigFile()
  93. except:
  94. pass
  95. elif os.access(xorgconf + ".dist", os.F_OK):
  96. import shutil
  97. shutil.copy(xorgconf + ".dist", xorgconf)
  98. try:
  99. (xconfig, xconfigpath) = ixf86config.readConfigFile()
  100. except:
  101. pass
  102. else:
  103. printError("Could not read X config file")
  104. sys.exit(1)
  105. # Check number of arguments
  106. if (len(sys.argv) == 2):
  107. arg = sys.argv[1]
  108. else:
  109. printError("Wrong number of arguments")
  110. printUsage()
  111. sys.exit(1)
  112. # Check value of argument
  113. if arg != "enable" and arg != "disable":
  114. printError("Invalid command")
  115. printUsage()
  116. sys.exit(1)
  117. # Backup original X config file to .backup-nvidia
  118. backup_file = None
  119. output_file = xconfigpath
  120. driver_dir = TOP_MOD_DIR + "/drivers/"
  121. if output_file != None and os.access(output_file, os.F_OK):
  122. backup_file = output_file + ".backup-nvidia"
  123. try:
  124. os.rename(output_file, backup_file)
  125. except:
  126. printError("Cannot write backup file")
  127. sys.exit(1)
  128. else:
  129. printError("Cannot open X config file (missing or malformed)")
  130. sys.exit(1)
  131. try:
  132. if (arg == "enable"):
  133. # Enable nvidia driver:
  134. # Add nvidia module path and change driver to 'nvidia'
  135. addModulePath(xconfig.files, "/extensions/nvidia")
  136. toggleDriver(xconfig.device, "nv", "nvidia")
  137. toggleDriver(xconfig.device, "nouveau", "nvidia")
  138. elif (arg == "disable"):
  139. # Disable nvidia driver:
  140. # Remove nvidia module path and change driver to 'nv'
  141. removeModulePath(xconfig.files, "/extensions/nvidia")
  142. if os.access(driver_dir+"nouveau_drv.so", os.F_OK):
  143. toggleDriver(xconfig.device, "nvidia", "nouveau")
  144. elif os.access(driver_dir+"nv_drv.so", os.F_OK):
  145. toggleDriver(xconfig.device, "nvidia", "nv")
  146. else:
  147. toggleDriver(xconfig.device, "nvidia", "vesa")
  148. else:
  149. # This shouldn't happen, but we handle it anyway
  150. raise
  151. # Write new X config file
  152. xconfig.write(output_file)
  153. except:
  154. printError("Editing failed, restoring backup")
  155. try:
  156. # Something went wrong, restore backup
  157. os.rename(backup_file, output_file)
  158. except:
  159. printError("Failed to restore backup")