summaryrefslogtreecommitdiff
path: root/main.lua
blob: 71e313cb23a145b3edbe2df83437249b6810357f (plain) (blame)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
local utils = require('mp.utils')

local script_dir = mp.get_script_directory()
local stream_prefix = mp.get_opt('iptv_menu.stream_prefix')

-- font size is in units of osd height, which is scaled to 720
local font_size = 20
local osd = mp.create_osd_overlay('ass-events')
local osd_lines = math.floor((720 / font_size) + 0.5) - 1
local osd_padding = math.floor((720 - (osd_lines * font_size)) / 2)
local key_bindings = {}

local categories = {}
local streams = {}

local depth = 0
local menus = {}
local menu_top = {}
local menu_pos = {}

local function load_json_file(path)
	local f = io.open(script_dir .. '/' .. path, 'r')
	local data = f:read('*all')
	f:close()
	return utils.parse_json(data)
end

local function load_data()
	categories = load_json_file('categories.json')
	for _, v in ipairs(categories) do
		v.type = 'category'
	end

	streams = load_json_file('streams.json')
	for _, v in ipairs(streams) do
		v.type = 'stream'
	end
end

local function osd_menu_lines()
	if depth > 1 then
		return osd_lines - depth
	else
		return osd_lines
	end
end

local function update_osd()
	local out = {}

	if depth > 1 then
		for i = 1, depth - 1 do
			out[#out+1] = '{\\c&H999999&} ยป [' ..
				menus[i][menu_pos[i]].category_name .. ']{\\c}'
		end
		out[#out+1] = ' ' -- space character for correct line height
	end

	for i = menu_top[depth], math.min(
			menu_top[depth] + osd_menu_lines() - 1,
			#menus[depth]) do
		local opt = menus[depth][i]
		local str

		if opt.type == 'category' then
			str = '[' .. opt.category_name .. ']'
		else
			str = opt.name
		end

		if i == menu_pos[depth] then
			str = '{\\c&HFF00&}* ' .. str .. '{\\c}'
		elseif opt.type == 'category' then
			str = '{\\c&H99DDFF&}' .. str .. '{\\c}'
		end

		out[#out+1] = str
	end

	-- \q2 disables line wrapping
	osd.data = '{\\q2}{\\fs' .. font_size .. '}{\\pos(' .. osd_padding ..
		',' .. osd_padding .. '}' .. table.concat(out, '\\N')
	osd:update()
end

local function advance_cursor(n, opts)
	pos = math.max(1, math.min(menu_pos[depth] + n, #menus[depth]))
	top = menu_top[depth]
	if opts and opts.advance_page then
		top = top + n
	end

	-- move page to keep selected option visible
	if pos < top then
		top = pos
	elseif pos > top + osd_menu_lines() - 1 then
		top = pos - osd_menu_lines() + 1
	end

	top = math.max(1, math.min(top, #menus[depth] - osd_menu_lines() + 1))

	menu_pos[depth] = pos
	menu_top[depth] = top
	update_osd()
end

local function next_option()
	advance_cursor(1)
end

local function prev_option()
	advance_cursor(-1)
end

local function next_page()
	advance_cursor(osd_menu_lines(), {advance_page=true})
end

local function prev_page()
	advance_cursor(-osd_menu_lines(), {advance_page=true})
end

local function first_option()
	advance_cursor(-math.huge)
end

local function last_option()
	advance_cursor(math.huge)
end

local function add_category_menu(category_id)
	local menu = {}
	for _, v in ipairs(categories) do
		if tostring(v.parent_id) == category_id then
			menu[#menu+1] = v
		end
	end
	for _, v in ipairs(streams) do
		if v.category_id == category_id then
			menu[#menu+1] = v
		end
	end

	depth = depth + 1
	menus[depth] = menu
	menu_top[depth] = 1
	menu_pos[depth] = 1
	update_osd()
end

local function play_stream(stream_id)
	local url = stream_prefix .. stream_id
	mp.commandv('loadfile', url)
end

local function select_option()
	local opt = menus[depth][menu_pos[depth]]

	if opt.type == 'category' then
		add_category_menu(opt.category_id)
	else
		play_stream(opt.stream_id)
	end
end

local function prev_menu()
	if depth > 1 then
		depth = depth - 1
		update_osd()
	end
end

local function bind_key(key, name, func, opts)
	key_bindings[#key_bindings+1] = name
	mp.add_forced_key_binding(key, name, func, opts)
end

local function unbind_keys()
	for _, name in ipairs(key_bindings) do
		mp.remove_key_binding(name)
	end
	key_bindings = {}
end

local function bind_keys()
	local repeatable = {repeatable=true}
	bind_key('DOWN', 'next-option', next_option, repeatable)
	bind_key('UP', 'prev-option', prev_option, repeatable)
	bind_key('PGDWN', 'next-page', next_page, repeatable)
	bind_key('PGUP', 'prev-page', prev_page, repeatable)
	bind_key('HOME', 'first-option', first_option)
	bind_key('END', 'last-option', last_option)
	bind_key('ENTER', 'select-option', select_option)
	bind_key('BS', 'prev-menu', prev_menu)
end

local function toggle_menu()
	osd.hidden = not osd.hidden
	osd:update()

	if osd.hidden then
		unbind_keys()
	else
		bind_keys()
	end
end

mp.add_forced_key_binding('TAB', 'toggle-menu', toggle_menu)
bind_keys()
load_data()
add_category_menu('0')
update_osd()