From 0f9e2d89589f05847fb7dc808da1594f0961b5b4 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Sun, 12 Jun 2022 22:34:23 +0200 Subject: Split peripheral-related logic into its own component This has several advantages: - `camera` no longer needs to define public functions for each peripheral type. - Peripheral-related is now no longer tighly coupled to human_player, so peripheral logic can be reused elsewhere e.g.: on menus. - Makes camera_update_touch consistent compared to equivalent functions, since now `pan` has now been moved to `camera` (as it should be). --- src/peripheral/CMakeLists.txt | 3 ++ src/peripheral/inc/peripheral.h | 64 +++++++++++++++++++++++++++++++++++++++++ src/peripheral/src/peripheral.c | 41 ++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 src/peripheral/CMakeLists.txt create mode 100644 src/peripheral/inc/peripheral.h create mode 100644 src/peripheral/src/peripheral.c (limited to 'src/peripheral') diff --git a/src/peripheral/CMakeLists.txt b/src/peripheral/CMakeLists.txt new file mode 100644 index 0000000..9666a5c --- /dev/null +++ b/src/peripheral/CMakeLists.txt @@ -0,0 +1,3 @@ +add_library(peripheral "src/peripheral.c") +target_include_directories(peripheral PUBLIC "inc") +target_link_libraries(peripheral PUBLIC pad mouse keyboard util) diff --git a/src/peripheral/inc/peripheral.h b/src/peripheral/inc/peripheral.h new file mode 100644 index 0000000..c405c3c --- /dev/null +++ b/src/peripheral/inc/peripheral.h @@ -0,0 +1,64 @@ +#ifndef PERIPHERAL_H +#define PERIPHERAL_H + +#include +#include +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +enum peripheral_type +{ + PERIPHERAL_TYPE_PAD, + PERIPHERAL_TYPE_TOUCH, + PERIPHERAL_TYPE_KEYBOARD_MOUSE +}; + +union peripheral +{ + struct peripheral_common + { + enum peripheral_type type; + } common; + + struct peripheral_pad + { + struct peripheral_common common; + struct pad pad; + } pad; + + struct peripheral_kbm + { + struct peripheral_common common; + struct mouse mouse; + struct keyboard keyboard; + bool long_press; + unsigned int lp_t; + } kbm; +}; + +struct peripheral_cfg +{ + enum peripheral_type type; + int padn; +}; + +UTIL_STATIC_ASSERT(!offsetof(struct peripheral_pad, common), + "unexpected offsetof for struct peripheral_pad"); +UTIL_STATIC_ASSERT(!offsetof(struct peripheral_kbm, common), + "unexpected offsetof for struct peripheral_kbm"); + +void peripheral_init(const struct peripheral_cfg *cfg, union peripheral *p); +void peripheral_update(union peripheral *p); + +#ifdef __cplusplus +} +#endif + +#endif /* PERIPHERAL_H */ diff --git a/src/peripheral/src/peripheral.c b/src/peripheral/src/peripheral.c new file mode 100644 index 0000000..a1f61be --- /dev/null +++ b/src/peripheral/src/peripheral.c @@ -0,0 +1,41 @@ +#include +#include +#include +#include + +void peripheral_update(union peripheral *const p) +{ + switch (p->common.type) + { + case PERIPHERAL_TYPE_TOUCH: + /* Fall through. */ + case PERIPHERAL_TYPE_KEYBOARD_MOUSE: + mouse_update(&p->kbm.mouse); + keyboard_update(&p->kbm.keyboard); + break; + + case PERIPHERAL_TYPE_PAD: + pad_update(&p->pad.pad); + break; + } +} + +void peripheral_init(const struct peripheral_cfg *const cfg, + union peripheral *const p) +{ + *p = (const union peripheral){0}; + + switch (p->common.type = cfg->type) + { + case PERIPHERAL_TYPE_TOUCH: + /* Fall through. */ + case PERIPHERAL_TYPE_KEYBOARD_MOUSE: + mouse_init(&p->kbm.mouse); + keyboard_init(&p->kbm.keyboard); + break; + + case PERIPHERAL_TYPE_PAD: + pad_init(cfg->padn, &p->pad.pad); + break; + } +} -- cgit v1.2.3