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
|
/*
* This file is part of MPlayer.
*
* MPlayer is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* MPlayer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include "mp_msg.h"
#include "m_struct.h"
#include "m_option.h"
#include "m_property.h"
#include "asxparser.h"
#include "libmpcodecs/mp_image.h"
#include "menu.h"
#include "menu_list.h"
#include "input/input.h"
#include "command.h"
struct list_entry_s {
struct list_entry p;
char* name;
char* txt;
char* prop;
m_option_t* opt;
char* menu;
int auto_update;
};
struct menu_priv_s {
menu_list_priv_t p;
char* ptr;
int edit;
/// Cfg fields
char* na;
int hide_na;
};
static struct menu_priv_s cfg_dflt = {
MENU_LIST_PRIV_DFLT,
NULL,
0,
"N/A",
1
};
static const m_option_t cfg_fields[] = {
MENU_LIST_PRIV_FIELDS,
{ "title", M_ST_OFF(menu_list_priv_t,title), CONF_TYPE_STRING, 0, 0, 0, NULL },
{ "na", M_ST_OFF(struct menu_priv_s,na), CONF_TYPE_STRING, 0, 0, 0, NULL },
{ "hide-na", M_ST_OFF(struct menu_priv_s,hide_na), CONF_TYPE_FLAG, CONF_RANGE, 0, 1, NULL },
{ NULL, NULL, NULL, 0,0,0,NULL }
};
#define mpriv (menu->priv)
static void entry_set_text(menu_t* menu, list_entry_t* e) {
char* val = e->txt ? property_expand_string(menu->ctx, e->txt) :
mp_property_print(e->prop, menu->ctx);
int l,edit = (mpriv->edit && e == mpriv->p.current);
if(!val || !val[0]) {
free(val);
if(mpriv->hide_na) {
e->p.hide = 1;
return;
}
val = strdup(mpriv->na);
} else if(mpriv->hide_na)
e->p.hide = 0;
l = strlen(e->name) + 2 + strlen(val) + (edit ? 4 : 0) + 1;
free(e->p.txt);
e->p.txt = malloc(l);
sprintf(e->p.txt,"%s: %s%s%s",e->name,edit ? "> " : "",val,edit ? " <" : "");
free(val);
}
static void update_entries(menu_t* menu, int auto_update) {
list_entry_t* e;
for(e = mpriv->p.menu ; e ; e = e->p.next)
if ((e->txt || e->prop) && (!auto_update || e->auto_update))
entry_set_text(menu, e);
}
static int parse_args(menu_t* menu,char* args) {
char *element,*body, **attribs, *name, *txt, *auto_update;
list_entry_t* m = NULL;
int r;
m_option_t* opt;
ASX_Parser_t* parser = asx_parser_new(menu->mconfig);
while(1) {
r = asx_get_element(parser,&args,&element,&body,&attribs);
if(r < 0) {
mp_tmsg(MSGT_OSD_MENU,MSGL_ERR,"[MENU] syntax error at line: %d\n",parser->line);
asx_parser_free(parser);
return -1;
} else if(r == 0) {
asx_parser_free(parser);
if(!m)
mp_tmsg(MSGT_OSD_MENU,MSGL_WARN,"[MENU] No entry found in the menu definition.\n");
m = calloc(1,sizeof(struct list_entry_s));
m->p.txt = strdup("Back");
menu_list_add_entry(menu,m);
return 1;
}
if(!strcmp(element,"menu")) {
name = asx_get_attrib("menu",attribs);
if(!name) {
mp_tmsg(MSGT_OSD_MENU,MSGL_WARN,"[MENU] Submenu definition needs a 'menu' attribute.\n");
goto next_element;
}
m = calloc(1,sizeof(struct list_entry_s));
m->menu = name;
name = NULL; // we want to keep it
m->p.txt = asx_get_attrib("name",attribs);
if(!m->p.txt) m->p.txt = strdup(m->menu);
menu_list_add_entry(menu,m);
goto next_element;
}
name = asx_get_attrib("property",attribs);
opt = NULL;
if(name && mp_property_do(name,M_PROPERTY_GET_TYPE,&opt,menu->ctx) <= 0) {
mp_tmsg(MSGT_OSD_MENU,MSGL_WARN,"[MENU] Invalid property '%s' in pref menu entry. (line %d).\n",
name,parser->line);
goto next_element;
}
txt = asx_get_attrib("txt",attribs);
if(!(name || txt)) {
mp_tmsg(MSGT_OSD_MENU,MSGL_WARN,"[MENU] Pref menu entry definitions need a valid 'property' or 'txt' attribute (line %d).\n",parser->line);
free(txt);
txt = NULL;
goto next_element;
}
m = calloc(1,sizeof(struct list_entry_s));
m->opt = opt;
m->txt = txt; txt = NULL;
m->prop = name; name = NULL;
m->name = asx_get_attrib("name",attribs);
if(!m->name) m->name = strdup(opt ? opt->name : "-");
auto_update = asx_get_attrib("auto-update", attribs);
if (auto_update) {
if (!strcmp(auto_update, "1") ||
!strcasecmp(auto_update, "on") ||
!strcasecmp(auto_update, "yes") ||
!strcasecmp(auto_update, "true"))
m->auto_update = 1;
free(auto_update);
}
entry_set_text(menu,m);
menu_list_add_entry(menu,m);
next_element:
free(element);
free(body);
free(name);
asx_free_attribs(attribs);
}
}
static void read_cmd(menu_t* menu,int cmd) {
list_entry_t* e = mpriv->p.current;
if(e->opt) {
switch(cmd) {
case MENU_CMD_UP:
if(!mpriv->edit) break;
case MENU_CMD_RIGHT:
if(mp_property_do(e->prop,M_PROPERTY_STEP_UP,NULL,menu->ctx) > 0)
update_entries(menu, 0);
return;
case MENU_CMD_DOWN:
if(!mpriv->edit) break;
case MENU_CMD_LEFT:
if(mp_property_do(e->prop,M_PROPERTY_STEP_DOWN,NULL,menu->ctx) > 0)
update_entries(menu, 0);
return;
case MENU_CMD_OK:
// check that the property is writable
if(mp_property_do(e->prop,M_PROPERTY_SET,NULL,menu->ctx) < 0) return;
// shortcut for flags
if(e->opt->type == CONF_TYPE_FLAG) {
if(mp_property_do(e->prop,M_PROPERTY_STEP_UP,NULL,menu->ctx) > 0)
update_entries(menu, 0);
return;
}
// switch
mpriv->edit = !mpriv->edit;
// update the menu
update_entries(menu, 0);
// switch the pointer
if(mpriv->edit) {
mpriv->ptr = mpriv->p.ptr;
mpriv->p.ptr = NULL;
} else
mpriv->p.ptr = mpriv->ptr;
return;
case MENU_CMD_CANCEL:
if(!mpriv->edit) break;
mpriv->edit = 0;
update_entries(menu, 0);
mpriv->p.ptr = mpriv->ptr;
return;
}
} else if(e->menu) {
switch(cmd) {
case MENU_CMD_RIGHT:
case MENU_CMD_OK: {
mp_cmd_t* c;
char* txt = malloc(10 + strlen(e->menu) + 1);
sprintf(txt,"set_menu %s",e->menu);
c = mp_input_parse_cmd(txt);
if(c) mp_input_queue_cmd(menu->input_ctx, c);
return;
}
}
} else {
switch(cmd) {
case MENU_CMD_RIGHT:
case MENU_CMD_OK:
menu->show = 0;
menu->cl = 1;
return;
}
}
menu_list_read_cmd(menu,cmd);
}
static void free_entry(list_entry_t* entry) {
free(entry->p.txt);
free(entry->name);
free(entry->txt);
free(entry->prop);
free(entry->menu);
free(entry);
}
static void closeMenu(menu_t* menu) {
menu_list_uninit(menu,free_entry);
}
static void menu_pref_draw(menu_t* menu, mp_image_t* mpi) {
update_entries(menu, 1);
menu_list_draw(menu, mpi);
}
static int openMenu(menu_t* menu, char* args) {
menu->draw = menu_pref_draw;
menu->read_cmd = read_cmd;
menu->close = closeMenu;
if(!args) {
mp_tmsg(MSGT_OSD_MENU,MSGL_ERR,"[MENU] Pref menu needs an argument.\n");
return 0;
}
menu_list_init(menu);
return parse_args(menu,args);
}
const menu_info_t menu_info_pref = {
"Preferences menu",
"pref",
"Albeu",
"",
{
"pref_cfg",
sizeof(struct menu_priv_s),
&cfg_dflt,
cfg_fields
},
openMenu
};
|