Skip to content

Commit a060555

Browse files
committed
seek-to: Retrieve clipboard content natively
It's been some time since mpv implemented native clipboard access on all platforms. The old method of retriving clipboard content using external subprocesses is only used as a fallback now, removing complex code. The implementation is shamelessly copied from mpv's console.lua script: https://github.com/mpv-player/mpv/blob/d837c43656876fb2fb7cbcf7e74bd438ba563217/player/lua/console.lua#L1329-L1358 Though to simplify the code further, I didn't add support for the primary clipboard in X11 and Wayland.
1 parent 85f13c9 commit a060555

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

scripts/seek-to.lua

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -240,36 +240,33 @@ function set_inactive()
240240
active = false
241241
end
242242

243-
function subprocess(args)
244-
local cmd = {
245-
name = "subprocess",
246-
args = args,
247-
playback_only = false,
248-
capture_stdout = true
249-
}
250-
local res = mp.command_native(cmd)
251-
if not res.error then
252-
return res.stdout
253-
else
254-
msg.error("Error getting data from clipboard")
255-
return
256-
end
257-
end
258-
259-
function get_clipboard()
260-
local res
261-
if platform == "windows" then
262-
res = subprocess({ "powershell", "-Command", "Get-Clipboard", "-Raw" })
263-
elseif platform == "darwin" then
264-
res = subprocess({ "pbpaste" })
265-
elseif platform == "linux" then
266-
if os.getenv("WAYLAND_DISPLAY") then
267-
res = subprocess({ "wl-paste", "-n" })
268-
else
269-
res = subprocess({ "xclip", "-selection", "clipboard", "-out" })
243+
local function get_clipboard()
244+
if platform == "x11" then
245+
local res = utils.subprocess({
246+
args = { "xclip", "-selection", "clipboard", "-out" },
247+
playback_only = false,
248+
})
249+
if not res.error then
250+
return res.stdout
251+
end
252+
elseif platform == "wayland" then
253+
if mp.get_property("current-clipboard-backend") == "wayland" then
254+
return mp.get_property("clipboard/text", "")
255+
end
256+
-- Wayland VO clipboard is only updated on window focus
257+
if get_property_cached("focused") then
258+
return mp.get_property("clipboard/text", "")
259+
end
260+
local res = utils.subprocess({
261+
args = { "wl-paste", "-n" },
262+
playback_only = false,
263+
})
264+
if not res.error then
265+
return res.stdout
270266
end
267+
elseif platform == "windows" or platform == "darwin" then
268+
return mp.get_property("clipboard/text", "")
271269
end
272-
return res
273270
end
274271

275272
function paste_timestamp()

0 commit comments

Comments
 (0)