summary refs log tree commit diff
path: root/.config/mvi/scripts/detect-image.lua
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2020-12-03 12:56:51 +0100
committerRobert Günzler <r@gnzler.io>2020-12-07 19:28:05 +0100
commit0eb11ed2c3dc235349b87d20bf657f2fb9729994 (patch)
treeceba665d591328bb47418ab82bb0de31abff98e5 /.config/mvi/scripts/detect-image.lua
parente93cee56264c7e1d07b422d66430d996fd8b0c40 (diff)
Add bin/mvi and config; mpv-based image viewer
Diffstat (limited to '.config/mvi/scripts/detect-image.lua')
-rw-r--r--.config/mvi/scripts/detect-image.lua89
1 files changed, 89 insertions, 0 deletions
diff --git a/.config/mvi/scripts/detect-image.lua b/.config/mvi/scripts/detect-image.lua
new file mode 100644
index 0000000..1b3496c
--- /dev/null
+++ b/.config/mvi/scripts/detect-image.lua
@@ -0,0 +1,89 @@
+local opts = {
+    command_on_first_image_loaded="",
+    command_on_image_loaded="",
+    command_on_non_image_loaded="",
+}
+(require 'mp.options').read_options(opts)
+
+if opts.command_on_first_image_loaded == ""
+    and opts.command_on_image_loaded == ""
+    and opts.command_on_non_image_loaded == ""
+then
+    return
+end
+
+local msg = require 'mp.msg'
+
+local was_image = false
+local frame_count = nil
+local audio_tracks = nil
+local out_params_ready = nil
+local path = nil
+
+function run_maybe(str)
+    if str ~= "" then
+        mp.command(str)
+    end
+end
+
+function set_image(is_image)
+    if is_image and not was_image then
+        msg.verbose("First image detected")
+        run_maybe(opts.command_on_first_image_loaded)
+    end
+    if is_image then
+        msg.verbose("Image detected")
+        run_maybe(opts.command_on_image_loaded)
+    end
+    if not is_image and was_image then
+        msg.verbose("Non-image detected")
+        run_maybe(opts.command_on_non_image_loaded)
+    end
+    was_image = is_image
+end
+
+function state_changed()
+    -- only do things when state is consistent
+    if path ~= nil and audio_tracks ~= nil then
+        if frame_count == nil and audio_tracks > 0 then
+            set_image(false)
+        elseif out_params_ready and frame_count ~= nil then
+            -- png have 0 frames, jpg 1 ¯\_(ツ)_/¯
+            set_image((frame_count == 0 or frame_count == 1) and audio_tracks == 0)
+        end
+    end
+end
+
+mp.observe_property("dwidth", "number", function(_, val)
+    out_params_ready = (val ~= nil and val > 0)
+    state_changed()
+end)
+
+mp.observe_property("estimated-frame-count", "number", function(_, val)
+    frame_count = val
+    state_changed()
+end)
+
+mp.observe_property("path", "string", function(_, val)
+    if not val or val == "" then
+        path = nil
+    else
+        path = val
+    end
+    state_changed()
+end)
+
+mp.register_event("tracks-changed", function()
+    audio_tracks = 0
+    local tracks = 0
+    for _, track in ipairs(mp.get_property_native("track-list")) do
+        tracks = tracks + 1
+         if track.type == "audio" then
+             audio_tracks = audio_tracks + 1
+         end
+    end
+    if tracks == 0 then
+        audio_tracks = nil
+    end
+    state_changed()
+end)