1
# (c) 2007 Canonical Ltd.
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.
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.
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.
17
'''Abstract handler for a restricted X.org graphics driver.'''
21
import xorgconfig # from kde-guidance
23
from handlers import ModulePackageHandler
24
from oslib import OSLib
26
#--------------------------------------------------------------------#
28
class XorgDriverHandler(ModulePackageHandler):
29
'''Abstract class for a restricted X.org graphics driver.'''
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.
36
This usually consists of a kernel module and a driver package, plus
37
some xorg.conf configuration.
39
- ui, module, driver_package, name, description, rationale: as in
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)
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
59
self.xorg_conf = xorgconfig.readConfig(OSLib.inst.xorg_conf_path)
60
except (IOError, xorgconfig.ParseException, AttributeError):
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
74
devices = self.xorg_conf.getSections('device')
75
if len(devices) > 0 and devices[0].driver != self.xorg_driver:
78
return ModulePackageHandler.enabled(self)
80
def _mod_enabled(self, module):
81
'''Check if current xorg configuration enables a module.'''
83
module_sections = self.xorg_conf.getSections('module')
84
if len(module_sections) < 1:
86
for m in module_sections[0]._contents:
87
if len(m._row) >= 2 and m._row[1] == module:
92
ModulePackageHandler.enable(self)
94
# do not mangle xorg.conf if package installation has been aborted
95
if not OSLib.inst.package_installed(self.package):
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())
102
device = self.xorg_conf.getSections('device')[0]
103
device.driver = self.xorg_driver
105
have_modules = len(self.xorg_conf.getSections('module')) > 0
107
for m in self.remove_modules:
108
self.xorg_conf.getSections('module')[0].removeModule(m)
111
self.xorg_conf.append(self.xorg_conf.makeSection(None, ['Section',
113
for m in self.add_modules:
114
if not self._mod_enabled(m):
115
self.xorg_conf.getSections('module')[0].addModule(m)
117
for k, v in self.extra_conf_options.iteritems():
118
device.option.removeOptionByName(k)
119
device.option.appendOptionRow([k, v])
121
self.enable_config_hook()
123
self.xorg_conf.writeConfig(OSLib.inst.xorg_conf_path)
125
OSLib.inst.ui_notify_reboot(self.ui)
128
ModulePackageHandler.disable(self)
130
# do not mangle xorg.conf if package uninstallation has been aborted
131
if OSLib.inst.package_installed(self.package):
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())
139
self.xorg_conf = xorgconfig.readConfig(OSLib.inst.xorg_conf_path)
140
else: # no backup, so mangle current config manually
142
# try to get the previous driver
144
# p = os.path.join(MANAGER_ROOT, self.name + '.olddriver')
145
# free_driver = open(p).read().strip()
148
# free_driver = self.free_driver
149
free_driver = self.alt_free_driver
151
device = self.xorg_conf.getSections('device')[0]
152
device.driver = free_driver
154
have_modules = len(self.xorg_conf.getSections('module')) > 0
156
for m in self.add_modules:
157
self.xorg_conf.getSections('module')[0].removeModule(m)
158
if self.remove_modules:
160
self.xorg_conf.append(self.xorg_conf.makeSection(None, ['Section',
162
for m in self.remove_modules:
163
if not self._mod_enabled(m):
164
self.xorg_conf.getSections('module')[0].addModule(m)
166
for k in self.extra_conf_options:
167
device.option.removeOptionByName(k)
169
self.disable_config_hook()
171
self.xorg_conf.writeConfig(OSLib.inst.xorg_conf_path)
173
OSLib.inst.ui_notify_reboot(self.ui)
175
def enable_config_hook(self):
176
'''Custom self.xorg_config changes after driver, modules, and extra
177
driver options have been changed.
181
def disable_config_hook(self):
182
'''Custom self.xorg_config changes after driver, modules, and extra
183
driver options have been changed.