-
Notifications
You must be signed in to change notification settings - Fork 2
/
mpv.lua
147 lines (116 loc) · 3.75 KB
/
mpv.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
-- WORKAROUND find ldbus.so under ~/.config/mpv/scripts/
for path in package.path:gmatch(";([^;]+)") do
if path:match(".config/mpv/scripts") then
package.cpath = path:match("(.*)%.lua$") .. ".so;" .. package.cpath
end
end
local Applet = require("mpris.applet")
local pid = tostring(mp):match(': (%w+)$') -- FIXME
local mpris = Applet:new({ name = "mpv", id = 'instance' .. pid })
--- sync
mp.add_periodic_timer(0.1, function () mpris.dbus.poll() end)
mp.register_event('shutdown', function() mpris.dbus.exit() end)
--- methods
function mpris.options.next()
mp.command("playlist_next")
end
function mpris.options.previous()
mp.command("playlist_prev")
end
function mpris.options.stop()
mp.command("stop")
end
function mpris.options.pause()
mp.set_property_bool('pause', true)
end
function mpris.options.playpause()
local paused = mp.get_property_bool('pause')
mp.set_property_bool('pause', not paused)
end
function mpris.options.play()
if mp.get_property_bool('pause') then
mp.set_property_bool('pause', false)
end
end
function mpris.options.seek(offset)
if type(offset) == 'number' then
-- offset is in microseconds
mp.commandv("seek", offset / 1e6)
end
end
function mpris.options.openuri(uri)
if type(uri) == 'string' then
mp.commandv("loadfile", uri)
end
end
function mpris.options.quit()
mp.command("quit")
end
function mpris.options.onvolume(vol)
mp.set_property_number('volume', (vol or 0) * 100)
end
function mpris.options.onfullscreen(isfullscreen)
mp.set_property_bool('fullscreen', not not isfullscreen)
end
--- properties
local function update_volume(name, value)
local vol = (value or 0) * 0.01
if vol ~= mpris.property:get('volume') then
mpris.property:set('volume', vol)
end
end
local function update_pause(name, paused)
mpris.property:sets({
status = (paused and "Paused" or "Playing"),
pause = not paused,
play = paused,
})
end
local function update_idle(name, idle)
if idle then
mpris.property:sets({
status = "Stopped",
previous = false,
next = false,
pause = false,
play = false,
})
end
end
local function update_title(name, title)
local meta = mpris.property:get('metadata')
if title or title ~= '' then
meta['xesam:title'] = title
else
meta['xesam:title'] = nil
end
mpris.property:set('metadata', meta)
end
local function update_length(name, len)
local meta = mpris.property:get('metadata')
if len then
meta['mpris:length'] = math.floor(len * 1e6) -- microseconds
else
meta['mpris:length'] = nil
end
mpris.property:set('metadata', meta)
end
local function update_fullscreen(name, isfullscreen)
mpris.property:set('fullscreen', isfullscreen)
end
update_volume('volume', mp.get_property_number('volume'))
mp.observe_property("volume", 'number', update_volume)
update_pause('pause', mp.get_property_bool('pause'))
mp.observe_property("pause", 'bool', update_pause)
update_idle('idle', mp.get_property_bool('idle'))
mp.observe_property("idle", 'bool', update_idle)
-- will be set later. always.
mp.observe_property("metadata/icy-title", 'string', update_title)
mp.observe_property("media-title", 'string', update_title)
update_length('length', mp.get_property_number('length'))
mp.observe_property("length", 'number', update_length)
update_fullscreen('fullscreen', mp.get_property_bool('fullscreen'))
mp.observe_property("fullscreen", 'bool', update_fullscreen)
mpris.property:set('setfullscreen', true)
mpris.property:set('urischemes', mp.get_protocols and mp.get_protocols() or {})
mpris.property:set('mimetypes', mp.get_mimetypes and mp.get_mimetypes() or {}) -- TODO