2024-05-15 15:44:45 -04:00
|
|
|
#include "vanilla.h"
|
|
|
|
|
2024-08-12 23:22:51 -04:00
|
|
|
#include <pthread.h>
|
2024-05-15 15:44:45 -04:00
|
|
|
#include <signal.h>
|
2024-06-13 21:43:58 -04:00
|
|
|
#include <string.h>
|
2024-05-15 15:44:45 -04:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2024-06-19 14:58:02 -04:00
|
|
|
#include "gamepad/command.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"
|
2024-06-13 21:43:58 -04:00
|
|
|
#include "gamepad/video.h"
|
2024-05-15 15:44:45 -04:00
|
|
|
#include "status.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "vanilla.h"
|
|
|
|
|
2024-08-12 23:22:51 -04:00
|
|
|
pthread_mutex_t main_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
2024-10-14 03:52:57 -04:00
|
|
|
struct gamepad_data_t
|
|
|
|
{
|
|
|
|
vanilla_event_handler_t event_handler;
|
|
|
|
void *context;
|
|
|
|
uint32_t server_address;
|
|
|
|
};
|
|
|
|
|
|
|
|
void *start_gamepad(void *arg)
|
|
|
|
{
|
|
|
|
struct gamepad_data_t *data = (struct gamepad_data_t *) arg;
|
|
|
|
connect_as_gamepad_internal(data->event_handler, data->context, data->server_address);
|
|
|
|
free(data);
|
|
|
|
pthread_mutex_unlock(&main_mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int vanilla_start_internal(vanilla_event_handler_t event_handler, void *context, uint32_t server_address)
|
2024-05-15 15:44:45 -04:00
|
|
|
{
|
2024-08-12 23:22:51 -04:00
|
|
|
if (pthread_mutex_trylock(&main_mutex) == 0) {
|
2024-10-14 03:52:57 -04:00
|
|
|
pthread_t other;
|
|
|
|
|
|
|
|
struct gamepad_data_t *data = malloc(sizeof(struct gamepad_data_t));
|
|
|
|
data->event_handler = event_handler;
|
|
|
|
data->context = context;
|
|
|
|
data->server_address = server_address;
|
|
|
|
|
|
|
|
pthread_create(&other, NULL, start_gamepad, data);
|
|
|
|
return VANILLA_SUCCESS;
|
2024-08-12 23:22:51 -04:00
|
|
|
} else {
|
|
|
|
return VANILLA_ERROR;
|
|
|
|
}
|
2024-08-06 06:11:25 -04:00
|
|
|
}
|
|
|
|
|
2024-10-14 03:52:57 -04:00
|
|
|
int vanilla_start(vanilla_event_handler_t event_handler, void *context)
|
|
|
|
{
|
|
|
|
return vanilla_start_internal(event_handler, context, 0);
|
|
|
|
}
|
|
|
|
|
2024-08-06 06:11:25 -04:00
|
|
|
int vanilla_start_udp(vanilla_event_handler_t event_handler, void *context, uint32_t server_address)
|
|
|
|
{
|
2024-10-14 03:52:57 -04:00
|
|
|
return vanilla_start_internal(event_handler, context, server_address);
|
2024-05-15 15:44:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void vanilla_stop()
|
|
|
|
{
|
2024-08-12 23:22:51 -04:00
|
|
|
// Signal to all other threads to exit gracefully
|
2024-05-15 15:44:45 -04:00
|
|
|
force_interrupt();
|
2024-08-12 23:22:51 -04:00
|
|
|
|
|
|
|
// Block until most recent vanilla_start finishes
|
|
|
|
pthread_mutex_lock(&main_mutex);
|
|
|
|
pthread_mutex_unlock(&main_mutex);
|
2024-05-15 15:44:45 -04:00
|
|
|
}
|
|
|
|
|
2024-06-13 21:38:24 -04:00
|
|
|
void vanilla_set_button(int button, int32_t value)
|
2024-05-15 15:44:45 -04:00
|
|
|
{
|
2024-05-20 23:25:55 -04:00
|
|
|
set_button_state(button, value);
|
2024-05-15 15:44:45 -04:00
|
|
|
}
|
2024-05-20 19:17:24 -04:00
|
|
|
|
|
|
|
void vanilla_set_touch(int x, int y)
|
|
|
|
{
|
|
|
|
set_touch_state(x, y);
|
2024-05-24 11:37:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2024-05-29 03:09:17 -04:00
|
|
|
custom_logger("\n", va);
|
2024-05-24 11:37:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
|
|
|
void vanilla_log_no_newline(const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
va_start(va, format);
|
|
|
|
|
2024-05-24 13:04:08 -04:00
|
|
|
vanilla_log_no_newline_va(format, va);
|
2024-05-24 11:37:54 -04:00
|
|
|
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
2024-05-24 13:04:08 -04:00
|
|
|
void vanilla_log_no_newline_va(const char *format, va_list args)
|
|
|
|
{
|
|
|
|
if (custom_logger) {
|
|
|
|
custom_logger(format, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-24 11:37:54 -04:00
|
|
|
void vanilla_install_logger(void (*logger)(const char *, va_list))
|
|
|
|
{
|
|
|
|
custom_logger = logger;
|
2024-06-13 21:43:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void vanilla_request_idr()
|
|
|
|
{
|
|
|
|
request_idr();
|
|
|
|
}
|
|
|
|
|
2024-06-19 14:58:02 -04:00
|
|
|
void vanilla_set_region(int region)
|
|
|
|
{
|
|
|
|
set_region(region);
|
2024-06-19 16:11:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void vanilla_set_battery_status(int battery_status)
|
|
|
|
{
|
|
|
|
set_battery_status(battery_status);
|
2024-09-18 14:57:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int vanilla_sync(uint16_t code, uint32_t server_address)
|
|
|
|
{
|
|
|
|
return sync_internal(code, server_address);
|
2024-05-20 19:17:24 -04:00
|
|
|
}
|