Skip to content

Commit 1d0b397

Browse files
Peter0x44MotoLegacy
authored andcommitted
Add hack abstraction to make mouse IDs sequential
SDL3 (and therefore sdl2-compat) changed mouse IDs from being seuqential (so they could be used as an index) to effectively random. We have to hack over an abstraction for this ourselves to make it work. Lol! fixes nzp-team/nzportable#1131
1 parent 9b42534 commit 1d0b397

1 file changed

Lines changed: 33 additions & 7 deletions

File tree

engine/client/in_sdl.c

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,28 @@ void INS_SetOSK(int osk)
11721172
{
11731173
}
11741174
#endif
1175+
1176+
// SDL2 gives IDs from zero, but SDL3 does not
1177+
// sdl2-compat also doesn't make any attempt to revert this behavior, so we have to do it ourselves
1178+
static uint32_t internal__mice[16];
1179+
static int internal__mice_count=0;
1180+
1181+
int INS_MouseID(uint32_t mid)
1182+
{
1183+
for (int i = 0; i < internal__mice_count; ++i)
1184+
{
1185+
if (internal__mice[i] == mid)
1186+
return i;
1187+
}
1188+
if (internal__mice_count < 16)
1189+
{
1190+
internal__mice[internal__mice_count] = mid;
1191+
return internal__mice_count++;
1192+
}
1193+
return -1;
1194+
}
1195+
1196+
11751197
void Sys_SendKeyEvents(void)
11761198
{
11771199
SDL_Event event;
@@ -1207,6 +1229,7 @@ void Sys_SendKeyEvents(void)
12071229
#endif
12081230
#endif
12091231
// NZP end
1232+
int which;
12101233

12111234
while(SDL_PollEvent(&event))
12121235
{
@@ -1352,26 +1375,28 @@ void Sys_SendKeyEvents(void)
13521375
#if SDL_MAJOR_VERSION >= 2
13531376
if (event.motion.which == SDL_TOUCH_MOUSEID)
13541377
break; //ignore legacy touch events.
1378+
which = INS_MouseID(event.motion.which);
13551379
#endif
13561380
if (!mouseactive)
1357-
IN_MouseMove(event.motion.which, true, event.motion.x, event.motion.y, 0, 0);
1381+
IN_MouseMove(which, true, event.motion.x, event.motion.y, 0, 0);
13581382
else
1359-
IN_MouseMove(event.motion.which, false, event.motion.xrel, event.motion.yrel, 0, 0);
1383+
IN_MouseMove(which, false, event.motion.xrel, event.motion.yrel, 0, 0);
13601384
break;
13611385

13621386
#if SDL_MAJOR_VERSION >= 2
13631387
case SDL_MOUSEWHEEL:
1388+
which = INS_MouseID(event.wheel.which);
13641389
if (event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED)
13651390
event.wheel.y *= -1;
13661391
for (; event.wheel.y > 0; event.wheel.y--)
13671392
{
1368-
IN_KeyEvent(event.button.which, true, K_MWHEELUP, 0);
1369-
IN_KeyEvent(event.button.which, false, K_MWHEELUP, 0);
1393+
IN_KeyEvent(which, true, K_MWHEELUP, 0);
1394+
IN_KeyEvent(which, false, K_MWHEELUP, 0);
13701395
}
13711396
for (; event.wheel.y < 0; event.wheel.y++)
13721397
{
1373-
IN_KeyEvent(event.button.which, true, K_MWHEELDOWN, 0);
1374-
IN_KeyEvent(event.button.which, false, K_MWHEELDOWN, 0);
1398+
IN_KeyEvent(which, true, K_MWHEELDOWN, 0);
1399+
IN_KeyEvent(which, false, K_MWHEELDOWN, 0);
13751400
}
13761401
/* for (; event.wheel.x > 0; event.wheel.x--)
13771402
{
@@ -1392,10 +1417,11 @@ void Sys_SendKeyEvents(void)
13921417
if (event.button.which == SDL_TOUCH_MOUSEID)
13931418
break; //ignore legacy touch events. SDL_FINGER* events above will handle it (for multitouch)
13941419
#endif
1420+
which = INS_MouseID(event.button.which);
13951421
//Hmm. SDL allows for 255 buttons, but only defines 5...
13961422
if (event.button.button > sizeof(tbl_sdltoquakemouse)/sizeof(tbl_sdltoquakemouse[0]))
13971423
event.button.button = sizeof(tbl_sdltoquakemouse)/sizeof(tbl_sdltoquakemouse[0]);
1398-
IN_KeyEvent(event.button.which, event.button.state, tbl_sdltoquakemouse[event.button.button-1], 0);
1424+
IN_KeyEvent(which, event.button.state, tbl_sdltoquakemouse[event.button.button-1], 0);
13991425
break;
14001426

14011427
#if SDL_MAJOR_VERSION >= 2

0 commit comments

Comments
 (0)