Skip to content

Commit

Permalink
Add display switcher script for linux
Browse files Browse the repository at this point in the history
  • Loading branch information
ka2n committed Jul 4, 2018
1 parent 14c0aa0 commit dc25ba6
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions bin-linux/switch-display-thinkpad.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/bin/env ruby

LVDS = 'eDP1'
DP = 'DP1'
HDMI = 'HDMI1'

ON = { LVDS: false, DP: false, HDMI: false }
CONN = { LVDS: false, DP: false, HDMI: false }

# Getting state

def updateState
xo = `xrandr`
CONN[:LVDS] = xo.match? /^#{LVDS} connected/
CONN[:DP] = xo.match? /^#{DP} connected/
CONN[:HDMI] = xo.match? /^#{HDMI} connected/
xo = `xrandr --listmonitors`
ON[:LVDS] = xo.match? /\s#{LVDS}/
ON[:DP] = xo.match? /\s#{DP}/
ON[:HDMI] = xo.match? /\s#{HDMI}/
end

def notify(msg = '')
`notify-send 'Display' '#{msg}'`
end

def switchByState
# 以下をイテレーション
# 1. メインのみ
# 2. メインとサブ
# 3. サブのみ

# サブが未接続ならメインのみに変更
case
when (CONN[:HDMI] || CONN[:DP]) && ON[:LVDS] && !ON[:HDMI] && !ON[:DP]
# メインとサブにする
if CONN[:HDMI] && CONN[:DP]
notify 'Use All(Main + HDMI + DP)'
`xrandr --output #{HDMI} --auto --right-of #{DP}`
`xrandr --output #{LVDS} --auto --below #{HDMI}`
else
if CONN[:HDMI]
notify 'Use HDMI + Main'
`xrandr --output #{HDMI} --auto --above #{LVDS}`
elsif CONN[:DP]
notify 'Use DP + Main'
`xrandr --output #{DP} --auto --above #{LVDS}`
end
end
when ON[:LVDS] && (ON[:HDMI] || ON[:DP])
# サブのみにする
`xrandr --output #{LVDS} --off`

if CONN[:HDMI] && CONN[:DP]
notify 'Use only Sub display(HDMI + DP)'
`xrandr --output #{HDMI} --auto --right-of #{DP}`
else
notify 'Use only Sub display(HDMI or DP)'
`xrandr --output #{HDMI} --auto` if CONN[:HDMI]
`xrandr --output #{DP} --auto` if CONN[:DP]
end
else
# メインのみにする
notify 'Use only main display'
`xrandr --output #{LVDS} --auto`
`xrandr --output #{HDMI} --off`
`xrandr --output #{DP} --off`
end
end

updateState
switchByState

0 comments on commit dc25ba6

Please sign in to comment.