Class: XDo::Mouse

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

Overview

The mouse state for a libxdo context.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xdo) ⇒ Mouse

Creates a mouse state wrapper for an XDo context.

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

Args:

xdo:: the XDo wrapping a libxdo context


13
14
15
16
# File 'lib/x_do/mouse.rb', line 13

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

Instance Attribute Details

#xdoObject

The XDo context that produced the window.



19
20
21
# File 'lib/x_do/mouse.rb', line 19

def xdo
  @xdo
end

Instance Method Details

#click(button) ⇒ Object

Clicks a mouse button.



70
71
72
# File 'lib/x_do/mouse.rb', line 70

def click(button)
  XDo::FFILib.xdo_click @_xdo_pointer, 0, button
end

#locationObject

x, y, screen

array of mouse coordinates.



22
23
24
25
26
27
28
29
# File 'lib/x_do/mouse.rb', line 22

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

#move(x, y, screen) ⇒ Object

Moves the mouse to a new position.



32
33
34
35
36
37
38
# File 'lib/x_do/mouse.rb', line 32

def move(x, y, screen)
  old_location = self.location
  move_async x, y, screen
  unless old_location[0, 2] == [x, y]
    wait_for_move_from old_location[0], old_location[1]
  end
end

#move_async(x, y, screen) ⇒ Object

Queues a mouse move request to the X server.



41
42
43
# File 'lib/x_do/mouse.rb', line 41

def move_async(x, y, screen)
  XDo::FFILib.xdo_mousemove @_xdo_pointer, x, y, screen
end

#move_relative(dx, dy) ⇒ Object

Moves the mouse relatively to its current position.



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

def move_relative(dx, dy)
  old_location = self.location
  move_relative_async dx, dy
  unless dx == 0 && dy == 0
    wait_for_move_from old_location[0], old_location[1]
  end
end

#move_relative_async(dx, dy) ⇒ Object

Queues a mouse move request to the X server.



55
56
57
# File 'lib/x_do/mouse.rb', line 55

def move_relative_async(dx, dy)
  XDo::FFILib.xdo_mousemove_relative @_xdo_pointer, dx, dy
end

#press(button) ⇒ Object

Presses a mouse button.



75
76
77
# File 'lib/x_do/mouse.rb', line 75

def press(button)
  XDo::FFILib.xdo_mousedown @_xdo_pointer, 0, button
end

#release(button) ⇒ Object

Releases a mouse button.



80
81
82
# File 'lib/x_do/mouse.rb', line 80

def release(button)
  XDo::FFILib.xdo_mouseup @_xdo_pointer, 0, button
end

#wait_for_move_from(x, y) ⇒ Object

Blocks until the mouse moves away from a position on screen.



60
61
62
# File 'lib/x_do/mouse.rb', line 60

def wait_for_move_from(x, y)
  XDo::FFILib.xdo_mouse_wait_for_move_from @_xdo_pointer, x, y
end

#wait_for_move_to(x, y) ⇒ Object

Blocks until the mouse moves to a position on screen.



65
66
67
# File 'lib/x_do/mouse.rb', line 65

def wait_for_move_to(x, y)
  XDo::FFILib.xdo_mouse_wait_for_move_to @_xdo_pointer, x, y
end