Skip to content

Commit cfd35f1

Browse files
committed
Merge remote-tracking branch 'origin/pr/poll-sdl-events' into android
2 parents 3f5e7bb + 962bc38 commit cfd35f1

8 files changed

Lines changed: 82 additions & 88 deletions

File tree

Descent3/sdlmain.cpp

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -177,37 +177,6 @@ class oeD3LnxDatabase final : public oeLnxAppDatabase {
177177
}
178178
};
179179

180-
bool sdlKeyFilter(const SDL_Event *event);
181-
bool sdlMouseButtonUpFilter(const SDL_Event *event);
182-
bool sdlMouseButtonDownFilter(const SDL_Event *event);
183-
bool sdlMouseWheelFilter(const SDL_Event *event);
184-
bool sdlMouseMotionFilter(const SDL_Event *event);
185-
186-
bool SDLCALL d3SDLEventFilter(void *userdata, SDL_Event *event) {
187-
switch (event->type) {
188-
case SDL_EVENT_KEY_UP:
189-
case SDL_EVENT_KEY_DOWN:
190-
return (sdlKeyFilter(event));
191-
case SDL_EVENT_JOYSTICK_BALL_MOTION:
192-
case SDL_EVENT_MOUSE_MOTION:
193-
return (sdlMouseMotionFilter(event));
194-
case SDL_EVENT_MOUSE_BUTTON_UP:
195-
return (sdlMouseButtonUpFilter(event));
196-
case SDL_EVENT_MOUSE_BUTTON_DOWN:
197-
return (sdlMouseButtonDownFilter(event));
198-
case SDL_EVENT_MOUSE_WHEEL:
199-
return (sdlMouseWheelFilter(event));
200-
case SDL_EVENT_QUIT:
201-
SDL_Quit();
202-
_exit(0);
203-
break;
204-
default:
205-
break;
206-
} // switch
207-
208-
return (1);
209-
}
210-
211180
// ---------------------------------------------------------------------------
212181
// Main
213182
// creates all the OS objects and then runs Descent 3.
@@ -252,8 +221,6 @@ int main(int argc, char *argv[]) {
252221
return (0);
253222
}
254223

255-
// !!! FIXME: Don't use an event filter!
256-
SDL_SetEventFilter(d3SDLEventFilter, nullptr);
257224
install_signal_handlers();
258225

259226
// Initialize our OS Object. This could be a game dependant OS object, or a default OS object.

ddio/ddio.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@
9090

9191
#include <cstdlib>
9292

93+
#include <SDL3/SDL.h>
94+
9395
#include "ddio.h"
96+
#include "ddio_lnx.h"
9497
#include "joystick.h"
9598
#include "log.h"
9699
#include "pserror.h"
@@ -140,9 +143,46 @@ void ddio_Resume() {
140143
ddio_InternalKeyResume();
141144
ddio_InternalMouseResume();
142145
}
146+
143147
// handles buffered input from devices once per frame.
148+
void sdlKeyEvent(const SDL_Event *event);
149+
void sdlMouseButtonUpEvent(const SDL_Event *event);
150+
void sdlMouseButtonDownEvent(const SDL_Event *event);
151+
void sdlMouseWheelEvent(const SDL_Event *event);
152+
void sdlMouseMotionEvent(const SDL_Event *event);
144153
void ddio_Frame() {
154+
if (Input_mode == Input_sdl) {
155+
SDL_Event event;
156+
while (SDL_PollEvent(&event)) {
157+
switch (event.type) {
158+
case SDL_EVENT_KEY_UP:
159+
case SDL_EVENT_KEY_DOWN:
160+
sdlKeyEvent(&event);
161+
break;
162+
case SDL_EVENT_JOYSTICK_BALL_MOTION:
163+
case SDL_EVENT_MOUSE_MOTION:
164+
sdlMouseMotionEvent(&event);
165+
break;
166+
case SDL_EVENT_MOUSE_BUTTON_UP:
167+
sdlMouseButtonUpEvent(&event);
168+
break;
169+
case SDL_EVENT_MOUSE_BUTTON_DOWN:
170+
sdlMouseButtonDownEvent(&event);
171+
break;
172+
case SDL_EVENT_MOUSE_WHEEL:
173+
sdlMouseWheelEvent(&event);
174+
break;
175+
case SDL_EVENT_QUIT:
176+
SDL_Quit();
177+
_exit(0);
178+
break;
179+
default:
180+
break;
181+
}
182+
}
183+
}
184+
145185
ddio_InternalKeyFrame();
146186
ddio_InternalMouseFrame();
147187
ddio_InternalJoyFrame();
148-
}
188+
}

