9
#include "video_out_internal.h"
13
#include <X11/Xutil.h>
16
#include <X11/extensions/xf86vmode.h>
20
#include "x11_common.h"
25
#include <X11/extensions/XShm.h>
27
static int Shmem_Flag;
29
//static int Quiet_Flag; Here also what is this for. It's used but isn't inited ?
30
static XShmSegmentInfo Shminfo[1];
31
static int gXErrorFlag;
32
static int CompletionType = -1;
34
/* since it doesn't seem to be defined on some platforms */
35
int XShmGetEventBase(Display *);
38
#include "fastmemcpy.h"
41
#include "postproc/swscale.h"
42
#include "postproc/swscale_internal.h" //FIXME
43
#include "postproc/rgb2rgb.h"
44
#include "libmpcodecs/vf_scale.h"
49
#include "Gui/interface.h"
53
static vo_info_t info = {
56
"Aaron Holtzman <aholtzma@ess.engr.uvic.ca>",
61
/* private prototypes */
62
static void Display_Image(XImage * myximage, unsigned char *ImageData);
63
static void (*draw_alpha_fnc) (int x0, int y0, int w, int h,
64
unsigned char *src, unsigned char *srca,
68
static unsigned char *ImageData;
70
/* X11 related variables */
71
static XImage *myximage = NULL;
72
static int depth, bpp, mode;
73
static XWindowAttributes attribs;
81
static uint32_t image_width;
82
static uint32_t image_height;
83
static uint32_t in_format;
84
static uint32_t out_format = 0;
87
static int aspect; // 1<<16 based fixed point aspect, so that the aspect stays correct during resizing
89
static int old_vo_dwidth = -1;
90
static int old_vo_dheight = -1;
92
static void check_events()
94
int ret = vo_x11_check_events(mDisplay);
96
/* clear left over borders and redraw frame if we are paused */
97
if (ret & VO_EVENT_EXPOSE && int_pause)
99
vo_x11_clearwindow_part(mDisplay, vo_window, myximage->width,
100
myximage->height, 0);
102
} else if ((ret & VO_EVENT_RESIZE) || (ret & VO_EVENT_EXPOSE))
103
vo_x11_clearwindow_part(mDisplay, vo_window, myximage->width,
104
myximage->height, 0);
108
static void draw_alpha_32(int x0, int y0, int w, int h, unsigned char *src,
109
unsigned char *srca, int stride)
111
vo_draw_alpha_rgb32(w, h, src, srca, stride,
112
ImageData + 4 * (y0 * image_width + x0),
116
static void draw_alpha_24(int x0, int y0, int w, int h, unsigned char *src,
117
unsigned char *srca, int stride)
119
vo_draw_alpha_rgb24(w, h, src, srca, stride,
120
ImageData + 3 * (y0 * image_width + x0),
124
static void draw_alpha_16(int x0, int y0, int w, int h, unsigned char *src,
125
unsigned char *srca, int stride)
127
vo_draw_alpha_rgb16(w, h, src, srca, stride,
128
ImageData + 2 * (y0 * image_width + x0),
132
static void draw_alpha_15(int x0, int y0, int w, int h, unsigned char *src,
133
unsigned char *srca, int stride)
135
vo_draw_alpha_rgb15(w, h, src, srca, stride,
136
ImageData + 2 * (y0 * image_width + x0),
140
static void draw_alpha_null(int x0, int y0, int w, int h,
141
unsigned char *src, unsigned char *srca,
146
static SwsContext *swsContext = NULL;
147
extern int sws_flags;
149
static XVisualInfo vinfo;
151
static void getMyXImage()
154
if (mLocalDisplay && XShmQueryExtension(mDisplay))
159
mp_msg(MSGT_VO, MSGL_WARN,
160
"Shared memory not supported\nReverting to normal Xlib\n");
163
CompletionType = XShmGetEventBase(mDisplay) + ShmCompletion;
168
XShmCreateImage(mDisplay, vinfo.visual, depth, ZPixmap, NULL,
169
&Shminfo[0], image_width, image_height);
170
if (myximage == NULL)
172
if (myximage != NULL)
173
XDestroyImage(myximage);
174
mp_msg(MSGT_VO, MSGL_WARN,
175
"Shared memory error,disabling ( Ximage error )\n");
178
Shminfo[0].shmid = shmget(IPC_PRIVATE,
179
myximage->bytes_per_line *
180
myximage->height, IPC_CREAT | 0777);
181
if (Shminfo[0].shmid < 0)
183
XDestroyImage(myximage);
184
mp_msg(MSGT_VO, MSGL_V, "%s\n", strerror(errno));
185
//perror( strerror( errno ) );
186
mp_msg(MSGT_VO, MSGL_WARN,
187
"Shared memory error,disabling ( seg id error )\n");
190
Shminfo[0].shmaddr = (char *) shmat(Shminfo[0].shmid, 0, 0);
192
if (Shminfo[0].shmaddr == ((char *) -1))
194
XDestroyImage(myximage);
195
if (Shminfo[0].shmaddr != ((char *) -1))
196
shmdt(Shminfo[0].shmaddr);
197
mp_msg(MSGT_VO, MSGL_WARN,
198
"Shared memory error,disabling ( address error )\n");
201
myximage->data = Shminfo[0].shmaddr;
202
ImageData = (unsigned char *) myximage->data;
203
Shminfo[0].readOnly = False;
204
XShmAttach(mDisplay, &Shminfo[0]);
206
XSync(mDisplay, False);
210
XDestroyImage(myximage);
211
shmdt(Shminfo[0].shmaddr);
212
mp_msg(MSGT_VO, MSGL_WARN, "Shared memory error,disabling.\n");
216
shmctl(Shminfo[0].shmid, IPC_RMID, 0);
219
static int firstTime = 1;
223
mp_msg(MSGT_VO, MSGL_V, "Sharing memory.\n");
232
myximage = XGetImage(mDisplay, vo_window, 0, 0,
233
image_width, image_height, AllPlanes,
235
ImageData = myximage->data;
241
static void freeMyXImage()
246
XShmDetach(mDisplay, &Shminfo[0]);
247
XDestroyImage(myximage);
248
shmdt(Shminfo[0].shmaddr);
252
XDestroyImage(myximage);
257
static int config(uint32_t width, uint32_t height, uint32_t d_width,
258
uint32_t d_height, uint32_t flags, char *title,
265
// int interval, prefer_blank, allow_exp, nothing;
270
XSetWindowAttributes xswa;
271
unsigned long xswamask;
274
unsigned int modeline_width, modeline_height;
275
static uint32_t vm_width;
276
static uint32_t vm_height;
279
vo_mouse_autohide = 1;
284
title = strdup("MPlayer X11 (XImage/Shm) render");
289
vo_dx = (vo_screenwidth - d_width) / 2;
290
vo_dy = (vo_screenheight - d_height) / 2;
291
geometry(&vo_dx, &vo_dy, &d_width, &d_height, vo_screenwidth,
294
vo_dheight = d_height;
296
if (flags & (VOFLAG_FULLSCREEN|VOFLAG_MODESWITCHING))
298
if (flags & VOFLAG_MODESWITCHING)
300
if (flags & VOFLAG_FLIPPING)
302
zoomFlag = flags & VOFLAG_SWSCALE;
305
// if(!fullscreen) zoomFlag=1; //it makes no sense to avoid zooming on windowd mode
307
//printf( "w: %d h: %d\n\n",vo_dwidth,vo_dheight );
309
XGetWindowAttributes(mDisplay, mRootWin, &attribs);
310
depth = attribs.depth;
312
if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
316
depth = vo_find_depth_from_visuals(mDisplay, mScreen, &visual);
318
if (!XMatchVisualInfo(mDisplay, mScreen, depth, DirectColor, &vinfo) ||
320
&& vinfo.visualid != XVisualIDFromVisual(attribs.visual)))
321
XMatchVisualInfo(mDisplay, mScreen, depth, TrueColor, &vinfo);
323
/* set image size (which is indeed neither the input nor output size),
324
if zoom is on it will be changed during draw_slice anyway so we don't duplicate the aspect code here
326
image_width = (width + 7) & (~7);
327
image_height = height;
329
aspect = ((1 << 16) * d_width + d_height / 2) / d_height;
333
guiGetEvent(guiSetShVideo, 0); // the GUI will set up / resize the window
340
if ((d_width == 0) && (d_height == 0))
342
vm_width = image_width;
343
vm_height = image_height;
347
vm_height = d_height;
349
vo_vm_switch(vm_width, vm_height, &modeline_width,
351
vo_dx = (vo_screenwidth - modeline_width) / 2;
352
vo_dy = (vo_screenheight - modeline_height) / 2;
353
vo_dwidth = modeline_width;
354
vo_dheight = modeline_height;
357
bg = WhitePixel(mDisplay, mScreen);
358
fg = BlackPixel(mDisplay, mScreen);
360
theCmap = vo_x11_create_colormap(&vinfo);
362
xswa.background_pixel = 0;
363
xswa.border_pixel = 0;
364
xswa.colormap = theCmap;
365
xswamask = CWBackPixel | CWBorderPixel | CWColormap;
370
xswa.override_redirect = True;
371
xswamask |= CWOverrideRedirect;
377
vo_window = WinID ? ((Window) WinID) : mRootWin;
382
XUnmapWindow(mDisplay, vo_window);
383
XChangeWindowAttributes(mDisplay, vo_window, xswamask,
385
vo_x11_selectinput_witherr(mDisplay, vo_window,
386
StructureNotifyMask |
393
XMapWindow(mDisplay, vo_window);
394
XGetGeometry(mDisplay, vo_window, &mRootWin,
395
&vo_dx, &vo_dy, &vo_dwidth, &vo_dheight,
398
XSelectInput(mDisplay, vo_window, ExposureMask);
401
if (vo_window == None)
404
vo_x11_create_smooth_window(mDisplay, mRootWin,
405
vinfo.visual, vo_dx, vo_dy,
406
vo_dwidth, vo_dheight,
409
vo_x11_classhint(mDisplay, vo_window, "x11");
410
vo_hidecursor(mDisplay, vo_window);
411
vo_x11_sizehint(vo_dx, vo_dy, vo_dwidth, vo_dheight, 0);
412
XSelectInput(mDisplay, vo_window, StructureNotifyMask);
413
XStoreName(mDisplay, vo_window, title);
414
XMapWindow(mDisplay, vo_window);
418
XNextEvent(mDisplay, &xev);
420
while (xev.type != MapNotify
421
|| xev.xmap.event != vo_window);
426
vo_x11_xinerama_move(mDisplay, vo_window);
428
} else if (!fullscreen)
429
XMoveResizeWindow(mDisplay, vo_window, vo_dx, vo_dy,
430
vo_dwidth, vo_dheight);
433
XSync(mDisplay, False);
435
// we cannot grab mouse events on root window :(
436
vo_x11_selectinput_witherr(mDisplay, vo_window,
437
StructureNotifyMask | KeyPressMask |
438
PropertyChangeMask | ExposureMask |
440
0) ? 0 : (ButtonPressMask |
442
PointerMotionMask)));
447
/* Grab the mouse pointer in our window */
449
XGrabPointer(mDisplay, vo_window, True, 0,
450
GrabModeAsync, GrabModeAsync,
451
vo_window, None, CurrentTime);
452
XSetInputFocus(mDisplay, vo_window, RevertToNone, CurrentTime);
458
XFreeGC(mDisplay, vo_gc);
459
vo_gc = XCreateGC(mDisplay, vo_window, 0L, &xgcv);
464
sws_freeContext(swsContext);
470
vo_dwidth = vo_screenwidth;
471
vo_dheight = vo_screenheight;
474
switch ((bpp = myximage->bits_per_pixel))
477
draw_alpha_fnc = draw_alpha_24;
478
out_format = IMGFMT_BGR24;
481
draw_alpha_fnc = draw_alpha_32;
482
out_format = IMGFMT_BGR32;
488
draw_alpha_fnc = draw_alpha_15;
489
out_format = IMGFMT_BGR15;
492
draw_alpha_fnc = draw_alpha_16;
493
out_format = IMGFMT_BGR16;
497
draw_alpha_fnc = draw_alpha_null;
498
out_format = IMGFMT_BGR8;
501
draw_alpha_fnc = draw_alpha_null;
504
/* always allocate swsContext as size could change between frames */
506
sws_getContextFromCmdLine(width, height, in_format, width, height,
511
//printf( "X11 bpp: %d color mask: R:%lX G:%lX B:%lX\n",bpp,myximage->red_mask,myximage->green_mask,myximage->blue_mask );
513
// If we have blue in the lowest bit then obviously RGB
514
mode = ((myximage->blue_mask & 0x01) != 0) ? MODE_RGB : MODE_BGR;
515
#ifdef WORDS_BIGENDIAN
516
if (myximage->byte_order != MSBFirst)
518
if (myximage->byte_order != LSBFirst)
521
mode = ((myximage->blue_mask & 0x01) != 0) ? MODE_BGR : MODE_RGB;
522
// printf( "No support for non-native XImage byte order!\n" );
525
#ifdef WORDS_BIGENDIAN
526
if (mode == MODE_BGR && bpp != 32)
528
mp_msg(MSGT_VO, MSGL_ERR,
529
"BGR%d not supported, please contact the developers\n",
534
if (mode == MODE_BGR)
536
mp_msg(MSGT_VO, MSGL_ERR,
537
"BGR not supported, please contact the developers\n");
543
vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
548
static void Display_Image(XImage * myximage, uint8_t * ImageData)
553
XShmPutImage(mDisplay, vo_window, vo_gc, myximage,
555
(vo_dwidth - swsContext->dstW) / 2,
556
(vo_dheight - myximage->height) / 2, swsContext->dstW,
557
myximage->height, True);
561
XPutImage(mDisplay, vo_window, vo_gc, myximage,
563
(vo_dwidth - swsContext->dstW) / 2,
564
(vo_dheight - myximage->height) / 2, swsContext->dstW,
569
static void draw_osd(void)
571
vo_draw_text(image_width, image_height, draw_alpha_fnc);
574
static void flip_page(void)
576
Display_Image(myximage, ImageData);
577
XSync(mDisplay, False);
580
static int draw_slice(uint8_t * src[], int stride[], int w, int h,
586
if ((old_vo_dwidth != vo_dwidth
587
|| old_vo_dheight != vo_dheight) /*&& y==0 */ && zoomFlag)
589
int newW = vo_dwidth;
590
int newH = vo_dheight;
591
int newAspect = (newW * (1 << 16) + (newH >> 1)) / newH;
592
SwsContext *oldContext = swsContext;
594
if (newAspect > aspect)
595
newW = (newH * aspect + (1 << 15)) >> 16;
597
newH = ((newW << 16) + (aspect >> 1)) / aspect;
599
old_vo_dwidth = vo_dwidth;
600
old_vo_dheight = vo_dheight;
603
newW &= (~31); // not needed but, if the user wants the FAST_BILINEAR SCALER, then its needed
605
swsContext = sws_getContextFromCmdLine(srcW, srcH, in_format,
606
newW, newH, out_format);
609
image_width = (newW + 7) & (~7);
614
sws_freeContext(oldContext);
617
swsContext = oldContext;
620
dstStride[1] = dstStride[2] = 0;
621
dst[1] = dst[2] = NULL;
625
dstStride[0] = -image_width * ((bpp + 7) / 8);
626
dst[0] = ImageData - dstStride[0] * (image_height - 1);
627
sws_scale_ordered(swsContext, src, stride, y, h, dst, dstStride);
630
dstStride[0] = image_width * ((bpp + 7) / 8);
632
sws_scale_ordered(swsContext, src, stride, y, h, dst, dstStride);
637
static int draw_frame(uint8_t * src[])
640
int stride[3] = { 0, 0, 0 };
642
if (in_format == IMGFMT_YUY2)
643
stride[0] = srcW * 2;
644
else if (in_format == IMGFMT_BGR8)
646
else if (in_format == IMGFMT_BGR15)
647
stride[0] = srcW * 2;
648
else if (in_format == IMGFMT_BGR16)
649
stride[0] = srcW * 2;
650
else if (in_format == IMGFMT_BGR24)
651
stride[0] = srcW * 3;
652
else if (in_format == IMGFMT_BGR32)
653
stride[0] = srcW * 4;
655
return draw_slice(src, stride, srcW, srcH, 0, 0);
657
printf("draw_frame() called!!!!!!\n");
662
static uint32_t get_image(mp_image_t * mpi)
665
!IMGFMT_IS_BGR(mpi->imgfmt) ||
666
(IMGFMT_BGR_DEPTH(mpi->imgfmt) != vo_depthonscreen) ||
667
((mpi->type != MP_IMGTYPE_STATIC)
668
&& (mpi->type != MP_IMGTYPE_TEMP))
669
|| (mpi->flags & MP_IMGFLAG_PLANAR)
670
|| (mpi->flags & MP_IMGFLAG_YUV) || (mpi->width != image_width)
671
|| (mpi->height != image_height))
676
mpi->stride[0] = -image_width * ((bpp + 7) / 8);
677
mpi->planes[0] = ImageData - mpi->stride[0] * (image_height - 1);
680
mpi->stride[0] = image_width * ((bpp + 7) / 8);
681
mpi->planes[0] = ImageData;
683
mpi->flags |= MP_IMGFLAG_DIRECT;
688
static int query_format(uint32_t format)
690
mp_msg(MSGT_VO, MSGL_DBG2,
691
"vo_x11: query_format was called: %x (%s)\n", format,
692
vo_format_name(format));
693
if (IMGFMT_IS_BGR(format))
695
if (IMGFMT_BGR_DEPTH(format) <= 8)
696
return 0; // TODO 8bpp not yet fully implemented
697
if (IMGFMT_BGR_DEPTH(format) == vo_depthonscreen)
698
return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_FLIP |
701
return VFCAP_CSP_SUPPORTED | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_FLIP |
708
// case IMGFMT_BGR15:
709
// case IMGFMT_BGR16:
710
// case IMGFMT_BGR24:
711
// case IMGFMT_BGR32:
717
return VFCAP_CSP_SUPPORTED | VFCAP_OSD | VFCAP_SWSCALE | VFCAP_ACCEPT_STRIDE;
723
static void uninit(void)
731
vo_vm_close(mDisplay);
737
sws_freeContext(swsContext);
740
static int preinit(const char *arg)
744
mp_msg(MSGT_VO, MSGL_ERR, "vo_x11: Unknown subdevice: %s\n", arg);
749
return -1; // Can't open X11
753
static int control(uint32_t request, void *data, ...)
758
return (int_pause = 1);
760
return (int_pause = 0);
761
case VOCTRL_QUERY_FORMAT:
762
return query_format(*((uint32_t *) data));
763
case VOCTRL_GUISUPPORT:
765
case VOCTRL_GET_IMAGE:
766
return get_image(data);
767
case VOCTRL_SET_EQUALIZER:
773
value = va_arg(ap, int);
776
return vo_x11_set_equalizer(data, value);
778
case VOCTRL_GET_EQUALIZER:
784
value = va_arg(ap, int *);
787
return vo_x11_get_equalizer(data, value);
792
case VOCTRL_FULLSCREEN:
795
vo_x11_clearwindow(mDisplay, vo_window);