Pass cursor move event to the script

Set initial window scale to 0 because wayland freezes the process when it's not >0.
Provide more information from `llua_mouse_hook` about why the callback is failing.
Prevent call to `llua_init` when provided lua_load script doesn't exist.
- This caused me a headache and now lua_L won't be initialized if the
  script doesn't exist. This prevents a missing file from causing later
  issues which might be harder to catch.
Feature gated X11 in mouse-events.h/.cc files.

Signed-off-by: Tin <tin.svagelj@live.com>
This commit is contained in:
Tin 2023-11-06 00:07:57 +01:00 committed by Brenden Matthews
parent 7afeaee755
commit 958bd1c525
No known key found for this signature in database
GPG Key ID: 137B7AC2BDFD8DF0
5 changed files with 95 additions and 10 deletions

View File

@ -61,6 +61,9 @@
#ifdef BUILD_WAYLAND
#include "fonts.h"
#endif
#ifdef BUILD_MOUSE_EVENTS
#include "mouse-events.h"
#endif
#pragma GCC diagnostic ignored "-Wunused-parameter"
@ -423,6 +426,7 @@ void window_layer_surface_set_size(struct window *window) {
global_window->rectangle.height);
}
#ifdef BUILD_MOUSE_EVENTS
static void on_pointer_enter(void *data, wl_pointer *pointer, uint32_t serial,
wl_surface *surface, wl_fixed_t sx,
wl_fixed_t sy) {
@ -441,10 +445,32 @@ static void on_pointer_motion(void *data,
uint32_t time,
wl_fixed_t surface_x,
wl_fixed_t surface_y) {
DBGP("POINTER MOTIONED! WOAH!");
window *w = reinterpret_cast<struct window *>(data);
size_t x = surface_x;
size_t y = surface_y;
size_t abs_x = w->rectangle.x + x;
size_t abs_y = w->rectangle.y + y;
mouse_move_event event {
mouse_event_type::MOUSE_MOVE,
time,
x,
y,
abs_x,
abs_y
};
llua_mouse_hook(event);
}
static void seat_capability_listener (void *data, wl_seat *seat, std::uint32_t capability_int) {
static void on_pointer_button(void *data,
struct wl_pointer *wl_pointer,
uint32_t serial,
uint32_t time,
uint32_t button,
uint32_t state) {
}
static void seat_capability_listener (void *data, wl_seat *seat, uint32_t capability_int) {
wl_seat_capability capabilities = static_cast<wl_seat_capability>(capability_int);
if (wl_globals.seat == seat) {
if ((capabilities & WL_SEAT_CAPABILITY_POINTER) > 0) {
@ -454,12 +480,14 @@ static void seat_capability_listener (void *data, wl_seat *seat, std::uint32_t c
.enter = on_pointer_enter,
.leave = on_pointer_leave,
.motion = on_pointer_motion,
.button = on_pointer_button,
};
DBGP("ADDED A LISTENER");
wl_pointer_add_listener(wl_globals.pointer, &listener, NULL);
wl_pointer_add_listener(wl_globals.pointer, &listener, data);
}
}
}
static void seat_name_listener(void *data, struct wl_seat *wl_seat, const char *name) {}
#endif /* BUILD_MOUSE_EVENTS */
bool display_output_wayland::initialize() {
epoll_fd = epoll_create1(0);
@ -490,6 +518,15 @@ bool display_output_wayland::initialize() {
zwlr_layer_surface_v1_add_listener(global_window->layer_surface,
&layer_surface_listener, nullptr);
#ifdef BUILD_MOUSE_EVENTS
wl_seat_listener listener {
.capabilities = seat_capability_listener,
.name = seat_name_listener,
};
wl_seat_add_listener(wl_globals.seat, &listener, global_window);
#endif /* BUILD_MOUSE_EVENTS */
wl_surface_set_buffer_scale(global_window->surface, global_window->scale);
wl_surface_commit(global_window->surface);
wl_display_roundtrip(global_display);
wayland_create_window();
@ -1123,7 +1160,7 @@ struct window *window_create(struct wl_surface *surface, struct wl_shm *shm,
window->rectangle.y = 0;
window->rectangle.width = width;
window->rectangle.height = height;
window->scale = 0;
window->scale = 1;
window->pending_scale = 1;
window->surface = surface;

View File

@ -44,7 +44,7 @@
#endif /* BUILD_IMLIB2 */
#ifdef BUILD_MOUSE_EVENTS
#include "mouse-events.h"
#endif
#endif /* BUILD_MOUSE_EVENTS */
#endif /* BUILD_X11 */
#include <iostream>

View File

@ -54,6 +54,9 @@ void llua_rm_notifies(void);
static int llua_block_notify = 0;
#endif /* HAVE_SYS_INOTIFY_H */
// POSIX compliant
#include <sys/stat.h>
static void llua_load(const char *script);
lua_State *lua_L = nullptr;
@ -207,9 +210,21 @@ void llua_init() {
#endif /* BUILD_X11 */
}
inline bool file_exists (const char *path) {
struct stat buffer;
return (stat (path, &buffer) == 0);
}
void llua_load(const char *script) {
int error;
if (!file_exists(script)) {
NORM_ERR("llua_load: specified script file '%s' doesn't exist", script);
// return without initializing lua_L because other parts of the code rely
// on it being null if the script is not loaded
return;
}
llua_init();
std::string path = to_real_path(script);
@ -501,13 +516,20 @@ bool llua_mouse_hook(const EventT &ev) {
return false;
}
const std::string func = lua_mouse_hook.get(*state);
lua_getglobal(lua_L, lua_mouse_hook.get(*state).c_str());
int ty = lua_getglobal(lua_L, lua_mouse_hook.get(*state).c_str());
if (ty == LUA_TNIL) {
NORM_ERR("llua_mouse_hook: hook %s is not defined", func.c_str());
return false;
} else if (ty != LUA_TFUNCTION) {
NORM_ERR("llua_mouse_hook: hook %s is not a function", func.c_str());
return false;
}
ev.push_lua_table(lua_L);
bool result = false;
if (lua_pcall(lua_L, 1, 1, 0) != 0) {
NORM_ERR("llua_mouse_hook: function %s execution failed: %s", func.c_str(),
NORM_ERR("llua_mouse_hook: hook %s execution failed: %s", func.c_str(),
lua_tostring(lua_L, -1));
lua_pop(lua_L, 1);
} else {

View File

@ -22,7 +22,10 @@
#include <array>
#include <string>
#ifdef BUILD_X11
#include "X11/Xlib.h"
#endif /* BUILD_X11 */
std::string event_type_to_str(int type) {
switch (type) {
@ -122,6 +125,7 @@ void mouse_positioned_event::push_lua_data(lua_State *L) const {
push_table_value(L, "time", this->time);
}
#ifdef BUILD_X11
mouse_move_event::mouse_move_event(XMotionEvent *ev) {
this->type = MOUSE_MOVE;
this->x = ev->x;
@ -130,12 +134,14 @@ mouse_move_event::mouse_move_event(XMotionEvent *ev) {
this->y_abs = ev->y_root;
this->time = ev->time;
}
#endif /* BUILD_X11 */
void mouse_move_event::push_lua_data(lua_State *L) const {
mouse_positioned_event::push_lua_data(L);
push_mods(L, this->mods);
}
#ifdef BUILD_X11
mouse_scroll_event::mouse_scroll_event(XButtonEvent *ev) {
this->type = MOUSE_SCROLL;
this->x = ev->x;
@ -146,6 +152,7 @@ mouse_scroll_event::mouse_scroll_event(XButtonEvent *ev) {
this->mods = ev->state;
this->up = ev->button == 4;
}
#endif /* BUILD_X11 */
void mouse_scroll_event::push_lua_data(lua_State *L) const {
mouse_positioned_event::push_lua_data(L);
@ -153,6 +160,7 @@ void mouse_scroll_event::push_lua_data(lua_State *L) const {
push_mods(L, this->mods);
}
#ifdef BUILD_X11
mouse_button_event::mouse_button_event(XButtonEvent *ev) {
this->type = ev->type == ButtonPress ? MOUSE_DOWN : MOUSE_UP;
this->x = ev->x;
@ -163,6 +171,7 @@ mouse_button_event::mouse_button_event(XButtonEvent *ev) {
this->mods = ev->state;
this->button = ev->button;
}
#endif /* BUILD_X11 */
void mouse_button_event::push_lua_data(lua_State *L) const {
mouse_positioned_event::push_lua_data(L);
@ -170,6 +179,7 @@ void mouse_button_event::push_lua_data(lua_State *L) const {
push_mods(L, this->mods);
}
#ifdef BUILD_X11
mouse_crossing_event::mouse_crossing_event(XCrossingEvent *ev) {
this->type = ev->type == EnterNotify ? AREA_ENTER : AREA_LEAVE;
this->x = ev->x;
@ -178,3 +188,4 @@ mouse_crossing_event::mouse_crossing_event(XCrossingEvent *ev) {
this->y_abs = ev->y_root;
this->time = ev->time;
}
#endif /* BUILD_X11 */

View File

@ -25,10 +25,12 @@
#include <cstdint>
extern "C" {
#ifdef BUILD_X11
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wvariadic-macros"
#include <X11/Xlib.h>
#pragma GCC diagnostic pop
#endif /* BUILD_X11 */
#include <lua.h>
}
@ -46,22 +48,29 @@ struct mouse_event {
mouse_event_type type;
uint64_t time = 0L; // event time
mouse_event(mouse_event_type type, uint64_t time): type(type), time(time) {};
void push_lua_table(lua_State *L) const;
virtual void push_lua_data(lua_State *L) const = 0;
};
struct mouse_positioned_event : public mouse_event {
int x = 0, y = 0; // positions relative to window
int x_abs = 0, y_abs = 0; // positions relative to root
size_t x = 0, y = 0; // positions relative to window
size_t x_abs = 0, y_abs = 0; // positions relative to root
mouse_positioned_event(mouse_event_type type, uint64_t time, size_t x, size_t y, size_t x_abs, size_t y_abs): mouse_event{type, time}, x(x), y(y), x_abs(x_abs), y_abs() {};
void push_lua_data(lua_State *L) const;
};
struct mouse_move_event : public mouse_positioned_event {
std::bitset<13> mods; // held buttons and modifiers (ctrl, shift, ...)
explicit mouse_move_event(mouse_event_type type, uint64_t time, size_t x, size_t y, size_t x_abs, size_t y_abs, std::bitset<13> mods = 0): mouse_positioned_event{type, time, x, y, x_abs, y_abs}, mods(mods) {};
#ifdef BUILD_X11
explicit mouse_move_event(XMotionEvent *ev);
#endif /* BUILD_X11 */
void push_lua_data(lua_State *L) const;
};
@ -70,7 +79,9 @@ struct mouse_scroll_event : public mouse_positioned_event {
std::bitset<13> mods; // held buttons and modifiers (ctrl, shift, ...)
bool up = false;
#ifdef BUILD_X11
explicit mouse_scroll_event(XButtonEvent *ev);
#endif /* BUILD_X11 */
void push_lua_data(lua_State *L) const;
};
@ -79,7 +90,9 @@ struct mouse_button_event : public mouse_positioned_event {
std::bitset<13> mods; // held buttons and modifiers (ctrl, shift, ...)
uint button = 0;
#ifdef BUILD_X11
explicit mouse_button_event(XButtonEvent *ev);
#endif /* BUILD_X11 */
void push_lua_data(lua_State *L) const;
};
@ -88,7 +101,9 @@ typedef struct mouse_button_event mouse_press_event;
typedef struct mouse_button_event mouse_release_event;
struct mouse_crossing_event : public mouse_positioned_event {
#ifdef BUILD_X11
explicit mouse_crossing_event(XCrossingEvent *ev);
#endif /* BUILD_X11 */
};
typedef struct mouse_crossing_event mouse_enter_event;