ddio/ddio_lnx.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,10 @@
2828

2929
extern oeLnxApplication *Lnx_app_obj;
3030

31+
enum input_mode_t {
32+
Input_null,
33+
Input_sdl,
34+
};
35+
extern enum input_mode_t Input_mode;
36+
3137
#endif

ddio/lnxio.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@
6969
#include "application.h"
7070
#include "ddio.h"
7171
#include "log.h"
72+
#include "ddio_lnx.h"
7273

7374
bool DDIO_init = false;
7475
oeLnxApplication *Lnx_app_obj = NULL;
76+
enum input_mode_t Input_mode;
7577

7678
// ----------------------------------------------------------------------------
7779
// Initialization and destruction functions
@@ -80,6 +82,17 @@ oeLnxApplication *Lnx_app_obj = NULL;
8082
bool ddio_InternalInit(ddio_init_info *init_info) {
8183
LOG_DEBUG << "DDIO: ddio_InternalInit() called.";
8284
Lnx_app_obj = (oeLnxApplication *)init_info->obj;
85+
86+
if (!Lnx_app_obj) {
87+
return false;
88+
}
89+
90+
tLnxAppInfo app_info;
91+
Lnx_app_obj->get_info(&app_info);
92+
93+
// determine if we are to use SDL or null mode
94+
Input_mode = (app_info.flags & APPFLAG_USESERVICE) ? Input_null : Input_sdl;
95+
8396
DDIO_init = true;
8497
return true;
8598
}

ddio/lnxkey.cpp

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "application.h"
5353
#include "ddio.h"
5454
#include "mono.h"
55+
#include "ddio_lnx.h"
5556

