#ifndef PLAYER_UI_H #define PLAYER_UI_H // NOTE: the ui is built like the rad debugger's: one kind of thing, a *view*, and every view is a // watch style table (columns, rows, tree expansion, inline editable values). views sit as tabs in // panels, panels form a binary split tree. tabs can be dragged anywhere: onto a panel to join its // tabs, onto a panel edge to split it. everything (reciters, surahs, offline library, now // playing, downloads, settings) goes through the same table code and the same vim motions. // // panels and views live in fixed pools and refer to each other by index, never by pointer. that // keeps the layout a plain blob: it is saved to disk as is and survives a hot reload of the ui. //- theme // NOTE: everything here is a setting (player_settings.h). these read the per frame copy // (ui_theme), so using them costs what the old constants cost. #define UI_THEME_COLOR(color) (ui_theme.colors[(color)]) #define COLOR_BG UI_THEME_COLOR(UI_COLOR_BG) #define COLOR_PANEL UI_THEME_COLOR(UI_COLOR_PANEL) #define COLOR_PANEL_EDGE UI_THEME_COLOR(UI_COLOR_PANEL_EDGE) #define COLOR_FOCUS_EDGE UI_THEME_COLOR(UI_COLOR_FOCUS_EDGE) #define COLOR_TAB UI_THEME_COLOR(UI_COLOR_TAB) #define COLOR_TAB_ACTIVE UI_THEME_COLOR(UI_COLOR_TAB_ACTIVE) #define COLOR_HEADER UI_THEME_COLOR(UI_COLOR_HEADER) #define COLOR_HOVER UI_THEME_COLOR(UI_COLOR_HOVER) #define COLOR_SELECTED UI_THEME_COLOR(UI_COLOR_SELECTED) #define COLOR_ZEBRA UI_THEME_COLOR(UI_COLOR_ZEBRA) #define COLOR_TEXT UI_THEME_COLOR(UI_COLOR_TEXT) #define COLOR_TEXT_DIM UI_THEME_COLOR(UI_COLOR_TEXT_DIM) #define COLOR_TEXT_FAINT UI_THEME_COLOR(UI_COLOR_TEXT_FAINT) #define COLOR_ACCENT UI_THEME_COLOR(UI_COLOR_ACCENT) #define COLOR_TRACK UI_THEME_COLOR(UI_COLOR_TRACK) #define COLOR_DROP UI_THEME_COLOR(UI_COLOR_DROP) #define COLOR_ON_ACCENT UI_THEME_COLOR(UI_COLOR_ON_ACCENT) #define COLOR_MARKED UI_THEME_COLOR(UI_COLOR_MARKED) #define UI_GAP (ui_theme.gap) #define UI_TAB_H (ui_theme.tab_height) #define UI_COLUMN_H (ui_theme.show_columns ? ui_theme.column_height : 0.0f) #define UI_ROW_H (ui_theme.row_height) #define UI_STATUS_H (ui_theme.status_height) #define UI_FONT (ui_theme.font_size) #define UI_FONT_SMALL (ui_theme.font_size_small) //- window, fixed #define UI_WIDTH 1100 #define UI_HEIGHT 720 #define UI_MIN_WIDTH 640 #define UI_MIN_HEIGHT 420 #define UI_ZOOM_DEFAULT 1.15f //- views typedef enum ViewKind { VIEW_NONE, VIEW_RECITERS, VIEW_SURAHS, VIEW_OFFLINE, // what's on disk: reciter -> surah tree VIEW_PLAYING, // now playing, as editable fields VIEW_DOWNLOADS, VIEW_SETTINGS, VIEW_QUEUE, // the play queue a multi selection builds VIEW_PROFILER, // memory, allocations, threads, stacks, timings VIEW_VIEWS, // every view there is, enter opens one VIEW_STATUS, // mode, message, now playing, downloads: the footer is just this view in a bar VIEW_KIND_COUNT, } ViewKind; typedef struct ScrollList ScrollList; struct ScrollList { f32 offset; f32 target; }; #define VIEW_EXPANDED_WORDS 4 // bitset over qari indices (256) #define UI_MAX_COLUMNS 6 typedef struct View View; struct View { u32 kind; // ViewKind, VIEW_NONE = free slot u32 group; // which part of a kind: the Colors settings, the Heap profiler, ... s32 cursor; // row (views whose cursor isn't derived from the selection) ScrollList scroll; u64 expanded[VIEW_EXPANDED_WORDS]; u32 first_visible; // last frame's visible rows, for H M L and ctrl-e / ctrl-y u32 visible_rows; u32 row_count; // NOTE: columns behave like tabs and panels: drag a header to reorder, drag an edge to resize, // middle click to hide. widths are relative weights, the visible ones always fill the table. u8 column_order[UI_MAX_COLUMNS]; // display position -> logical column f32 column_weight[UI_MAX_COLUMNS]; u32 column_hidden; // bit per logical column u32 column_count; // 0 = not set up yet }; //- panels typedef enum PanelKind { PANEL_FREE, PANEL_LEAF, // holds tabs PANEL_SPLIT, // holds two child panels } PanelKind; #define UI_MAX_PANELS 32 #define UI_MAX_VIEWS 32 #define UI_MAX_TABS 16 typedef struct Panel Panel; struct Panel { u32 kind; // PanelKind s32 parent; // -1 for the root // split u32 axis; // 0: children side by side, 1: stacked s32 children[2]; f32 ratio; // share of the first child. < 0 (stacked splits): the second child is a bar of // -ratio status lines, e.g. the footer. a panel too short for a table shows its // view as a bar: the rows' values side by side, no tabs, no header // leaf u32 tab_count; u32 active_tab; u8 tabs[UI_MAX_TABS]; // view indices // computed every frame Rect rect; }; typedef struct Layout Layout; struct Layout { u32 version; s32 root; s32 focused; // a leaf Panel panels[UI_MAX_PANELS]; View views[UI_MAX_VIEWS]; }; #define UI_LAYOUT_VERSION 3 // 2: views carry a group, 3: the footer is a view (2 migrates) //- overlays typedef enum UIOverlay { UI_OVERLAY_NONE, UI_OVERLAY_SEARCH, // '/' and '?' UI_OVERLAY_COMMAND, // ':' UI_OVERLAY_CONFIRM, // y/n question UI_OVERLAY_EDIT, // typing a new value into a settings / playing row } UIOverlay; typedef struct SearchResult SearchResult; struct SearchResult { u32 qari; // qari index for reciter results, UINT32_MAX for surah results u32 surah; // surah index for surah results }; // NOTE: a place in the library: reciter + surah. used by marks and the jump back (''). typedef struct UIPlace UIPlace; struct UIPlace { u32 qari_id; // catalog id, stable across catalog refreshes (0 = unset) u32 surah; }; #define UI_MAX_RESULTS 48 #define UI_MARKS 26 typedef struct App App; struct App { Arena *arena; Arena *scratch; // the main thread's scratch arena, handed to the hot loaded ui PlayerArgs args; String8 data_dir; u32 ui_generation; // bumped on every hot reload //- systems Catalog *catalog; TrackStore *store; AudioRing *ring; AudioOutput *output; Decoder *decoder; b32 audio_ok; PlatformWindow *window; VulkanRenderer renderer; FontSystem fonts; DrawList draw; u32 width, height, scale; // logical size + integer buffer scale Profiler *profiler; // the host's (the .so has its own unused global) Settings settings; Layout layout; String8 font_files[UI_FONT_CHOICES]; // loaded once on first use, shared by every reload //- selection, shared by every view (rad style: views follow the selected entity) s32 qari; // qari index into the catalog s32 surah; // 0..113 b32 keep_selection_visible; //- vim state u32 count; // pending count prefix, 0 = none u32 pending; // pending prefix key (g z d y m ' ` ctrl-w), 0 = none char message[128]; u64 message_ns; UIPlace marks[UI_MARKS]; UIPlace jump_back; //- overlays UIOverlay overlay; b32 overlay_opened; // this frame: don't type the key that opened it char input[64]; u32 input_size; b32 search_backward; char last_search[64]; u32 last_search_size; b32 last_search_backward; SearchResult results[UI_MAX_RESULTS]; u32 result_count; s32 result_cursor; u32 confirm_kind; u32 confirm_qari; u32 edit_view; // which row the edit overlay is editing u32 edit_field; //- mouse interaction //- multi selection: a bitset over track keys, shared by every view that lists tracks u64 *selected; u32 selected_count; b32 visual; // V: rows between the anchor and the cursor are selected too s32 visual_anchor; u32 visual_view; //- the play queue, mirrored from what we sent the decoder u32 queue[DECODER_QUEUE_CAPACITY]; u32 queue_count; //- column drag / resize, the same gestures as tabs and panel gaps s32 column_drag_view; // -1 none u32 column_drag_column; // logical column b32 column_drag_active; f32 column_drag_x; s32 column_resize_view; // -1 none u32 column_resize_index; // display position left of the edge u32 table_cursor; // cursor shape a table asked for this frame s32 drag_view; // tab being dragged, -1 none s32 drag_panel; // its panel b32 drag_active; // moved far enough to count as a drag f32 drag_x, drag_y; s32 resize_panel; // split whose ratio is being dragged, -1 none u64 last_click_ns; f32 last_click_x, last_click_y; u32 cursor_shape; //- playback state (main thread view) b32 paused; u32 requested_key; u64 resume_frame; PlaybackPosition position; u64 paused_since_ns; //- frame u64 last_frame_ns; f32 dt; f32 time; f32 frame_ms; // cpu time of a frame (ui + draw submission), smoothed f32 fps; // frames actually rendered over the last second b32 animating; // something moves: render every frame b32 ticking; // only the clock moves: a few redraws a second is plenty b32 quit; //- download speed readout u64 speed_last_bytes; u64 speed_last_ns; f64 speed; }; internal void ui_frame(App *app, PlatformInput *input); internal void ui_layout_default(Layout *layout); internal void app_play(App *app, u32 key, u64 frame); internal void app_toggle_pause(App *app); internal void app_message(App *app, const char *format, ...); internal void app_apply_settings(App *app); //- hot reload abi // NOTE: the ui (this file's .cpp and everything it calls) is also built as build/player_ui.so. the // host dlopens it and calls player_ui_frame each frame, so ui changes show up without restarting // (audio, downloads and the window keep running). App lives in the host and the .so only gets a // pointer: changing App's layout needs a restart, player_ui_app_size guards against that. typedef void PlayerUiFrame(App *app, PlatformInput *input, int wake_fd); #define PLAYER_UI_FRAME_SYMBOL "player_ui_frame" #define PLAYER_UI_SIZE_SYMBOL "player_ui_app_size" // NOTE: every struct the ui reads, embedded or through a pointer. any size change refuses the // reload ("restart to pick it up") instead of reading a mismatched layout. #define PLAYER_UI_SIGNATURE ((u64)sizeof(App) * 31 + (u64)sizeof(Profiler) * 1009 + (u64)sizeof(AudioOutput) * 7919 + \ (u64)sizeof(Decoder) * 104729 + (u64)sizeof(TrackStore) * 1299709 + (u64)sizeof(AudioRing) * 15485863 + (u64)sizeof(DrawInstance) * 86028121) #endif // PLAYER_UI_H