-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdisplay-name.lua
129 lines (102 loc) · 4.22 KB
/
display-name.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
--[[
A simple script designed for Windows that saves the name of the monitor that mpv is using into
the `display_name` field of the `user-data` property.
Only works with mpv v0.36+. See repo history for older versions that used shared-script-properties.
This means that one can use conditional auto profiles with the name of the monitor:
[PC]
profile-cond= 'SAMSUNG' or user_data.display_name ~= 'SAMSUNG'
script-opts-append=changerefresh-auto=no
[TV]
profile-cond= user_data.display_name == 'SAMSUNG'
script-opts-append=changerefresh-auto=yes
Run `mpv --idle=once --script-opts=display_names=yes` to get a list of names for the current displays.
This is necessary on windows because the default display names that mpv uses
are in the form \\.\DISPLAY#, which are completely useless for setting persistent profiles
as the numbers can change between boots or display configurations.
This script requires that MultiMonitorTool.exe (https://www.nirsoft.net/utils/multi_monitor_tool.html)
be available in the system path.
Available at: https://github.com/CogentRedTester/mpv-scripts/blob/master/display-name.lua
]]
local mp = require 'mp'
local msg = require 'mp.msg'
local utils = require 'mp.utils'
-- this platform test was taken from mpv's console.lua
local PLATFORM_WINDOWS = mp.get_property_native('options/vo-mmcss-profile', mp) ~= mp
-- a table of displays, uses the ID names returned by mpv's display-names property
local displays = {}
-- gets the path of a temporary file that can be used by the script
local function get_temp_file_name()
local file = os.tmpname()
if not PLATFORM_WINDOWS then return file
else return utils.join_path(mp.command_native({"expand-path", "~/AppData/Local/Temp"}), file) end
end
-- creates an iterator for cells in a csv row
local function csv_iter(str)
-- cleans up cells that are wrappen in brackets
str = str:gsub('".-"', function(substr) return substr:gsub(', ', 'x'):gsub(',', ' '):sub(2, -2) end)
return string.gmatch(str, '[^,\n\r]+')
end
-- loads the display information into the displays table
local function load_display_info()
local name = get_temp_file_name()
local cmd = mp.command_native({
name = 'subprocess',
playback_only = false,
capture_stdout = true,
args = {'MultiMonitorTool.exe', '/scomma', name}
})
mp.register_event('shutdown', function()
msg.debug('deleting', name)
os.remove(name)
end)
if cmd.status ~= 0 then
msg.error('failed to run MultiMonitorTool.exe. Status code:', cmd.status)
return false
end
local f = io.open(name, "r")
if not f then return msg.error('failed to open file ', name) end
local header_str = f:read("*l")
local headers = {}
for header in csv_iter(header_str) do
table.insert(headers, header)
end
for row in f:lines() do
local i = 1
local display = {}
for cell in csv_iter(row) do
-- print(headers[i] or '', cell)
display[headers[i]] = cell
i = i + 1
end
msg.debug(utils.to_string(display))
if not display.Name then return msg.error('display did not return a name') end
displays[display.Name] = display
end
end
mp.observe_property('display-names', 'native', function(_, display_names)
if not display_names then return end
local display = display_names[1]
if not display then
mp.set_property('user-data/display_name', '')
return
end
-- this script should really only be used on windows, but just in case I'll leave this here
if not PLATFORM_WINDOWS then
mp.set_property('user-data/display_name', display)
return
end
if not displays[display] then
load_display_info()
end
local name = 'unknown'
if displays and displays[display] then name = displays[display]['Monitor Name'] or name end
mp.set_property('user-data/display_name', name)
end)
mp.set_property('user-data/display_name', '')
-- prints the names of the current displays to the console
if mp.get_opt('display_names') then
load_display_info()
for name, display in pairs(displays) do
msg.info(name, display['Monitor Name'])
end
end