|
| 1 | +// license:GPLv3+ |
| 2 | +// Visual Pinball Engine - Native Input Polling |
| 3 | +// Provides OS-level input polling for sub-millisecond latency |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include <stdint.h> |
| 8 | + |
| 9 | +#ifdef __cplusplus |
| 10 | +extern "C" { |
| 11 | +#endif |
| 12 | + |
| 13 | +// Platform detection |
| 14 | +#if defined(_WIN32) || defined(_WIN64) |
| 15 | +#define VPE_PLATFORM_WINDOWS 1 |
| 16 | +#elif defined(__linux__) |
| 17 | +#define VPE_PLATFORM_LINUX 1 |
| 18 | +#elif defined(__APPLE__) |
| 19 | +#define VPE_PLATFORM_MACOS 1 |
| 20 | +#endif |
| 21 | + |
| 22 | +// Export macros |
| 23 | +#if defined(VPE_PLATFORM_WINDOWS) |
| 24 | +#define VPE_API __declspec(dllexport) |
| 25 | +#elif defined(__GNUC__) |
| 26 | +#define VPE_API __attribute__((visibility("default"))) |
| 27 | +#else |
| 28 | +#define VPE_API |
| 29 | +#endif |
| 30 | + |
| 31 | +// Input action enum (must match C# enum) |
| 32 | +typedef enum { |
| 33 | + VPE_INPUT_LEFT_FLIPPER = 0, |
| 34 | + VPE_INPUT_RIGHT_FLIPPER = 1, |
| 35 | + VPE_INPUT_UPPER_LEFT_FLIPPER = 2, |
| 36 | + VPE_INPUT_UPPER_RIGHT_FLIPPER = 3, |
| 37 | + VPE_INPUT_LEFT_MAGNASAVE = 4, |
| 38 | + VPE_INPUT_RIGHT_MAGNASAVE = 5, |
| 39 | + VPE_INPUT_START = 6, |
| 40 | + VPE_INPUT_PLUNGE = 7, |
| 41 | + VPE_INPUT_PLUNGER_ANALOG = 8, |
| 42 | + VPE_INPUT_COIN_INSERT_1 = 9, |
| 43 | + VPE_INPUT_COIN_INSERT_2 = 10, |
| 44 | + VPE_INPUT_COIN_INSERT_3 = 11, |
| 45 | + VPE_INPUT_COIN_INSERT_4 = 12, |
| 46 | + VPE_INPUT_EXIT_GAME = 13, |
| 47 | + VPE_INPUT_SLAM_TILT = 14, |
| 48 | + VPE_INPUT_MAX = 32 // Reserve space for future actions |
| 49 | +} VpeInputAction; |
| 50 | + |
| 51 | +// Input binding type |
| 52 | +typedef enum { |
| 53 | + VPE_BINDING_KEYBOARD = 0, |
| 54 | + VPE_BINDING_GAMEPAD = 1, |
| 55 | + VPE_BINDING_MOUSE = 2 |
| 56 | +} VpeBindingType; |
| 57 | + |
| 58 | +// Key codes (Windows virtual key codes, mapped on other platforms) |
| 59 | +typedef enum { |
| 60 | + VPE_KEY_LSHIFT = 0xA0, |
| 61 | + VPE_KEY_RSHIFT = 0xA1, |
| 62 | + VPE_KEY_LCONTROL = 0xA2, |
| 63 | + VPE_KEY_RCONTROL = 0xA3, |
| 64 | + VPE_KEY_SPACE = 0x20, |
| 65 | + VPE_KEY_RETURN = 0x0D, |
| 66 | + VPE_KEY_A = 0x41, |
| 67 | + VPE_KEY_S = 0x53, |
| 68 | + VPE_KEY_D = 0x44, |
| 69 | + VPE_KEY_W = 0x57, |
| 70 | + // Add more as needed |
| 71 | +} VpeKeyCode; |
| 72 | + |
| 73 | +// Input event structure (matches C# struct layout) |
| 74 | +typedef struct { |
| 75 | + int64_t timestampUsec; // Microsecond timestamp |
| 76 | + int32_t action; // VpeInputAction |
| 77 | + float value; // 0.0 (released) or 1.0 (pressed), or analog value |
| 78 | + int32_t _padding; // Ensure 16-byte alignment |
| 79 | +} VpeInputEvent; |
| 80 | + |
| 81 | +// Input binding structure |
| 82 | +typedef struct { |
| 83 | + int32_t action; // VpeInputAction |
| 84 | + int32_t bindingType; // VpeBindingType |
| 85 | + int32_t keyCode; // VpeKeyCode or gamepad button index |
| 86 | + int32_t _padding; |
| 87 | +} VpeInputBinding; |
| 88 | + |
| 89 | +// Callback for input events |
| 90 | +typedef void (*VpeInputEventCallback)(const VpeInputEvent* event, void* userData); |
| 91 | + |
| 92 | +// Initialize input system |
| 93 | +// Returns 1 on success, 0 on failure |
| 94 | +VPE_API int VpeInputInit(void); |
| 95 | + |
| 96 | +// Shutdown input system |
| 97 | +VPE_API void VpeInputShutdown(void); |
| 98 | + |
| 99 | +// Set input bindings |
| 100 | +// bindings: Array of VpeInputBinding |
| 101 | +// count: Number of bindings |
| 102 | +VPE_API void VpeInputSetBindings(const VpeInputBinding* bindings, int count); |
| 103 | + |
| 104 | +// Start polling thread |
| 105 | +// callback: Function called for each input event |
| 106 | +// userData: User data passed to callback |
| 107 | +// pollIntervalUs: Polling interval in microseconds (default 500) |
| 108 | +VPE_API int VpeInputStartPolling(VpeInputEventCallback callback, void* userData, int pollIntervalUs); |
| 109 | + |
| 110 | +// Stop polling thread |
| 111 | +VPE_API void VpeInputStopPolling(void); |
| 112 | + |
| 113 | +// Get high-resolution timestamp in microseconds |
| 114 | +VPE_API int64_t VpeGetTimestampUsec(void); |
| 115 | + |
| 116 | +// Set thread priority to time-critical (best effort) |
| 117 | +VPE_API void VpeSetThreadPriority(void); |
| 118 | + |
| 119 | +#ifdef __cplusplus |
| 120 | +} |
| 121 | +#endif |
0 commit comments