| 961b3c61 Andy McCall |
1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "retro_control.h" |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
|
| 14 |
#if defined(_WIN32) |
| 15 |
|
| 16 |
int retro_control_start(int port, const retro_control_backend_t *b) { (void)port; (void)b; return -1; } |
| 17 |
void retro_control_service(void) {} |
| 18 |
int retro_control_running(void) { return 1; } |
| 19 |
void retro_control_on_frame(void) {} |
| 20 |
void retro_control_stop(void) {} |
| 21 |
#else |
| 22 |
|
| 23 |
#include <pthread.h> |
| e60badb6 Andy McCall ↶ |
24 |
#include <stdatomic.h> |
| 961b3c61 Andy McCall |
25 |
#include <unistd.h> |
| 26 |
#include <errno.h> |
| 27 |
#include <signal.h> |
| 28 |
#include <sys/socket.h> |
| 29 |
#include <netinet/in.h> |
| 30 |
#include <arpa/inet.h> |
| 31 |
|
| 32 |
#ifndef MSG_NOSIGNAL |
| 33 |
#define MSG_NOSIGNAL 0 |
| 34 |
#endif |
| 35 |
|
| 36 |
#define MEM_CAP (1u << 20) |
| 37 |
|
| 38 |
static const retro_control_backend_t *g_be = NULL; |
| 39 |
static int g_listen_fd = -1; |
| 40 |
static pthread_t g_thread; |
| 41 |
|
| 42 |
static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; |
| 43 |
static pthread_cond_t g_cv = PTHREAD_COND_INITIALIZER; |
| 44 |
|
| e60badb6 Andy McCall ↶ |
45 |
|
| 46 |
|
| 47 |
|
| 48 |
static _Atomic long g_frames_to_run = -1; |
| 961b3c61 Andy McCall |
49 |
static int g_step_waiting = 0; |
| 50 |
|
| 51 |
|
| 52 |
typedef enum { REQ_NONE = 0, REQ_MEM, REQ_REGS, REQ_SHOT } req_t; |
| 53 |
static volatile req_t g_req = REQ_NONE; |
| 54 |
static uint32_t g_req_addr, g_req_len; |
| 55 |
static int32_t g_req_bank; |
| 56 |
|
| 57 |
static uint8_t *g_resp_body = NULL; |
| 58 |
static size_t g_resp_len = 0; |
| 59 |
static int g_resp_status = 200; |
| 60 |
static const char *g_resp_ctype = "application/octet-stream"; |
| 61 |
static int g_resp_ready = 0; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
static void build_ppm(void) |
| 66 |
{ |
| 67 |
retro_framebuffer_t fb; |
| 68 |
memset(&fb, 0, sizeof fb); |
| 69 |
g_be->get_framebuffer(&fb); |
| 70 |
int w = fb.width, h = fb.height; |
| 71 |
int bpp = (fb.fmt == RETRO_PIX_RGB888) ? 3 : 4; |
| 72 |
|
| 73 |
char hdr[64]; |
| 74 |
int hl = snprintf(hdr, sizeof hdr, "P6\n%d %d\n255\n", w, h); |
| 75 |
size_t total = (size_t)hl + (size_t)3 * w * h; |
| 76 |
uint8_t *out = (uint8_t *)malloc(total ? total : 1); |
| 77 |
if (!out) { g_resp_body = NULL; g_resp_len = 0; g_resp_status = 500; return; } |
| 78 |
|
| 79 |
memcpy(out, hdr, hl); |
| 80 |
uint8_t *p = out + hl; |
| 81 |
const uint8_t *s = fb.pixels; |
| 82 |
for (int i = 0; i < w * h; i++) { |
| 83 |
const uint8_t *px = s + (size_t)i * bpp; |
| 84 |
if (fb.fmt == RETRO_PIX_BGRA8888) { |
| 85 |
p[0] = px[2]; p[1] = px[1]; p[2] = px[0]; |
| 86 |
} else { |
| 87 |
p[0] = px[0]; p[1] = px[1]; p[2] = px[2]; |
| 88 |
} |
| 89 |
p += 3; |
| 90 |
} |
| 91 |
g_resp_body = out; |
| 92 |
g_resp_len = total; |
| 93 |
g_resp_status = 200; |
| 94 |
g_resp_ctype = "image/x-portable-pixmap"; |
| 95 |
} |
| 96 |
|
| 97 |
void retro_control_service(void) |
| 98 |
{ |
| 99 |
if (!g_be || g_req == REQ_NONE) return; |
| 100 |
|
| 101 |
pthread_mutex_lock(&g_lock); |
| 102 |
switch (g_req) { |
| 103 |
case REQ_MEM: { |
| 104 |
uint32_t len = g_req_len > MEM_CAP ? MEM_CAP : g_req_len; |
| 105 |
uint8_t *buf = (uint8_t *)malloc(len ? len : 1); |
| 106 |
uint32_t got = 0; |
| 107 |
if (buf && g_be->read_mem) got = g_be->read_mem(g_req_addr, g_req_bank, len, buf, len); |
| 108 |
g_resp_body = buf; g_resp_len = got; |
| 109 |
g_resp_status = g_be->read_mem ? 200 : 501; |
| 110 |
g_resp_ctype = "application/octet-stream"; |
| 111 |
break; |
| 112 |
} |
| 113 |
case REQ_REGS: { |
| 114 |
char *buf = (char *)malloc(1024); |
| 115 |
if (buf && g_be->get_regs_json) { g_be->get_regs_json(buf, 1024); } |
| 116 |
else if (buf) { buf[0] = 0; } |
| 117 |
g_resp_body = (uint8_t *)buf; g_resp_len = buf ? strlen(buf) : 0; |
| 118 |
g_resp_status = g_be->get_regs_json ? 200 : 501; |
| 119 |
g_resp_ctype = "application/json"; |
| 120 |
break; |
| 121 |
} |
| 122 |
case REQ_SHOT: |
| 123 |
if (g_be->get_framebuffer) build_ppm(); |
| 124 |
else { g_resp_body = NULL; g_resp_len = 0; g_resp_status = 501; } |
| 125 |
break; |
| 126 |
default: break; |
| 127 |
} |
| 128 |
g_req = REQ_NONE; |
| 129 |
g_resp_ready = 1; |
| 130 |
pthread_cond_broadcast(&g_cv); |
| 131 |
pthread_mutex_unlock(&g_lock); |
| 132 |
} |
| 133 |
|
| 134 |
int retro_control_running(void) |
| 135 |
{ |
| 136 |
if (!g_be) return 1; |
| e60badb6 Andy McCall ↶ |
137 |
|
| 138 |
|
| 139 |
|
| 140 |
return atomic_load_explicit(&g_frames_to_run, memory_order_relaxed) != 0; |
| 961b3c61 Andy McCall |
141 |
} |
| 142 |
|
| 143 |
void retro_control_on_frame(void) |
| 144 |
{ |
| 145 |
if (!g_be) return; |
| 146 |
pthread_mutex_lock(&g_lock); |
| 147 |
if (g_frames_to_run > 0) { |
| 148 |
g_frames_to_run--; |
| 149 |
if (g_frames_to_run == 0 && g_step_waiting) { |
| 150 |
g_step_waiting = 0; |
| 151 |
g_resp_ready = 1; |
| 152 |
pthread_cond_broadcast(&g_cv); |
| 153 |
} |
| 154 |
} |
| 155 |
pthread_mutex_unlock(&g_lock); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
static void write_all(int fd, const void *buf, size_t len) |
| 161 |
{ |
| 162 |
const char *p = (const char *)buf; |
| 163 |
while (len) { |
| 164 |
ssize_t w = send(fd, p, len, MSG_NOSIGNAL); |
| 165 |
if (w <= 0) return; |
| 166 |
p += w; len -= (size_t)w; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
static void send_http(int fd, int status, const char *ctype, |
| 171 |
const uint8_t *body, size_t len) |
| 172 |
{ |
| 173 |
const char *reason = |
| 174 |
status == 200 ? "OK" : |
| 175 |
status == 400 ? "Bad Request" : |
| 176 |
status == 404 ? "Not Found" : |
| 177 |
status == 405 ? "Method Not Allowed" : |
| 178 |
status == 501 ? "Not Implemented" : "Error"; |
| 179 |
char hdr[256]; |
| 180 |
int hl = snprintf(hdr, sizeof hdr, |
| 181 |
"HTTP/1.1 %d %s\r\nContent-Type: %s\r\nContent-Length: %zu\r\n" |
| 182 |
"Connection: close\r\nAccess-Control-Allow-Origin: *\r\n\r\n", |
| 183 |
status, reason, ctype, len); |
| 184 |
write_all(fd, hdr, hl); |
| 185 |
if (body && len) write_all(fd, body, len); |
| 186 |
} |
| 187 |
|
| 188 |
static void send_json(int fd, int status, const char *json) |
| 189 |
{ |
| 190 |
send_http(fd, status, "application/json", (const uint8_t *)json, strlen(json)); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
static long query_long(const char *q, const char *key, long def) |
| 195 |
{ |
| 196 |
size_t kl = strlen(key); |
| 197 |
const char *p = q; |
| 198 |
while (p && *p) { |
| 199 |
if (!strncmp(p, key, kl) && p[kl] == '=') |
| 200 |
return strtol(p + kl + 1, NULL, 0); |
| 201 |
p = strchr(p, '&'); |
| 202 |
if (p) p++; |
| 203 |
} |
| 204 |
return def; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
static void do_marshalled(int fd, req_t r) |
| 209 |
{ |
| 210 |
pthread_mutex_lock(&g_lock); |
| 211 |
g_resp_ready = 0; g_resp_body = NULL; g_resp_len = 0; |
| 212 |
g_req = r; |
| 213 |
while (!g_resp_ready) pthread_cond_wait(&g_cv, &g_lock); |
| 214 |
uint8_t *body = g_resp_body; size_t len = g_resp_len; |
| 215 |
int st = g_resp_status; const char *ct = g_resp_ctype; |
| 216 |
g_resp_body = NULL; |
| 217 |
pthread_mutex_unlock(&g_lock); |
| 218 |
|
| 219 |
send_http(fd, st, ct, body, len); |
| 220 |
free(body); |
| 221 |
} |
| 222 |
|
| 223 |
static void do_status(int fd) |
| 224 |
{ |
| 225 |
pthread_mutex_lock(&g_lock); |
| 226 |
long f = g_frames_to_run; |
| 227 |
pthread_mutex_unlock(&g_lock); |
| 228 |
unsigned long long fc = g_be->get_frame_count ? g_be->get_frame_count() : 0; |
| 229 |
char buf[320]; |
| 230 |
snprintf(buf, sizeof buf, |
| 231 |
"{\"contract\":\"%s\",\"emulator\":\"%s\",\"platform\":\"%s\"," |
| 232 |
"\"frame\":%llu,\"paused\":%s,\"running\":true}", |
| 233 |
RETRO_CONTROL_CONTRACT, |
| 234 |
g_be->emulator ? g_be->emulator : "", |
| 235 |
g_be->platform ? g_be->platform : "", |
| 236 |
fc, (f == 0) ? "true" : "false"); |
| 237 |
send_json(fd, 200, buf); |
| 238 |
} |
| 239 |
|
| 240 |
static void do_step(int fd, const char *query) |
| 241 |
{ |
| 242 |
long n = query_long(query, "frames", 1); |
| 243 |
if (n < 1) n = 1; |
| 244 |
pthread_mutex_lock(&g_lock); |
| 245 |
g_frames_to_run = n; |
| 246 |
g_step_waiting = 1; |
| 247 |
g_resp_ready = 0; |
| 248 |
while (!g_resp_ready) pthread_cond_wait(&g_cv, &g_lock); |
| 249 |
pthread_mutex_unlock(&g_lock); |
| 250 |
|
| 251 |
unsigned long long fc = g_be->get_frame_count ? g_be->get_frame_count() : 0; |
| 252 |
char buf[64]; |
| 253 |
snprintf(buf, sizeof buf, "{\"frame\":%llu}", fc); |
| 254 |
send_json(fd, 200, buf); |
| 255 |
} |
| 256 |
|
| 257 |
static void do_setrun(int fd, long v, int paused) |
| 258 |
{ |
| 259 |
pthread_mutex_lock(&g_lock); |
| 260 |
g_frames_to_run = v; |
| 261 |
pthread_mutex_unlock(&g_lock); |
| 262 |
send_json(fd, 200, paused ? "{\"paused\":true}" : "{\"paused\":false}"); |
| 263 |
} |
| 264 |
|
| 265 |
static void handle_conn(int fd) |
| 266 |
{ |
| 267 |
char req[2048]; |
| 268 |
size_t n = 0; |
| 269 |
while (n < sizeof(req) - 1) { |
| 270 |
ssize_t r = recv(fd, req + n, sizeof(req) - 1 - n, 0); |
| 271 |
if (r <= 0) break; |
| 272 |
n += (size_t)r; |
| 273 |
if (strstr(req, "\r\n\r\n")) break; |
| 274 |
} |
| 275 |
req[n] = 0; |
| 276 |
|
| 277 |
char method[8] = {0}, target[1024] = {0}; |
| 278 |
if (sscanf(req, "%7s %1023s", method, target) != 2) { |
| 279 |
send_json(fd, 400, "{\"error\":\"bad request\"}"); |
| 280 |
return; |
| 281 |
} |
| 282 |
char *q = strchr(target, '?'); |
| 283 |
const char *query = ""; |
| 284 |
if (q) { *q = 0; query = q + 1; } |
| 285 |
int is_post = !strcmp(method, "POST"); |
| 286 |
|
| 287 |
if (!strcmp(target, "/status")) { do_status(fd); return; } |
| 288 |
if (!strcmp(target, "/regs")) { do_marshalled(fd, REQ_REGS); return; } |
| 289 |
if (!strcmp(target, "/screenshot")) { do_marshalled(fd, REQ_SHOT); return; } |
| 290 |
if (!strcmp(target, "/mem")) { |
| 291 |
g_req_addr = (uint32_t)query_long(query, "addr", 0); |
| 292 |
g_req_len = (uint32_t)query_long(query, "len", 0); |
| 293 |
g_req_bank = (int32_t)query_long(query, "bank", -1); |
| 294 |
do_marshalled(fd, REQ_MEM); |
| 295 |
return; |
| 296 |
} |
| 297 |
if (!strcmp(target, "/step")) { |
| 298 |
if (!is_post) { send_json(fd, 405, "{\"error\":\"POST only\"}"); return; } |
| 299 |
do_step(fd, query); return; |
| 300 |
} |
| 301 |
if (!strcmp(target, "/pause")) { |
| 302 |
if (!is_post) { send_json(fd, 405, "{\"error\":\"POST only\"}"); return; } |
| 303 |
do_setrun(fd, 0, 1); return; |
| 304 |
} |
| 305 |
if (!strcmp(target, "/resume")) { |
| 306 |
if (!is_post) { send_json(fd, 405, "{\"error\":\"POST only\"}"); return; } |
| 307 |
do_setrun(fd, -1, 0); return; |
| 308 |
} |
| 309 |
send_json(fd, 404, "{\"error\":\"not found\"}"); |
| 310 |
} |
| 311 |
|
| 312 |
static void *server_main(void *arg) |
| 313 |
{ |
| 314 |
(void)arg; |
| 315 |
for (;;) { |
| 316 |
int fd = accept(g_listen_fd, NULL, NULL); |
| 317 |
if (fd < 0) { |
| 318 |
if (errno == EINTR) continue; |
| 319 |
break; |
| 320 |
} |
| 321 |
handle_conn(fd); |
| 322 |
close(fd); |
| 323 |
} |
| 324 |
return NULL; |
| 325 |
} |
| 326 |
|
| 327 |
int retro_control_start(int port, const retro_control_backend_t *backend) |
| 328 |
{ |
| 329 |
g_be = backend; |
| 330 |
signal(SIGPIPE, SIG_IGN); |
| 331 |
|
| 332 |
int fd = socket(AF_INET, SOCK_STREAM, 0); |
| 333 |
if (fd < 0) return -1; |
| 334 |
int one = 1; |
| 335 |
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one); |
| 336 |
|
| 337 |
struct sockaddr_in addr; |
| 338 |
memset(&addr, 0, sizeof addr); |
| 339 |
addr.sin_family = AF_INET; |
| 340 |
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 341 |
addr.sin_port = htons((uint16_t)port); |
| 342 |
if (bind(fd, (struct sockaddr *)&addr, sizeof addr) < 0) { |
| 343 |
fprintf(stderr, "[retro_control] bind 127.0.0.1:%d failed: %s\n", |
| 344 |
port, strerror(errno)); |
| 345 |
close(fd); |
| 346 |
return -1; |
| 347 |
} |
| 348 |
if (listen(fd, 4) < 0) { close(fd); return -1; } |
| 349 |
|
| 350 |
g_listen_fd = fd; |
| 351 |
if (pthread_create(&g_thread, NULL, server_main, NULL) != 0) { |
| 352 |
close(fd); g_listen_fd = -1; return -1; |
| 353 |
} |
| 354 |
pthread_detach(g_thread); |
| 355 |
fprintf(stderr, "[retro_control] listening on 127.0.0.1:%d (contract %s)\n", |
| 356 |
port, RETRO_CONTROL_CONTRACT); |
| 357 |
return 0; |
| 358 |
} |
| 359 |
|
| 360 |
void retro_control_stop(void) |
| 361 |
{ |
| 362 |
if (g_listen_fd >= 0) { close(g_listen_fd); g_listen_fd = -1; } |
| 363 |
} |
| 364 |
|
| 365 |
#endif |