increase max glyph and lines limit
[mplayer/glamo.git] / libvo / x11_common.c
blob919dec5c1f39cb1419a0b4742e5f3488b9105188
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <math.h>
5 #include <inttypes.h>
6 #include <limits.h>
8 #include "config.h"
9 #include "mp_msg.h"
10 #include "mp_fifo.h"
11 #include "libavutil/common.h"
12 #include "x11_common.h"
14 #ifdef X11_FULLSCREEN
16 #include <string.h>
17 #include <unistd.h>
18 #include <assert.h>
20 #include "video_out.h"
21 #include "aspect.h"
22 #include "geometry.h"
23 #include "help_mp.h"
24 #include "osdep/timer.h"
26 #include <X11/Xmd.h>
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
29 #include <X11/Xatom.h>
31 #ifdef CONFIG_XSS
32 #include <X11/extensions/scrnsaver.h>
33 #endif
35 #ifdef CONFIG_XDPMS
36 #include <X11/extensions/dpms.h>
37 #endif
39 #ifdef CONFIG_XINERAMA
40 #include <X11/extensions/Xinerama.h>
41 #endif
43 #ifdef CONFIG_XF86VM
44 #include <X11/extensions/xf86vmode.h>
45 #endif
47 #ifdef CONFIG_XF86XK
48 #include <X11/XF86keysym.h>
49 #endif
51 #ifdef CONFIG_XV
52 #include <X11/extensions/Xv.h>
53 #include <X11/extensions/Xvlib.h>
55 #include "subopt-helper.h"
56 #endif
58 #include "input/input.h"
59 #include "input/mouse.h"
61 #ifdef CONFIG_GUI
62 #include "gui/interface.h"
63 #include "mplayer.h"
64 #endif
66 #define WIN_LAYER_ONBOTTOM 2
67 #define WIN_LAYER_NORMAL 4
68 #define WIN_LAYER_ONTOP 6
69 #define WIN_LAYER_ABOVE_DOCK 10
71 extern int enable_mouse_movements;
72 int fs_layer = WIN_LAYER_ABOVE_DOCK;
73 static int orig_layer = 0;
74 static int old_gravity = NorthWestGravity;
76 int stop_xscreensaver = 0;
78 static int dpms_disabled = 0;
80 char *mDisplayName = NULL;
81 Display *mDisplay = NULL;
82 Window mRootWin;
83 int mScreen;
84 int mLocalDisplay;
86 /* output window id */
87 int vo_mouse_autohide = 0;
88 int vo_wm_type = 0;
89 int vo_fs_type = 0; // needs to be accessible for GUI X11 code
90 static int vo_fs_flip = 0;
91 char **vo_fstype_list;
93 /* 1 means that the WM is metacity (broken as hell) */
94 int metacity_hack = 0;
96 static Atom XA_NET_SUPPORTED;
97 static Atom XA_NET_WM_STATE;
98 static Atom XA_NET_WM_STATE_FULLSCREEN;
99 static Atom XA_NET_WM_STATE_ABOVE;
100 static Atom XA_NET_WM_STATE_STAYS_ON_TOP;
101 static Atom XA_NET_WM_STATE_BELOW;
102 static Atom XA_NET_WM_PID;
103 static Atom XA_WIN_PROTOCOLS;
104 static Atom XA_WIN_LAYER;
105 static Atom XA_WIN_HINTS;
106 static Atom XAWM_PROTOCOLS;
107 static Atom XAWM_DELETE_WINDOW;
109 #define XA_INIT(x) XA##x = XInternAtom(mDisplay, #x, False)
111 static int vo_old_x = 0;
112 static int vo_old_y = 0;
113 static int vo_old_width = 0;
114 static int vo_old_height = 0;
116 #ifdef CONFIG_XF86VM
117 XF86VidModeModeInfo **vidmodes = NULL;
118 XF86VidModeModeLine modeline;
119 #endif
121 static int vo_x11_get_fs_type(int supported);
125 * Sends the EWMH fullscreen state event.
127 * action: could be one of _NET_WM_STATE_REMOVE -- remove state
128 * _NET_WM_STATE_ADD -- add state
129 * _NET_WM_STATE_TOGGLE -- toggle
131 void vo_x11_ewmh_fullscreen(int action)
133 assert(action == _NET_WM_STATE_REMOVE ||
134 action == _NET_WM_STATE_ADD || action == _NET_WM_STATE_TOGGLE);
136 if (vo_fs_type & vo_wm_FULLSCREEN)
138 XEvent xev;
140 /* init X event structure for _NET_WM_FULLSCREEN client message */
141 xev.xclient.type = ClientMessage;
142 xev.xclient.serial = 0;
143 xev.xclient.send_event = True;
144 xev.xclient.message_type = XA_NET_WM_STATE;
145 xev.xclient.window = vo_window;
146 xev.xclient.format = 32;
147 xev.xclient.data.l[0] = action;
148 xev.xclient.data.l[1] = XA_NET_WM_STATE_FULLSCREEN;
149 xev.xclient.data.l[2] = 0;
150 xev.xclient.data.l[3] = 0;
151 xev.xclient.data.l[4] = 0;
153 /* finally send that damn thing */
154 if (!XSendEvent(mDisplay, DefaultRootWindow(mDisplay), False,
155 SubstructureRedirectMask | SubstructureNotifyMask,
156 &xev))
158 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_EwmhFullscreenStateFailed);
163 void vo_hidecursor(Display * disp, Window win)
165 Cursor no_ptr;
166 Pixmap bm_no;
167 XColor black, dummy;
168 Colormap colormap;
169 static char bm_no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
171 if (WinID == 0)
172 return; // do not hide if playing on the root window
174 colormap = DefaultColormap(disp, DefaultScreen(disp));
175 if ( !XAllocNamedColor(disp, colormap, "black", &black, &dummy) )
177 return; // color alloc failed, give up
179 bm_no = XCreateBitmapFromData(disp, win, bm_no_data, 8, 8);
180 no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
181 XDefineCursor(disp, win, no_ptr);
182 XFreeCursor(disp, no_ptr);
183 if (bm_no != None)
184 XFreePixmap(disp, bm_no);
185 XFreeColors(disp,colormap,&black.pixel,1,0);
188 void vo_showcursor(Display * disp, Window win)
190 if (WinID == 0)
191 return;
192 XDefineCursor(disp, win, 0);
195 static int x11_errorhandler(Display * display, XErrorEvent * event)
197 #define MSGLEN 60
198 char msg[MSGLEN];
200 XGetErrorText(display, event->error_code, (char *) &msg, MSGLEN);
202 mp_msg(MSGT_VO, MSGL_ERR, "X11 error: %s\n", msg);
204 mp_msg(MSGT_VO, MSGL_V,
205 "Type: %x, display: %p, resourceid: %lx, serial: %lx\n",
206 event->type, event->display, event->resourceid, event->serial);
207 mp_msg(MSGT_VO, MSGL_V,
208 "Error code: %x, request code: %x, minor code: %x\n",
209 event->error_code, event->request_code, event->minor_code);
211 // abort();
212 //exit_player("X11 error");
213 return 0;
214 #undef MSGLEN
217 void fstype_help(void)
219 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_AvailableFsType);
220 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FULL_SCREEN_TYPES\n");
222 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "none",
223 "don't set fullscreen window layer");
224 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer",
225 "use _WIN_LAYER hint with default layer");
226 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer=<0..15>",
227 "use _WIN_LAYER hint with a given layer number");
228 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "netwm",
229 "force NETWM style");
230 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "above",
231 "use _NETWM_STATE_ABOVE hint if available");
232 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "below",
233 "use _NETWM_STATE_BELOW hint if available");
234 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "fullscreen",
235 "use _NETWM_STATE_FULLSCREEN hint if availale");
236 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "stays_on_top",
237 "use _NETWM_STATE_STAYS_ON_TOP hint if available");
238 mp_msg(MSGT_VO, MSGL_INFO,
239 "You can also negate the settings with simply putting '-' in the beginning");
240 mp_msg(MSGT_VO, MSGL_INFO, "\n");
243 static void fstype_dump(int fstype)
245 if (fstype)
247 mp_msg(MSGT_VO, MSGL_V, "[x11] Current fstype setting honours");
248 if (fstype & vo_wm_LAYER)
249 mp_msg(MSGT_VO, MSGL_V, " LAYER");
250 if (fstype & vo_wm_FULLSCREEN)
251 mp_msg(MSGT_VO, MSGL_V, " FULLSCREEN");
252 if (fstype & vo_wm_STAYS_ON_TOP)
253 mp_msg(MSGT_VO, MSGL_V, " STAYS_ON_TOP");
254 if (fstype & vo_wm_ABOVE)
255 mp_msg(MSGT_VO, MSGL_V, " ABOVE");
256 if (fstype & vo_wm_BELOW)
257 mp_msg(MSGT_VO, MSGL_V, " BELOW");
258 mp_msg(MSGT_VO, MSGL_V, " X atoms\n");
259 } else
260 mp_msg(MSGT_VO, MSGL_V,
261 "[x11] Current fstype setting doesn't honour any X atoms\n");
264 static int net_wm_support_state_test(Atom atom)
266 #define NET_WM_STATE_TEST(x) { if (atom == XA_NET_WM_STATE_##x) { mp_msg( MSGT_VO,MSGL_V, "[x11] Detected wm supports " #x " state.\n" ); return vo_wm_##x; } }
268 NET_WM_STATE_TEST(FULLSCREEN);
269 NET_WM_STATE_TEST(ABOVE);
270 NET_WM_STATE_TEST(STAYS_ON_TOP);
271 NET_WM_STATE_TEST(BELOW);
272 return 0;
275 static int x11_get_property(Atom type, Atom ** args, unsigned long *nitems)
277 int format;
278 unsigned long bytesafter;
280 return Success ==
281 XGetWindowProperty(mDisplay, mRootWin, type, 0, 16384, False,
282 AnyPropertyType, &type, &format, nitems,
283 &bytesafter, (unsigned char **) args)
284 && *nitems > 0;
287 static int vo_wm_detect(void)
289 int i;
290 int wm = 0;
291 unsigned long nitems;
292 Atom *args = NULL;
294 if (WinID >= 0)
295 return 0;
297 // -- supports layers
298 if (x11_get_property(XA_WIN_PROTOCOLS, &args, &nitems))
300 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports layers.\n");
301 for (i = 0; i < nitems; i++)
303 if (args[i] == XA_WIN_LAYER)
305 wm |= vo_wm_LAYER;
306 metacity_hack |= 1;
307 } else
308 /* metacity is the only window manager I know which reports
309 * supporting only the _WIN_LAYER hint in _WIN_PROTOCOLS.
310 * (what's more support for it is broken) */
311 metacity_hack |= 2;
313 XFree(args);
314 if (wm && (metacity_hack == 1))
316 // metacity claims to support layers, but it is not the truth :-)
317 wm ^= vo_wm_LAYER;
318 mp_msg(MSGT_VO, MSGL_V,
319 "[x11] Using workaround for Metacity bugs.\n");
322 // --- netwm
323 if (x11_get_property(XA_NET_SUPPORTED, &args, &nitems))
325 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports NetWM.\n");
326 for (i = 0; i < nitems; i++)
327 wm |= net_wm_support_state_test(args[i]);
328 XFree(args);
331 if (wm == 0)
332 mp_msg(MSGT_VO, MSGL_V, "[x11] Unknown wm type...\n");
333 return wm;
336 static void init_atoms(void)
338 XA_INIT(_NET_SUPPORTED);
339 XA_INIT(_NET_WM_STATE);
340 XA_INIT(_NET_WM_STATE_FULLSCREEN);
341 XA_INIT(_NET_WM_STATE_ABOVE);
342 XA_INIT(_NET_WM_STATE_STAYS_ON_TOP);
343 XA_INIT(_NET_WM_STATE_BELOW);
344 XA_INIT(_NET_WM_PID);
345 XA_INIT(_WIN_PROTOCOLS);
346 XA_INIT(_WIN_LAYER);
347 XA_INIT(_WIN_HINTS);
348 XA_INIT(WM_PROTOCOLS);
349 XA_INIT(WM_DELETE_WINDOW);
352 void update_xinerama_info(void) {
353 int screen = xinerama_screen;
354 xinerama_x = xinerama_y = 0;
355 #ifdef CONFIG_XINERAMA
356 if (screen >= -1 && XineramaIsActive(mDisplay))
358 XineramaScreenInfo *screens;
359 int num_screens;
361 screens = XineramaQueryScreens(mDisplay, &num_screens);
362 if (screen >= num_screens)
363 screen = num_screens - 1;
364 if (screen == -1) {
365 int x = vo_dx + vo_dwidth / 2;
366 int y = vo_dy + vo_dheight / 2;
367 for (screen = num_screens - 1; screen > 0; screen--) {
368 int left = screens[screen].x_org;
369 int right = left + screens[screen].width;
370 int top = screens[screen].y_org;
371 int bottom = top + screens[screen].height;
372 if (left <= x && x <= right && top <= y && y <= bottom)
373 break;
376 if (screen < 0)
377 screen = 0;
378 vo_screenwidth = screens[screen].width;
379 vo_screenheight = screens[screen].height;
380 xinerama_x = screens[screen].x_org;
381 xinerama_y = screens[screen].y_org;
383 XFree(screens);
385 #endif
386 aspect_save_screenres(vo_screenwidth, vo_screenheight);
389 int vo_init(void)
391 // int mScreen;
392 int depth, bpp;
393 unsigned int mask;
395 // char * DisplayName = ":0.0";
396 // Display * mDisplay;
397 XImage *mXImage = NULL;
399 // Window mRootWin;
400 XWindowAttributes attribs;
401 char *dispName;
403 if (vo_rootwin)
404 WinID = 0; // use root window
406 if (vo_depthonscreen)
408 saver_off(mDisplay);
409 return 1; // already called
412 XSetErrorHandler(x11_errorhandler);
414 #if 0
415 if (!mDisplayName)
416 if (!(mDisplayName = getenv("DISPLAY")))
417 mDisplayName = strdup(":0.0");
418 #else
419 dispName = XDisplayName(mDisplayName);
420 #endif
422 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
424 mDisplay = XOpenDisplay(dispName);
425 if (!mDisplay)
427 mp_msg(MSGT_VO, MSGL_ERR,
428 "vo: couldn't open the X11 display (%s)!\n", dispName);
429 return 0;
431 mScreen = DefaultScreen(mDisplay); // screen ID
432 mRootWin = RootWindow(mDisplay, mScreen); // root window ID
434 init_atoms();
436 #ifdef CONFIG_XF86VM
438 int clock;
440 XF86VidModeGetModeLine(mDisplay, mScreen, &clock, &modeline);
441 if (!vo_screenwidth)
442 vo_screenwidth = modeline.hdisplay;
443 if (!vo_screenheight)
444 vo_screenheight = modeline.vdisplay;
446 #endif
448 if (!vo_screenwidth)
449 vo_screenwidth = DisplayWidth(mDisplay, mScreen);
450 if (!vo_screenheight)
451 vo_screenheight = DisplayHeight(mDisplay, mScreen);
453 // get color depth (from root window, or the best visual):
454 XGetWindowAttributes(mDisplay, mRootWin, &attribs);
455 depth = attribs.depth;
457 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
459 Visual *visual;
461 depth = vo_find_depth_from_visuals(mDisplay, mScreen, &visual);
462 if (depth != -1)
463 mXImage = XCreateImage(mDisplay, visual, depth, ZPixmap,
464 0, NULL, 1, 1, 8, 1);
465 } else
466 mXImage =
467 XGetImage(mDisplay, mRootWin, 0, 0, 1, 1, AllPlanes, ZPixmap);
469 vo_depthonscreen = depth; // display depth on screen
471 // get bits/pixel from XImage structure:
472 if (mXImage == NULL)
474 mask = 0;
475 } else
478 * for the depth==24 case, the XImage structures might use
479 * 24 or 32 bits of data per pixel. The global variable
480 * vo_depthonscreen stores the amount of data per pixel in the
481 * XImage structure!
483 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
485 bpp = mXImage->bits_per_pixel;
486 if ((vo_depthonscreen + 7) / 8 != (bpp + 7) / 8)
487 vo_depthonscreen = bpp; // by A'rpi
488 mask =
489 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
490 mp_msg(MSGT_VO, MSGL_V,
491 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
492 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
493 XDestroyImage(mXImage);
495 if (((vo_depthonscreen + 7) / 8) == 2)
497 if (mask == 0x7FFF)
498 vo_depthonscreen = 15;
499 else if (mask == 0xFFFF)
500 vo_depthonscreen = 16;
502 // XCloseDisplay( mDisplay );
503 /* slightly improved local display detection AST */
504 if (strncmp(dispName, "unix:", 5) == 0)
505 dispName += 4;
506 else if (strncmp(dispName, "localhost:", 10) == 0)
507 dispName += 9;
508 if (*dispName == ':' && atoi(dispName + 1) < 10)
509 mLocalDisplay = 1;
510 else
511 mLocalDisplay = 0;
512 mp_msg(MSGT_VO, MSGL_V,
513 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
514 vo_screenwidth, vo_screenheight, depth, vo_depthonscreen,
515 dispName, mLocalDisplay ? "local" : "remote");
517 vo_wm_type = vo_wm_detect();
519 vo_fs_type = vo_x11_get_fs_type(vo_wm_type);
521 fstype_dump(vo_fs_type);
523 saver_off(mDisplay);
524 return 1;
527 void vo_uninit(void)
529 if (!mDisplay)
531 mp_msg(MSGT_VO, MSGL_V,
532 "vo: x11 uninit called but X11 not initialized..\n");
533 return;
535 // if( !vo_depthonscreen ) return;
536 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
537 XSetErrorHandler(NULL);
538 XCloseDisplay(mDisplay);
539 vo_depthonscreen = 0;
540 mDisplay = NULL;
543 #include "osdep/keycodes.h"
544 #include "wskeys.h"
546 #ifdef XF86XK_AudioPause
547 static const struct keymap keysym_map[] = {
548 {XF86XK_MenuKB, KEY_MENU},
549 {XF86XK_AudioPlay, KEY_PLAY}, {XF86XK_AudioPause, KEY_PAUSE}, {XF86XK_AudioStop, KEY_STOP},
550 {XF86XK_AudioPrev, KEY_PREV}, {XF86XK_AudioNext, KEY_NEXT},
551 {XF86XK_AudioMute, KEY_MUTE}, {XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN}, {XF86XK_AudioRaiseVolume, KEY_VOLUME_UP},
552 {0, 0}
555 static void vo_x11_putkey_ext(int keysym)
557 int mpkey = lookup_keymap_table(keysym_map, keysym);
558 if (mpkey)
559 mplayer_put_key(mpkey);
561 #endif
563 static const struct keymap keymap[] = {
564 // special keys
565 {wsEscape, KEY_ESC}, {wsBackSpace, KEY_BS}, {wsTab, KEY_TAB}, {wsEnter, KEY_ENTER},
567 // cursor keys
568 {wsLeft, KEY_LEFT}, {wsRight, KEY_RIGHT}, {wsUp, KEY_UP}, {wsDown, KEY_DOWN},
570 // navigation block
571 {wsInsert, KEY_INSERT}, {wsDelete, KEY_DELETE}, {wsHome, KEY_HOME}, {wsEnd, KEY_END},
572 {wsPageUp, KEY_PAGE_UP}, {wsPageDown, KEY_PAGE_DOWN},
574 // F-keys
575 {wsF1, KEY_F+1}, {wsF2, KEY_F+2}, {wsF3, KEY_F+3}, {wsF4, KEY_F+4},
576 {wsF5, KEY_F+5}, {wsF6, KEY_F+6}, {wsF7, KEY_F+7}, {wsF8, KEY_F+8},
577 {wsF9, KEY_F+9}, {wsF10, KEY_F+10}, {wsF11, KEY_F+11}, {wsF12, KEY_F+12},
579 // numpad independent of numlock
580 {wsGrayMinus, '-'}, {wsGrayPlus, '+'}, {wsGrayMul, '*'}, {wsGrayDiv, '/'},
581 {wsGrayEnter, KEY_KPENTER},
583 // numpad with numlock
584 {wsGray0, KEY_KP0}, {wsGray1, KEY_KP1}, {wsGray2, KEY_KP2},
585 {wsGray3, KEY_KP3}, {wsGray4, KEY_KP4}, {wsGray5, KEY_KP5},
586 {wsGray6, KEY_KP6}, {wsGray7, KEY_KP7}, {wsGray8, KEY_KP8},
587 {wsGray9, KEY_KP9}, {wsGrayDecimal, KEY_KPDEC},
589 // numpad without numlock
590 {wsGrayInsert, KEY_KPINS}, {wsGrayEnd, KEY_KP1}, {wsGrayDown, KEY_KP2},
591 {wsGrayPgDn, KEY_KP3}, {wsGrayLeft, KEY_KP4}, {wsGray5Dup, KEY_KP5},
592 {wsGrayRight, KEY_KP6}, {wsGrayHome, KEY_KP7}, {wsGrayUp, KEY_KP8},
593 {wsGrayPgUp, KEY_KP9}, {wsGrayDelete, KEY_KPDEL},
595 {0, 0}
598 void vo_x11_putkey(int key)
600 static const char *passthrough_keys = " -+*/<>`~!@#$%^&()_{}:;\"\',.?\\|=[]";
601 int mpkey = 0;
602 if ((key >= 'a' && key <= 'z') ||
603 (key >= 'A' && key <= 'Z') ||
604 (key >= '0' && key <= '9') ||
605 (key > 0 && key < 256 && strchr(passthrough_keys, key)))
606 mpkey = key;
608 if (!mpkey)
609 mpkey = lookup_keymap_table(keymap, key);
611 if (mpkey)
612 mplayer_put_key(mpkey);
616 // ----- Motif header: -------
618 #define MWM_HINTS_FUNCTIONS (1L << 0)
619 #define MWM_HINTS_DECORATIONS (1L << 1)
620 #define MWM_HINTS_INPUT_MODE (1L << 2)
621 #define MWM_HINTS_STATUS (1L << 3)
623 #define MWM_FUNC_ALL (1L << 0)
624 #define MWM_FUNC_RESIZE (1L << 1)
625 #define MWM_FUNC_MOVE (1L << 2)
626 #define MWM_FUNC_MINIMIZE (1L << 3)
627 #define MWM_FUNC_MAXIMIZE (1L << 4)
628 #define MWM_FUNC_CLOSE (1L << 5)
630 #define MWM_DECOR_ALL (1L << 0)
631 #define MWM_DECOR_BORDER (1L << 1)
632 #define MWM_DECOR_RESIZEH (1L << 2)
633 #define MWM_DECOR_TITLE (1L << 3)
634 #define MWM_DECOR_MENU (1L << 4)
635 #define MWM_DECOR_MINIMIZE (1L << 5)
636 #define MWM_DECOR_MAXIMIZE (1L << 6)
638 #define MWM_INPUT_MODELESS 0
639 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
640 #define MWM_INPUT_SYSTEM_MODAL 2
641 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
642 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
644 #define MWM_TEAROFF_WINDOW (1L<<0)
646 typedef struct
648 long flags;
649 long functions;
650 long decorations;
651 long input_mode;
652 long state;
653 } MotifWmHints;
655 static MotifWmHints vo_MotifWmHints;
656 static Atom vo_MotifHints = None;
658 void vo_x11_decoration(Display * vo_Display, Window w, int d)
660 static unsigned int olddecor = MWM_DECOR_ALL;
661 static unsigned int oldfuncs =
662 MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
663 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE;
664 Atom mtype;
665 int mformat;
666 unsigned long mn, mb;
668 if (!WinID)
669 return;
671 if (vo_fsmode & 8)
673 XSetTransientForHint(vo_Display, w,
674 RootWindow(vo_Display, mScreen));
677 vo_MotifHints = XInternAtom(vo_Display, "_MOTIF_WM_HINTS", 0);
678 if (vo_MotifHints != None)
680 if (!d)
682 MotifWmHints *mhints = NULL;
684 XGetWindowProperty(vo_Display, w, vo_MotifHints, 0, 20, False,
685 vo_MotifHints, &mtype, &mformat, &mn,
686 &mb, (unsigned char **) &mhints);
687 if (mhints)
689 if (mhints->flags & MWM_HINTS_DECORATIONS)
690 olddecor = mhints->decorations;
691 if (mhints->flags & MWM_HINTS_FUNCTIONS)
692 oldfuncs = mhints->functions;
693 XFree(mhints);
697 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
698 vo_MotifWmHints.flags =
699 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
700 if (d)
702 vo_MotifWmHints.functions = oldfuncs;
703 d = olddecor;
705 #if 0
706 vo_MotifWmHints.decorations =
707 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
708 #else
709 vo_MotifWmHints.decorations =
710 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
711 #endif
712 XChangeProperty(vo_Display, w, vo_MotifHints, vo_MotifHints, 32,
713 PropModeReplace,
714 (unsigned char *) &vo_MotifWmHints,
715 (vo_fsmode & 4) ? 4 : 5);
719 void vo_x11_classhint(Display * display, Window window, char *name)
721 XClassHint wmClass;
722 pid_t pid = getpid();
724 wmClass.res_name = name;
725 wmClass.res_class = "MPlayer";
726 XSetClassHint(display, window, &wmClass);
727 XChangeProperty(display, window, XA_NET_WM_PID, XA_CARDINAL, 32,
728 PropModeReplace, (unsigned char *) &pid, 1);
731 Window vo_window = None;
732 GC vo_gc = NULL;
733 GC f_gc = NULL;
734 XSizeHints vo_hint;
736 #ifdef CONFIG_GUI
737 void vo_setwindow(Window w, GC g)
739 vo_window = w;
740 vo_gc = g;
742 #endif
744 void vo_x11_uninit(void)
746 saver_on(mDisplay);
747 if (vo_window != None)
748 vo_showcursor(mDisplay, vo_window);
750 if (f_gc)
752 XFreeGC(mDisplay, f_gc);
753 f_gc = NULL;
755 #ifdef CONFIG_GUI
756 /* destroy window only if it's not controlled by the GUI */
757 if (!use_gui)
758 #endif
760 if (vo_gc)
762 XSetBackground(mDisplay, vo_gc, 0);
763 XFreeGC(mDisplay, vo_gc);
764 vo_gc = NULL;
766 if (vo_window != None)
768 XClearWindow(mDisplay, vo_window);
769 if (WinID < 0)
771 XEvent xev;
773 XUnmapWindow(mDisplay, vo_window);
774 XDestroyWindow(mDisplay, vo_window);
777 XNextEvent(mDisplay, &xev);
779 while (xev.type != DestroyNotify
780 || xev.xdestroywindow.event != vo_window);
782 vo_window = None;
784 vo_fs = 0;
785 vo_old_width = vo_old_height = 0;
789 static unsigned int mouse_timer;
790 static int mouse_waiting_hide;
792 int vo_x11_check_events(Display * mydisplay)
794 int ret = 0;
795 XEvent Event;
796 char buf[100];
797 KeySym keySym;
798 static XComposeStatus stat;
800 // unsigned long vo_KeyTable[512];
802 if ((vo_mouse_autohide) && mouse_waiting_hide &&
803 (GetTimerMS() - mouse_timer >= 1000)) {
804 vo_hidecursor(mydisplay, vo_window);
805 mouse_waiting_hide = 0;
808 while (XPending(mydisplay))
810 XNextEvent(mydisplay, &Event);
811 #ifdef CONFIG_GUI
812 if (use_gui)
814 guiGetEvent(0, (char *) &Event);
815 if (vo_window != Event.xany.window)
816 continue;
818 #endif
819 // printf("\rEvent.type=%X \n",Event.type);
820 switch (Event.type)
822 case Expose:
823 ret |= VO_EVENT_EXPOSE;
824 break;
825 case ConfigureNotify:
826 // if (!vo_fs && (Event.xconfigure.width == vo_screenwidth || Event.xconfigure.height == vo_screenheight)) break;
827 // if (vo_fs && Event.xconfigure.width != vo_screenwidth && Event.xconfigure.height != vo_screenheight) break;
828 if (vo_window == None)
829 break;
830 vo_x11_update_geometry();
831 ret |= VO_EVENT_RESIZE;
832 break;
833 case KeyPress:
835 int key;
837 #ifdef CONFIG_GUI
838 if ( use_gui ) { break; }
839 #endif
841 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
842 &stat);
843 #ifdef XF86XK_AudioPause
844 vo_x11_putkey_ext(keySym);
845 #endif
846 key =
847 ((keySym & 0xff00) !=
848 0 ? ((keySym & 0x00ff) + 256) : (keySym));
849 vo_x11_putkey(key);
850 ret |= VO_EVENT_KEYPRESS;
852 break;
853 case MotionNotify:
854 if(enable_mouse_movements)
856 char cmd_str[40];
857 sprintf(cmd_str,"set_mouse_pos %i %i",Event.xmotion.x, Event.xmotion.y);
858 mp_input_queue_cmd(mp_input_parse_cmd(cmd_str));
861 if (vo_mouse_autohide)
863 vo_showcursor(mydisplay, vo_window);
864 mouse_waiting_hide = 1;
865 mouse_timer = GetTimerMS();
867 break;
868 case ButtonPress:
869 if (vo_mouse_autohide)
871 vo_showcursor(mydisplay, vo_window);
872 mouse_waiting_hide = 1;
873 mouse_timer = GetTimerMS();
875 #ifdef CONFIG_GUI
876 // Ignore mouse button 1-3 under GUI.
877 if (use_gui && (Event.xbutton.button >= 1)
878 && (Event.xbutton.button <= 3))
879 break;
880 #endif
881 mplayer_put_key((MOUSE_BTN0 + Event.xbutton.button -
882 1) | MP_KEY_DOWN);
883 break;
884 case ButtonRelease:
885 if (vo_mouse_autohide)
887 vo_showcursor(mydisplay, vo_window);
888 mouse_waiting_hide = 1;
889 mouse_timer = GetTimerMS();
891 #ifdef CONFIG_GUI
892 // Ignore mouse button 1-3 under GUI.
893 if (use_gui && (Event.xbutton.button >= 1)
894 && (Event.xbutton.button <= 3))
895 break;
896 #endif
897 mplayer_put_key(MOUSE_BTN0 + Event.xbutton.button - 1);
898 break;
899 case PropertyNotify:
901 char *name =
902 XGetAtomName(mydisplay, Event.xproperty.atom);
904 if (!name)
905 break;
907 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
909 XFree(name);
911 break;
912 case MapNotify:
913 vo_hint.win_gravity = old_gravity;
914 XSetWMNormalHints(mDisplay, vo_window, &vo_hint);
915 vo_fs_flip = 0;
916 break;
917 case ClientMessage:
918 if (Event.xclient.message_type == XAWM_PROTOCOLS &&
919 Event.xclient.data.l[0] == XAWM_DELETE_WINDOW)
920 mplayer_put_key(KEY_CLOSE_WIN);
921 break;
924 return ret;
928 * \brief sets the size and position of the non-fullscreen window.
930 void vo_x11_nofs_sizepos(int x, int y, int width, int height)
932 vo_x11_sizehint(x, y, width, height, 0);
933 if (vo_fs) {
934 vo_old_x = x;
935 vo_old_y = y;
936 vo_old_width = width;
937 vo_old_height = height;
939 else
941 vo_dwidth = width;
942 vo_dheight = height;
943 XMoveResizeWindow(mDisplay, vo_window, x, y, width, height);
947 void vo_x11_sizehint(int x, int y, int width, int height, int max)
949 vo_hint.flags = 0;
950 if (vo_keepaspect)
952 vo_hint.flags |= PAspect;
953 vo_hint.min_aspect.x = width;
954 vo_hint.min_aspect.y = height;
955 vo_hint.max_aspect.x = width;
956 vo_hint.max_aspect.y = height;
959 vo_hint.flags |= PPosition | PSize;
960 vo_hint.x = x;
961 vo_hint.y = y;
962 vo_hint.width = width;
963 vo_hint.height = height;
964 if (max)
966 vo_hint.flags |= PMaxSize;
967 vo_hint.max_width = width;
968 vo_hint.max_height = height;
969 } else
971 vo_hint.max_width = 0;
972 vo_hint.max_height = 0;
975 // Set minimum height/width to 4 to avoid off-by-one errors
976 // and because mga_vid requires a minimal size of 4 pixels.
977 vo_hint.flags |= PMinSize;
978 vo_hint.min_width = vo_hint.min_height = 4;
980 // Set the base size. A window manager might display the window
981 // size to the user relative to this.
982 // Setting these to width/height might be nice, but e.g. fluxbox can't handle it.
983 vo_hint.flags |= PBaseSize;
984 vo_hint.base_width = 0 /*width*/;
985 vo_hint.base_height = 0 /*height*/;
987 vo_hint.flags |= PWinGravity;
988 vo_hint.win_gravity = StaticGravity;
989 XSetWMNormalHints(mDisplay, vo_window, &vo_hint);
992 static int vo_x11_get_gnome_layer(Display * mDisplay, Window win)
994 Atom type;
995 int format;
996 unsigned long nitems;
997 unsigned long bytesafter;
998 unsigned short *args = NULL;
1000 if (XGetWindowProperty(mDisplay, win, XA_WIN_LAYER, 0, 16384,
1001 False, AnyPropertyType, &type, &format, &nitems,
1002 &bytesafter,
1003 (unsigned char **) &args) == Success
1004 && nitems > 0 && args)
1006 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
1007 *args);
1008 return *args;
1010 return WIN_LAYER_NORMAL;
1014 Window vo_x11_create_smooth_window(Display * mDisplay, Window mRoot,
1015 Visual * vis, int x, int y,
1016 unsigned int width, unsigned int height,
1017 int depth, Colormap col_map)
1019 unsigned long xswamask = CWBorderPixel;
1020 XSetWindowAttributes xswa;
1021 Window ret_win;
1023 if (col_map != CopyFromParent)
1025 xswa.colormap = col_map;
1026 xswamask |= CWColormap;
1028 xswa.background_pixel = 0;
1029 xswa.border_pixel = 0;
1030 xswa.backing_store = NotUseful;
1031 xswa.bit_gravity = StaticGravity;
1033 ret_win =
1034 XCreateWindow(mDisplay, mRootWin, x, y, width, height, 0, depth,
1035 CopyFromParent, vis, xswamask, &xswa);
1036 XSetWMProtocols(mDisplay, ret_win, &XAWM_DELETE_WINDOW, 1);
1037 if (!f_gc)
1038 f_gc = XCreateGC(mDisplay, ret_win, 0, 0);
1039 XSetForeground(mDisplay, f_gc, 0);
1041 return ret_win;
1045 * \brief create and setup a window suitable for display
1046 * \param vis Visual to use for creating the window
1047 * \param x x position of window
1048 * \param y y position of window
1049 * \param width width of window
1050 * \param height height of window
1051 * \param flags flags for window creation.
1052 * Only VOFLAG_FULLSCREEN is supported so far.
1053 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1054 * \param classname name to use for the classhint
1055 * \param title title for the window
1057 * This also does the grunt-work like setting Window Manager hints etc.
1058 * If vo_window is already set it just moves and resizes it.
1060 void vo_x11_create_vo_window(XVisualInfo *vis, int x, int y,
1061 unsigned int width, unsigned int height, int flags,
1062 Colormap col_map,
1063 const char *classname, const char *title)
1065 XGCValues xgcv;
1066 if (WinID >= 0) {
1067 vo_window = WinID ? (Window)WinID : mRootWin;
1068 if (col_map != CopyFromParent) {
1069 unsigned long xswamask = CWColormap;
1070 XSetWindowAttributes xswa;
1071 xswa.colormap = col_map;
1072 XUnmapWindow(mDisplay, vo_window);
1073 XChangeWindowAttributes(mDisplay, vo_window, xswamask, &xswa);
1074 XMapWindow(mDisplay, vo_window);
1076 if (WinID) vo_x11_update_geometry();
1077 vo_x11_selectinput_witherr(mDisplay, vo_window,
1078 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1079 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1080 goto final;
1082 if (vo_window == None) {
1083 XSizeHints hint;
1084 XEvent xev;
1085 vo_fs = 0;
1086 vo_dwidth = width;
1087 vo_dheight = height;
1088 vo_window = vo_x11_create_smooth_window(mDisplay, mRootWin, vis->visual,
1089 x, y, width, height, vis->depth, col_map);
1090 vo_x11_classhint(mDisplay, vo_window, classname);
1091 XStoreName(mDisplay, vo_window, title);
1092 vo_hidecursor(mDisplay, vo_window);
1093 XSelectInput(mDisplay, vo_window, StructureNotifyMask);
1094 hint.x = x; hint.y = y;
1095 hint.width = width; hint.height = height;
1096 hint.flags = PPosition | PSize;
1097 XSetStandardProperties(mDisplay, vo_window, title, title, None, NULL, 0, &hint);
1098 vo_x11_sizehint(x, y, width, height, 0);
1099 if (!vo_border) vo_x11_decoration(mDisplay, vo_window, 0);
1100 // map window
1101 XMapWindow(mDisplay, vo_window);
1102 XClearWindow(mDisplay, vo_window);
1103 // wait for map
1104 do {
1105 XNextEvent(mDisplay, &xev);
1106 } while (xev.type != MapNotify || xev.xmap.event != vo_window);
1107 XSelectInput(mDisplay, vo_window, NoEventMask);
1108 XSync(mDisplay, False);
1109 vo_x11_selectinput_witherr(mDisplay, vo_window,
1110 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1111 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1113 if (vo_ontop) vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
1114 vo_x11_nofs_sizepos(vo_dx, vo_dy, width, height);
1115 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1116 vo_x11_fullscreen();
1117 final:
1118 if (vo_gc != None)
1119 XFreeGC(mDisplay, vo_gc);
1120 vo_gc = XCreateGC(mDisplay, vo_window, GCForeground, &xgcv);
1121 XSync(mDisplay, False);
1122 vo_mouse_autohide = 1;
1125 void vo_x11_clearwindow_part(Display * mDisplay, Window vo_window,
1126 int img_width, int img_height, int use_fs)
1128 int u_dheight, u_dwidth, left_ov, left_ov2;
1130 if (!f_gc)
1131 return;
1133 u_dheight = use_fs ? vo_screenheight : vo_dheight;
1134 u_dwidth = use_fs ? vo_screenwidth : vo_dwidth;
1135 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1136 return;
1138 left_ov = (u_dheight - img_height) / 2;
1139 left_ov2 = (u_dwidth - img_width) / 2;
1141 XFillRectangle(mDisplay, vo_window, f_gc, 0, 0, u_dwidth, left_ov);
1142 XFillRectangle(mDisplay, vo_window, f_gc, 0, u_dheight - left_ov - 1,
1143 u_dwidth, left_ov + 1);
1145 if (u_dwidth > img_width)
1147 XFillRectangle(mDisplay, vo_window, f_gc, 0, left_ov, left_ov2,
1148 img_height);
1149 XFillRectangle(mDisplay, vo_window, f_gc, u_dwidth - left_ov2 - 1,
1150 left_ov, left_ov2 + 1, img_height);
1153 XFlush(mDisplay);
1156 void vo_x11_clearwindow(Display * mDisplay, Window vo_window)
1158 if (!f_gc)
1159 return;
1160 XFillRectangle(mDisplay, vo_window, f_gc, 0, 0, vo_screenwidth,
1161 vo_screenheight);
1163 XFlush(mDisplay);
1167 void vo_x11_setlayer(Display * mDisplay, Window vo_window, int layer)
1169 if (WinID >= 0)
1170 return;
1172 if (vo_fs_type & vo_wm_LAYER)
1174 XClientMessageEvent xev;
1176 if (!orig_layer)
1177 orig_layer = vo_x11_get_gnome_layer(mDisplay, vo_window);
1179 memset(&xev, 0, sizeof(xev));
1180 xev.type = ClientMessage;
1181 xev.display = mDisplay;
1182 xev.window = vo_window;
1183 xev.message_type = XA_WIN_LAYER;
1184 xev.format = 32;
1185 xev.data.l[0] = layer ? fs_layer : orig_layer; // if not fullscreen, stay on default layer
1186 xev.data.l[1] = CurrentTime;
1187 mp_msg(MSGT_VO, MSGL_V,
1188 "[x11] Layered style stay on top (layer %ld).\n",
1189 xev.data.l[0]);
1190 XSendEvent(mDisplay, mRootWin, False, SubstructureNotifyMask,
1191 (XEvent *) & xev);
1192 } else if (vo_fs_type & vo_wm_NETWM)
1194 XClientMessageEvent xev;
1195 char *state;
1197 memset(&xev, 0, sizeof(xev));
1198 xev.type = ClientMessage;
1199 xev.message_type = XA_NET_WM_STATE;
1200 xev.display = mDisplay;
1201 xev.window = vo_window;
1202 xev.format = 32;
1203 xev.data.l[0] = layer;
1205 if (vo_fs_type & vo_wm_STAYS_ON_TOP)
1206 xev.data.l[1] = XA_NET_WM_STATE_STAYS_ON_TOP;
1207 else if (vo_fs_type & vo_wm_ABOVE)
1208 xev.data.l[1] = XA_NET_WM_STATE_ABOVE;
1209 else if (vo_fs_type & vo_wm_FULLSCREEN)
1210 xev.data.l[1] = XA_NET_WM_STATE_FULLSCREEN;
1211 else if (vo_fs_type & vo_wm_BELOW)
1212 // This is not fallback. We can safely assume that the situation
1213 // where only NETWM_STATE_BELOW is supported doesn't exist.
1214 xev.data.l[1] = XA_NET_WM_STATE_BELOW;
1216 XSendEvent(mDisplay, mRootWin, False, SubstructureRedirectMask,
1217 (XEvent *) & xev);
1218 state = XGetAtomName(mDisplay, xev.data.l[1]);
1219 mp_msg(MSGT_VO, MSGL_V,
1220 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1221 layer, state);
1222 XFree(state);
1226 static int vo_x11_get_fs_type(int supported)
1228 int i;
1229 int type = supported;
1231 if (vo_fstype_list)
1233 i = 0;
1234 for (i = 0; vo_fstype_list[i]; i++)
1236 int neg = 0;
1237 char *arg = vo_fstype_list[i];
1239 if (vo_fstype_list[i][0] == '-')
1241 neg = 1;
1242 arg = vo_fstype_list[i] + 1;
1245 if (!strncmp(arg, "layer", 5))
1247 if (!neg && (arg[5] == '='))
1249 char *endptr = NULL;
1250 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1252 if (endptr && *endptr == '\0' && layer >= 0
1253 && layer <= 15)
1254 fs_layer = layer;
1256 if (neg)
1257 type &= ~vo_wm_LAYER;
1258 else
1259 type |= vo_wm_LAYER;
1260 } else if (!strcmp(arg, "above"))
1262 if (neg)
1263 type &= ~vo_wm_ABOVE;
1264 else
1265 type |= vo_wm_ABOVE;
1266 } else if (!strcmp(arg, "fullscreen"))
1268 if (neg)
1269 type &= ~vo_wm_FULLSCREEN;
1270 else
1271 type |= vo_wm_FULLSCREEN;
1272 } else if (!strcmp(arg, "stays_on_top"))
1274 if (neg)
1275 type &= ~vo_wm_STAYS_ON_TOP;
1276 else
1277 type |= vo_wm_STAYS_ON_TOP;
1278 } else if (!strcmp(arg, "below"))
1280 if (neg)
1281 type &= ~vo_wm_BELOW;
1282 else
1283 type |= vo_wm_BELOW;
1284 } else if (!strcmp(arg, "netwm"))
1286 if (neg)
1287 type &= ~vo_wm_NETWM;
1288 else
1289 type |= vo_wm_NETWM;
1290 } else if (!strcmp(arg, "none"))
1291 return 0;
1295 return type;
1299 * \brief update vo_dx, vo_dy, vo_dwidth and vo_dheight with current values of vo_window
1300 * \return returns current color depth of vo_window
1302 int vo_x11_update_geometry(void) {
1303 unsigned depth, w, h;
1304 int dummy_int;
1305 Window dummy_win;
1306 XGetGeometry(mDisplay, vo_window, &dummy_win, &dummy_int, &dummy_int,
1307 &w, &h, &dummy_int, &depth);
1308 if (w <= INT_MAX && h <= INT_MAX) { vo_dwidth = w; vo_dheight = h; }
1309 XTranslateCoordinates(mDisplay, vo_window, mRootWin, 0, 0, &vo_dx, &vo_dy,
1310 &dummy_win);
1311 return depth <= INT_MAX ? depth : 0;
1314 void vo_x11_fullscreen(void)
1316 int x, y, w, h;
1318 if (WinID >= 0 || vo_fs_flip)
1319 return;
1321 if (vo_fs)
1323 // fs->win
1324 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1326 x = vo_old_x;
1327 y = vo_old_y;
1328 w = vo_old_width;
1329 h = vo_old_height;
1332 vo_x11_ewmh_fullscreen(_NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1333 vo_fs = VO_FALSE;
1334 } else
1336 // win->fs
1337 vo_x11_ewmh_fullscreen(_NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1339 vo_fs = VO_TRUE;
1340 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1342 vo_old_x = vo_dx;
1343 vo_old_y = vo_dy;
1344 vo_old_width = vo_dwidth;
1345 vo_old_height = vo_dheight;
1347 update_xinerama_info();
1348 x = xinerama_x;
1349 y = xinerama_y;
1350 w = vo_screenwidth;
1351 h = vo_screenheight;
1354 long dummy;
1356 XGetWMNormalHints(mDisplay, vo_window, &vo_hint, &dummy);
1357 if (!(vo_hint.flags & PWinGravity))
1358 old_gravity = NorthWestGravity;
1359 else
1360 old_gravity = vo_hint.win_gravity;
1362 if (vo_wm_type == 0 && !(vo_fsmode & 16))
1364 XUnmapWindow(mDisplay, vo_window); // required for MWM
1365 XWithdrawWindow(mDisplay, vo_window, mScreen);
1366 vo_fs_flip = 1;
1369 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1371 vo_x11_decoration(mDisplay, vo_window, vo_border && !vo_fs);
1372 vo_x11_sizehint(x, y, w, h, 0);
1373 vo_x11_setlayer(mDisplay, vo_window, vo_fs);
1376 XMoveResizeWindow(mDisplay, vo_window, x, y, w, h);
1378 /* some WMs lose ontop after fullscreen */
1379 if ((!(vo_fs)) & vo_ontop)
1380 vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
1382 XMapRaised(mDisplay, vo_window);
1383 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1384 XMoveResizeWindow(mDisplay, vo_window, x, y, w, h);
1385 XRaiseWindow(mDisplay, vo_window);
1386 XFlush(mDisplay);
1389 void vo_x11_ontop(void)
1391 vo_ontop = (!(vo_ontop));
1393 vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
1396 void vo_x11_border(void)
1398 vo_border = !vo_border;
1399 vo_x11_decoration(mDisplay, vo_window, vo_border && !vo_fs);
1403 * XScreensaver stuff
1406 static int screensaver_off;
1407 static unsigned int time_last;
1409 void xscreensaver_heartbeat(void)
1411 unsigned int time = GetTimerMS();
1413 if (mDisplay && screensaver_off && (time - time_last) > 30000)
1415 time_last = time;
1417 XResetScreenSaver(mDisplay);
1421 static int xss_suspend(Bool suspend)
1423 #ifndef CONFIG_XSS
1424 return 0;
1425 #else
1426 int event, error, major, minor;
1427 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1428 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1429 return 0;
1430 if (major < 1 || (major == 1 && minor < 1))
1431 return 0;
1432 XScreenSaverSuspend(mDisplay, suspend);
1433 return 1;
1434 #endif
1438 * End of XScreensaver stuff
1441 void saver_on(Display * mDisplay)
1444 if (!screensaver_off)
1445 return;
1446 screensaver_off = 0;
1447 if (xss_suspend(False))
1448 return;
1449 #ifdef CONFIG_XDPMS
1450 if (dpms_disabled)
1452 int nothing;
1453 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1455 if (!DPMSEnable(mDisplay))
1456 { // restoring power saving settings
1457 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1458 } else
1460 // DPMS does not seem to be enabled unless we call DPMSInfo
1461 BOOL onoff;
1462 CARD16 state;
1464 DPMSForceLevel(mDisplay, DPMSModeOn);
1465 DPMSInfo(mDisplay, &state, &onoff);
1466 if (onoff)
1468 mp_msg(MSGT_VO, MSGL_V,
1469 "Successfully enabled DPMS\n");
1470 } else
1472 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1476 dpms_disabled = 0;
1478 #endif
1481 void saver_off(Display * mDisplay)
1483 int nothing;
1485 if (screensaver_off)
1486 return;
1487 screensaver_off = 1;
1488 if (xss_suspend(True))
1489 return;
1490 #ifdef CONFIG_XDPMS
1491 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1493 BOOL onoff;
1494 CARD16 state;
1496 DPMSInfo(mDisplay, &state, &onoff);
1497 if (onoff)
1499 Status stat;
1501 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1502 dpms_disabled = 1;
1503 stat = DPMSDisable(mDisplay); // monitor powersave off
1504 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1507 #endif
1510 static XErrorHandler old_handler = NULL;
1511 static int selectinput_err = 0;
1512 static int x11_selectinput_errorhandler(Display * display,
1513 XErrorEvent * event)
1515 if (event->error_code == BadAccess)
1517 selectinput_err = 1;
1518 mp_msg(MSGT_VO, MSGL_ERR,
1519 "X11 error: BadAccess during XSelectInput Call\n");
1520 mp_msg(MSGT_VO, MSGL_ERR,
1521 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1522 /* If you think MPlayer should shutdown with this error,
1523 * comment out the following line */
1524 return 0;
1526 if (old_handler != NULL)
1527 old_handler(display, event);
1528 else
1529 x11_errorhandler(display, event);
1530 return 0;
1533 void vo_x11_selectinput_witherr(Display * display, Window w,
1534 long event_mask)
1536 XSync(display, False);
1537 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1538 selectinput_err = 0;
1539 if (vo_nomouse_input)
1541 XSelectInput(display, w,
1542 event_mask &
1543 (~(ButtonPressMask | ButtonReleaseMask)));
1544 } else
1546 XSelectInput(display, w, event_mask);
1548 XSync(display, False);
1549 XSetErrorHandler(old_handler);
1550 if (selectinput_err)
1552 mp_msg(MSGT_VO, MSGL_ERR,
1553 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1554 XSelectInput(display, w,
1555 event_mask &
1557 (ButtonPressMask | ButtonReleaseMask |
1558 PointerMotionMask)));
1562 #ifdef CONFIG_XF86VM
1563 void vo_vm_switch(void)
1565 int vm_event, vm_error;
1566 int vm_ver, vm_rev;
1567 int i, j, have_vm = 0;
1568 int X = vo_dwidth, Y = vo_dheight;
1569 int modeline_width, modeline_height;
1571 int modecount;
1573 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1575 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1576 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1577 vm_rev);
1578 have_vm = 1;
1579 } else {
1580 mp_msg(MSGT_VO, MSGL_WARN,
1581 "XF86VidMode extension not available.\n");
1584 if (have_vm)
1586 if (vidmodes == NULL)
1587 XF86VidModeGetAllModeLines(mDisplay, mScreen, &modecount,
1588 &vidmodes);
1589 j = 0;
1590 modeline_width = vidmodes[0]->hdisplay;
1591 modeline_height = vidmodes[0]->vdisplay;
1593 for (i = 1; i < modecount; i++)
1594 if ((vidmodes[i]->hdisplay >= X)
1595 && (vidmodes[i]->vdisplay >= Y))
1596 if ((vidmodes[i]->hdisplay <= modeline_width)
1597 && (vidmodes[i]->vdisplay <= modeline_height))
1599 modeline_width = vidmodes[i]->hdisplay;
1600 modeline_height = vidmodes[i]->vdisplay;
1601 j = i;
1604 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_SelectedVideoMode,
1605 modeline_width, modeline_height, X, Y);
1606 XF86VidModeLockModeSwitch(mDisplay, mScreen, 0);
1607 XF86VidModeSwitchToMode(mDisplay, mScreen, vidmodes[j]);
1608 XF86VidModeSwitchToMode(mDisplay, mScreen, vidmodes[j]);
1610 // FIXME: all this is more of a hack than proper solution
1611 X = (vo_screenwidth - modeline_width) / 2;
1612 Y = (vo_screenheight - modeline_height) / 2;
1613 XF86VidModeSetViewPort(mDisplay, mScreen, X, Y);
1614 vo_dx = X;
1615 vo_dy = Y;
1616 vo_dwidth = modeline_width;
1617 vo_dheight = modeline_height;
1618 aspect_save_screenres(modeline_width, modeline_height);
1622 void vo_vm_close(void)
1624 #ifdef CONFIG_GUI
1625 if (vidmodes != NULL && vo_window != None)
1626 #else
1627 if (vidmodes != NULL)
1628 #endif
1630 int i, modecount;
1632 free(vidmodes);
1633 vidmodes = NULL;
1634 XF86VidModeGetAllModeLines(mDisplay, mScreen, &modecount,
1635 &vidmodes);
1636 for (i = 0; i < modecount; i++)
1637 if ((vidmodes[i]->hdisplay == vo_screenwidth)
1638 && (vidmodes[i]->vdisplay == vo_screenheight))
1640 mp_msg(MSGT_VO, MSGL_INFO,
1641 "Returning to original mode %dx%d\n",
1642 vo_screenwidth, vo_screenheight);
1643 break;
1646 XF86VidModeSwitchToMode(mDisplay, mScreen, vidmodes[i]);
1647 XF86VidModeSwitchToMode(mDisplay, mScreen, vidmodes[i]);
1648 free(vidmodes);
1649 vidmodes = NULL;
1652 #endif
1654 #endif /* X11_FULLSCREEN */
1658 * Scan the available visuals on this Display/Screen. Try to find
1659 * the 'best' available TrueColor visual that has a decent color
1660 * depth (at least 15bit). If there are multiple visuals with depth
1661 * >= 15bit, we prefer visuals with a smaller color depth.
1663 int vo_find_depth_from_visuals(Display * dpy, int screen,
1664 Visual ** visual_return)
1666 XVisualInfo visual_tmpl;
1667 XVisualInfo *visuals;
1668 int nvisuals, i;
1669 int bestvisual = -1;
1670 int bestvisual_depth = -1;
1672 visual_tmpl.screen = screen;
1673 visual_tmpl.class = TrueColor;
1674 visuals = XGetVisualInfo(dpy,
1675 VisualScreenMask | VisualClassMask,
1676 &visual_tmpl, &nvisuals);
1677 if (visuals != NULL)
1679 for (i = 0; i < nvisuals; i++)
1681 mp_msg(MSGT_VO, MSGL_V,
1682 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1683 visuals[i].visualid, visuals[i].depth,
1684 visuals[i].red_mask, visuals[i].green_mask,
1685 visuals[i].blue_mask);
1687 * Save the visual index and its depth, if this is the first
1688 * truecolor visul, or a visual that is 'preferred' over the
1689 * previous 'best' visual.
1691 if (bestvisual_depth == -1
1692 || (visuals[i].depth >= 15
1693 && (visuals[i].depth < bestvisual_depth
1694 || bestvisual_depth < 15)))
1696 bestvisual = i;
1697 bestvisual_depth = visuals[i].depth;
1701 if (bestvisual != -1 && visual_return != NULL)
1702 *visual_return = visuals[bestvisual].visual;
1704 XFree(visuals);
1706 return bestvisual_depth;
1710 static Colormap cmap = None;
1711 static XColor cols[256];
1712 static int cm_size, red_mask, green_mask, blue_mask;
1715 Colormap vo_x11_create_colormap(XVisualInfo * vinfo)
1717 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1719 if (vinfo->class != DirectColor)
1720 return XCreateColormap(mDisplay, mRootWin, vinfo->visual,
1721 AllocNone);
1723 /* can this function get called twice or more? */
1724 if (cmap)
1725 return cmap;
1726 cm_size = vinfo->colormap_size;
1727 red_mask = vinfo->red_mask;
1728 green_mask = vinfo->green_mask;
1729 blue_mask = vinfo->blue_mask;
1730 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1731 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1732 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1733 rvu = 65536ull * ru / (red_mask + ru);
1734 gvu = 65536ull * gu / (green_mask + gu);
1735 bvu = 65536ull * bu / (blue_mask + bu);
1736 r = g = b = 0;
1737 rv = gv = bv = 0;
1738 m = DoRed | DoGreen | DoBlue;
1739 for (k = 0; k < cm_size; k++)
1741 int t;
1743 cols[k].pixel = r | g | b;
1744 cols[k].red = rv;
1745 cols[k].green = gv;
1746 cols[k].blue = bv;
1747 cols[k].flags = m;
1748 t = (r + ru) & red_mask;
1749 if (t < r)
1750 m &= ~DoRed;
1751 r = t;
1752 t = (g + gu) & green_mask;
1753 if (t < g)
1754 m &= ~DoGreen;
1755 g = t;
1756 t = (b + bu) & blue_mask;
1757 if (t < b)
1758 m &= ~DoBlue;
1759 b = t;
1760 rv += rvu;
1761 gv += gvu;
1762 bv += bvu;
1764 cmap = XCreateColormap(mDisplay, mRootWin, vinfo->visual, AllocAll);
1765 XStoreColors(mDisplay, cmap, cols, cm_size);
1766 return cmap;
1770 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1771 * hue and red/green/blue intensity, but we cannot do saturation.
1772 * Currently only gamma, brightness and contrast are implemented.
1773 * Is there sufficient interest for hue and/or red/green/blue intensity?
1775 /* these values have range [-100,100] and are initially 0 */
1776 static int vo_gamma = 0;
1777 static int vo_brightness = 0;
1778 static int vo_contrast = 0;
1780 static int transform_color(float val,
1781 float brightness, float contrast, float gamma) {
1782 float s = pow(val, gamma);
1783 s = (s - 0.5) * contrast + 0.5;
1784 s += brightness;
1785 if (s < 0)
1786 s = 0;
1787 if (s > 1)
1788 s = 1;
1789 return (unsigned short) (s * 65535);
1792 uint32_t vo_x11_set_equalizer(char *name, int value)
1794 float gamma, brightness, contrast;
1795 float rf, gf, bf;
1796 int k;
1799 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1800 * of TrueColor-ed window but be careful:
1801 * Unlike the colormaps, which are private for the X client
1802 * who created them and thus automatically destroyed on client
1803 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1804 * setting and _must_ be restored before the process exits.
1805 * Unforunately when the process crashes (or gets killed
1806 * for some reason) it is impossible to restore the setting,
1807 * and such behaviour could be rather annoying for the users.
1809 if (cmap == None)
1810 return VO_NOTAVAIL;
1812 if (!strcasecmp(name, "brightness"))
1813 vo_brightness = value;
1814 else if (!strcasecmp(name, "contrast"))
1815 vo_contrast = value;
1816 else if (!strcasecmp(name, "gamma"))
1817 vo_gamma = value;
1818 else
1819 return VO_NOTIMPL;
1821 brightness = 0.01 * vo_brightness;
1822 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1823 gamma = pow(2, -0.02 * vo_gamma);
1825 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1826 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
1827 green_mask;
1828 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
1830 /* now recalculate the colormap using the newly set value */
1831 for (k = 0; k < cm_size; k++)
1833 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
1834 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
1835 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
1838 XStoreColors(mDisplay, cmap, cols, cm_size);
1839 XFlush(mDisplay);
1840 return VO_TRUE;
1843 uint32_t vo_x11_get_equalizer(char *name, int *value)
1845 if (cmap == None)
1846 return VO_NOTAVAIL;
1847 if (!strcasecmp(name, "brightness"))
1848 *value = vo_brightness;
1849 else if (!strcasecmp(name, "contrast"))
1850 *value = vo_contrast;
1851 else if (!strcasecmp(name, "gamma"))
1852 *value = vo_gamma;
1853 else
1854 return VO_NOTIMPL;
1855 return VO_TRUE;
1858 void vo_calc_drwXY(uint32_t *drwX, uint32_t *drwY)
1860 *drwX = *drwY = 0;
1861 if (vo_fs) {
1862 aspect(&vo_dwidth, &vo_dheight, A_ZOOM);
1863 vo_dwidth = FFMIN(vo_dwidth, vo_screenwidth);
1864 vo_dheight = FFMIN(vo_dheight, vo_screenheight);
1865 *drwX = (vo_screenwidth - vo_dwidth) / 2;
1866 *drwY = (vo_screenheight - vo_dheight) / 2;
1867 mp_msg(MSGT_VO, MSGL_V, "[vo-fs] dx: %d dy: %d dw: %d dh: %d\n",
1868 *drwX, *drwY, vo_dwidth, vo_dheight);
1869 } else if (WinID == 0) {
1870 *drwX = vo_dx;
1871 *drwY = vo_dy;
1875 #ifdef CONFIG_XV
1876 int vo_xv_set_eq(uint32_t xv_port, char *name, int value)
1878 XvAttribute *attributes;
1879 int i, howmany, xv_atom;
1881 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
1883 /* get available attributes */
1884 attributes = XvQueryPortAttributes(mDisplay, xv_port, &howmany);
1885 for (i = 0; i < howmany && attributes; i++)
1886 if (attributes[i].flags & XvSettable)
1888 xv_atom = XInternAtom(mDisplay, attributes[i].name, True);
1889 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1890 then trigger it if it's ok so that the other values are at default upon query */
1891 if (xv_atom != None)
1893 int hue = 0, port_value, port_min, port_max;
1895 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1896 (!strcasecmp(name, "brightness")))
1897 port_value = value;
1898 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1899 (!strcasecmp(name, "contrast")))
1900 port_value = value;
1901 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1902 (!strcasecmp(name, "saturation")))
1903 port_value = value;
1904 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1905 (!strcasecmp(name, "hue")))
1907 port_value = value;
1908 hue = 1;
1909 } else
1910 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1911 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1912 (!strcasecmp(name, "red_intensity")))
1913 port_value = value;
1914 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1915 && (!strcasecmp(name, "green_intensity")))
1916 port_value = value;
1917 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1918 && (!strcasecmp(name, "blue_intensity")))
1919 port_value = value;
1920 else
1921 continue;
1923 port_min = attributes[i].min_value;
1924 port_max = attributes[i].max_value;
1926 /* nvidia hue workaround */
1927 if (hue && port_min == 0 && port_max == 360)
1929 port_value =
1930 (port_value >=
1931 0) ? (port_value - 100) : (port_value + 100);
1933 // -100 -> min
1934 // 0 -> (max+min)/2
1935 // +100 -> max
1936 port_value =
1937 (port_value + 100) * (port_max - port_min) / 200 +
1938 port_min;
1939 XvSetPortAttribute(mDisplay, xv_port, xv_atom, port_value);
1940 return VO_TRUE;
1943 return VO_FALSE;
1946 int vo_xv_get_eq(uint32_t xv_port, char *name, int *value)
1949 XvAttribute *attributes;
1950 int i, howmany, xv_atom;
1952 /* get available attributes */
1953 attributes = XvQueryPortAttributes(mDisplay, xv_port, &howmany);
1954 for (i = 0; i < howmany && attributes; i++)
1955 if (attributes[i].flags & XvGettable)
1957 xv_atom = XInternAtom(mDisplay, attributes[i].name, True);
1958 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1959 then trigger it if it's ok so that the other values are at default upon query */
1960 if (xv_atom != None)
1962 int val, port_value = 0, port_min, port_max;
1964 XvGetPortAttribute(mDisplay, xv_port, xv_atom,
1965 &port_value);
1967 port_min = attributes[i].min_value;
1968 port_max = attributes[i].max_value;
1969 val =
1970 (port_value - port_min) * 200 / (port_max - port_min) -
1971 100;
1973 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1974 (!strcasecmp(name, "brightness")))
1975 *value = val;
1976 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1977 (!strcasecmp(name, "contrast")))
1978 *value = val;
1979 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1980 (!strcasecmp(name, "saturation")))
1981 *value = val;
1982 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1983 (!strcasecmp(name, "hue")))
1985 /* nasty nvidia detect */
1986 if (port_min == 0 && port_max == 360)
1987 *value = (val >= 0) ? (val - 100) : (val + 100);
1988 else
1989 *value = val;
1990 } else
1991 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1992 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1993 (!strcasecmp(name, "red_intensity")))
1994 *value = val;
1995 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1996 && (!strcasecmp(name, "green_intensity")))
1997 *value = val;
1998 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1999 && (!strcasecmp(name, "blue_intensity")))
2000 *value = val;
2001 else
2002 continue;
2004 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2005 name, *value);
2006 return VO_TRUE;
2009 return VO_FALSE;
2012 /** \brief contains flags changing the execution of the colorkeying code */
2013 xv_ck_info_t xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR };
2014 unsigned long xv_colorkey; ///< The color used for manual colorkeying.
2015 unsigned int xv_port; ///< The selected Xv port.
2018 * \brief Interns the requested atom if it is available.
2020 * \param atom_name String containing the name of the requested atom.
2022 * \return Returns the atom if available, else None is returned.
2025 static Atom xv_intern_atom_if_exists( char const * atom_name )
2027 XvAttribute * attributes;
2028 int attrib_count,i;
2029 Atom xv_atom = None;
2031 attributes = XvQueryPortAttributes( mDisplay, xv_port, &attrib_count );
2032 if( attributes!=NULL )
2034 for ( i = 0; i < attrib_count; ++i )
2036 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2038 xv_atom = XInternAtom( mDisplay, atom_name, False );
2039 break; // found what we want, break out
2042 XFree( attributes );
2045 return xv_atom;
2049 * \brief Try to enable vsync for xv.
2050 * \return Returns -1 if not available, 0 on failure and 1 on success.
2052 int vo_xv_enable_vsync(void)
2054 Atom xv_atom = xv_intern_atom_if_exists("XV_SYNC_TO_VBLANK");
2055 if (xv_atom == None)
2056 return -1;
2057 return XvSetPortAttribute(mDisplay, xv_port, xv_atom, 1) == Success;
2061 * \brief Get maximum supported source image dimensions.
2063 * This function does not set the variables pointed to by
2064 * width and height if the information could not be retrieved,
2065 * so the caller is reponsible for properly initializing them.
2067 * \param width [out] The maximum width gets stored here.
2068 * \param height [out] The maximum height gets stored here.
2071 void vo_xv_get_max_img_dim( uint32_t * width, uint32_t * height )
2073 XvEncodingInfo * encodings;
2074 //unsigned long num_encodings, idx; to int or too long?!
2075 unsigned int num_encodings, idx;
2077 XvQueryEncodings( mDisplay, xv_port, &num_encodings, &encodings);
2079 if ( encodings )
2081 for ( idx = 0; idx < num_encodings; ++idx )
2083 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2085 *width = encodings[idx].width;
2086 *height = encodings[idx].height;
2087 break;
2092 mp_msg( MSGT_VO, MSGL_V,
2093 "[xv common] Maximum source image dimensions: %ux%u\n",
2094 *width, *height );
2096 XvFreeEncodingInfo( encodings );
2100 * \brief Print information about the colorkey method and source.
2102 * \param ck_handling Integer value containing the information about
2103 * colorkey handling (see x11_common.h).
2105 * Outputs the content of |ck_handling| as a readable message.
2108 void vo_xv_print_ck_info(void)
2110 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2112 switch ( xv_ck_info.method )
2114 case CK_METHOD_NONE:
2115 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2116 case CK_METHOD_AUTOPAINT:
2117 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2118 case CK_METHOD_MANUALFILL:
2119 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2120 case CK_METHOD_BACKGROUND:
2121 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2124 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2126 switch ( xv_ck_info.source )
2128 case CK_SRC_CUR:
2129 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2130 xv_colorkey );
2131 break;
2132 case CK_SRC_USE:
2133 if ( xv_ck_info.method == CK_METHOD_AUTOPAINT )
2135 mp_msg( MSGT_VO, MSGL_V,
2136 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2137 xv_colorkey );
2139 else
2141 mp_msg( MSGT_VO, MSGL_V,
2142 "Using colorkey from MPlayer (0x%06lx)."
2143 " Use -colorkey to change.\n",
2144 xv_colorkey );
2146 break;
2147 case CK_SRC_SET:
2148 mp_msg( MSGT_VO, MSGL_V,
2149 "Setting and using colorkey from MPlayer (0x%06lx)."
2150 " Use -colorkey to change.\n",
2151 xv_colorkey );
2152 break;
2156 * \brief Init colorkey depending on the settings in xv_ck_info.
2158 * \return Returns 0 on failure and 1 on success.
2160 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2161 * flags in xv_ck_info.
2163 * Possiblilities:
2164 * * Methods
2165 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2166 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2167 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2168 * * Sources
2169 * - use currently set colorkey ( CK_SRC_CUR )
2170 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2171 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2173 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2174 * we don't draw anything as this means it was forced to off.
2176 int vo_xv_init_colorkey(void)
2178 Atom xv_atom;
2179 int rez;
2181 /* check if colorkeying is needed */
2182 xv_atom = xv_intern_atom_if_exists( "XV_COLORKEY" );
2184 /* if we have to deal with colorkeying ... */
2185 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2187 /* check if we should use the colorkey specified in vo_colorkey */
2188 if ( xv_ck_info.source != CK_SRC_CUR )
2190 xv_colorkey = vo_colorkey;
2192 /* check if we have to set the colorkey too */
2193 if ( xv_ck_info.source == CK_SRC_SET )
2195 xv_atom = XInternAtom(mDisplay, "XV_COLORKEY",False);
2197 rez = XvSetPortAttribute( mDisplay, xv_port, xv_atom, vo_colorkey );
2198 if ( rez != Success )
2200 mp_msg( MSGT_VO, MSGL_FATAL,
2201 "[xv common] Couldn't set colorkey!\n" );
2202 return 0; // error setting colorkey
2206 else
2208 int colorkey_ret;
2210 rez=XvGetPortAttribute(mDisplay,xv_port, xv_atom, &colorkey_ret);
2211 if ( rez == Success )
2213 xv_colorkey = colorkey_ret;
2215 else
2217 mp_msg( MSGT_VO, MSGL_FATAL,
2218 "[xv common] Couldn't get colorkey!"
2219 "Maybe the selected Xv port has no overlay.\n" );
2220 return 0; // error getting colorkey
2224 xv_atom = xv_intern_atom_if_exists( "XV_AUTOPAINT_COLORKEY" );
2226 /* should we draw the colorkey ourselves or activate autopainting? */
2227 if ( xv_ck_info.method == CK_METHOD_AUTOPAINT )
2229 rez = !Success; // reset rez to something different than Success
2231 if ( xv_atom != None ) // autopaint is supported
2233 rez = XvSetPortAttribute( mDisplay, xv_port, xv_atom, 1 );
2236 if ( rez != Success )
2238 // fallback to manual colorkey drawing
2239 xv_ck_info.method = CK_METHOD_MANUALFILL;
2242 else // disable colorkey autopainting if supported
2244 if ( xv_atom != None ) // we have autopaint attribute
2246 XvSetPortAttribute( mDisplay, xv_port, xv_atom, 0 );
2250 else // do no colorkey drawing at all
2252 xv_ck_info.method = CK_METHOD_NONE;
2253 } /* end: should we draw colorkey */
2255 /* output information about the current colorkey settings */
2256 vo_xv_print_ck_info();
2258 return 1; // success
2262 * \brief Draw the colorkey on the video window.
2264 * Draws the colorkey depending on the set method ( colorkey_handling ).
2266 * Also draws the black bars ( when the video doesn't fit the display in
2267 * fullscreen ) separately, so they don't overlap with the video area.
2268 * It doesn't call XFlush.
2271 void vo_xv_draw_colorkey( int32_t x, int32_t y,
2272 int32_t w, int32_t h )
2274 if( xv_ck_info.method == CK_METHOD_MANUALFILL ||
2275 xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2277 XSetForeground( mDisplay, vo_gc, xv_colorkey );
2278 XFillRectangle( mDisplay, vo_window, vo_gc,
2279 x, y,
2280 w, h );
2283 /* draw black bars if needed */
2284 /* TODO! move this to vo_x11_clearwindow_part() */
2285 if ( vo_fs )
2287 XSetForeground( mDisplay, vo_gc, 0 );
2288 /* making non-overlap fills, requires 8 checks instead of 4 */
2289 if ( y > 0 )
2290 XFillRectangle( mDisplay, vo_window, vo_gc,
2291 0, 0,
2292 vo_screenwidth, y);
2293 if (x > 0)
2294 XFillRectangle( mDisplay, vo_window, vo_gc,
2295 0, 0,
2296 x, vo_screenheight);
2297 if (x + w < vo_screenwidth)
2298 XFillRectangle( mDisplay, vo_window, vo_gc,
2299 x + w, 0,
2300 vo_screenwidth, vo_screenheight);
2301 if (y + h < vo_screenheight)
2302 XFillRectangle( mDisplay, vo_window, vo_gc,
2303 0, y + h,
2304 vo_screenwidth, vo_screenheight);
2308 /** \brief Tests if a valid argument for the ck suboption was given. */
2309 int xv_test_ck( void * arg )
2311 strarg_t * strarg = (strarg_t *)arg;
2313 if ( strargcmp( strarg, "use" ) == 0 ||
2314 strargcmp( strarg, "set" ) == 0 ||
2315 strargcmp( strarg, "cur" ) == 0 )
2317 return 1;
2320 return 0;
2322 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2323 int xv_test_ckm( void * arg )
2325 strarg_t * strarg = (strarg_t *)arg;
2327 if ( strargcmp( strarg, "bg" ) == 0 ||
2328 strargcmp( strarg, "man" ) == 0 ||
2329 strargcmp( strarg, "auto" ) == 0 )
2331 return 1;
2334 return 0;
2338 * \brief Modify the colorkey_handling var according to str
2340 * Checks if a valid pointer ( not NULL ) to the string
2341 * was given. And in that case modifies the colorkey_handling
2342 * var to reflect the requested behaviour.
2343 * If nothing happens the content of colorkey_handling stays
2344 * the same.
2346 * \param str Pointer to the string or NULL
2349 void xv_setup_colorkeyhandling( char const * ck_method_str,
2350 char const * ck_str )
2352 /* check if a valid pointer to the string was passed */
2353 if ( ck_str )
2355 if ( strncmp( ck_str, "use", 3 ) == 0 )
2357 xv_ck_info.source = CK_SRC_USE;
2359 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2361 xv_ck_info.source = CK_SRC_SET;
2364 /* check if a valid pointer to the string was passed */
2365 if ( ck_method_str )
2367 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2369 xv_ck_info.method = CK_METHOD_BACKGROUND;
2371 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2373 xv_ck_info.method = CK_METHOD_MANUALFILL;
2375 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2377 xv_ck_info.method = CK_METHOD_AUTOPAINT;
2382 #endif