ao_pulse: support native mute control
[mplayer.git] / libvo / x11_common.c
blobee50ba5575bb2d2e301b04fb22639cea5441d238
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 #ifdef X11_FULLSCREEN
36 #include <string.h>
37 #include <unistd.h>
38 #include <assert.h>
40 #include "video_out.h"
41 #include "aspect.h"
42 #include "geometry.h"
43 #include "osdep/timer.h"
45 #include <X11/Xmd.h>
46 #include <X11/Xlib.h>
47 #include <X11/Xutil.h>
48 #include <X11/Xatom.h>
49 #include <X11/keysym.h>
51 #ifdef CONFIG_XSS
52 #include <X11/extensions/scrnsaver.h>
53 #endif
55 #ifdef CONFIG_XDPMS
56 #include <X11/extensions/dpms.h>
57 #endif
59 #ifdef CONFIG_XINERAMA
60 #include <X11/extensions/Xinerama.h>
61 #endif
63 #ifdef CONFIG_XF86VM
64 #include <X11/extensions/xf86vmode.h>
65 #endif
67 #ifdef CONFIG_XF86XK
68 #include <X11/XF86keysym.h>
69 #endif
71 #ifdef CONFIG_XV
72 #include <X11/extensions/Xv.h>
73 #include <X11/extensions/Xvlib.h>
75 #include "subopt-helper.h"
76 #endif
78 #include "input/input.h"
79 #include "input/keycodes.h"
81 #define WIN_LAYER_ONBOTTOM 2
82 #define WIN_LAYER_NORMAL 4
83 #define WIN_LAYER_ONTOP 6
84 #define WIN_LAYER_ABOVE_DOCK 10
86 int fs_layer = WIN_LAYER_ABOVE_DOCK;
88 int stop_xscreensaver = 0;
90 static int dpms_disabled = 0;
92 char *mDisplayName = NULL;
94 char **vo_fstype_list;
96 /* 1 means that the WM is metacity (broken as hell) */
97 int metacity_hack = 0;
99 #ifdef CONFIG_XF86VM
100 XF86VidModeModeInfo **vidmodes = NULL;
101 XF86VidModeModeLine modeline;
102 #endif
104 static int vo_x11_get_fs_type(int supported);
105 static void saver_off(Display *);
106 static void saver_on(Display *);
109 * Sends the EWMH fullscreen state event.
111 * action: could be one of _NET_WM_STATE_REMOVE -- remove state
112 * _NET_WM_STATE_ADD -- add state
113 * _NET_WM_STATE_TOGGLE -- toggle
115 void vo_x11_ewmh_fullscreen(struct vo_x11_state *x11, int action)
117 assert(action == _NET_WM_STATE_REMOVE ||
118 action == _NET_WM_STATE_ADD || action == _NET_WM_STATE_TOGGLE);
120 if (x11->fs_type & vo_wm_FULLSCREEN)
122 XEvent xev;
124 /* init X event structure for _NET_WM_FULLSCREEN client message */
125 xev.xclient.type = ClientMessage;
126 xev.xclient.serial = 0;
127 xev.xclient.send_event = True;
128 xev.xclient.message_type = x11->XA_NET_WM_STATE;
129 xev.xclient.window = x11->window;
130 xev.xclient.format = 32;
131 xev.xclient.data.l[0] = action;
132 xev.xclient.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
133 xev.xclient.data.l[2] = 0;
134 xev.xclient.data.l[3] = 0;
135 xev.xclient.data.l[4] = 0;
137 /* finally send that damn thing */
138 if (!XSendEvent(x11->display, DefaultRootWindow(x11->display), False,
139 SubstructureRedirectMask | SubstructureNotifyMask,
140 &xev))
142 mp_tmsg(MSGT_VO, MSGL_ERR, "\nX11: Couldn't send EWMH fullscreen event!\n");
147 static void vo_hidecursor(Display * disp, Window win)
149 Cursor no_ptr;
150 Pixmap bm_no;
151 XColor black, dummy;
152 Colormap colormap;
153 const char bm_no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
155 if (WinID == 0)
156 return; // do not hide if playing on the root window
158 colormap = DefaultColormap(disp, DefaultScreen(disp));
159 if ( !XAllocNamedColor(disp, colormap, "black", &black, &dummy) )
161 return; // color alloc failed, give up
163 bm_no = XCreateBitmapFromData(disp, win, bm_no_data, 8, 8);
164 no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
165 XDefineCursor(disp, win, no_ptr);
166 XFreeCursor(disp, no_ptr);
167 if (bm_no != None)
168 XFreePixmap(disp, bm_no);
169 XFreeColors(disp,colormap,&black.pixel,1,0);
172 static void vo_showcursor(Display * disp, Window win)
174 if (WinID == 0)
175 return;
176 XDefineCursor(disp, win, 0);
179 static int x11_errorhandler(Display * display, XErrorEvent * event)
181 #define MSGLEN 60
182 char msg[MSGLEN];
184 XGetErrorText(display, event->error_code, (char *) &msg, MSGLEN);
186 mp_msg(MSGT_VO, MSGL_ERR, "X11 error: %s\n", msg);
188 mp_msg(MSGT_VO, MSGL_V,
189 "Type: %x, display: %p, resourceid: %lx, serial: %lx\n",
190 event->type, event->display, event->resourceid, event->serial);
191 mp_msg(MSGT_VO, MSGL_V,
192 "Error code: %x, request code: %x, minor code: %x\n",
193 event->error_code, event->request_code, event->minor_code);
195 // abort();
196 //exit_player("X11 error");
197 return 0;
198 #undef MSGLEN
201 void fstype_help(void)
203 mp_tmsg(MSGT_VO, MSGL_INFO, "Available fullscreen layer change modes:\n");
204 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FULL_SCREEN_TYPES\n");
206 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "none",
207 "don't set fullscreen window layer");
208 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer",
209 "use _WIN_LAYER hint with default layer");
210 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer=<0..15>",
211 "use _WIN_LAYER hint with a given layer number");
212 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "netwm",
213 "force NETWM style");
214 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "above",
215 "use _NETWM_STATE_ABOVE hint if available");
216 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "below",
217 "use _NETWM_STATE_BELOW hint if available");
218 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "fullscreen",
219 "use _NETWM_STATE_FULLSCREEN hint if available");
220 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "stays_on_top",
221 "use _NETWM_STATE_STAYS_ON_TOP hint if available");
222 mp_msg(MSGT_VO, MSGL_INFO,
223 "You can also negate the settings with simply putting '-' in the beginning");
224 mp_msg(MSGT_VO, MSGL_INFO, "\n");
227 static void fstype_dump(int fstype)
229 if (fstype)
231 mp_msg(MSGT_VO, MSGL_V, "[x11] Current fstype setting honours");
232 if (fstype & vo_wm_LAYER)
233 mp_msg(MSGT_VO, MSGL_V, " LAYER");
234 if (fstype & vo_wm_FULLSCREEN)
235 mp_msg(MSGT_VO, MSGL_V, " FULLSCREEN");
236 if (fstype & vo_wm_STAYS_ON_TOP)
237 mp_msg(MSGT_VO, MSGL_V, " STAYS_ON_TOP");
238 if (fstype & vo_wm_ABOVE)
239 mp_msg(MSGT_VO, MSGL_V, " ABOVE");
240 if (fstype & vo_wm_BELOW)
241 mp_msg(MSGT_VO, MSGL_V, " BELOW");
242 mp_msg(MSGT_VO, MSGL_V, " X atoms\n");
243 } else
244 mp_msg(MSGT_VO, MSGL_V,
245 "[x11] Current fstype setting doesn't honour any X atoms\n");
248 static int net_wm_support_state_test(struct vo_x11_state *x11, Atom atom)
250 #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; } }
252 NET_WM_STATE_TEST(FULLSCREEN);
253 NET_WM_STATE_TEST(ABOVE);
254 NET_WM_STATE_TEST(STAYS_ON_TOP);
255 NET_WM_STATE_TEST(BELOW);
256 return 0;
259 static int x11_get_property(struct vo_x11_state *x11, Atom type, Atom ** args,
260 unsigned long *nitems)
262 int format;
263 unsigned long bytesafter;
265 return Success ==
266 XGetWindowProperty(x11->display, x11->rootwin, type, 0, 16384, False,
267 AnyPropertyType, &type, &format, nitems,
268 &bytesafter, (unsigned char **) args)
269 && *nitems > 0;
272 static int vo_wm_detect(struct vo *vo)
274 struct vo_x11_state *x11 = vo->x11;
275 int i;
276 int wm = 0;
277 unsigned long nitems;
278 Atom *args = NULL;
280 if (WinID >= 0)
281 return 0;
283 // -- supports layers
284 if (x11_get_property(x11, x11->XA_WIN_PROTOCOLS, &args, &nitems))
286 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports layers.\n");
287 for (i = 0; i < nitems; i++)
289 if (args[i] == x11->XA_WIN_LAYER)
291 wm |= vo_wm_LAYER;
292 metacity_hack |= 1;
293 } else
294 /* metacity is the only window manager I know which reports
295 * supporting only the _WIN_LAYER hint in _WIN_PROTOCOLS.
296 * (what's more support for it is broken) */
297 metacity_hack |= 2;
299 XFree(args);
300 if (wm && (metacity_hack == 1))
302 // metacity claims to support layers, but it is not the truth :-)
303 wm ^= vo_wm_LAYER;
304 mp_msg(MSGT_VO, MSGL_V,
305 "[x11] Using workaround for Metacity bugs.\n");
308 // --- netwm
309 if (x11_get_property(x11, x11->XA_NET_SUPPORTED, &args, &nitems))
311 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports NetWM.\n");
312 for (i = 0; i < nitems; i++)
313 wm |= net_wm_support_state_test(vo->x11, args[i]);
314 XFree(args);
317 if (wm == 0)
318 mp_msg(MSGT_VO, MSGL_V, "[x11] Unknown wm type...\n");
319 return wm;
322 #define XA_INIT(x) x11->XA##x = XInternAtom(x11->display, #x, False)
323 static void init_atoms(struct vo_x11_state *x11)
325 XA_INIT(_NET_SUPPORTED);
326 XA_INIT(_NET_WM_STATE);
327 XA_INIT(_NET_WM_STATE_FULLSCREEN);
328 XA_INIT(_NET_WM_STATE_ABOVE);
329 XA_INIT(_NET_WM_STATE_STAYS_ON_TOP);
330 XA_INIT(_NET_WM_STATE_BELOW);
331 XA_INIT(_NET_WM_PID);
332 XA_INIT(_NET_WM_NAME);
333 XA_INIT(_NET_WM_ICON_NAME);
334 XA_INIT(_WIN_PROTOCOLS);
335 XA_INIT(_WIN_LAYER);
336 XA_INIT(_WIN_HINTS);
337 XA_INIT(WM_PROTOCOLS);
338 XA_INIT(WM_DELETE_WINDOW);
339 XA_INIT(UTF8_STRING);
342 void update_xinerama_info(struct vo *vo) {
343 struct MPOpts *opts = vo->opts;
344 xinerama_x = xinerama_y = 0;
345 #ifdef CONFIG_XINERAMA
346 if (xinerama_screen >= -1 && XineramaIsActive(vo->x11->display))
348 int screen = xinerama_screen;
349 XineramaScreenInfo *screens;
350 int num_screens;
352 screens = XineramaQueryScreens(vo->x11->display, &num_screens);
353 if (screen >= num_screens)
354 screen = num_screens - 1;
355 if (screen == -1) {
356 int x = vo->dx + vo->dwidth / 2;
357 int y = vo->dy + vo->dheight / 2;
358 for (screen = num_screens - 1; screen > 0; screen--) {
359 int left = screens[screen].x_org;
360 int right = left + screens[screen].width;
361 int top = screens[screen].y_org;
362 int bottom = top + screens[screen].height;
363 if (left <= x && x <= right && top <= y && y <= bottom)
364 break;
367 if (screen < 0)
368 screen = 0;
369 opts->vo_screenwidth = screens[screen].width;
370 opts->vo_screenheight = screens[screen].height;
371 xinerama_x = screens[screen].x_org;
372 xinerama_y = screens[screen].y_org;
374 XFree(screens);
376 #endif
377 aspect_save_screenres(vo, opts->vo_screenwidth, opts->vo_screenheight);
380 int vo_init(struct vo *vo)
382 struct MPOpts *opts = vo->opts;
383 struct vo_x11_state *x11 = vo->x11;
384 // int mScreen;
385 int depth, bpp;
386 unsigned int mask;
388 // char * DisplayName = ":0.0";
389 // Display * mDisplay;
390 XImage *mXImage = NULL;
392 // Window mRootWin;
393 XWindowAttributes attribs;
394 char *dispName;
396 if (vo_rootwin)
397 WinID = 0; // use root window
399 if (x11->depthonscreen)
401 saver_off(x11->display);
402 return 1; // already called
405 XSetErrorHandler(x11_errorhandler);
407 #if 0
408 if (!mDisplayName)
409 if (!(mDisplayName = getenv("DISPLAY")))
410 mDisplayName = strdup(":0.0");
411 #else
412 dispName = XDisplayName(mDisplayName);
413 #endif
415 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
417 x11->display = XOpenDisplay(dispName);
418 if (!x11->display)
420 mp_msg(MSGT_VO, MSGL_ERR,
421 "vo: couldn't open the X11 display (%s)!\n", dispName);
422 return 0;
424 x11->screen = DefaultScreen(x11->display); // screen ID
425 x11->rootwin = RootWindow(x11->display, x11->screen); // root window ID
427 x11->xim = XOpenIM(x11->display, NULL, NULL, NULL);
429 init_atoms(vo->x11);
431 #ifdef CONFIG_XF86VM
433 int clock;
435 XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline);
436 if (!opts->vo_screenwidth)
437 opts->vo_screenwidth = modeline.hdisplay;
438 if (!opts->vo_screenheight)
439 opts->vo_screenheight = modeline.vdisplay;
441 #endif
443 if (!opts->vo_screenwidth)
444 opts->vo_screenwidth = DisplayWidth(x11->display, x11->screen);
445 if (!opts->vo_screenheight)
446 opts->vo_screenheight = DisplayHeight(x11->display, x11->screen);
448 // get color depth (from root window, or the best visual):
449 XGetWindowAttributes(x11->display, x11->rootwin, &attribs);
450 depth = attribs.depth;
452 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
454 Visual *visual;
456 depth = vo_find_depth_from_visuals(x11->display, x11->screen, &visual);
457 if (depth != -1)
458 mXImage = XCreateImage(x11->display, visual, depth, ZPixmap,
459 0, NULL, 1, 1, 8, 1);
460 } else
461 mXImage =
462 XGetImage(x11->display, x11->rootwin, 0, 0, 1, 1, AllPlanes, ZPixmap);
464 x11->depthonscreen = depth; // display depth on screen
466 // get bits/pixel from XImage structure:
467 if (mXImage == NULL)
469 mask = 0;
470 } else
473 * for the depth==24 case, the XImage structures might use
474 * 24 or 32 bits of data per pixel. The x11->depthonscreen
475 * field stores the amount of data per pixel in the
476 * XImage structure!
478 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
480 bpp = mXImage->bits_per_pixel;
481 if ((x11->depthonscreen + 7) / 8 != (bpp + 7) / 8)
482 x11->depthonscreen = bpp; // by A'rpi
483 mask =
484 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
485 mp_msg(MSGT_VO, MSGL_V,
486 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
487 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
488 XDestroyImage(mXImage);
490 if (((x11->depthonscreen + 7) / 8) == 2)
492 if (mask == 0x7FFF)
493 x11->depthonscreen = 15;
494 else if (mask == 0xFFFF)
495 x11->depthonscreen = 16;
497 // XCloseDisplay( mDisplay );
498 /* slightly improved local display detection AST */
499 if (strncmp(dispName, "unix:", 5) == 0)
500 dispName += 4;
501 else if (strncmp(dispName, "localhost:", 10) == 0)
502 dispName += 9;
503 if (*dispName == ':' && atoi(dispName + 1) < 10)
504 x11->display_is_local = 1;
505 else
506 x11->display_is_local = 0;
507 mp_msg(MSGT_VO, MSGL_V,
508 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
509 opts->vo_screenwidth, opts->vo_screenheight, depth, x11->depthonscreen,
510 dispName, x11->display_is_local ? "local" : "remote");
512 x11->wm_type = vo_wm_detect(vo);
514 x11->fs_type = vo_x11_get_fs_type(x11->wm_type);
516 fstype_dump(x11->fs_type);
518 saver_off(x11->display);
519 return 1;
522 void vo_uninit(struct vo_x11_state *x11)
524 if (!x11->display)
526 mp_msg(MSGT_VO, MSGL_V,
527 "vo: x11 uninit called but X11 not initialized..\n");
528 } else {
529 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
530 if (x11->xim)
531 XCloseIM(x11->xim);
532 XSetErrorHandler(NULL);
533 XCloseDisplay(x11->display);
534 x11->depthonscreen = 0;
535 x11->display = NULL;
537 talloc_free(x11);
540 static const struct mp_keymap keymap[] = {
541 // special keys
542 {XK_Pause, KEY_PAUSE}, {XK_Escape, KEY_ESC}, {XK_BackSpace, KEY_BS},
543 {XK_Tab, KEY_TAB}, {XK_Return, KEY_ENTER},
544 {XK_Menu, KEY_MENU}, {XK_Print, KEY_PRINT},
546 // cursor keys
547 {XK_Left, KEY_LEFT}, {XK_Right, KEY_RIGHT}, {XK_Up, KEY_UP}, {XK_Down, KEY_DOWN},
549 // navigation block
550 {XK_Insert, KEY_INSERT}, {XK_Delete, KEY_DELETE}, {XK_Home, KEY_HOME}, {XK_End, KEY_END},
551 {XK_Page_Up, KEY_PAGE_UP}, {XK_Page_Down, KEY_PAGE_DOWN},
553 // F-keys
554 {XK_F1, KEY_F+1}, {XK_F2, KEY_F+2}, {XK_F3, KEY_F+3}, {XK_F4, KEY_F+4},
555 {XK_F5, KEY_F+5}, {XK_F6, KEY_F+6}, {XK_F7, KEY_F+7}, {XK_F8, KEY_F+8},
556 {XK_F9, KEY_F+9}, {XK_F10, KEY_F+10}, {XK_F11, KEY_F+11}, {XK_F12, KEY_F+12},
558 // numpad independent of numlock
559 {XK_KP_Subtract, '-'}, {XK_KP_Add, '+'}, {XK_KP_Multiply, '*'}, {XK_KP_Divide, '/'},
560 {XK_KP_Enter, KEY_KPENTER},
562 // numpad with numlock
563 {XK_KP_0, KEY_KP0}, {XK_KP_1, KEY_KP1}, {XK_KP_2, KEY_KP2},
564 {XK_KP_3, KEY_KP3}, {XK_KP_4, KEY_KP4}, {XK_KP_5, KEY_KP5},
565 {XK_KP_6, KEY_KP6}, {XK_KP_7, KEY_KP7}, {XK_KP_8, KEY_KP8},
566 {XK_KP_9, KEY_KP9}, {XK_KP_Decimal, KEY_KPDEC},
567 {XK_KP_Separator, KEY_KPDEC},
569 // numpad without numlock
570 {XK_KP_Insert, KEY_KPINS}, {XK_KP_End, KEY_KP1}, {XK_KP_Down, KEY_KP2},
571 {XK_KP_Page_Down, KEY_KP3}, {XK_KP_Left, KEY_KP4}, {XK_KP_Begin, KEY_KP5},
572 {XK_KP_Right, KEY_KP6}, {XK_KP_Home, KEY_KP7}, {XK_KP_Up, KEY_KP8},
573 {XK_KP_Page_Up, KEY_KP9}, {XK_KP_Delete, KEY_KPDEL},
575 #ifdef XF86XK_AudioPause
576 {XF86XK_MenuKB, KEY_MENU},
577 {XF86XK_AudioPlay, KEY_PLAY}, {XF86XK_AudioPause, KEY_PAUSE}, {XF86XK_AudioStop, KEY_STOP},
578 {XF86XK_AudioPrev, KEY_PREV}, {XF86XK_AudioNext, KEY_NEXT},
579 {XF86XK_AudioMute, KEY_MUTE}, {XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN}, {XF86XK_AudioRaiseVolume, KEY_VOLUME_UP},
580 #endif
582 {0, 0}
585 static int vo_x11_lookupkey(int key)
587 static const char *passthrough_keys = " -+*/<>`~!@#$%^&()_{}:;\"\',.?\\|=[]";
588 int mpkey = 0;
589 if ((key >= 'a' && key <= 'z') ||
590 (key >= 'A' && key <= 'Z') ||
591 (key >= '0' && key <= '9') ||
592 (key > 0 && key < 256 && strchr(passthrough_keys, key)))
593 mpkey = key;
595 if (!mpkey)
596 mpkey = lookup_keymap_table(keymap, key);
598 return mpkey;
602 // ----- Motif header: -------
604 #define MWM_HINTS_FUNCTIONS (1L << 0)
605 #define MWM_HINTS_DECORATIONS (1L << 1)
606 #define MWM_HINTS_INPUT_MODE (1L << 2)
607 #define MWM_HINTS_STATUS (1L << 3)
609 #define MWM_FUNC_ALL (1L << 0)
610 #define MWM_FUNC_RESIZE (1L << 1)
611 #define MWM_FUNC_MOVE (1L << 2)
612 #define MWM_FUNC_MINIMIZE (1L << 3)
613 #define MWM_FUNC_MAXIMIZE (1L << 4)
614 #define MWM_FUNC_CLOSE (1L << 5)
616 #define MWM_DECOR_ALL (1L << 0)
617 #define MWM_DECOR_BORDER (1L << 1)
618 #define MWM_DECOR_RESIZEH (1L << 2)
619 #define MWM_DECOR_TITLE (1L << 3)
620 #define MWM_DECOR_MENU (1L << 4)
621 #define MWM_DECOR_MINIMIZE (1L << 5)
622 #define MWM_DECOR_MAXIMIZE (1L << 6)
624 #define MWM_INPUT_MODELESS 0
625 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
626 #define MWM_INPUT_SYSTEM_MODAL 2
627 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
628 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
630 #define MWM_TEAROFF_WINDOW (1L<<0)
632 typedef struct
634 long flags;
635 long functions;
636 long decorations;
637 long input_mode;
638 long state;
639 } MotifWmHints;
641 static MotifWmHints vo_MotifWmHints;
642 static Atom vo_MotifHints = None;
644 void vo_x11_decoration(struct vo *vo, int d)
646 struct vo_x11_state *x11 = vo->x11;
647 Atom mtype;
648 int mformat;
649 unsigned long mn, mb;
651 if (!WinID)
652 return;
654 if (vo_fsmode & 8)
656 XSetTransientForHint(x11->display, x11->window,
657 RootWindow(x11->display, x11->screen));
660 vo_MotifHints = XInternAtom(x11->display, "_MOTIF_WM_HINTS", 0);
661 if (vo_MotifHints != None)
663 if (!d)
665 MotifWmHints *mhints = NULL;
667 XGetWindowProperty(x11->display, x11->window,
668 vo_MotifHints, 0, 20, False,
669 vo_MotifHints, &mtype, &mformat, &mn,
670 &mb, (unsigned char **) &mhints);
671 if (mhints)
673 if (mhints->flags & MWM_HINTS_DECORATIONS)
674 x11->olddecor = mhints->decorations;
675 if (mhints->flags & MWM_HINTS_FUNCTIONS)
676 x11->oldfuncs = mhints->functions;
677 XFree(mhints);
681 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
682 vo_MotifWmHints.flags =
683 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
684 if (d)
686 vo_MotifWmHints.functions = x11->oldfuncs;
687 d = x11->olddecor;
689 #if 0
690 vo_MotifWmHints.decorations =
691 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
692 #else
693 vo_MotifWmHints.decorations =
694 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
695 #endif
696 XChangeProperty(x11->display, x11->window, vo_MotifHints,
697 vo_MotifHints, 32,
698 PropModeReplace,
699 (unsigned char *) &vo_MotifWmHints,
700 (vo_fsmode & 4) ? 4 : 5);
704 void vo_x11_classhint(struct vo *vo, Window window, const char *name)
706 struct MPOpts *opts = vo->opts;
707 struct vo_x11_state *x11 = vo->x11;
708 XClassHint wmClass;
709 pid_t pid = getpid();
711 wmClass.res_name = opts->vo_winname ? opts->vo_winname : (char *)name;
712 wmClass.res_class = "mplayer2";
713 XSetClassHint(x11->display, window, &wmClass);
714 XChangeProperty(x11->display, window, x11->XA_NET_WM_PID, XA_CARDINAL,
715 32, PropModeReplace, (unsigned char *) &pid, 1);
718 void vo_x11_uninit(struct vo *vo)
720 struct vo_x11_state *x11 = vo->x11;
721 saver_on(x11->display);
722 if (x11->window != None)
723 vo_showcursor(x11->display, x11->window);
725 if (x11->f_gc != None)
727 XFreeGC(vo->x11->display, x11->f_gc);
728 x11->f_gc = None;
731 if (x11->vo_gc != None)
733 XFreeGC(vo->x11->display, x11->vo_gc);
734 x11->vo_gc = None;
736 if (x11->window != None)
738 XClearWindow(x11->display, x11->window);
739 if (WinID < 0)
741 XEvent xev;
743 if (x11->xic)
744 XDestroyIC(x11->xic);
745 x11->xic = NULL;
747 XUnmapWindow(x11->display, x11->window);
748 XSelectInput(x11->display, x11->window, StructureNotifyMask);
749 XDestroyWindow(x11->display, x11->window);
752 XNextEvent(x11->display, &xev);
754 while (xev.type != DestroyNotify
755 || xev.xdestroywindow.event != x11->window);
757 x11->window = None;
759 vo_fs = 0;
760 x11->vo_old_width = x11->vo_old_height = 0;
761 x11->last_video_width = 0;
762 x11->last_video_height = 0;
763 x11->size_changed_during_fs = false;
767 static int check_resize(struct vo *vo)
769 int old_w = vo->dwidth, old_h = vo->dheight;
770 int old_x = vo->dx, old_y = vo->dy;
771 int rc = 0;
772 vo_x11_update_geometry(vo, true);
773 if (vo->dwidth != old_w || vo->dheight != old_h)
774 rc |= VO_EVENT_RESIZE;
775 if (vo->dx != old_x || vo->dy != old_y)
776 rc |= VO_EVENT_MOVE;
777 return rc;
780 int vo_x11_check_events(struct vo *vo)
782 struct vo_x11_state *x11 = vo->x11;
783 struct MPOpts *opts = vo->opts;
784 Display *display = vo->x11->display;
785 int ret = 0;
786 XEvent Event;
788 if (x11->mouse_waiting_hide && opts->cursor_autohide_delay != -1 &&
789 (GetTimerMS() - x11->mouse_timer >= opts->cursor_autohide_delay)) {
790 vo_hidecursor(display, x11->window);
791 x11->mouse_waiting_hide = 0;
794 if (WinID > 0)
795 ret |= check_resize(vo);
796 while (XPending(display))
798 XNextEvent(display, &Event);
799 // printf("\rEvent.type=%X \n",Event.type);
800 switch (Event.type)
802 case Expose:
803 ret |= VO_EVENT_EXPOSE;
804 break;
805 case ConfigureNotify:
806 if (x11->window == None)
807 break;
808 ret |= check_resize(vo);
809 break;
810 case KeyPress:
812 char buf[100];
813 KeySym keySym = 0;
814 int modifiers = 0;
815 if (Event.xkey.state & ShiftMask)
816 modifiers |= KEY_MODIFIER_SHIFT;
817 if (Event.xkey.state & ControlMask)
818 modifiers |= KEY_MODIFIER_CTRL;
819 if (Event.xkey.state & Mod1Mask)
820 modifiers |= KEY_MODIFIER_ALT;
821 if (Event.xkey.state & Mod4Mask)
822 modifiers |= KEY_MODIFIER_META;
823 if (x11->xic) {
824 Status status;
825 int len = Xutf8LookupString(x11->xic, &Event.xkey, buf,
826 sizeof(buf), &keySym,
827 &status);
828 int mpkey = vo_x11_lookupkey(keySym);
829 if (mpkey) {
830 mplayer_put_key(vo->key_fifo, mpkey | modifiers);
831 } else if (status == XLookupChars
832 || status == XLookupBoth)
834 struct bstr t = { buf, len };
835 mplayer_put_key_utf8(vo->key_fifo, modifiers, t);
837 } else {
838 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
839 &x11->compose_status);
840 int mpkey = vo_x11_lookupkey(keySym);
841 if (mpkey)
842 mplayer_put_key(vo->key_fifo, mpkey | modifiers);
844 ret |= VO_EVENT_KEYPRESS;
846 break;
847 case MotionNotify:
848 vo_mouse_movement(vo, Event.xmotion.x, Event.xmotion.y);
850 if (opts->cursor_autohide_delay > -2) {
851 vo_showcursor(display, x11->window);
852 x11->mouse_waiting_hide = 1;
853 x11->mouse_timer = GetTimerMS();
855 break;
856 case ButtonPress:
857 if (opts->cursor_autohide_delay > -2) {
858 vo_showcursor(display, x11->window);
859 x11->mouse_waiting_hide = 1;
860 x11->mouse_timer = GetTimerMS();
862 mplayer_put_key(vo->key_fifo,
863 (MOUSE_BTN0 + Event.xbutton.button - 1)
864 | MP_KEY_DOWN);
865 break;
866 case ButtonRelease:
867 if (opts->cursor_autohide_delay > -2) {
868 vo_showcursor(display, x11->window);
869 x11->mouse_waiting_hide = 1;
870 x11->mouse_timer = GetTimerMS();
872 mplayer_put_key(vo->key_fifo,
873 MOUSE_BTN0 + Event.xbutton.button - 1);
874 break;
875 case PropertyNotify:
877 char *name =
878 XGetAtomName(display, Event.xproperty.atom);
880 if (!name)
881 break;
883 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
885 XFree(name);
887 break;
888 case MapNotify:
889 x11->vo_hint.win_gravity = x11->old_gravity;
890 XSetWMNormalHints(display, x11->window, &x11->vo_hint);
891 x11->fs_flip = 0;
892 break;
893 case DestroyNotify:
894 mp_msg(MSGT_VO, MSGL_WARN, "Our window was destroyed, exiting\n");
895 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
896 break;
897 case ClientMessage:
898 if (Event.xclient.message_type == x11->XAWM_PROTOCOLS &&
899 Event.xclient.data.l[0] == x11->XAWM_DELETE_WINDOW)
900 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
901 break;
904 return ret;
908 * \brief sets the size and position of the non-fullscreen window.
910 static void vo_x11_nofs_sizepos(struct vo *vo, int x, int y,
911 int width, int height)
913 struct vo_x11_state *x11 = vo->x11;
914 if (width == x11->last_video_width && height == x11->last_video_height) {
915 if (!vo->opts->force_window_position && !x11->size_changed_during_fs)
916 return;
917 } else if (vo_fs)
918 x11->size_changed_during_fs = true;
919 x11->last_video_height = height;
920 x11->last_video_width = width;
921 vo_x11_sizehint(vo, x, y, width, height, 0);
922 if (vo_fs) {
923 x11->vo_old_x = x;
924 x11->vo_old_y = y;
925 x11->vo_old_width = width;
926 x11->vo_old_height = height;
928 else
930 vo->dwidth = width;
931 vo->dheight = height;
932 if (vo->opts->force_window_position)
933 XMoveResizeWindow(vo->x11->display, vo->x11->window, x, y, width,
934 height);
935 else
936 XResizeWindow(vo->x11->display, vo->x11->window, width, height);
940 void vo_x11_sizehint(struct vo *vo, int x, int y, int width, int height, int max)
942 struct vo_x11_state *x11 = vo->x11;
943 x11->vo_hint.flags = 0;
944 if (vo_keepaspect)
946 x11->vo_hint.flags |= PAspect;
947 x11->vo_hint.min_aspect.x = width;
948 x11->vo_hint.min_aspect.y = height;
949 x11->vo_hint.max_aspect.x = width;
950 x11->vo_hint.max_aspect.y = height;
953 x11->vo_hint.flags |= PPosition | PSize;
954 x11->vo_hint.x = x;
955 x11->vo_hint.y = y;
956 x11->vo_hint.width = width;
957 x11->vo_hint.height = height;
958 if (max)
960 x11->vo_hint.flags |= PMaxSize;
961 x11->vo_hint.max_width = width;
962 x11->vo_hint.max_height = height;
963 } else
965 x11->vo_hint.max_width = 0;
966 x11->vo_hint.max_height = 0;
969 // Set minimum height/width to 4 to avoid off-by-one errors
970 // and because mga_vid requires a minimal size of 4 pixels.
971 x11->vo_hint.flags |= PMinSize;
972 x11->vo_hint.min_width = x11->vo_hint.min_height = 4;
974 // Set the base size. A window manager might display the window
975 // size to the user relative to this.
976 // Setting these to width/height might be nice, but e.g. fluxbox can't handle it.
977 x11->vo_hint.flags |= PBaseSize;
978 x11->vo_hint.base_width = 0 /*width*/;
979 x11->vo_hint.base_height = 0 /*height*/;
981 x11->vo_hint.flags |= PWinGravity;
982 x11->vo_hint.win_gravity = StaticGravity;
983 XSetWMNormalHints(x11->display, x11->window, &x11->vo_hint);
986 static int vo_x11_get_gnome_layer(struct vo_x11_state *x11, Window win)
988 Atom type;
989 int format;
990 unsigned long nitems;
991 unsigned long bytesafter;
992 unsigned short *args = NULL;
994 if (XGetWindowProperty(x11->display, win, x11->XA_WIN_LAYER, 0, 16384,
995 False, AnyPropertyType, &type, &format, &nitems,
996 &bytesafter,
997 (unsigned char **) &args) == Success
998 && nitems > 0 && args)
1000 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
1001 *args);
1002 return *args;
1004 return WIN_LAYER_NORMAL;
1007 // set a X text property that expects a UTF8_STRING type
1008 static void vo_x11_set_property_utf8(struct vo *vo, Atom name, const char *t)
1010 struct vo_x11_state *x11 = vo->x11;
1012 XChangeProperty(x11->display, x11->window, name, x11->XAUTF8_STRING, 8,
1013 PropModeReplace, t, strlen(t));
1016 // set a X text property that expects a STRING or COMPOUND_TEXT type
1017 static void vo_x11_set_property_string(struct vo *vo, Atom name, const char *t)
1019 struct vo_x11_state *x11 = vo->x11;
1020 XTextProperty prop = {0};
1022 if (Xutf8TextListToTextProperty(x11->display, (char **)&t, 1,
1023 XStdICCTextStyle, &prop) == Success) {
1024 XSetTextProperty(x11->display, x11->window, &prop, name);
1025 } else {
1026 // Strictly speaking this violates the ICCCM, but there's no way we
1027 // can do this correctly.
1028 vo_x11_set_property_utf8(vo, name, t);
1031 if (prop.value)
1032 XFree(prop.value);
1035 static void vo_x11_update_window_title(struct vo *vo)
1037 struct vo_x11_state *x11 = vo->x11;
1039 const char *title = vo_get_window_title(vo);
1040 vo_x11_set_property_string(vo, XA_WM_NAME, title);
1041 vo_x11_set_property_string(vo, XA_WM_ICON_NAME, title);
1042 vo_x11_set_property_utf8(vo, x11->XA_NET_WM_NAME, title);
1043 vo_x11_set_property_utf8(vo, x11->XA_NET_WM_ICON_NAME, title);
1047 static Window vo_x11_create_smooth_window(struct vo_x11_state *x11, Window mRoot,
1048 Visual * vis, int x, int y,
1049 unsigned int width, unsigned int height,
1050 int depth, Colormap col_map)
1052 unsigned long xswamask = CWBorderPixel;
1053 XSetWindowAttributes xswa;
1054 Window ret_win;
1056 if (col_map != CopyFromParent)
1058 xswa.colormap = col_map;
1059 xswamask |= CWColormap;
1061 xswa.background_pixel = 0;
1062 xswa.border_pixel = 0;
1063 xswa.backing_store = NotUseful;
1064 xswa.bit_gravity = StaticGravity;
1066 ret_win =
1067 XCreateWindow(x11->display, x11->rootwin, x, y, width, height, 0, depth,
1068 CopyFromParent, vis, xswamask, &xswa);
1069 XSetWMProtocols(x11->display, ret_win, &x11->XAWM_DELETE_WINDOW, 1);
1070 if (x11->f_gc == None)
1071 x11->f_gc = XCreateGC(x11->display, ret_win, 0, 0);
1072 XSetForeground(x11->display, x11->f_gc, 0);
1074 return ret_win;
1078 * \brief create and setup a window suitable for display
1079 * \param vis Visual to use for creating the window
1080 * \param x x position of window
1081 * \param y y position of window
1082 * \param width width of window
1083 * \param height height of window
1084 * \param flags flags for window creation.
1085 * Only VOFLAG_FULLSCREEN is supported so far.
1086 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1087 * \param classname name to use for the classhint
1089 * This also does the grunt-work like setting Window Manager hints etc.
1090 * If vo_window is already set it just moves and resizes it.
1092 void vo_x11_create_vo_window(struct vo *vo, XVisualInfo *vis, int x, int y,
1093 unsigned int width, unsigned int height, int flags,
1094 Colormap col_map, const char *classname)
1096 struct MPOpts *opts = vo->opts;
1097 struct vo_x11_state *x11 = vo->x11;
1098 Display *mDisplay = vo->x11->display;
1099 if (WinID >= 0) {
1100 vo_fs = flags & VOFLAG_FULLSCREEN;
1101 x11->window = WinID ? (Window)WinID : x11->rootwin;
1102 if (col_map != CopyFromParent) {
1103 unsigned long xswamask = CWColormap;
1104 XSetWindowAttributes xswa;
1105 xswa.colormap = col_map;
1106 XChangeWindowAttributes(mDisplay, x11->window, xswamask, &xswa);
1107 XInstallColormap(mDisplay, col_map);
1109 if (WinID) {
1110 // Expose events can only really be handled by us, so request them.
1111 vo_x11_selectinput_witherr(mDisplay, x11->window, ExposureMask);
1112 } else
1113 // Do not capture events since it might break the parent application
1114 // if it relies on events being forwarded to the parent of WinID.
1115 // It also is consistent with the w32_common.c code.
1116 vo_x11_selectinput_witherr(mDisplay, x11->window,
1117 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1118 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1120 vo_x11_update_geometry(vo, true);
1121 goto final;
1123 if (x11->window == None) {
1124 vo_fs = 0;
1125 vo->dwidth = width;
1126 vo->dheight = height;
1127 x11->window = vo_x11_create_smooth_window(x11, x11->rootwin, vis->visual,
1128 x, y, width, height, vis->depth, col_map);
1129 x11->window_state = VOFLAG_HIDDEN;
1131 if (flags & VOFLAG_HIDDEN)
1132 goto final;
1133 if (x11->window_state & VOFLAG_HIDDEN) {
1134 XSizeHints hint;
1135 x11->window_state &= ~VOFLAG_HIDDEN;
1136 vo_x11_classhint(vo, x11->window, classname);
1137 vo_hidecursor(mDisplay, x11->window);
1138 XSelectInput(mDisplay, x11->window, StructureNotifyMask);
1139 hint.x = x; hint.y = y;
1140 hint.width = width; hint.height = height;
1141 hint.flags = PSize;
1142 if (geometry_xy_changed)
1143 hint.flags |= PPosition;
1144 XSetWMNormalHints(mDisplay, x11->window, &hint);
1145 if (!vo_border) vo_x11_decoration(vo, 0);
1146 // map window
1147 x11->xic = XCreateIC(x11->xim,
1148 XNInputStyle, XIMPreeditNone | XIMStatusNone,
1149 XNClientWindow, x11->window,
1150 XNFocusWindow, x11->window,
1151 NULL);
1152 XSelectInput(mDisplay, x11->window, NoEventMask);
1153 vo_x11_selectinput_witherr(mDisplay, x11->window,
1154 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1155 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1156 XMapWindow(mDisplay, x11->window);
1157 vo_x11_clearwindow(vo, x11->window);
1159 vo_x11_update_window_title(vo);
1160 if (opts->vo_ontop) vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1161 vo_x11_update_geometry(vo, !geometry_xy_changed);
1162 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, width, height);
1163 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1164 vo_x11_fullscreen(vo);
1165 else if (vo_fs) {
1166 // if we are already in fullscreen do not switch back and forth, just
1167 // set the size values right.
1168 vo->dwidth = vo->opts->vo_screenwidth;
1169 vo->dheight = vo->opts->vo_screenheight;
1171 final:
1172 if (x11->vo_gc != None)
1173 XFreeGC(mDisplay, x11->vo_gc);
1174 x11->vo_gc = XCreateGC(mDisplay, x11->window, 0, NULL);
1176 XSync(mDisplay, False);
1177 vo->event_fd = ConnectionNumber(x11->display);
1180 void vo_x11_clearwindow_part(struct vo *vo, Window vo_window,
1181 int img_width, int img_height)
1183 struct vo_x11_state *x11 = vo->x11;
1184 Display *mDisplay = vo->x11->display;
1185 int u_dheight, u_dwidth, left_ov, left_ov2;
1187 if (x11->f_gc == None)
1188 return;
1190 u_dheight = vo->dheight;
1191 u_dwidth = vo->dwidth;
1192 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1193 return;
1195 left_ov = (u_dheight - img_height) / 2;
1196 left_ov2 = (u_dwidth - img_width) / 2;
1198 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, 0, u_dwidth, left_ov);
1199 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, u_dheight - left_ov - 1,
1200 u_dwidth, left_ov + 1);
1202 if (u_dwidth > img_width)
1204 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, left_ov, left_ov2,
1205 img_height);
1206 XFillRectangle(mDisplay, vo_window, x11->f_gc, u_dwidth - left_ov2 - 1,
1207 left_ov, left_ov2 + 1, img_height);
1210 XFlush(mDisplay);
1213 void vo_x11_clearwindow(struct vo *vo, Window vo_window)
1215 struct vo_x11_state *x11 = vo->x11;
1216 struct MPOpts *opts = vo->opts;
1217 if (x11->f_gc == None)
1218 return;
1219 XFillRectangle(x11->display, vo_window, x11->f_gc, 0, 0,
1220 opts->vo_screenwidth, opts->vo_screenheight);
1222 XFlush(x11->display);
1226 void vo_x11_setlayer(struct vo *vo, Window vo_window, int layer)
1228 struct vo_x11_state *x11 = vo->x11;
1229 if (WinID >= 0)
1230 return;
1232 if (x11->fs_type & vo_wm_LAYER)
1234 XClientMessageEvent xev;
1236 if (!x11->orig_layer)
1237 x11->orig_layer = vo_x11_get_gnome_layer(x11, vo_window);
1239 memset(&xev, 0, sizeof(xev));
1240 xev.type = ClientMessage;
1241 xev.display = x11->display;
1242 xev.window = vo_window;
1243 xev.message_type = x11->XA_WIN_LAYER;
1244 xev.format = 32;
1245 xev.data.l[0] = layer ? fs_layer : x11->orig_layer; // if not fullscreen, stay on default layer
1246 xev.data.l[1] = CurrentTime;
1247 mp_msg(MSGT_VO, MSGL_V,
1248 "[x11] Layered style stay on top (layer %ld).\n",
1249 xev.data.l[0]);
1250 XSendEvent(x11->display, x11->rootwin, False, SubstructureNotifyMask,
1251 (XEvent *) & xev);
1252 } else if (x11->fs_type & vo_wm_NETWM)
1254 XClientMessageEvent xev;
1255 char *state;
1257 memset(&xev, 0, sizeof(xev));
1258 xev.type = ClientMessage;
1259 xev.message_type = x11->XA_NET_WM_STATE;
1260 xev.display = x11->display;
1261 xev.window = vo_window;
1262 xev.format = 32;
1263 xev.data.l[0] = layer;
1265 if (x11->fs_type & vo_wm_STAYS_ON_TOP)
1266 xev.data.l[1] = x11->XA_NET_WM_STATE_STAYS_ON_TOP;
1267 else if (x11->fs_type & vo_wm_ABOVE)
1268 xev.data.l[1] = x11->XA_NET_WM_STATE_ABOVE;
1269 else if (x11->fs_type & vo_wm_FULLSCREEN)
1270 xev.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
1271 else if (x11->fs_type & vo_wm_BELOW)
1272 // This is not fallback. We can safely assume that the situation
1273 // where only NETWM_STATE_BELOW is supported doesn't exist.
1274 xev.data.l[1] = x11->XA_NET_WM_STATE_BELOW;
1276 XSendEvent(x11->display, x11->rootwin, False, SubstructureRedirectMask,
1277 (XEvent *) & xev);
1278 state = XGetAtomName(x11->display, xev.data.l[1]);
1279 mp_msg(MSGT_VO, MSGL_V,
1280 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1281 layer, state);
1282 XFree(state);
1286 static int vo_x11_get_fs_type(int supported)
1288 int i;
1289 int type = supported;
1291 if (vo_fstype_list)
1293 for (i = 0; vo_fstype_list[i]; i++)
1295 int neg = 0;
1296 char *arg = vo_fstype_list[i];
1298 if (vo_fstype_list[i][0] == '-')
1300 neg = 1;
1301 arg = vo_fstype_list[i] + 1;
1304 if (!strncmp(arg, "layer", 5))
1306 if (!neg && (arg[5] == '='))
1308 char *endptr = NULL;
1309 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1311 if (endptr && *endptr == '\0' && layer >= 0
1312 && layer <= 15)
1313 fs_layer = layer;
1315 if (neg)
1316 type &= ~vo_wm_LAYER;
1317 else
1318 type |= vo_wm_LAYER;
1319 } else if (!strcmp(arg, "above"))
1321 if (neg)
1322 type &= ~vo_wm_ABOVE;
1323 else
1324 type |= vo_wm_ABOVE;
1325 } else if (!strcmp(arg, "fullscreen"))
1327 if (neg)
1328 type &= ~vo_wm_FULLSCREEN;
1329 else
1330 type |= vo_wm_FULLSCREEN;
1331 } else if (!strcmp(arg, "stays_on_top"))
1333 if (neg)
1334 type &= ~vo_wm_STAYS_ON_TOP;
1335 else
1336 type |= vo_wm_STAYS_ON_TOP;
1337 } else if (!strcmp(arg, "below"))
1339 if (neg)
1340 type &= ~vo_wm_BELOW;
1341 else
1342 type |= vo_wm_BELOW;
1343 } else if (!strcmp(arg, "netwm"))
1345 if (neg)
1346 type &= ~vo_wm_NETWM;
1347 else
1348 type |= vo_wm_NETWM;
1349 } else if (!strcmp(arg, "none"))
1350 type = 0; // clear; keep parsing
1354 return type;
1358 * \brief update vo->dx, vo->dy, vo->dwidth and vo->dheight with current values of vo->x11->window
1359 * \return returns current color depth of vo->x11->window
1361 int vo_x11_update_geometry(struct vo *vo, bool update_pos)
1363 struct vo_x11_state *x11 = vo->x11;
1364 unsigned depth, w, h;
1365 int dummy_int;
1366 Window dummy_win;
1367 XGetGeometry(x11->display, x11->window, &dummy_win, &dummy_int, &dummy_int,
1368 &w, &h, &dummy_int, &depth);
1369 if (w <= INT_MAX && h <= INT_MAX) {
1370 vo->dwidth = w;
1371 vo->dheight = h;
1373 if (update_pos)
1374 XTranslateCoordinates(x11->display, x11->window, x11->rootwin, 0, 0,
1375 &vo->dx, &vo->dy, &dummy_win);
1377 return depth <= INT_MAX ? depth : 0;
1380 void vo_x11_fullscreen(struct vo *vo)
1382 struct MPOpts *opts = vo->opts;
1383 struct vo_x11_state *x11 = vo->x11;
1384 int x, y, w, h;
1385 x = x11->vo_old_x;
1386 y = x11->vo_old_y;
1387 w = x11->vo_old_width;
1388 h = x11->vo_old_height;
1390 if (WinID >= 0) {
1391 vo_fs = !vo_fs;
1392 return;
1394 if (x11->fs_flip)
1395 return;
1397 if (vo_fs)
1399 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1400 vo_fs = VO_FALSE;
1401 if (x11->size_changed_during_fs && (x11->fs_type & vo_wm_FULLSCREEN))
1402 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, x11->last_video_width,
1403 x11->last_video_height);
1404 x11->size_changed_during_fs = false;
1405 } else
1407 // win->fs
1408 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1410 vo_fs = VO_TRUE;
1411 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1413 x11->vo_old_x = vo->dx;
1414 x11->vo_old_y = vo->dy;
1415 x11->vo_old_width = vo->dwidth;
1416 x11->vo_old_height = vo->dheight;
1418 update_xinerama_info(vo);
1419 x = xinerama_x;
1420 y = xinerama_y;
1421 w = opts->vo_screenwidth;
1422 h = opts->vo_screenheight;
1425 long dummy;
1427 XGetWMNormalHints(x11->display, x11->window, &x11->vo_hint, &dummy);
1428 if (!(x11->vo_hint.flags & PWinGravity))
1429 x11->old_gravity = NorthWestGravity;
1430 else
1431 x11->old_gravity = x11->vo_hint.win_gravity;
1433 if (x11->wm_type == 0 && !(vo_fsmode & 16))
1435 XUnmapWindow(x11->display, x11->window); // required for MWM
1436 XWithdrawWindow(x11->display, x11->window, x11->screen);
1437 x11->fs_flip = 1;
1440 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1442 vo_x11_decoration(vo, vo_border && !vo_fs);
1443 vo_x11_sizehint(vo, x, y, w, h, 0);
1444 vo_x11_setlayer(vo, x11->window, vo_fs);
1447 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1449 /* some WMs lose ontop after fullscreen */
1450 if ((!(vo_fs)) & opts->vo_ontop)
1451 vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1453 XMapRaised(x11->display, x11->window);
1454 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1455 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1456 XRaiseWindow(x11->display, x11->window);
1457 XFlush(x11->display);
1460 void vo_x11_ontop(struct vo *vo)
1462 struct MPOpts *opts = vo->opts;
1463 opts->vo_ontop = !opts->vo_ontop;
1465 vo_x11_setlayer(vo, vo->x11->window, opts->vo_ontop);
1468 void vo_x11_border(struct vo *vo)
1470 vo_border = !vo_border;
1471 vo_x11_decoration(vo, vo_border && !vo_fs);
1475 * XScreensaver stuff
1478 static int screensaver_off;
1479 static unsigned int time_last;
1481 void xscreensaver_heartbeat(struct vo_x11_state *x11)
1483 unsigned int time = GetTimerMS();
1485 if (x11->display && screensaver_off && (time - time_last) > 30000)
1487 time_last = time;
1489 XResetScreenSaver(x11->display);
1493 static int xss_suspend(Display *mDisplay, Bool suspend)
1495 #ifndef CONFIG_XSS
1496 return 0;
1497 #else
1498 int event, error, major, minor;
1499 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1500 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1501 return 0;
1502 if (major < 1 || (major == 1 && minor < 1))
1503 return 0;
1504 XScreenSaverSuspend(mDisplay, suspend);
1505 return 1;
1506 #endif
1510 * End of XScreensaver stuff
1513 static void saver_on(Display * mDisplay)
1516 if (!screensaver_off)
1517 return;
1518 screensaver_off = 0;
1519 if (xss_suspend(mDisplay, False))
1520 return;
1521 #ifdef CONFIG_XDPMS
1522 if (dpms_disabled)
1524 int nothing;
1525 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1527 if (!DPMSEnable(mDisplay))
1528 { // restoring power saving settings
1529 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1530 } else
1532 // DPMS does not seem to be enabled unless we call DPMSInfo
1533 BOOL onoff;
1534 CARD16 state;
1536 DPMSForceLevel(mDisplay, DPMSModeOn);
1537 DPMSInfo(mDisplay, &state, &onoff);
1538 if (onoff)
1540 mp_msg(MSGT_VO, MSGL_V,
1541 "Successfully enabled DPMS\n");
1542 } else
1544 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1548 dpms_disabled = 0;
1550 #endif
1553 static void saver_off(Display * mDisplay)
1555 int nothing;
1557 if (screensaver_off)
1558 return;
1559 screensaver_off = 1;
1560 if (xss_suspend(mDisplay, True))
1561 return;
1562 #ifdef CONFIG_XDPMS
1563 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1565 BOOL onoff;
1566 CARD16 state;
1568 DPMSInfo(mDisplay, &state, &onoff);
1569 if (onoff)
1571 Status stat;
1573 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1574 dpms_disabled = 1;
1575 stat = DPMSDisable(mDisplay); // monitor powersave off
1576 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1579 #endif
1582 static XErrorHandler old_handler = NULL;
1583 static int selectinput_err = 0;
1584 static int x11_selectinput_errorhandler(Display * display,
1585 XErrorEvent * event)
1587 if (event->error_code == BadAccess)
1589 selectinput_err = 1;
1590 mp_msg(MSGT_VO, MSGL_ERR,
1591 "X11 error: BadAccess during XSelectInput Call\n");
1592 mp_msg(MSGT_VO, MSGL_ERR,
1593 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1594 /* If you think MPlayer should shutdown with this error,
1595 * comment out the following line */
1596 return 0;
1598 if (old_handler != NULL)
1599 old_handler(display, event);
1600 else
1601 x11_errorhandler(display, event);
1602 return 0;
1605 void vo_x11_selectinput_witherr(Display * display, Window w,
1606 long event_mask)
1608 XSync(display, False);
1609 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1610 selectinput_err = 0;
1611 if (vo_nomouse_input)
1613 XSelectInput(display, w,
1614 event_mask &
1615 (~(ButtonPressMask | ButtonReleaseMask)));
1616 } else
1618 XSelectInput(display, w, event_mask);
1620 XSync(display, False);
1621 XSetErrorHandler(old_handler);
1622 if (selectinput_err)
1624 mp_msg(MSGT_VO, MSGL_ERR,
1625 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1626 XSelectInput(display, w,
1627 event_mask &
1629 (ButtonPressMask | ButtonReleaseMask |
1630 PointerMotionMask)));
1634 #ifdef CONFIG_XF86VM
1635 void vo_vm_switch(struct vo *vo)
1637 struct vo_x11_state *x11 = vo->x11;
1638 struct MPOpts *opts = vo->opts;
1639 Display *mDisplay = x11->display;
1640 int vm_event, vm_error;
1641 int vm_ver, vm_rev;
1642 int i, j, have_vm = 0;
1643 int X = vo->dwidth, Y = vo->dheight;
1644 int modeline_width, modeline_height;
1646 int modecount;
1648 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1650 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1651 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1652 vm_rev);
1653 have_vm = 1;
1654 } else {
1655 mp_msg(MSGT_VO, MSGL_WARN,
1656 "XF86VidMode extension not available.\n");
1659 if (have_vm)
1661 if (vidmodes == NULL)
1662 XF86VidModeGetAllModeLines(mDisplay, x11->screen, &modecount,
1663 &vidmodes);
1664 j = 0;
1665 modeline_width = vidmodes[0]->hdisplay;
1666 modeline_height = vidmodes[0]->vdisplay;
1668 for (i = 1; i < modecount; i++)
1669 if ((vidmodes[i]->hdisplay >= X)
1670 && (vidmodes[i]->vdisplay >= Y))
1671 if ((vidmodes[i]->hdisplay <= modeline_width)
1672 && (vidmodes[i]->vdisplay <= modeline_height))
1674 modeline_width = vidmodes[i]->hdisplay;
1675 modeline_height = vidmodes[i]->vdisplay;
1676 j = i;
1679 mp_tmsg(MSGT_VO, MSGL_INFO, "XF86VM: Selected video mode %dx%d for image size %dx%d.\n",
1680 modeline_width, modeline_height, X, Y);
1681 XF86VidModeLockModeSwitch(mDisplay, x11->screen, 0);
1682 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1683 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1685 // FIXME: all this is more of a hack than proper solution
1686 X = (opts->vo_screenwidth - modeline_width) / 2;
1687 Y = (opts->vo_screenheight - modeline_height) / 2;
1688 XF86VidModeSetViewPort(mDisplay, x11->screen, X, Y);
1689 vo->dx = X;
1690 vo->dy = Y;
1691 vo->dwidth = modeline_width;
1692 vo->dheight = modeline_height;
1693 aspect_save_screenres(vo, modeline_width, modeline_height);
1697 void vo_vm_close(struct vo *vo)
1699 Display *dpy = vo->x11->display;
1700 struct MPOpts *opts = vo->opts;
1701 if (vidmodes != NULL)
1703 int i, modecount;
1705 free(vidmodes);
1706 vidmodes = NULL;
1707 XF86VidModeGetAllModeLines(dpy, vo->x11->screen, &modecount,
1708 &vidmodes);
1709 for (i = 0; i < modecount; i++)
1710 if ((vidmodes[i]->hdisplay == opts->vo_screenwidth)
1711 && (vidmodes[i]->vdisplay == opts->vo_screenheight))
1713 mp_msg(MSGT_VO, MSGL_INFO,
1714 "Returning to original mode %dx%d\n",
1715 opts->vo_screenwidth, opts->vo_screenheight);
1716 break;
1719 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1720 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1721 free(vidmodes);
1722 vidmodes = NULL;
1726 double vo_vm_get_fps(struct vo *vo)
1728 struct vo_x11_state *x11 = vo->x11;
1729 int clock;
1730 XF86VidModeModeLine modeline;
1731 if (!XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline))
1732 return 0;
1733 if (modeline.privsize)
1734 XFree(modeline.private);
1735 return 1e3 * clock / modeline.htotal / modeline.vtotal;
1737 #endif
1739 #endif /* X11_FULLSCREEN */
1743 * Scan the available visuals on this Display/Screen. Try to find
1744 * the 'best' available TrueColor visual that has a decent color
1745 * depth (at least 15bit). If there are multiple visuals with depth
1746 * >= 15bit, we prefer visuals with a smaller color depth.
1748 int vo_find_depth_from_visuals(Display * dpy, int screen,
1749 Visual ** visual_return)
1751 XVisualInfo visual_tmpl;
1752 XVisualInfo *visuals;
1753 int nvisuals, i;
1754 int bestvisual = -1;
1755 int bestvisual_depth = -1;
1757 visual_tmpl.screen = screen;
1758 visual_tmpl.class = TrueColor;
1759 visuals = XGetVisualInfo(dpy,
1760 VisualScreenMask | VisualClassMask,
1761 &visual_tmpl, &nvisuals);
1762 if (visuals != NULL)
1764 for (i = 0; i < nvisuals; i++)
1766 mp_msg(MSGT_VO, MSGL_V,
1767 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1768 visuals[i].visualid, visuals[i].depth,
1769 visuals[i].red_mask, visuals[i].green_mask,
1770 visuals[i].blue_mask);
1772 * Save the visual index and its depth, if this is the first
1773 * truecolor visul, or a visual that is 'preferred' over the
1774 * previous 'best' visual.
1776 if (bestvisual_depth == -1
1777 || (visuals[i].depth >= 15
1778 && (visuals[i].depth < bestvisual_depth
1779 || bestvisual_depth < 15)))
1781 bestvisual = i;
1782 bestvisual_depth = visuals[i].depth;
1786 if (bestvisual != -1 && visual_return != NULL)
1787 *visual_return = visuals[bestvisual].visual;
1789 XFree(visuals);
1791 return bestvisual_depth;
1795 static Colormap cmap = None;
1796 static XColor cols[256];
1797 static int cm_size, red_mask, green_mask, blue_mask;
1800 Colormap vo_x11_create_colormap(struct vo *vo, XVisualInfo *vinfo)
1802 struct vo_x11_state *x11 = vo->x11;
1803 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1805 if (vinfo->class != DirectColor)
1806 return XCreateColormap(x11->display, x11->rootwin, vinfo->visual,
1807 AllocNone);
1809 /* can this function get called twice or more? */
1810 if (cmap)
1811 return cmap;
1812 cm_size = vinfo->colormap_size;
1813 red_mask = vinfo->red_mask;
1814 green_mask = vinfo->green_mask;
1815 blue_mask = vinfo->blue_mask;
1816 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1817 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1818 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1819 rvu = 65536ull * ru / (red_mask + ru);
1820 gvu = 65536ull * gu / (green_mask + gu);
1821 bvu = 65536ull * bu / (blue_mask + bu);
1822 r = g = b = 0;
1823 rv = gv = bv = 0;
1824 m = DoRed | DoGreen | DoBlue;
1825 for (k = 0; k < cm_size; k++)
1827 int t;
1829 cols[k].pixel = r | g | b;
1830 cols[k].red = rv;
1831 cols[k].green = gv;
1832 cols[k].blue = bv;
1833 cols[k].flags = m;
1834 t = (r + ru) & red_mask;
1835 if (t < r)
1836 m &= ~DoRed;
1837 r = t;
1838 t = (g + gu) & green_mask;
1839 if (t < g)
1840 m &= ~DoGreen;
1841 g = t;
1842 t = (b + bu) & blue_mask;
1843 if (t < b)
1844 m &= ~DoBlue;
1845 b = t;
1846 rv += rvu;
1847 gv += gvu;
1848 bv += bvu;
1850 cmap = XCreateColormap(x11->display, x11->rootwin, vinfo->visual, AllocAll);
1851 XStoreColors(x11->display, cmap, cols, cm_size);
1852 return cmap;
1856 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1857 * hue and red/green/blue intensity, but we cannot do saturation.
1858 * Currently only gamma, brightness and contrast are implemented.
1859 * Is there sufficient interest for hue and/or red/green/blue intensity?
1861 /* these values have range [-100,100] and are initially 0 */
1862 static int vo_gamma = 0;
1863 static int vo_brightness = 0;
1864 static int vo_contrast = 0;
1866 static int transform_color(float val,
1867 float brightness, float contrast, float gamma) {
1868 float s = pow(val, gamma);
1869 s = (s - 0.5) * contrast + 0.5;
1870 s += brightness;
1871 if (s < 0)
1872 s = 0;
1873 if (s > 1)
1874 s = 1;
1875 return (unsigned short) (s * 65535);
1878 uint32_t vo_x11_set_equalizer(struct vo *vo, const char *name, int value)
1880 float gamma, brightness, contrast;
1881 float rf, gf, bf;
1882 int k;
1885 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1886 * of TrueColor-ed window but be careful:
1887 * Unlike the colormaps, which are private for the X client
1888 * who created them and thus automatically destroyed on client
1889 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1890 * setting and _must_ be restored before the process exits.
1891 * Unforunately when the process crashes (or gets killed
1892 * for some reason) it is impossible to restore the setting,
1893 * and such behaviour could be rather annoying for the users.
1895 if (cmap == None)
1896 return VO_NOTAVAIL;
1898 if (!strcasecmp(name, "brightness"))
1899 vo_brightness = value;
1900 else if (!strcasecmp(name, "contrast"))
1901 vo_contrast = value;
1902 else if (!strcasecmp(name, "gamma"))
1903 vo_gamma = value;
1904 else
1905 return VO_NOTIMPL;
1907 brightness = 0.01 * vo_brightness;
1908 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1909 gamma = pow(2, -0.02 * vo_gamma);
1911 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1912 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
1913 green_mask;
1914 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
1916 /* now recalculate the colormap using the newly set value */
1917 for (k = 0; k < cm_size; k++)
1919 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
1920 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
1921 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
1924 XStoreColors(vo->x11->display, cmap, cols, cm_size);
1925 XFlush(vo->x11->display);
1926 return VO_TRUE;
1929 uint32_t vo_x11_get_equalizer(const char *name, int *value)
1931 if (cmap == None)
1932 return VO_NOTAVAIL;
1933 if (!strcasecmp(name, "brightness"))
1934 *value = vo_brightness;
1935 else if (!strcasecmp(name, "contrast"))
1936 *value = vo_contrast;
1937 else if (!strcasecmp(name, "gamma"))
1938 *value = vo_gamma;
1939 else
1940 return VO_NOTIMPL;
1941 return VO_TRUE;
1944 #ifdef CONFIG_XV
1945 int vo_xv_set_eq(struct vo *vo, uint32_t xv_port, const char *name, int value)
1947 XvAttribute *attributes;
1948 int i, howmany, xv_atom;
1950 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
1952 /* get available attributes */
1953 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1954 for (i = 0; i < howmany && attributes; i++)
1955 if (attributes[i].flags & XvSettable)
1957 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1958 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1959 then trigger it if it's ok so that the other values are at default upon query */
1960 if (xv_atom != None)
1962 int hue = 0, port_value, port_min, port_max;
1964 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1965 (!strcasecmp(name, "brightness")))
1966 port_value = value;
1967 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1968 (!strcasecmp(name, "contrast")))
1969 port_value = value;
1970 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1971 (!strcasecmp(name, "saturation")))
1972 port_value = value;
1973 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1974 (!strcasecmp(name, "hue")))
1976 port_value = value;
1977 hue = 1;
1978 } else
1979 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1980 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1981 (!strcasecmp(name, "red_intensity")))
1982 port_value = value;
1983 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1984 && (!strcasecmp(name, "green_intensity")))
1985 port_value = value;
1986 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1987 && (!strcasecmp(name, "blue_intensity")))
1988 port_value = value;
1989 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
1990 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
1991 && (!strcasecmp(name, "bt_709")))
1992 port_value = value;
1993 else
1994 continue;
1996 port_min = attributes[i].min_value;
1997 port_max = attributes[i].max_value;
1999 /* nvidia hue workaround */
2000 if (hue && port_min == 0 && port_max == 360)
2002 port_value =
2003 (port_value >=
2004 0) ? (port_value - 100) : (port_value + 100);
2006 // -100 -> min
2007 // 0 -> (max+min)/2
2008 // +100 -> max
2009 port_value =
2010 (port_value + 100) * (port_max - port_min) / 200 +
2011 port_min;
2012 XvSetPortAttribute(vo->x11->display, xv_port, xv_atom, port_value);
2013 return VO_TRUE;
2016 return VO_FALSE;
2019 int vo_xv_get_eq(struct vo *vo, uint32_t xv_port, const char *name, int *value)
2022 XvAttribute *attributes;
2023 int i, howmany, xv_atom;
2025 /* get available attributes */
2026 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
2027 for (i = 0; i < howmany && attributes; i++)
2028 if (attributes[i].flags & XvGettable)
2030 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
2031 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2032 then trigger it if it's ok so that the other values are at default upon query */
2033 if (xv_atom != None)
2035 int val, port_value = 0, port_min, port_max;
2037 XvGetPortAttribute(vo->x11->display, xv_port, xv_atom,
2038 &port_value);
2040 port_min = attributes[i].min_value;
2041 port_max = attributes[i].max_value;
2042 val =
2043 (port_value - port_min) * 200 / (port_max - port_min) -
2044 100;
2046 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2047 (!strcasecmp(name, "brightness")))
2048 *value = val;
2049 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2050 (!strcasecmp(name, "contrast")))
2051 *value = val;
2052 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2053 (!strcasecmp(name, "saturation")))
2054 *value = val;
2055 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2056 (!strcasecmp(name, "hue")))
2058 /* nasty nvidia detect */
2059 if (port_min == 0 && port_max == 360)
2060 *value = (val >= 0) ? (val - 100) : (val + 100);
2061 else
2062 *value = val;
2063 } else
2064 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2065 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2066 (!strcasecmp(name, "red_intensity")))
2067 *value = val;
2068 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2069 && (!strcasecmp(name, "green_intensity")))
2070 *value = val;
2071 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2072 && (!strcasecmp(name, "blue_intensity")))
2073 *value = val;
2074 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
2075 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
2076 && (!strcasecmp(name, "bt_709")))
2077 *value = val;
2078 else
2079 continue;
2081 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2082 name, *value);
2083 return VO_TRUE;
2086 return VO_FALSE;
2090 * \brief Interns the requested atom if it is available.
2092 * \param atom_name String containing the name of the requested atom.
2094 * \return Returns the atom if available, else None is returned.
2097 static Atom xv_intern_atom_if_exists(struct vo_x11_state *x11,
2098 char const *atom_name)
2100 XvAttribute * attributes;
2101 int attrib_count,i;
2102 Atom xv_atom = None;
2104 attributes = XvQueryPortAttributes(x11->display, x11->xv_port, &attrib_count );
2105 if( attributes!=NULL )
2107 for ( i = 0; i < attrib_count; ++i )
2109 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2111 xv_atom = XInternAtom(x11->display, atom_name, False );
2112 break; // found what we want, break out
2115 XFree( attributes );
2118 return xv_atom;
2122 * \brief Try to enable vsync for xv.
2123 * \return Returns -1 if not available, 0 on failure and 1 on success.
2125 int vo_xv_enable_vsync(struct vo *vo)
2127 struct vo_x11_state *x11 = vo->x11;
2128 Atom xv_atom = xv_intern_atom_if_exists(x11, "XV_SYNC_TO_VBLANK");
2129 if (xv_atom == None)
2130 return -1;
2131 return XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1) == Success;
2135 * \brief Get maximum supported source image dimensions.
2137 * This function does not set the variables pointed to by
2138 * width and height if the information could not be retrieved,
2139 * so the caller is reponsible for properly initializing them.
2141 * \param width [out] The maximum width gets stored here.
2142 * \param height [out] The maximum height gets stored here.
2145 void vo_xv_get_max_img_dim(struct vo *vo, uint32_t * width, uint32_t * height)
2147 struct vo_x11_state *x11 = vo->x11;
2148 XvEncodingInfo * encodings;
2149 //unsigned long num_encodings, idx; to int or too long?!
2150 unsigned int num_encodings, idx;
2152 XvQueryEncodings(x11->display, x11->xv_port, &num_encodings, &encodings);
2154 if ( encodings )
2156 for ( idx = 0; idx < num_encodings; ++idx )
2158 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2160 *width = encodings[idx].width;
2161 *height = encodings[idx].height;
2162 break;
2167 mp_msg( MSGT_VO, MSGL_V,
2168 "[xv common] Maximum source image dimensions: %ux%u\n",
2169 *width, *height );
2171 XvFreeEncodingInfo( encodings );
2175 * \brief Print information about the colorkey method and source.
2177 * \param ck_handling Integer value containing the information about
2178 * colorkey handling (see x11_common.h).
2180 * Outputs the content of |ck_handling| as a readable message.
2183 static void vo_xv_print_ck_info(struct vo_x11_state *x11)
2185 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2187 switch ( x11->xv_ck_info.method )
2189 case CK_METHOD_NONE:
2190 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2191 case CK_METHOD_AUTOPAINT:
2192 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2193 case CK_METHOD_MANUALFILL:
2194 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2195 case CK_METHOD_BACKGROUND:
2196 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2199 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2201 switch ( x11->xv_ck_info.source )
2203 case CK_SRC_CUR:
2204 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2205 x11->xv_colorkey );
2206 break;
2207 case CK_SRC_USE:
2208 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2210 mp_msg( MSGT_VO, MSGL_V,
2211 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2212 x11->xv_colorkey );
2214 else
2216 mp_msg( MSGT_VO, MSGL_V,
2217 "Using colorkey from MPlayer (0x%06lx)."
2218 " Use -colorkey to change.\n",
2219 x11->xv_colorkey );
2221 break;
2222 case CK_SRC_SET:
2223 mp_msg( MSGT_VO, MSGL_V,
2224 "Setting and using colorkey from MPlayer (0x%06lx)."
2225 " Use -colorkey to change.\n",
2226 x11->xv_colorkey );
2227 break;
2231 * \brief Init colorkey depending on the settings in xv_ck_info.
2233 * \return Returns 0 on failure and 1 on success.
2235 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2236 * flags in xv_ck_info.
2238 * Possiblilities:
2239 * * Methods
2240 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2241 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2242 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2243 * * Sources
2244 * - use currently set colorkey ( CK_SRC_CUR )
2245 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2246 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2248 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2249 * we don't draw anything as this means it was forced to off.
2251 int vo_xv_init_colorkey(struct vo *vo)
2253 struct vo_x11_state *x11 = vo->x11;
2254 Atom xv_atom;
2255 int rez;
2257 /* check if colorkeying is needed */
2258 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_COLORKEY");
2260 /* if we have to deal with colorkeying ... */
2261 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2263 /* check if we should use the colorkey specified in vo_colorkey */
2264 if ( x11->xv_ck_info.source != CK_SRC_CUR )
2266 x11->xv_colorkey = vo_colorkey;
2268 /* check if we have to set the colorkey too */
2269 if ( x11->xv_ck_info.source == CK_SRC_SET )
2271 xv_atom = XInternAtom(x11->display, "XV_COLORKEY",False);
2273 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, vo_colorkey);
2274 if ( rez != Success )
2276 mp_msg( MSGT_VO, MSGL_FATAL,
2277 "[xv common] Couldn't set colorkey!\n" );
2278 return 0; // error setting colorkey
2282 else
2284 int colorkey_ret;
2286 rez=XvGetPortAttribute(x11->display,x11->xv_port, xv_atom, &colorkey_ret);
2287 if ( rez == Success )
2289 x11->xv_colorkey = colorkey_ret;
2291 else
2293 mp_msg( MSGT_VO, MSGL_FATAL,
2294 "[xv common] Couldn't get colorkey!"
2295 "Maybe the selected Xv port has no overlay.\n" );
2296 return 0; // error getting colorkey
2300 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_AUTOPAINT_COLORKEY");
2302 /* should we draw the colorkey ourselves or activate autopainting? */
2303 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2305 rez = !Success; // reset rez to something different than Success
2307 if ( xv_atom != None ) // autopaint is supported
2309 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1);
2312 if ( rez != Success )
2314 // fallback to manual colorkey drawing
2315 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2318 else // disable colorkey autopainting if supported
2320 if ( xv_atom != None ) // we have autopaint attribute
2322 XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 0);
2326 else // do no colorkey drawing at all
2328 x11->xv_ck_info.method = CK_METHOD_NONE;
2329 } /* end: should we draw colorkey */
2331 /* output information about the current colorkey settings */
2332 vo_xv_print_ck_info(x11);
2334 return 1; // success
2338 * \brief Draw the colorkey on the video window.
2340 * Draws the colorkey depending on the set method ( colorkey_handling ).
2342 * Also draws the black bars ( when the video doesn't fit the display in
2343 * fullscreen ) separately, so they don't overlap with the video area.
2344 * It doesn't call XFlush.
2347 void vo_xv_draw_colorkey(struct vo *vo, int32_t x, int32_t y,
2348 int32_t w, int32_t h)
2350 struct vo_x11_state *x11 = vo->x11;
2351 if( x11->xv_ck_info.method == CK_METHOD_MANUALFILL ||
2352 x11->xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2354 XSetForeground(x11->display, x11->vo_gc, x11->xv_colorkey );
2355 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2356 x, y,
2357 w, h );
2361 /** \brief Tests if a valid argument for the ck suboption was given. */
2362 int xv_test_ck( void * arg )
2364 strarg_t * strarg = (strarg_t *)arg;
2366 if ( strargcmp( strarg, "use" ) == 0 ||
2367 strargcmp( strarg, "set" ) == 0 ||
2368 strargcmp( strarg, "cur" ) == 0 )
2370 return 1;
2373 return 0;
2375 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2376 int xv_test_ckm( void * arg )
2378 strarg_t * strarg = (strarg_t *)arg;
2380 if ( strargcmp( strarg, "bg" ) == 0 ||
2381 strargcmp( strarg, "man" ) == 0 ||
2382 strargcmp( strarg, "auto" ) == 0 )
2384 return 1;
2387 return 0;
2391 * \brief Modify the colorkey_handling var according to str
2393 * Checks if a valid pointer ( not NULL ) to the string
2394 * was given. And in that case modifies the colorkey_handling
2395 * var to reflect the requested behaviour.
2396 * If nothing happens the content of colorkey_handling stays
2397 * the same.
2399 * \param str Pointer to the string or NULL
2402 void xv_setup_colorkeyhandling(struct vo *vo, const char *ck_method_str,
2403 const char *ck_str)
2405 struct vo_x11_state *x11 = vo->x11;
2406 /* check if a valid pointer to the string was passed */
2407 if ( ck_str )
2409 if ( strncmp( ck_str, "use", 3 ) == 0 )
2411 x11->xv_ck_info.source = CK_SRC_USE;
2413 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2415 x11->xv_ck_info.source = CK_SRC_SET;
2418 /* check if a valid pointer to the string was passed */
2419 if ( ck_method_str )
2421 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2423 x11->xv_ck_info.method = CK_METHOD_BACKGROUND;
2425 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2427 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2429 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2431 x11->xv_ck_info.method = CK_METHOD_AUTOPAINT;
2436 #endif
2438 struct vo_x11_state *vo_x11_init_state(void)
2440 struct vo_x11_state *s = talloc_ptrtype(NULL, s);
2441 *s = (struct vo_x11_state){
2442 .xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR },
2443 .olddecor = MWM_DECOR_ALL,
2444 .oldfuncs = MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
2445 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE,
2446 .old_gravity = NorthWestGravity,
2448 return s;