Class: ActiveWindowX::EventListener

Inherits:
Object
  • Object
show all
Defined in:
lib/active_window_x/event_listener.rb,
lib/active_window_x.rb

Overview

listen event changing active window

Defined Under Namespace

Classes: Event

Constant Summary collapse

DEFAULT_TIMEOUT =
0.5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, timeout = DEFAULT_TIMEOUT, &block) ⇒ EventListener

name : a display name. if you give nil, EventListener use a Default Display timeout : an intervel to check an interaction for the termination of the listening loop



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_window_x/event_listener.rb', line 21

def initialize name=nil, timeout=DEFAULT_TIMEOUT, &block
  @display = Display.new name
  @default_timeout = timeout

  @root = @display.root_window
  @aw_atom = Atom.new @display, '_NET_ACTIVE_WINDOW'
  @name_atom = Atom.new @display, 'WM_NAME'
  @delete_atom = Atom.new @display, 'WM_DELETE_WINDOW'
  @conn = @display.connection
  @active_window = @root.active_window

  @active_window.select_input Xlib::PropertyChangeMask if @active_window
  @root.select_input Xlib::PropertyChangeMask

  if block_given?
    start @default_timeout, &block
  end
end

Instance Attribute Details

#active_windowObject (readonly)

current active window



11
12
13
# File 'lib/active_window_x/event_listener.rb', line 11

def active_window
  @active_window
end

#continueObject

true if #start loop continued false if #start loop did not continue nil if #start loop was not started set false if you want to terminate #start loop when next timeout or event receiving



17
18
19
# File 'lib/active_window_x/event_listener.rb', line 17

def continue
  @continue
end

Instance Method Details

#connectionObject



119
120
121
# File 'lib/active_window_x/event_listener.rb', line 119

def connection
  @display.connection
end

#destroyObject



64
65
66
# File 'lib/active_window_x/event_listener.rb', line 64

def destroy
  @display.close
end

#listen(timeout = nil) ⇒ Object

receive a event

return value:

a ActiveWindowX::EventListener::Event if an event was send within _timeout_ sec
nil if timeout


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/active_window_x/event_listener.rb', line 73

def listen timeout=nil
  if @display.pending == 0 and
      select([@conn], [], [], timeout) == nil
    # ope on timeout
    return nil
  end

  event = listen_with_no_select

  event
end

#listen_with_no_selectObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/active_window_x/event_listener.rb', line 85

def listen_with_no_select
  type = nil
  active_window = nil

  event = @display.next_event
  if event.class != PropertyEvent
    return nil
  end
  if event.atom == @aw_atom
    type = :active_window
    active_window = @root.active_window
  elsif event.atom == @name_atom
    type = :title
    active_window = event.window
  end

  if type == :active_window and @active_window != active_window
    @active_window.select_input(Xlib::NoEventMask) if @active_window
    @active_window = active_window
    @active_window.select_input(Xlib::PropertyChangeMask) if @active_window
  end

  event = Event.new type, active_window
  if window_closed?(event.window)
    event.window = @root.active_window
  end

  event
end

#pending_events_numObject



115
116
117
# File 'lib/active_window_x/event_listener.rb', line 115

def pending_events_num
  @display.pending
end

#start(timeout = @default_timeout) ⇒ Object

event listener loop

ActiveWindowX::EventListener.new.start do |e|

puts e.type, e.window.id

end



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/active_window_x/event_listener.rb', line 45

def start timeout=@default_timeout

  if not block_given?
    raise ArgumentError, 'expect to give a block'
  end

  @continue = true
  begin
    while @continue
      event = listen timeout
      next if not event

      yield event if event.type
    end
  ensure
    destroy
  end
end

#window_closed?(w) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
127
128
129
130
131
# File 'lib/active_window_x/event_listener.rb', line 123

def window_closed? w
  return false if w.nil?
  begin
    w.prop_raw 'WM_STATE'
    false
  rescue Xlib::XErrorEvent
    true
  end
end