codecs.conf: Use fourcc instead of format where appropriate
[mplayer/greg.git] / libmenu / menu_dvbin.c
blob35ce114687d7e2b8aacf11edcca3a2f81db34828
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 <stdlib.h>
20 #include <stdio.h>
21 #include <dirent.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <ctype.h>
27 #include <unistd.h>
28 #include <limits.h>
31 #include "config.h"
33 #include "m_struct.h"
34 #include "m_option.h"
36 #include "libmpcodecs/img_format.h"
37 #include "libmpcodecs/mp_image.h"
39 #include "menu.h"
40 #include "menu_list.h"
41 #include "input/input.h"
42 #include "osdep/keycodes.h"
44 #include "stream/dvbin.h"
48 struct list_entry_s {
49 struct list_entry p;
50 int num; //the position of the chosen channel in the list
53 struct menu_priv_s {
54 menu_list_priv_t p;
55 char* title;
56 char* file;
57 int card;
58 int level;
59 int auto_close;
60 dvb_config_t *config;
64 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s, m)
65 #define mpriv (menu->priv)
67 static const m_option_t cfg_fields[] = {
68 MENU_LIST_PRIV_FIELDS,
69 { "title", ST_OFF(title), CONF_TYPE_STRING, 0, 0, 0, NULL },
70 { "auto-close", ST_OFF(auto_close), CONF_TYPE_FLAG, 0, 0, 1, NULL },
71 { NULL, NULL, NULL, 0,0,0,NULL },
75 static struct menu_priv_s cfg_dflt = {
76 MENU_LIST_PRIV_DFLT,
77 "Select a channel: ",
78 "channels.conf",
82 NULL,
88 static void free_entry(list_entry_t* entry)
90 free(entry->p.txt);
91 free(entry);
95 static int fill_channels_menu(menu_t *menu, dvb_channels_list *dvb_list_ptr)
97 int n;
98 dvb_channel_t *channel;
99 list_entry_t* elem;
101 mpriv->level = 2;
102 if(dvb_list_ptr == NULL)
104 mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_set_channel: LIST NULL PTR, quit\n");
105 n = 1;
106 if((elem = calloc(1, sizeof(list_entry_t))) != NULL)
108 elem->p.next = NULL;
109 elem->p.txt = strdup("There are no channels for this DVB card!");
111 menu_list_add_entry(menu, elem);
113 return 1;
115 for(n = 0; n < dvb_list_ptr->NUM_CHANNELS; n++)
117 channel = &(dvb_list_ptr->channels[n]);
118 if((elem = calloc(1, sizeof(list_entry_t))) != NULL)
120 elem->p.next = NULL;
121 elem->p.txt = strdup(channel->name);
122 elem->num = n;
124 menu_list_add_entry(menu, elem);
126 else
128 mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_menu: fill_menu: couldn't malloc %d bytes for menu item: %s, exit\n",
129 sizeof(list_entry_t), strerror(errno));
130 break;
134 return n;
138 static int fill_cards_menu(menu_t *menu, dvb_config_t *conf)
140 int n;
141 list_entry_t* elem;
143 for(n = 0; n < conf->count; n++)
145 if((elem = calloc(1, sizeof(list_entry_t))) != NULL)
147 elem->p.next = NULL;
148 elem->p.txt = strdup(conf->cards[n].name);
149 elem->num = n;
151 if(n == 0)
152 elem->p.prev = NULL;
154 menu_list_add_entry(menu, elem);
156 else
158 fprintf(stderr, "dvb_menu: fill_menu: couldn't malloc %d bytes for menu item: %s, exit\n",
159 sizeof(list_entry_t), strerror(errno));
160 if(n)
161 return 1;
163 return 0;
167 return n;
171 static int fill_menu(menu_t* menu)
173 list_entry_t* elem;
174 dvb_channels_list *dvb_list_ptr;
176 menu_list_init(menu);
178 if(mpriv->config == NULL)
180 if((elem = calloc(1, sizeof(list_entry_t))) != NULL)
182 elem->p.prev = elem->p.next = NULL;
183 elem->p.txt = strdup("NO DVB configuration present!");
185 menu_list_add_entry(menu, elem);
186 return 1;
188 return 0;
191 mpriv->p.title = mpriv->title;
192 if(mpriv->level == 1 && mpriv->config->count > 1)
193 return fill_cards_menu(menu, mpriv->config);
194 else
196 dvb_list_ptr = mpriv->config->cards[mpriv->card].list;
197 return fill_channels_menu(menu, dvb_list_ptr);
202 static void read_cmd(menu_t* menu, int cmd)
204 list_entry_t *elem;
205 mp_cmd_t* c;
206 char *cmd_name;
207 switch(cmd)
209 case MENU_CMD_RIGHT:
210 case MENU_CMD_OK:
212 elem = mpriv->p.current;
214 if(mpriv->level == 1)
216 mpriv->card = mpriv->p.current->num;
217 mpriv->level = 2;
218 menu_list_uninit(menu, free_entry);
219 fill_menu(menu);
221 else
223 dvb_priv_t *dvbp = (dvb_priv_t*) mpriv->config->priv;
224 cmd_name = malloc(25 + strlen(elem->p.txt));
225 if(dvbp != NULL)
226 sprintf(cmd_name, "dvb_set_channel %d %d", elem->num, mpriv->card);
227 else
228 sprintf(cmd_name, "loadfile 'dvb://%d@%s'", mpriv->card+1, elem->p.txt);
230 c = mp_input_parse_cmd(cmd_name);
231 free(cmd_name);
232 if(c)
234 if(mpriv->auto_close)
235 mp_input_queue_cmd(menu->input_ctx, mp_input_parse_cmd ("menu hide"));
236 mp_input_queue_cmd(menu->input_ctx, c);
240 break;
242 case MENU_CMD_LEFT:
243 case MENU_CMD_CANCEL:
245 elem = mpriv->p.current;
247 menu_list_uninit(menu, free_entry);
248 if(mpriv->config->count > 1)
249 mpriv->level--;
250 else
251 mpriv->level = 0;
253 if(mpriv->level > 0)
255 fill_menu(menu);
256 break;
260 default:
261 menu_list_read_cmd(menu, cmd);
266 static void close_menu(menu_t* menu)
268 dvb_free_config(mpriv->config);
269 menu_list_uninit(menu, free_entry);
273 static int open_dvb_sel(menu_t* menu, char* args)
275 mpriv->config = dvb_get_config();
276 if(mpriv->config == NULL)
277 return 0;
279 menu->draw = menu_list_draw;
280 menu->read_cmd = read_cmd;
281 menu->close = close_menu;
283 mpriv->card = 0;
284 mpriv->level = 1;
285 return fill_menu(menu);
288 const menu_info_t menu_info_dvbsel =
290 "DVB channels menu", //descr
291 "dvbsel", //name
292 "Nico", //author
293 "dvb_sel",
294 { //m_struct_t priv_st=
295 "dvb_cfg", //name
296 sizeof(struct menu_priv_s), //size
297 &cfg_dflt, //defaults
298 cfg_fields //settable fields
300 open_dvb_sel //open function