Fix vf_tcdump's compilation
[mplayer/kovensky.git] / libmenu / menu_param.c
blobf1836dffe0e23d1e860081199d29a9e278fa8442
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <dirent.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <ctype.h>
30 #include "mp_msg.h"
32 #include "m_struct.h"
33 #include "m_option.h"
34 #include "m_property.h"
35 #include "asxparser.h"
37 #include "libmpcodecs/mp_image.h"
39 #include "menu.h"
40 #include "menu_list.h"
41 #include "input/input.h"
42 #include "command.h"
44 struct list_entry_s {
45 struct list_entry p;
46 char* name;
47 char* txt;
48 char* prop;
49 m_option_t* opt;
50 char* menu;
51 int auto_update;
54 struct menu_priv_s {
55 menu_list_priv_t p;
56 char* ptr;
57 int edit;
58 /// Cfg fields
59 char* na;
60 int hide_na;
63 static struct menu_priv_s cfg_dflt = {
64 MENU_LIST_PRIV_DFLT,
65 NULL,
67 "N/A",
71 static m_option_t cfg_fields[] = {
72 MENU_LIST_PRIV_FIELDS,
73 { "title", M_ST_OFF(menu_list_priv_t,title), CONF_TYPE_STRING, 0, 0, 0, NULL },
74 { "na", M_ST_OFF(struct menu_priv_s,na), CONF_TYPE_STRING, 0, 0, 0, NULL },
75 { "hide-na", M_ST_OFF(struct menu_priv_s,hide_na), CONF_TYPE_FLAG, CONF_RANGE, 0, 1, NULL },
76 { NULL, NULL, NULL, 0,0,0,NULL }
79 #define mpriv (menu->priv)
81 static void entry_set_text(menu_t* menu, list_entry_t* e) {
82 char* val = e->txt ? property_expand_string(menu->ctx, e->txt) :
83 mp_property_print(e->prop, menu->ctx);
84 int l,edit = (mpriv->edit && e == mpriv->p.current);
85 if(!val || !val[0]) {
86 if(val) free(val);
87 if(mpriv->hide_na) {
88 e->p.hide = 1;
89 return;
91 val = strdup(mpriv->na);
92 } else if(mpriv->hide_na)
93 e->p.hide = 0;
94 l = strlen(e->name) + 2 + strlen(val) + (edit ? 4 : 0) + 1;
95 if(e->p.txt) free(e->p.txt);
96 e->p.txt = malloc(l);
97 sprintf(e->p.txt,"%s: %s%s%s",e->name,edit ? "> " : "",val,edit ? " <" : "");
98 free(val);
101 static void update_entries(menu_t* menu, int auto_update) {
102 list_entry_t* e;
103 for(e = mpriv->p.menu ; e ; e = e->p.next)
104 if ((e->txt || e->prop) && (!auto_update || e->auto_update))
105 entry_set_text(menu, e);
108 static int parse_args(menu_t* menu,char* args) {
109 char *element,*body, **attribs, *name, *txt, *auto_update;
110 list_entry_t* m = NULL;
111 int r;
112 m_option_t* opt;
113 ASX_Parser_t* parser = asx_parser_new(menu->mconfig);
116 while(1) {
117 r = asx_get_element(parser,&args,&element,&body,&attribs);
118 if(r < 0) {
119 mp_tmsg(MSGT_OSD_MENU,MSGL_ERR,"[MENU] syntax error at line: %d\n",parser->line);
120 asx_parser_free(parser);
121 return -1;
122 } else if(r == 0) {
123 asx_parser_free(parser);
124 if(!m)
125 mp_tmsg(MSGT_OSD_MENU,MSGL_WARN,"[MENU] No entry found in the menu definition.\n");
126 m = calloc(1,sizeof(struct list_entry_s));
127 m->p.txt = strdup("Back");
128 menu_list_add_entry(menu,m);
129 return 1;
131 if(!strcmp(element,"menu")) {
132 name = asx_get_attrib("menu",attribs);
133 if(!name) {
134 mp_tmsg(MSGT_OSD_MENU,MSGL_WARN,"[MENU] Submenu definition needs a 'menu' attribute.\n");
135 goto next_element;
137 m = calloc(1,sizeof(struct list_entry_s));
138 m->menu = name;
139 name = NULL; // we want to keep it
140 m->p.txt = asx_get_attrib("name",attribs);
141 if(!m->p.txt) m->p.txt = strdup(m->menu);
142 menu_list_add_entry(menu,m);
143 goto next_element;
146 name = asx_get_attrib("property",attribs);
147 opt = NULL;
148 if(name && mp_property_do(name,M_PROPERTY_GET_TYPE,&opt,menu->ctx) <= 0) {
149 mp_tmsg(MSGT_OSD_MENU,MSGL_WARN,"[MENU] Invalid property '%s' in pref menu entry. (line %d).\n",
150 name,parser->line);
151 goto next_element;
153 txt = asx_get_attrib("txt",attribs);
154 if(!(name || txt)) {
155 mp_tmsg(MSGT_OSD_MENU,MSGL_WARN,"[MENU] Pref menu entry definitions need a valid 'property' or 'txt' attribute (line %d).\n",parser->line);
156 if(txt) free(txt), txt = NULL;
157 goto next_element;
159 m = calloc(1,sizeof(struct list_entry_s));
160 m->opt = opt;
161 m->txt = txt; txt = NULL;
162 m->prop = name; name = NULL;
163 m->name = asx_get_attrib("name",attribs);
164 if(!m->name) m->name = strdup(opt ? opt->name : "-");
165 auto_update = asx_get_attrib("auto-update", attribs);
166 if (auto_update) {
167 if (!strcmp(auto_update, "1") ||
168 !strcasecmp(auto_update, "on") ||
169 !strcasecmp(auto_update, "yes") ||
170 !strcasecmp(auto_update, "true"))
171 m->auto_update = 1;
172 free(auto_update);
174 entry_set_text(menu,m);
175 menu_list_add_entry(menu,m);
177 next_element:
178 free(element);
179 if(body) free(body);
180 if(name) free(name);
181 asx_free_attribs(attribs);
185 static void read_cmd(menu_t* menu,int cmd) {
186 list_entry_t* e = mpriv->p.current;
188 if(e->opt) {
189 switch(cmd) {
190 case MENU_CMD_UP:
191 if(!mpriv->edit) break;
192 case MENU_CMD_RIGHT:
193 if(mp_property_do(e->prop,M_PROPERTY_STEP_UP,NULL,menu->ctx) > 0)
194 update_entries(menu, 0);
195 return;
196 case MENU_CMD_DOWN:
197 if(!mpriv->edit) break;
198 case MENU_CMD_LEFT:
199 if(mp_property_do(e->prop,M_PROPERTY_STEP_DOWN,NULL,menu->ctx) > 0)
200 update_entries(menu, 0);
201 return;
203 case MENU_CMD_OK:
204 // check that the property is writable
205 if(mp_property_do(e->prop,M_PROPERTY_SET,NULL,menu->ctx) < 0) return;
206 // shortcut for flags
207 if(e->opt->type == CONF_TYPE_FLAG) {
208 if(mp_property_do(e->prop,M_PROPERTY_STEP_UP,NULL,menu->ctx) > 0)
209 update_entries(menu, 0);
210 return;
212 // switch
213 mpriv->edit = !mpriv->edit;
214 // update the menu
215 update_entries(menu, 0);
216 // switch the pointer
217 if(mpriv->edit) {
218 mpriv->ptr = mpriv->p.ptr;
219 mpriv->p.ptr = NULL;
220 } else
221 mpriv->p.ptr = mpriv->ptr;
222 return;
223 case MENU_CMD_CANCEL:
224 if(!mpriv->edit) break;
225 mpriv->edit = 0;
226 update_entries(menu, 0);
227 mpriv->p.ptr = mpriv->ptr;
228 return;
230 } else if(e->menu) {
231 switch(cmd) {
232 case MENU_CMD_RIGHT:
233 case MENU_CMD_OK: {
234 mp_cmd_t* c;
235 char* txt = malloc(10 + strlen(e->menu) + 1);
236 sprintf(txt,"set_menu %s",e->menu);
237 c = mp_input_parse_cmd(txt);
238 if(c) mp_input_queue_cmd(menu->input_ctx, c);
239 return;
242 } else {
243 switch(cmd) {
244 case MENU_CMD_RIGHT:
245 case MENU_CMD_OK:
246 menu->show = 0;
247 menu->cl = 1;
248 return;
251 menu_list_read_cmd(menu,cmd);
254 static void free_entry(list_entry_t* entry) {
255 free(entry->p.txt);
256 if(entry->name) free(entry->name);
257 if(entry->txt) free(entry->txt);
258 if(entry->prop) free(entry->prop);
259 if(entry->menu) free(entry->menu);
260 free(entry);
263 static void closeMenu(menu_t* menu) {
264 menu_list_uninit(menu,free_entry);
267 static void menu_pref_draw(menu_t* menu, mp_image_t* mpi) {
268 update_entries(menu, 1);
269 menu_list_draw(menu, mpi);
272 static int openMenu(menu_t* menu, char* args) {
274 menu->draw = menu_pref_draw;
275 menu->read_cmd = read_cmd;
276 menu->close = closeMenu;
279 if(!args) {
280 mp_tmsg(MSGT_OSD_MENU,MSGL_ERR,"[MENU] Pref menu needs an argument.\n");
281 return 0;
284 menu_list_init(menu);
285 return parse_args(menu,args);
288 const menu_info_t menu_info_pref = {
289 "Preferences menu",
290 "pref",
291 "Albeu",
294 "pref_cfg",
295 sizeof(struct menu_priv_s),
296 &cfg_dflt,
297 cfg_fields
299 openMenu