1000l, play_tree_parser_stop_keeping broke 0-termination of buffer
[mplayer/glamo.git] / libvo / vo_x11.c
blob73062bce35baa7825e90f120f8a696bf89eb9c4d
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"
9 #include "aspect.h"
12 #include <X11/Xlib.h>
13 #include <X11/Xutil.h>
15 #include <errno.h>
17 #include "x11_common.h"
19 #ifdef HAVE_SHM
20 #include <sys/ipc.h>
21 #include <sys/shm.h>
22 #include <X11/extensions/XShm.h>
24 static int Shmem_Flag;
26 //static int Quiet_Flag; Here also what is this for. It's used but isn't initialized?
27 static XShmSegmentInfo Shminfo[1];
28 static int gXErrorFlag;
29 static int CompletionType = -1;
30 #endif
32 #include "sub.h"
34 #include "libswscale/swscale.h"
35 #include "libmpcodecs/vf_scale.h"
36 #define MODE_RGB 0x1
37 #define MODE_BGR 0x2
39 #include "mp_msg.h"
40 #include "help_mp.h"
42 #ifdef CONFIG_GUI
43 #include "gui/interface.h"
44 #include "mplayer.h"
45 #endif
47 static const vo_info_t info = {
48 "X11 ( XImage/Shm )",
49 "x11",
50 "Aaron Holtzman <aholtzma@ess.engr.uvic.ca>",
54 const LIBVO_EXTERN(x11)
55 /* private prototypes */
56 static void (*draw_alpha_fnc) (int x0, int y0, int w, int h,
57 unsigned char *src, unsigned char *srca,
58 int stride);
60 /* local data */
61 static unsigned char *ImageData;
62 //! original unaligned pointer for free
63 static unsigned char *ImageDataOrig;
65 /* X11 related variables */
66 static XImage *myximage = NULL;
67 static int depth, bpp;
68 static XWindowAttributes attribs;
70 static int int_pause;
72 static int Flip_Flag;
73 static int zoomFlag;
76 static uint32_t image_width;
77 static uint32_t image_height;
78 static uint32_t in_format;
79 static uint32_t out_format = 0;
80 static int out_offset;
81 static int srcW = -1;
82 static int srcH = -1;
84 static int old_vo_dwidth = -1;
85 static int old_vo_dheight = -1;
87 static void check_events(void)
89 int ret = vo_x11_check_events(mDisplay);
91 if (ret & VO_EVENT_RESIZE)
92 vo_x11_clearwindow(mDisplay, vo_window);
93 else if (ret & VO_EVENT_EXPOSE)
94 vo_x11_clearwindow_part(mDisplay, vo_window, myximage->width,
95 myximage->height, 0);
96 if (ret & VO_EVENT_EXPOSE && int_pause)
97 flip_page();
100 static void draw_alpha_32(int x0, int y0, int w, int h, unsigned char *src,
101 unsigned char *srca, int stride)
103 vo_draw_alpha_rgb32(w, h, src, srca, stride,
104 ImageData + 4 * (y0 * image_width + x0),
105 4 * image_width);
108 static void draw_alpha_24(int x0, int y0, int w, int h, unsigned char *src,
109 unsigned char *srca, int stride)
111 vo_draw_alpha_rgb24(w, h, src, srca, stride,
112 ImageData + 3 * (y0 * image_width + x0),
113 3 * image_width);
116 static void draw_alpha_16(int x0, int y0, int w, int h, unsigned char *src,
117 unsigned char *srca, int stride)
119 vo_draw_alpha_rgb16(w, h, src, srca, stride,
120 ImageData + 2 * (y0 * image_width + x0),
121 2 * image_width);
124 static void draw_alpha_15(int x0, int y0, int w, int h, unsigned char *src,
125 unsigned char *srca, int stride)
127 vo_draw_alpha_rgb15(w, h, src, srca, stride,
128 ImageData + 2 * (y0 * image_width + x0),
129 2 * image_width);
132 static void draw_alpha_null(int x0, int y0, int w, int h,
133 unsigned char *src, unsigned char *srca,
134 int stride)
138 static struct SwsContext *swsContext = NULL;
139 static int dst_width;
140 extern int sws_flags;
142 static XVisualInfo vinfo;
144 static void getMyXImage(void)
146 #ifdef HAVE_SHM
147 if (mLocalDisplay && XShmQueryExtension(mDisplay))
148 Shmem_Flag = 1;
149 else
151 Shmem_Flag = 0;
152 mp_msg(MSGT_VO, MSGL_WARN,
153 "Shared memory not supported\nReverting to normal Xlib\n");
155 if (Shmem_Flag)
156 CompletionType = XShmGetEventBase(mDisplay) + ShmCompletion;
158 if (Shmem_Flag)
160 myximage =
161 XShmCreateImage(mDisplay, vinfo.visual, depth, ZPixmap, NULL,
162 &Shminfo[0], image_width, image_height);
163 if (myximage == NULL)
165 mp_msg(MSGT_VO, MSGL_WARN,
166 "Shared memory error,disabling ( Ximage error )\n");
167 goto shmemerror;
169 Shminfo[0].shmid = shmget(IPC_PRIVATE,
170 myximage->bytes_per_line *
171 myximage->height, IPC_CREAT | 0777);
172 if (Shminfo[0].shmid < 0)
174 XDestroyImage(myximage);
175 mp_msg(MSGT_VO, MSGL_V, "%s\n", strerror(errno));
176 //perror( strerror( errno ) );
177 mp_msg(MSGT_VO, MSGL_WARN,
178 "Shared memory error,disabling ( seg id error )\n");
179 goto shmemerror;
181 Shminfo[0].shmaddr = (char *) shmat(Shminfo[0].shmid, 0, 0);
183 if (Shminfo[0].shmaddr == ((char *) -1))
185 XDestroyImage(myximage);
186 if (Shminfo[0].shmaddr != ((char *) -1))
187 shmdt(Shminfo[0].shmaddr);
188 mp_msg(MSGT_VO, MSGL_WARN,
189 "Shared memory error,disabling ( address error )\n");
190 goto shmemerror;
192 myximage->data = Shminfo[0].shmaddr;
193 ImageData = (unsigned char *) myximage->data;
194 Shminfo[0].readOnly = False;
195 XShmAttach(mDisplay, &Shminfo[0]);
197 XSync(mDisplay, False);
199 if (gXErrorFlag)
201 XDestroyImage(myximage);
202 shmdt(Shminfo[0].shmaddr);
203 mp_msg(MSGT_VO, MSGL_WARN, "Shared memory error,disabling.\n");
204 gXErrorFlag = 0;
205 goto shmemerror;
206 } else
207 shmctl(Shminfo[0].shmid, IPC_RMID, 0);
210 static int firstTime = 1;
212 if (firstTime)
214 mp_msg(MSGT_VO, MSGL_V, "Sharing memory.\n");
215 firstTime = 0;
218 } else
220 shmemerror:
221 Shmem_Flag = 0;
222 #endif
223 myximage = XCreateImage(mDisplay, vinfo.visual, depth, ZPixmap,
224 0, NULL, image_width, image_height, 8, 0);
225 ImageDataOrig = malloc(myximage->bytes_per_line * image_height + 32);
226 myximage->data = ImageDataOrig + 16 - ((long)ImageDataOrig & 15);
227 memset(myximage->data, 0, myximage->bytes_per_line * image_height);
228 ImageData = myximage->data;
229 #ifdef HAVE_SHM
231 #endif
234 static void freeMyXImage(void)
236 #ifdef HAVE_SHM
237 if (Shmem_Flag)
239 XShmDetach(mDisplay, &Shminfo[0]);
240 XDestroyImage(myximage);
241 shmdt(Shminfo[0].shmaddr);
242 } else
243 #endif
245 myximage->data = ImageDataOrig;
246 XDestroyImage(myximage);
247 ImageDataOrig = NULL;
249 myximage = NULL;
250 ImageData = NULL;
253 #ifdef WORDS_BIGENDIAN
254 #define BO_NATIVE MSBFirst
255 #define BO_NONNATIVE LSBFirst
256 #else
257 #define BO_NATIVE LSBFirst
258 #define BO_NONNATIVE MSBFirst
259 #endif
260 const struct fmt2Xfmtentry_s {
261 uint32_t mpfmt;
262 int byte_order;
263 unsigned red_mask;
264 unsigned green_mask;
265 unsigned blue_mask;
266 } fmt2Xfmt[] = {
267 {IMGFMT_RGB8, BO_NATIVE, 0x00000007, 0x00000038, 0x000000C0},
268 {IMGFMT_RGB8, BO_NONNATIVE, 0x00000007, 0x00000038, 0x000000C0},
269 {IMGFMT_BGR8, BO_NATIVE, 0x000000E0, 0x0000001C, 0x00000003},
270 {IMGFMT_BGR8, BO_NONNATIVE, 0x000000E0, 0x0000001C, 0x00000003},
271 {IMGFMT_RGB15, BO_NATIVE, 0x0000001F, 0x000003E0, 0x00007C00},
272 {IMGFMT_BGR15, BO_NATIVE, 0x00007C00, 0x000003E0, 0x0000001F},
273 {IMGFMT_RGB16, BO_NATIVE, 0x0000001F, 0x000007E0, 0x0000F800},
274 {IMGFMT_BGR16, BO_NATIVE, 0x0000F800, 0x000007E0, 0x0000001F},
275 {IMGFMT_RGB24, MSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
276 {IMGFMT_RGB24, LSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
277 {IMGFMT_BGR24, MSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
278 {IMGFMT_BGR24, LSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
279 {IMGFMT_RGB32, BO_NATIVE, 0x000000FF, 0x0000FF00, 0x00FF0000},
280 {IMGFMT_RGB32, BO_NONNATIVE, 0xFF000000, 0x00FF0000, 0x0000FF00},
281 {IMGFMT_BGR32, BO_NATIVE, 0x00FF0000, 0x0000FF00, 0x000000FF},
282 {IMGFMT_BGR32, BO_NONNATIVE, 0x0000FF00, 0x00FF0000, 0xFF000000},
283 {IMGFMT_ARGB, MSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
284 {IMGFMT_ARGB, LSBFirst, 0x0000FF00, 0x00FF0000, 0xFF000000},
285 {IMGFMT_ABGR, MSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
286 {IMGFMT_ABGR, LSBFirst, 0xFF000000, 0x00FF0000, 0x0000FF00},
287 {IMGFMT_RGBA, MSBFirst, 0xFF000000, 0x00FF0000, 0x0000FF00},
288 {IMGFMT_RGBA, LSBFirst, 0x000000FF, 0x0000FF00, 0x00FF0000},
289 {IMGFMT_BGRA, MSBFirst, 0x0000FF00, 0x00FF0000, 0xFF000000},
290 {IMGFMT_BGRA, LSBFirst, 0x00FF0000, 0x0000FF00, 0x000000FF},
291 {0, 0, 0, 0, 0}
294 static int config(uint32_t width, uint32_t height, uint32_t d_width,
295 uint32_t d_height, uint32_t flags, char *title,
296 uint32_t format)
298 // int screen;
300 // int interval, prefer_blank, allow_exp, nothing;
301 unsigned int fg, bg;
302 Colormap theCmap;
303 XSetWindowAttributes xswa;
304 unsigned long xswamask;
305 const struct fmt2Xfmtentry_s *fmte = fmt2Xfmt;
307 #ifdef CONFIG_XF86VM
308 int vm = flags & VOFLAG_MODESWITCHING;
309 #endif
310 Flip_Flag = flags & VOFLAG_FLIPPING;
311 zoomFlag = flags & VOFLAG_SWSCALE;
313 old_vo_dwidth = -1;
314 old_vo_dheight = -1;
316 int_pause = 0;
317 if (!title)
318 title = "MPlayer X11 (XImage/Shm) render";
320 in_format = format;
321 srcW = width;
322 srcH = height;
324 XGetWindowAttributes(mDisplay, mRootWin, &attribs);
325 depth = attribs.depth;
327 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
329 Visual *visual;
331 depth = vo_find_depth_from_visuals(mDisplay, mScreen, &visual);
333 if (!XMatchVisualInfo(mDisplay, mScreen, depth, DirectColor, &vinfo) ||
334 (WinID > 0
335 && vinfo.visualid != XVisualIDFromVisual(attribs.visual)))
336 XMatchVisualInfo(mDisplay, mScreen, depth, TrueColor, &vinfo);
338 /* set image size (which is indeed neither the input nor output size),
339 if zoom is on it will be changed during draw_slice anyway so we don't duplicate the aspect code here
341 image_width = (width + 7) & (~7);
342 image_height = height;
344 #ifdef CONFIG_GUI
345 if (use_gui)
346 guiGetEvent(guiSetShVideo, 0); // the GUI will set up / resize the window
347 else
348 #endif
350 #ifdef CONFIG_XF86VM
351 if (vm)
353 vo_vm_switch();
355 #endif
356 bg = WhitePixel(mDisplay, mScreen);
357 fg = BlackPixel(mDisplay, mScreen);
359 theCmap = vo_x11_create_colormap(&vinfo);
361 xswa.background_pixel = 0;
362 xswa.border_pixel = 0;
363 xswa.colormap = theCmap;
364 xswamask = CWBackPixel | CWBorderPixel | CWColormap;
366 #ifdef CONFIG_XF86VM
367 if (vm)
369 xswa.override_redirect = True;
370 xswamask |= CWOverrideRedirect;
372 #endif
374 vo_x11_create_vo_window(&vinfo, vo_dx, vo_dy, vo_dwidth, vo_dheight,
375 flags, theCmap, "x11", title);
376 if (WinID > 0)
377 depth = vo_x11_update_geometry();
379 #ifdef CONFIG_XF86VM
380 if (vm)
382 /* Grab the mouse pointer in our window */
383 if (vo_grabpointer)
384 XGrabPointer(mDisplay, vo_window, True, 0,
385 GrabModeAsync, GrabModeAsync,
386 vo_window, None, CurrentTime);
387 XSetInputFocus(mDisplay, vo_window, RevertToNone, CurrentTime);
389 #endif
392 if (myximage)
394 freeMyXImage();
395 sws_freeContext(swsContext);
397 getMyXImage();
399 while (fmte->mpfmt) {
400 int depth = IMGFMT_RGB_DEPTH(fmte->mpfmt);
401 /* bits_per_pixel in X seems to be set to 16 for 15 bit formats
402 => force depth to 16 so that only the color masks are used for the format check */
403 if (depth == 15)
404 depth = 16;
406 if (depth == myximage->bits_per_pixel &&
407 fmte->byte_order == myximage->byte_order &&
408 fmte->red_mask == myximage->red_mask &&
409 fmte->green_mask == myximage->green_mask &&
410 fmte->blue_mask == myximage->blue_mask)
411 break;
412 fmte++;
414 if (!fmte->mpfmt) {
415 mp_msg(MSGT_VO, MSGL_ERR,
416 "X server image format not supported, please contact the developers\n");
417 return -1;
419 out_format = fmte->mpfmt;
420 switch ((bpp = myximage->bits_per_pixel))
422 case 24:
423 draw_alpha_fnc = draw_alpha_24;
424 break;
425 case 32:
426 draw_alpha_fnc = draw_alpha_32;
427 break;
428 case 15:
429 case 16:
430 if (depth == 15)
431 draw_alpha_fnc = draw_alpha_15;
432 else
433 draw_alpha_fnc = draw_alpha_16;
434 break;
435 default:
436 draw_alpha_fnc = draw_alpha_null;
438 out_offset = 0;
439 // for these formats conversion is currently not support and
440 // we can easily "emulate" them.
441 if (out_format & 64 && (IMGFMT_IS_RGB(out_format) || IMGFMT_IS_BGR(out_format))) {
442 out_format &= ~64;
443 #ifdef WORDS_BIGENDIAN
444 out_offset = 1;
445 #else
446 out_offset = -1;
447 #endif
450 /* always allocate swsContext as size could change between frames */
451 swsContext =
452 sws_getContextFromCmdLine(width, height, in_format, width, height,
453 out_format);
454 if (!swsContext)
455 return -1;
457 dst_width = width;
458 //printf( "X11 bpp: %d color mask: R:%lX G:%lX B:%lX\n",bpp,myximage->red_mask,myximage->green_mask,myximage->blue_mask );
460 return 0;
463 static void Display_Image(XImage * myximage, uint8_t * ImageData)
465 int x = (vo_dwidth - dst_width) / 2;
466 int y = (vo_dheight - myximage->height) / 2;
468 // do not draw if the image needs rescaling
469 if ((old_vo_dwidth != vo_dwidth || old_vo_dheight != vo_dheight) && zoomFlag)
470 return;
472 if (WinID == 0) {
473 x = vo_dx;
474 y = vo_dy;
476 myximage->data += out_offset;
477 #ifdef HAVE_SHM
478 if (Shmem_Flag)
480 XShmPutImage(mDisplay, vo_window, vo_gc, myximage,
481 0, 0,
482 x, y, dst_width,
483 myximage->height, True);
484 } else
485 #endif
487 XPutImage(mDisplay, vo_window, vo_gc, myximage,
488 0, 0,
489 x, y, dst_width,
490 myximage->height);
492 myximage->data -= out_offset;
495 static void draw_osd(void)
497 vo_draw_text(image_width, image_height, draw_alpha_fnc);
500 static void flip_page(void)
502 Display_Image(myximage, ImageData);
503 XSync(mDisplay, False);
506 static int draw_slice(uint8_t * src[], int stride[], int w, int h,
507 int x, int y)
509 uint8_t *dst[3];
510 int dstStride[3];
512 if ((old_vo_dwidth != vo_dwidth
513 || old_vo_dheight != vo_dheight) /*&& y==0 */ && zoomFlag)
515 int newW = vo_dwidth;
516 int newH = vo_dheight;
517 struct SwsContext *oldContext = swsContext;
519 old_vo_dwidth = vo_dwidth;
520 old_vo_dheight = vo_dheight;
522 if (vo_fs)
523 aspect(&newW, &newH, A_ZOOM);
524 if (sws_flags == 0)
525 newW &= (~31); // not needed but, if the user wants the FAST_BILINEAR SCALER, then its needed
527 swsContext = sws_getContextFromCmdLine(srcW, srcH, in_format,
528 newW, newH, out_format);
529 if (swsContext)
531 image_width = (newW + 7) & (~7);
532 image_height = newH;
534 freeMyXImage();
535 getMyXImage();
536 sws_freeContext(oldContext);
537 } else
539 swsContext = oldContext;
541 dst_width = newW;
543 dstStride[1] = dstStride[2] = 0;
544 dst[1] = dst[2] = NULL;
546 dstStride[0] = image_width * ((bpp + 7) / 8);
547 dst[0] = ImageData;
548 if (Flip_Flag)
550 dst[0] += dstStride[0] * (image_height - 1);
551 dstStride[0] = -dstStride[0];
553 sws_scale_ordered(swsContext, src, stride, y, h, dst, dstStride);
554 return 0;
557 static int draw_frame(uint8_t * src[])
559 return VO_ERROR;
562 static uint32_t get_image(mp_image_t * mpi)
564 if (zoomFlag ||
565 !IMGFMT_IS_BGR(mpi->imgfmt) ||
566 (IMGFMT_BGR_DEPTH(mpi->imgfmt) != vo_depthonscreen) ||
567 ((mpi->type != MP_IMGTYPE_STATIC)
568 && (mpi->type != MP_IMGTYPE_TEMP))
569 || (mpi->flags & MP_IMGFLAG_PLANAR)
570 || (mpi->flags & MP_IMGFLAG_YUV) || (mpi->width != image_width)
571 || (mpi->height != image_height))
572 return VO_FALSE;
574 if (Flip_Flag)
576 mpi->stride[0] = -image_width * ((bpp + 7) / 8);
577 mpi->planes[0] = ImageData - mpi->stride[0] * (image_height - 1);
578 } else
580 mpi->stride[0] = image_width * ((bpp + 7) / 8);
581 mpi->planes[0] = ImageData;
583 mpi->flags |= MP_IMGFLAG_DIRECT;
585 return VO_TRUE;
588 static int query_format(uint32_t format)
590 mp_msg(MSGT_VO, MSGL_DBG2,
591 "vo_x11: query_format was called: %x (%s)\n", format,
592 vo_format_name(format));
593 if (IMGFMT_IS_BGR(format))
595 if (IMGFMT_BGR_DEPTH(format) <= 8)
596 return 0; // TODO 8bpp not yet fully implemented
597 if (IMGFMT_BGR_DEPTH(format) == vo_depthonscreen)
598 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_FLIP |
599 VFCAP_ACCEPT_STRIDE;
600 else
601 return VFCAP_CSP_SUPPORTED | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_FLIP |
602 VFCAP_ACCEPT_STRIDE;
605 switch (format)
607 // case IMGFMT_BGR8:
608 // case IMGFMT_BGR15:
609 // case IMGFMT_BGR16:
610 // case IMGFMT_BGR24:
611 // case IMGFMT_BGR32:
612 // return 0x2;
613 // case IMGFMT_YUY2:
614 case IMGFMT_I420:
615 case IMGFMT_IYUV:
616 case IMGFMT_YV12:
617 return VFCAP_CSP_SUPPORTED | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_ACCEPT_STRIDE;
619 return 0;
623 static void uninit(void)
625 if (!myximage)
626 return;
628 freeMyXImage();
630 #ifdef CONFIG_XF86VM
631 vo_vm_close();
632 #endif
634 zoomFlag = 0;
635 vo_x11_uninit();
637 sws_freeContext(swsContext);
640 static int preinit(const char *arg)
642 if (arg)
644 mp_msg(MSGT_VO, MSGL_ERR, "vo_x11: Unknown subdevice: %s\n", arg);
645 return ENOSYS;
648 if (!vo_init())
649 return -1; // Can't open X11
650 return 0;
653 static int control(uint32_t request, void *data, ...)
655 switch (request)
657 case VOCTRL_PAUSE:
658 return int_pause = 1;
659 case VOCTRL_RESUME:
660 return int_pause = 0;
661 case VOCTRL_QUERY_FORMAT:
662 return query_format(*((uint32_t *) data));
663 case VOCTRL_GET_IMAGE:
664 return get_image(data);
665 case VOCTRL_GUISUPPORT:
666 return VO_TRUE;
667 case VOCTRL_FULLSCREEN:
668 vo_x11_fullscreen();
669 vo_x11_clearwindow(mDisplay, vo_window);
670 return VO_TRUE;
671 case VOCTRL_SET_EQUALIZER:
673 va_list ap;
674 int value;
676 va_start(ap, data);
677 value = va_arg(ap, int);
679 va_end(ap);
680 return vo_x11_set_equalizer(data, value);
682 case VOCTRL_GET_EQUALIZER:
684 va_list ap;
685 int *value;
687 va_start(ap, data);
688 value = va_arg(ap, int *);
690 va_end(ap);
691 return vo_x11_get_equalizer(data, value);
693 case VOCTRL_ONTOP:
694 vo_x11_ontop();
695 return VO_TRUE;
696 case VOCTRL_UPDATE_SCREENINFO:
697 update_xinerama_info();
698 return VO_TRUE;
700 return VO_NOTIMPL;