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.
26 #include <sys/types.h>
35 #include "m_property.h"
36 #include "asxparser.h"
38 #include "libmpcodecs/mp_image.h"
41 #include "menu_list.h"
42 #include "input/input.h"
64 static struct menu_priv_s cfg_dflt
= {
72 static m_option_t cfg_fields
[] = {
73 MENU_LIST_PRIV_FIELDS
,
74 { "title", M_ST_OFF(menu_list_priv_t
,title
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
75 { "na", M_ST_OFF(struct menu_priv_s
,na
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
76 { "hide-na", M_ST_OFF(struct menu_priv_s
,hide_na
), CONF_TYPE_FLAG
, CONF_RANGE
, 0, 1, NULL
},
77 { NULL
, NULL
, NULL
, 0,0,0,NULL
}
80 #define mpriv (menu->priv)
82 static void entry_set_text(menu_t
* menu
, list_entry_t
* e
) {
83 char* val
= e
->txt
? property_expand_string(menu
->ctx
, e
->txt
) :
84 mp_property_print(e
->prop
, menu
->ctx
);
85 int l
,edit
= (mpriv
->edit
&& e
== mpriv
->p
.current
);
92 val
= strdup(mpriv
->na
);
93 } else if(mpriv
->hide_na
)
95 l
= strlen(e
->name
) + 2 + strlen(val
) + (edit
? 4 : 0) + 1;
96 if(e
->p
.txt
) free(e
->p
.txt
);
98 sprintf(e
->p
.txt
,"%s: %s%s%s",e
->name
,edit
? "> " : "",val
,edit
? " <" : "");
102 static void update_entries(menu_t
* menu
, int auto_update
) {
104 for(e
= mpriv
->p
.menu
; e
; e
= e
->p
.next
)
105 if ((e
->txt
|| e
->prop
) && (!auto_update
|| e
->auto_update
))
106 entry_set_text(menu
, e
);
109 static int parse_args(menu_t
* menu
,char* args
) {
110 char *element
,*body
, **attribs
, *name
, *txt
, *auto_update
;
111 list_entry_t
* m
= NULL
;
114 ASX_Parser_t
* parser
= asx_parser_new();
118 r
= asx_get_element(parser
,&args
,&element
,&body
,&attribs
);
120 mp_msg(MSGT_OSD_MENU
,MSGL_ERR
,MSGTR_LIBMENU_SyntaxErrorAtLine
,parser
->line
);
121 asx_parser_free(parser
);
124 asx_parser_free(parser
);
126 mp_msg(MSGT_OSD_MENU
,MSGL_WARN
,MSGTR_LIBMENU_NoEntryFoundInTheMenuDefinition
);
127 m
= calloc(1,sizeof(struct list_entry_s
));
128 m
->p
.txt
= strdup("Back");
129 menu_list_add_entry(menu
,m
);
132 if(!strcmp(element
,"menu")) {
133 name
= asx_get_attrib("menu",attribs
);
135 mp_msg(MSGT_OSD_MENU
,MSGL_WARN
,MSGTR_LIBMENU_SubmenuDefinitionNeedAMenuAttribut
);
138 m
= calloc(1,sizeof(struct list_entry_s
));
140 name
= NULL
; // we want to keep it
141 m
->p
.txt
= asx_get_attrib("name",attribs
);
142 if(!m
->p
.txt
) m
->p
.txt
= strdup(m
->menu
);
143 menu_list_add_entry(menu
,m
);
147 name
= asx_get_attrib("property",attribs
);
149 if(name
&& mp_property_do(name
,M_PROPERTY_GET_TYPE
,&opt
,menu
->ctx
) <= 0) {
150 mp_msg(MSGT_OSD_MENU
,MSGL_WARN
,MSGTR_LIBMENU_InvalidProperty
,
154 txt
= asx_get_attrib("txt",attribs
);
156 mp_msg(MSGT_OSD_MENU
,MSGL_WARN
,MSGTR_LIBMENU_PrefMenuEntryDefinitionsNeed
,parser
->line
);
157 if(txt
) free(txt
), txt
= NULL
;
160 m
= calloc(1,sizeof(struct list_entry_s
));
162 m
->txt
= txt
; txt
= NULL
;
163 m
->prop
= name
; name
= NULL
;
164 m
->name
= asx_get_attrib("name",attribs
);
165 if(!m
->name
) m
->name
= strdup(opt
? opt
->name
: "-");
166 auto_update
= asx_get_attrib("auto-update", attribs
);
168 if (!strcmp(auto_update
, "1") ||
169 !strcasecmp(auto_update
, "on") ||
170 !strcasecmp(auto_update
, "yes") ||
171 !strcasecmp(auto_update
, "true"))
175 entry_set_text(menu
,m
);
176 menu_list_add_entry(menu
,m
);
182 asx_free_attribs(attribs
);
186 static void read_cmd(menu_t
* menu
,int cmd
) {
187 list_entry_t
* e
= mpriv
->p
.current
;
192 if(!mpriv
->edit
) break;
194 if(mp_property_do(e
->prop
,M_PROPERTY_STEP_UP
,NULL
,menu
->ctx
) > 0)
195 update_entries(menu
, 0);
198 if(!mpriv
->edit
) break;
200 if(mp_property_do(e
->prop
,M_PROPERTY_STEP_DOWN
,NULL
,menu
->ctx
) > 0)
201 update_entries(menu
, 0);
205 // check that the property is writable
206 if(mp_property_do(e
->prop
,M_PROPERTY_SET
,NULL
,menu
->ctx
) < 0) return;
207 // shortcut for flags
208 if(e
->opt
->type
== CONF_TYPE_FLAG
) {
209 if(mp_property_do(e
->prop
,M_PROPERTY_STEP_UP
,NULL
,menu
->ctx
) > 0)
210 update_entries(menu
, 0);
214 mpriv
->edit
= !mpriv
->edit
;
216 update_entries(menu
, 0);
217 // switch the pointer
219 mpriv
->ptr
= mpriv
->p
.ptr
;
222 mpriv
->p
.ptr
= mpriv
->ptr
;
224 case MENU_CMD_CANCEL
:
225 if(!mpriv
->edit
) break;
227 update_entries(menu
, 0);
228 mpriv
->p
.ptr
= mpriv
->ptr
;
236 char* txt
= malloc(10 + strlen(e
->menu
) + 1);
237 sprintf(txt
,"set_menu %s",e
->menu
);
238 c
= mp_input_parse_cmd(txt
);
239 if(c
) mp_input_queue_cmd(c
);
252 menu_list_read_cmd(menu
,cmd
);
255 static void free_entry(list_entry_t
* entry
) {
257 if(entry
->name
) free(entry
->name
);
258 if(entry
->txt
) free(entry
->txt
);
259 if(entry
->prop
) free(entry
->prop
);
260 if(entry
->menu
) free(entry
->menu
);
264 static void closeMenu(menu_t
* menu
) {
265 menu_list_uninit(menu
,free_entry
);
268 static void menu_pref_draw(menu_t
* menu
, mp_image_t
* mpi
) {
269 update_entries(menu
, 1);
270 menu_list_draw(menu
, mpi
);
273 static int openMenu(menu_t
* menu
, char* args
) {
275 menu
->draw
= menu_pref_draw
;
276 menu
->read_cmd
= read_cmd
;
277 menu
->close
= closeMenu
;
281 mp_msg(MSGT_OSD_MENU
,MSGL_ERR
,MSGTR_LIBMENU_PrefMenuNeedsAnArgument
);
285 menu_list_init(menu
);
286 return parse_args(menu
,args
);
289 const menu_info_t menu_info_pref
= {
296 sizeof(struct menu_priv_s
),