VO: don't keep a persistent X11 connection across VOs
[mplayer.git] / libvo / video_out.c
blobf657e0edccbfe039c330be71e4005face46cadae
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 int vo_directrendering=0;
70 int vo_colorkey = 0x0000ff00; // default colorkey is green
71 // (0xff000000 means that colorkey has been disabled)
75 // Externally visible list of all vo drivers
77 extern struct vo_driver video_out_x11;
78 extern struct vo_driver video_out_vdpau;
79 extern struct vo_driver video_out_xv;
80 extern struct vo_driver video_out_gl_nosw;
81 extern struct vo_driver video_out_gl;
82 extern struct vo_driver video_out_gl_sdl;
83 extern struct vo_driver video_out_sdl;
84 extern struct vo_driver video_out_null;
85 extern struct vo_driver video_out_png;
86 extern struct vo_driver video_out_caca;
87 extern struct vo_driver video_out_yuv4mpeg;
88 extern struct vo_driver video_out_direct3d;
89 extern struct vo_driver video_out_directx;
90 extern struct vo_driver video_out_v4l2;
91 extern struct vo_driver video_out_jpeg;
92 extern struct vo_driver video_out_gif89a;
93 extern struct vo_driver video_out_directfb;
94 extern struct vo_driver video_out_tga;
95 extern struct vo_driver video_out_corevideo;
96 extern struct vo_driver video_out_sharedbuffer;
97 extern struct vo_driver video_out_pnm;
98 extern struct vo_driver video_out_md5sum;
100 const struct vo_driver *video_out_drivers[] =
102 #ifdef CONFIG_DIRECTX
103 &video_out_directx,
104 #endif
105 #ifdef CONFIG_DIRECT3D
106 &video_out_direct3d,
107 #endif
108 #ifdef CONFIG_GL_COCOA
109 &video_out_gl,
110 #endif
111 #ifdef CONFIG_COREVIDEO
112 &video_out_corevideo,
113 #endif
114 #if CONFIG_VDPAU
115 &video_out_vdpau,
116 #endif
117 #ifdef CONFIG_XV
118 &video_out_xv,
119 #endif
120 #ifdef CONFIG_X11
121 #ifdef CONFIG_GL
122 &video_out_gl_nosw,
123 #endif
124 &video_out_x11,
125 #endif
126 #ifdef CONFIG_SDL
127 &video_out_sdl,
128 #endif
129 #if (defined CONFIG_GL && !defined CONFIG_GL_COCOA)
130 &video_out_gl,
131 #endif
132 #ifdef CONFIG_GL_SDL
133 &video_out_gl_sdl,
134 #endif
135 #ifdef CONFIG_CACA
136 &video_out_caca,
137 #endif
138 #ifdef CONFIG_V4L2_DECODER
139 &video_out_v4l2,
140 #endif
141 &video_out_null,
142 // should not be auto-selected
143 #ifdef CONFIG_DIRECTFB
144 // vo directfb can call exit() if initialization fails
145 &video_out_directfb,
146 #endif
147 #ifdef CONFIG_YUV4MPEG
148 &video_out_yuv4mpeg,
149 #endif
150 &video_out_png,
151 #ifdef CONFIG_JPEG
152 &video_out_jpeg,
153 #endif
154 #ifdef CONFIG_GIF
155 &video_out_gif89a,
156 #endif
157 #ifdef CONFIG_TGA
158 &video_out_tga,
159 #endif
160 #ifdef CONFIG_PNM
161 &video_out_pnm,
162 #endif
163 #ifdef CONFIG_MD5SUM
164 &video_out_md5sum,
165 #endif
166 #ifdef CONFIG_SHAREDBUFFER
167 &video_out_sharedbuffer,
168 #endif
169 NULL
173 static int vo_preinit(struct vo *vo, char *arg)
175 if (vo->driver->privsize)
176 vo->priv = talloc_zero_size(vo, vo->driver->privsize);
177 if (vo->driver->options) {
178 struct m_config *cfg = m_config_simple(vo->driver->options);
179 m_config_initialize(cfg, vo->priv);
180 char n[50];
181 int l = snprintf(n, sizeof(n), "vo/%s", vo->driver->info->short_name);
182 assert(l < sizeof(n));
183 int r = m_config_parse_suboptions(cfg, vo->priv, n, arg);
184 talloc_free(cfg);
185 if (r < 0)
186 return r;
188 return vo->driver->preinit(vo, arg);
191 int vo_control(struct vo *vo, uint32_t request, void *data)
193 return vo->driver->control(vo, request, data);
196 // Return -1 if driver appears not to support a draw_image interface,
197 // 0 otherwise (whether the driver actually drew something or not).
198 int vo_draw_image(struct vo *vo, struct mp_image *mpi, double pts)
200 if (!vo->config_ok)
201 return 0;
202 if (vo->driver->buffer_frames) {
203 vo->driver->draw_image(vo, mpi, pts);
204 return 0;
206 vo->frame_loaded = true;
207 vo->next_pts = pts;
208 // Guaranteed to support at least DRAW_IMAGE later
209 if (vo->driver->is_new) {
210 vo->waiting_mpi = mpi;
211 return 0;
213 if (vo_control(vo, VOCTRL_DRAW_IMAGE, mpi) == VO_NOTIMPL)
214 return -1;
215 return 0;
218 int vo_redraw_frame(struct vo *vo)
220 if (!vo->config_ok || !vo->hasframe)
221 return -1;
222 if (vo_control(vo, VOCTRL_REDRAW_FRAME, NULL) == true) {
223 vo->redrawing = true;
224 return 0;
226 return -1;
229 int vo_get_buffered_frame(struct vo *vo, bool eof)
231 if (!vo->config_ok)
232 return -1;
233 if (vo->frame_loaded)
234 return 0;
235 if (!vo->driver->buffer_frames)
236 return -1;
237 vo->driver->get_buffered_frame(vo, eof);
238 return vo->frame_loaded ? 0 : -1;
241 void vo_skip_frame(struct vo *vo)
243 vo_control(vo, VOCTRL_SKIPFRAME, NULL);
244 vo->frame_loaded = false;
247 int vo_draw_frame(struct vo *vo, uint8_t *src[])
249 assert(!vo->driver->is_new);
250 if (!vo->config_ok)
251 return 0;
252 return old_vo_draw_frame(vo, src);
255 int vo_draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h, int x, int y)
257 return vo->driver->draw_slice(vo, src, stride, w, h, x, y);
260 void vo_new_frame_imminent(struct vo *vo)
262 if (!vo->driver->is_new)
263 return;
264 if (vo->driver->buffer_frames)
265 vo_control(vo, VOCTRL_NEWFRAME, NULL);
266 else {
267 vo_control(vo, VOCTRL_DRAW_IMAGE, vo->waiting_mpi);
268 vo->waiting_mpi = NULL;
272 void vo_draw_osd(struct vo *vo, struct osd_state *osd)
274 if (!vo->config_ok)
275 return;
276 vo->driver->draw_osd(vo, osd);
279 void vo_flip_page(struct vo *vo, unsigned int pts_us, int duration)
281 if (!vo->config_ok)
282 return;
283 if (!vo->redrawing) {
284 vo->frame_loaded = false;
285 vo->next_pts = MP_NOPTS_VALUE;
287 vo->want_redraw = false;
288 vo->redrawing = false;
289 if (vo->driver->flip_page_timed)
290 vo->driver->flip_page_timed(vo, pts_us, duration);
291 else
292 vo->driver->flip_page(vo);
293 vo->hasframe = true;
296 void vo_check_events(struct vo *vo)
298 if (!vo->config_ok) {
299 if (vo->registered_fd != -1)
300 mp_input_rm_key_fd(vo->input_ctx, vo->registered_fd);
301 vo->registered_fd = -1;
302 return;
304 vo->driver->check_events(vo);
307 void vo_seek_reset(struct vo *vo)
309 vo_control(vo, VOCTRL_RESET, NULL);
310 vo->frame_loaded = false;
311 vo->hasframe = false;
314 void vo_destroy(struct vo *vo)
316 if (vo->registered_fd != -1)
317 mp_input_rm_key_fd(vo->input_ctx, vo->registered_fd);
318 vo->driver->uninit(vo);
319 talloc_free(vo);
322 void list_video_out(void)
324 int i = 0;
325 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video output drivers:\n");
326 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_OUTPUTS\n");
327 while (video_out_drivers[i]) {
328 const vo_info_t *info = video_out_drivers[i++]->info;
329 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\t%s\t%s\n", info->short_name, info->name);
331 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\n");
334 struct vo *init_best_video_out(struct MPOpts *opts, struct mp_fifo *key_fifo,
335 struct input_ctx *input_ctx)
337 char **vo_list = opts->video_driver_list;
338 int i;
339 struct vo *vo = talloc_ptrtype(NULL, vo);
340 struct vo initial_values = {
341 .opts = opts,
342 .key_fifo = key_fifo,
343 .input_ctx = input_ctx,
344 .event_fd = -1,
345 .registered_fd = -1,
347 // first try the preferred drivers, with their optional subdevice param:
348 if (vo_list && vo_list[0])
349 while (vo_list[0][0]) {
350 char *name = strdup(vo_list[0]);
351 char *vo_subdevice = strchr(name,':');
352 if (!strcmp(name, "pgm"))
353 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "The pgm video output driver has been replaced by -vo pnm:pgmyuv.\n");
354 if (!strcmp(name, "md5"))
355 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "The md5 video output driver has been replaced by -vo md5sum.\n");
356 if (vo_subdevice) {
357 vo_subdevice[0] = 0;
358 ++vo_subdevice;
360 for (i = 0; video_out_drivers[i]; i++) {
361 const struct vo_driver *video_driver = video_out_drivers[i];
362 const vo_info_t *info = video_driver->info;
363 if (!strcmp(info->short_name, name)) {
364 // name matches, try it
365 *vo = initial_values;
366 vo->driver = video_driver;
367 if (!vo_preinit(vo, vo_subdevice)) {
368 free(name);
369 return vo; // success!
371 talloc_free_children(vo);
374 // continue...
375 free(name);
376 ++vo_list;
377 if (!(vo_list[0])) {
378 talloc_free(vo);
379 return NULL; // do NOT fallback to others
382 // now try the rest...
383 for (i = 0; video_out_drivers[i]; i++) {
384 const struct vo_driver *video_driver = video_out_drivers[i];
385 *vo = initial_values;
386 vo->driver = video_driver;
387 if (!vo_preinit(vo, NULL))
388 return vo; // success!
389 talloc_free_children(vo);
391 talloc_free(vo);
392 return NULL;
395 static int event_fd_callback(void *ctx, int fd)
397 struct vo *vo = ctx;
398 vo_check_events(vo);
399 return MP_INPUT_NOTHING;
402 int vo_config(struct vo *vo, uint32_t width, uint32_t height,
403 uint32_t d_width, uint32_t d_height, uint32_t flags,
404 uint32_t format)
406 struct MPOpts *opts = vo->opts;
407 panscan_init(vo);
408 aspect_save_orig(vo, width, height);
409 aspect_save_prescale(vo, d_width, d_height);
411 if (vo_control(vo, VOCTRL_UPDATE_SCREENINFO, NULL) == VO_TRUE) {
412 aspect(vo, &d_width, &d_height, A_NOZOOM);
413 vo->dx = (int)(opts->vo_screenwidth - d_width) / 2;
414 vo->dy = (int)(opts->vo_screenheight - d_height) / 2;
415 geometry(&vo->dx, &vo->dy, &d_width, &d_height,
416 opts->vo_screenwidth, opts->vo_screenheight);
417 geometry_xy_changed |= xinerama_screen >= 0;
418 vo->dx += xinerama_x;
419 vo->dy += xinerama_y;
420 vo->dwidth = d_width;
421 vo->dheight = d_height;
424 int ret = vo->driver->config(vo, width, height, d_width, d_height, flags,
425 format);
426 vo->config_ok = (ret == 0);
427 vo->config_count += vo->config_ok;
428 if (vo->registered_fd == -1 && vo->event_fd != -1 && vo->config_ok) {
429 mp_input_add_key_fd(vo->input_ctx, vo->event_fd, 1, event_fd_callback,
430 NULL, vo);
431 vo->registered_fd = vo->event_fd;
433 vo->frame_loaded = false;
434 vo->waiting_mpi = NULL;
435 vo->redrawing = false;
436 vo->hasframe = false;
437 return ret;
441 * \brief lookup an integer in a table, table must have 0 as the last key
442 * \param key key to search for
443 * \result translation corresponding to key or "to" value of last mapping
444 * if not found.
446 int lookup_keymap_table(const struct mp_keymap *map, int key) {
447 while (map->from && map->from != key) map++;
448 return map->to;
452 * \brief helper function for the kind of panscan-scaling that needs a source
453 * and destination rectangle like Direct3D and VDPAU
455 static void src_dst_split_scaling(int src_size, int dst_size, int scaled_src_size,
456 int *src_start, int *src_end, int *dst_start, int *dst_end) {
457 if (scaled_src_size > dst_size) {
458 int border = src_size * (scaled_src_size - dst_size) / scaled_src_size;
459 // round to a multiple of 2, this is at least needed for vo_direct3d and ATI cards
460 border = (border / 2 + 1) & ~1;
461 *src_start = border;
462 *src_end = src_size - border;
463 *dst_start = 0;
464 *dst_end = dst_size;
465 } else {
466 *src_start = 0;
467 *src_end = src_size;
468 *dst_start = (dst_size - scaled_src_size) / 2;
469 *dst_end = *dst_start + scaled_src_size;
474 * Calculate the appropriate source and destination rectangle to
475 * get a correctly scaled picture, including pan-scan.
476 * Can be extended to take future cropping support into account.
478 * \param crop specifies the cropping border size in the left, right, top and bottom members, may be NULL
479 * \param borders the border values as e.g. EOSD (ASS) and properly placed DVD highlight support requires,
480 * may be NULL and only left and top are currently valid.
482 void calc_src_dst_rects(struct vo *vo, int src_width, int src_height,
483 struct vo_rect *src, struct vo_rect *dst,
484 struct vo_rect *borders, const struct vo_rect *crop)
486 static const struct vo_rect no_crop = {0, 0, 0, 0, 0, 0};
487 int scaled_width = 0;
488 int scaled_height = 0;
489 if (!crop) crop = &no_crop;
490 src_width -= crop->left + crop->right;
491 src_height -= crop->top + crop->bottom;
492 if (src_width < 2) src_width = 2;
493 if (src_height < 2) src_height = 2;
494 dst->left = 0; dst->right = vo->dwidth;
495 dst->top = 0; dst->bottom = vo->dheight;
496 src->left = 0; src->right = src_width;
497 src->top = 0; src->bottom = src_height;
498 if (borders) {
499 borders->left = 0; borders->top = 0;
501 if (aspect_scaling()) {
502 aspect(vo, &scaled_width, &scaled_height, A_WINZOOM);
503 panscan_calc_windowed(vo);
504 scaled_width += vo->panscan_x;
505 scaled_height += vo->panscan_y;
506 if (borders) {
507 borders->left = (vo->dwidth - scaled_width ) / 2;
508 borders->top = (vo->dheight - scaled_height) / 2;
510 src_dst_split_scaling(src_width, vo->dwidth, scaled_width,
511 &src->left, &src->right, &dst->left, &dst->right);
512 src_dst_split_scaling(src_height, vo->dheight, scaled_height,
513 &src->top, &src->bottom, &dst->top, &dst->bottom);
515 src->left += crop->left; src->right += crop->left;
516 src->top += crop->top; src->bottom += crop->top;
517 src->width = src->right - src->left;
518 src->height = src->bottom - src->top;
519 dst->width = dst->right - dst->left;
520 dst->height = dst->bottom - dst->top;
523 // Return the window title the VO should set. Always returns a null terminated
524 // string. The string is valid until frontend code is invoked again. Copy it if
525 // you need to keep the string for an extended period of time.
526 const char *vo_get_window_title(struct vo *vo)
528 if (vo->opts->vo_wintitle) {
529 return vo->opts->vo_wintitle;
530 } else {
531 return "mplayer2";
536 * Generates a mouse movement message if those are enable and sends it
537 * to the "main" MPlayer.
539 * \param posx new x position of mouse
540 * \param posy new y position of mouse
542 void vo_mouse_movement(struct vo *vo, int posx, int posy)
544 char cmd_str[40];
545 if (!enable_mouse_movements)
546 return;
547 snprintf(cmd_str, sizeof(cmd_str), "set_mouse_pos %i %i", posx, posy);
548 mp_input_queue_cmd(vo->input_ctx, mp_input_parse_cmd(cmd_str));
551 #if defined(CONFIG_FBDEV) || defined(CONFIG_VESA)
552 /* Borrowed from vo_fbdev.c
553 Monitor ranges related functions*/
555 char *monitor_hfreq_str = NULL;
556 char *monitor_vfreq_str = NULL;
557 char *monitor_dotclock_str = NULL;
559 float range_max(range_t *r)
561 float max = 0;
563 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
564 if (max < r->max) max = r->max;
565 return max;
569 int in_range(range_t *r, float f)
571 for (/* NOTHING */; (r->min != -1 && r->max != -1); r++)
572 if (f >= r->min && f <= r->max)
573 return 1;
574 return 0;
577 range_t *str2range(char *s)
579 float tmp_min, tmp_max;
580 char *endptr = s; // to start the loop
581 range_t *r = NULL;
582 int i;
584 if (!s)
585 return NULL;
586 for (i = 0; *endptr; i++) {
587 if (*s == ',')
588 goto out_err;
589 if (!(r = realloc(r, sizeof(*r) * (i + 2)))) {
590 mp_msg(MSGT_GLOBAL, MSGL_WARN,"can't realloc 'r'\n");
591 return NULL;
593 tmp_min = strtod(s, &endptr);
594 if (*endptr == 'k' || *endptr == 'K') {
595 tmp_min *= 1000.0;
596 endptr++;
597 } else if (*endptr == 'm' || *endptr == 'M') {
598 tmp_min *= 1000000.0;
599 endptr++;
601 if (*endptr == '-') {
602 tmp_max = strtod(endptr + 1, &endptr);
603 if (*endptr == 'k' || *endptr == 'K') {
604 tmp_max *= 1000.0;
605 endptr++;
606 } else if (*endptr == 'm' || *endptr == 'M') {
607 tmp_max *= 1000000.0;
608 endptr++;
610 if (*endptr != ',' && *endptr)
611 goto out_err;
612 } else if (*endptr == ',' || !*endptr) {
613 tmp_max = tmp_min;
614 } else
615 goto out_err;
616 r[i].min = tmp_min;
617 r[i].max = tmp_max;
618 if (r[i].min < 0 || r[i].max < 0)
619 goto out_err;
620 s = endptr + 1;
622 r[i].min = r[i].max = -1;
623 return r;
624 out_err:
625 free(r);
626 return NULL;
629 /* Borrowed from vo_fbdev.c END */
630 #endif