-
Notifications
You must be signed in to change notification settings - Fork 1
/
oled-screensaver.lua
157 lines (138 loc) · 4.43 KB
/
oled-screensaver.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
148
149
150
151
152
153
154
155
156
157
local assdraw = require "mp.assdraw"
local options = require "mp.options"
local o = {
startAfter = 10,
screensaverColor = "000000",
rainbow = false,
rainbowStep = 1,
rainbowRedrawPrediod = 0.03,
alphaStep = 12,
luminance = 255,
}
local state = {
startScreensaverAfterTimer = nil,
mouseMovementTimer = nil,
drawScreensaverTimer = nil,
lastMouseXPos = nil,
lastMouseYPos = nil,
colour = {o.luminance, 0, 0}, -- RGB
alpha = 255,
paused = false,
fullscreen = false,
}
options.read_options(o)
function changeColour(num, op)
if op == "+" then
state.colour[num] = state.colour[num] + o.rainbowStep
if state.colour[num] > o.luminance then
state.colour[num] = o.luminance
end
elseif op == "-" then
state.colour[num] = state.colour[num] - o.rainbowStep
if state.colour[num] < 0 then
state.colour[num] = 0
end
end
end
function alphaHex()
if state.alpha > 0 then
state.alpha = state.alpha - o.alphaStep
if state.alpha < 0 then
state.alpha = 0
end
end
return string.format("%02x", state.alpha)
end
function rainbowHex()
if state.colour[1] >= o.luminance and state.colour[2] < o.luminance and state.colour[3] <= 0 then
changeColour(2, "+")
elseif state.colour[1] > 0 and state.colour[2] >= o.luminance and state.colour[3] <= 0 then
changeColour(1, "-")
elseif state.colour[1] <= 0 and state.colour[2] >= o.luminance and state.colour[3] < o.luminance then
changeColour(3, "+")
elseif state.colour[1] <= 0 and state.colour[2] > 0 and state.colour[3] >= o.luminance then
changeColour(2, "-")
elseif state.colour[1] < o.luminance and state.colour[2] <= 0 and state.colour[3] >= o.luminance then
changeColour(1, "+")
elseif state.colour[1] >= o.luminance and state.colour[2] <= 0 and state.colour[3] > 0 then
changeColour(3, "-")
end
-- ASS Sub colours BBGGRR
return string.format("%02x%02x%02x", state.colour[3], state.colour[2], state.colour[1])
end
function startScreensaver()
state.drawScreensaverTimer:resume()
mp.add_forced_key_binding("mouse_move", "screensaver_mouse_move", clearEvent)
state.mouseMovementTimer:kill()
end
function drawScreensaver()
local colour = o.screensaverColor
if o.rainbow == true then
colour = rainbowHex()
elseif state.alpha < 1 then
state.drawScreensaverTimer:kill()
end
local w, h = mp.get_osd_size()
ass = assdraw.ass_new()
ass:new_event()
ass:append("{\\c&H" .. colour .. "&}")
ass:append("{\\{\\bord0}")
ass:append("{\\shad0}")
ass:append("{\\1a&H" .. alphaHex() .. "&}")
ass:append("{\\2a&HFF&}")
ass:append("{\\3a&HFF&}")
ass:append("{\\4a&HFF&}")
ass:pos(-1, -1)
ass:draw_start()
ass:rect_cw(0, 0, w+2, h+2)
ass:draw_stop()
mp.set_osd_ass(w, h, ass.text)
end
function clearScreensaver()
state.startScreensaverAfterTimer:kill()
state.drawScreensaverTimer:kill()
state.mouseMovementTimer:kill()
state.alpha = 255
mp.set_osd_ass(0, 0, "")
mp.remove_key_binding("screensaver_mouse_move")
end
function checkMouseMovement()
local x, y = mp.get_mouse_pos()
if x == state.lastMouseXPos or y == state.lastMouseYPos then
state.startScreensaverAfterTimer:resume()
else
state.startScreensaverAfterTimer:kill()
end
state.lastMouseXPos = x
state.lastMouseYPos = y
end
function clearEvent(name, clear)
clearScreensaver()
if state.paused and state.fullscreen then
state.mouseMovementTimer:resume()
end
end
function fullscreenEvent(name, fs)
state.fullscreen = fs
updateState()
end
function pauseEvent(name, pause)
state.paused = pause
updateState()
end
function updateState()
if state.paused and state.fullscreen then
state.mouseMovementTimer:resume()
else
clearScreensaver()
end
end
state.startScreensaverAfterTimer = mp.add_timeout(o.startAfter, startScreensaver)
state.startScreensaverAfterTimer:kill()
state.drawScreensaverTimer = mp.add_periodic_timer(o.rainbowRedrawPrediod, drawScreensaver)
state.drawScreensaverTimer:kill()
state.mouseMovementTimer = mp.add_periodic_timer(0.5, checkMouseMovement)
state.mouseMovementTimer:kill()
mp.observe_property("pause", "bool", pauseEvent)
mp.observe_property("fullscreen", "bool", fullscreenEvent)
mp.observe_property("seeking", "bool", clearEvent)