Merge svn changes up to r28176
[mplayer.git] / libvo / x11_common.c
blob9e6e31bd59c84bfd418394c92828f7dccdf49e02
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 "options.h"
10 #include "mp_msg.h"
11 #include "mp_fifo.h"
12 #include "x11_common.h"
13 #include "talloc.h"
15 #ifdef X11_FULLSCREEN
17 #include <string.h>
18 #include <unistd.h>
19 #include <assert.h>
21 #include "video_out.h"
22 #include "aspect.h"
23 #include "geometry.h"
24 #include "help_mp.h"
25 #include "osdep/timer.h"
27 #include <X11/Xmd.h>
28 #include <X11/Xlib.h>
29 #include <X11/Xutil.h>
30 #include <X11/Xatom.h>
32 #ifdef CONFIG_XSS
33 #include <X11/extensions/scrnsaver.h>
34 #endif
36 #ifdef CONFIG_XDPMS
37 #include <X11/extensions/dpms.h>
38 #endif
40 #ifdef CONFIG_XINERAMA
41 #include <X11/extensions/Xinerama.h>
42 #endif
44 #ifdef CONFIG_XF86VM
45 #include <X11/extensions/xf86vmode.h>
46 #endif
48 #ifdef CONFIG_XF86XK
49 #include <X11/XF86keysym.h>
50 #endif
52 #ifdef CONFIG_XV
53 #include <X11/extensions/Xv.h>
54 #include <X11/extensions/Xvlib.h>
56 #include "subopt-helper.h"
57 #endif
59 #include "input/input.h"
60 #include "input/mouse.h"
62 #ifdef CONFIG_GUI
63 #include "gui/interface.h"
64 #include "mplayer.h"
65 #endif
67 #define WIN_LAYER_ONBOTTOM 2
68 #define WIN_LAYER_NORMAL 4
69 #define WIN_LAYER_ONTOP 6
70 #define WIN_LAYER_ABOVE_DOCK 10
72 extern int enable_mouse_movements;
73 int fs_layer = WIN_LAYER_ABOVE_DOCK;
75 int stop_xscreensaver = 0;
77 static int dpms_disabled = 0;
79 char *mDisplayName = NULL;
81 char **vo_fstype_list;
83 /* 1 means that the WM is metacity (broken as hell) */
84 int metacity_hack = 0;
86 #ifdef CONFIG_XF86VM
87 XF86VidModeModeInfo **vidmodes = NULL;
88 XF86VidModeModeLine modeline;
89 #endif
91 static int vo_x11_get_fs_type(int supported);
92 static void saver_off(Display *);
93 static void saver_on(Display *);
96 * Sends the EWMH fullscreen state event.
98 * action: could be one of _NET_WM_STATE_REMOVE -- remove state
99 * _NET_WM_STATE_ADD -- add state
100 * _NET_WM_STATE_TOGGLE -- toggle
102 void vo_x11_ewmh_fullscreen(struct vo_x11_state *x11, int action)
104 assert(action == _NET_WM_STATE_REMOVE ||
105 action == _NET_WM_STATE_ADD || action == _NET_WM_STATE_TOGGLE);
107 if (x11->fs_type & vo_wm_FULLSCREEN)
109 XEvent xev;
111 /* init X event structure for _NET_WM_FULLSCREEN client message */
112 xev.xclient.type = ClientMessage;
113 xev.xclient.serial = 0;
114 xev.xclient.send_event = True;
115 xev.xclient.message_type = x11->XA_NET_WM_STATE;
116 xev.xclient.window = x11->window;
117 xev.xclient.format = 32;
118 xev.xclient.data.l[0] = action;
119 xev.xclient.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
120 xev.xclient.data.l[2] = 0;
121 xev.xclient.data.l[3] = 0;
122 xev.xclient.data.l[4] = 0;
124 /* finally send that damn thing */
125 if (!XSendEvent(x11->display, DefaultRootWindow(x11->display), False,
126 SubstructureRedirectMask | SubstructureNotifyMask,
127 &xev))
129 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_EwmhFullscreenStateFailed);
134 static void vo_hidecursor(Display * disp, Window win)
136 Cursor no_ptr;
137 Pixmap bm_no;
138 XColor black, dummy;
139 Colormap colormap;
140 const char bm_no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
142 if (WinID == 0)
143 return; // do not hide if playing on the root window
145 colormap = DefaultColormap(disp, DefaultScreen(disp));
146 if ( !XAllocNamedColor(disp, colormap, "black", &black, &dummy) )
148 return; // color alloc failed, give up
150 bm_no = XCreateBitmapFromData(disp, win, bm_no_data, 8, 8);
151 no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
152 XDefineCursor(disp, win, no_ptr);
153 XFreeCursor(disp, no_ptr);
154 if (bm_no != None)
155 XFreePixmap(disp, bm_no);
156 XFreeColors(disp,colormap,&black.pixel,1,0);
159 static void vo_showcursor(Display * disp, Window win)
161 if (WinID == 0)
162 return;
163 XDefineCursor(disp, win, 0);
166 static int x11_errorhandler(Display * display, XErrorEvent * event)
168 #define MSGLEN 60
169 char msg[MSGLEN];
171 XGetErrorText(display, event->error_code, (char *) &msg, MSGLEN);
173 mp_msg(MSGT_VO, MSGL_ERR, "X11 error: %s\n", msg);
175 mp_msg(MSGT_VO, MSGL_V,
176 "Type: %x, display: %p, resourceid: %lx, serial: %lx\n",
177 event->type, event->display, event->resourceid, event->serial);
178 mp_msg(MSGT_VO, MSGL_V,
179 "Error code: %x, request code: %x, minor code: %x\n",
180 event->error_code, event->request_code, event->minor_code);
182 // abort();
183 //exit_player("X11 error");
184 return 0;
185 #undef MSGLEN
188 void fstype_help(void)
190 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_AvailableFsType);
191 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FULL_SCREEN_TYPES\n");
193 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "none",
194 "don't set fullscreen window layer");
195 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer",
196 "use _WIN_LAYER hint with default layer");
197 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer=<0..15>",
198 "use _WIN_LAYER hint with a given layer number");
199 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "netwm",
200 "force NETWM style");
201 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "above",
202 "use _NETWM_STATE_ABOVE hint if available");
203 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "below",
204 "use _NETWM_STATE_BELOW hint if available");
205 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "fullscreen",
206 "use _NETWM_STATE_FULLSCREEN hint if availale");
207 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "stays_on_top",
208 "use _NETWM_STATE_STAYS_ON_TOP hint if available");
209 mp_msg(MSGT_VO, MSGL_INFO,
210 "You can also negate the settings with simply putting '-' in the beginning");
211 mp_msg(MSGT_VO, MSGL_INFO, "\n");
214 static void fstype_dump(int fstype)
216 if (fstype)
218 mp_msg(MSGT_VO, MSGL_V, "[x11] Current fstype setting honours");
219 if (fstype & vo_wm_LAYER)
220 mp_msg(MSGT_VO, MSGL_V, " LAYER");
221 if (fstype & vo_wm_FULLSCREEN)
222 mp_msg(MSGT_VO, MSGL_V, " FULLSCREEN");
223 if (fstype & vo_wm_STAYS_ON_TOP)
224 mp_msg(MSGT_VO, MSGL_V, " STAYS_ON_TOP");
225 if (fstype & vo_wm_ABOVE)
226 mp_msg(MSGT_VO, MSGL_V, " ABOVE");
227 if (fstype & vo_wm_BELOW)
228 mp_msg(MSGT_VO, MSGL_V, " BELOW");
229 mp_msg(MSGT_VO, MSGL_V, " X atoms\n");
230 } else
231 mp_msg(MSGT_VO, MSGL_V,
232 "[x11] Current fstype setting doesn't honour any X atoms\n");
235 static int net_wm_support_state_test(struct vo_x11_state *x11, Atom atom)
237 #define NET_WM_STATE_TEST(x) { if (atom == x11->XA_NET_WM_STATE_##x) { mp_msg( MSGT_VO,MSGL_V, "[x11] Detected wm supports " #x " state.\n" ); return vo_wm_##x; } }
239 NET_WM_STATE_TEST(FULLSCREEN);
240 NET_WM_STATE_TEST(ABOVE);
241 NET_WM_STATE_TEST(STAYS_ON_TOP);
242 NET_WM_STATE_TEST(BELOW);
243 return 0;
246 static int x11_get_property(struct vo_x11_state *x11, Atom type, Atom ** args,
247 unsigned long *nitems)
249 int format;
250 unsigned long bytesafter;
252 return Success ==
253 XGetWindowProperty(x11->display, x11->rootwin, type, 0, 16384, False,
254 AnyPropertyType, &type, &format, nitems,
255 &bytesafter, (unsigned char **) args)
256 && *nitems > 0;
259 static int vo_wm_detect(struct vo *vo)
261 struct vo_x11_state *x11 = vo->x11;
262 int i;
263 int wm = 0;
264 unsigned long nitems;
265 Atom *args = NULL;
267 if (WinID >= 0)
268 return 0;
270 // -- supports layers
271 if (x11_get_property(x11, x11->XA_WIN_PROTOCOLS, &args, &nitems))
273 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports layers.\n");
274 for (i = 0; i < nitems; i++)
276 if (args[i] == x11->XA_WIN_LAYER)
278 wm |= vo_wm_LAYER;
279 metacity_hack |= 1;
280 } else
281 /* metacity is the only window manager I know which reports
282 * supporting only the _WIN_LAYER hint in _WIN_PROTOCOLS.
283 * (what's more support for it is broken) */
284 metacity_hack |= 2;
286 XFree(args);
287 if (wm && (metacity_hack == 1))
289 // metacity claims to support layers, but it is not the truth :-)
290 wm ^= vo_wm_LAYER;
291 mp_msg(MSGT_VO, MSGL_V,
292 "[x11] Using workaround for Metacity bugs.\n");
295 // --- netwm
296 if (x11_get_property(x11, x11->XA_NET_SUPPORTED, &args, &nitems))
298 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports NetWM.\n");
299 for (i = 0; i < nitems; i++)
300 wm |= net_wm_support_state_test(vo->x11, args[i]);
301 XFree(args);
304 if (wm == 0)
305 mp_msg(MSGT_VO, MSGL_V, "[x11] Unknown wm type...\n");
306 return wm;
309 #define XA_INIT(x) x11->XA##x = XInternAtom(x11->display, #x, False)
310 static void init_atoms(struct vo_x11_state *x11)
312 XA_INIT(_NET_SUPPORTED);
313 XA_INIT(_NET_WM_STATE);
314 XA_INIT(_NET_WM_STATE_FULLSCREEN);
315 XA_INIT(_NET_WM_STATE_ABOVE);
316 XA_INIT(_NET_WM_STATE_STAYS_ON_TOP);
317 XA_INIT(_NET_WM_STATE_BELOW);
318 XA_INIT(_NET_WM_PID);
319 XA_INIT(_WIN_PROTOCOLS);
320 XA_INIT(_WIN_LAYER);
321 XA_INIT(_WIN_HINTS);
322 XA_INIT(WM_PROTOCOLS);
323 XA_INIT(WM_DELETE_WINDOW);
326 void update_xinerama_info(struct vo *vo) {
327 struct MPOpts *opts = vo->opts;
328 int screen = xinerama_screen;
329 xinerama_x = xinerama_y = 0;
330 #ifdef CONFIG_XINERAMA
331 if (screen >= -1 && XineramaIsActive(vo->x11->display))
333 XineramaScreenInfo *screens;
334 int num_screens;
336 screens = XineramaQueryScreens(vo->x11->display, &num_screens);
337 if (screen >= num_screens)
338 screen = num_screens - 1;
339 if (screen == -1) {
340 int x = vo->dx + vo->dwidth / 2;
341 int y = vo->dy + vo->dheight / 2;
342 for (screen = num_screens - 1; screen > 0; screen--) {
343 int left = screens[screen].x_org;
344 int right = left + screens[screen].width;
345 int top = screens[screen].y_org;
346 int bottom = top + screens[screen].height;
347 if (left <= x && x <= right && top <= y && y <= bottom)
348 break;
351 if (screen < 0)
352 screen = 0;
353 opts->vo_screenwidth = screens[screen].width;
354 opts->vo_screenheight = screens[screen].height;
355 xinerama_x = screens[screen].x_org;
356 xinerama_y = screens[screen].y_org;
358 XFree(screens);
360 #endif
361 aspect_save_screenres(vo, opts->vo_screenwidth, opts->vo_screenheight);
364 int vo_init(struct vo *vo)
366 struct MPOpts *opts = vo->opts;
367 struct vo_x11_state *x11 = vo->x11;
368 // int mScreen;
369 int depth, bpp;
370 unsigned int mask;
372 // char * DisplayName = ":0.0";
373 // Display * mDisplay;
374 XImage *mXImage = NULL;
376 // Window mRootWin;
377 XWindowAttributes attribs;
378 char *dispName;
380 if (vo_rootwin)
381 WinID = 0; // use root window
383 if (x11->depthonscreen)
385 saver_off(x11->display);
386 return 1; // already called
389 XSetErrorHandler(x11_errorhandler);
391 #if 0
392 if (!mDisplayName)
393 if (!(mDisplayName = getenv("DISPLAY")))
394 mDisplayName = strdup(":0.0");
395 #else
396 dispName = XDisplayName(mDisplayName);
397 #endif
399 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
401 x11->display = XOpenDisplay(dispName);
402 if (!x11->display)
404 mp_msg(MSGT_VO, MSGL_ERR,
405 "vo: couldn't open the X11 display (%s)!\n", dispName);
406 return 0;
408 x11->screen = DefaultScreen(x11->display); // screen ID
409 x11->rootwin = RootWindow(x11->display, x11->screen); // root window ID
411 init_atoms(vo->x11);
413 #ifdef CONFIG_XF86VM
415 int clock;
417 XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline);
418 if (!opts->vo_screenwidth)
419 opts->vo_screenwidth = modeline.hdisplay;
420 if (!opts->vo_screenheight)
421 opts->vo_screenheight = modeline.vdisplay;
423 #endif
425 if (!opts->vo_screenwidth)
426 opts->vo_screenwidth = DisplayWidth(x11->display, x11->screen);
427 if (!opts->vo_screenheight)
428 opts->vo_screenheight = DisplayHeight(x11->display, x11->screen);
430 // get color depth (from root window, or the best visual):
431 XGetWindowAttributes(x11->display, x11->rootwin, &attribs);
432 depth = attribs.depth;
434 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
436 Visual *visual;
438 depth = vo_find_depth_from_visuals(x11->display, x11->screen, &visual);
439 if (depth != -1)
440 mXImage = XCreateImage(x11->display, visual, depth, ZPixmap,
441 0, NULL, 1, 1, 8, 1);
442 } else
443 mXImage =
444 XGetImage(x11->display, x11->rootwin, 0, 0, 1, 1, AllPlanes, ZPixmap);
446 x11->depthonscreen = depth; // display depth on screen
448 // get bits/pixel from XImage structure:
449 if (mXImage == NULL)
451 mask = 0;
452 } else
455 * for the depth==24 case, the XImage structures might use
456 * 24 or 32 bits of data per pixel. The x11->depthonscreen
457 * field stores the amount of data per pixel in the
458 * XImage structure!
460 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
462 bpp = mXImage->bits_per_pixel;
463 if ((x11->depthonscreen + 7) / 8 != (bpp + 7) / 8)
464 x11->depthonscreen = bpp; // by A'rpi
465 mask =
466 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
467 mp_msg(MSGT_VO, MSGL_V,
468 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
469 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
470 XDestroyImage(mXImage);
472 if (((x11->depthonscreen + 7) / 8) == 2)
474 if (mask == 0x7FFF)
475 x11->depthonscreen = 15;
476 else if (mask == 0xFFFF)
477 x11->depthonscreen = 16;
479 // XCloseDisplay( mDisplay );
480 /* slightly improved local display detection AST */
481 if (strncmp(dispName, "unix:", 5) == 0)
482 dispName += 4;
483 else if (strncmp(dispName, "localhost:", 10) == 0)
484 dispName += 9;
485 if (*dispName == ':' && atoi(dispName + 1) < 10)
486 x11->display_is_local = 1;
487 else
488 x11->display_is_local = 0;
489 mp_msg(MSGT_VO, MSGL_V,
490 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
491 opts->vo_screenwidth, opts->vo_screenheight, depth, x11->depthonscreen,
492 dispName, x11->display_is_local ? "local" : "remote");
494 x11->wm_type = vo_wm_detect(vo);
496 x11->fs_type = vo_x11_get_fs_type(x11->wm_type);
498 fstype_dump(x11->fs_type);
500 saver_off(x11->display);
501 return 1;
504 void vo_uninit(struct vo_x11_state *x11)
506 if (!x11->display)
508 mp_msg(MSGT_VO, MSGL_V,
509 "vo: x11 uninit called but X11 not initialized..\n");
510 return;
512 // if( !vo_depthonscreen ) return;
513 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
514 XSetErrorHandler(NULL);
515 XCloseDisplay(x11->display);
516 x11->depthonscreen = 0;
517 x11->display = NULL;
518 talloc_free(x11);
521 #include "osdep/keycodes.h"
522 #include "wskeys.h"
524 #ifdef XF86XK_AudioPause
525 static const struct keymap keysym_map[] = {
526 {XF86XK_MenuKB, KEY_MENU},
527 {XF86XK_AudioPlay, KEY_PLAY}, {XF86XK_AudioPause, KEY_PAUSE}, {XF86XK_AudioStop, KEY_STOP},
528 {XF86XK_AudioPrev, KEY_PREV}, {XF86XK_AudioNext, KEY_NEXT},
529 {XF86XK_AudioMute, KEY_MUTE}, {XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN}, {XF86XK_AudioRaiseVolume, KEY_VOLUME_UP},
530 {0, 0}
533 static void vo_x11_putkey_ext(struct vo *vo, int keysym)
535 struct mp_fifo *f = vo->key_fifo;
536 int mpkey = lookup_keymap_table(keysym_map, keysym);
537 if (mpkey)
538 mplayer_put_key(f, mpkey);
540 #endif
542 static const struct keymap keymap[] = {
543 // special keys
544 {wsEscape, KEY_ESC}, {wsBackSpace, KEY_BS}, {wsTab, KEY_TAB}, {wsEnter, KEY_ENTER},
546 // cursor keys
547 {wsLeft, KEY_LEFT}, {wsRight, KEY_RIGHT}, {wsUp, KEY_UP}, {wsDown, KEY_DOWN},
549 // navigation block
550 {wsInsert, KEY_INSERT}, {wsDelete, KEY_DELETE}, {wsHome, KEY_HOME}, {wsEnd, KEY_END},
551 {wsPageUp, KEY_PAGE_UP}, {wsPageDown, KEY_PAGE_DOWN},
553 // F-keys
554 {wsF1, KEY_F+1}, {wsF2, KEY_F+2}, {wsF3, KEY_F+3}, {wsF4, KEY_F+4},
555 {wsF5, KEY_F+5}, {wsF6, KEY_F+6}, {wsF7, KEY_F+7}, {wsF8, KEY_F+8},
556 {wsF9, KEY_F+9}, {wsF10, KEY_F+10}, {wsF11, KEY_F+11}, {wsF12, KEY_F+12},
558 // numpad independent of numlock
559 {wsGrayMinus, '-'}, {wsGrayPlus, '+'}, {wsGrayMul, '*'}, {wsGrayDiv, '/'},
560 {wsGrayEnter, KEY_KPENTER},
562 // numpad with numlock
563 {wsGray0, KEY_KP0}, {wsGray1, KEY_KP1}, {wsGray2, KEY_KP2},
564 {wsGray3, KEY_KP3}, {wsGray4, KEY_KP4}, {wsGray5, KEY_KP5},
565 {wsGray6, KEY_KP6}, {wsGray7, KEY_KP7}, {wsGray8, KEY_KP8},
566 {wsGray9, KEY_KP9}, {wsGrayDecimal, KEY_KPDEC},
568 // numpad without numlock
569 {wsGrayInsert, KEY_KPINS}, {wsGrayEnd, KEY_KP1}, {wsGrayDown, KEY_KP2},
570 {wsGrayPgDn, KEY_KP3}, {wsGrayLeft, KEY_KP4}, {wsGray5Dup, KEY_KP5},
571 {wsGrayRight, KEY_KP6}, {wsGrayHome, KEY_KP7}, {wsGrayUp, KEY_KP8},
572 {wsGrayPgUp, KEY_KP9}, {wsGrayDelete, KEY_KPDEL},
574 {0, 0}
577 void vo_x11_putkey(struct vo *vo, int key)
579 static const char *passthrough_keys = " -+*/<>`~!@#$%^&()_{}:;\"\',.?\\|=[]";
580 int mpkey = 0;
581 int i;
582 if ((key >= 'a' && key <= 'z') ||
583 (key >= 'A' && key <= 'Z') ||
584 (key >= '0' && key <= '9') ||
585 (key > 0 && key < 256 && strchr(passthrough_keys, key)))
586 mpkey = key;
588 if (!mpkey)
589 mpkey = lookup_keymap_table(keymap, key);
591 if (mpkey)
592 mplayer_put_key(vo->key_fifo, mpkey);
596 // ----- Motif header: -------
598 #define MWM_HINTS_FUNCTIONS (1L << 0)
599 #define MWM_HINTS_DECORATIONS (1L << 1)
600 #define MWM_HINTS_INPUT_MODE (1L << 2)
601 #define MWM_HINTS_STATUS (1L << 3)
603 #define MWM_FUNC_ALL (1L << 0)
604 #define MWM_FUNC_RESIZE (1L << 1)
605 #define MWM_FUNC_MOVE (1L << 2)
606 #define MWM_FUNC_MINIMIZE (1L << 3)
607 #define MWM_FUNC_MAXIMIZE (1L << 4)
608 #define MWM_FUNC_CLOSE (1L << 5)
610 #define MWM_DECOR_ALL (1L << 0)
611 #define MWM_DECOR_BORDER (1L << 1)
612 #define MWM_DECOR_RESIZEH (1L << 2)
613 #define MWM_DECOR_TITLE (1L << 3)
614 #define MWM_DECOR_MENU (1L << 4)
615 #define MWM_DECOR_MINIMIZE (1L << 5)
616 #define MWM_DECOR_MAXIMIZE (1L << 6)
618 #define MWM_INPUT_MODELESS 0
619 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
620 #define MWM_INPUT_SYSTEM_MODAL 2
621 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
622 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
624 #define MWM_TEAROFF_WINDOW (1L<<0)
626 typedef struct
628 long flags;
629 long functions;
630 long decorations;
631 long input_mode;
632 long state;
633 } MotifWmHints;
635 static MotifWmHints vo_MotifWmHints;
636 static Atom vo_MotifHints = None;
638 void vo_x11_decoration(struct vo *vo, int d)
640 struct vo_x11_state *x11 = vo->x11;
641 Atom mtype;
642 int mformat;
643 unsigned long mn, mb;
645 if (!WinID)
646 return;
648 if (vo_fsmode & 8)
650 XSetTransientForHint(x11->display, x11->window,
651 RootWindow(x11->display, x11->screen));
654 vo_MotifHints = XInternAtom(x11->display, "_MOTIF_WM_HINTS", 0);
655 if (vo_MotifHints != None)
657 if (!d)
659 MotifWmHints *mhints = NULL;
661 XGetWindowProperty(x11->display, x11->window,
662 vo_MotifHints, 0, 20, False,
663 vo_MotifHints, &mtype, &mformat, &mn,
664 &mb, (unsigned char **) &mhints);
665 if (mhints)
667 if (mhints->flags & MWM_HINTS_DECORATIONS)
668 x11->olddecor = mhints->decorations;
669 if (mhints->flags & MWM_HINTS_FUNCTIONS)
670 x11->oldfuncs = mhints->functions;
671 XFree(mhints);
675 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
676 vo_MotifWmHints.flags =
677 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
678 if (d)
680 vo_MotifWmHints.functions = x11->oldfuncs;
681 d = x11->olddecor;
683 #if 0
684 vo_MotifWmHints.decorations =
685 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
686 #else
687 vo_MotifWmHints.decorations =
688 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
689 #endif
690 XChangeProperty(x11->display, x11->window, vo_MotifHints,
691 vo_MotifHints, 32,
692 PropModeReplace,
693 (unsigned char *) &vo_MotifWmHints,
694 (vo_fsmode & 4) ? 4 : 5);
698 void vo_x11_classhint(struct vo *vo, Window window, char *name)
700 struct vo_x11_state *x11 = vo->x11;
701 XClassHint wmClass;
702 pid_t pid = getpid();
704 wmClass.res_name = name;
705 wmClass.res_class = "MPlayer";
706 XSetClassHint(x11->display, window, &wmClass);
707 XChangeProperty(x11->display, window, x11->XA_NET_WM_PID, XA_CARDINAL,
708 32, PropModeReplace, (unsigned char *) &pid, 1);
711 void vo_x11_uninit(struct vo *vo)
713 struct vo_x11_state *x11 = vo->x11;
714 saver_on(x11->display);
715 if (x11->window != None)
716 vo_showcursor(x11->display, x11->window);
718 if (x11->f_gc)
720 XFreeGC(vo->x11->display, x11->f_gc);
721 x11->f_gc = NULL;
723 #ifdef CONFIG_GUI
724 /* destroy window only if it's not controlled by the GUI */
725 if (!use_gui)
726 #endif
728 if (x11->vo_gc)
730 XSetBackground(vo->x11->display, x11->vo_gc, 0);
731 XFreeGC(vo->x11->display, x11->vo_gc);
732 x11->vo_gc = NULL;
734 if (x11->window != None)
736 XClearWindow(x11->display, x11->window);
737 if (WinID < 0)
739 XEvent xev;
741 XUnmapWindow(x11->display, x11->window);
742 XDestroyWindow(x11->display, x11->window);
745 XNextEvent(x11->display, &xev);
747 while (xev.type != DestroyNotify
748 || xev.xdestroywindow.event != x11->window);
750 x11->window = None;
752 vo_fs = 0;
753 x11->vo_old_width = x11->vo_old_height = 0;
757 int vo_x11_check_events(struct vo *vo)
759 struct vo_x11_state *x11 = vo->x11;
760 struct MPOpts *opts = vo->opts;
761 Display *display = vo->x11->display;
762 int ret = 0;
763 XEvent Event;
764 char buf[100];
765 KeySym keySym;
767 // unsigned long vo_KeyTable[512];
769 if ((x11->vo_mouse_autohide) && x11->mouse_waiting_hide &&
770 (GetTimerMS() - x11->mouse_timer >= 1000)) {
771 vo_hidecursor(display, x11->window);
772 x11->mouse_waiting_hide = 0;
775 while (XPending(display))
777 XNextEvent(display, &Event);
778 #ifdef CONFIG_GUI
779 if (use_gui)
781 guiGetEvent(0, (char *) &Event);
782 if (x11->window != Event.xany.window)
783 continue;
785 #endif
786 // printf("\rEvent.type=%X \n",Event.type);
787 switch (Event.type)
789 case Expose:
790 ret |= VO_EVENT_EXPOSE;
791 break;
792 case ConfigureNotify:
793 // if (!vo_fs && (Event.xconfigure.width == opts->vo_screenwidth || Event.xconfigure.height == opts->vo_screenheight)) break;
794 // if (vo_fs && Event.xconfigure.width != opts->vo_screenwidth && Event.xconfigure.height != opts->vo_screenheight) break;
795 if (x11->window == None)
796 break;
797 vo_x11_update_geometry(vo);
798 ret |= VO_EVENT_RESIZE;
799 break;
800 case KeyPress:
802 int key;
804 #ifdef CONFIG_GUI
805 if ( use_gui ) { break; }
806 #endif
808 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
809 &x11->compose_status);
810 #ifdef XF86XK_AudioPause
811 vo_x11_putkey_ext(vo, keySym);
812 #endif
813 key =
814 ((keySym & 0xff00) !=
815 0 ? ((keySym & 0x00ff) + 256) : (keySym));
816 vo_x11_putkey(vo, key);
817 ret |= VO_EVENT_KEYPRESS;
819 break;
820 case MotionNotify:
821 if(enable_mouse_movements)
823 char cmd_str[40];
824 sprintf(cmd_str,"set_mouse_pos %i %i",Event.xmotion.x, Event.xmotion.y);
825 mp_input_queue_cmd(vo->input_ctx,
826 mp_input_parse_cmd(cmd_str));
829 if (x11->vo_mouse_autohide)
831 vo_showcursor(display, x11->window);
832 x11->mouse_waiting_hide = 1;
833 x11->mouse_timer = GetTimerMS();
835 break;
836 case ButtonPress:
837 if (x11->vo_mouse_autohide)
839 vo_showcursor(display, x11->window);
840 x11->mouse_waiting_hide = 1;
841 x11->mouse_timer = GetTimerMS();
843 #ifdef CONFIG_GUI
844 // Ignore mouse button 1-3 under GUI.
845 if (use_gui && (Event.xbutton.button >= 1)
846 && (Event.xbutton.button <= 3))
847 break;
848 #endif
849 mplayer_put_key(vo->key_fifo,
850 (MOUSE_BTN0 + Event.xbutton.button - 1)
851 | MP_KEY_DOWN);
852 break;
853 case ButtonRelease:
854 if (x11->vo_mouse_autohide)
856 vo_showcursor(display, x11->window);
857 x11->mouse_waiting_hide = 1;
858 x11->mouse_timer = GetTimerMS();
860 #ifdef CONFIG_GUI
861 // Ignore mouse button 1-3 under GUI.
862 if (use_gui && (Event.xbutton.button >= 1)
863 && (Event.xbutton.button <= 3))
864 break;
865 #endif
866 mplayer_put_key(vo->key_fifo,
867 MOUSE_BTN0 + Event.xbutton.button - 1);
868 break;
869 case PropertyNotify:
871 char *name =
872 XGetAtomName(display, Event.xproperty.atom);
874 if (!name)
875 break;
877 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
879 XFree(name);
881 break;
882 case MapNotify:
883 x11->vo_hint.win_gravity = x11->old_gravity;
884 XSetWMNormalHints(display, x11->window, &x11->vo_hint);
885 x11->fs_flip = 0;
886 break;
887 case ClientMessage:
888 if (Event.xclient.message_type == x11->XAWM_PROTOCOLS &&
889 Event.xclient.data.l[0] == x11->XAWM_DELETE_WINDOW)
890 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
891 break;
894 return ret;
898 * \brief sets the size and position of the non-fullscreen window.
900 static void vo_x11_nofs_sizepos(struct vo *vo, int x, int y,
901 int width, int height)
903 struct vo_x11_state *x11 = vo->x11;
904 vo_x11_sizehint(vo, x, y, width, height, 0);
905 if (vo_fs) {
906 x11->vo_old_x = x;
907 x11->vo_old_y = y;
908 x11->vo_old_width = width;
909 x11->vo_old_height = height;
911 else
913 vo->dwidth = width;
914 vo->dheight = height;
915 XMoveResizeWindow(vo->x11->display, vo->x11->window, x, y, width, height);
919 void vo_x11_sizehint(struct vo *vo, int x, int y, int width, int height, int max)
921 struct vo_x11_state *x11 = vo->x11;
922 x11->vo_hint.flags = 0;
923 if (vo_keepaspect)
925 x11->vo_hint.flags |= PAspect;
926 x11->vo_hint.min_aspect.x = width;
927 x11->vo_hint.min_aspect.y = height;
928 x11->vo_hint.max_aspect.x = width;
929 x11->vo_hint.max_aspect.y = height;
932 x11->vo_hint.flags |= PPosition | PSize;
933 x11->vo_hint.x = x;
934 x11->vo_hint.y = y;
935 x11->vo_hint.width = width;
936 x11->vo_hint.height = height;
937 if (max)
939 x11->vo_hint.flags |= PMaxSize;
940 x11->vo_hint.max_width = width;
941 x11->vo_hint.max_height = height;
942 } else
944 x11->vo_hint.max_width = 0;
945 x11->vo_hint.max_height = 0;
948 // Set minimum height/width to 4 to avoid off-by-one errors
949 // and because mga_vid requires a minimal size of 4 pixels.
950 x11->vo_hint.flags |= PMinSize;
951 x11->vo_hint.min_width = x11->vo_hint.min_height = 4;
953 // Set the base size. A window manager might display the window
954 // size to the user relative to this.
955 // Setting these to width/height might be nice, but e.g. fluxbox can't handle it.
956 x11->vo_hint.flags |= PBaseSize;
957 x11->vo_hint.base_width = 0 /*width*/;
958 x11->vo_hint.base_height = 0 /*height*/;
960 x11->vo_hint.flags |= PWinGravity;
961 x11->vo_hint.win_gravity = StaticGravity;
962 XSetWMNormalHints(x11->display, x11->window, &x11->vo_hint);
965 static int vo_x11_get_gnome_layer(struct vo_x11_state *x11, Window win)
967 Atom type;
968 int format;
969 unsigned long nitems;
970 unsigned long bytesafter;
971 unsigned short *args = NULL;
973 if (XGetWindowProperty(x11->display, win, x11->XA_WIN_LAYER, 0, 16384,
974 False, AnyPropertyType, &type, &format, &nitems,
975 &bytesafter,
976 (unsigned char **) &args) == Success
977 && nitems > 0 && args)
979 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
980 *args);
981 return *args;
983 return WIN_LAYER_NORMAL;
987 static Window vo_x11_create_smooth_window(struct vo_x11_state *x11, Window mRoot,
988 Visual * vis, int x, int y,
989 unsigned int width, unsigned int height,
990 int depth, Colormap col_map)
992 unsigned long xswamask = CWBorderPixel;
993 XSetWindowAttributes xswa;
994 Window ret_win;
996 if (col_map != CopyFromParent)
998 xswa.colormap = col_map;
999 xswamask |= CWColormap;
1001 xswa.background_pixel = 0;
1002 xswa.border_pixel = 0;
1003 xswa.backing_store = NotUseful;
1004 xswa.bit_gravity = StaticGravity;
1006 ret_win =
1007 XCreateWindow(x11->display, x11->rootwin, x, y, width, height, 0, depth,
1008 CopyFromParent, vis, xswamask, &xswa);
1009 XSetWMProtocols(x11->display, ret_win, &x11->XAWM_DELETE_WINDOW, 1);
1010 if (!x11->f_gc)
1011 x11->f_gc = XCreateGC(x11->display, ret_win, 0, 0);
1012 XSetForeground(x11->display, x11->f_gc, 0);
1014 return ret_win;
1018 * \brief create and setup a window suitable for display
1019 * \param vis Visual to use for creating the window
1020 * \param x x position of window
1021 * \param y y position of window
1022 * \param width width of window
1023 * \param height height of window
1024 * \param flags flags for window creation.
1025 * Only VOFLAG_FULLSCREEN is supported so far.
1026 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1027 * \param classname name to use for the classhint
1028 * \param title title for the window
1030 * This also does the grunt-work like setting Window Manager hints etc.
1031 * If vo_window is already set it just moves and resizes it.
1033 void vo_x11_create_vo_window(struct vo *vo, XVisualInfo *vis, int x, int y,
1034 unsigned int width, unsigned int height, int flags,
1035 Colormap col_map,
1036 const char *classname, const char *title)
1038 struct MPOpts *opts = vo->opts;
1039 struct vo_x11_state *x11 = vo->x11;
1040 Display *mDisplay = vo->x11->display;
1041 XGCValues xgcv;
1042 if (WinID >= 0) {
1043 x11->window = WinID ? (Window)WinID : x11->rootwin;
1044 if (col_map != CopyFromParent) {
1045 unsigned long xswamask = CWColormap;
1046 XSetWindowAttributes xswa;
1047 xswa.colormap = col_map;
1048 XUnmapWindow(mDisplay, x11->window);
1049 XChangeWindowAttributes(mDisplay, x11->window, xswamask, &xswa);
1050 XMapWindow(mDisplay, x11->window);
1052 if (WinID) vo_x11_update_geometry(vo);
1053 vo_x11_selectinput_witherr(mDisplay, x11->window,
1054 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1055 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1056 goto final;
1058 if (x11->window == None) {
1059 XSizeHints hint;
1060 XEvent xev;
1061 vo_fs = 0;
1062 vo->dwidth = width;
1063 vo->dheight = height;
1064 x11->window = vo_x11_create_smooth_window(x11, x11->rootwin, vis->visual,
1065 x, y, width, height, vis->depth, col_map);
1066 vo_x11_classhint(vo, x11->window, classname);
1067 XStoreName(mDisplay, x11->window, title);
1068 vo_hidecursor(mDisplay, x11->window);
1069 XSelectInput(mDisplay, x11->window, StructureNotifyMask);
1070 hint.x = x; hint.y = y;
1071 hint.width = width; hint.height = height;
1072 hint.flags = PPosition | PSize;
1073 XSetStandardProperties(mDisplay, x11->window, title, title, None, NULL, 0, &hint);
1074 vo_x11_sizehint(vo, x, y, width, height, 0);
1075 if (!vo_border) vo_x11_decoration(vo, 0);
1076 // map window
1077 XMapWindow(mDisplay, x11->window);
1078 XClearWindow(mDisplay, x11->window);
1079 // wait for map
1080 do {
1081 XNextEvent(mDisplay, &xev);
1082 } while (xev.type != MapNotify || xev.xmap.event != x11->window);
1083 XSelectInput(mDisplay, x11->window, NoEventMask);
1084 XSync(mDisplay, False);
1085 vo_x11_selectinput_witherr(mDisplay, x11->window,
1086 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1087 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1089 if (opts->vo_ontop) vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1090 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, width, height);
1091 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1092 vo_x11_fullscreen(vo);
1093 final:
1094 if (x11->vo_gc != None)
1095 XFreeGC(mDisplay, x11->vo_gc);
1096 x11->vo_gc = XCreateGC(mDisplay, x11->window, GCForeground, &xgcv);
1097 XSync(mDisplay, False);
1098 x11->vo_mouse_autohide = 1;
1101 void vo_x11_clearwindow_part(struct vo *vo, Window vo_window,
1102 int img_width, int img_height, int use_fs)
1104 struct vo_x11_state *x11 = vo->x11;
1105 struct MPOpts *opts = vo->opts;
1106 Display *mDisplay = vo->x11->display;
1107 int u_dheight, u_dwidth, left_ov, left_ov2;
1109 if (!x11->f_gc)
1110 return;
1112 u_dheight = use_fs ? opts->vo_screenheight : vo->dheight;
1113 u_dwidth = use_fs ? opts->vo_screenwidth : vo->dwidth;
1114 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1115 return;
1117 left_ov = (u_dheight - img_height) / 2;
1118 left_ov2 = (u_dwidth - img_width) / 2;
1120 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, 0, u_dwidth, left_ov);
1121 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, u_dheight - left_ov - 1,
1122 u_dwidth, left_ov + 1);
1124 if (u_dwidth > img_width)
1126 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, left_ov, left_ov2,
1127 img_height);
1128 XFillRectangle(mDisplay, vo_window, x11->f_gc, u_dwidth - left_ov2 - 1,
1129 left_ov, left_ov2 + 1, img_height);
1132 XFlush(mDisplay);
1135 void vo_x11_clearwindow(struct vo *vo, Window vo_window)
1137 struct vo_x11_state *x11 = vo->x11;
1138 struct MPOpts *opts = vo->opts;
1139 if (!x11->f_gc)
1140 return;
1141 XFillRectangle(x11->display, vo_window, x11->f_gc, 0, 0,
1142 opts->vo_screenwidth, opts->vo_screenheight);
1144 XFlush(x11->display);
1148 void vo_x11_setlayer(struct vo *vo, Window vo_window, int layer)
1150 struct vo_x11_state *x11 = vo->x11;
1151 if (WinID >= 0)
1152 return;
1154 if (x11->fs_type & vo_wm_LAYER)
1156 XClientMessageEvent xev;
1158 if (!x11->orig_layer)
1159 x11->orig_layer = vo_x11_get_gnome_layer(x11, vo_window);
1161 memset(&xev, 0, sizeof(xev));
1162 xev.type = ClientMessage;
1163 xev.display = x11->display;
1164 xev.window = vo_window;
1165 xev.message_type = x11->XA_WIN_LAYER;
1166 xev.format = 32;
1167 xev.data.l[0] = layer ? fs_layer : x11->orig_layer; // if not fullscreen, stay on default layer
1168 xev.data.l[1] = CurrentTime;
1169 mp_msg(MSGT_VO, MSGL_V,
1170 "[x11] Layered style stay on top (layer %ld).\n",
1171 xev.data.l[0]);
1172 XSendEvent(x11->display, x11->rootwin, False, SubstructureNotifyMask,
1173 (XEvent *) & xev);
1174 } else if (x11->fs_type & vo_wm_NETWM)
1176 XClientMessageEvent xev;
1177 char *state;
1179 memset(&xev, 0, sizeof(xev));
1180 xev.type = ClientMessage;
1181 xev.message_type = x11->XA_NET_WM_STATE;
1182 xev.display = x11->display;
1183 xev.window = vo_window;
1184 xev.format = 32;
1185 xev.data.l[0] = layer;
1187 if (x11->fs_type & vo_wm_STAYS_ON_TOP)
1188 xev.data.l[1] = x11->XA_NET_WM_STATE_STAYS_ON_TOP;
1189 else if (x11->fs_type & vo_wm_ABOVE)
1190 xev.data.l[1] = x11->XA_NET_WM_STATE_ABOVE;
1191 else if (x11->fs_type & vo_wm_FULLSCREEN)
1192 xev.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
1193 else if (x11->fs_type & vo_wm_BELOW)
1194 // This is not fallback. We can safely assume that the situation
1195 // where only NETWM_STATE_BELOW is supported doesn't exist.
1196 xev.data.l[1] = x11->XA_NET_WM_STATE_BELOW;
1198 XSendEvent(x11->display, x11->rootwin, False, SubstructureRedirectMask,
1199 (XEvent *) & xev);
1200 state = XGetAtomName(x11->display, xev.data.l[1]);
1201 mp_msg(MSGT_VO, MSGL_V,
1202 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1203 layer, state);
1204 XFree(state);
1208 static int vo_x11_get_fs_type(int supported)
1210 int i;
1211 int type = supported;
1213 if (vo_fstype_list)
1215 i = 0;
1216 for (i = 0; vo_fstype_list[i]; i++)
1218 int neg = 0;
1219 char *arg = vo_fstype_list[i];
1221 if (vo_fstype_list[i][0] == '-')
1223 neg = 1;
1224 arg = vo_fstype_list[i] + 1;
1227 if (!strncmp(arg, "layer", 5))
1229 if (!neg && (arg[5] == '='))
1231 char *endptr = NULL;
1232 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1234 if (endptr && *endptr == '\0' && layer >= 0
1235 && layer <= 15)
1236 fs_layer = layer;
1238 if (neg)
1239 type &= ~vo_wm_LAYER;
1240 else
1241 type |= vo_wm_LAYER;
1242 } else if (!strcmp(arg, "above"))
1244 if (neg)
1245 type &= ~vo_wm_ABOVE;
1246 else
1247 type |= vo_wm_ABOVE;
1248 } else if (!strcmp(arg, "fullscreen"))
1250 if (neg)
1251 type &= ~vo_wm_FULLSCREEN;
1252 else
1253 type |= vo_wm_FULLSCREEN;
1254 } else if (!strcmp(arg, "stays_on_top"))
1256 if (neg)
1257 type &= ~vo_wm_STAYS_ON_TOP;
1258 else
1259 type |= vo_wm_STAYS_ON_TOP;
1260 } else if (!strcmp(arg, "below"))
1262 if (neg)
1263 type &= ~vo_wm_BELOW;
1264 else
1265 type |= vo_wm_BELOW;
1266 } else if (!strcmp(arg, "netwm"))
1268 if (neg)
1269 type &= ~vo_wm_NETWM;
1270 else
1271 type |= vo_wm_NETWM;
1272 } else if (!strcmp(arg, "none"))
1273 return 0;
1277 return type;
1281 * \brief update vo->dx, vo->dy, vo->dwidth and vo->dheight with current values of vo->x11->window
1282 * \return returns current color depth of vo->x11->window
1284 int vo_x11_update_geometry(struct vo *vo)
1286 struct vo_x11_state *x11 = vo->x11;
1287 unsigned depth, w, h;
1288 int dummy_int;
1289 Window dummy_win;
1290 XGetGeometry(x11->display, x11->window, &dummy_win, &dummy_int, &dummy_int,
1291 &w, &h, &dummy_int, &depth);
1292 if (w <= INT_MAX && h <= INT_MAX) {
1293 vo->dwidth = w;
1294 vo->dheight = h;
1296 XTranslateCoordinates(x11->display, x11->window, x11->rootwin, 0, 0,
1297 &vo->dx, &vo->dy, &dummy_win);
1298 return depth <= INT_MAX ? depth : 0;
1301 void vo_x11_fullscreen(struct vo *vo)
1303 struct MPOpts *opts = vo->opts;
1304 struct vo_x11_state *x11 = vo->x11;
1305 int x, y, w, h;
1307 if (WinID >= 0 || x11->fs_flip)
1308 return;
1310 if (vo_fs)
1312 // fs->win
1313 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1315 x = x11->vo_old_x;
1316 y = x11->vo_old_y;
1317 w = x11->vo_old_width;
1318 h = x11->vo_old_height;
1321 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1322 vo_fs = VO_FALSE;
1323 } else
1325 // win->fs
1326 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1328 vo_fs = VO_TRUE;
1329 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1331 x11->vo_old_x = vo->dx;
1332 x11->vo_old_y = vo->dy;
1333 x11->vo_old_width = vo->dwidth;
1334 x11->vo_old_height = vo->dheight;
1336 update_xinerama_info(vo);
1337 x = xinerama_x;
1338 y = xinerama_y;
1339 w = opts->vo_screenwidth;
1340 h = opts->vo_screenheight;
1343 long dummy;
1345 XGetWMNormalHints(x11->display, x11->window, &x11->vo_hint, &dummy);
1346 if (!(x11->vo_hint.flags & PWinGravity))
1347 x11->old_gravity = NorthWestGravity;
1348 else
1349 x11->old_gravity = x11->vo_hint.win_gravity;
1351 if (x11->wm_type == 0 && !(vo_fsmode & 16))
1353 XUnmapWindow(x11->display, x11->window); // required for MWM
1354 XWithdrawWindow(x11->display, x11->window, x11->screen);
1355 x11->fs_flip = 1;
1358 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1360 vo_x11_decoration(vo, vo_border && !vo_fs);
1361 vo_x11_sizehint(vo, x, y, w, h, 0);
1362 vo_x11_setlayer(vo, x11->window, vo_fs);
1365 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1367 /* some WMs lose ontop after fullscreen */
1368 if ((!(vo_fs)) & opts->vo_ontop)
1369 vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1371 XMapRaised(x11->display, x11->window);
1372 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1373 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1374 XRaiseWindow(x11->display, x11->window);
1375 XFlush(x11->display);
1378 void vo_x11_ontop(struct vo *vo)
1380 struct MPOpts *opts = vo->opts;
1381 opts->vo_ontop = !opts->vo_ontop;
1383 vo_x11_setlayer(vo, vo->x11->window, opts->vo_ontop);
1386 void vo_x11_border(struct vo *vo)
1388 vo_border = !vo_border;
1389 vo_x11_decoration(vo, vo_border && !vo_fs);
1393 * XScreensaver stuff
1396 static int screensaver_off;
1397 static unsigned int time_last;
1399 void xscreensaver_heartbeat(struct vo_x11_state *x11)
1401 unsigned int time = GetTimerMS();
1403 if (x11->display && screensaver_off && (time - time_last) > 30000)
1405 time_last = time;
1407 XResetScreenSaver(x11->display);
1411 static int xss_suspend(Display *mDisplay, Bool suspend)
1413 #ifndef CONFIG_XSS
1414 return 0;
1415 #else
1416 int event, error, major, minor;
1417 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1418 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1419 return 0;
1420 if (major < 1 || (major == 1 && minor < 1))
1421 return 0;
1422 XScreenSaverSuspend(mDisplay, suspend);
1423 return 1;
1424 #endif
1428 * End of XScreensaver stuff
1431 static void saver_on(Display * mDisplay)
1434 if (!screensaver_off)
1435 return;
1436 screensaver_off = 0;
1437 if (xss_suspend(mDisplay, False))
1438 return;
1439 #ifdef CONFIG_XDPMS
1440 if (dpms_disabled)
1442 int nothing;
1443 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1445 if (!DPMSEnable(mDisplay))
1446 { // restoring power saving settings
1447 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1448 } else
1450 // DPMS does not seem to be enabled unless we call DPMSInfo
1451 BOOL onoff;
1452 CARD16 state;
1454 DPMSForceLevel(mDisplay, DPMSModeOn);
1455 DPMSInfo(mDisplay, &state, &onoff);
1456 if (onoff)
1458 mp_msg(MSGT_VO, MSGL_V,
1459 "Successfully enabled DPMS\n");
1460 } else
1462 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1466 dpms_disabled = 0;
1468 #endif
1471 static void saver_off(Display * mDisplay)
1473 int nothing;
1475 if (screensaver_off)
1476 return;
1477 screensaver_off = 1;
1478 if (xss_suspend(mDisplay, True))
1479 return;
1480 #ifdef CONFIG_XDPMS
1481 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1483 BOOL onoff;
1484 CARD16 state;
1486 DPMSInfo(mDisplay, &state, &onoff);
1487 if (onoff)
1489 Status stat;
1491 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1492 dpms_disabled = 1;
1493 stat = DPMSDisable(mDisplay); // monitor powersave off
1494 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1497 #endif
1500 static XErrorHandler old_handler = NULL;
1501 static int selectinput_err = 0;
1502 static int x11_selectinput_errorhandler(Display * display,
1503 XErrorEvent * event)
1505 if (event->error_code == BadAccess)
1507 selectinput_err = 1;
1508 mp_msg(MSGT_VO, MSGL_ERR,
1509 "X11 error: BadAccess during XSelectInput Call\n");
1510 mp_msg(MSGT_VO, MSGL_ERR,
1511 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1512 /* If you think MPlayer should shutdown with this error,
1513 * comment out the following line */
1514 return 0;
1516 if (old_handler != NULL)
1517 old_handler(display, event);
1518 else
1519 x11_errorhandler(display, event);
1520 return 0;
1523 void vo_x11_selectinput_witherr(Display * display, Window w,
1524 long event_mask)
1526 XSync(display, False);
1527 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1528 selectinput_err = 0;
1529 if (vo_nomouse_input)
1531 XSelectInput(display, w,
1532 event_mask &
1533 (~(ButtonPressMask | ButtonReleaseMask)));
1534 } else
1536 XSelectInput(display, w, event_mask);
1538 XSync(display, False);
1539 XSetErrorHandler(old_handler);
1540 if (selectinput_err)
1542 mp_msg(MSGT_VO, MSGL_ERR,
1543 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1544 XSelectInput(display, w,
1545 event_mask &
1547 (ButtonPressMask | ButtonReleaseMask |
1548 PointerMotionMask)));
1552 #ifdef CONFIG_XF86VM
1553 void vo_vm_switch(struct vo *vo)
1555 struct vo_x11_state *x11 = vo->x11;
1556 struct MPOpts *opts = vo->opts;
1557 Display *mDisplay = x11->display;
1558 int vm_event, vm_error;
1559 int vm_ver, vm_rev;
1560 int i, j, have_vm = 0;
1561 int X = vo->dwidth, Y = vo->dheight;
1562 int modeline_width, modeline_height;
1564 int modecount;
1566 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1568 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1569 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1570 vm_rev);
1571 have_vm = 1;
1572 } else {
1573 mp_msg(MSGT_VO, MSGL_WARN,
1574 "XF86VidMode extension not available.\n");
1577 if (have_vm)
1579 if (vidmodes == NULL)
1580 XF86VidModeGetAllModeLines(mDisplay, x11->screen, &modecount,
1581 &vidmodes);
1582 j = 0;
1583 modeline_width = vidmodes[0]->hdisplay;
1584 modeline_height = vidmodes[0]->vdisplay;
1586 for (i = 1; i < modecount; i++)
1587 if ((vidmodes[i]->hdisplay >= X)
1588 && (vidmodes[i]->vdisplay >= Y))
1589 if ((vidmodes[i]->hdisplay <= modeline_width)
1590 && (vidmodes[i]->vdisplay <= modeline_height))
1592 modeline_width = vidmodes[i]->hdisplay;
1593 modeline_height = vidmodes[i]->vdisplay;
1594 j = i;
1597 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_SelectedVideoMode,
1598 modeline_width, modeline_height, X, Y);
1599 XF86VidModeLockModeSwitch(mDisplay, x11->screen, 0);
1600 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1601 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1603 // FIXME: all this is more of a hack than proper solution
1604 X = (opts->vo_screenwidth - modeline_width) / 2;
1605 Y = (opts->vo_screenheight - modeline_height) / 2;
1606 XF86VidModeSetViewPort(mDisplay, x11->screen, X, Y);
1607 vo->dx = X;
1608 vo->dy = Y;
1609 vo->dwidth = modeline_width;
1610 vo->dheight = modeline_height;
1611 aspect_save_screenres(vo, modeline_width, modeline_height);
1615 void vo_vm_close(struct vo *vo)
1617 Display *dpy = vo->x11->display;
1618 struct MPOpts *opts = vo->opts;
1619 #ifdef CONFIG_GUI
1620 if (vidmodes != NULL && vo->x11->vo_window != None)
1621 #else
1622 if (vidmodes != NULL)
1623 #endif
1625 int i, modecount;
1627 free(vidmodes);
1628 vidmodes = NULL;
1629 XF86VidModeGetAllModeLines(dpy, vo->x11->screen, &modecount,
1630 &vidmodes);
1631 for (i = 0; i < modecount; i++)
1632 if ((vidmodes[i]->hdisplay == opts->vo_screenwidth)
1633 && (vidmodes[i]->vdisplay == opts->vo_screenheight))
1635 mp_msg(MSGT_VO, MSGL_INFO,
1636 "Returning to original mode %dx%d\n",
1637 opts->vo_screenwidth, opts->vo_screenheight);
1638 break;
1641 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1642 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1643 free(vidmodes);
1644 vidmodes = NULL;
1647 #endif
1649 #endif /* X11_FULLSCREEN */
1653 * Scan the available visuals on this Display/Screen. Try to find
1654 * the 'best' available TrueColor visual that has a decent color
1655 * depth (at least 15bit). If there are multiple visuals with depth
1656 * >= 15bit, we prefer visuals with a smaller color depth.
1658 int vo_find_depth_from_visuals(Display * dpy, int screen,
1659 Visual ** visual_return)
1661 XVisualInfo visual_tmpl;
1662 XVisualInfo *visuals;
1663 int nvisuals, i;
1664 int bestvisual = -1;
1665 int bestvisual_depth = -1;
1667 visual_tmpl.screen = screen;
1668 visual_tmpl.class = TrueColor;
1669 visuals = XGetVisualInfo(dpy,
1670 VisualScreenMask | VisualClassMask,
1671 &visual_tmpl, &nvisuals);
1672 if (visuals != NULL)
1674 for (i = 0; i < nvisuals; i++)
1676 mp_msg(MSGT_VO, MSGL_V,
1677 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1678 visuals[i].visualid, visuals[i].depth,
1679 visuals[i].red_mask, visuals[i].green_mask,
1680 visuals[i].blue_mask);
1682 * Save the visual index and its depth, if this is the first
1683 * truecolor visul, or a visual that is 'preferred' over the
1684 * previous 'best' visual.
1686 if (bestvisual_depth == -1
1687 || (visuals[i].depth >= 15
1688 && (visuals[i].depth < bestvisual_depth
1689 || bestvisual_depth < 15)))
1691 bestvisual = i;
1692 bestvisual_depth = visuals[i].depth;
1696 if (bestvisual != -1 && visual_return != NULL)
1697 *visual_return = visuals[bestvisual].visual;
1699 XFree(visuals);
1701 return bestvisual_depth;
1705 static Colormap cmap = None;
1706 static XColor cols[256];
1707 static int cm_size, red_mask, green_mask, blue_mask;
1710 Colormap vo_x11_create_colormap(struct vo *vo, XVisualInfo *vinfo)
1712 struct vo_x11_state *x11 = vo->x11;
1713 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1715 if (vinfo->class != DirectColor)
1716 return XCreateColormap(x11->display, x11->rootwin, vinfo->visual,
1717 AllocNone);
1719 /* can this function get called twice or more? */
1720 if (cmap)
1721 return cmap;
1722 cm_size = vinfo->colormap_size;
1723 red_mask = vinfo->red_mask;
1724 green_mask = vinfo->green_mask;
1725 blue_mask = vinfo->blue_mask;
1726 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1727 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1728 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1729 rvu = 65536ull * ru / (red_mask + ru);
1730 gvu = 65536ull * gu / (green_mask + gu);
1731 bvu = 65536ull * bu / (blue_mask + bu);
1732 r = g = b = 0;
1733 rv = gv = bv = 0;
1734 m = DoRed | DoGreen | DoBlue;
1735 for (k = 0; k < cm_size; k++)
1737 int t;
1739 cols[k].pixel = r | g | b;
1740 cols[k].red = rv;
1741 cols[k].green = gv;
1742 cols[k].blue = bv;
1743 cols[k].flags = m;
1744 t = (r + ru) & red_mask;
1745 if (t < r)
1746 m &= ~DoRed;
1747 r = t;
1748 t = (g + gu) & green_mask;
1749 if (t < g)
1750 m &= ~DoGreen;
1751 g = t;
1752 t = (b + bu) & blue_mask;
1753 if (t < b)
1754 m &= ~DoBlue;
1755 b = t;
1756 rv += rvu;
1757 gv += gvu;
1758 bv += bvu;
1760 cmap = XCreateColormap(x11->display, x11->rootwin, vinfo->visual, AllocAll);
1761 XStoreColors(x11->display, cmap, cols, cm_size);
1762 return cmap;
1766 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1767 * hue and red/green/blue intensity, but we cannot do saturation.
1768 * Currently only gamma, brightness and contrast are implemented.
1769 * Is there sufficient interest for hue and/or red/green/blue intensity?
1771 /* these values have range [-100,100] and are initially 0 */
1772 static int vo_gamma = 0;
1773 static int vo_brightness = 0;
1774 static int vo_contrast = 0;
1776 static int transform_color(float val,
1777 float brightness, float contrast, float gamma) {
1778 float s = pow(val, gamma);
1779 s = (s - 0.5) * contrast + 0.5;
1780 s += brightness;
1781 if (s < 0)
1782 s = 0;
1783 if (s > 1)
1784 s = 1;
1785 return (unsigned short) (s * 65535);
1788 uint32_t vo_x11_set_equalizer(struct vo *vo, char *name, int value)
1790 float gamma, brightness, contrast;
1791 float rf, gf, bf;
1792 int k;
1795 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1796 * of TrueColor-ed window but be careful:
1797 * Unlike the colormaps, which are private for the X client
1798 * who created them and thus automatically destroyed on client
1799 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1800 * setting and _must_ be restored before the process exits.
1801 * Unforunately when the process crashes (or gets killed
1802 * for some reason) it is impossible to restore the setting,
1803 * and such behaviour could be rather annoying for the users.
1805 if (cmap == None)
1806 return VO_NOTAVAIL;
1808 if (!strcasecmp(name, "brightness"))
1809 vo_brightness = value;
1810 else if (!strcasecmp(name, "contrast"))
1811 vo_contrast = value;
1812 else if (!strcasecmp(name, "gamma"))
1813 vo_gamma = value;
1814 else
1815 return VO_NOTIMPL;
1817 brightness = 0.01 * vo_brightness;
1818 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1819 gamma = pow(2, -0.02 * vo_gamma);
1821 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1822 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
1823 green_mask;
1824 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
1826 /* now recalculate the colormap using the newly set value */
1827 for (k = 0; k < cm_size; k++)
1829 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
1830 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
1831 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
1834 XStoreColors(vo->x11->display, cmap, cols, cm_size);
1835 XFlush(vo->x11->display);
1836 return VO_TRUE;
1839 uint32_t vo_x11_get_equalizer(char *name, int *value)
1841 if (cmap == None)
1842 return VO_NOTAVAIL;
1843 if (!strcasecmp(name, "brightness"))
1844 *value = vo_brightness;
1845 else if (!strcasecmp(name, "contrast"))
1846 *value = vo_contrast;
1847 else if (!strcasecmp(name, "gamma"))
1848 *value = vo_gamma;
1849 else
1850 return VO_NOTIMPL;
1851 return VO_TRUE;
1854 #ifdef CONFIG_XV
1855 int vo_xv_set_eq(struct vo *vo, uint32_t xv_port, char *name, int value)
1857 XvAttribute *attributes;
1858 int i, howmany, xv_atom;
1860 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
1862 /* get available attributes */
1863 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1864 for (i = 0; i < howmany && attributes; i++)
1865 if (attributes[i].flags & XvSettable)
1867 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1868 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1869 then trigger it if it's ok so that the other values are at default upon query */
1870 if (xv_atom != None)
1872 int hue = 0, port_value, port_min, port_max;
1874 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1875 (!strcasecmp(name, "brightness")))
1876 port_value = value;
1877 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1878 (!strcasecmp(name, "contrast")))
1879 port_value = value;
1880 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1881 (!strcasecmp(name, "saturation")))
1882 port_value = value;
1883 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1884 (!strcasecmp(name, "hue")))
1886 port_value = value;
1887 hue = 1;
1888 } else
1889 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1890 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1891 (!strcasecmp(name, "red_intensity")))
1892 port_value = value;
1893 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1894 && (!strcasecmp(name, "green_intensity")))
1895 port_value = value;
1896 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1897 && (!strcasecmp(name, "blue_intensity")))
1898 port_value = value;
1899 else
1900 continue;
1902 port_min = attributes[i].min_value;
1903 port_max = attributes[i].max_value;
1905 /* nvidia hue workaround */
1906 if (hue && port_min == 0 && port_max == 360)
1908 port_value =
1909 (port_value >=
1910 0) ? (port_value - 100) : (port_value + 100);
1912 // -100 -> min
1913 // 0 -> (max+min)/2
1914 // +100 -> max
1915 port_value =
1916 (port_value + 100) * (port_max - port_min) / 200 +
1917 port_min;
1918 XvSetPortAttribute(vo->x11->display, xv_port, xv_atom, port_value);
1919 return VO_TRUE;
1922 return VO_FALSE;
1925 int vo_xv_get_eq(struct vo *vo, uint32_t xv_port, char *name, int *value)
1928 XvAttribute *attributes;
1929 int i, howmany, xv_atom;
1931 /* get available attributes */
1932 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1933 for (i = 0; i < howmany && attributes; i++)
1934 if (attributes[i].flags & XvGettable)
1936 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1937 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1938 then trigger it if it's ok so that the other values are at default upon query */
1939 if (xv_atom != None)
1941 int val, port_value = 0, port_min, port_max;
1943 XvGetPortAttribute(vo->x11->display, xv_port, xv_atom,
1944 &port_value);
1946 port_min = attributes[i].min_value;
1947 port_max = attributes[i].max_value;
1948 val =
1949 (port_value - port_min) * 200 / (port_max - port_min) -
1950 100;
1952 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1953 (!strcasecmp(name, "brightness")))
1954 *value = val;
1955 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1956 (!strcasecmp(name, "contrast")))
1957 *value = val;
1958 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1959 (!strcasecmp(name, "saturation")))
1960 *value = val;
1961 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1962 (!strcasecmp(name, "hue")))
1964 /* nasty nvidia detect */
1965 if (port_min == 0 && port_max == 360)
1966 *value = (val >= 0) ? (val - 100) : (val + 100);
1967 else
1968 *value = val;
1969 } else
1970 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1971 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1972 (!strcasecmp(name, "red_intensity")))
1973 *value = val;
1974 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1975 && (!strcasecmp(name, "green_intensity")))
1976 *value = val;
1977 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1978 && (!strcasecmp(name, "blue_intensity")))
1979 *value = val;
1980 else
1981 continue;
1983 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
1984 name, *value);
1985 return VO_TRUE;
1988 return VO_FALSE;
1992 * \brief Interns the requested atom if it is available.
1994 * \param atom_name String containing the name of the requested atom.
1996 * \return Returns the atom if available, else None is returned.
1999 static Atom xv_intern_atom_if_exists(struct vo_x11_state *x11,
2000 char const *atom_name)
2002 XvAttribute * attributes;
2003 int attrib_count,i;
2004 Atom xv_atom = None;
2006 attributes = XvQueryPortAttributes(x11->display, x11->xv_port, &attrib_count );
2007 if( attributes!=NULL )
2009 for ( i = 0; i < attrib_count; ++i )
2011 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2013 xv_atom = XInternAtom(x11->display, atom_name, False );
2014 break; // found what we want, break out
2017 XFree( attributes );
2020 return xv_atom;
2024 * \brief Try to enable vsync for xv.
2025 * \return Returns -1 if not available, 0 on failure and 1 on success.
2027 int vo_xv_enable_vsync(struct vo *vo)
2029 struct vo_x11_state *x11 = vo->x11;
2030 Atom xv_atom = xv_intern_atom_if_exists(x11, "XV_SYNC_TO_VBLANK");
2031 if (xv_atom == None)
2032 return -1;
2033 return XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1) == Success;
2037 * \brief Get maximum supported source image dimensions.
2039 * This function does not set the variables pointed to by
2040 * width and height if the information could not be retrieved,
2041 * so the caller is reponsible for properly initializing them.
2043 * \param width [out] The maximum width gets stored here.
2044 * \param height [out] The maximum height gets stored here.
2047 void vo_xv_get_max_img_dim(struct vo *vo, uint32_t * width, uint32_t * height)
2049 struct vo_x11_state *x11 = vo->x11;
2050 XvEncodingInfo * encodings;
2051 //unsigned long num_encodings, idx; to int or too long?!
2052 unsigned int num_encodings, idx;
2054 XvQueryEncodings(x11->display, x11->xv_port, &num_encodings, &encodings);
2056 if ( encodings )
2058 for ( idx = 0; idx < num_encodings; ++idx )
2060 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2062 *width = encodings[idx].width;
2063 *height = encodings[idx].height;
2064 break;
2069 mp_msg( MSGT_VO, MSGL_V,
2070 "[xv common] Maximum source image dimensions: %ux%u\n",
2071 *width, *height );
2073 XvFreeEncodingInfo( encodings );
2077 * \brief Print information about the colorkey method and source.
2079 * \param ck_handling Integer value containing the information about
2080 * colorkey handling (see x11_common.h).
2082 * Outputs the content of |ck_handling| as a readable message.
2085 static void vo_xv_print_ck_info(struct vo_x11_state *x11)
2087 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2089 switch ( x11->xv_ck_info.method )
2091 case CK_METHOD_NONE:
2092 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2093 case CK_METHOD_AUTOPAINT:
2094 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2095 case CK_METHOD_MANUALFILL:
2096 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2097 case CK_METHOD_BACKGROUND:
2098 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2101 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2103 switch ( x11->xv_ck_info.source )
2105 case CK_SRC_CUR:
2106 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2107 x11->xv_colorkey );
2108 break;
2109 case CK_SRC_USE:
2110 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2112 mp_msg( MSGT_VO, MSGL_V,
2113 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2114 x11->xv_colorkey );
2116 else
2118 mp_msg( MSGT_VO, MSGL_V,
2119 "Using colorkey from MPlayer (0x%06lx)."
2120 " Use -colorkey to change.\n",
2121 x11->xv_colorkey );
2123 break;
2124 case CK_SRC_SET:
2125 mp_msg( MSGT_VO, MSGL_V,
2126 "Setting and using colorkey from MPlayer (0x%06lx)."
2127 " Use -colorkey to change.\n",
2128 x11->xv_colorkey );
2129 break;
2133 * \brief Init colorkey depending on the settings in xv_ck_info.
2135 * \return Returns 0 on failure and 1 on success.
2137 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2138 * flags in xv_ck_info.
2140 * Possiblilities:
2141 * * Methods
2142 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2143 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2144 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2145 * * Sources
2146 * - use currently set colorkey ( CK_SRC_CUR )
2147 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2148 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2150 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2151 * we don't draw anything as this means it was forced to off.
2153 int vo_xv_init_colorkey(struct vo *vo)
2155 struct vo_x11_state *x11 = vo->x11;
2156 Atom xv_atom;
2157 int rez;
2159 /* check if colorkeying is needed */
2160 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_COLORKEY");
2162 /* if we have to deal with colorkeying ... */
2163 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2165 /* check if we should use the colorkey specified in vo_colorkey */
2166 if ( x11->xv_ck_info.source != CK_SRC_CUR )
2168 x11->xv_colorkey = vo_colorkey;
2170 /* check if we have to set the colorkey too */
2171 if ( x11->xv_ck_info.source == CK_SRC_SET )
2173 xv_atom = XInternAtom(x11->display, "XV_COLORKEY",False);
2175 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, vo_colorkey);
2176 if ( rez != Success )
2178 mp_msg( MSGT_VO, MSGL_FATAL,
2179 "[xv common] Couldn't set colorkey!\n" );
2180 return 0; // error setting colorkey
2184 else
2186 int colorkey_ret;
2188 rez=XvGetPortAttribute(x11->display,x11->xv_port, xv_atom, &colorkey_ret);
2189 if ( rez == Success )
2191 x11->xv_colorkey = colorkey_ret;
2193 else
2195 mp_msg( MSGT_VO, MSGL_FATAL,
2196 "[xv common] Couldn't get colorkey!"
2197 "Maybe the selected Xv port has no overlay.\n" );
2198 return 0; // error getting colorkey
2202 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_AUTOPAINT_COLORKEY");
2204 /* should we draw the colorkey ourselves or activate autopainting? */
2205 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2207 rez = !Success; // reset rez to something different than Success
2209 if ( xv_atom != None ) // autopaint is supported
2211 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1);
2214 if ( rez != Success )
2216 // fallback to manual colorkey drawing
2217 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2220 else // disable colorkey autopainting if supported
2222 if ( xv_atom != None ) // we have autopaint attribute
2224 XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 0);
2228 else // do no colorkey drawing at all
2230 x11->xv_ck_info.method = CK_METHOD_NONE;
2231 } /* end: should we draw colorkey */
2233 /* output information about the current colorkey settings */
2234 vo_xv_print_ck_info(x11);
2236 return 1; // success
2240 * \brief Draw the colorkey on the video window.
2242 * Draws the colorkey depending on the set method ( colorkey_handling ).
2244 * Also draws the black bars ( when the video doesn't fit the display in
2245 * fullscreen ) separately, so they don't overlap with the video area.
2246 * It doesn't call XFlush.
2249 void vo_xv_draw_colorkey(struct vo *vo, int32_t x, int32_t y,
2250 int32_t w, int32_t h)
2252 struct MPOpts *opts = vo->opts;
2253 struct vo_x11_state *x11 = vo->x11;
2254 if( x11->xv_ck_info.method == CK_METHOD_MANUALFILL ||
2255 x11->xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2257 XSetForeground(x11->display, x11->vo_gc, x11->xv_colorkey );
2258 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2259 x, y,
2260 w, h );
2263 /* draw black bars if needed */
2264 /* TODO! move this to vo_x11_clearwindow_part() */
2265 if ( vo_fs )
2267 XSetForeground(x11->display, x11->vo_gc, 0 );
2268 /* making non-overlap fills, requires 8 checks instead of 4 */
2269 if ( y > 0 )
2270 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2271 0, 0,
2272 opts->vo_screenwidth, y);
2273 if (x > 0)
2274 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2275 0, 0,
2276 x, opts->vo_screenheight);
2277 if (x + w < opts->vo_screenwidth)
2278 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2279 x + w, 0,
2280 opts->vo_screenwidth, opts->vo_screenheight);
2281 if (y + h < opts->vo_screenheight)
2282 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2283 0, y + h,
2284 opts->vo_screenwidth, opts->vo_screenheight);
2288 /** \brief Tests if a valid argument for the ck suboption was given. */
2289 int xv_test_ck( void * arg )
2291 strarg_t * strarg = (strarg_t *)arg;
2293 if ( strargcmp( strarg, "use" ) == 0 ||
2294 strargcmp( strarg, "set" ) == 0 ||
2295 strargcmp( strarg, "cur" ) == 0 )
2297 return 1;
2300 return 0;
2302 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2303 int xv_test_ckm( void * arg )
2305 strarg_t * strarg = (strarg_t *)arg;
2307 if ( strargcmp( strarg, "bg" ) == 0 ||
2308 strargcmp( strarg, "man" ) == 0 ||
2309 strargcmp( strarg, "auto" ) == 0 )
2311 return 1;
2314 return 0;
2318 * \brief Modify the colorkey_handling var according to str
2320 * Checks if a valid pointer ( not NULL ) to the string
2321 * was given. And in that case modifies the colorkey_handling
2322 * var to reflect the requested behaviour.
2323 * If nothing happens the content of colorkey_handling stays
2324 * the same.
2326 * \param str Pointer to the string or NULL
2329 void xv_setup_colorkeyhandling(struct vo *vo, const char *ck_method_str,
2330 const char *ck_str)
2332 struct vo_x11_state *x11 = vo->x11;
2333 /* check if a valid pointer to the string was passed */
2334 if ( ck_str )
2336 if ( strncmp( ck_str, "use", 3 ) == 0 )
2338 x11->xv_ck_info.source = CK_SRC_USE;
2340 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2342 x11->xv_ck_info.source = CK_SRC_SET;
2345 /* check if a valid pointer to the string was passed */
2346 if ( ck_method_str )
2348 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2350 x11->xv_ck_info.method = CK_METHOD_BACKGROUND;
2352 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2354 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2356 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2358 x11->xv_ck_info.method = CK_METHOD_AUTOPAINT;
2363 #endif
2365 struct vo_x11_state *vo_x11_init_state(void)
2367 struct vo_x11_state *s = talloc_ptrtype(NULL, s);
2368 *s = (struct vo_x11_state){
2369 .xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR },
2370 .olddecor = MWM_DECOR_ALL,
2371 .oldfuncs = MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
2372 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE,
2373 .old_gravity = NorthWestGravity,
2375 return s;