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