annotate libmenu/menu_chapsel.c @ 37032:4c7ab9a4f3cd

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