Fix vf_tcdump's compilation
[mplayer/kovensky.git] / libmenu / menu_cmdlist.c
blob4d29fa14fe48ed8b67f1bfb8f193cdb791152211
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"
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <string.h>
27 #include "libmpcodecs/img_format.h"
28 #include "libmpcodecs/mp_image.h"
30 #include "m_option.h"
31 #include "m_struct.h"
32 #include "asxparser.h"
33 #include "menu.h"
34 #include "menu_list.h"
36 #include "libvo/font_load.h"
38 #include "input/input.h"
42 struct list_entry_s {
43 struct list_entry p;
45 char* ok;
46 char* cancel;
47 char* left;
48 char* right;
51 struct menu_priv_s {
52 menu_list_priv_t p;
55 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s, m)
57 static struct menu_priv_s cfg_dflt = {
58 MENU_LIST_PRIV_DFLT,
61 static m_option_t cfg_fields[] = {
62 MENU_LIST_PRIV_FIELDS,
63 { "title",M_ST_OFF(struct menu_priv_s,p.title), CONF_TYPE_STRING, 0, 0, 0, NULL },
64 { NULL, NULL, NULL, 0,0,0,NULL }
67 #define mpriv (menu->priv)
69 static void read_cmd(menu_t* menu,int cmd) {
70 switch(cmd) {
71 case MENU_CMD_RIGHT:
72 if(mpriv->p.current->right) {
73 mp_input_parse_and_queue_cmds(menu->input_ctx, mpriv->p.current->right);
74 break;
75 } // fallback on ok if right is not defined
76 case MENU_CMD_OK:
77 if (mpriv->p.current->ok)
78 mp_input_parse_and_queue_cmds(menu->input_ctx, mpriv->p.current->ok);
79 break;
80 case MENU_CMD_LEFT:
81 if(mpriv->p.current->left) {
82 mp_input_parse_and_queue_cmds(menu->input_ctx, mpriv->p.current->left);
83 break;
84 } // fallback on cancel if left is not defined
85 case MENU_CMD_CANCEL:
86 if(mpriv->p.current->cancel) {
87 mp_input_parse_and_queue_cmds(menu->input_ctx, mpriv->p.current->cancel);
88 break;
90 default:
91 menu_list_read_cmd(menu,cmd);
95 static void free_entry(list_entry_t* entry) {
96 if(entry->ok)
97 free(entry->ok);
98 if(entry->cancel)
99 free(entry->cancel);
100 if(entry->left)
101 free(entry->left);
102 if(entry->right)
103 free(entry->right);
104 free(entry->p.txt);
105 free(entry);
108 static void close_menu(menu_t* menu) {
109 menu_list_uninit(menu,free_entry);
112 static int parse_args(menu_t* menu,char* args) {
113 char *element,*body, **attribs, *name;
114 list_entry_t* m = NULL;
115 int r;
116 ASX_Parser_t* parser = asx_parser_new(menu->mconfig);
118 while(1) {
119 r = asx_get_element(parser,&args,&element,&body,&attribs);
120 if(r < 0) {
121 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] syntax error at line: %d\n",parser->line);
122 asx_parser_free(parser);
123 return -1;
124 } else if(r == 0) {
125 asx_parser_free(parser);
126 if(!m)
127 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] No entry found in the menu definition.\n");
128 return m ? 1 : 0;
130 // Has it a name ?
131 name = asx_get_attrib("name",attribs);
132 if(!name) {
133 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] List menu entry definitions need a name (line %d).\n",parser->line);
134 free(element);
135 if(body) free(body);
136 asx_free_attribs(attribs);
137 continue;
139 m = calloc(1,sizeof(struct list_entry_s));
140 m->p.txt = name;
141 m->ok = asx_get_attrib("ok",attribs);
142 m->cancel = asx_get_attrib("cancel",attribs);
143 m->left = asx_get_attrib("left",attribs);
144 m->right = asx_get_attrib("right",attribs);
145 menu_list_add_entry(menu,m);
147 free(element);
148 if(body) free(body);
149 asx_free_attribs(attribs);
153 static int open_cmdlist(menu_t* menu, char* args) {
154 menu->draw = menu_list_draw;
155 menu->read_cmd = read_cmd;
156 menu->close = close_menu;
158 if(!args) {
159 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] List menu needs an argument.\n");
160 return 0;
163 menu_list_init(menu);
164 if(!parse_args(menu,args))
165 return 0;
166 return 1;
169 const menu_info_t menu_info_cmdlist = {
170 "Command list menu",
171 "cmdlist",
172 "Albeu",
175 "cmdlist_cfg",
176 sizeof(struct menu_priv_s),
177 &cfg_dflt,
178 cfg_fields
180 open_cmdlist