2026-01-02 22:53:34 +07:00
|
|
|
#include <wayland-server-core.h>
|
|
|
|
|
|
2026-05-19 12:17:13 +07:00
|
|
|
#include "cursor.h"
|
2026-04-26 15:08:37 +07:00
|
|
|
#include "layout.h"
|
2026-05-03 22:56:12 +07:00
|
|
|
#include "toplevel.h"
|
2026-04-28 18:46:46 +07:00
|
|
|
#include "types.h"
|
2026-01-02 22:53:34 +07:00
|
|
|
|
2026-05-19 12:17:13 +07:00
|
|
|
/*
|
|
|
|
|
* returns surface at given cursor coordinates
|
|
|
|
|
* and coordinates inside of it to process input event
|
|
|
|
|
*/
|
|
|
|
|
void client_from_coords(absn_server *server, double x, double y, struct wlr_surface **rsurface, absn_toplevel **rtoplevel, absn_layer_surface **rlayer_surface, double *rx, double *ry)
|
|
|
|
|
{
|
|
|
|
|
struct wlr_scene_node *pnode = NULL, *node = NULL;
|
|
|
|
|
struct wlr_scene_buffer *buffer = NULL;
|
|
|
|
|
struct wlr_surface *surface = NULL;
|
|
|
|
|
|
|
|
|
|
absn_layer_surface *layer_surface = NULL;
|
|
|
|
|
absn_toplevel *toplevel = NULL;
|
|
|
|
|
|
|
|
|
|
for (int i = LAYERS_COUNT - 1; i > 0; --i) {
|
|
|
|
|
node = wlr_scene_node_at(&server->layers[i]->node, x, y, rx, ry);
|
|
|
|
|
if (!node) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (node->type == WLR_SCENE_NODE_BUFFER) {
|
|
|
|
|
buffer = wlr_scene_buffer_from_node(node);
|
|
|
|
|
surface = wlr_scene_surface_try_from_buffer(buffer)->surface;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (pnode = node; pnode && !toplevel; pnode = &pnode->parent->node)
|
|
|
|
|
toplevel = pnode->data;
|
|
|
|
|
|
|
|
|
|
if (toplevel && toplevel->type == LAYER_SURFACE) {
|
|
|
|
|
toplevel = NULL;
|
|
|
|
|
layer_surface = pnode->data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rsurface)
|
|
|
|
|
*rsurface = surface;
|
|
|
|
|
if (rtoplevel)
|
|
|
|
|
*rtoplevel = toplevel;
|
|
|
|
|
if (rlayer_surface)
|
|
|
|
|
*rlayer_surface = layer_surface;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 18:16:40 +07:00
|
|
|
void reset_cursor_mode(absn_server *server)
|
2026-01-02 22:53:34 +07:00
|
|
|
{
|
2026-05-14 18:16:40 +07:00
|
|
|
server->cursor_mode = CURSOR_PASSTHROUGH;
|
|
|
|
|
wlr_cursor_set_xcursor(server->cursor, server->cursor_mgr, "default");
|
|
|
|
|
if (server->focused_toplevel) {
|
|
|
|
|
wlr_xdg_toplevel_set_resizing(server->focused_toplevel->xdg, false);
|
|
|
|
|
}
|
2026-01-02 22:53:34 +07:00
|
|
|
}
|
|
|
|
|
|
2026-05-14 18:16:40 +07:00
|
|
|
static void process_cursor_move(absn_server *server)
|
2026-04-28 18:46:46 +07:00
|
|
|
{
|
2026-05-14 18:16:40 +07:00
|
|
|
struct absn_toplevel *toplevel = server->focused_toplevel;
|
2026-04-25 14:09:14 +07:00
|
|
|
|
2026-05-17 20:03:02 +07:00
|
|
|
if (!toplevel) {
|
2026-05-14 18:16:40 +07:00
|
|
|
return;
|
2026-05-17 20:03:02 +07:00
|
|
|
}
|
2026-04-25 14:09:14 +07:00
|
|
|
|
2026-05-14 18:16:40 +07:00
|
|
|
if (toplevel->fullscreen) {
|
|
|
|
|
toplevel->prev_geom = toplevel->geom;
|
|
|
|
|
toplevel_set_fullscreen(toplevel, false);
|
|
|
|
|
}
|
2026-04-25 14:09:14 +07:00
|
|
|
|
2026-05-14 18:16:40 +07:00
|
|
|
uint32_t new_x, new_y;
|
|
|
|
|
new_x = server->cursor->x - server->grab_x + server->grab_geom.x;
|
|
|
|
|
new_y = server->cursor->y - server->grab_y + server->grab_geom.y;
|
|
|
|
|
toplevel_set_pos(toplevel, new_x, new_y);
|
2026-04-26 15:08:37 +07:00
|
|
|
|
2026-05-17 20:03:02 +07:00
|
|
|
if (!toplevel->floating) {
|
2026-05-14 18:16:40 +07:00
|
|
|
toplevel_set_floating(toplevel, true);
|
2026-05-17 20:03:02 +07:00
|
|
|
}
|
2026-05-21 12:34:35 +07:00
|
|
|
|
|
|
|
|
if (toplevel->output != server->focused_output) {
|
|
|
|
|
toplevel->output = server->focused_output;
|
|
|
|
|
toplevel->workspace = toplevel->output->workspace;
|
|
|
|
|
}
|
2026-01-02 22:53:34 +07:00
|
|
|
}
|
|
|
|
|
|
2026-05-14 18:16:40 +07:00
|
|
|
static void apply_resize(absn_toplevel *toplevel, struct wlr_box *new_geom)
|
2026-01-08 22:44:24 +07:00
|
|
|
{
|
2026-05-14 18:16:40 +07:00
|
|
|
if (toplevel->type == TOPLEVEL_XDG) {
|
|
|
|
|
int32_t min_width = toplevel->xdg->current.min_width;
|
|
|
|
|
int32_t min_height = toplevel->xdg->current.min_height;
|
2026-01-08 22:44:24 +07:00
|
|
|
|
2026-05-14 18:16:40 +07:00
|
|
|
int32_t max_width = toplevel->xdg->current.max_width;
|
|
|
|
|
int32_t max_height = toplevel->xdg->current.max_height;
|
2026-01-08 22:44:24 +07:00
|
|
|
|
2026-05-17 20:03:02 +07:00
|
|
|
if (max_width == 0) {
|
2026-05-14 18:16:40 +07:00
|
|
|
max_width = 10000;
|
2026-05-17 20:03:02 +07:00
|
|
|
}
|
2026-01-08 22:44:24 +07:00
|
|
|
|
2026-05-17 20:03:02 +07:00
|
|
|
if (max_height == 0) {
|
2026-05-14 18:16:40 +07:00
|
|
|
max_height = 10000;
|
2026-05-17 20:03:02 +07:00
|
|
|
}
|
2026-04-03 12:43:17 +07:00
|
|
|
|
2026-05-14 18:16:40 +07:00
|
|
|
if (!(new_geom->width >= min_width && new_geom->width <= max_width)) {
|
|
|
|
|
new_geom->width = toplevel->geom.width;
|
|
|
|
|
new_geom->x = toplevel->geom.x;
|
|
|
|
|
}
|
2026-04-28 18:46:46 +07:00
|
|
|
|
2026-05-18 23:25:05 +07:00
|
|
|
if (!(new_geom->height >= min_height && new_geom->height <= max_height)) {
|
2026-05-14 18:16:40 +07:00
|
|
|
new_geom->height = toplevel->geom.height;
|
|
|
|
|
new_geom->y = toplevel->geom.y;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-08 22:44:24 +07:00
|
|
|
|
2026-05-14 18:16:40 +07:00
|
|
|
toplevel_set_geom(toplevel, new_geom);
|
2026-01-08 22:44:24 +07:00
|
|
|
}
|
|
|
|
|
|
2026-05-14 18:16:40 +07:00
|
|
|
static void process_cursor_resize(absn_server *server)
|
2026-04-28 18:46:46 +07:00
|
|
|
{
|
2026-05-14 18:16:40 +07:00
|
|
|
struct absn_toplevel *toplevel = server->focused_toplevel;
|
|
|
|
|
|
2026-05-17 20:03:02 +07:00
|
|
|
if (!toplevel) {
|
2026-05-14 18:16:40 +07:00
|
|
|
return;
|
2026-05-17 20:03:02 +07:00
|
|
|
}
|
2026-05-14 18:16:40 +07:00
|
|
|
|
2026-05-17 20:03:02 +07:00
|
|
|
if (toplevel->resizing) {
|
2026-05-14 18:16:40 +07:00
|
|
|
return;
|
2026-05-17 20:03:02 +07:00
|
|
|
}
|
2026-05-14 18:16:40 +07:00
|
|
|
|
2026-05-17 20:03:02 +07:00
|
|
|
if (toplevel->fullscreen) {
|
2026-05-14 18:16:40 +07:00
|
|
|
toplevel_set_fullscreen(toplevel, false);
|
2026-05-17 20:03:02 +07:00
|
|
|
}
|
2026-05-14 18:16:40 +07:00
|
|
|
|
2026-05-17 20:03:02 +07:00
|
|
|
if (!toplevel->floating) {
|
2026-05-14 18:16:40 +07:00
|
|
|
toplevel_set_floating(toplevel, true);
|
2026-05-17 20:03:02 +07:00
|
|
|
}
|
2026-05-14 18:16:40 +07:00
|
|
|
|
|
|
|
|
int32_t new_x, new_y, new_width, new_height;
|
|
|
|
|
new_x = server->grab_geom.x;
|
|
|
|
|
new_y = server->grab_geom.y;
|
|
|
|
|
new_width = server->grab_geom.width;
|
|
|
|
|
new_height = server->grab_geom.height;
|
|
|
|
|
|
|
|
|
|
int32_t dx = server->cursor->x - server->grab_x;
|
|
|
|
|
int32_t dy = server->cursor->y - server->grab_y;
|
|
|
|
|
|
2026-05-17 20:03:02 +07:00
|
|
|
if (dx == 0 && dy == 0) {
|
2026-05-14 18:16:40 +07:00
|
|
|
return;
|
2026-05-17 20:03:02 +07:00
|
|
|
}
|
2026-05-14 18:16:40 +07:00
|
|
|
|
|
|
|
|
switch (server->resize_corner) {
|
|
|
|
|
case TOP_LEFT:
|
|
|
|
|
new_x += dx;
|
|
|
|
|
new_y += dy;
|
|
|
|
|
new_width -= dx;
|
|
|
|
|
new_height -= dy;
|
|
|
|
|
break;
|
|
|
|
|
case TOP_RIGHT:
|
|
|
|
|
new_y += dy;
|
|
|
|
|
new_width += dx;
|
|
|
|
|
new_height -= dy;
|
|
|
|
|
break;
|
|
|
|
|
case BOTTOM_LEFT:
|
|
|
|
|
new_x += dx;
|
|
|
|
|
new_width -= dx;
|
|
|
|
|
new_height += dy;
|
|
|
|
|
break;
|
|
|
|
|
case BOTTOM_RIGHT:
|
|
|
|
|
new_width += dx;
|
|
|
|
|
new_height += dy;
|
|
|
|
|
break;
|
|
|
|
|
default: // unreachable
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (new_width > 0 && new_height > 0) {
|
|
|
|
|
struct wlr_box new_geometry = {
|
|
|
|
|
.x = new_x,
|
|
|
|
|
.y = new_y,
|
|
|
|
|
.width = new_width,
|
|
|
|
|
.height = new_height,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
apply_resize(server->focused_toplevel, &new_geometry);
|
|
|
|
|
}
|
2026-01-02 22:53:34 +07:00
|
|
|
}
|
|
|
|
|
|
2026-05-14 18:16:40 +07:00
|
|
|
void process_cursor_motion(absn_server *server, uint32_t time)
|
2026-01-02 22:53:34 +07:00
|
|
|
{
|
2026-05-19 12:17:13 +07:00
|
|
|
double x, y;
|
2026-05-14 18:16:40 +07:00
|
|
|
struct wlr_surface *surface = NULL;
|
2026-05-19 12:17:13 +07:00
|
|
|
client_from_coords(server, server->cursor->x, server->cursor->y, &surface, NULL, NULL, &x, &y);
|
2026-05-14 18:16:40 +07:00
|
|
|
struct wlr_seat *seat = server->seat;
|
|
|
|
|
|
|
|
|
|
if (server->cursor_mode == CURSOR_MOVE) {
|
|
|
|
|
process_cursor_move(server);
|
|
|
|
|
return;
|
|
|
|
|
} else if (server->cursor_mode == CURSOR_RESIZE) {
|
|
|
|
|
process_cursor_resize(server);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (surface) {
|
2026-05-19 12:17:13 +07:00
|
|
|
wlr_seat_pointer_notify_enter(seat, surface, x, y);
|
|
|
|
|
wlr_seat_pointer_notify_motion(seat, time, x, y);
|
2026-05-14 18:16:40 +07:00
|
|
|
} else {
|
|
|
|
|
wlr_seat_pointer_clear_focus(seat);
|
|
|
|
|
}
|
2026-01-02 22:53:34 +07:00
|
|
|
}
|