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

Revision 4286, 4.7 KB checked in by daisuke, 13 years ago (diff)

add nvidia-config-display to repos.

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 ixf86config
20import string
21import os
22import sys
23
24if os.uname()[4] == "x86_64" :
25    TOP_MOD_DIR = "/usr/lib64/xorg/modules"
26else:
27    TOP_MOD_DIR = "/usr/lib/xorg/modules"
28
29# This will add an entry to ModulePath section,
30# with previous entries untouched.
31def 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#
59def 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#
82def toggleDriver(device, oldDriver, newDriver):
83    for dev in device:
84        if (dev.driver.lower() == oldDriver.lower()):
85            dev.driver = newDriver
86
87def printError(err):
88    print "Error:", err
89
90def printUsage():
91    print "Usage: nvidia-config-display [enable|disable]"
92
93# ------------
94# Main section
95# ------------
96
97try:
98    # Read config file
99    (xconfig, xconfigpath) = ixf86config.readConfigFile()
100except:
101    printError("Could not read X config file")
102    sys.exit(1)
103
104# Check number of arguments
105if (len(sys.argv) == 2):
106    arg = sys.argv[1]
107else:
108    printError("Wrong number of arguments")
109    printUsage()
110    sys.exit(1)
111
112# Check value of argument
113if arg != "enable" and arg != "disable":
114    printError("Invalid command")
115    printUsage()
116    sys.exit(1)
117
118# Backup original X config file to .backup-nvidia
119backup_file = None
120output_file = xconfigpath
121if output_file != None and os.access(output_file, os.F_OK):
122    backup_file = output_file + ".backup-nvidia"
123    try:
124        os.rename(output_file, backup_file)
125    except:
126        printError("Cannot write backup file")
127        sys.exit(1)
128else:
129    printError("Cannot open X config file (missing or malformed)")
130    sys.exit(1)
131try:
132    if (arg == "enable"):
133        # Enable nvidia driver:
134        # Add nvidia module path and change driver to 'nvidia'
135        addModulePath(xconfig.files, "/extensions/nvidia")
136        toggleDriver(xconfig.device, "nv", "nvidia")
137    elif (arg == "disable"):
138        # Disable nvidia driver:
139        # Remove nvidia module path and change driver to 'nv'
140        removeModulePath(xconfig.files, "/extensions/nvidia")
141        toggleDriver(xconfig.device, "nvidia", "nv")
142    else:
143        # This shouldn't happen, but we handle it anyway
144        raise
145    # Write new X config file
146    xconfig.write(output_file)
147except:
148    printError("Editing failed, restoring backup")
149    try:
150        # Something went wrong, restore backup
151        os.rename(backup_file, output_file)
152    except:
153        printError("Failed to restore backup")
154
Note: See TracBrowser for help on using the repository browser.