get some new maintainers
[mplayer/glamo.git] / libmenu / vf_menu.c
blob8ef88a1d6d997db7cf598d2421dbf4af6e3a8fbc
2 #include "../config.h"
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
8 #ifdef HAVE_MALLOC_H
9 #include <malloc.h>
10 #endif
12 #include "../mp_msg.h"
14 #include "../libmpcodecs/img_format.h"
15 #include "../libmpcodecs/mp_image.h"
16 #include "../libmpcodecs/vf.h"
18 #include "../libvo/fastmemcpy.h"
19 #include "../libvo/video_out.h"
20 #include "../libvo/font_load.h"
21 #include "../input/input.h"
22 #include "../m_struct.h"
23 #include "menu.h"
25 extern vo_functions_t* video_out;
28 static struct vf_priv_s* st_priv = NULL;
30 static mp_image_t* pause_mpi = NULL;
31 static int go2pause = 0;
33 struct vf_priv_s {
34 menu_t* root;
35 menu_t* current;
38 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi);
40 static mp_image_t* alloc_mpi(int w, int h, uint32_t fmt) {
41 mp_image_t* mpi = new_mp_image(w,h);
43 mp_image_setfmt(mpi,fmt);
44 // IF09 - allocate space for 4. plane delta info - unused
45 if (mpi->imgfmt == IMGFMT_IF09)
47 mpi->planes[0]=memalign(64, mpi->bpp*mpi->width*(mpi->height+2)/8+
48 mpi->chroma_width*mpi->chroma_height);
49 /* delta table, just for fun ;) */
50 mpi->planes[3]=mpi->planes[0]+2*(mpi->chroma_width*mpi->chroma_height);
52 else
53 mpi->planes[0]=memalign(64, mpi->bpp*mpi->width*(mpi->height+2)/8);
54 if(mpi->flags&MP_IMGFLAG_PLANAR){
55 // YV12/I420/YVU9/IF09. feel free to add other planar formats here...
56 if(!mpi->stride[0]) mpi->stride[0]=mpi->width;
57 if(!mpi->stride[1]) mpi->stride[1]=mpi->stride[2]=mpi->chroma_width;
58 if(mpi->flags&MP_IMGFLAG_SWAPPED){
59 // I420/IYUV (Y,U,V)
60 mpi->planes[1]=mpi->planes[0]+mpi->width*mpi->height;
61 mpi->planes[2]=mpi->planes[1]+mpi->chroma_width*mpi->chroma_height;
62 } else {
63 // YV12,YVU9,IF09 (Y,V,U)
64 mpi->planes[2]=mpi->planes[0]+mpi->width*mpi->height;
65 mpi->planes[1]=mpi->planes[2]+mpi->chroma_width*mpi->chroma_height;
67 } else {
68 if(!mpi->stride[0]) mpi->stride[0]=mpi->width*mpi->bpp/8;
70 mpi->flags|=MP_IMGFLAG_ALLOCATED;
72 return mpi;
75 void vf_menu_pause_update(struct vf_instance_s* vf) {
76 if(pause_mpi) {
77 put_image(vf,pause_mpi);
78 // Don't draw the osd atm
79 //vf->control(vf,VFCTRL_DRAW_OSD,NULL);
80 video_out->flip_page();
84 static int cmd_filter(mp_cmd_t* cmd, int paused, struct vf_priv_s * priv) {
86 switch(cmd->id) {
87 case MP_CMD_PAUSE :
88 #if 0 // http://mplayerhq.hu/pipermail/mplayer-dev-eng/2003-March/017331.html
89 if(!paused && !go2pause) { // Initial pause cmd -> wait the next put_image
90 go2pause = 1;
91 return 1;
93 #endif
94 if(go2pause == 2) // Msg resent by put_image after saving the image
95 go2pause = 0;
96 break;
97 case MP_CMD_MENU : { // Convert txt cmd from the users into libmenu stuff
98 char* arg = cmd->args[0].v.s;
100 if(!priv->current->show)
101 priv->current->show = 1;
102 else if(strcmp(arg,"up") == 0)
103 menu_read_cmd(priv->current,MENU_CMD_UP);
104 else if(strcmp(arg,"down") == 0)
105 menu_read_cmd(priv->current,MENU_CMD_DOWN);
106 else if(strcmp(arg,"ok") == 0)
107 menu_read_cmd(priv->current,MENU_CMD_OK);
108 else if(strcmp(arg,"cancel") == 0)
109 menu_read_cmd(priv->current,MENU_CMD_CANCEL);
110 else if(strcmp(arg,"hide") == 0)
111 priv->current->show = 0;
112 else
113 printf("Unknown menu command: '%s'\n",arg);
114 return 1;
116 case MP_CMD_SET_MENU : {
117 char* menu = cmd->args[0].v.s;
118 menu_t* l = priv->current;
119 priv->current = menu_open(menu);
120 if(!priv->current) {
121 printf("Failed to open menu '%s'\n",menu);
122 priv->current = l;
123 priv->current->show = 0;
124 } else {
125 priv->current->show = 1;
126 priv->current->parent = l;
128 return 1;
131 return 0;
134 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi){
135 mp_image_t *dmpi;
137 if(mpi->type == MP_IMGTYPE_TEMP && (!(mpi->flags&MP_IMGFLAG_PRESERVE)) ) {
138 dmpi = vf_get_image(vf->next,mpi->imgfmt,mpi->type, mpi->flags, mpi->w, mpi->h);
139 memcpy(mpi->planes,dmpi->planes,MP_MAX_PLANES*sizeof(unsigned char*));
140 memcpy(mpi->stride,dmpi->stride,MP_MAX_PLANES*sizeof(unsigned int));
141 mpi->flags|=MP_IMGFLAG_DIRECT;
142 mpi->priv=(void*)dmpi;
143 return;
147 static void key_cb(int code) {
148 menu_read_key(st_priv->current,code);
153 inline static void copy_mpi(mp_image_t *dmpi, mp_image_t *mpi) {
154 if(mpi->flags&MP_IMGFLAG_PLANAR){
155 memcpy_pic(dmpi->planes[0],mpi->planes[0], mpi->w, mpi->h,
156 dmpi->stride[0],mpi->stride[0]);
157 memcpy_pic(dmpi->planes[1],mpi->planes[1], mpi->chroma_width, mpi->chroma_height,
158 dmpi->stride[1],mpi->stride[1]);
159 memcpy_pic(dmpi->planes[2], mpi->planes[2], mpi->chroma_width, mpi->chroma_height,
160 dmpi->stride[2],mpi->stride[2]);
161 } else {
162 memcpy_pic(dmpi->planes[0],mpi->planes[0],
163 mpi->w*(dmpi->bpp/8), mpi->h,
164 dmpi->stride[0],mpi->stride[0]);
170 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){
171 mp_image_t *dmpi = NULL;
173 if(vf->priv->current->show
174 || (vf->priv->current->parent && vf->priv->current->parent->show)) {
175 // Close all menu who requested it
176 while(vf->priv->current->cl && vf->priv->current != vf->priv->root) {
177 menu_t* m = vf->priv->current;
178 vf->priv->current = m->parent ? m->parent : vf->priv->root;
179 menu_close(m);
182 // Step 1 : save the picture
183 while(go2pause == 1) {
184 static char delay = 0; // Hack : wait the 2 frame to be sure to show the right picture
185 delay ^= 1; // after a seek
186 if(!delay) break;
188 if(pause_mpi && (mpi->w != pause_mpi->w || mpi->h != pause_mpi->h ||
189 mpi->imgfmt != pause_mpi->imgfmt)) {
190 free_mp_image(pause_mpi);
191 pause_mpi = NULL;
193 if(!pause_mpi)
194 pause_mpi = alloc_mpi(mpi->w,mpi->h,mpi->imgfmt);
195 copy_mpi(pause_mpi,mpi);
196 mp_input_queue_cmd(mp_input_parse_cmd("pause"));
197 go2pause = 2;
198 break;
201 // Grab // Ungrab the keys
202 if(!mp_input_key_cb && vf->priv->current->show)
203 mp_input_key_cb = key_cb;
204 if(mp_input_key_cb && !vf->priv->current->show)
205 mp_input_key_cb = NULL;
207 if(mpi->flags&MP_IMGFLAG_DIRECT)
208 dmpi = mpi->priv;
209 else {
210 dmpi = vf_get_image(vf->next,mpi->imgfmt,
211 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
212 mpi->w,mpi->h);
213 copy_mpi(dmpi,mpi);
215 menu_draw(vf->priv->current,dmpi);
217 } else {
218 if(mp_input_key_cb)
219 mp_input_key_cb = NULL;
220 dmpi = vf_get_image(vf->next,mpi->imgfmt,
221 MP_IMGTYPE_EXPORT, MP_IMGFLAG_ACCEPT_STRIDE,
222 mpi->w,mpi->h);
224 dmpi->stride[0] = mpi->stride[0];
225 dmpi->stride[1] = mpi->stride[1];
226 dmpi->stride[2] = mpi->stride[2];
227 dmpi->planes[0] = mpi->planes[0];
228 dmpi->planes[1] = mpi->planes[1];
229 dmpi->planes[2] = mpi->planes[2];
230 dmpi->priv = mpi->priv;
232 return vf_next_put_image(vf,dmpi);
235 static void uninit(vf_instance_t *vf) {
236 vf->priv=NULL;
237 if(pause_mpi) {
238 free_mp_image(pause_mpi);
239 pause_mpi = NULL;
243 static int config(struct vf_instance_s* vf, int width, int height, int d_width, int d_height,
244 unsigned int flags, unsigned int outfmt) {
245 #ifdef HAVE_FREETYPE
246 // here is the right place to get screen dimensions
247 if (force_load_font) {
248 force_load_font = 0;
249 load_font_ft(width,height);
251 #endif
252 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
254 static int open(vf_instance_t *vf, char* args){
255 if(!st_priv) {
256 st_priv = calloc(1,sizeof(struct vf_priv_s));
257 st_priv->root = st_priv->current = menu_open(args);
258 if(!st_priv->current) {
259 free(st_priv);
260 st_priv = NULL;
261 return 0;
263 mp_input_add_cmd_filter((mp_input_cmd_filter)cmd_filter,st_priv);
266 vf->config = config;
267 vf->put_image = put_image;
268 vf->get_image = get_image;
269 vf->uninit=uninit;
270 vf->priv=st_priv;
271 go2pause=0;
273 return 1;
277 vf_info_t vf_info_menu = {
278 "Internal filter for libmenu",
279 "menu",
280 "Albeu",
282 open,
283 NULL