Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require 'singleton'
- class Trigger
- SLEEP_TIME = 120
- def initialize
- @seconds = 0
- end
- def tic_tac
- mouse = Mouse.instance
- mouse.read
- lcd = Lcd.instance
- lcd.off if @seconds >= SLEEP_TIME && lcd.on?
- if mouse.x_pos > 1900
- @seconds = 0
- lcd.on if lcd.off? if mouse.x_delta >= -10 && mouse.x_delta <= 10 && mouse.y_delta >= -10 && mouse.y_delta <= 10
- else
- @seconds += 1
- end
- end
- end
- class Lcd
- include Singleton
- def on
- return false if on?
- @state = :on
- `xrandr --output DVI-I-1 --auto --pos 1920x0`
- end
- def off
- return false if off?
- @state = :off
- `xrandr --output DVI-I-1 --off`
- end
- def on?
- state == :on
- end
- def off?
- state == :off
- end
- private
- def state
- @state = :on unless defined?(@state)
- @state
- end
- end
- class Mouse
- include Singleton
- def read
- save_last_position
- get_position
- true
- end
- def x_pos
- @x_pos.to_i
- end
- def x_delta
- @last_x_pos.to_i - @x_pos.to_i
- end
- def y_delta
- @last_y_pos.to_i - @y_pos.to_i
- end
- private
- def save_last_position
- @last_x_pos = @x_pos
- @last_y_pos = @y_pos
- end
- def get_position
- xdotool_output = `xdotool getmouselocation`.to_s
- @x_pos = xdotool_output[/x:(\d+)/, 1].to_i
- @y_pos = xdotool_output[/y:(\d+)/, 1].to_i
- end
- end
- trigger = Trigger.new
- while true do
- trigger.tic_tac
- sleep 2
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement