Fix compilation by adding forgotten comma.
[mplayer/glamo.git] / libmenu / menu_param.c
blobdd0933c4ac59d8f45567cb3bdaa2da73d3727673
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/img_format.h"
22 #include "libmpcodecs/mp_image.h"
24 #include "menu.h"
25 #include "menu_list.h"
26 #include "input/input.h"
27 #include "osdep/keycodes.h"
28 #include "command.h"
30 struct list_entry_s {
31 struct list_entry p;
32 char* name;
33 char* txt;
34 char* prop;
35 m_option_t* opt;
36 char* menu;
39 struct menu_priv_s {
40 menu_list_priv_t p;
41 char* ptr;
42 int edit;
43 /// Cfg fields
44 char* na;
45 int hide_na;
48 static struct menu_priv_s cfg_dflt = {
49 MENU_LIST_PRIV_DFLT,
50 NULL,
52 "N/A",
56 static m_option_t cfg_fields[] = {
57 MENU_LIST_PRIV_FIELDS,
58 { "title", M_ST_OFF(menu_list_priv_t,title), CONF_TYPE_STRING, 0, 0, 0, NULL },
59 { "na", M_ST_OFF(struct menu_priv_s,na), CONF_TYPE_STRING, 0, 0, 0, NULL },
60 { "hide-na", M_ST_OFF(struct menu_priv_s,hide_na), CONF_TYPE_FLAG, CONF_RANGE, 0, 1, NULL },
61 { NULL, NULL, NULL, 0,0,0,NULL }
64 #define mpriv (menu->priv)
66 static void entry_set_text(menu_t* menu, list_entry_t* e) {
67 char* val = e->txt ? property_expand_string(menu->ctx, e->txt) :
68 mp_property_print(e->prop, menu->ctx);
69 int l,edit = (mpriv->edit && e == mpriv->p.current);
70 if(!val || !val[0]) {
71 if(val) free(val);
72 if(mpriv->hide_na) {
73 e->p.hide = 1;
74 return;
76 val = strdup(mpriv->na);
77 } else if(mpriv->hide_na)
78 e->p.hide = 0;
79 l = strlen(e->name) + 2 + strlen(val) + (edit ? 4 : 0) + 1;
80 if(e->p.txt) free(e->p.txt);
81 e->p.txt = malloc(l);
82 sprintf(e->p.txt,"%s: %s%s%s",e->name,edit ? "> " : "",val,edit ? " <" : "");
83 free(val);
86 static void update_entries(menu_t* menu) {
87 list_entry_t* e;
88 for(e = mpriv->p.menu ; e ; e = e->p.next)
89 if(e->txt || e->prop) entry_set_text(menu,e);
92 static int parse_args(menu_t* menu,char* args) {
93 char *element,*body, **attribs, *name, *txt;
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 entry_set_text(menu,m);
150 menu_list_add_entry(menu,m);
152 next_element:
153 free(element);
154 if(body) free(body);
155 if(name) free(name);
156 asx_free_attribs(attribs);
160 static void read_key(menu_t* menu,int c) {
161 menu_list_read_key(menu,c,0);
164 static void read_cmd(menu_t* menu,int cmd) {
165 list_entry_t* e = mpriv->p.current;
167 if(e->opt) {
168 switch(cmd) {
169 case MENU_CMD_UP:
170 if(!mpriv->edit) break;
171 case MENU_CMD_RIGHT:
172 if(mp_property_do(e->prop,M_PROPERTY_STEP_UP,NULL,menu->ctx) > 0)
173 update_entries(menu);
174 return;
175 case MENU_CMD_DOWN:
176 if(!mpriv->edit) break;
177 case MENU_CMD_LEFT:
178 if(mp_property_do(e->prop,M_PROPERTY_STEP_DOWN,NULL,menu->ctx) > 0)
179 update_entries(menu);
180 return;
182 case MENU_CMD_OK:
183 // check that the property is writable
184 if(mp_property_do(e->prop,M_PROPERTY_SET,NULL,menu->ctx) < 0) return;
185 // shortcut for flags
186 if(e->opt->type == CONF_TYPE_FLAG) {
187 if(mp_property_do(e->prop,M_PROPERTY_STEP_UP,NULL,menu->ctx) > 0)
188 update_entries(menu);
189 return;
191 // switch
192 mpriv->edit = !mpriv->edit;
193 // update the menu
194 update_entries(menu);
195 // switch the pointer
196 if(mpriv->edit) {
197 mpriv->ptr = mpriv->p.ptr;
198 mpriv->p.ptr = NULL;
199 } else
200 mpriv->p.ptr = mpriv->ptr;
201 return;
202 case MENU_CMD_CANCEL:
203 if(!mpriv->edit) break;
204 mpriv->edit = 0;
205 update_entries(menu);
206 mpriv->p.ptr = mpriv->ptr;
207 return;
209 } else if(e->menu) {
210 switch(cmd) {
211 case MENU_CMD_RIGHT:
212 case MENU_CMD_OK: {
213 mp_cmd_t* c;
214 char* txt = malloc(10 + strlen(e->menu) + 1);
215 sprintf(txt,"set_menu %s",e->menu);
216 c = mp_input_parse_cmd(txt);
217 if(c) mp_input_queue_cmd(c);
218 return;
221 } else {
222 switch(cmd) {
223 case MENU_CMD_RIGHT:
224 case MENU_CMD_OK:
225 menu->show = 0;
226 menu->cl = 1;
227 return;
230 menu_list_read_cmd(menu,cmd);
233 static void free_entry(list_entry_t* entry) {
234 free(entry->p.txt);
235 if(entry->name) free(entry->name);
236 if(entry->txt) free(entry->txt);
237 if(entry->prop) free(entry->prop);
238 if(entry->menu) free(entry->menu);
239 free(entry);
242 static void closeMenu(menu_t* menu) {
243 menu_list_uninit(menu,free_entry);
246 static int openMenu(menu_t* menu, char* args) {
248 menu->draw = menu_list_draw;
249 menu->read_cmd = read_cmd;
250 menu->read_key = read_key;
251 menu->close = closeMenu;
254 if(!args) {
255 mp_msg(MSGT_OSD_MENU,MSGL_ERR,MSGTR_LIBMENU_PrefMenuNeedsAnArgument);
256 return 0;
259 menu_list_init(menu);
260 return parse_args(menu,args);
263 const menu_info_t menu_info_pref = {
264 "Preferences menu",
265 "pref",
266 "Albeu",
269 "pref_cfg",
270 sizeof(struct menu_priv_s),
271 &cfg_dflt,
272 cfg_fields
274 openMenu