2 # -*- coding: utf-8 -*-
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 ###############################################################################
18 from optparse import OptionParser
21 class Section(object):
22 def __init__(self, name, identifier, data):
24 self.identifer = identifier
29 s = 'Section "%s"\n\tIdentifier "%s"\n' % (self.name, self.identifer)
32 if isinstance(v, list):
34 elif not isinstance(v, str): # int, others
36 elif "-" in v: # sync range
40 s += "\t%s %s\n" % (k, v)
46 def get_monitor_section(options, force):
47 if not options.hsync and not options.vsync and not force:
50 d["HorizSync"] = options.hsync or "28.0 - 96.0"
51 d["VertRefresh"] = options.vsync or "50.0 - 60.0"
52 return Section("Monitor", "Monitor0", d)
55 def get_device_section(options):
56 if not options.module:
59 d["Driver"] = options.module
60 d["VendorName"] = "All"
61 d["BoardName"] = "All"
62 return Section("Device", "Card0", d)
65 def build_bootparams():
68 def walk_bootparams_path(p):
70 if not os.path.exists(p):
72 for root, dirs, files in os.walk(p):
74 f = open(os.path.join(root, name))
75 lines.extend(f.readlines())
78 print("W: Error while getting bootparams from %s" % p)
80 f = open("/proc/cmdline")
81 lines.append(f.readline())
83 walk_bootparams_path("/lib/live/mount/medium/bootparams")
84 walk_bootparams_path("/run/live/medium/bootparams")
86 for p in " ".join(lines).split(" "):
88 (k, v) = p.split("=", 1)
96 f = open("/proc/cpuinfo")
97 x = "".join(f.readlines())
104 def get_program_output(args):
105 p = subprocess.Popen(
106 args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True
108 return p.communicate()[0]
111 def run_program(args):
112 subprocess.Popen(args, close_fds=True).wait()
117 return os.path.exists(fpath) and os.access(fpath, os.X_OK)
119 fpath, fname = os.path.split(program)
124 for path in os.environ["PATH"].split(os.pathsep):
125 exe_file = os.path.join(path, program)
132 XORG_CONF_HEADER = "# Automatically generated by grml-x.\n"
135 def check_old_xorg_conf(filename, overwrite):
136 # True: no problem, we can create/overwrite the config file
137 # False: pre-existing config file, and we are not to overwrite it
140 if not os.path.exists(filename):
143 f = open(filename, "r")
144 lines = f.readlines()
146 return XORG_CONF_HEADER not in lines
151 parser = OptionParser(usage="usage: %prog [options] [window-manager]")
154 action="store_false",
157 help="Don't start X server",
164 help="Start X server on display DISPLAY",
171 help="Force writing a HorizSync range",
178 help="Force writing a VertRefresh range",
185 help="Force a specific resolution",
192 help="Force driver MODULE instead of Xorg autodetection",
199 default="/etc/X11/xorg.conf",
200 help="Specify alternate xorg.conf file [default: %default]",
208 help="Overwrite xorg.conf if it exists [default: %default]",
213 (options, args) = parser.parse_args()
214 bootparams = build_bootparams()
216 if os.getuid() == 0 and options.start_server:
217 print("W: running as root is unsupported and may not work.")
220 if not check_old_xorg_conf(options.xorg_conf, options.overwrite):
221 print("E: Not overwriting existing %r without --force." % options.xorg_conf)
222 print("I: If you previously ran grml-x, use startx /usr/bin/x-window-manager")
225 if "xmode" in bootparams and not options.mode:
226 options.mode = bootparams["xmode"]
227 if "xmodule" in bootparams and not options.module:
228 options.module = bootparams["xmodule"]
230 force_monitor = False
231 # cirrus driver for QEMU doesn't do 1024x768 without HorizSync set
235 monitor = get_monitor_section(options, force_monitor)
236 device = get_device_section(options)
238 # build Screen section ourselves
241 d["Monitor"] = monitor.identifer
243 d["Device"] = device.identifer
244 screen = Section("Screen", "Screen0", d)
246 d["DefaultColorDepth"] = 16
247 for depth in [8, 15, 16, 24, 32]:
249 'SubSection "Display"\n\tDepth %d\n\tModes "%s"\t\nEndSubSection\n'
250 % (depth, options.mode)
253 xinitrc = "~/.xinitrc"
254 if "XINITRC" in os.environ:
255 xinitrc = os.environ["XINITRC"]
256 xinitrc = os.path.expanduser(xinitrc)
258 window_manager = "x-window-manager"
260 window_manager = args[0]
261 window_manager_path = which(window_manager)
262 if not window_manager_path:
263 print("E: Cannot find window manager %r, aborting." % window_manager)
266 wm_exec = "exec %s\n" % window_manager_path
267 if not os.path.exists(xinitrc):
268 f = open(xinitrc, "w")
269 f.write("#!/bin/sh\n")
273 f = open(xinitrc, "r")
274 lines = f.readlines()
276 f = open(xinitrc, "w")
278 if line.strip().startswith("exec "):
281 os.fchmod(f.fileno(), 0o750)
285 if monitor or device or len(screen.data) > 0 or screen.subsect != "":
287 f = tempfile.NamedTemporaryFile(mode="w+", delete=False)
288 f.write(XORG_CONF_HEADER)
290 "# DO NOT MODIFY, YOUR CHANGES WILL BE LOST - OR REMOVE ALL HEADER LINES\n"
292 f.write("# See man xorg.conf or /etc/X11/xorg.conf.example for more\n")
294 f.write(str(monitor))
299 os.fchmod(f.fileno(), 0o644)
300 run_program(["sudo", "mv", "-f", f.name, options.xorg_conf])
304 if options.start_server:
305 startx = ["startx", xinitrc, "--"]
307 startx.append(":" + options.display)
308 print("Starting X: %r" % startx)
314 if __name__ == "__main__":
319 print("E: Exception: ", end=" ")
320 traceback.print_exc()