载入中...
搜索中...
未找到
EventSink.cpp
浏览该文件的文档.
1// eve-hot-path
2// Translates SDL joystick and game-controller events into engine messages, and
3// keeps the module's device table in sync on hotplug.
4//
5// This lived in the SDL event pump, which therefore had to know about the
6// Joystick module and its Pad type. Owning it here removes that edge.
7// The Joystick module binds itself as owner at construction so the hot path
8// never looks up ModuleManager.
9
10#include "common/Capability.h"
11#include "joystick/Joystick.h"
12#include "joystick/Pad.h"
16
17#include <SDL2/SDL_events.h>
18#include <SDL2/SDL_gamecontroller.h>
19
20#include <cmath>
21
23namespace {
24
27
28eve::joystick::Joystick *g_owner = nullptr;
29
31int64_t axisMilli(int16_t raw) {
32 return static_cast<int64_t>(std::lround(clampAxis(raw / 32768.0f) * 1000.0f));
33}
34
35class JoystickEventSink : public eve::platform_event::IPlatformEventSink {
36public:
37 Message *translatePlatformEvent(const void *nativeEvent) override {
38 const auto &e = *static_cast<const SDL_Event *>(nativeEvent);
39 auto *joy = g_owner;
40 if (!joy) return nullptr;
41
42 switch (e.type) {
43 case SDL_JOYDEVICEADDED: {
44 auto *pad = joy->addJoystick(e.jdevice.which);
45 if (!pad) return nullptr;
46 return new Message("joystickadded",
47 {Variant::makeInt(pad->getID()),
48 Variant::makeString(pad->getName()),
49 Variant::makeInt(pad->isGamepad() ? 1 : 0)});
50 }
51 case SDL_JOYDEVICEREMOVED: {
52 auto *pad = joy->getJoystickFromID(e.jdevice.which);
53 if (!pad) return nullptr;
54 const int id = pad->getID();
55 const std::string name = pad->getName();
56 joy->removeJoystick(pad);
57 return new Message("joystickremoved",
58 {Variant::makeInt(id), Variant::makeString(name)});
59 }
60 case SDL_JOYBUTTONDOWN:
61 case SDL_JOYBUTTONUP: {
62 auto *pad = joy->getJoystickFromID(e.jbutton.which);
63 if (!pad) return nullptr;
64 return new Message(
65 e.type == SDL_JOYBUTTONDOWN ? "joystickpressed" : "joystickreleased",
66 {Variant::makeInt(pad->getID()), Variant::makeInt(e.jbutton.button)});
67 }
68 case SDL_JOYAXISMOTION: {
69 auto *pad = joy->getJoystickFromID(e.jaxis.which);
70 if (!pad) return nullptr;
71 return new Message("joystickaxis",
72 {Variant::makeInt(pad->getID()), Variant::makeInt(e.jaxis.axis),
73 Variant::makeInt(axisMilli(e.jaxis.value))});
74 }
75 case SDL_JOYHATMOTION: {
76 auto *pad = joy->getJoystickFromID(e.jhat.which);
77 if (!pad) return nullptr;
78 return new Message("joystickhat",
79 {Variant::makeInt(pad->getID()), Variant::makeInt(e.jhat.hat),
80 Variant::makeString(pad->getHat(e.jhat.hat))});
81 }
82 case SDL_CONTROLLERBUTTONDOWN:
83 case SDL_CONTROLLERBUTTONUP: {
84 auto *pad = joy->getJoystickFromID(e.cbutton.which);
85 if (!pad) return nullptr;
86 const char *btn = SDL_GameControllerGetStringForButton(
87 static_cast<SDL_GameControllerButton>(e.cbutton.button));
88 return new Message(
89 e.type == SDL_CONTROLLERBUTTONDOWN ? "gamepadpressed" : "gamepadreleased",
90 {Variant::makeInt(pad->getID()), Variant::makeString(btn ? btn : "")});
91 }
92 case SDL_CONTROLLERAXISMOTION: {
93 auto *pad = joy->getJoystickFromID(e.caxis.which);
94 if (!pad) return nullptr;
95 const char *axis = SDL_GameControllerGetStringForAxis(
96 static_cast<SDL_GameControllerAxis>(e.caxis.axis));
97 return new Message("gamepadaxis", {Variant::makeInt(pad->getID()),
98 Variant::makeString(axis ? axis : ""),
99 Variant::makeInt(axisMilli(e.caxis.value))});
100 }
101 default:
102 return nullptr;
103 }
104 }
105};
106
107struct Register {
108 Register() {
109 static JoystickEventSink sink;
110 eve::cap::addListener<eve::platform_event::IPlatformEventSink>(&sink,
112 }
113} g_register;
114
115} // namespace
116
118
119} // namespace eve::joystick::sdl
std::string name
Definition ECS.cpp:154
double raw
JointPoint2D axis
std::shared_ptr< OceanGpuSpectrum > owner
Joystick / gamepad manager. Tracks connected pads and gamecontroller mappings.
Definition Joystick.h:14
virtual Pad * addJoystick(int deviceindex)=0
Open a device by SDL device index; returns null on failure.
EVENGINE_API_FOUNDATION public API.
A named event carrying an ordered list of Variant payloads. Pushed messages are heap-allocated; the q...
void bindJoystickEventSinkOwner(eve::joystick::Joystick *owner) noexcept
Bind the live Joystick module into the SDL event sink. @ownership Borrowed; the Joystick module owns ...
float clampAxis(float x)
Clamps a raw axis sample into [-1, 1].
Definition Joystick.cpp:14
uint32_t pad[2]