/
idle_pc.rb
executable file
·53 lines (38 loc) · 1.18 KB
/
idle_pc.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'rubygems'
require 'nestful'
require 'Win32API'
require 'trollop'
DEBUG = true
INTERVAL = 1
SLACK = 10
KEYEVENTF_KEYDOWN = 0
KEYEVENTF_KEYUP = 2
KEY_RALT = 165
def main
opts = Trollop::options do
opt :host, "idle server host", :type => :string, :required => true
opt :port, "idle server port", :type => :int, :default => 12389
end
url = "http://#{opts[:host]}:#{opts[:port]}/"
idle = 0 # there must be a more idiomatic way to do this
loop do
puts Time.now if DEBUG
idle = Nestful.get(url).to_i
puts "Idle: #{idle}" if DEBUG
# This could be smarter... trouble is we can't assume that the
# mac's clock is in sync with ours, or that its sleep interval
# is related to ours
if idle < SLACK
fake_activity
end
sleep INTERVAL
end
end
def fake_activity
puts "faking activity..." if DEBUG
# from http://bitbucket.org/undees/idgtr-code/src/tip/early%5Fsuccess/windows%5Fbasics.rb
# and http://stackoverflow.com/questions/1175803/how-to-automate-a-keystroke-using-win32-and-ruby
keybd_event = Win32API.new 'user32', 'keybd_event', ['I', 'I', 'L', 'L'], 'V'
keybd_event.call KEY_RALT, 0, KEYEVENTF_KEYUP, 0
end
main