Add auto-update property for property menu item.
[mplayer/greg.git] / libmenu / menu_param.c
bloba1aeef44bd7312643a46c8456eeec603210c04f5
2 #include "config.h"
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <dirent.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <ctype.h>
13 #include "mp_msg.h"
14 #include "help_mp.h"
16 #include "m_struct.h"
17 #include "m_option.h"
18 #include "m_property.h"
19 #include "asxparser.h"
21 #include "libmpcodecs/mp_image.h"
23 #include "menu.h"
24 #include "menu_list.h"
25 #include "input/input.h"
26 #include "command.h"
28 struct list_entry_s {
29 struct list_entry p;
30 char* name;
31 char* txt;
32 char* prop;
33 m_option_t* opt;
34 char* menu;
35 int auto_update;
38 struct menu_priv_s {
39 menu_list_priv_t p;
40 char* ptr;
41 int edit;
42 /// Cfg fields
43 char* na;
44 int hide_na;
47 static struct menu_priv_s cfg_dflt = {
48 MENU_LIST_PRIV_DFLT,
49 NULL,
51 "N/A",
55 static m_option_t cfg_fields[] = {
56 MENU_LIST_PRIV_FIELDS,
57 { "title", M_ST_OFF(menu_list_priv_t,title), CONF_TYPE_STRING, 0, 0, 0, NULL },
58 { "na", M_ST_OFF(struct menu_priv_s,na), CONF_TYPE_STRING, 0, 0, 0, NULL },
59 { "hide-na", M_ST_OFF(struct menu_priv_s,hide_na), CONF_TYPE_FLAG, CONF_RANGE, 0, 1, NULL },
60 { NULL, NULL, NULL, 0,0,0,NULL }
63 #define mpriv (menu->priv)
65 static void entry_set_text(menu_t* menu, list_entry_t* e) {
66 char* val = e->txt ? property_expand_string(menu->ctx, e->txt) :
67 mp_property_print(e->prop, menu->ctx);
68 int l,edit = (mpriv->edit && e == mpriv->p.current);
69 if(!val || !val[0]) {
70 if(val) free(val);
71 if(mpriv->hide_na) {
72 e->p.hide = 1;
73 return;
75 val = strdup(mpriv->na);
76 } else if(mpriv->hide_na)
77 e->p.hide = 0;
78 l = strlen(e->name) + 2 + strlen(val) + (edit ? 4 : 0) + 1;
79 if(e->p.txt) free(e->p.txt);
80 e->p.txt = malloc(l);
81 sprintf(e->p.txt,"%s: %s%s%s",e->name,edit ? "> " : "",val,edit ? " <" : "");
82 free(val);
85 static void update_entries(menu_t* menu, int auto_update) {
86 list_entry_t* e;
87 for(e = mpriv->p.menu ; e ; e = e->p.next)
88 if ((e->txt || e->prop) && (!auto_update || e->auto_update))
89 entry_set_text(menu, e);
92 static int parse_args(menu_t* menu,char* args) {
93 char *element,*body, **attribs, *name, *txt, *auto_update;
94 list_entry_t* m = NULL;
95 int r;
96 m_option_t* opt;
97 ASX_Parser_t* parser = asx_parser_new();
100 while(1) {
101 r = asx_get_element(parser,&args,&element,&body,&attribs);
102 if(r < 0) {
103 mp_msg(MSGT_OSD_MENU,MSGL_ERR,MSGTR_LIBMENU_SyntaxErrorAtLine,parser->line);
104 asx_parser_free(parser);
105 return -1;
106 } else if(r == 0) {
107 asx_parser_free(parser);
108 if(!m)
109 mp_msg(MSGT_OSD_MENU,MSGL_WARN,MSGTR_LIBMENU_NoEntryFoundInTheMenuDefinition);
110 m = calloc(1,sizeof(struct list_entry_s));
111 m->p.txt = strdup("Back");
112 menu_list_add_entry(menu,m);
113 return 1;
115 if(!strcmp(element,"menu")) {
116 name = asx_get_attrib("menu",attribs);
117 if(!name) {
118 mp_msg(MSGT_OSD_MENU,MSGL_WARN,MSGTR_LIBMENU_SubmenuDefinitionNeedAMenuAttribut);
119 goto next_element;
121 m = calloc(1,sizeof(struct list_entry_s));
122 m->menu = name;
123 name = NULL; // we want to keep it
124 m->p.txt = asx_get_attrib("name",attribs);
125 if(!m->p.txt) m->p.txt = strdup(m->menu);
126 menu_list_add_entry(menu,m);
127 goto next_element;
130 name = asx_get_attrib("property",attribs);
131 opt = NULL;
132 if(name && mp_property_do(name,M_PROPERTY_GET_TYPE,&opt,menu->ctx) <= 0) {
133 mp_msg(MSGT_OSD_MENU,MSGL_WARN,MSGTR_LIBMENU_InvalidProperty,
134 name,parser->line);
135 goto next_element;
137 txt = asx_get_attrib("txt",attribs);
138 if(!(name || txt)) {
139 mp_msg(MSGT_OSD_MENU,MSGL_WARN,MSGTR_LIBMENU_PrefMenuEntryDefinitionsNeed,parser->line);
140 if(txt) free(txt), txt = NULL;
141 goto next_element;
143 m = calloc(1,sizeof(struct list_entry_s));
144 m->opt = opt;
145 m->txt = txt; txt = NULL;
146 m->prop = name; name = NULL;
147 m->name = asx_get_attrib("name",attribs);
148 if(!m->name) m->name = strdup(opt ? opt->name : "-");
149 auto_update = asx_get_attrib("auto-update", attribs);
150 if (auto_update) {
151 if (!strcmp(auto_update, "1") ||
152 !strcasecmp(auto_update, "on") ||
153 !strcasecmp(auto_update, "yes") ||
154 !strcasecmp(auto_update, "true"))
155 m->auto_update = 1;
156 free(auto_update);
158 entry_set_text(menu,m);
159 menu_list_add_entry(menu,m);
161 next_element:
162 free(element);
163 if(body) free(body);
164 if(name) free(name);
165 asx_free_attribs(attribs);
169 static void read_cmd(menu_t* menu,int cmd) {
170 list_entry_t* e = mpriv->p.current;
172 if(e->opt) {
173 switch(cmd) {
174 case MENU_CMD_UP:
175 if(!mpriv->edit) break;
176 case MENU_CMD_RIGHT:
177 if(mp_property_do(e->prop,M_PROPERTY_STEP_UP,NULL,menu->ctx) > 0)
178 update_entries(menu, 0);
179 return;
180 case MENU_CMD_DOWN:
181 if(!mpriv->edit) break;
182 case MENU_CMD_LEFT:
183 if(mp_property_do(e->prop,M_PROPERTY_STEP_DOWN,NULL,menu->ctx) > 0)
184 update_entries(menu, 0);
185 return;
187 case MENU_CMD_OK:
188 // check that the property is writable
189 if(mp_property_do(e->prop,M_PROPERTY_SET,NULL,menu->ctx) < 0) return;
190 // shortcut for flags
191 if(e->opt->type == CONF_TYPE_FLAG) {
192 if(mp_property_do(e->prop,M_PROPERTY_STEP_UP,NULL,menu->ctx) > 0)
193 update_entries(menu, 0);
194 return;
196 // switch
197 mpriv->edit = !mpriv->edit;
198 // update the menu
199 update_entries(menu, 0);
200 // switch the pointer
201 if(mpriv->edit) {
202 mpriv->ptr = mpriv->p.ptr;
203 mpriv->p.ptr = NULL;
204 } else
205 mpriv->p.ptr = mpriv->ptr;
206 return;
207 case MENU_CMD_CANCEL:
208 if(!mpriv->edit) break;
209 mpriv->edit = 0;
210 update_entries(menu, 0);
211 mpriv->p.ptr = mpriv->ptr;
212 return;
214 } else if(e->menu) {
215 switch(cmd) {
216 case MENU_CMD_RIGHT:
217 case MENU_CMD_OK: {
218 mp_cmd_t* c;
219 char* txt = malloc(10 + strlen(e->menu) + 1);
220 sprintf(txt,"set_menu %s",e->menu);
221 c = mp_input_parse_cmd(txt);
222 if(c) mp_input_queue_cmd(c);
223 return;
226 } else {
227 switch(cmd) {
228 case MENU_CMD_RIGHT:
229 case MENU_CMD_OK:
230 menu->show = 0;
231 menu->cl = 1;
232 return;
235 menu_list_read_cmd(menu,cmd);
238 static void free_entry(list_entry_t* entry) {
239 free(entry->p.txt);
240 if(entry->name) free(entry->name);
241 if(entry->txt) free(entry->txt);
242 if(entry->prop) free(entry->prop);
243 if(entry->menu) free(entry->menu);
244 free(entry);
247 static void closeMenu(menu_t* menu) {
248 menu_list_uninit(menu,free_entry);
251 static void menu_pref_draw(menu_t* menu, mp_image_t* mpi) {
252 update_entries(menu, 1);
253 menu_list_draw(menu, mpi);
256 static int openMenu(menu_t* menu, char* args) {
258 menu->draw = menu_pref_draw;
259 menu->read_cmd = read_cmd;
260 menu->close = closeMenu;
263 if(!args) {
264 mp_msg(MSGT_OSD_MENU,MSGL_ERR,MSGTR_LIBMENU_PrefMenuNeedsAnArgument);
265 return 0;
268 menu_list_init(menu);
269 return parse_args(menu,args);
272 const menu_info_t menu_info_pref = {
273 "Preferences menu",
274 "pref",
275 "Albeu",
278 "pref_cfg",
279 sizeof(struct menu_priv_s),
280 &cfg_dflt,
281 cfg_fields
283 openMenu