gl_common: minor cleanup/refactor
[mplayer.git] / libvo / x11_common.c
blobb41e30e07bdb730cc236ec168929a330666d3af5
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;
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 assert(vo->x11 == NULL);
398 vo->x11 = vo_x11_init_state();
399 struct vo_x11_state *x11 = vo->x11;
401 if (vo_rootwin)
402 WinID = 0; // use root window
404 XSetErrorHandler(x11_errorhandler);
406 #if 0
407 if (!mDisplayName)
408 if (!(mDisplayName = getenv("DISPLAY")))
409 mDisplayName = strdup(":0.0");
410 #else
411 dispName = XDisplayName(mDisplayName);
412 #endif
414 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
416 x11->display = XOpenDisplay(dispName);
417 if (!x11->display)
419 mp_msg(MSGT_VO, MSGL_ERR,
420 "vo: couldn't open the X11 display (%s)!\n", dispName);
421 talloc_free(x11);
422 vo->x11 = NULL;
423 return 0;
425 x11->screen = DefaultScreen(x11->display); // screen ID
426 x11->rootwin = RootWindow(x11->display, x11->screen); // root window ID
428 x11->xim = XOpenIM(x11->display, NULL, NULL, NULL);
430 init_atoms(vo->x11);
432 #ifdef CONFIG_XF86VM
434 int clock;
436 XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline);
437 if (!opts->vo_screenwidth)
438 opts->vo_screenwidth = modeline.hdisplay;
439 if (!opts->vo_screenheight)
440 opts->vo_screenheight = modeline.vdisplay;
442 #endif
444 if (!opts->vo_screenwidth)
445 opts->vo_screenwidth = DisplayWidth(x11->display, x11->screen);
446 if (!opts->vo_screenheight)
447 opts->vo_screenheight = DisplayHeight(x11->display, x11->screen);
449 // get color depth (from root window, or the best visual):
450 XGetWindowAttributes(x11->display, x11->rootwin, &attribs);
451 depth = attribs.depth;
453 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
455 Visual *visual;
457 depth = vo_find_depth_from_visuals(x11->display, x11->screen, &visual);
458 if (depth != -1)
459 mXImage = XCreateImage(x11->display, visual, depth, ZPixmap,
460 0, NULL, 1, 1, 8, 1);
461 } else
462 mXImage =
463 XGetImage(x11->display, x11->rootwin, 0, 0, 1, 1, AllPlanes, ZPixmap);
465 x11->depthonscreen = depth; // display depth on screen
467 // get bits/pixel from XImage structure:
468 if (mXImage == NULL)
470 mask = 0;
471 } else
474 * for the depth==24 case, the XImage structures might use
475 * 24 or 32 bits of data per pixel. The x11->depthonscreen
476 * field stores the amount of data per pixel in the
477 * XImage structure!
479 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
481 bpp = mXImage->bits_per_pixel;
482 if ((x11->depthonscreen + 7) / 8 != (bpp + 7) / 8)
483 x11->depthonscreen = bpp; // by A'rpi
484 mask =
485 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
486 mp_msg(MSGT_VO, MSGL_V,
487 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
488 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
489 XDestroyImage(mXImage);
491 if (((x11->depthonscreen + 7) / 8) == 2)
493 if (mask == 0x7FFF)
494 x11->depthonscreen = 15;
495 else if (mask == 0xFFFF)
496 x11->depthonscreen = 16;
498 // XCloseDisplay( mDisplay );
499 /* slightly improved local display detection AST */
500 if (strncmp(dispName, "unix:", 5) == 0)
501 dispName += 4;
502 else if (strncmp(dispName, "localhost:", 10) == 0)
503 dispName += 9;
504 if (*dispName == ':' && atoi(dispName + 1) < 10)
505 x11->display_is_local = 1;
506 else
507 x11->display_is_local = 0;
508 mp_msg(MSGT_VO, MSGL_V,
509 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
510 opts->vo_screenwidth, opts->vo_screenheight, depth, x11->depthonscreen,
511 dispName, x11->display_is_local ? "local" : "remote");
513 x11->wm_type = vo_wm_detect(vo);
515 x11->fs_type = vo_x11_get_fs_type(x11->wm_type);
517 fstype_dump(x11->fs_type);
519 saver_off(x11->display);
520 return 1;
523 void vo_uninit(struct vo_x11_state *x11)
525 if (!x11)
526 return;
527 if (!x11->display)
529 mp_msg(MSGT_VO, MSGL_V,
530 "vo: x11 uninit called but X11 not initialized..\n");
531 } else {
532 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
533 if (x11->xim)
534 XCloseIM(x11->xim);
535 XSetErrorHandler(NULL);
536 XCloseDisplay(x11->display);
537 x11->depthonscreen = 0;
538 x11->display = NULL;
540 talloc_free(x11);
543 static const struct mp_keymap keymap[] = {
544 // special keys
545 {XK_Pause, KEY_PAUSE}, {XK_Escape, KEY_ESC}, {XK_BackSpace, KEY_BS},
546 {XK_Tab, KEY_TAB}, {XK_Return, KEY_ENTER},
547 {XK_Menu, KEY_MENU}, {XK_Print, KEY_PRINT},
549 // cursor keys
550 {XK_Left, KEY_LEFT}, {XK_Right, KEY_RIGHT}, {XK_Up, KEY_UP}, {XK_Down, KEY_DOWN},
552 // navigation block
553 {XK_Insert, KEY_INSERT}, {XK_Delete, KEY_DELETE}, {XK_Home, KEY_HOME}, {XK_End, KEY_END},
554 {XK_Page_Up, KEY_PAGE_UP}, {XK_Page_Down, KEY_PAGE_DOWN},
556 // F-keys
557 {XK_F1, KEY_F+1}, {XK_F2, KEY_F+2}, {XK_F3, KEY_F+3}, {XK_F4, KEY_F+4},
558 {XK_F5, KEY_F+5}, {XK_F6, KEY_F+6}, {XK_F7, KEY_F+7}, {XK_F8, KEY_F+8},
559 {XK_F9, KEY_F+9}, {XK_F10, KEY_F+10}, {XK_F11, KEY_F+11}, {XK_F12, KEY_F+12},
561 // numpad independent of numlock
562 {XK_KP_Subtract, '-'}, {XK_KP_Add, '+'}, {XK_KP_Multiply, '*'}, {XK_KP_Divide, '/'},
563 {XK_KP_Enter, KEY_KPENTER},
565 // numpad with numlock
566 {XK_KP_0, KEY_KP0}, {XK_KP_1, KEY_KP1}, {XK_KP_2, KEY_KP2},
567 {XK_KP_3, KEY_KP3}, {XK_KP_4, KEY_KP4}, {XK_KP_5, KEY_KP5},
568 {XK_KP_6, KEY_KP6}, {XK_KP_7, KEY_KP7}, {XK_KP_8, KEY_KP8},
569 {XK_KP_9, KEY_KP9}, {XK_KP_Decimal, KEY_KPDEC},
570 {XK_KP_Separator, KEY_KPDEC},
572 // numpad without numlock
573 {XK_KP_Insert, KEY_KPINS}, {XK_KP_End, KEY_KP1}, {XK_KP_Down, KEY_KP2},
574 {XK_KP_Page_Down, KEY_KP3}, {XK_KP_Left, KEY_KP4}, {XK_KP_Begin, KEY_KP5},
575 {XK_KP_Right, KEY_KP6}, {XK_KP_Home, KEY_KP7}, {XK_KP_Up, KEY_KP8},
576 {XK_KP_Page_Up, KEY_KP9}, {XK_KP_Delete, KEY_KPDEL},
578 #ifdef XF86XK_AudioPause
579 {XF86XK_MenuKB, KEY_MENU},
580 {XF86XK_AudioPlay, KEY_PLAY}, {XF86XK_AudioPause, KEY_PAUSE}, {XF86XK_AudioStop, KEY_STOP},
581 {XF86XK_AudioPrev, KEY_PREV}, {XF86XK_AudioNext, KEY_NEXT},
582 {XF86XK_AudioMute, KEY_MUTE}, {XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN}, {XF86XK_AudioRaiseVolume, KEY_VOLUME_UP},
583 #endif
585 {0, 0}
588 static int vo_x11_lookupkey(int key)
590 static const char *passthrough_keys = " -+*/<>`~!@#$%^&()_{}:;\"\',.?\\|=[]";
591 int mpkey = 0;
592 if ((key >= 'a' && key <= 'z') ||
593 (key >= 'A' && key <= 'Z') ||
594 (key >= '0' && key <= '9') ||
595 (key > 0 && key < 256 && strchr(passthrough_keys, key)))
596 mpkey = key;
598 if (!mpkey)
599 mpkey = lookup_keymap_table(keymap, key);
601 return mpkey;
605 // ----- Motif header: -------
607 #define MWM_HINTS_FUNCTIONS (1L << 0)
608 #define MWM_HINTS_DECORATIONS (1L << 1)
609 #define MWM_HINTS_INPUT_MODE (1L << 2)
610 #define MWM_HINTS_STATUS (1L << 3)
612 #define MWM_FUNC_ALL (1L << 0)
613 #define MWM_FUNC_RESIZE (1L << 1)
614 #define MWM_FUNC_MOVE (1L << 2)
615 #define MWM_FUNC_MINIMIZE (1L << 3)
616 #define MWM_FUNC_MAXIMIZE (1L << 4)
617 #define MWM_FUNC_CLOSE (1L << 5)
619 #define MWM_DECOR_ALL (1L << 0)
620 #define MWM_DECOR_BORDER (1L << 1)
621 #define MWM_DECOR_RESIZEH (1L << 2)
622 #define MWM_DECOR_TITLE (1L << 3)
623 #define MWM_DECOR_MENU (1L << 4)
624 #define MWM_DECOR_MINIMIZE (1L << 5)
625 #define MWM_DECOR_MAXIMIZE (1L << 6)
627 #define MWM_INPUT_MODELESS 0
628 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
629 #define MWM_INPUT_SYSTEM_MODAL 2
630 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
631 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
633 #define MWM_TEAROFF_WINDOW (1L<<0)
635 typedef struct
637 long flags;
638 long functions;
639 long decorations;
640 long input_mode;
641 long state;
642 } MotifWmHints;
644 static MotifWmHints vo_MotifWmHints;
645 static Atom vo_MotifHints = None;
647 void vo_x11_decoration(struct vo *vo, int d)
649 struct vo_x11_state *x11 = vo->x11;
650 Atom mtype;
651 int mformat;
652 unsigned long mn, mb;
654 if (!WinID)
655 return;
657 if (vo_fsmode & 8)
659 XSetTransientForHint(x11->display, x11->window,
660 RootWindow(x11->display, x11->screen));
663 vo_MotifHints = XInternAtom(x11->display, "_MOTIF_WM_HINTS", 0);
664 if (vo_MotifHints != None)
666 if (!d)
668 MotifWmHints *mhints = NULL;
670 XGetWindowProperty(x11->display, x11->window,
671 vo_MotifHints, 0, 20, False,
672 vo_MotifHints, &mtype, &mformat, &mn,
673 &mb, (unsigned char **) &mhints);
674 if (mhints)
676 if (mhints->flags & MWM_HINTS_DECORATIONS)
677 x11->olddecor = mhints->decorations;
678 if (mhints->flags & MWM_HINTS_FUNCTIONS)
679 x11->oldfuncs = mhints->functions;
680 XFree(mhints);
684 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
685 vo_MotifWmHints.flags =
686 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
687 if (d)
689 vo_MotifWmHints.functions = x11->oldfuncs;
690 d = x11->olddecor;
692 #if 0
693 vo_MotifWmHints.decorations =
694 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
695 #else
696 vo_MotifWmHints.decorations =
697 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
698 #endif
699 XChangeProperty(x11->display, x11->window, vo_MotifHints,
700 vo_MotifHints, 32,
701 PropModeReplace,
702 (unsigned char *) &vo_MotifWmHints,
703 (vo_fsmode & 4) ? 4 : 5);
707 void vo_x11_classhint(struct vo *vo, Window window, const char *name)
709 struct MPOpts *opts = vo->opts;
710 struct vo_x11_state *x11 = vo->x11;
711 XClassHint wmClass;
712 pid_t pid = getpid();
714 wmClass.res_name = opts->vo_winname ? opts->vo_winname : (char *)name;
715 wmClass.res_class = "mplayer2";
716 XSetClassHint(x11->display, window, &wmClass);
717 XChangeProperty(x11->display, window, x11->XA_NET_WM_PID, XA_CARDINAL,
718 32, PropModeReplace, (unsigned char *) &pid, 1);
721 void vo_x11_uninit(struct vo *vo)
723 struct vo_x11_state *x11 = vo->x11;
724 saver_on(x11->display);
725 if (x11->window != None)
726 vo_showcursor(x11->display, x11->window);
728 if (x11->f_gc != None)
730 XFreeGC(vo->x11->display, x11->f_gc);
731 x11->f_gc = None;
734 if (x11->vo_gc != None)
736 XFreeGC(vo->x11->display, x11->vo_gc);
737 x11->vo_gc = None;
739 if (x11->window != None)
741 XClearWindow(x11->display, x11->window);
742 if (WinID < 0)
744 XEvent xev;
746 if (x11->xic)
747 XDestroyIC(x11->xic);
748 x11->xic = NULL;
750 XUnmapWindow(x11->display, x11->window);
751 XSelectInput(x11->display, x11->window, StructureNotifyMask);
752 XDestroyWindow(x11->display, x11->window);
755 XNextEvent(x11->display, &xev);
757 while (xev.type != DestroyNotify
758 || xev.xdestroywindow.event != x11->window);
760 x11->window = None;
762 vo_fs = 0;
763 x11->vo_old_width = x11->vo_old_height = 0;
764 x11->last_video_width = 0;
765 x11->last_video_height = 0;
766 x11->size_changed_during_fs = false;
768 vo_uninit(x11);
769 vo->x11 = NULL;
772 static int check_resize(struct vo *vo)
774 int old_w = vo->dwidth, old_h = vo->dheight;
775 int old_x = vo->dx, old_y = vo->dy;
776 int rc = 0;
777 vo_x11_update_geometry(vo, true);
778 if (vo->dwidth != old_w || vo->dheight != old_h)
779 rc |= VO_EVENT_RESIZE;
780 if (vo->dx != old_x || vo->dy != old_y)
781 rc |= VO_EVENT_MOVE;
782 return rc;
785 int vo_x11_check_events(struct vo *vo)
787 struct vo_x11_state *x11 = vo->x11;
788 struct MPOpts *opts = vo->opts;
789 Display *display = vo->x11->display;
790 int ret = 0;
791 XEvent Event;
793 if (x11->mouse_waiting_hide && opts->cursor_autohide_delay != -1 &&
794 (GetTimerMS() - x11->mouse_timer >= opts->cursor_autohide_delay)) {
795 vo_hidecursor(display, x11->window);
796 x11->mouse_waiting_hide = 0;
799 if (WinID > 0)
800 ret |= check_resize(vo);
801 while (XPending(display))
803 XNextEvent(display, &Event);
804 // printf("\rEvent.type=%X \n",Event.type);
805 switch (Event.type)
807 case Expose:
808 ret |= VO_EVENT_EXPOSE;
809 break;
810 case ConfigureNotify:
811 if (x11->window == None)
812 break;
813 ret |= check_resize(vo);
814 break;
815 case KeyPress:
817 char buf[100];
818 KeySym keySym = 0;
819 int modifiers = 0;
820 if (Event.xkey.state & ShiftMask)
821 modifiers |= KEY_MODIFIER_SHIFT;
822 if (Event.xkey.state & ControlMask)
823 modifiers |= KEY_MODIFIER_CTRL;
824 if (Event.xkey.state & Mod1Mask)
825 modifiers |= KEY_MODIFIER_ALT;
826 if (Event.xkey.state & Mod4Mask)
827 modifiers |= KEY_MODIFIER_META;
828 if (x11->xic) {
829 Status status;
830 int len = Xutf8LookupString(x11->xic, &Event.xkey, buf,
831 sizeof(buf), &keySym,
832 &status);
833 int mpkey = vo_x11_lookupkey(keySym);
834 if (mpkey) {
835 mplayer_put_key(vo->key_fifo, mpkey | modifiers);
836 } else if (status == XLookupChars
837 || status == XLookupBoth)
839 struct bstr t = { buf, len };
840 mplayer_put_key_utf8(vo->key_fifo, modifiers, t);
842 } else {
843 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
844 &x11->compose_status);
845 int mpkey = vo_x11_lookupkey(keySym);
846 if (mpkey)
847 mplayer_put_key(vo->key_fifo, mpkey | modifiers);
849 ret |= VO_EVENT_KEYPRESS;
851 break;
852 case MotionNotify:
853 vo_mouse_movement(vo, Event.xmotion.x, Event.xmotion.y);
855 if (opts->cursor_autohide_delay > -2) {
856 vo_showcursor(display, x11->window);
857 x11->mouse_waiting_hide = 1;
858 x11->mouse_timer = GetTimerMS();
860 break;
861 case ButtonPress:
862 if (opts->cursor_autohide_delay > -2) {
863 vo_showcursor(display, x11->window);
864 x11->mouse_waiting_hide = 1;
865 x11->mouse_timer = GetTimerMS();
867 mplayer_put_key(vo->key_fifo,
868 (MOUSE_BTN0 + Event.xbutton.button - 1)
869 | MP_KEY_DOWN);
870 break;
871 case ButtonRelease:
872 if (opts->cursor_autohide_delay > -2) {
873 vo_showcursor(display, x11->window);
874 x11->mouse_waiting_hide = 1;
875 x11->mouse_timer = GetTimerMS();
877 mplayer_put_key(vo->key_fifo,
878 MOUSE_BTN0 + Event.xbutton.button - 1);
879 break;
880 case PropertyNotify:
882 char *name =
883 XGetAtomName(display, Event.xproperty.atom);
885 if (!name)
886 break;
888 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
890 XFree(name);
892 break;
893 case MapNotify:
894 x11->vo_hint.win_gravity = x11->old_gravity;
895 XSetWMNormalHints(display, x11->window, &x11->vo_hint);
896 x11->fs_flip = 0;
897 break;
898 case DestroyNotify:
899 mp_msg(MSGT_VO, MSGL_WARN, "Our window was destroyed, exiting\n");
900 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
901 break;
902 case ClientMessage:
903 if (Event.xclient.message_type == x11->XAWM_PROTOCOLS &&
904 Event.xclient.data.l[0] == x11->XAWM_DELETE_WINDOW)
905 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
906 break;
909 return ret;
913 * \brief sets the size and position of the non-fullscreen window.
915 static void vo_x11_nofs_sizepos(struct vo *vo, int x, int y,
916 int width, int height)
918 struct vo_x11_state *x11 = vo->x11;
919 if (width == x11->last_video_width && height == x11->last_video_height) {
920 if (!vo->opts->force_window_position && !x11->size_changed_during_fs)
921 return;
922 } else if (vo_fs)
923 x11->size_changed_during_fs = true;
924 x11->last_video_height = height;
925 x11->last_video_width = width;
926 vo_x11_sizehint(vo, x, y, width, height, 0);
927 if (vo_fs) {
928 x11->vo_old_x = x;
929 x11->vo_old_y = y;
930 x11->vo_old_width = width;
931 x11->vo_old_height = height;
933 else
935 vo->dwidth = width;
936 vo->dheight = height;
937 if (vo->opts->force_window_position)
938 XMoveResizeWindow(vo->x11->display, vo->x11->window, x, y, width,
939 height);
940 else
941 XResizeWindow(vo->x11->display, vo->x11->window, width, height);
945 void vo_x11_sizehint(struct vo *vo, int x, int y, int width, int height, int max)
947 struct vo_x11_state *x11 = vo->x11;
948 x11->vo_hint.flags = 0;
949 if (vo_keepaspect)
951 x11->vo_hint.flags |= PAspect;
952 x11->vo_hint.min_aspect.x = width;
953 x11->vo_hint.min_aspect.y = height;
954 x11->vo_hint.max_aspect.x = width;
955 x11->vo_hint.max_aspect.y = height;
958 x11->vo_hint.flags |= PPosition | PSize;
959 x11->vo_hint.x = x;
960 x11->vo_hint.y = y;
961 x11->vo_hint.width = width;
962 x11->vo_hint.height = height;
963 if (max)
965 x11->vo_hint.flags |= PMaxSize;
966 x11->vo_hint.max_width = width;
967 x11->vo_hint.max_height = height;
968 } else
970 x11->vo_hint.max_width = 0;
971 x11->vo_hint.max_height = 0;
974 // Set minimum height/width to 4 to avoid off-by-one errors.
975 x11->vo_hint.flags |= PMinSize;
976 x11->vo_hint.min_width = x11->vo_hint.min_height = 4;
978 // Set the base size. A window manager might display the window
979 // size to the user relative to this.
980 // Setting these to width/height might be nice, but e.g. fluxbox can't handle it.
981 x11->vo_hint.flags |= PBaseSize;
982 x11->vo_hint.base_width = 0 /*width*/;
983 x11->vo_hint.base_height = 0 /*height*/;
985 x11->vo_hint.flags |= PWinGravity;
986 x11->vo_hint.win_gravity = StaticGravity;
987 XSetWMNormalHints(x11->display, x11->window, &x11->vo_hint);
990 static int vo_x11_get_gnome_layer(struct vo_x11_state *x11, Window win)
992 Atom type;
993 int format;
994 unsigned long nitems;
995 unsigned long bytesafter;
996 unsigned short *args = NULL;
998 if (XGetWindowProperty(x11->display, win, x11->XA_WIN_LAYER, 0, 16384,
999 False, AnyPropertyType, &type, &format, &nitems,
1000 &bytesafter,
1001 (unsigned char **) &args) == Success
1002 && nitems > 0 && args)
1004 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
1005 *args);
1006 return *args;
1008 return WIN_LAYER_NORMAL;
1011 // set a X text property that expects a UTF8_STRING type
1012 static void vo_x11_set_property_utf8(struct vo *vo, Atom name, const char *t)
1014 struct vo_x11_state *x11 = vo->x11;
1016 XChangeProperty(x11->display, x11->window, name, x11->XAUTF8_STRING, 8,
1017 PropModeReplace, t, strlen(t));
1020 // set a X text property that expects a STRING or COMPOUND_TEXT type
1021 static void vo_x11_set_property_string(struct vo *vo, Atom name, const char *t)
1023 struct vo_x11_state *x11 = vo->x11;
1024 XTextProperty prop = {0};
1026 if (Xutf8TextListToTextProperty(x11->display, (char **)&t, 1,
1027 XStdICCTextStyle, &prop) == Success) {
1028 XSetTextProperty(x11->display, x11->window, &prop, name);
1029 } else {
1030 // Strictly speaking this violates the ICCCM, but there's no way we
1031 // can do this correctly.
1032 vo_x11_set_property_utf8(vo, name, t);
1035 if (prop.value)
1036 XFree(prop.value);
1039 static void vo_x11_update_window_title(struct vo *vo)
1041 struct vo_x11_state *x11 = vo->x11;
1043 const char *title = vo_get_window_title(vo);
1044 vo_x11_set_property_string(vo, XA_WM_NAME, title);
1045 vo_x11_set_property_string(vo, XA_WM_ICON_NAME, title);
1046 vo_x11_set_property_utf8(vo, x11->XA_NET_WM_NAME, title);
1047 vo_x11_set_property_utf8(vo, x11->XA_NET_WM_ICON_NAME, title);
1051 static Window vo_x11_create_smooth_window(struct vo_x11_state *x11, Window mRoot,
1052 Visual * vis, int x, int y,
1053 unsigned int width, unsigned int height,
1054 int depth, Colormap col_map)
1056 unsigned long xswamask = CWBorderPixel;
1057 XSetWindowAttributes xswa;
1058 Window ret_win;
1060 if (col_map != CopyFromParent)
1062 xswa.colormap = col_map;
1063 xswamask |= CWColormap;
1065 xswa.background_pixel = 0;
1066 xswa.border_pixel = 0;
1067 xswa.backing_store = NotUseful;
1068 xswa.bit_gravity = StaticGravity;
1070 ret_win =
1071 XCreateWindow(x11->display, x11->rootwin, x, y, width, height, 0, depth,
1072 CopyFromParent, vis, xswamask, &xswa);
1073 XSetWMProtocols(x11->display, ret_win, &x11->XAWM_DELETE_WINDOW, 1);
1074 if (x11->f_gc == None)
1075 x11->f_gc = XCreateGC(x11->display, ret_win, 0, 0);
1076 XSetForeground(x11->display, x11->f_gc, 0);
1078 return ret_win;
1082 * \brief create and setup a window suitable for display
1083 * \param vis Visual to use for creating the window
1084 * \param x x position of window
1085 * \param y y position of window
1086 * \param width width of window
1087 * \param height height of window
1088 * \param flags flags for window creation.
1089 * Only VOFLAG_FULLSCREEN is supported so far.
1090 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1091 * \param classname name to use for the classhint
1093 * This also does the grunt-work like setting Window Manager hints etc.
1094 * If vo_window is already set it just moves and resizes it.
1096 void vo_x11_create_vo_window(struct vo *vo, XVisualInfo *vis, int x, int y,
1097 unsigned int width, unsigned int height, int flags,
1098 Colormap col_map, const char *classname)
1100 struct MPOpts *opts = vo->opts;
1101 struct vo_x11_state *x11 = vo->x11;
1102 Display *mDisplay = vo->x11->display;
1103 if (WinID >= 0) {
1104 vo_fs = flags & VOFLAG_FULLSCREEN;
1105 x11->window = WinID ? (Window)WinID : x11->rootwin;
1106 if (col_map != CopyFromParent) {
1107 unsigned long xswamask = CWColormap;
1108 XSetWindowAttributes xswa;
1109 xswa.colormap = col_map;
1110 XChangeWindowAttributes(mDisplay, x11->window, xswamask, &xswa);
1111 XInstallColormap(mDisplay, col_map);
1113 if (WinID) {
1114 // Expose events can only really be handled by us, so request them.
1115 vo_x11_selectinput_witherr(mDisplay, x11->window, ExposureMask);
1116 } else
1117 // Do not capture events since it might break the parent application
1118 // if it relies on events being forwarded to the parent of WinID.
1119 // It also is consistent with the w32_common.c code.
1120 vo_x11_selectinput_witherr(mDisplay, x11->window,
1121 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1122 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1124 vo_x11_update_geometry(vo, true);
1125 goto final;
1127 if (x11->window == None) {
1128 vo_fs = 0;
1129 vo->dwidth = width;
1130 vo->dheight = height;
1131 x11->window = vo_x11_create_smooth_window(x11, x11->rootwin, vis->visual,
1132 x, y, width, height, vis->depth, col_map);
1133 x11->window_state = VOFLAG_HIDDEN;
1135 if (flags & VOFLAG_HIDDEN)
1136 goto final;
1137 if (x11->window_state & VOFLAG_HIDDEN) {
1138 XSizeHints hint;
1139 x11->window_state &= ~VOFLAG_HIDDEN;
1140 vo_x11_classhint(vo, x11->window, classname);
1141 vo_hidecursor(mDisplay, x11->window);
1142 XSelectInput(mDisplay, x11->window, StructureNotifyMask);
1143 hint.x = x; hint.y = y;
1144 hint.width = width; hint.height = height;
1145 hint.flags = PSize;
1146 if (geometry_xy_changed)
1147 hint.flags |= PPosition;
1148 XSetWMNormalHints(mDisplay, x11->window, &hint);
1149 if (!vo_border) vo_x11_decoration(vo, 0);
1150 // map window
1151 x11->xic = XCreateIC(x11->xim,
1152 XNInputStyle, XIMPreeditNone | XIMStatusNone,
1153 XNClientWindow, x11->window,
1154 XNFocusWindow, x11->window,
1155 NULL);
1156 XSelectInput(mDisplay, x11->window, NoEventMask);
1157 vo_x11_selectinput_witherr(mDisplay, x11->window,
1158 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1159 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1160 XMapWindow(mDisplay, x11->window);
1161 vo_x11_clearwindow(vo, x11->window);
1163 vo_x11_update_window_title(vo);
1164 if (opts->vo_ontop) vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1165 vo_x11_update_geometry(vo, !geometry_xy_changed);
1166 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, width, height);
1167 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1168 vo_x11_fullscreen(vo);
1169 else if (vo_fs) {
1170 // if we are already in fullscreen do not switch back and forth, just
1171 // set the size values right.
1172 vo->dwidth = vo->opts->vo_screenwidth;
1173 vo->dheight = vo->opts->vo_screenheight;
1175 final:
1176 if (x11->vo_gc != None)
1177 XFreeGC(mDisplay, x11->vo_gc);
1178 x11->vo_gc = XCreateGC(mDisplay, x11->window, 0, NULL);
1180 XSync(mDisplay, False);
1181 vo->event_fd = ConnectionNumber(x11->display);
1184 void vo_x11_clearwindow_part(struct vo *vo, Window vo_window,
1185 int img_width, int img_height)
1187 struct vo_x11_state *x11 = vo->x11;
1188 Display *mDisplay = vo->x11->display;
1189 int u_dheight, u_dwidth, left_ov, left_ov2;
1191 if (x11->f_gc == None)
1192 return;
1194 u_dheight = vo->dheight;
1195 u_dwidth = vo->dwidth;
1196 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1197 return;
1199 left_ov = (u_dheight - img_height) / 2;
1200 left_ov2 = (u_dwidth - img_width) / 2;
1202 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, 0, u_dwidth, left_ov);
1203 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, u_dheight - left_ov - 1,
1204 u_dwidth, left_ov + 1);
1206 if (u_dwidth > img_width)
1208 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, left_ov, left_ov2,
1209 img_height);
1210 XFillRectangle(mDisplay, vo_window, x11->f_gc, u_dwidth - left_ov2 - 1,
1211 left_ov, left_ov2 + 1, img_height);
1214 XFlush(mDisplay);
1217 void vo_x11_clearwindow(struct vo *vo, Window vo_window)
1219 struct vo_x11_state *x11 = vo->x11;
1220 struct MPOpts *opts = vo->opts;
1221 if (x11->f_gc == None)
1222 return;
1223 XFillRectangle(x11->display, vo_window, x11->f_gc, 0, 0,
1224 opts->vo_screenwidth, opts->vo_screenheight);
1226 XFlush(x11->display);
1230 void vo_x11_setlayer(struct vo *vo, Window vo_window, int layer)
1232 struct vo_x11_state *x11 = vo->x11;
1233 if (WinID >= 0)
1234 return;
1236 if (x11->fs_type & vo_wm_LAYER)
1238 XClientMessageEvent xev;
1240 if (!x11->orig_layer)
1241 x11->orig_layer = vo_x11_get_gnome_layer(x11, vo_window);
1243 memset(&xev, 0, sizeof(xev));
1244 xev.type = ClientMessage;
1245 xev.display = x11->display;
1246 xev.window = vo_window;
1247 xev.message_type = x11->XA_WIN_LAYER;
1248 xev.format = 32;
1249 xev.data.l[0] = layer ? fs_layer : x11->orig_layer; // if not fullscreen, stay on default layer
1250 xev.data.l[1] = CurrentTime;
1251 mp_msg(MSGT_VO, MSGL_V,
1252 "[x11] Layered style stay on top (layer %ld).\n",
1253 xev.data.l[0]);
1254 XSendEvent(x11->display, x11->rootwin, False, SubstructureNotifyMask,
1255 (XEvent *) & xev);
1256 } else if (x11->fs_type & vo_wm_NETWM)
1258 XClientMessageEvent xev;
1259 char *state;
1261 memset(&xev, 0, sizeof(xev));
1262 xev.type = ClientMessage;
1263 xev.message_type = x11->XA_NET_WM_STATE;
1264 xev.display = x11->display;
1265 xev.window = vo_window;
1266 xev.format = 32;
1267 xev.data.l[0] = layer;
1269 if (x11->fs_type & vo_wm_STAYS_ON_TOP)
1270 xev.data.l[1] = x11->XA_NET_WM_STATE_STAYS_ON_TOP;
1271 else if (x11->fs_type & vo_wm_ABOVE)
1272 xev.data.l[1] = x11->XA_NET_WM_STATE_ABOVE;
1273 else if (x11->fs_type & vo_wm_FULLSCREEN)
1274 xev.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
1275 else if (x11->fs_type & vo_wm_BELOW)
1276 // This is not fallback. We can safely assume that the situation
1277 // where only NETWM_STATE_BELOW is supported doesn't exist.
1278 xev.data.l[1] = x11->XA_NET_WM_STATE_BELOW;
1280 XSendEvent(x11->display, x11->rootwin, False, SubstructureRedirectMask,
1281 (XEvent *) & xev);
1282 state = XGetAtomName(x11->display, xev.data.l[1]);
1283 mp_msg(MSGT_VO, MSGL_V,
1284 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1285 layer, state);
1286 XFree(state);
1290 static int vo_x11_get_fs_type(int supported)
1292 int i;
1293 int type = supported;
1295 if (vo_fstype_list)
1297 for (i = 0; vo_fstype_list[i]; i++)
1299 int neg = 0;
1300 char *arg = vo_fstype_list[i];
1302 if (vo_fstype_list[i][0] == '-')
1304 neg = 1;
1305 arg = vo_fstype_list[i] + 1;
1308 if (!strncmp(arg, "layer", 5))
1310 if (!neg && (arg[5] == '='))
1312 char *endptr = NULL;
1313 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1315 if (endptr && *endptr == '\0' && layer >= 0
1316 && layer <= 15)
1317 fs_layer = layer;
1319 if (neg)
1320 type &= ~vo_wm_LAYER;
1321 else
1322 type |= vo_wm_LAYER;
1323 } else if (!strcmp(arg, "above"))
1325 if (neg)
1326 type &= ~vo_wm_ABOVE;
1327 else
1328 type |= vo_wm_ABOVE;
1329 } else if (!strcmp(arg, "fullscreen"))
1331 if (neg)
1332 type &= ~vo_wm_FULLSCREEN;
1333 else
1334 type |= vo_wm_FULLSCREEN;
1335 } else if (!strcmp(arg, "stays_on_top"))
1337 if (neg)
1338 type &= ~vo_wm_STAYS_ON_TOP;
1339 else
1340 type |= vo_wm_STAYS_ON_TOP;
1341 } else if (!strcmp(arg, "below"))
1343 if (neg)
1344 type &= ~vo_wm_BELOW;
1345 else
1346 type |= vo_wm_BELOW;
1347 } else if (!strcmp(arg, "netwm"))
1349 if (neg)
1350 type &= ~vo_wm_NETWM;
1351 else
1352 type |= vo_wm_NETWM;
1353 } else if (!strcmp(arg, "none"))
1354 type = 0; // clear; keep parsing
1358 return type;
1362 * \brief update vo->dx, vo->dy, vo->dwidth and vo->dheight with current values of vo->x11->window
1363 * \return returns current color depth of vo->x11->window
1365 int vo_x11_update_geometry(struct vo *vo, bool update_pos)
1367 struct vo_x11_state *x11 = vo->x11;
1368 unsigned depth, w, h;
1369 int dummy_int;
1370 Window dummy_win;
1371 XGetGeometry(x11->display, x11->window, &dummy_win, &dummy_int, &dummy_int,
1372 &w, &h, &dummy_int, &depth);
1373 if (w <= INT_MAX && h <= INT_MAX) {
1374 vo->dwidth = w;
1375 vo->dheight = h;
1377 if (update_pos)
1378 XTranslateCoordinates(x11->display, x11->window, x11->rootwin, 0, 0,
1379 &vo->dx, &vo->dy, &dummy_win);
1381 return depth <= INT_MAX ? depth : 0;
1384 void vo_x11_fullscreen(struct vo *vo)
1386 struct MPOpts *opts = vo->opts;
1387 struct vo_x11_state *x11 = vo->x11;
1388 int x, y, w, h;
1389 x = x11->vo_old_x;
1390 y = x11->vo_old_y;
1391 w = x11->vo_old_width;
1392 h = x11->vo_old_height;
1394 if (WinID >= 0) {
1395 vo_fs = !vo_fs;
1396 return;
1398 if (x11->fs_flip)
1399 return;
1401 if (vo_fs)
1403 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1404 vo_fs = VO_FALSE;
1405 if (x11->size_changed_during_fs && (x11->fs_type & vo_wm_FULLSCREEN))
1406 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, x11->last_video_width,
1407 x11->last_video_height);
1408 x11->size_changed_during_fs = false;
1409 } else
1411 // win->fs
1412 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1414 vo_fs = VO_TRUE;
1415 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1417 x11->vo_old_x = vo->dx;
1418 x11->vo_old_y = vo->dy;
1419 x11->vo_old_width = vo->dwidth;
1420 x11->vo_old_height = vo->dheight;
1422 update_xinerama_info(vo);
1423 x = xinerama_x;
1424 y = xinerama_y;
1425 w = opts->vo_screenwidth;
1426 h = opts->vo_screenheight;
1429 long dummy;
1431 XGetWMNormalHints(x11->display, x11->window, &x11->vo_hint, &dummy);
1432 if (!(x11->vo_hint.flags & PWinGravity))
1433 x11->old_gravity = NorthWestGravity;
1434 else
1435 x11->old_gravity = x11->vo_hint.win_gravity;
1437 if (x11->wm_type == 0 && !(vo_fsmode & 16))
1439 XUnmapWindow(x11->display, x11->window); // required for MWM
1440 XWithdrawWindow(x11->display, x11->window, x11->screen);
1441 x11->fs_flip = 1;
1444 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1446 vo_x11_decoration(vo, vo_border && !vo_fs);
1447 vo_x11_sizehint(vo, x, y, w, h, 0);
1448 vo_x11_setlayer(vo, x11->window, vo_fs);
1451 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1453 /* some WMs lose ontop after fullscreen */
1454 if ((!(vo_fs)) & opts->vo_ontop)
1455 vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1457 XMapRaised(x11->display, x11->window);
1458 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1459 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1460 XRaiseWindow(x11->display, x11->window);
1461 XFlush(x11->display);
1464 void vo_x11_ontop(struct vo *vo)
1466 struct MPOpts *opts = vo->opts;
1467 opts->vo_ontop = !opts->vo_ontop;
1469 vo_x11_setlayer(vo, vo->x11->window, opts->vo_ontop);
1472 void vo_x11_border(struct vo *vo)
1474 vo_border = !vo_border;
1475 vo_x11_decoration(vo, vo_border && !vo_fs);
1479 * XScreensaver stuff
1482 static int screensaver_off;
1483 static unsigned int time_last;
1485 void xscreensaver_heartbeat(struct vo_x11_state *x11)
1487 unsigned int time = GetTimerMS();
1489 if (x11->display && screensaver_off && (time - time_last) > 30000)
1491 time_last = time;
1493 XResetScreenSaver(x11->display);
1497 static int xss_suspend(Display *mDisplay, Bool suspend)
1499 #ifndef CONFIG_XSS
1500 return 0;
1501 #else
1502 int event, error, major, minor;
1503 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1504 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1505 return 0;
1506 if (major < 1 || (major == 1 && minor < 1))
1507 return 0;
1508 XScreenSaverSuspend(mDisplay, suspend);
1509 return 1;
1510 #endif
1514 * End of XScreensaver stuff
1517 static void saver_on(Display * mDisplay)
1520 if (!screensaver_off)
1521 return;
1522 screensaver_off = 0;
1523 if (xss_suspend(mDisplay, False))
1524 return;
1525 #ifdef CONFIG_XDPMS
1526 if (dpms_disabled)
1528 int nothing;
1529 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1531 if (!DPMSEnable(mDisplay))
1532 { // restoring power saving settings
1533 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1534 } else
1536 // DPMS does not seem to be enabled unless we call DPMSInfo
1537 BOOL onoff;
1538 CARD16 state;
1540 DPMSForceLevel(mDisplay, DPMSModeOn);
1541 DPMSInfo(mDisplay, &state, &onoff);
1542 if (onoff)
1544 mp_msg(MSGT_VO, MSGL_V,
1545 "Successfully enabled DPMS\n");
1546 } else
1548 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1552 dpms_disabled = 0;
1554 #endif
1557 static void saver_off(Display * mDisplay)
1559 int nothing;
1561 if (screensaver_off)
1562 return;
1563 screensaver_off = 1;
1564 if (xss_suspend(mDisplay, True))
1565 return;
1566 #ifdef CONFIG_XDPMS
1567 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1569 BOOL onoff;
1570 CARD16 state;
1572 DPMSInfo(mDisplay, &state, &onoff);
1573 if (onoff)
1575 Status stat;
1577 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1578 dpms_disabled = 1;
1579 stat = DPMSDisable(mDisplay); // monitor powersave off
1580 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1583 #endif
1586 static XErrorHandler old_handler = NULL;
1587 static int selectinput_err = 0;
1588 static int x11_selectinput_errorhandler(Display * display,
1589 XErrorEvent * event)
1591 if (event->error_code == BadAccess)
1593 selectinput_err = 1;
1594 mp_msg(MSGT_VO, MSGL_ERR,
1595 "X11 error: BadAccess during XSelectInput Call\n");
1596 mp_msg(MSGT_VO, MSGL_ERR,
1597 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1598 /* If you think MPlayer should shutdown with this error,
1599 * comment out the following line */
1600 return 0;
1602 if (old_handler != NULL)
1603 old_handler(display, event);
1604 else
1605 x11_errorhandler(display, event);
1606 return 0;
1609 void vo_x11_selectinput_witherr(Display * display, Window w,
1610 long event_mask)
1612 XSync(display, False);
1613 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1614 selectinput_err = 0;
1615 if (vo_nomouse_input)
1617 XSelectInput(display, w,
1618 event_mask &
1619 (~(ButtonPressMask | ButtonReleaseMask)));
1620 } else
1622 XSelectInput(display, w, event_mask);
1624 XSync(display, False);
1625 XSetErrorHandler(old_handler);
1626 if (selectinput_err)
1628 mp_msg(MSGT_VO, MSGL_ERR,
1629 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1630 XSelectInput(display, w,
1631 event_mask &
1633 (ButtonPressMask | ButtonReleaseMask |
1634 PointerMotionMask)));
1638 #ifdef CONFIG_XF86VM
1639 void vo_vm_switch(struct vo *vo)
1641 struct vo_x11_state *x11 = vo->x11;
1642 struct MPOpts *opts = vo->opts;
1643 Display *mDisplay = x11->display;
1644 int vm_event, vm_error;
1645 int vm_ver, vm_rev;
1646 int i, j, have_vm = 0;
1647 int X = vo->dwidth, Y = vo->dheight;
1648 int modeline_width, modeline_height;
1650 int modecount;
1652 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1654 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1655 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1656 vm_rev);
1657 have_vm = 1;
1658 } else {
1659 mp_msg(MSGT_VO, MSGL_WARN,
1660 "XF86VidMode extension not available.\n");
1663 if (have_vm)
1665 if (vidmodes == NULL)
1666 XF86VidModeGetAllModeLines(mDisplay, x11->screen, &modecount,
1667 &vidmodes);
1668 j = 0;
1669 modeline_width = vidmodes[0]->hdisplay;
1670 modeline_height = vidmodes[0]->vdisplay;
1672 for (i = 1; i < modecount; i++)
1673 if ((vidmodes[i]->hdisplay >= X)
1674 && (vidmodes[i]->vdisplay >= Y))
1675 if ((vidmodes[i]->hdisplay <= modeline_width)
1676 && (vidmodes[i]->vdisplay <= modeline_height))
1678 modeline_width = vidmodes[i]->hdisplay;
1679 modeline_height = vidmodes[i]->vdisplay;
1680 j = i;
1683 mp_tmsg(MSGT_VO, MSGL_INFO, "XF86VM: Selected video mode %dx%d for image size %dx%d.\n",
1684 modeline_width, modeline_height, X, Y);
1685 XF86VidModeLockModeSwitch(mDisplay, x11->screen, 0);
1686 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1687 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1689 // FIXME: all this is more of a hack than proper solution
1690 X = (opts->vo_screenwidth - modeline_width) / 2;
1691 Y = (opts->vo_screenheight - modeline_height) / 2;
1692 XF86VidModeSetViewPort(mDisplay, x11->screen, X, Y);
1693 vo->dx = X;
1694 vo->dy = Y;
1695 vo->dwidth = modeline_width;
1696 vo->dheight = modeline_height;
1697 aspect_save_screenres(vo, modeline_width, modeline_height);
1701 void vo_vm_close(struct vo *vo)
1703 Display *dpy = vo->x11->display;
1704 struct MPOpts *opts = vo->opts;
1705 if (vidmodes != NULL)
1707 int i, modecount;
1709 free(vidmodes);
1710 vidmodes = NULL;
1711 XF86VidModeGetAllModeLines(dpy, vo->x11->screen, &modecount,
1712 &vidmodes);
1713 for (i = 0; i < modecount; i++)
1714 if ((vidmodes[i]->hdisplay == opts->vo_screenwidth)
1715 && (vidmodes[i]->vdisplay == opts->vo_screenheight))
1717 mp_msg(MSGT_VO, MSGL_INFO,
1718 "Returning to original mode %dx%d\n",
1719 opts->vo_screenwidth, opts->vo_screenheight);
1720 break;
1723 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1724 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1725 free(vidmodes);
1726 vidmodes = NULL;
1730 double vo_vm_get_fps(struct vo *vo)
1732 struct vo_x11_state *x11 = vo->x11;
1733 int clock;
1734 XF86VidModeModeLine modeline;
1735 if (!XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline))
1736 return 0;
1737 if (modeline.privsize)
1738 XFree(modeline.private);
1739 return 1e3 * clock / modeline.htotal / modeline.vtotal;
1741 #endif
1745 * Scan the available visuals on this Display/Screen. Try to find
1746 * the 'best' available TrueColor visual that has a decent color
1747 * depth (at least 15bit). If there are multiple visuals with depth
1748 * >= 15bit, we prefer visuals with a smaller color depth.
1750 int vo_find_depth_from_visuals(Display * dpy, int screen,
1751 Visual ** visual_return)
1753 XVisualInfo visual_tmpl;
1754 XVisualInfo *visuals;
1755 int nvisuals, i;
1756 int bestvisual = -1;
1757 int bestvisual_depth = -1;
1759 visual_tmpl.screen = screen;
1760 visual_tmpl.class = TrueColor;
1761 visuals = XGetVisualInfo(dpy,
1762 VisualScreenMask | VisualClassMask,
1763 &visual_tmpl, &nvisuals);
1764 if (visuals != NULL)
1766 for (i = 0; i < nvisuals; i++)
1768 mp_msg(MSGT_VO, MSGL_V,
1769 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1770 visuals[i].visualid, visuals[i].depth,
1771 visuals[i].red_mask, visuals[i].green_mask,
1772 visuals[i].blue_mask);
1774 * Save the visual index and its depth, if this is the first
1775 * truecolor visul, or a visual that is 'preferred' over the
1776 * previous 'best' visual.
1778 if (bestvisual_depth == -1
1779 || (visuals[i].depth >= 15
1780 && (visuals[i].depth < bestvisual_depth
1781 || bestvisual_depth < 15)))
1783 bestvisual = i;
1784 bestvisual_depth = visuals[i].depth;
1788 if (bestvisual != -1 && visual_return != NULL)
1789 *visual_return = visuals[bestvisual].visual;
1791 XFree(visuals);
1793 return bestvisual_depth;
1797 static Colormap cmap = None;
1798 static XColor cols[256];
1799 static int cm_size, red_mask, green_mask, blue_mask;
1802 Colormap vo_x11_create_colormap(struct vo *vo, XVisualInfo *vinfo)
1804 struct vo_x11_state *x11 = vo->x11;
1805 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1807 if (vinfo->class != DirectColor)
1808 return XCreateColormap(x11->display, x11->rootwin, vinfo->visual,
1809 AllocNone);
1811 /* can this function get called twice or more? */
1812 if (cmap)
1813 return cmap;
1814 cm_size = vinfo->colormap_size;
1815 red_mask = vinfo->red_mask;
1816 green_mask = vinfo->green_mask;
1817 blue_mask = vinfo->blue_mask;
1818 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1819 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1820 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1821 rvu = 65536ull * ru / (red_mask + ru);
1822 gvu = 65536ull * gu / (green_mask + gu);
1823 bvu = 65536ull * bu / (blue_mask + bu);
1824 r = g = b = 0;
1825 rv = gv = bv = 0;
1826 m = DoRed | DoGreen | DoBlue;
1827 for (k = 0; k < cm_size; k++)
1829 int t;
1831 cols[k].pixel = r | g | b;
1832 cols[k].red = rv;
1833 cols[k].green = gv;
1834 cols[k].blue = bv;
1835 cols[k].flags = m;
1836 t = (r + ru) & red_mask;
1837 if (t < r)
1838 m &= ~DoRed;
1839 r = t;
1840 t = (g + gu) & green_mask;
1841 if (t < g)
1842 m &= ~DoGreen;
1843 g = t;
1844 t = (b + bu) & blue_mask;
1845 if (t < b)
1846 m &= ~DoBlue;
1847 b = t;
1848 rv += rvu;
1849 gv += gvu;
1850 bv += bvu;
1852 cmap = XCreateColormap(x11->display, x11->rootwin, vinfo->visual, AllocAll);
1853 XStoreColors(x11->display, cmap, cols, cm_size);
1854 return cmap;
1858 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1859 * hue and red/green/blue intensity, but we cannot do saturation.
1860 * Currently only gamma, brightness and contrast are implemented.
1861 * Is there sufficient interest for hue and/or red/green/blue intensity?
1863 /* these values have range [-100,100] and are initially 0 */
1864 static int vo_gamma = 0;
1865 static int vo_brightness = 0;
1866 static int vo_contrast = 0;
1868 static int transform_color(float val,
1869 float brightness, float contrast, float gamma) {
1870 float s = pow(val, gamma);
1871 s = (s - 0.5) * contrast + 0.5;
1872 s += brightness;
1873 if (s < 0)
1874 s = 0;
1875 if (s > 1)
1876 s = 1;
1877 return (unsigned short) (s * 65535);
1880 uint32_t vo_x11_set_equalizer(struct vo *vo, const char *name, int value)
1882 float gamma, brightness, contrast;
1883 float rf, gf, bf;
1884 int k;
1887 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1888 * of TrueColor-ed window but be careful:
1889 * Unlike the colormaps, which are private for the X client
1890 * who created them and thus automatically destroyed on client
1891 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1892 * setting and _must_ be restored before the process exits.
1893 * Unforunately when the process crashes (or gets killed
1894 * for some reason) it is impossible to restore the setting,
1895 * and such behaviour could be rather annoying for the users.
1897 if (cmap == None)
1898 return VO_NOTAVAIL;
1900 if (!strcasecmp(name, "brightness"))
1901 vo_brightness = value;
1902 else if (!strcasecmp(name, "contrast"))
1903 vo_contrast = value;
1904 else if (!strcasecmp(name, "gamma"))
1905 vo_gamma = value;
1906 else
1907 return VO_NOTIMPL;
1909 brightness = 0.01 * vo_brightness;
1910 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1911 gamma = pow(2, -0.02 * vo_gamma);
1913 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1914 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
1915 green_mask;
1916 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
1918 /* now recalculate the colormap using the newly set value */
1919 for (k = 0; k < cm_size; k++)
1921 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
1922 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
1923 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
1926 XStoreColors(vo->x11->display, cmap, cols, cm_size);
1927 XFlush(vo->x11->display);
1928 return VO_TRUE;
1931 uint32_t vo_x11_get_equalizer(const char *name, int *value)
1933 if (cmap == None)
1934 return VO_NOTAVAIL;
1935 if (!strcasecmp(name, "brightness"))
1936 *value = vo_brightness;
1937 else if (!strcasecmp(name, "contrast"))
1938 *value = vo_contrast;
1939 else if (!strcasecmp(name, "gamma"))
1940 *value = vo_gamma;
1941 else
1942 return VO_NOTIMPL;
1943 return VO_TRUE;
1946 bool vo_x11_screen_is_composited(struct vo *vo)
1948 struct vo_x11_state *x11 = vo->x11;
1949 return XGetSelectionOwner(x11->display, x11->XA_NET_WM_CM) != None;
1952 #ifdef CONFIG_XV
1953 int vo_xv_set_eq(struct vo *vo, uint32_t xv_port, const char *name, int value)
1955 XvAttribute *attributes;
1956 int i, howmany, xv_atom;
1958 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
1960 /* get available attributes */
1961 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1962 for (i = 0; i < howmany && attributes; i++)
1963 if (attributes[i].flags & XvSettable)
1965 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1966 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1967 then trigger it if it's ok so that the other values are at default upon query */
1968 if (xv_atom != None)
1970 int hue = 0, port_value, port_min, port_max;
1972 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1973 (!strcasecmp(name, "brightness")))
1974 port_value = value;
1975 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1976 (!strcasecmp(name, "contrast")))
1977 port_value = value;
1978 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1979 (!strcasecmp(name, "saturation")))
1980 port_value = value;
1981 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1982 (!strcasecmp(name, "hue")))
1984 port_value = value;
1985 hue = 1;
1986 } else
1987 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1988 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1989 (!strcasecmp(name, "red_intensity")))
1990 port_value = value;
1991 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1992 && (!strcasecmp(name, "green_intensity")))
1993 port_value = value;
1994 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1995 && (!strcasecmp(name, "blue_intensity")))
1996 port_value = value;
1997 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
1998 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
1999 && (!strcasecmp(name, "bt_709")))
2000 port_value = value;
2001 else
2002 continue;
2004 port_min = attributes[i].min_value;
2005 port_max = attributes[i].max_value;
2007 /* nvidia hue workaround */
2008 if (hue && port_min == 0 && port_max == 360)
2010 port_value =
2011 (port_value >=
2012 0) ? (port_value - 100) : (port_value + 100);
2014 // -100 -> min
2015 // 0 -> (max+min)/2
2016 // +100 -> max
2017 port_value =
2018 (port_value + 100) * (port_max - port_min) / 200 +
2019 port_min;
2020 XvSetPortAttribute(vo->x11->display, xv_port, xv_atom, port_value);
2021 return VO_TRUE;
2024 return VO_FALSE;
2027 int vo_xv_get_eq(struct vo *vo, uint32_t xv_port, const char *name, int *value)
2030 XvAttribute *attributes;
2031 int i, howmany, xv_atom;
2033 /* get available attributes */
2034 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
2035 for (i = 0; i < howmany && attributes; i++)
2036 if (attributes[i].flags & XvGettable)
2038 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
2039 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2040 then trigger it if it's ok so that the other values are at default upon query */
2041 if (xv_atom != None)
2043 int val, port_value = 0, port_min, port_max;
2045 XvGetPortAttribute(vo->x11->display, xv_port, xv_atom,
2046 &port_value);
2048 port_min = attributes[i].min_value;
2049 port_max = attributes[i].max_value;
2050 val =
2051 (port_value - port_min) * 200 / (port_max - port_min) -
2052 100;
2054 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2055 (!strcasecmp(name, "brightness")))
2056 *value = val;
2057 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2058 (!strcasecmp(name, "contrast")))
2059 *value = val;
2060 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2061 (!strcasecmp(name, "saturation")))
2062 *value = val;
2063 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2064 (!strcasecmp(name, "hue")))
2066 /* nasty nvidia detect */
2067 if (port_min == 0 && port_max == 360)
2068 *value = (val >= 0) ? (val - 100) : (val + 100);
2069 else
2070 *value = val;
2071 } else
2072 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2073 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2074 (!strcasecmp(name, "red_intensity")))
2075 *value = val;
2076 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2077 && (!strcasecmp(name, "green_intensity")))
2078 *value = val;
2079 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2080 && (!strcasecmp(name, "blue_intensity")))
2081 *value = val;
2082 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
2083 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
2084 && (!strcasecmp(name, "bt_709")))
2085 *value = val;
2086 else
2087 continue;
2089 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2090 name, *value);
2091 return VO_TRUE;
2094 return VO_FALSE;
2098 * \brief Interns the requested atom if it is available.
2100 * \param atom_name String containing the name of the requested atom.
2102 * \return Returns the atom if available, else None is returned.
2105 static Atom xv_intern_atom_if_exists(struct vo_x11_state *x11,
2106 char const *atom_name)
2108 XvAttribute * attributes;
2109 int attrib_count,i;
2110 Atom xv_atom = None;
2112 attributes = XvQueryPortAttributes(x11->display, x11->xv_port, &attrib_count );
2113 if( attributes!=NULL )
2115 for ( i = 0; i < attrib_count; ++i )
2117 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2119 xv_atom = XInternAtom(x11->display, atom_name, False );
2120 break; // found what we want, break out
2123 XFree( attributes );
2126 return xv_atom;
2130 * \brief Try to enable vsync for xv.
2131 * \return Returns -1 if not available, 0 on failure and 1 on success.
2133 int vo_xv_enable_vsync(struct vo *vo)
2135 struct vo_x11_state *x11 = vo->x11;
2136 Atom xv_atom = xv_intern_atom_if_exists(x11, "XV_SYNC_TO_VBLANK");
2137 if (xv_atom == None)
2138 return -1;
2139 return XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1) == Success;
2143 * \brief Get maximum supported source image dimensions.
2145 * This function does not set the variables pointed to by
2146 * width and height if the information could not be retrieved,
2147 * so the caller is reponsible for properly initializing them.
2149 * \param width [out] The maximum width gets stored here.
2150 * \param height [out] The maximum height gets stored here.
2153 void vo_xv_get_max_img_dim(struct vo *vo, uint32_t * width, uint32_t * height)
2155 struct vo_x11_state *x11 = vo->x11;
2156 XvEncodingInfo * encodings;
2157 //unsigned long num_encodings, idx; to int or too long?!
2158 unsigned int num_encodings, idx;
2160 XvQueryEncodings(x11->display, x11->xv_port, &num_encodings, &encodings);
2162 if ( encodings )
2164 for ( idx = 0; idx < num_encodings; ++idx )
2166 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2168 *width = encodings[idx].width;
2169 *height = encodings[idx].height;
2170 break;
2175 mp_msg( MSGT_VO, MSGL_V,
2176 "[xv common] Maximum source image dimensions: %ux%u\n",
2177 *width, *height );
2179 XvFreeEncodingInfo( encodings );
2183 * \brief Print information about the colorkey method and source.
2185 * \param ck_handling Integer value containing the information about
2186 * colorkey handling (see x11_common.h).
2188 * Outputs the content of |ck_handling| as a readable message.
2191 static void vo_xv_print_ck_info(struct vo_x11_state *x11)
2193 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2195 switch ( x11->xv_ck_info.method )
2197 case CK_METHOD_NONE:
2198 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2199 case CK_METHOD_AUTOPAINT:
2200 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2201 case CK_METHOD_MANUALFILL:
2202 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2203 case CK_METHOD_BACKGROUND:
2204 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2207 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2209 switch ( x11->xv_ck_info.source )
2211 case CK_SRC_CUR:
2212 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2213 x11->xv_colorkey );
2214 break;
2215 case CK_SRC_USE:
2216 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2218 mp_msg( MSGT_VO, MSGL_V,
2219 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2220 x11->xv_colorkey );
2222 else
2224 mp_msg( MSGT_VO, MSGL_V,
2225 "Using colorkey from MPlayer (0x%06lx)."
2226 " Use -colorkey to change.\n",
2227 x11->xv_colorkey );
2229 break;
2230 case CK_SRC_SET:
2231 mp_msg( MSGT_VO, MSGL_V,
2232 "Setting and using colorkey from MPlayer (0x%06lx)."
2233 " Use -colorkey to change.\n",
2234 x11->xv_colorkey );
2235 break;
2239 * \brief Init colorkey depending on the settings in xv_ck_info.
2241 * \return Returns 0 on failure and 1 on success.
2243 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2244 * flags in xv_ck_info.
2246 * Possiblilities:
2247 * * Methods
2248 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2249 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2250 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2251 * * Sources
2252 * - use currently set colorkey ( CK_SRC_CUR )
2253 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2254 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2256 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2257 * we don't draw anything as this means it was forced to off.
2259 int vo_xv_init_colorkey(struct vo *vo)
2261 struct vo_x11_state *x11 = vo->x11;
2262 Atom xv_atom;
2263 int rez;
2265 /* check if colorkeying is needed */
2266 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_COLORKEY");
2268 /* if we have to deal with colorkeying ... */
2269 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2271 /* check if we should use the colorkey specified in vo_colorkey */
2272 if ( x11->xv_ck_info.source != CK_SRC_CUR )
2274 x11->xv_colorkey = vo_colorkey;
2276 /* check if we have to set the colorkey too */
2277 if ( x11->xv_ck_info.source == CK_SRC_SET )
2279 xv_atom = XInternAtom(x11->display, "XV_COLORKEY",False);
2281 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, vo_colorkey);
2282 if ( rez != Success )
2284 mp_msg( MSGT_VO, MSGL_FATAL,
2285 "[xv common] Couldn't set colorkey!\n" );
2286 return 0; // error setting colorkey
2290 else
2292 int colorkey_ret;
2294 rez=XvGetPortAttribute(x11->display,x11->xv_port, xv_atom, &colorkey_ret);
2295 if ( rez == Success )
2297 x11->xv_colorkey = colorkey_ret;
2299 else
2301 mp_msg( MSGT_VO, MSGL_FATAL,
2302 "[xv common] Couldn't get colorkey!"
2303 "Maybe the selected Xv port has no overlay.\n" );
2304 return 0; // error getting colorkey
2308 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_AUTOPAINT_COLORKEY");
2310 /* should we draw the colorkey ourselves or activate autopainting? */
2311 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2313 rez = !Success; // reset rez to something different than Success
2315 if ( xv_atom != None ) // autopaint is supported
2317 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1);
2320 if ( rez != Success )
2322 // fallback to manual colorkey drawing
2323 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2326 else // disable colorkey autopainting if supported
2328 if ( xv_atom != None ) // we have autopaint attribute
2330 XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 0);
2334 else // do no colorkey drawing at all
2336 x11->xv_ck_info.method = CK_METHOD_NONE;
2337 } /* end: should we draw colorkey */
2339 /* output information about the current colorkey settings */
2340 vo_xv_print_ck_info(x11);
2342 return 1; // success
2346 * \brief Draw the colorkey on the video window.
2348 * Draws the colorkey depending on the set method ( colorkey_handling ).
2350 * Also draws the black bars ( when the video doesn't fit the display in
2351 * fullscreen ) separately, so they don't overlap with the video area.
2352 * It doesn't call XFlush.
2355 void vo_xv_draw_colorkey(struct vo *vo, int32_t x, int32_t y,
2356 int32_t w, int32_t h)
2358 struct vo_x11_state *x11 = vo->x11;
2359 if( x11->xv_ck_info.method == CK_METHOD_MANUALFILL ||
2360 x11->xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2362 XSetForeground(x11->display, x11->vo_gc, x11->xv_colorkey );
2363 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2364 x, y,
2365 w, h );
2369 /** \brief Tests if a valid argument for the ck suboption was given. */
2370 int xv_test_ck( void * arg )
2372 strarg_t * strarg = (strarg_t *)arg;
2374 if ( strargcmp( strarg, "use" ) == 0 ||
2375 strargcmp( strarg, "set" ) == 0 ||
2376 strargcmp( strarg, "cur" ) == 0 )
2378 return 1;
2381 return 0;
2383 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2384 int xv_test_ckm( void * arg )
2386 strarg_t * strarg = (strarg_t *)arg;
2388 if ( strargcmp( strarg, "bg" ) == 0 ||
2389 strargcmp( strarg, "man" ) == 0 ||
2390 strargcmp( strarg, "auto" ) == 0 )
2392 return 1;
2395 return 0;
2399 * \brief Modify the colorkey_handling var according to str
2401 * Checks if a valid pointer ( not NULL ) to the string
2402 * was given. And in that case modifies the colorkey_handling
2403 * var to reflect the requested behaviour.
2404 * If nothing happens the content of colorkey_handling stays
2405 * the same.
2407 * \param str Pointer to the string or NULL
2410 void xv_setup_colorkeyhandling(struct vo *vo, const char *ck_method_str,
2411 const char *ck_str)
2413 struct vo_x11_state *x11 = vo->x11;
2414 /* check if a valid pointer to the string was passed */
2415 if ( ck_str )
2417 if ( strncmp( ck_str, "use", 3 ) == 0 )
2419 x11->xv_ck_info.source = CK_SRC_USE;
2421 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2423 x11->xv_ck_info.source = CK_SRC_SET;
2426 /* check if a valid pointer to the string was passed */
2427 if ( ck_method_str )
2429 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2431 x11->xv_ck_info.method = CK_METHOD_BACKGROUND;
2433 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2435 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2437 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2439 x11->xv_ck_info.method = CK_METHOD_AUTOPAINT;
2444 #endif
2446 struct vo_x11_state *vo_x11_init_state(void)
2448 struct vo_x11_state *s = talloc_ptrtype(NULL, s);
2449 *s = (struct vo_x11_state){
2450 .xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR },
2451 .olddecor = MWM_DECOR_ALL,
2452 .oldfuncs = MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
2453 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE,
2454 .old_gravity = NorthWestGravity,
2456 return s;