2020-03-22 14:42:55 -07:00
|
|
|
const std = @import("std");
|
2020-03-29 10:36:15 -07:00
|
|
|
const c = @import("c.zig");
|
2020-03-22 14:42:55 -07:00
|
|
|
|
2020-03-25 07:59:24 -07:00
|
|
|
const Root = @import("root.zig").Root;
|
2020-04-03 09:53:36 -07:00
|
|
|
const ViewStack = @import("view_stack.zig").ViewStack;
|
2020-03-23 08:50:20 -07:00
|
|
|
|
2020-03-22 14:42:55 -07:00
|
|
|
pub const View = struct {
|
2020-03-24 12:48:38 -07:00
|
|
|
const Self = @This();
|
|
|
|
|
2020-03-25 07:59:24 -07:00
|
|
|
root: *Root,
|
2020-03-23 06:04:54 -07:00
|
|
|
wlr_xdg_surface: *c.wlr_xdg_surface,
|
|
|
|
|
2020-03-23 13:51:46 -07:00
|
|
|
mapped: bool,
|
2020-03-26 13:32:30 -07:00
|
|
|
|
2020-04-02 04:44:24 -07:00
|
|
|
pub const Box = struct {
|
2020-04-01 08:50:49 -07:00
|
|
|
x: i32,
|
|
|
|
y: i32,
|
|
|
|
width: u32,
|
|
|
|
height: u32,
|
|
|
|
};
|
|
|
|
|
2020-04-02 04:44:24 -07:00
|
|
|
current_box: Box,
|
|
|
|
pending_box: ?Box,
|
|
|
|
|
|
|
|
current_tags: u32,
|
|
|
|
pending_tags: ?u32,
|
2020-03-26 13:32:30 -07:00
|
|
|
|
|
|
|
pending_serial: ?u32,
|
|
|
|
|
|
|
|
// This is what we render while a transaction is in progress
|
|
|
|
stashed_buffer: ?*c.wlr_buffer,
|
2020-03-23 13:51:46 -07:00
|
|
|
|
2020-03-23 06:04:54 -07:00
|
|
|
listen_map: c.wl_listener,
|
|
|
|
listen_unmap: c.wl_listener,
|
|
|
|
listen_destroy: c.wl_listener,
|
2020-03-26 13:32:30 -07:00
|
|
|
listen_commit: c.wl_listener,
|
2020-03-23 06:04:54 -07:00
|
|
|
// listen_request_move: c.wl_listener,
|
|
|
|
// listen_request_resize: c.wl_listener,
|
|
|
|
|
2020-04-01 08:50:49 -07:00
|
|
|
pub fn init(self: *Self, root: *Root, wlr_xdg_surface: *c.wlr_xdg_surface, tags: u32) void {
|
2020-03-25 07:59:24 -07:00
|
|
|
self.root = root;
|
2020-03-23 13:51:46 -07:00
|
|
|
self.wlr_xdg_surface = wlr_xdg_surface;
|
|
|
|
|
2020-03-28 12:27:50 -07:00
|
|
|
// Inform the xdg toplevel that it is tiled.
|
|
|
|
// For example this prevents firefox from drawing shadows around itself
|
|
|
|
_ = c.wlr_xdg_toplevel_set_tiled(self.wlr_xdg_surface, c.WLR_EDGE_LEFT |
|
|
|
|
c.WLR_EDGE_RIGHT | c.WLR_EDGE_TOP | c.WLR_EDGE_BOTTOM);
|
|
|
|
|
2020-03-23 13:51:46 -07:00
|
|
|
self.mapped = false;
|
2020-04-01 08:50:49 -07:00
|
|
|
|
2020-04-02 04:44:24 -07:00
|
|
|
self.current_box = Box{
|
2020-03-26 13:32:30 -07:00
|
|
|
.x = 0,
|
|
|
|
.y = 0,
|
|
|
|
.height = 0,
|
|
|
|
.width = 0,
|
|
|
|
};
|
2020-04-02 04:44:24 -07:00
|
|
|
self.pending_box = null;
|
|
|
|
|
|
|
|
self.current_tags = tags;
|
|
|
|
self.pending_tags = null;
|
2020-04-01 08:50:49 -07:00
|
|
|
|
|
|
|
self.pending_serial = null;
|
|
|
|
|
2020-03-26 13:32:30 -07:00
|
|
|
self.stashed_buffer = null;
|
2020-03-22 14:42:55 -07:00
|
|
|
|
2020-03-24 13:13:56 -07:00
|
|
|
self.listen_map.notify = handleMap;
|
2020-03-23 13:51:46 -07:00
|
|
|
c.wl_signal_add(&self.wlr_xdg_surface.events.map, &self.listen_map);
|
|
|
|
|
2020-03-24 13:13:56 -07:00
|
|
|
self.listen_unmap.notify = handleUnmap;
|
2020-03-23 13:51:46 -07:00
|
|
|
c.wl_signal_add(&self.wlr_xdg_surface.events.unmap, &self.listen_unmap);
|
|
|
|
|
2020-03-24 13:13:56 -07:00
|
|
|
self.listen_destroy.notify = handleDestroy;
|
2020-03-23 13:51:46 -07:00
|
|
|
c.wl_signal_add(&self.wlr_xdg_surface.events.destroy, &self.listen_destroy);
|
2020-03-23 06:04:54 -07:00
|
|
|
|
2020-03-26 13:32:30 -07:00
|
|
|
self.listen_commit.notify = handleCommit;
|
|
|
|
c.wl_signal_add(&self.wlr_xdg_surface.surface.*.events.commit, &self.listen_commit);
|
|
|
|
|
2020-03-24 12:35:45 -07:00
|
|
|
// const toplevel = xdg_surface.unnamed_160.toplevel;
|
2020-03-24 12:03:48 -07:00
|
|
|
// c.wl_signal_add(&toplevel.events.request_move, &view.request_move);
|
|
|
|
// c.wl_signal_add(&toplevel.events.request_resize, &view.request_resize);
|
2020-03-23 06:04:54 -07:00
|
|
|
}
|
|
|
|
|
2020-03-29 06:58:04 -07:00
|
|
|
pub fn needsConfigure(self: Self) bool {
|
2020-04-02 04:44:24 -07:00
|
|
|
if (self.pending_box) |pending_box| {
|
|
|
|
return pending_box.width != self.current_box.width or
|
|
|
|
pending_box.height != self.current_box.height;
|
2020-04-01 08:50:49 -07:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-26 13:32:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn configurePending(self: *Self) void {
|
2020-04-02 04:44:24 -07:00
|
|
|
if (self.pending_box) |pending_box| {
|
2020-04-01 08:50:49 -07:00
|
|
|
self.pending_serial = c.wlr_xdg_toplevel_set_size(
|
|
|
|
self.wlr_xdg_surface,
|
2020-04-04 14:26:15 -07:00
|
|
|
pending_box.width - self.root.border_width * 2,
|
|
|
|
pending_box.height - self.root.border_width * 2,
|
2020-04-01 08:50:49 -07:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// TODO: log warning
|
|
|
|
}
|
2020-03-26 13:32:30 -07:00
|
|
|
}
|
|
|
|
|
2020-03-29 06:58:04 -07:00
|
|
|
pub fn sendFrameDone(self: Self) void {
|
2020-03-26 13:32:30 -07:00
|
|
|
var now: c.struct_timespec = undefined;
|
|
|
|
_ = c.clock_gettime(c.CLOCK_MONOTONIC, &now);
|
|
|
|
c.wlr_surface_send_frame_done(self.wlr_xdg_surface.surface, &now);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn dropStashedBuffer(self: *Self) void {
|
|
|
|
// TODO: log debug error
|
|
|
|
if (self.stashed_buffer) |buffer| {
|
|
|
|
c.wlr_buffer_unref(buffer);
|
|
|
|
self.stashed_buffer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stashBuffer(self: *Self) void {
|
|
|
|
// TODO: log debug error if there is already a saved buffer
|
|
|
|
const wlr_surface = self.wlr_xdg_surface.surface;
|
|
|
|
if (c.wlr_surface_has_buffer(wlr_surface)) {
|
|
|
|
_ = c.wlr_buffer_ref(wlr_surface.*.buffer);
|
|
|
|
self.stashed_buffer = wlr_surface.*.buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 13:13:56 -07:00
|
|
|
fn handleMap(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
|
2020-03-23 06:04:54 -07:00
|
|
|
// Called when the surface is mapped, or ready to display on-screen.
|
2020-03-24 12:35:45 -07:00
|
|
|
const view = @fieldParentPtr(View, "listen_map", listener.?);
|
2020-03-23 08:50:20 -07:00
|
|
|
view.mapped = true;
|
|
|
|
view.focus(view.wlr_xdg_surface.surface);
|
2020-03-26 13:43:02 -07:00
|
|
|
view.root.arrange();
|
2020-03-23 06:04:54 -07:00
|
|
|
}
|
2020-03-22 14:42:55 -07:00
|
|
|
|
2020-03-24 13:13:56 -07:00
|
|
|
fn handleUnmap(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
|
2020-03-24 12:35:45 -07:00
|
|
|
const view = @fieldParentPtr(View, "listen_unmap", listener.?);
|
2020-03-26 18:05:57 -07:00
|
|
|
const root = view.root;
|
2020-03-24 12:03:48 -07:00
|
|
|
view.mapped = false;
|
2020-03-26 13:43:02 -07:00
|
|
|
|
2020-03-26 18:05:57 -07:00
|
|
|
if (root.focused_view) |current_focus| {
|
|
|
|
// If the view being unmapped is focused
|
|
|
|
if (current_focus == view) {
|
2020-04-03 09:53:36 -07:00
|
|
|
// Focus the previous view. This clears the focus if there are no visible views.
|
|
|
|
root.focusPrevView();
|
2020-03-26 18:05:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
root.arrange();
|
2020-03-22 14:42:55 -07:00
|
|
|
}
|
|
|
|
|
2020-03-24 13:13:56 -07:00
|
|
|
fn handleDestroy(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
|
2020-03-24 12:35:45 -07:00
|
|
|
const view = @fieldParentPtr(View, "listen_destroy", listener.?);
|
2020-03-25 07:59:24 -07:00
|
|
|
const root = view.root;
|
2020-03-23 13:51:46 -07:00
|
|
|
|
2020-04-03 09:53:36 -07:00
|
|
|
const node = @fieldParentPtr(ViewStack.Node, "view", view);
|
|
|
|
root.views.remove(node);
|
|
|
|
root.server.allocator.destroy(node);
|
2020-03-22 14:42:55 -07:00
|
|
|
}
|
|
|
|
|
2020-03-26 13:32:30 -07:00
|
|
|
fn handleCommit(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
|
|
|
|
const view = @fieldParentPtr(View, "listen_commit", listener.?);
|
|
|
|
if (view.pending_serial) |s| {
|
|
|
|
if (s == view.wlr_xdg_surface.configure_serial) {
|
|
|
|
view.root.notifyConfigured();
|
|
|
|
view.pending_serial = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: check for unexpected change in size and react as needed
|
|
|
|
}
|
|
|
|
|
2020-03-24 13:13:56 -07:00
|
|
|
// fn xdgToplevelRequestMove(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
|
2020-03-23 06:04:54 -07:00
|
|
|
// // ignore for now
|
|
|
|
// }
|
|
|
|
|
2020-03-24 13:13:56 -07:00
|
|
|
// fn xdgToplevelRequestResize(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
|
2020-03-23 06:04:54 -07:00
|
|
|
// // ignore for now
|
|
|
|
// }
|
|
|
|
|
2020-03-24 12:48:38 -07:00
|
|
|
fn focus(self: *Self, surface: *c.wlr_surface) void {
|
2020-03-25 07:59:24 -07:00
|
|
|
const root = self.root;
|
|
|
|
const wlr_seat = root.server.seat.wlr_seat;
|
2020-03-23 08:50:20 -07:00
|
|
|
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.
|
2020-03-26 18:05:57 -07:00
|
|
|
// TODO: debug message?
|
2020-03-23 06:04:54 -07:00
|
|
|
return;
|
2020-03-22 14:42:55 -07:00
|
|
|
}
|
2020-03-23 06:04:54 -07:00
|
|
|
|
2020-03-26 18:05:57 -07:00
|
|
|
root.focused_view = self;
|
|
|
|
|
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.
|
2020-03-24 12:35:45 -07:00
|
|
|
const prev_xdg_surface = c.wlr_xdg_surface_from_wlr_surface(prev_surface);
|
2020-03-23 06:04:54 -07:00
|
|
|
_ = c.wlr_xdg_toplevel_set_activated(prev_xdg_surface, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Activate the new surface
|
2020-03-23 13:51:46 -07:00
|
|
|
_ = 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-24 12:35:45 -07:00
|
|
|
const keyboard: *c.wlr_keyboard = c.wlr_seat_get_keyboard(wlr_seat);
|
2020-03-23 08:50:20 -07:00
|
|
|
c.wlr_seat_keyboard_notify_enter(
|
|
|
|
wlr_seat,
|
2020-03-23 13:51:46 -07:00
|
|
|
self.wlr_xdg_surface.surface,
|
2020-03-24 12:03:48 -07:00
|
|
|
&keyboard.keycodes,
|
|
|
|
keyboard.num_keycodes,
|
|
|
|
&keyboard.modifiers,
|
2020-03-23 08:50:20 -07:00
|
|
|
);
|
2020-03-22 14:42:55 -07:00
|
|
|
}
|
|
|
|
|
2020-03-29 06:58:04 -07:00
|
|
|
fn isAt(self: Self, lx: f64, ly: f64, surface: *?*c.wlr_surface, sx: *f64, sy: *f64) bool {
|
2020-03-23 06:04:54 -07:00
|
|
|
// 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-04-02 04:44:24 -07:00
|
|
|
const view_sx = lx - @intToFloat(f64, self.current_box.x);
|
|
|
|
const view_sy = ly - @intToFloat(f64, self.current_box.y);
|
2020-03-23 06:04:54 -07:00
|
|
|
|
|
|
|
// This variable seems to have been unsued in TinyWL
|
2020-04-02 04:44:24 -07:00
|
|
|
// struct wlr_surface_box *state = &view->xdg_surface->surface->current;
|
2020-03-23 06:04:54 -07:00
|
|
|
|
|
|
|
var _sx: f64 = undefined;
|
|
|
|
var _sy: f64 = undefined;
|
2020-03-24 12:35:45 -07:00
|
|
|
const _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
|
|
|
};
|