x11_common: always free x11 context struct on exit
[mplayer/kovensky.git] / libvo / x11_common.c
blob85ba55cc3fd4301054175ee61f873ffb7a55e82c
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <math.h>
22 #include <inttypes.h>
23 #include <limits.h>
25 #include "config.h"
26 #include "options.h"
27 #include "mp_msg.h"
28 #include "mp_fifo.h"
29 #include "libavutil/common.h"
30 #include "x11_common.h"
31 #include "talloc.h"
33 #ifdef X11_FULLSCREEN
35 #include <string.h>
36 #include <unistd.h>
37 #include <assert.h>
39 #include "video_out.h"
40 #include "aspect.h"
41 #include "geometry.h"
42 #include "help_mp.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>
50 #ifdef CONFIG_XSS
51 #include <X11/extensions/scrnsaver.h>
52 #endif
54 #ifdef CONFIG_XDPMS
55 #include <X11/extensions/dpms.h>
56 #endif
58 #ifdef CONFIG_XINERAMA
59 #include <X11/extensions/Xinerama.h>
60 #endif
62 #ifdef CONFIG_XF86VM
63 #include <X11/extensions/xf86vmode.h>
64 #endif
66 #ifdef CONFIG_XF86XK
67 #include <X11/XF86keysym.h>
68 #endif
70 #ifdef CONFIG_XV
71 #include <X11/extensions/Xv.h>
72 #include <X11/extensions/Xvlib.h>
74 #include "subopt-helper.h"
75 #endif
77 #include "input/input.h"
78 #include "input/mouse.h"
80 #define WIN_LAYER_ONBOTTOM 2
81 #define WIN_LAYER_NORMAL 4
82 #define WIN_LAYER_ONTOP 6
83 #define WIN_LAYER_ABOVE_DOCK 10
85 extern int enable_mouse_movements;
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 availale");
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(_WIN_PROTOCOLS);
333 XA_INIT(_WIN_LAYER);
334 XA_INIT(_WIN_HINTS);
335 XA_INIT(WM_PROTOCOLS);
336 XA_INIT(WM_DELETE_WINDOW);
339 void update_xinerama_info(struct vo *vo) {
340 struct MPOpts *opts = vo->opts;
341 int screen = xinerama_screen;
342 xinerama_x = xinerama_y = 0;
343 #ifdef CONFIG_XINERAMA
344 if (screen >= -1 && XineramaIsActive(vo->x11->display))
346 XineramaScreenInfo *screens;
347 int num_screens;
349 screens = XineramaQueryScreens(vo->x11->display, &num_screens);
350 if (screen >= num_screens)
351 screen = num_screens - 1;
352 if (screen == -1) {
353 int x = vo->dx + vo->dwidth / 2;
354 int y = vo->dy + vo->dheight / 2;
355 for (screen = num_screens - 1; screen > 0; screen--) {
356 int left = screens[screen].x_org;
357 int right = left + screens[screen].width;
358 int top = screens[screen].y_org;
359 int bottom = top + screens[screen].height;
360 if (left <= x && x <= right && top <= y && y <= bottom)
361 break;
364 if (screen < 0)
365 screen = 0;
366 opts->vo_screenwidth = screens[screen].width;
367 opts->vo_screenheight = screens[screen].height;
368 xinerama_x = screens[screen].x_org;
369 xinerama_y = screens[screen].y_org;
371 XFree(screens);
373 #endif
374 aspect_save_screenres(vo, opts->vo_screenwidth, opts->vo_screenheight);
377 int vo_init(struct vo *vo)
379 struct MPOpts *opts = vo->opts;
380 struct vo_x11_state *x11 = vo->x11;
381 // int mScreen;
382 int depth, bpp;
383 unsigned int mask;
385 // char * DisplayName = ":0.0";
386 // Display * mDisplay;
387 XImage *mXImage = NULL;
389 // Window mRootWin;
390 XWindowAttributes attribs;
391 char *dispName;
393 if (vo_rootwin)
394 WinID = 0; // use root window
396 if (x11->depthonscreen)
398 saver_off(x11->display);
399 return 1; // already called
402 XSetErrorHandler(x11_errorhandler);
404 #if 0
405 if (!mDisplayName)
406 if (!(mDisplayName = getenv("DISPLAY")))
407 mDisplayName = strdup(":0.0");
408 #else
409 dispName = XDisplayName(mDisplayName);
410 #endif
412 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
414 x11->display = XOpenDisplay(dispName);
415 if (!x11->display)
417 mp_msg(MSGT_VO, MSGL_ERR,
418 "vo: couldn't open the X11 display (%s)!\n", dispName);
419 return 0;
421 x11->screen = DefaultScreen(x11->display); // screen ID
422 x11->rootwin = RootWindow(x11->display, x11->screen); // root window ID
424 init_atoms(vo->x11);
426 #ifdef CONFIG_XF86VM
428 int clock;
430 XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline);
431 if (!opts->vo_screenwidth)
432 opts->vo_screenwidth = modeline.hdisplay;
433 if (!opts->vo_screenheight)
434 opts->vo_screenheight = modeline.vdisplay;
436 #endif
438 if (!opts->vo_screenwidth)
439 opts->vo_screenwidth = DisplayWidth(x11->display, x11->screen);
440 if (!opts->vo_screenheight)
441 opts->vo_screenheight = DisplayHeight(x11->display, x11->screen);
443 // get color depth (from root window, or the best visual):
444 XGetWindowAttributes(x11->display, x11->rootwin, &attribs);
445 depth = attribs.depth;
447 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
449 Visual *visual;
451 depth = vo_find_depth_from_visuals(x11->display, x11->screen, &visual);
452 if (depth != -1)
453 mXImage = XCreateImage(x11->display, visual, depth, ZPixmap,
454 0, NULL, 1, 1, 8, 1);
455 } else
456 mXImage =
457 XGetImage(x11->display, x11->rootwin, 0, 0, 1, 1, AllPlanes, ZPixmap);
459 x11->depthonscreen = depth; // display depth on screen
461 // get bits/pixel from XImage structure:
462 if (mXImage == NULL)
464 mask = 0;
465 } else
468 * for the depth==24 case, the XImage structures might use
469 * 24 or 32 bits of data per pixel. The x11->depthonscreen
470 * field stores the amount of data per pixel in the
471 * XImage structure!
473 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
475 bpp = mXImage->bits_per_pixel;
476 if ((x11->depthonscreen + 7) / 8 != (bpp + 7) / 8)
477 x11->depthonscreen = bpp; // by A'rpi
478 mask =
479 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
480 mp_msg(MSGT_VO, MSGL_V,
481 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
482 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
483 XDestroyImage(mXImage);
485 if (((x11->depthonscreen + 7) / 8) == 2)
487 if (mask == 0x7FFF)
488 x11->depthonscreen = 15;
489 else if (mask == 0xFFFF)
490 x11->depthonscreen = 16;
492 // XCloseDisplay( mDisplay );
493 /* slightly improved local display detection AST */
494 if (strncmp(dispName, "unix:", 5) == 0)
495 dispName += 4;
496 else if (strncmp(dispName, "localhost:", 10) == 0)
497 dispName += 9;
498 if (*dispName == ':' && atoi(dispName + 1) < 10)
499 x11->display_is_local = 1;
500 else
501 x11->display_is_local = 0;
502 mp_msg(MSGT_VO, MSGL_V,
503 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
504 opts->vo_screenwidth, opts->vo_screenheight, depth, x11->depthonscreen,
505 dispName, x11->display_is_local ? "local" : "remote");
507 x11->wm_type = vo_wm_detect(vo);
509 x11->fs_type = vo_x11_get_fs_type(x11->wm_type);
511 fstype_dump(x11->fs_type);
513 saver_off(x11->display);
514 return 1;
517 void vo_uninit(struct vo_x11_state *x11)
519 if (!x11->display)
521 mp_msg(MSGT_VO, MSGL_V,
522 "vo: x11 uninit called but X11 not initialized..\n");
523 } else {
524 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
525 XSetErrorHandler(NULL);
526 XCloseDisplay(x11->display);
527 x11->depthonscreen = 0;
528 x11->display = NULL;
530 talloc_free(x11);
533 #include "osdep/keycodes.h"
534 #include "wskeys.h"
536 #ifdef XF86XK_AudioPause
537 static const struct keymap keysym_map[] = {
538 {XF86XK_MenuKB, KEY_MENU},
539 {XF86XK_AudioPlay, KEY_PLAY}, {XF86XK_AudioPause, KEY_PAUSE}, {XF86XK_AudioStop, KEY_STOP},
540 {XF86XK_AudioPrev, KEY_PREV}, {XF86XK_AudioNext, KEY_NEXT},
541 {XF86XK_AudioMute, KEY_MUTE}, {XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN}, {XF86XK_AudioRaiseVolume, KEY_VOLUME_UP},
542 {0, 0}
545 static void vo_x11_putkey_ext(struct vo *vo, int keysym)
547 struct mp_fifo *f = vo->key_fifo;
548 int mpkey = lookup_keymap_table(keysym_map, keysym);
549 if (mpkey)
550 mplayer_put_key(f, mpkey);
552 #endif
554 static const struct keymap keymap[] = {
555 // special keys
556 {wsEscape, KEY_ESC}, {wsBackSpace, KEY_BS}, {wsTab, KEY_TAB}, {wsEnter, KEY_ENTER},
558 // cursor keys
559 {wsLeft, KEY_LEFT}, {wsRight, KEY_RIGHT}, {wsUp, KEY_UP}, {wsDown, KEY_DOWN},
561 // navigation block
562 {wsInsert, KEY_INSERT}, {wsDelete, KEY_DELETE}, {wsHome, KEY_HOME}, {wsEnd, KEY_END},
563 {wsPageUp, KEY_PAGE_UP}, {wsPageDown, KEY_PAGE_DOWN},
565 // F-keys
566 {wsF1, KEY_F+1}, {wsF2, KEY_F+2}, {wsF3, KEY_F+3}, {wsF4, KEY_F+4},
567 {wsF5, KEY_F+5}, {wsF6, KEY_F+6}, {wsF7, KEY_F+7}, {wsF8, KEY_F+8},
568 {wsF9, KEY_F+9}, {wsF10, KEY_F+10}, {wsF11, KEY_F+11}, {wsF12, KEY_F+12},
570 // numpad independent of numlock
571 {wsGrayMinus, '-'}, {wsGrayPlus, '+'}, {wsGrayMul, '*'}, {wsGrayDiv, '/'},
572 {wsGrayEnter, KEY_KPENTER},
574 // numpad with numlock
575 {wsGray0, KEY_KP0}, {wsGray1, KEY_KP1}, {wsGray2, KEY_KP2},
576 {wsGray3, KEY_KP3}, {wsGray4, KEY_KP4}, {wsGray5, KEY_KP5},
577 {wsGray6, KEY_KP6}, {wsGray7, KEY_KP7}, {wsGray8, KEY_KP8},
578 {wsGray9, KEY_KP9}, {wsGrayDecimal, KEY_KPDEC},
580 // numpad without numlock
581 {wsGrayInsert, KEY_KPINS}, {wsGrayEnd, KEY_KP1}, {wsGrayDown, KEY_KP2},
582 {wsGrayPgDn, KEY_KP3}, {wsGrayLeft, KEY_KP4}, {wsGray5Dup, KEY_KP5},
583 {wsGrayRight, KEY_KP6}, {wsGrayHome, KEY_KP7}, {wsGrayUp, KEY_KP8},
584 {wsGrayPgUp, KEY_KP9}, {wsGrayDelete, KEY_KPDEL},
586 {0, 0}
589 void vo_x11_putkey(struct vo *vo, int key)
591 static const char *passthrough_keys = " -+*/<>`~!@#$%^&()_{}:;\"\',.?\\|=[]";
592 int mpkey = 0;
593 if ((key >= 'a' && key <= 'z') ||
594 (key >= 'A' && key <= 'Z') ||
595 (key >= '0' && key <= '9') ||
596 (key > 0 && key < 256 && strchr(passthrough_keys, key)))
597 mpkey = key;
599 if (!mpkey)
600 mpkey = lookup_keymap_table(keymap, key);
602 if (mpkey)
603 mplayer_put_key(vo->key_fifo, mpkey);
607 // ----- Motif header: -------
609 #define MWM_HINTS_FUNCTIONS (1L << 0)
610 #define MWM_HINTS_DECORATIONS (1L << 1)
611 #define MWM_HINTS_INPUT_MODE (1L << 2)
612 #define MWM_HINTS_STATUS (1L << 3)
614 #define MWM_FUNC_ALL (1L << 0)
615 #define MWM_FUNC_RESIZE (1L << 1)
616 #define MWM_FUNC_MOVE (1L << 2)
617 #define MWM_FUNC_MINIMIZE (1L << 3)
618 #define MWM_FUNC_MAXIMIZE (1L << 4)
619 #define MWM_FUNC_CLOSE (1L << 5)
621 #define MWM_DECOR_ALL (1L << 0)
622 #define MWM_DECOR_BORDER (1L << 1)
623 #define MWM_DECOR_RESIZEH (1L << 2)
624 #define MWM_DECOR_TITLE (1L << 3)
625 #define MWM_DECOR_MENU (1L << 4)
626 #define MWM_DECOR_MINIMIZE (1L << 5)
627 #define MWM_DECOR_MAXIMIZE (1L << 6)
629 #define MWM_INPUT_MODELESS 0
630 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
631 #define MWM_INPUT_SYSTEM_MODAL 2
632 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
633 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
635 #define MWM_TEAROFF_WINDOW (1L<<0)
637 typedef struct
639 long flags;
640 long functions;
641 long decorations;
642 long input_mode;
643 long state;
644 } MotifWmHints;
646 static MotifWmHints vo_MotifWmHints;
647 static Atom vo_MotifHints = None;
649 void vo_x11_decoration(struct vo *vo, int d)
651 struct vo_x11_state *x11 = vo->x11;
652 Atom mtype;
653 int mformat;
654 unsigned long mn, mb;
656 if (!WinID)
657 return;
659 if (vo_fsmode & 8)
661 XSetTransientForHint(x11->display, x11->window,
662 RootWindow(x11->display, x11->screen));
665 vo_MotifHints = XInternAtom(x11->display, "_MOTIF_WM_HINTS", 0);
666 if (vo_MotifHints != None)
668 if (!d)
670 MotifWmHints *mhints = NULL;
672 XGetWindowProperty(x11->display, x11->window,
673 vo_MotifHints, 0, 20, False,
674 vo_MotifHints, &mtype, &mformat, &mn,
675 &mb, (unsigned char **) &mhints);
676 if (mhints)
678 if (mhints->flags & MWM_HINTS_DECORATIONS)
679 x11->olddecor = mhints->decorations;
680 if (mhints->flags & MWM_HINTS_FUNCTIONS)
681 x11->oldfuncs = mhints->functions;
682 XFree(mhints);
686 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
687 vo_MotifWmHints.flags =
688 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
689 if (d)
691 vo_MotifWmHints.functions = x11->oldfuncs;
692 d = x11->olddecor;
694 #if 0
695 vo_MotifWmHints.decorations =
696 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
697 #else
698 vo_MotifWmHints.decorations =
699 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
700 #endif
701 XChangeProperty(x11->display, x11->window, vo_MotifHints,
702 vo_MotifHints, 32,
703 PropModeReplace,
704 (unsigned char *) &vo_MotifWmHints,
705 (vo_fsmode & 4) ? 4 : 5);
709 void vo_x11_classhint(struct vo *vo, Window window, const char *name)
711 struct vo_x11_state *x11 = vo->x11;
712 XClassHint wmClass;
713 pid_t pid = getpid();
715 wmClass.res_name = vo_winname ? vo_winname : name;
716 wmClass.res_class = "MPlayer";
717 XSetClassHint(x11->display, window, &wmClass);
718 XChangeProperty(x11->display, window, x11->XA_NET_WM_PID, XA_CARDINAL,
719 32, PropModeReplace, (unsigned char *) &pid, 1);
722 void vo_x11_uninit(struct vo *vo)
724 struct vo_x11_state *x11 = vo->x11;
725 saver_on(x11->display);
726 if (x11->window != None)
727 vo_showcursor(x11->display, x11->window);
729 if (x11->f_gc)
731 XFreeGC(vo->x11->display, x11->f_gc);
732 x11->f_gc = NULL;
735 if (x11->vo_gc)
737 XSetBackground(vo->x11->display, x11->vo_gc, 0);
738 XFreeGC(vo->x11->display, x11->vo_gc);
739 x11->vo_gc = NULL;
741 if (x11->window != None)
743 XClearWindow(x11->display, x11->window);
744 if (WinID < 0)
746 XEvent xev;
748 XUnmapWindow(x11->display, x11->window);
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;
764 int vo_x11_check_events(struct vo *vo)
766 struct vo_x11_state *x11 = vo->x11;
767 Display *display = vo->x11->display;
768 int ret = 0;
769 XEvent Event;
770 char buf[100];
771 KeySym keySym;
773 // unsigned long vo_KeyTable[512];
775 if ((x11->vo_mouse_autohide) && x11->mouse_waiting_hide &&
776 (GetTimerMS() - x11->mouse_timer >= 1000)) {
777 vo_hidecursor(display, x11->window);
778 x11->mouse_waiting_hide = 0;
781 while (XPending(display))
783 XNextEvent(display, &Event);
784 // printf("\rEvent.type=%X \n",Event.type);
785 switch (Event.type)
787 case Expose:
788 ret |= VO_EVENT_EXPOSE;
789 break;
790 case ConfigureNotify:
791 if (x11->window == None)
792 break;
794 int old_w = vo->dwidth, old_h = vo->dheight;
795 vo_x11_update_geometry(vo);
796 if (vo->dwidth != old_w || vo->dheight != old_h)
797 ret |= VO_EVENT_RESIZE;
799 break;
800 case KeyPress:
802 int key;
804 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
805 &x11->compose_status);
806 #ifdef XF86XK_AudioPause
807 vo_x11_putkey_ext(vo, keySym);
808 #endif
809 key =
810 ((keySym & 0xff00) !=
811 0 ? ((keySym & 0x00ff) + 256) : (keySym));
812 vo_x11_putkey(vo, key);
813 ret |= VO_EVENT_KEYPRESS;
815 break;
816 case MotionNotify:
817 if(enable_mouse_movements)
819 char cmd_str[40];
820 sprintf(cmd_str,"set_mouse_pos %i %i",Event.xmotion.x, Event.xmotion.y);
821 mp_input_queue_cmd(vo->input_ctx,
822 mp_input_parse_cmd(cmd_str));
825 if (x11->vo_mouse_autohide)
827 vo_showcursor(display, x11->window);
828 x11->mouse_waiting_hide = 1;
829 x11->mouse_timer = GetTimerMS();
831 break;
832 case ButtonPress:
833 if (x11->vo_mouse_autohide)
835 vo_showcursor(display, x11->window);
836 x11->mouse_waiting_hide = 1;
837 x11->mouse_timer = GetTimerMS();
839 mplayer_put_key(vo->key_fifo,
840 (MOUSE_BTN0 + Event.xbutton.button - 1)
841 | MP_KEY_DOWN);
842 break;
843 case ButtonRelease:
844 if (x11->vo_mouse_autohide)
846 vo_showcursor(display, x11->window);
847 x11->mouse_waiting_hide = 1;
848 x11->mouse_timer = GetTimerMS();
850 mplayer_put_key(vo->key_fifo,
851 MOUSE_BTN0 + Event.xbutton.button - 1);
852 break;
853 case PropertyNotify:
855 char *name =
856 XGetAtomName(display, Event.xproperty.atom);
858 if (!name)
859 break;
861 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
863 XFree(name);
865 break;
866 case MapNotify:
867 x11->vo_hint.win_gravity = x11->old_gravity;
868 XSetWMNormalHints(display, x11->window, &x11->vo_hint);
869 x11->fs_flip = 0;
870 break;
871 case ClientMessage:
872 if (Event.xclient.message_type == x11->XAWM_PROTOCOLS &&
873 Event.xclient.data.l[0] == x11->XAWM_DELETE_WINDOW)
874 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
875 break;
878 return ret;
882 * \brief sets the size and position of the non-fullscreen window.
884 static void vo_x11_nofs_sizepos(struct vo *vo, int x, int y,
885 int width, int height)
887 struct vo_x11_state *x11 = vo->x11;
888 vo_x11_sizehint(vo, x, y, width, height, 0);
889 if (vo_fs) {
890 x11->vo_old_x = x;
891 x11->vo_old_y = y;
892 x11->vo_old_width = width;
893 x11->vo_old_height = height;
895 else
897 vo->dwidth = width;
898 vo->dheight = height;
899 if (vo->opts->force_window_position)
900 XMoveResizeWindow(vo->x11->display, vo->x11->window, x, y, width,
901 height);
902 else
903 XResizeWindow(vo->x11->display, vo->x11->window, width, height);
907 void vo_x11_sizehint(struct vo *vo, int x, int y, int width, int height, int max)
909 struct vo_x11_state *x11 = vo->x11;
910 x11->vo_hint.flags = 0;
911 if (vo_keepaspect)
913 x11->vo_hint.flags |= PAspect;
914 x11->vo_hint.min_aspect.x = width;
915 x11->vo_hint.min_aspect.y = height;
916 x11->vo_hint.max_aspect.x = width;
917 x11->vo_hint.max_aspect.y = height;
920 x11->vo_hint.flags |= PPosition | PSize;
921 x11->vo_hint.x = x;
922 x11->vo_hint.y = y;
923 x11->vo_hint.width = width;
924 x11->vo_hint.height = height;
925 if (max)
927 x11->vo_hint.flags |= PMaxSize;
928 x11->vo_hint.max_width = width;
929 x11->vo_hint.max_height = height;
930 } else
932 x11->vo_hint.max_width = 0;
933 x11->vo_hint.max_height = 0;
936 // Set minimum height/width to 4 to avoid off-by-one errors
937 // and because mga_vid requires a minimal size of 4 pixels.
938 x11->vo_hint.flags |= PMinSize;
939 x11->vo_hint.min_width = x11->vo_hint.min_height = 4;
941 // Set the base size. A window manager might display the window
942 // size to the user relative to this.
943 // Setting these to width/height might be nice, but e.g. fluxbox can't handle it.
944 x11->vo_hint.flags |= PBaseSize;
945 x11->vo_hint.base_width = 0 /*width*/;
946 x11->vo_hint.base_height = 0 /*height*/;
948 x11->vo_hint.flags |= PWinGravity;
949 x11->vo_hint.win_gravity = StaticGravity;
950 XSetWMNormalHints(x11->display, x11->window, &x11->vo_hint);
953 static int vo_x11_get_gnome_layer(struct vo_x11_state *x11, Window win)
955 Atom type;
956 int format;
957 unsigned long nitems;
958 unsigned long bytesafter;
959 unsigned short *args = NULL;
961 if (XGetWindowProperty(x11->display, win, x11->XA_WIN_LAYER, 0, 16384,
962 False, AnyPropertyType, &type, &format, &nitems,
963 &bytesafter,
964 (unsigned char **) &args) == Success
965 && nitems > 0 && args)
967 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
968 *args);
969 return *args;
971 return WIN_LAYER_NORMAL;
975 static Window vo_x11_create_smooth_window(struct vo_x11_state *x11, Window mRoot,
976 Visual * vis, int x, int y,
977 unsigned int width, unsigned int height,
978 int depth, Colormap col_map)
980 unsigned long xswamask = CWBorderPixel;
981 XSetWindowAttributes xswa;
982 Window ret_win;
984 if (col_map != CopyFromParent)
986 xswa.colormap = col_map;
987 xswamask |= CWColormap;
989 xswa.background_pixel = 0;
990 xswa.border_pixel = 0;
991 xswa.backing_store = NotUseful;
992 xswa.bit_gravity = StaticGravity;
994 ret_win =
995 XCreateWindow(x11->display, x11->rootwin, x, y, width, height, 0, depth,
996 CopyFromParent, vis, xswamask, &xswa);
997 XSetWMProtocols(x11->display, ret_win, &x11->XAWM_DELETE_WINDOW, 1);
998 if (!x11->f_gc)
999 x11->f_gc = XCreateGC(x11->display, ret_win, 0, 0);
1000 XSetForeground(x11->display, x11->f_gc, 0);
1002 return ret_win;
1006 * \brief create and setup a window suitable for display
1007 * \param vis Visual to use for creating the window
1008 * \param x x position of window
1009 * \param y y position of window
1010 * \param width width of window
1011 * \param height height of window
1012 * \param flags flags for window creation.
1013 * Only VOFLAG_FULLSCREEN is supported so far.
1014 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1015 * \param classname name to use for the classhint
1016 * \param title title for the window
1018 * This also does the grunt-work like setting Window Manager hints etc.
1019 * If vo_window is already set it just moves and resizes it.
1021 void vo_x11_create_vo_window(struct vo *vo, XVisualInfo *vis, int x, int y,
1022 unsigned int width, unsigned int height, int flags,
1023 Colormap col_map,
1024 const char *classname, const char *title)
1026 struct MPOpts *opts = vo->opts;
1027 struct vo_x11_state *x11 = vo->x11;
1028 Display *mDisplay = vo->x11->display;
1029 XGCValues xgcv;
1030 if (WinID >= 0) {
1031 vo_fs = flags & VOFLAG_FULLSCREEN;
1032 x11->window = WinID ? (Window)WinID : x11->rootwin;
1033 if (col_map != CopyFromParent) {
1034 unsigned long xswamask = CWColormap;
1035 XSetWindowAttributes xswa;
1036 xswa.colormap = col_map;
1037 XChangeWindowAttributes(mDisplay, x11->window, xswamask, &xswa);
1038 XInstallColormap(mDisplay, col_map);
1040 if (WinID) vo_x11_update_geometry(vo);
1041 vo_x11_selectinput_witherr(mDisplay, x11->window,
1042 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1043 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1044 goto final;
1046 if (x11->window == None) {
1047 XSizeHints hint;
1048 XEvent xev;
1049 vo_fs = 0;
1050 vo->dwidth = width;
1051 vo->dheight = height;
1052 x11->window = vo_x11_create_smooth_window(x11, x11->rootwin, vis->visual,
1053 x, y, width, height, vis->depth, col_map);
1054 vo_x11_classhint(vo, x11->window, classname);
1055 XStoreName(mDisplay, x11->window, title);
1056 vo_hidecursor(mDisplay, x11->window);
1057 XSelectInput(mDisplay, x11->window, StructureNotifyMask);
1058 hint.x = x; hint.y = y;
1059 hint.width = width; hint.height = height;
1060 hint.flags = PPosition | PSize;
1061 XSetStandardProperties(mDisplay, x11->window, title, title, None, NULL, 0, &hint);
1062 vo_x11_sizehint(vo, x, y, width, height, 0);
1063 if (!vo_border) vo_x11_decoration(vo, 0);
1064 // map window
1065 XMapWindow(mDisplay, x11->window);
1066 XClearWindow(mDisplay, x11->window);
1067 // wait for map
1068 do {
1069 XNextEvent(mDisplay, &xev);
1070 } while (xev.type != MapNotify || xev.xmap.event != x11->window);
1071 XSelectInput(mDisplay, x11->window, NoEventMask);
1072 XSync(mDisplay, False);
1073 vo_x11_selectinput_witherr(mDisplay, x11->window,
1074 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1075 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1077 if (opts->vo_ontop) vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1078 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, width, height);
1079 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1080 vo_x11_fullscreen(vo);
1081 else if (vo_fs) {
1082 // if we are already in fullscreen do not switch back and forth, just
1083 // set the size values right.
1084 vo->dwidth = vo->opts->vo_screenwidth;
1085 vo->dheight = vo->opts->vo_screenheight;
1087 final:
1088 if (x11->vo_gc != None)
1089 XFreeGC(mDisplay, x11->vo_gc);
1090 x11->vo_gc = XCreateGC(mDisplay, x11->window, GCForeground, &xgcv);
1091 XSync(mDisplay, False);
1092 x11->vo_mouse_autohide = 1;
1095 void vo_x11_clearwindow_part(struct vo *vo, Window vo_window,
1096 int img_width, int img_height, int use_fs)
1098 struct vo_x11_state *x11 = vo->x11;
1099 struct MPOpts *opts = vo->opts;
1100 Display *mDisplay = vo->x11->display;
1101 int u_dheight, u_dwidth, left_ov, left_ov2;
1103 if (!x11->f_gc)
1104 return;
1106 u_dheight = use_fs ? opts->vo_screenheight : vo->dheight;
1107 u_dwidth = use_fs ? opts->vo_screenwidth : vo->dwidth;
1108 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1109 return;
1111 left_ov = (u_dheight - img_height) / 2;
1112 left_ov2 = (u_dwidth - img_width) / 2;
1114 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, 0, u_dwidth, left_ov);
1115 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, u_dheight - left_ov - 1,
1116 u_dwidth, left_ov + 1);
1118 if (u_dwidth > img_width)
1120 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, left_ov, left_ov2,
1121 img_height);
1122 XFillRectangle(mDisplay, vo_window, x11->f_gc, u_dwidth - left_ov2 - 1,
1123 left_ov, left_ov2 + 1, img_height);
1126 XFlush(mDisplay);
1129 void vo_x11_clearwindow(struct vo *vo, Window vo_window)
1131 struct vo_x11_state *x11 = vo->x11;
1132 struct MPOpts *opts = vo->opts;
1133 if (!x11->f_gc)
1134 return;
1135 XFillRectangle(x11->display, vo_window, x11->f_gc, 0, 0,
1136 opts->vo_screenwidth, opts->vo_screenheight);
1138 XFlush(x11->display);
1142 void vo_x11_setlayer(struct vo *vo, Window vo_window, int layer)
1144 struct vo_x11_state *x11 = vo->x11;
1145 if (WinID >= 0)
1146 return;
1148 if (x11->fs_type & vo_wm_LAYER)
1150 XClientMessageEvent xev;
1152 if (!x11->orig_layer)
1153 x11->orig_layer = vo_x11_get_gnome_layer(x11, vo_window);
1155 memset(&xev, 0, sizeof(xev));
1156 xev.type = ClientMessage;
1157 xev.display = x11->display;
1158 xev.window = vo_window;
1159 xev.message_type = x11->XA_WIN_LAYER;
1160 xev.format = 32;
1161 xev.data.l[0] = layer ? fs_layer : x11->orig_layer; // if not fullscreen, stay on default layer
1162 xev.data.l[1] = CurrentTime;
1163 mp_msg(MSGT_VO, MSGL_V,
1164 "[x11] Layered style stay on top (layer %ld).\n",
1165 xev.data.l[0]);
1166 XSendEvent(x11->display, x11->rootwin, False, SubstructureNotifyMask,
1167 (XEvent *) & xev);
1168 } else if (x11->fs_type & vo_wm_NETWM)
1170 XClientMessageEvent xev;
1171 char *state;
1173 memset(&xev, 0, sizeof(xev));
1174 xev.type = ClientMessage;
1175 xev.message_type = x11->XA_NET_WM_STATE;
1176 xev.display = x11->display;
1177 xev.window = vo_window;
1178 xev.format = 32;
1179 xev.data.l[0] = layer;
1181 if (x11->fs_type & vo_wm_STAYS_ON_TOP)
1182 xev.data.l[1] = x11->XA_NET_WM_STATE_STAYS_ON_TOP;
1183 else if (x11->fs_type & vo_wm_ABOVE)
1184 xev.data.l[1] = x11->XA_NET_WM_STATE_ABOVE;
1185 else if (x11->fs_type & vo_wm_FULLSCREEN)
1186 xev.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
1187 else if (x11->fs_type & vo_wm_BELOW)
1188 // This is not fallback. We can safely assume that the situation
1189 // where only NETWM_STATE_BELOW is supported doesn't exist.
1190 xev.data.l[1] = x11->XA_NET_WM_STATE_BELOW;
1192 XSendEvent(x11->display, x11->rootwin, False, SubstructureRedirectMask,
1193 (XEvent *) & xev);
1194 state = XGetAtomName(x11->display, xev.data.l[1]);
1195 mp_msg(MSGT_VO, MSGL_V,
1196 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1197 layer, state);
1198 XFree(state);
1202 static int vo_x11_get_fs_type(int supported)
1204 int i;
1205 int type = supported;
1207 if (vo_fstype_list)
1209 for (i = 0; vo_fstype_list[i]; i++)
1211 int neg = 0;
1212 char *arg = vo_fstype_list[i];
1214 if (vo_fstype_list[i][0] == '-')
1216 neg = 1;
1217 arg = vo_fstype_list[i] + 1;
1220 if (!strncmp(arg, "layer", 5))
1222 if (!neg && (arg[5] == '='))
1224 char *endptr = NULL;
1225 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1227 if (endptr && *endptr == '\0' && layer >= 0
1228 && layer <= 15)
1229 fs_layer = layer;
1231 if (neg)
1232 type &= ~vo_wm_LAYER;
1233 else
1234 type |= vo_wm_LAYER;
1235 } else if (!strcmp(arg, "above"))
1237 if (neg)
1238 type &= ~vo_wm_ABOVE;
1239 else
1240 type |= vo_wm_ABOVE;
1241 } else if (!strcmp(arg, "fullscreen"))
1243 if (neg)
1244 type &= ~vo_wm_FULLSCREEN;
1245 else
1246 type |= vo_wm_FULLSCREEN;
1247 } else if (!strcmp(arg, "stays_on_top"))
1249 if (neg)
1250 type &= ~vo_wm_STAYS_ON_TOP;
1251 else
1252 type |= vo_wm_STAYS_ON_TOP;
1253 } else if (!strcmp(arg, "below"))
1255 if (neg)
1256 type &= ~vo_wm_BELOW;
1257 else
1258 type |= vo_wm_BELOW;
1259 } else if (!strcmp(arg, "netwm"))
1261 if (neg)
1262 type &= ~vo_wm_NETWM;
1263 else
1264 type |= vo_wm_NETWM;
1265 } else if (!strcmp(arg, "none"))
1266 type = 0; // clear; keep parsing
1270 return type;
1274 * \brief update vo->dx, vo->dy, vo->dwidth and vo->dheight with current values of vo->x11->window
1275 * \return returns current color depth of vo->x11->window
1277 int vo_x11_update_geometry(struct vo *vo)
1279 struct vo_x11_state *x11 = vo->x11;
1280 unsigned depth, w, h;
1281 int dummy_int;
1282 Window dummy_win;
1283 XGetGeometry(x11->display, x11->window, &dummy_win, &dummy_int, &dummy_int,
1284 &w, &h, &dummy_int, &depth);
1285 if (w <= INT_MAX && h <= INT_MAX) {
1286 vo->dwidth = w;
1287 vo->dheight = h;
1289 XTranslateCoordinates(x11->display, x11->window, x11->rootwin, 0, 0,
1290 &vo->dx, &vo->dy, &dummy_win);
1291 if (vo_wintitle)
1292 XStoreName(x11->display, x11->window, vo_wintitle);
1294 return depth <= INT_MAX ? depth : 0;
1297 void vo_x11_fullscreen(struct vo *vo)
1299 struct MPOpts *opts = vo->opts;
1300 struct vo_x11_state *x11 = vo->x11;
1301 int x, y, w, h;
1302 x = x11->vo_old_x;
1303 y = x11->vo_old_y;
1304 w = x11->vo_old_width;
1305 h = x11->vo_old_height;
1307 if (WinID >= 0) {
1308 vo_fs = !vo_fs;
1309 return;
1311 if (x11->fs_flip)
1312 return;
1314 if (vo_fs)
1316 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1317 vo_fs = VO_FALSE;
1318 } else
1320 // win->fs
1321 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1323 vo_fs = VO_TRUE;
1324 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1326 x11->vo_old_x = vo->dx;
1327 x11->vo_old_y = vo->dy;
1328 x11->vo_old_width = vo->dwidth;
1329 x11->vo_old_height = vo->dheight;
1331 update_xinerama_info(vo);
1332 x = xinerama_x;
1333 y = xinerama_y;
1334 w = opts->vo_screenwidth;
1335 h = opts->vo_screenheight;
1338 long dummy;
1340 XGetWMNormalHints(x11->display, x11->window, &x11->vo_hint, &dummy);
1341 if (!(x11->vo_hint.flags & PWinGravity))
1342 x11->old_gravity = NorthWestGravity;
1343 else
1344 x11->old_gravity = x11->vo_hint.win_gravity;
1346 if (x11->wm_type == 0 && !(vo_fsmode & 16))
1348 XUnmapWindow(x11->display, x11->window); // required for MWM
1349 XWithdrawWindow(x11->display, x11->window, x11->screen);
1350 x11->fs_flip = 1;
1353 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1355 vo_x11_decoration(vo, vo_border && !vo_fs);
1356 vo_x11_sizehint(vo, x, y, w, h, 0);
1357 vo_x11_setlayer(vo, x11->window, vo_fs);
1360 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1362 /* some WMs lose ontop after fullscreen */
1363 if ((!(vo_fs)) & opts->vo_ontop)
1364 vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1366 XMapRaised(x11->display, x11->window);
1367 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1368 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1369 XRaiseWindow(x11->display, x11->window);
1370 XFlush(x11->display);
1373 void vo_x11_ontop(struct vo *vo)
1375 struct MPOpts *opts = vo->opts;
1376 opts->vo_ontop = !opts->vo_ontop;
1378 vo_x11_setlayer(vo, vo->x11->window, opts->vo_ontop);
1381 void vo_x11_border(struct vo *vo)
1383 vo_border = !vo_border;
1384 vo_x11_decoration(vo, vo_border && !vo_fs);
1388 * XScreensaver stuff
1391 static int screensaver_off;
1392 static unsigned int time_last;
1394 void xscreensaver_heartbeat(struct vo_x11_state *x11)
1396 unsigned int time = GetTimerMS();
1398 if (x11->display && screensaver_off && (time - time_last) > 30000)
1400 time_last = time;
1402 XResetScreenSaver(x11->display);
1406 static int xss_suspend(Display *mDisplay, Bool suspend)
1408 #ifndef CONFIG_XSS
1409 return 0;
1410 #else
1411 int event, error, major, minor;
1412 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1413 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1414 return 0;
1415 if (major < 1 || (major == 1 && minor < 1))
1416 return 0;
1417 XScreenSaverSuspend(mDisplay, suspend);
1418 return 1;
1419 #endif
1423 * End of XScreensaver stuff
1426 static void saver_on(Display * mDisplay)
1429 if (!screensaver_off)
1430 return;
1431 screensaver_off = 0;
1432 if (xss_suspend(mDisplay, False))
1433 return;
1434 #ifdef CONFIG_XDPMS
1435 if (dpms_disabled)
1437 int nothing;
1438 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1440 if (!DPMSEnable(mDisplay))
1441 { // restoring power saving settings
1442 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1443 } else
1445 // DPMS does not seem to be enabled unless we call DPMSInfo
1446 BOOL onoff;
1447 CARD16 state;
1449 DPMSForceLevel(mDisplay, DPMSModeOn);
1450 DPMSInfo(mDisplay, &state, &onoff);
1451 if (onoff)
1453 mp_msg(MSGT_VO, MSGL_V,
1454 "Successfully enabled DPMS\n");
1455 } else
1457 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1461 dpms_disabled = 0;
1463 #endif
1466 static void saver_off(Display * mDisplay)
1468 int nothing;
1470 if (screensaver_off)
1471 return;
1472 screensaver_off = 1;
1473 if (xss_suspend(mDisplay, True))
1474 return;
1475 #ifdef CONFIG_XDPMS
1476 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1478 BOOL onoff;
1479 CARD16 state;
1481 DPMSInfo(mDisplay, &state, &onoff);
1482 if (onoff)
1484 Status stat;
1486 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1487 dpms_disabled = 1;
1488 stat = DPMSDisable(mDisplay); // monitor powersave off
1489 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1492 #endif
1495 static XErrorHandler old_handler = NULL;
1496 static int selectinput_err = 0;
1497 static int x11_selectinput_errorhandler(Display * display,
1498 XErrorEvent * event)
1500 if (event->error_code == BadAccess)
1502 selectinput_err = 1;
1503 mp_msg(MSGT_VO, MSGL_ERR,
1504 "X11 error: BadAccess during XSelectInput Call\n");
1505 mp_msg(MSGT_VO, MSGL_ERR,
1506 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1507 /* If you think MPlayer should shutdown with this error,
1508 * comment out the following line */
1509 return 0;
1511 if (old_handler != NULL)
1512 old_handler(display, event);
1513 else
1514 x11_errorhandler(display, event);
1515 return 0;
1518 void vo_x11_selectinput_witherr(Display * display, Window w,
1519 long event_mask)
1521 XSync(display, False);
1522 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1523 selectinput_err = 0;
1524 if (vo_nomouse_input)
1526 XSelectInput(display, w,
1527 event_mask &
1528 (~(ButtonPressMask | ButtonReleaseMask)));
1529 } else
1531 XSelectInput(display, w, event_mask);
1533 XSync(display, False);
1534 XSetErrorHandler(old_handler);
1535 if (selectinput_err)
1537 mp_msg(MSGT_VO, MSGL_ERR,
1538 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1539 XSelectInput(display, w,
1540 event_mask &
1542 (ButtonPressMask | ButtonReleaseMask |
1543 PointerMotionMask)));
1547 #ifdef CONFIG_XF86VM
1548 void vo_vm_switch(struct vo *vo)
1550 struct vo_x11_state *x11 = vo->x11;
1551 struct MPOpts *opts = vo->opts;
1552 Display *mDisplay = x11->display;
1553 int vm_event, vm_error;
1554 int vm_ver, vm_rev;
1555 int i, j, have_vm = 0;
1556 int X = vo->dwidth, Y = vo->dheight;
1557 int modeline_width, modeline_height;
1559 int modecount;
1561 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1563 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1564 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1565 vm_rev);
1566 have_vm = 1;
1567 } else {
1568 mp_msg(MSGT_VO, MSGL_WARN,
1569 "XF86VidMode extension not available.\n");
1572 if (have_vm)
1574 if (vidmodes == NULL)
1575 XF86VidModeGetAllModeLines(mDisplay, x11->screen, &modecount,
1576 &vidmodes);
1577 j = 0;
1578 modeline_width = vidmodes[0]->hdisplay;
1579 modeline_height = vidmodes[0]->vdisplay;
1581 for (i = 1; i < modecount; i++)
1582 if ((vidmodes[i]->hdisplay >= X)
1583 && (vidmodes[i]->vdisplay >= Y))
1584 if ((vidmodes[i]->hdisplay <= modeline_width)
1585 && (vidmodes[i]->vdisplay <= modeline_height))
1587 modeline_width = vidmodes[i]->hdisplay;
1588 modeline_height = vidmodes[i]->vdisplay;
1589 j = i;
1592 mp_tmsg(MSGT_VO, MSGL_INFO, "XF86VM: Selected video mode %dx%d for image size %dx%d.\n",
1593 modeline_width, modeline_height, X, Y);
1594 XF86VidModeLockModeSwitch(mDisplay, x11->screen, 0);
1595 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1596 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1598 // FIXME: all this is more of a hack than proper solution
1599 X = (opts->vo_screenwidth - modeline_width) / 2;
1600 Y = (opts->vo_screenheight - modeline_height) / 2;
1601 XF86VidModeSetViewPort(mDisplay, x11->screen, X, Y);
1602 vo->dx = X;
1603 vo->dy = Y;
1604 vo->dwidth = modeline_width;
1605 vo->dheight = modeline_height;
1606 aspect_save_screenres(vo, modeline_width, modeline_height);
1610 void vo_vm_close(struct vo *vo)
1612 Display *dpy = vo->x11->display;
1613 struct MPOpts *opts = vo->opts;
1614 if (vidmodes != NULL)
1616 int i, modecount;
1618 free(vidmodes);
1619 vidmodes = NULL;
1620 XF86VidModeGetAllModeLines(dpy, vo->x11->screen, &modecount,
1621 &vidmodes);
1622 for (i = 0; i < modecount; i++)
1623 if ((vidmodes[i]->hdisplay == opts->vo_screenwidth)
1624 && (vidmodes[i]->vdisplay == opts->vo_screenheight))
1626 mp_msg(MSGT_VO, MSGL_INFO,
1627 "Returning to original mode %dx%d\n",
1628 opts->vo_screenwidth, opts->vo_screenheight);
1629 break;
1632 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1633 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1634 free(vidmodes);
1635 vidmodes = NULL;
1639 double vo_vm_get_fps(struct vo *vo)
1641 struct vo_x11_state *x11 = vo->x11;
1642 int clock;
1643 XF86VidModeModeLine modeline;
1644 if (!XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline))
1645 return 0;
1646 if (modeline.privsize)
1647 XFree(modeline.private);
1648 return 1e3 * clock / modeline.htotal / modeline.vtotal;
1650 #endif
1652 #endif /* X11_FULLSCREEN */
1656 * Scan the available visuals on this Display/Screen. Try to find
1657 * the 'best' available TrueColor visual that has a decent color
1658 * depth (at least 15bit). If there are multiple visuals with depth
1659 * >= 15bit, we prefer visuals with a smaller color depth.
1661 int vo_find_depth_from_visuals(Display * dpy, int screen,
1662 Visual ** visual_return)
1664 XVisualInfo visual_tmpl;
1665 XVisualInfo *visuals;
1666 int nvisuals, i;
1667 int bestvisual = -1;
1668 int bestvisual_depth = -1;
1670 visual_tmpl.screen = screen;
1671 visual_tmpl.class = TrueColor;
1672 visuals = XGetVisualInfo(dpy,
1673 VisualScreenMask | VisualClassMask,
1674 &visual_tmpl, &nvisuals);
1675 if (visuals != NULL)
1677 for (i = 0; i < nvisuals; i++)
1679 mp_msg(MSGT_VO, MSGL_V,
1680 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1681 visuals[i].visualid, visuals[i].depth,
1682 visuals[i].red_mask, visuals[i].green_mask,
1683 visuals[i].blue_mask);
1685 * Save the visual index and its depth, if this is the first
1686 * truecolor visul, or a visual that is 'preferred' over the
1687 * previous 'best' visual.
1689 if (bestvisual_depth == -1
1690 || (visuals[i].depth >= 15
1691 && (visuals[i].depth < bestvisual_depth
1692 || bestvisual_depth < 15)))
1694 bestvisual = i;
1695 bestvisual_depth = visuals[i].depth;
1699 if (bestvisual != -1 && visual_return != NULL)
1700 *visual_return = visuals[bestvisual].visual;
1702 XFree(visuals);
1704 return bestvisual_depth;
1708 static Colormap cmap = None;
1709 static XColor cols[256];
1710 static int cm_size, red_mask, green_mask, blue_mask;
1713 Colormap vo_x11_create_colormap(struct vo *vo, XVisualInfo *vinfo)
1715 struct vo_x11_state *x11 = vo->x11;
1716 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1718 if (vinfo->class != DirectColor)
1719 return XCreateColormap(x11->display, x11->rootwin, vinfo->visual,
1720 AllocNone);
1722 /* can this function get called twice or more? */
1723 if (cmap)
1724 return cmap;
1725 cm_size = vinfo->colormap_size;
1726 red_mask = vinfo->red_mask;
1727 green_mask = vinfo->green_mask;
1728 blue_mask = vinfo->blue_mask;
1729 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1730 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1731 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1732 rvu = 65536ull * ru / (red_mask + ru);
1733 gvu = 65536ull * gu / (green_mask + gu);
1734 bvu = 65536ull * bu / (blue_mask + bu);
1735 r = g = b = 0;
1736 rv = gv = bv = 0;
1737 m = DoRed | DoGreen | DoBlue;
1738 for (k = 0; k < cm_size; k++)
1740 int t;
1742 cols[k].pixel = r | g | b;
1743 cols[k].red = rv;
1744 cols[k].green = gv;
1745 cols[k].blue = bv;
1746 cols[k].flags = m;
1747 t = (r + ru) & red_mask;
1748 if (t < r)
1749 m &= ~DoRed;
1750 r = t;
1751 t = (g + gu) & green_mask;
1752 if (t < g)
1753 m &= ~DoGreen;
1754 g = t;
1755 t = (b + bu) & blue_mask;
1756 if (t < b)
1757 m &= ~DoBlue;
1758 b = t;
1759 rv += rvu;
1760 gv += gvu;
1761 bv += bvu;
1763 cmap = XCreateColormap(x11->display, x11->rootwin, vinfo->visual, AllocAll);
1764 XStoreColors(x11->display, cmap, cols, cm_size);
1765 return cmap;
1769 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1770 * hue and red/green/blue intensity, but we cannot do saturation.
1771 * Currently only gamma, brightness and contrast are implemented.
1772 * Is there sufficient interest for hue and/or red/green/blue intensity?
1774 /* these values have range [-100,100] and are initially 0 */
1775 static int vo_gamma = 0;
1776 static int vo_brightness = 0;
1777 static int vo_contrast = 0;
1779 static int transform_color(float val,
1780 float brightness, float contrast, float gamma) {
1781 float s = pow(val, gamma);
1782 s = (s - 0.5) * contrast + 0.5;
1783 s += brightness;
1784 if (s < 0)
1785 s = 0;
1786 if (s > 1)
1787 s = 1;
1788 return (unsigned short) (s * 65535);
1791 uint32_t vo_x11_set_equalizer(struct vo *vo, char *name, int value)
1793 float gamma, brightness, contrast;
1794 float rf, gf, bf;
1795 int k;
1798 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1799 * of TrueColor-ed window but be careful:
1800 * Unlike the colormaps, which are private for the X client
1801 * who created them and thus automatically destroyed on client
1802 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1803 * setting and _must_ be restored before the process exits.
1804 * Unforunately when the process crashes (or gets killed
1805 * for some reason) it is impossible to restore the setting,
1806 * and such behaviour could be rather annoying for the users.
1808 if (cmap == None)
1809 return VO_NOTAVAIL;
1811 if (!strcasecmp(name, "brightness"))
1812 vo_brightness = value;
1813 else if (!strcasecmp(name, "contrast"))
1814 vo_contrast = value;
1815 else if (!strcasecmp(name, "gamma"))
1816 vo_gamma = value;
1817 else
1818 return VO_NOTIMPL;
1820 brightness = 0.01 * vo_brightness;
1821 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1822 gamma = pow(2, -0.02 * vo_gamma);
1824 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1825 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
1826 green_mask;
1827 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
1829 /* now recalculate the colormap using the newly set value */
1830 for (k = 0; k < cm_size; k++)
1832 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
1833 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
1834 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
1837 XStoreColors(vo->x11->display, cmap, cols, cm_size);
1838 XFlush(vo->x11->display);
1839 return VO_TRUE;
1842 uint32_t vo_x11_get_equalizer(char *name, int *value)
1844 if (cmap == None)
1845 return VO_NOTAVAIL;
1846 if (!strcasecmp(name, "brightness"))
1847 *value = vo_brightness;
1848 else if (!strcasecmp(name, "contrast"))
1849 *value = vo_contrast;
1850 else if (!strcasecmp(name, "gamma"))
1851 *value = vo_gamma;
1852 else
1853 return VO_NOTIMPL;
1854 return VO_TRUE;
1857 #ifdef CONFIG_XV
1858 int vo_xv_set_eq(struct vo *vo, uint32_t xv_port, char *name, int value)
1860 XvAttribute *attributes;
1861 int i, howmany, xv_atom;
1863 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
1865 /* get available attributes */
1866 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1867 for (i = 0; i < howmany && attributes; i++)
1868 if (attributes[i].flags & XvSettable)
1870 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1871 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1872 then trigger it if it's ok so that the other values are at default upon query */
1873 if (xv_atom != None)
1875 int hue = 0, port_value, port_min, port_max;
1877 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1878 (!strcasecmp(name, "brightness")))
1879 port_value = value;
1880 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1881 (!strcasecmp(name, "contrast")))
1882 port_value = value;
1883 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1884 (!strcasecmp(name, "saturation")))
1885 port_value = value;
1886 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1887 (!strcasecmp(name, "hue")))
1889 port_value = value;
1890 hue = 1;
1891 } else
1892 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1893 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1894 (!strcasecmp(name, "red_intensity")))
1895 port_value = value;
1896 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1897 && (!strcasecmp(name, "green_intensity")))
1898 port_value = value;
1899 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1900 && (!strcasecmp(name, "blue_intensity")))
1901 port_value = value;
1902 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
1903 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
1904 && (!strcasecmp(name, "bt_709")))
1905 port_value = value;
1906 else
1907 continue;
1909 port_min = attributes[i].min_value;
1910 port_max = attributes[i].max_value;
1912 /* nvidia hue workaround */
1913 if (hue && port_min == 0 && port_max == 360)
1915 port_value =
1916 (port_value >=
1917 0) ? (port_value - 100) : (port_value + 100);
1919 // -100 -> min
1920 // 0 -> (max+min)/2
1921 // +100 -> max
1922 port_value =
1923 (port_value + 100) * (port_max - port_min) / 200 +
1924 port_min;
1925 XvSetPortAttribute(vo->x11->display, xv_port, xv_atom, port_value);
1926 return VO_TRUE;
1929 return VO_FALSE;
1932 int vo_xv_get_eq(struct vo *vo, uint32_t xv_port, char *name, int *value)
1935 XvAttribute *attributes;
1936 int i, howmany, xv_atom;
1938 /* get available attributes */
1939 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1940 for (i = 0; i < howmany && attributes; i++)
1941 if (attributes[i].flags & XvGettable)
1943 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1944 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1945 then trigger it if it's ok so that the other values are at default upon query */
1946 if (xv_atom != None)
1948 int val, port_value = 0, port_min, port_max;
1950 XvGetPortAttribute(vo->x11->display, xv_port, xv_atom,
1951 &port_value);
1953 port_min = attributes[i].min_value;
1954 port_max = attributes[i].max_value;
1955 val =
1956 (port_value - port_min) * 200 / (port_max - port_min) -
1957 100;
1959 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1960 (!strcasecmp(name, "brightness")))
1961 *value = val;
1962 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1963 (!strcasecmp(name, "contrast")))
1964 *value = val;
1965 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1966 (!strcasecmp(name, "saturation")))
1967 *value = val;
1968 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1969 (!strcasecmp(name, "hue")))
1971 /* nasty nvidia detect */
1972 if (port_min == 0 && port_max == 360)
1973 *value = (val >= 0) ? (val - 100) : (val + 100);
1974 else
1975 *value = val;
1976 } else
1977 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1978 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1979 (!strcasecmp(name, "red_intensity")))
1980 *value = val;
1981 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1982 && (!strcasecmp(name, "green_intensity")))
1983 *value = val;
1984 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1985 && (!strcasecmp(name, "blue_intensity")))
1986 *value = val;
1987 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
1988 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
1989 && (!strcasecmp(name, "bt_709")))
1990 *value = val;
1991 else
1992 continue;
1994 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
1995 name, *value);
1996 return VO_TRUE;
1999 return VO_FALSE;
2003 * \brief Interns the requested atom if it is available.
2005 * \param atom_name String containing the name of the requested atom.
2007 * \return Returns the atom if available, else None is returned.
2010 static Atom xv_intern_atom_if_exists(struct vo_x11_state *x11,
2011 char const *atom_name)
2013 XvAttribute * attributes;
2014 int attrib_count,i;
2015 Atom xv_atom = None;
2017 attributes = XvQueryPortAttributes(x11->display, x11->xv_port, &attrib_count );
2018 if( attributes!=NULL )
2020 for ( i = 0; i < attrib_count; ++i )
2022 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2024 xv_atom = XInternAtom(x11->display, atom_name, False );
2025 break; // found what we want, break out
2028 XFree( attributes );
2031 return xv_atom;
2035 * \brief Try to enable vsync for xv.
2036 * \return Returns -1 if not available, 0 on failure and 1 on success.
2038 int vo_xv_enable_vsync(struct vo *vo)
2040 struct vo_x11_state *x11 = vo->x11;
2041 Atom xv_atom = xv_intern_atom_if_exists(x11, "XV_SYNC_TO_VBLANK");
2042 if (xv_atom == None)
2043 return -1;
2044 return XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1) == Success;
2048 * \brief Get maximum supported source image dimensions.
2050 * This function does not set the variables pointed to by
2051 * width and height if the information could not be retrieved,
2052 * so the caller is reponsible for properly initializing them.
2054 * \param width [out] The maximum width gets stored here.
2055 * \param height [out] The maximum height gets stored here.
2058 void vo_xv_get_max_img_dim(struct vo *vo, uint32_t * width, uint32_t * height)
2060 struct vo_x11_state *x11 = vo->x11;
2061 XvEncodingInfo * encodings;
2062 //unsigned long num_encodings, idx; to int or too long?!
2063 unsigned int num_encodings, idx;
2065 XvQueryEncodings(x11->display, x11->xv_port, &num_encodings, &encodings);
2067 if ( encodings )
2069 for ( idx = 0; idx < num_encodings; ++idx )
2071 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2073 *width = encodings[idx].width;
2074 *height = encodings[idx].height;
2075 break;
2080 mp_msg( MSGT_VO, MSGL_V,
2081 "[xv common] Maximum source image dimensions: %ux%u\n",
2082 *width, *height );
2084 XvFreeEncodingInfo( encodings );
2088 * \brief Print information about the colorkey method and source.
2090 * \param ck_handling Integer value containing the information about
2091 * colorkey handling (see x11_common.h).
2093 * Outputs the content of |ck_handling| as a readable message.
2096 static void vo_xv_print_ck_info(struct vo_x11_state *x11)
2098 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2100 switch ( x11->xv_ck_info.method )
2102 case CK_METHOD_NONE:
2103 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2104 case CK_METHOD_AUTOPAINT:
2105 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2106 case CK_METHOD_MANUALFILL:
2107 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2108 case CK_METHOD_BACKGROUND:
2109 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2112 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2114 switch ( x11->xv_ck_info.source )
2116 case CK_SRC_CUR:
2117 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2118 x11->xv_colorkey );
2119 break;
2120 case CK_SRC_USE:
2121 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2123 mp_msg( MSGT_VO, MSGL_V,
2124 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2125 x11->xv_colorkey );
2127 else
2129 mp_msg( MSGT_VO, MSGL_V,
2130 "Using colorkey from MPlayer (0x%06lx)."
2131 " Use -colorkey to change.\n",
2132 x11->xv_colorkey );
2134 break;
2135 case CK_SRC_SET:
2136 mp_msg( MSGT_VO, MSGL_V,
2137 "Setting and using colorkey from MPlayer (0x%06lx)."
2138 " Use -colorkey to change.\n",
2139 x11->xv_colorkey );
2140 break;
2144 * \brief Init colorkey depending on the settings in xv_ck_info.
2146 * \return Returns 0 on failure and 1 on success.
2148 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2149 * flags in xv_ck_info.
2151 * Possiblilities:
2152 * * Methods
2153 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2154 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2155 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2156 * * Sources
2157 * - use currently set colorkey ( CK_SRC_CUR )
2158 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2159 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2161 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2162 * we don't draw anything as this means it was forced to off.
2164 int vo_xv_init_colorkey(struct vo *vo)
2166 struct vo_x11_state *x11 = vo->x11;
2167 Atom xv_atom;
2168 int rez;
2170 /* check if colorkeying is needed */
2171 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_COLORKEY");
2173 /* if we have to deal with colorkeying ... */
2174 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2176 /* check if we should use the colorkey specified in vo_colorkey */
2177 if ( x11->xv_ck_info.source != CK_SRC_CUR )
2179 x11->xv_colorkey = vo_colorkey;
2181 /* check if we have to set the colorkey too */
2182 if ( x11->xv_ck_info.source == CK_SRC_SET )
2184 xv_atom = XInternAtom(x11->display, "XV_COLORKEY",False);
2186 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, vo_colorkey);
2187 if ( rez != Success )
2189 mp_msg( MSGT_VO, MSGL_FATAL,
2190 "[xv common] Couldn't set colorkey!\n" );
2191 return 0; // error setting colorkey
2195 else
2197 int colorkey_ret;
2199 rez=XvGetPortAttribute(x11->display,x11->xv_port, xv_atom, &colorkey_ret);
2200 if ( rez == Success )
2202 x11->xv_colorkey = colorkey_ret;
2204 else
2206 mp_msg( MSGT_VO, MSGL_FATAL,
2207 "[xv common] Couldn't get colorkey!"
2208 "Maybe the selected Xv port has no overlay.\n" );
2209 return 0; // error getting colorkey
2213 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_AUTOPAINT_COLORKEY");
2215 /* should we draw the colorkey ourselves or activate autopainting? */
2216 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2218 rez = !Success; // reset rez to something different than Success
2220 if ( xv_atom != None ) // autopaint is supported
2222 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1);
2225 if ( rez != Success )
2227 // fallback to manual colorkey drawing
2228 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2231 else // disable colorkey autopainting if supported
2233 if ( xv_atom != None ) // we have autopaint attribute
2235 XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 0);
2239 else // do no colorkey drawing at all
2241 x11->xv_ck_info.method = CK_METHOD_NONE;
2242 } /* end: should we draw colorkey */
2244 /* output information about the current colorkey settings */
2245 vo_xv_print_ck_info(x11);
2247 return 1; // success
2251 * \brief Draw the colorkey on the video window.
2253 * Draws the colorkey depending on the set method ( colorkey_handling ).
2255 * Also draws the black bars ( when the video doesn't fit the display in
2256 * fullscreen ) separately, so they don't overlap with the video area.
2257 * It doesn't call XFlush.
2260 void vo_xv_draw_colorkey(struct vo *vo, int32_t x, int32_t y,
2261 int32_t w, int32_t h)
2263 struct MPOpts *opts = vo->opts;
2264 struct vo_x11_state *x11 = vo->x11;
2265 if( x11->xv_ck_info.method == CK_METHOD_MANUALFILL ||
2266 x11->xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2268 XSetForeground(x11->display, x11->vo_gc, x11->xv_colorkey );
2269 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2270 x, y,
2271 w, h );
2274 /* draw black bars if needed */
2275 /* TODO! move this to vo_x11_clearwindow_part() */
2276 if ( vo_fs )
2278 XSetForeground(x11->display, x11->vo_gc, 0 );
2279 /* making non-overlap fills, requires 8 checks instead of 4 */
2280 if ( y > 0 )
2281 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2282 0, 0,
2283 opts->vo_screenwidth, y);
2284 if (x > 0)
2285 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2286 0, 0,
2287 x, opts->vo_screenheight);
2288 if (x + w < opts->vo_screenwidth)
2289 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2290 x + w, 0,
2291 opts->vo_screenwidth, opts->vo_screenheight);
2292 if (y + h < opts->vo_screenheight)
2293 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2294 0, y + h,
2295 opts->vo_screenwidth, opts->vo_screenheight);
2299 /** \brief Tests if a valid argument for the ck suboption was given. */
2300 int xv_test_ck( void * arg )
2302 strarg_t * strarg = (strarg_t *)arg;
2304 if ( strargcmp( strarg, "use" ) == 0 ||
2305 strargcmp( strarg, "set" ) == 0 ||
2306 strargcmp( strarg, "cur" ) == 0 )
2308 return 1;
2311 return 0;
2313 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2314 int xv_test_ckm( void * arg )
2316 strarg_t * strarg = (strarg_t *)arg;
2318 if ( strargcmp( strarg, "bg" ) == 0 ||
2319 strargcmp( strarg, "man" ) == 0 ||
2320 strargcmp( strarg, "auto" ) == 0 )
2322 return 1;
2325 return 0;
2329 * \brief Modify the colorkey_handling var according to str
2331 * Checks if a valid pointer ( not NULL ) to the string
2332 * was given. And in that case modifies the colorkey_handling
2333 * var to reflect the requested behaviour.
2334 * If nothing happens the content of colorkey_handling stays
2335 * the same.
2337 * \param str Pointer to the string or NULL
2340 void xv_setup_colorkeyhandling(struct vo *vo, const char *ck_method_str,
2341 const char *ck_str)
2343 struct vo_x11_state *x11 = vo->x11;
2344 /* check if a valid pointer to the string was passed */
2345 if ( ck_str )
2347 if ( strncmp( ck_str, "use", 3 ) == 0 )
2349 x11->xv_ck_info.source = CK_SRC_USE;
2351 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2353 x11->xv_ck_info.source = CK_SRC_SET;
2356 /* check if a valid pointer to the string was passed */
2357 if ( ck_method_str )
2359 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2361 x11->xv_ck_info.method = CK_METHOD_BACKGROUND;
2363 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2365 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2367 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2369 x11->xv_ck_info.method = CK_METHOD_AUTOPAINT;
2374 #endif
2376 struct vo_x11_state *vo_x11_init_state(void)
2378 struct vo_x11_state *s = talloc_ptrtype(NULL, s);
2379 *s = (struct vo_x11_state){
2380 .xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR },
2381 .olddecor = MWM_DECOR_ALL,
2382 .oldfuncs = MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
2383 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE,
2384 .old_gravity = NorthWestGravity,
2386 return s;