Skip to content

Commit

Permalink
Add options: keep buttons enabled, custom commands
Browse files Browse the repository at this point in the history
Closes #2, closes #3
  • Loading branch information
qwerty12 committed Oct 10, 2022
1 parent b61bc1b commit f31d768
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
13 changes: 13 additions & 0 deletions common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ for i = ffi.C.BUTTON_FIRST, ffi.C.BUTTON_LAST - 1 do
common.button_ids[i] = 0x0400 + i
end

common.user_opts = {
never_disable_buttons = false,

prev_command = "",
play_pause_command = "",
next_command = ""
}

function common.read_options()
require("mp.options").read_options(common.user_opts, "mpv-taskbar-buttons")
return common.user_opts
end

function common.get_mpv_hwnd()
local our_pid = mp.get_property_number("pid")
local hwnd_pid = ffi.new("unsigned int[1]")
Expand Down
17 changes: 13 additions & 4 deletions hook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ local common = require("common")
local C = ffi.C

local callbacks = {
[common.button_ids[C.BUTTON_PREV]] = function() mp.command("playlist-prev") end,
[common.button_ids[C.BUTTON_PLAY_PAUSE]] = function() mp.commandv("cycle", "pause") end,
[common.button_ids[C.BUTTON_NEXT]] = function() mp.command("playlist-next") end
[common.button_ids[C.BUTTON_PREV]] = function() mp.command(common.user_opts.prev_command == "" and "playlist-prev" or common.user_opts.prev_command) end,
[common.button_ids[C.BUTTON_PLAY_PAUSE]] = function()
if common.user_opts.play_pause_command == "" then
mp.commandv("cycle", "pause")
else
mp.command(common.user_opts.play_pause_command)
end
end,
[common.button_ids[C.BUTTON_NEXT]] = function() mp.command(common.user_opts.next_command == "" and "playlist-next" or common.user_opts.next_command) end
}

ffi.cdef [[
Expand Down Expand Up @@ -87,7 +93,6 @@ local hHook = nil
local function generate_hook_callback()
-- Taken from https://github.com/nucular/tcclua
ffi.cdef [[
struct TCCState;
typedef struct TCCState TCCState;
TCCState *tcc_new(void);
void tcc_delete(TCCState *s);
Expand Down Expand Up @@ -162,6 +167,7 @@ local function generate_hook_callback()
assert(tcc.tcc_compile_string(state, hook) == 0)
local size = tcc.tcc_relocate(state, nil)
assert(size > 0)
-- a buffer allocated with ffi.new here eventually causes a crash
local lpCompiled = C.GlobalAlloc(GPTR, size)
assert(lpCompiled)
assert(C.VirtualProtect(lpCompiled, size, PAGE_EXECUTE_READWRITE, ffi.new("int[1]")))
Expand All @@ -187,9 +193,11 @@ local function start()
if mpv_tid == 0 then
return
end
common.read_options()
lpCompiledCallback, lpGetMsgProc = generate_hook_callback()
hHook = C.SetWindowsHookExW(WH_GETMESSAGE, lpGetMsgProc, nil, mpv_tid)
if hHook then
-- allow unelevated Explorer to send message to mpv running as adminstrator
C.ChangeWindowMessageFilterEx(mpv_hwnd, WM_COMMAND, MSGFLT_ADD, nil)
end

Expand Down Expand Up @@ -225,6 +233,7 @@ _G.mp_event_loop = function()

dwElapsed = (mp.get_time() * 1000) - dwStart
if dwElapsed < dwTimeout then
-- should re-call mp.get_next_timeout() here and break if it's less than dwTimeout
-- continue
else -- timed out
mp.dispatch_events(false)
Expand Down
12 changes: 8 additions & 4 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ local buttons = ffi.new("THUMBBUTTON[?]", C.BUTTON_LAST)
local updated_buttons = ffi.new("THUMBBUTTON[?]", C.BUTTON_LAST)

local function on_pause(_, value)
if icons[C.BUTTON_PLAY_PAUSE][value] ~= buttons[C.BUTTON_PLAY_PAUSE].hIcon then
buttons[C.BUTTON_PLAY_PAUSE].hIcon = icons[C.BUTTON_PLAY_PAUSE][value]
local newIcon = icons[C.BUTTON_PLAY_PAUSE][value]
if newIcon and newIcon ~= buttons[C.BUTTON_PLAY_PAUSE].hIcon then
buttons[C.BUTTON_PLAY_PAUSE].hIcon = newIcon
buttons[C.BUTTON_PLAY_PAUSE].dwMask = C.THB_ICON
w7taskbar:ThumbBarUpdateButtons(mpv_hwnd, 1, buttons + C.BUTTON_PLAY_PAUSE)
end
Expand Down Expand Up @@ -156,7 +157,8 @@ local function on_idle()
return
end

local is_idle_active = mp.get_property_bool("idle-active", false)
local options = common.read_options()
local is_idle_active = not options.never_disable_buttons and mp.get_property_bool("idle-active", false)
for i = C.BUTTON_FIRST, C.BUTTON_LAST - 1 do
if not is_idle_active then
buttons[i].dwMask = C.THB_ICON
Expand All @@ -179,7 +181,9 @@ local function on_idle()
local hr = w7taskbar:ThumbBarAddButtons(mpv_hwnd, C.BUTTON_LAST, buttons)
if hr >= 0 then
mp.observe_property("pause", "bool", on_pause)
mp.observe_property("playlist-pos-1", "number", on_pl_pos_change)
if not options.never_disable_buttons then
mp.observe_property("playlist-pos-1", "number", on_pl_pos_change)
end
mp.commandv("load-script", script_dir .. "/hook.lua")
else
mp.msg.error("ITaskbarList3::ThumbBarAddButtons failed with " .. hr)
Expand Down
4 changes: 4 additions & 0 deletions script-opts/mpv-taskbar-buttons.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
never_disable_buttons=no
prev_command=playlist-prev
play_pause_command=cycle pause
next_command=playlist-next

0 comments on commit f31d768

Please sign in to comment.