taglists.c: update to match current libavformat riff.c
[mplayer/glamo.git] / libvo / x11_common.c
blobb6ab203a6a24e45923163cb702b9c2c9ade95a4d
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <math.h>
22 #include <inttypes.h>
23 #include <limits.h>
25 #include "config.h"
26 #include "options.h"
27 #include "mp_msg.h"
28 #include "mp_fifo.h"
29 #include "libavutil/common.h"
30 #include "x11_common.h"
31 #include "talloc.h"
33 #ifdef X11_FULLSCREEN
35 #include <string.h>
36 #include <unistd.h>
37 #include <assert.h>
39 #include "video_out.h"
40 #include "aspect.h"
41 #include "geometry.h"
42 #include "help_mp.h"
43 #include "osdep/timer.h"
45 #include <X11/Xmd.h>
46 #include <X11/Xlib.h>
47 #include <X11/Xutil.h>
48 #include <X11/Xatom.h>
50 #ifdef CONFIG_XSS
51 #include <X11/extensions/scrnsaver.h>
52 #endif
54 #ifdef CONFIG_XDPMS
55 #include <X11/extensions/dpms.h>
56 #endif
58 #ifdef CONFIG_XINERAMA
59 #include <X11/extensions/Xinerama.h>
60 #endif
62 #ifdef CONFIG_XF86VM
63 #include <X11/extensions/xf86vmode.h>
64 #endif
66 #ifdef CONFIG_XF86XK
67 #include <X11/XF86keysym.h>
68 #endif
70 #ifdef CONFIG_XV
71 #include <X11/extensions/Xv.h>
72 #include <X11/extensions/Xvlib.h>
74 #include "subopt-helper.h"
75 #endif
77 #include "input/input.h"
78 #include "input/mouse.h"
80 #define WIN_LAYER_ONBOTTOM 2
81 #define WIN_LAYER_NORMAL 4
82 #define WIN_LAYER_ONTOP 6
83 #define WIN_LAYER_ABOVE_DOCK 10
85 extern int enable_mouse_movements;
86 int fs_layer = WIN_LAYER_ABOVE_DOCK;
88 int stop_xscreensaver = 0;
90 static int dpms_disabled = 0;
92 char *mDisplayName = NULL;
94 char **vo_fstype_list;
96 /* 1 means that the WM is metacity (broken as hell) */
97 int metacity_hack = 0;
99 #ifdef CONFIG_XF86VM
100 XF86VidModeModeInfo **vidmodes = NULL;
101 XF86VidModeModeLine modeline;
102 #endif
104 static int vo_x11_get_fs_type(int supported);
105 static void saver_off(Display *);
106 static void saver_on(Display *);
109 * Sends the EWMH fullscreen state event.
111 * action: could be one of _NET_WM_STATE_REMOVE -- remove state
112 * _NET_WM_STATE_ADD -- add state
113 * _NET_WM_STATE_TOGGLE -- toggle
115 void vo_x11_ewmh_fullscreen(struct vo_x11_state *x11, int action)
117 assert(action == _NET_WM_STATE_REMOVE ||
118 action == _NET_WM_STATE_ADD || action == _NET_WM_STATE_TOGGLE);
120 if (x11->fs_type & vo_wm_FULLSCREEN)
122 XEvent xev;
124 /* init X event structure for _NET_WM_FULLSCREEN client message */
125 xev.xclient.type = ClientMessage;
126 xev.xclient.serial = 0;
127 xev.xclient.send_event = True;
128 xev.xclient.message_type = x11->XA_NET_WM_STATE;
129 xev.xclient.window = x11->window;
130 xev.xclient.format = 32;
131 xev.xclient.data.l[0] = action;
132 xev.xclient.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
133 xev.xclient.data.l[2] = 0;
134 xev.xclient.data.l[3] = 0;
135 xev.xclient.data.l[4] = 0;
137 /* finally send that damn thing */
138 if (!XSendEvent(x11->display, DefaultRootWindow(x11->display), False,
139 SubstructureRedirectMask | SubstructureNotifyMask,
140 &xev))
142 mp_tmsg(MSGT_VO, MSGL_ERR, "\nX11: Couldn't send EWMH fullscreen event!\n");
147 static void vo_hidecursor(Display * disp, Window win)
149 Cursor no_ptr;
150 Pixmap bm_no;
151 XColor black, dummy;
152 Colormap colormap;
153 const char bm_no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
155 if (WinID == 0)
156 return; // do not hide if playing on the root window
158 colormap = DefaultColormap(disp, DefaultScreen(disp));
159 if ( !XAllocNamedColor(disp, colormap, "black", &black, &dummy) )
161 return; // color alloc failed, give up
163 bm_no = XCreateBitmapFromData(disp, win, bm_no_data, 8, 8);
164 no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
165 XDefineCursor(disp, win, no_ptr);
166 XFreeCursor(disp, no_ptr);
167 if (bm_no != None)
168 XFreePixmap(disp, bm_no);
169 XFreeColors(disp,colormap,&black.pixel,1,0);
172 static void vo_showcursor(Display * disp, Window win)
174 if (WinID == 0)
175 return;
176 XDefineCursor(disp, win, 0);
179 static int x11_errorhandler(Display * display, XErrorEvent * event)
181 #define MSGLEN 60
182 char msg[MSGLEN];
184 XGetErrorText(display, event->error_code, (char *) &msg, MSGLEN);
186 mp_msg(MSGT_VO, MSGL_ERR, "X11 error: %s\n", msg);
188 mp_msg(MSGT_VO, MSGL_V,
189 "Type: %x, display: %p, resourceid: %lx, serial: %lx\n",
190 event->type, event->display, event->resourceid, event->serial);
191 mp_msg(MSGT_VO, MSGL_V,
192 "Error code: %x, request code: %x, minor code: %x\n",
193 event->error_code, event->request_code, event->minor_code);
195 // abort();
196 //exit_player("X11 error");
197 return 0;
198 #undef MSGLEN
201 void fstype_help(void)
203 mp_tmsg(MSGT_VO, MSGL_INFO, "Available fullscreen layer change modes:\n");
204 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FULL_SCREEN_TYPES\n");
206 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "none",
207 "don't set fullscreen window layer");
208 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer",
209 "use _WIN_LAYER hint with default layer");
210 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer=<0..15>",
211 "use _WIN_LAYER hint with a given layer number");
212 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "netwm",
213 "force NETWM style");
214 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "above",
215 "use _NETWM_STATE_ABOVE hint if available");
216 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "below",
217 "use _NETWM_STATE_BELOW hint if available");
218 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "fullscreen",
219 "use _NETWM_STATE_FULLSCREEN hint if availale");
220 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "stays_on_top",
221 "use _NETWM_STATE_STAYS_ON_TOP hint if available");
222 mp_msg(MSGT_VO, MSGL_INFO,
223 "You can also negate the settings with simply putting '-' in the beginning");
224 mp_msg(MSGT_VO, MSGL_INFO, "\n");
227 static void fstype_dump(int fstype)
229 if (fstype)
231 mp_msg(MSGT_VO, MSGL_V, "[x11] Current fstype setting honours");
232 if (fstype & vo_wm_LAYER)
233 mp_msg(MSGT_VO, MSGL_V, " LAYER");
234 if (fstype & vo_wm_FULLSCREEN)
235 mp_msg(MSGT_VO, MSGL_V, " FULLSCREEN");
236 if (fstype & vo_wm_STAYS_ON_TOP)
237 mp_msg(MSGT_VO, MSGL_V, " STAYS_ON_TOP");
238 if (fstype & vo_wm_ABOVE)
239 mp_msg(MSGT_VO, MSGL_V, " ABOVE");
240 if (fstype & vo_wm_BELOW)
241 mp_msg(MSGT_VO, MSGL_V, " BELOW");
242 mp_msg(MSGT_VO, MSGL_V, " X atoms\n");
243 } else
244 mp_msg(MSGT_VO, MSGL_V,
245 "[x11] Current fstype setting doesn't honour any X atoms\n");
248 static int net_wm_support_state_test(struct vo_x11_state *x11, Atom atom)
250 #define NET_WM_STATE_TEST(x) { if (atom == x11->XA_NET_WM_STATE_##x) { mp_msg( MSGT_VO,MSGL_V, "[x11] Detected wm supports " #x " state.\n" ); return vo_wm_##x; } }
252 NET_WM_STATE_TEST(FULLSCREEN);
253 NET_WM_STATE_TEST(ABOVE);
254 NET_WM_STATE_TEST(STAYS_ON_TOP);
255 NET_WM_STATE_TEST(BELOW);
256 return 0;
259 static int x11_get_property(struct vo_x11_state *x11, Atom type, Atom ** args,
260 unsigned long *nitems)
262 int format;
263 unsigned long bytesafter;
265 return Success ==
266 XGetWindowProperty(x11->display, x11->rootwin, type, 0, 16384, False,
267 AnyPropertyType, &type, &format, nitems,
268 &bytesafter, (unsigned char **) args)
269 && *nitems > 0;
272 static int vo_wm_detect(struct vo *vo)
274 struct vo_x11_state *x11 = vo->x11;
275 int i;
276 int wm = 0;
277 unsigned long nitems;
278 Atom *args = NULL;
280 if (WinID >= 0)
281 return 0;
283 // -- supports layers
284 if (x11_get_property(x11, x11->XA_WIN_PROTOCOLS, &args, &nitems))
286 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports layers.\n");
287 for (i = 0; i < nitems; i++)
289 if (args[i] == x11->XA_WIN_LAYER)
291 wm |= vo_wm_LAYER;
292 metacity_hack |= 1;
293 } else
294 /* metacity is the only window manager I know which reports
295 * supporting only the _WIN_LAYER hint in _WIN_PROTOCOLS.
296 * (what's more support for it is broken) */
297 metacity_hack |= 2;
299 XFree(args);
300 if (wm && (metacity_hack == 1))
302 // metacity claims to support layers, but it is not the truth :-)
303 wm ^= vo_wm_LAYER;
304 mp_msg(MSGT_VO, MSGL_V,
305 "[x11] Using workaround for Metacity bugs.\n");
308 // --- netwm
309 if (x11_get_property(x11, x11->XA_NET_SUPPORTED, &args, &nitems))
311 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports NetWM.\n");
312 for (i = 0; i < nitems; i++)
313 wm |= net_wm_support_state_test(vo->x11, args[i]);
314 XFree(args);
317 if (wm == 0)
318 mp_msg(MSGT_VO, MSGL_V, "[x11] Unknown wm type...\n");
319 return wm;
322 #define XA_INIT(x) x11->XA##x = XInternAtom(x11->display, #x, False)
323 static void init_atoms(struct vo_x11_state *x11)
325 XA_INIT(_NET_SUPPORTED);
326 XA_INIT(_NET_WM_STATE);
327 XA_INIT(_NET_WM_STATE_FULLSCREEN);
328 XA_INIT(_NET_WM_STATE_ABOVE);
329 XA_INIT(_NET_WM_STATE_STAYS_ON_TOP);
330 XA_INIT(_NET_WM_STATE_BELOW);
331 XA_INIT(_NET_WM_PID);
332 XA_INIT(_WIN_PROTOCOLS);
333 XA_INIT(_WIN_LAYER);
334 XA_INIT(_WIN_HINTS);
335 XA_INIT(WM_PROTOCOLS);
336 XA_INIT(WM_DELETE_WINDOW);
339 void update_xinerama_info(struct vo *vo) {
340 struct MPOpts *opts = vo->opts;
341 int screen = xinerama_screen;
342 xinerama_x = xinerama_y = 0;
343 #ifdef CONFIG_XINERAMA
344 if (screen >= -1 && XineramaIsActive(vo->x11->display))
346 XineramaScreenInfo *screens;
347 int num_screens;
349 screens = XineramaQueryScreens(vo->x11->display, &num_screens);
350 if (screen >= num_screens)
351 screen = num_screens - 1;
352 if (screen == -1) {
353 int x = vo->dx + vo->dwidth / 2;
354 int y = vo->dy + vo->dheight / 2;
355 for (screen = num_screens - 1; screen > 0; screen--) {
356 int left = screens[screen].x_org;
357 int right = left + screens[screen].width;
358 int top = screens[screen].y_org;
359 int bottom = top + screens[screen].height;
360 if (left <= x && x <= right && top <= y && y <= bottom)
361 break;
364 if (screen < 0)
365 screen = 0;
366 opts->vo_screenwidth = screens[screen].width;
367 opts->vo_screenheight = screens[screen].height;
368 xinerama_x = screens[screen].x_org;
369 xinerama_y = screens[screen].y_org;
371 XFree(screens);
373 #endif
374 aspect_save_screenres(vo, opts->vo_screenwidth, opts->vo_screenheight);
377 int vo_init(struct vo *vo)
379 struct MPOpts *opts = vo->opts;
380 struct vo_x11_state *x11 = vo->x11;
381 // int mScreen;
382 int depth, bpp;
383 unsigned int mask;
385 // char * DisplayName = ":0.0";
386 // Display * mDisplay;
387 XImage *mXImage = NULL;
389 // Window mRootWin;
390 XWindowAttributes attribs;
391 char *dispName;
393 if (vo_rootwin)
394 WinID = 0; // use root window
396 if (x11->depthonscreen)
398 saver_off(x11->display);
399 return 1; // already called
402 XSetErrorHandler(x11_errorhandler);
404 #if 0
405 if (!mDisplayName)
406 if (!(mDisplayName = getenv("DISPLAY")))
407 mDisplayName = strdup(":0.0");
408 #else
409 dispName = XDisplayName(mDisplayName);
410 #endif
412 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
414 x11->display = XOpenDisplay(dispName);
415 if (!x11->display)
417 mp_msg(MSGT_VO, MSGL_ERR,
418 "vo: couldn't open the X11 display (%s)!\n", dispName);
419 return 0;
421 x11->screen = DefaultScreen(x11->display); // screen ID
422 x11->rootwin = RootWindow(x11->display, x11->screen); // root window ID
424 init_atoms(vo->x11);
426 #ifdef CONFIG_XF86VM
428 int clock;
430 XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline);
431 if (!opts->vo_screenwidth)
432 opts->vo_screenwidth = modeline.hdisplay;
433 if (!opts->vo_screenheight)
434 opts->vo_screenheight = modeline.vdisplay;
436 #endif
438 if (!opts->vo_screenwidth)
439 opts->vo_screenwidth = DisplayWidth(x11->display, x11->screen);
440 if (!opts->vo_screenheight)
441 opts->vo_screenheight = DisplayHeight(x11->display, x11->screen);
443 // get color depth (from root window, or the best visual):
444 XGetWindowAttributes(x11->display, x11->rootwin, &attribs);
445 depth = attribs.depth;
447 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
449 Visual *visual;
451 depth = vo_find_depth_from_visuals(x11->display, x11->screen, &visual);
452 if (depth != -1)
453 mXImage = XCreateImage(x11->display, visual, depth, ZPixmap,
454 0, NULL, 1, 1, 8, 1);
455 } else
456 mXImage =
457 XGetImage(x11->display, x11->rootwin, 0, 0, 1, 1, AllPlanes, ZPixmap);
459 x11->depthonscreen = depth; // display depth on screen
461 // get bits/pixel from XImage structure:
462 if (mXImage == NULL)
464 mask = 0;
465 } else
468 * for the depth==24 case, the XImage structures might use
469 * 24 or 32 bits of data per pixel. The x11->depthonscreen
470 * field stores the amount of data per pixel in the
471 * XImage structure!
473 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
475 bpp = mXImage->bits_per_pixel;
476 if ((x11->depthonscreen + 7) / 8 != (bpp + 7) / 8)
477 x11->depthonscreen = bpp; // by A'rpi
478 mask =
479 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
480 mp_msg(MSGT_VO, MSGL_V,
481 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
482 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
483 XDestroyImage(mXImage);
485 if (((x11->depthonscreen + 7) / 8) == 2)
487 if (mask == 0x7FFF)
488 x11->depthonscreen = 15;
489 else if (mask == 0xFFFF)
490 x11->depthonscreen = 16;
492 // XCloseDisplay( mDisplay );
493 /* slightly improved local display detection AST */
494 if (strncmp(dispName, "unix:", 5) == 0)
495 dispName += 4;
496 else if (strncmp(dispName, "localhost:", 10) == 0)
497 dispName += 9;
498 if (*dispName == ':' && atoi(dispName + 1) < 10)
499 x11->display_is_local = 1;
500 else
501 x11->display_is_local = 0;
502 mp_msg(MSGT_VO, MSGL_V,
503 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
504 opts->vo_screenwidth, opts->vo_screenheight, depth, x11->depthonscreen,
505 dispName, x11->display_is_local ? "local" : "remote");
507 x11->wm_type = vo_wm_detect(vo);
509 x11->fs_type = vo_x11_get_fs_type(x11->wm_type);
511 fstype_dump(x11->fs_type);
513 saver_off(x11->display);
514 return 1;
517 void vo_uninit(struct vo_x11_state *x11)
519 if (!x11->display)
521 mp_msg(MSGT_VO, MSGL_V,
522 "vo: x11 uninit called but X11 not initialized..\n");
523 return;
525 // if( !vo_depthonscreen ) return;
526 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
527 XSetErrorHandler(NULL);
528 XCloseDisplay(x11->display);
529 x11->depthonscreen = 0;
530 x11->display = NULL;
531 talloc_free(x11);
534 #include "osdep/keycodes.h"
535 #include "wskeys.h"
537 #ifdef XF86XK_AudioPause
538 static const struct keymap keysym_map[] = {
539 {XF86XK_MenuKB, KEY_MENU},
540 {XF86XK_AudioPlay, KEY_PLAY}, {XF86XK_AudioPause, KEY_PAUSE}, {XF86XK_AudioStop, KEY_STOP},
541 {XF86XK_AudioPrev, KEY_PREV}, {XF86XK_AudioNext, KEY_NEXT},
542 {XF86XK_AudioMute, KEY_MUTE}, {XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN}, {XF86XK_AudioRaiseVolume, KEY_VOLUME_UP},
543 {0, 0}
546 static void vo_x11_putkey_ext(struct vo *vo, int keysym)
548 struct mp_fifo *f = vo->key_fifo;
549 int mpkey = lookup_keymap_table(keysym_map, keysym);
550 if (mpkey)
551 mplayer_put_key(f, mpkey);
553 #endif
555 static const struct keymap keymap[] = {
556 // special keys
557 {wsEscape, KEY_ESC}, {wsBackSpace, KEY_BS}, {wsTab, KEY_TAB}, {wsEnter, KEY_ENTER},
559 // cursor keys
560 {wsLeft, KEY_LEFT}, {wsRight, KEY_RIGHT}, {wsUp, KEY_UP}, {wsDown, KEY_DOWN},
562 // navigation block
563 {wsInsert, KEY_INSERT}, {wsDelete, KEY_DELETE}, {wsHome, KEY_HOME}, {wsEnd, KEY_END},
564 {wsPageUp, KEY_PAGE_UP}, {wsPageDown, KEY_PAGE_DOWN},
566 // F-keys
567 {wsF1, KEY_F+1}, {wsF2, KEY_F+2}, {wsF3, KEY_F+3}, {wsF4, KEY_F+4},
568 {wsF5, KEY_F+5}, {wsF6, KEY_F+6}, {wsF7, KEY_F+7}, {wsF8, KEY_F+8},
569 {wsF9, KEY_F+9}, {wsF10, KEY_F+10}, {wsF11, KEY_F+11}, {wsF12, KEY_F+12},
571 // numpad independent of numlock
572 {wsGrayMinus, '-'}, {wsGrayPlus, '+'}, {wsGrayMul, '*'}, {wsGrayDiv, '/'},
573 {wsGrayEnter, KEY_KPENTER},
575 // numpad with numlock
576 {wsGray0, KEY_KP0}, {wsGray1, KEY_KP1}, {wsGray2, KEY_KP2},
577 {wsGray3, KEY_KP3}, {wsGray4, KEY_KP4}, {wsGray5, KEY_KP5},
578 {wsGray6, KEY_KP6}, {wsGray7, KEY_KP7}, {wsGray8, KEY_KP8},
579 {wsGray9, KEY_KP9}, {wsGrayDecimal, KEY_KPDEC},
581 // numpad without numlock
582 {wsGrayInsert, KEY_KPINS}, {wsGrayEnd, KEY_KP1}, {wsGrayDown, KEY_KP2},
583 {wsGrayPgDn, KEY_KP3}, {wsGrayLeft, KEY_KP4}, {wsGray5Dup, KEY_KP5},
584 {wsGrayRight, KEY_KP6}, {wsGrayHome, KEY_KP7}, {wsGrayUp, KEY_KP8},
585 {wsGrayPgUp, KEY_KP9}, {wsGrayDelete, KEY_KPDEL},
587 {0, 0}
590 void vo_x11_putkey(struct vo *vo, int key)
592 static const char *passthrough_keys = " -+*/<>`~!@#$%^&()_{}:;\"\',.?\\|=[]";
593 int mpkey = 0;
594 if ((key >= 'a' && key <= 'z') ||
595 (key >= 'A' && key <= 'Z') ||
596 (key >= '0' && key <= '9') ||
597 (key > 0 && key < 256 && strchr(passthrough_keys, key)))
598 mpkey = key;
600 if (!mpkey)
601 mpkey = lookup_keymap_table(keymap, key);
603 if (mpkey)
604 mplayer_put_key(vo->key_fifo, mpkey);
608 // ----- Motif header: -------
610 #define MWM_HINTS_FUNCTIONS (1L << 0)
611 #define MWM_HINTS_DECORATIONS (1L << 1)
612 #define MWM_HINTS_INPUT_MODE (1L << 2)
613 #define MWM_HINTS_STATUS (1L << 3)
615 #define MWM_FUNC_ALL (1L << 0)
616 #define MWM_FUNC_RESIZE (1L << 1)
617 #define MWM_FUNC_MOVE (1L << 2)
618 #define MWM_FUNC_MINIMIZE (1L << 3)
619 #define MWM_FUNC_MAXIMIZE (1L << 4)
620 #define MWM_FUNC_CLOSE (1L << 5)
622 #define MWM_DECOR_ALL (1L << 0)
623 #define MWM_DECOR_BORDER (1L << 1)
624 #define MWM_DECOR_RESIZEH (1L << 2)
625 #define MWM_DECOR_TITLE (1L << 3)
626 #define MWM_DECOR_MENU (1L << 4)
627 #define MWM_DECOR_MINIMIZE (1L << 5)
628 #define MWM_DECOR_MAXIMIZE (1L << 6)
630 #define MWM_INPUT_MODELESS 0
631 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
632 #define MWM_INPUT_SYSTEM_MODAL 2
633 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
634 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
636 #define MWM_TEAROFF_WINDOW (1L<<0)
638 typedef struct
640 long flags;
641 long functions;
642 long decorations;
643 long input_mode;
644 long state;
645 } MotifWmHints;
647 static MotifWmHints vo_MotifWmHints;
648 static Atom vo_MotifHints = None;
650 void vo_x11_decoration(struct vo *vo, int d)
652 struct vo_x11_state *x11 = vo->x11;
653 Atom mtype;
654 int mformat;
655 unsigned long mn, mb;
657 if (!WinID)
658 return;
660 if (vo_fsmode & 8)
662 XSetTransientForHint(x11->display, x11->window,
663 RootWindow(x11->display, x11->screen));
666 vo_MotifHints = XInternAtom(x11->display, "_MOTIF_WM_HINTS", 0);
667 if (vo_MotifHints != None)
669 if (!d)
671 MotifWmHints *mhints = NULL;
673 XGetWindowProperty(x11->display, x11->window,
674 vo_MotifHints, 0, 20, False,
675 vo_MotifHints, &mtype, &mformat, &mn,
676 &mb, (unsigned char **) &mhints);
677 if (mhints)
679 if (mhints->flags & MWM_HINTS_DECORATIONS)
680 x11->olddecor = mhints->decorations;
681 if (mhints->flags & MWM_HINTS_FUNCTIONS)
682 x11->oldfuncs = mhints->functions;
683 XFree(mhints);
687 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
688 vo_MotifWmHints.flags =
689 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
690 if (d)
692 vo_MotifWmHints.functions = x11->oldfuncs;
693 d = x11->olddecor;
695 #if 0
696 vo_MotifWmHints.decorations =
697 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
698 #else
699 vo_MotifWmHints.decorations =
700 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
701 #endif
702 XChangeProperty(x11->display, x11->window, vo_MotifHints,
703 vo_MotifHints, 32,
704 PropModeReplace,
705 (unsigned char *) &vo_MotifWmHints,
706 (vo_fsmode & 4) ? 4 : 5);
710 void vo_x11_classhint(struct vo *vo, Window window, const char *name)
712 struct vo_x11_state *x11 = vo->x11;
713 XClassHint wmClass;
714 pid_t pid = getpid();
716 wmClass.res_name = vo_winname ? vo_winname : name;
717 wmClass.res_class = "MPlayer";
718 XSetClassHint(x11->display, window, &wmClass);
719 XChangeProperty(x11->display, window, x11->XA_NET_WM_PID, XA_CARDINAL,
720 32, PropModeReplace, (unsigned char *) &pid, 1);
723 void vo_x11_uninit(struct vo *vo)
725 struct vo_x11_state *x11 = vo->x11;
726 saver_on(x11->display);
727 if (x11->window != None)
728 vo_showcursor(x11->display, x11->window);
730 if (x11->f_gc)
732 XFreeGC(vo->x11->display, x11->f_gc);
733 x11->f_gc = NULL;
736 if (x11->vo_gc)
738 XSetBackground(vo->x11->display, x11->vo_gc, 0);
739 XFreeGC(vo->x11->display, x11->vo_gc);
740 x11->vo_gc = NULL;
742 if (x11->window != None)
744 XClearWindow(x11->display, x11->window);
745 if (WinID < 0)
747 XEvent xev;
749 XUnmapWindow(x11->display, x11->window);
750 XDestroyWindow(x11->display, x11->window);
753 XNextEvent(x11->display, &xev);
755 while (xev.type != DestroyNotify
756 || xev.xdestroywindow.event != x11->window);
758 x11->window = None;
760 vo_fs = 0;
761 x11->vo_old_width = x11->vo_old_height = 0;
765 int vo_x11_check_events(struct vo *vo)
767 struct vo_x11_state *x11 = vo->x11;
768 Display *display = vo->x11->display;
769 int ret = 0;
770 XEvent Event;
771 char buf[100];
772 KeySym keySym;
774 // unsigned long vo_KeyTable[512];
776 if ((x11->vo_mouse_autohide) && x11->mouse_waiting_hide &&
777 (GetTimerMS() - x11->mouse_timer >= 1000)) {
778 vo_hidecursor(display, x11->window);
779 x11->mouse_waiting_hide = 0;
782 while (XPending(display))
784 XNextEvent(display, &Event);
785 // printf("\rEvent.type=%X \n",Event.type);
786 switch (Event.type)
788 case Expose:
789 ret |= VO_EVENT_EXPOSE;
790 break;
791 case ConfigureNotify:
792 if (x11->window == None)
793 break;
795 int old_w = vo->dwidth, old_h = vo->dheight;
796 vo_x11_update_geometry(vo);
797 if (vo->dwidth != old_w || vo->dheight != old_h)
798 ret |= VO_EVENT_RESIZE;
800 break;
801 case KeyPress:
803 int key;
805 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
806 &x11->compose_status);
807 #ifdef XF86XK_AudioPause
808 vo_x11_putkey_ext(vo, keySym);
809 #endif
810 key =
811 ((keySym & 0xff00) !=
812 0 ? ((keySym & 0x00ff) + 256) : (keySym));
813 vo_x11_putkey(vo, key);
814 ret |= VO_EVENT_KEYPRESS;
816 break;
817 case MotionNotify:
818 if(enable_mouse_movements)
820 char cmd_str[40];
821 sprintf(cmd_str,"set_mouse_pos %i %i",Event.xmotion.x, Event.xmotion.y);
822 mp_input_queue_cmd(vo->input_ctx,
823 mp_input_parse_cmd(cmd_str));
826 if (x11->vo_mouse_autohide)
828 vo_showcursor(display, x11->window);
829 x11->mouse_waiting_hide = 1;
830 x11->mouse_timer = GetTimerMS();
832 break;
833 case ButtonPress:
834 if (x11->vo_mouse_autohide)
836 vo_showcursor(display, x11->window);
837 x11->mouse_waiting_hide = 1;
838 x11->mouse_timer = GetTimerMS();
840 mplayer_put_key(vo->key_fifo,
841 (MOUSE_BTN0 + Event.xbutton.button - 1)
842 | MP_KEY_DOWN);
843 break;
844 case ButtonRelease:
845 if (x11->vo_mouse_autohide)
847 vo_showcursor(display, x11->window);
848 x11->mouse_waiting_hide = 1;
849 x11->mouse_timer = GetTimerMS();
851 mplayer_put_key(vo->key_fifo,
852 MOUSE_BTN0 + Event.xbutton.button - 1);
853 break;
854 case PropertyNotify:
856 char *name =
857 XGetAtomName(display, Event.xproperty.atom);
859 if (!name)
860 break;
862 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
864 XFree(name);
866 break;
867 case MapNotify:
868 x11->vo_hint.win_gravity = x11->old_gravity;
869 XSetWMNormalHints(display, x11->window, &x11->vo_hint);
870 x11->fs_flip = 0;
871 break;
872 case ClientMessage:
873 if (Event.xclient.message_type == x11->XAWM_PROTOCOLS &&
874 Event.xclient.data.l[0] == x11->XAWM_DELETE_WINDOW)
875 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
876 break;
879 return ret;
883 * \brief sets the size and position of the non-fullscreen window.
885 static void vo_x11_nofs_sizepos(struct vo *vo, int x, int y,
886 int width, int height)
888 struct vo_x11_state *x11 = vo->x11;
889 vo_x11_sizehint(vo, x, y, width, height, 0);
890 if (vo_fs) {
891 x11->vo_old_x = x;
892 x11->vo_old_y = y;
893 x11->vo_old_width = width;
894 x11->vo_old_height = height;
896 else
898 vo->dwidth = width;
899 vo->dheight = height;
900 if (vo->opts->force_window_position)
901 XMoveResizeWindow(vo->x11->display, vo->x11->window, x, y, width,
902 height);
903 else
904 XResizeWindow(vo->x11->display, vo->x11->window, width, height);
908 void vo_x11_sizehint(struct vo *vo, int x, int y, int width, int height, int max)
910 struct vo_x11_state *x11 = vo->x11;
911 x11->vo_hint.flags = 0;
912 if (vo_keepaspect)
914 x11->vo_hint.flags |= PAspect;
915 x11->vo_hint.min_aspect.x = width;
916 x11->vo_hint.min_aspect.y = height;
917 x11->vo_hint.max_aspect.x = width;
918 x11->vo_hint.max_aspect.y = height;
921 x11->vo_hint.flags |= PPosition | PSize;
922 x11->vo_hint.x = x;
923 x11->vo_hint.y = y;
924 x11->vo_hint.width = width;
925 x11->vo_hint.height = height;
926 if (max)
928 x11->vo_hint.flags |= PMaxSize;
929 x11->vo_hint.max_width = width;
930 x11->vo_hint.max_height = height;
931 } else
933 x11->vo_hint.max_width = 0;
934 x11->vo_hint.max_height = 0;
937 // Set minimum height/width to 4 to avoid off-by-one errors
938 // and because mga_vid requires a minimal size of 4 pixels.
939 x11->vo_hint.flags |= PMinSize;
940 x11->vo_hint.min_width = x11->vo_hint.min_height = 4;
942 // Set the base size. A window manager might display the window
943 // size to the user relative to this.
944 // Setting these to width/height might be nice, but e.g. fluxbox can't handle it.
945 x11->vo_hint.flags |= PBaseSize;
946 x11->vo_hint.base_width = 0 /*width*/;
947 x11->vo_hint.base_height = 0 /*height*/;
949 x11->vo_hint.flags |= PWinGravity;
950 x11->vo_hint.win_gravity = StaticGravity;
951 XSetWMNormalHints(x11->display, x11->window, &x11->vo_hint);
954 static int vo_x11_get_gnome_layer(struct vo_x11_state *x11, Window win)
956 Atom type;
957 int format;
958 unsigned long nitems;
959 unsigned long bytesafter;
960 unsigned short *args = NULL;
962 if (XGetWindowProperty(x11->display, win, x11->XA_WIN_LAYER, 0, 16384,
963 False, AnyPropertyType, &type, &format, &nitems,
964 &bytesafter,
965 (unsigned char **) &args) == Success
966 && nitems > 0 && args)
968 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
969 *args);
970 return *args;
972 return WIN_LAYER_NORMAL;
976 static Window vo_x11_create_smooth_window(struct vo_x11_state *x11, Window mRoot,
977 Visual * vis, int x, int y,
978 unsigned int width, unsigned int height,
979 int depth, Colormap col_map)
981 unsigned long xswamask = CWBorderPixel;
982 XSetWindowAttributes xswa;
983 Window ret_win;
985 if (col_map != CopyFromParent)
987 xswa.colormap = col_map;
988 xswamask |= CWColormap;
990 xswa.background_pixel = 0;
991 xswa.border_pixel = 0;
992 xswa.backing_store = NotUseful;
993 xswa.bit_gravity = StaticGravity;
995 ret_win =
996 XCreateWindow(x11->display, x11->rootwin, x, y, width, height, 0, depth,
997 CopyFromParent, vis, xswamask, &xswa);
998 XSetWMProtocols(x11->display, ret_win, &x11->XAWM_DELETE_WINDOW, 1);
999 if (!x11->f_gc)
1000 x11->f_gc = XCreateGC(x11->display, ret_win, 0, 0);
1001 XSetForeground(x11->display, x11->f_gc, 0);
1003 return ret_win;
1007 * \brief create and setup a window suitable for display
1008 * \param vis Visual to use for creating the window
1009 * \param x x position of window
1010 * \param y y position of window
1011 * \param width width of window
1012 * \param height height of window
1013 * \param flags flags for window creation.
1014 * Only VOFLAG_FULLSCREEN is supported so far.
1015 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1016 * \param classname name to use for the classhint
1017 * \param title title for the window
1019 * This also does the grunt-work like setting Window Manager hints etc.
1020 * If vo_window is already set it just moves and resizes it.
1022 void vo_x11_create_vo_window(struct vo *vo, XVisualInfo *vis, int x, int y,
1023 unsigned int width, unsigned int height, int flags,
1024 Colormap col_map,
1025 const char *classname, const char *title)
1027 struct MPOpts *opts = vo->opts;
1028 struct vo_x11_state *x11 = vo->x11;
1029 Display *mDisplay = vo->x11->display;
1030 XGCValues xgcv;
1031 if (WinID >= 0) {
1032 vo_fs = flags & VOFLAG_FULLSCREEN;
1033 x11->window = WinID ? (Window)WinID : x11->rootwin;
1034 if (col_map != CopyFromParent) {
1035 unsigned long xswamask = CWColormap;
1036 XSetWindowAttributes xswa;
1037 xswa.colormap = col_map;
1038 XChangeWindowAttributes(mDisplay, x11->window, xswamask, &xswa);
1039 XInstallColormap(mDisplay, col_map);
1041 if (WinID) vo_x11_update_geometry(vo);
1042 vo_x11_selectinput_witherr(mDisplay, x11->window,
1043 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1044 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1045 goto final;
1047 if (x11->window == None) {
1048 XSizeHints hint;
1049 XEvent xev;
1050 vo_fs = 0;
1051 vo->dwidth = width;
1052 vo->dheight = height;
1053 x11->window = vo_x11_create_smooth_window(x11, x11->rootwin, vis->visual,
1054 x, y, width, height, vis->depth, col_map);
1055 vo_x11_classhint(vo, x11->window, classname);
1056 XStoreName(mDisplay, x11->window, title);
1057 vo_hidecursor(mDisplay, x11->window);
1058 XSelectInput(mDisplay, x11->window, StructureNotifyMask);
1059 hint.x = x; hint.y = y;
1060 hint.width = width; hint.height = height;
1061 hint.flags = PPosition | PSize;
1062 XSetStandardProperties(mDisplay, x11->window, title, title, None, NULL, 0, &hint);
1063 vo_x11_sizehint(vo, x, y, width, height, 0);
1064 if (!vo_border) vo_x11_decoration(vo, 0);
1065 // map window
1066 XMapWindow(mDisplay, x11->window);
1067 XClearWindow(mDisplay, x11->window);
1068 // wait for map
1069 do {
1070 XNextEvent(mDisplay, &xev);
1071 } while (xev.type != MapNotify || xev.xmap.event != x11->window);
1072 XSelectInput(mDisplay, x11->window, NoEventMask);
1073 XSync(mDisplay, False);
1074 vo_x11_selectinput_witherr(mDisplay, x11->window,
1075 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1076 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1078 if (opts->vo_ontop) vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1079 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, width, height);
1080 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1081 vo_x11_fullscreen(vo);
1082 else if (vo_fs) {
1083 // if we are already in fullscreen do not switch back and forth, just
1084 // set the size values right.
1085 vo->dwidth = vo->opts->vo_screenwidth;
1086 vo->dheight = vo->opts->vo_screenheight;
1088 final:
1089 if (x11->vo_gc != None)
1090 XFreeGC(mDisplay, x11->vo_gc);
1091 x11->vo_gc = XCreateGC(mDisplay, x11->window, GCForeground, &xgcv);
1092 XSync(mDisplay, False);
1093 x11->vo_mouse_autohide = 1;
1096 void vo_x11_clearwindow_part(struct vo *vo, Window vo_window,
1097 int img_width, int img_height, int use_fs)
1099 struct vo_x11_state *x11 = vo->x11;
1100 struct MPOpts *opts = vo->opts;
1101 Display *mDisplay = vo->x11->display;
1102 int u_dheight, u_dwidth, left_ov, left_ov2;
1104 if (!x11->f_gc)
1105 return;
1107 u_dheight = use_fs ? opts->vo_screenheight : vo->dheight;
1108 u_dwidth = use_fs ? opts->vo_screenwidth : vo->dwidth;
1109 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1110 return;
1112 left_ov = (u_dheight - img_height) / 2;
1113 left_ov2 = (u_dwidth - img_width) / 2;
1115 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, 0, u_dwidth, left_ov);
1116 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, u_dheight - left_ov - 1,
1117 u_dwidth, left_ov + 1);
1119 if (u_dwidth > img_width)
1121 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, left_ov, left_ov2,
1122 img_height);
1123 XFillRectangle(mDisplay, vo_window, x11->f_gc, u_dwidth - left_ov2 - 1,
1124 left_ov, left_ov2 + 1, img_height);
1127 XFlush(mDisplay);
1130 void vo_x11_clearwindow(struct vo *vo, Window vo_window)
1132 struct vo_x11_state *x11 = vo->x11;
1133 struct MPOpts *opts = vo->opts;
1134 if (!x11->f_gc)
1135 return;
1136 XFillRectangle(x11->display, vo_window, x11->f_gc, 0, 0,
1137 opts->vo_screenwidth, opts->vo_screenheight);
1139 XFlush(x11->display);
1143 void vo_x11_setlayer(struct vo *vo, Window vo_window, int layer)
1145 struct vo_x11_state *x11 = vo->x11;
1146 if (WinID >= 0)
1147 return;
1149 if (x11->fs_type & vo_wm_LAYER)
1151 XClientMessageEvent xev;
1153 if (!x11->orig_layer)
1154 x11->orig_layer = vo_x11_get_gnome_layer(x11, vo_window);
1156 memset(&xev, 0, sizeof(xev));
1157 xev.type = ClientMessage;
1158 xev.display = x11->display;
1159 xev.window = vo_window;
1160 xev.message_type = x11->XA_WIN_LAYER;
1161 xev.format = 32;
1162 xev.data.l[0] = layer ? fs_layer : x11->orig_layer; // if not fullscreen, stay on default layer
1163 xev.data.l[1] = CurrentTime;
1164 mp_msg(MSGT_VO, MSGL_V,
1165 "[x11] Layered style stay on top (layer %ld).\n",
1166 xev.data.l[0]);
1167 XSendEvent(x11->display, x11->rootwin, False, SubstructureNotifyMask,
1168 (XEvent *) & xev);
1169 } else if (x11->fs_type & vo_wm_NETWM)
1171 XClientMessageEvent xev;
1172 char *state;
1174 memset(&xev, 0, sizeof(xev));
1175 xev.type = ClientMessage;
1176 xev.message_type = x11->XA_NET_WM_STATE;
1177 xev.display = x11->display;
1178 xev.window = vo_window;
1179 xev.format = 32;
1180 xev.data.l[0] = layer;
1182 if (x11->fs_type & vo_wm_STAYS_ON_TOP)
1183 xev.data.l[1] = x11->XA_NET_WM_STATE_STAYS_ON_TOP;
1184 else if (x11->fs_type & vo_wm_ABOVE)
1185 xev.data.l[1] = x11->XA_NET_WM_STATE_ABOVE;
1186 else if (x11->fs_type & vo_wm_FULLSCREEN)
1187 xev.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
1188 else if (x11->fs_type & vo_wm_BELOW)
1189 // This is not fallback. We can safely assume that the situation
1190 // where only NETWM_STATE_BELOW is supported doesn't exist.
1191 xev.data.l[1] = x11->XA_NET_WM_STATE_BELOW;
1193 XSendEvent(x11->display, x11->rootwin, False, SubstructureRedirectMask,
1194 (XEvent *) & xev);
1195 state = XGetAtomName(x11->display, xev.data.l[1]);
1196 mp_msg(MSGT_VO, MSGL_V,
1197 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1198 layer, state);
1199 XFree(state);
1203 static int vo_x11_get_fs_type(int supported)
1205 int i;
1206 int type = supported;
1208 if (vo_fstype_list)
1210 for (i = 0; vo_fstype_list[i]; i++)
1212 int neg = 0;
1213 char *arg = vo_fstype_list[i];
1215 if (vo_fstype_list[i][0] == '-')
1217 neg = 1;
1218 arg = vo_fstype_list[i] + 1;
1221 if (!strncmp(arg, "layer", 5))
1223 if (!neg && (arg[5] == '='))
1225 char *endptr = NULL;
1226 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1228 if (endptr && *endptr == '\0' && layer >= 0
1229 && layer <= 15)
1230 fs_layer = layer;
1232 if (neg)
1233 type &= ~vo_wm_LAYER;
1234 else
1235 type |= vo_wm_LAYER;
1236 } else if (!strcmp(arg, "above"))
1238 if (neg)
1239 type &= ~vo_wm_ABOVE;
1240 else
1241 type |= vo_wm_ABOVE;
1242 } else if (!strcmp(arg, "fullscreen"))
1244 if (neg)
1245 type &= ~vo_wm_FULLSCREEN;
1246 else
1247 type |= vo_wm_FULLSCREEN;
1248 } else if (!strcmp(arg, "stays_on_top"))
1250 if (neg)
1251 type &= ~vo_wm_STAYS_ON_TOP;
1252 else
1253 type |= vo_wm_STAYS_ON_TOP;
1254 } else if (!strcmp(arg, "below"))
1256 if (neg)
1257 type &= ~vo_wm_BELOW;
1258 else
1259 type |= vo_wm_BELOW;
1260 } else if (!strcmp(arg, "netwm"))
1262 if (neg)
1263 type &= ~vo_wm_NETWM;
1264 else
1265 type |= vo_wm_NETWM;
1266 } else if (!strcmp(arg, "none"))
1267 type = 0; // clear; keep parsing
1271 return type;
1275 * \brief update vo->dx, vo->dy, vo->dwidth and vo->dheight with current values of vo->x11->window
1276 * \return returns current color depth of vo->x11->window
1278 int vo_x11_update_geometry(struct vo *vo)
1280 struct vo_x11_state *x11 = vo->x11;
1281 unsigned depth, w, h;
1282 int dummy_int;
1283 Window dummy_win;
1284 XGetGeometry(x11->display, x11->window, &dummy_win, &dummy_int, &dummy_int,
1285 &w, &h, &dummy_int, &depth);
1286 if (w <= INT_MAX && h <= INT_MAX) {
1287 vo->dwidth = w;
1288 vo->dheight = h;
1290 XTranslateCoordinates(x11->display, x11->window, x11->rootwin, 0, 0,
1291 &vo->dx, &vo->dy, &dummy_win);
1292 if (vo_wintitle)
1293 XStoreName(x11->display, x11->window, vo_wintitle);
1295 return depth <= INT_MAX ? depth : 0;
1298 void vo_x11_fullscreen(struct vo *vo)
1300 struct MPOpts *opts = vo->opts;
1301 struct vo_x11_state *x11 = vo->x11;
1302 int x, y, w, h;
1303 x = x11->vo_old_x;
1304 y = x11->vo_old_y;
1305 w = x11->vo_old_width;
1306 h = x11->vo_old_height;
1308 if (WinID >= 0) {
1309 vo_fs = !vo_fs;
1310 return;
1312 if (x11->fs_flip)
1313 return;
1315 if (vo_fs)
1317 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1318 vo_fs = VO_FALSE;
1319 } else
1321 // win->fs
1322 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1324 vo_fs = VO_TRUE;
1325 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1327 x11->vo_old_x = vo->dx;
1328 x11->vo_old_y = vo->dy;
1329 x11->vo_old_width = vo->dwidth;
1330 x11->vo_old_height = vo->dheight;
1332 update_xinerama_info(vo);
1333 x = xinerama_x;
1334 y = xinerama_y;
1335 w = opts->vo_screenwidth;
1336 h = opts->vo_screenheight;
1339 long dummy;
1341 XGetWMNormalHints(x11->display, x11->window, &x11->vo_hint, &dummy);
1342 if (!(x11->vo_hint.flags & PWinGravity))
1343 x11->old_gravity = NorthWestGravity;
1344 else
1345 x11->old_gravity = x11->vo_hint.win_gravity;
1347 if (x11->wm_type == 0 && !(vo_fsmode & 16))
1349 XUnmapWindow(x11->display, x11->window); // required for MWM
1350 XWithdrawWindow(x11->display, x11->window, x11->screen);
1351 x11->fs_flip = 1;
1354 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1356 vo_x11_decoration(vo, vo_border && !vo_fs);
1357 vo_x11_sizehint(vo, x, y, w, h, 0);
1358 vo_x11_setlayer(vo, x11->window, vo_fs);
1361 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1363 /* some WMs lose ontop after fullscreen */
1364 if ((!(vo_fs)) & opts->vo_ontop)
1365 vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1367 XMapRaised(x11->display, x11->window);
1368 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1369 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1370 XRaiseWindow(x11->display, x11->window);
1371 XFlush(x11->display);
1374 void vo_x11_ontop(struct vo *vo)
1376 struct MPOpts *opts = vo->opts;
1377 opts->vo_ontop = !opts->vo_ontop;
1379 vo_x11_setlayer(vo, vo->x11->window, opts->vo_ontop);
1382 void vo_x11_border(struct vo *vo)
1384 vo_border = !vo_border;
1385 vo_x11_decoration(vo, vo_border && !vo_fs);
1389 * XScreensaver stuff
1392 static int screensaver_off;
1393 static unsigned int time_last;
1395 void xscreensaver_heartbeat(struct vo_x11_state *x11)
1397 unsigned int time = GetTimerMS();
1399 if (x11->display && screensaver_off && (time - time_last) > 30000)
1401 time_last = time;
1403 XResetScreenSaver(x11->display);
1407 static int xss_suspend(Display *mDisplay, Bool suspend)
1409 #ifndef CONFIG_XSS
1410 return 0;
1411 #else
1412 int event, error, major, minor;
1413 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1414 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1415 return 0;
1416 if (major < 1 || (major == 1 && minor < 1))
1417 return 0;
1418 XScreenSaverSuspend(mDisplay, suspend);
1419 return 1;
1420 #endif
1424 * End of XScreensaver stuff
1427 static void saver_on(Display * mDisplay)
1430 if (!screensaver_off)
1431 return;
1432 screensaver_off = 0;
1433 if (xss_suspend(mDisplay, False))
1434 return;
1435 #ifdef CONFIG_XDPMS
1436 if (dpms_disabled)
1438 int nothing;
1439 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1441 if (!DPMSEnable(mDisplay))
1442 { // restoring power saving settings
1443 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1444 } else
1446 // DPMS does not seem to be enabled unless we call DPMSInfo
1447 BOOL onoff;
1448 CARD16 state;
1450 DPMSForceLevel(mDisplay, DPMSModeOn);
1451 DPMSInfo(mDisplay, &state, &onoff);
1452 if (onoff)
1454 mp_msg(MSGT_VO, MSGL_V,
1455 "Successfully enabled DPMS\n");
1456 } else
1458 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1462 dpms_disabled = 0;
1464 #endif
1467 static void saver_off(Display * mDisplay)
1469 int nothing;
1471 if (screensaver_off)
1472 return;
1473 screensaver_off = 1;
1474 if (xss_suspend(mDisplay, True))
1475 return;
1476 #ifdef CONFIG_XDPMS
1477 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1479 BOOL onoff;
1480 CARD16 state;
1482 DPMSInfo(mDisplay, &state, &onoff);
1483 if (onoff)
1485 Status stat;
1487 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1488 dpms_disabled = 1;
1489 stat = DPMSDisable(mDisplay); // monitor powersave off
1490 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1493 #endif
1496 static XErrorHandler old_handler = NULL;
1497 static int selectinput_err = 0;
1498 static int x11_selectinput_errorhandler(Display * display,
1499 XErrorEvent * event)
1501 if (event->error_code == BadAccess)
1503 selectinput_err = 1;
1504 mp_msg(MSGT_VO, MSGL_ERR,
1505 "X11 error: BadAccess during XSelectInput Call\n");
1506 mp_msg(MSGT_VO, MSGL_ERR,
1507 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1508 /* If you think MPlayer should shutdown with this error,
1509 * comment out the following line */
1510 return 0;
1512 if (old_handler != NULL)
1513 old_handler(display, event);
1514 else
1515 x11_errorhandler(display, event);
1516 return 0;
1519 void vo_x11_selectinput_witherr(Display * display, Window w,
1520 long event_mask)
1522 XSync(display, False);
1523 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1524 selectinput_err = 0;
1525 if (vo_nomouse_input)
1527 XSelectInput(display, w,
1528 event_mask &
1529 (~(ButtonPressMask | ButtonReleaseMask)));
1530 } else
1532 XSelectInput(display, w, event_mask);
1534 XSync(display, False);
1535 XSetErrorHandler(old_handler);
1536 if (selectinput_err)
1538 mp_msg(MSGT_VO, MSGL_ERR,
1539 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1540 XSelectInput(display, w,
1541 event_mask &
1543 (ButtonPressMask | ButtonReleaseMask |
1544 PointerMotionMask)));
1548 #ifdef CONFIG_XF86VM
1549 void vo_vm_switch(struct vo *vo)
1551 struct vo_x11_state *x11 = vo->x11;
1552 struct MPOpts *opts = vo->opts;
1553 Display *mDisplay = x11->display;
1554 int vm_event, vm_error;
1555 int vm_ver, vm_rev;
1556 int i, j, have_vm = 0;
1557 int X = vo->dwidth, Y = vo->dheight;
1558 int modeline_width, modeline_height;
1560 int modecount;
1562 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1564 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1565 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1566 vm_rev);
1567 have_vm = 1;
1568 } else {
1569 mp_msg(MSGT_VO, MSGL_WARN,
1570 "XF86VidMode extension not available.\n");
1573 if (have_vm)
1575 if (vidmodes == NULL)
1576 XF86VidModeGetAllModeLines(mDisplay, x11->screen, &modecount,
1577 &vidmodes);
1578 j = 0;
1579 modeline_width = vidmodes[0]->hdisplay;
1580 modeline_height = vidmodes[0]->vdisplay;
1582 for (i = 1; i < modecount; i++)
1583 if ((vidmodes[i]->hdisplay >= X)
1584 && (vidmodes[i]->vdisplay >= Y))
1585 if ((vidmodes[i]->hdisplay <= modeline_width)
1586 && (vidmodes[i]->vdisplay <= modeline_height))
1588 modeline_width = vidmodes[i]->hdisplay;
1589 modeline_height = vidmodes[i]->vdisplay;
1590 j = i;
1593 mp_tmsg(MSGT_VO, MSGL_INFO, "XF86VM: Selected video mode %dx%d for image size %dx%d.\n",
1594 modeline_width, modeline_height, X, Y);
1595 XF86VidModeLockModeSwitch(mDisplay, x11->screen, 0);
1596 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1597 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1599 // FIXME: all this is more of a hack than proper solution
1600 X = (opts->vo_screenwidth - modeline_width) / 2;
1601 Y = (opts->vo_screenheight - modeline_height) / 2;
1602 XF86VidModeSetViewPort(mDisplay, x11->screen, X, Y);
1603 vo->dx = X;
1604 vo->dy = Y;
1605 vo->dwidth = modeline_width;
1606 vo->dheight = modeline_height;
1607 aspect_save_screenres(vo, modeline_width, modeline_height);
1611 void vo_vm_close(struct vo *vo)
1613 Display *dpy = vo->x11->display;
1614 struct MPOpts *opts = vo->opts;
1615 if (vidmodes != NULL)
1617 int i, modecount;
1619 free(vidmodes);
1620 vidmodes = NULL;
1621 XF86VidModeGetAllModeLines(dpy, vo->x11->screen, &modecount,
1622 &vidmodes);
1623 for (i = 0; i < modecount; i++)
1624 if ((vidmodes[i]->hdisplay == opts->vo_screenwidth)
1625 && (vidmodes[i]->vdisplay == opts->vo_screenheight))
1627 mp_msg(MSGT_VO, MSGL_INFO,
1628 "Returning to original mode %dx%d\n",
1629 opts->vo_screenwidth, opts->vo_screenheight);
1630 break;
1633 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1634 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1635 free(vidmodes);
1636 vidmodes = NULL;
1640 double vo_vm_get_fps(struct vo *vo)
1642 struct vo_x11_state *x11 = vo->x11;
1643 int clock;
1644 XF86VidModeModeLine modeline;
1645 if (!XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline))
1646 return 0;
1647 if (modeline.privsize)
1648 XFree(modeline.private);
1649 return 1e3 * clock / modeline.htotal / modeline.vtotal;
1651 #endif
1653 #endif /* X11_FULLSCREEN */
1657 * Scan the available visuals on this Display/Screen. Try to find
1658 * the 'best' available TrueColor visual that has a decent color
1659 * depth (at least 15bit). If there are multiple visuals with depth
1660 * >= 15bit, we prefer visuals with a smaller color depth.
1662 int vo_find_depth_from_visuals(Display * dpy, int screen,
1663 Visual ** visual_return)
1665 XVisualInfo visual_tmpl;
1666 XVisualInfo *visuals;
1667 int nvisuals, i;
1668 int bestvisual = -1;
1669 int bestvisual_depth = -1;
1671 visual_tmpl.screen = screen;
1672 visual_tmpl.class = TrueColor;
1673 visuals = XGetVisualInfo(dpy,
1674 VisualScreenMask | VisualClassMask,
1675 &visual_tmpl, &nvisuals);
1676 if (visuals != NULL)
1678 for (i = 0; i < nvisuals; i++)
1680 mp_msg(MSGT_VO, MSGL_V,
1681 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1682 visuals[i].visualid, visuals[i].depth,
1683 visuals[i].red_mask, visuals[i].green_mask,
1684 visuals[i].blue_mask);
1686 * Save the visual index and its depth, if this is the first
1687 * truecolor visul, or a visual that is 'preferred' over the
1688 * previous 'best' visual.
1690 if (bestvisual_depth == -1
1691 || (visuals[i].depth >= 15
1692 && (visuals[i].depth < bestvisual_depth
1693 || bestvisual_depth < 15)))
1695 bestvisual = i;
1696 bestvisual_depth = visuals[i].depth;
1700 if (bestvisual != -1 && visual_return != NULL)
1701 *visual_return = visuals[bestvisual].visual;
1703 XFree(visuals);
1705 return bestvisual_depth;
1709 static Colormap cmap = None;
1710 static XColor cols[256];
1711 static int cm_size, red_mask, green_mask, blue_mask;
1714 Colormap vo_x11_create_colormap(struct vo *vo, XVisualInfo *vinfo)
1716 struct vo_x11_state *x11 = vo->x11;
1717 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1719 if (vinfo->class != DirectColor)
1720 return XCreateColormap(x11->display, x11->rootwin, vinfo->visual,
1721 AllocNone);
1723 /* can this function get called twice or more? */
1724 if (cmap)
1725 return cmap;
1726 cm_size = vinfo->colormap_size;
1727 red_mask = vinfo->red_mask;
1728 green_mask = vinfo->green_mask;
1729 blue_mask = vinfo->blue_mask;
1730 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1731 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1732 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1733 rvu = 65536ull * ru / (red_mask + ru);
1734 gvu = 65536ull * gu / (green_mask + gu);
1735 bvu = 65536ull * bu / (blue_mask + bu);
1736 r = g = b = 0;
1737 rv = gv = bv = 0;
1738 m = DoRed | DoGreen | DoBlue;
1739 for (k = 0; k < cm_size; k++)
1741 int t;
1743 cols[k].pixel = r | g | b;
1744 cols[k].red = rv;
1745 cols[k].green = gv;
1746 cols[k].blue = bv;
1747 cols[k].flags = m;
1748 t = (r + ru) & red_mask;
1749 if (t < r)
1750 m &= ~DoRed;
1751 r = t;
1752 t = (g + gu) & green_mask;
1753 if (t < g)
1754 m &= ~DoGreen;
1755 g = t;
1756 t = (b + bu) & blue_mask;
1757 if (t < b)
1758 m &= ~DoBlue;
1759 b = t;
1760 rv += rvu;
1761 gv += gvu;
1762 bv += bvu;
1764 cmap = XCreateColormap(x11->display, x11->rootwin, vinfo->visual, AllocAll);
1765 XStoreColors(x11->display, cmap, cols, cm_size);
1766 return cmap;
1770 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1771 * hue and red/green/blue intensity, but we cannot do saturation.
1772 * Currently only gamma, brightness and contrast are implemented.
1773 * Is there sufficient interest for hue and/or red/green/blue intensity?
1775 /* these values have range [-100,100] and are initially 0 */
1776 static int vo_gamma = 0;
1777 static int vo_brightness = 0;
1778 static int vo_contrast = 0;
1780 static int transform_color(float val,
1781 float brightness, float contrast, float gamma) {
1782 float s = pow(val, gamma);
1783 s = (s - 0.5) * contrast + 0.5;
1784 s += brightness;
1785 if (s < 0)
1786 s = 0;
1787 if (s > 1)
1788 s = 1;
1789 return (unsigned short) (s * 65535);
1792 uint32_t vo_x11_set_equalizer(struct vo *vo, char *name, int value)
1794 float gamma, brightness, contrast;
1795 float rf, gf, bf;
1796 int k;
1799 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1800 * of TrueColor-ed window but be careful:
1801 * Unlike the colormaps, which are private for the X client
1802 * who created them and thus automatically destroyed on client
1803 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1804 * setting and _must_ be restored before the process exits.
1805 * Unforunately when the process crashes (or gets killed
1806 * for some reason) it is impossible to restore the setting,
1807 * and such behaviour could be rather annoying for the users.
1809 if (cmap == None)
1810 return VO_NOTAVAIL;
1812 if (!strcasecmp(name, "brightness"))
1813 vo_brightness = value;
1814 else if (!strcasecmp(name, "contrast"))
1815 vo_contrast = value;
1816 else if (!strcasecmp(name, "gamma"))
1817 vo_gamma = value;
1818 else
1819 return VO_NOTIMPL;
1821 brightness = 0.01 * vo_brightness;
1822 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1823 gamma = pow(2, -0.02 * vo_gamma);
1825 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1826 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
1827 green_mask;
1828 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
1830 /* now recalculate the colormap using the newly set value */
1831 for (k = 0; k < cm_size; k++)
1833 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
1834 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
1835 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
1838 XStoreColors(vo->x11->display, cmap, cols, cm_size);
1839 XFlush(vo->x11->display);
1840 return VO_TRUE;
1843 uint32_t vo_x11_get_equalizer(char *name, int *value)
1845 if (cmap == None)
1846 return VO_NOTAVAIL;
1847 if (!strcasecmp(name, "brightness"))
1848 *value = vo_brightness;
1849 else if (!strcasecmp(name, "contrast"))
1850 *value = vo_contrast;
1851 else if (!strcasecmp(name, "gamma"))
1852 *value = vo_gamma;
1853 else
1854 return VO_NOTIMPL;
1855 return VO_TRUE;
1858 #ifdef CONFIG_XV
1859 int vo_xv_set_eq(struct vo *vo, uint32_t xv_port, char *name, int value)
1861 XvAttribute *attributes;
1862 int i, howmany, xv_atom;
1864 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
1866 /* get available attributes */
1867 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1868 for (i = 0; i < howmany && attributes; i++)
1869 if (attributes[i].flags & XvSettable)
1871 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1872 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1873 then trigger it if it's ok so that the other values are at default upon query */
1874 if (xv_atom != None)
1876 int hue = 0, port_value, port_min, port_max;
1878 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1879 (!strcasecmp(name, "brightness")))
1880 port_value = value;
1881 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1882 (!strcasecmp(name, "contrast")))
1883 port_value = value;
1884 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1885 (!strcasecmp(name, "saturation")))
1886 port_value = value;
1887 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1888 (!strcasecmp(name, "hue")))
1890 port_value = value;
1891 hue = 1;
1892 } else
1893 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1894 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1895 (!strcasecmp(name, "red_intensity")))
1896 port_value = value;
1897 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1898 && (!strcasecmp(name, "green_intensity")))
1899 port_value = value;
1900 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1901 && (!strcasecmp(name, "blue_intensity")))
1902 port_value = value;
1903 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
1904 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
1905 && (!strcasecmp(name, "bt_709")))
1906 port_value = value;
1907 else
1908 continue;
1910 port_min = attributes[i].min_value;
1911 port_max = attributes[i].max_value;
1913 /* nvidia hue workaround */
1914 if (hue && port_min == 0 && port_max == 360)
1916 port_value =
1917 (port_value >=
1918 0) ? (port_value - 100) : (port_value + 100);
1920 // -100 -> min
1921 // 0 -> (max+min)/2
1922 // +100 -> max
1923 port_value =
1924 (port_value + 100) * (port_max - port_min) / 200 +
1925 port_min;
1926 XvSetPortAttribute(vo->x11->display, xv_port, xv_atom, port_value);
1927 return VO_TRUE;
1930 return VO_FALSE;
1933 int vo_xv_get_eq(struct vo *vo, uint32_t xv_port, char *name, int *value)
1936 XvAttribute *attributes;
1937 int i, howmany, xv_atom;
1939 /* get available attributes */
1940 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1941 for (i = 0; i < howmany && attributes; i++)
1942 if (attributes[i].flags & XvGettable)
1944 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1945 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1946 then trigger it if it's ok so that the other values are at default upon query */
1947 if (xv_atom != None)
1949 int val, port_value = 0, port_min, port_max;
1951 XvGetPortAttribute(vo->x11->display, xv_port, xv_atom,
1952 &port_value);
1954 port_min = attributes[i].min_value;
1955 port_max = attributes[i].max_value;
1956 val =
1957 (port_value - port_min) * 200 / (port_max - port_min) -
1958 100;
1960 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1961 (!strcasecmp(name, "brightness")))
1962 *value = val;
1963 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1964 (!strcasecmp(name, "contrast")))
1965 *value = val;
1966 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1967 (!strcasecmp(name, "saturation")))
1968 *value = val;
1969 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1970 (!strcasecmp(name, "hue")))
1972 /* nasty nvidia detect */
1973 if (port_min == 0 && port_max == 360)
1974 *value = (val >= 0) ? (val - 100) : (val + 100);
1975 else
1976 *value = val;
1977 } else
1978 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1979 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1980 (!strcasecmp(name, "red_intensity")))
1981 *value = val;
1982 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1983 && (!strcasecmp(name, "green_intensity")))
1984 *value = val;
1985 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1986 && (!strcasecmp(name, "blue_intensity")))
1987 *value = val;
1988 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
1989 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
1990 && (!strcasecmp(name, "bt_709")))
1991 *value = val;
1992 else
1993 continue;
1995 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
1996 name, *value);
1997 return VO_TRUE;
2000 return VO_FALSE;
2004 * \brief Interns the requested atom if it is available.
2006 * \param atom_name String containing the name of the requested atom.
2008 * \return Returns the atom if available, else None is returned.
2011 static Atom xv_intern_atom_if_exists(struct vo_x11_state *x11,
2012 char const *atom_name)
2014 XvAttribute * attributes;
2015 int attrib_count,i;
2016 Atom xv_atom = None;
2018 attributes = XvQueryPortAttributes(x11->display, x11->xv_port, &attrib_count );
2019 if( attributes!=NULL )
2021 for ( i = 0; i < attrib_count; ++i )
2023 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2025 xv_atom = XInternAtom(x11->display, atom_name, False );
2026 break; // found what we want, break out
2029 XFree( attributes );
2032 return xv_atom;
2036 * \brief Try to enable vsync for xv.
2037 * \return Returns -1 if not available, 0 on failure and 1 on success.
2039 int vo_xv_enable_vsync(struct vo *vo)
2041 struct vo_x11_state *x11 = vo->x11;
2042 Atom xv_atom = xv_intern_atom_if_exists(x11, "XV_SYNC_TO_VBLANK");
2043 if (xv_atom == None)
2044 return -1;
2045 return XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1) == Success;
2049 * \brief Get maximum supported source image dimensions.
2051 * This function does not set the variables pointed to by
2052 * width and height if the information could not be retrieved,
2053 * so the caller is reponsible for properly initializing them.
2055 * \param width [out] The maximum width gets stored here.
2056 * \param height [out] The maximum height gets stored here.
2059 void vo_xv_get_max_img_dim(struct vo *vo, uint32_t * width, uint32_t * height)
2061 struct vo_x11_state *x11 = vo->x11;
2062 XvEncodingInfo * encodings;
2063 //unsigned long num_encodings, idx; to int or too long?!
2064 unsigned int num_encodings, idx;
2066 XvQueryEncodings(x11->display, x11->xv_port, &num_encodings, &encodings);
2068 if ( encodings )
2070 for ( idx = 0; idx < num_encodings; ++idx )
2072 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2074 *width = encodings[idx].width;
2075 *height = encodings[idx].height;
2076 break;
2081 mp_msg( MSGT_VO, MSGL_V,
2082 "[xv common] Maximum source image dimensions: %ux%u\n",
2083 *width, *height );
2085 XvFreeEncodingInfo( encodings );
2089 * \brief Print information about the colorkey method and source.
2091 * \param ck_handling Integer value containing the information about
2092 * colorkey handling (see x11_common.h).
2094 * Outputs the content of |ck_handling| as a readable message.
2097 static void vo_xv_print_ck_info(struct vo_x11_state *x11)
2099 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2101 switch ( x11->xv_ck_info.method )
2103 case CK_METHOD_NONE:
2104 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2105 case CK_METHOD_AUTOPAINT:
2106 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2107 case CK_METHOD_MANUALFILL:
2108 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2109 case CK_METHOD_BACKGROUND:
2110 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2113 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2115 switch ( x11->xv_ck_info.source )
2117 case CK_SRC_CUR:
2118 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2119 x11->xv_colorkey );
2120 break;
2121 case CK_SRC_USE:
2122 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2124 mp_msg( MSGT_VO, MSGL_V,
2125 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2126 x11->xv_colorkey );
2128 else
2130 mp_msg( MSGT_VO, MSGL_V,
2131 "Using colorkey from MPlayer (0x%06lx)."
2132 " Use -colorkey to change.\n",
2133 x11->xv_colorkey );
2135 break;
2136 case CK_SRC_SET:
2137 mp_msg( MSGT_VO, MSGL_V,
2138 "Setting and using colorkey from MPlayer (0x%06lx)."
2139 " Use -colorkey to change.\n",
2140 x11->xv_colorkey );
2141 break;
2145 * \brief Init colorkey depending on the settings in xv_ck_info.
2147 * \return Returns 0 on failure and 1 on success.
2149 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2150 * flags in xv_ck_info.
2152 * Possiblilities:
2153 * * Methods
2154 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2155 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2156 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2157 * * Sources
2158 * - use currently set colorkey ( CK_SRC_CUR )
2159 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2160 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2162 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2163 * we don't draw anything as this means it was forced to off.
2165 int vo_xv_init_colorkey(struct vo *vo)
2167 struct vo_x11_state *x11 = vo->x11;
2168 Atom xv_atom;
2169 int rez;
2171 /* check if colorkeying is needed */
2172 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_COLORKEY");
2174 /* if we have to deal with colorkeying ... */
2175 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2177 /* check if we should use the colorkey specified in vo_colorkey */
2178 if ( x11->xv_ck_info.source != CK_SRC_CUR )
2180 x11->xv_colorkey = vo_colorkey;
2182 /* check if we have to set the colorkey too */
2183 if ( x11->xv_ck_info.source == CK_SRC_SET )
2185 xv_atom = XInternAtom(x11->display, "XV_COLORKEY",False);
2187 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, vo_colorkey);
2188 if ( rez != Success )
2190 mp_msg( MSGT_VO, MSGL_FATAL,
2191 "[xv common] Couldn't set colorkey!\n" );
2192 return 0; // error setting colorkey
2196 else
2198 int colorkey_ret;
2200 rez=XvGetPortAttribute(x11->display,x11->xv_port, xv_atom, &colorkey_ret);
2201 if ( rez == Success )
2203 x11->xv_colorkey = colorkey_ret;
2205 else
2207 mp_msg( MSGT_VO, MSGL_FATAL,
2208 "[xv common] Couldn't get colorkey!"
2209 "Maybe the selected Xv port has no overlay.\n" );
2210 return 0; // error getting colorkey
2214 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_AUTOPAINT_COLORKEY");
2216 /* should we draw the colorkey ourselves or activate autopainting? */
2217 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2219 rez = !Success; // reset rez to something different than Success
2221 if ( xv_atom != None ) // autopaint is supported
2223 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1);
2226 if ( rez != Success )
2228 // fallback to manual colorkey drawing
2229 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2232 else // disable colorkey autopainting if supported
2234 if ( xv_atom != None ) // we have autopaint attribute
2236 XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 0);
2240 else // do no colorkey drawing at all
2242 x11->xv_ck_info.method = CK_METHOD_NONE;
2243 } /* end: should we draw colorkey */
2245 /* output information about the current colorkey settings */
2246 vo_xv_print_ck_info(x11);
2248 return 1; // success
2252 * \brief Draw the colorkey on the video window.
2254 * Draws the colorkey depending on the set method ( colorkey_handling ).
2256 * Also draws the black bars ( when the video doesn't fit the display in
2257 * fullscreen ) separately, so they don't overlap with the video area.
2258 * It doesn't call XFlush.
2261 void vo_xv_draw_colorkey(struct vo *vo, int32_t x, int32_t y,
2262 int32_t w, int32_t h)
2264 struct MPOpts *opts = vo->opts;
2265 struct vo_x11_state *x11 = vo->x11;
2266 if( x11->xv_ck_info.method == CK_METHOD_MANUALFILL ||
2267 x11->xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2269 XSetForeground(x11->display, x11->vo_gc, x11->xv_colorkey );
2270 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2271 x, y,
2272 w, h );
2275 /* draw black bars if needed */
2276 /* TODO! move this to vo_x11_clearwindow_part() */
2277 if ( vo_fs )
2279 XSetForeground(x11->display, x11->vo_gc, 0 );
2280 /* making non-overlap fills, requires 8 checks instead of 4 */
2281 if ( y > 0 )
2282 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2283 0, 0,
2284 opts->vo_screenwidth, y);
2285 if (x > 0)
2286 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2287 0, 0,
2288 x, opts->vo_screenheight);
2289 if (x + w < opts->vo_screenwidth)
2290 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2291 x + w, 0,
2292 opts->vo_screenwidth, opts->vo_screenheight);
2293 if (y + h < opts->vo_screenheight)
2294 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2295 0, y + h,
2296 opts->vo_screenwidth, opts->vo_screenheight);
2300 /** \brief Tests if a valid argument for the ck suboption was given. */
2301 int xv_test_ck( void * arg )
2303 strarg_t * strarg = (strarg_t *)arg;
2305 if ( strargcmp( strarg, "use" ) == 0 ||
2306 strargcmp( strarg, "set" ) == 0 ||
2307 strargcmp( strarg, "cur" ) == 0 )
2309 return 1;
2312 return 0;
2314 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2315 int xv_test_ckm( void * arg )
2317 strarg_t * strarg = (strarg_t *)arg;
2319 if ( strargcmp( strarg, "bg" ) == 0 ||
2320 strargcmp( strarg, "man" ) == 0 ||
2321 strargcmp( strarg, "auto" ) == 0 )
2323 return 1;
2326 return 0;
2330 * \brief Modify the colorkey_handling var according to str
2332 * Checks if a valid pointer ( not NULL ) to the string
2333 * was given. And in that case modifies the colorkey_handling
2334 * var to reflect the requested behaviour.
2335 * If nothing happens the content of colorkey_handling stays
2336 * the same.
2338 * \param str Pointer to the string or NULL
2341 void xv_setup_colorkeyhandling(struct vo *vo, const char *ck_method_str,
2342 const char *ck_str)
2344 struct vo_x11_state *x11 = vo->x11;
2345 /* check if a valid pointer to the string was passed */
2346 if ( ck_str )
2348 if ( strncmp( ck_str, "use", 3 ) == 0 )
2350 x11->xv_ck_info.source = CK_SRC_USE;
2352 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2354 x11->xv_ck_info.source = CK_SRC_SET;
2357 /* check if a valid pointer to the string was passed */
2358 if ( ck_method_str )
2360 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2362 x11->xv_ck_info.method = CK_METHOD_BACKGROUND;
2364 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2366 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2368 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2370 x11->xv_ck_info.method = CK_METHOD_AUTOPAINT;
2375 #endif
2377 struct vo_x11_state *vo_x11_init_state(void)
2379 struct vo_x11_state *s = talloc_ptrtype(NULL, s);
2380 *s = (struct vo_x11_state){
2381 .xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR },
2382 .olddecor = MWM_DECOR_ALL,
2383 .oldfuncs = MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
2384 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE,
2385 .old_gravity = NorthWestGravity,
2387 return s;