Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add input filter callback #1068

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
/.vscode
/projects/unix/*.dll
/src/asm_defines/*.h

.cache/
compile_commands.json
7 changes: 7 additions & 0 deletions src/api/frontend.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <stdlib.h>
#include <string.h>
#include <md5.h>
#include "backends/plugins_compat/plugins_compat.h"

#define M64P_CORE_PROTOTYPES 1
#include "callbacks.h"
Expand Down Expand Up @@ -318,6 +319,7 @@ EXPORT m64p_error CALL CoreDoCommand(m64p_command Command, int ParamInt, void *P
case M64CMD_SET_FRAME_CALLBACK:
*(void**)&g_FrameCallback = ParamPtr;
return M64ERR_SUCCESS;
return M64ERR_SUCCESS;
case M64CMD_TAKE_NEXT_SCREENSHOT:
if (!g_EmulatorRunning)
return M64ERR_INVALID_STATE;
Expand Down Expand Up @@ -347,6 +349,11 @@ EXPORT m64p_error CALL CoreDoCommand(m64p_command Command, int ParamInt, void *P
return M64ERR_INPUT_INVALID;
g_media_loader = *(m64p_media_loader*)ParamPtr;
return M64ERR_SUCCESS;
case M64CMD_SET_INPUT_FILTER:
if (ParamInt != sizeof(m64p_input_filter) || ParamPtr == NULL)
return M64ERR_INPUT_INVALID;
g_input_filter = *(m64p_input_filter*)ParamPtr;
return M64ERR_SUCCESS;
case M64CMD_NETPLAY_INIT:
if (ParamInt < 1 || ParamPtr == NULL)
return M64ERR_INPUT_INVALID;
Expand Down
26 changes: 0 additions & 26 deletions src/api/m64p_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,32 +147,6 @@ typedef struct {
int Type;
} CONTROL;

typedef union {
unsigned int Value;
struct {
unsigned R_DPAD : 1;
unsigned L_DPAD : 1;
unsigned D_DPAD : 1;
unsigned U_DPAD : 1;
unsigned START_BUTTON : 1;
unsigned Z_TRIG : 1;
unsigned B_BUTTON : 1;
unsigned A_BUTTON : 1;

unsigned R_CBUTTON : 1;
unsigned L_CBUTTON : 1;
unsigned D_CBUTTON : 1;
unsigned U_CBUTTON : 1;
unsigned R_TRIG : 1;
unsigned L_TRIG : 1;
unsigned Reserved1 : 1;
unsigned Reserved2 : 1;

signed X_AXIS : 8;
signed Y_AXIS : 8;
};
} BUTTONS;

typedef struct {
CONTROL *Controls; /* A pointer to an array of 4 controllers .. eg:
CONTROL Controls[4]; */
Expand Down
37 changes: 36 additions & 1 deletion src/api/m64p_types.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Mupen64plus-core - m64p_types.h *
* Mupen64Plus homepage: https://mupen64plus.org/ *
* Copyright (C) 2024 Jacky Guo *
* Copyright (C) 2012 CasualJames *
* Copyright (C) 2009 Richard Goedeken *
* *
Expand Down Expand Up @@ -29,6 +30,7 @@

/* necessary headers */
#include <stdint.h>
#include <stdbool.h>
#if defined(WIN32)
#include <windows.h>
#endif
Expand Down Expand Up @@ -62,6 +64,32 @@ typedef void (*m64p_input_callback)(void);
typedef void (*m64p_audio_callback)(void);
typedef void (*m64p_vi_callback)(void);

typedef union {
unsigned int Value;
struct {
unsigned R_DPAD : 1;
unsigned L_DPAD : 1;
unsigned D_DPAD : 1;
unsigned U_DPAD : 1;
unsigned START_BUTTON : 1;
unsigned Z_TRIG : 1;
unsigned B_BUTTON : 1;
unsigned A_BUTTON : 1;

unsigned R_CBUTTON : 1;
unsigned L_CBUTTON : 1;
unsigned D_CBUTTON : 1;
unsigned U_CBUTTON : 1;
unsigned R_TRIG : 1;
unsigned L_TRIG : 1;
unsigned Reserved1 : 1;
unsigned Reserved2 : 1;

signed X_AXIS : 8;
signed Y_AXIS : 8;
};
} BUTTONS;

typedef enum {
M64TYPE_INT = 1,
M64TYPE_FLOAT,
Expand Down Expand Up @@ -171,7 +199,8 @@ typedef enum {
M64CMD_PIF_OPEN,
M64CMD_ROM_SET_SETTINGS,
M64CMD_DISK_OPEN,
M64CMD_DISK_CLOSE
M64CMD_DISK_CLOSE,
M64CMD_SET_INPUT_FILTER,
} m64p_command;

typedef struct {
Expand Down Expand Up @@ -220,6 +249,12 @@ typedef struct {
char* (*get_dd_disk)(void* cb_data);
} m64p_media_loader;

typedef struct {
void* cb_data;

bool (*filter_input)(void* cb_data, int controller_num, BUTTONS* keys);
} m64p_input_filter;

/* ----------------------------------------- */
/* Structures to hold ROM image information */
/* ----------------------------------------- */
Expand Down
9 changes: 7 additions & 2 deletions src/backends/plugins_compat/input_plugin_compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "main/main.h"
#include "main/netplay.h"

#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <api/m64p_plugin.h>
Expand Down Expand Up @@ -85,8 +86,10 @@ static m64p_error input_plugin_get_input(void* opaque, uint32_t* input_)
cin_compat->last_pak_type = Controls[cin_compat->control_id].Plugin; //disable pak switching for netplay
}

/* return an error if controller is not plugged */
if (!Controls[cin_compat->control_id].Present) {
bool is_override = g_input_filter.filter_input(g_input_filter.cb_data, cin_compat->control_id, &keys);

/* return an error if controller is not plugged OR input was not provided by the filter callback */
if (!Controls[cin_compat->control_id].Present || is_override) {
return M64ERR_SYSTEM_FAIL;
}

Expand Down Expand Up @@ -228,3 +231,5 @@ const struct joybus_device_interface
input_plugin_read_controller,
input_plugin_controller_command,
};

m64p_input_filter g_input_filter;
3 changes: 3 additions & 0 deletions src/backends/plugins_compat/plugins_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#ifndef M64P_BACKENDS_PLUGINS_COMPAT_PLUGINS_COMPAT_H
#define M64P_BACKENDS_PLUGINS_COMPAT_PLUGINS_COMPAT_H

#include "api/m64p_types.h"
#include "backends/api/audio_out_backend.h"
#include "backends/api/controller_input_backend.h"
#include "backends/api/rumble_backend.h"
Expand Down Expand Up @@ -55,6 +56,8 @@ struct controller_input_compat
struct netplay_event* event_first;
};

extern m64p_input_filter g_input_filter;

extern const struct controller_input_backend_interface
g_icontroller_input_backend_plugin_compat;

Expand Down