Skip to content

Commit

Permalink
Add support for all previously supported events
Browse files Browse the repository at this point in the history
Signed-off-by: Tin <[email protected]>
  • Loading branch information
Caellian committed Nov 6, 2023
1 parent 9799417 commit 51de904
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 38 deletions.
130 changes: 116 additions & 14 deletions src/display-wayland.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*/

#include <config.h>
#include <wayland-util.h>
#include <utility>
#include "logging.h"

#ifdef BUILD_WAYLAND
Expand Down Expand Up @@ -63,6 +65,7 @@
#endif
#ifdef BUILD_MOUSE_EVENTS
#include "mouse-events.h"
#include <map>
#endif

#pragma GCC diagnostic ignored "-Wunused-parameter"
Expand Down Expand Up @@ -427,32 +430,66 @@ void window_layer_surface_set_size(struct window *window) {
}

#ifdef BUILD_MOUSE_EVENTS
static std::map<wl_pointer*, std::array<size_t, 2>> last_known_positions{};

static void on_pointer_enter(void *data, wl_pointer *pointer, uint32_t serial,
wl_surface *surface, wl_fixed_t sx,
wl_fixed_t sy) {
DBGP("POINTER ENTERED! WOAH!");
wl_surface *surface, wl_fixed_t surface_x,
wl_fixed_t surface_y) {
auto w = reinterpret_cast<struct window *>(data);

size_t x = static_cast<size_t>(wl_fixed_to_double(surface_x));
size_t y = static_cast<size_t>(wl_fixed_to_double(surface_y));
last_known_positions[pointer] = {x, y};

size_t abs_x = w->rectangle.x + x;
size_t abs_y = w->rectangle.y + y;

mouse_crossing_event event {
mouse_event_t::AREA_ENTER,
x,
y,
abs_x,
abs_y
};
}

static void on_pointer_leave(void *data,
struct wl_pointer *wl_pointer,
struct wl_pointer *pointer,
uint32_t serial,
struct wl_surface *surface) {
DBGP("POINTER LEFT! WOAH!");
auto w = reinterpret_cast<struct window *>(data);

std::array<size_t, 2> last = last_known_positions[pointer];
size_t x = last[0];
size_t y = last[1];
size_t abs_x = w->rectangle.x + x;
size_t abs_y = w->rectangle.y + y;

mouse_crossing_event event {
mouse_event_t::AREA_LEAVE,
x,
y,
abs_x,
abs_y
};
}

