Skip to content

Instantly share code, notes, and snippets.

@phanirithvij
Last active March 24, 2020 14:03
Show Gist options
  • Save phanirithvij/203a47f16195aa0ed3aa4eaf99474595 to your computer and use it in GitHub Desktop.
Save phanirithvij/203a47f16195aa0ed3aa4eaf99474595 to your computer and use it in GitHub Desktop.
mpv js extension to save the last position, dimensions of the mpv windows. i.e. open same as before. Only WINDOWS
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Window {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ClientToScreen(IntPtr hWnd, ref POINT lpPoint);
}
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public struct POINT
{
public int x;
public int y;
}
"@
$Handle = (Get-Process -Id $Args[0]).MainWindowHandle
$ClientRect = New-Object RECT
$Position = New-Object POINT
$Size = New-Object POINT
[Window]::GetClientRect($Handle, [ref]$ClientRect) | out-null
$Position.x = 0 # $ClientRect.Left is always 0
$Position.y = 0 # $ClientRect.Top
$Size.x = $ClientRect.Right
$Size.y = $ClientRect.Bottom
[Window]::ClientToScreen($Handle, [ref]$Position) | out-null
Write-Output "$($Position.x) $($Position.y) $($Size.x) $($Size.y)"
// Modified by Phani Rithvij (github.com/phanirithvij)
// Extended from Pablo Bollansée's https://github.com/TheOddler/mpv-config
// Sometime the leftmost screen doesn't have id 0
// not sure yet how to detect this automatically
var leftMostScreen = 1
// Some setup used by both reading and writing
var dir = mp.utils.split_path(mp.get_script_file())[0]
var rect_path = mp.utils.join_path(dir, "last_window_rect.txt")
dump(rect_path)
// Read last window rect if present
var rect = null
try {
rect = mp.utils.read_file(rect_path).trim().split(' ')
} catch (e) {
dump(e)
}
if (rect != null) {
var x = rect[0]
var y = rect[1]
var width = rect[2]
var height = rect[3]
mp.set_property("screen", leftMostScreen)
var autofit = width + "x" + height
mp.set_property("autofit", autofit)
var geometry = width + "x" + height + "+" + x + "+" + y
mp.set_property("geometry", geometry)
// fetch the rect on load
mp.register_event("file-loaded", fetch_rect)
}
// fetch the screen dimensions
function fetch_rect() {
var ps1_script = mp.utils.join_path(dir, "Get-Client-Rect.ps1")
var args = ["powershell", "-File", ps1_script, mp.utils.getpid().toString()]
var output = mp.utils.subprocess({
args: args,
cancellable: false
}).stdout
var newRect = output.trim().split(' ')
var fact = 1.25
var i = 0;
newRect.forEach(function(_) {
newRect[i] *= fact
newRect[i] = Math.round(newRect[i])
i++
})
return newRect.join(" ")
}
// fetches and saves the dims
function fetchAndSave() {
var output = fetch_rect()
save_rect(output)
}
// Save the rect in a file
// Need to do this before exit to save last screen dims
function save_rect(rect) {
mp.utils.write_file("file://" + rect_path, rect)
}
// i.e. this
mp.register_event("shutdown", fetchAndSave)
@phanirithvij
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment