~alexeftimie/jockey/fix-gobject

« back to all changes in this revision

Viewing changes to jockey/xorg_driver.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2008-01-17 15:02:40 UTC
  • Revision ID: james.westby@ubuntu.com-20080117150240-djmsi8giu255vzzn
Tags: 0.1~r118
* Initial release, result of completely rewriting restricted-manager to be
  maintainable, robust, and suitable for other distributions. Some features
  and the KDE UI still need to be ported.
* See restricted-manager-rewrite specification for details.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# (c) 2007 Canonical Ltd.
 
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 General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License along
 
14
# with this program; if not, write to the Free Software Foundation, Inc.,
 
15
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
16
 
 
17
'''Abstract handler for a restricted X.org graphics driver.'''
 
18
 
 
19
import os.path
 
20
 
 
21
import xorgconfig # from kde-guidance
 
22
 
 
23
from handlers import ModulePackageHandler
 
24
from oslib import OSLib
 
25
 
 
26
#--------------------------------------------------------------------#
 
27
 
 
28
class XorgDriverHandler(ModulePackageHandler):
 
29
    '''Abstract class for a restricted X.org graphics driver.'''
 
30
 
 
31
    def __init__(self, ui, module, driver_package, xorg_driver,
 
32
        alt_free_driver, extra_conf_options={}, add_modules=[],
 
33
            remove_modules=[], name=None, description=None, rationale=None):
 
34
        '''Create handler for a particular X.org graphics driver.
 
35
        
 
36
        This usually consists of a kernel module and a driver package, plus
 
37
        some xorg.conf configuration.
 
38
 
 
39
        - ui, module, driver_package, name, description, rationale: as in
 
40
          ModulePackageHandler
 
41
        - xorg_driver: name of the X.org driver as it appears in xorg.conf
 
42
        - alt_free_driver: free fallback driver if this one gets disabled
 
43
        - extra_conf_options: dictionary of extra "Option"s that go into the
 
44
          Device section of xorg.conf
 
45
        - add_modules: list of modules that get added to the "Module" section
 
46
          of xorg.conf when enabling this driver (and removed when disabling)
 
47
        - remove_modules: list of modules that get removed from the "Module"
 
48
          section of xorg.conf when enabling this driver (and added when disabling)
 
49
        '''
 
50
        ModulePackageHandler.__init__(self, ui, module, driver_package, name,
 
51
            description, rationale)
 
52
        self.xorg_driver = xorg_driver
 
53
        self.alt_free_driver = alt_free_driver
 
54
        self.extra_conf_options = extra_conf_options
 
55
        self.add_modules = add_modules
 
56
        self.remove_modules = remove_modules
 
57
 
 
58
        try:
 
59
            self.xorg_conf = xorgconfig.readConfig(OSLib.inst.xorg_conf_path)
 
60
        except (IOError, xorgconfig.ParseException, AttributeError):
 
61
            self.xorg_conf = None
 
62
 
 
63
    def can_change(self):
 
64
        if self.xorg_conf:
 
65
            return None
 
66
        else:
 
67
            # translators: %s is the path to xorg.conf
 
68
            return self.ui._('Reconfiguring X.org video drivers is not '
 
69
                'possible: %s is invalid or does not exist.') % \
 
70
                OSLib.inst.xorg_conf_path
 
71
 
 
72
    def enabled(self):
 
73
        if self.xorg_conf:
 
74
            devices = self.xorg_conf.getSections('device') 
 
75
            if len(devices) > 0 and devices[0].driver != self.xorg_driver:
 
76
                return False
 
77
 
 
78
        return ModulePackageHandler.enabled(self)
 
79
 
 
80
    def _mod_enabled(self, module):
 
81
        '''Check if current xorg configuration enables a module.'''
 
82
 
 
83
        module_sections = self.xorg_conf.getSections('module')
 
84
        if len(module_sections) < 1:
 
85
            return False
 
86
        for m in module_sections[0]._contents:
 
87
            if len(m._row) >= 2 and m._row[1] == module:
 
88
                return True
 
89
        return False
 
90
 
 
91
    def enable(self):
 
92
        ModulePackageHandler.enable(self)
 
93
 
 
94
        # do not mangle xorg.conf if package installation has been aborted
 
