Move common vo initialization code to video_out.c
[mplayer/glamo.git] / libvo / vo_xvidix.c
blobe8360e7266cae4ef4bd6606c32e1b971fb512320
1 /*
2 VIDIX accelerated overlay in a X window
4 (C) Alex Beregszaszi & Zoltan Ponekker & Nick Kurshev
6 WS window manager by Pontscho/Fresh!
8 Based on vo_gl.c and vo_vesa.c and vo_xmga.c (.so mastah! ;))
9 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <math.h>
15 #include <errno.h>
17 #include "config.h"
18 #include "video_out.h"
19 #include "video_out_internal.h"
21 #include <X11/Xlib.h>
22 #include <X11/Xutil.h>
23 //#include <X11/keysym.h>
25 #ifdef HAVE_XINERAMA
26 #include <X11/extensions/Xinerama.h>
27 #endif
29 #include "x11_common.h"
30 #include "aspect.h"
31 #include "mp_msg.h"
33 #include "vosub_vidix.h"
34 #include "vidix/vidixlib.h"
36 #ifdef HAVE_NEW_GUI
37 #include "Gui/interface.h"
38 #endif
41 static vo_info_t info = {
42 "X11 (VIDIX)",
43 "xvidix",
44 "Alex Beregszaszi",
48 LIBVO_EXTERN(xvidix)
49 #define UNUSED(x) ((void)(x)) /* Removes warning about unused arguments */
50 /* X11 related variables */
51 /* Colorkey handling */
52 static XGCValues mGCV;
53 static int colorkey;
54 static vidix_grkey_t gr_key;
56 /* VIDIX related */
57 static char *vidix_name;
59 /* Image parameters */
60 static uint32_t image_width;
61 static uint32_t image_height;
62 static uint32_t image_format;
64 /* Window parameters */
65 static uint32_t window_x, window_y;
66 static uint32_t window_width, window_height;
68 /* used by XGetGeometry & XTranslateCoordinates for moving/resizing window */
69 static uint32_t drwX, drwY, drwWidth, drwHeight, drwBorderWidth,
70 drwDepth, drwcX, drwcY, dwidth, dheight;
72 extern void set_video_eq(int cap);
74 #ifdef HAVE_XINERAMA
75 extern int xinerama_screen;
76 #endif
78 static void set_window(int force_update)
80 Window mRoot;
82 if (WinID)
84 XGetGeometry(mDisplay, vo_window, &mRoot, &drwX, &drwY, &drwWidth,
85 &drwHeight, &drwBorderWidth, &drwDepth);
86 drwX = drwY = 0;
88 XTranslateCoordinates(mDisplay, vo_window, mRoot, 0, 0,
89 &drwcX, &drwcY, &mRoot);
90 aspect(&dwidth, &dheight, A_NOZOOM);
91 if (!vo_fs)
92 mp_msg(MSGT_VO, MSGL_V,
93 "[xvidix] dcx: %d dcy: %d dx: %d dy: %d dw: %d dh: %d\n",
94 drwcX, drwcY, drwX, drwY, drwWidth, drwHeight);
96 /* following stuff copied from vo_xmga.c */
97 } else
99 aspect(&dwidth, &dheight, A_NOZOOM);
100 drwcX = drwX = vo_dx;
101 drwcY = drwY = vo_dy;
102 drwWidth = vo_dwidth;
103 drwHeight = vo_dheight;
106 #if X11_FULLSCREEN
107 if (vo_fs)
109 aspect(&dwidth, &dheight, A_ZOOM);
110 drwX =
111 (vo_screenwidth -
112 (dwidth > vo_screenwidth ? vo_screenwidth : dwidth)) / 2;
113 drwcX = drwX;
114 drwY =
115 (vo_screenheight -
116 (dheight > vo_screenheight ? vo_screenheight : dheight)) / 2;
117 drwcY = drwY;
118 drwWidth = (dwidth > vo_screenwidth ? vo_screenwidth : dwidth);
119 drwHeight =
120 (dheight > vo_screenheight ? vo_screenheight : dheight);
121 mp_msg(MSGT_VO, MSGL_V,
122 "[xvidix-fs] dcx: %d dcy: %d dx: %d dy: %d dw: %d dh: %d\n",
123 drwcX, drwcY, drwX, drwY, drwWidth, drwHeight);
125 #endif
127 vo_dwidth = drwWidth;
128 vo_dheight = drwHeight;
130 #ifdef HAVE_XINERAMA
131 if (XineramaIsActive(mDisplay))
133 XineramaScreenInfo *screens;
134 int num_screens;
135 int i = 0;
137 screens = XineramaQueryScreens(mDisplay, &num_screens);
139 /* find the screen we are on */
140 while (i < num_screens &&
141 ((screens[i].x_org < drwcX) || (screens[i].y_org < drwcY) ||
142 (screens[i].x_org + screens[i].width >= drwcX) ||
143 (screens[i].y_org + screens[i].height >= drwcY)))
145 i++;
148 if (i < num_screens)
150 /* save the screen we are on */
151 xinerama_screen = i;
152 } else
154 /* oops.. couldnt find the screen we are on
155 * because the upper left corner left the
156 * visual range. assume we are still on the
157 * same screen
159 i = xinerama_screen;
162 /* set drwcX and drwcY to the right values */
163 drwcX = drwcX - screens[i].x_org;
164 drwcY = drwcY - screens[i].y_org;
165 XFree(screens);
167 #endif
169 if (vo_panscan > 0.0f && vo_fs)
171 drwcX -= vo_panscan_x >> 1;
172 drwcY -= vo_panscan_y >> 1;
173 drwX -= vo_panscan_x >> 1;
174 drwY -= vo_panscan_y >> 1;
175 drwWidth += vo_panscan_x;
176 drwHeight += vo_panscan_y;
179 /* set new values in VIDIX */
180 if (force_update || (window_x != drwcX) || (window_y != drwcY) ||
181 (window_width != drwWidth) || (window_height != drwHeight))
183 // do a backup of window coordinates
184 window_x = drwcX;
185 window_y = drwcY;
186 vo_dx = drwcX;
187 vo_dy = drwcY;
188 window_width = drwWidth;
189 window_height = drwHeight;
191 /* FIXME: implement runtime resize/move if possible, this way is very ugly! */
192 vidix_stop();
193 if (vidix_init(image_width, image_height, vo_dx, vo_dy,
194 window_width, window_height, image_format,
195 vo_depthonscreen, vo_screenwidth,
196 vo_screenheight) != 0)
198 mp_msg(MSGT_VO, MSGL_FATAL,
199 "Can't initialize VIDIX driver: %s\n", strerror(errno));
200 abort();
202 vidix_start();
205 mp_msg(MSGT_VO, MSGL_V,
206 "[xvidix] window properties: pos: %dx%d, size: %dx%d\n", vo_dx,
207 vo_dy, window_width, window_height);
209 /* mDrawColorKey: */
211 /* fill drawable with specified color */
212 if (!(vo_colorkey & 0xff000000))
214 XSetBackground(mDisplay, vo_gc, 0L);
215 XClearWindow(mDisplay, vo_window);
216 XSetForeground(mDisplay, vo_gc, colorkey);
217 XFillRectangle(mDisplay, vo_window, vo_gc, drwX, drwY, drwWidth,
218 (vo_fs ? drwHeight - 1 : drwHeight));
220 /* flush, update drawable */
221 XFlush(mDisplay);
223 return;
226 /* connect to server, create and map window,
227 * allocate colors and (shared) memory
229 static int config(uint32_t width, uint32_t height, uint32_t d_width,
230 uint32_t d_height, uint32_t flags, char *title,
231 uint32_t format)
233 XVisualInfo vinfo;
235 // XSizeHints hint;
236 XSetWindowAttributes xswa;
237 unsigned long xswamask;
238 XWindowAttributes attribs;
239 int window_depth, r, g, b;
241 title = "MPlayer VIDIX X11 Overlay";
243 image_height = height;
244 image_width = width;
245 image_format = format;
246 vo_mouse_autohide = 1;
248 window_width = d_width;
249 window_height = d_height;
251 // vo_fs = flags&0x01;
252 // if (vo_fs)
253 // { vo_old_width=d_width; vo_old_height=d_height; }
255 r = (vo_colorkey & 0x00ff0000) >> 16;
256 g = (vo_colorkey & 0x0000ff00) >> 8;
257 b = vo_colorkey & 0x000000ff;
258 switch (vo_depthonscreen)
260 case 32:
261 colorkey = vo_colorkey;
262 break;
263 case 24:
264 colorkey = vo_colorkey & 0x00ffffff;
265 break;
266 case 16:
267 colorkey = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
268 break;
269 case 15:
270 colorkey = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);
271 break;
272 default:
273 mp_msg(MSGT_VO, MSGL_ERR,
274 "Sorry, this (%d) color depth is not supported\n",
275 vo_depthonscreen);
277 mp_msg(MSGT_VO, MSGL_V, "Using colorkey: %x\n", colorkey);
279 #ifdef HAVE_NEW_GUI
280 if (use_gui)
281 guiGetEvent(guiSetShVideo, 0); // the GUI will set up / resize the window
282 else
284 #endif
286 #ifdef X11_FULLSCREEN
287 if ((flags & VOFLAG_FULLSCREEN) || (flags & VOFLAG_SWSCALE))
288 aspect(&d_width, &d_height, A_ZOOM);
289 #endif
290 dwidth = d_width;
291 dheight = d_height;
292 /* Make the window */
293 XGetWindowAttributes(mDisplay, DefaultRootWindow(mDisplay),
294 &attribs);
296 /* from vo_x11 */
297 window_depth = attribs.depth;
298 if ((window_depth != 15) && (window_depth != 16)
299 && (window_depth != 24) && (window_depth != 32))
300 window_depth = 24;
301 XMatchVisualInfo(mDisplay, mScreen, window_depth, TrueColor,
302 &vinfo);
304 xswa.background_pixel = BlackPixel(mDisplay, mScreen);
305 xswa.border_pixel = 0;
306 xswa.colormap =
307 XCreateColormap(mDisplay, RootWindow(mDisplay, mScreen),
308 vinfo.visual, AllocNone);
309 xswa.event_mask =
310 StructureNotifyMask | ExposureMask | KeyPressMask |
311 PropertyChangeMask | ((WinID == 0) ? 0
312 : (ButtonPressMask | ButtonReleaseMask |
313 PointerMotionMask));
314 xswamask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
316 if (WinID >= 0)
318 vo_window =
319 WinID ? ((Window) WinID) : RootWindow(mDisplay, mScreen);
320 if (WinID)
322 XUnmapWindow(mDisplay, vo_window);
323 XChangeWindowAttributes(mDisplay, vo_window, xswamask,
324 &xswa);
325 vo_x11_selectinput_witherr(mDisplay, vo_window,
326 StructureNotifyMask |
327 KeyPressMask |
328 PropertyChangeMask |
329 PointerMotionMask |
330 ButtonPressMask |
331 ButtonReleaseMask |
332 ExposureMask);
333 XMapWindow(mDisplay, vo_window);
334 } else
335 XSelectInput(mDisplay, vo_window, ExposureMask);
336 } else
338 if (vo_window == None)
340 vo_window =
341 XCreateWindow(mDisplay, RootWindow(mDisplay, mScreen),
342 vo_dx, vo_dy, window_width,
343 window_height, xswa.border_pixel,
344 vinfo.depth, InputOutput, vinfo.visual,
345 xswamask, &xswa);
347 vo_x11_classhint(mDisplay, vo_window, "xvidix");
348 vo_hidecursor(mDisplay, vo_window);
349 vo_x11_sizehint(vo_dx, vo_dy, vo_dwidth, vo_dheight, 0);
351 XStoreName(mDisplay, vo_window, title);
352 XMapWindow(mDisplay, vo_window);
353 vo_x11_nofs_sizepos(vo_dx, vo_dy, vo_dwidth, vo_dheight);
355 if (flags & VOFLAG_FULLSCREEN)
356 vo_x11_fullscreen();
358 } else
359 vo_x11_nofs_sizepos(vo_dx, vo_dy, vo_dwidth, vo_dheight);
362 if (vo_gc != None)
363 XFreeGC(mDisplay, vo_gc);
364 vo_gc = XCreateGC(mDisplay, vo_window, GCForeground, &mGCV);
365 #ifdef HAVE_NEW_GUI
367 #endif
369 if ((!WinID) && (flags & VOFLAG_FULLSCREEN))
371 vo_dx = 0;
372 vo_dy = 0;
373 vo_dwidth = vo_screenwidth;
374 vo_dheight = vo_screenheight;
375 vo_fs = 1;
378 if (vidix_grkey_support())
380 vidix_grkey_get(&gr_key);
381 gr_key.key_op = KEYS_PUT;
382 if (!(vo_colorkey & 0xff000000))
384 gr_key.ckey.op = CKEY_TRUE;
385 gr_key.ckey.red = r;
386 gr_key.ckey.green = g;
387 gr_key.ckey.blue = b;
388 } else
389 gr_key.ckey.op = CKEY_FALSE;
390 vidix_grkey_set(&gr_key);
393 set_window(1);
395 XSync(mDisplay, False);
397 panscan_calc();
399 if (vo_ontop)
400 vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
402 return (0);
405 static void check_events(void)
407 const int event = vo_x11_check_events(mDisplay);
409 if ((event & VO_EVENT_RESIZE) || (event & VO_EVENT_EXPOSE))
410 set_window(0);
412 return;
415 /* draw_osd, flip_page, draw_slice, draw_frame should be
416 overwritten with vidix functions (vosub_vidix.c) */
417 static void draw_osd(void)
419 mp_msg(MSGT_VO, MSGL_FATAL,
420 "[xvidix] error: didn't used vidix draw_osd!\n");
421 return;
424 static void flip_page(void)
426 mp_msg(MSGT_VO, MSGL_FATAL,
427 "[xvidix] error: didn't used vidix flip_page!\n");
428 return;
431 static int draw_slice(uint8_t * src[], int stride[],
432 int w, int h, int x, int y)
434 UNUSED(src);
435 UNUSED(stride);
436 UNUSED(w);
437 UNUSED(h);
438 UNUSED(x);
439 UNUSED(y);
440 mp_msg(MSGT_VO, MSGL_FATAL,
441 "[xvidix] error: didn't used vidix draw_slice!\n");
442 return (-1);
445 static int draw_frame(uint8_t * src[])
447 UNUSED(src);
448 mp_msg(MSGT_VO, MSGL_FATAL,
449 "[xvidix] error: didn't used vidix draw_frame!\n");
450 return (-1);
453 static int query_format(uint32_t format)
455 return (vidix_query_fourcc(format));
458 static void uninit(void)
460 if (!vo_config_count)
461 return;
462 vidix_term();
464 if (vidix_name)
466 free(vidix_name);
467 vidix_name = NULL;
470 vo_x11_uninit();
473 static int preinit(const char *arg)
476 if (arg)
477 vidix_name = strdup(arg);
478 else
480 mp_msg(MSGT_VO, MSGL_INFO,
481 "No vidix driver name provided, probing available ones (-v option for details)!\n");
482 vidix_name = NULL;
485 if (!vo_init())
486 return (-1);
488 if (vidix_preinit(vidix_name, &video_out_xvidix) != 0)
489 return (1);
491 return (0);
494 static int control(uint32_t request, void *data, ...)
496 switch (request)
498 case VOCTRL_QUERY_FORMAT:
499 return query_format(*((uint32_t *) data));
500 case VOCTRL_GUISUPPORT:
501 return VO_TRUE;
502 case VOCTRL_GET_PANSCAN:
503 if (!vo_config_count || !vo_fs)
504 return VO_FALSE;
505 return VO_TRUE;
506 case VOCTRL_ONTOP:
507 vo_x11_ontop();
508 return VO_TRUE;
509 case VOCTRL_FULLSCREEN:
510 vo_x11_fullscreen();
511 case VOCTRL_SET_PANSCAN:
512 if (vo_fs && (vo_panscan != vo_panscan_amount))
514 panscan_calc();
515 set_window(0);
517 return VO_TRUE;
518 case VOCTRL_SET_EQUALIZER:
520 va_list ap;
521 int value;
523 va_start(ap, data);
524 value = va_arg(ap, int);
526 va_end(ap);
528 return vidix_control(request, data, value);
530 case VOCTRL_GET_EQUALIZER:
532 va_list ap;
533 int *value;
535 va_start(ap, data);
536 value = va_arg(ap, int *);
538 va_end(ap);
540 return vidix_control(request, data, value);
542 case VOCTRL_UPDATE_SCREENINFO:
543 aspect_save_screenres(vo_screenwidth, vo_screenheight);
544 return VO_TRUE;
547 return vidix_control(request, data);
548 // return VO_NOTIMPL;