source: projects/specs/trunk/nonfree/xorg-x11-drv-nvidia/nvidia-config-display @ 7994

Revision 7994, 5.4 KB checked in by daisuke, 10 years ago (diff)

xorg-x11-drv-nvidia: update to 331.20

  • Property svn:executable set to *
Line 
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
19import xf86config
20import ixf86config
21import string
22import os
23import sys
24
25if os.uname()[4] == "x86_64" :
26    TOP_MOD_DIR = "/usr/lib64/xorg/modules"
27else:
28    TOP_MOD_DIR = "/usr/lib/xorg/modules"
29
30# This will add an entry to ModulePath section,
31# with previous entries untouched.
32def 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#
60def 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#
83def toggleDriver(device, oldDriver, newDriver):
84    for dev in device:
85        if (dev.driver.lower() == oldDriver.lower()):
86            dev.driver = newDriver
87
88def printError(err):
89    print "Error:", err
90
91def printUsage():
92    print "Usage: nvidia-config-display [enable|disable]"
93
94# ------------
95# Main section
96# ------------
97
98xorgconf = "/etc/X11/xorg.conf"
99if os.access(xorgconf, os.F_OK):
100    try:
101        (xconfig, xconfigpath) = ixf86config.readConfigFile()
102    except:
103        pass
104elif 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
111else:
112    printError("Could not read X config file")
113    sys.exit(1)
114
115# Check number of arguments
116if (len(sys.argv) == 2):
117    arg = sys.argv[1]
118else:
119    printError("Wrong number of arguments")
120    printUsage()
121    sys.exit(1)
122
123# Check value of argument
124if 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
130backup_file = None
131output_file = xconfigpath
132driver_dir = TOP_MOD_DIR + "/drivers/"
133if output_file != None and os.access(output_file, os.F_OK):
134    backup_file = output_file + ".backup-nvidia"
135    try:
136        os.rename(output_file, backup_file)
137    except:
138        printError("Cannot write backup file")
139        sys.exit(1)
140else:
141    printError("Cannot open X config file (missing or malformed)")
142    sys.exit(1)
143try:
144    if (arg == "enable"):
145        # Enable nvidia driver:
146        # Add nvidia module path and change driver to 'nvidia'
147        addModulePath(xconfig.files, "/extensions/nvidia")
148        toggleDriver(xconfig.device, "nv", "nvidia")
149        toggleDriver(xconfig.device, "nouveau", "nvidia")
150    elif (arg == "disable"):
151        # Disable nvidia driver:
152        # Remove nvidia module path and change driver to 'nv'
153        removeModulePath(xconfig.files, "/extensions/nvidia")
154        if os.access(driver_dir+"nouveau_drv.so", os.F_OK):
155            toggleDriver(xconfig.device, "nvidia", "nouveau")
156        elif os.access(driver_dir+"nv_drv.so", os.F_OK):
157            toggleDriver(xconfig.device, "nvidia", "nv")
158        else:
159            toggleDriver(xconfig.device, "nvidia", "vesa")
160    else:
161        # This shouldn't happen, but we handle it anyway
162        raise
163    # Write new X config file
164    xconfig.write(output_file)
165except:
166    printError("Editing failed, restoring backup")
167    try:
168        # Something went wrong, restore backup
169        os.rename(backup_file, output_file)
170    except:
171        printError("Failed to restore backup")
172
Note: See TracBrowser for help on using the repository browser.