summaryrefslogtreecommitdiff
path: root/main.lua
blob: a9d3cfb4c42ec7d54ecc20285309e1b392ce60f9 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
-- Copyright 2025 David Vazgenovich Shakaryan

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 osd_cursor_glyph = '{\\p1\\pbo' .. math.floor(font_size / 5) .. '}' ..
	'm 0 0 l ' .. math.ceil(font_size / 32) .. ' 0 ' ..
	math.ceil(font_size / 32) .. ' ' .. font_size ..
	' 0 ' .. font_size .. '{\\p0}'
local osd_bg = mp.create_osd_overlay('ass-events')
osd_bg.z = -1
osd_bg.data = '{\\alpha&H44&\\c&H&\\pos(0,0)}' ..
	'{\\p1}m 0 0 l 9999 0 9999 720 0 720{\\p0}'

local categories = {}
local streams = {}
local playing_stream_id

local depth = 0
local menus = {}
local key_bindings = {}

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'
		v.name = v.category_name
	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
		-- leaves an extra line for padding between titles and options
		return osd_lines - depth
	else
		return osd_lines
	end
end

local function update_osd()
	local out = {}

	if depth > 1 then
		for i = 2, depth do
			local col = '999999'
			if menus[i].search_active then
				col = 'FF00'
			end
			out[#out+1] = '{\\c&H' .. col .. '&} » [' ..
				menus[i].title .. ']{\\c}'
		end
		out[#out+1] = ' ' -- space character for correct line height
	end

	local menu = menus[depth]
	for i = menu.page_pos, math.min(
			menu.page_pos + osd_menu_lines() - 1, #menu.options) do
		local opt = menu.options[i]
		local str = opt.name
		local col = '{\\c}'
		local icons = ''

		if i == menu.cursor_pos and not menu.search_active then
			col = '{\\c&HFF00&}'
			icons = col .. '› ' .. icons
		elseif opt.type == 'category' then
			col = '{\\c&H99DDFF&}'
		end

		if opt.hl then
			local buf = ''
			local n = 0
			for _, hl in ipairs(opt.hl) do
				buf = buf .. col ..
					str:sub(n + 1, hl.m_start - 1) ..
					'{\\c&HFFDD}' ..
					str:sub(hl.m_start, hl.m_end)
				n = hl.m_end
			end
			str = buf .. col .. str:sub(n + 1)
		else
			str = col .. str
		end

		if opt.type == 'category' then
			str = col .. '[' .. str .. ']'
		elseif opt.stream_id == playing_stream_id then
			icons = icons .. '{\\c&HFF6633&&}\226\143\186 '
		end

		if opt.path and #opt.path > 0 then
			local path = '{\\c&H666666&&}'
			for i = #opt.path, 1, -1 do
				local node = opt.path[i]
				path = path .. ' « [' .. node .. ']'
			end
			str = str .. path
		end

		out[#out+1] = icons .. str
	end

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

local function advance_cursor(n, opts)
	local menu = menus[depth]
	local lines = osd_menu_lines()

	local pos = math.max(1, math.min(menu.cursor_pos + n, #menu.options))
	local top = menu.page_pos
	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 + lines - 1 then
		top = pos - lines + 1
	end

	top = math.max(1, math.min(top, #menu.options - lines + 1))

	menu.cursor_pos = pos
	menu.page_pos = 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 push_menu(t)
	local menu = {
		options={},
		title=title,
		cursor_pos=1,
		page_pos=1,
	}

	if t then
		for k, v in pairs(t) do
			menu[k] = v
		end
	end

	depth = depth + 1
	menus[depth] = menu
end

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

local function add_category_menu(category_id, category_name)
	push_menu({
		options=category_menu_options(category_id),
		title=category_name,
	})
	update_osd()
end

local function play_stream(stream_id)
	-- add a per-file option containing the stream_id, allowing it to be
	-- retrieved when a start-file event is received
	mp.commandv('loadfile', stream_prefix .. stream_id, 'replace', -1,
		'script-opt=iptv_menu.tmp.stream_id=' .. stream_id)
end

local function select_option()
	local menu = menus[depth]
	local opt = menu.options[menu.cursor_pos]

	if opt.type == 'category' then
		add_category_menu(opt.category_id, opt.name)
	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] = name
	mp.add_forced_key_binding(key, name, func, opts)
end

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

local function split_search_text()
	local menu = menus[depth]

	return menu.search_text:sub(1, menu.search_text_cursor_pos - 1),
		menu.search_text:sub(menu.search_text_cursor_pos)
end

local function search_update_osd()
	local bc, ac = split_search_text()
	menus[depth].title = 'Searching: ' .. bc .. osd_cursor_glyph .. ac
	update_osd()
end

local function copy_table(t)
	local u = {}
	for k, v in pairs(t) do
		u[k] = v
	end
	return u
end

local function search_menu_options_build(options, t, path)
	local menu = menus[depth]
	local path = path or {}

	for _, v in ipairs(options) do
		if not t[v.type] then
			t[v.type] = {}
		end

		local v = copy_table(v)
		v.path = path
		t[v.type][#t[v.type]+1] = v

		if v.type == 'category' then
			local path = copy_table(path)
			path[#path+1] = v.name
			search_menu_options_build(
				category_menu_options(v.category_id), t, path)
		end
	end
end

local function search_menu_options(options)
	local t = {}
	search_menu_options_build(options, t)

	local ret = t.category or {}
	if t.stream then
		for _, v in ipairs(t.stream) do
			ret[#ret+1] = v
		end
	end
	return ret
end

local function search_update()
	local menu = menus[depth]

	if #menu.search_text == 0 then
		menu.options = menu.search_options
		search_update_osd()
		return
	end

	local matches = {}
	for _, v in ipairs(menu.search_options) do
		local hl = {}

		local i, j = 0, 0
		while true do
			i, j = v.name:find(menu.search_text, j + 1, true)
			if not i then
				break
			end
			hl[#hl+1] = {m_start=i, m_end=j}
		end

		if #hl > 0 then
			local t = copy_table(v)
			t.hl = hl
			matches[#matches+1] = t
		end
	end

	menu.options = matches
	search_update_osd()
end

local function search_input(event)
	local menu = menus[depth]

	if event.event ~= 'down' and event.event ~= 'repeat' then
		return
	end

	local bc, ac = split_search_text()
	menu.search_text = bc .. event.key_text .. ac
	menu.search_text_cursor_pos = menu.search_text_cursor_pos + 1
	search_update()
end

local function search_backspace()
	local menu = menus[depth]

	if menu.search_text_cursor_pos == 1 then
		return
	end

	local bc, ac = split_search_text()
	menu.search_text = bc:sub(1, -2) .. ac
	menu.search_text_cursor_pos = menu.search_text_cursor_pos - 1
	search_update()
end

local function search_delete()
	local menu = menus[depth]

	if menu.search_text_cursor_pos == #menu.search_text + 1 then
		return
	end

	local bc, ac = split_search_text()
	menu.search_text = bc .. ac:sub(2)
	search_update()
end

local function search_cursor_set_pos(pos)
	local menu = menus[depth]

	local pos = math.max(1, math.min(#menu.search_text + 1, pos))
	if pos == menu.search_text_cursor_pos then
		return
	end

	menu.search_text_cursor_pos = pos
	search_update_osd()
end

local function search_cursor_left()
	search_cursor_set_pos(menus[depth].search_text_cursor_pos - 1)
end

local function search_cursor_right()
	search_cursor_set_pos(menus[depth].search_text_cursor_pos + 1)
end

local function search_cursor_start()
	search_cursor_set_pos(1)
end

local function search_cursor_end()
	search_cursor_set_pos(#menus[depth].search_text + 1)
end

local bind_search_keys
local bind_menu_keys

local function search_start()
	local menu = menus[depth]
	if menu.type == 'search' then
		menu.search_active = true
		menu.search_text_cursor_pos = #menu.search_text + 1
		menu.cursor_pos = 1
		menu.page_pos = 1
		search_update_osd()
	else
		push_menu({
			type='search',
			search_active=true,
			search_options=search_menu_options(menu.options),
			search_text='',
			search_text_cursor_pos=1,
		})
		search_update()
	end

	bind_search_keys()
end

local function search_finish()
	local menu = menus[depth]

	menu.search_active = false
	menu.title = 'Search results: ' .. menu.search_text
	update_osd()
	bind_menu_keys()
end

local function search_cancel()
	menus[depth].search_active = false
	depth = depth - 1
	update_osd()
	bind_menu_keys()
end

function bind_search_keys()
	unbind_keys()

	local r = {repeatable=true}
	bind_key('ANY_UNICODE', 'search-input', search_input, {complex=true})
	bind_key('ENTER', 'finish-search', search_finish)
	bind_key('ESC', 'cancel-search', search_cancel)
	bind_key('BS', 'search-backspace', search_backspace, r)
	bind_key('DEL', 'search-delete', search_delete, r)
	bind_key('LEFT', 'search-cursor-left', search_cursor_left, r)
	bind_key('RIGHT', 'search-cursor-right', search_cursor_right, r)
	bind_key('Ctrl+a', 'search-cursor-start', search_cursor_start)
	bind_key('Ctrl+e', 'search-cursor-end', search_cursor_end)
	bind_key('Ctrl+c', 'cancel-search-ctrl-c', search_cancel)
end

function bind_menu_keys()
	unbind_keys()

	local r = {repeatable=true}
	bind_key('DOWN', 'next-option', next_option, r)
	bind_key('UP', 'prev-option', prev_option, r)
	bind_key('PGDWN', 'next-page', next_page, r)
	bind_key('PGUP', 'prev-page', prev_page, r)
	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)
	bind_key('/', 'start-search', search_start)
end

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

	if osd.hidden then
		unbind_keys()
	elseif menus[depth].search_active then
		bind_search_keys()
	else
		bind_menu_keys()
	end
end

mp.register_event('start-file', function()
	playing_stream_id = tonumber(mp.get_opt('iptv_menu.tmp.stream_id'))
	update_osd()
end)

mp.register_event('end-file', function()
	playing_stream_id = nil
	update_osd()
end)

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