mplayer.c: #include mplayer.h and access_mpcontext.h
[mplayer.git] / libmenu / vf_menu.c
blobcb0af69afa1ea095091f08aa7cedeb2a035baac5
2 #include "config.h"
3 #include "mp_msg.h"
4 #include "help_mp.h"
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
10 #ifdef HAVE_MALLOC_H
11 #include <malloc.h>
12 #endif
14 #include "mplayer.h"
15 #include "mp_msg.h"
17 #include "libmpcodecs/img_format.h"
18 #include "libmpcodecs/mp_image.h"
19 #include "libmpcodecs/vf.h"
21 #include "libvo/fastmemcpy.h"
22 #include "libvo/video_out.h"
23 #include "libvo/font_load.h"
24 #include "libvo/sub.h"
25 #include "input/input.h"
26 #include "m_struct.h"
27 #include "menu.h"
28 #include "access_mpcontext.h"
31 static struct vf_priv_s* st_priv = NULL;
33 static mp_image_t* pause_mpi = NULL;
34 static int go2pause = 0;
35 /// if nonzero display menu at startup
36 int attribute_used menu_startup = 0;
38 struct vf_priv_s {
39 menu_t* root;
40 menu_t* current;
41 int passthrough;
44 static int put_image(struct vf_instance* vf, mp_image_t *mpi, double pts);
46 void vf_menu_pause_update(struct vf_instance* vf) {
47 const struct vo *video_out = mpctx_get_video_out(vf->priv->current->ctx);
48 if(pause_mpi) {
49 put_image(vf,pause_mpi, MP_NOPTS_VALUE);
50 // Don't draw the osd atm
51 //vf->control(vf,VFCTRL_DRAW_OSD,NULL);
52 vo_flip_page(video_out);
56 static int cmd_filter(mp_cmd_t* cmd, int paused, struct vf_priv_s * priv) {
58 switch(cmd->id) {
59 case MP_CMD_MENU : { // Convert txt cmd from the users into libmenu stuff
60 char* arg = cmd->args[0].v.s;
62 if (!priv->current->show && strcmp(arg,"hide"))
63 priv->current->show = 1;
64 else if(strcmp(arg,"up") == 0)
65 menu_read_cmd(priv->current,MENU_CMD_UP);
66 else if(strcmp(arg,"down") == 0)
67 menu_read_cmd(priv->current,MENU_CMD_DOWN);
68 else if(strcmp(arg,"left") == 0)
69 menu_read_cmd(priv->current,MENU_CMD_LEFT);
70 else if(strcmp(arg,"right") == 0)
71 menu_read_cmd(priv->current,MENU_CMD_RIGHT);
72 else if(strcmp(arg,"ok") == 0)
73 menu_read_cmd(priv->current,MENU_CMD_OK);
74 else if(strcmp(arg,"cancel") == 0)
75 menu_read_cmd(priv->current,MENU_CMD_CANCEL);
76 else if(strcmp(arg,"home") == 0)
77 menu_read_cmd(priv->current,MENU_CMD_HOME);
78 else if(strcmp(arg,"end") == 0)
79 menu_read_cmd(priv->current,MENU_CMD_END);
80 else if(strcmp(arg,"pageup") == 0)
81 menu_read_cmd(priv->current,MENU_CMD_PAGE_UP);
82 else if(strcmp(arg,"pagedown") == 0)
83 menu_read_cmd(priv->current,MENU_CMD_PAGE_DOWN);
84 else if(strcmp(arg,"click") == 0)
85 menu_read_cmd(priv->current,MENU_CMD_CLICK);
86 else if(strcmp(arg,"hide") == 0 || strcmp(arg,"toggle") == 0)
87 priv->current->show = 0;
88 else
89 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_UnknownMenuCommand,arg);
90 return 1;
92 case MP_CMD_SET_MENU : {
93 char* menu = cmd->args[0].v.s;
94 menu_t* l = priv->current;
95 priv->current = menu_open(menu);
96 if(!priv->current) {
97 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_FailedToOpenMenu,menu);
98 priv->current = l;
99 priv->current->show = 0;
100 } else {
101 priv->current->show = 1;
102 priv->current->parent = l;
104 return 1;
107 return 0;
110 static void get_image(struct vf_instance* vf, mp_image_t *mpi){
111 mp_image_t *dmpi;
113 if(mpi->type == MP_IMGTYPE_TEMP && (!(mpi->flags&MP_IMGFLAG_PRESERVE)) ) {
114 dmpi = vf_get_image(vf->next,mpi->imgfmt,mpi->type, mpi->flags, mpi->w, mpi->h);
115 memcpy(mpi->planes,dmpi->planes,MP_MAX_PLANES*sizeof(unsigned char*));
116 memcpy(mpi->stride,dmpi->stride,MP_MAX_PLANES*sizeof(unsigned int));
117 mpi->flags|=MP_IMGFLAG_DIRECT;
118 mpi->priv=(void*)dmpi;
119 return;
123 static int key_cb(int code) {
124 return menu_read_key(st_priv->current,code);
127 static int put_image(struct vf_instance* vf, mp_image_t *mpi, double pts){
128 mp_image_t *dmpi = NULL;
130 if (vf->priv->passthrough) {
131 dmpi=vf_get_image(vf->next, IMGFMT_MPEGPES, MP_IMGTYPE_EXPORT,
132 0, mpi->w, mpi->h);
133 dmpi->planes[0]=mpi->planes[0];
134 return vf_next_put_image(vf,dmpi, pts);
137 // Close all menu who requested it
138 while(vf->priv->current->cl && vf->priv->current != vf->priv->root) {
139 menu_t* m = vf->priv->current;
140 vf->priv->current = m->parent ? m->parent : vf->priv->root;
141 menu_close(m);
144 // Try to capture the last frame before pause, or fallback to use
145 // last captured frame.
146 if(pause_mpi && (mpi->w != pause_mpi->w || mpi->h != pause_mpi->h ||
147 mpi->imgfmt != pause_mpi->imgfmt)) {
148 free_mp_image(pause_mpi);
149 pause_mpi = NULL;
151 if (!pause_mpi) {
152 pause_mpi = alloc_mpi(mpi->w,mpi->h,mpi->imgfmt);
153 copy_mpi(pause_mpi,mpi);
155 else if (mpctx_get_osd_function(vf->priv->root->ctx) == OSD_PAUSE)
156 copy_mpi(pause_mpi,mpi);
158 if (vf->priv->current->show) {
159 if (!mp_input_key_cb)
160 mp_input_key_cb = key_cb;
162 if(mpi->flags&MP_IMGFLAG_DIRECT)
163 dmpi = mpi->priv;
164 else {
165 dmpi = vf_get_image(vf->next,mpi->imgfmt,
166 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
167 mpi->w,mpi->h);
168 copy_mpi(dmpi,mpi);
170 menu_draw(vf->priv->current,dmpi);
172 } else {
173 if(mp_input_key_cb)
174 mp_input_key_cb = NULL;
176 if(mpi->flags&MP_IMGFLAG_DIRECT)
177 dmpi = mpi->priv;
178 else {
179 dmpi = vf_get_image(vf->next,mpi->imgfmt,
180 MP_IMGTYPE_EXPORT, MP_IMGFLAG_ACCEPT_STRIDE,
181 mpi->w,mpi->h);
183 dmpi->stride[0] = mpi->stride[0];
184 dmpi->stride[1] = mpi->stride[1];
185 dmpi->stride[2] = mpi->stride[2];
186 dmpi->planes[0] = mpi->planes[0];
187 dmpi->planes[1] = mpi->planes[1];
188 dmpi->planes[2] = mpi->planes[2];
189 dmpi->priv = mpi->priv;
192 return vf_next_put_image(vf,dmpi, pts);
195 static void uninit(vf_instance_t *vf) {
196 vf->priv=NULL;
197 if(pause_mpi) {
198 free_mp_image(pause_mpi);
199 pause_mpi = NULL;
203 static int config(struct vf_instance* vf, int width, int height, int d_width, int d_height,
204 unsigned int flags, unsigned int outfmt) {
205 #ifdef CONFIG_FREETYPE
206 // here is the right place to get screen dimensions
207 if (force_load_font) {
208 force_load_font = 0;
209 load_font_ft(width,height,&vo_font,font_name,osd_font_scale_factor);
211 #endif
212 if(outfmt == IMGFMT_MPEGPES)
213 vf->priv->passthrough = 1;
214 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
217 static int query_format(struct vf_instance* vf, unsigned int fmt){
218 return vf_next_query_format(vf, fmt);
221 static int open_vf(vf_instance_t *vf, char* args){
222 if(!st_priv) {
223 st_priv = calloc(1,sizeof(struct vf_priv_s));
224 st_priv->root = st_priv->current = menu_open(args);
225 if(!st_priv->current) {
226 free(st_priv);
227 st_priv = NULL;
228 return 0;
230 st_priv->root->show = menu_startup;
231 mp_input_add_cmd_filter((mp_input_cmd_filter)cmd_filter,st_priv);
234 vf->config = config;
235 vf->query_format=query_format;
236 vf->put_image = put_image;
237 vf->get_image = get_image;
238 vf->uninit=uninit;
239 vf->priv=st_priv;
240 go2pause=0;
242 return 1;
246 vf_info_t vf_info_menu = {
247 "Internal filter for libmenu",
248 "menu",
249 "Albeu",
251 open_vf,
252 NULL