diff --git a/src/command.h b/src/command.h new file mode 100644 index 0000000..2358a6f --- /dev/null +++ b/src/command.h @@ -0,0 +1,11 @@ +#pragma once + +#define exe(cmd) cmd.command(cmd.args, cmd.ret); + +typedef void (*CommandFunction)(void* args, void* ret); + +typedef struct Command_s{ + void* args; + void* ret; + CommandFunction command; +}Command_t; diff --git a/src/input.c b/src/input.c new file mode 100644 index 0000000..42f499c --- /dev/null +++ b/src/input.c @@ -0,0 +1,16 @@ +#include +#include "input.h" +#include "command.h" + +// in theroy your input should be a single ton but i want to support multiple interfaces +void handleInput(Input_t input_device) { + Inputs_e input = input_device.handle.getInput(); + + switch (input) { + case BUTTON_UP: exe(input_device.button_up_cmd); break; + case BUTTON_LEFT: exe(input_device.button_left_cmd); break; + case BUTTON_RIGHT: exe(input_device.button_right_cmd); break; + case BUTTON_DOWN: exe(input_device.button_down_cmd); break; + default: printf("HOW THE FUCK DID YOU PRESS A NON EXISTANT BUTTON"); break; + } +} \ No newline at end of file diff --git a/src/input.h b/src/input.h new file mode 100644 index 0000000..24e5838 --- /dev/null +++ b/src/input.h @@ -0,0 +1,26 @@ +#pragma once + +#include "command.h " + +typedef enum { + BUTTON_UP, + BUTTON_LEFT, + BUTTON_RIGHT, + BUTTON_DOWN, + TOTAL_COMMANDS, +} Inputs_e; + +typedef struct InputHandler_s { + Inputs_e (*getInput)(void); +}InputHandler_t; + +typedef struct Input_s{ + Command_t button_up_cmd; + Command_t button_left_cmd; + Command_t button_right_cmd; + Command_t button_down_cmd; + InputHandler_t handle; +}Input_t; + +void handleInput(Input_t input_device); + diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..9272dfd --- /dev/null +++ b/src/main.c @@ -0,0 +1,73 @@ +#include +#include "command.h " +#include "command.h" +#include "input.h" + + + + +// this is the underlying function +int printButton(const char* string) { + return printf(string); +} + +void commandWrapper(void* args, void* ret) { + // we cast ret to a int and then place the return val in to it + *(int*)(ret) = printButton(*(const char**)(args)); +} + + +Inputs_e getinput() { + return BUTTON_DOWN; +} + + +// this is the only part i dont like +const char* button_up_cmd_args = "up button pressed"; +int button_up_cmd_return_val = 0; // dont leave thing uninstalised + +const char* button_left_cmd_args = "left button pressed"; +int button_left_cmd_return_val = 0; // dont leave thing uninstalised + +const char* button_right_cmd_args = "right button pressed"; +int button_right_cmd_return_val = 0; // dont leave thing uninstalised + +const char* button_down_cmd_args = "down button pressed"; +int button_down_cmd_return_val = 0; // dont leave thing uninstalised + +static Input_t input_device = (Input_t){ + .button_up_cmd = (Command_t){ + .args = (void*)(&button_up_cmd_args), + .ret = (void*)(&button_up_cmd_return_val), + .command = &commandWrapper, + }, + .button_left_cmd = (Command_t){ + .args = (void*)(&button_left_cmd_args), + .ret = (void*)(&button_left_cmd_return_val), + .command = &commandWrapper, + }, + .button_right_cmd = (Command_t){ + .args = (void*)(&button_right_cmd_args), + .ret = (void*)(&button_right_cmd_return_val), + .command = &commandWrapper, + }, + .button_down_cmd = (Command_t){ + .args = (void*)(&button_down_cmd_args), + .ret = (void*)(&button_down_cmd_return_val), + .command = &commandWrapper, + }, + .handle = (InputHandler_t) { + .getInput = &getinput, + } +}; + +int main() { + + // we have our input now we will handle it + handleInput(input_device); + + return 0; +} + + +