1 /*
2 * Support chapter list and selection.
3 *
4 * Copyright (C) 2006-2007 Benjamin Zores <ben A geexbox P org>
5 * Copyright (C) 2007 Ulion <ulion A gmail P com>
6 *
7 * This file is part of MPlayer.
8 *
9 * MPlayer is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MPlayer is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "libavutil/attributes.h"
29
30 #include "config.h"
31
32 #include "m_struct.h"
33 #include "m_option.h"
34 #include "input/input.h"
35
36 #include "stream/stream.h"
37 #include "libmpdemux/demuxer.h"
38 #include "access_mpcontext.h"
39
40 #include "libmpcodecs/mp_image.h"
41
42 #include "menu.h"
43 #include "menu_list.h"
44
45 struct list_entry_s {
46 struct list_entry p;
47 int cid;
48 };
49
50 struct menu_priv_s {
51 menu_list_priv_t p;
52 char* title;
53 int auto_close;
54 char* fmt_with_time;
55 };
56
57 static const struct menu_priv_s cfg_dflt = {
58 MENU_LIST_PRIV_DFLT,
59 "Select chapter",
60 0,
61 "${chapter_name} [${start}]"
62 };
63
64 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s,m)
65
66 static const m_option_t cfg_fields[] = {
67 MENU_LIST_PRIV_FIELDS,
68 { "title", ST_OFF (title), CONF_TYPE_STRING, 0, 0, 0, NULL },
69 { "auto-close", ST_OFF (auto_close), CONF_TYPE_FLAG, 0, 0, 1, NULL },
70 { "fmt-with-time", ST_OFF (fmt_with_time), CONF_TYPE_STRING, 0, 0, 0, NULL },
71 { NULL, NULL, NULL, 0, 0, 0, NULL }
72 };
73
fmt_replace(const char * fmt,const char * chapter_name,const char * start)74 static char *fmt_replace(const char *fmt, const char *chapter_name,
75 const char *start) {
76 static const char ctag[] = "${chapter_name}";
77 static const char stag[] = "${start}";
78 int l = strlen(fmt);
79 int cl = strlen(chapter_name);
80 int sl = strlen(start);
81 char *str = malloc(l + cl + sl + 1);
82 char *p;
83 strcpy(str, fmt);
84 p = strstr(str, ctag);
85 if (p) {
86 memmove(p+cl, p+sizeof(ctag)-1, str+l+1 - (p+sizeof(ctag)-1));
87 memcpy(p, chapter_name, cl);
88 l -= sizeof(ctag) + 1;
89 l += cl;
90 }
91 p = strstr(str, stag);
92 if (p) {
93 memmove(p+sl, p+sizeof(stag)-1, str+l+1 - (p+sizeof(stag)-1));
94 memcpy(p, start, sl);
95 l -= sizeof(stag) + 1;
96 l += sl;
97 }
98 return str;
99 }
100
fill_menu(menu_t * menu)101 static int fill_menu (menu_t* menu)
102 {
103 list_entry_t* e;
104 int cid, chapter_num = 0;
105 int start_time;
106 demuxer_t* demuxer = mpctx_get_demuxer(menu->ctx);
107
108 if (demuxer)
109 chapter_num = demuxer_chapter_count(demuxer);
110 if (chapter_num > 0) {
111 menu_list_init (menu);
112 for (cid = 0; cid < chapter_num; ++cid)
113 if ((e = calloc (1, sizeof (list_entry_t))) != NULL) {
114 e->cid = cid + 1;
115 e->p.next = NULL;
116 e->p.txt = demuxer_chapter_display_name(demuxer, cid);
117 start_time = demuxer_chapter_time(demuxer, cid, NULL);
118 if (start_time >= 0) {
119 char timestr[13];
120 char *tmp;
121 int hour = start_time / 3600;
122 int minute = (start_time / 60) % 60;
123 int seconds = start_time % 60;
124 sprintf(timestr,"%02d:%02d:%02d", hour, minute, seconds);
125
126 tmp = fmt_replace(menu->priv->fmt_with_time, e->p.txt, timestr);
127 free(e->p.txt);
128 e->p.txt = tmp;
129 }
130 menu_list_add_entry(menu, e);
131 }
132 }
133 else
134 menu_list_read_cmd(menu, MENU_CMD_CANCEL);
135
136 return 1;
137 }
138
read_cmd(menu_t * menu,int cmd)139 static void read_cmd (menu_t* menu, int cmd)
140 {
141 switch (cmd) {
142 case MENU_CMD_RIGHT:
143 case MENU_CMD_OK: {
144 char cmdbuf[26];
145 sprintf(cmdbuf, "seek_chapter %d 1", menu->priv->p.current->cid);
146 mp_input_queue_cmd(mp_input_parse_cmd(cmdbuf));
147 if (menu->priv->auto_close)
148 mp_input_queue_cmd(mp_input_parse_cmd("menu hide"));
149 break;
150 }
151 default:
152 menu_list_read_cmd (menu, cmd);
153 }
154 }
155
close_cs(menu_t * menu)156 static void close_cs (menu_t* menu)
157 {
158 menu_list_uninit (menu, NULL);
159 }
160
open_cs(menu_t * menu,char * av_unused args)161 static int open_cs (menu_t* menu, char* av_unused args)
162 {
163 menu->draw = menu_list_draw;
164 menu->read_cmd = read_cmd;
165 menu->close = close_cs;
166 menu->priv->p.title = menu->priv->title;
167
168 return fill_menu (menu);
169 }
170
171 const menu_info_t menu_info_chapsel = {
172 "Chapter selector menu",
173 "chapsel",
174 "Benjamin Zores & Ulion",
175 "",
176 {
177 "chapsel_cfg",
178 sizeof(struct menu_priv_s),
179 &cfg_dflt,
180 cfg_fields
181 },
182 open_cs
183 };
184