Class: Ukey::UsbWatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ukey/usb_watcher.rb

Overview

Watches usb devices and locks screen if removed

Instance Method Summary collapse

Constructor Details

#initialize(device_name: '', interval: 5) ⇒ UsbWatcher

Returns a new instance of UsbWatcher.



8
9
10
11
12
# File 'lib/ukey/usb_watcher.rb', line 8

def initialize(device_name: '', interval: 5)
  @device_name = device_name
  @interval = interval
  @usb = LIBUSB::Context.new
end

Instance Method Details

#list_devicesObject



27
28
29
# File 'lib/ukey/usb_watcher.rb', line 27

def list_devices
  usb.devices.map(&:product)
end

#watchObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ukey/usb_watcher.rb', line 14

def watch
  loop do
    if screen_locked?
      sleep(interval * 2)
    else
      lock_screen if device_removed?
      sleep(interval)
    end
  end
rescue SystemExit, Interrupt, KeyboardInterrupt
  puts "\nWatcher terminated."
end