-
Notifications
You must be signed in to change notification settings - Fork 1
/
x_do.cr
301 lines (258 loc) · 7.09 KB
/
x_do.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
74
75
76
77
78
79
80
81
82
83
84
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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
require "./x_do/*"
# `XDo` is a Crystal interface for `libxdo`,
# the C library that backs [`xdotool`](https://github.com/jordansissel/xdotool).
#
# It exposes most of the functionality of `xdotool`, allowing
# users to write Crystal programs that manage windows in an X11 instance.
#
# The easiest way to use `XDo` is via `XDo.act`:
#
# ```
# XDo.act do
# active_window do |win|
# win.type "hello from Crystal!"
# end
# end
# ```
class XDo
private getter xdo_p : LibXDo::XDo*
getter display : String?
DEFAULT_DELAY = 12000
# Returns the version of `libxdo` being used as a `String`.
def self.lib_version
String.new(LibXDo.version)
end
# Returns a `Hash(String, String)` indicating the current symbol map.
#
# ```
# XDo.symbol_map # => {"alt" => "Alt_L", "ctrl" => "Control_L"}
# ```
def self.symbol_map
map_p = LibXDo.get_symbol_map
map = Array(String).new
n = 0
while map_p[n]
map << String.new(map_p[n])
n += 1
end
map.in_groups_of(2, "").to_h
end
# Yields a block with an `XDo` instance, providing a DSL for interaction.
#
# ```
# XDo.act do
# select_window do |win|
# win.unmap!
# sleep 1
# win.map!
# win.type "hello"
# end
#
# desktop = 3
# end
# ```
def self.act
xdo = new
with xdo yield
end
# Creates a new `XDo` instance with the given X11 *display*.
#
# ```
# # create an instance on the default display or `DISPLAY` env variable
# xdo = XDo.new
#
# # ...or with a different X display
# xdo2 = XDo.new(":2")
#
# # ... do some work ...
# ```
def initialize(display = ENV["DISPLAY"]?)
@xdo_p = if display
@display = display
LibXDo.new(display)
else
LibXDo.new(Pointer(UInt8).null)
end
end
# Destroys the instance's internal state.
def finalize
LibXDo.free(xdo_p)
end
# Moves the mouse to coordinates *x*, *y* on the given *screen*.
def move_mouse(x, y, screen)
LibXDo.move_mouse(xdo_p, x, y, screen)
end
# Moves the mouse to coordinates *x*, *y* relative to its current position.
def move_mouse(x, y)
LibXDo.move_mouse_relative(xdo_p, x, y)
end
# Returns the mouse's current position as a tuple of `x` and `y` coordinates,
# the `screen` it's on, and the `Window` it's over.
#
# ```
# x, y, screen, win = xdo.mouse_location
# ```
def mouse_location
LibXDo.get_mouse_location2(xdo_p, out x, out y, out screen, out window)
{x, y, screen, Window.new(xdo_p, window)}
end
# Wait for the mouse to move from the coordinates *x*, *y*.
def wait_for_mouse_move_from(x, y)
LibXDo.wait_for_mouse_move_from(xdo_p, x, y)
end
# ditto
def on_mouse_move_from(x, y, &block)
wait_for_mouse_move_from(x, y)
with self yield
end
# Wait for the mouse to move to the coordinates *x*, *y*.
def wait_for_mouse_move_to(x, y)
LibXDo.wait_for_mouse_move_to(xdo_p, x, y)
end
# ditto
def on_mouse_move_to(x, y, &block)
wait_for_mouse_move_to(x, y)
with self yield
end
def on_mouse_move(&block)
x, y, _, _ = mouse_location
wait_for_mouse_move_from(x, y)
with self yield
end
# Returns a list of `XDo::LibXDo::Charcodemap`s indicating all active modifier keys.
def active_modifiers
LibXDo.get_active_modifiers(xdo_p, out keys, out nkeys)
Array.new(nkeys) { |i| keys[i] }
end
# TODO: implement
def set_active_modifiers
# NOTE: xdo_set_active_modifiers
raise "implement me!"
end
# TODO: implement
def clear_active_modifiers
# NOTE: xdo_clear_active_modifiers
raise "implement me!"
end
# Returns the `Window` that currently has focus.
#
# When *sane* is set to `true`, returns the first ancestor-or-self window
# with the `WM_CLASS` property. When set to `false`, returns the actual focused
# window (which may not be the application's top-level window).
def focused_window(*, sane = true)
window = uninitialized LibXDo::Window
if sane
LibXDo.get_focused_window_sane(xdo_p, pointerof(window))
else
LibXDo.get_focused_window(xdo_p, pointerof(window))
end
Window.new(xdo_p, window)
end
# ditto
def focused_window(*, sane = true, &block)
yield focused_window(sane: sane)
end
# Returns the `Window` that the mouse is currently over.
def mouse_window
LibXDo.get_window_at_mouse(xdo_p, out window)
Window.new(xdo_p, window)
end
# ditto
def mouse_window(&block)
yield mouse_window
end
# Returns the `Window` that is currently active.
def active_window
LibXDo.get_active_window(xdo_p, out window)
Window.new(xdo_p, window)
end
# ditto
def active_window(&block)
yield active_window
end
# Returns the `Window` selected interactively.
def select_window
LibXDo.select_window_with_click(xdo_p, out window)
Window.new(xdo_p, window)
end
# ditto
def select_window(&block)
yield select_window
end
# Takes a `Search` and runs it, returning a list of `Window`s matching the search.
#
# ```
# XDo.act do
# query = XDo::Search.build { window_name "Firefox" }
# winds = search(query)
# puts winds
# end
# ```
def search(query)
search = query.to_struct
LibXDo.search_windows(xdo_p, pointerof(search), out windowlist, out nwindows)
Array.new(nwindows) { |i| Window.new(xdo_p, windowlist[i]) }
end
# Like `#search(query)`, but yields a block to build the query directly.
#
# ```
# XDo.act do
# winds = search { window_name "Firefox" }
# puts winds
# end
# ```
def search(&block)
query = Search.new
with query yield
search(query)
end
# Sets the number of desktops.
def desktops=(ndesktops)
LibXDo.set_number_of_desktops(xdo_p, ndesktops)
end
# Returns the number of desktops.
def desktops
LibXDo.get_number_of_desktops(xdo_p, out ndesktops)
ndesktops
end
# Sets the current desktop.
def desktop=(desktop)
LibXDo.set_current_desktop(xdo_p, desktop)
end
# Returns the current desktop's number.
def desktop
LibXDo.get_current_desktop(xdo_p, out desktop)
desktop
end
# Returns the input state, which is the `OR` of any active modifiers
# in the `KeyMask`.
def input_state : KeyMask
LibXDo.get_input_state(xdo_p)
end
# Sets the desktop viewport (only relevant if `_NET_DESKTOP_VIEWPORT` is supported).
def viewport=(x, y)
LibXDo.set_desktop_viewport(xdo_p, x, y)
end
# Gets the desktop viewport as an `x`, `y` tuple.
def viewport
LibXDo.get_desktop_viewport(xdo_p, out x, out y)
{x, y}
end
# Gets the dimensions of the given screen's viewport as a `width`, `height` tuple.
def viewport_dimensions(screen)
LibXDo.get_viewport_dimensions(xdo_p, out width, out height, screen)
{width, height}
end
# Disable an `xdo` feature.
def disable_feature(feature : XDoFeatures)
LibXDo.disable_feature(xdo_p, feature)
end
# Enable an `xdo` feature.
def enable_feature(feature : XDoFeatures)
LibXDo.enable_feature(xdo_p, feature)
end
# Test whether a feature is enabled.
def has_feature?(feature : XDoFeatures)
LibXDo.has_feature(xdo_p, feature) == 1
end
end