Class: XDo::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/x_do/window.rb

Overview

Wraps an xdolib Window pointer.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xdo, _window) ⇒ Window

Creates a wrapper for an X Window handle.

This constructor is called internally by XDo#find_windows and client code should not need to call it directly.

Args:

xdo:: the XDo wrapping the libxdo context used to get this Window
_window:: the X Window handle to be wrapped


171
172
173
174
175
# File 'lib/x_do/window.rb', line 171

def initialize(xdo, _window)
  @xdo = xdo
  @_xdo_pointer = xdo._pointer
  @_window = _window
end

Instance Attribute Details

#_windowObject

The underlying X Window handle.



181
182
183
# File 'lib/x_do/window.rb', line 181

def _window
  @_window
end

#xdoObject

The XDo context that produced the window.



178
179
180
# File 'lib/x_do/window.rb', line 178

def xdo
  @xdo
end

Instance Method Details

#==(other) ⇒ Object

:nodoc: underlying window handle should impact equality



184
185
186
# File 'lib/x_do/window.rb', line 184

def ==(other)
  other.kind_of?(XDo::Window) && @_window == other._window
end

#activateObject

Brings a window forward and gives it focus.



7
8
9
# File 'lib/x_do/window.rb', line 7

def activate
  XDo::FFILib.xdo_window_activate @_xdo_pointer, @_window
end

#click_mouse(button) ⇒ Object

Clicks a mouse button.



119
120
121
# File 'lib/x_do/window.rb', line 119

def click_mouse(button)
  XDo::FFILib.xdo_click @_xdo_pointer, @_window, button
end

#focusObject

Gives the input focus to a window



12
13
14
# File 'lib/x_do/window.rb', line 12

def focus
  XDo::FFILib.xdo_window_focus @_xdo_pointer, @_window
end

#hashObject

:nodoc: override hash to match ==



189
190
191
# File 'lib/x_do/window.rb', line 189

def hash
  _window.hash
end

#locationObject

x, y

array containing the window’s coordinates.



38
39
40
41
42
43
44
# File 'lib/x_do/window.rb', line 38

def location
  x_pointer = FFI::MemoryPointer.new :int, 1
  y_pointer = FFI::MemoryPointer.new :int, 1
  XDo::FFILib.xdo_get_window_location @_xdo_pointer, @_window, x_pointer,
                                      y_pointer, nil
  [x_pointer.read_int, y_pointer.read_int]
end

#move(x, y) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/x_do/window.rb', line 55

def move(x, y)
  move_raw x, y
  glitched_location = self.location
  x_decoration = glitched_location.first - x
  y_decoration = glitched_location.last - y
  move_raw x - x_decoration, y - y_decoration
end

#move_mouse(window_x, window_y) ⇒ Object

Moves the mouse in window coordinates.



106
107
108
109
110
# File 'lib/x_do/window.rb', line 106

def move_mouse(window_x, window_y)
  old_location = @xdo.mouse.location
  move_mouse_async window_x, window_y
  @xdo.mouse.wait_for_move_from old_location[0], old_location[1]
end

#move_mouse_async(window_x, window_y) ⇒ Object

Moves the mouse in window coordinates.



113
114
115
116
# File 'lib/x_do/window.rb', line 113

def move_mouse_async(window_x, window_y)
  XDo::FFILib.xdo_mousemove_relative_to_window @_xdo_pointer, @_window,
                                               window_x, window_y
end

#move_raw(x, y) ⇒ Object

Moves this window to a new position.

The position is given directly to X, and does not account for window decorations.



67
68
69
70
71
72
73
74
75
# File 'lib/x_do/window.rb', line 67

def move_raw(x, y)
  old_location = self.location
  return_value = move_raw_async x, y
  100.times do
    break unless self.location == old_location
    sleep 0.01
  end
  return_value
end

#move_raw_async(x, y) ⇒ Object

Asks X to move this window to a new position.



78
79
80
# File 'lib/x_do/window.rb', line 78

def move_raw_async(x, y)
  XDo::FFILib.xdo_window_move @_xdo_pointer, @_window, x, y
end

#nameObject

The name of the window.



27
28
29
30
31
32
33
34
35
# File 'lib/x_do/window.rb', line 27

def name
  name_ppointer = FFI::MemoryPointer.new :pointer, 1
  len_pointer = FFI::MemoryPointer.new :int, 1 
  name_type_pointer = FFI::MemoryPointer.new :int, 1 
  XDo::FFILib.xdo_get_window_name @_xdo_pointer, @_window, name_ppointer,
                                  len_pointer, name_type_pointer
  name_pointer = name_ppointer.read_pointer
  name_pointer.nil? ? nil : name_pointer.read_string(len_pointer.read_int)
