SilverOS includes a lightweight software window manager built directly into the desktop shell. It supports up to 16 simultaneous windows, each with its own title bar, close button, drag mechanics, and per-window callbacks for drawing content, handling keyboard input, and responding to mouse clicks. The WM composites all windows into the framebuffer every frame, back-to-front, based on a Z-order array.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/pastaboy12345/SilverOS/llms.txt
Use this file to discover all available pages before exploring further.
Window structure
Every window is represented by awindow_t struct. The WM maintains an internal array of 16 of these and exposes them via the public API:
content_w and content_h are computed automatically by window_create as the inner drawable area:
content_w = width - WINDOW_BORDER * 2content_h = height - TITLEBAR_HEIGHT - WINDOW_BORDER
Window manager API
window_create — Scans the internal windows[MAX_WINDOWS] array for the first slot where visible == false, initialises it with the given position and size, pushes the new ID to the top of the Z-order array, and gives it focus. Returns the integer ID (0–15) on success, -1 if all slots are occupied or no free slot is found.
window_destroy — Marks the window as visible = false, removes it from the Z-order array, decrements the window count, and transfers focus to the next window at the top of the Z-order (if any).
window_set_draw_callback — Registers a function that the WM calls every frame while compositing. Use this to draw the window’s content area.
window_set_key_callback — Registers a function invoked when a key is pressed and this window has focus. The key parameter is a char using the constants from keyboard.h (e.g. KEY_ENTER, KEY_BACKSPACE, KEY_ESCAPE).
window_set_click_callback — Registers a function called when the user clicks inside the window’s content area (below the title bar). Coordinates are screen-absolute.
window_get — Returns a pointer to the internal window_t for read/write access, or NULL if the ID is out of range or the window is not visible.
Z-ordering
The WM maintains awindow_order[MAX_WINDOWS] integer array alongside a counter order_count. This array stores window IDs ordered from back (index 0) to front (index order_count - 1).
- New windows are appended to the end of
window_order, placing them on top of all existing windows. - Clicking a window calls the internal
focus_window(), which removes the window’s ID from its current position in the array and re-appends it at the end — bringing it to the front. - Rendering iterates
window_order[0]→window_order[order_count - 1], drawing each window in back-to-front order so later windows always appear on top. - Destroying a window shifts all subsequent entries in
window_orderleft by one and decrementsorder_count.
Creating a custom window
The pattern for all windows in SilverOS is: callwindow_create, then register your callbacks.
cx, cy, cw, ch define the inner content rectangle, accounting for the title bar height and window border. Always re-derive these from the current win->x / win->y inside your draw callback — the values change whenever the user drags the window.
Click handling
Click callbacks receive screen-absolute mouse coordinates. Convert them to content-relative coordinates for hit-testing UI elements inside the window:Close button
Each window has a red close button (RGB(180, 50, 50)) drawn in the top-right corner of the title bar, sized CLOSE_BTN_SIZE × CLOSE_BTN_SIZE (16×16 pixels). The WM checks every left-click against the close button bounds before dispatching to any window callback.
When the close button is clicked:
- The WM calls
window_destroy(win->id)directly. - Focus shifts to the new top-of-Z-order window, if any.
- No callback is fired — the window simply disappears. If you need cleanup on close (e.g. resetting a global ID), track the window ID in your module and detect the next call to
window_getreturningNULL.
The window manager supports a maximum of
MAX_WINDOWS = 16 windows at one time. window_create returns -1 if all 16 slots are occupied. Slots are reused after window_destroy — the WM scans for the first entry where visible == false on each window_create call.