cache: don't modify argument when stream control fails
[mplayer.git] / libvo / x11_common.c
blob0a193fb0d3cec104d151b7052e209014f1528743
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <math.h>
22 #include <inttypes.h>
23 #include <limits.h>
25 #include "config.h"
26 #include "options.h"
27 #include "mp_msg.h"
28 #include "mp_fifo.h"
29 #include "libavutil/common.h"
30 #include "x11_common.h"
31 #include "talloc.h"
33 #ifdef X11_FULLSCREEN
35 #include <string.h>
36 #include <unistd.h>
37 #include <assert.h>
39 #include "video_out.h"
40 #include "aspect.h"
41 #include "geometry.h"
42 #include "osdep/timer.h"
44 #include <X11/Xmd.h>
45 #include <X11/Xlib.h>
46 #include <X11/Xutil.h>
47 #include <X11/Xatom.h>
49 #ifdef CONFIG_XSS
50 #include <X11/extensions/scrnsaver.h>
51 #endif
53 #ifdef CONFIG_XDPMS
54 #include <X11/extensions/dpms.h>
55 #endif
57 #ifdef CONFIG_XINERAMA
58 #include <X11/extensions/Xinerama.h>
59 #endif
61 #ifdef CONFIG_XF86VM
62 #include <X11/extensions/xf86vmode.h>
63 #endif
65 #ifdef CONFIG_XF86XK
66 #include <X11/XF86keysym.h>
67 #endif
69 #ifdef CONFIG_XV
70 #include <X11/extensions/Xv.h>
71 #include <X11/extensions/Xvlib.h>
73 #include "subopt-helper.h"
74 #endif
76 #include "input/input.h"
77 #include "input/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(_WIN_PROTOCOLS);
331 XA_INIT(_WIN_LAYER);
332 XA_INIT(_WIN_HINTS);
333 XA_INIT(WM_PROTOCOLS);
334 XA_INIT(WM_DELETE_WINDOW);
337 void update_xinerama_info(struct vo *vo) {
338 struct MPOpts *opts = vo->opts;
339 xinerama_x = xinerama_y = 0;
340 #ifdef CONFIG_XINERAMA
341 if (xinerama_screen >= -1 && XineramaIsActive(vo->x11->display))
343 int screen = xinerama_screen;
344 XineramaScreenInfo *screens;
345 int num_screens;
347 screens = XineramaQueryScreens(vo->x11->display, &num_screens);
348 if (screen >= num_screens)
349 screen = num_screens - 1;
350 if (screen == -1) {
351 int x = vo->dx + vo->dwidth / 2;
352 int y = vo->dy + vo->dheight / 2;
353 for (screen = num_screens - 1; screen > 0; screen--) {
354 int left = screens[screen].x_org;
355 int right = left + screens[screen].width;
356 int top = screens[screen].y_org;
357 int bottom = top + screens[screen].height;
358 if (left <= x && x <= right && top <= y && y <= bottom)
359 break;
362 if (screen < 0)
363 screen = 0;
364 opts->vo_screenwidth = screens[screen].width;
365 opts->vo_screenheight = screens[screen].height;
366 xinerama_x = screens[screen].x_org;
367 xinerama_y = screens[screen].y_org;
369 XFree(screens);
371 #endif
372 aspect_save_screenres(vo, opts->vo_screenwidth, opts->vo_screenheight);
375 int vo_init(struct vo *vo)
377 struct MPOpts *opts = vo->opts;
378 struct vo_x11_state *x11 = vo->x11;
379 // int mScreen;
380 int depth, bpp;
381 unsigned int mask;
383 // char * DisplayName = ":0.0";
384 // Display * mDisplay;
385 XImage *mXImage = NULL;
387 // Window mRootWin;
388 XWindowAttributes attribs;
389 char *dispName;
391 if (vo_rootwin)
392 WinID = 0; // use root window
394 if (x11->depthonscreen)
396 saver_off(x11->display);
397 return 1; // already called
400 XSetErrorHandler(x11_errorhandler);
402 #if 0
403 if (!mDisplayName)
404 if (!(mDisplayName = getenv("DISPLAY")))
405 mDisplayName = strdup(":0.0");
406 #else
407 dispName = XDisplayName(mDisplayName);
408 #endif
410 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
412 x11->display = XOpenDisplay(dispName);
413 if (!x11->display)
415 mp_msg(MSGT_VO, MSGL_ERR,
416 "vo: couldn't open the X11 display (%s)!\n", dispName);
417 return 0;
419 x11->screen = DefaultScreen(x11->display); // screen ID
420 x11->rootwin = RootWindow(x11->display, x11->screen); // root window ID
422 init_atoms(vo->x11);
424 #ifdef CONFIG_XF86VM
426 int clock;
428 XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline);
429 if (!opts->vo_screenwidth)
430 opts->vo_screenwidth = modeline.hdisplay;
431 if (!opts->vo_screenheight)
432 opts->vo_screenheight = modeline.vdisplay;
434 #endif
436 if (!opts->vo_screenwidth)
437 opts->vo_screenwidth = DisplayWidth(x11->display, x11->screen);
438 if (!opts->vo_screenheight)
439 opts->vo_screenheight = DisplayHeight(x11->display, x11->screen);
441 // get color depth (from root window, or the best visual):
442 XGetWindowAttributes(x11->display, x11->rootwin, &attribs);
443 depth = attribs.depth;
445 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
447 Visual *visual;
449 depth = vo_find_depth_from_visuals(x11->display, x11->screen, &visual);
450 if (depth != -1)
451 mXImage = XCreateImage(x11->display, visual, depth, ZPixmap,
452 0, NULL, 1, 1, 8, 1);
453 } else
454 mXImage =
455 XGetImage(x11->display, x11->rootwin, 0, 0, 1, 1, AllPlanes, ZPixmap);
457 x11->depthonscreen = depth; // display depth on screen
459 // get bits/pixel from XImage structure:
460 if (mXImage == NULL)
462 mask = 0;
463 } else
466 * for the depth==24 case, the XImage structures might use
467 * 24 or 32 bits of data per pixel. The x11->depthonscreen
468 * field stores the amount of data per pixel in the
469 * XImage structure!
471 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
473 bpp = mXImage->bits_per_pixel;
474 if ((x11->depthonscreen + 7) / 8 != (bpp + 7) / 8)
475 x11->depthonscreen = bpp; // by A'rpi
476 mask =
477 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
478 mp_msg(MSGT_VO, MSGL_V,
479 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
480 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
481 XDestroyImage(mXImage);
483 if (((x11->depthonscreen + 7) / 8) == 2)
485 if (mask == 0x7FFF)
486 x11->depthonscreen = 15;
487 else if (mask == 0xFFFF)
488 x11->depthonscreen = 16;
490 // XCloseDisplay( mDisplay );
491 /* slightly improved local display detection AST */
492 if (strncmp(dispName, "unix:", 5) == 0)
493 dispName += 4;
494 else if (strncmp(dispName, "localhost:", 10) == 0)
495 dispName += 9;
496 if (*dispName == ':' && atoi(dispName + 1) < 10)
497 x11->display_is_local = 1;
498 else
499 x11->display_is_local = 0;
500 mp_msg(MSGT_VO, MSGL_V,
501 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
502 opts->vo_screenwidth, opts->vo_screenheight, depth, x11->depthonscreen,
503 dispName, x11->display_is_local ? "local" : "remote");
505 x11->wm_type = vo_wm_detect(vo);
507 x11->fs_type = vo_x11_get_fs_type(x11->wm_type);
509 fstype_dump(x11->fs_type);
511 saver_off(x11->display);
512 return 1;
515 void vo_uninit(struct vo_x11_state *x11)
517 if (!x11->display)
519 mp_msg(MSGT_VO, MSGL_V,
520 "vo: x11 uninit called but X11 not initialized..\n");
521 } else {
522 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
523 XSetErrorHandler(NULL);
524 XCloseDisplay(x11->display);
525 x11->depthonscreen = 0;
526 x11->display = NULL;
528 talloc_free(x11);
531 #include "wskeys.h"
533 #ifdef XF86XK_AudioPause
534 static const struct mp_keymap keysym_map[] = {
535 {XF86XK_MenuKB, KEY_MENU},
536 {XF86XK_AudioPlay, KEY_PLAY}, {XF86XK_AudioPause, KEY_PAUSE}, {XF86XK_AudioStop, KEY_STOP},
537 {XF86XK_AudioPrev, KEY_PREV}, {XF86XK_AudioNext, KEY_NEXT},
538 {XF86XK_AudioMute, KEY_MUTE}, {XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN}, {XF86XK_AudioRaiseVolume, KEY_VOLUME_UP},
539 {0, 0}
542 static void vo_x11_putkey_ext(struct vo *vo, int keysym, int modifiers)
544 struct mp_fifo *f = vo->key_fifo;
545 int mpkey = lookup_keymap_table(keysym_map, keysym);
546 if (mpkey)
547 mplayer_put_key(f, mpkey + modifiers);
549 #endif
551 static const struct mp_keymap keymap[] = {
552 // special keys
553 {wsPause, KEY_PAUSE}, {wsEscape, KEY_ESC}, {wsBackSpace, KEY_BS},
554 {wsTab, KEY_TAB}, {wsEnter, KEY_ENTER},
556 // cursor keys
557 {wsLeft, KEY_LEFT}, {wsRight, KEY_RIGHT}, {wsUp, KEY_UP}, {wsDown, KEY_DOWN},
559 // navigation block
560 {wsInsert, KEY_INSERT}, {wsDelete, KEY_DELETE}, {wsHome, KEY_HOME}, {wsEnd, KEY_END},
561 {wsPageUp, KEY_PAGE_UP}, {wsPageDown, KEY_PAGE_DOWN},
563 // F-keys
564 {wsF1, KEY_F+1}, {wsF2, KEY_F+2}, {wsF3, KEY_F+3}, {wsF4, KEY_F+4},
565 {wsF5, KEY_F+5}, {wsF6, KEY_F+6}, {wsF7, KEY_F+7}, {wsF8, KEY_F+8},
566 {wsF9, KEY_F+9}, {wsF10, KEY_F+10}, {wsF11, KEY_F+11}, {wsF12, KEY_F+12},
568 // numpad independent of numlock
569 {wsGrayMinus, '-'}, {wsGrayPlus, '+'}, {wsGrayMul, '*'}, {wsGrayDiv, '/'},
570 {wsGrayEnter, KEY_KPENTER},
572 // numpad with numlock
573 {wsGray0, KEY_KP0}, {wsGray1, KEY_KP1}, {wsGray2, KEY_KP2},
574 {wsGray3, KEY_KP3}, {wsGray4, KEY_KP4}, {wsGray5, KEY_KP5},
575 {wsGray6, KEY_KP6}, {wsGray7, KEY_KP7}, {wsGray8, KEY_KP8},
576 {wsGray9, KEY_KP9}, {wsGrayDecimal, KEY_KPDEC},
578 // numpad without numlock
579 {wsGrayInsert, KEY_KPINS}, {wsGrayEnd, KEY_KP1}, {wsGrayDown, KEY_KP2},
580 {wsGrayPgDn, KEY_KP3}, {wsGrayLeft, KEY_KP4}, {wsGray5Dup, KEY_KP5},
581 {wsGrayRight, KEY_KP6}, {wsGrayHome, KEY_KP7}, {wsGrayUp, KEY_KP8},
582 {wsGrayPgUp, KEY_KP9}, {wsGrayDelete, KEY_KPDEL},
584 {0, 0}
587 static void vo_x11_putkey(struct vo *vo, int key, int modifiers)
589 static const char *passthrough_keys = " -+*/<>`~!@#$%^&()_{}:;\"\',.?\\|=[]";
590 int mpkey = 0;
591 if ((key >= 'a' && key <= 'z') ||
592 (key >= 'A' && key <= 'Z') ||
593 (key >= '0' && key <= '9') ||
594 (key > 0 && key < 256 && strchr(passthrough_keys, key)))
595 mpkey = key;
597 if (!mpkey)
598 mpkey = lookup_keymap_table(keymap, key);
600 if (mpkey)
601 mplayer_put_key(vo->key_fifo, mpkey + modifiers);
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 = "MPlayer";
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 XUnmapWindow(x11->display, x11->window);
747 XSelectInput(x11->display, x11->window, StructureNotifyMask);
748 XDestroyWindow(x11->display, x11->window);
751 XNextEvent(x11->display, &xev);
753 while (xev.type != DestroyNotify
754 || xev.xdestroywindow.event != x11->window);
756 x11->window = None;
758 vo_fs = 0;
759 x11->vo_old_width = x11->vo_old_height = 0;
760 x11->last_video_width = 0;
761 x11->last_video_height = 0;
762 x11->size_changed_during_fs = false;
766 static int check_resize(struct vo *vo)
768 int old_w = vo->dwidth, old_h = vo->dheight;
769 int old_x = vo->dx, old_y = vo->dy;
770 int rc = 0;
771 vo_x11_update_geometry(vo, true);
772 if (vo->dwidth != old_w || vo->dheight != old_h)
773 rc |= VO_EVENT_RESIZE;
774 if (vo->dx != old_x || vo->dy != old_y)
775 rc |= VO_EVENT_MOVE;
776 return rc;
779 int vo_x11_check_events(struct vo *vo)
781 struct vo_x11_state *x11 = vo->x11;
782 Display *display = vo->x11->display;
783 int ret = 0;
784 XEvent Event;
785 char buf[100];
786 KeySym keySym;
788 if (x11->vo_mouse_autohide && x11->mouse_waiting_hide &&
789 (GetTimerMS() - x11->mouse_timer >= 1000)) {
790 vo_hidecursor(display, x11->window);
791 x11->mouse_waiting_hide = 0;
794 if (WinID > 0)
795 ret |= check_resize(vo);
796 while (XPending(display))
798 XNextEvent(display, &Event);
799 // printf("\rEvent.type=%X \n",Event.type);
800 switch (Event.type)
802 case Expose:
803 ret |= VO_EVENT_EXPOSE;
804 break;
805 case ConfigureNotify:
806 if (x11->window == None)
807 break;
808 ret |= check_resize(vo);
809 break;
810 case KeyPress:
812 int key;
814 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
815 &x11->compose_status);
816 int modifiers = 0;
817 if (Event.xkey.state & ShiftMask)
818 modifiers |= KEY_MODIFIER_SHIFT;
819 if (Event.xkey.state & ControlMask)
820 modifiers |= KEY_MODIFIER_CTRL;
821 if (Event.xkey.state & Mod1Mask)
822 modifiers |= KEY_MODIFIER_ALT;
823 if (Event.xkey.state & Mod4Mask)
824 modifiers |= KEY_MODIFIER_META;
825 #ifdef XF86XK_AudioPause
826 vo_x11_putkey_ext(vo, keySym, modifiers);
827 #endif
828 key =
829 ((keySym & 0xff00) !=
830 0 ? ((keySym & 0x00ff) + 256) : (keySym));
831 vo_x11_putkey(vo, key, modifiers);
832 ret |= VO_EVENT_KEYPRESS;
834 break;
835 case MotionNotify:
836 vo_mouse_movement(vo, Event.xmotion.x, Event.xmotion.y);
838 if (x11->vo_mouse_autohide)
840 vo_showcursor(display, x11->window);
841 x11->mouse_waiting_hide = 1;
842 x11->mouse_timer = GetTimerMS();
844 break;
845 case ButtonPress:
846 if (x11->vo_mouse_autohide)
848 vo_showcursor(display, x11->window);
849 x11->mouse_waiting_hide = 1;
850 x11->mouse_timer = GetTimerMS();
852 mplayer_put_key(vo->key_fifo,
853 (MOUSE_BTN0 + Event.xbutton.button - 1)
854 | MP_KEY_DOWN);
855 break;
856 case ButtonRelease:
857 if (x11->vo_mouse_autohide)
859 vo_showcursor(display, x11->window);
860 x11->mouse_waiting_hide = 1;
861 x11->mouse_timer = GetTimerMS();
863 mplayer_put_key(vo->key_fifo,
864 MOUSE_BTN0 + Event.xbutton.button - 1);
865 break;
866 case PropertyNotify:
868 char *name =
869 XGetAtomName(display, Event.xproperty.atom);
871 if (!name)
872 break;
874 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
876 XFree(name);
878 break;
879 case MapNotify:
880 x11->vo_hint.win_gravity = x11->old_gravity;
881 XSetWMNormalHints(display, x11->window, &x11->vo_hint);
882 x11->fs_flip = 0;
883 break;
884 case DestroyNotify:
885 mp_msg(MSGT_VO, MSGL_WARN, "Our window was destroyed, exiting\n");
886 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
887 break;
888 case ClientMessage:
889 if (Event.xclient.message_type == x11->XAWM_PROTOCOLS &&
890 Event.xclient.data.l[0] == x11->XAWM_DELETE_WINDOW)
891 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
892 break;
895 return ret;
899 * \brief sets the size and position of the non-fullscreen window.
901 static void vo_x11_nofs_sizepos(struct vo *vo, int x, int y,
902 int width, int height)
904 struct vo_x11_state *x11 = vo->x11;
905 if (width == x11->last_video_width && height == x11->last_video_height) {
906 if (!vo->opts->force_window_position && !x11->size_changed_during_fs)
907 return;
908 } else if (vo_fs)
909 x11->size_changed_during_fs = true;
910 x11->last_video_height = height;
911 x11->last_video_width = width;
912 vo_x11_sizehint(vo, x, y, width, height, 0);
913 if (vo_fs) {
914 x11->vo_old_x = x;
915 x11->vo_old_y = y;
916 x11->vo_old_width = width;
917 x11->vo_old_height = height;
919 else
921 vo->dwidth = width;
922 vo->dheight = height;
923 if (vo->opts->force_window_position)
924 XMoveResizeWindow(vo->x11->display, vo->x11->window, x, y, width,
925 height);
926 else
927 XResizeWindow(vo->x11->display, vo->x11->window, width, height);
931 void vo_x11_sizehint(struct vo *vo, int x, int y, int width, int height, int max)
933 struct vo_x11_state *x11 = vo->x11;
934 x11->vo_hint.flags = 0;
935 if (vo_keepaspect)
937 x11->vo_hint.flags |= PAspect;
938 x11->vo_hint.min_aspect.x = width;
939 x11->vo_hint.min_aspect.y = height;
940 x11->vo_hint.max_aspect.x = width;
941 x11->vo_hint.max_aspect.y = height;
944 x11->vo_hint.flags |= PPosition | PSize;
945 x11->vo_hint.x = x;
946 x11->vo_hint.y = y;
947 x11->vo_hint.width = width;
948 x11->vo_hint.height = height;
949 if (max)
951 x11->vo_hint.flags |= PMaxSize;
952 x11->vo_hint.max_width = width;
953 x11->vo_hint.max_height = height;
954 } else
956 x11->vo_hint.max_width = 0;
957 x11->vo_hint.max_height = 0;
960 // Set minimum height/width to 4 to avoid off-by-one errors
961 // and because mga_vid requires a minimal size of 4 pixels.
962 x11->vo_hint.flags |= PMinSize;
963 x11->vo_hint.min_width = x11->vo_hint.min_height = 4;
965 // Set the base size. A window manager might display the window
966 // size to the user relative to this.
967 // Setting these to width/height might be nice, but e.g. fluxbox can't handle it.
968 x11->vo_hint.flags |= PBaseSize;
969 x11->vo_hint.base_width = 0 /*width*/;
970 x11->vo_hint.base_height = 0 /*height*/;
972 x11->vo_hint.flags |= PWinGravity;
973 x11->vo_hint.win_gravity = StaticGravity;
974 XSetWMNormalHints(x11->display, x11->window, &x11->vo_hint);
977 static int vo_x11_get_gnome_layer(struct vo_x11_state *x11, Window win)
979 Atom type;
980 int format;
981 unsigned long nitems;
982 unsigned long bytesafter;
983 unsigned short *args = NULL;
985 if (XGetWindowProperty(x11->display, win, x11->XA_WIN_LAYER, 0, 16384,
986 False, AnyPropertyType, &type, &format, &nitems,
987 &bytesafter,
988 (unsigned char **) &args) == Success
989 && nitems > 0 && args)
991 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
992 *args);
993 return *args;
995 return WIN_LAYER_NORMAL;
999 static Window vo_x11_create_smooth_window(struct vo_x11_state *x11, Window mRoot,
1000 Visual * vis, int x, int y,
1001 unsigned int width, unsigned int height,
1002 int depth, Colormap col_map)
1004 unsigned long xswamask = CWBorderPixel;
1005 XSetWindowAttributes xswa;
1006 Window ret_win;
1008 if (col_map != CopyFromParent)
1010 xswa.colormap = col_map;
1011 xswamask |= CWColormap;
1013 xswa.background_pixel = 0;
1014 xswa.border_pixel = 0;
1015 xswa.backing_store = NotUseful;
1016 xswa.bit_gravity = StaticGravity;
1018 ret_win =
1019 XCreateWindow(x11->display, x11->rootwin, x, y, width, height, 0, depth,
1020 CopyFromParent, vis, xswamask, &xswa);
1021 XSetWMProtocols(x11->display, ret_win, &x11->XAWM_DELETE_WINDOW, 1);
1022 if (x11->f_gc == None)
1023 x11->f_gc = XCreateGC(x11->display, ret_win, 0, 0);
1024 XSetForeground(x11->display, x11->f_gc, 0);
1026 return ret_win;
1030 * \brief create and setup a window suitable for display
1031 * \param vis Visual to use for creating the window
1032 * \param x x position of window
1033 * \param y y position of window
1034 * \param width width of window
1035 * \param height height of window
1036 * \param flags flags for window creation.
1037 * Only VOFLAG_FULLSCREEN is supported so far.
1038 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1039 * \param classname name to use for the classhint
1040 * \param title title for the window
1042 * This also does the grunt-work like setting Window Manager hints etc.
1043 * If vo_window is already set it just moves and resizes it.
1045 void vo_x11_create_vo_window(struct vo *vo, XVisualInfo *vis, int x, int y,
1046 unsigned int width, unsigned int height, int flags,
1047 Colormap col_map,
1048 const char *classname, const char *title)
1050 struct MPOpts *opts = vo->opts;
1051 struct vo_x11_state *x11 = vo->x11;
1052 Display *mDisplay = vo->x11->display;
1053 if (WinID >= 0) {
1054 vo_fs = flags & VOFLAG_FULLSCREEN;
1055 x11->window = WinID ? (Window)WinID : x11->rootwin;
1056 if (col_map != CopyFromParent) {
1057 unsigned long xswamask = CWColormap;
1058 XSetWindowAttributes xswa;
1059 xswa.colormap = col_map;
1060 XChangeWindowAttributes(mDisplay, x11->window, xswamask, &xswa);
1061 XInstallColormap(mDisplay, col_map);
1063 if (WinID) {
1064 // Expose events can only really be handled by us, so request them.
1065 vo_x11_selectinput_witherr(mDisplay, x11->window, ExposureMask);
1066 } else
1067 // Do not capture events since it might break the parent application
1068 // if it relies on events being forwarded to the parent of WinID.
1069 // It also is consistent with the w32_common.c code.
1070 vo_x11_selectinput_witherr(mDisplay, x11->window,
1071 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1072 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1074 vo_x11_update_geometry(vo, true);
1075 goto final;
1077 if (x11->window == None) {
1078 vo_fs = 0;
1079 vo->dwidth = width;
1080 vo->dheight = height;
1081 x11->window = vo_x11_create_smooth_window(x11, x11->rootwin, vis->visual,
1082 x, y, width, height, vis->depth, col_map);
1083 x11->window_state = VOFLAG_HIDDEN;
1085 if (flags & VOFLAG_HIDDEN)
1086 goto final;
1087 if (x11->window_state & VOFLAG_HIDDEN) {
1088 XSizeHints hint;
1089 x11->window_state &= ~VOFLAG_HIDDEN;
1090 vo_x11_classhint(vo, x11->window, classname);
1091 XStoreName(mDisplay, x11->window, title);
1092 vo_hidecursor(mDisplay, x11->window);
1093 XSelectInput(mDisplay, x11->window, StructureNotifyMask);
1094 hint.x = x; hint.y = y;
1095 hint.width = width; hint.height = height;
1096 hint.flags = PSize;
1097 if (geometry_xy_changed)
1098 hint.flags |= PPosition;
1099 XSetStandardProperties(mDisplay, x11->window, title, title, None, NULL, 0, &hint);
1100 if (!vo_border) vo_x11_decoration(vo, 0);
1101 // map window
1102 XMapWindow(mDisplay, x11->window);
1103 vo_x11_clearwindow(vo, x11->window);
1104 XSelectInput(mDisplay, x11->window, NoEventMask);
1105 XSync(mDisplay, False);
1106 vo_x11_selectinput_witherr(mDisplay, x11->window,
1107 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1108 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1110 if (opts->vo_ontop) vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1111 vo_x11_update_geometry(vo, !geometry_xy_changed);
1112 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, width, height);
1113 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1114 vo_x11_fullscreen(vo);
1115 else if (vo_fs) {
1116 // if we are already in fullscreen do not switch back and forth, just
1117 // set the size values right.
1118 vo->dwidth = vo->opts->vo_screenwidth;
1119 vo->dheight = vo->opts->vo_screenheight;
1121 final:
1122 if (x11->vo_gc != None)
1123 XFreeGC(mDisplay, x11->vo_gc);
1124 x11->vo_gc = XCreateGC(mDisplay, x11->window, 0, NULL);
1126 XSync(mDisplay, False);
1127 x11->vo_mouse_autohide = 1;
1128 vo->event_fd = ConnectionNumber(x11->display);
1131 void vo_x11_clearwindow_part(struct vo *vo, Window vo_window,
1132 int img_width, int img_height, int use_fs)
1134 struct vo_x11_state *x11 = vo->x11;
1135 struct MPOpts *opts = vo->opts;
1136 Display *mDisplay = vo->x11->display;
1137 int u_dheight, u_dwidth, left_ov, left_ov2;
1139 if (x11->f_gc == None)
1140 return;
1142 u_dheight = use_fs ? opts->vo_screenheight : vo->dheight;
1143 u_dwidth = use_fs ? opts->vo_screenwidth : vo->dwidth;
1144 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1145 return;
1147 left_ov = (u_dheight - img_height) / 2;
1148 left_ov2 = (u_dwidth - img_width) / 2;
1150 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, 0, u_dwidth, left_ov);
1151 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, u_dheight - left_ov - 1,
1152 u_dwidth, left_ov + 1);
1154 if (u_dwidth > img_width)
1156 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, left_ov, left_ov2,
1157 img_height);
1158 XFillRectangle(mDisplay, vo_window, x11->f_gc, u_dwidth - left_ov2 - 1,
1159 left_ov, left_ov2 + 1, img_height);
1162 XFlush(mDisplay);
1165 void vo_x11_clearwindow(struct vo *vo, Window vo_window)
1167 struct vo_x11_state *x11 = vo->x11;
1168 struct MPOpts *opts = vo->opts;
1169 if (x11->f_gc == None)
1170 return;
1171 XFillRectangle(x11->display, vo_window, x11->f_gc, 0, 0,
1172 opts->vo_screenwidth, opts->vo_screenheight);
1174 XFlush(x11->display);
1178 void vo_x11_setlayer(struct vo *vo, Window vo_window, int layer)
1180 struct vo_x11_state *x11 = vo->x11;
1181 if (WinID >= 0)
1182 return;
1184 if (x11->fs_type & vo_wm_LAYER)
1186 XClientMessageEvent xev;
1188 if (!x11->orig_layer)
1189 x11->orig_layer = vo_x11_get_gnome_layer(x11, vo_window);
1191 memset(&xev, 0, sizeof(xev));
1192 xev.type = ClientMessage;
1193 xev.display = x11->display;
1194 xev.window = vo_window;
1195 xev.message_type = x11->XA_WIN_LAYER;
1196 xev.format = 32;
1197 xev.data.l[0] = layer ? fs_layer : x11->orig_layer; // if not fullscreen, stay on default layer
1198 xev.data.l[1] = CurrentTime;
1199 mp_msg(MSGT_VO, MSGL_V,
1200 "[x11] Layered style stay on top (layer %ld).\n",
1201 xev.data.l[0]);
1202 XSendEvent(x11->display, x11->rootwin, False, SubstructureNotifyMask,
1203 (XEvent *) & xev);
1204 } else if (x11->fs_type & vo_wm_NETWM)
1206 XClientMessageEvent xev;
1207 char *state;
1209 memset(&xev, 0, sizeof(xev));
1210 xev.type = ClientMessage;
1211 xev.message_type = x11->XA_NET_WM_STATE;
1212 xev.display = x11->display;
1213 xev.window = vo_window;
1214 xev.format = 32;
1215 xev.data.l[0] = layer;
1217 if (x11->fs_type & vo_wm_STAYS_ON_TOP)
1218 xev.data.l[1] = x11->XA_NET_WM_STATE_STAYS_ON_TOP;
1219 else if (x11->fs_type & vo_wm_ABOVE)
1220 xev.data.l[1] = x11->XA_NET_WM_STATE_ABOVE;
1221 else if (x11->fs_type & vo_wm_FULLSCREEN)
1222 xev.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
1223 else if (x11->fs_type & vo_wm_BELOW)
1224 // This is not fallback. We can safely assume that the situation
1225 // where only NETWM_STATE_BELOW is supported doesn't exist.
1226 xev.data.l[1] = x11->XA_NET_WM_STATE_BELOW;
1228 XSendEvent(x11->display, x11->rootwin, False, SubstructureRedirectMask,
1229 (XEvent *) & xev);
1230 state = XGetAtomName(x11->display, xev.data.l[1]);
1231 mp_msg(MSGT_VO, MSGL_V,
1232 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1233 layer, state);
1234 XFree(state);
1238 static int vo_x11_get_fs_type(int supported)
1240 int i;
1241 int type = supported;
1243 if (vo_fstype_list)
1245 for (i = 0; vo_fstype_list[i]; i++)
1247 int neg = 0;
1248 char *arg = vo_fstype_list[i];
1250 if (vo_fstype_list[i][0] == '-')
1252 neg = 1;
1253 arg = vo_fstype_list[i] + 1;
1256 if (!strncmp(arg, "layer", 5))
1258 if (!neg && (arg[5] == '='))
1260 char *endptr = NULL;
1261 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1263 if (endptr && *endptr == '\0' && layer >= 0
1264 && layer <= 15)
1265 fs_layer = layer;
1267 if (neg)
1268 type &= ~vo_wm_LAYER;
1269 else
1270 type |= vo_wm_LAYER;
1271 } else if (!strcmp(arg, "above"))
1273 if (neg)
1274 type &= ~vo_wm_ABOVE;
1275 else
1276 type |= vo_wm_ABOVE;
1277 } else if (!strcmp(arg, "fullscreen"))
1279 if (neg)
1280 type &= ~vo_wm_FULLSCREEN;
1281 else
1282 type |= vo_wm_FULLSCREEN;
1283 } else if (!strcmp(arg, "stays_on_top"))
1285 if (neg)
1286 type &= ~vo_wm_STAYS_ON_TOP;
1287 else
1288 type |= vo_wm_STAYS_ON_TOP;
1289 } else if (!strcmp(arg, "below"))
1291 if (neg)
1292 type &= ~vo_wm_BELOW;
1293 else
1294 type |= vo_wm_BELOW;
1295 } else if (!strcmp(arg, "netwm"))
1297 if (neg)
1298 type &= ~vo_wm_NETWM;
1299 else
1300 type |= vo_wm_NETWM;
1301 } else if (!strcmp(arg, "none"))
1302 type = 0; // clear; keep parsing
1306 return type;
1310 * \brief update vo->dx, vo->dy, vo->dwidth and vo->dheight with current values of vo->x11->window
1311 * \return returns current color depth of vo->x11->window
1313 int vo_x11_update_geometry(struct vo *vo, bool update_pos)
1315 struct MPOpts *opts = vo->opts;
1316 struct vo_x11_state *x11 = vo->x11;
1317 unsigned depth, w, h;
1318 int dummy_int;
1319 Window dummy_win;
1320 XGetGeometry(x11->display, x11->window, &dummy_win, &dummy_int, &dummy_int,
1321 &w, &h, &dummy_int, &depth);
1322 if (w <= INT_MAX && h <= INT_MAX) {
1323 vo->dwidth = w;
1324 vo->dheight = h;
1326 if (update_pos)
1327 XTranslateCoordinates(x11->display, x11->window, x11->rootwin, 0, 0,
1328 &vo->dx, &vo->dy, &dummy_win);
1329 if (opts->vo_wintitle)
1330 XStoreName(x11->display, x11->window, opts->vo_wintitle);
1332 return depth <= INT_MAX ? depth : 0;
1335 void vo_x11_fullscreen(struct vo *vo)
1337 struct MPOpts *opts = vo->opts;
1338 struct vo_x11_state *x11 = vo->x11;
1339 int x, y, w, h;
1340 x = x11->vo_old_x;
1341 y = x11->vo_old_y;
1342 w = x11->vo_old_width;
1343 h = x11->vo_old_height;
1345 if (WinID >= 0) {
1346 vo_fs = !vo_fs;
1347 return;
1349 if (x11->fs_flip)
1350 return;
1352 if (vo_fs)
1354 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1355 vo_fs = VO_FALSE;
1356 if (x11->size_changed_during_fs && (x11->fs_type & vo_wm_FULLSCREEN))
1357 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, x11->last_video_width,
1358 x11->last_video_height);
1359 x11->size_changed_during_fs = false;
1360 } else
1362 // win->fs
1363 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1365 vo_fs = VO_TRUE;
1366 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1368 x11->vo_old_x = vo->dx;
1369 x11->vo_old_y = vo->dy;
1370 x11->vo_old_width = vo->dwidth;
1371 x11->vo_old_height = vo->dheight;
1373 update_xinerama_info(vo);
1374 x = xinerama_x;
1375 y = xinerama_y;
1376 w = opts->vo_screenwidth;
1377 h = opts->vo_screenheight;
1380 long dummy;
1382 XGetWMNormalHints(x11->display, x11->window, &x11->vo_hint, &dummy);
1383 if (!(x11->vo_hint.flags & PWinGravity))
1384 x11->old_gravity = NorthWestGravity;
1385 else
1386 x11->old_gravity = x11->vo_hint.win_gravity;
1388 if (x11->wm_type == 0 && !(vo_fsmode & 16))
1390 XUnmapWindow(x11->display, x11->window); // required for MWM
1391 XWithdrawWindow(x11->display, x11->window, x11->screen);
1392 x11->fs_flip = 1;
1395 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1397 vo_x11_decoration(vo, vo_border && !vo_fs);
1398 vo_x11_sizehint(vo, x, y, w, h, 0);
1399 vo_x11_setlayer(vo, x11->window, vo_fs);
1402 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1404 /* some WMs lose ontop after fullscreen */
1405 if ((!(vo_fs)) & opts->vo_ontop)
1406 vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1408 XMapRaised(x11->display, x11->window);
1409 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1410 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1411 XRaiseWindow(x11->display, x11->window);
1412 XFlush(x11->display);
1415 void vo_x11_ontop(struct vo *vo)
1417 struct MPOpts *opts = vo->opts;
1418 opts->vo_ontop = !opts->vo_ontop;
1420 vo_x11_setlayer(vo, vo->x11->window, opts->vo_ontop);
1423 void vo_x11_border(struct vo *vo)
1425 vo_border = !vo_border;
1426 vo_x11_decoration(vo, vo_border && !vo_fs);
1430 * XScreensaver stuff
1433 static int screensaver_off;
1434 static unsigned int time_last;
1436 void xscreensaver_heartbeat(struct vo_x11_state *x11)
1438 unsigned int time = GetTimerMS();
1440 if (x11->display && screensaver_off && (time - time_last) > 30000)
1442 time_last = time;
1444 XResetScreenSaver(x11->display);
1448 static int xss_suspend(Display *mDisplay, Bool suspend)
1450 #ifndef CONFIG_XSS
1451 return 0;
1452 #else
1453 int event, error, major, minor;
1454 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1455 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1456 return 0;
1457 if (major < 1 || (major == 1 && minor < 1))
1458 return 0;
1459 XScreenSaverSuspend(mDisplay, suspend);
1460 return 1;
1461 #endif
1465 * End of XScreensaver stuff
1468 static void saver_on(Display * mDisplay)
1471 if (!screensaver_off)
1472 return;
1473 screensaver_off = 0;
1474 if (xss_suspend(mDisplay, False))
1475 return;
1476 #ifdef CONFIG_XDPMS
1477 if (dpms_disabled)
1479 int nothing;
1480 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1482 if (!DPMSEnable(mDisplay))
1483 { // restoring power saving settings
1484 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1485 } else
1487 // DPMS does not seem to be enabled unless we call DPMSInfo
1488 BOOL onoff;
1489 CARD16 state;
1491 DPMSForceLevel(mDisplay, DPMSModeOn);
1492 DPMSInfo(mDisplay, &state, &onoff);
1493 if (onoff)
1495 mp_msg(MSGT_VO, MSGL_V,
1496 "Successfully enabled DPMS\n");
1497 } else
1499 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1503 dpms_disabled = 0;
1505 #endif
1508 static void saver_off(Display * mDisplay)
1510 int nothing;
1512 if (screensaver_off)
1513 return;
1514 screensaver_off = 1;
1515 if (xss_suspend(mDisplay, True))
1516 return;
1517 #ifdef CONFIG_XDPMS
1518 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1520 BOOL onoff;
1521 CARD16 state;
1523 DPMSInfo(mDisplay, &state, &onoff);
1524 if (onoff)
1526 Status stat;
1528 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1529 dpms_disabled = 1;
1530 stat = DPMSDisable(mDisplay); // monitor powersave off
1531 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1534 #endif
1537 static XErrorHandler old_handler = NULL;
1538 static int selectinput_err = 0;
1539 static int x11_selectinput_errorhandler(Display * display,
1540 XErrorEvent * event)
1542 if (event->error_code == BadAccess)
1544 selectinput_err = 1;
1545 mp_msg(MSGT_VO, MSGL_ERR,
1546 "X11 error: BadAccess during XSelectInput Call\n");
1547 mp_msg(MSGT_VO, MSGL_ERR,
1548 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1549 /* If you think MPlayer should shutdown with this error,
1550 * comment out the following line */
1551 return 0;
1553 if (old_handler != NULL)
1554 old_handler(display, event);
1555 else
1556 x11_errorhandler(display, event);
1557 return 0;
1560 void vo_x11_selectinput_witherr(Display * display, Window w,
1561 long event_mask)
1563 XSync(display, False);
1564 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1565 selectinput_err = 0;
1566 if (vo_nomouse_input)
1568 XSelectInput(display, w,
1569 event_mask &
1570 (~(ButtonPressMask | ButtonReleaseMask)));
1571 } else
1573 XSelectInput(display, w, event_mask);
1575 XSync(display, False);
1576 XSetErrorHandler(old_handler);
1577 if (selectinput_err)
1579 mp_msg(MSGT_VO, MSGL_ERR,
1580 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1581 XSelectInput(display, w,
1582 event_mask &
1584 (ButtonPressMask | ButtonReleaseMask |
1585 PointerMotionMask)));
1589 #ifdef CONFIG_XF86VM
1590 void vo_vm_switch(struct vo *vo)
1592 struct vo_x11_state *x11 = vo->x11;
1593 struct MPOpts *opts = vo->opts;
1594 Display *mDisplay = x11->display;
1595 int vm_event, vm_error;
1596 int vm_ver, vm_rev;
1597 int i, j, have_vm = 0;
1598 int X = vo->dwidth, Y = vo->dheight;
1599 int modeline_width, modeline_height;
1601 int modecount;
1603 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1605 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1606 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1607 vm_rev);
1608 have_vm = 1;
1609 } else {
1610 mp_msg(MSGT_VO, MSGL_WARN,
1611 "XF86VidMode extension not available.\n");
1614 if (have_vm)
1616 if (vidmodes == NULL)
1617 XF86VidModeGetAllModeLines(mDisplay, x11->screen, &modecount,
1618 &vidmodes);
1619 j = 0;
1620 modeline_width = vidmodes[0]->hdisplay;
1621 modeline_height = vidmodes[0]->vdisplay;
1623 for (i = 1; i < modecount; i++)
1624 if ((vidmodes[i]->hdisplay >= X)
1625 && (vidmodes[i]->vdisplay >= Y))
1626 if ((vidmodes[i]->hdisplay <= modeline_width)
1627 && (vidmodes[i]->vdisplay <= modeline_height))
1629 modeline_width = vidmodes[i]->hdisplay;
1630 modeline_height = vidmodes[i]->vdisplay;
1631 j = i;
1634 mp_tmsg(MSGT_VO, MSGL_INFO, "XF86VM: Selected video mode %dx%d for image size %dx%d.\n",
1635 modeline_width, modeline_height, X, Y);
1636 XF86VidModeLockModeSwitch(mDisplay, x11->screen, 0);
1637 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1638 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1640 // FIXME: all this is more of a hack than proper solution
1641 X = (opts->vo_screenwidth - modeline_width) / 2;
1642 Y = (opts->vo_screenheight - modeline_height) / 2;
1643 XF86VidModeSetViewPort(mDisplay, x11->screen, X, Y);
1644 vo->dx = X;
1645 vo->dy = Y;
1646 vo->dwidth = modeline_width;
1647 vo->dheight = modeline_height;
1648 aspect_save_screenres(vo, modeline_width, modeline_height);
1652 void vo_vm_close(struct vo *vo)
1654 Display *dpy = vo->x11->display;
1655 struct MPOpts *opts = vo->opts;
1656 if (vidmodes != NULL)
1658 int i, modecount;
1660 free(vidmodes);
1661 vidmodes = NULL;
1662 XF86VidModeGetAllModeLines(dpy, vo->x11->screen, &modecount,
1663 &vidmodes);
1664 for (i = 0; i < modecount; i++)
1665 if ((vidmodes[i]->hdisplay == opts->vo_screenwidth)
1666 && (vidmodes[i]->vdisplay == opts->vo_screenheight))
1668 mp_msg(MSGT_VO, MSGL_INFO,
1669 "Returning to original mode %dx%d\n",
1670 opts->vo_screenwidth, opts->vo_screenheight);
1671 break;
1674 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1675 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1676 free(vidmodes);
1677 vidmodes = NULL;
1681 double vo_vm_get_fps(struct vo *vo)
1683 struct vo_x11_state *x11 = vo->x11;
1684 int clock;
1685 XF86VidModeModeLine modeline;
1686 if (!XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline))
1687 return 0;
1688 if (modeline.privsize)
1689 XFree(modeline.private);
1690 return 1e3 * clock / modeline.htotal / modeline.vtotal;
1692 #endif
1694 #endif /* X11_FULLSCREEN */
1698 * Scan the available visuals on this Display/Screen. Try to find
1699 * the 'best' available TrueColor visual that has a decent color
1700 * depth (at least 15bit). If there are multiple visuals with depth
1701 * >= 15bit, we prefer visuals with a smaller color depth.
1703 int vo_find_depth_from_visuals(Display * dpy, int screen,
1704 Visual ** visual_return)
1706 XVisualInfo visual_tmpl;
1707 XVisualInfo *visuals;
1708 int nvisuals, i;
1709 int bestvisual = -1;
1710 int bestvisual_depth = -1;
1712 visual_tmpl.screen = screen;
1713 visual_tmpl.class = TrueColor;
1714 visuals = XGetVisualInfo(dpy,
1715 VisualScreenMask | VisualClassMask,
1716 &visual_tmpl, &nvisuals);
1717 if (visuals != NULL)
1719 for (i = 0; i < nvisuals; i++)
1721 mp_msg(MSGT_VO, MSGL_V,
1722 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1723 visuals[i].visualid, visuals[i].depth,
1724 visuals[i].red_mask, visuals[i].green_mask,
1725 visuals[i].blue_mask);
1727 * Save the visual index and its depth, if this is the first
1728 * truecolor visul, or a visual that is 'preferred' over the
1729 * previous 'best' visual.
1731 if (bestvisual_depth == -1
1732 || (visuals[i].depth >= 15
1733 && (visuals[i].depth < bestvisual_depth
1734 || bestvisual_depth < 15)))
1736 bestvisual = i;
1737 bestvisual_depth = visuals[i].depth;
1741 if (bestvisual != -1 && visual_return != NULL)
1742 *visual_return = visuals[bestvisual].visual;
1744 XFree(visuals);
1746 return bestvisual_depth;
1750 static Colormap cmap = None;
1751 static XColor cols[256];
1752 static int cm_size, red_mask, green_mask, blue_mask;
1755 Colormap vo_x11_create_colormap(struct vo *vo, XVisualInfo *vinfo)
1757 struct vo_x11_state *x11 = vo->x11;
1758 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1760 if (vinfo->class != DirectColor)
1761 return XCreateColormap(x11->display, x11->rootwin, vinfo->visual,
1762 AllocNone);
1764 /* can this function get called twice or more? */
1765 if (cmap)
1766 return cmap;
1767 cm_size = vinfo->colormap_size;
1768 red_mask = vinfo->red_mask;
1769 green_mask = vinfo->green_mask;
1770 blue_mask = vinfo->blue_mask;
1771 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1772 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1773 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1774 rvu = 65536ull * ru / (red_mask + ru);
1775 gvu = 65536ull * gu / (green_mask + gu);
1776 bvu = 65536ull * bu / (blue_mask + bu);
1777 r = g = b = 0;
1778 rv = gv = bv = 0;
1779 m = DoRed | DoGreen | DoBlue;
1780 for (k = 0; k < cm_size; k++)
1782 int t;
1784 cols[k].pixel = r | g | b;
1785 cols[k].red = rv;
1786 cols[k].green = gv;
1787 cols[k].blue = bv;
1788 cols[k].flags = m;
1789 t = (r + ru) & red_mask;
1790 if (t < r)
1791 m &= ~DoRed;
1792 r = t;
1793 t = (g + gu) & green_mask;
1794 if (t < g)
1795 m &= ~DoGreen;
1796 g = t;
1797 t = (b + bu) & blue_mask;
1798 if (t < b)
1799 m &= ~DoBlue;
1800 b = t;
1801 rv += rvu;
1802 gv += gvu;
1803 bv += bvu;
1805 cmap = XCreateColormap(x11->display, x11->rootwin, vinfo->visual, AllocAll);
1806 XStoreColors(x11->display, cmap, cols, cm_size);
1807 return cmap;
1811 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1812 * hue and red/green/blue intensity, but we cannot do saturation.
1813 * Currently only gamma, brightness and contrast are implemented.
1814 * Is there sufficient interest for hue and/or red/green/blue intensity?
1816 /* these values have range [-100,100] and are initially 0 */
1817 static int vo_gamma = 0;
1818 static int vo_brightness = 0;
1819 static int vo_contrast = 0;
1821 static int transform_color(float val,
1822 float brightness, float contrast, float gamma) {
1823 float s = pow(val, gamma);
1824 s = (s - 0.5) * contrast + 0.5;
1825 s += brightness;
1826 if (s < 0)
1827 s = 0;
1828 if (s > 1)
1829 s = 1;
1830 return (unsigned short) (s * 65535);
1833 uint32_t vo_x11_set_equalizer(struct vo *vo, const char *name, int value)
1835 float gamma, brightness, contrast;
1836 float rf, gf, bf;
1837 int k;
1840 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1841 * of TrueColor-ed window but be careful:
1842 * Unlike the colormaps, which are private for the X client
1843 * who created them and thus automatically destroyed on client
1844 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1845 * setting and _must_ be restored before the process exits.
1846 * Unforunately when the process crashes (or gets killed
1847 * for some reason) it is impossible to restore the setting,
1848 * and such behaviour could be rather annoying for the users.
1850 if (cmap == None)
1851 return VO_NOTAVAIL;
1853 if (!strcasecmp(name, "brightness"))
1854 vo_brightness = value;
1855 else if (!strcasecmp(name, "contrast"))
1856 vo_contrast = value;
1857 else if (!strcasecmp(name, "gamma"))
1858 vo_gamma = value;
1859 else
1860 return VO_NOTIMPL;
1862 brightness = 0.01 * vo_brightness;
1863 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1864 gamma = pow(2, -0.02 * vo_gamma);
1866 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1867 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
1868 green_mask;
1869 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
1871 /* now recalculate the colormap using the newly set value */
1872 for (k = 0; k < cm_size; k++)
1874 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
1875 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
1876 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
1879 XStoreColors(vo->x11->display, cmap, cols, cm_size);
1880 XFlush(vo->x11->display);
1881 return VO_TRUE;
1884 uint32_t vo_x11_get_equalizer(const char *name, int *value)
1886 if (cmap == None)
1887 return VO_NOTAVAIL;
1888 if (!strcasecmp(name, "brightness"))
1889 *value = vo_brightness;
1890 else if (!strcasecmp(name, "contrast"))
1891 *value = vo_contrast;
1892 else if (!strcasecmp(name, "gamma"))
1893 *value = vo_gamma;
1894 else
1895 return VO_NOTIMPL;
1896 return VO_TRUE;
1899 #ifdef CONFIG_XV
1900 int vo_xv_set_eq(struct vo *vo, uint32_t xv_port, const char *name, int value)
1902 XvAttribute *attributes;
1903 int i, howmany, xv_atom;
1905 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
1907 /* get available attributes */
1908 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1909 for (i = 0; i < howmany && attributes; i++)
1910 if (attributes[i].flags & XvSettable)
1912 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1913 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1914 then trigger it if it's ok so that the other values are at default upon query */
1915 if (xv_atom != None)
1917 int hue = 0, port_value, port_min, port_max;
1919 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1920 (!strcasecmp(name, "brightness")))
1921 port_value = value;
1922 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1923 (!strcasecmp(name, "contrast")))
1924 port_value = value;
1925 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1926 (!strcasecmp(name, "saturation")))
1927 port_value = value;
1928 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1929 (!strcasecmp(name, "hue")))
1931 port_value = value;
1932 hue = 1;
1933 } else
1934 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1935 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1936 (!strcasecmp(name, "red_intensity")))
1937 port_value = value;
1938 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1939 && (!strcasecmp(name, "green_intensity")))
1940 port_value = value;
1941 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1942 && (!strcasecmp(name, "blue_intensity")))
1943 port_value = value;
1944 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
1945 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
1946 && (!strcasecmp(name, "bt_709")))
1947 port_value = value;
1948 else
1949 continue;
1951 port_min = attributes[i].min_value;
1952 port_max = attributes[i].max_value;
1954 /* nvidia hue workaround */
1955 if (hue && port_min == 0 && port_max == 360)
1957 port_value =
1958 (port_value >=
1959 0) ? (port_value - 100) : (port_value + 100);
1961 // -100 -> min
1962 // 0 -> (max+min)/2
1963 // +100 -> max
1964 port_value =
1965 (port_value + 100) * (port_max - port_min) / 200 +
1966 port_min;
1967 XvSetPortAttribute(vo->x11->display, xv_port, xv_atom, port_value);
1968 return VO_TRUE;
1971 return VO_FALSE;
1974 int vo_xv_get_eq(struct vo *vo, uint32_t xv_port, const char *name, int *value)
1977 XvAttribute *attributes;
1978 int i, howmany, xv_atom;
1980 /* get available attributes */
1981 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1982 for (i = 0; i < howmany && attributes; i++)
1983 if (attributes[i].flags & XvGettable)
1985 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1986 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1987 then trigger it if it's ok so that the other values are at default upon query */
1988 if (xv_atom != None)
1990 int val, port_value = 0, port_min, port_max;
1992 XvGetPortAttribute(vo->x11->display, xv_port, xv_atom,
1993 &port_value);
1995 port_min = attributes[i].min_value;
1996 port_max = attributes[i].max_value;
1997 val =
1998 (port_value - port_min) * 200 / (port_max - port_min) -
1999 100;
2001 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2002 (!strcasecmp(name, "brightness")))
2003 *value = val;
2004 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2005 (!strcasecmp(name, "contrast")))
2006 *value = val;
2007 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2008 (!strcasecmp(name, "saturation")))
2009 *value = val;
2010 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2011 (!strcasecmp(name, "hue")))
2013 /* nasty nvidia detect */
2014 if (port_min == 0 && port_max == 360)
2015 *value = (val >= 0) ? (val - 100) : (val + 100);
2016 else
2017 *value = val;
2018 } else
2019 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2020 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2021 (!strcasecmp(name, "red_intensity")))
2022 *value = val;
2023 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2024 && (!strcasecmp(name, "green_intensity")))
2025 *value = val;
2026 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2027 && (!strcasecmp(name, "blue_intensity")))
2028 *value = val;
2029 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
2030 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
2031 && (!strcasecmp(name, "bt_709")))
2032 *value = val;
2033 else
2034 continue;
2036 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2037 name, *value);
2038 return VO_TRUE;
2041 return VO_FALSE;
2045 * \brief Interns the requested atom if it is available.
2047 * \param atom_name String containing the name of the requested atom.
2049 * \return Returns the atom if available, else None is returned.
2052 static Atom xv_intern_atom_if_exists(struct vo_x11_state *x11,
2053 char const *atom_name)
2055 XvAttribute * attributes;
2056 int attrib_count,i;
2057 Atom xv_atom = None;
2059 attributes = XvQueryPortAttributes(x11->display, x11->xv_port, &attrib_count );
2060 if( attributes!=NULL )
2062 for ( i = 0; i < attrib_count; ++i )
2064 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2066 xv_atom = XInternAtom(x11->display, atom_name, False );
2067 break; // found what we want, break out
2070 XFree( attributes );
2073 return xv_atom;
2077 * \brief Try to enable vsync for xv.
2078 * \return Returns -1 if not available, 0 on failure and 1 on success.
2080 int vo_xv_enable_vsync(struct vo *vo)
2082 struct vo_x11_state *x11 = vo->x11;
2083 Atom xv_atom = xv_intern_atom_if_exists(x11, "XV_SYNC_TO_VBLANK");
2084 if (xv_atom == None)
2085 return -1;
2086 return XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1) == Success;
2090 * \brief Get maximum supported source image dimensions.
2092 * This function does not set the variables pointed to by
2093 * width and height if the information could not be retrieved,
2094 * so the caller is reponsible for properly initializing them.
2096 * \param width [out] The maximum width gets stored here.
2097 * \param height [out] The maximum height gets stored here.
2100 void vo_xv_get_max_img_dim(struct vo *vo, uint32_t * width, uint32_t * height)
2102 struct vo_x11_state *x11 = vo->x11;
2103 XvEncodingInfo * encodings;
2104 //unsigned long num_encodings, idx; to int or too long?!
2105 unsigned int num_encodings, idx;
2107 XvQueryEncodings(x11->display, x11->xv_port, &num_encodings, &encodings);
2109 if ( encodings )
2111 for ( idx = 0; idx < num_encodings; ++idx )
2113 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2115 *width = encodings[idx].width;
2116 *height = encodings[idx].height;
2117 break;
2122 mp_msg( MSGT_VO, MSGL_V,
2123 "[xv common] Maximum source image dimensions: %ux%u\n",
2124 *width, *height );
2126 XvFreeEncodingInfo( encodings );
2130 * \brief Print information about the colorkey method and source.
2132 * \param ck_handling Integer value containing the information about
2133 * colorkey handling (see x11_common.h).
2135 * Outputs the content of |ck_handling| as a readable message.
2138 static void vo_xv_print_ck_info(struct vo_x11_state *x11)
2140 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2142 switch ( x11->xv_ck_info.method )
2144 case CK_METHOD_NONE:
2145 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2146 case CK_METHOD_AUTOPAINT:
2147 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2148 case CK_METHOD_MANUALFILL:
2149 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2150 case CK_METHOD_BACKGROUND:
2151 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2154 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2156 switch ( x11->xv_ck_info.source )
2158 case CK_SRC_CUR:
2159 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2160 x11->xv_colorkey );
2161 break;
2162 case CK_SRC_USE:
2163 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2165 mp_msg( MSGT_VO, MSGL_V,
2166 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2167 x11->xv_colorkey );
2169 else
2171 mp_msg( MSGT_VO, MSGL_V,
2172 "Using colorkey from MPlayer (0x%06lx)."
2173 " Use -colorkey to change.\n",
2174 x11->xv_colorkey );
2176 break;
2177 case CK_SRC_SET:
2178 mp_msg( MSGT_VO, MSGL_V,
2179 "Setting and using colorkey from MPlayer (0x%06lx)."
2180 " Use -colorkey to change.\n",
2181 x11->xv_colorkey );
2182 break;
2186 * \brief Init colorkey depending on the settings in xv_ck_info.
2188 * \return Returns 0 on failure and 1 on success.
2190 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2191 * flags in xv_ck_info.
2193 * Possiblilities:
2194 * * Methods
2195 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2196 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2197 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2198 * * Sources
2199 * - use currently set colorkey ( CK_SRC_CUR )
2200 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2201 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2203 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2204 * we don't draw anything as this means it was forced to off.
2206 int vo_xv_init_colorkey(struct vo *vo)
2208 struct vo_x11_state *x11 = vo->x11;
2209 Atom xv_atom;
2210 int rez;
2212 /* check if colorkeying is needed */
2213 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_COLORKEY");
2215 /* if we have to deal with colorkeying ... */
2216 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2218 /* check if we should use the colorkey specified in vo_colorkey */
2219 if ( x11->xv_ck_info.source != CK_SRC_CUR )
2221 x11->xv_colorkey = vo_colorkey;
2223 /* check if we have to set the colorkey too */
2224 if ( x11->xv_ck_info.source == CK_SRC_SET )
2226 xv_atom = XInternAtom(x11->display, "XV_COLORKEY",False);
2228 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, vo_colorkey);
2229 if ( rez != Success )
2231 mp_msg( MSGT_VO, MSGL_FATAL,
2232 "[xv common] Couldn't set colorkey!\n" );
2233 return 0; // error setting colorkey
2237 else
2239 int colorkey_ret;
2241 rez=XvGetPortAttribute(x11->display,x11->xv_port, xv_atom, &colorkey_ret);
2242 if ( rez == Success )
2244 x11->xv_colorkey = colorkey_ret;
2246 else
2248 mp_msg( MSGT_VO, MSGL_FATAL,
2249 "[xv common] Couldn't get colorkey!"
2250 "Maybe the selected Xv port has no overlay.\n" );
2251 return 0; // error getting colorkey
2255 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_AUTOPAINT_COLORKEY");
2257 /* should we draw the colorkey ourselves or activate autopainting? */
2258 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2260 rez = !Success; // reset rez to something different than Success
2262 if ( xv_atom != None ) // autopaint is supported
2264 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1);
2267 if ( rez != Success )
2269 // fallback to manual colorkey drawing
2270 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2273 else // disable colorkey autopainting if supported
2275 if ( xv_atom != None ) // we have autopaint attribute
2277 XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 0);
2281 else // do no colorkey drawing at all
2283 x11->xv_ck_info.method = CK_METHOD_NONE;
2284 } /* end: should we draw colorkey */
2286 /* output information about the current colorkey settings */
2287 vo_xv_print_ck_info(x11);
2289 return 1; // success
2293 * \brief Draw the colorkey on the video window.
2295 * Draws the colorkey depending on the set method ( colorkey_handling ).
2297 * Also draws the black bars ( when the video doesn't fit the display in
2298 * fullscreen ) separately, so they don't overlap with the video area.
2299 * It doesn't call XFlush.
2302 void vo_xv_draw_colorkey(struct vo *vo, int32_t x, int32_t y,
2303 int32_t w, int32_t h)
2305 struct MPOpts *opts = vo->opts;
2306 struct vo_x11_state *x11 = vo->x11;
2307 if( x11->xv_ck_info.method == CK_METHOD_MANUALFILL ||
2308 x11->xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2310 XSetForeground(x11->display, x11->vo_gc, x11->xv_colorkey );
2311 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2312 x, y,
2313 w, h );
2316 /* draw black bars if needed */
2317 /* TODO! move this to vo_x11_clearwindow_part() */
2318 if ( vo_fs )
2320 XSetForeground(x11->display, x11->vo_gc, 0 );
2321 /* making non-overlap fills, requires 8 checks instead of 4 */
2322 if ( y > 0 )
2323 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2324 0, 0,
2325 opts->vo_screenwidth, y);
2326 if (x > 0)
2327 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2328 0, 0,
2329 x, opts->vo_screenheight);
2330 if (x + w < opts->vo_screenwidth)
2331 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2332 x + w, 0,
2333 opts->vo_screenwidth, opts->vo_screenheight);
2334 if (y + h < opts->vo_screenheight)
2335 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2336 0, y + h,
2337 opts->vo_screenwidth, opts->vo_screenheight);
2341 /** \brief Tests if a valid argument for the ck suboption was given. */
2342 int xv_test_ck( void * arg )
2344 strarg_t * strarg = (strarg_t *)arg;
2346 if ( strargcmp( strarg, "use" ) == 0 ||
2347 strargcmp( strarg, "set" ) == 0 ||
2348 strargcmp( strarg, "cur" ) == 0 )
2350 return 1;
2353 return 0;
2355 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2356 int xv_test_ckm( void * arg )
2358 strarg_t * strarg = (strarg_t *)arg;
2360 if ( strargcmp( strarg, "bg" ) == 0 ||
2361 strargcmp( strarg, "man" ) == 0 ||
2362 strargcmp( strarg, "auto" ) == 0 )
2364 return 1;
2367 return 0;
2371 * \brief Modify the colorkey_handling var according to str
2373 * Checks if a valid pointer ( not NULL ) to the string
2374 * was given. And in that case modifies the colorkey_handling
2375 * var to reflect the requested behaviour.
2376 * If nothing happens the content of colorkey_handling stays
2377 * the same.
2379 * \param str Pointer to the string or NULL
2382 void xv_setup_colorkeyhandling(struct vo *vo, const char *ck_method_str,
2383 const char *ck_str)
2385 struct vo_x11_state *x11 = vo->x11;
2386 /* check if a valid pointer to the string was passed */
2387 if ( ck_str )
2389 if ( strncmp( ck_str, "use", 3 ) == 0 )
2391 x11->xv_ck_info.source = CK_SRC_USE;
2393 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2395 x11->xv_ck_info.source = CK_SRC_SET;
2398 /* check if a valid pointer to the string was passed */
2399 if ( ck_method_str )
2401 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2403 x11->xv_ck_info.method = CK_METHOD_BACKGROUND;
2405 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2407 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2409 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2411 x11->xv_ck_info.method = CK_METHOD_AUTOPAINT;
2416 #endif
2418 struct vo_x11_state *vo_x11_init_state(void)
2420 struct vo_x11_state *s = talloc_ptrtype(NULL, s);
2421 *s = (struct vo_x11_state){
2422 .xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR },
2423 .olddecor = MWM_DECOR_ALL,
2424 .oldfuncs = MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
2425 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE,
2426 .old_gravity = NorthWestGravity,
2428 return s;