vo: improve fixed-vo behavior when video size changes in x11 VOs
[mplayer/greg.git] / libvo / x11_common.c
blob9fcd4f3f055f6b84a454ca2d935b7d01a12c0012
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <math.h>
22 #include <inttypes.h>
23 #include <limits.h>
25 #include "config.h"
26 #include "options.h"
27 #include "mp_msg.h"
28 #include "mp_fifo.h"
29 #include "libavutil/common.h"
30 #include "x11_common.h"
31 #include "talloc.h"
33 #ifdef X11_FULLSCREEN
35 #include <string.h>
36 #include <unistd.h>
37 #include <assert.h>
39 #include "video_out.h"
40 #include "aspect.h"
41 #include "geometry.h"
42 #include "osdep/timer.h"
44 #include <X11/Xmd.h>
45 #include <X11/Xlib.h>
46 #include <X11/Xutil.h>
47 #include <X11/Xatom.h>
49 #ifdef CONFIG_XSS
50 #include <X11/extensions/scrnsaver.h>
51 #endif
53 #ifdef CONFIG_XDPMS
54 #include <X11/extensions/dpms.h>
55 #endif
57 #ifdef CONFIG_XINERAMA
58 #include <X11/extensions/Xinerama.h>
59 #endif
61 #ifdef CONFIG_XF86VM
62 #include <X11/extensions/xf86vmode.h>
63 #endif
65 #ifdef CONFIG_XF86XK
66 #include <X11/XF86keysym.h>
67 #endif
69 #ifdef CONFIG_XV
70 #include <X11/extensions/Xv.h>
71 #include <X11/extensions/Xvlib.h>
73 #include "subopt-helper.h"
74 #endif
76 #include "input/input.h"
77 #include "input/mouse.h"
79 #define WIN_LAYER_ONBOTTOM 2
80 #define WIN_LAYER_NORMAL 4
81 #define WIN_LAYER_ONTOP 6
82 #define WIN_LAYER_ABOVE_DOCK 10
84 int fs_layer = WIN_LAYER_ABOVE_DOCK;
86 int stop_xscreensaver = 0;
88 static int dpms_disabled = 0;
90 char *mDisplayName = NULL;
92 char **vo_fstype_list;
94 /* 1 means that the WM is metacity (broken as hell) */
95 int metacity_hack = 0;
97 #ifdef CONFIG_XF86VM
98 XF86VidModeModeInfo **vidmodes = NULL;
99 XF86VidModeModeLine modeline;
100 #endif
102 static int vo_x11_get_fs_type(int supported);
103 static void saver_off(Display *);
104 static void saver_on(Display *);
107 * Sends the EWMH fullscreen state event.
109 * action: could be one of _NET_WM_STATE_REMOVE -- remove state
110 * _NET_WM_STATE_ADD -- add state
111 * _NET_WM_STATE_TOGGLE -- toggle
113 void vo_x11_ewmh_fullscreen(struct vo_x11_state *x11, int action)
115 assert(action == _NET_WM_STATE_REMOVE ||
116 action == _NET_WM_STATE_ADD || action == _NET_WM_STATE_TOGGLE);
118 if (x11->fs_type & vo_wm_FULLSCREEN)
120 XEvent xev;
122 /* init X event structure for _NET_WM_FULLSCREEN client message */
123 xev.xclient.type = ClientMessage;
124 xev.xclient.serial = 0;
125 xev.xclient.send_event = True;
126 xev.xclient.message_type = x11->XA_NET_WM_STATE;
127 xev.xclient.window = x11->window;
128 xev.xclient.format = 32;
129 xev.xclient.data.l[0] = action;
130 xev.xclient.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
131 xev.xclient.data.l[2] = 0;
132 xev.xclient.data.l[3] = 0;
133 xev.xclient.data.l[4] = 0;
135 /* finally send that damn thing */
136 if (!XSendEvent(x11->display, DefaultRootWindow(x11->display), False,
137 SubstructureRedirectMask | SubstructureNotifyMask,
138 &xev))
140 mp_tmsg(MSGT_VO, MSGL_ERR, "\nX11: Couldn't send EWMH fullscreen event!\n");
145 static void vo_hidecursor(Display * disp, Window win)
147 Cursor no_ptr;
148 Pixmap bm_no;
149 XColor black, dummy;
150 Colormap colormap;
151 const char bm_no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
153 if (WinID == 0)
154 return; // do not hide if playing on the root window
156 colormap = DefaultColormap(disp, DefaultScreen(disp));
157 if ( !XAllocNamedColor(disp, colormap, "black", &black, &dummy) )
159 return; // color alloc failed, give up
161 bm_no = XCreateBitmapFromData(disp, win, bm_no_data, 8, 8);
162 no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
163 XDefineCursor(disp, win, no_ptr);
164 XFreeCursor(disp, no_ptr);
165 if (bm_no != None)
166 XFreePixmap(disp, bm_no);
167 XFreeColors(disp,colormap,&black.pixel,1,0);
170 static void vo_showcursor(Display * disp, Window win)
172 if (WinID == 0)
173 return;
174 XDefineCursor(disp, win, 0);
177 static int x11_errorhandler(Display * display, XErrorEvent * event)
179 #define MSGLEN 60
180 char msg[MSGLEN];
182 XGetErrorText(display, event->error_code, (char *) &msg, MSGLEN);
184 mp_msg(MSGT_VO, MSGL_ERR, "X11 error: %s\n", msg);
186 mp_msg(MSGT_VO, MSGL_V,
187 "Type: %x, display: %p, resourceid: %lx, serial: %lx\n",
188 event->type, event->display, event->resourceid, event->serial);
189 mp_msg(MSGT_VO, MSGL_V,
190 "Error code: %x, request code: %x, minor code: %x\n",
191 event->error_code, event->request_code, event->minor_code);
193 // abort();
194 //exit_player("X11 error");
195 return 0;
196 #undef MSGLEN
199 void fstype_help(void)
201 mp_tmsg(MSGT_VO, MSGL_INFO, "Available fullscreen layer change modes:\n");
202 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FULL_SCREEN_TYPES\n");
204 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "none",
205 "don't set fullscreen window layer");
206 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer",
207 "use _WIN_LAYER hint with default layer");
208 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer=<0..15>",
209 "use _WIN_LAYER hint with a given layer number");
210 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "netwm",
211 "force NETWM style");
212 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "above",
213 "use _NETWM_STATE_ABOVE hint if available");
214 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "below",
215 "use _NETWM_STATE_BELOW hint if available");
216 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "fullscreen",
217 "use _NETWM_STATE_FULLSCREEN hint if availale");
218 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "stays_on_top",
219 "use _NETWM_STATE_STAYS_ON_TOP hint if available");
220 mp_msg(MSGT_VO, MSGL_INFO,
221 "You can also negate the settings with simply putting '-' in the beginning");
222 mp_msg(MSGT_VO, MSGL_INFO, "\n");
225 static void fstype_dump(int fstype)
227 if (fstype)
229 mp_msg(MSGT_VO, MSGL_V, "[x11] Current fstype setting honours");
230 if (fstype & vo_wm_LAYER)
231 mp_msg(MSGT_VO, MSGL_V, " LAYER");
232 if (fstype & vo_wm_FULLSCREEN)
233 mp_msg(MSGT_VO, MSGL_V, " FULLSCREEN");
234 if (fstype & vo_wm_STAYS_ON_TOP)
235 mp_msg(MSGT_VO, MSGL_V, " STAYS_ON_TOP");
236 if (fstype & vo_wm_ABOVE)
237 mp_msg(MSGT_VO, MSGL_V, " ABOVE");
238 if (fstype & vo_wm_BELOW)
239 mp_msg(MSGT_VO, MSGL_V, " BELOW");
240 mp_msg(MSGT_VO, MSGL_V, " X atoms\n");
241 } else
242 mp_msg(MSGT_VO, MSGL_V,
243 "[x11] Current fstype setting doesn't honour any X atoms\n");
246 static int net_wm_support_state_test(struct vo_x11_state *x11, Atom atom)
248 #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; } }
250 NET_WM_STATE_TEST(FULLSCREEN);
251 NET_WM_STATE_TEST(ABOVE);
252 NET_WM_STATE_TEST(STAYS_ON_TOP);
253 NET_WM_STATE_TEST(BELOW);
254 return 0;
257 static int x11_get_property(struct vo_x11_state *x11, Atom type, Atom ** args,
258 unsigned long *nitems)
260 int format;
261 unsigned long bytesafter;
263 return Success ==
264 XGetWindowProperty(x11->display, x11->rootwin, type, 0, 16384, False,
265 AnyPropertyType, &type, &format, nitems,
266 &bytesafter, (unsigned char **) args)
267 && *nitems > 0;
270 static int vo_wm_detect(struct vo *vo)
272 struct vo_x11_state *x11 = vo->x11;
273 int i;
274 int wm = 0;
275 unsigned long nitems;
276 Atom *args = NULL;
278 if (WinID >= 0)
279 return 0;
281 // -- supports layers
282 if (x11_get_property(x11, x11->XA_WIN_PROTOCOLS, &args, &nitems))
284 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports layers.\n");
285 for (i = 0; i < nitems; i++)
287 if (args[i] == x11->XA_WIN_LAYER)
289 wm |= vo_wm_LAYER;
290 metacity_hack |= 1;
291 } else
292 /* metacity is the only window manager I know which reports
293 * supporting only the _WIN_LAYER hint in _WIN_PROTOCOLS.
294 * (what's more support for it is broken) */
295 metacity_hack |= 2;
297 XFree(args);
298 if (wm && (metacity_hack == 1))
300 // metacity claims to support layers, but it is not the truth :-)
301 wm ^= vo_wm_LAYER;
302 mp_msg(MSGT_VO, MSGL_V,
303 "[x11] Using workaround for Metacity bugs.\n");
306 // --- netwm
307 if (x11_get_property(x11, x11->XA_NET_SUPPORTED, &args, &nitems))
309 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports NetWM.\n");
310 for (i = 0; i < nitems; i++)
311 wm |= net_wm_support_state_test(vo->x11, args[i]);
312 XFree(args);
315 if (wm == 0)
316 mp_msg(MSGT_VO, MSGL_V, "[x11] Unknown wm type...\n");
317 return wm;
320 #define XA_INIT(x) x11->XA##x = XInternAtom(x11->display, #x, False)
321 static void init_atoms(struct vo_x11_state *x11)
323 XA_INIT(_NET_SUPPORTED);
324 XA_INIT(_NET_WM_STATE);
325 XA_INIT(_NET_WM_STATE_FULLSCREEN);
326 XA_INIT(_NET_WM_STATE_ABOVE);
327 XA_INIT(_NET_WM_STATE_STAYS_ON_TOP);
328 XA_INIT(_NET_WM_STATE_BELOW);
329 XA_INIT(_NET_WM_PID);
330 XA_INIT(_WIN_PROTOCOLS);
331 XA_INIT(_WIN_LAYER);
332 XA_INIT(_WIN_HINTS);
333 XA_INIT(WM_PROTOCOLS);
334 XA_INIT(WM_DELETE_WINDOW);
337 void update_xinerama_info(struct vo *vo) {
338 struct MPOpts *opts = vo->opts;
339 xinerama_x = xinerama_y = 0;
340 #ifdef CONFIG_XINERAMA
341 if (xinerama_screen >= -1 && XineramaIsActive(vo->x11->display))
343 int screen = xinerama_screen;
344 XineramaScreenInfo *screens;
345 int num_screens;
347 screens = XineramaQueryScreens(vo->x11->display, &num_screens);
348 if (screen >= num_screens)
349 screen = num_screens - 1;
350 if (screen == -1) {
351 int x = vo->dx + vo->dwidth / 2;
352 int y = vo->dy + vo->dheight / 2;
353 for (screen = num_screens - 1; screen > 0; screen--) {
354 int left = screens[screen].x_org;
355 int right = left + screens[screen].width;
356 int top = screens[screen].y_org;
357 int bottom = top + screens[screen].height;
358 if (left <= x && x <= right && top <= y && y <= bottom)
359 break;
362 if (screen < 0)
363 screen = 0;
364 opts->vo_screenwidth = screens[screen].width;
365 opts->vo_screenheight = screens[screen].height;
366 xinerama_x = screens[screen].x_org;
367 xinerama_y = screens[screen].y_org;
369 XFree(screens);
371 #endif
372 aspect_save_screenres(vo, opts->vo_screenwidth, opts->vo_screenheight);
375 int vo_init(struct vo *vo)
377 struct MPOpts *opts = vo->opts;
378 struct vo_x11_state *x11 = vo->x11;
379 // int mScreen;
380 int depth, bpp;
381 unsigned int mask;
383 // char * DisplayName = ":0.0";
384 // Display * mDisplay;
385 XImage *mXImage = NULL;
387 // Window mRootWin;
388 XWindowAttributes attribs;
389 char *dispName;
391 if (vo_rootwin)
392 WinID = 0; // use root window
394 if (x11->depthonscreen)
396 saver_off(x11->display);
397 return 1; // already called
400 XSetErrorHandler(x11_errorhandler);
402 #if 0
403 if (!mDisplayName)
404 if (!(mDisplayName = getenv("DISPLAY")))
405 mDisplayName = strdup(":0.0");
406 #else
407 dispName = XDisplayName(mDisplayName);
408 #endif
410 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
412 x11->display = XOpenDisplay(dispName);
413 if (!x11->display)
415 mp_msg(MSGT_VO, MSGL_ERR,
416 "vo: couldn't open the X11 display (%s)!\n", dispName);
417 return 0;
419 x11->screen = DefaultScreen(x11->display); // screen ID
420 x11->rootwin = RootWindow(x11->display, x11->screen); // root window ID
422 init_atoms(vo->x11);
424 #ifdef CONFIG_XF86VM
426 int clock;
428 XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline);
429 if (!opts->vo_screenwidth)
430 opts->vo_screenwidth = modeline.hdisplay;
431 if (!opts->vo_screenheight)
432 opts->vo_screenheight = modeline.vdisplay;
434 #endif
436 if (!opts->vo_screenwidth)
437 opts->vo_screenwidth = DisplayWidth(x11->display, x11->screen);
438 if (!opts->vo_screenheight)
439 opts->vo_screenheight = DisplayHeight(x11->display, x11->screen);
441 // get color depth (from root window, or the best visual):
442 XGetWindowAttributes(x11->display, x11->rootwin, &attribs);
443 depth = attribs.depth;
445 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
447 Visual *visual;
449 depth = vo_find_depth_from_visuals(x11->display, x11->screen, &visual);
450 if (depth != -1)
451 mXImage = XCreateImage(x11->display, visual, depth, ZPixmap,
452 0, NULL, 1, 1, 8, 1);
453 } else
454 mXImage =
455 XGetImage(x11->display, x11->rootwin, 0, 0, 1, 1, AllPlanes, ZPixmap);
457 x11->depthonscreen = depth; // display depth on screen
459 // get bits/pixel from XImage structure:
460 if (mXImage == NULL)
462 mask = 0;
463 } else
466 * for the depth==24 case, the XImage structures might use
467 * 24 or 32 bits of data per pixel. The x11->depthonscreen
468 * field stores the amount of data per pixel in the
469 * XImage structure!
471 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
473 bpp = mXImage->bits_per_pixel;
474 if ((x11->depthonscreen + 7) / 8 != (bpp + 7) / 8)
475 x11->depthonscreen = bpp; // by A'rpi
476 mask =
477 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
478 mp_msg(MSGT_VO, MSGL_V,
479 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
480 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
481 XDestroyImage(mXImage);
483 if (((x11->depthonscreen + 7) / 8) == 2)
485 if (mask == 0x7FFF)
486 x11->depthonscreen = 15;
487 else if (mask == 0xFFFF)
488 x11->depthonscreen = 16;
490 // XCloseDisplay( mDisplay );
491 /* slightly improved local display detection AST */
492 if (strncmp(dispName, "unix:", 5) == 0)
493 dispName += 4;
494 else if (strncmp(dispName, "localhost:", 10) == 0)
495 dispName += 9;
496 if (*dispName == ':' && atoi(dispName + 1) < 10)
497 x11->display_is_local = 1;
498 else
499 x11->display_is_local = 0;
500 mp_msg(MSGT_VO, MSGL_V,
501 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
502 opts->vo_screenwidth, opts->vo_screenheight, depth, x11->depthonscreen,
503 dispName, x11->display_is_local ? "local" : "remote");
505 x11->wm_type = vo_wm_detect(vo);
507 x11->fs_type = vo_x11_get_fs_type(x11->wm_type);
509 fstype_dump(x11->fs_type);
511 saver_off(x11->display);
512 return 1;
515 void vo_uninit(struct vo_x11_state *x11)
517 if (!x11->display)
519 mp_msg(MSGT_VO, MSGL_V,
520 "vo: x11 uninit called but X11 not initialized..\n");
521 } else {
522 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
523 XSetErrorHandler(NULL);
524 XCloseDisplay(x11->display);
525 x11->depthonscreen = 0;
526 x11->display = NULL;
528 talloc_free(x11);
531 #include "osdep/keycodes.h"
532 #include "wskeys.h"
534 #ifdef XF86XK_AudioPause
535 static const struct mp_keymap keysym_map[] = {
536 {XF86XK_MenuKB, KEY_MENU},
537 {XF86XK_AudioPlay, KEY_PLAY}, {XF86XK_AudioPause, KEY_PAUSE}, {XF86XK_AudioStop, KEY_STOP},
538 {XF86XK_AudioPrev, KEY_PREV}, {XF86XK_AudioNext, KEY_NEXT},
539 {XF86XK_AudioMute, KEY_MUTE}, {XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN}, {XF86XK_AudioRaiseVolume, KEY_VOLUME_UP},
540 {0, 0}
543 static void vo_x11_putkey_ext(struct vo *vo, int keysym)
545 struct mp_fifo *f = vo->key_fifo;
546 int mpkey = lookup_keymap_table(keysym_map, keysym);
547 if (mpkey)
548 mplayer_put_key(f, mpkey);
550 #endif
552 static const struct mp_keymap keymap[] = {
553 // special keys
554 {wsEscape, KEY_ESC}, {wsBackSpace, KEY_BS}, {wsTab, KEY_TAB}, {wsEnter, KEY_ENTER},
556 // cursor keys
557 {wsLeft, KEY_LEFT}, {wsRight, KEY_RIGHT}, {wsUp, KEY_UP}, {wsDown, KEY_DOWN},
559 // navigation block
560 {wsInsert, KEY_INSERT}, {wsDelete, KEY_DELETE}, {wsHome, KEY_HOME}, {wsEnd, KEY_END},
561 {wsPageUp, KEY_PAGE_UP}, {wsPageDown, KEY_PAGE_DOWN},
563 // F-keys
564 {wsF1, KEY_F+1}, {wsF2, KEY_F+2}, {wsF3, KEY_F+3}, {wsF4, KEY_F+4},
565 {wsF5, KEY_F+5}, {wsF6, KEY_F+6}, {wsF7, KEY_F+7}, {wsF8, KEY_F+8},
566 {wsF9, KEY_F+9}, {wsF10, KEY_F+10}, {wsF11, KEY_F+11}, {wsF12, KEY_F+12},
568 // numpad independent of numlock
569 {wsGrayMinus, '-'}, {wsGrayPlus, '+'}, {wsGrayMul, '*'}, {wsGrayDiv, '/'},
570 {wsGrayEnter, KEY_KPENTER},
572 // numpad with numlock
573 {wsGray0, KEY_KP0}, {wsGray1, KEY_KP1}, {wsGray2, KEY_KP2},
574 {wsGray3, KEY_KP3}, {wsGray4, KEY_KP4}, {wsGray5, KEY_KP5},
575 {wsGray6, KEY_KP6}, {wsGray7, KEY_KP7}, {wsGray8, KEY_KP8},
576 {wsGray9, KEY_KP9}, {wsGrayDecimal, KEY_KPDEC},
578 // numpad without numlock
579 {wsGrayInsert, KEY_KPINS}, {wsGrayEnd, KEY_KP1}, {wsGrayDown, KEY_KP2},
580 {wsGrayPgDn, KEY_KP3}, {wsGrayLeft, KEY_KP4}, {wsGray5Dup, KEY_KP5},
581 {wsGrayRight, KEY_KP6}, {wsGrayHome, KEY_KP7}, {wsGrayUp, KEY_KP8},
582 {wsGrayPgUp, KEY_KP9}, {wsGrayDelete, KEY_KPDEL},
584 {0, 0}
587 void vo_x11_putkey(struct vo *vo, int key)
589 static const char *passthrough_keys = " -+*/<>`~!@#$%^&()_{}:;\"\',.?\\|=[]";
590 int mpkey = 0;
591 if ((key >= 'a' && key <= 'z') ||
592 (key >= 'A' && key <= 'Z') ||
593 (key >= '0' && key <= '9') ||
594 (key > 0 && key < 256 && strchr(passthrough_keys, key)))
595 mpkey = key;
597 if (!mpkey)
598 mpkey = lookup_keymap_table(keymap, key);
600 if (mpkey)
601 mplayer_put_key(vo->key_fifo, mpkey);
605 // ----- Motif header: -------
607 #define MWM_HINTS_FUNCTIONS (1L << 0)
608 #define MWM_HINTS_DECORATIONS (1L << 1)
609 #define MWM_HINTS_INPUT_MODE (1L << 2)
610 #define MWM_HINTS_STATUS (1L << 3)
612 #define MWM_FUNC_ALL (1L << 0)
613 #define MWM_FUNC_RESIZE (1L << 1)
614 #define MWM_FUNC_MOVE (1L << 2)
615 #define MWM_FUNC_MINIMIZE (1L << 3)
616 #define MWM_FUNC_MAXIMIZE (1L << 4)
617 #define MWM_FUNC_CLOSE (1L << 5)
619 #define MWM_DECOR_ALL (1L << 0)
620 #define MWM_DECOR_BORDER (1L << 1)
621 #define MWM_DECOR_RESIZEH (1L << 2)
622 #define MWM_DECOR_TITLE (1L << 3)
623 #define MWM_DECOR_MENU (1L << 4)
624 #define MWM_DECOR_MINIMIZE (1L << 5)
625 #define MWM_DECOR_MAXIMIZE (1L << 6)
627 #define MWM_INPUT_MODELESS 0
628 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
629 #define MWM_INPUT_SYSTEM_MODAL 2
630 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
631 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
633 #define MWM_TEAROFF_WINDOW (1L<<0)
635 typedef struct
637 long flags;
638 long functions;
639 long decorations;
640 long input_mode;
641 long state;
642 } MotifWmHints;
644 static MotifWmHints vo_MotifWmHints;
645 static Atom vo_MotifHints = None;
647 void vo_x11_decoration(struct vo *vo, int d)
649 struct vo_x11_state *x11 = vo->x11;
650 Atom mtype;
651 int mformat;
652 unsigned long mn, mb;
654 if (!WinID)
655 return;
657 if (vo_fsmode & 8)
659 XSetTransientForHint(x11->display, x11->window,
660 RootWindow(x11->display, x11->screen));
663 vo_MotifHints = XInternAtom(x11->display, "_MOTIF_WM_HINTS", 0);
664 if (vo_MotifHints != None)
666 if (!d)
668 MotifWmHints *mhints = NULL;
670 XGetWindowProperty(x11->display, x11->window,
671 vo_MotifHints, 0, 20, False,
672 vo_MotifHints, &mtype, &mformat, &mn,
673 &mb, (unsigned char **) &mhints);
674 if (mhints)
676 if (mhints->flags & MWM_HINTS_DECORATIONS)
677 x11->olddecor = mhints->decorations;
678 if (mhints->flags & MWM_HINTS_FUNCTIONS)
679 x11->oldfuncs = mhints->functions;
680 XFree(mhints);
684 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
685 vo_MotifWmHints.flags =
686 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
687 if (d)
689 vo_MotifWmHints.functions = x11->oldfuncs;
690 d = x11->olddecor;
692 #if 0
693 vo_MotifWmHints.decorations =
694 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
695 #else
696 vo_MotifWmHints.decorations =
697 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
698 #endif
699 XChangeProperty(x11->display, x11->window, vo_MotifHints,
700 vo_MotifHints, 32,
701 PropModeReplace,
702 (unsigned char *) &vo_MotifWmHints,
703 (vo_fsmode & 4) ? 4 : 5);
707 void vo_x11_classhint(struct vo *vo, Window window, const char *name)
709 struct vo_x11_state *x11 = vo->x11;
710 XClassHint wmClass;
711 pid_t pid = getpid();
713 wmClass.res_name = vo_winname ? vo_winname : name;
714 wmClass.res_class = "MPlayer";
715 XSetClassHint(x11->display, window, &wmClass);
716 XChangeProperty(x11->display, window, x11->XA_NET_WM_PID, XA_CARDINAL,
717 32, PropModeReplace, (unsigned char *) &pid, 1);
720 void vo_x11_uninit(struct vo *vo)
722 struct vo_x11_state *x11 = vo->x11;
723 saver_on(x11->display);
724 if (x11->window != None)
725 vo_showcursor(x11->display, x11->window);
727 if (x11->f_gc)
729 XFreeGC(vo->x11->display, x11->f_gc);
730 x11->f_gc = NULL;
733 if (x11->vo_gc)
735 XSetBackground(vo->x11->display, x11->vo_gc, 0);
736 XFreeGC(vo->x11->display, x11->vo_gc);
737 x11->vo_gc = NULL;
739 if (x11->window != None)
741 XClearWindow(x11->display, x11->window);
742 if (WinID < 0)
744 XEvent xev;
746 XUnmapWindow(x11->display, x11->window);
747 XSelectInput(x11->display, x11->window, StructureNotifyMask);
748 XDestroyWindow(x11->display, x11->window);
751 XNextEvent(x11->display, &xev);
753 while (xev.type != DestroyNotify
754 || xev.xdestroywindow.event != x11->window);
756 x11->window = None;
758 vo_fs = 0;
759 x11->vo_old_width = x11->vo_old_height = 0;
760 x11->last_video_width = 0;
761 x11->last_video_height = 0;
762 x11->size_changed_during_fs = false;
766 int vo_x11_check_events(struct vo *vo)
768 struct vo_x11_state *x11 = vo->x11;
769 Display *display = vo->x11->display;
770 int ret = 0;
771 XEvent Event;
772 char buf[100];
773 KeySym keySym;
775 // unsigned long vo_KeyTable[512];
777 if ((x11->vo_mouse_autohide) && x11->mouse_waiting_hide &&
778 (GetTimerMS() - x11->mouse_timer >= 1000)) {
779 vo_hidecursor(display, x11->window);
780 x11->mouse_waiting_hide = 0;
783 while (XPending(display))
785 XNextEvent(display, &Event);
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 (x11->window == None)
794 break;
796 int old_w = vo->dwidth, old_h = vo->dheight;
797 vo_x11_update_geometry(vo);
798 if (vo->dwidth != old_w || vo->dheight != old_h)
799 ret |= VO_EVENT_RESIZE;
801 break;
802 case KeyPress:
804 int key;
806 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
807 &x11->compose_status);
808 #ifdef XF86XK_AudioPause
809 vo_x11_putkey_ext(vo, keySym);
810 #endif
811 key =
812 ((keySym & 0xff00) !=
813 0 ? ((keySym & 0x00ff) + 256) : (keySym));
814 vo_x11_putkey(vo, key);
815 ret |= VO_EVENT_KEYPRESS;
817 break;
818 case MotionNotify:
819 vo_mouse_movement(vo, Event.xmotion.x, Event.xmotion.y);
821 if (x11->vo_mouse_autohide)
823 vo_showcursor(display, x11->window);
824 x11->mouse_waiting_hide = 1;
825 x11->mouse_timer = GetTimerMS();
827 break;
828 case ButtonPress:
829 if (x11->vo_mouse_autohide)
831 vo_showcursor(display, x11->window);
832 x11->mouse_waiting_hide = 1;
833 x11->mouse_timer = GetTimerMS();
835 mplayer_put_key(vo->key_fifo,
836 (MOUSE_BTN0 + Event.xbutton.button - 1)
837 | MP_KEY_DOWN);
838 break;
839 case ButtonRelease:
840 if (x11->vo_mouse_autohide)
842 vo_showcursor(display, x11->window);
843 x11->mouse_waiting_hide = 1;
844 x11->mouse_timer = GetTimerMS();
846 mplayer_put_key(vo->key_fifo,
847 MOUSE_BTN0 + Event.xbutton.button - 1);
848 break;
849 case PropertyNotify:
851 char *name =
852 XGetAtomName(display, Event.xproperty.atom);
854 if (!name)
855 break;
857 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
859 XFree(name);
861 break;
862 case MapNotify:
863 x11->vo_hint.win_gravity = x11->old_gravity;
864 XSetWMNormalHints(display, x11->window, &x11->vo_hint);
865 x11->fs_flip = 0;
866 break;
867 case DestroyNotify:
868 mp_msg(MSGT_VO, MSGL_WARN, "Our window was destroyed, exiting\n");
869 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
870 break;
871 case ClientMessage:
872 if (Event.xclient.message_type == x11->XAWM_PROTOCOLS &&
873 Event.xclient.data.l[0] == x11->XAWM_DELETE_WINDOW)
874 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
875 break;
878 return ret;
882 * \brief sets the size and position of the non-fullscreen window.
884 static void vo_x11_nofs_sizepos(struct vo *vo, int x, int y,
885 int width, int height)
887 struct vo_x11_state *x11 = vo->x11;
888 if (width == x11->last_video_width && height == x11->last_video_height) {
889 if (!vo->opts->force_window_position && !x11->size_changed_during_fs)
890 return;
891 } else if (vo_fs)
892 x11->size_changed_during_fs = true;
893 x11->last_video_height = height;
894 x11->last_video_width = width;
895 vo_x11_sizehint(vo, x, y, width, height, 0);
896 if (vo_fs) {
897 x11->vo_old_x = x;
898 x11->vo_old_y = y;
899 x11->vo_old_width = width;
900 x11->vo_old_height = height;
902 else
904 vo->dwidth = width;
905 vo->dheight = height;
906 if (vo->opts->force_window_position)
907 XMoveResizeWindow(vo->x11->display, vo->x11->window, x, y, width,
908 height);
909 else
910 XResizeWindow(vo->x11->display, vo->x11->window, width, height);
914 void vo_x11_sizehint(struct vo *vo, int x, int y, int width, int height, int max)
916 struct vo_x11_state *x11 = vo->x11;
917 x11->vo_hint.flags = 0;
918 if (vo_keepaspect)
920 x11->vo_hint.flags |= PAspect;
921 x11->vo_hint.min_aspect.x = width;
922 x11->vo_hint.min_aspect.y = height;
923 x11->vo_hint.max_aspect.x = width;
924 x11->vo_hint.max_aspect.y = height;
927 x11->vo_hint.flags |= PPosition | PSize;
928 x11->vo_hint.x = x;
929 x11->vo_hint.y = y;
930 x11->vo_hint.width = width;
931 x11->vo_hint.height = height;
932 if (max)
934 x11->vo_hint.flags |= PMaxSize;
935 x11->vo_hint.max_width = width;
936 x11->vo_hint.max_height = height;
937 } else
939 x11->vo_hint.max_width = 0;
940 x11->vo_hint.max_height = 0;
943 // Set minimum height/width to 4 to avoid off-by-one errors
944 // and because mga_vid requires a minimal size of 4 pixels.
945 x11->vo_hint.flags |= PMinSize;
946 x11->vo_hint.min_width = x11->vo_hint.min_height = 4;
948 // Set the base size. A window manager might display the window
949 // size to the user relative to this.
950 // Setting these to width/height might be nice, but e.g. fluxbox can't handle it.
951 x11->vo_hint.flags |= PBaseSize;
952 x11->vo_hint.base_width = 0 /*width*/;
953 x11->vo_hint.base_height = 0 /*height*/;
955 x11->vo_hint.flags |= PWinGravity;
956 x11->vo_hint.win_gravity = StaticGravity;
957 XSetWMNormalHints(x11->display, x11->window, &x11->vo_hint);
960 static int vo_x11_get_gnome_layer(struct vo_x11_state *x11, Window win)
962 Atom type;
963 int format;
964 unsigned long nitems;
965 unsigned long bytesafter;
966 unsigned short *args = NULL;
968 if (XGetWindowProperty(x11->display, win, x11->XA_WIN_LAYER, 0, 16384,
969 False, AnyPropertyType, &type, &format, &nitems,
970 &bytesafter,
971 (unsigned char **) &args) == Success
972 && nitems > 0 && args)
974 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
975 *args);
976 return *args;
978 return WIN_LAYER_NORMAL;
982 static Window vo_x11_create_smooth_window(struct vo_x11_state *x11, Window mRoot,
983 Visual * vis, int x, int y,
984 unsigned int width, unsigned int height,
985 int depth, Colormap col_map)
987 unsigned long xswamask = CWBorderPixel;
988 XSetWindowAttributes xswa;
989 Window ret_win;
991 if (col_map != CopyFromParent)
993 xswa.colormap = col_map;
994 xswamask |= CWColormap;
996 xswa.background_pixel = 0;
997 xswa.border_pixel = 0;
998 xswa.backing_store = NotUseful;
999 xswa.bit_gravity = StaticGravity;
1001 ret_win =
1002 XCreateWindow(x11->display, x11->rootwin, x, y, width, height, 0, depth,
1003 CopyFromParent, vis, xswamask, &xswa);
1004 XSetWMProtocols(x11->display, ret_win, &x11->XAWM_DELETE_WINDOW, 1);
1005 if (!x11->f_gc)
1006 x11->f_gc = XCreateGC(x11->display, ret_win, 0, 0);
1007 XSetForeground(x11->display, x11->f_gc, 0);
1009 return ret_win;
1013 * \brief create and setup a window suitable for display
1014 * \param vis Visual to use for creating the window
1015 * \param x x position of window
1016 * \param y y position of window
1017 * \param width width of window
1018 * \param height height of window
1019 * \param flags flags for window creation.
1020 * Only VOFLAG_FULLSCREEN is supported so far.
1021 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1022 * \param classname name to use for the classhint
1023 * \param title title for the window
1025 * This also does the grunt-work like setting Window Manager hints etc.
1026 * If vo_window is already set it just moves and resizes it.
1028 void vo_x11_create_vo_window(struct vo *vo, XVisualInfo *vis, int x, int y,
1029 unsigned int width, unsigned int height, int flags,
1030 Colormap col_map,
1031 const char *classname, const char *title)
1033 struct MPOpts *opts = vo->opts;
1034 struct vo_x11_state *x11 = vo->x11;
1035 Display *mDisplay = vo->x11->display;
1036 XGCValues xgcv;
1037 if (WinID >= 0) {
1038 vo_fs = flags & VOFLAG_FULLSCREEN;
1039 x11->window = WinID ? (Window)WinID : x11->rootwin;
1040 if (col_map != CopyFromParent) {
1041 unsigned long xswamask = CWColormap;
1042 XSetWindowAttributes xswa;
1043 xswa.colormap = col_map;
1044 XChangeWindowAttributes(mDisplay, x11->window, xswamask, &xswa);
1045 XInstallColormap(mDisplay, col_map);
1047 if (WinID) vo_x11_update_geometry(vo);
1048 vo_x11_selectinput_witherr(mDisplay, x11->window,
1049 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1050 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1051 goto final;
1053 if (x11->window == None) {
1054 vo_fs = 0;
1055 vo->dwidth = width;
1056 vo->dheight = height;
1057 x11->window = vo_x11_create_smooth_window(x11, x11->rootwin, vis->visual,
1058 x, y, width, height, vis->depth, col_map);
1059 x11->window_state = VOFLAG_HIDDEN;
1061 if (flags & VOFLAG_HIDDEN)
1062 goto final;
1063 if (x11->window_state & VOFLAG_HIDDEN) {
1064 XSizeHints hint;
1065 XEvent xev;
1066 x11->window_state &= ~VOFLAG_HIDDEN;
1067 vo_x11_classhint(vo, x11->window, classname);
1068 XStoreName(mDisplay, x11->window, title);
1069 vo_hidecursor(mDisplay, x11->window);
1070 XSelectInput(mDisplay, x11->window, StructureNotifyMask);
1071 hint.x = x; hint.y = y;
1072 hint.width = width; hint.height = height;
1073 hint.flags = PSize;
1074 if (geometry_xy_changed)
1075 hint.flags |= PPosition;
1076 XSetStandardProperties(mDisplay, x11->window, title, title, None, NULL, 0, &hint);
1077 if (!vo_border) vo_x11_decoration(vo, 0);
1078 // map window
1079 XMapWindow(mDisplay, x11->window);
1080 vo_x11_clearwindow(vo, x11->window);
1081 // wait for map
1082 do {
1083 XNextEvent(mDisplay, &xev);
1084 } while (xev.type != MapNotify || xev.xmap.event != x11->window);
1085 XSelectInput(mDisplay, x11->window, NoEventMask);
1086 XSync(mDisplay, False);
1087 vo_x11_selectinput_witherr(mDisplay, x11->window,
1088 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1089 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1091 if (opts->vo_ontop) vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1092 if (!geometry_xy_changed)
1093 vo_x11_update_geometry(vo);
1094 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, width, height);
1095 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1096 vo_x11_fullscreen(vo);
1097 else if (vo_fs) {
1098 // if we are already in fullscreen do not switch back and forth, just
1099 // set the size values right.
1100 vo->dwidth = vo->opts->vo_screenwidth;
1101 vo->dheight = vo->opts->vo_screenheight;
1103 final:
1104 if (x11->vo_gc != None)
1105 XFreeGC(mDisplay, x11->vo_gc);
1106 x11->vo_gc = XCreateGC(mDisplay, x11->window, GCForeground, &xgcv);
1107 XSync(mDisplay, False);
1108 x11->vo_mouse_autohide = 1;
1111 void vo_x11_clearwindow_part(struct vo *vo, Window vo_window,
1112 int img_width, int img_height, int use_fs)
1114 struct vo_x11_state *x11 = vo->x11;
1115 struct MPOpts *opts = vo->opts;
1116 Display *mDisplay = vo->x11->display;
1117 int u_dheight, u_dwidth, left_ov, left_ov2;
1119 if (!x11->f_gc)
1120 return;
1122 u_dheight = use_fs ? opts->vo_screenheight : vo->dheight;
1123 u_dwidth = use_fs ? opts->vo_screenwidth : vo->dwidth;
1124 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1125 return;
1127 left_ov = (u_dheight - img_height) / 2;
1128 left_ov2 = (u_dwidth - img_width) / 2;
1130 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, 0, u_dwidth, left_ov);
1131 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, u_dheight - left_ov - 1,
1132 u_dwidth, left_ov + 1);
1134 if (u_dwidth > img_width)
1136 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, left_ov, left_ov2,
1137 img_height);
1138 XFillRectangle(mDisplay, vo_window, x11->f_gc, u_dwidth - left_ov2 - 1,
1139 left_ov, left_ov2 + 1, img_height);
1142 XFlush(mDisplay);
1145 void vo_x11_clearwindow(struct vo *vo, Window vo_window)
1147 struct vo_x11_state *x11 = vo->x11;
1148 struct MPOpts *opts = vo->opts;
1149 if (!x11->f_gc)
1150 return;
1151 XFillRectangle(x11->display, vo_window, x11->f_gc, 0, 0,
1152 opts->vo_screenwidth, opts->vo_screenheight);
1154 XFlush(x11->display);
1158 void vo_x11_setlayer(struct vo *vo, Window vo_window, int layer)
1160 struct vo_x11_state *x11 = vo->x11;
1161 if (WinID >= 0)
1162 return;
1164 if (x11->fs_type & vo_wm_LAYER)
1166 XClientMessageEvent xev;
1168 if (!x11->orig_layer)
1169 x11->orig_layer = vo_x11_get_gnome_layer(x11, vo_window);
1171 memset(&xev, 0, sizeof(xev));
1172 xev.type = ClientMessage;
1173 xev.display = x11->display;
1174 xev.window = vo_window;
1175 xev.message_type = x11->XA_WIN_LAYER;
1176 xev.format = 32;
1177 xev.data.l[0] = layer ? fs_layer : x11->orig_layer; // if not fullscreen, stay on default layer
1178 xev.data.l[1] = CurrentTime;
1179 mp_msg(MSGT_VO, MSGL_V,
1180 "[x11] Layered style stay on top (layer %ld).\n",
1181 xev.data.l[0]);
1182 XSendEvent(x11->display, x11->rootwin, False, SubstructureNotifyMask,
1183 (XEvent *) & xev);
1184 } else if (x11->fs_type & vo_wm_NETWM)
1186 XClientMessageEvent xev;
1187 char *state;
1189 memset(&xev, 0, sizeof(xev));
1190 xev.type = ClientMessage;
1191 xev.message_type = x11->XA_NET_WM_STATE;
1192 xev.display = x11->display;
1193 xev.window = vo_window;
1194 xev.format = 32;
1195 xev.data.l[0] = layer;
1197 if (x11->fs_type & vo_wm_STAYS_ON_TOP)
1198 xev.data.l[1] = x11->XA_NET_WM_STATE_STAYS_ON_TOP;
1199 else if (x11->fs_type & vo_wm_ABOVE)
1200 xev.data.l[1] = x11->XA_NET_WM_STATE_ABOVE;
1201 else if (x11->fs_type & vo_wm_FULLSCREEN)
1202 xev.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
1203 else if (x11->fs_type & vo_wm_BELOW)
1204 // This is not fallback. We can safely assume that the situation
1205 // where only NETWM_STATE_BELOW is supported doesn't exist.
1206 xev.data.l[1] = x11->XA_NET_WM_STATE_BELOW;
1208 XSendEvent(x11->display, x11->rootwin, False, SubstructureRedirectMask,
1209 (XEvent *) & xev);
1210 state = XGetAtomName(x11->display, xev.data.l[1]);
1211 mp_msg(MSGT_VO, MSGL_V,
1212 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1213 layer, state);
1214 XFree(state);
1218 static int vo_x11_get_fs_type(int supported)
1220 int i;
1221 int type = supported;
1223 if (vo_fstype_list)
1225 for (i = 0; vo_fstype_list[i]; i++)
1227 int neg = 0;
1228 char *arg = vo_fstype_list[i];
1230 if (vo_fstype_list[i][0] == '-')
1232 neg = 1;
1233 arg = vo_fstype_list[i] + 1;
1236 if (!strncmp(arg, "layer", 5))
1238 if (!neg && (arg[5] == '='))
1240 char *endptr = NULL;
1241 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1243 if (endptr && *endptr == '\0' && layer >= 0
1244 && layer <= 15)
1245 fs_layer = layer;
1247 if (neg)
1248 type &= ~vo_wm_LAYER;
1249 else
1250 type |= vo_wm_LAYER;
1251 } else if (!strcmp(arg, "above"))
1253 if (neg)
1254 type &= ~vo_wm_ABOVE;
1255 else
1256 type |= vo_wm_ABOVE;
1257 } else if (!strcmp(arg, "fullscreen"))
1259 if (neg)
1260 type &= ~vo_wm_FULLSCREEN;
1261 else
1262 type |= vo_wm_FULLSCREEN;
1263 } else if (!strcmp(arg, "stays_on_top"))
1265 if (neg)
1266 type &= ~vo_wm_STAYS_ON_TOP;
1267 else
1268 type |= vo_wm_STAYS_ON_TOP;
1269 } else if (!strcmp(arg, "below"))
1271 if (neg)
1272 type &= ~vo_wm_BELOW;
1273 else
1274 type |= vo_wm_BELOW;
1275 } else if (!strcmp(arg, "netwm"))
1277 if (neg)
1278 type &= ~vo_wm_NETWM;
1279 else
1280 type |= vo_wm_NETWM;
1281 } else if (!strcmp(arg, "none"))
1282 type = 0; // clear; keep parsing
1286 return type;
1290 * \brief update vo->dx, vo->dy, vo->dwidth and vo->dheight with current values of vo->x11->window
1291 * \return returns current color depth of vo->x11->window
1293 int vo_x11_update_geometry(struct vo *vo)
1295 struct vo_x11_state *x11 = vo->x11;
1296 unsigned depth, w, h;
1297 int dummy_int;
1298 Window dummy_win;
1299 XGetGeometry(x11->display, x11->window, &dummy_win, &dummy_int, &dummy_int,
1300 &w, &h, &dummy_int, &depth);
1301 if (w <= INT_MAX && h <= INT_MAX) {
1302 vo->dwidth = w;
1303 vo->dheight = h;
1305 XTranslateCoordinates(x11->display, x11->window, x11->rootwin, 0, 0,
1306 &vo->dx, &vo->dy, &dummy_win);
1307 if (vo_wintitle)
1308 XStoreName(x11->display, x11->window, vo_wintitle);
1310 return depth <= INT_MAX ? depth : 0;
1313 void vo_x11_fullscreen(struct vo *vo)
1315 struct MPOpts *opts = vo->opts;
1316 struct vo_x11_state *x11 = vo->x11;
1317 int x, y, w, h;
1318 x = x11->vo_old_x;
1319 y = x11->vo_old_y;
1320 w = x11->vo_old_width;
1321 h = x11->vo_old_height;
1323 if (WinID >= 0) {
1324 vo_fs = !vo_fs;
1325 return;
1327 if (x11->fs_flip)
1328 return;
1330 if (vo_fs)
1332 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1333 vo_fs = VO_FALSE;
1334 if (x11->size_changed_during_fs && (x11->fs_type & vo_wm_FULLSCREEN))
1335 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, x11->last_video_width,
1336 x11->last_video_height);
1337 x11->size_changed_during_fs = false;
1338 } else
1340 // win->fs
1341 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1343 vo_fs = VO_TRUE;
1344 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1346 x11->vo_old_x = vo->dx;
1347 x11->vo_old_y = vo->dy;
1348 x11->vo_old_width = vo->dwidth;
1349 x11->vo_old_height = vo->dheight;
1351 update_xinerama_info(vo);
1352 x = xinerama_x;
1353 y = xinerama_y;
1354 w = opts->vo_screenwidth;
1355 h = opts->vo_screenheight;
1358 long dummy;
1360 XGetWMNormalHints(x11->display, x11->window, &x11->vo_hint, &dummy);
1361 if (!(x11->vo_hint.flags & PWinGravity))
1362 x11->old_gravity = NorthWestGravity;
1363 else
1364 x11->old_gravity = x11->vo_hint.win_gravity;
1366 if (x11->wm_type == 0 && !(vo_fsmode & 16))
1368 XUnmapWindow(x11->display, x11->window); // required for MWM
1369 XWithdrawWindow(x11->display, x11->window, x11->screen);
1370 x11->fs_flip = 1;
1373 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1375 vo_x11_decoration(vo, vo_border && !vo_fs);
1376 vo_x11_sizehint(vo, x, y, w, h, 0);
1377 vo_x11_setlayer(vo, x11->window, vo_fs);
1380 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1382 /* some WMs lose ontop after fullscreen */
1383 if ((!(vo_fs)) & opts->vo_ontop)
1384 vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1386 XMapRaised(x11->display, x11->window);
1387 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1388 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1389 XRaiseWindow(x11->display, x11->window);
1390 XFlush(x11->display);
1393 void vo_x11_ontop(struct vo *vo)
1395 struct MPOpts *opts = vo->opts;
1396 opts->vo_ontop = !opts->vo_ontop;
1398 vo_x11_setlayer(vo, vo->x11->window, opts->vo_ontop);
1401 void vo_x11_border(struct vo *vo)
1403 vo_border = !vo_border;
1404 vo_x11_decoration(vo, vo_border && !vo_fs);
1408 * XScreensaver stuff
1411 static int screensaver_off;
1412 static unsigned int time_last;
1414 void xscreensaver_heartbeat(struct vo_x11_state *x11)
1416 unsigned int time = GetTimerMS();
1418 if (x11->display && screensaver_off && (time - time_last) > 30000)
1420 time_last = time;
1422 XResetScreenSaver(x11->display);
1426 static int xss_suspend(Display *mDisplay, Bool suspend)
1428 #ifndef CONFIG_XSS
1429 return 0;
1430 #else
1431 int event, error, major, minor;
1432 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1433 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1434 return 0;
1435 if (major < 1 || (major == 1 && minor < 1))
1436 return 0;
1437 XScreenSaverSuspend(mDisplay, suspend);
1438 return 1;
1439 #endif
1443 * End of XScreensaver stuff
1446 static void saver_on(Display * mDisplay)
1449 if (!screensaver_off)
1450 return;
1451 screensaver_off = 0;
1452 if (xss_suspend(mDisplay, False))
1453 return;
1454 #ifdef CONFIG_XDPMS
1455 if (dpms_disabled)
1457 int nothing;
1458 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1460 if (!DPMSEnable(mDisplay))
1461 { // restoring power saving settings
1462 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1463 } else
1465 // DPMS does not seem to be enabled unless we call DPMSInfo
1466 BOOL onoff;
1467 CARD16 state;
1469 DPMSForceLevel(mDisplay, DPMSModeOn);
1470 DPMSInfo(mDisplay, &state, &onoff);
1471 if (onoff)
1473 mp_msg(MSGT_VO, MSGL_V,
1474 "Successfully enabled DPMS\n");
1475 } else
1477 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1481 dpms_disabled = 0;
1483 #endif
1486 static void saver_off(Display * mDisplay)
1488 int nothing;
1490 if (screensaver_off)
1491 return;
1492 screensaver_off = 1;
1493 if (xss_suspend(mDisplay, True))
1494 return;
1495 #ifdef CONFIG_XDPMS
1496 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1498 BOOL onoff;
1499 CARD16 state;
1501 DPMSInfo(mDisplay, &state, &onoff);
1502 if (onoff)
1504 Status stat;
1506 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1507 dpms_disabled = 1;
1508 stat = DPMSDisable(mDisplay); // monitor powersave off
1509 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1512 #endif
1515 static XErrorHandler old_handler = NULL;
1516 static int selectinput_err = 0;
1517 static int x11_selectinput_errorhandler(Display * display,
1518 XErrorEvent * event)
1520 if (event->error_code == BadAccess)
1522 selectinput_err = 1;
1523 mp_msg(MSGT_VO, MSGL_ERR,
1524 "X11 error: BadAccess during XSelectInput Call\n");
1525 mp_msg(MSGT_VO, MSGL_ERR,
1526 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1527 /* If you think MPlayer should shutdown with this error,
1528 * comment out the following line */
1529 return 0;
1531 if (old_handler != NULL)
1532 old_handler(display, event);
1533 else
1534 x11_errorhandler(display, event);
1535 return 0;
1538 void vo_x11_selectinput_witherr(Display * display, Window w,
1539 long event_mask)
1541 XSync(display, False);
1542 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1543 selectinput_err = 0;
1544 if (vo_nomouse_input)
1546 XSelectInput(display, w,
1547 event_mask &
1548 (~(ButtonPressMask | ButtonReleaseMask)));
1549 } else
1551 XSelectInput(display, w, event_mask);
1553 XSync(display, False);
1554 XSetErrorHandler(old_handler);
1555 if (selectinput_err)
1557 mp_msg(MSGT_VO, MSGL_ERR,
1558 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1559 XSelectInput(display, w,
1560 event_mask &
1562 (ButtonPressMask | ButtonReleaseMask |
1563 PointerMotionMask)));
1567 #ifdef CONFIG_XF86VM
1568 void vo_vm_switch(struct vo *vo)
1570 struct vo_x11_state *x11 = vo->x11;
1571 struct MPOpts *opts = vo->opts;
1572 Display *mDisplay = x11->display;
1573 int vm_event, vm_error;
1574 int vm_ver, vm_rev;
1575 int i, j, have_vm = 0;
1576 int X = vo->dwidth, Y = vo->dheight;
1577 int modeline_width, modeline_height;
1579 int modecount;
1581 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1583 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1584 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1585 vm_rev);
1586 have_vm = 1;
1587 } else {
1588 mp_msg(MSGT_VO, MSGL_WARN,
1589 "XF86VidMode extension not available.\n");
1592 if (have_vm)
1594 if (vidmodes == NULL)
1595 XF86VidModeGetAllModeLines(mDisplay, x11->screen, &modecount,
1596 &vidmodes);
1597 j = 0;
1598 modeline_width = vidmodes[0]->hdisplay;
1599 modeline_height = vidmodes[0]->vdisplay;
1601 for (i = 1; i < modecount; i++)
1602 if ((vidmodes[i]->hdisplay >= X)
1603 && (vidmodes[i]->vdisplay >= Y))
1604 if ((vidmodes[i]->hdisplay <= modeline_width)
1605 && (vidmodes[i]->vdisplay <= modeline_height))
1607 modeline_width = vidmodes[i]->hdisplay;
1608 modeline_height = vidmodes[i]->vdisplay;
1609 j = i;
1612 mp_tmsg(MSGT_VO, MSGL_INFO, "XF86VM: Selected video mode %dx%d for image size %dx%d.\n",
1613 modeline_width, modeline_height, X, Y);
1614 XF86VidModeLockModeSwitch(mDisplay, x11->screen, 0);
1615 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1616 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1618 // FIXME: all this is more of a hack than proper solution
1619 X = (opts->vo_screenwidth - modeline_width) / 2;
1620 Y = (opts->vo_screenheight - modeline_height) / 2;
1621 XF86VidModeSetViewPort(mDisplay, x11->screen, X, Y);
1622 vo->dx = X;
1623 vo->dy = Y;
1624 vo->dwidth = modeline_width;
1625 vo->dheight = modeline_height;
1626 aspect_save_screenres(vo, modeline_width, modeline_height);
1630 void vo_vm_close(struct vo *vo)
1632 Display *dpy = vo->x11->display;
1633 struct MPOpts *opts = vo->opts;
1634 if (vidmodes != NULL)
1636 int i, modecount;
1638 free(vidmodes);
1639 vidmodes = NULL;
1640 XF86VidModeGetAllModeLines(dpy, vo->x11->screen, &modecount,
1641 &vidmodes);
1642 for (i = 0; i < modecount; i++)
1643 if ((vidmodes[i]->hdisplay == opts->vo_screenwidth)
1644 && (vidmodes[i]->vdisplay == opts->vo_screenheight))
1646 mp_msg(MSGT_VO, MSGL_INFO,
1647 "Returning to original mode %dx%d\n",
1648 opts->vo_screenwidth, opts->vo_screenheight);
1649 break;
1652 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1653 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1654 free(vidmodes);
1655 vidmodes = NULL;
1659 double vo_vm_get_fps(struct vo *vo)
1661 struct vo_x11_state *x11 = vo->x11;
1662 int clock;
1663 XF86VidModeModeLine modeline;
1664 if (!XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline))
1665 return 0;
1666 if (modeline.privsize)
1667 XFree(modeline.private);
1668 return 1e3 * clock / modeline.htotal / modeline.vtotal;
1670 #endif
1672 #endif /* X11_FULLSCREEN */
1676 * Scan the available visuals on this Display/Screen. Try to find
1677 * the 'best' available TrueColor visual that has a decent color
1678 * depth (at least 15bit). If there are multiple visuals with depth
1679 * >= 15bit, we prefer visuals with a smaller color depth.
1681 int vo_find_depth_from_visuals(Display * dpy, int screen,
1682 Visual ** visual_return)
1684 XVisualInfo visual_tmpl;
1685 XVisualInfo *visuals;
1686 int nvisuals, i;
1687 int bestvisual = -1;
1688 int bestvisual_depth = -1;
1690 visual_tmpl.screen = screen;
1691 visual_tmpl.class = TrueColor;
1692 visuals = XGetVisualInfo(dpy,
1693 VisualScreenMask | VisualClassMask,
1694 &visual_tmpl, &nvisuals);
1695 if (visuals != NULL)
1697 for (i = 0; i < nvisuals; i++)
1699 mp_msg(MSGT_VO, MSGL_V,
1700 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1701 visuals[i].visualid, visuals[i].depth,
1702 visuals[i].red_mask, visuals[i].green_mask,
1703 visuals[i].blue_mask);
1705 * Save the visual index and its depth, if this is the first
1706 * truecolor visul, or a visual that is 'preferred' over the
1707 * previous 'best' visual.
1709 if (bestvisual_depth == -1
1710 || (visuals[i].depth >= 15
1711 && (visuals[i].depth < bestvisual_depth
1712 || bestvisual_depth < 15)))
1714 bestvisual = i;
1715 bestvisual_depth = visuals[i].depth;
1719 if (bestvisual != -1 && visual_return != NULL)
1720 *visual_return = visuals[bestvisual].visual;
1722 XFree(visuals);
1724 return bestvisual_depth;
1728 static Colormap cmap = None;
1729 static XColor cols[256];
1730 static int cm_size, red_mask, green_mask, blue_mask;
1733 Colormap vo_x11_create_colormap(struct vo *vo, XVisualInfo *vinfo)
1735 struct vo_x11_state *x11 = vo->x11;
1736 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1738 if (vinfo->class != DirectColor)
1739 return XCreateColormap(x11->display, x11->rootwin, vinfo->visual,
1740 AllocNone);
1742 /* can this function get called twice or more? */
1743 if (cmap)
1744 return cmap;
1745 cm_size = vinfo->colormap_size;
1746 red_mask = vinfo->red_mask;
1747 green_mask = vinfo->green_mask;
1748 blue_mask = vinfo->blue_mask;
1749 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1750 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1751 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1752 rvu = 65536ull * ru / (red_mask + ru);
1753 gvu = 65536ull * gu / (green_mask + gu);
1754 bvu = 65536ull * bu / (blue_mask + bu);
1755 r = g = b = 0;
1756 rv = gv = bv = 0;
1757 m = DoRed | DoGreen | DoBlue;
1758 for (k = 0; k < cm_size; k++)
1760 int t;
1762 cols[k].pixel = r | g | b;
1763 cols[k].red = rv;
1764 cols[k].green = gv;
1765 cols[k].blue = bv;
1766 cols[k].flags = m;
1767 t = (r + ru) & red_mask;
1768 if (t < r)
1769 m &= ~DoRed;
1770 r = t;
1771 t = (g + gu) & green_mask;
1772 if (t < g)
1773 m &= ~DoGreen;
1774 g = t;
1775 t = (b + bu) & blue_mask;
1776 if (t < b)
1777 m &= ~DoBlue;
1778 b = t;
1779 rv += rvu;
1780 gv += gvu;
1781 bv += bvu;
1783 cmap = XCreateColormap(x11->display, x11->rootwin, vinfo->visual, AllocAll);
1784 XStoreColors(x11->display, cmap, cols, cm_size);
1785 return cmap;
1789 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1790 * hue and red/green/blue intensity, but we cannot do saturation.
1791 * Currently only gamma, brightness and contrast are implemented.
1792 * Is there sufficient interest for hue and/or red/green/blue intensity?
1794 /* these values have range [-100,100] and are initially 0 */
1795 static int vo_gamma = 0;
1796 static int vo_brightness = 0;
1797 static int vo_contrast = 0;
1799 static int transform_color(float val,
1800 float brightness, float contrast, float gamma) {
1801 float s = pow(val, gamma);
1802 s = (s - 0.5) * contrast + 0.5;
1803 s += brightness;
1804 if (s < 0)
1805 s = 0;
1806 if (s > 1)
1807 s = 1;
1808 return (unsigned short) (s * 65535);
1811 uint32_t vo_x11_set_equalizer(struct vo *vo, char *name, int value)
1813 float gamma, brightness, contrast;
1814 float rf, gf, bf;
1815 int k;
1818 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1819 * of TrueColor-ed window but be careful:
1820 * Unlike the colormaps, which are private for the X client
1821 * who created them and thus automatically destroyed on client
1822 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1823 * setting and _must_ be restored before the process exits.
1824 * Unforunately when the process crashes (or gets killed
1825 * for some reason) it is impossible to restore the setting,
1826 * and such behaviour could be rather annoying for the users.
1828 if (cmap == None)
1829 return VO_NOTAVAIL;
1831 if (!strcasecmp(name, "brightness"))
1832 vo_brightness = value;
1833 else if (!strcasecmp(name, "contrast"))
1834 vo_contrast = value;
1835 else if (!strcasecmp(name, "gamma"))
1836 vo_gamma = value;
1837 else
1838 return VO_NOTIMPL;
1840 brightness = 0.01 * vo_brightness;
1841 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1842 gamma = pow(2, -0.02 * vo_gamma);
1844 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1845 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
1846 green_mask;
1847 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
1849 /* now recalculate the colormap using the newly set value */
1850 for (k = 0; k < cm_size; k++)
1852 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
1853 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
1854 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
1857 XStoreColors(vo->x11->display, cmap, cols, cm_size);
1858 XFlush(vo->x11->display);
1859 return VO_TRUE;
1862 uint32_t vo_x11_get_equalizer(char *name, int *value)
1864 if (cmap == None)
1865 return VO_NOTAVAIL;
1866 if (!strcasecmp(name, "brightness"))
1867 *value = vo_brightness;
1868 else if (!strcasecmp(name, "contrast"))
1869 *value = vo_contrast;
1870 else if (!strcasecmp(name, "gamma"))
1871 *value = vo_gamma;
1872 else
1873 return VO_NOTIMPL;
1874 return VO_TRUE;
1877 #ifdef CONFIG_XV
1878 int vo_xv_set_eq(struct vo *vo, uint32_t xv_port, char *name, int value)
1880 XvAttribute *attributes;
1881 int i, howmany, xv_atom;
1883 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
1885 /* get available attributes */
1886 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1887 for (i = 0; i < howmany && attributes; i++)
1888 if (attributes[i].flags & XvSettable)
1890 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1891 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1892 then trigger it if it's ok so that the other values are at default upon query */
1893 if (xv_atom != None)
1895 int hue = 0, port_value, port_min, port_max;
1897 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1898 (!strcasecmp(name, "brightness")))
1899 port_value = value;
1900 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1901 (!strcasecmp(name, "contrast")))
1902 port_value = value;
1903 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1904 (!strcasecmp(name, "saturation")))
1905 port_value = value;
1906 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1907 (!strcasecmp(name, "hue")))
1909 port_value = value;
1910 hue = 1;
1911 } else
1912 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1913 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1914 (!strcasecmp(name, "red_intensity")))
1915 port_value = value;
1916 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1917 && (!strcasecmp(name, "green_intensity")))
1918 port_value = value;
1919 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1920 && (!strcasecmp(name, "blue_intensity")))
1921 port_value = value;
1922 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
1923 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
1924 && (!strcasecmp(name, "bt_709")))
1925 port_value = value;
1926 else
1927 continue;
1929 port_min = attributes[i].min_value;
1930 port_max = attributes[i].max_value;
1932 /* nvidia hue workaround */
1933 if (hue && port_min == 0 && port_max == 360)
1935 port_value =
1936 (port_value >=
1937 0) ? (port_value - 100) : (port_value + 100);
1939 // -100 -> min
1940 // 0 -> (max+min)/2
1941 // +100 -> max
1942 port_value =
1943 (port_value + 100) * (port_max - port_min) / 200 +
1944 port_min;
1945 XvSetPortAttribute(vo->x11->display, xv_port, xv_atom, port_value);
1946 return VO_TRUE;
1949 return VO_FALSE;
1952 int vo_xv_get_eq(struct vo *vo, uint32_t xv_port, char *name, int *value)
1955 XvAttribute *attributes;
1956 int i, howmany, xv_atom;
1958 /* get available attributes */
1959 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1960 for (i = 0; i < howmany && attributes; i++)
1961 if (attributes[i].flags & XvGettable)
1963 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1964 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1965 then trigger it if it's ok so that the other values are at default upon query */
1966 if (xv_atom != None)
1968 int val, port_value = 0, port_min, port_max;
1970 XvGetPortAttribute(vo->x11->display, xv_port, xv_atom,
1971 &port_value);
1973 port_min = attributes[i].min_value;
1974 port_max = attributes[i].max_value;
1975 val =
1976 (port_value - port_min) * 200 / (port_max - port_min) -
1977 100;
1979 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1980 (!strcasecmp(name, "brightness")))
1981 *value = val;
1982 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1983 (!strcasecmp(name, "contrast")))
1984 *value = val;
1985 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1986 (!strcasecmp(name, "saturation")))
1987 *value = val;
1988 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1989 (!strcasecmp(name, "hue")))
1991 /* nasty nvidia detect */
1992 if (port_min == 0 && port_max == 360)
1993 *value = (val >= 0) ? (val - 100) : (val + 100);
1994 else
1995 *value = val;
1996 } else
1997 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1998 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1999 (!strcasecmp(name, "red_intensity")))
2000 *value = val;
2001 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2002 && (!strcasecmp(name, "green_intensity")))
2003 *value = val;
2004 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2005 && (!strcasecmp(name, "blue_intensity")))
2006 *value = val;
2007 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
2008 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
2009 && (!strcasecmp(name, "bt_709")))
2010 *value = val;
2011 else
2012 continue;
2014 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2015 name, *value);
2016 return VO_TRUE;
2019 return VO_FALSE;
2023 * \brief Interns the requested atom if it is available.
2025 * \param atom_name String containing the name of the requested atom.
2027 * \return Returns the atom if available, else None is returned.
2030 static Atom xv_intern_atom_if_exists(struct vo_x11_state *x11,
2031 char const *atom_name)
2033 XvAttribute * attributes;
2034 int attrib_count,i;
2035 Atom xv_atom = None;
2037 attributes = XvQueryPortAttributes(x11->display, x11->xv_port, &attrib_count );
2038 if( attributes!=NULL )
2040 for ( i = 0; i < attrib_count; ++i )
2042 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2044 xv_atom = XInternAtom(x11->display, atom_name, False );
2045 break; // found what we want, break out
2048 XFree( attributes );
2051 return xv_atom;
2055 * \brief Try to enable vsync for xv.
2056 * \return Returns -1 if not available, 0 on failure and 1 on success.
2058 int vo_xv_enable_vsync(struct vo *vo)
2060 struct vo_x11_state *x11 = vo->x11;
2061 Atom xv_atom = xv_intern_atom_if_exists(x11, "XV_SYNC_TO_VBLANK");
2062 if (xv_atom == None)
2063 return -1;
2064 return XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1) == Success;
2068 * \brief Get maximum supported source image dimensions.
2070 * This function does not set the variables pointed to by
2071 * width and height if the information could not be retrieved,
2072 * so the caller is reponsible for properly initializing them.
2074 * \param width [out] The maximum width gets stored here.
2075 * \param height [out] The maximum height gets stored here.
2078 void vo_xv_get_max_img_dim(struct vo *vo, uint32_t * width, uint32_t * height)
2080 struct vo_x11_state *x11 = vo->x11;
2081 XvEncodingInfo * encodings;
2082 //unsigned long num_encodings, idx; to int or too long?!
2083 unsigned int num_encodings, idx;
2085 XvQueryEncodings(x11->display, x11->xv_port, &num_encodings, &encodings);
2087 if ( encodings )
2089 for ( idx = 0; idx < num_encodings; ++idx )
2091 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2093 *width = encodings[idx].width;
2094 *height = encodings[idx].height;
2095 break;
2100 mp_msg( MSGT_VO, MSGL_V,
2101 "[xv common] Maximum source image dimensions: %ux%u\n",
2102 *width, *height );
2104 XvFreeEncodingInfo( encodings );
2108 * \brief Print information about the colorkey method and source.
2110 * \param ck_handling Integer value containing the information about
2111 * colorkey handling (see x11_common.h).
2113 * Outputs the content of |ck_handling| as a readable message.
2116 static void vo_xv_print_ck_info(struct vo_x11_state *x11)
2118 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2120 switch ( x11->xv_ck_info.method )
2122 case CK_METHOD_NONE:
2123 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2124 case CK_METHOD_AUTOPAINT:
2125 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2126 case CK_METHOD_MANUALFILL:
2127 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2128 case CK_METHOD_BACKGROUND:
2129 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2132 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2134 switch ( x11->xv_ck_info.source )
2136 case CK_SRC_CUR:
2137 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2138 x11->xv_colorkey );
2139 break;
2140 case CK_SRC_USE:
2141 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2143 mp_msg( MSGT_VO, MSGL_V,
2144 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2145 x11->xv_colorkey );
2147 else
2149 mp_msg( MSGT_VO, MSGL_V,
2150 "Using colorkey from MPlayer (0x%06lx)."
2151 " Use -colorkey to change.\n",
2152 x11->xv_colorkey );
2154 break;
2155 case CK_SRC_SET:
2156 mp_msg( MSGT_VO, MSGL_V,
2157 "Setting and using colorkey from MPlayer (0x%06lx)."
2158 " Use -colorkey to change.\n",
2159 x11->xv_colorkey );
2160 break;
2164 * \brief Init colorkey depending on the settings in xv_ck_info.
2166 * \return Returns 0 on failure and 1 on success.
2168 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2169 * flags in xv_ck_info.
2171 * Possiblilities:
2172 * * Methods
2173 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2174 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2175 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2176 * * Sources
2177 * - use currently set colorkey ( CK_SRC_CUR )
2178 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2179 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2181 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2182 * we don't draw anything as this means it was forced to off.
2184 int vo_xv_init_colorkey(struct vo *vo)
2186 struct vo_x11_state *x11 = vo->x11;
2187 Atom xv_atom;
2188 int rez;
2190 /* check if colorkeying is needed */
2191 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_COLORKEY");
2193 /* if we have to deal with colorkeying ... */
2194 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2196 /* check if we should use the colorkey specified in vo_colorkey */
2197 if ( x11->xv_ck_info.source != CK_SRC_CUR )
2199 x11->xv_colorkey = vo_colorkey;
2201 /* check if we have to set the colorkey too */
2202 if ( x11->xv_ck_info.source == CK_SRC_SET )
2204 xv_atom = XInternAtom(x11->display, "XV_COLORKEY",False);
2206 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, vo_colorkey);
2207 if ( rez != Success )
2209 mp_msg( MSGT_VO, MSGL_FATAL,
2210 "[xv common] Couldn't set colorkey!\n" );
2211 return 0; // error setting colorkey
2215 else
2217 int colorkey_ret;
2219 rez=XvGetPortAttribute(x11->display,x11->xv_port, xv_atom, &colorkey_ret);
2220 if ( rez == Success )
2222 x11->xv_colorkey = colorkey_ret;
2224 else
2226 mp_msg( MSGT_VO, MSGL_FATAL,
2227 "[xv common] Couldn't get colorkey!"
2228 "Maybe the selected Xv port has no overlay.\n" );
2229 return 0; // error getting colorkey
2233 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_AUTOPAINT_COLORKEY");
2235 /* should we draw the colorkey ourselves or activate autopainting? */
2236 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2238 rez = !Success; // reset rez to something different than Success
2240 if ( xv_atom != None ) // autopaint is supported
2242 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1);
2245 if ( rez != Success )
2247 // fallback to manual colorkey drawing
2248 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2251 else // disable colorkey autopainting if supported
2253 if ( xv_atom != None ) // we have autopaint attribute
2255 XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 0);
2259 else // do no colorkey drawing at all
2261 x11->xv_ck_info.method = CK_METHOD_NONE;
2262 } /* end: should we draw colorkey */
2264 /* output information about the current colorkey settings */
2265 vo_xv_print_ck_info(x11);
2267 return 1; // success
2271 * \brief Draw the colorkey on the video window.
2273 * Draws the colorkey depending on the set method ( colorkey_handling ).
2275 * Also draws the black bars ( when the video doesn't fit the display in
2276 * fullscreen ) separately, so they don't overlap with the video area.
2277 * It doesn't call XFlush.
2280 void vo_xv_draw_colorkey(struct vo *vo, int32_t x, int32_t y,
2281 int32_t w, int32_t h)
2283 struct MPOpts *opts = vo->opts;
2284 struct vo_x11_state *x11 = vo->x11;
2285 if( x11->xv_ck_info.method == CK_METHOD_MANUALFILL ||
2286 x11->xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2288 XSetForeground(x11->display, x11->vo_gc, x11->xv_colorkey );
2289 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2290 x, y,
2291 w, h );
2294 /* draw black bars if needed */
2295 /* TODO! move this to vo_x11_clearwindow_part() */
2296 if ( vo_fs )
2298 XSetForeground(x11->display, x11->vo_gc, 0 );
2299 /* making non-overlap fills, requires 8 checks instead of 4 */
2300 if ( y > 0 )
2301 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2302 0, 0,
2303 opts->vo_screenwidth, y);
2304 if (x > 0)
2305 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2306 0, 0,
2307 x, opts->vo_screenheight);
2308 if (x + w < opts->vo_screenwidth)
2309 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2310 x + w, 0,
2311 opts->vo_screenwidth, opts->vo_screenheight);
2312 if (y + h < opts->vo_screenheight)
2313 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2314 0, y + h,
2315 opts->vo_screenwidth, opts->vo_screenheight);
2319 /** \brief Tests if a valid argument for the ck suboption was given. */
2320 int xv_test_ck( void * arg )
2322 strarg_t * strarg = (strarg_t *)arg;
2324 if ( strargcmp( strarg, "use" ) == 0 ||
2325 strargcmp( strarg, "set" ) == 0 ||
2326 strargcmp( strarg, "cur" ) == 0 )
2328 return 1;
2331 return 0;
2333 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2334 int xv_test_ckm( void * arg )
2336 strarg_t * strarg = (strarg_t *)arg;
2338 if ( strargcmp( strarg, "bg" ) == 0 ||
2339 strargcmp( strarg, "man" ) == 0 ||
2340 strargcmp( strarg, "auto" ) == 0 )
2342 return 1;
2345 return 0;
2349 * \brief Modify the colorkey_handling var according to str
2351 * Checks if a valid pointer ( not NULL ) to the string
2352 * was given. And in that case modifies the colorkey_handling
2353 * var to reflect the requested behaviour.
2354 * If nothing happens the content of colorkey_handling stays
2355 * the same.
2357 * \param str Pointer to the string or NULL
2360 void xv_setup_colorkeyhandling(struct vo *vo, const char *ck_method_str,
2361 const char *ck_str)
2363 struct vo_x11_state *x11 = vo->x11;
2364 /* check if a valid pointer to the string was passed */
2365 if ( ck_str )
2367 if ( strncmp( ck_str, "use", 3 ) == 0 )
2369 x11->xv_ck_info.source = CK_SRC_USE;
2371 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2373 x11->xv_ck_info.source = CK_SRC_SET;
2376 /* check if a valid pointer to the string was passed */
2377 if ( ck_method_str )
2379 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2381 x11->xv_ck_info.method = CK_METHOD_BACKGROUND;
2383 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2385 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2387 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2389 x11->xv_ck_info.method = CK_METHOD_AUTOPAINT;
2394 #endif
2396 struct vo_x11_state *vo_x11_init_state(void)
2398 struct vo_x11_state *s = talloc_ptrtype(NULL, s);
2399 *s = (struct vo_x11_state){
2400 .xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR },
2401 .olddecor = MWM_DECOR_ALL,
2402 .oldfuncs = MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
2403 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE,
2404 .old_gravity = NorthWestGravity,
2406 return s;