Class: Rbindkeys::Observer
- Inherits:
-
Object
- Object
- Rbindkeys::Observer
- Includes:
- Revdev
- Defined in:
- lib/rbindkeys/observer.rb,
lib/rbindkeys.rb
Overview
main event loop class
Constant Summary collapse
- LOG =
LogUtils.get_logger name
- VIRTUAL_DEVICE_NAME =
"rbindkyes"
- DEFAULT_TIMEOUT =
0.5
Instance Attribute Summary collapse
-
#config_file ⇒ Object
readonly
Returns the value of attribute config_file.
-
#device ⇒ Object
readonly
Returns the value of attribute device.
-
#event_handler ⇒ Object
readonly
Returns the value of attribute event_handler.
-
#virtual ⇒ Object
readonly
Returns the value of attribute virtual.
Instance Method Summary collapse
- #destroy(*args) ⇒ Object
- #handle_device_event ⇒ Object
- #handle_x_event ⇒ Object
-
#initialize(config_name, device_location) ⇒ Observer
constructor
A new instance of Observer.
- #select_ios ⇒ Object
-
#start ⇒ Object
main loop.
Constructor Details
#initialize(config_name, device_location) ⇒ Observer
Returns a new instance of Observer.
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rbindkeys/observer.rb', line 20 def initialize config_name, device_location @device = Device.new device_location @virtual = VirtualDevice.new operator = DeviceOperator.new @device, @virtual @event_handler = KeyEventHandler.new operator @config_file = config_name @started = false @window_observer = ActiveWindowX::EventListener.new @timeout = DEFAULT_TIMEOUT end |
Instance Attribute Details
#config_file ⇒ Object (readonly)
Returns the value of attribute config_file.
18 19 20 |
# File 'lib/rbindkeys/observer.rb', line 18 def config_file @config_file end |
#device ⇒ Object (readonly)
Returns the value of attribute device.
15 16 17 |
# File 'lib/rbindkeys/observer.rb', line 15 def device @device end |
#event_handler ⇒ Object (readonly)
Returns the value of attribute event_handler.
17 18 19 |
# File 'lib/rbindkeys/observer.rb', line 17 def event_handler @event_handler end |
#virtual ⇒ Object (readonly)
Returns the value of attribute virtual.
16 17 18 |
# File 'lib/rbindkeys/observer.rb', line 16 def virtual @virtual end |
Instance Method Details
#destroy(*args) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/rbindkeys/observer.rb', line 109 def destroy *args if not @started LOG.error 'did not start to observe' return end begin LOG.info "try @device.ungrab" @device.ungrab LOG.info "=> success" rescue => e LOG.error e end begin LOG.info "try @virtural.destroy" @virtual.destroy LOG.info "=> success" rescue => e LOG.error e end begin LOG.info "try @window_observer.destory" @window_observer.destroy LOG.info "=> success" rescue => e LOG.error e end exit true end |
#handle_device_event ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/rbindkeys/observer.rb', line 99 def handle_device_event event = @device.read_input_event if event.type != Revdev::EV_KEY @virtual.write_input_event event else @event_handler.handle event end end |
#handle_x_event ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rbindkeys/observer.rb', line 88 def handle_x_event event = @window_observer.listen_with_no_select return if event.nil? or event.window.nil? begin @event_handler.active_window_changed event.window rescue ActiveWindowX::Xlib::XErrorEvent => e LOG.error e end end |
#select_ios ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rbindkeys/observer.rb', line 75 def select_ios if @window_observer.pending_events_num != 0 [@window_observer.connection] else ios = select [@window_observer.connection, @device.file], nil, nil, @timeout if ios.nil? nil else ios.first end end end |
#start ⇒ Object
main loop
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 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rbindkeys/observer.rb', line 32 def start @device.grab @device.release_all_key @virtual.create VIRTUAL_DEVICE_NAME #, @device.device_id @event_handler.load_config @config_file @event_handler.bind_resolver.tree.each do |k,v| puts "#{k} => #{v.inspect}" end trap :INT, method(:destroy) trap :TERM, method(:destroy) active_window = @window_observer.active_window @event_handler.active_window_changed active_window if active_window # start main loop @started = true while true ios = select_ios if ios.nil? # select timeout # no op next end if LOG.debug? LOG.debug "" LOG.debug "select => #{ios.inspect}" end ios.each do |io| case io when @window_observer.connection then handle_x_event when @device.file then handle_device_event else LOG.error "unknown IO #{io.inspect}" end end end end |