Repository navigation
Expand file tree
/
Copy pathapi.lua
More file actions
191 lines (157 loc) · 7.4 KB
/
Copy pathapi.lua
File metadata and controls
191 lines (157 loc) · 7.4 KB
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
-- The `boring` API: what configuration files and plugins use to talk to the
-- compositor. Available as the global `boring` in both.
--
-- Events (boring.events:on(name, fn)):
-- window:configure(w) (before window:map: set w.props, see compositor/window.lua)
-- window:map(w) window:unmap(w) window:focus(w) window:blur(w)
-- window:commit(w) window:title(w)
-- window:fullscreen(w) window:maximize(w) (state changed)
-- window:urgent(w) (w.urgent changed; focus clears it)
-- output:add(o) output:change(o) output:usable(o) output:remove(o)
-- ready() shutdown()
--
-- What clients (or mouse shortcuts and actions) ask for; plugins that
-- manage windows answer them, core does nothing on its own:
-- window:request_move(w) window:request_resize(w, edges)
-- window:request_fullscreen(w, on) window:request_maximize(w, on)
-- window:request_minimize(w)
-- window:request_geometry(w, box) (X11 windows asking for a place and size)
--
-- Windows and outputs are the records described in compositor/window.lua and
-- compositor/output.lua.
local wl = require("shared.wl")
local log = require("shared.log")
local registry = require("shared.wayland.registry")
local window = require("compositor.window")
local windows = require("compositor.windows")
local pointer = require("compositor.pointer")
local output = require("compositor.output")
local process = require("compositor.process")
local loop = require("compositor.loop")
local api = {}
local current = nil
local function copy(list)
local out = {}
for i = 1, #list do out[i] = list[i] end
return out
end
--- Build the API for a server and make it current.
function api.create(server)
local boring = {
server = server, -- escape hatch: display, seat, compositor, scene, ...
wl = wl,
events = server.events,
layers = server.layers,
scene = server.scene,
log = log,
process = process,
}
--- A scope that closes with the compositor (or earlier, if you close it).
function boring.scope(name)
return server.scope:child(name)
end
-- Windows ---------------------------------------------------------------
--- Mapped windows, most recently focused first.
function boring.windows() return copy(server.windows) end
function boring.focused() return server.focused end
function boring.focus(w) windows.focus(server, w) end
function boring.blur() windows.blur(server) end
function boring.cycle_focus() windows.cycle(server) end
function boring.window_at(x, y) return (windows.at(server, x, y)) end
function boring.close_focused()
if server.focused then server.focused:close() end
end
--- For shells implemented as plugins (XWayland): create, map, unmap and
--- forget windows. See compositor/window.lua for `impl`.
boring.window = {
new = function(surface, tree, impl)
local w = window.new(server, surface, tree, impl)
windows.register(server, w)
return w
end,
map = function(w) windows.map(server, w) end,
unmap = function(w) windows.unmap(server, w) end,
destroy = function(w)
windows.unmap(server, w)
windows.unregister(server, w)
end,
from_surface = function(surface) return windows.from_surface(server, surface) end,
}
-- Outputs ---------------------------------------------------------------
function boring.outputs() return copy(server.outputs) end
function boring.output_at(x, y) return output.at(server, x, y) end
-- Pointer ---------------------------------------------------------------
function boring.cursor() return server.cursor.x, server.cursor.y end
function boring.set_cursor(name) pointer.set_cursor(server, name) end
--- Free move/resize: the window follows the pointer until the button
--- is released.
function boring.begin_move(w) pointer.begin_move(server, w) end
function boring.begin_resize(w, edges) pointer.begin_resize(server, w, edges) end
--- Take the pointer: { motion = fn(x, y), button = fn(button, pressed)
--- -> true to end, axis = fn(steps), finish = fn(cancelled), cursor =
--- name, window = w }.
--- Without `button`, releasing a button ends the grab. Returns a
--- function that ends it.
function boring.grab_pointer(handlers) return pointer.grab(server, handlers) end
--- Take the keyboard (before shortcuts and clients): { key = fn(event),
--- finish = fn(cancelled) }, event = { sym, name ("Return", "a"), text
--- (what it types), mods = { shift, ctrl, alt, logo }, pressed, repeat }.
--- Returns a function that ends it.
function boring.grab_keyboard(handlers)
return require("compositor.keyboard").grab(server, handlers)
end
--- The cursor name for resizing by `edges` ("se-resize", ...).
boring.resize_cursor = pointer.resize_cursor
--- Call fn(window, button) when a scene node (or one of its children) is
--- clicked, e.g. a title bar. Returns a function that removes the handler.
function boring.on_click(node, fn)
local key = wl.addr(node)
server.click_handlers[key] = fn
return function() server.click_handlers[key] = nil end
end
-- Compositor ------------------------------------------------------------
--- Whether the session is locked (a lock client is up, or the screen
--- is blanked waiting for one).
function boring.locked() return server.locked ~= nil end
--- The Wayland display name clients connect to (WAYLAND_DISPLAY).
function boring.socket() return server.socket end
function boring.spawn(cmd) return process.spawn(cmd) end
--- Put text (a string) or { [mime type] = bytes } on the clipboard.
function boring.set_clipboard(data) require("compositor.clipboard").set(server, data) end
--- Run fn after `ms` milliseconds; if it returns a number, again after
--- that many. Returns a function that cancels it.
function boring.after(ms, fn) return loop.after(server, ms, fn) end
--- Monotonic time in milliseconds.
boring.now = loop.now
--- Call fn() whenever a file descriptor is readable. Returns a function
--- that stops watching (the fd stays open).
function boring.on_readable(fd, fn) return loop.readable(server, fd, fn) end
--- Call handlers.idle() after `ms` without input (unless an idle
--- inhibitor is active), handlers.resume() when input comes back.
--- Returns a function that stops watching.
function boring.idle(ms, handlers) return require("compositor.idle").watch(server, ms, handlers) end
function boring.exit()
process.stop_all()
require("compositor.server").terminate(server)
end
--- The globals of the loaded config (what it defined), or nil.
function boring.config_env()
local current = require("compositor.config")._current
return current and current.env
end
function boring.reload()
require("compositor.state").reload_config()
require("compositor.keyboard").reconfigure_all(server)
end
--- Mark Wayland interfaces as implemented (for `--protocols`).
function boring.implement(...)
for _, name in ipairs({ ... }) do registry.implement(name) end
end
current = boring
return boring
end
--- The API of the running compositor (nil before startup).
function api.current()
return current
end
return api