Move input options to option struct
[mplayer.git] / libmenu / menu_dvbin.c
blob5b007afaddc03653c8887c94c593390b38362a40
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 "libmpcodecs/img_format.h"
20 #include "libmpcodecs/mp_image.h"
22 #include "menu.h"
23 #include "menu_list.h"
24 #include "input/input.h"
25 #include "osdep/keycodes.h"
27 #include "stream/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 int auto_close;
43 dvb_config_t *config;
47 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s, m)
48 #define mpriv (menu->priv)
50 static m_option_t cfg_fields[] = {
51 MENU_LIST_PRIV_FIELDS,
52 { "title", ST_OFF(title), CONF_TYPE_STRING, 0, 0, 0, NULL },
53 { "auto-close", ST_OFF(auto_close), CONF_TYPE_FLAG, 0, 0, 1, NULL },
54 { NULL, NULL, NULL, 0,0,0,NULL },
58 static struct menu_priv_s cfg_dflt = {
59 MENU_LIST_PRIV_DFLT,
60 "Select a channel: ",
61 "channels.conf",
65 NULL,
71 static void free_entry(list_entry_t* entry)
73 free(entry->p.txt);
74 free(entry);
78 static int fill_channels_menu(menu_t *menu, dvb_channels_list *dvb_list_ptr)
80 int n;
81 dvb_channel_t *channel;
82 list_entry_t* elem;
84 mpriv->level = 2;
85 if(dvb_list_ptr == NULL)
87 mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_set_channel: LIST NULL PTR, quit\n");
88 n = 1;
89 if((elem = calloc(1, sizeof(list_entry_t))) != NULL)
91 elem->p.next = NULL;
92 elem->p.txt = strdup("There are no channels for this DVB card!");
94 menu_list_add_entry(menu, elem);
96 return 1;
98 for(n = 0; n < dvb_list_ptr->NUM_CHANNELS; n++)
100 channel = &(dvb_list_ptr->channels[n]);
101 if((elem = calloc(1, sizeof(list_entry_t))) != NULL)
103 elem->p.next = NULL;
104 elem->p.txt = strdup(channel->name);
105 elem->num = n;
107 menu_list_add_entry(menu, elem);
109 else
111 mp_msg(MSGT_DEMUX, MSGL_ERR, "dvb_menu: fill_menu: couldn't malloc %d bytes for menu item: %s, exit\n",
112 sizeof(list_entry_t), strerror(errno));
113 break;
117 return n;
121 static int fill_cards_menu(menu_t *menu, dvb_config_t *conf)
123 int n;
124 list_entry_t* elem;
126 for(n = 0; n < conf->count; n++)
128 if((elem = calloc(1, sizeof(list_entry_t))) != NULL)
130 elem->p.next = NULL;
131 elem->p.txt = strdup(conf->cards[n].name);
132 elem->num = n;
134 if(n == 0)
135 elem->p.prev = NULL;
137 menu_list_add_entry(menu, elem);
139 else
141 fprintf(stderr, "dvb_menu: fill_menu: couldn't malloc %d bytes for menu item: %s, exit\n",
142 sizeof(list_entry_t), strerror(errno));
143 if(n)
144 return 1;
146 return 0;
150 return n;
154 static int fill_menu(menu_t* menu)
156 list_entry_t* elem;
157 dvb_channels_list *dvb_list_ptr;
159 menu_list_init(menu);
161 if(mpriv->config == NULL)
163 if((elem = calloc(1, sizeof(list_entry_t))) != NULL)
165 elem->p.prev = elem->p.next = NULL;
166 elem->p.txt = strdup("NO DVB configuration present!");
168 menu_list_add_entry(menu, elem);
169 return 1;
171 return 0;
174 mpriv->p.title = mpriv->title;
175 if(mpriv->level == 1 && mpriv->config->count > 1)
176 return fill_cards_menu(menu, mpriv->config);
177 else
179 dvb_list_ptr = mpriv->config->cards[mpriv->card].list;
180 return fill_channels_menu(menu, dvb_list_ptr);
185 static void read_cmd(menu_t* menu, int cmd)
187 list_entry_t *elem;
188 mp_cmd_t* c;
189 char *cmd_name;
190 switch(cmd)
192 case MENU_CMD_RIGHT:
193 case MENU_CMD_OK:
195 elem = mpriv->p.current;
197 if(mpriv->level == 1)
199 mpriv->card = mpriv->p.current->num;
200 mpriv->level = 2;
201 menu_list_uninit(menu, free_entry);
202 fill_menu(menu);
204 else
206 dvb_priv_t *dvbp = (dvb_priv_t*) mpriv->config->priv;
207 cmd_name = malloc(25 + strlen(elem->p.txt));
208 if(dvbp != NULL)
209 sprintf(cmd_name, "dvb_set_channel %d %d", elem->num, mpriv->card);
210 else
211 sprintf(cmd_name, "loadfile 'dvb://%d@%s'", mpriv->card+1, elem->p.txt);
213 c = mp_input_parse_cmd(cmd_name);
214 free(cmd_name);
215 if(c)
217 if(mpriv->auto_close)
218 mp_input_queue_cmd(menu->input_ctx, mp_input_parse_cmd ("menu hide"));
219 mp_input_queue_cmd(menu->input_ctx, c);
223 break;
225 case MENU_CMD_LEFT:
226 case MENU_CMD_CANCEL:
228 elem = mpriv->p.current;
230 menu_list_uninit(menu, free_entry);
231 if(mpriv->config->count > 1)
232 mpriv->level--;
233 else
234 mpriv->level = 0;
236 if(mpriv->level > 0)
238 fill_menu(menu);
239 break;
243 default:
244 menu_list_read_cmd(menu, cmd);
249 static void close_menu(menu_t* menu)
251 dvb_free_config(mpriv->config);
252 menu_list_uninit(menu, free_entry);
256 static int open_dvb_sel(menu_t* menu, char* args)
258 mpriv->config = dvb_get_config();
259 if(mpriv->config == NULL)
260 return 0;
262 menu->draw = menu_list_draw;
263 menu->read_cmd = read_cmd;
264 menu->close = close_menu;
266 mpriv->card = 0;
267 mpriv->level = 1;
268 return fill_menu(menu);
271 const menu_info_t menu_info_dvbsel =
273 "DVB channels menu", //descr
274 "dvbsel", //name
275 "Nico", //author
276 "dvb_sel",
277 { //m_struct_t priv_st=
278 "dvb_cfg", //name
279 sizeof(struct menu_priv_s), //size
280 &cfg_dflt, //defaults
281 cfg_fields //settable fields
283 open_dvb_sel //open function