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