View difference between Paste ID: kWhaY53e and 66u0gQP5
SHOW: | | - or go back to the newest paste.
1
#!/usr/bin/env ruby
2
3
require 'singleton'
4
5
class Trigger
6
  SLEEP_TIME = 120
7
8
  def initialize
9
    @seconds = 0
10
  end
11
12
  def tic_tac
13
    mouse = Mouse.instance
14
    mouse.read
15
    lcd = Lcd.instance
16
    lcd.off if @seconds >= SLEEP_TIME && lcd.on?
17
18
    if mouse.x_pos > 1900
19
      @seconds = 0
20
      lcd.on if lcd.off? if mouse.x_delta >= -10 && mouse.x_delta <= 10 && mouse.y_delta >= -10 && mouse.y_delta <= 10
21
    else
22
      @seconds += 1
23
    end
24
  end
25
end
26
27
class Lcd
28
  include Singleton
29
30
  def on
31
    return false if on?
32
    @state = :on
33
    `xrandr --output DVI-I-1 --auto --pos 1920x0`
34
  end
35
36
  def off
37
    return false if off?
38
    @state = :off
39
    `xrandr --output DVI-I-1 --off`
40
  end
41
42
  def on?
43
    state == :on
44
  end
45
46
  def off?
47
    state == :off
48
  end
49
50
  private
51
52
  def state
53
    @state = :on unless defined?(@state)
54
    @state
55
  end
56
end
57
58
class Mouse
59
  include Singleton
60
61
  def read
62
    save_last_position
63
    get_position
64
    true
65
  end
66
67
  def x_pos
68
    @x_pos.to_i
69
  end
70
71
  def x_delta
72
    @last_x_pos.to_i - @x_pos.to_i
73
  end
74
75
  def y_delta
76
    @last_y_pos.to_i - @y_pos.to_i
77
  end
78
79
  private
80
81
  def save_last_position
82
    @last_x_pos = @x_pos
83
    @last_y_pos = @y_pos
84
  end
85
86
  def get_position
87
    xdotool_output = `xdotool getmouselocation`.to_s
88
    @x_pos = xdotool_output[/x:(\d+)/, 1].to_i
89
    @y_pos = xdotool_output[/y:(\d+)/, 1].to_i
90
  end
91
end
92
93
trigger = Trigger.new
94
95
while true do
96
  trigger.tic_tac
97
  sleep 2
98
end