#! /usr/bin/perl # # 液晶パネルと外部モニタの出力切り替えをローテートするスクリプト # copyright (c) 2007/10/17 by HIRAMOTO Kouji # # 実行される度に「液晶のみ→液晶&外部→外部のみ→液晶のみ」と切り替えます。 use strict; my $xrandr_command = "/usr/bin/xrandr"; my %Connect = (); my %View = (); my %Next = (); parse_current_mode(); select_next_mode(); change_mode(); sub parse_current_mode { my $fh; open($fh, "$xrandr_command |") or die "$!: $xrandr_command"; my $display = ""; while (<$fh>) { if (/^(VGA|LVDS)\s(\S+)/) { $display = $1; if ($2 eq "connected") { $Connect{$display} = 1; } else { $Connect{$display} = 0; } } elsif (/^\s+\d+x\d+\s+[\d\.]+(.)/) { print "$display $1\n"; if ($1 eq "*") { $View{$display} = 1; } } } close($fh) or die "$!: $xrandr_command"; } sub select_next_mode { # 液晶パネルのみ接続されている if ($Connect{LVDS} and not $Connect{VGA}) { print "now connect LVDS only.\n"; $Next{LVDS} = 1; $Next{VGA} = 0; } # 外部モニタのみ接続されている (あるのか?) elsif ($Connect{LVDS} and not $Connect{VGA}) { print "now connect VGA only.\n"; $Next{LVDS} = 0; $Next{VGA} = 1; } # どっちも繋がってない (???) elsif (not $Connect{LVDS} and not $Connect{VGA}) { print "both disconnect!\n"; $Next{LVDS} = 1; $Next{VGA} = 1; } # 両方接続されている else { print "both connect.\n"; # 今は液晶パネルのみ表示 → 次は両方 if ($View{LVDS} and not $View{VGA}) { print "now view LVDS only.\n"; $Next{LVDS} = 1; $Next{VGA} = 1; } # 今は両方表示 → 次は外部モニタのみ elsif ($View{LVDS} and $View{VGA}) { print "now view both.\n"; $Next{LVDS} = 0; $Next{VGA} = 1; } # 今は外部モニタのみ → 次は液晶パネルのみ elsif (not $View{LVDS} and $View{VGA}) { print "now view VGA only.\n"; $Next{LVDS} = 1; $Next{VGA} = 0; } # どっちも表示されてない (???) → 両方に表示してみる else { print "now no view anything!\n"; $Next{LVDS} = 1; $Next{VGA} = 1; } } } sub change_mode { my $option = ""; if ($Next{LVDS}) { $option .= " --output LVDS --mode 1366x768 "; } else { $option .= " --output LVDS --off "; } if ($Next{VGA}) { $option .= " --output VGA --mode 1280x1024 "; } else { $option .= " --output VGA --off "; } system("$xrandr_command $option"); }