feat(host): vendor PyroWave + minimal Granite subset as crates/pyrowave-sys

Phase 0 of design/pyrowave-codec-plan.md — the opt-in wired-LAN ultra-low-
latency codec. Vendored at upstream 509e4f88 (API 0.4.0, Granite 44362775,
volk + vulkan-headers pins in PUNKTFUNK-VENDOR.txt), pruned to the 6.6 MB
the standalone no-renderer build needs; scripts/vendor-pyrowave.sh
reproduces the tree (a pin bump is protocol-affecting, plan §4.2).

build.rs drives the wrapper CMakeLists (static archives incl. a static
C-API lib upstream only ships shared) + bindgen over pyrowave.h; Linux and
Windows only, empty stub elsewhere (Apple gets a native Metal port, §4.7).
Offline-safe by construction: no network, no system lib, vendored Vulkan
headers — same model as the opus dep (flatpak builder has no network).

Phase-0 validation on .21 (RTX 5070 Ti, driver 610.43.03):
- upstream pyrowave-c-test + interop test (incl. dmabuf/DRM-modifier
  Vulkan<->Vulkan) pass, from the pristine AND the pruned tree
- GPU kernel times at ~1.6 bpp noise: encode/decode 0.090/0.042 ms @800p,
  0.146/0.067 @1080p, 0.226/0.103 @1440p, 0.477/0.201 @4K — order of
  magnitude under NVENC's 1-2 ms retrieve, CBR lands within ~100 B of
  target
