Skip to content

Commit

Permalink
sokol_app.h: Add proper relative pointer motion
Browse files Browse the repository at this point in the history
Replace the manually tracked relative pointer motion with the
relative-pointer protocol extension to track the appropriate events.

- Amend wayland-specific data to handle relative-pointer objects,
- bind to the relative-pointer manager registry interface,
- use this manager to create a relative-pointer object when enabling
  pointer capabilities,
- register the relative pointer listener,
- implement the appropriate callback, using the previously manually
  tracked relative pointer motions,
- amend cleanup routine with the new data, and
- pry the relative-pointer header include path.
  • Loading branch information
fleischie committed Dec 2, 2020
1 parent 2503d43 commit 66b1f13
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions sokol_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -1561,6 +1561,9 @@ inline int sapp_run(const sapp_desc& desc) { return sapp_run(&desc); }
#if defined(SOKOL_WAYLAND_XDG_SHELL_HEADER_PATH)
#include SOKOL_WAYLAND_XDG_SHELL_HEADER_PATH
#endif /* SOKOL_WAYLAND_XDG_SHELL_HEADER_PATH */
#if defined(SOKOL_WAYLAND_RELATIVE_POINTER_HEADER_PATH)
#include SOKOL_WAYLAND_RELATIVE_POINTER_HEADER_PATH
#endif /*SOKOL_WAYLAND_RELATIVE_POINTER_HEADER_PATH*/
#endif /* SOKOL_WAYLAND */
#include <GL/gl.h>
#include <dlfcn.h> /* dlopen, dlsym, dlclose */
Expand Down Expand Up @@ -1948,6 +1951,8 @@ typedef struct {
/* pointer/cursor related data */
struct wl_cursor *cursor;
struct wl_surface *cursor_surface;
struct zwp_relative_pointer_manager_v1 *relative_pointer_manager;
struct zwp_relative_pointer_v1 *relative_pointer;
uint32_t serial;

/* accumulated touch state */
Expand Down Expand Up @@ -10422,6 +10427,8 @@ _SOKOL_PRIVATE void _sapp_wl_cleanup(void) {
if (NULL != _sapp.wl.registry) wl_registry_destroy(_sapp.wl.registry);
if (NULL != _sapp.wl.event_queue) wl_event_queue_destroy(_sapp.wl.event_queue);
if (NULL != _sapp.wl.touch) wl_touch_destroy(_sapp.wl.touch);
if (NULL != _sapp.wl.relative_pointer) zwp_relative_pointer_v1_destroy(_sapp.wl.relative_pointer);
if (NULL != _sapp.wl.relative_pointer_manager) zwp_relative_pointer_manager_v1_destroy(_sapp.wl.relative_pointer_manager);
if (NULL != _sapp.wl.pointer) wl_pointer_destroy(_sapp.wl.pointer);
if (NULL != _sapp.wl.cursor_surface) wl_surface_destroy(_sapp.wl.cursor_surface);
if (NULL != _sapp.wl.keyboard) wl_keyboard_destroy(_sapp.wl.keyboard);
Expand Down Expand Up @@ -10814,14 +10821,8 @@ _SOKOL_PRIVATE void _sapp_wl_pointer_motion(void* data, struct wl_pointer* point

float new_x = (float) wl_fixed_to_double(surface_x) * _sapp.dpi_scale;
float new_y = (float) wl_fixed_to_double(surface_y) * _sapp.dpi_scale;
/* don't update dx/dy in the very first update */
if (_sapp.mouse.pos_valid) {
_sapp.mouse.dx = new_x - _sapp.mouse.x;
_sapp.mouse.dy = new_y - _sapp.mouse.y;
}
_sapp.mouse.x = new_x;
_sapp.mouse.y = new_y;
_sapp.mouse.pos_valid = true;
_sapp_wl_mouse_event(SAPP_EVENTTYPE_MOUSE_MOVE, SAPP_MOUSEBUTTON_INVALID, _sapp_wl_get_modifiers());
}

Expand All @@ -10837,6 +10838,27 @@ _SOKOL_PRIVATE const struct wl_pointer_listener _sapp_wl_pointer_listener = {
.motion = _sapp_wl_pointer_motion,
};

_SOKOL_PRIVATE void _sapp_wl_relative_pointer_motion(void* data, struct zwp_relative_pointer_v1* relative_pointer, uint32_t utime_hi, uint32_t utime_lo, wl_fixed_t dx, wl_fixed_t dy, wl_fixed_t dx_unaccel, wl_fixed_t dy_unaccel) {
_SOKOL_UNUSED(data);
_SOKOL_UNUSED(relative_pointer);
_SOKOL_UNUSED(utime_hi);
_SOKOL_UNUSED(utime_lo);
_SOKOL_UNUSED(dx_unaccel);
_SOKOL_UNUSED(dy_unaccel);

/* don't update dx/dy in the very first update */
if (_sapp.mouse.pos_valid) {
_sapp.mouse.dx = (float) wl_fixed_to_double(dx);
_sapp.mouse.dy = (float) wl_fixed_to_double(dy);
}
_sapp.mouse.pos_valid = true;
_sapp_wl_mouse_event(SAPP_EVENTTYPE_MOUSE_MOVE, SAPP_MOUSEBUTTON_INVALID, _sapp_wl_get_modifiers());
}

_SOKOL_PRIVATE const struct zwp_relative_pointer_v1_listener _sapp_wl_relative_pointer_listener = {
.relative_motion = _sapp_wl_relative_pointer_motion,
};

_SOKOL_PRIVATE void _sapp_wl_touch_cancel(void* data, struct wl_touch* touch) {
_SOKOL_UNUSED(data);
_SOKOL_UNUSED(touch);
Expand Down Expand Up @@ -10935,6 +10957,11 @@ _SOKOL_PRIVATE void _sapp_wl_seat_handle_capabilities(void* data, struct wl_seat
_sapp.wl.pointer = wl_seat_get_pointer(seat);
wl_pointer_add_listener(_sapp.wl.pointer, &_sapp_wl_pointer_listener, NULL);

if (NULL != _sapp.wl.relative_pointer_manager) {
_sapp.wl.relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(_sapp.wl.relative_pointer_manager, _sapp.wl.pointer);
zwp_relative_pointer_v1_add_listener(_sapp.wl.relative_pointer, &_sapp_wl_relative_pointer_listener, NULL);
}

if (NULL == _sapp.wl.cursor_surface) {
struct wl_cursor_theme *cursor_theme = wl_cursor_theme_load(NULL, 24, _sapp.wl.shm);
_sapp.wl.cursor = wl_cursor_theme_get_cursor(cursor_theme, "left_ptr");
Expand Down Expand Up @@ -11047,6 +11074,8 @@ _SOKOL_PRIVATE void _sapp_wl_registry_handle_global(void* data, struct wl_regist
}
} else if (0 == strcmp(interface, wl_shm_interface.name)) {
_sapp.wl.shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
} else if (0 == strcmp(interface, zwp_relative_pointer_manager_v1_interface.name)) {
_sapp.wl.relative_pointer_manager = wl_registry_bind(registry, name, &zwp_relative_pointer_manager_v1_interface, 1);
}
}

Expand Down

0 comments on commit 66b1f13

Please sign in to comment.