Merge svn changes up to r27979
[mplayer.git] / libvo / vo_x11.c
blob66c21ebea70b569669b5e7af41a08f36fc46aacc
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
6 #include "config.h"
7 #include "video_out.h"
8 #include "video_out_internal.h"
11 #include <X11/Xlib.h>
12 #include <X11/Xutil.h>
14 #ifdef CONFIG_XF86VM
15 #include <X11/extensions/xf86vmode.h>
16 #endif
17 #include <errno.h>
19 #include "x11_common.h"
21 #ifdef HAVE_SHM
22 #include <sys/ipc.h>
23 #include <sys/shm.h>
24 #include <X11/extensions/XShm.h>
26 static int Shmem_Flag;
28 //static int Quiet_Flag; Here also what is this for. It's used but isn't initialized?
29 static XShmSegmentInfo Shminfo[1];
30 static int gXErrorFlag;
31 static int CompletionType = -1;
32 #endif
34 #include "sub.h"
36 #include "libswscale/swscale.h"
37 #include "libmpcodecs/vf_scale.h"
38 #define MODE_RGB 0x1
39 #define MODE_BGR 0x2
41 #include "mp_msg.h"
42 #include "help_mp.h"
44 #ifdef CONFIG_GUI
45 #include "gui/interface.h"
46 #include "mplayer.h"
47 #endif
49 static const vo_info_t info = {
50 "X11 ( XImage/Shm )",
51 "x11",
52 "Aaron Holtzman <aholtzma@ess.engr.uvic.ca>",
56 const LIBVO_EXTERN(x11)
57 /* private prototypes */
58 static void Display_Image(XImage * myximage, unsigned char *ImageData);
59 static void (*draw_alpha_fnc) (int x0, int y0, int w, int h,
60 unsigned char *src, unsigned char *srca,
61 int stride);
63 /* local data */
64 static unsigned char *ImageData;
65 //! original unaligned pointer for free
66 static unsigned char *ImageDataOrig;
68 /* X11 related variables */
69 static XImage *myximage = NULL;
70 static int depth, bpp;
71 static XWindowAttributes attribs;
73 static int int_pause;
75 static int Flip_Flag;
76 static int zoomFlag;
79 static uint32_t image_width;
80 static uint32_t image_height;
81 static uint32_t in_format;
82 static uint32_t out_format = 0;
83 static int out_offset;
84 static int srcW = -1;
85 static int srcH = -1;
86 static int aspect; // 1<<16 based fixed point aspect, so that the aspect stays correct during resizing
88 static int old_vo_dwidth = -1;
89 static int old_vo_dheight = -1;
91 static void check_events(void)
93 int ret = vo_x11_check_events(mDisplay);
95 /* clear left over borders and redraw frame if we are paused */
96 if (ret & VO_EVENT_EXPOSE && int_pause)
98 vo_x11_clearwindow_part(mDisplay, vo_window, myximage->width,
99 myximage->height, 0);
100 flip_page();
101 } else if ((ret & VO_EVENT_RESIZE) || (ret & VO_EVENT_EXPOSE))
102 vo_x11_clearwindow_part(mDisplay, vo_window, myximage->width,
103 myximage->height, 0);
107 static void draw_alpha_32(int x0, int y0, int w, int h, unsigned char *src,
108 unsigned char *srca, int stride)
110 vo_draw_alpha_rgb32(w, h, src, srca, stride,
111 ImageData + 4 * (y0 * image_width + x0),
112 4 * image_width);
115 static void draw_alpha_24(int x0, int y0, int w, int h, unsigned char *src,
116 unsigned char *srca, int stride)
118 vo_draw_alpha_rgb24(w, h, src, srca, stride,
119 ImageData + 3 * (y0 * image_width + x0),
120 3 * image_width);
123 static void draw_alpha_16(int x0, int y0, int w, int h, unsigned char *src,
124 unsigned char *srca, int stride)
126 vo_draw_alpha_rgb16(w, h, src, srca, stride,
127 ImageData + 2 * (y0 * image_width + x0),
128 2 * image_width);
131 static void draw_alpha_15(int x0, int y0, int w, int h, unsigned char *src,
132 unsigned char *srca, int stride)
134 vo_draw_alpha_rgb15(w, h, src, srca, stride,
135 ImageData + 2 * (y0 * image_width + x0),
136 2 * image_width);
139 static void draw_alpha_null(int x0, int y0, int w, int h,
140 unsigned char *src, unsigned char *srca,
141 int stride)
145 static struct SwsContext *swsContext = NULL;
146 static int dst_width;
147 extern int sws_flags;
149 static XVisualInfo vinfo;
151 static void getMyXImage(void)
153 #ifdef HAVE_SHM
154 if (mLocalDisplay && XShmQueryExtension(mDisplay))
155 Shmem_Flag = 1;
156 else
158 Shmem_Flag = 0;
159 mp_msg(MSGT_VO, MSGL_WARN,
160 "Shared memory not supported\nReverting to normal Xlib\n");
162 if (Shmem_Flag)
163 CompletionType = XShmGetEventBase(mDisplay) + ShmCompletion;
165 if (Shmem_Flag)
167 myximage =
168 XShmCreateImage(mDisplay, vinfo.visual, depth, ZPixmap, NULL,
169 &Shminfo[0], image_width, image_height);
170 if (myximage == NULL)
172 mp_msg(MSGT_VO, MSGL_WARN,
173 "Shared memory error,disabling ( Ximage error )\n");
174 goto shmemerror;
176 Shminfo[0].shmid = shmget(IPC_PRIVATE,
177 myximage->bytes_per_line *
178 myximage->height, IPC_CREAT | 0777);
179 if (Shminfo[0].shmid < 0)
181 XDestroyImage(myximage);
182 mp_msg(MSGT_VO, MSGL_V, "%s\n", strerror(errno));
183 //perror( strerror( errno ) );
184 mp_msg(MSGT_VO, MSGL_WARN,
185 "Shared memory error,disabling ( seg id error )\n");
186 goto shmemerror;
188 Shminfo[0].shmaddr = (char *) shmat(Shminfo[0].shmid, 0, 0);
190 if (Shminfo[0].shmaddr == ((char *) -1))
192 XDestroyImage(myximage);
193 if (Shminfo[0].shmaddr != ((char *) -1))
194 shmdt(Shminfo[0].shmaddr);
195 mp_msg(MSGT_VO, MSGL_WARN,
196 "Shared memory error,disabling ( address error )\n");
197 goto shmemerror;
199 myximage->data = Shminfo[0].shmaddr;
200 ImageData = (unsigned char *) myximage->data;
201 Shminfo[0].readOnly = False;
202 XShmAttach(mDisplay, &Shminfo[0]);
204 XSync(mDisplay, False);
206 if (gXErrorFlag)
208 XDestroyImage(myximage);
209 shmdt(Shminfo[0].shmaddr);
210 mp_msg(MSGT_VO, MSGL_WARN, "Shared memory error,disabling.\n");
211 gXErrorFlag = 0;
212 goto shmemerror;
213 } else
214 shmctl(Shminfo[0].shmid, IPC_RMID, 0);
217 static int firstTime = 1;
219 if (firstTime)
221 mp_msg(MSGT_VO, MSGL_V, "Sharing memory.\n");
222 firstTime = 0;
225 } else
227 shmemerror:
228 Shmem_Flag = 0;
229 #endif
230 myximage = XCreateImage(mDisplay, vinfo.visual, depth, ZPixmap,
231 0, NULL, image_width, image_height, 8, 0);
232 ImageDataOrig = malloc(myximage->bytes_per_line * image_height + 32);
233 myximage->data = ImageDataOrig + 16 - ((long)ImageDataOrig & 15);
234 memset(myximage->data, 0, myximage->bytes_per_line * image_height);
235 ImageData = myximage->data;
236 #ifdef HAVE_SHM
238 #endif
241 static void freeMyXImage(void)
243 #ifdef HAVE_SHM
244 if (Shmem_Flag)
246 XShmDetach(mDisplay, &Shminfo[0]);
247 XDestroyImage(myximage);
248 shmdt(Shminfo[0].shmaddr);
249 } else
250 #endif
252 myximage->data = ImageDataOrig;
253 XDestroyImage(myximage);
254 ImageDataOrig = NULL;
256 myximage = NULL;
257 ImageData = NULL;
260 #ifdef WORDS_BIGENDIAN
261 #define BO_NATIVE MSBFirst
262 #define BO_NONNATIVE LSBFirst
263 #else
264 #define BO_NATIVE LSBFirst
265 #define BO_NONNATIVE MSBFirst
266 #endif
267 const struct fmt2Xfmtentry_s {
268 uint32_t mpfmt;
269 int byte_order;
270 unsigned red_mask;
271 unsigned green_mask;
272 unsigned blue_mask;
273 } fmt2Xfmt[] = {
274 {IMGFMT_RGB8, BO_NATIVE, 0x00000007, 0x00000038, 0x000000C0},
275 {IMGFMT_RGB8, BO_NONNATIVE, 0x00000007, 0x00000038, 0x000000C0},
276 {IMGFMT_BGR8, BO_NATIVE, 0x000000E0, 0x0000001C, 0x00000003},
277 {IMGFMT_BGR8, BO_NONNATIVE, 0x000000E0, 0x0000001C, 0x00000003},
278 {IMGFMT_RGB15, BO_NATIVE, 0x0000001F, 0x000003E0, 0x00007C00},
279 {IMGFMT_BGR15, BO_NATIVE, 0x00007C00, 0x000003E0, 0x0000001F},
280 {IMGFMT_RGB16, BO_NATIVE, 0x0000001F, 0x000007E0, 0x0000F800},
281 {IMGFMT_BGR16, BO_NATIVE, 0x0000F800, 0x000007E0, 0x0000001F},
282 {IMGFMT_RGB24, MSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
283 {IMGFMT_RGB24, LSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
284 {IMGFMT_BGR24, MSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
285 {IMGFMT_BGR24, LSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
286 {IMGFMT_RGB32, BO_NATIVE, 0x000000FF, 0x0000FF00, 0x00FF0000},
287 {IMGFMT_RGB32, BO_NONNATIVE, 0xFF000000, 0x00FF0000, 0x0000FF00},
288 {IMGFMT_BGR32, BO_NATIVE, 0x00FF0000, 0x0000FF00, 0x000000FF},
289 {IMGFMT_BGR32, BO_NONNATIVE, 0x0000FF00, 0x00FF0000, 0xFF000000},
290 {IMGFMT_ARGB, MSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
291 {IMGFMT_ARGB, LSBFirst, 0x0000FF00, 0x00FF0000, 0xFF000000},
292 {IMGFMT_ABGR, MSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
293 {IMGFMT_ABGR, LSBFirst, 0xFF000000, 0x00FF0000, 0x0000FF00},
294 {IMGFMT_RGBA, MSBFirst, 0xFF000000, 0x00FF0000, 0x0000FF00},
295 {IMGFMT_RGBA, LSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
296 {IMGFMT_BGRA, MSBFirst, 0x0000FF00, 0x00FF0000, 0xFF000000},
297 {IMGFMT_BGRA, LSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
298 {0, 0, 0, 0, 0}
301 static int config(uint32_t width, uint32_t height, uint32_t d_width,
302 uint32_t d_height, uint32_t flags, char *title,
303 uint32_t format)
305 // int screen;
307 // int interval, prefer_blank, allow_exp, nothing;
308 unsigned int fg, bg;
309 XGCValues xgcv;
310 Colormap theCmap;
311 XSetWindowAttributes xswa;
312 unsigned long xswamask;
313 const struct fmt2Xfmtentry_s *fmte = fmt2Xfmt;
315 #ifdef CONFIG_XF86VM
316 int vm = flags & VOFLAG_MODESWITCHING;
317 #endif
318 int fullscreen = flags & (VOFLAG_FULLSCREEN|VOFLAG_MODESWITCHING);
319 Flip_Flag = flags & VOFLAG_FLIPPING;
320 zoomFlag = flags & VOFLAG_SWSCALE;
322 old_vo_dwidth = -1;
323 old_vo_dheight = -1;
325 int_pause = 0;
326 if (!title)
327 title = "MPlayer X11 (XImage/Shm) render";
329 in_format = format;
330 srcW = width;
331 srcH = height;
333 // if(!fullscreen) zoomFlag=1; //it makes no sense to avoid zooming on windowd mode
335 //printf( "w: %d h: %d\n\n",vo_dwidth,vo_dheight );
337 XGetWindowAttributes(mDisplay, mRootWin, &attribs);
338 depth = attribs.depth;
340 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
342 Visual *visual;
344 depth = vo_find_depth_from_visuals(mDisplay, mScreen, &visual);
346 if (!XMatchVisualInfo(mDisplay, mScreen, depth, DirectColor, &vinfo) ||
347 (WinID > 0
348 && vinfo.visualid != XVisualIDFromVisual(attribs.visual)))
349 XMatchVisualInfo(mDisplay, mScreen, depth, TrueColor, &vinfo);
351 /* set image size (which is indeed neither the input nor output size),
352 if zoom is on it will be changed during draw_slice anyway so we don't duplicate the aspect code here
354 image_width = (width + 7) & (~7);
355 image_height = height;
357 aspect = ((1 << 16) * d_width + d_height / 2) / d_height;
359 #ifdef CONFIG_GUI
360 if (use_gui)
361 guiGetEvent(guiSetShVideo, 0); // the GUI will set up / resize the window
362 else
363 #endif
365 #ifdef CONFIG_XF86VM
366 if (vm)
368 vo_vm_switch();
370 #endif
371 bg = WhitePixel(mDisplay, mScreen);
372 fg = BlackPixel(mDisplay, mScreen);
374 theCmap = vo_x11_create_colormap(&vinfo);
376 xswa.background_pixel = 0;
377 xswa.border_pixel = 0;
378 xswa.colormap = theCmap;
379 xswamask = CWBackPixel | CWBorderPixel | CWColormap;
381 #ifdef CONFIG_XF86VM
382 if (vm)
384 xswa.override_redirect = True;
385 xswamask |= CWOverrideRedirect;
387 #endif
389 vo_x11_create_vo_window(&vinfo, vo_dx, vo_dy, vo_dwidth, vo_dheight,
390 flags, theCmap, "x11", title);
391 if (WinID > 0)
392 depth = vo_x11_update_geometry();
394 #ifdef CONFIG_XF86VM
395 if (vm)
397 /* Grab the mouse pointer in our window */
398 if (vo_grabpointer)
399 XGrabPointer(mDisplay, vo_window, True, 0,
400 GrabModeAsync, GrabModeAsync,
401 vo_window, None, CurrentTime);
402 XSetInputFocus(mDisplay, vo_window, RevertToNone, CurrentTime);
404 #endif
407 if (myximage)
409 freeMyXImage();
410 sws_freeContext(swsContext);
412 getMyXImage();
414 while (fmte->mpfmt) {
415 int depth = IMGFMT_RGB_DEPTH(fmte->mpfmt);
416 /* bits_per_pixel in X seems to be set to 16 for 15 bit formats
417 => force depth to 16 so that only the color masks are used for the format check */
418 if (depth == 15)
419 depth = 16;
421 if (depth == myximage->bits_per_pixel &&
422 fmte->byte_order == myximage->byte_order &&
423 fmte->red_mask == myximage->red_mask &&
424 fmte->green_mask == myximage->green_mask &&
425 fmte->blue_mask == myximage->blue_mask)
426 break;
427 fmte++;
429 if (!fmte->mpfmt) {
430 mp_msg(MSGT_VO, MSGL_ERR,
431 "X server image format not supported, please contact the developers\n");
432 return -1;
434 out_format = fmte->mpfmt;
435 switch ((bpp = myximage->bits_per_pixel))
437 case 24:
438 draw_alpha_fnc = draw_alpha_24;
439 break;
440 case 32:
441 draw_alpha_fnc = draw_alpha_32;
442 break;
443 case 15:
444 case 16:
445 if (depth == 15)
446 draw_alpha_fnc = draw_alpha_15;
447 else
448 draw_alpha_fnc = draw_alpha_16;
449 break;
450 default:
451 draw_alpha_fnc = draw_alpha_null;
453 out_offset = 0;
454 // for these formats conversion is currently not support and
455 // we can easily "emulate" them.
456 if (out_format & 64 && (IMGFMT_IS_RGB(out_format) || IMGFMT_IS_BGR(out_format))) {
457 out_format &= ~64;
458 #ifdef WORDS_BIGENDIAN
459 out_offset = 1;
460 #else
461 out_offset = -1;
462 #endif
465 /* always allocate swsContext as size could change between frames */
466 swsContext =
467 sws_getContextFromCmdLine(width, height, in_format, width, height,
468 out_format);
469 if (!swsContext)
470 return -1;
472 dst_width = width;
473 //printf( "X11 bpp: %d color mask: R:%lX G:%lX B:%lX\n",bpp,myximage->red_mask,myximage->green_mask,myximage->blue_mask );
475 return 0;
478 static void Display_Image(XImage * myximage, uint8_t * ImageData)
480 int x = (vo_dwidth - dst_width) / 2;
481 int y = (vo_dheight - myximage->height) / 2;
482 if (WinID == 0) {
483 x = vo_dx;
484 y = vo_dy;
486 myximage->data += out_offset;
487 #ifdef HAVE_SHM
488 if (Shmem_Flag)
490 XShmPutImage(mDisplay, vo_window, vo_gc, myximage,
491 0, 0,
492 x, y, dst_width,
493 myximage->height, True);
494 } else
495 #endif
497 XPutImage(mDisplay, vo_window, vo_gc, myximage,
498 0, 0,
499 x, y, dst_width,
500 myximage->height);
502 myximage->data -= out_offset;
505 static void draw_osd(void)
507 vo_draw_text(image_width, image_height, draw_alpha_fnc);
510 static void flip_page(void)
512 Display_Image(myximage, ImageData);
513 XSync(mDisplay, False);
516 static int draw_slice(uint8_t * src[], int stride[], int w, int h,
517 int x, int y)
519 uint8_t *dst[3];
520 int dstStride[3];
522 if ((old_vo_dwidth != vo_dwidth
523 || old_vo_dheight != vo_dheight) /*&& y==0 */ && zoomFlag)
525 int newW = vo_dwidth;
526 int newH = vo_dheight;
527 int newAspect = (newW * (1 << 16) + (newH >> 1)) / newH;
528 struct SwsContext *oldContext = swsContext;
530 if (newAspect > aspect)
531 newW = (newH * aspect + (1 << 15)) >> 16;
532 else
533 newH = ((newW << 16) + (aspect >> 1)) / aspect;
535 old_vo_dwidth = vo_dwidth;
536 old_vo_dheight = vo_dheight;
538 if (sws_flags == 0)
539 newW &= (~31); // not needed but, if the user wants the FAST_BILINEAR SCALER, then its needed
541 swsContext = sws_getContextFromCmdLine(srcW, srcH, in_format,
542 newW, newH, out_format);
543 if (swsContext)
545 image_width = (newW + 7) & (~7);
546 image_height = newH;
548 freeMyXImage();
549 getMyXImage();
550 sws_freeContext(oldContext);
551 } else
553 swsContext = oldContext;
555 dst_width = newW;
557 dstStride[1] = dstStride[2] = 0;
558 dst[1] = dst[2] = NULL;
560 dstStride[0] = image_width * ((bpp + 7) / 8);
561 dst[0] = ImageData;
562 if (Flip_Flag)
564 dst[0] += dstStride[0] * (image_height - 1);
565 dstStride[0] = -dstStride[0];
567 sws_scale_ordered(swsContext, src, stride, y, h, dst, dstStride);
568 return 0;
571 static int draw_frame(uint8_t * src[])
573 return VO_ERROR;
576 static uint32_t get_image(mp_image_t * mpi)
578 if (zoomFlag ||
579 !IMGFMT_IS_BGR(mpi->imgfmt) ||
580 (IMGFMT_BGR_DEPTH(mpi->imgfmt) != vo_depthonscreen) ||
581 ((mpi->type != MP_IMGTYPE_STATIC)
582 && (mpi->type != MP_IMGTYPE_TEMP))
583 || (mpi->flags & MP_IMGFLAG_PLANAR)
584 || (mpi->flags & MP_IMGFLAG_YUV) || (mpi->width != image_width)
585 || (mpi->height != image_height))
586 return VO_FALSE;
588 if (Flip_Flag)
590 mpi->stride[0] = -image_width * ((bpp + 7) / 8);
591 mpi->planes[0] = ImageData - mpi->stride[0] * (image_height - 1);
592 } else
594 mpi->stride[0] = image_width * ((bpp + 7) / 8);
595 mpi->planes[0] = ImageData;
597 mpi->flags |= MP_IMGFLAG_DIRECT;
599 return VO_TRUE;
602 static int query_format(uint32_t format)
604 mp_msg(MSGT_VO, MSGL_DBG2,
605 "vo_x11: query_format was called: %x (%s)\n", format,
606 vo_format_name(format));
607 if (IMGFMT_IS_BGR(format))
609 if (IMGFMT_BGR_DEPTH(format) <= 8)
610 return 0; // TODO 8bpp not yet fully implemented
611 if (IMGFMT_BGR_DEPTH(format) == vo_depthonscreen)
612 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_FLIP |
613 VFCAP_ACCEPT_STRIDE;
614 else
615 return VFCAP_CSP_SUPPORTED | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_FLIP |
616 VFCAP_ACCEPT_STRIDE;
619 switch (format)
621 // case IMGFMT_BGR8:
622 // case IMGFMT_BGR15:
623 // case IMGFMT_BGR16:
624 // case IMGFMT_BGR24:
625 // case IMGFMT_BGR32:
626 // return 0x2;
627 // case IMGFMT_YUY2:
628 case IMGFMT_I420:
629 case IMGFMT_IYUV:
630 case IMGFMT_YV12:
631 return VFCAP_CSP_SUPPORTED | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_ACCEPT_STRIDE;
633 return 0;
637 static void uninit(void)
639 if (!myximage)
640 return;
642 freeMyXImage();
644 #ifdef CONFIG_XF86VM
645 vo_vm_close();
646 #endif
648 zoomFlag = 0;
649 vo_x11_uninit();
651 sws_freeContext(swsContext);
654 static int preinit(const char *arg)
656 if (arg)
658 mp_msg(MSGT_VO, MSGL_ERR, "vo_x11: Unknown subdevice: %s\n", arg);
659 return ENOSYS;
662 if (!vo_init())
663 return -1; // Can't open X11
664 return 0;
667 static int control(uint32_t request, void *data)
669 switch (request)
671 case VOCTRL_PAUSE:
672 return int_pause = 1;
673 case VOCTRL_RESUME:
674 return int_pause = 0;
675 case VOCTRL_QUERY_FORMAT:
676 return query_format(*((uint32_t *) data));
677 case VOCTRL_GET_IMAGE:
678 return get_image(data);
679 case VOCTRL_GUISUPPORT:
680 return VO_TRUE;
681 case VOCTRL_FULLSCREEN:
682 vo_x11_fullscreen();
683 vo_x11_clearwindow(mDisplay, vo_window);
684 return VO_TRUE;
685 case VOCTRL_SET_EQUALIZER:
687 struct voctrl_set_equalizer_args *args = data;
688 return vo_x11_set_equalizer(args->name, args->value);
690 case VOCTRL_GET_EQUALIZER:
692 struct voctrl_get_equalizer_args *args = data;
693 return vo_x11_get_equalizer(args->name, args->valueptr);
695 case VOCTRL_ONTOP:
696 vo_x11_ontop();
697 return VO_TRUE;
698 case VOCTRL_UPDATE_SCREENINFO:
699 update_xinerama_info();
700 return VO_TRUE;
702 return VO_NOTIMPL;