#!/usr/bin/python

import sys
import subprocess
import random

def force_output():
	for output in outputs:
		for mode in modes:
			xrandr('--addmode %s %s' % (output, mode))

def xrandr(opts):
	args = '/usr/bin/xrandr ' + opts
	print '[%s]' % args,

	rc = subprocess.call(args.split(' '))
	print 'rc=%d' % rc

random.seed(42)

outputs = ('HDMI1', 'HDMI2') # , 'DP1', 'DP2', 'VGA1') # Intel Arrandale
# outputs = ('HDMI-0', 'LVDS', 'VGA-0') # Radeon
rotations = ('normal', 'left', 'right', 'inverted')
reflections = ('normal', 'x', 'y', 'xy')
positions = ('left-of', 'right-of', 'above', 'below', 'same-as')
modes = ('1920x1200', '1920x1080', '1600x1200', '1440x900', '1280x1024', '1152x864', '1024x768', '800x600', '720x576', '640x480', '720x400', 'off')

# force_output()

try:
	while True:
		output = random.choice(outputs)
		mode = random.choice(modes)
		if mode == 'off':
			mode_str = mode
		else:
			mode_str = 'mode %s' % mode
		
		rotation = random.choice(rotations)
		reflection = random.choice(reflections)

		xrandr('--output %s --%s --rotation %s --reflection %s' % (output, mode_str, rotation, reflection))

		output_sec = random.choice(outputs)
		position = random.choice(positions)
		xrandr('--output %s --%s %s' % (output, position, output_sec))
except KeyboardInterrupt:
	print '\n\npress enter to restore output, or Ctrl-C to abort...'

	sys.stdin.readline()

	for output in outputs:
		xrandr('--output %s --auto --rotation normal --reflection normal' % output)

