summaryrefslogtreecommitdiff
path: root/main.lua
blob: 586d5366eecd3bbc7dd4fd71d3ffba652d17fde1 (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
local utils = require('mp.utils')

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

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 osd = mp.create_osd_overlay('ass-events')
local osd_lines = 23

local categories = load_json_file('categories.json')
local streams = load_json_file('streams.json')

local depth = 1
local menus = {categories}
local menu_top = {1}
local menu_pos = {1}

function osd_menu_lines()
	return osd_lines - depth + 1
end

function update_osd()
	local out = {}
	for i = 1, depth - 1 do
		out[#out+1] = '{\\c&H999999&}[' ..
			menus[i][menu_pos[i]]['category_name'] .. ']{\\c}'
	end
	for i = menu_top[depth], math.min(
			menu_top[depth] + osd_menu_lines() - 1,
			#menus[depth]) do
		local key = (depth == 1 and 'category_name' or 'name')
		local str = menus[depth][i][key]
		if i == menu_pos[depth] then
			str = '{\\c&HFF00&}* ' .. str .. '{\\c}'
		end
		out[#out+1] = str
	end

	osd.data = '{\\q2}' .. table.concat(out, '\\N')
	osd:update()
end

function handle_up()
	if menu_pos[depth] <= 1 then
		return
	end

	menu_pos[depth] = menu_pos[depth] - 1
	if menu_pos[depth] < menu_top[depth] then
		menu_top[depth] = menu_top[depth] - 1
	end

	update_osd()
end

function handle_down()
	if menu_pos[depth] >= #menus[depth] then
		return
	end

	menu_pos[depth] = menu_pos[depth] + 1
	if menu_pos[depth] > menu_top[depth] + osd_menu_lines() - 1 then
		menu_top[depth] = menu_top[depth] + 1
	end

	update_osd()
end

function handle_page_up()
	menu_pos[depth] = math.max(menu_pos[depth] - osd_menu_lines(), 1)
	menu_top[depth] = math.max(menu_top[depth] - osd_menu_lines(), 1)

	update_osd()
end

function handle_page_down()
	menu_pos[depth] = math.min(menu_pos[depth] + osd_menu_lines(),
		#menus[depth])
	menu_top[depth] = math.min(menu_top[depth] + osd_menu_lines(),
		math.max(1, #menus[depth] - osd_menu_lines() + 1))

	update_osd()
end

function select_category()
	local cat = categories[menu_pos[depth]]
	local menu = {}
	for _, v in ipairs(streams) do
		if v['category_id'] == cat['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

function select_stream()
	local url = stream_prefix .. menus[depth][menu_pos[depth]]['stream_id']
	mp.commandv('loadfile', url)
end

function handle_enter()
	if depth == 1 then
		select_category()
	else
		select_stream()
	end
end

function handle_backspace()
	if depth > 1 then
		depth = depth - 1
	end

	update_osd()
end

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

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

local key_bindings = {}

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

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

function bind_keys()
	local repeatable = {repeatable=true}
	bind_key('UP', handle_up, repeatable)
	bind_key('DOWN', handle_down, repeatable)
	bind_key('PGUP', handle_page_up, repeatable)
	bind_key('PGDWN', handle_page_down, repeatable)
	bind_key('ENTER', handle_enter)
	bind_key('BS', handle_backspace)
end

mp.add_forced_key_binding('TAB', 'toggle-menu', toggle_menu)
bind_keys()

update_osd()