Make all things const

The quest for idiomatic zig continues
This commit is contained in:
Isaac Freund 2020-03-24 20:35:45 +01:00
parent 4872a68378
commit b3bad0ca93
No known key found for this signature in database
GPG key ID: 86DED400DDFD7A11
7 changed files with 51 additions and 49 deletions

View file

@ -33,7 +33,7 @@ pub const Cursor = struct {
resize_edges: u32, resize_edges: u32,
pub fn create(seat: *Seat) !@This() { pub fn create(seat: *Seat) !@This() {
var cursor = @This(){ const 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.
@ -127,15 +127,17 @@ pub const Cursor = struct {
// commit any movement that was prepared. // commit any movement that was prepared.
// TODO: Handle null view // TODO: Handle null view
var view = self.grabbed_view; const view = self.grabbed_view;
const dx: f64 = self.wlr_cursor.x - self.grab_x;
const dy: f64 = self.wlr_cursor.y - self.grab_y;
var dx: f64 = self.wlr_cursor.x - self.grab_x;
var dy: f64 = self.wlr_cursor.y - self.grab_y;
var x: f64 = @intToFloat(f64, view.?.x); var x: f64 = @intToFloat(f64, view.?.x);
var y: f64 = @intToFloat(f64, view.?.y); var y: f64 = @intToFloat(f64, view.?.y);
var width = @intToFloat(f64, self.grab_width); var width = @intToFloat(f64, self.grab_width);
var height = @intToFloat(f64, self.grab_height); var height = @intToFloat(f64, self.grab_height);
if (self.resize_edges & @intCast(u32, c.WLR_EDGE_TOP) != 0) { if (self.resize_edges & @intCast(u32, c.WLR_EDGE_TOP) != 0) {
y = self.grab_y + dy; y = self.grab_y + dy;
height -= dy; height -= dy;
@ -177,7 +179,7 @@ pub const Cursor = struct {
var sx: f64 = undefined; var sx: f64 = undefined;
var sy: f64 = undefined; var sy: f64 = undefined;
var opt_surface: ?*c.wlr_surface = null; var opt_surface: ?*c.wlr_surface = null;
var view = self.seat.server.desktop_view_at( const view = self.seat.server.desktop_view_at(
self.wlr_cursor.x, self.wlr_cursor.x,
self.wlr_cursor.y, self.wlr_cursor.y,
&opt_surface, &opt_surface,
@ -196,7 +198,7 @@ pub const Cursor = struct {
); );
} }
var wlr_seat = self.seat.wlr_seat; const wlr_seat = self.seat.wlr_seat;
if (opt_surface) |surface| { if (opt_surface) |surface| {
const focus_changed = wlr_seat.pointer_state.focused_surface != surface; const focus_changed = wlr_seat.pointer_state.focused_surface != surface;
// "Enter" the surface if necessary. This lets the client know that the // "Enter" the surface if necessary. This lets the client know that the
@ -221,8 +223,8 @@ pub const Cursor = struct {
fn handle_motion(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { fn handle_motion(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
// This event is forwarded by the cursor when a pointer emits a _relative_ // This event is forwarded by the cursor when a pointer emits a _relative_
// pointer motion event (i.e. a delta) // pointer motion event (i.e. a delta)
var cursor = @fieldParentPtr(Cursor, "listen_motion", listener.?); const cursor = @fieldParentPtr(Cursor, "listen_motion", listener.?);
var event = @ptrCast( const event = @ptrCast(
*c.wlr_event_pointer_motion, *c.wlr_event_pointer_motion,
@alignCast(@alignOf(*c.wlr_event_pointer_motion), data), @alignCast(@alignOf(*c.wlr_event_pointer_motion), data),
); );
@ -242,8 +244,8 @@ pub const Cursor = struct {
// move the mouse over the window. You could enter the window from any edge, // move the mouse over the window. You could enter the window from any edge,
// so we have to warp the mouse there. There is also some hardware which // so we have to warp the mouse there. There is also some hardware which
// emits these events. // emits these events.
var cursor = @fieldParentPtr(Cursor, "listen_motion_absolute", listener.?); const cursor = @fieldParentPtr(Cursor, "listen_motion_absolute", listener.?);
var event = @ptrCast( const event = @ptrCast(
*c.wlr_event_pointer_motion_absolute, *c.wlr_event_pointer_motion_absolute,
@alignCast(@alignOf(*c.wlr_event_pointer_motion_absolute), data), @alignCast(@alignOf(*c.wlr_event_pointer_motion_absolute), data),
); );
@ -254,8 +256,8 @@ pub const Cursor = struct {
fn handle_button(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { fn handle_button(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
// This event is forwarded by the cursor when a pointer emits a button // This event is forwarded by the cursor when a pointer emits a button
// event. // event.
var cursor = @fieldParentPtr(Cursor, "listen_button", listener.?); const cursor = @fieldParentPtr(Cursor, "listen_button", listener.?);
var event = @ptrCast( const event = @ptrCast(
*c.wlr_event_pointer_button, *c.wlr_event_pointer_button,
@alignCast(@alignOf(*c.wlr_event_pointer_button), data), @alignCast(@alignOf(*c.wlr_event_pointer_button), data),
); );
@ -271,7 +273,7 @@ pub const Cursor = struct {
var sy: f64 = undefined; var sy: f64 = undefined;
var surface: ?*c.wlr_surface = null; var surface: ?*c.wlr_surface = null;
var view = cursor.seat.server.desktop_view_at( const view = cursor.seat.server.desktop_view_at(
cursor.wlr_cursor.x, cursor.wlr_cursor.x,
cursor.wlr_cursor.y, cursor.wlr_cursor.y,
&surface, &surface,
@ -293,8 +295,8 @@ pub const Cursor = struct {
fn handle_axis(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { fn handle_axis(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
// This event is forwarded by the cursor when a pointer emits an axis event, // This event is forwarded by the cursor when a pointer emits an axis event,
// for example when you move the scroll wheel. // for example when you move the scroll wheel.
var cursor = @fieldParentPtr(Cursor, "listen_axis", listener.?); const cursor = @fieldParentPtr(Cursor, "listen_axis", listener.?);
var event = @ptrCast( const event = @ptrCast(
*c.wlr_event_pointer_axis, *c.wlr_event_pointer_axis,
@alignCast(@alignOf(*c.wlr_event_pointer_axis), data), @alignCast(@alignOf(*c.wlr_event_pointer_axis), data),
); );
@ -315,15 +317,15 @@ pub const Cursor = struct {
// event. Frame events are sent after regular pointer events to group // event. Frame events are sent after regular pointer events to group
// multiple events together. For instance, two axis events may happen at the // multiple events together. For instance, two axis events may happen at the
// same time, in which case a frame event won't be sent in between. // same time, in which case a frame event won't be sent in between.
var cursor = @fieldParentPtr(Cursor, "listen_frame", listener.?); const cursor = @fieldParentPtr(Cursor, "listen_frame", listener.?);
// Notify the client with pointer focus of the frame event. // Notify the client with pointer focus of the frame event.
c.wlr_seat_pointer_notify_frame(cursor.seat.wlr_seat); c.wlr_seat_pointer_notify_frame(cursor.seat.wlr_seat);
} }
fn handle_request_set_cursor(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { fn handle_request_set_cursor(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
// This event is rasied by the seat when a client provides a cursor image // This event is rasied by the seat when a client provides a cursor image
var cursor = @fieldParentPtr(Cursor, "listen_request_set_cursor", listener.?); const cursor = @fieldParentPtr(Cursor, "listen_request_set_cursor", listener.?);
var event = @ptrCast( const event = @ptrCast(
*c.wlr_seat_pointer_request_set_cursor_event, *c.wlr_seat_pointer_request_set_cursor_event,
@alignCast(@alignOf(*c.wlr_seat_pointer_request_set_cursor_event), data), @alignCast(@alignOf(*c.wlr_seat_pointer_request_set_cursor_event), data),
); );

View file

@ -52,7 +52,7 @@ pub const Keyboard = struct {
fn handle_modifiers(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { fn handle_modifiers(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
// This event is raised when a modifier key, such as shift or alt, is // This event is raised when a modifier key, such as shift or alt, is
// pressed. We simply communicate this to the client. */ // pressed. We simply communicate this to the client. */
var keyboard = @fieldParentPtr(Keyboard, "listen_modifiers", listener.?); const keyboard = @fieldParentPtr(Keyboard, "listen_modifiers", listener.?);
// A seat can only have one keyboard, but this is a limitation of the // A seat can only have one keyboard, but this is a limitation of the
// Wayland protocol - not wlroots. We assign all connected keyboards to the // Wayland protocol - not wlroots. We assign all connected keyboards to the

View file

@ -18,7 +18,7 @@ pub fn main() !void {
// Spawn an instance of alacritty // Spawn an instance of alacritty
// const argv = [_][]const u8{ "/bin/sh", "-c", "WAYLAND_DEBUG=1 alacritty" }; // const argv = [_][]const u8{ "/bin/sh", "-c", "WAYLAND_DEBUG=1 alacritty" };
const argv = [_][]const u8{ "/bin/sh", "-c", "alacritty" }; const argv = [_][]const u8{ "/bin/sh", "-c", "alacritty" };
var child = try std.ChildProcess.init(&argv, std.heap.c_allocator); const child = try std.ChildProcess.init(&argv, std.heap.c_allocator);
try std.ChildProcess.spawn(child); try std.ChildProcess.spawn(child);
server.run(); server.run();

View file

@ -55,8 +55,8 @@ pub const Output = struct {
fn handle_frame(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { fn handle_frame(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
// This function is called every time an output is ready to display a frame, // This function is called every time an output is ready to display a frame,
// generally at the output's refresh rate (e.g. 60Hz). // generally at the output's refresh rate (e.g. 60Hz).
var output = @fieldParentPtr(Output, "listen_frame", listener.?); const output = @fieldParentPtr(Output, "listen_frame", listener.?);
var renderer = output.server.wlr_renderer; const renderer = output.server.wlr_renderer;
var now: c.struct_timespec = undefined; var now: c.struct_timespec = undefined;
_ = c.clock_gettime(c.CLOCK_MONOTONIC, &now); _ = c.clock_gettime(c.CLOCK_MONOTONIC, &now);
@ -79,7 +79,7 @@ pub const Output = struct {
// The first view in the list is "on top" so iterate in reverse. // The first view in the list is "on top" so iterate in reverse.
var it = output.server.views.last; var it = output.server.views.last;
while (it) |node| : (it = node.prev) { while (it) |node| : (it = node.prev) {
var view = &node.data; const view = &node.data;
if (!view.mapped) { if (!view.mapped) {
// An unmapped view should not be rendered. // An unmapped view should not be rendered.
continue; continue;
@ -112,18 +112,18 @@ pub const Output = struct {
fn render_surface(opt_surface: ?*c.wlr_surface, sx: c_int, sy: c_int, data: ?*c_void) callconv(.C) void { fn render_surface(opt_surface: ?*c.wlr_surface, sx: c_int, sy: c_int, data: ?*c_void) callconv(.C) void {
// wlroots says this will never be null // wlroots says this will never be null
var surface = opt_surface.?; const surface = opt_surface.?;
// This function is called for every surface that needs to be rendered. // This function is called for every surface that needs to be rendered.
var rdata = @ptrCast(*RenderData, @alignCast(@alignOf(RenderData), data)); const rdata = @ptrCast(*RenderData, @alignCast(@alignOf(RenderData), data));
var view = rdata.view; const view = rdata.view;
var output = rdata.output; const output = rdata.output;
// We first obtain a wlr_texture, which is a GPU resource. wlroots // We first obtain a wlr_texture, which is a GPU resource. wlroots
// automatically handles negotiating these with the client. The underlying // automatically handles negotiating these with the client. The underlying
// resource could be an opaque handle passed from the client, or the client // resource could be an opaque handle passed from the client, or the client
// could have sent a pixel buffer which we copied to the GPU, or a few other // could have sent a pixel buffer which we copied to the GPU, or a few other
// means. You don't have to worry about this, wlroots takes care of it. // means. You don't have to worry about this, wlroots takes care of it.
var texture = c.wlr_surface_get_texture(surface); const texture = c.wlr_surface_get_texture(surface);
if (texture == null) { if (texture == null) {
return; return;
} }
@ -140,7 +140,7 @@ pub const Output = struct {
// We also have to apply the scale factor for HiDPI outputs. This is only // We also have to apply the scale factor for HiDPI outputs. This is only
// part of the puzzle, TinyWL does not fully support HiDPI. // part of the puzzle, TinyWL does not fully support HiDPI.
var box = c.wlr_box{ const box = c.wlr_box{
.x = @floatToInt(c_int, ox * output.scale), .x = @floatToInt(c_int, ox * output.scale),
.y = @floatToInt(c_int, oy * output.scale), .y = @floatToInt(c_int, oy * output.scale),
.width = @floatToInt(c_int, @intToFloat(f32, surface.current.width) * output.scale), .width = @floatToInt(c_int, @intToFloat(f32, surface.current.width) * output.scale),
@ -157,7 +157,7 @@ pub const Output = struct {
// Naturally you can do this any way you like, for example to make a 3D // Naturally you can do this any way you like, for example to make a 3D
// compositor. // compositor.
var matrix: [9]f32 = undefined; var matrix: [9]f32 = undefined;
var transform = c.wlr_output_transform_invert(surface.current.transform); const transform = c.wlr_output_transform_invert(surface.current.transform);
c.wlr_matrix_project_box(&matrix, &box, transform, 0.0, &output.transform_matrix); c.wlr_matrix_project_box(&matrix, &box, transform, 0.0, &output.transform_matrix);
// This takes our matrix, the texture, and an alpha, and performs the actual // This takes our matrix, the texture, and an alpha, and performs the actual

View file

@ -47,7 +47,7 @@ pub const Seat = struct {
fn add_keyboard(self: *@This(), device: *c.wlr_input_device) !void { fn add_keyboard(self: *@This(), device: *c.wlr_input_device) !void {
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); const node = try self.keyboards.allocateNode(self.server.allocator);
try node.data.init(self, device); try node.data.init(self, device);
self.keyboards.append(node); self.keyboards.append(node);
} }
@ -62,8 +62,8 @@ pub const Seat = struct {
fn handle_new_input(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { fn handle_new_input(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
// This event is raised by the backend when a new input device becomes available. // This event is raised by the backend when a new input device becomes available.
var seat = @fieldParentPtr(Seat, "listen_new_input", listener.?); const seat = @fieldParentPtr(Seat, "listen_new_input", listener.?);
var device = @ptrCast(*c.wlr_input_device, @alignCast(@alignOf(*c.wlr_input_device), data)); const device = @ptrCast(*c.wlr_input_device, @alignCast(@alignOf(*c.wlr_input_device), data));
switch (device.type) { switch (device.type) {
.WLR_INPUT_DEVICE_KEYBOARD => seat.add_keyboard(device) catch unreachable, .WLR_INPUT_DEVICE_KEYBOARD => seat.add_keyboard(device) catch unreachable,

View file

@ -144,11 +144,11 @@ pub const Server = struct {
} }
fn handle_new_output(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { fn handle_new_output(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
var server = @fieldParentPtr(Server, "listen_new_output", listener.?); const server = @fieldParentPtr(Server, "listen_new_output", listener.?);
var wlr_output = @ptrCast(*c.wlr_output, @alignCast(@alignOf(*c.wlr_output), data)); const wlr_output = @ptrCast(*c.wlr_output, @alignCast(@alignOf(*c.wlr_output), data));
// TODO: Handle failure // TODO: Handle failure
var node = server.outputs.allocateNode(server.allocator) catch unreachable; const node = server.outputs.allocateNode(server.allocator) catch unreachable;
node.data.init(server, wlr_output) catch unreachable; node.data.init(server, wlr_output) catch unreachable;
server.outputs.append(node); server.outputs.append(node);
} }
@ -156,15 +156,15 @@ pub const Server = struct {
fn handle_new_xdg_surface(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { fn handle_new_xdg_surface(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 // This event is raised when wlr_xdg_shell receives a new xdg surface from a
// client, either a toplevel (application window) or popup. // client, either a toplevel (application window) or popup.
var server = @fieldParentPtr(Server, "listen_new_xdg_surface", listener.?); const server = @fieldParentPtr(Server, "listen_new_xdg_surface", listener.?);
var wlr_xdg_surface = @ptrCast(*c.wlr_xdg_surface, @alignCast(@alignOf(*c.wlr_xdg_surface), data)); const wlr_xdg_surface = @ptrCast(*c.wlr_xdg_surface, @alignCast(@alignOf(*c.wlr_xdg_surface), data));
if (wlr_xdg_surface.role != c.enum_wlr_xdg_surface_role.WLR_XDG_SURFACE_ROLE_TOPLEVEL) { if (wlr_xdg_surface.role != c.enum_wlr_xdg_surface_role.WLR_XDG_SURFACE_ROLE_TOPLEVEL) {
return; return;
} }
// Create a View to handle this toplevel surface // Create a View to handle this toplevel surface
var node = server.views.allocateNode(server.allocator) catch unreachable; const node = server.views.allocateNode(server.allocator) catch unreachable;
node.data.init(server, wlr_xdg_surface); node.data.init(server, wlr_xdg_surface);
server.views.append(node); server.views.append(node);
} }

View file

@ -34,26 +34,26 @@ pub const View = struct {
self.listen_destroy.notify = handle_destroy; self.listen_destroy.notify = handle_destroy;
c.wl_signal_add(&self.wlr_xdg_surface.events.destroy, &self.listen_destroy); c.wl_signal_add(&self.wlr_xdg_surface.events.destroy, &self.listen_destroy);
// var toplevel = xdg_surface.unnamed_160.toplevel; // const 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);
} }
fn handle_map(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { fn handle_map(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
// Called when the surface is mapped, or ready to display on-screen. // Called when the surface is mapped, or ready to display on-screen.
var view = @fieldParentPtr(View, "listen_map", listener.?); const view = @fieldParentPtr(View, "listen_map", listener.?);
view.mapped = true; view.mapped = true;
view.focus(view.wlr_xdg_surface.surface); view.focus(view.wlr_xdg_surface.surface);
} }
fn handle_unmap(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { fn handle_unmap(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
var view = @fieldParentPtr(View, "listen_unmap", listener.?); const view = @fieldParentPtr(View, "listen_unmap", listener.?);
view.mapped = false; view.mapped = false;
} }
fn handle_destroy(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { fn handle_destroy(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
var view = @fieldParentPtr(View, "listen_destroy", listener.?); const view = @fieldParentPtr(View, "listen_destroy", listener.?);
var server = view.server; const server = view.server;
var it = server.views.first; var it = server.views.first;
const target = while (it) |node| : (it = node.next) { const target = while (it) |node| : (it = node.next) {
@ -88,7 +88,7 @@ pub const View = struct {
// Deactivate the previously focused surface. This lets the client know // Deactivate the previously focused surface. This lets the client know
// it no longer has focus and the client will repaint accordingly, e.g. // it no longer has focus and the client will repaint accordingly, e.g.
// stop displaying a caret. // stop displaying a caret.
var prev_xdg_surface = c.wlr_xdg_surface_from_wlr_surface(prev_surface); const prev_xdg_surface = c.wlr_xdg_surface_from_wlr_surface(prev_surface);
_ = c.wlr_xdg_toplevel_set_activated(prev_xdg_surface, false); _ = c.wlr_xdg_toplevel_set_activated(prev_xdg_surface, false);
} }
@ -110,7 +110,7 @@ pub const View = struct {
// 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
// clients without additional work on your part. // clients without additional work on your part.
var keyboard: *c.wlr_keyboard = c.wlr_seat_get_keyboard(wlr_seat); const keyboard: *c.wlr_keyboard = c.wlr_seat_get_keyboard(wlr_seat);
c.wlr_seat_keyboard_notify_enter( c.wlr_seat_keyboard_notify_enter(
wlr_seat, wlr_seat,
self.wlr_xdg_surface.surface, self.wlr_xdg_surface.surface,
@ -126,15 +126,15 @@ pub const View = struct {
// coordinates lx and ly (in output Layout Coordinates). If so, it sets the // coordinates lx and ly (in output Layout Coordinates). If so, it sets the
// surface pointer to that wlr_surface and the sx and sy coordinates to the // surface pointer to that wlr_surface and the sx and sy coordinates to the
// coordinates relative to that surface's top-left corner. // coordinates relative to that surface's top-left corner.
var view_sx = lx - @intToFloat(f64, self.x); const view_sx = lx - @intToFloat(f64, self.x);
var view_sy = ly - @intToFloat(f64, self.y); const view_sy = ly - @intToFloat(f64, self.y);
// This variable seems to have been unsued in TinyWL // This variable seems to have been unsued in TinyWL
// struct wlr_surface_state *state = &view->xdg_surface->surface->current; // struct wlr_surface_state *state = &view->xdg_surface->surface->current;
var _sx: f64 = undefined; var _sx: f64 = undefined;
var _sy: f64 = undefined; var _sy: f64 = undefined;
var _surface = c.wlr_xdg_surface_surface_at(self.wlr_xdg_surface, view_sx, view_sy, &_sx, &_sy); const _surface = c.wlr_xdg_surface_surface_at(self.wlr_xdg_surface, view_sx, view_sy, &_sx, &_sy);
if (_surface) |surface_at| { if (_surface) |surface_at| {
sx.* = _sx; sx.* = _sx;