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-10-14 06:09:20 -04:00
|
|
|
#include <stdlib.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 06:09:20 -04:00
|
|
|
pthread_mutex_t gamepad_mutex = PTHREAD_MUTEX_INITIALIZER;
|
2024-10-14 15:04:22 -04:00
|
|
|
event_loop_t event_loop = {{0}, 0, 0, 0, PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER};
|
2024-08-12 23:22:51 -04:00
|
|
|
|
2024-10-14 03:52:57 -04:00
|
|
|
struct gamepad_data_t
|
|
|
|
{
|
|
|
|
uint32_t server_address;
|
|
|
|
};
|
|
|
|
|
|
|
|
void *start_gamepad(void *arg)
|
|
|
|
{
|
|
|
|
struct gamepad_data_t *data = (struct gamepad_data_t *) arg;
|
2024-10-14 15:04:22 -04:00
|
|
|
|
|
|
|
pthread_mutex_lock(&event_loop.mutex);
|
|
|
|
event_loop.active = 1;
|
2024-10-14 06:09:20 -04:00
|
|
|
event_loop.new_index = 0;
|
|
|
|
event_loop.used_index = 0;
|
2024-10-14 15:04:22 -04:00
|
|
|
pthread_cond_broadcast(&event_loop.waitcond);
|
|
|
|
pthread_mutex_unlock(&event_loop.mutex);
|
2024-10-14 06:09:20 -04:00
|
|
|
|
|
|
|
connect_as_gamepad_internal(&event_loop, data->server_address);
|
|
|
|
|
2024-10-14 03:52:57 -04:00
|
|
|
free(data);
|
2024-10-14 15:04:22 -04:00
|
|
|
|
|
|
|
pthread_mutex_lock(&event_loop.mutex);
|
|
|
|
event_loop.active = 0;
|
|
|
|
pthread_cond_broadcast(&event_loop.waitcond);
|
|
|
|
pthread_mutex_unlock(&event_loop.mutex);
|
|
|
|
|
2024-10-14 03:52:57 -04:00
|
|
|
pthread_mutex_unlock(&main_mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-10-14 06:09:20 -04:00
|
|
|
int vanilla_start_internal(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->server_address = server_address;
|
|
|
|
|
2024-10-14 15:04:22 -04:00
|
|
|
// Lock event loop mutex so it can't be set to active until we're ready
|
|
|
|
pthread_mutex_lock(&event_loop.mutex);
|
|
|
|
|
|
|
|
// Start other thread (which will set event loop to active)
|
2024-10-14 03:52:57 -04:00
|
|
|
pthread_create(&other, NULL, start_gamepad, data);
|
2024-10-27 19:30:42 -04:00
|
|
|
name_thread(other, "vanilla-gamepad");
|
2024-10-14 15:04:22 -04:00
|
|
|
|
|
|
|
// Wait for event loop to be set active before returning
|
|
|
|
while (!event_loop.active) {
|
|
|
|
pthread_cond_wait(&event_loop.waitcond, &event_loop.mutex);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&event_loop.mutex);
|
|
|
|
|
2024-10-14 03:52:57 -04:00
|
|
|
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 06:09:20 -04:00
|
|
|
int vanilla_start(uint32_t server_address)
|
2024-10-14 03:52:57 -04:00
|
|
|
{
|
2024-10-14 06:09:20 -04:00
|
|
|
return vanilla_start_internal(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-10-14 06:09:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int vanilla_poll_event(vanilla_event_t *event)
|
|
|
|
{
|
2024-10-14 15:04:22 -04:00
|
|
|
return get_event(&event_loop, event, 0);
|
2024-10-14 06:09:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int vanilla_wait_event(vanilla_event_t *event)
|
|
|
|
{
|
2024-10-14 15:04:22 -04:00
|
|
|
return get_event(&event_loop, event, 1);
|
2024-05-20 19:17:24 -04:00
|
|
|
}
|