| 1 | module libvendetta |
| 2 | |
| 3 | import math |
| 4 | import mpv |
| 5 | import os |
| 6 | import sokol.gfx |
| 7 | import sokol.sapp |
| 8 | import time |
| 9 | import ui2 |
| 10 | import wake |
| 11 | |
| 12 | // register_android_vm hands the NativeActivity's JavaVM to mpv's libavcodec, |
| 13 | // which needs it for Android audio and hardware decoding. Without it mpv logs |
| 14 | // that no Java virtual machine is registered and plays without them. |
| 15 | $if android { |
| 16 | fn register_android_vm() { |
| 17 | act := unsafe { &os.NativeActivity(sapp.android_get_native_activity()) } |
| 18 | if isnil(act) { |
| 19 | return |
| 20 | } |
| 21 | mpv.register_java_vm(act.vm) |
| 22 | } |
| 23 | } $else { |
| 24 | fn register_android_vm() {} |
| 25 | } |
| 26 | |
| 27 | // Desktop GL on the Unix targets links the same Mesa libGL as Linux: the |
| 28 | // headers sit in the default clang search path on FreeBSD, in xenocara's |
| 29 | // /usr/X11R6 tree on OpenBSD, the base system's /usr/X11R7 on NetBSD, |
| 30 | // dports' /usr/local on DragonFly, and plain /usr on Solaris — the trees |
| 31 | // sokol's own flags put on the search paths the whole build shares (Solaris |
| 32 | // needs none of that, its compiler already searches /usr). All take the |
| 33 | // Linux include block. |
| 34 | $if linux || freebsd || openbsd || netbsd || dragonfly || solaris { |
| 35 | #flag -lGL |
| 36 | #flag -DGL_GLEXT_PROTOTYPES |
| 37 | #include <GL/gl.h> |
| 38 | #include <GL/glext.h> |
| 39 | } |
| 40 | $if windows { |
| 41 | // Desktop GL on Windows: sokol's GL backend loads every entry point at |
| 42 | // runtime (opengl32.dll only exports GL 1.1, and its static function |
| 43 | // pointers in sokol_gfx.h collide with GL header declarations), so this |
| 44 | // file declares no GL prototypes and includes no GL header. GL calls go |
| 45 | // through mpv.wgl_proc, the same source mpv itself asks; the opengl32 |
| 46 | // link flag lives in the mpv module next to that loader. |
| 47 | } |
| 48 | $if macos { |
| 49 | // Desktop GL on macOS links the OpenGL framework, the way the Linux path |
| 50 | // links -lGL: sokol's GL backend declares and calls the same functions |
| 51 | // through <OpenGL/gl3.h>, so this file includes it and runs its surface |
| 52 | // texture work through the header like Linux does. The framework link |
| 53 | // flag lives in the mpv module next to mpv's own proc resolver. The |
| 54 | // deprecation marks Apple puts on GL functions are silenced the same |
| 55 | // way sokol silences them. |
| 56 | #define GL_SILENCE_DEPRECATION |
| 57 | #include <OpenGL/gl3.h> |
| 58 | } |
| 59 | $if android { |
| 60 | // GLES 3 on Android links the NDK's GLESv3 the way the Linux path links |
| 61 | // Mesa libGL: sokol's GL backend declares and calls the same functions |
| 62 | // through <GLES3/gl3.h>, so this file includes it and runs its surface |
| 63 | // texture work through the header like Linux does. The link flags live |
| 64 | // in the mpv module next to mpv's own proc resolver, which is where the |
| 65 | // Windows and macOS story keeps their link flags too. |
| 66 | #include <GLES3/gl3.h> |
| 67 | } |
| 68 | |
| 69 | // The macOS build is pinned to the paths the other platforms take. Without |
| 70 | // ui2_custom_rendering, macOS defaults ui2 to its native AppKit backend, |
| 71 | // which never calls the gl surface renderer the video arrives through; |
| 72 | // without darwin_sokol_glcore33, macOS defaults sokol to Metal, which |
| 73 | // cannot wrap the GL textures mpv draws into. Linux takes the custom |
| 74 | // renderer without a define. Windows keeps sokol's default GL backend, so its |
| 75 | // build passes only ui2_custom_rendering: darwin_sokol_glcore33 picks GL over |
| 76 | // Metal on macOS and means nothing there. |
| 77 | $if macos && !ui2_custom_rendering ? { |
| 78 | $compile_error("the macOS build needs -d ui2_custom_rendering: vendetta's video surface reaches ui2's custom renderer, and macOS defaults ui2 to its AppKit backend") |
| 79 | } |
| 80 | $if macos && !darwin_sokol_glcore33 ? { |
| 81 | $compile_error('the macOS build needs -d darwin_sokol_glcore33: mpv draws into OpenGL textures, and macOS defaults sokol to Metal') |
| 82 | } |
| 83 | |
| 84 | const gl_color_buffer_bit = 0x00004000 |
| 85 | const gl_scissor_test = 0x0c11 |
| 86 | const gl_texture_2d = 0x0de1 |
| 87 | const gl_texture_min_filter = 0x2801 |
| 88 | const gl_texture_mag_filter = 0x2800 |
| 89 | const gl_linear = 0x2601 |
| 90 | |
| 91 | const gl_rgba = 0x1908 |
| 92 | const gl_rgba8 = 0x8058 |
| 93 | const gl_unsigned_byte = 0x1401 |
| 94 | const gl_framebuffer = 0x8d40 |
| 95 | const gl_color_attachment0 = 0x8ce0 |
| 96 | |
| 97 | // One shim per GL entry point the surface's texture work needs, so the |
| 98 | // surface code below stays platform-free. Linux, the BSDs, Solaris, macOS |
| 99 | // and Android declare the C prototypes and call the linked GL; the |
| 100 | // declarations only type-check the calls, because the C compiler takes the |
| 101 | // real headers' prototypes (V emits no prototype for a file that includes |
| 102 | // headers, the reason the const qualifiers those headers carry never |
| 103 | // conflict). Windows cannot declare prototypes at all — sokol_gfx.h defines |
| 104 | // every GL entry point as a static function pointer it loads itself, and |
| 105 | // those static pointers collide with GL header declarations — so each shim |
| 106 | // casts its name out of mpv.wgl_proc, the same source mpv's own render asks, |
| 107 | // at call time. They all run on the surface's cold paths (allocate, clear, |
| 108 | // release), so the per-call lookup costs nothing that matters. |
| 109 | $if linux || freebsd || openbsd || netbsd || dragonfly || solaris || macos || android { |
| 110 | fn C.glGenTextures(n i32, textures &u32) |
| 111 | fn C.glDeleteTextures(n i32, textures &u32) |
| 112 | fn C.glBindTexture(target u32, texture u32) |
| 113 | fn C.glTexImage2D(target u32, level i32, internal_format i32, width i32, height i32, border i32, format u32, typ u32, pixels voidptr) |
| 114 | fn C.glTexParameteri(target u32, pname u32, param i32) |
| 115 | fn C.glGenFramebuffers(n i32, framebuffers &u32) |
| 116 | fn C.glDeleteFramebuffers(n i32, framebuffers &u32) |
| 117 | fn C.glBindFramebuffer(target u32, framebuffer u32) |
| 118 | fn C.glFramebufferTexture2D(target u32, attachment u32, textarget u32, texture u32, level i32) |
| 119 | fn C.glClearColor(r f32, g f32, b f32, a f32) |
| 120 | fn C.glClear(mask u32) |
| 121 | fn C.glDisable(cap u32) |
| 122 | fn C.glViewport(x i32, y i32, width i32, height i32) |
| 123 | fn C.glColorMask(r u8, g u8, b u8, a u8) |
| 124 | |
| 125 | fn gl_gen_textures(n i32, textures &u32) { C.glGenTextures(n, textures) } |
| 126 | fn gl_delete_textures(n i32, textures &u32) { C.glDeleteTextures(n, textures) } |
| 127 | fn gl_bind_texture(target u32, texture u32) { C.glBindTexture(target, texture) } |
| 128 | fn gl_tex_image_2d(target u32, level i32, internal_format i32, width i32, height i32, border i32, format u32, typ u32, pixels voidptr) { |
| 129 | C.glTexImage2D(target, level, internal_format, width, height, border, format, typ, pixels) |
| 130 | } |
| 131 | fn gl_tex_parameter_i(target u32, pname u32, param i32) { |
| 132 | C.glTexParameteri(target, pname, param) |
| 133 | } |
| 134 | fn gl_gen_framebuffers(n i32, framebuffers &u32) { C.glGenFramebuffers(n, framebuffers) } |
| 135 | fn gl_delete_framebuffers(n i32, framebuffers &u32) { C.glDeleteFramebuffers(n, framebuffers) } |
| 136 | fn gl_bind_framebuffer(target u32, framebuffer u32) { C.glBindFramebuffer(target, framebuffer) } |
| 137 | fn gl_framebuffer_texture_2d(target u32, attachment u32, textarget u32, texture u32, level i32) { |
| 138 | C.glFramebufferTexture2D(target, attachment, textarget, texture, level) |
| 139 | } |
| 140 | fn gl_clear_color(r f32, g f32, b f32, a f32) { C.glClearColor(r, g, b, a) } |
| 141 | fn gl_clear(mask u32) { C.glClear(mask) } |
| 142 | // gl_prepare_mpv_target readies the state mpv draws with: ui2 leaves its own |
| 143 | // scissor and viewport set, and mpv does not reset either, so the frame would |
| 144 | // be clipped to, and scaled into, the last UI strip drawn. |
| 145 | fn gl_prepare_mpv_target(w i32, h i32) { |
| 146 | C.glColorMask(1, 1, 1, 1) |
| 147 | C.glDisable(gl_scissor_test) |
| 148 | C.glViewport(0, 0, w, h) |
| 149 | } |
| 150 | } |
| 151 | $if windows { |
| 152 | type GlGenTexturesProc = fn (i32, &u32) |
| 153 | type GlDeleteTexturesProc = fn (i32, &u32) |
| 154 | type GlBindTextureProc = fn (u32, u32) |
| 155 | type GlTexImage2DProc = fn (u32, i32, i32, i32, i32, i32, u32, u32, voidptr) |
| 156 | type GlTexParameteriProc = fn (u32, u32, i32) |
| 157 | type GlGenFramebuffersProc = fn (i32, &u32) |
| 158 | type GlDeleteFramebuffersProc = fn (i32, &u32) |
| 159 | type GlBindFramebufferProc = fn (u32, u32) |
| 160 | type GlFramebufferTexture2DProc = fn (u32, u32, u32, u32, i32) |
| 161 | type GlClearColorProc = fn (f32, f32, f32, f32) |
| 162 | type GlClearProc = fn (u32) |
| 163 | |
| 164 | fn gl_gen_textures(n i32, textures &u32) { |
| 165 | GlGenTexturesProc(mpv.wgl_proc(c'glGenTextures'))(n, textures) |
| 166 | } |
| 167 | fn gl_delete_textures(n i32, textures &u32) { |
| 168 | GlDeleteTexturesProc(mpv.wgl_proc(c'glDeleteTextures'))(n, textures) |
| 169 | } |
| 170 | fn gl_bind_texture(target u32, texture u32) { |
| 171 | GlBindTextureProc(mpv.wgl_proc(c'glBindTexture'))(target, texture) |
| 172 | } |
| 173 | fn gl_tex_image_2d(target u32, level i32, internal_format i32, width i32, height i32, border i32, format u32, typ u32, pixels voidptr) { |
| 174 | GlTexImage2DProc(mpv.wgl_proc(c'glTexImage2D'))(target, level, internal_format, width, height, border, format, typ, pixels) |
| 175 | } |
| 176 | fn gl_tex_parameter_i(target u32, pname u32, param i32) { |
| 177 | GlTexParameteriProc(mpv.wgl_proc(c'glTexParameteri'))(target, pname, param) |
| 178 | } |
| 179 | fn gl_gen_framebuffers(n i32, framebuffers &u32) { |
| 180 | GlGenFramebuffersProc(mpv.wgl_proc(c'glGenFramebuffers'))(n, framebuffers) |
| 181 | } |
| 182 | fn gl_delete_framebuffers(n i32, framebuffers &u32) { |
| 183 | GlDeleteFramebuffersProc(mpv.wgl_proc(c'glDeleteFramebuffers'))(n, framebuffers) |
| 184 | } |
| 185 | fn gl_bind_framebuffer(target u32, framebuffer u32) { |
| 186 | GlBindFramebufferProc(mpv.wgl_proc(c'glBindFramebuffer'))(target, framebuffer) |
| 187 | } |
| 188 | fn gl_framebuffer_texture_2d(target u32, attachment u32, textarget u32, texture u32, level i32) { |
| 189 | GlFramebufferTexture2DProc(mpv.wgl_proc(c'glFramebufferTexture2D'))(target, attachment, textarget, texture, level) |
| 190 | } |
| 191 | fn gl_clear_color(r f32, g f32, b f32, a f32) { |
| 192 | GlClearColorProc(mpv.wgl_proc(c'glClearColor'))(r, g, b, a) |
| 193 | } |
| 194 | fn gl_clear(mask u32) { GlClearProc(mpv.wgl_proc(c'glClear'))(mask) } |
| 195 | type GlDisableProc = fn (u32) |
| 196 | type GlColorMaskProc = fn (u8, u8, u8, u8) |
| 197 | type GlViewportProc = fn (i32, i32, i32, i32) |
| 198 | fn gl_prepare_mpv_target(w i32, h i32) { |
| 199 | GlColorMaskProc(mpv.wgl_proc(c'glColorMask'))(1, 1, 1, 1) |
| 200 | GlDisableProc(mpv.wgl_proc(c'glDisable'))(gl_scissor_test) |
| 201 | GlViewportProc(mpv.wgl_proc(c'glViewport'))(0, 0, w, h) |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // render_advanced_control reports whether mpv may prepare frames ahead through |
| 206 | // its render thread. Android uses the plain render contract: the advanced mode's |
| 207 | // threading rules are especially fragile around the first asynchronous file load. |
| 208 | fn render_advanced_control() bool { |
| 209 | $if android { |
| 210 | return false |
| 211 | } |
| 212 | return true |
| 213 | } |
| 214 | |
| 215 | // Surface is the framebuffer mpv draws the video into: a texture the size of |
| 216 | // the .opengl element in pixels, with an FBO attached to it. |
| 217 | struct Surface { |
| 218 | mut: |
| 219 | w i32 |
| 220 | h i32 |
| 221 | tex u32 |
| 222 | fbo u32 |
| 223 | // The (tex, w, h) triple ui2 has already registered, so the hand-off below |
| 224 | // can skip repeats: ui2 matches an unchanged request and keeps the old |
| 225 | // sokol image, and mpv keeps rendering into the same GL texture. |
| 226 | handed_tex u32 |
| 227 | handed_w i32 |
| 228 | handed_h i32 |
| 229 | } |
| 230 | |
| 231 | // resize (re)allocates the surface when the element's size in pixels changes. |
| 232 | // It reports whether it did, since the new surface is empty until it is |
| 233 | // rendered. |
| 234 | fn (mut s Surface) resize(w i32, h i32) bool { |
| 235 | if s.fbo != 0 && s.w == w && s.h == h { |
| 236 | return false |
| 237 | } |
| 238 | s.release() |
| 239 | s.w = w |
| 240 | s.h = h |
| 241 | gl_gen_textures(1, &s.tex) |
| 242 | gl_bind_texture(gl_texture_2d, s.tex) |
| 243 | gl_tex_image_2d(gl_texture_2d, 0, gl_rgba8, w, h, 0, gl_rgba, gl_unsigned_byte, unsafe { nil }) |
| 244 | gl_tex_parameter_i(gl_texture_2d, gl_texture_min_filter, gl_linear) |
| 245 | gl_tex_parameter_i(gl_texture_2d, gl_texture_mag_filter, gl_linear) |
| 246 | gl_gen_framebuffers(1, &s.fbo) |
| 247 | gl_bind_framebuffer(gl_framebuffer, s.fbo) |
| 248 | gl_framebuffer_texture_2d(gl_framebuffer, gl_color_attachment0, gl_texture_2d, s.tex, 0) |
| 249 | s.clear() |
| 250 | gl_bind_framebuffer(gl_framebuffer, 0) |
| 251 | return true |
| 252 | } |
| 253 | |
| 254 | // clear paints the surface black, as when no file is open. |
| 255 | fn (s Surface) clear() { |
| 256 | if s.fbo == 0 { |
| 257 | return |
| 258 | } |
| 259 | gl_bind_framebuffer(gl_framebuffer, s.fbo) |
| 260 | gl_clear_color(0, 0, 0, 1) |
| 261 | gl_clear(gl_color_buffer_bit) |
| 262 | gl_bind_framebuffer(gl_framebuffer, 0) |
| 263 | } |
| 264 | |
| 265 | fn (mut s Surface) release() { |
| 266 | if s.fbo != 0 { |
| 267 | gl_delete_framebuffers(1, &s.fbo) |
| 268 | s.fbo = 0 |
| 269 | } |
| 270 | if s.tex != 0 { |
| 271 | gl_delete_textures(1, &s.tex) |
| 272 | s.tex = 0 |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // render is the entry the host's gl surface renderer calls for every element. |
| 277 | // It draws only the widget's own surface id and ignores the rest, so the host |
| 278 | // keeps one renderer for all its GL elements and routes by id. |
| 279 | pub fn (mut p Player) render(id string, frame ui2.Rect, scale f64, height f64) { |
| 280 | if id != video_surface_id { |
| 281 | return |
| 282 | } |
| 283 | p.draw(frame, scale, height) |
| 284 | } |
| 285 | |
| 286 | // draw runs after the host's UI has been drawn, with the GL context current. |
| 287 | // The render context is created here, not at new(), because it needs that GL |
| 288 | // context; the startup files load right after, so mpv finds somewhere to draw. |
| 289 | fn (mut p Player) draw(frame ui2.Rect, scale f64, height f64) { |
| 290 | // Under the render-stats probe, time the present: the mpv render call and |
| 291 | // the swap block here, and the probe prints their duration per frame. |
| 292 | mut draw_t0 := u64(0) |
| 293 | $if render_stats_probe ? { |
| 294 | draw_t0 = time.sys_mono_now() |
| 295 | } |
| 296 | _ = height |
| 297 | if frame.width * scale < 1 || frame.height * scale < 1 { |
| 298 | return |
| 299 | } |
| 300 | if p.render_ctx == unsafe { nil } { |
| 301 | video_debug_note('draw: creating mpv render context') |
| 302 | // Advanced control lets the core prepare frames ahead and decode |
| 303 | // straight to a texture. Its price: this thread may never wait on the |
| 304 | // core, which is why property reads drain from the event queue. |
| 305 | // Linux, the BSDs and Solaris pass the X11 display along (sokol |
| 306 | // picks its X11 backend on all of them, so sokol's handle is the |
| 307 | // right one); Windows, macOS and Android pass no display |
| 308 | // parameter — mpv's GL backend reads the context sokol has already |
| 309 | // made current on this thread, and Android's EGL context is one of |
| 310 | // those. |
| 311 | $if linux || freebsd || openbsd || netbsd || dragonfly || solaris { |
| 312 | p.render_ctx = mpv.render_context_create_gl_advanced(p.mpv_ctx, sapp.x11_get_display(), render_advanced_control()) |
| 313 | } $else { |
| 314 | p.render_ctx = mpv.render_context_create_gl_advanced(p.mpv_ctx, unsafe { nil }, render_advanced_control()) |
| 315 | } |
| 316 | if p.render_ctx == unsafe { nil } { |
| 317 | eprintln('libvendetta: could not create the mpv render context') |
| 318 | return |
| 319 | } |
| 320 | video_debug_note('draw: mpv render context created') |
| 321 | // Required soon after creation with advanced control. The callback only |
| 322 | // bumps a data-segment counter — its user_data carries this player's |
| 323 | // wake slot, claimed before the relay spawns, so two players never wake |
| 324 | // each other — and it reads p.paused so the ask it cannot see as a |
| 325 | // frame tick is capped while the picture rests. |
| 326 | p.wake_slot = wake.claim() |
| 327 | mpv.render_context_set_update_callback(p.render_ctx, render_update_wake, |
| 328 | voidptr(u64(p.wake_slot))) |
| 329 | p.relay_stop = false |
| 330 | p.relay_thread = spawn render_wake_relay(p) |
| 331 | for i, file in p.startup { |
| 332 | flag := if i == 0 { 'replace' } else { 'append-play' } |
| 333 | video_debug_note('draw: loadfile ${file}') |
| 334 | load_rc := mpv.command_async(p.mpv_ctx, u64(2 + i), ['loadfile', file, flag]) |
| 335 | video_debug_note('draw: loadfile returned ${load_rc}') |
| 336 | if load_rc < 0 { |
| 337 | eprintln('libvendetta: loadfile ${file}: ${mpv.error_string(load_rc)}') |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | p.drain_events() |
| 342 | resized := p.surface.resize(i32(frame.width * scale), i32(frame.height * scale)) |
| 343 | $if video_debug ? { |
| 344 | // Every geometry change logs: a scale or rotation while paused asks |
| 345 | // exactly one frame, and a presents-divisor probe would sleep through |
| 346 | // the only draw that shows the broken rect. window_h is the physical |
| 347 | // framebuffer height, so the uncovered remainder of the window (the |
| 348 | // clear-color band) is height minus the element's covered span. |
| 349 | if resized || p.presents % 60 == 0 { |
| 350 | eprintln('VIDEO_DEBUG draw frame=${frame.x:.2f},${frame.y:.2f} ${frame.width:.2f}x${frame.height:.2f} scale=${scale:.3f} surface=${p.surface.w}x${p.surface.h} window_h=${height:.1f} resized=${resized}') |
| 351 | } |
| 352 | } |
| 353 | if p.clear_next { |
| 354 | p.surface.clear() |
| 355 | p.clear_next = false |
| 356 | } |
| 357 | p.present(p.surface.w, p.surface.h, resized) |
| 358 | // ui2 draws the texture as a quad in its own pass, so the host's overlays |
| 359 | // stay on top of the video. mpv changed GL state, so sokol must reset its |
| 360 | // cache. The hand-off matters only when the texture object or its size |
| 361 | // changes: a repeated request re-registers the same texture and asks ui2 |
| 362 | // for a frame, and since this call comes from inside the frame ui2 asked |
| 363 | // for, the ask would keep that ask going — build, hand-off, ask, forever, |
| 364 | // burning a vsync's worth of CPU even with the picture paused. ui2 |
| 365 | // consumes one request per frame in sync_gl_surface_textures, and the |
| 366 | // frame this call belongs to already picked up the previous hand-off. |
| 367 | if p.surface.tex != p.surface.handed_tex |
| 368 | || p.surface.w != p.surface.handed_w || p.surface.h != p.surface.handed_h { |
| 369 | ui2.set_gl_surface_texture(video_surface_id, p.surface.tex, int(p.surface.w), int(p.surface.h)) |
| 370 | p.surface.handed_tex = p.surface.tex |
| 371 | p.surface.handed_w = p.surface.w |
| 372 | p.surface.handed_h = p.surface.h |
| 373 | } |
| 374 | gfx.reset_state_cache() |
| 375 | // The glide rides on the frames it paints: its slider write asks for the |
| 376 | // next frame, so playback keeps redrawing at the display rate and the ask |
| 377 | // dies with a pause, a hidden strip or the option off. |
| 378 | p.smooth_frame() |
| 379 | $if render_stats_probe ? { |
| 380 | draw_ms := f64(time.sys_mono_now() - draw_t0) / 1e6 |
| 381 | eprintln('probe draw_present ${draw_ms:.1f}ms') |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | // video_debug_readback samples the target immediately after mpv's render call, |
| 386 | // while the result is still known to be in this surface. It is Android-only and |
| 387 | // compiled only for -d video_debug. |
| 388 | $if video_debug ? { |
| 389 | $if android || linux { |
| 390 | fn C.glReadPixels(x i32, y i32, width i32, height i32, format u32, typ u32, pixels voidptr) |
| 391 | fn C.glGetFramebufferAttachmentParameteriv(target u32, attachment u32, pname u32, params &i32) |
| 392 | fn C.glGetIntegerv(pname u32, data &i32) |
| 393 | |
| 394 | fn (p &Player) video_debug_readback(frame_flags u64, skip bool) { |
| 395 | if p.presents % 30 != 0 { |
| 396 | return |
| 397 | } |
| 398 | // GL state mpv left behind: its viewport and scissor decide how the |
| 399 | // frame landed in the surface, and the draw binding says whether it |
| 400 | // unbound the target. |
| 401 | mut gl_state := [7]i32{} |
| 402 | C.glGetIntegerv(0x0ba2, &gl_state[0]) |
| 403 | C.glGetIntegerv(0x0c10, &gl_state[4]) |
| 404 | C.glGetIntegerv(0x8ca6, &gl_state[6]) |
| 405 | mut px := [24]u8{} |
| 406 | gl_bind_framebuffer(gl_framebuffer, p.surface.fbo) |
| 407 | for i, fy in [0.1, 0.3, 0.5, 0.7, 0.9] { |
| 408 | C.glReadPixels(p.surface.w / 2, i32(f64(p.surface.h) * fy), 1, 1, gl_rgba, |
| 409 | gl_unsigned_byte, &px[i * 4]) |
| 410 | } |
| 411 | C.glReadPixels(p.surface.w - 1, p.surface.h - 1, 1, 1, gl_rgba, gl_unsigned_byte, |
| 412 | &px[20]) |
| 413 | // Exact extent of the drawn content: one full-width row and one |
| 414 | // full-height column, scanned for the first/last bright pixel. |
| 415 | mut line_buf := []u8{len: p.surface.w * 4} |
| 416 | mut col_buf := []u8{len: p.surface.h * 4} |
| 417 | C.glReadPixels(0, p.surface.h / 2, p.surface.w, 1, gl_rgba, gl_unsigned_byte, |
| 418 | unsafe { voidptr(&line_buf[0]) }) |
| 419 | C.glReadPixels(p.surface.w / 2, 0, 1, p.surface.h, gl_rgba, gl_unsigned_byte, |
| 420 | unsafe { voidptr(&col_buf[0]) }) |
| 421 | mut x0 := p.surface.w |
| 422 | mut x1 := 0 |
| 423 | for x in 0 .. p.surface.w { |
| 424 | b := x * 4 |
| 425 | if line_buf[b] > 60 || line_buf[b + 1] > 60 || line_buf[b + 2] > 60 { |
| 426 | if x < x0 { |
| 427 | x0 = x |
| 428 | } |
| 429 | if x > x1 { |
| 430 | x1 = x |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | mut y0 := p.surface.h |
| 435 | mut y1 := 0 |
| 436 | for y in 0 .. p.surface.h { |
| 437 | b := y * 4 |
| 438 | if col_buf[b] > 60 || col_buf[b + 1] > 60 || col_buf[b + 2] > 60 { |
| 439 | if y < y0 { |
| 440 | y0 = y |
| 441 | } |
| 442 | if y > y1 { |
| 443 | y1 = y |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | eprintln('VIDEO_DEBUG dst-rect x[${x0}..${x1}] y[${y0}..${y1}] = ${x1 - x0 + 1}x${y1 - y0 + 1} of ${p.surface.w}x${p.surface.h}') |
| 448 | // The center column sampled every 1/24th of the surface height, |
| 449 | // as GL y (bottom origin): the color sequence against a striped |
| 450 | // picture says how the frame mapped into the surface — a squashed |
| 451 | // draw shows every source stripe, a truncated one stops early. |
| 452 | mut ystep := p.surface.h / 24 |
| 453 | if ystep < 1 { |
| 454 | ystep = 1 |
| 455 | } |
| 456 | mut samples := []string{} |
| 457 | for s in 0 .. 24 { |
| 458 | b := s * ystep * 4 |
| 459 | samples << '0x${col_buf[b + 2]:02x}${col_buf[b + 1]:02x}${col_buf[b]:02x}' |
| 460 | } |
| 461 | eprintln('VIDEO_DEBUG colscan glY0->${p.surface.h - 1}@${ystep} ${samples.join(' ')}') |
| 462 | mut attached := i32(0) |
| 463 | C.glGetFramebufferAttachmentParameteriv(gl_framebuffer, gl_color_attachment0, 0x8cd1, |
| 464 | &attached) |
| 465 | gl_bind_framebuffer(gl_framebuffer, 0) |
| 466 | eprintln('VIDEO_DEBUG fbo tex=${p.surface.tex} attached=${attached} vp=${gl_state[0]},${gl_state[1]}x${gl_state[2]}x${gl_state[3]} scissor=${gl_state[4]},${gl_state[5]}x${gl_state[6]}x... drawfb=${gl_state[6]} paused=${p.paused} flags=${frame_flags} skip=${skip} 10%=${px[0]},${px[1]},${px[2]},${px[3]} 30%=${px[4]},${px[5]},${px[6]},${px[7]} 50%=${px[8]},${px[9]},${px[10]},${px[11]} 70%=${px[12]},${px[13]},${px[14]},${px[15]} 90%=${px[16]},${px[17]},${px[18]},${px[19]} tr=${px[20]},${px[21]},${px[22]},${px[23]}') |
| 467 | } |
| 468 | } $else { |
| 469 | fn (p &Player) video_debug_readback(_ u64, _ bool) {} |
| 470 | } |
| 471 | } $else { |
| 472 | fn (p &Player) video_debug_readback(_ u64, _ bool) {} |
| 473 | } |
| 474 | |
| 475 | // present draws mpv's pending frame into the video surface. Every queued |
| 476 | // frame is drawn: a client-side skip is invisible to mpv — the render call |
| 477 | // consumes the frame and the swap report claims it was shown either way — so |
| 478 | // skipping can only freeze the picture while mpv believes the display kept |
| 479 | // up, which is how a slow machine ends up with long drop storms. mpv already |
| 480 | // sheds what cannot be kept (decoder drops, vo throttling) and its vo thread |
| 481 | // waits for this call to consume each frame, so drawing everything keeps the |
| 482 | // pipeline moving at whatever pace the UI thread manages. |
| 483 | // The one exception is a repeat: mpv re-queues the picture already on screen |
| 484 | // for intermediate vsyncs, and drawing it again would only repeat the same |
| 485 | // pixels, so the render call consumes it without a draw while the swap |
| 486 | // report keeps its timing honest. A repeat may still not be skipped when the |
| 487 | // surface it would land on was just reallocated — that surface is blank, so |
| 488 | // the first repeat after a resize must be drawn for real or the video area |
| 489 | // shows black until the next picture change. |
| 490 | fn (mut p Player) present(w i32, h i32, resized bool) { |
| 491 | p.presents++ |
| 492 | flags := mpv.render_context_update(p.render_ctx) |
| 493 | // A new surface is empty, and mpv only reports a frame when the picture |
| 494 | // changes, so a still picture would stay blank until the next change. |
| 495 | if !resized && flags & mpv.render_update_frame == 0 { |
| 496 | return |
| 497 | } |
| 498 | info := mpv.render_context_frame_info(p.render_ctx) |
| 499 | repeat := info.flags & mpv.render_frame_info_repeat != 0 |
| 500 | skip := repeat && !resized |
| 501 | if skip { |
| 502 | p.repeat_skips++ |
| 503 | } |
| 504 | gl_prepare_mpv_target(w, h) |
| 505 | render_rc := mpv.render_context_draw(p.render_ctx, p.surface.fbo, w, h, skip, p.cfg.block_target_time) |
| 506 | p.video_debug_readback(info.flags, skip) |
| 507 | if render_rc < 0 { |
| 508 | eprintln('libvendetta: render: ${mpv.error_string(render_rc)}') |
| 509 | } |
| 510 | mpv.render_context_report_swap(p.render_ctx) |
| 511 | if p.presents % 60 == 0 { |
| 512 | p.video_debug_dump('periodic') |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | // render_update_wake is the mpv render update callback, called on a libmpv |
| 517 | // thread. It must never call mpv_render_* functions from here, and it must |
| 518 | // stay off the GC heap too: a store the collector never sees becomes invisible |
| 519 | // to the UI thread. So the callback only bumps a plain counter in data-segment |
| 520 | // memory; render_wake_relay, a normal spawned thread, watches it and asks |
| 521 | // ui2's loop for a frame. |
| 522 | fn render_update_wake(user_data voidptr) { |
| 523 | // The slot index rides in as the callback's user_data: a foreign thread |
| 524 | // touching a GC-managed Player here would be invisible to the UI thread, |
| 525 | // but a data-segment counter keyed by an integer is not. |
| 526 | wake.bump(int(u64(user_data))) |
| 527 | } |
| 528 | |
| 529 | // render_wake_relay forwards mpv's frame hand-offs to the UI loop. The wake |
| 530 | // semaphore delivers each bump immediately, so the relay sleeps until mpv |
| 531 | // actually has a frame instead of polling on a timer, and it only watches its |
| 532 | // own player's slot — another embedded player's bumps go to their own relay. |
| 533 | // |
| 534 | // A frame tick asks ui2 for a paint, not a rebuild: the picture lands in the |
| 535 | // GL surface the retained tree already draws, so the tree only has to rebuild |
| 536 | // when something it declares changed — the elapsed label at a second |
| 537 | // boundary, a control's show/hide — which apply_property and the event loop |
| 538 | // ask for themselves. With advanced control, mpv fires the update callback |
| 539 | // once per frame slot even while paused — the picture has not changed — so |
| 540 | // while paused the paint rate is capped at one per second: slow enough to |
| 541 | // drain mpv's event queue once, and soon enough for the still of a paused |
| 542 | // seek and the notice line to show. mpv has somewhere to put the picture at |
| 543 | // render updates; if it stops bumping, a later bump lands as the first ask |
| 544 | // anyway. |
| 545 | fn render_wake_relay(p &Player) { |
| 546 | mut seen := u64(0) |
| 547 | mut last_ask := i64(0) |
| 548 | for { |
| 549 | if p.relay_stop { |
| 550 | return |
| 551 | } |
| 552 | if p.wake_slot < 0 { |
| 553 | // The wake pool was exhausted at setup: nothing will ever bump |
| 554 | // this relay awake, so poll instead — still a paint ask per |
| 555 | // display frame, only without the semaphore's sleep between |
| 556 | // frames. Players beyond the pool's capacity run this, and the |
| 557 | // event-driven path above stays what every normal player uses. |
| 558 | time.sleep(15 * time.millisecond) |
| 559 | if p.paused && time.ticks() - last_ask < time.second { |
| 560 | continue |
| 561 | } |
| 562 | last_ask = time.ticks() |
| 563 | ui2.request_paint() |
| 564 | continue |
| 565 | } |
| 566 | wake.wait(p.wake_slot) |
| 567 | if p.relay_stop { |
| 568 | return |
| 569 | } |
| 570 | cur := wake.current(p.wake_slot) |
| 571 | if cur == seen { |
| 572 | continue |
| 573 | } |
| 574 | seen = cur |
| 575 | now := time.ticks() |
| 576 | if p.paused && now - last_ask < time.second { |
| 577 | continue |
| 578 | } |
| 579 | last_ask = now |
| 580 | ui2.request_paint() |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | // stop_relay parks the frame-relay thread for good. The flag plus one bump |
| 585 | // empties its parked wait: the loop checks the flag before waiting and again |
| 586 | // once it wakes, so a destroy cannot miss it, and the join keeps a |
| 587 | // teardown-and-re-embed host from leaking a thread per cycle. |
| 588 | fn (mut p Player) stop_relay() { |
| 589 | if p.relay_stop { |
| 590 | return |
| 591 | } |
| 592 | p.relay_stop = true |
| 593 | // A claimed slot posts the bump that ends the parked wait; a fallback |
| 594 | // relay (slot −1, the pool exhausted) sees the flag between polls. |
| 595 | wake.bump(p.wake_slot) |
| 596 | p.relay_thread.wait() |
| 597 | } |
| 598 | |
| 599 | // The control line uses vendetta's own geometry: buttons are bar_button tall |
| 600 | // (the play toggle and the seek steps a bit wider than tall), sliders and time |
| 601 | // labels are bar_row tall, and the time labels are time_column_w wide so both |
| 602 | // ends of the seek slider line up in columns. |
| 603 | const bar_button = 27.0 |
| 604 | const bar_button_play_w = bar_button + 10.0 |
| 605 | const bar_button_seek_w = bar_button + 7.0 |
| 606 | const bar_row = 20.0 |
| 607 | const bar_height = bar_button + 3.0 |
| 608 | const bar_pad = 2.0 |
| 609 | const bar_gap = 4.0 |
| 610 | const bar_time_w = 72.0 |
| 611 | // An under-hour file renders its labels as mm:ss, five characters narrower |
| 612 | // than the hh:mm:ss the wide column holds, so its columns follow fmt_time. |
| 613 | // A file with no duration keeps the wide column: its elapsed label is free |
| 614 | // to grow into the hours. |
| 615 | const bar_time_short_w = 52.0 |
| 616 | const bar_time_hour = 60.0 * 60.0 |
| 617 | // The volume column plus its mute button and the gap before it mirror the |
| 618 | // button cluster left of the elapsed label exactly; the seek slider sits |
| 619 | // between the two columns, so only an exact mirror puts it at the strip's |
| 620 | // centre. |
| 621 | const bar_mute_w = 24.0 |
| 622 | const bar_volume_w = bar_button_play_w + bar_button_seek_w * 2 + bar_gap * 2 - bar_mute_w - bar_gap |
| 623 | const bar_time_pad = 20.0 |
| 624 | const bar_time_gap = 5.0 |
| 625 | // The time labels sit this far below the row's centre: the font's digits ride |
| 626 | // high, so a centred label reads above the sliders beside it. |
| 627 | const bar_time_nudge = 1.0 |
| 628 | const bar_seek_min_w = 60.0 |
| 629 | const bar_volume_min_w = 40.0 |
| 630 | const bar_time_min_w = 56.0 |
| 631 | |
| 632 | // time_column_w is the width each time label needs for a file of the given |
| 633 | // duration: fmt_time draws mm:ss until the total earns its hours column, so |
| 634 | // the columns match what the labels will actually render. |
| 635 | fn time_column_w(duration f64) f64 { |
| 636 | return if duration >= 0 && duration < bar_time_hour { bar_time_short_w } else { bar_time_w } |
| 637 | } |
| 638 | |
| 639 | // slider_frame is a slider's rect for a track of height h: a track taller than |
| 640 | // the row grows the frame around its centre, so the whole track takes input. |
| 641 | fn slider_frame(row ui2.Rect, h f64) ui2.Rect { |
| 642 | if h <= row.height { |
| 643 | return row |
| 644 | } |
| 645 | return ui2.rect(row.x, row.y - (h - row.height) / 2, row.width, h) |
| 646 | } |
| 647 | |
| 648 | // A negative slider thumb_size draws no thumb; the bars are bare tracks. |
| 649 | const seek_thumb_hidden = -1.0 |
| 650 | |
| 651 | // The overlay's hide deadline: after this many ms without pointer motion the |
| 652 | // built frame hides the controls. The wake chain that delivers the ask |
| 653 | // overshoots the deadline by hide_wake_margin_ms, so the build it wakes sees |
| 654 | // the deadline passed and schedules the hide for real instead of re-arming. |
| 655 | const hide_deadline_ms = 2500 |
| 656 | const hide_wake_margin_ms = 250 |
| 657 | |
| 658 | // ControlBar holds the rects of every control, in the coordinates of the rect |
| 659 | // control_bar was given. |
| 660 | pub struct ControlBar { |
| 661 | pub: |
| 662 | seek_back ui2.Rect |
| 663 | toggle ui2.Rect |
| 664 | seek_forward ui2.Rect |
| 665 | elapsed ui2.Rect |
| 666 | seek ui2.Rect |
| 667 | total ui2.Rect |
| 668 | mute ui2.Rect |
| 669 | volume ui2.Rect |
| 670 | bar ui2.Rect |
| 671 | } |
| 672 | |
| 673 | // control_bar lays the controls out left to right in the given rect's |
| 674 | // coordinates: seek back, play, seek forward, elapsed, seek slider, total, |
| 675 | // mute, volume. The seek slider takes the leftover width; in a narrow strip |
| 676 | // the volume slider shrinks first, then the time columns. |
| 677 | pub fn control_bar(b ui2.Rect, duration f64) ControlBar { |
| 678 | bar_y := b.height - bar_height |
| 679 | mid := bar_y + bar_height / 2 |
| 680 | outer_gap := bar_gap + bar_time_pad |
| 681 | mut volume_w := bar_volume_w |
| 682 | mut time_w := time_column_w(duration) |
| 683 | // The narrow floor follows the columns: the short labels give back the |
| 684 | // same slack they saved. |
| 685 | time_min_w := bar_time_min_w - (bar_time_w - time_w) |
| 686 | mut seek_w := b.width - bar_pad * 2 - bar_button_play_w - bar_button_seek_w * 2 - time_w * 2 - |
| 687 | volume_w - bar_mute_w - bar_gap * 3 - outer_gap * 2 - bar_time_gap * 2 |
| 688 | if seek_w < bar_seek_min_w { |
| 689 | shrink := math.min(bar_seek_min_w - seek_w, volume_w - bar_volume_min_w) |
| 690 | if shrink > 0 { |
| 691 | volume_w -= shrink |
| 692 | seek_w += shrink |
| 693 | } |
| 694 | } |
| 695 | if seek_w < bar_seek_min_w { |
| 696 | shrink := math.min(bar_seek_min_w - seek_w, time_w - time_min_w) |
| 697 | if shrink > 0 { |
| 698 | time_w -= shrink |
| 699 | seek_w += shrink * 2 |
| 700 | } |
| 701 | } |
| 702 | if seek_w < 0 { |
| 703 | seek_w = 0 |
| 704 | } |
| 705 | seek_back := ui2.rect(bar_pad, mid - bar_button / 2, bar_button_seek_w, bar_button) |
| 706 | toggle := ui2.rect(seek_back.x + bar_button_seek_w + bar_gap, mid - bar_button / 2, bar_button_play_w, bar_button) |
| 707 | seek_forward := ui2.rect(toggle.x + bar_button_play_w + bar_gap, mid - bar_button / 2, bar_button_seek_w, bar_button) |
| 708 | elapsed := ui2.rect(seek_forward.x + bar_button_seek_w + outer_gap, mid - bar_row / 2 + bar_time_nudge, |
| 709 | time_w, bar_row) |
| 710 | seek := ui2.rect(elapsed.x + time_w + bar_time_gap, mid - bar_row / 2, seek_w, bar_row) |
| 711 | total := ui2.rect(seek.x + seek_w + bar_time_gap, mid - bar_row / 2 + bar_time_nudge, time_w, bar_row) |
| 712 | mute := ui2.rect(total.x + time_w + outer_gap, mid - bar_row / 2, bar_mute_w, bar_row) |
| 713 | volume := ui2.rect(mute.x + bar_mute_w + bar_gap, mid - bar_row / 2, volume_w, bar_row) |
| 714 | return ControlBar{ |
| 715 | seek_back: seek_back |
| 716 | toggle: toggle |
| 717 | seek_forward: seek_forward |
| 718 | elapsed: elapsed |
| 719 | seek: seek |
| 720 | total: total |
| 721 | mute: mute |
| 722 | volume: volume |
| 723 | bar: ui2.rect(0, bar_y, b.width, bar_height) |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | // control_box is the control strip's rect in frame-local coordinates (origin |
| 728 | // top-left, y grows downward): the frame's bottom band, full width, in both |
| 729 | // layouts. Overlay mode lets the video fill the frame behind it instead. |
| 730 | fn control_box(frame ui2.Rect) ui2.Rect { |
| 731 | return ui2.rect(0, frame.height - bar_height, frame.width, bar_height) |
| 732 | } |
| 733 | |
| 734 | // strip_shown reports whether the control strip is visible in the frame the |
| 735 | // widget last built: docked strips never hide, overlay strips show while the |
| 736 | // pointer has moved within the rest deadline or rests near the strip. The |
| 737 | // smooth bar's frame chain asks it, so a bar nobody sees costs no frames. |
| 738 | fn (p &Player) strip_shown() bool { |
| 739 | if !p.cfg.overlay { |
| 740 | return true |
| 741 | } |
| 742 | if time.ticks() - p.last_motion < hide_deadline_ms { |
| 743 | return true |
| 744 | } |
| 745 | strip := control_box(p.frame) |
| 746 | // near is a legacy macro in Windows headers, so the local spells it out. |
| 747 | return p.accepts(p.pointer_x, p.pointer_y) && p.pointer_y - p.frame.y > strip.y - 20 |
| 748 | } |
| 749 | |
| 750 | // build returns the widget as one root element the host places in its tree |
| 751 | // at the given frame. The root sits at frame, so everything inside it is |
| 752 | // frame-local. The video fills the frame in overlay mode and sits above the |
| 753 | // strip when docked. The strip is a backdrop view and its controls are its |
| 754 | // children, so their rects are strip-local. Pointer coordinates given to the |
| 755 | // widget are in the same space as the frame. |
| 756 | pub fn (mut p Player) build(frame ui2.Rect) ui2.Element { |
| 757 | p.frame = frame |
| 758 | strip := control_box(frame) |
| 759 | mut video_h := frame.height |
| 760 | if !p.cfg.overlay { |
| 761 | video_h = if frame.height - bar_height > 40 { frame.height - bar_height } else { 40.0 } |
| 762 | } |
| 763 | video_rect := ui2.rect(0, 0, frame.width, video_h) |
| 764 | // Overlay controls hide on a rest deadline; docked controls never hide. |
| 765 | controls_shown := p.strip_shown() |
| 766 | mut children := [ui2.opengl(video_surface_id, video_rect)] |
| 767 | if !controls_shown { |
| 768 | return ui2.view(root_id, frame, ui2.BoxStyle{ transparent: true }, children) |
| 769 | } |
| 770 | bar := control_bar(ui2.rect(0, 0, strip.width, strip.height), p.duration) |
| 771 | // The docked strip sits statically, so it takes its own colour; the |
| 772 | // floating one keeps the plain backdrop. |
| 773 | children << ui2.view('vd_strip', strip, ui2.BoxStyle{ |
| 774 | bg: if p.cfg.overlay { p.palette.bg_dark } else { p.palette.bg_docked } |
| 775 | }, build_controls(p, bar)) |
| 776 | return ui2.view(root_id, frame, ui2.BoxStyle{ transparent: true }, children) |
| 777 | } |
| 778 | |
| 779 | // The control strip's icons come from assets/icons: the Material Symbols the |
| 780 | // SVGs there export raster to PNGs at 64 px (stb_image, what ui2 draws button |
| 781 | // images through, cannot parse SVG). The keys are the roles the strip's |
| 782 | // buttons play, not the files' names, so an icon can change under them. |
| 783 | const strip_icon_names = ['play', 'pause', 'replay30', 'forward30', 'volume_up', 'volume_off'] |
| 784 | |
| 785 | // strip_icon_glyph names the Material glyph each icon role falls back to: a |
| 786 | // build with no raster icons (`-d no_png_icons`) or a layout without the |
| 787 | // assets bundle draws these, rendered from the fonts on every platform — |
| 788 | // a missing PNG would otherwise come out as ui2's outline box. |
| 789 | fn strip_icon_glyph(name string) string { |
| 790 | return match name { |
| 791 | 'play' { |
| 792 | 'symbol:play.fill' |
| 793 | } |
| 794 | 'pause' { |
| 795 | 'symbol:pause.fill' |
| 796 | } |
| 797 | 'replay30' { |
| 798 | 'symbol:gobackward.30' |
| 799 | } |
| 800 | 'forward30' { |
| 801 | 'symbol:goforward.30' |
| 802 | } |
| 803 | 'volume_up' { |
| 804 | 'symbol:speaker.wave.2.fill' |
| 805 | } |
| 806 | 'volume_off' { |
| 807 | 'symbol:speaker.slash.fill' |
| 808 | } |
| 809 | else { |
| 810 | '' |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | // icon_bundle_dirs lists the places assets/icons ships: beside the executable |
| 816 | // (the release layout zips assets/ next to the binary), in the module tree the |
| 817 | // build came from, and the working directory. The first hit wins. |
| 818 | fn icon_bundle_dirs() []string { |
| 819 | return [ |
| 820 | os.join_path(os.dir(os.executable()), 'assets', 'icons'), |
| 821 | os.join_path(@VMODROOT, 'assets', 'icons'), |
| 822 | os.join_path(os.getwd(), 'assets', 'icons'), |
| 823 | ] |
| 824 | } |
| 825 | |
| 826 | // bundled_icon is the path of a PNG in assets/icons, or '' where the layout |
| 827 | // ships none or the build compiled the icons out. |
| 828 | pub fn bundled_icon(name string) string { |
| 829 | $if no_png_icons ? { |
| 830 | return '' |
| 831 | } $else { |
| 832 | for dir_path in icon_bundle_dirs() { |
| 833 | bundle_path := os.join_path(dir_path, name + '.png') |
| 834 | if os.is_file(bundle_path) { |
| 835 | return bundle_path |
| 836 | } |
| 837 | } |
| 838 | return '' |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | // resolve_strip_icons maps the icon roles to what the strip's buttons draw: |
| 843 | // each bundled PNG's path, or '' where this layout has none (the buttons then |
| 844 | // fall to the glyphs). `-d no_png_icons` compiles the resolution out entirely, |
| 845 | // leaving the glyphs alone to carry the strip. |
| 846 | fn resolve_strip_icons() map[string]string { |
| 847 | mut icons := map[string]string{} |
| 848 | $if no_png_icons ? { |
| 849 | return icons |
| 850 | } $else { |
| 851 | for name in strip_icon_names { |
| 852 | icons[name] = bundled_icon(name) |
| 853 | } |
| 854 | return icons |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | // strip_icon is the string the button for `name` draws: the bundled PNG's |
| 859 | // path, or the Material glyph the old build carried when the role has none. |
| 860 | fn (p &Player) strip_icon(name string) string { |
| 861 | if icon := p.icon_files[name] { |
| 862 | if icon.len > 0 { |
| 863 | return icon |
| 864 | } |
| 865 | } |
| 866 | return strip_icon_glyph(name) |
| 867 | } |
| 868 | |
| 869 | // build_controls returns the control elements in the strip's coordinates. |
| 870 | // They are appended one at a time: the checker panics on array literals that |
| 871 | // mix many of these constructor calls. |
| 872 | fn build_controls(p &Player, bar ControlBar) []ui2.Element { |
| 873 | button_box := ui2.BoxStyle{ |
| 874 | bg: p.palette.btn_play |
| 875 | radius: 6 |
| 876 | } |
| 877 | button_box_seek := ui2.BoxStyle{ |
| 878 | bg: p.palette.btn_seek |
| 879 | radius: 6 |
| 880 | opacity: 0.8 |
| 881 | } |
| 882 | button_text := ui2.TextStyle{ |
| 883 | color: p.palette.text_main |
| 884 | size: 16 |
| 885 | valign: .middle |
| 886 | } |
| 887 | label_text := ui2.TextStyle{ |
| 888 | color: p.palette.text_muted |
| 889 | size: 13 |
| 890 | } |
| 891 | elapsed_text := ui2.TextStyle{ |
| 892 | ...label_text |
| 893 | align: .right |
| 894 | } |
| 895 | // The seek and volume tracks share one height, kept inside the strip. |
| 896 | slider_h := math.min(math.max(p.cfg.slider_h, 1.0), bar_height) |
| 897 | mut children := []ui2.Element{} |
| 898 | children << glyph_button('vd_seek_back', bar.seek_back, button_box_seek, button_text, p.strip_icon('replay30')) |
| 899 | children << glyph_button('vd_toggle', bar.toggle, button_box, button_text, if p.paused { |
| 900 | p.strip_icon('play') |
| 901 | } else { |
| 902 | p.strip_icon('pause') |
| 903 | }) |
| 904 | children << glyph_button('vd_seek_forward', bar.seek_forward, button_box_seek, button_text, p.strip_icon('forward30')) |
| 905 | children << ui2.label('vd_elapsed', fmt_time(p.pos), bar.elapsed, elapsed_text) |
| 906 | children << ui2.slider(ui2.SliderConfig{ |
| 907 | id: 'vd_seek' |
| 908 | frame: slider_frame(bar.seek, slider_h) |
| 909 | min: 0 |
| 910 | max: if p.duration > 0 { p.duration } else { 1.0 } |
| 911 | // The glide's position, not the raw report: a rebuild lands between |
| 912 | // reports, and seeding the thumb with the reported value would snap it |
| 913 | // back to where the last report found it. |
| 914 | value: if p.has_file { p.glide_pos() } else { 0.0 } |
| 915 | padding: 0 |
| 916 | value_track: true |
| 917 | style: ui2.SliderStyle{ |
| 918 | track_color: p.palette.slider_track |
| 919 | value_track_color: p.palette.slider_value |
| 920 | track_width: slider_h |
| 921 | thumb_size: seek_thumb_hidden |
| 922 | } |
| 923 | }) |
| 924 | children << ui2.label('vd_total', fmt_time(p.duration), bar.total, label_text) |
| 925 | children << ui2.slider(ui2.SliderConfig{ |
| 926 | id: 'vd_volume' |
| 927 | frame: slider_frame(bar.volume, slider_h) |
| 928 | min: 0 |
| 929 | max: 100 |
| 930 | value: p.volume |
| 931 | padding: 0 |
| 932 | value_track: true |
| 933 | style: ui2.SliderStyle{ |
| 934 | track_color: p.palette.slider_track |
| 935 | value_track_color: if p.muted { |
| 936 | p.palette.btn_secondary |
| 937 | } else { |
| 938 | p.palette.slider_value |
| 939 | } |
| 940 | track_width: slider_h |
| 941 | thumb_size: seek_thumb_hidden |
| 942 | } |
| 943 | }) |
| 944 | children << glyph_button('vd_volume_icon', bar.mute, ui2.BoxStyle{ |
| 945 | bg: p.palette.btn_mute |
| 946 | radius: 6 |
| 947 | opacity: 0.8 |
| 948 | }, button_text, if p.muted { p.strip_icon('volume_off') } else { p.strip_icon('volume_up') }) |
| 949 | return children |
| 950 | } |
| 951 | |
| 952 | // glyph_button builds an image-only control button from the string the icon |
| 953 | // resolution hands it: a file path draws the rastered PNG as it is (no tint — |
| 954 | // a theme cannot restyle it), a 'symbol:' name draws the glyph tinted by the |
| 955 | // button's text style. |
| 956 | fn glyph_button(id string, frame ui2.Rect, box_style ui2.BoxStyle, text_style ui2.TextStyle, icon string) ui2.Element { |
| 957 | return ui2.Element{ |
| 958 | ...ui2.button(id, '', frame, box_style, text_style) |
| 959 | image_path: icon |
| 960 | } |
| 961 | } |
| 962 | |