1DOLLY 82PACKAGE raylib3REQUIRES HOST runtime@04REQUIRES HOST display@05REQUIRES HOST input@06REQUIRES HOST touch@078# raylib 6.0 from upstream's source, unchanged, with its software renderer and9# Dolly's platform backend (rcore_dolly.c, compiled into raylib's rcore.c as10# its custom platform): InitWindow shows the frames through display@0, keys,11# text, the pointer and the wheel come from input@0, fingers from touch@0, and12# GetTime reads the monotonic clock. <dolly/raylib.h> serves a program that holds the display13# itself. For programs the cc package builds; <dolly/raylib.h> includes the14# display and input headers, whose packages come with it.15FROM /Dollyfile-system 5eb06684bc61be6a1d9af98b8a740fba55b22ff4216f6cdb117c6ff6583104f016INSTALL /Dollyfile-libdolly-touch e575ebfcfdeb0bd32c82b34d59b0a25b627a1189a5a5ebdcc930073fe86594f017INSTALL /Dollyfile-libdolly-display afbf05e4d39eda376f6195f86762fb635a3effb3f2e5414ef1ffa70b298af91918INSTALL /Dollyfile-libdolly-input 22d9db2a107fd5721f70d9662921b2e3fd9432549056e9e27e0ee36e6a898c8419REQUIRES HEADER libc20REQUIRES HEADER display21REQUIRES HEADER input22REQUIRES HEADER touch23REQUIRES TOOL   ar24REQUIRES TOOL   cc25REQUIRES TOOL   make26REQUIRES TOOL   mkdir27REQUIRES TOOL   rm28REQUIRES TOOL   tar2930SOURCE /dist/static/gamedev/raylib.tar b16dd083b9205e14f8b79a1d91c934b579c9a92bcb4e3af0e374f3e71dcf82d3 /tmp/raylib.tar31FILE /usr/src/dolly/raylib/raylib.mk32    CC := cc33    AR := ar34    SOURCE := /usr/src/raylib/src35    BUILD := /usr/src/dolly/raylib/build36    FLAGS := -std=gnu99 -O2 -D_GNU_SOURCE -DGRAPHICS_API_OPENGL_SOFTWARE -DSW_FRAMEBUFFER_OUTPUT_BGRA=0 -fno-strict-aliasing -I $(SOURCE)37    OBJECTS := $(patsubst %,$(BUILD)/%.o,rcore rshapes rtextures rtext rmodels)38    39    .PHONY: all40    41    all: /usr/lib/libraylib.a /usr/lib/libdolly-raylib.a42    43    $(BUILD)/%.o: $(SOURCE)/%.c44    	mkdir -p $(BUILD)45    	$(CC) $(FLAGS) -c $< -o $@46    47    # raylib's core with Dolly's platform backend in the same unit.48    $(BUILD)/rcore.o: /usr/src/dolly/raylib/rcore-dolly.c /usr/src/dolly/raylib/rcore_dolly.c $(SOURCE)/rcore.c49    	mkdir -p $(BUILD)50    	$(CC) $(FLAGS) -I /usr/src/dolly/raylib -c $< -o $@51    52    /usr/lib/libraylib.a: $(OBJECTS)53    	$(AR) rcs $@ $(OBJECTS)54    	cp $(SOURCE)/raylib.h $(SOURCE)/raymath.h $(SOURCE)/rlgl.h /usr/include/55    56    /usr/lib/libdolly-raylib.a: /usr/src/dolly/raylib/dolly-raylib.c /usr/src/dolly/raylib/dolly-raylib.h /usr/lib/libraylib.a57    	mkdir -p /usr/include/dolly58    	cp /usr/src/dolly/raylib/dolly-raylib.h /usr/include/dolly/raylib.h59    	$(CC) -std=c17 -O2 -I /usr/src/dolly/raylib -c /usr/src/dolly/raylib/dolly-raylib.c -o $(BUILD)/dolly-raylib.o60    	$(AR) rcs $@ $(BUILD)/dolly-raylib.o61FILE /usr/src/dolly/raylib/rcore-dolly.c62    // raylib's core with Dolly's platform backend: built with no PLATFORM_* defined,63    // rcore.c includes no backend of its own, and Dolly's follows it in this unit.64    #include "rcore.c"65    #include "rcore_dolly.c"66FILE /usr/src/dolly/raylib/rcore_dolly.c67    // raylib's platform backend for Dolly, built into raylib's own rcore.c as its68    // custom platform (rcore-dolly.c includes rcore.c, then this file):69    //   - the window is display@0's surface, sized as InitWindow asks; a frame is70    //     presented in SwapScreenBuffer and paced by the page's animation frames;71    //   - keys, text, the pointer and the wheel come from input@0 in PollInputEvents;72    //   - fingers come from touch@0 where the page has a touchscreen for the program;73    //   - the clock is CLOCK_MONOTONIC.74    // A hidden window (FLAG_WINDOW_HIDDEN), or a program that already holds the75    // display (it composes raylib's frame into its own, or opened it through76    // <dolly/raylib.h>), has raylib render to memory only: the program presents77    // the frame and hands raylib its key events through DollyRaylibInputEvent.78    #include <dolly/display.h>79    #include <dolly/input.h>80    #include <dolly/touch.h>81    #include <time.h>82    83    typedef struct {84        int window;                         // raylib holds the display and the input85        dolly_display_surface surface;86        uint64_t input;87        uint64_t touch;                     // 0 without fingers88        uint32_t animationFrame;89        double start;90    } PlatformData;91    92    static PlatformData platform = { 0 };93    94    // A key or button pressed and released between two frames still reads as95    // pressed for one frame: its release waits for the next PollInputEvents.96    static char keyReleaseNext[MAX_KEYBOARD_KEYS] = { 0 };97    static char buttonReleaseNext[MAX_MOUSE_BUTTONS] = { 0 };98    static char touchReleaseNext = 0;99    100    static double MonotonicSeconds(void)101    {102        struct timespec now = { 0 };103        clock_gettime(CLOCK_MONOTONIC, &now);104        return (double)now.tv_sec + (double)now.tv_nsec*1e-9;105    }106    107    // KeyboardEvent.code of a key record, as raylib's KeyboardKey.108    static int KeyFromCode(const char *code, int length)109    {110        static const struct { const char *code; int key; } keys[] = {111            { "Space", KEY_SPACE }, { "Quote", KEY_APOSTROPHE }, { "Comma", KEY_COMMA }, { "Minus", KEY_MINUS },112            { "Period", KEY_PERIOD }, { "Slash", KEY_SLASH }, { "Semicolon", KEY_SEMICOLON }, { "Equal", KEY_EQUAL },113            { "BracketLeft", KEY_LEFT_BRACKET }, { "Backslash", KEY_BACKSLASH }, { "BracketRight", KEY_RIGHT_BRACKET },114            { "Backquote", KEY_GRAVE }, { "Escape", KEY_ESCAPE }, { "Enter", KEY_ENTER }, { "Tab", KEY_TAB },115            { "Backspace", KEY_BACKSPACE }, { "Insert", KEY_INSERT }, { "Delete", KEY_DELETE },116            { "ArrowRight", KEY_RIGHT }, { "ArrowLeft", KEY_LEFT }, { "ArrowDown", KEY_DOWN }, { "ArrowUp", KEY_UP },117            { "PageUp", KEY_PAGE_UP }, { "PageDown", KEY_PAGE_DOWN }, { "Home", KEY_HOME }, { "End", KEY_END },118            { "CapsLock", KEY_CAPS_LOCK }, { "ScrollLock", KEY_SCROLL_LOCK }, { "NumLock", KEY_NUM_LOCK },119            { "PrintScreen", KEY_PRINT_SCREEN }, { "Pause", KEY_PAUSE },120            { "ShiftLeft", KEY_LEFT_SHIFT }, { "ControlLeft", KEY_LEFT_CONTROL }, { "AltLeft", KEY_LEFT_ALT },121            { "MetaLeft", KEY_LEFT_SUPER }, { "ShiftRight", KEY_RIGHT_SHIFT }, { "ControlRight", KEY_RIGHT_CONTROL },122            { "AltRight", KEY_RIGHT_ALT }, { "MetaRight", KEY_RIGHT_SUPER }, { "ContextMenu", KEY_KB_MENU },123            { "NumpadDecimal", KEY_KP_DECIMAL }, { "NumpadDivide", KEY_KP_DIVIDE }, { "NumpadMultiply", KEY_KP_MULTIPLY },124            { "NumpadSubtract", KEY_KP_SUBTRACT }, { "NumpadAdd", KEY_KP_ADD }, { "NumpadEnter", KEY_KP_ENTER },125            { "NumpadEqual", KEY_KP_EQUAL },126        };127        if ((length == 4) && (strncmp(code, "Key", 3) == 0) && (code[3] >= 'A') && (code[3] <= 'Z')) return KEY_A + (code[3] - 'A');128        if ((length == 6) && (strncmp(code, "Digit", 5) == 0) && (code[5] >= '0') && (code[5] <= '9')) return KEY_ZERO + (code[5] - '0');129        if ((length == 7) && (strncmp(code, "Numpad", 6) == 0) && (code[6] >= '0') && (code[6] <= '9')) return KEY_KP_0 + (code[6] - '0');130        if ((length >= 2) && (length <= 3) && (code[0] == 'F'))131        {132            int number = atoi(code + 1);133            if ((number >= 1) && (number <= 12)) return KEY_F1 + number - 1;134        }135        for (size_t i = 0; i < sizeof(keys)/sizeof(keys[0]); i++)136        {137            if (((int)strlen(keys[i].code) == length) && (strncmp(keys[i].code, code, length) == 0)) return keys[i].key;138        }139        return KEY_NULL;140    }141    142    static void QueueCharacter(int codepoint)143    {144        if (CORE.Input.Keyboard.charPressedQueueCount < MAX_CHAR_PRESSED_QUEUE)145        {146            CORE.Input.Keyboard.charPressedQueue[CORE.Input.Keyboard.charPressedQueueCount] = codepoint;147            CORE.Input.Keyboard.charPressedQueueCount++;148        }149    }150    151    // One input@0 record into raylib's keyboard, character and mouse state.152    void DollyRaylibInputEvent(const dolly_input_event *event)153    {154        if (event->type == DOLLY_INPUT_EVENT_KEY)155        {156            int key = KeyFromCode((const char *)event->data + event->key_length, event->code_length);157            if ((key <= KEY_NULL) || (key >= MAX_KEYBOARD_KEYS)) return;158            if (event->action == DOLLY_KEY_ACTION_RELEASE)159            {160                if (CORE.Input.Keyboard.previousKeyState[key] == 0) keyReleaseNext[key] = 1;161                else CORE.Input.Keyboard.currentKeyState[key] = 0;162            }163            else164            {165                if (event->action == DOLLY_KEY_ACTION_REPEAT) CORE.Input.Keyboard.keyRepeatInFrame[key] = 1;166                else167                {168                    CORE.Input.Keyboard.currentKeyState[key] = 1;169                    if (CORE.Input.Keyboard.keyPressedQueueCount < MAX_KEY_PRESSED_QUEUE)170                    {171                        CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = key;172                        CORE.Input.Keyboard.keyPressedQueueCount++;173                    }174                }175                if (key == CORE.Input.Keyboard.exitKey) CORE.Window.shouldClose = true;176            }177        }178        else if (event->type == DOLLY_INPUT_EVENT_TEXT)179        {180            const unsigned char *text = event->data + event->key_length + event->code_length;181            for (int offset = 0; offset < event->text_length;)182            {183                int size = 0;184                int codepoint = GetCodepointNext((const char *)text + offset, &size);185                if (size <= 0) break;186                QueueCharacter(codepoint);187                offset += size;188            }189        }190        else if (event->type == DOLLY_INPUT_EVENT_POINTER)191        {192            // A browser numbers left, middle, right, back, forward; raylib left, right, middle, side, extra, forward, back.193            static const int buttons[] = { MOUSE_BUTTON_LEFT, MOUSE_BUTTON_MIDDLE, MOUSE_BUTTON_RIGHT, MOUSE_BUTTON_BACK, MOUSE_BUTTON_FORWARD };194            unsigned int button = event->flags >> 8;195            CORE.Input.Mouse.currentPosition = (Vector2){ (float)event->x, (float)event->y };196            CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;197            if ((button < 5) && (event->action != DOLLY_POINTER_ACTION_DRAG))198            {199                int mouse = buttons[button];200                if (event->action == DOLLY_POINTER_ACTION_PRESS) CORE.Input.Mouse.currentButtonState[mouse] = 1;201                else if (CORE.Input.Mouse.previousButtonState[mouse] == 0) buttonReleaseNext[mouse] = 1;202                else CORE.Input.Mouse.currentButtonState[mouse] = 0;203                CORE.Input.Touch.currentTouchState[0] = CORE.Input.Mouse.currentButtonState[MOUSE_BUTTON_LEFT];204            }205        }206        else if (event->type == DOLLY_INPUT_EVENT_SCROLL)207        {208            // Thousandths of a pixel, line or page toward later content; raylib counts lines up.209            float lines = (float)event->y/1000.0f;210            if (event->action == DOLLY_SCROLL_UNIT_PIXEL) lines /= 100.0f;211            else if (event->action == DOLLY_SCROLL_UNIT_PAGE) lines *= 3.0f;212            CORE.Input.Mouse.currentWheelMove.y -= lines;213        }214        else if (event->type == DOLLY_INPUT_EVENT_FOCUS)215        {216            if (event->action) CORE.Window.flags &= ~FLAG_WINDOW_UNFOCUSED;217            else CORE.Window.flags |= FLAG_WINDOW_UNFOCUSED;218        }219    }220    221    // One touch@0 record into raylib's touch points as its own touch platforms222    // keep them: the fingers down in the order they came, each change a gesture223    // event, and the first finger also the mouse.224    static void DollyRaylibTouchRecord(const dolly_touch_record *record)225    {226        int index = 0;227        while ((index < CORE.Input.Touch.pointCount) && (CORE.Input.Touch.pointId[index] != (int)record->id)) index++;228        if (index == CORE.Input.Touch.pointCount)229        {230            if ((record->phase != DOLLY_TOUCH_DOWN) || (index == MAX_TOUCH_POINTS)) return;231            CORE.Input.Touch.pointId[index] = (int)record->id;232            CORE.Input.Touch.pointCount++;233        }234        CORE.Input.Touch.position[index] = (Vector2){ (float)record->x, (float)record->y };235    #if SUPPORT_GESTURES_SYSTEM236        GestureEvent gesture = { 0 };237        gesture.touchAction = (record->phase == DOLLY_TOUCH_DOWN)? TOUCH_ACTION_DOWN :238            (record->phase == DOLLY_TOUCH_MOVE)? TOUCH_ACTION_MOVE : (record->phase == DOLLY_TOUCH_UP)? TOUCH_ACTION_UP : TOUCH_ACTION_CANCEL;239        gesture.pointCount = CORE.Input.Touch.pointCount;240        for (int i = 0; i < gesture.pointCount; i++)241        {242            gesture.pointId[i] = CORE.Input.Touch.pointId[i];243            gesture.position[i] = (Vector2){ CORE.Input.Touch.position[i].x/(float)GetScreenWidth(),244                CORE.Input.Touch.position[i].y/(float)GetScreenHeight() };245        }246        ProcessGestureEvent(gesture);247    #endif248        if ((record->phase == DOLLY_TOUCH_UP) || (record->phase == DOLLY_TOUCH_CANCEL))249        {250            for (int i = index; i < CORE.Input.Touch.pointCount - 1; i++)251            {252                CORE.Input.Touch.pointId[i] = CORE.Input.Touch.pointId[i + 1];253                CORE.Input.Touch.position[i] = CORE.Input.Touch.position[i + 1];254            }255            CORE.Input.Touch.pointCount--;256        }257        // A tap inside one frame still reads as down for a frame, as keys do.258        if (CORE.Input.Touch.pointCount > 0) CORE.Input.Touch.currentTouchState[MOUSE_BUTTON_LEFT] = 1;259        else if (CORE.Input.Touch.previousTouchState[MOUSE_BUTTON_LEFT] == 0) touchReleaseNext = 1;260        else CORE.Input.Touch.currentTouchState[MOUSE_BUTTON_LEFT] = 0;261        if (index == 0) CORE.Input.Mouse.currentPosition = (Vector2){ (float)record->x, (float)record->y };262    }263    264    bool WindowShouldClose(void)265    {266        if (CORE.Window.ready) return CORE.Window.shouldClose;267        else return true;268    }269    270    void ToggleFullscreen(void) { TRACELOG(LOG_WARNING, "ToggleFullscreen() not available on target platform"); }271    void ToggleBorderlessWindowed(void) { TRACELOG(LOG_WARNING, "ToggleBorderlessWindowed() not available on target platform"); }272    void MaximizeWindow(void) { TRACELOG(LOG_WARNING, "MaximizeWindow() not available on target platform"); }273    void MinimizeWindow(void) { TRACELOG(LOG_WARNING, "MinimizeWindow() not available on target platform"); }274    void RestoreWindow(void) { TRACELOG(LOG_WARNING, "RestoreWindow() not available on target platform"); }275    void SetWindowState(unsigned int flags) { TRACELOG(LOG_WARNING, "SetWindowState() not available on target platform"); }276    void ClearWindowState(unsigned int flags) { TRACELOG(LOG_WARNING, "ClearWindowState() not available on target platform"); }277    void SetWindowIcon(Image image) { TRACELOG(LOG_WARNING, "SetWindowIcon() not available on target platform"); }278    void SetWindowIcons(Image *images, int count) { TRACELOG(LOG_WARNING, "SetWindowIcons() not available on target platform"); }279    void SetWindowTitle(const char *title) { CORE.Window.title = title; }280    void SetWindowPosition(int x, int y) { TRACELOG(LOG_WARNING, "SetWindowPosition() not available on target platform"); }281    void SetWindowMonitor(int monitor) { TRACELOG(LOG_WARNING, "SetWindowMonitor() not available on target platform"); }282    void SetWindowMinSize(int width, int height) { CORE.Window.screenMin.width = width; CORE.Window.screenMin.height = height; }283    void SetWindowMaxSize(int width, int height) { CORE.Window.screenMax.width = width; CORE.Window.screenMax.height = height; }284    void SetWindowSize(int width, int height) { TRACELOG(LOG_WARNING, "SetWindowSize() not available on target platform"); }285    void SetWindowOpacity(float opacity) { TRACELOG(LOG_WARNING, "SetWindowOpacity() not available on target platform"); }286    void SetWindowFocused(void) { TRACELOG(LOG_WARNING, "SetWindowFocused() not available on target platform"); }287    void *GetWindowHandle(void) { return NULL; }288    int GetMonitorCount(void) { return 1; }289    int GetCurrentMonitor(void) { return 0; }290    Vector2 GetMonitorPosition(int monitor) { return (Vector2){ 0, 0 }; }291    int GetMonitorWidth(int monitor) { return CORE.Window.display.width; }292    int GetMonitorHeight(int monitor) { return CORE.Window.display.height; }293    int GetMonitorPhysicalWidth(int monitor) { return 0; }294    int GetMonitorPhysicalHeight(int monitor) { return 0; }295    int GetMonitorRefreshRate(int monitor) { return 60; }296    const char *GetMonitorName(int monitor) { return "Dolly"; }297    Vector2 GetWindowPosition(void) { return (Vector2){ 0, 0 }; }298    Vector2 GetWindowScaleDPI(void) { return (Vector2){ 1.0f, 1.0f }; }299    void SetClipboardText(const char *text) { TRACELOG(LOG_WARNING, "SetClipboardText() not available on target platform"); }300    const char *GetClipboardText(void) { return NULL; }301    Image GetClipboardImage(void) { Image image = { 0 }; return image; }302    303    void ShowCursor(void)304    {305        if (platform.window) dolly_display_set_cursor(platform.surface.generation, DOLLY_DISPLAY_CURSOR_DEFAULT);306        CORE.Input.Mouse.cursorHidden = false;307    }308    309    void HideCursor(void)310    {311        if (platform.window) dolly_display_set_cursor(platform.surface.generation, DOLLY_DISPLAY_CURSOR_HIDDEN);312        CORE.Input.Mouse.cursorHidden = true;313    }314    315    void EnableCursor(void)316    {317        if (platform.window) dolly_input_set_pointer_relative(platform.input, 0);318        ShowCursor();319        CORE.Input.Mouse.cursorLocked = false;320    }321    322    void DisableCursor(void)323    {324        if (platform.window) dolly_input_set_pointer_relative(platform.input, 1);325        HideCursor();326        CORE.Input.Mouse.cursorLocked = true;327    }328    329    // Presents raylib's frame and waits for the page's next animation frame.330    void SwapScreenBuffer(void)331    {332        if (!platform.window) return;333        dolly_display_frame frame = { 0 };334        if ((dolly_display_begin_frame(platform.surface.generation, &frame) == 0) &&335            (frame.pixel_format == DOLLY_DISPLAY_PIXEL_RGBA8) && ((int)frame.width == CORE.Window.render.width) &&336            ((int)frame.height == CORE.Window.render.height) && ((size_t)frame.stride*frame.height <= frame.capacity))337        {338            rlCopyFramebuffer(0, 0, CORE.Window.render.width, CORE.Window.render.height, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, frame.pixels);339            dolly_display_present(platform.surface.generation, frame.buffer_index);340        }341        dolly_display_wait_frame(platform.surface.generation, &platform.animationFrame, 100.0);342    }343    344    double GetTime(void)345    {346        return MonotonicSeconds() - platform.start;347    }348    349    void OpenURL(const char *url) { TRACELOG(LOG_WARNING, "OpenURL() not available on target platform"); }350    int SetGamepadMappings(const char *mappings) { return 0; }351    void SetGamepadVibration(int gamepad, float leftMotor, float rightMotor, float duration) { }352    353    void SetMousePosition(int x, int y)354    {355        CORE.Input.Mouse.currentPosition = (Vector2){ (float)x, (float)y };356        CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;357    }358    359    void SetMouseCursor(int cursor)360    {361        static const uint32_t cursors[] = { DOLLY_DISPLAY_CURSOR_DEFAULT, DOLLY_DISPLAY_CURSOR_DEFAULT, DOLLY_DISPLAY_CURSOR_TEXT,362            DOLLY_DISPLAY_CURSOR_CROSSHAIR, DOLLY_DISPLAY_CURSOR_POINTER, DOLLY_DISPLAY_CURSOR_EW_RESIZE, DOLLY_DISPLAY_CURSOR_NS_RESIZE,363            DOLLY_DISPLAY_CURSOR_NWSE_RESIZE, DOLLY_DISPLAY_CURSOR_NESW_RESIZE, DOLLY_DISPLAY_CURSOR_MOVE, DOLLY_DISPLAY_CURSOR_NOT_ALLOWED };364        if (platform.window && (cursor >= 0) && (cursor < (int)(sizeof(cursors)/sizeof(cursors[0]))))365        {366            dolly_display_set_cursor(platform.surface.generation, cursors[cursor]);367            CORE.Input.Mouse.cursor = cursor;368        }369    }370    371    const char *GetKeyName(int key) { return ""; }372    373    void PollInputEvents(void)374    {375    #if SUPPORT_GESTURES_SYSTEM376        UpdateGestures();377    #endif378        CORE.Input.Keyboard.keyPressedQueueCount = 0;379        CORE.Input.Keyboard.charPressedQueueCount = 0;380        for (int i = 0; i < MAX_KEYBOARD_KEYS; i++)381        {382            CORE.Input.Keyboard.previousKeyState[i] = CORE.Input.Keyboard.currentKeyState[i];383            CORE.Input.Keyboard.keyRepeatInFrame[i] = 0;384            if (keyReleaseNext[i]) CORE.Input.Keyboard.currentKeyState[i] = keyReleaseNext[i] = 0;385        }386        for (int i = 0; i < MAX_MOUSE_BUTTONS; i++)387        {388            CORE.Input.Mouse.previousButtonState[i] = CORE.Input.Mouse.currentButtonState[i];389            if (buttonReleaseNext[i]) CORE.Input.Mouse.currentButtonState[i] = buttonReleaseNext[i] = 0;390        }391        CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;392        CORE.Input.Mouse.previousWheelMove = CORE.Input.Mouse.currentWheelMove;393        CORE.Input.Mouse.currentWheelMove = (Vector2){ 0.0f, 0.0f };394        for (int i = 0; i < MAX_TOUCH_POINTS; i++) CORE.Input.Touch.previousTouchState[i] = CORE.Input.Touch.currentTouchState[i];395        if (touchReleaseNext) CORE.Input.Touch.currentTouchState[MOUSE_BUTTON_LEFT] = touchReleaseNext = 0;396    397        if (!platform.window) return;398        dolly_input_event event;399        while (dolly_input_next_event(platform.input, &event, 0.0) > 0) DollyRaylibInputEvent(&event);400        int count = DOLLY_TOUCH_READ_RECORDS;401        while (platform.touch && (count == DOLLY_TOUCH_READ_RECORDS))402        {403            dolly_touch_record records[DOLLY_TOUCH_READ_RECORDS];404            count = dolly_touch_read(platform.touch, records, DOLLY_TOUCH_READ_RECORDS, 0.0);405            for (int i = 0; i < count; i++) DollyRaylibTouchRecord(&records[i]);406            if (count < 0)407            {408                dolly_touch_release(platform.touch);409                platform.touch = 0;410            }411        }412    }413    414    int InitPlatform(void)415    {416        if (rlGetVersion() != RL_OPENGL_SOFTWARE)417        {418            TRACELOG(LOG_FATAL, "PLATFORM: DOLLY: raylib must be built with its software renderer (GRAPHICS_API_OPENGL_SOFTWARE)");419            return -1;420        }421        platform.start = MonotonicSeconds();422    423        // A hidden window (FLAG_WINDOW_HIDDEN), or a program that holds the display424        // already, presents raylib's frames itself.425        if (!(CORE.Window.flags & FLAG_WINDOW_HIDDEN) && (dolly_display_acquire(&platform.surface) == 0))426        {427            if (dolly_input_acquire(&platform.input) != 0)428            {429                dolly_display_release(platform.surface.generation);430                TRACELOG(LOG_WARNING, "PLATFORM: DOLLY: no input: raylib renders to memory");431            }432            else433            {434                platform.window = 1;435                // Fingers where the page has a touchscreen for the program; without436                // one the program goes on with the mouse and keys.437                if (dolly_touch_acquire(&platform.touch) != 0) platform.touch = 0;438                CORE.Window.display.width = (int)platform.surface.width;439                CORE.Window.display.height = (int)platform.surface.height;440                if (CORE.Window.screen.width == 0) CORE.Window.screen.width = (int)platform.surface.width;441                if (CORE.Window.screen.height == 0) CORE.Window.screen.height = (int)platform.surface.height;442                if (((int)platform.surface.width != CORE.Window.screen.width) || ((int)platform.surface.height != CORE.Window.screen.height))443                {444                    if (dolly_display_set_size(platform.surface.generation, (uint32_t)CORE.Window.screen.width,445                        (uint32_t)CORE.Window.screen.height, &platform.surface) != 0)446                    {447                        TRACELOG(LOG_FATAL, "PLATFORM: DOLLY: the display cannot show %i x %i", CORE.Window.screen.width, CORE.Window.screen.height);448                        if (platform.touch) dolly_touch_release(platform.touch);449                        dolly_input_release(platform.input);450                        dolly_display_release(platform.surface.generation);451                        platform.window = 0;452                        return -1;453                    }454                }455            }456        }457    458        CORE.Window.render.width = CORE.Window.screen.width;459        CORE.Window.render.height = CORE.Window.screen.height;460        CORE.Window.currentFbo.width = CORE.Window.render.width;461        CORE.Window.currentFbo.height = CORE.Window.render.height;462        CORE.Window.ready = true;463        CORE.Storage.basePath = GetWorkingDirectory();464        InitTimer();465        TRACELOG(LOG_INFO, "PLATFORM: DOLLY: %s, %i x %i", platform.window ? "display and input" : "memory only",466            CORE.Window.screen.width, CORE.Window.screen.height);467        return 0;468    }469    470    void ClosePlatform(void)471    {472        if (platform.window)473        {474            if (platform.touch) dolly_touch_release(platform.touch);475            dolly_input_release(platform.input);476            dolly_display_release(platform.surface.generation);477        }478        platform.window = 0;479        platform.touch = 0;480    }481FILE /usr/src/dolly/raylib/dolly-raylib.c482    #include "dolly-raylib.h"483    484    #include <rlgl.h>485    486    #include <errno.h>487    #include <stddef.h>488    #include <string.h>489    490    // raylib's Dolly backend (rcore_dolly.c) keeps raylib's key, character and491    // mouse state from the records it is handed.492    void DollyRaylibInputEvent(const dolly_input_event *event);493    494    int dolly_raylib_open_sized(dolly_raylib *context, const char *title,495                                uint32_t max_width, uint32_t max_height) {496      if (context == NULL || max_width == 0 || max_height == 0) return -EINVAL;497      memset(context, 0, sizeof(*context));498      int status = dolly_display_acquire(&context->surface);499      if (status != 0) return status;500      status = dolly_input_acquire(&context->input);501      if (status != 0) {502        dolly_display_release(context->surface.generation);503        memset(context, 0, sizeof(*context));504        return status;505      }506    507      uint32_t width = context->surface.width;508      uint32_t height = context->surface.height;509      if (width > max_width || height > max_height) {510        if ((uint64_t)width * max_height > (uint64_t)height * max_width) {511          height = (uint32_t)((uint64_t)height * max_width / width);512          width = max_width;513        } else {514          width = (uint32_t)((uint64_t)width * max_height / height);515          height = max_height;516        }517        if (width == 0) width = 1;518        if (height == 0) height = 1;519        status = dolly_display_set_size(context->surface.generation, width, height,520                                        &context->surface);521        if (status != 0) {522          dolly_input_release(context->input);523          dolly_display_release(context->surface.generation);524          memset(context, 0, sizeof(*context));525          return status;526        }527      }528    529      InitWindow((int)context->surface.width, (int)context->surface.height, title);530      if (!IsWindowReady()) {531        dolly_input_release(context->input);532        dolly_display_release(context->surface.generation);533        memset(context, 0, sizeof(*context));534        return -EIO;535      }536      SetTraceLogLevel(LOG_WARNING);537      context->open = 1;538      return 0;539    }540    541    int dolly_raylib_open(dolly_raylib *context, const char *title) {542      return dolly_raylib_open_sized(context, title, 800, 450);543    }544    545    int dolly_raylib_end_frame(dolly_raylib *context) {546      if (context == NULL || !context->open) return -EINVAL;547      // The program holds the display, so raylib's EndDrawing only flushes its548      // render batch and times the frame; the frame is presented here.549      EndDrawing();550    551      dolly_display_frame frame;552      int status = dolly_display_begin_frame(context->surface.generation, &frame);553      if (status == 0 && frame.pixel_format == DOLLY_DISPLAY_PIXEL_RGBA8 &&554          frame.width == context->surface.width &&555          frame.height == context->surface.height &&556          (size_t)frame.stride * frame.height <= frame.capacity) {557        rlCopyFramebuffer(0, 0, (int)frame.width, (int)frame.height,558                          PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, frame.pixels);559        status = dolly_display_present(context->surface.generation,560                                       frame.buffer_index);561      } else if (status == 0) {562        status = -EIO;563      }564      return status;565    }566    567    int dolly_raylib_wait_frame(dolly_raylib *context,568                                double timeout_milliseconds) {569      if (context == NULL || !context->open) return -EINVAL;570      return dolly_display_wait_frame(context->surface.generation,571                                      &context->animation_frame_sequence,572                                      timeout_milliseconds);573    }574    575    int dolly_raylib_set_cursor(dolly_raylib *context, uint32_t cursor) {576      if (context == NULL || !context->open) return -EINVAL;577      return dolly_display_set_cursor(context->surface.generation, cursor);578    }579    580    int dolly_raylib_next_event(dolly_raylib *context, dolly_input_event *event,581                                double timeout_milliseconds) {582      if (context == NULL || !context->open) return -EINVAL;583      int status = dolly_input_next_event(context->input, event, timeout_milliseconds);584      if (status > 0) DollyRaylibInputEvent(event);585      return status;586    }587    588    int dolly_raylib_close(dolly_raylib *context) {589      if (context == NULL || !context->open) return -EINVAL;590      CloseWindow();591      dolly_input_release(context->input);592      int status = dolly_display_release(context->surface.generation);593      memset(context, 0, sizeof(*context));594      return status;595    }596    597    int dolly_raylib_code_is(const dolly_input_event *event, const char *code) {598      if (event == NULL || code == NULL || event->type != DOLLY_INPUT_EVENT_KEY)599        return 0;600      size_t length = strlen(code);601      return event->code_length == length &&602             memcmp(event->data + event->key_length, code, length) == 0;603    }604FILE /usr/src/dolly/raylib/dolly-raylib.h605    #ifndef DOLLY_RAYLIB_H606    #define DOLLY_RAYLIB_H607    608    #include <dolly/display.h>609    #include <dolly/input.h>610    #include <raylib.h>611    612    typedef struct {613      dolly_display_surface surface;614      uint64_t input;615      uint32_t animation_frame_sequence;616      int open;617    } dolly_raylib;618    619    // For a program that holds the display itself: open acquires the display and620    // input before InitWindow, so raylib renders to memory; end_frame presents its621    // frame and next_event hands each record to raylib's key and mouse state too.622    // The default open caps software rendering at 800x450; callers can choose623    // another upper bound while retaining the browser viewport ratio.624    int dolly_raylib_open(dolly_raylib *context, const char *title);625    int dolly_raylib_open_sized(dolly_raylib *context, const char *title,626                                uint32_t max_width, uint32_t max_height);627    int dolly_raylib_end_frame(dolly_raylib *context);628    int dolly_raylib_wait_frame(dolly_raylib *context, double timeout_milliseconds);629    int dolly_raylib_set_cursor(dolly_raylib *context, uint32_t cursor);630    int dolly_raylib_next_event(dolly_raylib *context, dolly_input_event *event,631                                double timeout_milliseconds);632    int dolly_raylib_close(dolly_raylib *context);633    int dolly_raylib_code_is(const dolly_input_event *event, const char *code);634    635    #endif636SLOP tar -xf /tmp/raylib.tar -C /637SLOP CWD /usr/src/dolly/raylib make -f /usr/src/dolly/raylib/raylib.mk638639EXPORTS LIB    raylib       /usr/lib/libraylib.a640EXPORTS LIB    dolly-raylib /usr/lib/libdolly-raylib.a641EXPORTS HEADER raylib       /usr/include/raylib.h642EXPORTS HEADER dolly-raylib /usr/include/dolly/raylib.h643FILE /usr/include/raymath.h644FILE /usr/include/rlgl.h645FILE /usr/share/licenses/raylib/LICENSE646SLOP rm -rf /tmp/raylib.tar /usr/src/raylib /usr/src/dolly/raylib647