river/src/view.zig

149 lines
5.3 KiB
Zig
Raw Normal View History

2020-03-22 14:42:55 -07:00
const std = @import("std");
const c = @import("c.zig").c;
2020-03-23 08:50:20 -07:00
const Server = @import("server.zig").Server;
2020-03-22 14:42:55 -07:00
pub const View = struct {
server: *Server,
2020-03-23 06:04:54 -07:00
wlr_xdg_surface: *c.wlr_xdg_surface,
mapped: bool,
x: c_int,
y: c_int,
2020-03-23 06:04:54 -07:00
listen_map: c.wl_listener,
listen_unmap: c.wl_listener,
listen_destroy: c.wl_listener,
// listen_request_move: c.wl_listener,
// listen_request_resize: c.wl_listener,
pub fn init(self: *@This(), server: *Server, wlr_xdg_surface: *c.wlr_xdg_surface) void {
self.server = server;
self.wlr_xdg_surface = wlr_xdg_surface;
self.mapped = false;
self.x = 0;
self.y = 0;
2020-03-22 14:42:55 -07:00
self.listen_map.notify = handle_map;
c.wl_signal_add(&self.wlr_xdg_surface.events.map, &self.listen_map);
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);
2020-03-23 06:04:54 -07:00
// 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_resize, &view.*.request_resize);
}
fn handle_map(listener: [*c]c.wl_listener, data: ?*c_void) callconv(.C) void {
// Called when the surface is mapped, or ready to display on-screen.
2020-03-23 08:50:20 -07:00
var view = @fieldParentPtr(View, "listen_map", listener);
view.mapped = true;
view.focus(view.wlr_xdg_surface.surface);
2020-03-23 06:04:54 -07:00
}
2020-03-22 14:42:55 -07:00
2020-03-23 06:04:54 -07:00
fn handle_unmap(listener: [*c]c.wl_listener, data: ?*c_void) callconv(.C) void {
2020-03-23 08:50:20 -07:00
var view = @fieldParentPtr(View, "listen_unmap", listener);
2020-03-23 06:04:54 -07:00
view.*.mapped = false;
2020-03-22 14:42:55 -07:00
}
2020-03-23 06:04:54 -07:00
fn handle_destroy(listener: [*c]c.wl_listener, data: ?*c_void) callconv(.C) void {
2020-03-23 08:50:20 -07:00
var view = @fieldParentPtr(View, "listen_destroy", listener);
var server = view.server;
var it = server.views.first;
const target = while (it) |node| : (it = node.next) {
if (&node.data == view) {
break node;
2020-03-23 06:04:54 -07:00
}
} else unreachable;
server.views.remove(target);
server.views.destroyNode(target, server.allocator);
2020-03-22 14:42:55 -07:00
}
2020-03-23 06:04:54 -07:00
// fn xdg_toplevel_request_move(listener: [*c]c.wl_listener, data: ?*c_void) callconv(.C) void {
// // ignore for now
// }
// fn xdg_toplevel_request_resize(listener: [*c]c.wl_listener, data: ?*c_void) callconv(.C) void {
// // ignore for now
// }
2020-03-23 08:50:20 -07:00
fn focus(self: *@This(), surface: *c.wlr_surface) void {
const server = self.server;
const wlr_seat = server.seat.wlr_seat;
const prev_surface = wlr_seat.keyboard_state.focused_surface;
2020-03-23 06:04:54 -07:00
if (prev_surface == surface) {
// Don't re-focus an already focused surface.
return;
2020-03-22 14:42:55 -07:00
}
2020-03-23 06:04:54 -07:00
if (prev_surface != null) {
// Deactivate the previously focused surface. This lets the client know
// it no longer has focus and the client will repaint accordingly, e.g.
// stop displaying a caret.
var prev_xdg_surface = c.wlr_xdg_surface_from_wlr_surface(prev_surface);
_ = c.wlr_xdg_toplevel_set_activated(prev_xdg_surface, false);
}
// Find the node
var it = server.views.first;
const target = while (it) |node| : (it = node.next) {
if (&node.data == self) {
break node;
2020-03-23 06:04:54 -07:00
}
} else unreachable;
// Move the view to the front
server.views.remove(target);
server.views.prepend(target);
2020-03-23 06:04:54 -07:00
// Activate the new surface
_ = c.wlr_xdg_toplevel_set_activated(self.wlr_xdg_surface, true);
2020-03-23 06:04:54 -07:00
// Tell the seat to have the keyboard enter this surface. wlroots will keep
// track of this and automatically send key events to the appropriate
// clients without additional work on your part.
2020-03-23 08:50:20 -07:00
var keyboard = c.wlr_seat_get_keyboard(wlr_seat);
c.wlr_seat_keyboard_notify_enter(
wlr_seat,
self.wlr_xdg_surface.surface,
2020-03-23 08:50:20 -07:00
&keyboard.*.keycodes,
keyboard.*.num_keycodes,
&keyboard.*.modifiers,
);
2020-03-22 14:42:55 -07:00
}
2020-03-23 06:04:54 -07:00
fn is_at(self: *@This(), lx: f64, ly: f64, surface: *?*c.wlr_surface, sx: *f64, sy: *f64) bool {
// XDG toplevels may have nested surfaces, such as popup windows for context
// menus or tooltips. This function tests if any of those are underneath 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
// coordinates relative to that surface's top-left corner.
2020-03-23 08:50:20 -07:00
var view_sx = lx - @intToFloat(f64, self.x);
var view_sy = ly - @intToFloat(f64, self.y);
2020-03-23 06:04:54 -07:00
// This variable seems to have been unsued in TinyWL
// struct wlr_surface_state *state = &view->xdg_surface->surface->current;
var _sx: f64 = undefined;
var _sy: f64 = undefined;
2020-03-23 08:50:20 -07:00
var _surface = c.wlr_xdg_surface_surface_at(self.wlr_xdg_surface, view_sx, view_sy, &_sx, &_sy);
2020-03-22 14:42:55 -07:00
2020-03-23 06:04:54 -07:00
if (_surface) |surface_at| {
sx.* = _sx;
sy.* = _sy;
surface.* = surface_at;
return true;
2020-03-22 14:42:55 -07:00
}
2020-03-23 06:04:54 -07:00
return false;
2020-03-22 14:42:55 -07:00
}
2020-03-23 06:04:54 -07:00
};