vo_gl3: call glFlush() after frame drawing is complete
[mplayer.git] / libvo / x11_common.c
blob7caa5c6a9092c0846a1302f9c51ebd5a7084fa47
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 "bstr.h"
27 #include "options.h"
28 #include "mp_msg.h"
29 #include "mp_fifo.h"
30 #include "libavutil/common.h"
31 #include "x11_common.h"
32 #include "talloc.h"
34 #include <string.h>
35 #include <unistd.h>
36 #include <assert.h>
38 #include "video_out.h"
39 #include "aspect.h"
40 #include "geometry.h"
41 #include "osdep/timer.h"
43 #include <X11/Xmd.h>
44 #include <X11/Xlib.h>
45 #include <X11/Xutil.h>
46 #include <X11/Xatom.h>
47 #include <X11/keysym.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/keycodes.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 available");
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(_NET_WM_NAME);
331 XA_INIT(_NET_WM_ICON_NAME);
332 XA_INIT(_WIN_PROTOCOLS);
333 XA_INIT(_WIN_LAYER);
334 XA_INIT(_WIN_HINTS);
335 XA_INIT(WM_PROTOCOLS);
336 XA_INIT(WM_DELETE_WINDOW);
337 XA_INIT(UTF8_STRING);
338 char buf[50];
339 sprintf(buf, "_NET_WM_CM_S%d", x11->screen);
340 x11->XA_NET_WM_CM = XInternAtom(x11->display, buf, False);
343 void update_xinerama_info(struct vo *vo) {
344 struct MPOpts *opts = vo->opts;
345 xinerama_x = xinerama_y = 0;
346 #ifdef CONFIG_XINERAMA
347 if (xinerama_screen >= -1 && XineramaIsActive(vo->x11->display))
349 int screen = xinerama_screen;
350 XineramaScreenInfo *screens;
351 int num_screens;
353 screens = XineramaQueryScreens(vo->x11->display, &num_screens);
354 if (screen >= num_screens)
355 screen = num_screens - 1;
356 if (screen == -1) {
357 int x = vo->dx + vo->dwidth / 2;
358 int y = vo->dy + vo->dheight / 2;
359 for (screen = num_screens - 1; screen > 0; screen--) {
360 int left = screens[screen].x_org;
361 int right = left + screens[screen].width;
362 int top = screens[screen].y_org;
363 int bottom = top + screens[screen].height;
364 if (left <= x && x <= right && top <= y && y <= bottom)
365 break;
368 if (screen < 0)
369 screen = 0;
370 opts->vo_screenwidth = screens[screen].width;
371 opts->vo_screenheight = screens[screen].height;
372 xinerama_x = screens[screen].x_org;
373 xinerama_y = screens[screen].y_org;
375 XFree(screens);
377 #endif
378 aspect_save_screenres(vo, opts->vo_screenwidth, opts->vo_screenheight);
381 int vo_init(struct vo *vo)
383 struct MPOpts *opts = vo->opts;
385 char *dispName;
387 assert(vo->x11 == NULL);
389 vo->x11 = vo_x11_init_state();
390 struct vo_x11_state *x11 = vo->x11;
392 if (vo_rootwin)
393 WinID = 0; // use root window
395 XSetErrorHandler(x11_errorhandler);
397 #if 0
398 if (!mDisplayName)
399 if (!(mDisplayName = getenv("DISPLAY")))
400 mDisplayName = strdup(":0.0");
401 #else
402 dispName = XDisplayName(mDisplayName);
403 #endif
405 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
407 x11->display = XOpenDisplay(dispName);
408 if (!x11->display)
410 mp_msg(MSGT_VO, MSGL_ERR,
411 "vo: couldn't open the X11 display (%s)!\n", dispName);
412 talloc_free(x11);
413 vo->x11 = NULL;
414 return 0;
416 x11->screen = DefaultScreen(x11->display); // screen ID
417 x11->rootwin = RootWindow(x11->display, x11->screen); // root window ID
419 x11->xim = XOpenIM(x11->display, NULL, NULL, NULL);
421 init_atoms(vo->x11);
423 #ifdef CONFIG_XF86VM
425 int clock;
427 XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline);
428 if (!opts->vo_screenwidth)
429 opts->vo_screenwidth = modeline.hdisplay;
430 if (!opts->vo_screenheight)
431 opts->vo_screenheight = modeline.vdisplay;
433 #endif
435 if (!opts->vo_screenwidth)
436 opts->vo_screenwidth = DisplayWidth(x11->display, x11->screen);
437 if (!opts->vo_screenheight)
438 opts->vo_screenheight = DisplayHeight(x11->display, x11->screen);
440 // get color depth (from root window, or the best visual):
441 XWindowAttributes attribs;
442 XGetWindowAttributes(x11->display, x11->rootwin, &attribs);
444 // XCloseDisplay( mDisplay );
445 /* slightly improved local display detection AST */
446 if (strncmp(dispName, "unix:", 5) == 0)
447 dispName += 4;
448 else if (strncmp(dispName, "localhost:", 10) == 0)
449 dispName += 9;
450 if (*dispName == ':' && atoi(dispName + 1) < 10)
451 x11->display_is_local = 1;
452 else
453 x11->display_is_local = 0;
454 mp_msg(MSGT_VO, MSGL_V,
455 "vo: X11 running at %dx%d with depth %d (\"%s\" => %s display)\n",
456 opts->vo_screenwidth, opts->vo_screenheight, attribs.depth,
457 dispName, x11->display_is_local ? "local" : "remote");
459 x11->wm_type = vo_wm_detect(vo);
461 x11->fs_type = vo_x11_get_fs_type(x11->wm_type);
463 fstype_dump(x11->fs_type);
465 saver_off(x11->display);
466 return 1;
469 void vo_uninit(struct vo_x11_state *x11)
471 if (!x11)
472 return;
473 if (!x11->display)
475 mp_msg(MSGT_VO, MSGL_V,
476 "vo: x11 uninit called but X11 not initialized..\n");
477 } else {
478 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
479 if (x11->xim)
480 XCloseIM(x11->xim);
481 XSetErrorHandler(NULL);
482 XCloseDisplay(x11->display);
483 x11->display = NULL;
485 talloc_free(x11);
488 static const struct mp_keymap keymap[] = {
489 // special keys
490 {XK_Pause, KEY_PAUSE}, {XK_Escape, KEY_ESC}, {XK_BackSpace, KEY_BS},
491 {XK_Tab, KEY_TAB}, {XK_Return, KEY_ENTER},
492 {XK_Menu, KEY_MENU}, {XK_Print, KEY_PRINT},
494 // cursor keys
495 {XK_Left, KEY_LEFT}, {XK_Right, KEY_RIGHT}, {XK_Up, KEY_UP}, {XK_Down, KEY_DOWN},
497 // navigation block
498 {XK_Insert, KEY_INSERT}, {XK_Delete, KEY_DELETE}, {XK_Home, KEY_HOME}, {XK_End, KEY_END},
499 {XK_Page_Up, KEY_PAGE_UP}, {XK_Page_Down, KEY_PAGE_DOWN},
501 // F-keys
502 {XK_F1, KEY_F+1}, {XK_F2, KEY_F+2}, {XK_F3, KEY_F+3}, {XK_F4, KEY_F+4},
503 {XK_F5, KEY_F+5}, {XK_F6, KEY_F+6}, {XK_F7, KEY_F+7}, {XK_F8, KEY_F+8},
504 {XK_F9, KEY_F+9}, {XK_F10, KEY_F+10}, {XK_F11, KEY_F+11}, {XK_F12, KEY_F+12},
506 // numpad independent of numlock
507 {XK_KP_Subtract, '-'}, {XK_KP_Add, '+'}, {XK_KP_Multiply, '*'}, {XK_KP_Divide, '/'},
508 {XK_KP_Enter, KEY_KPENTER},
510 // numpad with numlock
511 {XK_KP_0, KEY_KP0}, {XK_KP_1, KEY_KP1}, {XK_KP_2, KEY_KP2},
512 {XK_KP_3, KEY_KP3}, {XK_KP_4, KEY_KP4}, {XK_KP_5, KEY_KP5},
513 {XK_KP_6, KEY_KP6}, {XK_KP_7, KEY_KP7}, {XK_KP_8, KEY_KP8},
514 {XK_KP_9, KEY_KP9}, {XK_KP_Decimal, KEY_KPDEC},
515 {XK_KP_Separator, KEY_KPDEC},
517 // numpad without numlock
518 {XK_KP_Insert, KEY_KPINS}, {XK_KP_End, KEY_KP1}, {XK_KP_Down, KEY_KP2},
519 {XK_KP_Page_Down, KEY_KP3}, {XK_KP_Left, KEY_KP4}, {XK_KP_Begin, KEY_KP5},
520 {XK_KP_Right, KEY_KP6}, {XK_KP_Home, KEY_KP7}, {XK_KP_Up, KEY_KP8},
521 {XK_KP_Page_Up, KEY_KP9}, {XK_KP_Delete, KEY_KPDEL},
523 #ifdef XF86XK_AudioPause
524 {XF86XK_MenuKB, KEY_MENU},
525 {XF86XK_AudioPlay, KEY_PLAY}, {XF86XK_AudioPause, KEY_PAUSE}, {XF86XK_AudioStop, KEY_STOP},
526 {XF86XK_AudioPrev, KEY_PREV}, {XF86XK_AudioNext, KEY_NEXT},
527 {XF86XK_AudioMute, KEY_MUTE}, {XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN}, {XF86XK_AudioRaiseVolume, KEY_VOLUME_UP},
528 #endif
530 {0, 0}
533 static int vo_x11_lookupkey(int key)
535 static const char *passthrough_keys = " -+*/<>`~!@#$%^&()_{}:;\"\',.?\\|=[]";
536 int mpkey = 0;
537 if ((key >= 'a' && key <= 'z') ||
538 (key >= 'A' && key <= 'Z') ||
539 (key >= '0' && key <= '9') ||
540 (key > 0 && key < 256 && strchr(passthrough_keys, key)))
541 mpkey = key;
543 if (!mpkey)
544 mpkey = lookup_keymap_table(keymap, key);
546 return mpkey;
550 // ----- Motif header: -------
552 #define MWM_HINTS_FUNCTIONS (1L << 0)
553 #define MWM_HINTS_DECORATIONS (1L << 1)
554 #define MWM_HINTS_INPUT_MODE (1L << 2)
555 #define MWM_HINTS_STATUS (1L << 3)
557 #define MWM_FUNC_ALL (1L << 0)
558 #define MWM_FUNC_RESIZE (1L << 1)
559 #define MWM_FUNC_MOVE (1L << 2)
560 #define MWM_FUNC_MINIMIZE (1L << 3)
561 #define MWM_FUNC_MAXIMIZE (1L << 4)
562 #define MWM_FUNC_CLOSE (1L << 5)
564 #define MWM_DECOR_ALL (1L << 0)
565 #define MWM_DECOR_BORDER (1L << 1)
566 #define MWM_DECOR_RESIZEH (1L << 2)
567 #define MWM_DECOR_TITLE (1L << 3)
568 #define MWM_DECOR_MENU (1L << 4)
569 #define MWM_DECOR_MINIMIZE (1L << 5)
570 #define MWM_DECOR_MAXIMIZE (1L << 6)
572 #define MWM_INPUT_MODELESS 0
573 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
574 #define MWM_INPUT_SYSTEM_MODAL 2
575 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
576 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
578 #define MWM_TEAROFF_WINDOW (1L<<0)
580 typedef struct
582 long flags;
583 long functions;
584 long decorations;
585 long input_mode;
586 long state;
587 } MotifWmHints;
589 static MotifWmHints vo_MotifWmHints;
590 static Atom vo_MotifHints = None;
592 void vo_x11_decoration(struct vo *vo, int d)
594 struct vo_x11_state *x11 = vo->x11;
595 Atom mtype;
596 int mformat;
597 unsigned long mn, mb;
599 if (!WinID)
600 return;
602 if (vo_fsmode & 8)
604 XSetTransientForHint(x11->display, x11->window,
605 RootWindow(x11->display, x11->screen));
608 vo_MotifHints = XInternAtom(x11->display, "_MOTIF_WM_HINTS", 0);
609 if (vo_MotifHints != None)
611 if (!d)
613 MotifWmHints *mhints = NULL;
615 XGetWindowProperty(x11->display, x11->window,
616 vo_MotifHints, 0, 20, False,
617 vo_MotifHints, &mtype, &mformat, &mn,
618 &mb, (unsigned char **) &mhints);
619 if (mhints)
621 if (mhints->flags & MWM_HINTS_DECORATIONS)
622 x11->olddecor = mhints->decorations;
623 if (mhints->flags & MWM_HINTS_FUNCTIONS)
624 x11->oldfuncs = mhints->functions;
625 XFree(mhints);
629 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
630 vo_MotifWmHints.flags =
631 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
632 if (d)
634 vo_MotifWmHints.functions = x11->oldfuncs;
635 d = x11->olddecor;
637 #if 0
638 vo_MotifWmHints.decorations =
639 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
640 #else
641 vo_MotifWmHints.decorations =
642 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
643 #endif
644 XChangeProperty(x11->display, x11->window, vo_MotifHints,
645 vo_MotifHints, 32,
646 PropModeReplace,
647 (unsigned char *) &vo_MotifWmHints,
648 (vo_fsmode & 4) ? 4 : 5);
652 void vo_x11_classhint(struct vo *vo, Window window, const char *name)
654 struct MPOpts *opts = vo->opts;
655 struct vo_x11_state *x11 = vo->x11;
656 XClassHint wmClass;
657 pid_t pid = getpid();
659 wmClass.res_name = opts->vo_winname ? opts->vo_winname : (char *)name;
660 wmClass.res_class = "mplayer2";
661 XSetClassHint(x11->display, window, &wmClass);
662 XChangeProperty(x11->display, window, x11->XA_NET_WM_PID, XA_CARDINAL,
663 32, PropModeReplace, (unsigned char *) &pid, 1);
666 void vo_x11_uninit(struct vo *vo)
668 struct vo_x11_state *x11 = vo->x11;
669 saver_on(x11->display);
670 if (x11->window != None)
671 vo_showcursor(x11->display, x11->window);
673 if (x11->f_gc != None)
675 XFreeGC(vo->x11->display, x11->f_gc);
676 x11->f_gc = None;
679 if (x11->vo_gc != None)
681 XFreeGC(vo->x11->display, x11->vo_gc);
682 x11->vo_gc = None;
684 if (x11->window != None)
686 XClearWindow(x11->display, x11->window);
687 if (WinID < 0)
689 XEvent xev;
691 if (x11->xic)
692 XDestroyIC(x11->xic);
693 x11->xic = NULL;
695 XUnmapWindow(x11->display, x11->window);
696 XSelectInput(x11->display, x11->window, StructureNotifyMask);
697 XDestroyWindow(x11->display, x11->window);
700 XNextEvent(x11->display, &xev);
702 while (xev.type != DestroyNotify
703 || xev.xdestroywindow.event != x11->window);
705 x11->window = None;
707 vo_fs = 0;
708 x11->vo_old_width = x11->vo_old_height = 0;
709 x11->last_video_width = 0;
710 x11->last_video_height = 0;
711 x11->size_changed_during_fs = false;
713 vo_uninit(x11);
714 vo->x11 = NULL;
717 static int check_resize(struct vo *vo)
719 int old_w = vo->dwidth, old_h = vo->dheight;
720 int old_x = vo->dx, old_y = vo->dy;
721 int rc = 0;
722 vo_x11_update_geometry(vo, true);
723 if (vo->dwidth != old_w || vo->dheight != old_h)
724 rc |= VO_EVENT_RESIZE;
725 if (vo->dx != old_x || vo->dy != old_y)
726 rc |= VO_EVENT_MOVE;
727 return rc;
730 int vo_x11_check_events(struct vo *vo)
732 struct vo_x11_state *x11 = vo->x11;
733 struct MPOpts *opts = vo->opts;
734 Display *display = vo->x11->display;
735 int ret = 0;
736 XEvent Event;
738 if (x11->mouse_waiting_hide && opts->cursor_autohide_delay != -1 &&
739 (GetTimerMS() - x11->mouse_timer >= opts->cursor_autohide_delay)) {
740 vo_hidecursor(display, x11->window);
741 x11->mouse_waiting_hide = 0;
744 if (WinID > 0)
745 ret |= check_resize(vo);
746 while (XPending(display))
748 XNextEvent(display, &Event);
749 // printf("\rEvent.type=%X \n",Event.type);
750 switch (Event.type)
752 case Expose:
753 ret |= VO_EVENT_EXPOSE;
754 break;
755 case ConfigureNotify:
756 if (x11->window == None)
757 break;
758 ret |= check_resize(vo);
759 break;
760 case KeyPress:
762 char buf[100];
763 KeySym keySym = 0;
764 int modifiers = 0;
765 if (Event.xkey.state & ShiftMask)
766 modifiers |= KEY_MODIFIER_SHIFT;
767 if (Event.xkey.state & ControlMask)
768 modifiers |= KEY_MODIFIER_CTRL;
769 if (Event.xkey.state & Mod1Mask)
770 modifiers |= KEY_MODIFIER_ALT;
771 if (Event.xkey.state & Mod4Mask)
772 modifiers |= KEY_MODIFIER_META;
773 if (x11->xic) {
774 Status status;
775 int len = Xutf8LookupString(x11->xic, &Event.xkey, buf,
776 sizeof(buf), &keySym,
777 &status);
778 int mpkey = vo_x11_lookupkey(keySym);
779 if (mpkey) {
780 mplayer_put_key(vo->key_fifo, mpkey | modifiers);
781 } else if (status == XLookupChars
782 || status == XLookupBoth)
784 struct bstr t = { buf, len };
785 mplayer_put_key_utf8(vo->key_fifo, modifiers, t);
787 } else {
788 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
789 &x11->compose_status);
790 int mpkey = vo_x11_lookupkey(keySym);
791 if (mpkey)
792 mplayer_put_key(vo->key_fifo, mpkey | modifiers);
794 ret |= VO_EVENT_KEYPRESS;
796 break;
797 case MotionNotify:
798 vo_mouse_movement(vo, Event.xmotion.x, Event.xmotion.y);
800 if (opts->cursor_autohide_delay > -2) {
801 vo_showcursor(display, x11->window);
802 x11->mouse_waiting_hide = 1;
803 x11->mouse_timer = GetTimerMS();
805 break;
806 case ButtonPress:
807 if (opts->cursor_autohide_delay > -2) {
808 vo_showcursor(display, x11->window);
809 x11->mouse_waiting_hide = 1;
810 x11->mouse_timer = GetTimerMS();
812 mplayer_put_key(vo->key_fifo,
813 (MOUSE_BTN0 + Event.xbutton.button - 1)
814 | MP_KEY_DOWN);
815 break;
816 case ButtonRelease:
817 if (opts->cursor_autohide_delay > -2) {
818 vo_showcursor(display, x11->window);
819 x11->mouse_waiting_hide = 1;
820 x11->mouse_timer = GetTimerMS();
822 mplayer_put_key(vo->key_fifo,
823 MOUSE_BTN0 + Event.xbutton.button - 1);
824 break;
825 case PropertyNotify:
827 char *name =
828 XGetAtomName(display, Event.xproperty.atom);
830 if (!name)
831 break;
833 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
835 XFree(name);
837 break;
838 case MapNotify:
839 x11->vo_hint.win_gravity = x11->old_gravity;
840 XSetWMNormalHints(display, x11->window, &x11->vo_hint);
841 x11->fs_flip = 0;
842 break;
843 case DestroyNotify:
844 mp_msg(MSGT_VO, MSGL_WARN, "Our window was destroyed, exiting\n");
845 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
846 break;
847 case ClientMessage:
848 if (Event.xclient.message_type == x11->XAWM_PROTOCOLS &&
849 Event.xclient.data.l[0] == x11->XAWM_DELETE_WINDOW)
850 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
851 break;
854 return ret;
858 * \brief sets the size and position of the non-fullscreen window.
860 static void vo_x11_nofs_sizepos(struct vo *vo, int x, int y,
861 int width, int height)
863 struct vo_x11_state *x11 = vo->x11;
864 if (width == x11->last_video_width && height == x11->last_video_height) {
865 if (!vo->opts->force_window_position && !x11->size_changed_during_fs)
866 return;
867 } else if (vo_fs)
868 x11->size_changed_during_fs = true;
869 x11->last_video_height = height;
870 x11->last_video_width = width;
871 vo_x11_sizehint(vo, x, y, width, height, 0);
872 if (vo_fs) {
873 x11->vo_old_x = x;
874 x11->vo_old_y = y;
875 x11->vo_old_width = width;
876 x11->vo_old_height = height;
878 else
880 vo->dwidth = width;
881 vo->dheight = height;
882 if (vo->opts->force_window_position)
883 XMoveResizeWindow(vo->x11->display, vo->x11->window, x, y, width,
884 height);
885 else
886 XResizeWindow(vo->x11->display, vo->x11->window, width, height);
890 void vo_x11_sizehint(struct vo *vo, int x, int y, int width, int height, int max)
892 struct vo_x11_state *x11 = vo->x11;
893 x11->vo_hint.flags = 0;
894 if (vo_keepaspect)
896 x11->vo_hint.flags |= PAspect;
897 x11->vo_hint.min_aspect.x = width;
898 x11->vo_hint.min_aspect.y = height;
899 x11->vo_hint.max_aspect.x = width;
900 x11->vo_hint.max_aspect.y = height;
903 x11->vo_hint.flags |= PPosition | PSize;
904 x11->vo_hint.x = x;
905 x11->vo_hint.y = y;
906 x11->vo_hint.width = width;
907 x11->vo_hint.height = height;
908 if (max)
910 x11->vo_hint.flags |= PMaxSize;
911 x11->vo_hint.max_width = width;
912 x11->vo_hint.max_height = height;
913 } else
915 x11->vo_hint.max_width = 0;
916 x11->vo_hint.max_height = 0;
919 // Set minimum height/width to 4 to avoid off-by-one errors.
920 x11->vo_hint.flags |= PMinSize;
921 x11->vo_hint.min_width = x11->vo_hint.min_height = 4;
923 // Set the base size. A window manager might display the window
924 // size to the user relative to this.
925 // Setting these to width/height might be nice, but e.g. fluxbox can't handle it.
926 x11->vo_hint.flags |= PBaseSize;
927 x11->vo_hint.base_width = 0 /*width*/;
928 x11->vo_hint.base_height = 0 /*height*/;
930 x11->vo_hint.flags |= PWinGravity;
931 x11->vo_hint.win_gravity = StaticGravity;
932 XSetWMNormalHints(x11->display, x11->window, &x11->vo_hint);
935 static int vo_x11_get_gnome_layer(struct vo_x11_state *x11, Window win)
937 Atom type;
938 int format;
939 unsigned long nitems;
940 unsigned long bytesafter;
941 unsigned short *args = NULL;
943 if (XGetWindowProperty(x11->display, win, x11->XA_WIN_LAYER, 0, 16384,
944 False, AnyPropertyType, &type, &format, &nitems,
945 &bytesafter,
946 (unsigned char **) &args) == Success
947 && nitems > 0 && args)
949 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
950 *args);
951 return *args;
953 return WIN_LAYER_NORMAL;
956 // set a X text property that expects a UTF8_STRING type
957 static void vo_x11_set_property_utf8(struct vo *vo, Atom name, const char *t)
959 struct vo_x11_state *x11 = vo->x11;
961 XChangeProperty(x11->display, x11->window, name, x11->XAUTF8_STRING, 8,
962 PropModeReplace, t, strlen(t));
965 // set a X text property that expects a STRING or COMPOUND_TEXT type
966 static void vo_x11_set_property_string(struct vo *vo, Atom name, const char *t)
968 struct vo_x11_state *x11 = vo->x11;
969 XTextProperty prop = {0};
971 if (Xutf8TextListToTextProperty(x11->display, (char **)&t, 1,
972 XStdICCTextStyle, &prop) == Success) {
973 XSetTextProperty(x11->display, x11->window, &prop, name);
974 } else {
975 // Strictly speaking this violates the ICCCM, but there's no way we
976 // can do this correctly.
977 vo_x11_set_property_utf8(vo, name, t);
980 if (prop.value)
981 XFree(prop.value);
984 static void vo_x11_update_window_title(struct vo *vo)
986 struct vo_x11_state *x11 = vo->x11;
988 const char *title = vo_get_window_title(vo);
989 vo_x11_set_property_string(vo, XA_WM_NAME, title);
990 vo_x11_set_property_string(vo, XA_WM_ICON_NAME, title);
991 vo_x11_set_property_utf8(vo, x11->XA_NET_WM_NAME, title);
992 vo_x11_set_property_utf8(vo, x11->XA_NET_WM_ICON_NAME, title);
996 static Window vo_x11_create_smooth_window(struct vo_x11_state *x11, Window mRoot,
997 Visual * vis, int x, int y,
998 unsigned int width, unsigned int height,
999 int depth, Colormap col_map)
1001 unsigned long xswamask = CWBorderPixel;
1002 XSetWindowAttributes xswa;
1003 Window ret_win;
1005 if (col_map != CopyFromParent)
1007 xswa.colormap = col_map;
1008 xswamask |= CWColormap;
1010 xswa.background_pixel = 0;
1011 xswa.border_pixel = 0;
1012 xswa.backing_store = NotUseful;
1013 xswa.bit_gravity = StaticGravity;
1015 ret_win =
1016 XCreateWindow(x11->display, x11->rootwin, x, y, width, height, 0, depth,
1017 CopyFromParent, vis, xswamask, &xswa);
1018 XSetWMProtocols(x11->display, ret_win, &x11->XAWM_DELETE_WINDOW, 1);
1019 if (x11->f_gc == None)
1020 x11->f_gc = XCreateGC(x11->display, ret_win, 0, 0);
1021 XSetForeground(x11->display, x11->f_gc, 0);
1023 return ret_win;
1027 * \brief create and setup a window suitable for display
1028 * \param vis Visual to use for creating the window
1029 * \param x x position of window
1030 * \param y y position of window
1031 * \param width width of window
1032 * \param height height of window
1033 * \param flags flags for window creation.
1034 * Only VOFLAG_FULLSCREEN is supported so far.
1035 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1036 * \param classname name to use for the classhint
1038 * This also does the grunt-work like setting Window Manager hints etc.
1039 * If vo_window is already set it just moves and resizes it.
1041 void vo_x11_create_vo_window(struct vo *vo, XVisualInfo *vis, int x, int y,
1042 unsigned int width, unsigned int height, int flags,
1043 Colormap col_map, const char *classname)
1045 struct MPOpts *opts = vo->opts;
1046 struct vo_x11_state *x11 = vo->x11;
1047 Display *mDisplay = vo->x11->display;
1048 if (WinID >= 0) {
1049 vo_fs = flags & VOFLAG_FULLSCREEN;
1050 x11->window = WinID ? (Window)WinID : x11->rootwin;
1051 if (col_map != CopyFromParent) {
1052 unsigned long xswamask = CWColormap;
1053 XSetWindowAttributes xswa;
1054 xswa.colormap = col_map;
1055 XChangeWindowAttributes(mDisplay, x11->window, xswamask, &xswa);
1056 XInstallColormap(mDisplay, col_map);
1058 if (WinID) {
1059 // Expose events can only really be handled by us, so request them.
1060 vo_x11_selectinput_witherr(mDisplay, x11->window, ExposureMask);
1061 } else
1062 // Do not capture events since it might break the parent application
1063 // if it relies on events being forwarded to the parent of WinID.
1064 // It also is consistent with the w32_common.c code.
1065 vo_x11_selectinput_witherr(mDisplay, x11->window,
1066 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1067 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1069 vo_x11_update_geometry(vo, true);
1070 goto final;
1072 if (x11->window == None) {
1073 vo_fs = 0;
1074 vo->dwidth = width;
1075 vo->dheight = height;
1076 x11->window = vo_x11_create_smooth_window(x11, x11->rootwin, vis->visual,
1077 x, y, width, height, vis->depth, col_map);
1078 x11->window_state = VOFLAG_HIDDEN;
1080 if (flags & VOFLAG_HIDDEN)
1081 goto final;
1082 if (x11->window_state & VOFLAG_HIDDEN) {
1083 XSizeHints hint;
1084 x11->window_state &= ~VOFLAG_HIDDEN;
1085 vo_x11_classhint(vo, x11->window, classname);
1086 vo_hidecursor(mDisplay, x11->window);
1087 XSelectInput(mDisplay, x11->window, StructureNotifyMask);
1088 hint.x = x; hint.y = y;
1089 hint.width = width; hint.height = height;
1090 hint.flags = PSize;
1091 if (geometry_xy_changed)
1092 hint.flags |= PPosition;
1093 XSetWMNormalHints(mDisplay, x11->window, &hint);
1094 if (!vo_border) vo_x11_decoration(vo, 0);
1095 // map window
1096 x11->xic = XCreateIC(x11->xim,
1097 XNInputStyle, XIMPreeditNone | XIMStatusNone,
1098 XNClientWindow, x11->window,
1099 XNFocusWindow, x11->window,
1100 NULL);
1101 XSelectInput(mDisplay, x11->window, NoEventMask);
1102 vo_x11_selectinput_witherr(mDisplay, x11->window,
1103 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1104 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1105 XMapWindow(mDisplay, x11->window);
1106 vo_x11_clearwindow(vo, x11->window);
1108 vo_x11_update_window_title(vo);
1109 if (opts->vo_ontop) vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1110 vo_x11_update_geometry(vo, !geometry_xy_changed);
1111 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, width, height);
1112 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1113 vo_x11_fullscreen(vo);
1114 else if (vo_fs) {
1115 // if we are already in fullscreen do not switch back and forth, just
1116 // set the size values right.
1117 vo->dwidth = vo->opts->vo_screenwidth;
1118 vo->dheight = vo->opts->vo_screenheight;
1120 final:
1121 if (x11->vo_gc != None)
1122 XFreeGC(mDisplay, x11->vo_gc);
1123 x11->vo_gc = XCreateGC(mDisplay, x11->window, 0, NULL);
1125 XSync(mDisplay, False);
1126 vo->event_fd = ConnectionNumber(x11->display);
1129 void vo_x11_clearwindow_part(struct vo *vo, Window vo_window,
1130 int img_width, int img_height)
1132 struct vo_x11_state *x11 = vo->x11;
1133 Display *mDisplay = vo->x11->display;
1134 int u_dheight, u_dwidth, left_ov, left_ov2;
1136 if (x11->f_gc == None)
1137 return;
1139 u_dheight = vo->dheight;
1140 u_dwidth = vo->dwidth;
1141 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1142 return;
1144 left_ov = (u_dheight - img_height) / 2;
1145 left_ov2 = (u_dwidth - img_width) / 2;
1147 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, 0, u_dwidth, left_ov);
1148 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, u_dheight - left_ov - 1,
1149 u_dwidth, left_ov + 1);
1151 if (u_dwidth > img_width)
1153 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, left_ov, left_ov2,
1154 img_height);
1155 XFillRectangle(mDisplay, vo_window, x11->f_gc, u_dwidth - left_ov2 - 1,
1156 left_ov, left_ov2 + 1, img_height);
1159 XFlush(mDisplay);
1162 void vo_x11_clearwindow(struct vo *vo, Window vo_window)
1164 struct vo_x11_state *x11 = vo->x11;
1165 struct MPOpts *opts = vo->opts;
1166 if (x11->f_gc == None)
1167 return;
1168 XFillRectangle(x11->display, vo_window, x11->f_gc, 0, 0,
1169 opts->vo_screenwidth, opts->vo_screenheight);
1171 XFlush(x11->display);
1175 void vo_x11_setlayer(struct vo *vo, Window vo_window, int layer)
1177 struct vo_x11_state *x11 = vo->x11;
1178 if (WinID >= 0)
1179 return;
1181 if (x11->fs_type & vo_wm_LAYER)
1183 XClientMessageEvent xev;
1185 if (!x11->orig_layer)
1186 x11->orig_layer = vo_x11_get_gnome_layer(x11, vo_window);
1188 memset(&xev, 0, sizeof(xev));
1189 xev.type = ClientMessage;
1190 xev.display = x11->display;
1191 xev.window = vo_window;
1192 xev.message_type = x11->XA_WIN_LAYER;
1193 xev.format = 32;
1194 xev.data.l[0] = layer ? fs_layer : x11->orig_layer; // if not fullscreen, stay on default layer
1195 xev.data.l[1] = CurrentTime;
1196 mp_msg(MSGT_VO, MSGL_V,
1197 "[x11] Layered style stay on top (layer %ld).\n",
1198 xev.data.l[0]);
1199 XSendEvent(x11->display, x11->rootwin, False, SubstructureNotifyMask,
1200 (XEvent *) & xev);
1201 } else if (x11->fs_type & vo_wm_NETWM)
1203 XClientMessageEvent xev;
1204 char *state;
1206 memset(&xev, 0, sizeof(xev));
1207 xev.type = ClientMessage;
1208 xev.message_type = x11->XA_NET_WM_STATE;
1209 xev.display = x11->display;
1210 xev.window = vo_window;
1211 xev.format = 32;
1212 xev.data.l[0] = layer;
1214 if (x11->fs_type & vo_wm_STAYS_ON_TOP)
1215 xev.data.l[1] = x11->XA_NET_WM_STATE_STAYS_ON_TOP;
1216 else if (x11->fs_type & vo_wm_ABOVE)
1217 xev.data.l[1] = x11->XA_NET_WM_STATE_ABOVE;
1218 else if (x11->fs_type & vo_wm_FULLSCREEN)
1219 xev.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
1220 else if (x11->fs_type & vo_wm_BELOW)
1221 // This is not fallback. We can safely assume that the situation
1222 // where only NETWM_STATE_BELOW is supported doesn't exist.
1223 xev.data.l[1] = x11->XA_NET_WM_STATE_BELOW;
1225 XSendEvent(x11->display, x11->rootwin, False, SubstructureRedirectMask,
1226 (XEvent *) & xev);
1227 state = XGetAtomName(x11->display, xev.data.l[1]);
1228 mp_msg(MSGT_VO, MSGL_V,
1229 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1230 layer, state);
1231 XFree(state);
1235 static int vo_x11_get_fs_type(int supported)
1237 int i;
1238 int type = supported;
1240 if (vo_fstype_list)
1242 for (i = 0; vo_fstype_list[i]; i++)
1244 int neg = 0;
1245 char *arg = vo_fstype_list[i];
1247 if (vo_fstype_list[i][0] == '-')
1249 neg = 1;
1250 arg = vo_fstype_list[i] + 1;
1253 if (!strncmp(arg, "layer", 5))
1255 if (!neg && (arg[5] == '='))
1257 char *endptr = NULL;
1258 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1260 if (endptr && *endptr == '\0' && layer >= 0
1261 && layer <= 15)
1262 fs_layer = layer;
1264 if (neg)
1265 type &= ~vo_wm_LAYER;
1266 else
1267 type |= vo_wm_LAYER;
1268 } else if (!strcmp(arg, "above"))
1270 if (neg)
1271 type &= ~vo_wm_ABOVE;
1272 else
1273 type |= vo_wm_ABOVE;
1274 } else if (!strcmp(arg, "fullscreen"))
1276 if (neg)
1277 type &= ~vo_wm_FULLSCREEN;
1278 else
1279 type |= vo_wm_FULLSCREEN;
1280 } else if (!strcmp(arg, "stays_on_top"))
1282 if (neg)
1283 type &= ~vo_wm_STAYS_ON_TOP;
1284 else
1285 type |= vo_wm_STAYS_ON_TOP;
1286 } else if (!strcmp(arg, "below"))
1288 if (neg)
1289 type &= ~vo_wm_BELOW;
1290 else
1291 type |= vo_wm_BELOW;
1292 } else if (!strcmp(arg, "netwm"))
1294 if (neg)
1295 type &= ~vo_wm_NETWM;
1296 else
1297 type |= vo_wm_NETWM;
1298 } else if (!strcmp(arg, "none"))
1299 type = 0; // clear; keep parsing
1303 return type;
1307 * \brief update vo->dx, vo->dy, vo->dwidth and vo->dheight with current values of vo->x11->window
1308 * \return returns current color depth of vo->x11->window
1310 int vo_x11_update_geometry(struct vo *vo, bool update_pos)
1312 struct vo_x11_state *x11 = vo->x11;
1313 unsigned depth, w, h;
1314 int dummy_int;
1315 Window dummy_win;
1316 XGetGeometry(x11->display, x11->window, &dummy_win, &dummy_int, &dummy_int,
1317 &w, &h, &dummy_int, &depth);
1318 if (w <= INT_MAX && h <= INT_MAX) {
1319 vo->dwidth = w;
1320 vo->dheight = h;
1322 if (update_pos)
1323 XTranslateCoordinates(x11->display, x11->window, x11->rootwin, 0, 0,
1324 &vo->dx, &vo->dy, &dummy_win);
1326 return depth <= INT_MAX ? depth : 0;
1329 void vo_x11_fullscreen(struct vo *vo)
1331 struct MPOpts *opts = vo->opts;
1332 struct vo_x11_state *x11 = vo->x11;
1333 int x, y, w, h;
1334 x = x11->vo_old_x;
1335 y = x11->vo_old_y;
1336 w = x11->vo_old_width;
1337 h = x11->vo_old_height;
1339 if (WinID >= 0) {
1340 vo_fs = !vo_fs;
1341 return;
1343 if (x11->fs_flip)
1344 return;
1346 if (vo_fs)
1348 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1349 vo_fs = VO_FALSE;
1350 if (x11->size_changed_during_fs && (x11->fs_type & vo_wm_FULLSCREEN))
1351 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, x11->last_video_width,
1352 x11->last_video_height);
1353 x11->size_changed_during_fs = false;
1354 } else
1356 // win->fs
1357 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1359 vo_fs = VO_TRUE;
1360 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1362 x11->vo_old_x = vo->dx;
1363 x11->vo_old_y = vo->dy;
1364 x11->vo_old_width = vo->dwidth;
1365 x11->vo_old_height = vo->dheight;
1367 update_xinerama_info(vo);
1368 x = xinerama_x;
1369 y = xinerama_y;
1370 w = opts->vo_screenwidth;
1371 h = opts->vo_screenheight;
1374 long dummy;
1376 XGetWMNormalHints(x11->display, x11->window, &x11->vo_hint, &dummy);
1377 if (!(x11->vo_hint.flags & PWinGravity))
1378 x11->old_gravity = NorthWestGravity;
1379 else
1380 x11->old_gravity = x11->vo_hint.win_gravity;
1382 if (x11->wm_type == 0 && !(vo_fsmode & 16))
1384 XUnmapWindow(x11->display, x11->window); // required for MWM
1385 XWithdrawWindow(x11->display, x11->window, x11->screen);
1386 x11->fs_flip = 1;
1389 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1391 vo_x11_decoration(vo, vo_border && !vo_fs);
1392 vo_x11_sizehint(vo, x, y, w, h, 0);
1393 vo_x11_setlayer(vo, x11->window, vo_fs);
1396 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1398 /* some WMs lose ontop after fullscreen */
1399 if ((!(vo_fs)) & opts->vo_ontop)
1400 vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1402 XMapRaised(x11->display, x11->window);
1403 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1404 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1405 XRaiseWindow(x11->display, x11->window);
1406 XFlush(x11->display);
1409 void vo_x11_ontop(struct vo *vo)
1411 struct MPOpts *opts = vo->opts;
1412 opts->vo_ontop = !opts->vo_ontop;
1414 vo_x11_setlayer(vo, vo->x11->window, opts->vo_ontop);
1417 void vo_x11_border(struct vo *vo)
1419 vo_border = !vo_border;
1420 vo_x11_decoration(vo, vo_border && !vo_fs);
1424 * XScreensaver stuff
1427 static int screensaver_off;
1428 static unsigned int time_last;
1430 void xscreensaver_heartbeat(struct vo_x11_state *x11)
1432 unsigned int time = GetTimerMS();
1434 if (x11->display && screensaver_off && (time - time_last) > 30000)
1436 time_last = time;
1438 XResetScreenSaver(x11->display);
1442 static int xss_suspend(Display *mDisplay, Bool suspend)
1444 #ifndef CONFIG_XSS
1445 return 0;
1446 #else
1447 int event, error, major, minor;
1448 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1449 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1450 return 0;
1451 if (major < 1 || (major == 1 && minor < 1))
1452 return 0;
1453 XScreenSaverSuspend(mDisplay, suspend);
1454 return 1;
1455 #endif
1459 * End of XScreensaver stuff
1462 static void saver_on(Display * mDisplay)
1465 if (!screensaver_off)
1466 return;
1467 screensaver_off = 0;
1468 if (xss_suspend(mDisplay, False))
1469 return;
1470 #ifdef CONFIG_XDPMS
1471 if (dpms_disabled)
1473 int nothing;
1474 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1476 if (!DPMSEnable(mDisplay))
1477 { // restoring power saving settings
1478 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1479 } else
1481 // DPMS does not seem to be enabled unless we call DPMSInfo
1482 BOOL onoff;
1483 CARD16 state;
1485 DPMSForceLevel(mDisplay, DPMSModeOn);
1486 DPMSInfo(mDisplay, &state, &onoff);
1487 if (onoff)
1489 mp_msg(MSGT_VO, MSGL_V,
1490 "Successfully enabled DPMS\n");
1491 } else
1493 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1497 dpms_disabled = 0;
1499 #endif
1502 static void saver_off(Display * mDisplay)
1504 int nothing;
1506 if (screensaver_off)
1507 return;
1508 screensaver_off = 1;
1509 if (xss_suspend(mDisplay, True))
1510 return;
1511 #ifdef CONFIG_XDPMS
1512 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1514 BOOL onoff;
1515 CARD16 state;
1517 DPMSInfo(mDisplay, &state, &onoff);
1518 if (onoff)
1520 Status stat;
1522 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1523 dpms_disabled = 1;
1524 stat = DPMSDisable(mDisplay); // monitor powersave off
1525 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1528 #endif
1531 static XErrorHandler old_handler = NULL;
1532 static int selectinput_err = 0;
1533 static int x11_selectinput_errorhandler(Display * display,
1534 XErrorEvent * event)
1536 if (event->error_code == BadAccess)
1538 selectinput_err = 1;
1539 mp_msg(MSGT_VO, MSGL_ERR,
1540 "X11 error: BadAccess during XSelectInput Call\n");
1541 mp_msg(MSGT_VO, MSGL_ERR,
1542 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1543 /* If you think MPlayer should shutdown with this error,
1544 * comment out the following line */
1545 return 0;
1547 if (old_handler != NULL)
1548 old_handler(display, event);
1549 else
1550 x11_errorhandler(display, event);
1551 return 0;
1554 void vo_x11_selectinput_witherr(Display * display, Window w,
1555 long event_mask)
1557 XSync(display, False);
1558 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1559 selectinput_err = 0;
1560 if (vo_nomouse_input)
1562 XSelectInput(display, w,
1563 event_mask &
1564 (~(ButtonPressMask | ButtonReleaseMask)));
1565 } else
1567 XSelectInput(display, w, event_mask);
1569 XSync(display, False);
1570 XSetErrorHandler(old_handler);
1571 if (selectinput_err)
1573 mp_msg(MSGT_VO, MSGL_ERR,
1574 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1575 XSelectInput(display, w,
1576 event_mask &
1578 (ButtonPressMask | ButtonReleaseMask |
1579 PointerMotionMask)));
1583 #ifdef CONFIG_XF86VM
1584 void vo_vm_switch(struct vo *vo)
1586 struct vo_x11_state *x11 = vo->x11;
1587 struct MPOpts *opts = vo->opts;
1588 Display *mDisplay = x11->display;
1589 int vm_event, vm_error;
1590 int vm_ver, vm_rev;
1591 int i, j, have_vm = 0;
1592 int X = vo->dwidth, Y = vo->dheight;
1593 int modeline_width, modeline_height;
1595 int modecount;
1597 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1599 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1600 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1601 vm_rev);
1602 have_vm = 1;
1603 } else {
1604 mp_msg(MSGT_VO, MSGL_WARN,
1605 "XF86VidMode extension not available.\n");
1608 if (have_vm)
1610 if (vidmodes == NULL)
1611 XF86VidModeGetAllModeLines(mDisplay, x11->screen, &modecount,
1612 &vidmodes);
1613 j = 0;
1614 modeline_width = vidmodes[0]->hdisplay;
1615 modeline_height = vidmodes[0]->vdisplay;
1617 for (i = 1; i < modecount; i++)
1618 if ((vidmodes[i]->hdisplay >= X)
1619 && (vidmodes[i]->vdisplay >= Y))
1620 if ((vidmodes[i]->hdisplay <= modeline_width)
1621 && (vidmodes[i]->vdisplay <= modeline_height))
1623 modeline_width = vidmodes[i]->hdisplay;
1624 modeline_height = vidmodes[i]->vdisplay;
1625 j = i;
1628 mp_tmsg(MSGT_VO, MSGL_INFO, "XF86VM: Selected video mode %dx%d for image size %dx%d.\n",
1629 modeline_width, modeline_height, X, Y);
1630 XF86VidModeLockModeSwitch(mDisplay, x11->screen, 0);
1631 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1632 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1634 // FIXME: all this is more of a hack than proper solution
1635 X = (opts->vo_screenwidth - modeline_width) / 2;
1636 Y = (opts->vo_screenheight - modeline_height) / 2;
1637 XF86VidModeSetViewPort(mDisplay, x11->screen, X, Y);
1638 vo->dx = X;
1639 vo->dy = Y;
1640 vo->dwidth = modeline_width;
1641 vo->dheight = modeline_height;
1642 aspect_save_screenres(vo, modeline_width, modeline_height);
1646 void vo_vm_close(struct vo *vo)
1648 Display *dpy = vo->x11->display;
1649 struct MPOpts *opts = vo->opts;
1650 if (vidmodes != NULL)
1652 int i, modecount;
1654 free(vidmodes);
1655 vidmodes = NULL;
1656 XF86VidModeGetAllModeLines(dpy, vo->x11->screen, &modecount,
1657 &vidmodes);
1658 for (i = 0; i < modecount; i++)
1659 if ((vidmodes[i]->hdisplay == opts->vo_screenwidth)
1660 && (vidmodes[i]->vdisplay == opts->vo_screenheight))
1662 mp_msg(MSGT_VO, MSGL_INFO,
1663 "Returning to original mode %dx%d\n",
1664 opts->vo_screenwidth, opts->vo_screenheight);
1665 break;
1668 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1669 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1670 free(vidmodes);
1671 vidmodes = NULL;
1675 double vo_vm_get_fps(struct vo *vo)
1677 struct vo_x11_state *x11 = vo->x11;
1678 int clock;
1679 XF86VidModeModeLine modeline;
1680 if (!XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline))
1681 return 0;
1682 if (modeline.privsize)
1683 XFree(modeline.private);
1684 return 1e3 * clock / modeline.htotal / modeline.vtotal;
1686 #endif
1689 static Colormap cmap = None;
1690 static XColor cols[256];
1691 static int cm_size, red_mask, green_mask, blue_mask;
1694 Colormap vo_x11_create_colormap(struct vo *vo, XVisualInfo *vinfo)
1696 struct vo_x11_state *x11 = vo->x11;
1697 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1699 if (vinfo->class != DirectColor)
1700 return XCreateColormap(x11->display, x11->rootwin, vinfo->visual,
1701 AllocNone);
1703 /* can this function get called twice or more? */
1704 if (cmap)
1705 return cmap;
1706 cm_size = vinfo->colormap_size;
1707 red_mask = vinfo->red_mask;
1708 green_mask = vinfo->green_mask;
1709 blue_mask = vinfo->blue_mask;
1710 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1711 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1712 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1713 rvu = 65536ull * ru / (red_mask + ru);
1714 gvu = 65536ull * gu / (green_mask + gu);
1715 bvu = 65536ull * bu / (blue_mask + bu);
1716 r = g = b = 0;
1717 rv = gv = bv = 0;
1718 m = DoRed | DoGreen | DoBlue;
1719 for (k = 0; k < cm_size; k++)
1721 int t;
1723 cols[k].pixel = r | g | b;
1724 cols[k].red = rv;
1725 cols[k].green = gv;
1726 cols[k].blue = bv;
1727 cols[k].flags = m;
1728 t = (r + ru) & red_mask;
1729 if (t < r)
1730 m &= ~DoRed;
1731 r = t;
1732 t = (g + gu) & green_mask;
1733 if (t < g)
1734 m &= ~DoGreen;
1735 g = t;
1736 t = (b + bu) & blue_mask;
1737 if (t < b)
1738 m &= ~DoBlue;
1739 b = t;
1740 rv += rvu;
1741 gv += gvu;
1742 bv += bvu;
1744 cmap = XCreateColormap(x11->display, x11->rootwin, vinfo->visual, AllocAll);
1745 XStoreColors(x11->display, cmap, cols, cm_size);
1746 return cmap;
1750 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1751 * hue and red/green/blue intensity, but we cannot do saturation.
1752 * Currently only gamma, brightness and contrast are implemented.
1753 * Is there sufficient interest for hue and/or red/green/blue intensity?
1755 /* these values have range [-100,100] and are initially 0 */
1756 static int vo_gamma = 0;
1757 static int vo_brightness = 0;
1758 static int vo_contrast = 0;
1760 static int transform_color(float val,
1761 float brightness, float contrast, float gamma) {
1762 float s = pow(val, gamma);
1763 s = (s - 0.5) * contrast + 0.5;
1764 s += brightness;
1765 if (s < 0)
1766 s = 0;
1767 if (s > 1)
1768 s = 1;
1769 return (unsigned short) (s * 65535);
1772 uint32_t vo_x11_set_equalizer(struct vo *vo, const char *name, int value)
1774 float gamma, brightness, contrast;
1775 float rf, gf, bf;
1776 int k;
1779 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1780 * of TrueColor-ed window but be careful:
1781 * Unlike the colormaps, which are private for the X client
1782 * who created them and thus automatically destroyed on client
1783 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1784 * setting and _must_ be restored before the process exits.
1785 * Unforunately when the process crashes (or gets killed
1786 * for some reason) it is impossible to restore the setting,
1787 * and such behaviour could be rather annoying for the users.
1789 if (cmap == None)
1790 return VO_NOTAVAIL;
1792 if (!strcasecmp(name, "brightness"))
1793 vo_brightness = value;
1794 else if (!strcasecmp(name, "contrast"))
1795 vo_contrast = value;
1796 else if (!strcasecmp(name, "gamma"))
1797 vo_gamma = value;
1798 else
1799 return VO_NOTIMPL;
1801 brightness = 0.01 * vo_brightness;
1802 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1803 gamma = pow(2, -0.02 * vo_gamma);
1805 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1806 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
1807 green_mask;
1808 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
1810 /* now recalculate the colormap using the newly set value */
1811 for (k = 0; k < cm_size; k++)
1813 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
1814 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
1815 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
1818 XStoreColors(vo->x11->display, cmap, cols, cm_size);
1819 XFlush(vo->x11->display);
1820 return VO_TRUE;
1823 uint32_t vo_x11_get_equalizer(const char *name, int *value)
1825 if (cmap == None)
1826 return VO_NOTAVAIL;
1827 if (!strcasecmp(name, "brightness"))
1828 *value = vo_brightness;
1829 else if (!strcasecmp(name, "contrast"))
1830 *value = vo_contrast;
1831 else if (!strcasecmp(name, "gamma"))
1832 *value = vo_gamma;
1833 else
1834 return VO_NOTIMPL;
1835 return VO_TRUE;
1838 bool vo_x11_screen_is_composited(struct vo *vo)
1840 struct vo_x11_state *x11 = vo->x11;
1841 return XGetSelectionOwner(x11->display, x11->XA_NET_WM_CM) != None;
1844 #ifdef CONFIG_XV
1845 int vo_xv_set_eq(struct vo *vo, uint32_t xv_port, const char *name, int value)
1847 XvAttribute *attributes;
1848 int i, howmany, xv_atom;
1850 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
1852 /* get available attributes */
1853 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1854 for (i = 0; i < howmany && attributes; i++)
1855 if (attributes[i].flags & XvSettable)
1857 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1858 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1859 then trigger it if it's ok so that the other values are at default upon query */
1860 if (xv_atom != None)
1862 int hue = 0, port_value, port_min, port_max;
1864 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1865 (!strcasecmp(name, "brightness")))
1866 port_value = value;
1867 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1868 (!strcasecmp(name, "contrast")))
1869 port_value = value;
1870 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1871 (!strcasecmp(name, "saturation")))
1872 port_value = value;
1873 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1874 (!strcasecmp(name, "hue")))
1876 port_value = value;
1877 hue = 1;
1878 } else
1879 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1880 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1881 (!strcasecmp(name, "red_intensity")))
1882 port_value = value;
1883 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1884 && (!strcasecmp(name, "green_intensity")))
1885 port_value = value;
1886 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1887 && (!strcasecmp(name, "blue_intensity")))
1888 port_value = value;
1889 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
1890 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
1891 && (!strcasecmp(name, "bt_709")))
1892 port_value = value;
1893 else
1894 continue;
1896 port_min = attributes[i].min_value;
1897 port_max = attributes[i].max_value;
1899 /* nvidia hue workaround */
1900 if (hue && port_min == 0 && port_max == 360)
1902 port_value =
1903 (port_value >=
1904 0) ? (port_value - 100) : (port_value + 100);
1906 // -100 -> min
1907 // 0 -> (max+min)/2
1908 // +100 -> max
1909 port_value =
1910 (port_value + 100) * (port_max - port_min) / 200 +
1911 port_min;
1912 XvSetPortAttribute(vo->x11->display, xv_port, xv_atom, port_value);
1913 return VO_TRUE;
1916 return VO_FALSE;
1919 int vo_xv_get_eq(struct vo *vo, uint32_t xv_port, const char *name, int *value)
1922 XvAttribute *attributes;
1923 int i, howmany, xv_atom;
1925 /* get available attributes */
1926 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1927 for (i = 0; i < howmany && attributes; i++)
1928 if (attributes[i].flags & XvGettable)
1930 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1931 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1932 then trigger it if it's ok so that the other values are at default upon query */
1933 if (xv_atom != None)
1935 int val, port_value = 0, port_min, port_max;
1937 XvGetPortAttribute(vo->x11->display, xv_port, xv_atom,
1938 &port_value);
1940 port_min = attributes[i].min_value;
1941 port_max = attributes[i].max_value;
1942 val =
1943 (port_value - port_min) * 200 / (port_max - port_min) -
1944 100;
1946 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1947 (!strcasecmp(name, "brightness")))
1948 *value = val;
1949 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1950 (!strcasecmp(name, "contrast")))
1951 *value = val;
1952 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1953 (!strcasecmp(name, "saturation")))
1954 *value = val;
1955 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1956 (!strcasecmp(name, "hue")))
1958 /* nasty nvidia detect */
1959 if (port_min == 0 && port_max == 360)
1960 *value = (val >= 0) ? (val - 100) : (val + 100);
1961 else
1962 *value = val;
1963 } else
1964 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1965 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1966 (!strcasecmp(name, "red_intensity")))
1967 *value = val;
1968 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1969 && (!strcasecmp(name, "green_intensity")))
1970 *value = val;
1971 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1972 && (!strcasecmp(name, "blue_intensity")))
1973 *value = val;
1974 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
1975 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
1976 && (!strcasecmp(name, "bt_709")))
1977 *value = val;
1978 else
1979 continue;
1981 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
1982 name, *value);
1983 return VO_TRUE;
1986 return VO_FALSE;
1990 * \brief Interns the requested atom if it is available.
1992 * \param atom_name String containing the name of the requested atom.
1994 * \return Returns the atom if available, else None is returned.
1997 static Atom xv_intern_atom_if_exists(struct vo_x11_state *x11,
1998 char const *atom_name)
2000 XvAttribute * attributes;
2001 int attrib_count,i;
2002 Atom xv_atom = None;
2004 attributes = XvQueryPortAttributes(x11->display, x11->xv_port, &attrib_count );
2005 if( attributes!=NULL )
2007 for ( i = 0; i < attrib_count; ++i )
2009 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2011 xv_atom = XInternAtom(x11->display, atom_name, False );
2012 break; // found what we want, break out
2015 XFree( attributes );
2018 return xv_atom;
2022 * \brief Try to enable vsync for xv.
2023 * \return Returns -1 if not available, 0 on failure and 1 on success.
2025 int vo_xv_enable_vsync(struct vo *vo)
2027 struct vo_x11_state *x11 = vo->x11;
2028 Atom xv_atom = xv_intern_atom_if_exists(x11, "XV_SYNC_TO_VBLANK");
2029 if (xv_atom == None)
2030 return -1;
2031 return XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1) == Success;
2035 * \brief Get maximum supported source image dimensions.
2037 * This function does not set the variables pointed to by
2038 * width and height if the information could not be retrieved,
2039 * so the caller is reponsible for properly initializing them.
2041 * \param width [out] The maximum width gets stored here.
2042 * \param height [out] The maximum height gets stored here.
2045 void vo_xv_get_max_img_dim(struct vo *vo, uint32_t * width, uint32_t * height)
2047 struct vo_x11_state *x11 = vo->x11;
2048 XvEncodingInfo * encodings;
2049 //unsigned long num_encodings, idx; to int or too long?!
2050 unsigned int num_encodings, idx;
2052 XvQueryEncodings(x11->display, x11->xv_port, &num_encodings, &encodings);
2054 if ( encodings )
2056 for ( idx = 0; idx < num_encodings; ++idx )
2058 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2060 *width = encodings[idx].width;
2061 *height = encodings[idx].height;
2062 break;
2067 mp_msg( MSGT_VO, MSGL_V,
2068 "[xv common] Maximum source image dimensions: %ux%u\n",
2069 *width, *height );
2071 XvFreeEncodingInfo( encodings );
2075 * \brief Print information about the colorkey method and source.
2077 * \param ck_handling Integer value containing the information about
2078 * colorkey handling (see x11_common.h).
2080 * Outputs the content of |ck_handling| as a readable message.
2083 static void vo_xv_print_ck_info(struct vo_x11_state *x11)
2085 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2087 switch ( x11->xv_ck_info.method )
2089 case CK_METHOD_NONE:
2090 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2091 case CK_METHOD_AUTOPAINT:
2092 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2093 case CK_METHOD_MANUALFILL:
2094 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2095 case CK_METHOD_BACKGROUND:
2096 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2099 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2101 switch ( x11->xv_ck_info.source )
2103 case CK_SRC_CUR:
2104 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2105 x11->xv_colorkey );
2106 break;
2107 case CK_SRC_USE:
2108 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2110 mp_msg( MSGT_VO, MSGL_V,
2111 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2112 x11->xv_colorkey );
2114 else
2116 mp_msg( MSGT_VO, MSGL_V,
2117 "Using colorkey from MPlayer (0x%06lx)."
2118 " Use -colorkey to change.\n",
2119 x11->xv_colorkey );
2121 break;
2122 case CK_SRC_SET:
2123 mp_msg( MSGT_VO, MSGL_V,
2124 "Setting and using colorkey from MPlayer (0x%06lx)."
2125 " Use -colorkey to change.\n",
2126 x11->xv_colorkey );
2127 break;
2131 * \brief Init colorkey depending on the settings in xv_ck_info.
2133 * \return Returns 0 on failure and 1 on success.
2135 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2136 * flags in xv_ck_info.
2138 * Possiblilities:
2139 * * Methods
2140 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2141 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2142 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2143 * * Sources
2144 * - use currently set colorkey ( CK_SRC_CUR )
2145 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2146 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2148 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2149 * we don't draw anything as this means it was forced to off.
2151 int vo_xv_init_colorkey(struct vo *vo)
2153 struct vo_x11_state *x11 = vo->x11;
2154 Atom xv_atom;
2155 int rez;
2157 /* check if colorkeying is needed */
2158 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_COLORKEY");
2160 /* if we have to deal with colorkeying ... */
2161 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2163 /* check if we should use the colorkey specified in vo_colorkey */
2164 if ( x11->xv_ck_info.source != CK_SRC_CUR )
2166 x11->xv_colorkey = vo_colorkey;
2168 /* check if we have to set the colorkey too */
2169 if ( x11->xv_ck_info.source == CK_SRC_SET )
2171 xv_atom = XInternAtom(x11->display, "XV_COLORKEY",False);
2173 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, vo_colorkey);
2174 if ( rez != Success )
2176 mp_msg( MSGT_VO, MSGL_FATAL,
2177 "[xv common] Couldn't set colorkey!\n" );
2178 return 0; // error setting colorkey
2182 else
2184 int colorkey_ret;
2186 rez=XvGetPortAttribute(x11->display,x11->xv_port, xv_atom, &colorkey_ret);
2187 if ( rez == Success )
2189 x11->xv_colorkey = colorkey_ret;
2191 else
2193 mp_msg( MSGT_VO, MSGL_FATAL,
2194 "[xv common] Couldn't get colorkey!"
2195 "Maybe the selected Xv port has no overlay.\n" );
2196 return 0; // error getting colorkey
2200 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_AUTOPAINT_COLORKEY");
2202 /* should we draw the colorkey ourselves or activate autopainting? */
2203 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2205 rez = !Success; // reset rez to something different than Success
2207 if ( xv_atom != None ) // autopaint is supported
2209 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1);
2212 if ( rez != Success )
2214 // fallback to manual colorkey drawing
2215 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2218 else // disable colorkey autopainting if supported
2220 if ( xv_atom != None ) // we have autopaint attribute
2222 XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 0);
2226 else // do no colorkey drawing at all
2228 x11->xv_ck_info.method = CK_METHOD_NONE;
2229 } /* end: should we draw colorkey */
2231 /* output information about the current colorkey settings */
2232 vo_xv_print_ck_info(x11);
2234 return 1; // success
2238 * \brief Draw the colorkey on the video window.
2240 * Draws the colorkey depending on the set method ( colorkey_handling ).
2242 * Also draws the black bars ( when the video doesn't fit the display in
2243 * fullscreen ) separately, so they don't overlap with the video area.
2244 * It doesn't call XFlush.
2247 void vo_xv_draw_colorkey(struct vo *vo, int32_t x, int32_t y,
2248 int32_t w, int32_t h)
2250 struct vo_x11_state *x11 = vo->x11;
2251 if( x11->xv_ck_info.method == CK_METHOD_MANUALFILL ||
2252 x11->xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2254 XSetForeground(x11->display, x11->vo_gc, x11->xv_colorkey );
2255 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2256 x, y,
2257 w, h );
2261 /** \brief Tests if a valid argument for the ck suboption was given. */
2262 int xv_test_ck( void * arg )
2264 strarg_t * strarg = (strarg_t *)arg;
2266 if ( strargcmp( strarg, "use" ) == 0 ||
2267 strargcmp( strarg, "set" ) == 0 ||
2268 strargcmp( strarg, "cur" ) == 0 )
2270 return 1;
2273 return 0;
2275 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2276 int xv_test_ckm( void * arg )
2278 strarg_t * strarg = (strarg_t *)arg;
2280 if ( strargcmp( strarg, "bg" ) == 0 ||
2281 strargcmp( strarg, "man" ) == 0 ||
2282 strargcmp( strarg, "auto" ) == 0 )
2284 return 1;
2287 return 0;
2291 * \brief Modify the colorkey_handling var according to str
2293 * Checks if a valid pointer ( not NULL ) to the string
2294 * was given. And in that case modifies the colorkey_handling
2295 * var to reflect the requested behaviour.
2296 * If nothing happens the content of colorkey_handling stays
2297 * the same.
2299 * \param str Pointer to the string or NULL
2302 void xv_setup_colorkeyhandling(struct vo *vo, const char *ck_method_str,
2303 const char *ck_str)
2305 struct vo_x11_state *x11 = vo->x11;
2306 /* check if a valid pointer to the string was passed */
2307 if ( ck_str )
2309 if ( strncmp( ck_str, "use", 3 ) == 0 )
2311 x11->xv_ck_info.source = CK_SRC_USE;
2313 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2315 x11->xv_ck_info.source = CK_SRC_SET;
2318 /* check if a valid pointer to the string was passed */
2319 if ( ck_method_str )
2321 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2323 x11->xv_ck_info.method = CK_METHOD_BACKGROUND;
2325 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2327 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2329 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2331 x11->xv_ck_info.method = CK_METHOD_AUTOPAINT;
2336 #endif
2338 struct vo_x11_state *vo_x11_init_state(void)
2340 struct vo_x11_state *s = talloc_ptrtype(NULL, s);
2341 *s = (struct vo_x11_state){
2342 .xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR },
2343 .olddecor = MWM_DECOR_ALL,
2344 .oldfuncs = MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
2345 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE,
2346 .old_gravity = NorthWestGravity,
2348 return s;