cocoa_common: refactor menu generation
[mplayer.git] / libvo / video_out.c
blobd0cd991e8bd971db117015c05c166a1e7e62fb05
1 /*
2 * libvo common functions, variables used by many/all drivers.
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25 #include <stdbool.h>
27 #include <unistd.h>
28 //#include <sys/mman.h>
30 #include "config.h"
31 #include "options.h"
32 #include "talloc.h"
33 #include "video_out.h"
34 #include "aspect.h"
35 #include "geometry.h"
36 #include "old_vo_wrapper.h"
37 #include "input/input.h"
38 #include "mp_fifo.h"
39 #include "m_config.h"
40 #include "mp_msg.h"
42 #include "osdep/shmem.h"
43 #ifdef CONFIG_X11
44 #include "x11_common.h"
45 #endif
47 int xinerama_screen = -1;
48 int xinerama_x;
49 int xinerama_y;
51 int vo_nomouse_input = 0;
52 int vo_grabpointer = 1;
53 int vo_doublebuffering = 1;
54 int vo_vsync = 0;
55 int vo_fs = 0;
56 int vo_fsmode = 0;
57 float vo_panscan = 0.0f;
58 int vo_adapter_num=0;
59 int vo_refresh_rate=0;
60 int vo_keepaspect=1;
61 int vo_rootwin=0;
62 int vo_border=1;
63 int64_t WinID = -1;
65 int vo_pts=0; // for hw decoding
66 float vo_fps=0;
68 char *vo_subdevice = NULL;
69 int vo_directrendering=0;
71 int vo_colorkey = 0x0000ff00; // default colorkey is green
72 // (0xff000000 means that colorkey has been disabled)
76 // Externally visible list of all vo drivers
78 extern struct vo_driver video_out_mga;
79 extern struct vo_driver video_out_xmga;
80 extern struct vo_driver video_out_x11;
81 extern struct vo_driver video_out_xover;
82 extern struct vo_driver video_out_vdpau;
83 extern struct vo_driver video_out_xv;
84 extern struct vo_driver video_out_gl_nosw;
85 extern struct vo_driver video_out_gl;
86 extern struct vo_driver video_out_gl_sdl;
87 extern struct vo_driver video_out_dga;
88 extern struct vo_driver video_out_sdl;
89 extern struct vo_driver video_out_3dfx;
90 extern struct vo_driver video_out_tdfxfb;
91 extern struct vo_driver video_out_s3fb;
92 extern struct vo_driver video_out_wii;
93 extern struct vo_driver video_out_null;
94 extern struct vo_driver video_out_bl;
95 extern struct vo_driver video_out_fbdev;
96 extern struct vo_driver video_out_fbdev2;
97 extern struct vo_driver video_out_svga;
98 extern struct vo_driver video_out_png;
99 extern struct vo_driver video_out_ggi;
100 extern struct vo_driver video_out_aa;
101 extern struct vo_driver video_out_caca;
102 extern struct vo_driver video_out_mpegpes;
103 extern struct vo_driver video_out_yuv4mpeg;
104 extern struct vo_driver video_out_direct3d;
105 extern struct vo_driver video_out_directx;
106 extern struct vo_driver video_out_kva;
107 extern struct vo_driver video_out_dxr3;
108 extern struct vo_driver video_out_ivtv;
109 extern struct vo_driver video_out_v4l2;
110 extern struct vo_driver video_out_jpeg;
111 extern struct vo_driver video_out_gif89a;
112 extern struct vo_driver video_out_vesa;
113 extern struct vo_driver video_out_directfb;
114 extern struct vo_driver video_out_dfbmga;
115 extern struct vo_driver video_out_tdfx_vid;
116 extern struct vo_driver video_out_xvr100;
117 extern struct vo_driver video_out_tga;
118 extern struct vo_driver video_out_corevideo;
119 extern struct vo_driver video_out_sharedbuffer;
120 extern struct vo_driver video_out_pnm;
121 extern struct vo_driver video_out_md5sum;
123 const struct vo_driver *video_out_drivers[] =
125 #ifdef CONFIG_XVR100
126 &video_out_xvr100,
127 #endif
128 #ifdef CONFIG_TDFX_VID
129 &video_out_tdfx_vid,
130 #endif
131 #ifdef CONFIG_DIRECTX
132 &video_out_directx,
133 #endif
134 #ifdef CONFIG_DIRECT3D
135 &video_out_direct3d,
136 #endif
137 #ifdef CONFIG_KVA
138 &video_out_kva,
139 #endif
140 #ifdef CONFIG_GL_COCOA
141 &video_out_gl,
142 #endif
143 #ifdef CONFIG_COREVIDEO
144 &video_out_corevideo,
145 #endif
146 #ifdef CONFIG_XMGA
147 &video_out_xmga,
148 #endif
149 #ifdef CONFIG_MGA
150 &video_out_mga,
151 #endif
152 #ifdef CONFIG_TDFXFB
153 &video_out_tdfxfb,
154 #endif
155 #ifdef CONFIG_S3FB
156 &video_out_s3fb,
157 #endif
158 #ifdef CONFIG_WII
159 &video_out_wii,
160 #endif
161 #ifdef CONFIG_3DFX
162 &video_out_3dfx,
163 #endif
164 #if CONFIG_VDPAU
165 &video_out_vdpau,
166 #endif
167 #ifdef CONFIG_XV
168 &video_out_xv,
169 #endif
170 #ifdef CONFIG_X11
171 #ifdef CONFIG_GL
172 &video_out_gl_nosw,
173 #endif
174 &video_out_x11,
175 &video_out_xover,
176 #endif
177 #ifdef CONFIG_SDL
178 &video_out_sdl,
179 #endif
180 #if (defined CONFIG_GL && !defined CONFIG_GL_COCOA)
181 &video_out_gl,
182 #endif
183 #ifdef CONFIG_GL_SDL
184 &video_out_gl_sdl,
185 #endif
186 #ifdef CONFIG_DGA
187 &video_out_dga,
188 #endif
189 #ifdef CONFIG_GGI
190 &video_out_ggi,
191 #endif
192 #ifdef CONFIG_FBDEV
193 &video_out_fbdev,
194 &video_out_fbdev2,
195 #endif
196 #ifdef CONFIG_SVGALIB
197 &video_out_svga,
198 #endif
199 #ifdef CONFIG_AA
200 &video_out_aa,
201 #endif
202 #ifdef CONFIG_CACA
203 &video_out_caca,
204 #endif
205 #ifdef CONFIG_DXR3
206 &video_out_dxr3,
207 #endif
208 #ifdef CONFIG_IVTV
209 &video_out_ivtv,
210 #endif
211 #ifdef CONFIG_V4L2_DECODER
212 &video_out_v4l2,
213 #endif
214 #ifdef CONFIG_BL
215 &video_out_bl,
216 #endif
217 #ifdef CONFIG_VESA
218 &video_out_vesa,
219 #endif
220 #ifdef CONFIG_DIRECTFB
221 &video_out_dfbmga,
222 #endif
223 &video_out_null,
224 // should not be auto-selected
225 #ifdef CONFIG_DIRECTFB
226 // vo directfb can call exit() if initialization fails
227 &video_out_directfb,
228 #endif
229 &video_out_mpegpes,
230 #ifdef CONFIG_YUV4MPEG
231 &video_out_yuv4mpeg,
232 #endif
233 &video_out_png,
234 #ifdef CONFIG_JPEG
235 &video_out_jpeg,
236 #endif
237 #ifdef CONFIG_GIF
238 &video_out_gif89a,
239 #endif
240 #ifdef CONFIG_TGA
241 &video_out_tga,
242 #endif
243 #ifdef CONFIG_PNM
244 &video_out_pnm,
245 #endif
246 #ifdef CONFIG_MD5SUM
247 &video_out_md5sum,
248 #endif
249 #ifdef CONFIG_SHAREDBUFFER
250 &video_out_sharedbuffer,
251 #endif
252 NULL
256 static int vo_preinit(struct vo *vo, char *arg)
258 if (vo->driver->privsize)
259 vo->priv = talloc_zero_size(vo, vo->driver->privsize);
260 if (vo->driver->options) {
261 struct m_config *cfg = m_config_simple(vo->driver->options);
262 m_config_initialize(cfg, vo->priv);
263 char n[50];
264 int l = snprintf(n, sizeof(n), "vo/%s", vo->driver->info->short_name);
265 assert(l < sizeof(n));
266 int r = m_config_parse_suboptions(cfg, vo->priv, n, arg);
267 talloc_free(cfg);
268 if (r < 0)
269 return r;
271 return vo->driver->preinit(vo, arg);
274 int vo_control(struct vo *vo, uint32_t request, void *data)
276 return vo->driver->control(vo, request, data);
279 // Return -1 if driver appears not to support a draw_image interface,
280 // 0 otherwise (whether the driver actually drew something or not).
281 int vo_draw_image(struct vo *vo, struct mp_image *mpi, double pts)
283 if (!vo->config_ok)
284 return 0;
285 if (vo->driver->buffer_frames) {
286 vo->driver->draw_image(vo, mpi, pts);
287 return 0;
289 vo->frame_loaded = true;
290 vo->next_pts = pts;
291 // Guaranteed to support at least DRAW_IMAGE later
292 if (vo->driver->is_new) {
293 vo->waiting_mpi = mpi;
294 return 0;
296 if (vo_control(vo, VOCTRL_DRAW_IMAGE, mpi) == VO_NOTIMPL)
297 return -1;
298 return 0;
301 int vo_redraw_frame(struct vo *vo)
303 if (!vo->config_ok || !vo->hasframe)
304 return -1;
305 if (vo_control(vo, VOCTRL_REDRAW_FRAME, NULL) == true) {
306 vo->redrawing = true;
307 return 0;
309 return -1;
312 int vo_get_buffered_frame(struct vo *vo, bool eof)
314 if (!vo->config_ok)
315 return -1;
316 if (vo->frame_loaded)
317 return 0;
318 if (!vo->driver->buffer_frames)
319 return -1;
320 vo->driver->get_buffered_frame(vo, eof);
321 return vo->frame_loaded ? 0 : -1;
324 void vo_skip_frame(struct vo *vo)
326 vo_control(vo, VOCTRL_SKIPFRAME, NULL);
327 vo->frame_loaded = false;
330 int vo_draw_frame(struct vo *vo, uint8_t *src[])
332 assert(!vo->driver->is_new);
333 if (!vo->config_ok)
334 return 0;
335 return old_vo_draw_frame(vo, src);
338 int vo_draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h, int x, int y)
340 return vo->driver->draw_slice(vo, src, stride, w, h, x, y);
343 void vo_new_frame_imminent(struct vo *vo)
345 if (!vo->driver->is_new)
346 return;
347 if (vo->driver->buffer_frames)
348 vo_control(vo, VOCTRL_NEWFRAME, NULL);
349 else {
350 vo_control(vo, VOCTRL_DRAW_IMAGE, vo->waiting_mpi);
351 vo->waiting_mpi = NULL;
355 void vo_draw_osd(struct vo *vo, struct osd_state *osd)
357 if (!vo->config_ok)
358 return;
359 vo->driver->draw_osd(vo, osd);
362 void vo_flip_page(struct vo *vo, unsigned int pts_us, int duration)
364 if (!vo->config_ok)
365 return;
366 if (!vo->redrawing) {
367 vo->frame_loaded = false;
368 vo->next_pts = MP_NOPTS_VALUE;
370 vo->want_redraw = false;
371 vo->redrawing = false;
372 if (vo->driver->flip_page_timed)
373 vo->driver->flip_page_timed(vo, pts_us, duration);
374 else
375 vo->driver->flip_page(vo);
376 vo->hasframe = true;
379 void vo_check_events(struct vo *vo)
381 if (!vo->config_ok) {
382 if (vo->registered_fd != -1)
383 mp_input_rm_key_fd(vo->input_ctx, vo->registered_fd);
384 vo->registered_fd = -1;
385 return;
387 vo->driver->check_events(vo);
390 void vo_seek_reset(struct vo *vo)
392 vo_control(vo, VOCTRL_RESET, NULL);
393 vo->frame_loaded = false;
394 vo->hasframe = false;
397 void vo_destroy(struct vo *vo)
399 if (vo->registered_fd != -1)
400 mp_input_rm_key_fd(vo->input_ctx, vo->registered_fd);
401 vo->driver->uninit(vo);
402 talloc_free(vo);
405 void list_video_out(void)
407 int i = 0;
408 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video output drivers:\n");
409 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_OUTPUTS\n");
410 while (video_out_drivers[i]) {
411 const vo_info_t *info = video_out_drivers[i++]->info;
412 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\t%s\t%s\n", info->short_name, info->name);
414 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\n");
417 struct vo *init_best_video_out(struct MPOpts *opts, struct vo_x11_state *x11,
418 struct mp_fifo *key_fifo,
419 struct input_ctx *input_ctx)
421 char **vo_list = opts->video_driver_list;
422 int i;
423 struct vo *vo = talloc_ptrtype(NULL, vo);
424 struct vo initial_values = {
425 .opts = opts,
426 .x11 = x11,
427 .key_fifo = key_fifo,
428 .input_ctx = input_ctx,
429 .event_fd = -1,
430 .registered_fd = -1,
432 // first try the preferred drivers, with their optional subdevice param:
433 if (vo_list && vo_list[0])
434 while (vo_list[0][0]) {
435 char *name = strdup(vo_list[0]);
436 vo_subdevice = strchr(name,':');
437 if (!strcmp(name, "pgm"))
438 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "The pgm video output driver has been replaced by -vo pnm:pgmyuv.\n");
439 if (!strcmp(name, "md5"))
440 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "The md5 video output driver has been replaced by -vo md5sum.\n");
441 if (vo_subdevice) {
442 vo_subdevice[0] = 0;
443 ++vo_subdevice;
445 for (i = 0; video_out_drivers[i]; i++) {
446 const struct vo_driver *video_driver = video_out_drivers[i];
447 const vo_info_t *info = video_driver->info;
448 if (!strcmp(info->short_name, name)) {
449 // name matches, try it
450 *vo = initial_values;
451 vo->driver = video_driver;
452 if (!vo_preinit(vo, vo_subdevice)) {
453 free(name);
454 return vo; // success!
456 talloc_free_children(vo);
459 // continue...
460 free(name);
461 ++vo_list;
462 if (!(vo_list[0]))
463 return NULL; // do NOT fallback to others
465 // now try the rest...
466 vo_subdevice = NULL;
467 for (i = 0; video_out_drivers[i]; i++) {
468 const struct vo_driver *video_driver = video_out_drivers[i];
469 *vo = initial_values;
470 vo->driver = video_driver;
471 if (!vo_preinit(vo, vo_subdevice))
472 return vo; // success!
473 talloc_free_children(vo);
475 free(vo);
476 return NULL;
479 static int event_fd_callback(void *ctx, int fd)
481 struct vo *vo = ctx;
482 vo_check_events(vo);
483 return MP_INPUT_NOTHING;
486 int vo_config(struct vo *vo, uint32_t width, uint32_t height,
487 uint32_t d_width, uint32_t d_height, uint32_t flags,
488 uint32_t format)
490 struct MPOpts *opts = vo->opts;
491 panscan_init(vo);
492 aspect_save_orig(vo, width, height);
493 aspect_save_prescale(vo, d_width, d_height);
495 if (vo_control(vo, VOCTRL_UPDATE_SCREENINFO, NULL) == VO_TRUE) {
496 aspect(vo, &d_width, &d_height, A_NOZOOM);
497 vo->dx = (int)(opts->vo_screenwidth - d_width) / 2;
498 vo->dy = (int)(opts->vo_screenheight - d_height) / 2;
499 geometry(&vo->dx, &vo->dy, &d_width, &d_height,
500 opts->vo_screenwidth, opts->vo_screenheight);
501 geometry_xy_changed |= xinerama_screen >= 0;
502 vo->dx += xinerama_x;
503 vo->dy += xinerama_y;
504 vo->dwidth = d_width;
505 vo->dheight = d_height;
508 int ret = vo->driver->config(vo, width, height, d_width, d_height, flags,
509 format);
510 vo->config_ok = (ret == 0);
511 vo->config_count += vo->config_ok;
512 if (vo->registered_fd == -1 && vo->event_fd != -1 && vo->config_ok) {
513 mp_input_add_key_fd(vo->input_ctx, vo->event_fd, 1, event_fd_callback,
514 NULL, vo);
515 vo->registered_fd = vo->event_fd;
517 vo->frame_loaded = false;
518 vo->waiting_mpi = NULL;
519 vo->redrawing = false;
520 vo->hasframe = false;
521 return ret;
525 * \brief lookup an integer in a table, table must have 0 as the last key
526 * \param key key to search for
527 * \result translation corresponding to key or "to" value of last mapping
528 * if not found.
530 int lookup_keymap_table(const struct mp_keymap *map, int key) {
531 while (map->from && map->from != key) map++;
532 return map->to;
536 * \brief helper function for the kind of panscan-scaling that needs a source
537 * and destination rectangle like Direct3D and VDPAU
539 static void src_dst_split_scaling(int src_size, int dst_size, int scaled_src_size,
540 int *src_start, int *src_end, int *dst_start, int *dst_end) {
541 if (scaled_src_size > dst_size) {
542 int border = src_size * (scaled_src_size - dst_size) / scaled_src_size;
543 // round to a multiple of 2, this is at least needed for vo_direct3d and ATI cards
544 border = (border / 2 + 1) & ~1;
545 *src_start = border;
546 *src_end = src_size - border;
547 *dst_start = 0;
548 *dst_end = dst_size;
549 } else {
550 *src_start = 0;
551 *src_end = src_size;
552 *dst_start = (dst_size - scaled_src_size) / 2;
553 *dst_end = *dst_start + scaled_src_size;
558 * Calculate the appropriate source and destination rectangle to
559 * get a correctly scaled picture, including pan-scan.
560 * Can be extended to take future cropping support into account.
562 * \param crop specifies the cropping border size in the left, right, top and bottom members, may be NULL
563 * \param borders the border values as e.g. EOSD (ASS) and properly placed DVD highlight support requires,
564 * may be NULL and only left and top are currently valid.
566 void calc_src_dst_rects(struct vo *vo, int src_width, int src_height,
567 struct vo_rect *src, struct vo_rect *dst,
568 struct vo_rect *borders, const struct vo_rect *crop)
570 static const struct vo_rect no_crop = {0, 0, 0, 0, 0, 0};
571 int scaled_width = 0;
572 int scaled_height = 0;
573 if (!crop) crop = &no_crop;
574 src_width -= crop->left + crop->right;
575 src_height -= crop->top + crop->bottom;
576 if (src_width < 2) src_width = 2;
577 if (src_height < 2) src_height = 2;
578 dst->left = 0; dst->right = vo->dwidth;
579 dst->top = 0; dst->bottom = vo->dheight;
580 src->left = 0; src->right = src_width;
581 src->top = 0; src->bottom = src_height;
582 if (borders) {
583 borders->left = 0; borders->top = 0;
585 if (aspect_scaling()) {
586 aspect(vo, &scaled_width, &scaled_height, A_WINZOOM);
587 panscan_calc_windowed(vo);
588 scaled_width += vo->panscan_x;
589 scaled_height += vo->panscan_y;
590 if (borders) {
591 borders->left = (vo->dwidth - scaled_width ) / 2;
592 borders->top = (vo->dheight - scaled_height) / 2;
594 src_dst_split_scaling(src_width, vo->dwidth, scaled_width,
595 &src->left, &src->right, &dst->left, &dst->right);
596 src_dst_split_scaling(src_height, vo->dheight, scaled_height,
597 &src->top, &src->bottom, &dst->top, &dst->bottom);
599 src->left += crop->left; src->right += crop->left;
600 src->top += crop->top; src->bottom += crop->top;
601 src->width = src->right - src->left;
602 src->height = src->bottom - src->top;
603 dst->width = dst->right - dst->left;
604 dst->height = dst->bottom - dst->top;
607 // Return the window title the VO should set. Always returns a null terminated
608 // string. The string is valid until frontend code is invoked again. Copy it if
609 // you need to keep the string for an extended period of time.
610 const char *vo_get_window_title(struct vo *vo)
612 if (vo->opts->vo_wintitle) {
613 return vo->opts->vo_wintitle;
614 } else {
615 return "mplayer2";
620 * Generates a mouse movement message if those are enable and sends it
621 * to the "main" MPlayer.
623 * \param posx new x position of mouse
624 * \param posy new y position of mouse
626 void vo_mouse_movement(struct vo *vo, int posx, int posy)
628 char cmd_str[40];
629 if (!enable_mouse_movements)
630 return;
631 snprintf(cmd_str, sizeof(cmd_str), "set_mouse_pos %i %i", posx, posy);
632 mp_input_queue_cmd(vo->input_ctx, mp_input_parse_cmd(cmd_str));
635 #if defined(CONFIG_FBDEV) || defined(CONFIG_VESA)
636 /* Borrowed from vo_fbdev.c
637 Monitor ranges related functions*/
639 char *monitor_hfreq_str = NULL;
640 char *monitor_vfreq_str = NULL;
641 char *monitor_dotclock_str = NULL;
643 float range_max(range_t *r)
645 float max = 0;
647 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
648 if (max < r->max) max = r->max;
649 return max;
653 int in_range(range_t *r, float f)
655 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
656 if (f >= r->min && f <= r->max)
657 return 1;
658 return 0;
661 range_t *str2range(char *s)
663 float tmp_min, tmp_max;
664 char *endptr = s; // to start the loop
665 range_t *r = NULL;
666 int i;
668 if (!s)
669 return NULL;
670 for (i = 0; *endptr; i++) {
671 if (*s == ',')
672 goto out_err;
673 if (!(r = realloc(r, sizeof(*r) * (i + 2)))) {
674 mp_msg(MSGT_GLOBAL, MSGL_WARN,"can't realloc 'r'\n");
675 return NULL;
677 tmp_min = strtod(s, &endptr);
678 if (*endptr == 'k' || *endptr == 'K') {
679 tmp_min *= 1000.0;
680 endptr++;
681 } else if (*endptr == 'm' || *endptr == 'M') {
682 tmp_min *= 1000000.0;
683 endptr++;
685 if (*endptr == '-') {
686 tmp_max = strtod(endptr + 1, &endptr);
687 if (*endptr == 'k' || *endptr == 'K') {
688 tmp_max *= 1000.0;
689 endptr++;
690 } else if (*endptr == 'm' || *endptr == 'M') {
691 tmp_max *= 1000000.0;
692 endptr++;
694 if (*endptr != ',' && *endptr)
695 goto out_err;
696 } else if (*endptr == ',' || !*endptr) {
697 tmp_max = tmp_min;
698 } else
699 goto out_err;
700 r[i].min = tmp_min;
701 r[i].max = tmp_max;
702 if (r[i].min < 0 || r[i].max < 0)
703 goto out_err;
704 s = endptr + 1;
706 r[i].min = r[i].max = -1;
707 return r;
708 out_err:
709 free(r);
710 return NULL;
713 /* Borrowed from vo_fbdev.c END */
714 #endif