Rework things to stop invalidating pointers

This commit is contained in:
Isaac Freund 2020-03-23 21:51:46 +01:00
parent 523d629fe0
commit a5a84d8164
No known key found for this signature in database
GPG key ID: 86DED400DDFD7A11
7 changed files with 136 additions and 135 deletions

View file

@ -2,6 +2,7 @@ const std = @import("std");
const c = @import("c.zig").c; const c = @import("c.zig").c;
const Seat = @import("seat.zig").Seat; const Seat = @import("seat.zig").Seat;
const Server = @import("server.zig").Server;
const View = @import("view.zig").View; const View = @import("view.zig").View;
const CursorMode = enum { const CursorMode = enum {
@ -12,7 +13,6 @@ const CursorMode = enum {
pub const Cursor = struct { pub const Cursor = struct {
seat: *Seat, seat: *Seat,
wlr_cursor: *c.wlr_cursor, wlr_cursor: *c.wlr_cursor,
wlr_xcursor_manager: *c.wlr_xcursor_manager, wlr_xcursor_manager: *c.wlr_xcursor_manager,
@ -32,11 +32,13 @@ pub const Cursor = struct {
grab_height: c_int, grab_height: c_int,
resize_edges: u32, resize_edges: u32,
pub fn init(seat: *Seat) !@This() { pub fn create(seat: *Seat) !@This() {
var cursor = @This(){ var cursor = @This(){
.seat = seat, .seat = seat,
// Creates a wlroots utility for tracking the cursor image shown on screen. // Creates a wlroots utility for tracking the cursor image shown on screen.
//
// TODO: free this, it allocates!
.wlr_cursor = c.wlr_cursor_create() orelse .wlr_cursor = c.wlr_cursor_create() orelse
return error.CantCreateWlrCursor, return error.CantCreateWlrCursor,
@ -44,6 +46,8 @@ pub const Cursor = struct {
// Xcursor themes to source cursor images from and makes sure that cursor // Xcursor themes to source cursor images from and makes sure that cursor
// images are available at all scale factors on the screen (necessary for // images are available at all scale factors on the screen (necessary for
// HiDPI support). We add a cursor theme at scale factor 1 to begin with. // HiDPI support). We add a cursor theme at scale factor 1 to begin with.
//
// TODO: free this, it allocates!
.wlr_xcursor_manager = c.wlr_xcursor_manager_create(null, 24) orelse .wlr_xcursor_manager = c.wlr_xcursor_manager_create(null, 24) orelse
return error.CantCreateWlrXCursorManager, return error.CantCreateWlrXCursorManager,
@ -86,22 +90,24 @@ pub const Cursor = struct {
c.wlr_cursor_attach_output_layout(cursor.wlr_cursor, seat.server.wlr_output_layout); c.wlr_cursor_attach_output_layout(cursor.wlr_cursor, seat.server.wlr_output_layout);
_ = c.wlr_xcursor_manager_load(cursor.wlr_xcursor_manager, 1); _ = c.wlr_xcursor_manager_load(cursor.wlr_xcursor_manager, 1);
return cursor;
}
pub fn init(self: *@This()) void {
// wlr_cursor *only* displays an image on screen. It does not move around // wlr_cursor *only* displays an image on screen. It does not move around
// when the pointer moves. However, we can attach input devices to it, and // when the pointer moves. However, we can attach input devices to it, and
// it will generate aggregate events for all of them. In these events, we // it will generate aggregate events for all of them. In these events, we
// can choose how we want to process them, forwarding them to clients and // can choose how we want to process them, forwarding them to clients and
// moving the cursor around. See following post for more detail: // moving the cursor around. See following post for more detail:
// https://drewdevault.com/2018/07/17/Input-handling-in-wlroots.html // https://drewdevault.com/2018/07/17/Input-handling-in-wlroots.html
c.wl_signal_add(&cursor.wlr_cursor.*.events.motion, &cursor.listen_motion); c.wl_signal_add(&self.wlr_cursor.*.events.motion, &self.listen_motion);
c.wl_signal_add(&cursor.wlr_cursor.*.events.motion_absolute, &cursor.listen_motion_absolute); c.wl_signal_add(&self.wlr_cursor.*.events.motion_absolute, &self.listen_motion_absolute);
c.wl_signal_add(&cursor.wlr_cursor.*.events.button, &cursor.listen_button); c.wl_signal_add(&self.wlr_cursor.*.events.button, &self.listen_button);
c.wl_signal_add(&cursor.wlr_cursor.*.events.axis, &cursor.listen_axis); c.wl_signal_add(&self.wlr_cursor.*.events.axis, &self.listen_axis);
c.wl_signal_add(&cursor.wlr_cursor.*.events.frame, &cursor.listen_frame); c.wl_signal_add(&self.wlr_cursor.*.events.frame, &self.listen_frame);
// This listens for clients requesting a specific cursor image // This listens for clients requesting a specific cursor image
c.wl_signal_add(&seat.wlr_seat.events.request_set_cursor, &cursor.listen_request_set_cursor); c.wl_signal_add(&self.seat.wlr_seat.events.request_set_cursor, &self.listen_request_set_cursor);
return cursor;
} }
fn process_move(self: *@This(), time: u32) void { fn process_move(self: *@This(), time: u32) void {

View file

@ -10,20 +10,9 @@ pub const Keyboard = struct {
listen_modifiers: c.wl_listener, listen_modifiers: c.wl_listener,
listen_key: c.wl_listener, listen_key: c.wl_listener,
pub fn init(seat: *Seat, device: *c.wlr_input_device) @This() { pub fn init(self: *@This(), seat: *Seat, device: *c.wlr_input_device) !void {
var keyboard = @This(){ self.seat = seat;
.seat = seat, self.device = device;
.device = device,
.listen_modifiers = c.wl_listener{
.link = undefined,
.notify = handle_modifiers,
},
.listen_key = c.wl_listener{
.link = undefined,
.notify = handle_key,
},
};
// We need to prepare an XKB keymap and assign it to the keyboard. This // We need to prepare an XKB keymap and assign it to the keyboard. This
// assumes the defaults (e.g. layout = "us"). // assumes the defaults (e.g. layout = "us").
@ -34,25 +23,29 @@ pub const Keyboard = struct {
.variant = null, .variant = null,
.options = null, .options = null,
}; };
const context = c.xkb_context_new(c.enum_xkb_context_flags.XKB_CONTEXT_NO_FLAGS); const context = c.xkb_context_new(c.enum_xkb_context_flags.XKB_CONTEXT_NO_FLAGS) orelse
return error.CantCreateXkbContext;
defer c.xkb_context_unref(context); defer c.xkb_context_unref(context);
const keymap = c.xkb_keymap_new_from_names( const keymap = c.xkb_keymap_new_from_names(
context, context,
&rules, &rules,
c.enum_xkb_keymap_compile_flags.XKB_KEYMAP_COMPILE_NO_FLAGS, c.enum_xkb_keymap_compile_flags.XKB_KEYMAP_COMPILE_NO_FLAGS,
); ) orelse
return error.CantCreateXkbKeymap;
defer c.xkb_keymap_unref(keymap); defer c.xkb_keymap_unref(keymap);
var keyboard_device = device.*.unnamed_37.keyboard; var keyboard_device = self.device.unnamed_37.keyboard;
// TODO: handle failure after https://github.com/swaywm/wlroots/pull/2081
c.wlr_keyboard_set_keymap(keyboard_device, keymap); c.wlr_keyboard_set_keymap(keyboard_device, keymap);
c.wlr_keyboard_set_repeat_info(keyboard_device, 25, 600); c.wlr_keyboard_set_repeat_info(keyboard_device, 25, 600);
// Setup listeners for keyboard events // Setup listeners for keyboard events
c.wl_signal_add(&keyboard_device.*.events.modifiers, &keyboard.listen_modifiers); self.listen_modifiers.notify = handle_modifiers;
c.wl_signal_add(&keyboard_device.*.events.key, &keyboard.listen_key); c.wl_signal_add(&keyboard_device.*.events.modifiers, &self.listen_modifiers);
return keyboard; self.listen_key.notify = handle_key;
c.wl_signal_add(&keyboard_device.*.events.key, &self.listen_key);
} }
fn handle_modifiers(listener: [*c]c.wl_listener, data: ?*c_void) callconv(.C) void { fn handle_modifiers(listener: [*c]c.wl_listener, data: ?*c_void) callconv(.C) void {

View file

@ -8,7 +8,9 @@ pub fn main() !void {
c.wlr_log_init(c.enum_wlr_log_importance.WLR_DEBUG, null); c.wlr_log_init(c.enum_wlr_log_importance.WLR_DEBUG, null);
var server = try Server.init(std.heap.c_allocator); var server = try Server.create(std.heap.c_allocator);
try server.init();
defer server.deinit(); defer server.deinit();
try server.start(); try server.start();

View file

@ -16,7 +16,7 @@ pub const Output = struct {
wlr_output: *c.wlr_output, wlr_output: *c.wlr_output,
listen_frame: c.wl_listener, listen_frame: c.wl_listener,
pub fn init(server: *Server, wlr_output: *c.wlr_output) !@This() { pub fn init(self: *@This(), server: *Server, wlr_output: *c.wlr_output) !void {
// Some backends don't have modes. DRM+KMS does, and we need to set a mode // Some backends don't have modes. DRM+KMS does, and we need to set a mode
// before we can use the output. The mode is a tuple of (width, height, // before we can use the output. The mode is a tuple of (width, height,
// refresh rate), and each monitor supports only a specific set of modes. We // refresh rate), and each monitor supports only a specific set of modes. We
@ -33,17 +33,12 @@ pub const Output = struct {
} }
} }
var output = @This(){ self.server = server;
.server = server, self.wlr_output = wlr_output;
.wlr_output = wlr_output,
.listen_frame = c.wl_listener{
.link = undefined,
.notify = handle_frame,
},
};
// Sets up a listener for the frame notify event. // Sets up a listener for the frame notify event.
c.wl_signal_add(&wlr_output.events.frame, &output.listen_frame); self.listen_frame.notify = handle_frame;
c.wl_signal_add(&wlr_output.events.frame, &self.listen_frame);
// Add the new output to the layout. The add_auto function arranges outputs // Add the new output to the layout. The add_auto function arranges outputs
// from left-to-right in the order they appear. A more sophisticated // from left-to-right in the order they appear. A more sophisticated
@ -55,8 +50,6 @@ pub const Output = struct {
// clients can see to find out information about the output (such as // clients can see to find out information about the output (such as
// DPI, scale factor, manufacturer, etc). // DPI, scale factor, manufacturer, etc).
c.wlr_output_create_global(wlr_output); c.wlr_output_create_global(wlr_output);
return output;
} }
fn handle_frame(listener: [*c]c.wl_listener, data: ?*c_void) callconv(.C) void { fn handle_frame(listener: [*c]c.wl_listener, data: ?*c_void) callconv(.C) void {
@ -83,8 +76,11 @@ pub const Output = struct {
c.wlr_renderer_clear(renderer, &color); c.wlr_renderer_clear(renderer, &color);
// Each subsequent view is rendered on top of the last. // Each subsequent view is rendered on top of the last.
for (output.*.server.views.span()) |*view| { // The first view in the list is "on top" so iterate in reverse.
if (!view.*.mapped) { var it = output.server.views.last;
while (it) |node| : (it = node.prev) {
var view = &node.data;
if (!view.mapped) {
// An unmapped view should not be rendered. // An unmapped view should not be rendered.
continue; continue;
} }

View file

@ -8,41 +8,48 @@ const Server = @import("server.zig").Server;
// TODO: InputManager and multi-seat support // TODO: InputManager and multi-seat support
pub const Seat = struct { pub const Seat = struct {
server: *Server, server: *Server,
wlr_seat: *c.wlr_seat, wlr_seat: *c.wlr_seat,
listen_new_input: c.wl_listener, listen_new_input: c.wl_listener,
// Multiple mice are handled by the same Cursor // Multiple mice are handled by the same Cursor
cursor: Cursor, cursor: Cursor,
// Mulitple keyboards are handled separately // Mulitple keyboards are handled separately
keyboards: std.ArrayList(Keyboard), keyboards: std.TailQueue(Keyboard),
pub fn init(server: *Server, allocator: *std.mem.Allocator) !@This() { pub fn create(server: *Server) !@This() {
var seat = @This(){ var seat = @This(){
.server = server, .server = server,
// This seems to be the default seat name used by compositors .wlr_seat = undefined,
.wlr_seat = c.wlr_seat_create(server.*.wl_display, "seat0"),
.cursor = undefined,
.keyboards = std.ArrayList(Keyboard).init(allocator),
.listen_new_input = c.wl_listener{ .listen_new_input = c.wl_listener{
.link = undefined, .link = undefined,
.notify = handle_new_input, .notify = handle_new_input,
}, },
.cursor = undefined,
.keyboards = std.TailQueue(Keyboard).init(),
}; };
seat.cursor = try Cursor.init(&seat); // This seems to be the default seat name used by compositors
seat.wlr_seat = c.wlr_seat_create(server.*.wl_display, "seat0") orelse
// Set up handler for all new input devices made available. This return error.CantCreateWlrSeat;
// includes keyboards, pointers, touch, etc.
c.wl_signal_add(&server.wlr_backend.events.new_input, &seat.listen_new_input);
return seat; return seat;
} }
pub fn init(self: *@This()) !void {
self.cursor = try Cursor.create(self);
self.cursor.init();
// Set up handler for all new input devices made available. This
// includes keyboards, pointers, touch, etc.
c.wl_signal_add(&self.server.wlr_backend.events.new_input, &self.listen_new_input);
}
fn add_keyboard(self: *@This(), device: *c.wlr_input_device) !void { fn add_keyboard(self: *@This(), device: *c.wlr_input_device) !void {
try self.keyboards.append(Keyboard.init(self, device));
c.wlr_seat_set_keyboard(self.wlr_seat, device); c.wlr_seat_set_keyboard(self.wlr_seat, device);
var node = try self.keyboards.allocateNode(self.server.allocator);
try node.data.init(self, device);
self.keyboards.append(node);
} }
fn add_pointer(self: *@This(), device: *c.struct_wlr_input_device) void { fn add_pointer(self: *@This(), device: *c.struct_wlr_input_device) void {
@ -69,7 +76,7 @@ pub const Seat = struct {
// there are no pointer devices, so we always include that capability. // there are no pointer devices, so we always include that capability.
var caps: u32 = @intCast(u32, c.WL_SEAT_CAPABILITY_POINTER); var caps: u32 = @intCast(u32, c.WL_SEAT_CAPABILITY_POINTER);
// if list not empty // if list not empty
if (seat.keyboards.span().len > 0) { if (seat.keyboards.len > 0) {
caps |= @intCast(u32, c.WL_SEAT_CAPABILITY_KEYBOARD); caps |= @intCast(u32, c.WL_SEAT_CAPABILITY_KEYBOARD);
} }
c.wlr_seat_set_capabilities(seat.wlr_seat, caps); c.wlr_seat_set_capabilities(seat.wlr_seat, caps);

View file

@ -6,25 +6,28 @@ const Seat = @import("seat.zig").Seat;
const View = @import("view.zig").View; const View = @import("view.zig").View;
pub const Server = struct { pub const Server = struct {
allocator: *std.mem.Allocator,
wl_display: *c.wl_display, wl_display: *c.wl_display,
wlr_backend: *c.wlr_backend, wlr_backend: *c.wlr_backend,
wlr_renderer: *c.wlr_renderer, wlr_renderer: *c.wlr_renderer,
wlr_output_layout: *c.wlr_output_layout, wlr_output_layout: *c.wlr_output_layout,
outputs: std.ArrayList(Output), outputs: std.TailQueue(Output),
listen_new_output: c.wl_listener, listen_new_output: c.wl_listener,
wlr_xdg_shell: *c.wlr_xdg_shell, wlr_xdg_shell: *c.wlr_xdg_shell,
listen_new_xdg_surface: c.wl_listener, listen_new_xdg_surface: c.wl_listener,
// Must stay ordered bottom to top // Must stay ordered, first in list is "on top" visually
views: std.ArrayList(View), views: std.TailQueue(View),
seat: Seat, seat: Seat,
pub fn init(allocator: *std.mem.Allocator) !@This() { pub fn create(allocator: *std.mem.Allocator) !@This() {
var server: @This() = undefined; var server: @This() = undefined;
server.allocator = allocator;
// The Wayland display is managed by libwayland. It handles accepting // The Wayland display is managed by libwayland. It handles accepting
// clients from the Unix socket, manging Wayland globals, and so on. // clients from the Unix socket, manging Wayland globals, and so on.
@ -36,7 +39,7 @@ pub const Server = struct {
// the best option based on the environment, for example DRM when run from // the best option based on the environment, for example DRM when run from
// a tty or wayland if WAYLAND_DISPLAY is set. // a tty or wayland if WAYLAND_DISPLAY is set.
// //
// This frees itself when the wl_display is destroyed. // This frees itserver.when the wl_display is destroyed.
server.wlr_backend = c.zag_wlr_backend_autocreate(server.wl_display) orelse server.wlr_backend = c.zag_wlr_backend_autocreate(server.wl_display) orelse
return error.CantCreateWlrBackend; return error.CantCreateWlrBackend;
@ -46,7 +49,7 @@ pub const Server = struct {
server.wlr_renderer = c.zag_wlr_backend_get_renderer(server.wlr_backend) orelse server.wlr_renderer = c.zag_wlr_backend_get_renderer(server.wlr_backend) orelse
return error.CantGetWlrRenderer; return error.CantGetWlrRenderer;
// TODO: Handle failure after https://github.com/swaywm/wlroots/pull/2080 // TODO: Handle failure after https://github.com/swaywm/wlroots/pull/2080
c.wlr_renderer_init_wl_display(server.wlr_renderer, server.wl_display);// orelse c.wlr_renderer_init_wl_display(server.wlr_renderer, server.wl_display); // orelse
// return error.CantInitWlDisplay; // return error.CantInitWlDisplay;
// These both free themselves when the wl_display is destroyed // These both free themselves when the wl_display is destroyed
@ -61,26 +64,30 @@ pub const Server = struct {
return error.CantCreateWlrOutputLayout; return error.CantCreateWlrOutputLayout;
errdefer c.wlr_output_layout_destroy(server.wlr_output_layout); errdefer c.wlr_output_layout_destroy(server.wlr_output_layout);
server.outputs = std.ArrayList(Output).init(std.heap.c_allocator); // Don't register the wl_listeners yet as they must first be pointer-stable
server.outputs = std.TailQueue(Output).init();
// Setup a listener for new outputs
server.listen_new_output.notify = handle_new_output; server.listen_new_output.notify = handle_new_output;
c.wl_signal_add(&server.wlr_backend.*.events.new_output, &server.listen_new_output);
// Set up our list of views and the xdg-shell. The xdg-shell is a Wayland server.views = std.TailQueue(View).init();
// protocol which is used for application windows.
// https://drewdevault.com/2018/07/29/Wayland-shells.html
server.views = std.ArrayList(View).init(std.heap.c_allocator);
server.wlr_xdg_shell = c.wlr_xdg_shell_create(server.wl_display) orelse server.wlr_xdg_shell = c.wlr_xdg_shell_create(server.wl_display) orelse
return error.CantCreateWlrXdgShell; return error.CantCreateWlrXdgShell;
server.listen_new_xdg_surface.notify = handle_new_xdg_surface; server.listen_new_xdg_surface.notify = handle_new_xdg_surface;
c.wl_signal_add(&server.wlr_xdg_shell.*.events.new_surface, &server.listen_new_xdg_surface);
server.seat = try Seat.init(&server, std.heap.c_allocator);
return server; return server;
} }
pub fn init(self: *@This()) !void {
self.seat = try Seat.create(self);
try self.seat.init();
// Register our listeners for new outputs and xdg_surfaces.
// This can't be done in create() as wl_signal_add() creates a pointer
// to the wl_list link in our wl_listener, a pointer that would be
// broken when returning from create();
c.wl_signal_add(&self.wlr_backend.*.events.new_output, &self.listen_new_output);
c.wl_signal_add(&self.wlr_xdg_shell.*.events.new_surface, &self.listen_new_xdg_surface);
}
/// Free allocated memory and clean up /// Free allocated memory and clean up
pub fn deinit(self: @This()) void { pub fn deinit(self: @This()) void {
c.wl_display_destroy_clients(self.wl_display); c.wl_display_destroy_clients(self.wl_display);
@ -141,7 +148,9 @@ pub const Server = struct {
var wlr_output = @ptrCast(*c.wlr_output, @alignCast(@alignOf(*c.wlr_output), data)); var wlr_output = @ptrCast(*c.wlr_output, @alignCast(@alignOf(*c.wlr_output), data));
// TODO: Handle failure // TODO: Handle failure
server.outputs.append(Output.init(server, wlr_output) catch unreachable) catch unreachable; var node = server.outputs.allocateNode(server.allocator) catch unreachable;
node.data.init(server, wlr_output) catch unreachable;
server.outputs.append(node);
} }
fn handle_new_xdg_surface(listener: [*c]c.wl_listener, data: ?*c_void) callconv(.C) void { fn handle_new_xdg_surface(listener: [*c]c.wl_listener, data: ?*c_void) callconv(.C) void {
@ -154,16 +163,19 @@ pub const Server = struct {
return; return;
} }
// Init a View to handle this surface // Create a View to handle this toplevel surface
server.*.views.append(View.init(server, wlr_xdg_surface)) catch unreachable; var node = server.views.allocateNode(server.allocator) catch unreachable;
node.data.init(server, wlr_xdg_surface);
server.views.append(node);
} }
/// Finds the top most view under the output layout coordinates lx, ly /// Finds the top most view under the output layout coordinates lx, ly
/// returns the view if found, and a pointer to the wlr_surface as well as the surface coordinates /// returns the view if found, and a pointer to the wlr_surface as well as the surface coordinates
pub fn desktop_view_at(self: *@This(), lx: f64, ly: f64, surface: *?*c.wlr_surface, sx: *f64, sy: *f64) ?*View { pub fn desktop_view_at(self: *@This(), lx: f64, ly: f64, surface: *?*c.wlr_surface, sx: *f64, sy: *f64) ?*View {
for (self.views.span()) |*view| { var it = self.views.last;
if (view.is_at(lx, ly, surface, sx, sy)) { while (it) |node| : (it = node.prev) {
return view; if (node.data.is_at(lx, ly, surface, sx, sy)) {
return &node.data;
} }
} }
return null; return null;

View file

@ -7,55 +7,36 @@ pub const View = struct {
server: *Server, server: *Server,
wlr_xdg_surface: *c.wlr_xdg_surface, wlr_xdg_surface: *c.wlr_xdg_surface,
mapped: bool,
x: c_int,
y: c_int,
listen_map: c.wl_listener, listen_map: c.wl_listener,
listen_unmap: c.wl_listener, listen_unmap: c.wl_listener,
listen_destroy: c.wl_listener, listen_destroy: c.wl_listener,
// listen_request_move: c.wl_listener, // listen_request_move: c.wl_listener,
// listen_request_resize: c.wl_listener, // listen_request_resize: c.wl_listener,
mapped: bool, pub fn init(self: *@This(), server: *Server, wlr_xdg_surface: *c.wlr_xdg_surface) void {
x: c_int, self.server = server;
y: c_int, self.wlr_xdg_surface = wlr_xdg_surface;
pub fn init(server: *Server, wlr_xdg_surface: *c.wlr_xdg_surface) @This() { self.mapped = false;
var view = @This(){ self.x = 0;
.server = server, self.y = 0;
.wlr_xdg_surface = wlr_xdg_surface,
.listen_map = c.wl_listener{
.link = undefined,
.notify = handle_map,
},
.listen_unmap = c.wl_listener{
.link = undefined,
.notify = handle_unmap,
},
.listen_destroy = c.wl_listener{
.link = undefined,
.notify = handle_destroy,
},
// .listen_request_move = c.wl_listener{
// .link = undefined,
// .notify = handle_request_move,
// },
// .listen_request_resize = c.wl_listener{
// .link = undefined,
// .notify = handle_request_resize,
// },
.mapped = false,
.x = 0,
.y = 0,
};
// Listen to the various events it can emit self.listen_map.notify = handle_map;
c.wl_signal_add(&view.wlr_xdg_surface.events.map, &view.listen_map); c.wl_signal_add(&self.wlr_xdg_surface.events.map, &self.listen_map);
c.wl_signal_add(&view.wlr_xdg_surface.events.unmap, &view.listen_unmap);
c.wl_signal_add(&view.wlr_xdg_surface.events.destroy, &view.listen_destroy); self.listen_unmap.notify = handle_unmap;
c.wl_signal_add(&self.wlr_xdg_surface.events.unmap, &self.listen_unmap);
self.listen_destroy.notify = handle_destroy;
c.wl_signal_add(&self.wlr_xdg_surface.events.destroy, &self.listen_destroy);
// var toplevel = xdg_surface.*.unnamed_160.toplevel; // var toplevel = xdg_surface.*.unnamed_160.toplevel;
// c.wl_signal_add(&toplevel.*.events.request_move, &view.*.request_move); // c.wl_signal_add(&toplevel.*.events.request_move, &view.*.request_move);
// c.wl_signal_add(&toplevel.*.events.request_resize, &view.*.request_resize); // c.wl_signal_add(&toplevel.*.events.request_resize, &view.*.request_resize);
return view;
} }
fn handle_map(listener: [*c]c.wl_listener, data: ?*c_void) callconv(.C) void { fn handle_map(listener: [*c]c.wl_listener, data: ?*c_void) callconv(.C) void {
@ -72,13 +53,17 @@ pub const View = struct {
fn handle_destroy(listener: [*c]c.wl_listener, data: ?*c_void) callconv(.C) void { fn handle_destroy(listener: [*c]c.wl_listener, data: ?*c_void) callconv(.C) void {
var view = @fieldParentPtr(View, "listen_destroy", listener); var view = @fieldParentPtr(View, "listen_destroy", listener);
var server = view.*.server; var server = view.server;
const idx = for (server.*.views.span()) |*v, i| {
if (v == view) { var it = server.views.first;
break i; const target = while (it) |node| : (it = node.next) {
if (&node.data == view) {
break node;
} }
} else return; } else unreachable;
_ = server.*.views.orderedRemove(idx);
server.views.remove(target);
server.views.destroyNode(target, server.allocator);
} }
// fn xdg_toplevel_request_move(listener: [*c]c.wl_listener, data: ?*c_void) callconv(.C) void { // fn xdg_toplevel_request_move(listener: [*c]c.wl_listener, data: ?*c_void) callconv(.C) void {
@ -107,20 +92,20 @@ pub const View = struct {
_ = c.wlr_xdg_toplevel_set_activated(prev_xdg_surface, false); _ = c.wlr_xdg_toplevel_set_activated(prev_xdg_surface, false);
} }
// Find the index // Find the node
const idx = for (server.views.span()) |*v, i| { var it = server.views.first;
if (self == v) { const target = while (it) |node| : (it = node.next) {
break i; if (&node.data == self) {
break node;
} }
} else unreachable; } else unreachable;
// Move the view to the front // Move the view to the front
server.views.append(server.views.orderedRemove(idx)) catch unreachable; server.views.remove(target);
server.views.prepend(target);
var moved_self = &server.views.span()[server.views.span().len - 1];
// Activate the new surface // Activate the new surface
_ = c.wlr_xdg_toplevel_set_activated(moved_self.wlr_xdg_surface, true); _ = c.wlr_xdg_toplevel_set_activated(self.wlr_xdg_surface, true);
// Tell the seat to have the keyboard enter this surface. wlroots will keep // Tell the seat to have the keyboard enter this surface. wlroots will keep
// track of this and automatically send key events to the appropriate // track of this and automatically send key events to the appropriate
@ -128,7 +113,7 @@ pub const View = struct {
var keyboard = c.wlr_seat_get_keyboard(wlr_seat); var keyboard = c.wlr_seat_get_keyboard(wlr_seat);
c.wlr_seat_keyboard_notify_enter( c.wlr_seat_keyboard_notify_enter(
wlr_seat, wlr_seat,
moved_self.wlr_xdg_surface.surface, self.wlr_xdg_surface.surface,
&keyboard.*.keycodes, &keyboard.*.keycodes,
keyboard.*.num_keycodes, keyboard.*.num_keycodes,
&keyboard.*.modifiers, &keyboard.*.modifiers,