Class: Clicker::Program
- Inherits:
-
Object
- Object
- Clicker::Program
- Defined in:
- lib/clicker/program.rb
Instance Method Summary collapse
- #do_run ⇒ Object
-
#initialize ⇒ Program
constructor
A new instance of Program.
- #print_version ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize ⇒ Program
Returns a new instance of Program.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/clicker/program.rb', line 7 def initialize @mode = :normal OptionParser.new do |opt| opt.on('--start', 'start as a daemon') { @mode = :start} opt.on('--stop', 'stop the daemon') { @mode = :stop } opt.on('-v', '--version', 'print version information and exit') { print_version; exit 0 } opt.parse!(ARGV) end @lock = LaunchLock.new('clicker') end |
Instance Method Details
#do_run ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/clicker/program.rb', line 64 def do_run model = KeyboardSoundModel.new begin io = IO.popen("evtest /dev/input/by-path/platform-i8042-serio-0-event-kbd", 'r') rescue Errno::ENOENT raise 'evtest command not found.' end loop do IO.select([io], [], []) line = io.gets if line == nil break elsif line =~ /^Event: time \d+\.\d+, type 1 \(EV_KEY\), code (\d+) \(KEY_.*?\), value (\d+)$/ code, value = $1.to_i, $2.to_i case value when 0 model.deactivate_key(code) when 1 model.activate_key(code) when 2 model.repeat_key(code) end end model.update end rescue RuntimeError => e STDERR.puts "Error: #{e.to_s}" exit 1 rescue Interrupt end |
#print_version ⇒ Object
19 20 21 22 |
# File 'lib/clicker/program.rb', line 19 def print_version STDERR.puts "clicker #{Clicker::VERSION}" STDERR.puts "Copyright © 2016 Yoteichi" end |
#run ⇒ Object
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 54 55 56 57 58 59 60 61 62 |
# File 'lib/clicker/program.rb', line 24 def run case @mode when :normal success = @lock.try_lock do do_run end unless success STDERR.puts("Error: another instance of clicker is running. (PID #{@lock.owner})") exit 1 end when :start if @lock.locked? STDERR.puts("Error: another instance of clicker is running. (PID #{@lock.owner})") exit 1 else if fork == nil if fork == nil @lock.try_lock do do_run end # ここでは端末から切り離されているのでロックに失敗しても出来ることはない。 end end end when :stop if @lock.locked? owner = @lock.owner begin Process.kill("TERM", owner) STDERR.puts("SIGTERM has been sent to PID #{owner}.") rescue Errno::ESRCH STDERR.puts("Warning: Process (#{owner}) not found.") end else STDERR.puts("no instance is running.") end end end |