Remove headers not used.
[mplayer/greg.git] / libmenu / menu_param.c
blob02a90d6304ee64c6ebd5bc4e131d28067d3a2596
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;
37 struct menu_priv_s {
38 menu_list_priv_t p;
39 char* ptr;
40 int edit;
41 /// Cfg fields
42 char* na;
43 int hide_na;
46 static struct menu_priv_s cfg_dflt = {
47 MENU_LIST_PRIV_DFLT,
48 NULL,
50 "N/A",
54 static m_option_t cfg_fields[] = {
55 MENU_LIST_PRIV_FIELDS,
56 { "title", M_ST_OFF(menu_list_priv_t,title), CONF_TYPE_STRING, 0, 0, 0, NULL },
57 { "na", M_ST_OFF(struct menu_priv_s,na), CONF_TYPE_STRING, 0, 0, 0, NULL },
58 { "hide-na", M_ST_OFF(struct menu_priv_s,hide_na), CONF_TYPE_FLAG, CONF_RANGE, 0, 1, NULL },
59 { NULL, NULL, NULL, 0,0,0,NULL }
62 #define mpriv (menu->priv)
64 static void entry_set_text(menu_t* menu, list_entry_t* e) {
65 char* val = e->txt ? property_expand_string(menu->ctx, e->txt) :
66 mp_property_print(e->prop, menu->ctx);
67 int l,edit = (mpriv->edit && e == mpriv->p.current);
68 if(!val || !val[0]) {
69 if(val) free(val);
70 if(mpriv->hide_na) {
71 e->p.hide = 1;
72 return;
74 val = strdup(mpriv->na);
75 } else if(mpriv->hide_na)
76 e->p.hide = 0;
77 l = strlen(e->name) + 2 + strlen(val) + (edit ? 4 : 0) + 1;
78 if(e->p.txt) free(e->p.txt);
79 e->p.txt = malloc(l);
80 sprintf(e->p.txt,"%s: %s%s%s",e->name,edit ? "> " : "",val,edit ? " <" : "");
81 free(val);
84 static void update_entries(menu_t* menu) {
85 list_entry_t* e;
86 for(e = mpriv->p.menu ; e ; e = e->p.next)
87 if(e->txt || e->prop) entry_set_text(menu,e);
90 static int parse_args(menu_t* menu,char* args) {
91 char *element,*body, **attribs, *name, *txt;
92 list_entry_t* m = NULL;
93 int r;
94 m_option_t* opt;
95 ASX_Parser_t* parser = asx_parser_new();
98 while(1) {
99 r = asx_get_element(parser,&args,&element,&body,&attribs);
100 if(r < 0) {
101 mp_msg(MSGT_OSD_MENU,MSGL_ERR,MSGTR_LIBMENU_SyntaxErrorAtLine,parser->line);
102 asx_parser_free(parser);
103 return -1;
104 } else if(r == 0) {
105 asx_parser_free(parser);
106 if(!m)
107 mp_msg(MSGT_OSD_MENU,MSGL_WARN,MSGTR_LIBMENU_NoEntryFoundInTheMenuDefinition);
108 m = calloc(1,sizeof(struct list_entry_s));
109 m->p.txt = strdup("Back");
110 menu_list_add_entry(menu,m);
111 return 1;
113 if(!strcmp(element,"menu")) {
114 name = asx_get_attrib("menu",attribs);
115 if(!name) {
116 mp_msg(MSGT_OSD_MENU,MSGL_WARN,MSGTR_LIBMENU_SubmenuDefinitionNeedAMenuAttribut);
117 goto next_element;
119 m = calloc(1,sizeof(struct list_entry_s));
120 m->menu = name;
121 name = NULL; // we want to keep it
122 m->p.txt = asx_get_attrib("name",attribs);
123 if(!m->p.txt) m->p.txt = strdup(m->menu);
124 menu_list_add_entry(menu,m);
125 goto next_element;
128 name = asx_get_attrib("property",attribs);
129 opt = NULL;
130 if(name && mp_property_do(name,M_PROPERTY_GET_TYPE,&opt,menu->ctx) <= 0) {
131 mp_msg(MSGT_OSD_MENU,MSGL_WARN,MSGTR_LIBMENU_InvalidProperty,
132 name,parser->line);
133 goto next_element;
135 txt = asx_get_attrib("txt",attribs);
136 if(!(name || txt)) {
137 mp_msg(MSGT_OSD_MENU,MSGL_WARN,MSGTR_LIBMENU_PrefMenuEntryDefinitionsNeed,parser->line);
138 if(txt) free(txt), txt = NULL;
139 goto next_element;
141 m = calloc(1,sizeof(struct list_entry_s));
142 m->opt = opt;
143 m->txt = txt; txt = NULL;
144 m->prop = name; name = NULL;
145 m->name = asx_get_attrib("name",attribs);
146 if(!m->name) m->name = strdup(opt ? opt->name : "-");
147 entry_set_text(menu,m);
148 menu_list_add_entry(menu,m);
150 next_element:
151 free(element);
152 if(body) free(body);
153 if(name) free(name);
154 asx_free_attribs(attribs);
158 static void read_cmd(menu_t* menu,int cmd) {
159 list_entry_t* e = mpriv->p.current;
161 if(e->opt) {
162 switch(cmd) {
163 case MENU_CMD_UP:
164 if(!mpriv->edit) break;
165 case MENU_CMD_RIGHT:
166 if(mp_property_do(e->prop,M_PROPERTY_STEP_UP,NULL,menu->ctx) > 0)
167 update_entries(menu);
168 return;
169 case MENU_CMD_DOWN:
170 if(!mpriv->edit) break;
171 case MENU_CMD_LEFT:
172 if(mp_property_do(e->prop,M_PROPERTY_STEP_DOWN,NULL,menu->ctx) > 0)
173 update_entries(menu);
174 return;
176 case MENU_CMD_OK:
177 // check that the property is writable
178 if(mp_property_do(e->prop,M_PROPERTY_SET,NULL,menu->ctx) < 0) return;
179 // shortcut for flags
180 if(e->opt->type == CONF_TYPE_FLAG) {
181 if(mp_property_do(e->prop,M_PROPERTY_STEP_UP,NULL,menu->ctx) > 0)
182 update_entries(menu);
183 return;
185 // switch
186 mpriv->edit = !mpriv->edit;
187 // update the menu
188 update_entries(menu);
189 // switch the pointer
190 if(mpriv->edit) {
191 mpriv->ptr = mpriv->p.ptr;
192 mpriv->p.ptr = NULL;
193 } else
194 mpriv->p.ptr = mpriv->ptr;
195 return;
196 case MENU_CMD_CANCEL:
197 if(!mpriv->edit) break;
198 mpriv->edit = 0;
199 update_entries(menu);
200 mpriv->p.ptr = mpriv->ptr;
201 return;
203 } else if(e->menu) {
204 switch(cmd) {
205 case MENU_CMD_RIGHT:
206 case MENU_CMD_OK: {
207 mp_cmd_t* c;
208 char* txt = malloc(10 + strlen(e->menu) + 1);
209 sprintf(txt,"set_menu %s",e->menu);
210 c = mp_input_parse_cmd(txt);
211 if(c) mp_input_queue_cmd(c);
212 return;
215 } else {
216 switch(cmd) {
217 case MENU_CMD_RIGHT:
218 case MENU_CMD_OK:
219 menu->show = 0;
220 menu->cl = 1;
221 return;
224 menu_list_read_cmd(menu,cmd);
227 static void free_entry(list_entry_t* entry) {
228 free(entry->p.txt);
229 if(entry->name) free(entry->name);
230 if(entry->txt) free(entry->txt);
231 if(entry->prop) free(entry->prop);
232 if(entry->menu) free(entry->menu);
233 free(entry);
236 static void closeMenu(menu_t* menu) {
237 menu_list_uninit(menu,free_entry);
240 static int openMenu(menu_t* menu, char* args) {
242 menu->draw = menu_list_draw;
243 menu->read_cmd = read_cmd;
244 menu->close = closeMenu;
247 if(!args) {
248 mp_msg(MSGT_OSD_MENU,MSGL_ERR,MSGTR_LIBMENU_PrefMenuNeedsAnArgument);
249 return 0;
252 menu_list_init(menu);
253 return parse_args(menu,args);
256 const menu_info_t menu_info_pref = {
257 "Preferences menu",
258 "pref",
259 "Albeu",
262 "pref_cfg",
263 sizeof(struct menu_priv_s),
264 &cfg_dflt,
265 cfg_fields
267 openMenu