Something went wrong. Try again.
Monorepo for Aesthetic.Computer aesthetic.computer
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370#pragma once
#include <cstdint>#include <functional>#include <memory>#include <optional>#include <string>#include <string_view>#include <unordered_set>#include <vector>
namespace ac::xbox {
// Keep these spellings aligned with the browser/ac-native piece API. The// Xbox host translates Windows.Gaming.Input readings into these event names.struct Event { std::string name; float value = 1.0f; std::uint64_t timestamp_us = 0; [[nodiscard]] bool is(std::string_view candidate) const noexcept { return name == candidate; }};
struct Screen { int width = 1920; int height = 1080; float scale = 1.0f; };struct ControllerInfo { std::string id; std::string name; std::uint16_t vendor_id = 0; std::uint16_t product_id = 0; std::uint16_t axes = 0; std::uint16_t buttons = 0; std::uint16_t switches = 0; bool gamepad = false;};struct PadState { float left_x = 0, left_y = 0, right_x = 0, right_y = 0; float left_trigger = 0, right_trigger = 0; std::unordered_set<std::string> down; bool connected = false; [[nodiscard]] bool pressed(std::string_view button) const { return down.find(std::string(button)) != down.end(); }};struct GamepadState : PadState { std::vector<PadState> pads; std::vector<ControllerInfo> controllers;};
struct Color { std::uint8_t r, g, b, a = 255; };struct Rect { float x, y, width, height; Color color; };struct Line { float x1, y1, x2, y2, width; Color color; };struct Triangle { float x1, y1, z1, x2, y2, z2, x3, y3, z3; Color color; };struct TexturedTriangle { float x1, y1, z1, u1, v1; float x2, y2, z2, u2, v2; float x3, y3, z3, u3, v3; Color color;};struct Sprite { float x, y, z, size; Color color; std::uint8_t frame = 0; };struct Text { std::string value; float x, y, size; Color color; };struct SystemText { std::string value; std::string family = "Segoe UI"; float x = 0, y = 0, size = 32; Color color{255, 255, 255, 255};};struct SystemGlyph { std::string name; float x = 0, y = 0, size = 64; Color color{255, 255, 255, 255};};struct ImageDraw { std::string source = "latest-painting"; float x = 0, y = 0, width = 0, height = 0; float scale = 1; bool centered = false;};
class Graphics { public: virtual ~Graphics() = default; virtual void wipe(Color color) = 0; virtual void box(const Rect&) = 0; virtual void line(const Line&) = 0; virtual void triangle(const Triangle&) {} virtual void textured_triangle(const TexturedTriangle&) {} virtual void sprite(const Sprite&) {} virtual void write(const Text&) = 0; virtual void system_write(const SystemText&) {} virtual void system_glyph(const SystemGlyph&) {} virtual void image(const ImageDraw&) {} virtual void blur(unsigned) {}};
struct SynthVoice { float frequency_hz = 440; float duration_s = .1f; float volume = .25f; float attack_s = .001f; std::string wave = "sine";};
class Sound { public: virtual ~Sound() = default; // Implementations enqueue directly to the preallocated XAudio2 voice. This // mirrors api.sound.synth({...}) without waiting for the render frame. virtual void synth(const SynthVoice&) = 0; virtual void stop_all() = 0; virtual int sample_rate() const = 0; virtual void oscillator(float, float) {} virtual void oscillator_stop() {} virtual void drum(std::string_view, float = 1.0f, float = 0.0f) {}};
struct Clock { std::uint64_t monotonic_us = 0; std::int64_t unix_ms = 0; double seconds = 0; bool network_synced = false; std::int64_t network_offset_ms = 0; std::uint32_t network_rtt_ms = 0; std::uint64_t network_sync_age_ms = 0;};
struct AudioStats { double output_latency_ms = 0; double submit_us = 0; double input_to_submit_us = 0; std::uint32_t glitches = 0; std::uint32_t midi_inputs = 0; std::uint64_t midi_events = 0; int midi_note = -1; int midi_velocity = 0; int midi_channel = 0; int midi_pitch_bend = 8192; int midi_control = -1; int midi_control_value = 0; bool midi_gate = false; std::string midi_status = "scanning";};
struct System { std::string platform = "xbox"; std::string version; std::string device_family; std::string device_family_version; std::string product_name; std::string handle; std::string network_level = "none"; std::string network_name; std::uint64_t memory_usage_bytes = 0; std::uint64_t memory_limit_bytes = 0; std::uint64_t expected_memory_limit_bytes = 0; std::uint32_t render_width = 1920; std::uint32_t render_height = 1080; double refresh_hz = 0; double frame_ms = 0; double render_cpu_ms = 0; double present_ms = 0; std::uint32_t antialiasing_samples = 1; std::string antialiasing_mode = "geometry-fxaa"; bool dark = true; bool online = false;};
struct AcSnapshot { struct FighterProfile { std::string handle; std::string mood; std::string last_chat; std::vector<Color> colors; }; std::string mood; std::string mood_handle; std::string clock_from; std::string clock_text; std::string painting_url; std::string painting_handle; std::string status = "loading"; std::int64_t refreshed_unix_ms = 0; std::vector<FighterProfile> fighters;};
// Read-only view of a host-mediated photo disc. Pieces can request a scan,// choose a discovered image, draw the decoded current image, or ask the host// to copy the allowlisted photos into app-local storage. They never receive a// path, StorageFile, raw sector handle, or general filesystem primitive.struct PhotoDiscSnapshot { std::string status = "idle"; std::string volume; std::string name; std::string copy_status = "idle"; std::size_t count = 0; std::size_t index = 0; std::size_t copied = 0; std::size_t copy_failed = 0; unsigned width = 0; unsigned height = 0; bool current_ready = false;};
struct PhotoDisc { std::shared_ptr<const PhotoDiscSnapshot> snapshot = std::make_shared<const PhotoDiscSnapshot>(); std::function<void()> scan = {}; std::function<void(std::int64_t)> show = {}; std::function<void()> copy = {};};
// The stable native lifecycle context. Names intentionally follow piece API// fields so portable engines need a thin adapter rather than a rewrite.struct Api { Screen screen; Clock clock; System system; GamepadState gamepad; Graphics& graphics; Sound& sound; AudioStats audio; std::uint64_t sim_count = 0; std::uint64_t paint_count = 0; double seconds = 0; // Host-polled, read-only snapshots from allowlisted aesthetic.computer // endpoints. Pieces never receive a general network primitive. std::shared_ptr<const AcSnapshot> ac = std::make_shared<const AcSnapshot>(); PhotoDisc disc; // Sandboxed pieces can emit structured diagnostic lines without receiving // filesystem, process, Device Portal, or arbitrary WinRT access. std::function<void(std::string_view)> telemetry = {}; // Read-only receipt for the most recent host-routed client error. The host // reports queued/posting/retrying/posted without exposing a network client. std::function<std::string()> client_error_report_status = {}; // A bounded, host-routed game signal. The host owns the destination and wire // protocol; pieces can only choose a short event name and numeric payload. std::function<void(std::string_view, int, float, float)> game_signal = {}; // A bounded, host-routed replay artifact. The host validates and uploads the // complete demo; pieces do not receive a URL, credential, or response body. std::function<void(std::string_view)> replay_save = {}; // Latest-only live match state. The native host owns the fixed session-server // destination; pieces receive no socket, URL, credential, or response data. std::function<void(std::string_view, std::string_view)> live_publish = {};};
class Piece { public: virtual ~Piece() = default; virtual void boot(Api&) {} virtual void sim(Api&) = 0; virtual void paint(Api&) = 0; virtual void act(Api&, const Event&) = 0; virtual void leave(Api&) {}};
// A downloaded piece is JavaScript source evaluated by an embedded engine. It// is data to the UWP package: no PE/DLL/native code is accepted by this API.// sha256 is supplied by the AC endpoint and verified by the platform host// before stage() is called.struct PieceBundle { std::string slug; std::string version; std::string source; std::string sha256;};
struct JsLimits { std::size_t max_source_bytes = 2 * 1024 * 1024; std::size_t max_heap_bytes = 32 * 1024 * 1024; std::uint64_t max_callback_us = 8'000;};
// Adapter seam for QuickJS (or another interpreter compiled into the app).// The engine exposes only the AC Api bindings; browser, filesystem, process,// WinRT and arbitrary network globals are intentionally absent.class JsPiece : public Piece { public: ~JsPiece() override = default; [[nodiscard]] virtual std::string_view slug() const noexcept = 0; [[nodiscard]] virtual std::string_view version() const noexcept = 0;};
class JsEngine { public: virtual ~JsEngine() = default; virtual std::unique_ptr<JsPiece> compile(const PieceBundle&, const JsLimits&, std::string& error) = 0;};
// Owns the active and last-known-good JS contexts. Reload is transactional:// compile and boot the candidate first, then swap at a frame boundary. If a// callback throws or exceeds its budget the previous generation is restored.class PieceSupervisor { public: explicit PieceSupervisor(JsEngine& engine, JsLimits limits = {}) : engine_(engine), limits_(limits) {}
bool stage(const PieceBundle& bundle, Api& api, std::string& error) { if (bundle.source.size() > limits_.max_source_bytes) { error = "piece source exceeds configured byte limit"; return false; } auto candidate = engine_.compile(bundle, limits_, error); if (!candidate) return false; try { candidate->boot(api); } catch (...) { error = "piece boot failed"; return false; } staged_ = std::move(candidate); return true; }
bool activate(Api& api) { if (!staged_) return false; if (active_) { active_->leave(api); fallback_ = std::move(active_); } active_ = std::move(staged_); ++generation_; return true; }
bool rollback(Api& api) { if (!fallback_) return false; if (active_) active_->leave(api); active_ = std::move(fallback_); ++generation_; return true; }
[[nodiscard]] Piece* active() const noexcept { return active_.get(); } [[nodiscard]] std::uint64_t generation() const noexcept { return generation_; }
private: JsEngine& engine_; JsLimits limits_; std::unique_ptr<JsPiece> active_; std::unique_ptr<JsPiece> staged_; std::unique_ptr<JsPiece> fallback_; std::uint64_t generation_ = 0;};
// Control-plane commands are data/configuration or sandboxed JS piece source,// never downloaded native executable code.enum class CommandKind { load_piece, configure, run_probe, reload, ping };struct Command { CommandKind kind; std::string request_id; std::string target; std::string json;};struct Telemetry { std::string type; std::string request_id; std::uint64_t timestamp_us = 0; std::string json;};
class ControlChannel { public: virtual ~ControlChannel() = default; virtual std::optional<Command> poll() = 0; virtual void send(const Telemetry&) = 0;};
} // namespace ac::xbox