OXYGENHUB
# 398756c0e9b2 retro-remote-debug-controller / core / retro_control.h
6.3 KiB · Objective-C · last changed 0.4.0: pointer injection (POST/GET /pointer) 3 months ago source blame history raw
1
/* retro_control.h — portable emulator control server (see ../SPEC.md).
2
 *
3
 * A retro-platform emulator implements the small read-only backend below and
4
 * calls retro_control_start(). The shared server (retro_control.c) owns the
5
 * socket loop, HTTP parsing, PPM/JSON encoding, and control flow
6
 * (pause / step / free-run). It always binds 127.0.0.1. Contract version is
7
 * RETRO_CONTROL_CONTRACT (below); the server advertises the highest level whose
8
 * backend callbacks are all present.
9
 *
10
 * Integration is three calls in the emulator's main loop:
11
 *   retro_control_service();                 // once per iteration (cheap)
12
 *   if (!retro_control_running()) { idle; }  // gate advancing the machine
13
 *   ... run one instruction / frame ...
14
 *   if (frame_completed) retro_control_on_frame();
15
 *
16
 * Threading: the server runs on its own thread but never touches machine state
17
 * there. State-reading endpoints (/mem, /regs, /screenshot) are executed inside
18
 * retro_control_service() ON THE EMULATOR THREAD, so every read is consistent.
19
 * The backend callbacks are therefore called from the emulator thread (except
20
 * get_frame_count, which the server may also read for /status — make it a plain
21
 * atomic-ish counter read).
22
 */
23
#ifndef RETRO_CONTROL_H
24
#define RETRO_CONTROL_H
25
26
#include <stdint.h>
27
#include <stddef.h>
28
29
/* Highest contract the core implements. The server advertises the highest level
30
 * whose backend callbacks are all present: 0.4.0 needs set_pointer + get_pointer
31
 * (on top of 0.3); 0.3.0 needs inject_key + write_mem + capture_audio; 0.2.0
32
 * needs inject_key; else 0.1.0. Keeps a partial backend honest. */
33
#define RETRO_CONTROL_CONTRACT "0.4.0"
34
35
/* Native framebuffer pixel layout; the server swizzles to PPM RGB. */
36
typedef enum {
37
    RETRO_PIX_RGB888 = 0,   /* 3 bytes/px: R,G,B                       */
38
    RETRO_PIX_RGBA8888,     /* 4 bytes/px: R,G,B,A                     */
39
    RETRO_PIX_BGRA8888      /* 4 bytes/px: B,G,R,A (x16 framebuffer)   */
40
} retro_pix_fmt_t;
41
42
typedef struct {
43
    const uint8_t *pixels;  /* start of the current complete frame     */
44
    int width;
45
    int height;
46
    retro_pix_fmt_t fmt;
47
} retro_framebuffer_t;
48
49
/* Key action for inject_key (0.2). */
50
typedef enum {
51
    RETRO_KEY_TAP  = 0,   /* press then release */
52
    RETRO_KEY_DOWN = 1,   /* press / hold       */
53
    RETRO_KEY_UP   = 2    /* release            */
54
} retro_key_action_t;
55
56
/* The emulator implements these. Any may be NULL: the matching endpoint then
57
 * returns 501 Not Implemented. */
58
typedef struct retro_control_backend {
59
    const char *platform;   /* "x16" | "neo6502" | "agon" | ...        */
60
    const char *emulator;   /* human name, e.g. "x16emu"               */
61
62
    /* Debug-read up to min(len, out_cap) bytes at addr into out, side-effect
63
     * free. bank is the optional bank selector (<0 = current bank). Returns the
64
     * number of bytes written. */
65
    uint32_t (*read_mem)(uint32_t addr, int32_t bank, uint32_t len,
66
                         uint8_t *out, uint32_t out_cap);
67
68
    /* Write a JSON object of registers into buf (NUL-terminated). */
69
    void (*get_regs_json)(char *buf, size_t cap);
70
71
    /* Report the current complete-frame framebuffer. */
72
    void (*get_framebuffer)(retro_framebuffer_t *out);
73
74
    /* Monotonic completed-frame counter. */
75
    uint64_t (*get_frame_count)(void);
76
77
    /* 0.2: inject a key. is_text=1 => `value` is a character code point;
78
     * is_text=0 => `value` is a raw platform key code. `action` is a
79
     * retro_key_action_t. Return 1 on success, 0 if unmappable (server -> 400).
80
     * NULL => /key returns 501 and the server advertises contract 0.1.0. */
81
    int (*inject_key)(int is_text, uint32_t value, int action);
82
83
    /* 0.2: soft/cold reset the machine. NULL => /reset returns 501. */
84
    void (*reset)(void);
85
86
    /* 0.3: write len bytes from `in` at addr (debug/state poke; bank<0 =
87
     * current). Intended for RAM/state — I/O-register writes may trigger device
88
     * side effects. Return bytes written. NULL => POST /mem returns 501. */
89
    uint32_t (*write_mem)(uint32_t addr, int32_t bank, uint32_t len,
90
                          const uint8_t *in);
91
92
    /* 0.3: drain up to `cap` interleaved signed-16-bit audio samples the
93
     * emulator has synthesised since the last call into `out`; set *rate and
94
     * *channels; set *dropped to samples lost to overflow since the last drain.
95
     * Return the number of int16 samples written. NULL => /audio returns 501. */
96
    uint32_t (*capture_audio)(int16_t *out, uint32_t cap,
97
                              int *rate, int *channels, uint32_t *dropped);
98
99
    /* 0.4: inject a pointer (mouse) move/click through the platform's native
100
     * pointer path. `absolute` nonzero => (x,y) are absolute pointer
101
     * coordinates (framebuffer/pointer space); zero => (x,y) are relative
102
     * deltas applied to the current position. `buttons` is a bitmask
103
     * (bit0 = primary, bit1 = secondary), or -1 to leave the button state
104
     * unchanged (a pure move). Return 1 on success, 0 if the platform has no
105
     * pointer (server -> 400). NULL => POST /pointer returns 501. */
106
    int (*set_pointer)(int absolute, int32_t x, int32_t y, int buttons);
107
108
    /* 0.4: report the current pointer position and button bitmask (same
109
     * semantics as set_pointer's `buttons`). Return 1 on success (values
110
     * written), 0 if no pointer is available. NULL => GET /pointer returns 501. */
111
    int (*get_pointer)(int32_t *x, int32_t *y, int *buttons);
112
} retro_control_backend_t;
113
114
/* Start the server thread bound to 127.0.0.1:port. Non-blocking; 0 on success,
115
 * negative on error. backend must outlive the server. */
116
int  retro_control_start(int port, const retro_control_backend_t *backend);
117
118
/* Drain a pending control request. Call once per main-loop iteration, at an
119
 * instruction/frame boundary. Cheap no-op when idle and when control is off. */
120
void retro_control_service(void);
121
122
/* Nonzero => the machine should advance this iteration. Returns 1 always when
123
 * control is off or the machine is free-running; 0 when paused with no steps
124
 * left. */
125
int  retro_control_running(void);
126
127
/* Call when a video frame completes. Drives /step (decrements the step budget,
128
 * halts and answers when it reaches zero). No-op when control is off. */
129
void retro_control_on_frame(void);
130
131
/* Stop the server (optional; process exit is fine too). */
132
void retro_control_stop(void);
133
134
#endif /* RETRO_CONTROL_H */