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


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

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

Instance Attribute Details

#_windowObject

The underlying X Window handle.



169
170
171
# File 'lib/x_do/window.rb', line 169

def _window
  @_window
end

#xdoObject

The XDo context that produced the window.



166
167
168
# File 'lib/x_do/window.rb', line 166

def xdo
  @xdo
end

Instance Method Details

#==(other) ⇒ Object

:nodoc: underlying window handle should impact equality



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

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.



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

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 ==



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

def hash
  _window.hash
end

#locationObject

x, y

array containing the window’s coordinates.



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

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



44
45
46
47
48
49
50
# File 'lib/x_do/window.rb', line 44

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.



95
96
97
98
99
# File 'lib/x_do/window.rb', line 95

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.



102
103
104
105
# File 'lib/x_do/window.rb', line 102

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.



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

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.



67
68
69
# File 'lib/x_do/window.rb', line 67

def move_raw_async(x, y)
  XDo::FFILib.xdo_window_move @_xdo_pointer, @_window, x, y
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.12) ⇒ Object

Presses a keysequence in this window.

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



138
139
140
141
# File 'lib/x_do/window.rb', line 138

def press_keysequence(keysequence, delay = 0.12)
  XDo::FFILib.xdo_keysequence_down @_xdo_pointer, @_window, keysequence,
                                   (delay * 100_000).to_i
end

#press_mouse(button) ⇒ Object

Presses a mouse button.



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

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.12) ⇒ Object

Releases a keysequence in this window.

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



146
147
148
149
# File 'lib/x_do/window.rb', line 146

def release_keysequence(keysequence, delay = 0.12)
  XDo::FFILib.xdo_keysequence_up @_xdo_pointer, @_window, keysequence,
                                 (delay * 100_000).to_i
end

#release_mouse(button) ⇒ Object

Releases a mouse button.



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

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


78
79
80
81
82
83
84
85
86
# File 'lib/x_do/window.rb', line 78

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.



89
90
91
92
# File 'lib/x_do/window.rb', line 89

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.



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

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

#type_keysequence(keysequence, delay = 0.12) ⇒ Object

Sends a keysequence to this window.

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



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

def type_keysequence(keysequence, delay = 0.12)
  XDo::FFILib.xdo_keysequence @_xdo_pointer, @_window, keysequence,
                              (delay * 100_000).to_i
end

#type_string(string, delay = 0.12) ⇒ Object

Types a string into this window.



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

def type_string(string, delay = 0.12)
  XDo::FFILib.xdo_type @_xdo_pointer, @_window, string, (delay * 100_000).to_i
end