Skip to content

Commit

Permalink
console.lua: use native clipboard property on win32 and wayland
Browse files Browse the repository at this point in the history
Since native clipboard is implemented on win32 and wayland,
use the clipboard property instead.

This fixes the problems with commandline implementations of clipboard:
- On win32, the powershell implementation is complex, and it can take
  several seconds to run for the first time.
- On wayland, it requires wl-paste to be installed, which isn't always
  available. It also works poorly on GNOME.
  • Loading branch information
na-na-hi committed Nov 23, 2024
1 parent 33a05c0 commit 1839279
Showing 1 changed file with 5 additions and 24 deletions.
29 changes: 5 additions & 24 deletions player/lua/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,10 @@ local function get_clipboard(clip)
return res.stdout
end
elseif platform == 'wayland' then
-- Wayland clipboard is only updated on window focus
if clip and mp.get_property_native('focused') then
return mp.get_property('clipboard/text', '')
end
local res = utils.subprocess({
args = { 'wl-paste', clip and '-n' or '-np' },
playback_only = false,
Expand All @@ -1104,30 +1108,7 @@ local function get_clipboard(clip)
return res.stdout
end
elseif platform == 'windows' then
local res = utils.subprocess({
args = { 'powershell', '-NoProfile', '-Command', [[& {
Trap {
Write-Error -ErrorRecord $_
Exit 1
}
$clip = ""
if (Get-Command "Get-Clipboard" -errorAction SilentlyContinue) {
$clip = Get-Clipboard -Raw -Format Text -TextFormatType UnicodeText
} else {
Add-Type -AssemblyName PresentationCore
$clip = [Windows.Clipboard]::GetText()
}
$clip = $clip -Replace "`r",""
$u8clip = [System.Text.Encoding]::UTF8.GetBytes($clip)
[Console]::OpenStandardOutput().Write($u8clip, 0, $u8clip.Length)
}]] },
playback_only = false,
})
if not res.error then
return res.stdout
end
return mp.get_property('clipboard/text', '')
elseif platform == 'darwin' then
local res = utils.subprocess({
args = { 'pbpaste' },
Expand Down

0 comments on commit 1839279

Please sign in to comment.