switch_ratio may not work with some filter chains
[mplayer/greg.git] / libmenu / menu_dvbin.c
blob0885014848f97ad34a2f67366909ab33d7d2e51e
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <dirent.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <ctype.h>
10 #include <unistd.h>
11 #include <limits.h>
14 #include "../config.h"
16 #include "../m_struct.h"
17 #include "../m_option.h"
19 #include "img_format.h"
20 #include "mp_image.h"
22 #include "menu.h"
23 #include "menu_list.h"
24 #include "../input/input.h"
25 #include "../osdep/keycodes.h"
27 #include "../libmpdemux/dvbin.h"
31 struct list_entry_s {
32 struct list_entry p;
33 int num; //the position of the chosen channel in the list
36 struct menu_priv_s {
37 menu_list_priv_t p;
38 char* title;
39 char* file;
40 int card;
41 int level;
42 dvb_config_t *config;
46 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s, m)
47 #define mpriv (menu->priv)
49 static m_option_t cfg_fields[] = {
50 MENU_LIST_PRIV_FIELDS,
51 { "title", ST_OFF(title), CONF_TYPE_STRING, 0, 0, 0, NULL },
52 { NULL, NULL, NULL, 0,0,0,NULL },
56 static struct menu_priv_s cfg_dflt = {
57 MENU_LIST_PRIV_DFLT,
58 "Select a channel: ",
59 "channels.conf",
62 NULL,
68 static void free_entry(list_entry_t* entry)
70 free(entry->p.txt);
71 free(entry);
75 static int fill_channels_menu(menu_t *menu, dvb_channels_list *dvb_list_ptr)
77 int n;
78 dvb_channel_t *channel;
79 list_entry_t* elem;
81 mpriv->level = 2;
82 if(dvb_list_ptr == NULL)
84 mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_set_channel: LIST NULL PTR, quit\n");
85 n = 1;
86 if((elem = calloc(1, sizeof(list_entry_t))) != NULL)
88 elem->p.next = NULL;
89 elem->p.txt = strdup("There are no channels for this DVB card!");
91 menu_list_add_entry(menu, elem);
93 return 1;
95 for(n = 0; n < dvb_list_ptr->NUM_CHANNELS; n++)
97 channel = &(dvb_list_ptr->channels[n]);
98 if((elem = calloc(1, sizeof(list_entry_t))) != NULL)
100 elem->p.next = NULL;
101 elem->p.txt = strdup(channel->name);
102 elem->num = n;
104 menu_list_add_entry(menu, elem);
106 else
108 mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_menu: fill_menu: couldn't malloc %d bytes for menu item: %s, exit\n",
109 sizeof(list_entry_t), strerror(errno));
111 break;
115 return n;
119 static int fill_cards_menu(menu_t *menu, dvb_config_t *conf)
121 int n;
122 list_entry_t* elem;
124 for(n = 0; n < conf->count; n++)
126 if((elem = calloc(1, sizeof(list_entry_t))) != NULL)
128 elem->p.next = NULL;
129 elem->p.txt = strdup(conf->cards[n].name);
130 elem->num = n;
132 if(n == 0)
133 elem->p.prev = NULL;
135 menu_list_add_entry(menu, elem);
137 else
139 fprintf(stderr, "dvb_menu: fill_menu: couldn't malloc %d bytes for menu item: %s, exit\n",
140 sizeof(list_entry_t), strerror(errno));
142 if(n)
143 return 1;
145 return 0;
149 return n;
153 static int fill_menu(menu_t* menu)
155 list_entry_t* elem;
156 dvb_channels_list *dvb_list_ptr;
158 menu_list_init(menu);
160 if(mpriv->config == NULL)
162 if((elem = calloc(1, sizeof(list_entry_t))) != NULL)
164 elem->p.prev = elem->p.next = NULL;
165 elem->p.txt = strdup("NO DVB configuration present!");
167 menu_list_add_entry(menu, elem);
168 return 1;
170 return 0;
173 mpriv->p.title = mpriv->title;
174 if(mpriv->level == 1 && mpriv->config->count > 1)
175 return fill_cards_menu(menu, mpriv->config);
176 else
178 dvb_list_ptr = mpriv->config->cards[mpriv->card].list;
179 return fill_channels_menu(menu, dvb_list_ptr);
184 static void read_cmd(menu_t* menu, int cmd)
186 list_entry_t *elem;
187 mp_cmd_t* c;
188 char *cmd_name;
189 switch(cmd)
191 case MENU_CMD_OK:
193 elem = mpriv->p.current;
195 if(mpriv->level == 1)
197 mpriv->card = mpriv->p.current->num;
198 mpriv->level = 2;
199 menu_list_uninit(menu, free_entry);
200 fill_menu(menu);
202 else
204 dvb_priv_t *dvbp = (dvb_priv_t*) mpriv->config->priv;
205 cmd_name = malloc(25 + strlen(elem->p.txt));
206 if(dvbp != NULL)
207 sprintf(cmd_name, "dvb_set_channel %d %d", elem->num, mpriv->card);
208 else
209 sprintf(cmd_name, "loadfile 'dvb://%d@%s'", mpriv->card+1, elem->p.txt);
211 c = mp_input_parse_cmd(cmd_name);
212 if(c)
213 mp_input_queue_cmd(c);
216 break;
218 case MENU_CMD_CANCEL:
220 elem = mpriv->p.current;
222 menu_list_uninit(menu, free_entry);
223 if(mpriv->config->count > 1)
224 mpriv->level--;
225 else
226 mpriv->level = 0;
228 if(mpriv->level > 0)
230 fill_menu(menu);
231 break;
235 default:
236 menu_list_read_cmd(menu, cmd);
241 static void close_menu(menu_t* menu)
243 menu_list_uninit(menu, free_entry);
247 static int open_dvb_sel(menu_t* menu, char* args)
249 mpriv->config = dvb_get_config();
250 if(mpriv->config == NULL)
251 return 0;
253 menu->draw = menu_list_draw;
254 menu->read_cmd = read_cmd;
255 menu->close = close_menu;
257 mpriv->card = 0;
258 mpriv->level = 1;
259 return fill_menu(menu);
262 const menu_info_t menu_info_dvbsel =
264 "DVB channels menu", //descr
265 "dvbsel", //name
266 "Nico", //author
267 "dvb_sel",
268 { //m_struct_t priv_st=
269 "dvb_cfg", //name
270 sizeof(struct menu_priv_s), //size
271 &cfg_dflt, //defaults
272 cfg_fields //settable fields
274 open_dvb_sel //open function