1 /*
2  * XOver a general x11 vo for MPlayer overlay drivers based on:
3  * VIDIX-accelerated overlay in an X window
4  *
5  * copyright (C) Alex Beregszaszi & Zoltan Ponekker & Nick Kurshev
6  *
7  * WS window manager by Pontscho/Fresh!
8  *
9  * based on vo_gl.c and vo_vesa.c and vo_xmga.c (.so mastah! ;))
10  *
11  * This file is part of MPlayer.
12  *
13  * MPlayer is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * MPlayer is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with MPlayer; if not, write to the Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26  */
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <math.h>
32 #include <errno.h>
33 #include <unistd.h>
34 
35 #include "config.h"
36 #include "video_out.h"
37 #include "video_out_internal.h"
38 
39 #include <X11/Xlib.h>
40 #include <X11/Xutil.h>
41 //#include <X11/keysym.h>
42 
43 #ifdef CONFIG_XINERAMA
44 #include <X11/extensions/Xinerama.h>
45 #endif
46 
47 #include "x11_common.h"
48 #include "aspect.h"
49 #include "mp_msg.h"
50 
51 #ifdef CONFIG_GUI
52 #include "gui/interface.h"
53 #endif
54 
55 
56 static const vo_info_t info =
57 {
58     "General X11 driver for overlay capable video output drivers",
59     "xover",
60     "Albeu",
61     ""
62 };
63 
64 LIBVO_EXTERN(xover)
65 
66 /* X11 related variables */
67 /* Colorkey handling */
68 static int colorkey;
69 
70 /* Image parameters */
71 static uint32_t image_width;
72 static uint32_t image_height;
73 static uint32_t image_format;
74 
75 /* Window parameters */
76 static uint32_t window_x, window_y;
77 static uint32_t window_width, window_height;
78 
79 /* used by XGetGeometry & XTranslateCoordinates for moving/resizing window */
80 static uint32_t drwX, drwY, drwWidth, drwHeight, drwBorderWidth,
81     drwDepth, drwcX, drwcY, dwidth, dheight;
82 
83 static const vo_functions_t* sub_vo = NULL;
84 
85 
set_window(int force_update)86 static void set_window(int force_update)
87 {
88   Window mRoot;
89   if ( WinID )
90     {
91       XGetGeometry(mDisplay, vo_window, &mRoot, &drwX, &drwY, &drwWidth,
92 		   &drwHeight, &drwBorderWidth, &drwDepth);
93       drwX = drwY = 0;
94 
95       XTranslateCoordinates(mDisplay, vo_window, mRoot, 0, 0,
96 			    &drwcX, &drwcY, &mRoot);
97       aspect(&dwidth,&dheight,A_NOZOOM);
98       if (!vo_fs)
99 	mp_msg(MSGT_VO, MSGL_V, "[xvidix] dcx: %d dcy: %d dx: %d dy: %d dw: %d dh: %d\n",
100 	       drwcX, drwcY, drwX, drwY, drwWidth, drwHeight);
101 
102       /* following stuff copied from vo_xmga.c */
103     }
104   else
105     {
106       aspect(&dwidth,&dheight,A_NOZOOM);
107       drwcX=drwX=vo_dx; drwcY=drwY=vo_dy; drwWidth=vo_dwidth; drwHeight=vo_dheight;
108     }
109 
110   if (vo_fs)
111     {
112       aspect(&dwidth,&dheight,A_ZOOM);
113       drwX = (vo_screenwidth - ((int)dwidth > vo_screenwidth ? vo_screenwidth : dwidth)) / 2;
114       drwcX = drwX;
115       drwY = (vo_screenheight - ((int)dheight > vo_screenheight ? vo_screenheight : dheight)) / 2;
116       drwcY = drwY;
117       drwWidth = ((int)dwidth > vo_screenwidth ? vo_screenwidth : dwidth);
118       drwHeight = ((int)dheight > vo_screenheight ? vo_screenheight : dheight);
119       mp_msg(MSGT_VO, MSGL_V, "[xvidix-fs] dcx: %d dcy: %d dx: %d dy: %d dw: %d dh: %d\n",
120 	     drwcX, drwcY, drwX, drwY, drwWidth, drwHeight);
121     }
122 
123   vo_dwidth=drwWidth; vo_dheight=drwHeight;
124 
125 #ifdef CONFIG_XINERAMA
126   if (XineramaIsActive(mDisplay))
127     {
128       XineramaScreenInfo *screens;
129       int num_screens;
130       int i = 0;
131 
132       screens = XineramaQueryScreens(mDisplay, &num_screens);
133 
134       /* find the screen we are on */
135       while (i<num_screens &&
136 	     ((screens[i].x_org < (int)drwcX) ||
137 	      (screens[i].y_org < (int)drwcY) ||
138 	      (screens[i].x_org + screens[i].width >= (int)drwcX) ||
139 	      (screens[i].y_org + screens[i].height >= (int)drwcY)))
140 	{
141 	  i++;
142 	}
143 
144       if(i<num_screens)
145 	{
146 	  /* save the screen we are on */
147 	  xinerama_screen = i;
148 	} else {
149 	  /* oops.. couldnt find the screen we are on
150 	   * because the upper left corner left the
151 	   * visual range. assume we are still on the
152 	   * same screen
153 	   */
154 	  i = xinerama_screen;
155 	}
156 
157       /* set drwcX and drwcY to the right values */
158       drwcX = drwcX - screens[i].x_org;
159       drwcY = drwcY - screens[i].y_org;
160       XFree(screens);
161     }
162 #endif
163 
164   if ( vo_panscan > 0.0f && vo_fs )
165     {
166       drwcX-=vo_panscan_x >> 1;
167       drwcY-=vo_panscan_y >> 1;
168       drwX-=vo_panscan_x >> 1;
169       drwY-=vo_panscan_y >> 1;
170       drwWidth+=vo_panscan_x;
171       drwHeight+=vo_panscan_y;
172     }
173 
174   /* set new values in VIDIX */
175   if (force_update || (window_x != drwcX) || (window_y != drwcY) ||
176       (window_width != drwWidth) || (window_height != drwHeight))
177     {
178       mp_win_t w;
179       // do a backup of window coordinates
180       w.x = window_x = drwcX;
181       w.y = window_y = drwcY;
182       vo_dx = drwcX;
183       vo_dy = drwcY;
184       w.w = window_width = drwWidth;
185       w.h = window_height = drwHeight;
186 
187       if(sub_vo->control(VOCTRL_XOVERLAY_SET_WIN,&w) != VO_TRUE)
188 	mp_msg(MSGT_VO, MSGL_ERR, "xvidx: set_overlay failed\n");
189 
190       mp_msg(MSGT_VO, MSGL_V, "[xvidix] window properties: pos: %dx%d, size: %dx%d\n", vo_dx, vo_dy, window_width, window_height);
191     }
192 
193   /* mDrawColorKey: */
194 
195   /* fill drawable with specified color */
196   XSetBackground(mDisplay, vo_gc, 0L);
197   XClearWindow( mDisplay,vo_window );
198   XSetForeground(mDisplay, vo_gc, colorkey);
199   XFillRectangle(mDisplay, vo_window, vo_gc, drwX, drwY, drwWidth,
200 		 (vo_fs ? drwHeight - 1 : drwHeight));
201 
202   /* flush, update drawable */
203   XFlush(mDisplay);
204 
205   return;
206 }
207 
208 /* connect to server, create and map window,
209  * allocate colors and (shared) memory
210  */
config(uint32_t width,uint32_t height,uint32_t d_width,uint32_t d_height,uint32_t flags,char * title,uint32_t format)211 static int config(uint32_t width, uint32_t height, uint32_t d_width,
212 		       uint32_t d_height, uint32_t flags, char *title, uint32_t format)
213 {
214   XVisualInfo vinfo;
215   //    XSizeHints hint;
216   XSetWindowAttributes xswa;
217   unsigned long xswamask;
218   XWindowAttributes attribs;
219   int window_depth, r, g, b;
220   mp_colorkey_t colork;
221   char _title[255];
222 
223   sprintf(_title,"MPlayer %s X11 Overlay",sub_vo->info->name);
224   title = _title;
225 
226   panscan_init();
227 
228   image_height = height;
229   image_width = width;
230   image_format = format;
231 
232   aspect_save_orig(width, height);
233   aspect_save_prescale(d_width, d_height);
234   update_xinerama_info();
235 
236   window_width = d_width;
237   window_height = d_height;
238 
239   r = (vo_colorkey & 0x00ff0000) >> 16;
240   g = (vo_colorkey & 0x0000ff00) >> 8;
241   b = vo_colorkey & 0x000000ff;
242   switch(vo_depthonscreen)
243     {
244     case 32:
245       colorkey = vo_colorkey;
246       break;
247     case 24:
248       colorkey = vo_colorkey & 0x00ffffff;
249       break;
250     case 16:
251       colorkey = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
252       break;
253     case 15:
254       colorkey = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);
255       break;
256     default:
257       mp_msg(MSGT_VO, MSGL_ERR, "Sorry, this (%d) color depth is not supported\n",
258 	     vo_depthonscreen);
259     }
260   mp_msg(MSGT_VO, MSGL_V, "Using colorkey: %x\n", colorkey);
261 
262   aspect(&d_width, &d_height, A_NOZOOM);
263 
264   vo_dx=( vo_screenwidth - d_width ) / 2; vo_dy=( vo_screenheight - d_height ) / 2;
265   vo_dx += xinerama_x;
266   vo_dy += xinerama_y;
267   vo_dwidth=d_width; vo_dheight=d_height;
268 
269 #ifdef CONFIG_GUI
270   if(use_gui) gui(GUI_SETUP_VIDEO_WINDOW, 0); // the GUI will set up / resize the window
271   else
272     {
273 #endif
274 
275       if ( ( flags&VOFLAG_FULLSCREEN )||(flags & VOFLAG_SWSCALE) ) aspect(&d_width, &d_height, A_ZOOM);
276       dwidth = d_width;
277       dheight = d_height;
278       /* Make the window */
279       XGetWindowAttributes(mDisplay, DefaultRootWindow(mDisplay), &attribs);
280 
281       /* from vo_x11 */
282       window_depth = attribs.depth;
283       if ((window_depth != 15) && (window_depth != 16) && (window_depth != 24)
284 	  && (window_depth != 32))
285         window_depth = 24;
286       XMatchVisualInfo(mDisplay, mScreen, window_depth, TrueColor, &vinfo);
287 
288       xswa.background_pixel = BlackPixel(mDisplay, mScreen);
289       xswa.border_pixel     = 0;
290       xswa.colormap         = XCreateColormap(mDisplay, RootWindow(mDisplay, mScreen),
291 					      vinfo.visual, AllocNone);
292       xswamask = CWBackPixel | CWBorderPixel | CWColormap;
293 
294 	    vo_x11_create_vo_window(&vinfo, vo_dx, vo_dy,
295                   window_width, window_height, flags,
296 	          xswa.colormap, "xvidix", title);
297 	    XChangeWindowAttributes(mDisplay, vo_window, xswamask, &xswa);
298 
299 #ifdef CONFIG_GUI
300     }
301 #endif
302 
303   if ( ( !WinID )&&( flags&VOFLAG_FULLSCREEN ) ) { vo_dx=0; vo_dy=0; vo_dwidth=vo_screenwidth; vo_dheight=vo_screenheight; vo_fs=1; }
304 
305   if(sub_vo->config(image_width,image_height,vo_dwidth,vo_dheight,
306 		    flags | VOFLAG_XOVERLAY_SUB_VO,NULL,format)) {
307     mp_msg(MSGT_VO, MSGL_ERR, "xover: sub vo config failed\n");
308     return 1;
309   }
310   colork.x11 = colorkey;
311   colork.r = r;
312   colork.g = g;
313   colork.b = b;
314   if(sub_vo->control(VOCTRL_XOVERLAY_SET_COLORKEY,&colork) != VO_TRUE)
315     mp_msg(MSGT_VO, MSGL_WARN, "xover: set_colorkey failed\n");
316 
317   set_window(1);
318 
319   XSync(mDisplay, False);
320 
321   panscan_calc();
322 
323   return 0;
324 }
325 
check_events(void)326 static void check_events(void)
327 {
328   const int event = vo_x11_check_events(mDisplay);
329 
330   if ((event & VO_EVENT_RESIZE) || (event & VO_EVENT_EXPOSE))
331     set_window(0);
332   sub_vo->check_events();
333   return;
334 }
335 
336 /* draw_osd, flip_page, draw_slice, draw_frame should be
337    overwritten with vidix functions (vosub_vidix.c) */
draw_osd(void)338 static void draw_osd(void)
339 {
340   mp_msg(MSGT_VO, MSGL_FATAL, "xover error: didn't used sub vo draw_osd!\n");
341 }
342 
flip_page(void)343 static void flip_page(void)
344 {
345   mp_msg(MSGT_VO, MSGL_FATAL, "xover error: didn't used sub vo flip_page!\n");
346 }
347 
draw_slice(uint8_t * src[],int stride[],int w,int h,int x,int y)348 static int draw_slice(uint8_t *src[], int stride[],
349 			   int w, int h, int x, int y)
350 {
351   mp_msg(MSGT_VO, MSGL_FATAL, "xover error: didn't used sub vo draw_slice!\n");
352   return 1;
353 }
354 
draw_frame(uint8_t * src[])355 static int draw_frame(uint8_t *src[])
356 {
357   mp_msg(MSGT_VO, MSGL_FATAL, "xover error: didn't used sub vo draw_frame!\n");
358   return 1;
359 }
360 
uninit(void)361 static void uninit(void)
362 {
363   if(!vo_config_count) return;
364   if(sub_vo) sub_vo->uninit();
365   sub_vo = NULL;
366   vo_x11_uninit();
367   // Restore our callbacks
368   video_out_xover.draw_frame = draw_frame;
369   video_out_xover.draw_slice = draw_slice;
370   video_out_xover.flip_page = flip_page;
371   video_out_xover.draw_osd  = draw_osd;
372 }
373 
preinit(const char * arg)374 static int preinit(const char *arg)
375 {
376   int i;
377 
378   if(!arg) {
379     mp_msg(MSGT_VO, MSGL_ERR, "VO XOverlay need a subdriver\n");
380     return 1;
381   }
382 
383   for(i = 0 ; video_out_drivers[i] != NULL ; i++) {
384     if(!strcmp(video_out_drivers[i]->info->short_name,arg) &&
385        strcmp(video_out_drivers[i]->info->short_name,"xover"))
386       break;
387   }
388   if(!video_out_drivers[i]) {
389     mp_msg(MSGT_VO, MSGL_ERR, "VO XOverlay: Subdriver %s not found\n", arg);
390     return 1;
391   }
392   if(video_out_drivers[i]->control(VOCTRL_XOVERLAY_SUPPORT,NULL) != VO_TRUE) {
393     mp_msg(MSGT_VO, MSGL_ERR, "VO XOverlay: %s doesn't support XOverlay\n", arg);
394     return 1;
395   }
396   // X11 init
397   if (!vo_init()) return VO_FALSE;
398   if(video_out_drivers[i]->preinit(NULL)) {
399     mp_msg(MSGT_VO, MSGL_ERR, "VO XOverlay: Subvo init failed\n");
400     return 1;
401   }
402   sub_vo = video_out_drivers[i];
403   // Setup the sub vo callbacks
404   video_out_xover.draw_frame = sub_vo->draw_frame;
405   video_out_xover.draw_slice = sub_vo->draw_slice;
406   video_out_xover.flip_page = sub_vo->flip_page;
407   video_out_xover.draw_osd  = sub_vo->draw_osd;
408   return 0;
409 }
410 
control(uint32_t request,void * data)411 static int control(uint32_t request, void *data)
412 {
413   if(!sub_vo) return VO_ERROR;
414   switch (request) {
415   case VOCTRL_GUISUPPORT:
416     return VO_TRUE;
417   case VOCTRL_GET_PANSCAN:
418     if ( !vo_config_count || !vo_fs ) return VO_FALSE;
419     return VO_TRUE;
420   case VOCTRL_ONTOP:
421     vo_x11_ontop();
422     return VO_TRUE;
423   case VOCTRL_FULLSCREEN:
424     vo_x11_fullscreen();
425     // fallthrough
426   case VOCTRL_SET_PANSCAN:
427     if ( vo_fs && ( vo_panscan != vo_panscan_amount ) )
428       {
429 	panscan_calc();
430 	set_window(0);
431       }
432     return VO_TRUE;
433   default:
434     // Safe atm bcs nothing use more than 1 arg
435     return sub_vo->control(request,data);
436   }
437   return VO_NOTIMPL;
438 }
439