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
|
// This file is part of river, a dynamic tiling wayland compositor.
//
// Copyright 2020 Isaac Freund
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const Self = @This();
const build_options = @import("build_options");
const std = @import("std");
const c = @import("c.zig");
const Config = @import("Config.zig");
const DecorationManager = @import("DecorationManager.zig");
const InputManager = @import("InputManager.zig");
const LayerSurface = @import("LayerSurface.zig");
const Log = @import("log.zig").Log;
const Output = @import("Output.zig");
const Root = @import("Root.zig");
const View = @import("View.zig");
const ViewStack = @import("view_stack.zig").ViewStack;
const WindowManagement = @import("WindowManagement.zig");
const XwaylandUnmanaged = @import("XwaylandUnmanaged.zig");
allocator: *std.mem.Allocator,
wl_display: *c.wl_display,
wl_event_loop: *c.wl_event_loop,
wlr_backend: *c.wlr_backend,
noop_backend: *c.wlr_backend,
listen_new_output: c.wl_listener,
wlr_xdg_shell: *c.wlr_xdg_shell,
listen_new_xdg_surface: c.wl_listener,
wlr_layer_shell: *c.wlr_layer_shell_v1,
listen_new_layer_surface: c.wl_listener,
wlr_xwayland: if (build_options.xwayland) *c.wlr_xwayland else void,
listen_new_xwayland_surface: if (build_options.xwayland) c.wl_listener else void,
decoration_manager: DecorationManager,
input_manager: InputManager,
root: Root,
config: Config,
window_management: WindowManagement,
pub fn init(self: *Self, allocator: *std.mem.Allocator) !void {
self.allocator = allocator;
// The Wayland display is managed by libwayland. It handles accepting
// clients from the Unix socket, managing Wayland globals, and so on.
self.wl_display = c.wl_display_create() orelse
return error.CantCreateWlDisplay;
errdefer c.wl_display_destroy(self.wl_display);
// Should never return null if the display was created successfully
self.wl_event_loop = c.wl_display_get_event_loop(self.wl_display) orelse
return error.CantGetEventLoop;
// The wlr_backend abstracts the input/output hardware. Autocreate chooses
// the best option based on the environment, for example DRM when run from
// a tty or wayland if WAYLAND_DISPLAY is set. This frees itself when the
// wl_display is destroyed.
self.wlr_backend = c.river_wlr_backend_autocreate(self.wl_display) orelse
return error.CantCreateWlrBackend;
// This backend is used to create a noop output for use when no actual
// outputs are available. This frees itself when the wl_display is destroyed.
self.noop_backend = c.river_wlr_noop_backend_create(self.wl_display) orelse
return error.CantCreateNoopBackend;
// If we don't provide a renderer, autocreate makes a GLES2 renderer for us.
// The renderer is responsible for defining the various pixel formats it
// supports for shared memory, this configures that for clients.
const wlr_renderer = c.river_wlr_backend_get_renderer(self.wlr_backend) orelse
return error.CantGetWlrRenderer;
// TODO: Handle failure after https://github.com/swaywm/wlroots/pull/2080
c.wlr_renderer_init_wl_display(wlr_renderer, self.wl_display); // orelse
// return error.CantInitWlDisplay;
self.listen_new_output.notify = handleNewOutput;
c.wl_signal_add(&self.wlr_backend.events.new_output, &self.listen_new_output);
const wlr_compositor = c.wlr_compositor_create(self.wl_display, wlr_renderer) orelse
return error.CantCreateWlrCompositor;
// Set up xdg shell
self.wlr_xdg_shell = c.wlr_xdg_shell_create(self.wl_display) orelse
return error.CantCreateWlrXdgShell;
self.listen_new_xdg_surface.notify = handleNewXdgSurface;
c.wl_signal_add(&self.wlr_xdg_shell.events.new_surface, &self.listen_new_xdg_surface);
// Set up layer shell
self.wlr_layer_shell = c.wlr_layer_shell_v1_create(self.wl_display) orelse
return error.CantCreateWlrLayerShell;
self.listen_new_layer_surface.notify = handleNewLayerSurface;
c.wl_signal_add(&self.wlr_layer_shell.events.new_surface, &self.listen_new_layer_surface);
// Set up xwayland if built with support
if (build_options.xwayland) {
self.wlr_xwayland = c.wlr_xwayland_create(self.wl_display, wlr_compositor, false) orelse
return error.CantCreateWlrXwayland;
self.listen_new_xwayland_surface.notify = handleNewXwaylandSurface;
c.wl_signal_add(&self.wlr_xwayland.events.new_surface, &self.listen_new_xwayland_surface);
}
try self.config.init(self.allocator);
try self.decoration_manager.init(self);
try self.root.init(self);
// Must be called after root is initialized
try self.input_manager.init(self);
try self.window_management.init(self);
// These all free themselves when the wl_display is destroyed
_ = c.wlr_data_device_manager_create(self.wl_display) orelse
return error.CantCreateWlrDataDeviceManager;
_ = c.wlr_screencopy_manager_v1_create(self.wl_display) orelse
return error.CantCreateWlrScreencopyManager;
_ = c.wlr_xdg_output_manager_v1_create(self.wl_display, self.root.wlr_output_layout) orelse
return error.CantCreateWlrOutputManager;
}
/// Free allocated memory and clean up
pub fn deinit(self: *Self) void {
// Note: order is important here
if (build_options.xwayland) {
c.wlr_xwayland_destroy(self.wlr_xwayland);
}
c.wl_display_destroy_clients(self.wl_display);
c.wl_display_destroy(self.wl_display);
self.input_manager.deinit();
self.root.deinit();
self.config.deinit(self.allocator);
}
/// Create the socket, set WAYLAND_DISPLAY, and start the backend
pub fn start(self: Self) !void {
// Add a Unix socket to the Wayland display.
const socket = c.wl_display_add_socket_auto(self.wl_display) orelse
return error.CantAddSocket;
// Start the backend. This will enumerate outputs and inputs, become the DRM
// master, etc
if (!c.river_wlr_backend_start(self.wlr_backend)) {
return error.CantStartBackend;
}
// Set the WAYLAND_DISPLAY environment variable to our socket
if (c.setenv("WAYLAND_DISPLAY", socket, 1) == -1) {
return error.CantSetEnv;
}
if (build_options.xwayland) {
if (c.setenv("DISPLAY", &self.wlr_xwayland.display_name, 1) == -1) {
return error.CantSetEnv;
}
}
}
/// Enter the wayland event loop and block until the compositor is exited
pub fn run(self: Self) void {
c.wl_display_run(self.wl_display);
}
fn handleNewOutput(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
const self = @fieldParentPtr(Self, "listen_new_output", listener.?);
const wlr_output = @ptrCast(*c.wlr_output, @alignCast(@alignOf(*c.wlr_output), data));
Log.Debug.log("New output {}", .{wlr_output.name});
self.root.addOutput(wlr_output);
}
fn handleNewXdgSurface(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
// This event is raised when wlr_xdg_shell receives a new xdg surface from a
// client, either a toplevel (application window) or popup.
const self = @fieldParentPtr(Self, "listen_new_xdg_surface", listener.?);
const wlr_xdg_surface = @ptrCast(*c.wlr_xdg_surface, @alignCast(@alignOf(*c.wlr_xdg_surface), data));
if (wlr_xdg_surface.role == .WLR_XDG_SURFACE_ROLE_POPUP) {
Log.Debug.log("New xdg_popup", .{});
return;
}
Log.Debug.log("New xdg_toplevel", .{});
// The View will add itself to the output's view stack on map
const output = self.input_manager.default_seat.focused_output;
const node = self.allocator.create(ViewStack(View).Node) catch unreachable;
node.view.init(output, output.current_focused_tags, wlr_xdg_surface);
}
/// This event is raised when the layer_shell recieves a new surface from a client.
fn handleNewLayerSurface(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
const self = @fieldParentPtr(Self, "listen_new_layer_surface", listener.?);
const wlr_layer_surface = @ptrCast(
*c.wlr_layer_surface_v1,
@alignCast(@alignOf(*c.wlr_layer_surface_v1), data),
);
Log.Debug.log(
"New layer surface: namespace {}, layer {}, anchor {}, size {}x{}, margin ({},{},{},{}), exclusive_zone {}",
.{
wlr_layer_surface.namespace,
wlr_layer_surface.client_pending.layer,
wlr_layer_surface.client_pending.anchor,
wlr_layer_surface.client_pending.desired_width,
wlr_layer_surface.client_pending.desired_height,
wlr_layer_surface.client_pending.margin.top,
wlr_layer_surface.client_pending.margin.right,
wlr_layer_surface.client_pending.margin.bottom,
wlr_layer_surface.client_pending.margin.left,
wlr_layer_surface.client_pending.exclusive_zone,
},
);
// If the new layer surface does not have an output assigned to it, use the
// first output or close the surface if none are available.
if (wlr_layer_surface.output == null) {
if (self.root.outputs.first) |node| {
const output = &node.data;
Log.Debug.log(
"New layer surface had null output, assigning it to output {}",
.{output.wlr_output.name},
);
wlr_layer_surface.output = output.wlr_output;
} else {
Log.Error.log(
"No output available for layer surface '{}'",
.{wlr_layer_surface.namespace},
);
c.wlr_layer_surface_v1_close(wlr_layer_surface);
return;
}
}
// The layer surface will add itself to the proper list of the output on map
const output = @ptrCast(*Output, @alignCast(@alignOf(*Output), wlr_layer_surface.output.*.data));
const node = self.allocator.create(std.TailQueue(LayerSurface).Node) catch unreachable;
node.data.init(output, wlr_layer_surface);
}
fn handleNewXwaylandSurface(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
const self = @fieldParentPtr(Self, "listen_new_xwayland_surface", listener.?);
const wlr_xwayland_surface = @ptrCast(
*c.wlr_xwayland_surface,
@alignCast(@alignOf(*c.wlr_xwayland_surface), data),
);
if (wlr_xwayland_surface.override_redirect) {
Log.Debug.log("New unmanaged xwayland surface", .{});
// The unmanged surface will add itself to the list of unmanaged views
// in Root when it is mapped.
const node = self.allocator.create(std.TailQueue(XwaylandUnmanaged).Node) catch unreachable;
node.data.init(&self.root, wlr_xwayland_surface);
return;
}
Log.Debug.log(
"New xwayland surface: title '{}', class '{}'",
.{ wlr_xwayland_surface.title, wlr_xwayland_surface.class },
);
// The View will add itself to the output's view stack on map
const output = self.input_manager.default_seat.focused_output;
const node = self.allocator.create(ViewStack(View).Node) catch unreachable;
node.view.init(output, output.current_focused_tags, wlr_xwayland_surface);
}
|