sync with en/mplayer.1 rev. 30677
[mplayer/glamo.git] / libmenu / menu_cmdlist.c
blobe146a4e9dbe2d78757a193d2538ed3c4a0ed6531
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"
20 #include "mp_msg.h"
21 #include "help_mp.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <ctype.h>
26 #include <string.h>
28 #include "libmpcodecs/img_format.h"
29 #include "libmpcodecs/mp_image.h"
31 #include "m_option.h"
32 #include "m_struct.h"
33 #include "asxparser.h"
34 #include "menu.h"
35 #include "menu_list.h"
37 #include "libvo/font_load.h"
39 #include "input/input.h"
43 struct list_entry_s {
44 struct list_entry p;
46 char* ok;
47 char* cancel;
48 char* left;
49 char* right;
52 struct menu_priv_s {
53 menu_list_priv_t p;
56 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s, m)
58 static struct menu_priv_s cfg_dflt = {
59 MENU_LIST_PRIV_DFLT,
62 static m_option_t cfg_fields[] = {
63 MENU_LIST_PRIV_FIELDS,
64 { "title",M_ST_OFF(struct menu_priv_s,p.title), CONF_TYPE_STRING, 0, 0, 0, NULL },
65 { NULL, NULL, NULL, 0,0,0,NULL }
68 #define mpriv (menu->priv)
70 static void read_cmd(menu_t* menu,int cmd) {
71 switch(cmd) {
72 case MENU_CMD_RIGHT:
73 if(mpriv->p.current->right) {
74 mp_input_parse_and_queue_cmds(mpriv->p.current->right);
75 break;
76 } // fallback on ok if right is not defined
77 case MENU_CMD_OK:
78 if (mpriv->p.current->ok)
79 mp_input_parse_and_queue_cmds(mpriv->p.current->ok);
80 break;
81 case MENU_CMD_LEFT:
82 if(mpriv->p.current->left) {
83 mp_input_parse_and_queue_cmds(mpriv->p.current->left);
84 break;
85 } // fallback on cancel if left is not defined
86 case MENU_CMD_CANCEL:
87 if(mpriv->p.current->cancel) {
88 mp_input_parse_and_queue_cmds(mpriv->p.current->cancel);
89 break;
91 default:
92 menu_list_read_cmd(menu,cmd);
96 static void free_entry(list_entry_t* entry) {
97 if(entry->ok)
98 free(entry->ok);
99 if(entry->cancel)
100 free(entry->cancel);
101 if(entry->left)
102 free(entry->left);
103 if(entry->right)
104 free(entry->right);
105 free(entry->p.txt);
106 free(entry);
109 static void close_menu(menu_t* menu) {
110 menu_list_uninit(menu,free_entry);
113 static int parse_args(menu_t* menu,char* args) {
114 char *element,*body, **attribs, *name;
115 list_entry_t* m = NULL;
116 int r;
117 ASX_Parser_t* parser = asx_parser_new();
119 while(1) {
120 r = asx_get_element(parser,&args,&element,&body,&attribs);
121 if(r < 0) {
122 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_SyntaxErrorAtLine,parser->line);
123 asx_parser_free(parser);
124 return -1;
125 } else if(r == 0) {
126 asx_parser_free(parser);
127 if(!m)
128 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_NoEntryFoundInTheMenuDefinition);
129 return m ? 1 : 0;
131 // Has it a name ?
132 name = asx_get_attrib("name",attribs);
133 if(!name) {
134 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_ListMenuEntryDefinitionsNeedAName,parser->line);
135 free(element);
136 if(body) free(body);
137 asx_free_attribs(attribs);
138 continue;
140 m = calloc(1,sizeof(struct list_entry_s));
141 m->p.txt = name;
142 m->ok = asx_get_attrib("ok",attribs);
143 m->cancel = asx_get_attrib("cancel",attribs);
144 m->left = asx_get_attrib("left",attribs);
145 m->right = asx_get_attrib("right",attribs);
146 menu_list_add_entry(menu,m);
148 free(element);
149 if(body) free(body);
150 asx_free_attribs(attribs);
154 static int open_cmdlist(menu_t* menu, char* args) {
155 menu->draw = menu_list_draw;
156 menu->read_cmd = read_cmd;
157 menu->close = close_menu;
159 if(!args) {
160 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_ListMenuNeedsAnArgument);
161 return 0;
164 menu_list_init(menu);
165 if(!parse_args(menu,args))
166 return 0;
167 return 1;
170 const menu_info_t menu_info_cmdlist = {
171 "Command list menu",
172 "cmdlist",
173 "Albeu",
176 "cmdlist_cfg",
177 sizeof(struct menu_priv_s),
178 &cfg_dflt,
179 cfg_fields
181 open_cmdlist