diff --git a/src/display-wayland.cc b/src/display-wayland.cc index 56b57ed1..49dbb77f 100644 --- a/src/display-wayland.cc +++ b/src/display-wayland.cc @@ -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(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(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; diff --git a/src/display-x11.cc b/src/display-x11.cc index 61d47200..6a6d37a2 100644 --- a/src/display-x11.cc +++ b/src/display-x11.cc @@ -44,7 +44,7 @@ #endif /* BUILD_IMLIB2 */ #ifdef BUILD_MOUSE_EVENTS #include "mouse-events.h" -#endif +#endif /* BUILD_MOUSE_EVENTS */ #endif /* BUILD_X11 */ #include diff --git a/src/llua.cc b/src/llua.cc index cbbf2a29..1f2fb13d 100644 --- a/src/llua.cc +++ b/src/llua.cc @@ -54,6 +54,9 @@ void llua_rm_notifies(void); static int llua_block_notify = 0; #endif /* HAVE_SYS_INOTIFY_H */ +// POSIX compliant +#include + 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 { diff --git a/src/mouse-events.cc b/src/mouse-events.cc index 7a647787..c9663b29 100644 --- a/src/mouse-events.cc +++ b/src/mouse-events.cc @@ -22,7 +22,10 @@ #include #include + +#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 */ diff --git a/src/mouse-events.h b/src/mouse-events.h index 76dc35bd..97172132 100644 --- a/src/mouse-events.h +++ b/src/mouse-events.h @@ -25,10 +25,12 @@ #include extern "C" { +#ifdef BUILD_X11 #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wvariadic-macros" #include #pragma GCC diagnostic pop +#endif /* BUILD_X11 */ #include } @@ -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;