5657
volatile struct tLnxKeys {
5758
union {
@@ -87,29 +88,12 @@ void ddio_sdl_InternalResetKey(uint8_t key);
8788
bool ddio_sdl_KeyFrame();
8889
void ddio_sdl_InternalKeyFrame(void);
8990

90-
enum {
91-
Input_null,
92-
Input_sdl // Input_svga,Input_xwin
93-
} Keyboard_mode;
94-
9591
// ----------------------------------------------------------------------------
9692
// Initialization of keyboard device.
9793
// ----------------------------------------------------------------------------
9894

9995
bool ddio_InternalKeyInit(ddio_init_info *init_info) {
100-
oeLnxApplication *app = (oeLnxApplication *)init_info->obj;
101-
tLnxAppInfo app_info;
102-
103-
if (!app) {
104-
return false;
105-
}
106-
107-
app->get_info(&app_info);
108-
109-
// determine if we are to use SDL or null mode
110-
Keyboard_mode = (app_info.flags & APPFLAG_USESERVICE) ? Input_null : Input_sdl;
111-
112-
switch (Keyboard_mode) {
96+
switch (Input_mode) {
11397
case Input_null:
11498
return ddio_null_InternalKeyInit(init_info);
11599
case Input_sdl:
@@ -120,7 +104,7 @@ bool ddio_InternalKeyInit(ddio_init_info *init_info) {
120104
}
121105

122106
void ddio_InternalKeyClose() {
123-
switch (Keyboard_mode) {
107+
switch (Input_mode) {
124108
case Input_null:
125109
return ddio_null_InternalKeyClose();
126110
case Input_sdl:
@@ -129,7 +113,7 @@ void ddio_InternalKeyClose() {
129113
}
130114

131115
bool ddio_InternalKeyState(uint8_t key) {
132-
switch (Keyboard_mode) {
116+
switch (Input_mode) {
133117
case Input_null:
134118
return ddio_null_InternalKeyState(key);
135119
case Input_sdl:
@@ -140,7 +124,7 @@ bool ddio_InternalKeyState(uint8_t key) {
140124
}
141125

142126
void ddio_InternalKeySuspend() {
143-
switch (Keyboard_mode) {
127+
switch (Input_mode) {
144128
case Input_null:
145129
ddio_null_InternalKeySuspend();
146130
break;
@@ -150,7 +134,7 @@ void ddio_InternalKeySuspend() {
150134
}
151135

152136
void ddio_InternalKeyResume() {
153-
switch (Keyboard_mode) {
137+
switch (Input_mode) {
154138
case Input_null:
155139
ddio_null_InternalKeyResume();
156140
break;
@@ -160,7 +144,7 @@ void ddio_InternalKeyResume() {
160144
}
161145

162146
float ddio_InternalKeyDownTime(uint8_t key) {
163-
switch (Keyboard_mode) {
147+
switch (Input_mode) {
164148
case Input_null:
165149
return ddio_null_InternalKeyDownTime(key);
166150
case Input_sdl:
@@ -171,7 +155,7 @@ float ddio_InternalKeyDownTime(uint8_t key) {
171155
}
172156

173157
void ddio_InternalResetKey(uint8_t key) {
174-
switch (Keyboard_mode) {
158+
switch (Input_mode) {
175159
case Input_null:
176160
ddio_null_InternalResetKey(key);
177161
break;
@@ -181,7 +165,7 @@ void ddio_InternalResetKey(uint8_t key) {
181165
}
182166

183167
bool ddio_KeyFrame() {
184-
switch (Keyboard_mode) {
168+
switch (Input_mode) {
185169
case Input_sdl:
186170
break;
187171
case Input_null:
@@ -192,7 +176,7 @@ bool ddio_KeyFrame() {
192176
}
193177

194178
void ddio_InternalKeyFrame(void) {
195-
switch (Keyboard_mode) {
179+
switch (Input_mode) {
196180
case Input_null:
197181
ddio_null_InternalKeyFrame();
198182
break;

ddio/lnxkey_sdl.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -340,15 +340,15 @@ static inline uint8_t sdlkeycode_to_keycode(uint32_t sdlkeycode) {
340340
return (uint8_t)rc;
341341
}
342342

343-
bool sdlKeyFilter(const SDL_Event *event) {
343+
void sdlKeyEvent(const SDL_Event *event) {
344344
uint8_t kc = 0;
345345

346346
if ((event->type != SDL_EVENT_KEY_UP) && (event->type != SDL_EVENT_KEY_DOWN))
347-
return true;
347+
return;
348348

349349
if (event->key.down) {
350350
if (event->key.repeat) {
351-
return false; // ignore these, we only want to know if it's a first time pressed, not a key-repeat.
351+
return; // ignore these, we only want to know if it's a first time pressed, not a key-repeat.
352352
}
353353
kc = sdlkeycode_to_keycode(event->key.key);
354354
if (event->key.mod & SDL_KMOD_CTRL) {
@@ -358,15 +358,15 @@ bool sdlKeyFilter(const SDL_Event *event) {
358358
bool grab = !ddio_MouseGetGrab();
359359
ddio_MouseSetGrab(grab);
360360
SDL_SetWindowRelativeMouseMode(GSDLWindow, grab);
361-
return false;
361+
return;
362362
} // switch
363363
} // if
364364

365365
else if (event->key.mod & SDL_KMOD_ALT) {
366366
if ((kc == KEY_ENTER) || (kc == KEY_PADENTER)) {
367367
Game_fullscreen = !Game_fullscreen;
368368
rend_SetFullScreen(Game_fullscreen);
369-
return false;
369+
return;
370370
} // if
371371
} // else if
372372

@@ -382,9 +382,7 @@ bool sdlKeyFilter(const SDL_Event *event) {
382382
ddio_UpdateKeyState(kc, false);
383383
} // if
384384
}
385-
386-
return false;
387-
} // sdlKeyFilter
385+
}
388386

389387
bool ddio_sdl_InternalKeyInit(ddio_init_info *init_info) {
390388
// reset key list

ddio/lnxmouse.cpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ void ddio_MouseMode(int mode) { Mouse_mode = mode; }
190190
// virtual coordinate system for mouse (match to video resolution set for optimal mouse usage.
191191
void ddio_MouseSetVCoords(int width, int height) { ddio_MouseSetLimits(0, 0, width, height); }
192192

193-
bool sdlMouseButtonDownFilter(SDL_Event const *event) {
193+
void sdlMouseButtonDownEvent(SDL_Event const *event) {
194194
ASSERT(event->type == SDL_EVENT_MOUSE_BUTTON_DOWN);
195195

196196
const SDL_MouseButtonEvent *ev = &event->button;
@@ -258,11 +258,9 @@ bool sdlMouseButtonDownFilter(SDL_Event const *event) {
258258
MB_queue.send(mevt);
259259
// mprintf(0, "MOUSE Button 7: Down\n");
260260
}
261-
262-
return false;
263261
}
264262

265-
bool sdlMouseButtonUpFilter(SDL_Event const *event) {
263+
void sdlMouseButtonUpEvent(SDL_Event const *event) {
266264
ASSERT(event->type == SDL_EVENT_MOUSE_BUTTON_UP);
267265

268266
const SDL_MouseButtonEvent *ev = &event->button;
@@ -332,11 +330,9 @@ bool sdlMouseButtonUpFilter(SDL_Event const *event) {
332330
MB_queue.send(mevt);
333331
// mprintf(0, "MOUSE Button 7: Up\n");
334332
}
335-
336-
return false;
337333
}
338334

339-
bool sdlMouseWheelFilter(SDL_Event const *event) {
335+
void sdlMouseWheelEvent(SDL_Event const *event) {
340336
ASSERT(event->type == SDL_EVENT_MOUSE_WHEEL);
341337

342338
const SDL_MouseWheelEvent *ev = &event->wheel;
@@ -383,11 +379,9 @@ bool sdlMouseWheelFilter(SDL_Event const *event) {
383379
MB_queue.send(mevt);
384380
// mprintf(0, "MOUSE Scrollwheel: Rolled Down\n");
385381
}
386-
387-
return false;
388382
}
389383

390-
bool sdlMouseMotionFilter(SDL_Event const *event) {
384+
void sdlMouseMotionEvent(SDL_Event const *event) {
391385
if (event->type == SDL_EVENT_JOYSTICK_BALL_MOTION) {
392386
DDIO_mouse_state.dx = event->jball.xrel / 100.0f;
393387
DDIO_mouse_state.dy = event->jball.yrel / 100.0f;
@@ -408,16 +402,10 @@ bool sdlMouseMotionFilter(SDL_Event const *event) {
408402
DDIO_mouse_state.y = DDIO_mouse_state.t;
409403
if (DDIO_mouse_state.y >= DDIO_mouse_state.b)
410404
DDIO_mouse_state.y = DDIO_mouse_state.b - 1;
411-
412-
return false;
413405
}
414406

415407
// This function will handle all mouse events.
416-
void ddio_InternalMouseFrame(void) {
417-
static unsigned frame_count = 0;
418-
SDL_PumpEvents();
419-
frame_count++;
420-
}
408+
void ddio_InternalMouseFrame(void) {}
421409

422410
/* x, y = absolute mouse position
423411
dx, dy = mouse deltas since last call

ddio/sdljoy.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,4 @@ static int joyGetNumDevs(void) {
341341
return found;
342342
}
343343

344-
void ddio_InternalJoyFrame(void) {
345-
// All the work is done already in SDL_PumpEvents()
346-
}
344+
void ddio_InternalJoyFrame(void) {}

0 commit comments

Comments
 (0)