Ignore svn change r30495
[mplayer/kovensky.git] / libvo / vo_x11.c
blob11c563d05dbe92af393f4b3a26901bb85749022e
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 static const vo_info_t info = {
60 "X11 ( XImage/Shm )",
61 "x11",
62 "Aaron Holtzman <aholtzma@ess.engr.uvic.ca>",
66 const LIBVO_EXTERN(x11)
67 /* private prototypes */
68 static void (*draw_alpha_fnc) (int x0, int y0, int w, int h,
69 unsigned char *src, unsigned char *srca,
70 int stride);
72 /* local data */
73 static unsigned char *ImageData;
74 //! original unaligned pointer for free
75 static unsigned char *ImageDataOrig;
77 /* X11 related variables */
78 static XImage *myximage = NULL;
79 static int depth, bpp;
80 static XWindowAttributes attribs;
82 static int int_pause;
84 static int Flip_Flag;
85 static int zoomFlag;
88 static uint32_t image_width;
89 static uint32_t image_height;
90 static uint32_t in_format;
91 static uint32_t out_format = 0;
92 static int out_offset;
93 static int srcW = -1;
94 static int srcH = -1;
96 static int old_vo_dwidth = -1;
97 static int old_vo_dheight = -1;
99 static void check_events(void)
101 int ret = vo_x11_check_events(mDisplay);
103 if (ret & VO_EVENT_RESIZE)
104 vo_x11_clearwindow(mDisplay, vo_window);
105 else if (ret & VO_EVENT_EXPOSE)
106 vo_x11_clearwindow_part(mDisplay, vo_window, myximage->width,
107 myximage->height, 0);
108 if (ret & VO_EVENT_EXPOSE && int_pause)
109 flip_page();
112 static void draw_alpha_32(int x0, int y0, int w, int h, unsigned char *src,
113 unsigned char *srca, int stride)
115 vo_draw_alpha_rgb32(w, h, src, srca, stride,
116 ImageData + 4 * (y0 * image_width + x0),
117 4 * image_width);
120 static void draw_alpha_24(int x0, int y0, int w, int h, unsigned char *src,
121 unsigned char *srca, int stride)
123 vo_draw_alpha_rgb24(w, h, src, srca, stride,
124 ImageData + 3 * (y0 * image_width + x0),
125 3 * image_width);
128 static void draw_alpha_16(int x0, int y0, int w, int h, unsigned char *src,
129 unsigned char *srca, int stride)
131 vo_draw_alpha_rgb16(w, h, src, srca, stride,
132 ImageData + 2 * (y0 * image_width + x0),
133 2 * image_width);
136 static void draw_alpha_15(int x0, int y0, int w, int h, unsigned char *src,
137 unsigned char *srca, int stride)
139 vo_draw_alpha_rgb15(w, h, src, srca, stride,
140 ImageData + 2 * (y0 * image_width + x0),
141 2 * image_width);
144 static void draw_alpha_null(int x0, int y0, int w, int h,
145 unsigned char *src, unsigned char *srca,
146 int stride)
150 static struct SwsContext *swsContext = NULL;
151 static int dst_width;
152 extern int sws_flags;
154 static XVisualInfo vinfo;
156 static void getMyXImage(void)
158 #ifdef HAVE_SHM
159 if (mLocalDisplay && XShmQueryExtension(mDisplay))
160 Shmem_Flag = 1;
161 else
163 Shmem_Flag = 0;
164 mp_msg(MSGT_VO, MSGL_WARN,
165 "Shared memory not supported\nReverting to normal Xlib\n");
167 if (Shmem_Flag)
168 CompletionType = XShmGetEventBase(mDisplay) + ShmCompletion;
170 if (Shmem_Flag)
172 myximage =
173 XShmCreateImage(mDisplay, vinfo.visual, depth, ZPixmap, NULL,
174 &Shminfo[0], image_width, image_height);
175 if (myximage == NULL)
177 mp_msg(MSGT_VO, MSGL_WARN,
178 "Shared memory error,disabling ( Ximage error )\n");
179 goto shmemerror;
181 Shminfo[0].shmid = shmget(IPC_PRIVATE,
182 myximage->bytes_per_line *
183 myximage->height, IPC_CREAT | 0777);
184 if (Shminfo[0].shmid < 0)
186 XDestroyImage(myximage);
187 mp_msg(MSGT_VO, MSGL_V, "%s\n", strerror(errno));
188 //perror( strerror( errno ) );
189 mp_msg(MSGT_VO, MSGL_WARN,
190 "Shared memory error,disabling ( seg id error )\n");
191 goto shmemerror;
193 Shminfo[0].shmaddr = (char *) shmat(Shminfo[0].shmid, 0, 0);
195 if (Shminfo[0].shmaddr == ((char *) -1))
197 XDestroyImage(myximage);
198 if (Shminfo[0].shmaddr != ((char *) -1))
199 shmdt(Shminfo[0].shmaddr);
200 mp_msg(MSGT_VO, MSGL_WARN,
201 "Shared memory error,disabling ( address error )\n");
202 goto shmemerror;
204 myximage->data = Shminfo[0].shmaddr;
205 ImageData = (unsigned char *) myximage->data;
206 Shminfo[0].readOnly = False;
207 XShmAttach(mDisplay, &Shminfo[0]);
209 XSync(mDisplay, False);
211 if (gXErrorFlag)
213 XDestroyImage(myximage);
214 shmdt(Shminfo[0].shmaddr);
215 mp_msg(MSGT_VO, MSGL_WARN, "Shared memory error,disabling.\n");
216 gXErrorFlag = 0;
217 goto shmemerror;
218 } else
219 shmctl(Shminfo[0].shmid, IPC_RMID, 0);
222 static int firstTime = 1;
224 if (firstTime)
226 mp_msg(MSGT_VO, MSGL_V, "Sharing memory.\n");
227 firstTime = 0;
230 } else
232 shmemerror:
233 Shmem_Flag = 0;
234 #endif
235 myximage = XCreateImage(mDisplay, vinfo.visual, depth, ZPixmap,
236 0, NULL, image_width, image_height, 8, 0);
237 ImageDataOrig = malloc(myximage->bytes_per_line * image_height + 32);
238 myximage->data = ImageDataOrig + 16 - ((long)ImageDataOrig & 15);
239 memset(myximage->data, 0, myximage->bytes_per_line * image_height);
240 ImageData = myximage->data;
241 #ifdef HAVE_SHM
243 #endif
246 static void freeMyXImage(void)
248 #ifdef HAVE_SHM
249 if (Shmem_Flag)
251 XShmDetach(mDisplay, &Shminfo[0]);
252 XDestroyImage(myximage);
253 shmdt(Shminfo[0].shmaddr);
254 } else
255 #endif
257 myximage->data = ImageDataOrig;
258 XDestroyImage(myximage);
259 ImageDataOrig = NULL;
261 myximage = NULL;
262 ImageData = NULL;
265 #if HAVE_BIGENDIAN
266 #define BO_NATIVE MSBFirst
267 #define BO_NONNATIVE LSBFirst
268 #else
269 #define BO_NATIVE LSBFirst
270 #define BO_NONNATIVE MSBFirst
271 #endif
272 const struct fmt2Xfmtentry_s {
273 uint32_t mpfmt;
274 int byte_order;
275 unsigned red_mask;
276 unsigned green_mask;
277 unsigned blue_mask;
278 } fmt2Xfmt[] = {
279 {IMGFMT_RGB8, BO_NATIVE, 0x00000007, 0x00000038, 0x000000C0},
280 {IMGFMT_RGB8, BO_NONNATIVE, 0x00000007, 0x00000038, 0x000000C0},
281 {IMGFMT_BGR8, BO_NATIVE, 0x000000E0, 0x0000001C, 0x00000003},
282 {IMGFMT_BGR8, BO_NONNATIVE, 0x000000E0, 0x0000001C, 0x00000003},
283 {IMGFMT_RGB15, BO_NATIVE, 0x0000001F, 0x000003E0, 0x00007C00},
284 {IMGFMT_BGR15, BO_NATIVE, 0x00007C00, 0x000003E0, 0x0000001F},
285 {IMGFMT_RGB16, BO_NATIVE, 0x0000001F, 0x000007E0, 0x0000F800},
286 {IMGFMT_BGR16, BO_NATIVE, 0x0000F800, 0x000007E0, 0x0000001F},
287 {IMGFMT_RGB24, MSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
288 {IMGFMT_RGB24, LSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
289 {IMGFMT_BGR24, MSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
290 {IMGFMT_BGR24, LSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
291 {IMGFMT_RGB32, BO_NATIVE, 0x000000FF, 0x0000FF00, 0x00FF0000},
292 {IMGFMT_RGB32, BO_NONNATIVE, 0xFF000000, 0x00FF0000, 0x0000FF00},
293 {IMGFMT_BGR32, BO_NATIVE, 0x00FF0000, 0x0000FF00, 0x000000FF},
294 {IMGFMT_BGR32, BO_NONNATIVE, 0x0000FF00, 0x00FF0000, 0xFF000000},
295 {IMGFMT_ARGB, MSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
296 {IMGFMT_ARGB, LSBFirst, 0x0000FF00, 0x00FF0000, 0xFF000000},
297 {IMGFMT_ABGR, MSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
298 {IMGFMT_ABGR, LSBFirst, 0xFF000000, 0x00FF0000, 0x0000FF00},
299 {IMGFMT_RGBA, MSBFirst, 0xFF000000, 0x00FF0000, 0x0000FF00},
300 {IMGFMT_RGBA, LSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
301 {IMGFMT_BGRA, MSBFirst, 0x0000FF00, 0x00FF0000, 0xFF000000},
302 {IMGFMT_BGRA, LSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
303 {0, 0, 0, 0, 0}
306 static int config(uint32_t width, uint32_t height, uint32_t d_width,
307 uint32_t d_height, uint32_t flags, char *title,
308 uint32_t format)
310 // int screen;
312 // int interval, prefer_blank, allow_exp, nothing;
313 unsigned int fg, bg;
314 Colormap theCmap;
315 XSetWindowAttributes xswa;
316 unsigned long xswamask;
317 const struct fmt2Xfmtentry_s *fmte = fmt2Xfmt;
319 #ifdef CONFIG_XF86VM
320 int vm = flags & VOFLAG_MODESWITCHING;
321 #endif
322 Flip_Flag = flags & VOFLAG_FLIPPING;
323 zoomFlag = flags & VOFLAG_SWSCALE;
325 old_vo_dwidth = -1;
326 old_vo_dheight = -1;
328 if (!title)
329 title = "MPlayer X11 (XImage/Shm) render";
331 in_format = format;
332 srcW = width;
333 srcH = height;
335 XGetWindowAttributes(mDisplay, mRootWin, &attribs);
336 depth = attribs.depth;
338 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
340 Visual *visual;
342 depth = vo_find_depth_from_visuals(mDisplay, mScreen, &visual);
344 if (!XMatchVisualInfo(mDisplay, mScreen, depth, DirectColor, &vinfo) ||
345 (WinID > 0
346 && vinfo.visualid != XVisualIDFromVisual(attribs.visual)))
347 XMatchVisualInfo(mDisplay, mScreen, depth, TrueColor, &vinfo);
349 /* set image size (which is indeed neither the input nor output size),
350 if zoom is on it will be changed during draw_slice anyway so we don't duplicate the aspect code here
352 image_width = (width + 7) & (~7);
353 image_height = height;
356 #ifdef CONFIG_XF86VM
357 if (vm)
359 vo_vm_switch();
361 #endif
362 bg = WhitePixel(mDisplay, mScreen);
363 fg = BlackPixel(mDisplay, mScreen);
365 theCmap = vo_x11_create_colormap(&vinfo);
367 xswa.background_pixel = 0;
368 xswa.border_pixel = 0;
369 xswa.colormap = theCmap;
370 xswamask = CWBackPixel | CWBorderPixel | CWColormap;
372 #ifdef CONFIG_XF86VM
373 if (vm)
375 xswa.override_redirect = True;
376 xswamask |= CWOverrideRedirect;
378 #endif
380 vo_x11_create_vo_window(&vinfo, vo_dx, vo_dy, vo_dwidth, vo_dheight,
381 flags, theCmap, "x11", title);
382 if (WinID > 0)
383 depth = vo_x11_update_geometry();
385 #ifdef CONFIG_XF86VM
386 if (vm)
388 /* Grab the mouse pointer in our window */
389 if (vo_grabpointer)
390 XGrabPointer(mDisplay, vo_window, True, 0,
391 GrabModeAsync, GrabModeAsync,
392 vo_window, None, CurrentTime);
393 XSetInputFocus(mDisplay, vo_window, RevertToNone, CurrentTime);
395 #endif
398 if (myximage)
400 freeMyXImage();
401 sws_freeContext(swsContext);
403 getMyXImage();
405 while (fmte->mpfmt) {
406 int depth = IMGFMT_RGB_DEPTH(fmte->mpfmt);
407 /* bits_per_pixel in X seems to be set to 16 for 15 bit formats
408 => force depth to 16 so that only the color masks are used for the format check */
409 if (depth == 15)
410 depth = 16;
412 if (depth == myximage->bits_per_pixel &&
413 fmte->byte_order == myximage->byte_order &&
414 fmte->red_mask == myximage->red_mask &&
415 fmte->green_mask == myximage->green_mask &&
416 fmte->blue_mask == myximage->blue_mask)
417 break;
418 fmte++;
420 if (!fmte->mpfmt) {
421 mp_msg(MSGT_VO, MSGL_ERR,
422 "X server image format not supported, please contact the developers\n");
423 return -1;
425 out_format = fmte->mpfmt;
426 switch ((bpp = myximage->bits_per_pixel))
428 case 24:
429 draw_alpha_fnc = draw_alpha_24;
430 break;
431 case 32:
432 draw_alpha_fnc = draw_alpha_32;
433 break;
434 case 15:
435 case 16:
436 if (depth == 15)
437 draw_alpha_fnc = draw_alpha_15;
438 else
439 draw_alpha_fnc = draw_alpha_16;
440 break;
441 default:
442 draw_alpha_fnc = draw_alpha_null;
444 out_offset = 0;
445 // for these formats conversion is currently not support and
446 // we can easily "emulate" them.
447 if (out_format & 64 && (IMGFMT_IS_RGB(out_format) || IMGFMT_IS_BGR(out_format))) {
448 out_format &= ~64;
449 #if HAVE_BIGENDIAN
450 out_offset = 1;
451 #else
452 out_offset = -1;
453 #endif
456 /* always allocate swsContext as size could change between frames */
457 swsContext =
458 sws_getContextFromCmdLine(width, height, in_format, width, height,
459 out_format);
460 if (!swsContext)
461 return -1;
463 dst_width = width;
464 //printf( "X11 bpp: %d color mask: R:%lX G:%lX B:%lX\n",bpp,myximage->red_mask,myximage->green_mask,myximage->blue_mask );
466 return 0;
469 static void Display_Image(XImage * myximage, uint8_t * ImageData)
471 int x = (vo_dwidth - dst_width) / 2;
472 int y = (vo_dheight - myximage->height) / 2;
474 // do not draw if the image needs rescaling
475 if ((old_vo_dwidth != vo_dwidth || old_vo_dheight != vo_dheight) && zoomFlag)
476 return;
478 if (WinID == 0) {
479 x = vo_dx;
480 y = vo_dy;
482 myximage->data += out_offset;
483 #ifdef HAVE_SHM
484 if (Shmem_Flag)
486 XShmPutImage(mDisplay, vo_window, vo_gc, myximage,
487 0, 0,
488 x, y, dst_width,
489 myximage->height, True);
490 } else
491 #endif
493 XPutImage(mDisplay, vo_window, vo_gc, myximage,
494 0, 0,
495 x, y, dst_width,
496 myximage->height);
498 myximage->data -= out_offset;
501 static void draw_osd(void)
503 vo_draw_text(image_width, image_height, draw_alpha_fnc);
506 static void flip_page(void)
508 Display_Image(myximage, ImageData);
509 XSync(mDisplay, False);
512 static int draw_slice(uint8_t * src[], int stride[], int w, int h,
513 int x, int y)
515 uint8_t *dst[MP_MAX_PLANES] = {NULL};
516 int dstStride[MP_MAX_PLANES] = {0};
518 if ((old_vo_dwidth != vo_dwidth
519 || old_vo_dheight != vo_dheight) /*&& y==0 */ && zoomFlag)
521 int newW = vo_dwidth;
522 int newH = vo_dheight;
523 struct SwsContext *oldContext = swsContext;
525 old_vo_dwidth = vo_dwidth;
526 old_vo_dheight = vo_dheight;
528 if (vo_fs)
529 aspect(&newW, &newH, A_ZOOM);
530 if (sws_flags == 0)
531 newW &= (~31); // not needed but, if the user wants the FAST_BILINEAR SCALER, then its needed
533 swsContext = sws_getContextFromCmdLine(srcW, srcH, in_format,
534 newW, newH, out_format);
535 if (swsContext)
537 image_width = (newW + 7) & (~7);
538 image_height = newH;
540 freeMyXImage();
541 getMyXImage();
542 sws_freeContext(oldContext);
543 } else
545 swsContext = oldContext;
547 dst_width = newW;
550 dstStride[0] = image_width * ((bpp + 7) / 8);
551 dst[0] = ImageData;
552 if (Flip_Flag)
554 dst[0] += dstStride[0] * (image_height - 1);
555 dstStride[0] = -dstStride[0];
557 sws_scale(swsContext, src, stride, y, h, dst, dstStride);
558 return 0;
561 static int draw_frame(uint8_t * src[])
563 return VO_ERROR;
566 static uint32_t get_image(mp_image_t * mpi)
568 if (zoomFlag ||
569 !IMGFMT_IS_BGR(mpi->imgfmt) ||
570 (IMGFMT_BGR_DEPTH(mpi->imgfmt) != vo_depthonscreen) ||
571 ((mpi->type != MP_IMGTYPE_STATIC)
572 && (mpi->type != MP_IMGTYPE_TEMP))
573 || (mpi->flags & MP_IMGFLAG_PLANAR)
574 || (mpi->flags & MP_IMGFLAG_YUV) || (mpi->width != image_width)
575 || (mpi->height != image_height))
576 return VO_FALSE;
578 if (Flip_Flag)
580 mpi->stride[0] = -image_width * ((bpp + 7) / 8);
581 mpi->planes[0] = ImageData - mpi->stride[0] * (image_height - 1);
582 } else
584 mpi->stride[0] = image_width * ((bpp + 7) / 8);
585 mpi->planes[0] = ImageData;
587 mpi->flags |= MP_IMGFLAG_DIRECT;
589 return VO_TRUE;
592 static int query_format(uint32_t format)
594 mp_msg(MSGT_VO, MSGL_DBG2,
595 "vo_x11: query_format was called: %x (%s)\n", format,
596 vo_format_name(format));
597 if (IMGFMT_IS_BGR(format))
599 if (IMGFMT_BGR_DEPTH(format) <= 8)
600 return 0; // TODO 8bpp not yet fully implemented
601 if (IMGFMT_BGR_DEPTH(format) == vo_depthonscreen)
602 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_FLIP |
603 VFCAP_ACCEPT_STRIDE;
604 else
605 return VFCAP_CSP_SUPPORTED | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_FLIP |
606 VFCAP_ACCEPT_STRIDE;
609 switch (format)
611 // case IMGFMT_BGR8:
612 // case IMGFMT_BGR15:
613 // case IMGFMT_BGR16:
614 // case IMGFMT_BGR24:
615 // case IMGFMT_BGR32:
616 // return 0x2;
617 // case IMGFMT_YUY2:
618 case IMGFMT_I420:
619 case IMGFMT_IYUV:
620 case IMGFMT_YV12:
621 return VFCAP_CSP_SUPPORTED | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_ACCEPT_STRIDE;
623 return 0;
627 static void uninit(void)
629 if (!myximage)
630 return;
632 freeMyXImage();
634 #ifdef CONFIG_XF86VM
635 vo_vm_close();
636 #endif
638 zoomFlag = 0;
639 vo_x11_uninit();
641 sws_freeContext(swsContext);
644 static int preinit(const char *arg)
646 if (arg)
648 mp_msg(MSGT_VO, MSGL_ERR, "vo_x11: Unknown subdevice: %s\n", arg);
649 return ENOSYS;
652 if (!vo_init())
653 return -1; // Can't open X11
654 return 0;
657 static int control(uint32_t request, void *data)
659 switch (request)
661 case VOCTRL_PAUSE:
662 return int_pause = 1;
663 case VOCTRL_RESUME:
664 return int_pause = 0;
665 case VOCTRL_QUERY_FORMAT:
666 return query_format(*((uint32_t *) data));
667 case VOCTRL_GET_IMAGE:
668 return get_image(data);
669 case VOCTRL_FULLSCREEN:
670 vo_x11_fullscreen();
671 vo_x11_clearwindow(mDisplay, vo_window);
672 return VO_TRUE;
673 case VOCTRL_SET_EQUALIZER:
675 struct voctrl_set_equalizer_args *args = data;
676 return vo_x11_set_equalizer(args->name, args->value);
678 case VOCTRL_GET_EQUALIZER:
680 struct voctrl_get_equalizer_args *args = data;
681 return vo_x11_get_equalizer(args->name, args->valueptr);
683 case VOCTRL_ONTOP:
684 vo_x11_ontop();
685 return VO_TRUE;
686 case VOCTRL_UPDATE_SCREENINFO:
687 update_xinerama_info();
688 return VO_TRUE;
690 return VO_NOTIMPL;