Merge svn changes up to r28537
[mplayer.git] / libvo / x11_common.c
blobc7864b14dd0baceb58464071b4138085c3ddc883
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 #ifdef CONFIG_GUI
81 #include "gui/interface.h"
82 #include "mplayer.h"
83 #endif
85 #define WIN_LAYER_ONBOTTOM 2
86 #define WIN_LAYER_NORMAL 4
87 #define WIN_LAYER_ONTOP 6
88 #define WIN_LAYER_ABOVE_DOCK 10
90 extern int enable_mouse_movements;
91 int fs_layer = WIN_LAYER_ABOVE_DOCK;
93 int stop_xscreensaver = 0;
95 static int dpms_disabled = 0;
97 char *mDisplayName = NULL;
99 char **vo_fstype_list;
101 /* 1 means that the WM is metacity (broken as hell) */
102 int metacity_hack = 0;
104 #ifdef CONFIG_XF86VM
105 XF86VidModeModeInfo **vidmodes = NULL;
106 XF86VidModeModeLine modeline;
107 #endif
109 static int vo_x11_get_fs_type(int supported);
110 static void saver_off(Display *);
111 static void saver_on(Display *);
114 * Sends the EWMH fullscreen state event.
116 * action: could be one of _NET_WM_STATE_REMOVE -- remove state
117 * _NET_WM_STATE_ADD -- add state
118 * _NET_WM_STATE_TOGGLE -- toggle
120 void vo_x11_ewmh_fullscreen(struct vo_x11_state *x11, int action)
122 assert(action == _NET_WM_STATE_REMOVE ||
123 action == _NET_WM_STATE_ADD || action == _NET_WM_STATE_TOGGLE);
125 if (x11->fs_type & vo_wm_FULLSCREEN)
127 XEvent xev;
129 /* init X event structure for _NET_WM_FULLSCREEN client message */
130 xev.xclient.type = ClientMessage;
131 xev.xclient.serial = 0;
132 xev.xclient.send_event = True;
133 xev.xclient.message_type = x11->XA_NET_WM_STATE;
134 xev.xclient.window = x11->window;
135 xev.xclient.format = 32;
136 xev.xclient.data.l[0] = action;
137 xev.xclient.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
138 xev.xclient.data.l[2] = 0;
139 xev.xclient.data.l[3] = 0;
140 xev.xclient.data.l[4] = 0;
142 /* finally send that damn thing */
143 if (!XSendEvent(x11->display, DefaultRootWindow(x11->display), False,
144 SubstructureRedirectMask | SubstructureNotifyMask,
145 &xev))
147 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_EwmhFullscreenStateFailed);
152 static void vo_hidecursor(Display * disp, Window win)
154 Cursor no_ptr;
155 Pixmap bm_no;
156 XColor black, dummy;
157 Colormap colormap;
158 const char bm_no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
160 if (WinID == 0)
161 return; // do not hide if playing on the root window
163 colormap = DefaultColormap(disp, DefaultScreen(disp));
164 if ( !XAllocNamedColor(disp, colormap, "black", &black, &dummy) )
166 return; // color alloc failed, give up
168 bm_no = XCreateBitmapFromData(disp, win, bm_no_data, 8, 8);
169 no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
170 XDefineCursor(disp, win, no_ptr);
171 XFreeCursor(disp, no_ptr);
172 if (bm_no != None)
173 XFreePixmap(disp, bm_no);
174 XFreeColors(disp,colormap,&black.pixel,1,0);
177 static void vo_showcursor(Display * disp, Window win)
179 if (WinID == 0)
180 return;
181 XDefineCursor(disp, win, 0);
184 static int x11_errorhandler(Display * display, XErrorEvent * event)
186 #define MSGLEN 60
187 char msg[MSGLEN];
189 XGetErrorText(display, event->error_code, (char *) &msg, MSGLEN);
191 mp_msg(MSGT_VO, MSGL_ERR, "X11 error: %s\n", msg);
193 mp_msg(MSGT_VO, MSGL_V,
194 "Type: %x, display: %p, resourceid: %lx, serial: %lx\n",
195 event->type, event->display, event->resourceid, event->serial);
196 mp_msg(MSGT_VO, MSGL_V,
197 "Error code: %x, request code: %x, minor code: %x\n",
198 event->error_code, event->request_code, event->minor_code);
200 // abort();
201 //exit_player("X11 error");
202 return 0;
203 #undef MSGLEN
206 void fstype_help(void)
208 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_AvailableFsType);
209 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FULL_SCREEN_TYPES\n");
211 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "none",
212 "don't set fullscreen window layer");
213 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer",
214 "use _WIN_LAYER hint with default layer");
215 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer=<0..15>",
216 "use _WIN_LAYER hint with a given layer number");
217 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "netwm",
218 "force NETWM style");
219 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "above",
220 "use _NETWM_STATE_ABOVE hint if available");
221 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "below",
222 "use _NETWM_STATE_BELOW hint if available");
223 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "fullscreen",
224 "use _NETWM_STATE_FULLSCREEN hint if availale");
225 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "stays_on_top",
226 "use _NETWM_STATE_STAYS_ON_TOP hint if available");
227 mp_msg(MSGT_VO, MSGL_INFO,
228 "You can also negate the settings with simply putting '-' in the beginning");
229 mp_msg(MSGT_VO, MSGL_INFO, "\n");
232 static void fstype_dump(int fstype)
234 if (fstype)
236 mp_msg(MSGT_VO, MSGL_V, "[x11] Current fstype setting honours");
237 if (fstype & vo_wm_LAYER)
238 mp_msg(MSGT_VO, MSGL_V, " LAYER");
239 if (fstype & vo_wm_FULLSCREEN)
240 mp_msg(MSGT_VO, MSGL_V, " FULLSCREEN");
241 if (fstype & vo_wm_STAYS_ON_TOP)
242 mp_msg(MSGT_VO, MSGL_V, " STAYS_ON_TOP");
243 if (fstype & vo_wm_ABOVE)
244 mp_msg(MSGT_VO, MSGL_V, " ABOVE");
245 if (fstype & vo_wm_BELOW)
246 mp_msg(MSGT_VO, MSGL_V, " BELOW");
247 mp_msg(MSGT_VO, MSGL_V, " X atoms\n");
248 } else
249 mp_msg(MSGT_VO, MSGL_V,
250 "[x11] Current fstype setting doesn't honour any X atoms\n");
253 static int net_wm_support_state_test(struct vo_x11_state *x11, Atom atom)
255 #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; } }
257 NET_WM_STATE_TEST(FULLSCREEN);
258 NET_WM_STATE_TEST(ABOVE);
259 NET_WM_STATE_TEST(STAYS_ON_TOP);
260 NET_WM_STATE_TEST(BELOW);
261 return 0;
264 static int x11_get_property(struct vo_x11_state *x11, Atom type, Atom ** args,
265 unsigned long *nitems)
267 int format;
268 unsigned long bytesafter;
270 return Success ==
271 XGetWindowProperty(x11->display, x11->rootwin, type, 0, 16384, False,
272 AnyPropertyType, &type, &format, nitems,
273 &bytesafter, (unsigned char **) args)
274 && *nitems > 0;
277 static int vo_wm_detect(struct vo *vo)
279 struct vo_x11_state *x11 = vo->x11;
280 int i;
281 int wm = 0;
282 unsigned long nitems;
283 Atom *args = NULL;
285 if (WinID >= 0)
286 return 0;
288 // -- supports layers
289 if (x11_get_property(x11, x11->XA_WIN_PROTOCOLS, &args, &nitems))
291 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports layers.\n");
292 for (i = 0; i < nitems; i++)
294 if (args[i] == x11->XA_WIN_LAYER)
296 wm |= vo_wm_LAYER;
297 metacity_hack |= 1;
298 } else
299 /* metacity is the only window manager I know which reports
300 * supporting only the _WIN_LAYER hint in _WIN_PROTOCOLS.
301 * (what's more support for it is broken) */
302 metacity_hack |= 2;
304 XFree(args);
305 if (wm && (metacity_hack == 1))
307 // metacity claims to support layers, but it is not the truth :-)
308 wm ^= vo_wm_LAYER;
309 mp_msg(MSGT_VO, MSGL_V,
310 "[x11] Using workaround for Metacity bugs.\n");
313 // --- netwm
314 if (x11_get_property(x11, x11->XA_NET_SUPPORTED, &args, &nitems))
316 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports NetWM.\n");
317 for (i = 0; i < nitems; i++)
318 wm |= net_wm_support_state_test(vo->x11, args[i]);
319 XFree(args);
322 if (wm == 0)
323 mp_msg(MSGT_VO, MSGL_V, "[x11] Unknown wm type...\n");
324 return wm;
327 #define XA_INIT(x) x11->XA##x = XInternAtom(x11->display, #x, False)
328 static void init_atoms(struct vo_x11_state *x11)
330 XA_INIT(_NET_SUPPORTED);
331 XA_INIT(_NET_WM_STATE);
332 XA_INIT(_NET_WM_STATE_FULLSCREEN);
333 XA_INIT(_NET_WM_STATE_ABOVE);
334 XA_INIT(_NET_WM_STATE_STAYS_ON_TOP);
335 XA_INIT(_NET_WM_STATE_BELOW);
336 XA_INIT(_NET_WM_PID);
337 XA_INIT(_WIN_PROTOCOLS);
338 XA_INIT(_WIN_LAYER);
339 XA_INIT(_WIN_HINTS);
340 XA_INIT(WM_PROTOCOLS);
341 XA_INIT(WM_DELETE_WINDOW);
344 void update_xinerama_info(struct vo *vo) {
345 struct MPOpts *opts = vo->opts;
346 int screen = xinerama_screen;
347 xinerama_x = xinerama_y = 0;
348 #ifdef CONFIG_XINERAMA
349 if (screen >= -1 && XineramaIsActive(vo->x11->display))
351 XineramaScreenInfo *screens;
352 int num_screens;
354 screens = XineramaQueryScreens(vo->x11->display, &num_screens);
355 if (screen >= num_screens)
356 screen = num_screens - 1;
357 if (screen == -1) {
358 int x = vo->dx + vo->dwidth / 2;
359 int y = vo->dy + vo->dheight / 2;
360 for (screen = num_screens - 1; screen > 0; screen--) {
361 int left = screens[screen].x_org;
362 int right = left + screens[screen].width;
363 int top = screens[screen].y_org;
364 int bottom = top + screens[screen].height;
365 if (left <= x && x <= right && top <= y && y <= bottom)
366 break;
369 if (screen < 0)
370 screen = 0;
371 opts->vo_screenwidth = screens[screen].width;
372 opts->vo_screenheight = screens[screen].height;
373 xinerama_x = screens[screen].x_org;
374 xinerama_y = screens[screen].y_org;
376 XFree(screens);
378 #endif
379 aspect_save_screenres(vo, opts->vo_screenwidth, opts->vo_screenheight);
382 int vo_init(struct vo *vo)
384 struct MPOpts *opts = vo->opts;
385 struct vo_x11_state *x11 = vo->x11;
386 // int mScreen;
387 int depth, bpp;
388 unsigned int mask;
390 // char * DisplayName = ":0.0";
391 // Display * mDisplay;
392 XImage *mXImage = NULL;
394 // Window mRootWin;
395 XWindowAttributes attribs;
396 char *dispName;
398 if (vo_rootwin)
399 WinID = 0; // use root window
401 if (x11->depthonscreen)
403 saver_off(x11->display);
404 return 1; // already called
407 XSetErrorHandler(x11_errorhandler);
409 #if 0
410 if (!mDisplayName)
411 if (!(mDisplayName = getenv("DISPLAY")))
412 mDisplayName = strdup(":0.0");
413 #else
414 dispName = XDisplayName(mDisplayName);
415 #endif
417 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
419 x11->display = XOpenDisplay(dispName);
420 if (!x11->display)
422 mp_msg(MSGT_VO, MSGL_ERR,
423 "vo: couldn't open the X11 display (%s)!\n", dispName);
424 return 0;
426 x11->screen = DefaultScreen(x11->display); // screen ID
427 x11->rootwin = RootWindow(x11->display, x11->screen); // root window ID
429 init_atoms(vo->x11);
431 #ifdef CONFIG_XF86VM
433 int clock;
435 XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline);
436 if (!opts->vo_screenwidth)
437 opts->vo_screenwidth = modeline.hdisplay;
438 if (!opts->vo_screenheight)
439 opts->vo_screenheight = modeline.vdisplay;
441 #endif
443 if (!opts->vo_screenwidth)
444 opts->vo_screenwidth = DisplayWidth(x11->display, x11->screen);
445 if (!opts->vo_screenheight)
446 opts->vo_screenheight = DisplayHeight(x11->display, x11->screen);
448 // get color depth (from root window, or the best visual):
449 XGetWindowAttributes(x11->display, x11->rootwin, &attribs);
450 depth = attribs.depth;
452 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
454 Visual *visual;
456 depth = vo_find_depth_from_visuals(x11->display, x11->screen, &visual);
457 if (depth != -1)
458 mXImage = XCreateImage(x11->display, visual, depth, ZPixmap,
459 0, NULL, 1, 1, 8, 1);
460 } else
461 mXImage =
462 XGetImage(x11->display, x11->rootwin, 0, 0, 1, 1, AllPlanes, ZPixmap);
464 x11->depthonscreen = depth; // display depth on screen
466 // get bits/pixel from XImage structure:
467 if (mXImage == NULL)
469 mask = 0;
470 } else
473 * for the depth==24 case, the XImage structures might use
474 * 24 or 32 bits of data per pixel. The x11->depthonscreen
475 * field stores the amount of data per pixel in the
476 * XImage structure!
478 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
480 bpp = mXImage->bits_per_pixel;
481 if ((x11->depthonscreen + 7) / 8 != (bpp + 7) / 8)
482 x11->depthonscreen = bpp; // by A'rpi
483 mask =
484 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
485 mp_msg(MSGT_VO, MSGL_V,
486 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
487 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
488 XDestroyImage(mXImage);
490 if (((x11->depthonscreen + 7) / 8) == 2)
492 if (mask == 0x7FFF)
493 x11->depthonscreen = 15;
494 else if (mask == 0xFFFF)
495 x11->depthonscreen = 16;
497 // XCloseDisplay( mDisplay );
498 /* slightly improved local display detection AST */
499 if (strncmp(dispName, "unix:", 5) == 0)
500 dispName += 4;
501 else if (strncmp(dispName, "localhost:", 10) == 0)
502 dispName += 9;
503 if (*dispName == ':' && atoi(dispName + 1) < 10)
504 x11->display_is_local = 1;
505 else
506 x11->display_is_local = 0;
507 mp_msg(MSGT_VO, MSGL_V,
508 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
509 opts->vo_screenwidth, opts->vo_screenheight, depth, x11->depthonscreen,
510 dispName, x11->display_is_local ? "local" : "remote");
512 x11->wm_type = vo_wm_detect(vo);
514 x11->fs_type = vo_x11_get_fs_type(x11->wm_type);
516 fstype_dump(x11->fs_type);
518 saver_off(x11->display);
519 return 1;
522 void vo_uninit(struct vo_x11_state *x11)
524 if (!x11->display)
526 mp_msg(MSGT_VO, MSGL_V,
527 "vo: x11 uninit called but X11 not initialized..\n");
528 return;
530 // if( !vo_depthonscreen ) return;
531 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
532 XSetErrorHandler(NULL);
533 XCloseDisplay(x11->display);
534 x11->depthonscreen = 0;
535 x11->display = NULL;
536 talloc_free(x11);
539 #include "osdep/keycodes.h"
540 #include "wskeys.h"
542 #ifdef XF86XK_AudioPause
543 static const struct keymap keysym_map[] = {
544 {XF86XK_MenuKB, KEY_MENU},
545 {XF86XK_AudioPlay, KEY_PLAY}, {XF86XK_AudioPause, KEY_PAUSE}, {XF86XK_AudioStop, KEY_STOP},
546 {XF86XK_AudioPrev, KEY_PREV}, {XF86XK_AudioNext, KEY_NEXT},
547 {XF86XK_AudioMute, KEY_MUTE}, {XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN}, {XF86XK_AudioRaiseVolume, KEY_VOLUME_UP},
548 {0, 0}
551 static void vo_x11_putkey_ext(struct vo *vo, int keysym)
553 struct mp_fifo *f = vo->key_fifo;
554 int mpkey = lookup_keymap_table(keysym_map, keysym);
555 if (mpkey)
556 mplayer_put_key(f, mpkey);
558 #endif
560 static const struct keymap keymap[] = {
561 // special keys
562 {wsEscape, KEY_ESC}, {wsBackSpace, KEY_BS}, {wsTab, KEY_TAB}, {wsEnter, KEY_ENTER},
564 // cursor keys
565 {wsLeft, KEY_LEFT}, {wsRight, KEY_RIGHT}, {wsUp, KEY_UP}, {wsDown, KEY_DOWN},
567 // navigation block
568 {wsInsert, KEY_INSERT}, {wsDelete, KEY_DELETE}, {wsHome, KEY_HOME}, {wsEnd, KEY_END},
569 {wsPageUp, KEY_PAGE_UP}, {wsPageDown, KEY_PAGE_DOWN},
571 // F-keys
572 {wsF1, KEY_F+1}, {wsF2, KEY_F+2}, {wsF3, KEY_F+3}, {wsF4, KEY_F+4},
573 {wsF5, KEY_F+5}, {wsF6, KEY_F+6}, {wsF7, KEY_F+7}, {wsF8, KEY_F+8},
574 {wsF9, KEY_F+9}, {wsF10, KEY_F+10}, {wsF11, KEY_F+11}, {wsF12, KEY_F+12},
576 // numpad independent of numlock
577 {wsGrayMinus, '-'}, {wsGrayPlus, '+'}, {wsGrayMul, '*'}, {wsGrayDiv, '/'},
578 {wsGrayEnter, KEY_KPENTER},
580 // numpad with numlock
581 {wsGray0, KEY_KP0}, {wsGray1, KEY_KP1}, {wsGray2, KEY_KP2},
582 {wsGray3, KEY_KP3}, {wsGray4, KEY_KP4}, {wsGray5, KEY_KP5},
583 {wsGray6, KEY_KP6}, {wsGray7, KEY_KP7}, {wsGray8, KEY_KP8},
584 {wsGray9, KEY_KP9}, {wsGrayDecimal, KEY_KPDEC},
586 // numpad without numlock
587 {wsGrayInsert, KEY_KPINS}, {wsGrayEnd, KEY_KP1}, {wsGrayDown, KEY_KP2},
588 {wsGrayPgDn, KEY_KP3}, {wsGrayLeft, KEY_KP4}, {wsGray5Dup, KEY_KP5},
589 {wsGrayRight, KEY_KP6}, {wsGrayHome, KEY_KP7}, {wsGrayUp, KEY_KP8},
590 {wsGrayPgUp, KEY_KP9}, {wsGrayDelete, KEY_KPDEL},
592 {0, 0}
595 void vo_x11_putkey(struct vo *vo, int key)
597 static const char *passthrough_keys = " -+*/<>`~!@#$%^&()_{}:;\"\',.?\\|=[]";
598 int mpkey = 0;
599 if ((key >= 'a' && key <= 'z') ||
600 (key >= 'A' && key <= 'Z') ||
601 (key >= '0' && key <= '9') ||
602 (key > 0 && key < 256 && strchr(passthrough_keys, key)))
603 mpkey = key;
605 if (!mpkey)
606 mpkey = lookup_keymap_table(keymap, key);
608 if (mpkey)
609 mplayer_put_key(vo->key_fifo, mpkey);
613 // ----- Motif header: -------
615 #define MWM_HINTS_FUNCTIONS (1L << 0)
616 #define MWM_HINTS_DECORATIONS (1L << 1)
617 #define MWM_HINTS_INPUT_MODE (1L << 2)
618 #define MWM_HINTS_STATUS (1L << 3)
620 #define MWM_FUNC_ALL (1L << 0)
621 #define MWM_FUNC_RESIZE (1L << 1)
622 #define MWM_FUNC_MOVE (1L << 2)
623 #define MWM_FUNC_MINIMIZE (1L << 3)
624 #define MWM_FUNC_MAXIMIZE (1L << 4)
625 #define MWM_FUNC_CLOSE (1L << 5)
627 #define MWM_DECOR_ALL (1L << 0)
628 #define MWM_DECOR_BORDER (1L << 1)
629 #define MWM_DECOR_RESIZEH (1L << 2)
630 #define MWM_DECOR_TITLE (1L << 3)
631 #define MWM_DECOR_MENU (1L << 4)
632 #define MWM_DECOR_MINIMIZE (1L << 5)
633 #define MWM_DECOR_MAXIMIZE (1L << 6)
635 #define MWM_INPUT_MODELESS 0
636 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
637 #define MWM_INPUT_SYSTEM_MODAL 2
638 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
639 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
641 #define MWM_TEAROFF_WINDOW (1L<<0)
643 typedef struct
645 long flags;
646 long functions;
647 long decorations;
648 long input_mode;
649 long state;
650 } MotifWmHints;
652 static MotifWmHints vo_MotifWmHints;
653 static Atom vo_MotifHints = None;
655 void vo_x11_decoration(struct vo *vo, int d)
657 struct vo_x11_state *x11 = vo->x11;
658 Atom mtype;
659 int mformat;
660 unsigned long mn, mb;
662 if (!WinID)
663 return;
665 if (vo_fsmode & 8)
667 XSetTransientForHint(x11->display, x11->window,
668 RootWindow(x11->display, x11->screen));
671 vo_MotifHints = XInternAtom(x11->display, "_MOTIF_WM_HINTS", 0);
672 if (vo_MotifHints != None)
674 if (!d)
676 MotifWmHints *mhints = NULL;
678 XGetWindowProperty(x11->display, x11->window,
679 vo_MotifHints, 0, 20, False,
680 vo_MotifHints, &mtype, &mformat, &mn,
681 &mb, (unsigned char **) &mhints);
682 if (mhints)
684 if (mhints->flags & MWM_HINTS_DECORATIONS)
685 x11->olddecor = mhints->decorations;
686 if (mhints->flags & MWM_HINTS_FUNCTIONS)
687 x11->oldfuncs = mhints->functions;
688 XFree(mhints);
692 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
693 vo_MotifWmHints.flags =
694 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
695 if (d)
697 vo_MotifWmHints.functions = x11->oldfuncs;
698 d = x11->olddecor;
700 #if 0
701 vo_MotifWmHints.decorations =
702 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
703 #else
704 vo_MotifWmHints.decorations =
705 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
706 #endif
707 XChangeProperty(x11->display, x11->window, vo_MotifHints,
708 vo_MotifHints, 32,
709 PropModeReplace,
710 (unsigned char *) &vo_MotifWmHints,
711 (vo_fsmode & 4) ? 4 : 5);
715 void vo_x11_classhint(struct vo *vo, Window window, char *name)
717 struct vo_x11_state *x11 = vo->x11;
718 XClassHint wmClass;
719 pid_t pid = getpid();
721 wmClass.res_name = name;
722 wmClass.res_class = "MPlayer";
723 XSetClassHint(x11->display, window, &wmClass);
724 XChangeProperty(x11->display, window, x11->XA_NET_WM_PID, XA_CARDINAL,
725 32, PropModeReplace, (unsigned char *) &pid, 1);
728 void vo_x11_uninit(struct vo *vo)
730 struct vo_x11_state *x11 = vo->x11;
731 saver_on(x11->display);
732 if (x11->window != None)
733 vo_showcursor(x11->display, x11->window);
735 if (x11->f_gc)
737 XFreeGC(vo->x11->display, x11->f_gc);
738 x11->f_gc = NULL;
740 #ifdef CONFIG_GUI
741 /* destroy window only if it's not controlled by the GUI */
742 if (!use_gui)
743 #endif
745 if (x11->vo_gc)
747 XSetBackground(vo->x11->display, x11->vo_gc, 0);
748 XFreeGC(vo->x11->display, x11->vo_gc);
749 x11->vo_gc = NULL;
751 if (x11->window != None)
753 XClearWindow(x11->display, x11->window);
754 if (WinID < 0)
756 XEvent xev;
758 XUnmapWindow(x11->display, x11->window);
759 XDestroyWindow(x11->display, x11->window);
762 XNextEvent(x11->display, &xev);
764 while (xev.type != DestroyNotify
765 || xev.xdestroywindow.event != x11->window);
767 x11->window = None;
769 vo_fs = 0;
770 x11->vo_old_width = x11->vo_old_height = 0;
774 int vo_x11_check_events(struct vo *vo)
776 struct vo_x11_state *x11 = vo->x11;
777 struct MPOpts *opts = vo->opts;
778 Display *display = vo->x11->display;
779 int ret = 0;
780 XEvent Event;
781 char buf[100];
782 KeySym keySym;
784 // unsigned long vo_KeyTable[512];
786 if ((x11->vo_mouse_autohide) && x11->mouse_waiting_hide &&
787 (GetTimerMS() - x11->mouse_timer >= 1000)) {
788 vo_hidecursor(display, x11->window);
789 x11->mouse_waiting_hide = 0;
792 while (XPending(display))
794 XNextEvent(display, &Event);
795 #ifdef CONFIG_GUI
796 if (use_gui)
798 guiGetEvent(0, (char *) &Event);
799 if (x11->window != Event.xany.window)
800 continue;
802 #endif
803 // printf("\rEvent.type=%X \n",Event.type);
804 switch (Event.type)
806 case Expose:
807 ret |= VO_EVENT_EXPOSE;
808 break;
809 case ConfigureNotify:
810 // if (!vo_fs && (Event.xconfigure.width == opts->vo_screenwidth || Event.xconfigure.height == opts->vo_screenheight)) break;
811 // if (vo_fs && Event.xconfigure.width != opts->vo_screenwidth && Event.xconfigure.height != opts->vo_screenheight) break;
812 if (x11->window == None)
813 break;
814 vo_x11_update_geometry(vo);
815 ret |= VO_EVENT_RESIZE;
816 break;
817 case KeyPress:
819 int key;
821 #ifdef CONFIG_GUI
822 if ( use_gui ) { break; }
823 #endif
825 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
826 &x11->compose_status);
827 #ifdef XF86XK_AudioPause
828 vo_x11_putkey_ext(vo, keySym);
829 #endif
830 key =
831 ((keySym & 0xff00) !=
832 0 ? ((keySym & 0x00ff) + 256) : (keySym));
833 vo_x11_putkey(vo, key);
834 ret |= VO_EVENT_KEYPRESS;
836 break;
837 case MotionNotify:
838 if(enable_mouse_movements)
840 char cmd_str[40];
841 sprintf(cmd_str,"set_mouse_pos %i %i",Event.xmotion.x, Event.xmotion.y);
842 mp_input_queue_cmd(vo->input_ctx,
843 mp_input_parse_cmd(cmd_str));
846 if (x11->vo_mouse_autohide)
848 vo_showcursor(display, x11->window);
849 x11->mouse_waiting_hide = 1;
850 x11->mouse_timer = GetTimerMS();
852 break;
853 case ButtonPress:
854 if (x11->vo_mouse_autohide)
856 vo_showcursor(display, x11->window);
857 x11->mouse_waiting_hide = 1;
858 x11->mouse_timer = GetTimerMS();
860 #ifdef CONFIG_GUI
861 // Ignore mouse button 1-3 under GUI.
862 if (use_gui && (Event.xbutton.button >= 1)
863 && (Event.xbutton.button <= 3))
864 break;
865 #endif
866 mplayer_put_key(vo->key_fifo,
867 (MOUSE_BTN0 + Event.xbutton.button - 1)
868 | MP_KEY_DOWN);
869 break;
870 case ButtonRelease:
871 if (x11->vo_mouse_autohide)
873 vo_showcursor(display, x11->window);
874 x11->mouse_waiting_hide = 1;
875 x11->mouse_timer = GetTimerMS();
877 #ifdef CONFIG_GUI
878 // Ignore mouse button 1-3 under GUI.
879 if (use_gui && (Event.xbutton.button >= 1)
880 && (Event.xbutton.button <= 3))
881 break;
882 #endif
883 mplayer_put_key(vo->key_fifo,
884 MOUSE_BTN0 + Event.xbutton.button - 1);
885 break;
886 case PropertyNotify:
888 char *name =
889 XGetAtomName(display, Event.xproperty.atom);
891 if (!name)
892 break;
894 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
896 XFree(name);
898 break;
899 case MapNotify:
900 x11->vo_hint.win_gravity = x11->old_gravity;
901 XSetWMNormalHints(display, x11->window, &x11->vo_hint);
902 x11->fs_flip = 0;
903 break;
904 case ClientMessage:
905 if (Event.xclient.message_type == x11->XAWM_PROTOCOLS &&
906 Event.xclient.data.l[0] == x11->XAWM_DELETE_WINDOW)
907 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
908 break;
911 return ret;
915 * \brief sets the size and position of the non-fullscreen window.
917 static void vo_x11_nofs_sizepos(struct vo *vo, int x, int y,
918 int width, int height)
920 struct vo_x11_state *x11 = vo->x11;
921 vo_x11_sizehint(vo, x, y, width, height, 0);
922 if (vo_fs) {
923 x11->vo_old_x = x;
924 x11->vo_old_y = y;
925 x11->vo_old_width = width;
926 x11->vo_old_height = height;
928 else
930 vo->dwidth = width;
931 vo->dheight = height;
932 XMoveResizeWindow(vo->x11->display, vo->x11->window, x, y, width, height);
936 void vo_x11_sizehint(struct vo *vo, int x, int y, int width, int height, int max)
938 struct vo_x11_state *x11 = vo->x11;
939 x11->vo_hint.flags = 0;
940 if (vo_keepaspect)
942 x11->vo_hint.flags |= PAspect;
943 x11->vo_hint.min_aspect.x = width;
944 x11->vo_hint.min_aspect.y = height;
945 x11->vo_hint.max_aspect.x = width;
946 x11->vo_hint.max_aspect.y = height;
949 x11->vo_hint.flags |= PPosition | PSize;
950 x11->vo_hint.x = x;
951 x11->vo_hint.y = y;
952 x11->vo_hint.width = width;
953 x11->vo_hint.height = height;
954 if (max)
956 x11->vo_hint.flags |= PMaxSize;
957 x11->vo_hint.max_width = width;
958 x11->vo_hint.max_height = height;
959 } else
961 x11->vo_hint.max_width = 0;
962 x11->vo_hint.max_height = 0;
965 // Set minimum height/width to 4 to avoid off-by-one errors
966 // and because mga_vid requires a minimal size of 4 pixels.
967 x11->vo_hint.flags |= PMinSize;
968 x11->vo_hint.min_width = x11->vo_hint.min_height = 4;
970 // Set the base size. A window manager might display the window
971 // size to the user relative to this.
972 // Setting these to width/height might be nice, but e.g. fluxbox can't handle it.
973 x11->vo_hint.flags |= PBaseSize;
974 x11->vo_hint.base_width = 0 /*width*/;
975 x11->vo_hint.base_height = 0 /*height*/;
977 x11->vo_hint.flags |= PWinGravity;
978 x11->vo_hint.win_gravity = StaticGravity;
979 XSetWMNormalHints(x11->display, x11->window, &x11->vo_hint);
982 static int vo_x11_get_gnome_layer(struct vo_x11_state *x11, Window win)
984 Atom type;
985 int format;
986 unsigned long nitems;
987 unsigned long bytesafter;
988 unsigned short *args = NULL;
990 if (XGetWindowProperty(x11->display, win, x11->XA_WIN_LAYER, 0, 16384,
991 False, AnyPropertyType, &type, &format, &nitems,
992 &bytesafter,
993 (unsigned char **) &args) == Success
994 && nitems > 0 && args)
996 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
997 *args);
998 return *args;
1000 return WIN_LAYER_NORMAL;
1004 static Window vo_x11_create_smooth_window(struct vo_x11_state *x11, Window mRoot,
1005 Visual * vis, int x, int y,
1006 unsigned int width, unsigned int height,
1007 int depth, Colormap col_map)
1009 unsigned long xswamask = CWBorderPixel;
1010 XSetWindowAttributes xswa;
1011 Window ret_win;
1013 if (col_map != CopyFromParent)
1015 xswa.colormap = col_map;
1016 xswamask |= CWColormap;
1018 xswa.background_pixel = 0;
1019 xswa.border_pixel = 0;
1020 xswa.backing_store = NotUseful;
1021 xswa.bit_gravity = StaticGravity;
1023 ret_win =
1024 XCreateWindow(x11->display, x11->rootwin, x, y, width, height, 0, depth,
1025 CopyFromParent, vis, xswamask, &xswa);
1026 XSetWMProtocols(x11->display, ret_win, &x11->XAWM_DELETE_WINDOW, 1);
1027 if (!x11->f_gc)
1028 x11->f_gc = XCreateGC(x11->display, ret_win, 0, 0);
1029 XSetForeground(x11->display, x11->f_gc, 0);
1031 return ret_win;
1035 * \brief create and setup a window suitable for display
1036 * \param vis Visual to use for creating the window
1037 * \param x x position of window
1038 * \param y y position of window
1039 * \param width width of window
1040 * \param height height of window
1041 * \param flags flags for window creation.
1042 * Only VOFLAG_FULLSCREEN is supported so far.
1043 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1044 * \param classname name to use for the classhint
1045 * \param title title for the window
1047 * This also does the grunt-work like setting Window Manager hints etc.
1048 * If vo_window is already set it just moves and resizes it.
1050 void vo_x11_create_vo_window(struct vo *vo, XVisualInfo *vis, int x, int y,
1051 unsigned int width, unsigned int height, int flags,
1052 Colormap col_map,
1053 const char *classname, const char *title)
1055 struct MPOpts *opts = vo->opts;
1056 struct vo_x11_state *x11 = vo->x11;
1057 Display *mDisplay = vo->x11->display;
1058 XGCValues xgcv;
1059 if (WinID >= 0) {
1060 x11->window = WinID ? (Window)WinID : x11->rootwin;
1061 if (col_map != CopyFromParent) {
1062 unsigned long xswamask = CWColormap;
1063 XSetWindowAttributes xswa;
1064 xswa.colormap = col_map;
1065 XUnmapWindow(mDisplay, x11->window);
1066 XChangeWindowAttributes(mDisplay, x11->window, xswamask, &xswa);
1067 XMapWindow(mDisplay, x11->window);
1069 if (WinID) vo_x11_update_geometry(vo);
1070 vo_x11_selectinput_witherr(mDisplay, x11->window,
1071 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1072 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1073 goto final;
1075 if (x11->window == None) {
1076 XSizeHints hint;
1077 XEvent xev;
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 vo_x11_classhint(vo, x11->window, classname);
1084 XStoreName(mDisplay, x11->window, title);
1085 vo_hidecursor(mDisplay, x11->window);
1086 XSelectInput(mDisplay, x11->window, StructureNotifyMask);
1087 hint.x = x; hint.y = y;
1088 hint.width = width; hint.height = height;
1089 hint.flags = PPosition | PSize;
1090 XSetStandardProperties(mDisplay, x11->window, title, title, None, NULL, 0, &hint);
1091 vo_x11_sizehint(vo, x, y, width, height, 0);
1092 if (!vo_border) vo_x11_decoration(vo, 0);
1093 // map window
1094 XMapWindow(mDisplay, x11->window);
1095 XClearWindow(mDisplay, x11->window);
1096 // wait for map
1097 do {
1098 XNextEvent(mDisplay, &xev);
1099 } while (xev.type != MapNotify || xev.xmap.event != x11->window);
1100 XSelectInput(mDisplay, x11->window, NoEventMask);
1101 XSync(mDisplay, False);
1102 vo_x11_selectinput_witherr(mDisplay, x11->window,
1103 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1104 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1106 if (opts->vo_ontop) vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1107 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, width, height);
1108 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1109 vo_x11_fullscreen(vo);
1110 final:
1111 if (x11->vo_gc != None)
1112 XFreeGC(mDisplay, x11->vo_gc);
1113 x11->vo_gc = XCreateGC(mDisplay, x11->window, GCForeground, &xgcv);
1114 XSync(mDisplay, False);
1115 x11->vo_mouse_autohide = 1;
1118 void vo_x11_clearwindow_part(struct vo *vo, Window vo_window,
1119 int img_width, int img_height, int use_fs)
1121 struct vo_x11_state *x11 = vo->x11;
1122 struct MPOpts *opts = vo->opts;
1123 Display *mDisplay = vo->x11->display;
1124 int u_dheight, u_dwidth, left_ov, left_ov2;
1126 if (!x11->f_gc)
1127 return;
1129 u_dheight = use_fs ? opts->vo_screenheight : vo->dheight;
1130 u_dwidth = use_fs ? opts->vo_screenwidth : vo->dwidth;
1131 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1132 return;
1134 left_ov = (u_dheight - img_height) / 2;
1135 left_ov2 = (u_dwidth - img_width) / 2;
1137 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, 0, u_dwidth, left_ov);
1138 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, u_dheight - left_ov - 1,
1139 u_dwidth, left_ov + 1);
1141 if (u_dwidth > img_width)
1143 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, left_ov, left_ov2,
1144 img_height);
1145 XFillRectangle(mDisplay, vo_window, x11->f_gc, u_dwidth - left_ov2 - 1,
1146 left_ov, left_ov2 + 1, img_height);
1149 XFlush(mDisplay);
1152 void vo_x11_clearwindow(struct vo *vo, Window vo_window)
1154 struct vo_x11_state *x11 = vo->x11;
1155 struct MPOpts *opts = vo->opts;
1156 if (!x11->f_gc)
1157 return;
1158 XFillRectangle(x11->display, vo_window, x11->f_gc, 0, 0,
1159 opts->vo_screenwidth, opts->vo_screenheight);
1161 XFlush(x11->display);
1165 void vo_x11_setlayer(struct vo *vo, Window vo_window, int layer)
1167 struct vo_x11_state *x11 = vo->x11;
1168 if (WinID >= 0)
1169 return;
1171 if (x11->fs_type & vo_wm_LAYER)
1173 XClientMessageEvent xev;
1175 if (!x11->orig_layer)
1176 x11->orig_layer = vo_x11_get_gnome_layer(x11, vo_window);
1178 memset(&xev, 0, sizeof(xev));
1179 xev.type = ClientMessage;
1180 xev.display = x11->display;
1181 xev.window = vo_window;
1182 xev.message_type = x11->XA_WIN_LAYER;
1183 xev.format = 32;
1184 xev.data.l[0] = layer ? fs_layer : x11->orig_layer; // if not fullscreen, stay on default layer
1185 xev.data.l[1] = CurrentTime;
1186 mp_msg(MSGT_VO, MSGL_V,
1187 "[x11] Layered style stay on top (layer %ld).\n",
1188 xev.data.l[0]);
1189 XSendEvent(x11->display, x11->rootwin, False, SubstructureNotifyMask,
1190 (XEvent *) & xev);
1191 } else if (x11->fs_type & vo_wm_NETWM)
1193 XClientMessageEvent xev;
1194 char *state;
1196 memset(&xev, 0, sizeof(xev));
1197 xev.type = ClientMessage;
1198 xev.message_type = x11->XA_NET_WM_STATE;
1199 xev.display = x11->display;
1200 xev.window = vo_window;
1201 xev.format = 32;
1202 xev.data.l[0] = layer;
1204 if (x11->fs_type & vo_wm_STAYS_ON_TOP)
1205 xev.data.l[1] = x11->XA_NET_WM_STATE_STAYS_ON_TOP;
1206 else if (x11->fs_type & vo_wm_ABOVE)
1207 xev.data.l[1] = x11->XA_NET_WM_STATE_ABOVE;
1208 else if (x11->fs_type & vo_wm_FULLSCREEN)
1209 xev.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
1210 else if (x11->fs_type & vo_wm_BELOW)
1211 // This is not fallback. We can safely assume that the situation
1212 // where only NETWM_STATE_BELOW is supported doesn't exist.
1213 xev.data.l[1] = x11->XA_NET_WM_STATE_BELOW;
1215 XSendEvent(x11->display, x11->rootwin, False, SubstructureRedirectMask,
1216 (XEvent *) & xev);
1217 state = XGetAtomName(x11->display, xev.data.l[1]);
1218 mp_msg(MSGT_VO, MSGL_V,
1219 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1220 layer, state);
1221 XFree(state);
1225 static int vo_x11_get_fs_type(int supported)
1227 int i;
1228 int type = supported;
1230 if (vo_fstype_list)
1232 i = 0;
1233 for (i = 0; vo_fstype_list[i]; i++)
1235 int neg = 0;
1236 char *arg = vo_fstype_list[i];
1238 if (vo_fstype_list[i][0] == '-')
1240 neg = 1;
1241 arg = vo_fstype_list[i] + 1;
1244 if (!strncmp(arg, "layer", 5))
1246 if (!neg && (arg[5] == '='))
1248 char *endptr = NULL;
1249 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1251 if (endptr && *endptr == '\0' && layer >= 0
1252 && layer <= 15)
1253 fs_layer = layer;
1255 if (neg)
1256 type &= ~vo_wm_LAYER;
1257 else
1258 type |= vo_wm_LAYER;
1259 } else if (!strcmp(arg, "above"))
1261 if (neg)
1262 type &= ~vo_wm_ABOVE;
1263 else
1264 type |= vo_wm_ABOVE;
1265 } else if (!strcmp(arg, "fullscreen"))
1267 if (neg)
1268 type &= ~vo_wm_FULLSCREEN;
1269 else
1270 type |= vo_wm_FULLSCREEN;
1271 } else if (!strcmp(arg, "stays_on_top"))
1273 if (neg)
1274 type &= ~vo_wm_STAYS_ON_TOP;
1275 else
1276 type |= vo_wm_STAYS_ON_TOP;
1277 } else if (!strcmp(arg, "below"))
1279 if (neg)
1280 type &= ~vo_wm_BELOW;
1281 else
1282 type |= vo_wm_BELOW;
1283 } else if (!strcmp(arg, "netwm"))
1285 if (neg)
1286 type &= ~vo_wm_NETWM;
1287 else
1288 type |= vo_wm_NETWM;
1289 } else if (!strcmp(arg, "none"))
1290 return 0;
1294 return type;
1298 * \brief update vo->dx, vo->dy, vo->dwidth and vo->dheight with current values of vo->x11->window
1299 * \return returns current color depth of vo->x11->window
1301 int vo_x11_update_geometry(struct vo *vo)
1303 struct vo_x11_state *x11 = vo->x11;
1304 unsigned depth, w, h;
1305 int dummy_int;
1306 Window dummy_win;
1307 XGetGeometry(x11->display, x11->window, &dummy_win, &dummy_int, &dummy_int,
1308 &w, &h, &dummy_int, &depth);
1309 if (w <= INT_MAX && h <= INT_MAX) {
1310 vo->dwidth = w;
1311 vo->dheight = h;
1313 XTranslateCoordinates(x11->display, x11->window, x11->rootwin, 0, 0,
1314 &vo->dx, &vo->dy, &dummy_win);
1315 return depth <= INT_MAX ? depth : 0;
1318 void vo_x11_fullscreen(struct vo *vo)
1320 struct MPOpts *opts = vo->opts;
1321 struct vo_x11_state *x11 = vo->x11;
1322 int x, y, w, h;
1324 if (WinID >= 0 || x11->fs_flip)
1325 return;
1327 if (vo_fs)
1329 // fs->win
1330 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1332 x = x11->vo_old_x;
1333 y = x11->vo_old_y;
1334 w = x11->vo_old_width;
1335 h = x11->vo_old_height;
1338 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1339 vo_fs = VO_FALSE;
1340 } else
1342 // win->fs
1343 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1345 vo_fs = VO_TRUE;
1346 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1348 x11->vo_old_x = vo->dx;
1349 x11->vo_old_y = vo->dy;
1350 x11->vo_old_width = vo->dwidth;
1351 x11->vo_old_height = vo->dheight;
1353 update_xinerama_info(vo);
1354 x = xinerama_x;
1355 y = xinerama_y;
1356 w = opts->vo_screenwidth;
1357 h = opts->vo_screenheight;
1360 long dummy;
1362 XGetWMNormalHints(x11->display, x11->window, &x11->vo_hint, &dummy);
1363 if (!(x11->vo_hint.flags & PWinGravity))
1364 x11->old_gravity = NorthWestGravity;
1365 else
1366 x11->old_gravity = x11->vo_hint.win_gravity;
1368 if (x11->wm_type == 0 && !(vo_fsmode & 16))
1370 XUnmapWindow(x11->display, x11->window); // required for MWM
1371 XWithdrawWindow(x11->display, x11->window, x11->screen);
1372 x11->fs_flip = 1;
1375 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1377 vo_x11_decoration(vo, vo_border && !vo_fs);
1378 vo_x11_sizehint(vo, x, y, w, h, 0);
1379 vo_x11_setlayer(vo, x11->window, vo_fs);
1382 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1384 /* some WMs lose ontop after fullscreen */
1385 if ((!(vo_fs)) & opts->vo_ontop)
1386 vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1388 XMapRaised(x11->display, x11->window);
1389 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1390 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1391 XRaiseWindow(x11->display, x11->window);
1392 XFlush(x11->display);
1395 void vo_x11_ontop(struct vo *vo)
1397 struct MPOpts *opts = vo->opts;
1398 opts->vo_ontop = !opts->vo_ontop;
1400 vo_x11_setlayer(vo, vo->x11->window, opts->vo_ontop);
1403 void vo_x11_border(struct vo *vo)
1405 vo_border = !vo_border;
1406 vo_x11_decoration(vo, vo_border && !vo_fs);
1410 * XScreensaver stuff
1413 static int screensaver_off;
1414 static unsigned int time_last;
1416 void xscreensaver_heartbeat(struct vo_x11_state *x11)
1418 unsigned int time = GetTimerMS();
1420 if (x11->display && screensaver_off && (time - time_last) > 30000)
1422 time_last = time;
1424 XResetScreenSaver(x11->display);
1428 static int xss_suspend(Display *mDisplay, Bool suspend)
1430 #ifndef CONFIG_XSS
1431 return 0;
1432 #else
1433 int event, error, major, minor;
1434 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1435 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1436 return 0;
1437 if (major < 1 || (major == 1 && minor < 1))
1438 return 0;
1439 XScreenSaverSuspend(mDisplay, suspend);
1440 return 1;
1441 #endif
1445 * End of XScreensaver stuff
1448 static void saver_on(Display * mDisplay)
1451 if (!screensaver_off)
1452 return;
1453 screensaver_off = 0;
1454 if (xss_suspend(mDisplay, False))
1455 return;
1456 #ifdef CONFIG_XDPMS
1457 if (dpms_disabled)
1459 int nothing;
1460 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1462 if (!DPMSEnable(mDisplay))
1463 { // restoring power saving settings
1464 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1465 } else
1467 // DPMS does not seem to be enabled unless we call DPMSInfo
1468 BOOL onoff;
1469 CARD16 state;
1471 DPMSForceLevel(mDisplay, DPMSModeOn);
1472 DPMSInfo(mDisplay, &state, &onoff);
1473 if (onoff)
1475 mp_msg(MSGT_VO, MSGL_V,
1476 "Successfully enabled DPMS\n");
1477 } else
1479 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1483 dpms_disabled = 0;
1485 #endif
1488 static void saver_off(Display * mDisplay)
1490 int nothing;
1492 if (screensaver_off)
1493 return;
1494 screensaver_off = 1;
1495 if (xss_suspend(mDisplay, True))
1496 return;
1497 #ifdef CONFIG_XDPMS
1498 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1500 BOOL onoff;
1501 CARD16 state;
1503 DPMSInfo(mDisplay, &state, &onoff);
1504 if (onoff)
1506 Status stat;
1508 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1509 dpms_disabled = 1;
1510 stat = DPMSDisable(mDisplay); // monitor powersave off
1511 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1514 #endif
1517 static XErrorHandler old_handler = NULL;
1518 static int selectinput_err = 0;
1519 static int x11_selectinput_errorhandler(Display * display,
1520 XErrorEvent * event)
1522 if (event->error_code == BadAccess)
1524 selectinput_err = 1;
1525 mp_msg(MSGT_VO, MSGL_ERR,
1526 "X11 error: BadAccess during XSelectInput Call\n");
1527 mp_msg(MSGT_VO, MSGL_ERR,
1528 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1529 /* If you think MPlayer should shutdown with this error,
1530 * comment out the following line */
1531 return 0;
1533 if (old_handler != NULL)
1534 old_handler(display, event);
1535 else
1536 x11_errorhandler(display, event);
1537 return 0;
1540 void vo_x11_selectinput_witherr(Display * display, Window w,
1541 long event_mask)
1543 XSync(display, False);
1544 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1545 selectinput_err = 0;
1546 if (vo_nomouse_input)
1548 XSelectInput(display, w,
1549 event_mask &
1550 (~(ButtonPressMask | ButtonReleaseMask)));
1551 } else
1553 XSelectInput(display, w, event_mask);
1555 XSync(display, False);
1556 XSetErrorHandler(old_handler);
1557 if (selectinput_err)
1559 mp_msg(MSGT_VO, MSGL_ERR,
1560 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1561 XSelectInput(display, w,
1562 event_mask &
1564 (ButtonPressMask | ButtonReleaseMask |
1565 PointerMotionMask)));
1569 #ifdef CONFIG_XF86VM
1570 void vo_vm_switch(struct vo *vo)
1572 struct vo_x11_state *x11 = vo->x11;
1573 struct MPOpts *opts = vo->opts;
1574 Display *mDisplay = x11->display;
1575 int vm_event, vm_error;
1576 int vm_ver, vm_rev;
1577 int i, j, have_vm = 0;
1578 int X = vo->dwidth, Y = vo->dheight;
1579 int modeline_width, modeline_height;
1581 int modecount;
1583 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1585 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1586 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1587 vm_rev);
1588 have_vm = 1;
1589 } else {
1590 mp_msg(MSGT_VO, MSGL_WARN,
1591 "XF86VidMode extension not available.\n");
1594 if (have_vm)
1596 if (vidmodes == NULL)
1597 XF86VidModeGetAllModeLines(mDisplay, x11->screen, &modecount,
1598 &vidmodes);
1599 j = 0;
1600 modeline_width = vidmodes[0]->hdisplay;
1601 modeline_height = vidmodes[0]->vdisplay;
1603 for (i = 1; i < modecount; i++)
1604 if ((vidmodes[i]->hdisplay >= X)
1605 && (vidmodes[i]->vdisplay >= Y))
1606 if ((vidmodes[i]->hdisplay <= modeline_width)
1607 && (vidmodes[i]->vdisplay <= modeline_height))
1609 modeline_width = vidmodes[i]->hdisplay;
1610 modeline_height = vidmodes[i]->vdisplay;
1611 j = i;
1614 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_SelectedVideoMode,
1615 modeline_width, modeline_height, X, Y);
1616 XF86VidModeLockModeSwitch(mDisplay, x11->screen, 0);
1617 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1618 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1620 // FIXME: all this is more of a hack than proper solution
1621 X = (opts->vo_screenwidth - modeline_width) / 2;
1622 Y = (opts->vo_screenheight - modeline_height) / 2;
1623 XF86VidModeSetViewPort(mDisplay, x11->screen, X, Y);
1624 vo->dx = X;
1625 vo->dy = Y;
1626 vo->dwidth = modeline_width;
1627 vo->dheight = modeline_height;
1628 aspect_save_screenres(vo, modeline_width, modeline_height);
1632 void vo_vm_close(struct vo *vo)
1634 Display *dpy = vo->x11->display;
1635 struct MPOpts *opts = vo->opts;
1636 #ifdef CONFIG_GUI
1637 if (vidmodes != NULL && vo->x11->vo_window != None)
1638 #else
1639 if (vidmodes != NULL)
1640 #endif
1642 int i, modecount;
1644 free(vidmodes);
1645 vidmodes = NULL;
1646 XF86VidModeGetAllModeLines(dpy, vo->x11->screen, &modecount,
1647 &vidmodes);
1648 for (i = 0; i < modecount; i++)
1649 if ((vidmodes[i]->hdisplay == opts->vo_screenwidth)
1650 && (vidmodes[i]->vdisplay == opts->vo_screenheight))
1652 mp_msg(MSGT_VO, MSGL_INFO,
1653 "Returning to original mode %dx%d\n",
1654 opts->vo_screenwidth, opts->vo_screenheight);
1655 break;
1658 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1659 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1660 free(vidmodes);
1661 vidmodes = NULL;
1664 #endif
1666 #endif /* X11_FULLSCREEN */
1670 * Scan the available visuals on this Display/Screen. Try to find
1671 * the 'best' available TrueColor visual that has a decent color
1672 * depth (at least 15bit). If there are multiple visuals with depth
1673 * >= 15bit, we prefer visuals with a smaller color depth.
1675 int vo_find_depth_from_visuals(Display * dpy, int screen,
1676 Visual ** visual_return)
1678 XVisualInfo visual_tmpl;
1679 XVisualInfo *visuals;
1680 int nvisuals, i;
1681 int bestvisual = -1;
1682 int bestvisual_depth = -1;
1684 visual_tmpl.screen = screen;
1685 visual_tmpl.class = TrueColor;
1686 visuals = XGetVisualInfo(dpy,
1687 VisualScreenMask | VisualClassMask,
1688 &visual_tmpl, &nvisuals);
1689 if (visuals != NULL)
1691 for (i = 0; i < nvisuals; i++)
1693 mp_msg(MSGT_VO, MSGL_V,
1694 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1695 visuals[i].visualid, visuals[i].depth,
1696 visuals[i].red_mask, visuals[i].green_mask,
1697 visuals[i].blue_mask);
1699 * Save the visual index and its depth, if this is the first
1700 * truecolor visul, or a visual that is 'preferred' over the
1701 * previous 'best' visual.
1703 if (bestvisual_depth == -1
1704 || (visuals[i].depth >= 15
1705 && (visuals[i].depth < bestvisual_depth
1706 || bestvisual_depth < 15)))
1708 bestvisual = i;
1709 bestvisual_depth = visuals[i].depth;
1713 if (bestvisual != -1 && visual_return != NULL)
1714 *visual_return = visuals[bestvisual].visual;
1716 XFree(visuals);
1718 return bestvisual_depth;
1722 static Colormap cmap = None;
1723 static XColor cols[256];
1724 static int cm_size, red_mask, green_mask, blue_mask;
1727 Colormap vo_x11_create_colormap(struct vo *vo, XVisualInfo *vinfo)
1729 struct vo_x11_state *x11 = vo->x11;
1730 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1732 if (vinfo->class != DirectColor)
1733 return XCreateColormap(x11->display, x11->rootwin, vinfo->visual,
1734 AllocNone);
1736 /* can this function get called twice or more? */
1737 if (cmap)
1738 return cmap;
1739 cm_size = vinfo->colormap_size;
1740 red_mask = vinfo->red_mask;
1741 green_mask = vinfo->green_mask;
1742 blue_mask = vinfo->blue_mask;
1743 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1744 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1745 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1746 rvu = 65536ull * ru / (red_mask + ru);
1747 gvu = 65536ull * gu / (green_mask + gu);
1748 bvu = 65536ull * bu / (blue_mask + bu);
1749 r = g = b = 0;
1750 rv = gv = bv = 0;
1751 m = DoRed | DoGreen | DoBlue;
1752 for (k = 0; k < cm_size; k++)
1754 int t;
1756 cols[k].pixel = r | g | b;
1757 cols[k].red = rv;
1758 cols[k].green = gv;
1759 cols[k].blue = bv;
1760 cols[k].flags = m;
1761 t = (r + ru) & red_mask;
1762 if (t < r)
1763 m &= ~DoRed;
1764 r = t;
1765 t = (g + gu) & green_mask;
1766 if (t < g)
1767 m &= ~DoGreen;
1768 g = t;
1769 t = (b + bu) & blue_mask;
1770 if (t < b)
1771 m &= ~DoBlue;
1772 b = t;
1773 rv += rvu;
1774 gv += gvu;
1775 bv += bvu;
1777 cmap = XCreateColormap(x11->display, x11->rootwin, vinfo->visual, AllocAll);
1778 XStoreColors(x11->display, cmap, cols, cm_size);
1779 return cmap;
1783 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1784 * hue and red/green/blue intensity, but we cannot do saturation.
1785 * Currently only gamma, brightness and contrast are implemented.
1786 * Is there sufficient interest for hue and/or red/green/blue intensity?
1788 /* these values have range [-100,100] and are initially 0 */
1789 static int vo_gamma = 0;
1790 static int vo_brightness = 0;
1791 static int vo_contrast = 0;
1793 static int transform_color(float val,
1794 float brightness, float contrast, float gamma) {
1795 float s = pow(val, gamma);
1796 s = (s - 0.5) * contrast + 0.5;
1797 s += brightness;
1798 if (s < 0)
1799 s = 0;
1800 if (s > 1)
1801 s = 1;
1802 return (unsigned short) (s * 65535);
1805 uint32_t vo_x11_set_equalizer(struct vo *vo, char *name, int value)
1807 float gamma, brightness, contrast;
1808 float rf, gf, bf;
1809 int k;
1812 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1813 * of TrueColor-ed window but be careful:
1814 * Unlike the colormaps, which are private for the X client
1815 * who created them and thus automatically destroyed on client
1816 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1817 * setting and _must_ be restored before the process exits.
1818 * Unforunately when the process crashes (or gets killed
1819 * for some reason) it is impossible to restore the setting,
1820 * and such behaviour could be rather annoying for the users.
1822 if (cmap == None)
1823 return VO_NOTAVAIL;
1825 if (!strcasecmp(name, "brightness"))
1826 vo_brightness = value;
1827 else if (!strcasecmp(name, "contrast"))
1828 vo_contrast = value;
1829 else if (!strcasecmp(name, "gamma"))
1830 vo_gamma = value;
1831 else
1832 return VO_NOTIMPL;
1834 brightness = 0.01 * vo_brightness;
1835 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1836 gamma = pow(2, -0.02 * vo_gamma);
1838 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1839 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
1840 green_mask;
1841 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
1843 /* now recalculate the colormap using the newly set value */
1844 for (k = 0; k < cm_size; k++)
1846 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
1847 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
1848 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
1851 XStoreColors(vo->x11->display, cmap, cols, cm_size);
1852 XFlush(vo->x11->display);
1853 return VO_TRUE;
1856 uint32_t vo_x11_get_equalizer(char *name, int *value)
1858 if (cmap == None)
1859 return VO_NOTAVAIL;
1860 if (!strcasecmp(name, "brightness"))
1861 *value = vo_brightness;
1862 else if (!strcasecmp(name, "contrast"))
1863 *value = vo_contrast;
1864 else if (!strcasecmp(name, "gamma"))
1865 *value = vo_gamma;
1866 else
1867 return VO_NOTIMPL;
1868 return VO_TRUE;
1871 void vo_calc_drwXY(struct vo *vo, uint32_t *drwX, uint32_t *drwY)
1873 struct MPOpts *opts = vo->opts;
1874 *drwX = *drwY = 0;
1875 if (vo_fs) {
1876 aspect(vo, &vo->dwidth, &vo->dheight, A_ZOOM);
1877 vo->dwidth = FFMIN(vo->dwidth, opts->vo_screenwidth);
1878 vo->dheight = FFMIN(vo->dheight, opts->vo_screenheight);
1879 *drwX = (opts->vo_screenwidth - vo->dwidth) / 2;
1880 *drwY = (opts->vo_screenheight - vo->dheight) / 2;
1881 mp_msg(MSGT_VO, MSGL_V, "[xv-fs] dx: %d dy: %d dw: %d dh: %d\n", *drwX,
1882 *drwY, vo->dwidth, vo->dheight);
1883 } else if (WinID == 0) {
1884 *drwX = vo->dx;
1885 *drwY = vo->dy;
1889 #ifdef CONFIG_XV
1890 int vo_xv_set_eq(struct vo *vo, uint32_t xv_port, char *name, int value)
1892 XvAttribute *attributes;
1893 int i, howmany, xv_atom;
1895 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
1897 /* get available attributes */
1898 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1899 for (i = 0; i < howmany && attributes; i++)
1900 if (attributes[i].flags & XvSettable)
1902 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1903 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1904 then trigger it if it's ok so that the other values are at default upon query */
1905 if (xv_atom != None)
1907 int hue = 0, port_value, port_min, port_max;
1909 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1910 (!strcasecmp(name, "brightness")))
1911 port_value = value;
1912 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1913 (!strcasecmp(name, "contrast")))
1914 port_value = value;
1915 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1916 (!strcasecmp(name, "saturation")))
1917 port_value = value;
1918 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1919 (!strcasecmp(name, "hue")))
1921 port_value = value;
1922 hue = 1;
1923 } else
1924 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1925 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1926 (!strcasecmp(name, "red_intensity")))
1927 port_value = value;
1928 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1929 && (!strcasecmp(name, "green_intensity")))
1930 port_value = value;
1931 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1932 && (!strcasecmp(name, "blue_intensity")))
1933 port_value = value;
1934 else
1935 continue;
1937 port_min = attributes[i].min_value;
1938 port_max = attributes[i].max_value;
1940 /* nvidia hue workaround */
1941 if (hue && port_min == 0 && port_max == 360)
1943 port_value =
1944 (port_value >=
1945 0) ? (port_value - 100) : (port_value + 100);
1947 // -100 -> min
1948 // 0 -> (max+min)/2
1949 // +100 -> max
1950 port_value =
1951 (port_value + 100) * (port_max - port_min) / 200 +
1952 port_min;
1953 XvSetPortAttribute(vo->x11->display, xv_port, xv_atom, port_value);
1954 return VO_TRUE;
1957 return VO_FALSE;
1960 int vo_xv_get_eq(struct vo *vo, uint32_t xv_port, char *name, int *value)
1963 XvAttribute *attributes;
1964 int i, howmany, xv_atom;
1966 /* get available attributes */
1967 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1968 for (i = 0; i < howmany && attributes; i++)
1969 if (attributes[i].flags & XvGettable)
1971 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1972 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1973 then trigger it if it's ok so that the other values are at default upon query */
1974 if (xv_atom != None)
1976 int val, port_value = 0, port_min, port_max;
1978 XvGetPortAttribute(vo->x11->display, xv_port, xv_atom,
1979 &port_value);
1981 port_min = attributes[i].min_value;
1982 port_max = attributes[i].max_value;
1983 val =
1984 (port_value - port_min) * 200 / (port_max - port_min) -
1985 100;
1987 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1988 (!strcasecmp(name, "brightness")))
1989 *value = val;
1990 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1991 (!strcasecmp(name, "contrast")))
1992 *value = val;
1993 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1994 (!strcasecmp(name, "saturation")))
1995 *value = val;
1996 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1997 (!strcasecmp(name, "hue")))
1999 /* nasty nvidia detect */
2000 if (port_min == 0 && port_max == 360)
2001 *value = (val >= 0) ? (val - 100) : (val + 100);
2002 else
2003 *value = val;
2004 } else
2005 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2006 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2007 (!strcasecmp(name, "red_intensity")))
2008 *value = val;
2009 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2010 && (!strcasecmp(name, "green_intensity")))
2011 *value = val;
2012 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2013 && (!strcasecmp(name, "blue_intensity")))
2014 *value = val;
2015 else
2016 continue;
2018 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2019 name, *value);
2020 return VO_TRUE;
2023 return VO_FALSE;
2027 * \brief Interns the requested atom if it is available.
2029 * \param atom_name String containing the name of the requested atom.
2031 * \return Returns the atom if available, else None is returned.
2034 static Atom xv_intern_atom_if_exists(struct vo_x11_state *x11,
2035 char const *atom_name)
2037 XvAttribute * attributes;
2038 int attrib_count,i;
2039 Atom xv_atom = None;
2041 attributes = XvQueryPortAttributes(x11->display, x11->xv_port, &attrib_count );
2042 if( attributes!=NULL )
2044 for ( i = 0; i < attrib_count; ++i )
2046 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2048 xv_atom = XInternAtom(x11->display, atom_name, False );
2049 break; // found what we want, break out
2052 XFree( attributes );
2055 return xv_atom;
2059 * \brief Try to enable vsync for xv.
2060 * \return Returns -1 if not available, 0 on failure and 1 on success.
2062 int vo_xv_enable_vsync(struct vo *vo)
2064 struct vo_x11_state *x11 = vo->x11;
2065 Atom xv_atom = xv_intern_atom_if_exists(x11, "XV_SYNC_TO_VBLANK");
2066 if (xv_atom == None)
2067 return -1;
2068 return XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1) == Success;
2072 * \brief Get maximum supported source image dimensions.
2074 * This function does not set the variables pointed to by
2075 * width and height if the information could not be retrieved,
2076 * so the caller is reponsible for properly initializing them.
2078 * \param width [out] The maximum width gets stored here.
2079 * \param height [out] The maximum height gets stored here.
2082 void vo_xv_get_max_img_dim(struct vo *vo, uint32_t * width, uint32_t * height)
2084 struct vo_x11_state *x11 = vo->x11;
2085 XvEncodingInfo * encodings;
2086 //unsigned long num_encodings, idx; to int or too long?!
2087 unsigned int num_encodings, idx;
2089 XvQueryEncodings(x11->display, x11->xv_port, &num_encodings, &encodings);
2091 if ( encodings )
2093 for ( idx = 0; idx < num_encodings; ++idx )
2095 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2097 *width = encodings[idx].width;
2098 *height = encodings[idx].height;
2099 break;
2104 mp_msg( MSGT_VO, MSGL_V,
2105 "[xv common] Maximum source image dimensions: %ux%u\n",
2106 *width, *height );
2108 XvFreeEncodingInfo( encodings );
2112 * \brief Print information about the colorkey method and source.
2114 * \param ck_handling Integer value containing the information about
2115 * colorkey handling (see x11_common.h).
2117 * Outputs the content of |ck_handling| as a readable message.
2120 static void vo_xv_print_ck_info(struct vo_x11_state *x11)
2122 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2124 switch ( x11->xv_ck_info.method )
2126 case CK_METHOD_NONE:
2127 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2128 case CK_METHOD_AUTOPAINT:
2129 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2130 case CK_METHOD_MANUALFILL:
2131 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2132 case CK_METHOD_BACKGROUND:
2133 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2136 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2138 switch ( x11->xv_ck_info.source )
2140 case CK_SRC_CUR:
2141 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2142 x11->xv_colorkey );
2143 break;
2144 case CK_SRC_USE:
2145 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2147 mp_msg( MSGT_VO, MSGL_V,
2148 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2149 x11->xv_colorkey );
2151 else
2153 mp_msg( MSGT_VO, MSGL_V,
2154 "Using colorkey from MPlayer (0x%06lx)."
2155 " Use -colorkey to change.\n",
2156 x11->xv_colorkey );
2158 break;
2159 case CK_SRC_SET:
2160 mp_msg( MSGT_VO, MSGL_V,
2161 "Setting and using colorkey from MPlayer (0x%06lx)."
2162 " Use -colorkey to change.\n",
2163 x11->xv_colorkey );
2164 break;
2168 * \brief Init colorkey depending on the settings in xv_ck_info.
2170 * \return Returns 0 on failure and 1 on success.
2172 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2173 * flags in xv_ck_info.
2175 * Possiblilities:
2176 * * Methods
2177 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2178 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2179 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2180 * * Sources
2181 * - use currently set colorkey ( CK_SRC_CUR )
2182 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2183 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2185 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2186 * we don't draw anything as this means it was forced to off.
2188 int vo_xv_init_colorkey(struct vo *vo)
2190 struct vo_x11_state *x11 = vo->x11;
2191 Atom xv_atom;
2192 int rez;
2194 /* check if colorkeying is needed */
2195 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_COLORKEY");
2197 /* if we have to deal with colorkeying ... */
2198 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2200 /* check if we should use the colorkey specified in vo_colorkey */
2201 if ( x11->xv_ck_info.source != CK_SRC_CUR )
2203 x11->xv_colorkey = vo_colorkey;
2205 /* check if we have to set the colorkey too */
2206 if ( x11->xv_ck_info.source == CK_SRC_SET )
2208 xv_atom = XInternAtom(x11->display, "XV_COLORKEY",False);
2210 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, vo_colorkey);
2211 if ( rez != Success )
2213 mp_msg( MSGT_VO, MSGL_FATAL,
2214 "[xv common] Couldn't set colorkey!\n" );
2215 return 0; // error setting colorkey
2219 else
2221 int colorkey_ret;
2223 rez=XvGetPortAttribute(x11->display,x11->xv_port, xv_atom, &colorkey_ret);
2224 if ( rez == Success )
2226 x11->xv_colorkey = colorkey_ret;
2228 else
2230 mp_msg( MSGT_VO, MSGL_FATAL,
2231 "[xv common] Couldn't get colorkey!"
2232 "Maybe the selected Xv port has no overlay.\n" );
2233 return 0; // error getting colorkey
2237 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_AUTOPAINT_COLORKEY");
2239 /* should we draw the colorkey ourselves or activate autopainting? */
2240 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2242 rez = !Success; // reset rez to something different than Success
2244 if ( xv_atom != None ) // autopaint is supported
2246 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1);
2249 if ( rez != Success )
2251 // fallback to manual colorkey drawing
2252 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2255 else // disable colorkey autopainting if supported
2257 if ( xv_atom != None ) // we have autopaint attribute
2259 XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 0);
2263 else // do no colorkey drawing at all
2265 x11->xv_ck_info.method = CK_METHOD_NONE;
2266 } /* end: should we draw colorkey */
2268 /* output information about the current colorkey settings */
2269 vo_xv_print_ck_info(x11);
2271 return 1; // success
2275 * \brief Draw the colorkey on the video window.
2277 * Draws the colorkey depending on the set method ( colorkey_handling ).
2279 * Also draws the black bars ( when the video doesn't fit the display in
2280 * fullscreen ) separately, so they don't overlap with the video area.
2281 * It doesn't call XFlush.
2284 void vo_xv_draw_colorkey(struct vo *vo, int32_t x, int32_t y,
2285 int32_t w, int32_t h)
2287 struct MPOpts *opts = vo->opts;
2288 struct vo_x11_state *x11 = vo->x11;
2289 if( x11->xv_ck_info.method == CK_METHOD_MANUALFILL ||
2290 x11->xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2292 XSetForeground(x11->display, x11->vo_gc, x11->xv_colorkey );
2293 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2294 x, y,
2295 w, h );
2298 /* draw black bars if needed */
2299 /* TODO! move this to vo_x11_clearwindow_part() */
2300 if ( vo_fs )
2302 XSetForeground(x11->display, x11->vo_gc, 0 );
2303 /* making non-overlap fills, requires 8 checks instead of 4 */
2304 if ( y > 0 )
2305 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2306 0, 0,
2307 opts->vo_screenwidth, y);
2308 if (x > 0)
2309 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2310 0, 0,
2311 x, opts->vo_screenheight);
2312 if (x + w < opts->vo_screenwidth)
2313 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2314 x + w, 0,
2315 opts->vo_screenwidth, opts->vo_screenheight);
2316 if (y + h < opts->vo_screenheight)
2317 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2318 0, y + h,
2319 opts->vo_screenwidth, opts->vo_screenheight);
2323 /** \brief Tests if a valid argument for the ck suboption was given. */
2324 int xv_test_ck( void * arg )
2326 strarg_t * strarg = (strarg_t *)arg;
2328 if ( strargcmp( strarg, "use" ) == 0 ||
2329 strargcmp( strarg, "set" ) == 0 ||
2330 strargcmp( strarg, "cur" ) == 0 )
2332 return 1;
2335 return 0;
2337 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2338 int xv_test_ckm( void * arg )
2340 strarg_t * strarg = (strarg_t *)arg;
2342 if ( strargcmp( strarg, "bg" ) == 0 ||
2343 strargcmp( strarg, "man" ) == 0 ||
2344 strargcmp( strarg, "auto" ) == 0 )
2346 return 1;
2349 return 0;
2353 * \brief Modify the colorkey_handling var according to str
2355 * Checks if a valid pointer ( not NULL ) to the string
2356 * was given. And in that case modifies the colorkey_handling
2357 * var to reflect the requested behaviour.
2358 * If nothing happens the content of colorkey_handling stays
2359 * the same.
2361 * \param str Pointer to the string or NULL
2364 void xv_setup_colorkeyhandling(struct vo *vo, const char *ck_method_str,
2365 const char *ck_str)
2367 struct vo_x11_state *x11 = vo->x11;
2368 /* check if a valid pointer to the string was passed */
2369 if ( ck_str )
2371 if ( strncmp( ck_str, "use", 3 ) == 0 )
2373 x11->xv_ck_info.source = CK_SRC_USE;
2375 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2377 x11->xv_ck_info.source = CK_SRC_SET;
2380 /* check if a valid pointer to the string was passed */
2381 if ( ck_method_str )
2383 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2385 x11->xv_ck_info.method = CK_METHOD_BACKGROUND;
2387 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2389 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2391 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2393 x11->xv_ck_info.method = CK_METHOD_AUTOPAINT;
2398 #endif
2400 struct vo_x11_state *vo_x11_init_state(void)
2402 struct vo_x11_state *s = talloc_ptrtype(NULL, s);
2403 *s = (struct vo_x11_state){
2404 .xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR },
2405 .olddecor = MWM_DECOR_ALL,
2406 .oldfuncs = MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
2407 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE,
2408 .old_gravity = NorthWestGravity,
2410 return s;