nvidia-config-display 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. try:
  89. # Read config file
  90. (xconfig, xconfigpath) = ixf86config.readConfigFile()
  91. except:
  92. printError("Could not read X config file")
  93. sys.exit(1)
  94. # Check number of arguments
  95. if (len(sys.argv) == 2):
  96. arg = sys.argv[1]
  97. else:
  98. printError("Wrong number of arguments")
  99. printUsage()
  100. sys.exit(1)
  101. # Check value of argument
  102. if arg != "enable" and arg != "disable":
  103. printError("Invalid command")
  104. printUsage()
  105. sys.exit(1)
  106. # Backup original X config file to .backup-nvidia
  107. backup_file = None
  108. output_file = xconfigpath
  109. if output_file != None and os.access(output_file, os.F_OK):
  110. backup_file = output_file + ".backup-nvidia"
  111. try:
  112. os.rename(output_file, backup_file)
  113. except:
  114. printError("Cannot write backup file")
  115. sys.exit(1)
  116. else:
  117. printError("Cannot open X config file (missing or malformed)")
  118. sys.exit(1)
  119. try:
  120. if (arg == "enable"):
  121. # Enable nvidia driver:
  122. # Add nvidia module path and change driver to 'nvidia'
  123. addModulePath(xconfig.files, "/extensions/nvidia")
  124. toggleDriver(xconfig.device, "nv", "nvidia")
  125. elif (arg == "disable"):
  126. # Disable nvidia driver:
  127. # Remove nvidia module path and change driver to 'nv'
  128. removeModulePath(xconfig.files, "/extensions/nvidia")
  129. toggleDriver(xconfig.device, "nvidia", "nv")
  130. else:
  131. # This shouldn't happen, but we handle it anyway
  132. raise
  133. # Write new X config file
  134. xconfig.write(output_file)
  135. except:
  136. printError("Editing failed, restoring backup")
  137. try:
  138. # Something went wrong, restore backup
  139. os.rename(backup_file, output_file)
  140. except:
  141. printError("Failed to restore backup")