static void on_pointer_motion(void *data,
struct wl_pointer *wl_pointer,
uint32_t time,
struct wl_pointer *pointer,
uint32_t _time,
wl_fixed_t surface_x,
wl_fixed_t surface_y) {
window *w = reinterpret_cast<struct window *>(data);
size_t x = surface_x;
size_t y = surface_y;
auto w = reinterpret_cast<struct window *>(data);

size_t x = static_cast<size_t>(wl_fixed_to_double(surface_x));
size_t y = static_cast<size_t>(wl_fixed_to_double(surface_y));
last_known_positions[pointer] = {x, 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,
mouse_event_t::MOUSE_MOVE,
x,
y,
abs_x,
Expand All @@ -462,12 +499,76 @@ static void on_pointer_motion(void *data,
}

static void on_pointer_button(void *data,
struct wl_pointer *wl_pointer,
struct wl_pointer *pointer,
uint32_t serial,
uint32_t time,
uint32_t button,
uint32_t state) {

auto w = reinterpret_cast<struct window *>(data);

std::array<size_t, 2> last = last_known_positions[pointer];
size_t x = last[0];
size_t y = last[1];
size_t abs_x = w->rectangle.x + x;
size_t abs_y = w->rectangle.y + y;

mouse_button_event event {
mouse_event_t::MOUSE_RELEASE,
x,
y,
abs_x,
abs_y,
static_cast<mouse_button_t>(button),
};

switch (static_cast<wl_pointer_button_state>(state)) {
case WL_POINTER_BUTTON_STATE_RELEASED:
// pass; default is MOUSE_RELEASE
break;
case WL_POINTER_BUTTON_STATE_PRESSED:
event.type = mouse_event_t::MOUSE_PRESS;
break;
default:
return;
}
llua_mouse_hook(event);
}

void on_pointer_axis(void *data,
struct wl_pointer *pointer,
uint32_t time,
uint32_t axis,
wl_fixed_t value) {
if (value == 0) return;

auto w = reinterpret_cast<struct window *>(data);

std::array<size_t, 2> last = last_known_positions[pointer];
size_t x = last[0];
size_t y = last[1];
size_t abs_x = w->rectangle.x + x;
size_t abs_y = w->rectangle.y + y;

mouse_scroll_event event {
mouse_event_t::MOUSE_SCROLL,
x,
y,
abs_x,
abs_y,
scroll_direction_t::SCROLL_UP,
};

switch (static_cast<wl_pointer_axis>(axis)) {
case WL_POINTER_AXIS_VERTICAL_SCROLL:
event.direction = value > 0 ? scroll_direction_t::SCROLL_DOWN : scroll_direction_t::SCROLL_UP;
break;
case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
event.direction = value > 0 ? scroll_direction_t::SCROLL_RIGHT : scroll_direction_t::SCROLL_LEFT;
break;
default:
return;
}
llua_mouse_hook(event);
}

static void seat_capability_listener (void *data, wl_seat *seat, uint32_t capability_int) {
Expand All @@ -481,6 +582,7 @@ static void seat_capability_listener (void *data, wl_seat *seat, uint32_t capabi
.leave = on_pointer_leave,
.motion = on_pointer_motion,
.button = on_pointer_button,
.axis = on_pointer_axis,
};
wl_pointer_add_listener(wl_globals.pointer, &listener, data);
}
Expand Down
74 changes: 62 additions & 12 deletions src/mouse-events.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@

#include <array>
#include <string>
#include <time.h>

#ifdef BUILD_X11
#include "X11/Xlib.h"
#endif /* BUILD_X11 */

std::string event_type_to_str(int type) {
switch (type) {
case MOUSE_DOWN:
case MOUSE_PRESS:
return "button_down";
case MOUSE_UP:
case MOUSE_RELEASE:
return "button_up";
case MOUSE_SCROLL:
return "mouse_scroll";
Expand Down Expand Up @@ -109,11 +110,20 @@ void push_mods(lua_State *L, std::bitset<13> mods) {
lua_settable(L, -3);
}

// Returns ms since Epoch.
inline size_t current_time_ms() {
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
return static_cast<size_t>(static_cast<uint64_t>(spec.tv_sec)*1'000 + spec.tv_nsec/1'000'000);
}

/* Class methods */
mouse_event::mouse_event(mouse_event_t type): type(type), time(current_time_ms()) {};

void mouse_event::push_lua_table(lua_State *L) const {
lua_newtable(L);
push_table_value(L, "type", event_type_to_str(this->type));
push_table_value(L, "time", this->time);
push_lua_data(L);
}

Expand All @@ -122,7 +132,6 @@ void mouse_positioned_event::push_lua_data(lua_State *L) const {
push_table_value(L, "y", this->y);
push_table_value(L, "x_abs", this->x_abs);
push_table_value(L, "y_abs", this->y_abs);
push_table_value(L, "time", this->time);
}

#ifdef BUILD_X11
Expand All @@ -132,7 +141,6 @@ mouse_move_event::mouse_move_event(XMotionEvent *ev) {
this->y = ev->y;
this->x_abs = ev->x_root;
this->y_abs = ev->y_root;
this->time = ev->time;
}
#endif /* BUILD_X11 */

Expand All @@ -148,34 +156,77 @@ mouse_scroll_event::mouse_scroll_event(XButtonEvent *ev) {
this->y = ev->y;
this->x_abs = ev->x_root;
this->y_abs = ev->y_root;
this->time = ev->time;
this->mods = ev->state;
this->up = ev->button == 4;
if (ev->button == 4) {
this->direction = scroll_direction_t::UP;
} else if (ev->button == 5) {
this->direction = scroll_direction_t::DOWN;
}
}
#endif /* BUILD_X11 */

void mouse_scroll_event::push_lua_data(lua_State *L) const {
mouse_positioned_event::push_lua_data(L);
push_table_value(L, "direction", std::string(this->up ? "up" : "down"));
std::string direction = "up";
switch (this->direction) {
case SCROLL_DOWN:
direction = "down";
break;
case SCROLL_UP:
break;
case SCROLL_LEFT:
direction = "left";
break;
case SCROLL_RIGHT:
direction = "right";
break;
default:
direction = "err";
break;
}
push_table_value(L, "direction", direction);
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->type = ev->type == ButtonPress ? MOUSE_PRESS : MOUSE_RELEASE;
this->x = ev->x;
this->y = ev->y;
this->x_abs = ev->x_root;
this->y_abs = ev->y_root;
this->time = ev->time;
this->mods = ev->state;
this->button = ev->button;
switch (ev->button) {
case Button1:
this->button = BUTTON_LEFT;
break;
case Button2:
this->button = BUTTON_RIGHT;
break;
case Button3:
this->button = BUTTON_MIDDLE;
break;
}
}
#endif /* BUILD_X11 */

std::string button_name(mouse_button_t button) {
switch (button) {
case BUTTON_LEFT:
return "left";
case BUTTON_RIGHT:
return "right";
case BUTTON_MIDDLE:
return "middle";
default:
return std::to_string(button);
}
}

void mouse_button_event::push_lua_data(lua_State *L) const {
mouse_positioned_event::push_lua_data(L);
push_table_value(L, "button", this->button);
push_table_value(L, "button_code", static_cast<uint32_t>(this->button));
push_table_value(L, "button", button_name(this->button));
push_mods(L, this->mods);
}

Expand All @@ -186,6 +237,5 @@ mouse_crossing_event::mouse_crossing_event(XCrossingEvent *ev) {
this->y = ev->y;
this->x_abs = ev->x_root;
this->y_abs = ev->y_root;
this->time = ev->time;
}
#endif /* BUILD_X11 */
Loading

0 comments on commit 51de904

Please sign in to comment.