if self.complete():
return {'name':self.name, 'enabled':self.enabled,
'connected':self.connected, 'width':self.width,
- 'height':self.height, 'posx':self.posx, 'posy':self.posy}
+ 'height':self.height, 'posx':self.posx, 'posy':self.posy,
+ 'use':False, 'done':False}
else:
raise ValueError
print_output(output)
print
+ def all_done(outputs):
+ for output in outputs:
+ if not output['done']:
+ return False
+ return True
+
+ def disable_an_entry(outputs):
+ for output in outputs:
+ if not output['done'] and not output['use'] and output['enabled']:
+ xrandr_off(output)
+ output['done'] = True
+ return 1
+ return 0
+
+ def enable_an_entry(outputs):
+ enables = 0
+ for output in reversed(outputs):
+ if not output['done'] and output['use']:
+ xrandr_on(output)
+ output['done'] = True
+ return 1
+ return 0
+
def main(argv):
global dry_run
if len(argv) >= 2 and argv[1] == '--dry-run':
dry_run = True
outputs = parse()
- #outputs[1]['connected'] = False
- #outputs[2]['connected'] = False
- print_outputs('Raw outputs', outputs)
+ print_outputs('Outputs', outputs)
- preferred_order = list(reversed([output['name'] for output in outputs]))
- print 'preferred order:', preferred_order
- print
-
- tmp = outputs
- outputs = []
- for name in preferred_order:
- output = output_by_name(tmp, name)
- if output:
- outputs.append(output)
- print_outputs('Sorted outputs', outputs)
-
- connected_outputs = [output for output in outputs if output['connected']]
- print_outputs('Connected outputs', connected_outputs)
-
- enabled_outputs = [output for output in outputs if output['enabled']]
- print_outputs('Enabled outputs', enabled_outputs)
-
- # The outputs to use will be a subset of connected outputs
- touse_outputs = connected_outputs
-
- # If there are more then 2 connected outputs, start removing them by
- # order of desirability. The LCD panel is least desirable under the
- # assumption that having more than two means the LCD panel shouldn't be
- # used.
- while len(touse_outputs) > 2:
- print 'trim', touse_outputs[-1]['name']
- touse_outputs = touse_outputs[:-1]
- print_outputs('To use outputs', touse_outputs)
-
- # Determine which outputs to turn off, and which to turn on
- off_outputs = [output for output in enabled_outputs if output not in
- touse_outputs]
- print_outputs('Turn these off', off_outputs)
- on_outputs = [output for output in touse_outputs if output not in
- enabled_outputs]
- print_outputs('Turn these on', on_outputs)
-
- if len(enabled_outputs) < 1 or len(enabled_outputs) > 2 or \
- len(touse_outputs) < 1 or len(touse_outputs) > 2:
- print 'Something is wrong; doing nothing'
- return
-
- # Compute positions for final outputs
+ # Find the first two connected devices, in reverse list order. On the
+ # Thinkpad X201, the reversed list order is the preferred order of
+ # precedence, highest quality external display first, down to internal
+ # LVDS panel used as a last resort. Also find new posx values.
+ use_count = 0
posx = 0
- for output in touse_outputs:
- output['posx'] = posx
- output['posy'] = 0
- posx += output['width']
- print_outputs('touse, with positions', touse_outputs)
-
- if touse_outputs != enabled_outputs:
- # Remove all enabled outputs save one
- while len(enabled_outputs) > 1:
- output = enabled_outputs[0]
- enabled_outputs = enabled_outputs[1:]
- xrandr_off(output)
+ for output in reversed(outputs):
+ if output['connected']:
+ print 'use', output['name']
+ output['use'] = True
+ output['posx'] = posx
+ posx += output['width']
+ use_count += 1
+ if use_count >= 2:
+ break
+
+ # How many outputs are currently enabled?
+ enabled_count = 0
+ for output in outputs:
+ if output['enabled']:
+ enabled_count += 1
- # Move the remaining output to a safe position
- last_old_output = enabled_outputs[0]
- tmp = last_old_output['posx']
- last_old_output['posx'] = posx # from above
- xrandr_on(last_old_output)
- last_old_output['posx'] = tmp
-
- # Add the first touse output
- first_new_output = touse_outputs[0]
- touse_outputs = touse_outputs[1:]
- xrandr_on(first_new_output)
-
- # If last old is not to be used, remove it
- if not output_by_name(touse_outputs, last_old_output['name']):
- xrandr_off(last_old_output)
-
- # Now add the remaining touse outputs
- for output in touse_outputs:
- xrandr_on(output)
+ # Mark outputs that receive no change as done
+ for output in outputs:
+ if not output['use'] and not output['enabled']:
+ # Nothing to do for this output, mark as done
+ output['done'] = True
+
+ while not all_done(outputs):
+ if enabled_count > 1:
+ enabled_count -= disable_an_entry(outputs)
+ if enabled_count < 2:
+ enabled_count += enable_an_entry(outputs)
main(sys.argv)