nvidia-config-display 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 ixf86config
  19. import string
  20. import os
  21. import sys
  22. if os.uname()[4] == "x86_64" :
  23. TOP_MOD_DIR = "/usr/lib64/xorg/modules"
  24. else:
  25. TOP_MOD_DIR = "/usr/lib/xorg/modules"
  26. # This will add an entry to ModulePath section,
  27. # with previous entries untouched.
  28. def addModulePath(files, newPathEntry):
  29. prevModPath = []
  30. # Check for existing ModulePath
  31. if (files.module != None):
  32. prevModPath = string.split(files.module, ",")
  33. # First, add the default module dirs. We add the dirs in
  34. # reversed order, and reverse the list at the end.
  35. newModPath = [TOP_MOD_DIR]
  36. #newModPath.append(TOP_MOD_DIR + "/extensions")
  37. for i in range(len(prevModPath)):
  38. mp = prevModPath[i]
  39. # Remove trailing "/", in case the config file
  40. # has been hand-edited
  41. if mp[len(mp) - 1] == "/":
  42. mp = mp[:(len(mp) - 1)]
  43. # Add to new module path
  44. if not mp in newModPath and mp != (TOP_MOD_DIR + "/extensions"):
  45. newModPath.append(mp)
  46. # Add new path entry
  47. if not (TOP_MOD_DIR + newPathEntry) in newModPath:
  48. newModPath.append(TOP_MOD_DIR + newPathEntry)
  49. # Reverse list
  50. newModPath.reverse()
  51. files.module = string.join(newModPath, ",")
  52. #
  53. # Removes an entry in the ModulePath list.
  54. #
  55. def removeModulePath(files, modulePath):
  56. prevModPath = []
  57. # Check for existing ModulePath
  58. if (files.module != None):
  59. prevModPath = string.split(files.module, ",")
  60. if (len(prevModPath) < 1):
  61. # ModulePath empty, do nothing.
  62. return
  63. newModPath = []
  64. for i in range(len(prevModPath)):
  65. mp = prevModPath[i]
  66. # Remove trailing "/", in case the config file
  67. # has been hand-edited
  68. if mp[len(mp) - 1] == "/":
  69. mp = mp[:(len(mp) - 1)]
  70. if mp != (TOP_MOD_DIR + modulePath) and mp != (TOP_MOD_DIR + "/extensions"):
  71. newModPath.append(mp)
  72. files.module = string.join(newModPath, ",")
  73. #
  74. # Set driver to newDriver where
  75. # if driver is oldDriver
  76. #
  77. def toggleDriver(device, oldDriver, newDriver):
  78. for dev in device:
  79. if (dev.driver.lower() == oldDriver.lower()):
  80. dev.driver = newDriver
  81. def printError(err):
  82. print "Error:", err
  83. def printUsage():
  84. print "Usage: nvidia-config-display [enable|disable]"
  85. # ------------
  86. # Main section
  87. # ------------
  88. xorgconf = "/etc/X11/xorg.conf"
  89. if os.access(xorgconf, os.F_OK):
  90. try:
  91. (xconfig, xconfigpath) = ixf86config.readConfigFile()
  92. except:
  93. pass
  94. elif os.access(xorgconf + ".dist", os.F_OK):
  95. import shutil
  96. shutil.copy(xorgconf + ".dist", xorgconf)
  97. try:
  98. (xconfig, xconfigpath) = ixf86config.readConfigFile()
  99. except:
  100. pass
  101. else:
  102. printError("Could not read X config file")
  103. sys.exit(1)
  104. # Check number of arguments
  105. if (len(sys.argv) == 2):
  106. arg = sys.argv[1]
  107. else:
  108. printError("Wrong number of arguments")
  109. printUsage()
  110. sys.exit(1)
  111. # Check value of argument
  112. if arg != "enable" and arg != "disable":
  113. printError("Invalid command")
  114. printUsage()
  115. sys.exit(1)
  116. # Backup original X config file to .backup-nvidia
  117. backup_file = None
  118. output_file = xconfigpath
  119. if output_file != None and os.access(output_file, os.F_OK):
  120. backup_file = output_file + ".backup-nvidia"
  121. try:
  122. os.rename(output_file, backup_file)
  123. except:
  124. printError("Cannot write backup file")
  125. sys.exit(1)
  126. else:
  127. printError("Cannot open X config file (missing or malformed)")
  128. sys.exit(1)
  129. try:
  130. if (arg == "enable"):
  131. # Enable nvidia driver:
  132. # Add nvidia module path and change driver to 'nvidia'
  133. addModulePath(xconfig.files, "/extensions/nvidia")
  134. toggleDriver(xconfig.device, "nv", "nvidia")
  135. elif (arg == "disable"):
  136. # Disable nvidia driver:
  137. # Remove nvidia module path and change driver to 'nv'
  138. removeModulePath(xconfig.files, "/extensions/nvidia")
  139. toggleDriver(xconfig.device, "nvidia", "nv")
  140. else:
  141. # This shouldn't happen, but we handle it anyway
  142. raise
  143. # Write new X config file
  144. xconfig.write(output_file)
  145. except:
  146. printError("Editing failed, restoring backup")
  147. try:
  148. # Something went wrong, restore backup
  149. os.rename(backup_file, output_file)
  150. except:
  151. printError("Failed to restore backup")