import sys
import subprocess
import re
+import datetime
+import time
dry_run = False
+class record:
+ def open(self, filename):
+ self.of = open(filename, 'a')
+
+ def out(self, line):
+ data = '%s\n' % line
+ self.of.write(data)
+ sys.stdout.write(data)
+
+ def outTime(self, line):
+ tstamp = datetime.datetime.fromtimestamp(time.time())
+ data = '%s %s\n' % (tstamp.strftime('%Y-%m-%d %H:%M:%S'), line)
+ self.of.write(data)
+ sys.stdout.write(data)
+
+ def foutTime(self, line):
+ tstamp = datetime.datetime.fromtimestamp(time.time())
+ data = '%s %s\n' % (tstamp.strftime('%Y-%m-%d %H:%M:%S'), line)
+ self.of.write(data)
+
+ def close(self):
+ self.of.close()
+
+out = record()
+
class OutputInfo():
def __init__(self):
self.clear()
if output['posx'] == 0:
command += ['--primary']
if not dry_run:
- print 'xrandr_on:', command
+ out.out(' '.join(command))
subprocess.call(command)
else:
- print '(dry-run) xrandr_on:', command
+ out.out('(dry-run) %s' % ' '.join(command))
def xrandr_off(output):
command = ['xrandr', '--output', output['name'], '--off']
if not dry_run:
subprocess.call(command)
- print 'xrandr_off:', command
+ out.out(' '.join(command))
else:
- print '(dry-run) xrandr_off:', command
+ out.out('(dry-run) %s' % ' '.join(command))
def output_by_name(outputs, name):
return None
-def print_outputs(header, outputs):
- print '%s:' % header
- for output in outputs:
- print_output(output)
- print
-
-
def all_done(outputs):
for output in outputs:
if not output['done']:
if __name__ == '__main__':
- def print_output(output):
- print 'name:%s connected:%r enabled:%r width:%d height:%d pos:%dx%d' % (
- output['name'], output['connected'], output['enabled'],
- output['width'], output['height'], output['posx'],
- output['posy'])
def main(argv):
global dry_run
if len(argv) >= 2 and argv[1] == '--dry-run':
dry_run = True
+ global out
+ from os.path import expanduser, join
+ path = join(expanduser("~"), 'dispcfg.log')
+ out.open(path)
+ out.outTime('started')
+
outputs = parse()
- print_outputs('Outputs', outputs)
+
+ for output in outputs:
+ s = '%s' % output['name']
+ if output['connected']:
+ s += ' connected'
+ else:
+ s += ' disconnected'
+ if output['enabled']:
+ s += ' enabled'
+ t = '%dx%d' % (output['width'], output['height'])
+ if t != '-1x-1':
+ s += ' size:%s' % t
+ t = '%dx%d' % (output['posx'], output['posy'])
+ if t != '-1x-1':
+ s += ' pos:%s' % t
+ out.out(s)
# Find the first two connected devices, in reverse list order. On the
# Thinkpad X201, the reversed list order is the preferred order of
# LVDS panel used as a last resort. Also find new posx values.
use_count = 0
posx = 0
+ use = []
for output in reversed(outputs):
if output['connected']:
- print 'use', output['name']
+ use.append(output['name'])
output['use'] = True
output['posx'] = posx
posx += output['width']
use_count += 1
if use_count >= 2:
break
+ out.out('use %s' % ' '.join(use))
# How many outputs are currently enabled?
enabled_count = 0
if not dry_run:
subprocess.call('/usr/local/bin/setctrl')
+ out.outTime('completed')
+
main(sys.argv)