Skip to content

Commit

Permalink
ogc: deliver Wiimote IR events as mouse events
Browse files Browse the repository at this point in the history
  • Loading branch information
mardy authored and WinterMute committed Jan 14, 2024
1 parent 4628b37 commit e50f963
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions src/video/ogc/SDL_ogcevents.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,63 @@

#ifdef SDL_VIDEO_DRIVER_OGC

/* Being a null driver, there's no event stream. We just define stubs for
most of the API. */
#include "SDL.h"

#include "../../events/SDL_events_c.h"

#include "SDL_ogcevents_c.h"
#include "SDL_ogcvideo.h"

#include <ogc/system.h>
#include <wiiuse/wpad.h>

/* These variables can be set from the handlers registered in SDL_main() */
bool OGC_PowerOffRequested = false;
bool OGC_ResetRequested = false;

#ifdef __wii__
#define MAX_WII_MOUSE_BUTTONS 2
static const struct {
int wii;
int mouse;
} s_mouse_button_map[MAX_WII_MOUSE_BUTTONS] = {
{ WPAD_BUTTON_B, SDL_BUTTON_LEFT },
{ WPAD_BUTTON_A, SDL_BUTTON_RIGHT },
};

static void pump_ir_events(_THIS)
{
if (!_this->windows) return;

if (!SDL_WasInit(SDL_INIT_JOYSTICK)) {
/* Get events from WPAD; we don't need to do this if the joystick
* system was initialized, because in that case this operation is done
* there at every event loop iteration. */
WPAD_ReadPending(WPAD_CHAN_ALL, NULL);
}

for (int i = 0; i < 4; i++) {
WPADData *data = WPAD_Data(i);

if (!data->ir.smooth_valid) continue;

SDL_SendMouseMotion(_this->windows, i,
0, data->ir.sx, data->ir.sy);

for (int b = 0; b < MAX_WII_MOUSE_BUTTONS; b++) {
if (data->btns_d & s_mouse_button_map[b].wii) {
SDL_SendMouseButton(_this->windows, i,
SDL_PRESSED, s_mouse_button_map[b].mouse);
}
if (data->btns_u & s_mouse_button_map[b].wii) {
SDL_SendMouseButton(_this->windows, i,
SDL_RELEASED, s_mouse_button_map[b].mouse);
}
}
}
}
#endif

void OGC_PumpEvents(_THIS)
{
if (OGC_ResetRequested || OGC_PowerOffRequested) {
Expand All @@ -46,6 +89,10 @@ void OGC_PumpEvents(_THIS)
SYS_ResetSystem(SYS_POWEROFF, 0, 0);
}
}

#ifdef __wii__
pump_ir_events(_this);
#endif
}

#endif /* SDL_VIDEO_DRIVER_OGC */
Expand Down

0 comments on commit e50f963

Please sign in to comment.