|
[PHP]#!/usr/bin/env python
# Copyright (C) 2005, Yancy Zhang.
# Author: Yancy Zhang <yancy_zhang@yahoo.com.cn>
# Decription: a python script to autoconfig Xfree86 ported from
# Charles Wang <charles@linux.net.cn>'s MagicInstaller code.
#
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANT; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public LIcense for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
from gettext import gettext as _
import string
import os
import os.path
import sys
import rhpl.monitor
import rhpl.videocard
import rhpl.mouse
class Xconfigurator:
def __init__(self):
self.x_settings = {}
def probe_monitor(self):
mon = rhpl.monitor.MonitorInfo()
return (mon.monName, mon.monHoriz, mon.monVert)
def probe_videocard(self):
vci = rhpl.videocard.VideoCardInfo()
vclist = []
for vc in vci.orig_videocards:
vclist.append((vc.cardData['NAME'], vc.cardData['DRIVER'], vc.vidRam))
return vclist
def probe_mouse(self):
# Call rhpl.mouse.Mouse will affect the action of mouse, so do not use it.
#mouse = rhpl.mouse.Mouse()
#if mouse.device == None:
mouse = rhpl.mouse.Mouse(skipProbe = 1)
if mouse.device == None:
dev = 'none'
else:
dev = mouse.device
xemu3 = 'false'
if mouse.info.has_key('XEMU3') and mouse.info['XEMU3'] != 'no':
xemu3 = 'true'
return (mouse.info['FULLNAME'], mouse.info['XMOUSETYPE'], dev, xemu3)
def gen_x_config(self):
def trim_scope(s):
def trim_num(s):
try:
n = str(float(string.strip(s)))
except:
n = '0.0'
return n
if string.find(s, '-') >= 0:
(n0, n1) = string.split(s, '-', 1)
return trim_num(n0) + ' - ' + trim_num(n1)
else:
return trim_num(s)
# Generate XF86Config.
resmap = {640: '640x480', 800: '800x600', 1024: '1024x768', 1280: '1280x1024'}
# ServerLayout Section
if self.x_settings['mouse'][2] != 'input/mice':
alwayscore_mouse = ''
else:
alwayscore_mouse = ' InputDevice "DevInputMice" "AlwaysCore"\n'
serverlayout = '''
# XFree86 4 configuration created by magicxconf.
Section "ServerLayout"
Identifier "Default Layout"
Screen 0 "Screen0" 0 0
InputDevice "Mouse0" "CorePointer"
InputDevice "Keyboard0" "CoreKeyboard"
%sEndSection
''' % alwayscore_mouse
# File Section
files = '''
Section "Files"
RgbPath "/usr/X11R6/lib/X11/rgb"
FontPath "/usr/X11R6/lib/X11/fonts/misc:unscaled"
FontPath "/usr/X11R6/lib/X11/fonts/75dpi:unscaled"
FontPath "/usr/X11R6/lib/X11/fonts/100dpi:unscaled"
FontPath "/usr/X11R6/lib/X11/fonts/misc"
FontPath "/usr/X11R6/lib/X11/fonts/Type1"
FontPath "/usr/X11R6/lib/X11/fonts/cyrillic"
FontPath "/usr/X11R6/lib/X11/fonts/TTF"
FontPath "/usr/share/fonts/default/Type1"
FontPath "/usr/share/fonts/ttf/zh_CN"
%sEndSection
''' % string.join(map(lambda fp: " FontPath \"" + fp + "\"\n",
self.x_settings['FontPathes']), '\n')
# Module Section
module = '''
Section "Module"
Load "dbe"
Load "extmod"
Load "fbdevhw"
Load "glx"
Load "record"
Load "freetype"
Load "type1"
EndSection
'''
# InputDevice Section for Keyboard
keyboard = '''
Section "InputDevice"
Identifier "Keyboard0"
Driver "keyboard"
Option "XkbRules" "xfree86"
Option "XbkModel" "pc105"
Option "XkbLayout" "us"
EndSection
'''
# InputDevice Section for Mouse
if self.x_settings['mouse'][3] == 'true':
xemu3 = 'yes'
else:
xemu3 = 'no'
mouse = '''
Section "InputDevice"
Identifier "Mouse0"
Driver "mouse"
Option " rotocol" "%s"
Option "Device" "/dev/%s"
Option "ZAxisMapping" "4 5"
Option "Emulate3Buttons" "%s"
EndSection
''' % (self.x_settings['mouse'][1], self.x_settings['mouse'][2], xemu3)
# Add DevInputMice as needed.
if self.x_settings['mouse'][2] == 'input/mice':
mouse = mouse + '''
Section "InputDevice"
# If the normal CorePointer mouse is not a USB mouse then
# this input device can be used in AlwaysCore mode to let you
# also use USB mice at the same time.
Identifier "DevInputMice"
Driver "mouse"
Option " rotocol" "%s"
Option "Device" "/dev/input/mice"
Option "ZAxisMapping" "4 5"
Option "Emulate3Buttons" "%s"
EndSection
''' % (self.x_settings['mouse'][1], xemu3)
# Monitor Section
monitor = '''
Section "Monitor"
Identifier "Monitor0"
VendorName "Monitor Vendor"
ModelName "%s"
HorizSync %s
VertRefresh %s
EndSection
''' % (self.x_settings['monitor'][0],
trim_scope(self.x_settings['monitor'][1]),
trim_scope(self.x_settings['monitor'][2]))
# Device Section
videocard = '''
Section "Device"
Identifier "Videocard0"
Driver "%s"
VendorName "Videocard vendor"
BoardName "%s"
VideoRam %s
EndSection
''' % (self.x_settings['videocard'][1],
self.x_settings['videocard'][0],
self.x_settings['videocard'][2])
# Screen Section
depthlist = map(lambda depth: int(depth), self.x_settings['modes'].keys())
depthlist.sort()
default_depth = depthlist[-1]
display_list = []
display_string = ''
for depth in depthlist:
modes_string = string.join(map(lambda m: '"' + resmap[m] + '"',
self.x_settings['modes'][str(depth)]))
display_string = display_string + '\tSubSection\t"Display%d"\n' % depth
display_string = display_string + '\t\tDepth\t%d\n' % depth
display_string = display_string + '\t\tModes\t%s\n' % modes_string
display_string = display_string + '\tEndSubSection\n'
screen = '''
Section "Screen"
Identifier "Screen0"
Device "Videocard0"
Monitor "Monitor0"
DefaultDepth %d
%sEndSection
''' % (default_depth, display_string)
# Generate the XF86Config file.
try:
f = file('/root/XF86Config', 'w')
f.write(serverlayout)
f.write(files)
f.write(module)
f.write(keyboard)
f.write(mouse)
f.write(monitor)
f.write(videocard)
f.write(screen)
f.close()
except:
sys.stderr.write("write configure file error")
sys.exit(1)
print "/root/XF86Config generated\n"
return 1
if __name__ == "__main__":
def usage():
help_str = """Usage:
%s Configure the Xfree86 interactively
%s -auto Configure the Xfree86 automaticly\n
""" % (os.path.basename(sys.argv[0]), os.path.basename(sys.argv[0]))
sys.stderr.write(help_str)
sys.exit(1)
if len(sys.argv) > 2:
usage()
if len(sys.argv) == 1:
sys.stderr.write("interactive configure not implemented yet, please use '%s -auto' to autoconfigure\n"
% os.path.basename(sys.argv[0]))
usage()
elif sys.argv[1].strip() == '-auto':
xconf = Xconfigurator()
xconf.x_settings['FontPathes'] = ""
xconf.x_settings['mouse'] = xconf.probe_mouse()
xconf.x_settings['videocard'] = xconf.probe_videocard()[0]
xconf.x_settings['monitor'] = xconf.probe_monitor()
xconf.x_settings['modes'] = {}
for depth in [8, 16, 24]:
for res in [640, 800, 1024]:
xconf.x_settings['modes'][str(depth)] = [res]
xconf.gen_x_config()
else:
usage()[/PHP]
请把上边的脚本保存为magicxconf, 然后chmod +x magicxconf
用root帐号执行./magicxconf -auto, 若没有出错,会生成/root/XF86Config文件。
请检查一下该文件中的设置,是否正确识别了你的硬件如显卡等。
若识别正确,请把系统初始的/etc/X11/XF86Config改名备份,把/root/XF86Config拷贝到
/etc/X11/下,然后重启X,注意这有可能造成X无法启动,把系统原来的XF86Config还原回去即可(一定要提前备份)。
若识别不正确,请指出与系统默认的XF86Config中的差异。谢谢!! |
|