95
        if not OSLib.inst.package_installed(self.package):
 
96
            return
 
97
 
 
98
        # backup the current xorg.conf
 
99
        open(os.path.join(OSLib.inst.backup_dir, self.xorg_driver + '.oldconf'), 'w').write(
 
100
            open(OSLib.inst.xorg_conf_path).read())
 
101
 
 
102
        device = self.xorg_conf.getSections('device')[0]
 
103
        device.driver = self.xorg_driver
 
104
 
 
105
        have_modules = len(self.xorg_conf.getSections('module')) > 0
 
106
        if have_modules:
 
107
            for m in self.remove_modules:
 
108
                self.xorg_conf.getSections('module')[0].removeModule(m)
 
109
        if self.add_modules:
 
110
            if not have_modules:
 
111
                self.xorg_conf.append(self.xorg_conf.makeSection(None, ['Section',
 
112
                    'Module']))
 
113
            for m in self.add_modules:
 
114
                if not self._mod_enabled(m):
 
115
                    self.xorg_conf.getSections('module')[0].addModule(m)
 
116
 
 
117
        for k, v in self.extra_conf_options.iteritems():
 
118
            device.option.removeOptionByName(k)
 
119
            device.option.appendOptionRow([k, v])
 
120
 
 
121
        self.enable_config_hook()
 
122
 
 
123
        self.xorg_conf.writeConfig(OSLib.inst.xorg_conf_path)
 
124
        
 
125
        OSLib.inst.ui_notify_reboot(self.ui)
 
126
 
 
127
    def disable(self):
 
128
        ModulePackageHandler.disable(self)
 
129
 
 
130
        # do not mangle xorg.conf if package uninstallation has been aborted
 
131
        if OSLib.inst.package_installed(self.package):
 
132
            return
 
133
 
 
134
        # if we have the previous xorg.conf, restore that
 
135
        oldconf = os.path.join(OSLib.inst.backup_dir, self.xorg_driver + '.oldconf')
 
136
        if os.path.exists(oldconf):
 
137
            open(OSLib.inst.xorg_conf_path, 'w').write(open(oldconf).read())
 
138
            os.unlink(oldconf)
 
139
            self.xorg_conf = xorgconfig.readConfig(OSLib.inst.xorg_conf_path)
 
140
        else: # no backup, so mangle current config manually
 
141
            # TODO
 
142
            # try to get the previous driver
 
143
            #try:
 
144
            #    p = os.path.join(MANAGER_ROOT, self.name + '.olddriver')
 
145
            #    free_driver = open(p).read().strip()
 
146
            #    os.unlink(p)
 
147
            #except IOError:
 
148
            #    free_driver = self.free_driver
 
149
            free_driver = self.alt_free_driver
 
150
 
 
151
            device = self.xorg_conf.getSections('device')[0]
 
152
            device.driver = free_driver
 
153
 
 
154
            have_modules = len(self.xorg_conf.getSections('module')) > 0
 
155
            if have_modules:
 
156
                for m in self.add_modules:
 
157
                    self.xorg_conf.getSections('module')[0].removeModule(m)
 
158
            if self.remove_modules:
 
159
                if not have_modules:
 
160
                    self.xorg_conf.append(self.xorg_conf.makeSection(None, ['Section',
 
161
                        'Module']))
 
162
                for m in self.remove_modules:
 
163
                    if not self._mod_enabled(m):
 
164
                        self.xorg_conf.getSections('module')[0].addModule(m)
 
165
 
 
166
            for k in self.extra_conf_options:
 
167
                device.option.removeOptionByName(k)
 
168
 
 
169
            self.disable_config_hook()
 
170
 
 
171
            self.xorg_conf.writeConfig(OSLib.inst.xorg_conf_path)
 
172
 
 
173
        OSLib.inst.ui_notify_reboot(self.ui)
 
174
 
 
175
    def enable_config_hook(self):
 
176
        '''Custom self.xorg_config changes after driver, modules, and extra
 
177
        driver options have been changed.
 
178
        '''
 
179
        pass
 
180
 
 
181
    def disable_config_hook(self):
 
182
        '''Custom self.xorg_config changes after driver, modules, and extra
 
183
        driver options have been changed.
 
184
        '''
 
185
        pass
 
186