Created
June 12, 2014 05:35
-
-
Save sgnix/548a7d95876afdc49ab1 to your computer and use it in GitHub Desktop.
Monitor switcher using xrandr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
my $xrandr = '/usr/bin/xrandr'; | |
my $LVDS = 'LVDS-1'; | |
my @a = `$xrandr -q`; | |
my %connected = (); | |
while (@a) { | |
my $line = shift @a; | |
if ( $line =~ /(\w+\-\d+) connected/ ) { | |
my $o = $1; | |
$connected{$o} = 0; | |
while ( @a && $a[0] =~ s/^\s+// ) { | |
if ( shift(@a) =~ /\*/ ) { | |
$connected{$o} = 1; | |
last; | |
} | |
} | |
} | |
} | |
# This one is always connected (it's the laptop screen) | |
my $lvds_on = delete $connected{$LVDS}; | |
# We only take the first other connected | |
my $OTHER = %connected ? ( keys(%connected) )[0] : undef; | |
if ( !$OTHER ) { | |
# Laptop only, no other output attached | |
unless ($lvds_on) { | |
`$xrandr --auto`; | |
} | |
} | |
else { | |
if ($lvds_on) { | |
# LVDS is on, then turn it off and leave the other on | |
`$xrandr --output $OTHER --auto --output $LVDS --off`; | |
} | |
else { | |
# LVDS is off, then turn it on together with the other | |
`$xrandr --output $LVDS --auto --output $OTHER --right-of $LVDS --primary`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment