aboutsummaryrefslogtreecommitdiffstats
path: root/source/player/player_ui.h
blob: d0881bafb22a5cff241a6b29e6ac4e4e2f2b7e76 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#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