- cargo test -p pyrowave-sys green (static link + API-version pin check)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-15 00:35:10 +02:00
parent 1b73361372
commit 4c3b11445c
396 changed files with 140058 additions and 0 deletions
@@ -0,0 +1,14 @@
add_granite_internal_lib(granite-input input.hpp input.cpp)
target_include_directories(granite-input PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(granite-input PUBLIC granite-util granite-event granite-math)
if (${GRANITE_PLATFORM} MATCHES "SDL")
add_granite_internal_static_lib(granite-input-sdl input_sdl.cpp input_sdl.hpp)
target_link_libraries(granite-input-sdl PUBLIC granite-input)
if (GRANITE_SYSTEM_SDL)
find_package(SDL3 REQUIRED)
target_link_libraries(granite-input-sdl PUBLIC SDL3::SDL3-shared)
else()
target_link_libraries(granite-input-sdl PUBLIC SDL3-static)
endif()
endif()
@@ -0,0 +1,376 @@
/* Copyright (c) 2017-2026 Hans-Kristian Arntzen
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "input.hpp"
#include "event.hpp"
#include "muglm/muglm_impl.hpp"
#include "logging.hpp"
#include <algorithm>
#include <string.h>
using namespace Util;
namespace Granite
{
const char *joypad_key_to_tag(JoypadKey key)
{
#define D(k) case JoypadKey::k: return #k
switch (key)
{
D(Left);
D(Right);
D(Up);
D(Down);
D(LeftShoulder);
D(RightShoulder);
D(West);
D(East);
D(North);
D(South);
D(LeftThumb);
D(RightThumb);
D(Mode);
D(Start);
D(Select);
default:
return "Unknown";
}
#undef D
}
const char *joypad_axis_to_tag(JoypadAxis axis)
{
#define D(k) case JoypadAxis::k: return #k
switch (axis)
{
D(LeftX);
D(LeftY);
D(RightX);
D(RightY);
D(LeftTrigger);
D(RightTrigger);
default:
return "Unknown";
}
#undef D
}
void InputTracker::orientation_event(quat rot)
{
OrientationEvent event(rot);
if (handler)
handler->dispatch(event);
}
void InputTracker::on_touch_down(unsigned id, float x, float y)
{
if (touch.active_pointers >= TouchCount)
{
LOGE("Touch pointer overflow!\n");
return;
}
unsigned index = touch.active_pointers++;
auto &pointer = touch.pointers[index];
pointer.id = id;
pointer.start_x = x;
pointer.start_y = y;
pointer.last_x = x;
pointer.last_y = y;
pointer.x = x;
pointer.y = y;
TouchDownEvent event(index, id, x, y, touch.width, touch.height);
if (handler)
handler->dispatch(event);
}
void InputTracker::dispatch_touch_gesture()
{
TouchGestureEvent event(touch);
if (handler)
handler->dispatch(event);
}
void InputTracker::on_touch_move(unsigned id, float x, float y)
{
auto &pointers = touch.pointers;
auto itr = std::find_if(std::begin(pointers), std::begin(pointers) + touch.active_pointers, [id](const TouchState::Pointer &pointer) {
return pointer.id == id;
});
if (itr == std::end(pointers))
{
LOGE("Could not find pointer!\n");
return;
}
itr->x = x;
itr->y = y;
}
void InputTracker::on_touch_up(unsigned id, float x, float y)
{
auto &pointers = touch.pointers;
auto itr = std::find_if(std::begin(pointers), std::begin(pointers) + touch.active_pointers, [id](const TouchState::Pointer &pointer) {
return pointer.id == id;
});
if (itr == std::end(pointers))
{
LOGE("Could not find pointer!\n");
return;
}
auto index = itr - std::begin(pointers);
TouchUpEvent event(itr->id, x, y, itr->start_x, itr->start_y, touch.width, touch.height);
if (handler)
handler->dispatch(event);
memmove(&pointers[index], &pointers[index + 1], (TouchCount - (index + 1)) * sizeof(TouchState::Pointer));
touch.active_pointers--;
}
void InputTracker::joypad_key_state(unsigned index, JoypadKey key, JoypadKeyState state)
{
if (index >= Joypads)
return;
assert(active_joypads & (1u << index));
auto &joy = joypads[index];
unsigned key_index = Util::ecast(key);
unsigned key_mask = 1u << key_index;
if (state == JoypadKeyState::Pressed)
{
if ((joy.button_mask & key_mask) == 0)
{
JoypadButtonEvent event(index, key, state);
if (handler)
handler->dispatch(event);
}
joy.button_mask |= key_mask;
}
else if (state == JoypadKeyState::Released)
{
if ((joy.button_mask & key_mask) != 0)
{
JoypadButtonEvent event(index, key, state);
if (handler)
handler->dispatch(event);
}
joy.button_mask &= ~key_mask;
}
}
void JoypadState::snap_deadzone(float deadzone)
{
memcpy(snapped_axis, raw_axis, sizeof(raw_axis));
static const JoypadAxis fused_axes[2][2] = {
{ JoypadAxis::LeftX, JoypadAxis::LeftY },
{ JoypadAxis::RightX, JoypadAxis::RightY },
};
for (auto &fused : fused_axes)
{
if (std::abs(raw_axis[int(fused[0])]) < deadzone && std::abs(raw_axis[int(fused[1])]) < deadzone)
for (auto &axis : fused)
snapped_axis[int(axis)] = 0.0f;
}
}
void InputTracker::joyaxis_state(unsigned index, JoypadAxis axis, float value)
{
if (index >= Joypads)
return;
assert(active_joypads & (1u << index));
auto &joy = joypads[index];
unsigned axis_index = Util::ecast(axis);
auto &a = joy.raw_axis[axis_index];
if (a != value)
{
JoypadAxisEvent event(index, axis, value);
if (handler)
handler->dispatch(event);
}
a = value;
}
void InputTracker::key_event(Key key, KeyState state)
{
if (state == KeyState::Released)
key_state &= ~(1ull << ecast(key));
else if (state == KeyState::Pressed)
key_state |= 1ull << ecast(key);
KeyboardEvent event(key, state);
if (handler)
handler->dispatch(event);
}
void InputTracker::mouse_button_event(Granite::MouseButton button, bool pressed)
{
mouse_button_event(button, last_mouse_x, last_mouse_y, pressed);
}
void InputTracker::mouse_button_event(MouseButton button, double x, double y, bool pressed)
{
if (pressed)
mouse_button_state |= 1ull << ecast(button);
else
mouse_button_state &= ~(1ull << ecast(button));
if (mouse_active)
{
last_mouse_x = x;
last_mouse_y = y;
}
MouseButtonEvent event(button, x, y, pressed);
if (handler)
handler->dispatch(event);
}
void InputTracker::mouse_move_event_relative(double x, double y)
{
x *= mouse_speed_x;
y *= mouse_speed_y;
if (mouse_active)
{
last_mouse_x += x;
last_mouse_y += y;
last_mouse_x = clamp(last_mouse_x, mouse_relative_range_x,
mouse_relative_range_x + mouse_relative_range_width);
last_mouse_y = clamp(last_mouse_y, mouse_relative_range_y,
mouse_relative_range_y + mouse_relative_range_height);
MouseMoveEvent event(x, y, last_mouse_x, last_mouse_y, key_state, mouse_button_state);
if (handler)
handler->dispatch(event);
}
}
void InputTracker::mouse_move_event_absolute(double x, double y)
{
if (mouse_active)
{
double delta_x = x - last_mouse_x;
double delta_y = y - last_mouse_y;
last_mouse_x = x;
last_mouse_y = y;
MouseMoveEvent event(delta_x, delta_y, x, y, key_state, mouse_button_state);
if (handler)
handler->dispatch(event);
}
}
void InputTracker::mouse_move_event_absolute_normalized(double x, double y)
{
mouse_move_event_absolute(x * double(touch.width), y * double(touch.height));
}
void InputTracker::mouse_button_event_normalized(MouseButton button, double x, double y, bool pressed)
{
mouse_button_event(button, x * double(touch.width), y * double(touch.height), pressed);
}
void InputTracker::mouse_enter(double x, double y)
{
mouse_active = true;
last_mouse_x = x;
last_mouse_y = y;
}
void InputTracker::mouse_leave()
{
mouse_active = false;
}
void InputTracker::dispatch_current_state(double delta_time, InputTrackerHandler *override_handler)
{
if (!override_handler)
override_handler = handler;
if (override_handler)
{
for (auto &pad : joypads)
pad.snap_deadzone(axis_deadzone);
override_handler->dispatch(JoypadStateEvent{active_joypads, joypads, Joypads, delta_time});
override_handler->dispatch(InputStateEvent{last_mouse_x, last_mouse_y,
delta_time, key_state, mouse_button_state, mouse_active});
}
}
int InputTracker::find_vacant_joypad_index() const
{
for (int i = 0; i < Joypads; i++)
{
if ((active_joypads & (1 << i)) == 0)
return i;
}
return -1;
}
void InputTracker::enable_joypad(unsigned index, uint32_t vid, uint32_t pid)
{
if (index >= Joypads)
return;
if (active_joypads & (1u << index))
return;
active_joypads |= 1u << index;
joypads[index] = {};
joypads[index].vid = vid;
joypads[index].pid = pid;
JoypadConnectionEvent event(index, true, vid, pid);
if (handler)
handler->dispatch(event);
}
void InputTracker::disable_joypad(unsigned index, uint32_t vid, uint32_t pid)
{
if (index >= Joypads)
return;
if ((active_joypads & (1u << index)) == 0)
return;
active_joypads &= ~(1u << index);
joypads[index] = {};
JoypadConnectionEvent event(index, false, vid, pid);
if (handler)
handler->dispatch(event);
}
std::mutex &InputTracker::get_lock()
{
return dispatch_lock;
}
}
@@ -0,0 +1,719 @@
/* Copyright (c) 2017-2026 Hans-Kristian Arntzen
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include "enum_cast.hpp"
#include "event.hpp"
#include <stdint.h>
#include "math.hpp"
#include <limits.h>
#include <float.h>
#include <mutex>
namespace Granite
{
enum class JoypadKey
{
Left,
Right,
Up,
Down,
East,
South,
West,
North,
LeftShoulder,
RightShoulder,
LeftThumb,
RightThumb,
Start,
Select,
Mode,
Count,
Unknown
};
const char *joypad_key_to_tag(JoypadKey key);
enum class JoypadAxis
{
LeftX,
LeftY,
RightX,
RightY,
LeftTrigger,
RightTrigger,
Count,
Unknown
};
const char *joypad_axis_to_tag(JoypadAxis axis);
enum class JoypadKeyState
{
Pressed,
Released,
Count
};
enum class Key
{
Unknown,
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
Return,
LeftCtrl,
LeftAlt,
LeftShift,
Space,
Escape,
Left, Right, Up, Down,
_1, _2, _3, _4, _5, _6, _7, _8, _9, _0,
Count
};
enum class MouseButton
{
Left,
Middle,
Right,
Count
};
enum class KeyState
{
Pressed,
Released,
Repeat,
Count
};
static_assert(Util::ecast(Key::Count) <= 64, "Cannot have more than 64 keys for bit-packing.");
struct TouchState
{
enum { PointerCount = 16 };
struct Pointer
{
unsigned id;
float start_x;
float start_y;
float last_x;
float last_y;
float x;
float y;
};
Pointer pointers[PointerCount] = {};
unsigned active_pointers = 0;
unsigned width;
unsigned height;
};
struct JoypadState
{
bool is_button_pressed(JoypadKey key) const
{
return (button_mask & (1u << Util::ecast(key))) != 0;
}
float get_axis(JoypadAxis a) const
{
return snapped_axis[Util::ecast(a)];
}
void snap_deadzone(float deadzone);
float raw_axis[Util::ecast(JoypadAxis::Count)] = {};
float snapped_axis[Util::ecast(JoypadAxis::Count)] = {};
uint32_t button_mask = 0;
uint32_t vid = 0;
uint32_t pid = 0;
};
static_assert(Util::ecast(JoypadKey::Count) <= 32, "Cannot have more than 32 joypad buttons.");
class InputTrackerHandler;
class InputTracker
{
public:
void key_event(Key key, KeyState state);
void mouse_button_event(MouseButton button, double x, double y, bool pressed);
void mouse_button_event_normalized(MouseButton button, double x, double y, bool pressed);
void mouse_button_event(MouseButton button, bool pressed);
void mouse_move_event_absolute(double x, double y);
void mouse_move_event_absolute_normalized(double x, double y);
void mouse_move_event_relative(double x, double y);
void dispatch_current_state(double delta_time, InputTrackerHandler *override_handler = nullptr);
void orientation_event(quat rot);
void joypad_key_state(unsigned index, JoypadKey key, JoypadKeyState state);
void joyaxis_state(unsigned index, JoypadAxis axis, float value);
void on_touch_down(unsigned id, float x, float y);
void on_touch_move(unsigned id, float x, float y);
void on_touch_up(unsigned id, float x, float y);
void mouse_enter(double x, double y);
void mouse_leave();
bool key_pressed(Key key) const
{
return (key_state & (1ull << Util::ecast(key))) != 0;
}
bool joykey_pressed(unsigned index, JoypadKey key) const
{
if (index >= Joypads)
return false;
return (joypads[index].button_mask & (1u << Util::ecast(key))) != 0;
}
bool mouse_button_pressed(MouseButton button) const
{
return (mouse_button_state & (1ull << Util::ecast(button))) != 0;
}
void dispatch_touch_gesture();
void set_axis_deadzone(float deadzone)
{
axis_deadzone = deadzone;
}
void set_relative_mouse_rect(double x, double y, double width, double height)
{
mouse_relative_range_x = x;
mouse_relative_range_y = y;
mouse_relative_range_width = width;
mouse_relative_range_height = height;
}
void set_relative_mouse_speed(double speed_x, double speed_y)
{
mouse_speed_x = speed_x;
mouse_speed_y = speed_y;
}
void enable_joypad(unsigned index, uint32_t vid, uint32_t pid);
void disable_joypad(unsigned index, uint32_t vid, uint32_t pid);
int find_vacant_joypad_index() const;
void set_touch_resolution(unsigned width, unsigned height)
{
touch.width = width;
touch.height = height;
}
void set_input_handler(InputTrackerHandler *handler_)
{
handler = handler_;
}
// To support dispatching input manager (i.e. polling) state from async threads.
std::mutex &get_lock();
enum { TouchCount = 16 };
enum { Joypads = 8 };
private:
InputTrackerHandler *handler = nullptr;
std::mutex dispatch_lock;
uint64_t key_state = 0;
uint8_t mouse_button_state = 0;
bool mouse_active = false;
double last_mouse_x = 0.0;
double last_mouse_y = 0.0;
double mouse_relative_range_x = 0.0;
double mouse_relative_range_y = 0.0;
double mouse_relative_range_width = DBL_MAX;
double mouse_relative_range_height = DBL_MAX;
double mouse_speed_x = 1.0;
double mouse_speed_y = 1.0;
uint8_t active_joypads = 0;
JoypadState joypads[Joypads] = {};
TouchState touch;
float axis_deadzone = 0.3f;
};
class JoypadConnectionEvent : public Granite::Event
{
public:
GRANITE_EVENT_TYPE_DECL(JoypadConnectionEvent)
JoypadConnectionEvent(unsigned index_, bool connected_, uint32_t vid_, uint32_t pid_)
: index(index_), connected(connected_), vid(vid_), pid(pid_)
{
}
unsigned get_index() const
{
return index;
}
bool is_connected() const
{
return connected;
}
uint32_t get_vid() const
{
return vid;
}
uint32_t get_pid() const
{
return pid;
}
private:
unsigned index;
bool connected;
uint32_t vid, pid;
};
class TouchGestureEvent : public Granite::Event
{
public:
GRANITE_EVENT_TYPE_DECL(TouchGestureEvent)
explicit TouchGestureEvent(const TouchState &state_)
: state(state_)
{
}
const TouchState &get_state() const
{
return state;
}
private:
const TouchState &state;
};
class TouchDownEvent : public Granite::Event
{
public:
GRANITE_EVENT_TYPE_DECL(TouchDownEvent)
TouchDownEvent(unsigned index_, unsigned id_,
float x_, float y_,
unsigned screen_width_, unsigned screen_height_)
: index(index_), id(id_),
x(x_), y(y_),
width(screen_width_), height(screen_height_)
{
}
float get_x() const
{
return x;
}
float get_y() const
{
return y;
}
unsigned get_index() const
{
return index;
}
unsigned get_id() const
{
return id;
}
unsigned get_screen_width() const
{
return width;
}
unsigned get_screen_height() const
{
return height;
}
private:
unsigned index, id;
float x, y;
unsigned width, height;
};
class TouchUpEvent : public Granite::Event
{
public:
GRANITE_EVENT_TYPE_DECL(TouchUpEvent)
TouchUpEvent(unsigned id_, float x_, float y_,
float start_x_, float start_y_,
unsigned screen_width_, unsigned screen_height_)
: id(id_), x(x_), y(y_),
start_x(start_x_), start_y(start_y_),
width(screen_width_), height(screen_height_)
{
}
float get_x() const
{
return x;
}
float get_y() const
{
return y;
}
float get_start_x() const
{
return start_x;
}
float get_start_y() const
{
return start_y;
}
unsigned get_id() const
{
return id;
}
unsigned get_screen_width() const
{
return width;
}
unsigned get_screen_height() const
{
return height;
}
private:
unsigned id;
float x, y;
float start_x, start_y;
unsigned width, height;
};
class JoypadButtonEvent : public Granite::Event
{
public:
GRANITE_EVENT_TYPE_DECL(JoypadButtonEvent)
JoypadButtonEvent(unsigned index_, JoypadKey key_, JoypadKeyState state_)
: index(index_), key(key_), state(state_)
{
}
unsigned get_index() const
{
return index;
}
JoypadKey get_key() const
{
return key;
}
JoypadKeyState get_state() const
{
return state;
}
private:
unsigned index;
JoypadKey key;
JoypadKeyState state;
};
class JoypadAxisEvent : public Granite::Event
{
public:
GRANITE_EVENT_TYPE_DECL(JoypadAxisEvent)
JoypadAxisEvent(unsigned index_, JoypadAxis axis_, float value_)
: index(index_), axis(axis_), value(value_)
{
}
unsigned get_index() const
{
return index;
}
JoypadAxis get_axis() const
{
return axis;
}
float get_value() const
{
return value;
}
private:
unsigned index;
JoypadAxis axis;
float value;
};
class KeyboardEvent : public Granite::Event
{
public:
GRANITE_EVENT_TYPE_DECL(KeyboardEvent)
KeyboardEvent(Key key_, KeyState state_)
: key(key_), state(state_)
{
}
Key get_key() const
{
return key;
}
KeyState get_key_state() const
{
return state;
}
private:
Key key;
KeyState state;
};
class OrientationEvent : public Granite::Event
{
public:
GRANITE_EVENT_TYPE_DECL(OrientationEvent)
explicit OrientationEvent(const quat &rot_)
: rot(rot_)
{
}
const quat &get_rotation() const
{
return rot;
}
private:
quat rot;
};
class MouseButtonEvent : public Granite::Event
{
public:
GRANITE_EVENT_TYPE_DECL(MouseButtonEvent)
MouseButtonEvent(MouseButton button_, double abs_x_, double abs_y_, bool pressed_)
: button(button_), abs_x(abs_x_), abs_y(abs_y_), pressed(pressed_)
{
}
MouseButton get_button() const
{
return button;
}
double get_abs_x() const
{
return abs_x;
}
double get_abs_y() const
{
return abs_y;
}
bool get_pressed() const
{
return pressed;
}
private:
MouseButton button;
double abs_x;
double abs_y;
bool pressed;
};
class MouseMoveEvent : public Granite::Event
{
public:
GRANITE_EVENT_TYPE_DECL(MouseMoveEvent);
MouseMoveEvent(double delta_x_, double delta_y_, double abs_x_, double abs_y_,
uint64_t key_mask_, uint8_t btn_mask_)
: delta_x(delta_x_), delta_y(delta_y_),
abs_x(abs_x_), abs_y(abs_y_),
key_mask(key_mask_), btn_mask(btn_mask_)
{
}
bool get_mouse_button_pressed(MouseButton button) const
{
return (btn_mask & (1 << Util::ecast(button))) != 0;
}
bool get_key_pressed(Key key) const
{
return (key_mask & (1ull << Util::ecast(key))) != 0;
}
double get_delta_x() const
{
return delta_x;
}
double get_delta_y() const
{
return delta_y;
}
double get_abs_x() const
{
return abs_x;
}
double get_abs_y() const
{
return abs_y;
}
private:
double delta_x, delta_y, abs_x, abs_y;
uint64_t key_mask;
uint8_t btn_mask;
};
class JoypadStateEvent : public Granite::Event
{
public:
GRANITE_EVENT_TYPE_DECL(JoypadStateEvent)
JoypadStateEvent(uint8_t active_mask_, const JoypadState *states_,
unsigned count_, double delta_time_)
: states(states_), count(count_), delta_time(delta_time_), active_mask(active_mask_)
{
}
bool is_connected(unsigned index) const
{
if (index >= count)
return false;
return (active_mask & (1u << index)) != 0;
}
unsigned get_num_indices() const
{
return count;
}
const JoypadState &get_state(unsigned index) const
{
return states[index];
}
double get_delta_time() const
{
return delta_time;
}
private:
const JoypadState *states;
unsigned count;
double delta_time;
uint8_t active_mask;
};
class InputStateEvent : public Granite::Event
{
public:
GRANITE_EVENT_TYPE_DECL(InputStateEvent)
InputStateEvent(double abs_x_, double abs_y_,
double delta_time_, uint64_t key_mask_, uint8_t btn_mask_, bool mouse_active_)
: abs_x(abs_x_), abs_y(abs_y_),
delta_time(delta_time_), key_mask(key_mask_),
btn_mask(btn_mask_), mouse_active(mouse_active_)
{
}
double get_delta_time() const
{
return delta_time;
}
bool get_mouse_active() const
{
return mouse_active;
}
bool get_mouse_button_pressed(MouseButton button) const
{
return (btn_mask & (1 << Util::ecast(button))) != 0;
}
bool get_key_pressed(Key key) const
{
return (key_mask & (1ull << Util::ecast(key))) != 0;
}
double get_mouse_x() const
{
return abs_x;
}
double get_mouse_y() const
{
return abs_y;
}
private:
double abs_x, abs_y;
double delta_time;
uint64_t key_mask;
uint8_t btn_mask;
bool mouse_active;
};
class InputTrackerHandler
{
public:
virtual ~InputTrackerHandler() = default;
virtual void dispatch(const TouchDownEvent &e) = 0;
virtual void dispatch(const TouchUpEvent &e) = 0;
virtual void dispatch(const TouchGestureEvent &e) = 0;
virtual void dispatch(const JoypadButtonEvent &e) = 0;
virtual void dispatch(const JoypadAxisEvent &e) = 0;
virtual void dispatch(const KeyboardEvent &e) = 0;
virtual void dispatch(const OrientationEvent &e) = 0;
virtual void dispatch(const MouseButtonEvent &e) = 0;
virtual void dispatch(const MouseMoveEvent &e) = 0;
virtual void dispatch(const JoypadStateEvent &e) = 0;
virtual void dispatch(const InputStateEvent &e) = 0;
virtual void dispatch(const JoypadConnectionEvent &e) = 0;
};
}
@@ -0,0 +1,184 @@
/* Copyright (c) 2017-2026 Hans-Kristian Arntzen
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "input_sdl.hpp"
namespace Granite
{
bool InputTrackerSDL::init(InputTracker &tracker, const Dispatcher &dispatcher)
{
// Open existing gamepads.
int num_pads = 0;
SDL_JoystickID *gamepad_ids = SDL_GetGamepads(&num_pads);
for (int i = 0; i < num_pads; i++)
add_gamepad(gamepad_ids[i], tracker, dispatcher);
if (gamepad_ids)
SDL_free(gamepad_ids);
// Poll these separately, inline in poll_input().
SDL_SetGamepadEventsEnabled(false);
SDL_SetJoystickEventsEnabled(false);
SDL_SetEventEnabled(SDL_EVENT_GAMEPAD_ADDED, true);
SDL_SetEventEnabled(SDL_EVENT_GAMEPAD_REMOVED, true);
SDL_SetEventEnabled(SDL_EVENT_JOYSTICK_UPDATE_COMPLETE, false);
SDL_SetEventEnabled(SDL_EVENT_GAMEPAD_UPDATE_COMPLETE, false);
return true;
}
void InputTrackerSDL::update(InputTracker &tracker)
{
SDL_UpdateGamepads();
for (int i = 0; i < int(InputTracker::Joypads); i++)
{
auto *pad = pads[i];
if (!pad)
continue;
static const struct
{
JoypadKey gkey;
SDL_GamepadButton sdl;
} buttons[] = {
{ JoypadKey::Left, SDL_GAMEPAD_BUTTON_DPAD_LEFT },
{ JoypadKey::Right, SDL_GAMEPAD_BUTTON_DPAD_RIGHT },
{ JoypadKey::Up, SDL_GAMEPAD_BUTTON_DPAD_UP },
{ JoypadKey::Down, SDL_GAMEPAD_BUTTON_DPAD_DOWN },
{ JoypadKey::Start, SDL_GAMEPAD_BUTTON_START },
{ JoypadKey::Select, SDL_GAMEPAD_BUTTON_BACK },
{ JoypadKey::East, SDL_GAMEPAD_BUTTON_EAST },
{ JoypadKey::West, SDL_GAMEPAD_BUTTON_WEST },
{ JoypadKey::North, SDL_GAMEPAD_BUTTON_NORTH },
{ JoypadKey::South, SDL_GAMEPAD_BUTTON_SOUTH },
{ JoypadKey::LeftShoulder, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER },
{ JoypadKey::RightShoulder, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER },
{ JoypadKey::LeftThumb, SDL_GAMEPAD_BUTTON_LEFT_STICK },
{ JoypadKey::RightThumb, SDL_GAMEPAD_BUTTON_RIGHT_STICK },
{ JoypadKey::Mode, SDL_GAMEPAD_BUTTON_GUIDE },
};
for (auto &b : buttons)
{
tracker.joypad_key_state(i, b.gkey,
SDL_GetGamepadButton(pad, b.sdl) ?
JoypadKeyState::Pressed : JoypadKeyState::Released);
}
static const struct
{
JoypadAxis gaxis;
SDL_GamepadAxis sdl;
} axes[] = {
{ JoypadAxis::LeftX, SDL_GAMEPAD_AXIS_LEFTX },
{ JoypadAxis::LeftY, SDL_GAMEPAD_AXIS_LEFTY },
{ JoypadAxis::RightX, SDL_GAMEPAD_AXIS_RIGHTX },
{ JoypadAxis::RightY, SDL_GAMEPAD_AXIS_RIGHTY },
};
for (auto &a : axes)
{
float value = float(SDL_GetGamepadAxis(pad, a.sdl) - SDL_JOYSTICK_AXIS_MIN) /
float(SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN);
value = 2.0f * value - 1.0f;
tracker.joyaxis_state(i, a.gaxis, value);
}
tracker.joyaxis_state(i, JoypadAxis::LeftTrigger,
float(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER)) /
float(SDL_JOYSTICK_AXIS_MAX));
tracker.joyaxis_state(i, JoypadAxis::RightTrigger,
float(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER)) /
float(SDL_JOYSTICK_AXIS_MAX));
}
}
void InputTrackerSDL::close()
{
for (auto *pad : pads)
if (pad)
SDL_CloseGamepad(pad);
}
bool InputTrackerSDL::process_sdl_event(const SDL_Event &e, InputTracker &tracker,
const InputTrackerSDL::Dispatcher &dispatcher)
{
switch (e.type)
{
case SDL_EVENT_GAMEPAD_ADDED:
{
add_gamepad(e.gdevice.which, tracker, dispatcher);
return true;
}
case SDL_EVENT_GAMEPAD_REMOVED:
{
remove_gamepad(e.gdevice.which, tracker, dispatcher);
return true;
}
default:
break;
}
return false;
}
void InputTrackerSDL::add_gamepad(SDL_JoystickID id, InputTracker &tracker, const Dispatcher &dispatcher)
{
int player_index = SDL_GetJoystickPlayerIndexForID(id);
if (player_index >= 0 && player_index < int(InputTracker::Joypads) && !pads[player_index])
{
uint32_t vid = SDL_GetGamepadVendorForID(id);
uint32_t pid = SDL_GetGamepadProductForID(id);
const char *name = SDL_GetGamepadNameForID(id);
LOGI("Plugging in controller: \"%s\" (%u/%u).\n", name, vid, pid);
const char *mapping = SDL_GetGamepadMappingForID(id);
LOGI(" Using mapping: \"%s\"\n", mapping);
pads[player_index] = SDL_OpenGamepad(id);
ids[player_index] = id;
dispatcher([player_index, vid, pid, &tracker]() {
tracker.enable_joypad(player_index, vid, pid);
});
}
}
void InputTrackerSDL::remove_gamepad(SDL_JoystickID id, InputTracker &tracker, const Dispatcher &dispatcher)
{
for (int i = 0; i < int(InputTracker::Joypads); i++)
{
if (pads[i] && ids[i] == id)
{
uint32_t vid = SDL_GetGamepadVendor(pads[i]);
uint32_t pid = SDL_GetGamepadProduct(pads[i]);
SDL_CloseGamepad(pads[i]);
pads[i] = nullptr;
ids[i] = 0;
dispatcher([i, vid, pid, &tracker]() {
tracker.disable_joypad(i, vid, pid);
});
break;
}
}
}
}
@@ -0,0 +1,45 @@
/* Copyright (c) 2017-2026 Hans-Kristian Arntzen
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <SDL3/SDL.h>
#include "input.hpp"
#include <functional>
namespace Granite
{
class InputTrackerSDL
{
public:
using Dispatcher = std::function<void (std::function<void ()>)>;
bool init(InputTracker &tracker, const Dispatcher &dispatcher);
void close();
bool process_sdl_event(const SDL_Event &event, InputTracker &tracker, const Dispatcher &dispatcher);
void update(InputTracker &tracker);
private:
SDL_Gamepad *pads[InputTracker::Joypads] = {};
SDL_JoystickID ids[InputTracker::Joypads] = {};
void add_gamepad(SDL_JoystickID id, InputTracker &tracker, const Dispatcher &dispatcher);
void remove_gamepad(SDL_JoystickID id, InputTracker &tracker, const Dispatcher &dispatcher);
};
}