vanilla/lib/vanilla.c

124 lines
2.8 KiB
C
Raw Normal View History

#include "vanilla.h"
#include <signal.h>
#include <unistd.h>
#include <wpa_ctrl.h>
2024-05-18 15:55:00 -04:00
#include "gamepad/gamepad.h"
2024-05-20 19:17:24 -04:00
#include "gamepad/input.h"
#include "status.h"
#include "sync.h"
#include "util.h"
#include "vanilla.h"
#include "wpa.h"
2024-05-23 19:02:55 -04:00
struct sync_args {
uint16_t code;
};
struct connect_args {
const char *wireless_interface;
vanilla_event_handler_t event_handler;
void *event_handler_context;
};
int thunk_to_sync(struct wpa_ctrl *ctrl, void *data)
{
2024-05-23 19:02:55 -04:00
struct sync_args *args = (struct sync_args *) data;
return sync_with_console_internal(ctrl, args->code);
2024-05-23 19:02:55 -04:00
}
int thunk_to_connect(struct wpa_ctrl *ctrl, void *data)
2024-05-23 19:02:55 -04:00
{
struct connect_args *args = (struct connect_args *) data;
return connect_as_gamepad_internal(ctrl, args->wireless_interface, args->event_handler, args->event_handler_context);
2024-05-23 19:02:55 -04:00
}
2024-05-23 19:02:55 -04:00
int vanilla_sync_with_console(const char *wireless_interface, uint16_t code)
{
const char *wireless_conf_file;
2024-05-23 19:02:55 -04:00
FILE *config;
wireless_conf_file = get_wireless_authenticate_config_filename();
config = fopen(wireless_conf_file, "w");
if (!config) {
print_info("FAILED TO WRITE TEMP CONFIG: %s", wireless_conf_file);
2024-05-23 19:02:55 -04:00
return VANILLA_ERROR;
}
2024-05-23 21:47:04 -04:00
fprintf(config, "ctrl_interface=%s\nupdate_config=1\n", wpa_ctrl_interface);
2024-05-23 19:02:55 -04:00
fclose(config);
2024-05-23 19:02:55 -04:00
struct sync_args args;
args.code = code;
return wpa_setup_environment(wireless_interface, wireless_conf_file, thunk_to_sync, &args);
}
2024-05-23 19:02:55 -04:00
int vanilla_connect_to_console(const char *wireless_interface, vanilla_event_handler_t event_handler, void *context)
{
2024-05-23 19:02:55 -04:00
struct connect_args args;
args.wireless_interface = wireless_interface;
args.event_handler = event_handler;
args.event_handler_context = context;
print_info("connecting with config: %s", get_wireless_connect_config_filename());
2024-05-23 19:02:55 -04:00
return wpa_setup_environment(wireless_interface, get_wireless_connect_config_filename(), thunk_to_connect, &args);
}
2024-05-23 19:02:55 -04:00
int vanilla_has_config()
{
2024-05-23 19:02:55 -04:00
return (access(get_wireless_connect_config_filename(), F_OK) == 0);
}
void vanilla_stop()
{
force_interrupt();
}
2024-05-20 23:25:55 -04:00
void vanilla_set_button(int button, int16_t value)
{
2024-05-20 23:25:55 -04:00
set_button_state(button, value);
}
2024-05-20 19:17:24 -04:00
void vanilla_set_touch(int x, int y)
{
set_touch_state(x, y);
}
void default_logger(const char *format, va_list args)
{
vprintf(format, args);
}
void (*custom_logger)(const char *, va_list) = default_logger;
void vanilla_log(const char *format, ...)
{
va_list va;
va_start(va, format);
if (custom_logger) {
custom_logger(format, va);
custom_logger("\n", NULL);
}
va_end(va);
}
void vanilla_log_no_newline(const char *format, ...)
{
va_list va;
va_start(va, format);
if (custom_logger) {
custom_logger(format, va);
}
va_end(va);
}
void vanilla_install_logger(void (*logger)(const char *, va_list))
{
custom_logger = logger;
2024-05-20 19:17:24 -04:00
}