end

#pidObject

The PID of the process owning the window.



22
23
24
# File 'lib/x_do/window.rb', line 22

def pid
  XDo::FFILib.xdo_window_get_pid @_xdo_pointer, @_window
end

#press_keysequence(keysequence, delay = 0.012) ⇒ Object

Presses a keysequence in this window.

Examples: “alt+Return”, “Alt_L+Tab”, “l”, “semicolon”



150
151
152
153
# File 'lib/x_do/window.rb', line 150

def press_keysequence(keysequence, delay = 0.012)
  XDo::FFILib.xdo_keysequence_down @_xdo_pointer, @_window, keysequence,
                                   (delay * 1_000_000).to_i
end

#press_mouse(button) ⇒ Object

Presses a mouse button.



124
125
126
# File 'lib/x_do/window.rb', line 124

def press_mouse(button)
  XDo::FFILib.xdo_mousedown @_xdo_pointer, @_window, button
end

#raiseObject

Moves the window at the top of the stack, making it visible.



17
18
19
# File 'lib/x_do/window.rb', line 17

def raise
  XDo::FFILib.xdo_window_raise @_xdo_pointer, @_window
end

#release_keysequence(keysequence, delay = 0.012) ⇒ Object

Releases a keysequence in this window.

Examples: “alt+Return”, “Alt_L+Tab”, “l”, “semicolon”



158
159
160
161
# File 'lib/x_do/window.rb', line 158

def release_keysequence(keysequence, delay = 0.012)
  XDo::FFILib.xdo_keysequence_up @_xdo_pointer, @_window, keysequence,
                                 (delay * 1_000_000).to_i
end

#release_mouse(button) ⇒ Object

Releases a mouse button.



129
130
131
# File 'lib/x_do/window.rb', line 129

def release_mouse(button)
  XDo::FFILib.xdo_mouseup @_xdo_pointer, @_window, button
end

#resize(width, height, use_hints = false) ⇒ Object

Resizes this window.

Args:

width:: the new window's width
height:: the new window's height
use_hints:: if false, width and height are specified in pixels; otherwise,
            the unit is relative to window size hints


89
90
91
92
93
94
95
96
97
# File 'lib/x_do/window.rb', line 89

def resize(width, height, use_hints = false)
  old_size = self.size
  return_value = resize_async width, height, use_hints
  100.times do
    break unless self.size == old_size
    sleep 0.01
  end
  return_value
end

#resize_async(width, height, use_hints = false) ⇒ Object

Asks X to resize this window.



100
101
102
103
# File 'lib/x_do/window.rb', line 100

def resize_async(width, height, use_hints = false)
  flags = use_hints ? XDo::FFILib::Consts::SIZE_U : 0
  XDo::FFILib.xdo_window_setsize @_xdo_pointer, @_window, width, height, flags
end

#sizeObject

width, height

array containing the window’s size.



47
48
49
50
51
52
53
# File 'lib/x_do/window.rb', line 47

def size
  width_pointer = FFI::MemoryPointer.new :int, 1
  height_pointer = FFI::MemoryPointer.new :int, 1
  XDo::FFILib.xdo_get_window_size @_xdo_pointer, @_window, width_pointer,
                                  height_pointer
  [width_pointer.read_int, height_pointer.read_int]
end

#to_sObject

:nodoc: override to_s for so Windows don’t look so bad



194
195
196
197
198
199
200
# File 'lib/x_do/window.rb', line 194

def to_s
  name_str = name.inspect
  at_str = location.inspect
  size_str = size.inspect
  "#<XDo::Window:#{_window} pid=#{pid} title=#{name.inspect} at=#{at_str} " +
                           "size=#{size_str}>"
end

#type_keysequence(keysequence, delay = 0.012) ⇒ Object

Sends a keysequence to this window.

Examples: “alt+Return”, “Alt_L+Tab”, “l”, “semicolon”



142
143
144
145
# File 'lib/x_do/window.rb', line 142

def type_keysequence(keysequence, delay = 0.012)
  XDo::FFILib.xdo_keysequence @_xdo_pointer, @_window, keysequence,
                              (delay * 1_000_000).to_i
end

#type_string(string, delay = 0.012) ⇒ Object

Types a string into this window.



134
135
136
137
# File 'lib/x_do/window.rb', line 134

def type_string(string, delay = 0.012)
  XDo::FFILib.xdo_type @_xdo_pointer, @_window, string,
                       (delay * 1_000_000).to_i
end