Release new version 0.7.2
[grml-x.git] / grml-x
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # vim: set et ts=4:
4
5 # Filename:      grml-x
6 # Purpose:       wrapper for startx on grml [providing new xconfiguration tool]
7 # Authors:       grml-team (grml.org), (c) Christian Hofstaedtler <ch@grml.org>
8 # Bug-Reports:   see http://grml.org/bugs/
9 # License:       This file is licensed under the GPL v2.
10 ###############################################################################
11
12 # Requires python 2.6 or, possibly, a newer version of python 2.X.
13
14 import fileinput, os, subprocess, sys, tempfile, time, traceback
15 from optparse import OptionParser
16
17 class Section(object):
18     def __init__(self, name, identifier, data):
19         self.name = name
20         self.identifer = identifier
21         self.data = data
22         self.subsect = ""
23     def __str__(self):
24         s = "Section \"%s\"\n\tIdentifier \"%s\"\n" % (self.name, self.identifer)
25         for k in self.data:
26             v = self.data[k]
27             if isinstance(v, list):
28                 v = '" "'.join(v)
29             elif not isinstance(v, basestring): # int, others
30                 v = str(v)
31             elif '-' in v: # sync range
32                 pass
33             else:
34                 v = '"%s"' % v
35             s += "\t%s %s\n" % (k, v)
36         s += self.subsect
37         s += 'EndSection\n'
38         return s
39
40 def get_monitor_section(options, force):
41     if not options.hsync and not options.vsync and not force:
42         return None
43     d = {}
44     d['HorizSync'] = options.hsync or '28.0 - 96.0'
45     d['VertRefresh'] = options.vsync or '50.0 - 60.0'
46     return Section('Monitor', 'Monitor0', d)
47
48 def get_device_section(options):
49     if not options.module:
50         return None
51     d = {}
52     d['Driver'] = options.module
53     d['VendorName'] = 'All'
54     d['BoardName'] = 'All'
55     return Section('Device', 'Card0', d)
56
57 def build_bootparams():
58     lines = []
59     def walk_bootparams_path(p):
60         try:
61             if not os.path.exists(p): return
62             for root, dirs, files in os.walk(p):
63                 for name in files:
64                     f = open(os.path.join(root, name))
65                     lines.extend(f.readlines())
66                     f.close()
67         except:
68             print 'W: Error while getting bootparams from %s' % p
69     f = open('/proc/cmdline')
70     lines.append(f.readline())
71     f.close()
72     walk_bootparams_path('/lib/live/mount/medium/bootparams')
73     walk_bootparams_path('/run/live/medium/bootparams')
74     params = {}
75     for p in ' '.join(lines).split(' '):
76         if '=' in p:
77             (k,v) = p.split('=', 1)
78             params[k] = v
79         else:
80             params[p] = True
81     return params
82
83 def detect_qemu():
84     f = open('/proc/cpuinfo')
85     x = ''.join(f.readlines())
86     f.close()
87     if 'QEMU' in x: return True
88     return False
89
90 def get_program_output(args):
91     p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
92     return p.communicate()[0]
93
94 def run_program(args):
95     subprocess.Popen(args, close_fds=True).wait()
96
97 def which(program):
98     def is_exe(fpath):
99         return os.path.exists(fpath) and os.access(fpath, os.X_OK)
100
101     fpath, fname = os.path.split(program)
102     if fpath:
103         if is_exe(program):
104             return program
105     else:
106         for path in os.environ["PATH"].split(os.pathsep):
107             exe_file = os.path.join(path, program)
108             if is_exe(exe_file):
109                 return exe_file
110
111     return None
112
113 XORG_CONF_HEADER = "# Automatically generated by grml-x."
114 def check_old_xorg_conf(filename, overwrite):
115     # True: no problem, we can create/overwrite the config file
116     # False: pre-existing config file, and we are not to overwrite it
117     if overwrite: return True
118     if not os.path.exists(filename): return True
119     try:
120         f = file(filename, 'r')
121         lines = f.readlines()
122         f.close()
123         return (not XORG_CONF_HEADER in lines)
124     except IOError:
125         return False
126
127 parser = OptionParser(usage="usage: %prog [options] [window-manager]")
128 parser.add_option("--nostart", action="store_false", dest="start_server", default=True,
129                 help="Don't start X server")
130 parser.add_option("--display", action="store", type="string", dest="display",
131                 help="Start X server on display DISPLAY")
132 parser.add_option("--hsync", action="store", type="string", dest="hsync",
133                 help="Force writing a HorizSync range")
134 parser.add_option("--vsync", action="store", type="string", dest="vsync",
135                 help="Force writing a VertRefresh range")
136 parser.add_option("--mode", action="store", type="string", dest="mode",
137                 help="Force a specific resolution")
138 parser.add_option("--module", action="store", type="string", dest="module",
139                 help="Force driver MODULE instead of Xorg autodetection")
140 parser.add_option("-o", action="store", type="string", dest="xorg_conf", default="/etc/X11/xorg.conf",
141                 help="Specify alternate xorg.conf file [default: %default]")
142 parser.add_option("-f", "--force", action="store_true", dest="overwrite", default=False,
143                 help="Overwrite xorg.conf if it exists [default: %default]")
144
145 def main():
146     (options, args) = parser.parse_args()
147     bootparams = build_bootparams()
148
149     if os.getuid() == 0 and options.start_server:
150         print "W: running as root is unsupported and may not work."
151         time.sleep(1)
152
153     if not check_old_xorg_conf(options.xorg_conf, options.overwrite):
154         print "E: Not overwriting existing %r without --force." % options.xorg_conf
155         print "I: If you previously ran grml-x, use startx /usr/bin/x-window-manager"
156         return 1
157
158     if 'xmode' in bootparams and not options.mode: options.mode = bootparams['xmode']
159     if 'xmodule' in bootparams and not options.module: options.module = bootparams['xmodule']
160
161     force_monitor = False
162     # cirrus driver for QEMU doesn't do 1024x768 without HorizSync set
163     if detect_qemu(): force_monitor = True
164
165     monitor = get_monitor_section(options, force_monitor)
166     device = get_device_section(options)
167
168     # build Screen section ourselves
169     d = {}
170     if monitor: d['Monitor'] = monitor.identifer
171     if device: d['Device'] = device.identifer
172     screen = Section('Screen', 'Screen0', d)
173     if options.mode:
174         d['DefaultColorDepth'] = 16
175         for depth in [8, 15, 16, 24, 32]:
176             screen.subsect += "SubSection \"Display\"\n\tDepth %d\n\tModes \"%s\"\t\nEndSubSection\n" % (depth, options.mode)
177
178     xinitrc = '~/.xinitrc'
179     if 'XINITRC' in os.environ: xinitrc = os.environ['XINITRC']
180     xinitrc = os.path.expanduser(xinitrc)
181
182     window_manager = 'x-window-manager'
183     if len(args) == 1: window_manager = args[0]
184     window_manager_path = which(window_manager)
185     if not window_manager_path:
186         print "E: Cannot find window manager %r, aborting." % window_manager
187         return 2
188
189     wm_exec = "exec %s\n" % window_manager_path
190     if not os.path.exists(xinitrc):
191         f = open(xinitrc, 'w')
192         f.write("#!/bin/sh\n")
193         f.write(wm_exec)
194         f.close()
195     else:
196         f = open(xinitrc, 'r')
197         lines = f.readlines()
198         f.close()
199         f = open(xinitrc, 'w')
200         for line in lines:
201             if line.strip().startswith('exec '): line = wm_exec
202             f.write(line)
203         os.fchmod(f.fileno(), 0750)
204         f.close()
205
206     # write new config
207     if monitor or device or len(screen.data) > 0 or screen.subsect != '':
208         try:
209             f = tempfile.NamedTemporaryFile(delete=False)
210             f.write(XORG_CONF_HEADER + "\n")
211             f.write("# DO NOT MODIFY, YOUR CHANGES WILL BE LOST - OR REMOVE ALL HEADER LINES\n")
212             f.write("# See man xorg.conf or /etc/X11/xorg.conf.example for more\n")
213             if monitor: f.write(str(monitor))
214             if device: f.write(str(device))
215             f.write(str(screen))
216             f.flush()
217             os.fchmod(f.fileno(), 0644)
218             run_program(['sudo', 'mv', '-f', f.name, options.xorg_conf])
219         finally:
220             f.close()
221
222     if options.start_server:
223         startx = ['startx', xinitrc, '--']
224         if options.display: startx.append(':' + options.display)
225         print "Starting X: %r" % startx
226         run_program(startx)
227
228     return 0
229
230 if __name__ == '__main__':
231     rc = 1
232     try:
233         rc = main()
234     except Exception:
235         print "E: Exception: ",
236         traceback.print_exc()
237     sys.exit(1)
238