diff --git a/lib/include/chiaki/controller.h b/lib/include/chiaki/controller.h index 2a870d0..258cd89 100644 --- a/lib/include/chiaki/controller.h +++ b/lib/include/chiaki/controller.h @@ -85,6 +85,15 @@ typedef struct chiaki_controller_state_t CHIAKI_EXPORT void chiaki_controller_state_set_idle(ChiakiControllerState *state); +/** + * @return A non-negative newly allocated touch id allocated or -1 if there are no slots left + */ +CHIAKI_EXPORT int8_t chiaki_controller_state_start_touch(ChiakiControllerState *state, uint16_t x, uint16_t y); + +CHIAKI_EXPORT void chiaki_controller_state_stop_touch(ChiakiControllerState *state, uint8_t id); + +CHIAKI_EXPORT void chiaki_controller_state_set_touch_pos(ChiakiControllerState *state, uint8_t id, uint16_t x, uint16_t y); + static inline bool chiaki_controller_state_equals(ChiakiControllerState *a, ChiakiControllerState *b) { if(!(a->buttons == b->buttons diff --git a/lib/src/controller.c b/lib/src/controller.c index a3853bd..a50cac4 100644 --- a/lib/src/controller.c +++ b/lib/src/controller.c @@ -17,6 +17,8 @@ #include +#define TOUCH_ID_MASK 0x7f + CHIAKI_EXPORT void chiaki_controller_state_set_idle(ChiakiControllerState *state) { state->buttons = 0; @@ -26,6 +28,48 @@ CHIAKI_EXPORT void chiaki_controller_state_set_idle(ChiakiControllerState *state state->right_y = 0; } +CHIAKI_EXPORT int8_t chiaki_controller_state_start_touch(ChiakiControllerState *state, uint16_t x, uint16_t y) +{ + for(size_t i=0; itouches[i].id < 0) + { + state->touches[i].id = state->touch_id_next; + state->touch_id_next = (state->touch_id_next + 1) & TOUCH_ID_MASK; + state->touches[i].x = x; + state->touches[i].y = y; + break; + } + } + return -1; +} + +CHIAKI_EXPORT void chiaki_controller_state_stop_touch(ChiakiControllerState *state, uint8_t id) +{ + for(size_t i=0; itouches[i].id == id) + { + state->touches[i].id = -1; + break; + } + } +} + +CHIAKI_EXPORT void chiaki_controller_state_set_touch_pos(ChiakiControllerState *state, uint8_t id, uint16_t x, uint16_t y) +{ + id &= TOUCH_ID_MASK; + for(size_t i=0; itouches[i].id == id) + { + state->touches[i].x = x; + state->touches[i].y = y; + break; + } + } +} + #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define ABS(a) ((a) > 0 ? (a) : -(a)) #define MAX_ABS(a, b) (ABS(a) > ABS(b) ? (a) : (b))