Revert r30324 which broken the struct addrinfo check by making it always pass.
[mplayer.git] / libvo / vo_x11.c
blobda0826576098ab81eb03486dc5f5946c90508ad1
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #include "config.h"
24 #include "video_out.h"
25 #include "video_out_internal.h"
26 #include "aspect.h"
29 #include <X11/Xlib.h>
30 #include <X11/Xutil.h>
32 #include <errno.h>
34 #include "x11_common.h"
36 #ifdef HAVE_SHM
37 #include <sys/ipc.h>
38 #include <sys/shm.h>
39 #include <X11/extensions/XShm.h>
41 static int Shmem_Flag;
43 //static int Quiet_Flag; Here also what is this for. It's used but isn't initialized?
44 static XShmSegmentInfo Shminfo[1];
45 static int gXErrorFlag;
46 static int CompletionType = -1;
47 #endif
49 #include "sub.h"
51 #include "libswscale/swscale.h"
52 #include "libmpcodecs/vf_scale.h"
53 #define MODE_RGB 0x1
54 #define MODE_BGR 0x2
56 #include "mp_msg.h"
57 #include "help_mp.h"
59 #ifdef CONFIG_GUI
60 #include "gui/interface.h"
61 #include "mplayer.h"
62 #endif
64 static const vo_info_t info = {
65 "X11 ( XImage/Shm )",
66 "x11",
67 "Aaron Holtzman <aholtzma@ess.engr.uvic.ca>",
71 const LIBVO_EXTERN(x11)
72 /* private prototypes */
73 static void (*draw_alpha_fnc) (int x0, int y0, int w, int h,
74 unsigned char *src, unsigned char *srca,
75 int stride);
77 /* local data */
78 static unsigned char *ImageData;
79 //! original unaligned pointer for free
80 static unsigned char *ImageDataOrig;
82 /* X11 related variables */
83 static XImage *myximage = NULL;
84 static int depth, bpp;
85 static XWindowAttributes attribs;
87 static int int_pause;
89 static int Flip_Flag;
90 static int zoomFlag;
93 static uint32_t image_width;
94 static uint32_t image_height;
95 static uint32_t in_format;
96 static uint32_t out_format = 0;
97 static int out_offset;
98 static int srcW = -1;
99 static int srcH = -1;
101 static int old_vo_dwidth = -1;
102 static int old_vo_dheight = -1;
104 static void check_events(void)
106 int ret = vo_x11_check_events(mDisplay);
108 if (ret & VO_EVENT_RESIZE)
109 vo_x11_clearwindow(mDisplay, vo_window);
110 else if (ret & VO_EVENT_EXPOSE)
111 vo_x11_clearwindow_part(mDisplay, vo_window, myximage->width,
112 myximage->height, 0);
113 if (ret & VO_EVENT_EXPOSE && int_pause)
114 flip_page();
117 static void draw_alpha_32(int x0, int y0, int w, int h, unsigned char *src,
118 unsigned char *srca, int stride)
120 vo_draw_alpha_rgb32(w, h, src, srca, stride,
121 ImageData + 4 * (y0 * image_width + x0),
122 4 * image_width);
125 static void draw_alpha_24(int x0, int y0, int w, int h, unsigned char *src,
126 unsigned char *srca, int stride)
128 vo_draw_alpha_rgb24(w, h, src, srca, stride,
129 ImageData + 3 * (y0 * image_width + x0),
130 3 * image_width);
133 static void draw_alpha_16(int x0, int y0, int w, int h, unsigned char *src,
134 unsigned char *srca, int stride)
136 vo_draw_alpha_rgb16(w, h, src, srca, stride,
137 ImageData + 2 * (y0 * image_width + x0),
138 2 * image_width);
141 static void draw_alpha_15(int x0, int y0, int w, int h, unsigned char *src,
142 unsigned char *srca, int stride)
144 vo_draw_alpha_rgb15(w, h, src, srca, stride,
145 ImageData + 2 * (y0 * image_width + x0),
146 2 * image_width);
149 static void draw_alpha_null(int x0, int y0, int w, int h,
150 unsigned char *src, unsigned char *srca,
151 int stride)
155 static struct SwsContext *swsContext = NULL;
156 static int dst_width;
157 extern int sws_flags;
159 static XVisualInfo vinfo;
161 static void getMyXImage(void)
163 #ifdef HAVE_SHM
164 if (mLocalDisplay && XShmQueryExtension(mDisplay))
165 Shmem_Flag = 1;
166 else
168 Shmem_Flag = 0;
169 mp_msg(MSGT_VO, MSGL_WARN,
170 "Shared memory not supported\nReverting to normal Xlib\n");
172 if (Shmem_Flag)
173 CompletionType = XShmGetEventBase(mDisplay) + ShmCompletion;
175 if (Shmem_Flag)
177 myximage =
178 XShmCreateImage(mDisplay, vinfo.visual, depth, ZPixmap, NULL,
179 &Shminfo[0], image_width, image_height);
180 if (myximage == NULL)
182 mp_msg(MSGT_VO, MSGL_WARN,
183 "Shared memory error,disabling ( Ximage error )\n");
184 goto shmemerror;
186 Shminfo[0].shmid = shmget(IPC_PRIVATE,
187 myximage->bytes_per_line *
188 myximage->height, IPC_CREAT | 0777);
189 if (Shminfo[0].shmid < 0)
191 XDestroyImage(myximage);
192 mp_msg(MSGT_VO, MSGL_V, "%s\n", strerror(errno));
193 //perror( strerror( errno ) );
194 mp_msg(MSGT_VO, MSGL_WARN,
195 "Shared memory error,disabling ( seg id error )\n");
196 goto shmemerror;
198 Shminfo[0].shmaddr = (char *) shmat(Shminfo[0].shmid, 0, 0);
200 if (Shminfo[0].shmaddr == ((char *) -1))
202 XDestroyImage(myximage);
203 if (Shminfo[0].shmaddr != ((char *) -1))
204 shmdt(Shminfo[0].shmaddr);
205 mp_msg(MSGT_VO, MSGL_WARN,
206 "Shared memory error,disabling ( address error )\n");
207 goto shmemerror;
209 myximage->data = Shminfo[0].shmaddr;
210 ImageData = (unsigned char *) myximage->data;
211 Shminfo[0].readOnly = False;
212 XShmAttach(mDisplay, &Shminfo[0]);
214 XSync(mDisplay, False);
216 if (gXErrorFlag)
218 XDestroyImage(myximage);
219 shmdt(Shminfo[0].shmaddr);
220 mp_msg(MSGT_VO, MSGL_WARN, "Shared memory error,disabling.\n");
221 gXErrorFlag = 0;
222 goto shmemerror;
223 } else
224 shmctl(Shminfo[0].shmid, IPC_RMID, 0);
227 static int firstTime = 1;
229 if (firstTime)
231 mp_msg(MSGT_VO, MSGL_V, "Sharing memory.\n");
232 firstTime = 0;
235 } else
237 shmemerror:
238 Shmem_Flag = 0;
239 #endif
240 myximage = XCreateImage(mDisplay, vinfo.visual, depth, ZPixmap,
241 0, NULL, image_width, image_height, 8, 0);
242 ImageDataOrig = malloc(myximage->bytes_per_line * image_height + 32);
243 myximage->data = ImageDataOrig + 16 - ((long)ImageDataOrig & 15);
244 memset(myximage->data, 0, myximage->bytes_per_line * image_height);
245 ImageData = myximage->data;
246 #ifdef HAVE_SHM
248 #endif
251 static void freeMyXImage(void)
253 #ifdef HAVE_SHM
254 if (Shmem_Flag)
256 XShmDetach(mDisplay, &Shminfo[0]);
257 XDestroyImage(myximage);
258 shmdt(Shminfo[0].shmaddr);
259 } else
260 #endif
262 myximage->data = ImageDataOrig;
263 XDestroyImage(myximage);
264 ImageDataOrig = NULL;
266 myximage = NULL;
267 ImageData = NULL;
270 #if HAVE_BIGENDIAN
271 #define BO_NATIVE MSBFirst
272 #define BO_NONNATIVE LSBFirst
273 #else
274 #define BO_NATIVE LSBFirst
275 #define BO_NONNATIVE MSBFirst
276 #endif
277 const struct fmt2Xfmtentry_s {
278 uint32_t mpfmt;
279 int byte_order;
280 unsigned red_mask;
281 unsigned green_mask;
282 unsigned blue_mask;
283 } fmt2Xfmt[] = {
284 {IMGFMT_RGB8, BO_NATIVE, 0x00000007, 0x00000038, 0x000000C0},
285 {IMGFMT_RGB8, BO_NONNATIVE, 0x00000007, 0x00000038, 0x000000C0},
286 {IMGFMT_BGR8, BO_NATIVE, 0x000000E0, 0x0000001C, 0x00000003},
287 {IMGFMT_BGR8, BO_NONNATIVE, 0x000000E0, 0x0000001C, 0x00000003},
288 {IMGFMT_RGB15, BO_NATIVE, 0x0000001F, 0x000003E0, 0x00007C00},
289 {IMGFMT_BGR15, BO_NATIVE, 0x00007C00, 0x000003E0, 0x0000001F},
290 {IMGFMT_RGB16, BO_NATIVE, 0x0000001F, 0x000007E0, 0x0000F800},
291 {IMGFMT_BGR16, BO_NATIVE, 0x0000F800, 0x000007E0, 0x0000001F},
292 {IMGFMT_RGB24, MSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
293 {IMGFMT_RGB24, LSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
294 {IMGFMT_BGR24, MSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
295 {IMGFMT_BGR24, LSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
296 {IMGFMT_RGB32, BO_NATIVE, 0x000000FF, 0x0000FF00, 0x00FF0000},
297 {IMGFMT_RGB32, BO_NONNATIVE, 0xFF000000, 0x00FF0000, 0x0000FF00},
298 {IMGFMT_BGR32, BO_NATIVE, 0x00FF0000, 0x0000FF00, 0x000000FF},
299 {IMGFMT_BGR32, BO_NONNATIVE, 0x0000FF00, 0x00FF0000, 0xFF000000},
300 {IMGFMT_ARGB, MSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
301 {IMGFMT_ARGB, LSBFirst, 0x0000FF00, 0x00FF0000, 0xFF000000},
302 {IMGFMT_ABGR, MSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
303 {IMGFMT_ABGR, LSBFirst, 0xFF000000, 0x00FF0000, 0x0000FF00},
304 {IMGFMT_RGBA, MSBFirst, 0xFF000000, 0x00FF0000, 0x0000FF00},
305 {IMGFMT_RGBA, LSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
306 {IMGFMT_BGRA, MSBFirst, 0x0000FF00, 0x00FF0000, 0xFF000000},
307 {IMGFMT_BGRA, LSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
308 {0, 0, 0, 0, 0}
311 static int config(uint32_t width, uint32_t height, uint32_t d_width,
312 uint32_t d_height, uint32_t flags, char *title,
313 uint32_t format)
315 // int screen;
317 // int interval, prefer_blank, allow_exp, nothing;
318 unsigned int fg, bg;
319 Colormap theCmap;
320 XSetWindowAttributes xswa;
321 unsigned long xswamask;
322 const struct fmt2Xfmtentry_s *fmte = fmt2Xfmt;
324 #ifdef CONFIG_XF86VM
325 int vm = flags & VOFLAG_MODESWITCHING;
326 #endif
327 Flip_Flag = flags & VOFLAG_FLIPPING;
328 zoomFlag = flags & VOFLAG_SWSCALE;
330 old_vo_dwidth = -1;
331 old_vo_dheight = -1;
333 int_pause = 0;
334 if (!title)
335 title = "MPlayer X11 (XImage/Shm) render";
337 in_format = format;
338 srcW = width;
339 srcH = height;
341 XGetWindowAttributes(mDisplay, mRootWin, &attribs);
342 depth = attribs.depth;
344 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
346 Visual *visual;
348 depth = vo_find_depth_from_visuals(mDisplay, mScreen, &visual);
350 if (!XMatchVisualInfo(mDisplay, mScreen, depth, DirectColor, &vinfo) ||
351 (WinID > 0
352 && vinfo.visualid != XVisualIDFromVisual(attribs.visual)))
353 XMatchVisualInfo(mDisplay, mScreen, depth, TrueColor, &vinfo);
355 /* set image size (which is indeed neither the input nor output size),
356 if zoom is on it will be changed during draw_slice anyway so we don't duplicate the aspect code here
358 image_width = (width + 7) & (~7);
359 image_height = height;
361 #ifdef CONFIG_GUI
362 if (use_gui)
363 guiGetEvent(guiSetShVideo, 0); // the GUI will set up / resize the window
364 else
365 #endif
367 #ifdef CONFIG_XF86VM
368 if (vm)
370 vo_vm_switch();
372 #endif
373 bg = WhitePixel(mDisplay, mScreen);
374 fg = BlackPixel(mDisplay, mScreen);
376 theCmap = vo_x11_create_colormap(&vinfo);
378 xswa.background_pixel = 0;
379 xswa.border_pixel = 0;
380 xswa.colormap = theCmap;
381 xswamask = CWBackPixel | CWBorderPixel | CWColormap;
383 #ifdef CONFIG_XF86VM
384 if (vm)
386 xswa.override_redirect = True;
387 xswamask |= CWOverrideRedirect;
389 #endif
391 vo_x11_create_vo_window(&vinfo, vo_dx, vo_dy, vo_dwidth, vo_dheight,
392 flags, theCmap, "x11", title);
393 if (WinID > 0)
394 depth = vo_x11_update_geometry();
396 #ifdef CONFIG_XF86VM
397 if (vm)
399 /* Grab the mouse pointer in our window */
400 if (vo_grabpointer)
401 XGrabPointer(mDisplay, vo_window, True, 0,
402 GrabModeAsync, GrabModeAsync,
403 vo_window, None, CurrentTime);
404 XSetInputFocus(mDisplay, vo_window, RevertToNone, CurrentTime);
406 #endif
409 if (myximage)
411 freeMyXImage();
412 sws_freeContext(swsContext);
414 getMyXImage();
416 while (fmte->mpfmt) {
417 int depth = IMGFMT_RGB_DEPTH(fmte->mpfmt);
418 /* bits_per_pixel in X seems to be set to 16 for 15 bit formats
419 => force depth to 16 so that only the color masks are used for the format check */
420 if (depth == 15)
421 depth = 16;
423 if (depth == myximage->bits_per_pixel &&
424 fmte->byte_order == myximage->byte_order &&
425 fmte->red_mask == myximage->red_mask &&
426 fmte->green_mask == myximage->green_mask &&
427 fmte->blue_mask == myximage->blue_mask)
428 break;
429 fmte++;
431 if (!fmte->mpfmt) {
432 mp_msg(MSGT_VO, MSGL_ERR,
433 "X server image format not supported, please contact the developers\n");
434 return -1;
436 out_format = fmte->mpfmt;
437 switch ((bpp = myximage->bits_per_pixel))
439 case 24:
440 draw_alpha_fnc = draw_alpha_24;
441 break;
442 case 32:
443 draw_alpha_fnc = draw_alpha_32;
444 break;
445 case 15:
446 case 16:
447 if (depth == 15)
448 draw_alpha_fnc = draw_alpha_15;
449 else
450 draw_alpha_fnc = draw_alpha_16;
451 break;
452 default:
453 draw_alpha_fnc = draw_alpha_null;
455 out_offset = 0;
456 // for these formats conversion is currently not support and
457 // we can easily "emulate" them.
458 if (out_format & 64 && (IMGFMT_IS_RGB(out_format) || IMGFMT_IS_BGR(out_format))) {
459 out_format &= ~64;
460 #if HAVE_BIGENDIAN
461 out_offset = 1;
462 #else
463 out_offset = -1;
464 #endif
467 /* always allocate swsContext as size could change between frames */
468 swsContext =
469 sws_getContextFromCmdLine(width, height, in_format, width, height,
470 out_format);
471 if (!swsContext)
472 return -1;
474 dst_width = width;
475 //printf( "X11 bpp: %d color mask: R:%lX G:%lX B:%lX\n",bpp,myximage->red_mask,myximage->green_mask,myximage->blue_mask );
477 return 0;
480 static void Display_Image(XImage * myximage, uint8_t * ImageData)
482 int x = (vo_dwidth - dst_width) / 2;
483 int y = (vo_dheight - myximage->height) / 2;
485 // do not draw if the image needs rescaling
486 if ((old_vo_dwidth != vo_dwidth || old_vo_dheight != vo_dheight) && zoomFlag)
487 return;
489 if (WinID == 0) {
490 x = vo_dx;
491 y = vo_dy;
493 myximage->data += out_offset;
494 #ifdef HAVE_SHM
495 if (Shmem_Flag)
497 XShmPutImage(mDisplay, vo_window, vo_gc, myximage,
498 0, 0,
499 x, y, dst_width,
500 myximage->height, True);
501 } else
502 #endif
504 XPutImage(mDisplay, vo_window, vo_gc, myximage,
505 0, 0,
506 x, y, dst_width,
507 myximage->height);
509 myximage->data -= out_offset;
512 static void draw_osd(void)
514 vo_draw_text(image_width, image_height, draw_alpha_fnc);
517 static void flip_page(void)
519 Display_Image(myximage, ImageData);
520 XSync(mDisplay, False);
523 static int draw_slice(uint8_t * src[], int stride[], int w, int h,
524 int x, int y)
526 uint8_t *dst[MP_MAX_PLANES] = {NULL};
527 int dstStride[MP_MAX_PLANES] = {0};
529 if ((old_vo_dwidth != vo_dwidth
530 || old_vo_dheight != vo_dheight) /*&& y==0 */ && zoomFlag)
532 int newW = vo_dwidth;
533 int newH = vo_dheight;
534 struct SwsContext *oldContext = swsContext;
536 old_vo_dwidth = vo_dwidth;
537 old_vo_dheight = vo_dheight;
539 if (vo_fs)
540 aspect(&newW, &newH, A_ZOOM);
541 if (sws_flags == 0)
542 newW &= (~31); // not needed but, if the user wants the FAST_BILINEAR SCALER, then its needed
544 swsContext = sws_getContextFromCmdLine(srcW, srcH, in_format,
545 newW, newH, out_format);
546 if (swsContext)
548 image_width = (newW + 7) & (~7);
549 image_height = newH;
551 freeMyXImage();
552 getMyXImage();
553 sws_freeContext(oldContext);
554 } else
556 swsContext = oldContext;
558 dst_width = newW;
561 dstStride[0] = image_width * ((bpp + 7) / 8);
562 dst[0] = ImageData;
563 if (Flip_Flag)
565 dst[0] += dstStride[0] * (image_height - 1);
566 dstStride[0] = -dstStride[0];
568 sws_scale(swsContext, src, stride, y, h, dst, dstStride);
569 return 0;
572 static int draw_frame(uint8_t * src[])
574 return VO_ERROR;
577 static uint32_t get_image(mp_image_t * mpi)
579 if (zoomFlag ||
580 !IMGFMT_IS_BGR(mpi->imgfmt) ||
581 (IMGFMT_BGR_DEPTH(mpi->imgfmt) != vo_depthonscreen) ||
582 ((mpi->type != MP_IMGTYPE_STATIC)
583 && (mpi->type != MP_IMGTYPE_TEMP))
584 || (mpi->flags & MP_IMGFLAG_PLANAR)
585 || (mpi->flags & MP_IMGFLAG_YUV) || (mpi->width != image_width)
586 || (mpi->height != image_height))
587 return VO_FALSE;
589 if (Flip_Flag)
591 mpi->stride[0] = -image_width * ((bpp + 7) / 8);
592 mpi->planes[0] = ImageData - mpi->stride[0] * (image_height - 1);
593 } else
595 mpi->stride[0] = image_width * ((bpp + 7) / 8);
596 mpi->planes[0] = ImageData;
598 mpi->flags |= MP_IMGFLAG_DIRECT;
600 return VO_TRUE;
603 static int query_format(uint32_t format)
605 mp_msg(MSGT_VO, MSGL_DBG2,
606 "vo_x11: query_format was called: %x (%s)\n", format,
607 vo_format_name(format));
608 if (IMGFMT_IS_BGR(format))
610 if (IMGFMT_BGR_DEPTH(format) <= 8)
611 return 0; // TODO 8bpp not yet fully implemented
612 if (IMGFMT_BGR_DEPTH(format) == vo_depthonscreen)
613 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_FLIP |
614 VFCAP_ACCEPT_STRIDE;
615 else
616 return VFCAP_CSP_SUPPORTED | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_FLIP |
617 VFCAP_ACCEPT_STRIDE;
620 switch (format)
622 // case IMGFMT_BGR8:
623 // case IMGFMT_BGR15:
624 // case IMGFMT_BGR16:
625 // case IMGFMT_BGR24:
626 // case IMGFMT_BGR32:
627 // return 0x2;
628 // case IMGFMT_YUY2:
629 case IMGFMT_I420:
630 case IMGFMT_IYUV:
631 case IMGFMT_YV12:
632 return VFCAP_CSP_SUPPORTED | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_ACCEPT_STRIDE;
634 return 0;
638 static void uninit(void)
640 if (!myximage)
641 return;
643 freeMyXImage();
645 #ifdef CONFIG_XF86VM
646 vo_vm_close();
647 #endif
649 zoomFlag = 0;
650 vo_x11_uninit();
652 sws_freeContext(swsContext);
655 static int preinit(const char *arg)
657 if (arg)
659 mp_msg(MSGT_VO, MSGL_ERR, "vo_x11: Unknown subdevice: %s\n", arg);
660 return ENOSYS;
663 if (!vo_init())
664 return -1; // Can't open X11
665 return 0;
668 static int control(uint32_t request, void *data, ...)
670 switch (request)
672 case VOCTRL_PAUSE:
673 return int_pause = 1;
674 case VOCTRL_RESUME:
675 return int_pause = 0;
676 case VOCTRL_QUERY_FORMAT:
677 return query_format(*((uint32_t *) data));
678 case VOCTRL_GET_IMAGE:
679 return get_image(data);
680 case VOCTRL_GUISUPPORT:
681 return VO_TRUE;
682 case VOCTRL_FULLSCREEN:
683 vo_x11_fullscreen();
684 vo_x11_clearwindow(mDisplay, vo_window);
685 return VO_TRUE;
686 case VOCTRL_SET_EQUALIZER:
688 va_list ap;
689 int value;
691 va_start(ap, data);
692 value = va_arg(ap, int);
694 va_end(ap);
695 return vo_x11_set_equalizer(data, value);
697 case VOCTRL_GET_EQUALIZER:
699 va_list ap;
700 int *value;
702 va_start(ap, data);
703 value = va_arg(ap, int *);
705 va_end(ap);
706 return vo_x11_get_equalizer(data, value);
708 case VOCTRL_ONTOP:
709 vo_x11_ontop();
710 return VO_TRUE;
711 case VOCTRL_UPDATE_SCREENINFO:
712 update_xinerama_info();
713 return VO_TRUE;
715 return VO_NOTIMPL;