vo_xv: Fix context Shminfo table size
[mplayer.git] / libvo / x11_common.c
blobb70c0b8dd678313f3fe60940659c6f48db589210
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;
815 int old_w = vo->dwidth, old_h = vo->dheight;
816 vo_x11_update_geometry(vo);
817 if (vo->dwidth != old_w || vo->dheight != old_h)
818 ret |= VO_EVENT_RESIZE;
820 break;
821 case KeyPress:
823 int key;
825 #ifdef CONFIG_GUI
826 if ( use_gui ) { break; }
827 #endif
829 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
830 &x11->compose_status);
831 #ifdef XF86XK_AudioPause
832 vo_x11_putkey_ext(vo, keySym);
833 #endif
834 key =
835 ((keySym & 0xff00) !=
836 0 ? ((keySym & 0x00ff) + 256) : (keySym));
837 vo_x11_putkey(vo, key);
838 ret |= VO_EVENT_KEYPRESS;
840 break;
841 case MotionNotify:
842 if(enable_mouse_movements)
844 char cmd_str[40];
845 sprintf(cmd_str,"set_mouse_pos %i %i",Event.xmotion.x, Event.xmotion.y);
846 mp_input_queue_cmd(vo->input_ctx,
847 mp_input_parse_cmd(cmd_str));
850 if (x11->vo_mouse_autohide)
852 vo_showcursor(display, x11->window);
853 x11->mouse_waiting_hide = 1;
854 x11->mouse_timer = GetTimerMS();
856 break;
857 case ButtonPress:
858 if (x11->vo_mouse_autohide)
860 vo_showcursor(display, x11->window);
861 x11->mouse_waiting_hide = 1;
862 x11->mouse_timer = GetTimerMS();
864 #ifdef CONFIG_GUI
865 // Ignore mouse button 1-3 under GUI.
866 if (use_gui && (Event.xbutton.button >= 1)
867 && (Event.xbutton.button <= 3))
868 break;
869 #endif
870 mplayer_put_key(vo->key_fifo,
871 (MOUSE_BTN0 + Event.xbutton.button - 1)
872 | MP_KEY_DOWN);
873 break;
874 case ButtonRelease:
875 if (x11->vo_mouse_autohide)
877 vo_showcursor(display, x11->window);
878 x11->mouse_waiting_hide = 1;
879 x11->mouse_timer = GetTimerMS();
881 #ifdef CONFIG_GUI
882 // Ignore mouse button 1-3 under GUI.
883 if (use_gui && (Event.xbutton.button >= 1)
884 && (Event.xbutton.button <= 3))
885 break;
886 #endif
887 mplayer_put_key(vo->key_fifo,
888 MOUSE_BTN0 + Event.xbutton.button - 1);
889 break;
890 case PropertyNotify:
892 char *name =
893 XGetAtomName(display, Event.xproperty.atom);
895 if (!name)
896 break;
898 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
900 XFree(name);
902 break;
903 case MapNotify:
904 x11->vo_hint.win_gravity = x11->old_gravity;
905 XSetWMNormalHints(display, x11->window, &x11->vo_hint);
906 x11->fs_flip = 0;
907 break;
908 case ClientMessage:
909 if (Event.xclient.message_type == x11->XAWM_PROTOCOLS &&
910 Event.xclient.data.l[0] == x11->XAWM_DELETE_WINDOW)
911 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
912 break;
915 return ret;
919 * \brief sets the size and position of the non-fullscreen window.
921 static void vo_x11_nofs_sizepos(struct vo *vo, int x, int y,
922 int width, int height)
924 struct vo_x11_state *x11 = vo->x11;
925 vo_x11_sizehint(vo, x, y, width, height, 0);
926 if (vo_fs) {
927 x11->vo_old_x = x;
928 x11->vo_old_y = y;
929 x11->vo_old_width = width;
930 x11->vo_old_height = height;
932 else
934 vo->dwidth = width;
935 vo->dheight = height;
936 XMoveResizeWindow(vo->x11->display, vo->x11->window, x, y, width, height);
940 void vo_x11_sizehint(struct vo *vo, int x, int y, int width, int height, int max)
942 struct vo_x11_state *x11 = vo->x11;
943 x11->vo_hint.flags = 0;
944 if (vo_keepaspect)
946 x11->vo_hint.flags |= PAspect;
947 x11->vo_hint.min_aspect.x = width;
948 x11->vo_hint.min_aspect.y = height;
949 x11->vo_hint.max_aspect.x = width;
950 x11->vo_hint.max_aspect.y = height;
953 x11->vo_hint.flags |= PPosition | PSize;
954 x11->vo_hint.x = x;
955 x11->vo_hint.y = y;
956 x11->vo_hint.width = width;
957 x11->vo_hint.height = height;
958 if (max)
960 x11->vo_hint.flags |= PMaxSize;
961 x11->vo_hint.max_width = width;
962 x11->vo_hint.max_height = height;
963 } else
965 x11->vo_hint.max_width = 0;
966 x11->vo_hint.max_height = 0;
969 // Set minimum height/width to 4 to avoid off-by-one errors
970 // and because mga_vid requires a minimal size of 4 pixels.
971 x11->vo_hint.flags |= PMinSize;
972 x11->vo_hint.min_width = x11->vo_hint.min_height = 4;
974 // Set the base size. A window manager might display the window
975 // size to the user relative to this.
976 // Setting these to width/height might be nice, but e.g. fluxbox can't handle it.
977 x11->vo_hint.flags |= PBaseSize;
978 x11->vo_hint.base_width = 0 /*width*/;
979 x11->vo_hint.base_height = 0 /*height*/;
981 x11->vo_hint.flags |= PWinGravity;
982 x11->vo_hint.win_gravity = StaticGravity;
983 XSetWMNormalHints(x11->display, x11->window, &x11->vo_hint);
986 static int vo_x11_get_gnome_layer(struct vo_x11_state *x11, Window win)
988 Atom type;
989 int format;
990 unsigned long nitems;
991 unsigned long bytesafter;
992 unsigned short *args = NULL;
994 if (XGetWindowProperty(x11->display, win, x11->XA_WIN_LAYER, 0, 16384,
995 False, AnyPropertyType, &type, &format, &nitems,
996 &bytesafter,
997 (unsigned char **) &args) == Success
998 && nitems > 0 && args)
1000 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
1001 *args);
1002 return *args;
1004 return WIN_LAYER_NORMAL;
1008 static Window vo_x11_create_smooth_window(struct vo_x11_state *x11, Window mRoot,
1009 Visual * vis, int x, int y,
1010 unsigned int width, unsigned int height,
1011 int depth, Colormap col_map)
1013 unsigned long xswamask = CWBorderPixel;
1014 XSetWindowAttributes xswa;
1015 Window ret_win;
1017 if (col_map != CopyFromParent)
1019 xswa.colormap = col_map;
1020 xswamask |= CWColormap;
1022 xswa.background_pixel = 0;
1023 xswa.border_pixel = 0;
1024 xswa.backing_store = NotUseful;
1025 xswa.bit_gravity = StaticGravity;
1027 ret_win =
1028 XCreateWindow(x11->display, x11->rootwin, x, y, width, height, 0, depth,
1029 CopyFromParent, vis, xswamask, &xswa);
1030 XSetWMProtocols(x11->display, ret_win, &x11->XAWM_DELETE_WINDOW, 1);
1031 if (!x11->f_gc)
1032 x11->f_gc = XCreateGC(x11->display, ret_win, 0, 0);
1033 XSetForeground(x11->display, x11->f_gc, 0);
1035 return ret_win;
1039 * \brief create and setup a window suitable for display
1040 * \param vis Visual to use for creating the window
1041 * \param x x position of window
1042 * \param y y position of window
1043 * \param width width of window
1044 * \param height height of window
1045 * \param flags flags for window creation.
1046 * Only VOFLAG_FULLSCREEN is supported so far.
1047 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1048 * \param classname name to use for the classhint
1049 * \param title title for the window
1051 * This also does the grunt-work like setting Window Manager hints etc.
1052 * If vo_window is already set it just moves and resizes it.
1054 void vo_x11_create_vo_window(struct vo *vo, XVisualInfo *vis, int x, int y,
1055 unsigned int width, unsigned int height, int flags,
1056 Colormap col_map,
1057 const char *classname, const char *title)
1059 struct MPOpts *opts = vo->opts;
1060 struct vo_x11_state *x11 = vo->x11;
1061 Display *mDisplay = vo->x11->display;
1062 XGCValues xgcv;
1063 if (WinID >= 0) {
1064 x11->window = WinID ? (Window)WinID : x11->rootwin;
1065 if (col_map != CopyFromParent) {
1066 unsigned long xswamask = CWColormap;
1067 XSetWindowAttributes xswa;
1068 xswa.colormap = col_map;
1069 XUnmapWindow(mDisplay, x11->window);
1070 XChangeWindowAttributes(mDisplay, x11->window, xswamask, &xswa);
1071 XMapWindow(mDisplay, x11->window);
1073 if (WinID) vo_x11_update_geometry(vo);
1074 vo_x11_selectinput_witherr(mDisplay, x11->window,
1075 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1076 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1077 goto final;
1079 if (x11->window == None) {
1080 XSizeHints hint;
1081 XEvent xev;
1082 vo_fs = 0;
1083 vo->dwidth = width;
1084 vo->dheight = height;
1085 x11->window = vo_x11_create_smooth_window(x11, x11->rootwin, vis->visual,
1086 x, y, width, height, vis->depth, col_map);
1087 vo_x11_classhint(vo, x11->window, classname);
1088 XStoreName(mDisplay, x11->window, title);
1089 vo_hidecursor(mDisplay, x11->window);
1090 XSelectInput(mDisplay, x11->window, StructureNotifyMask);
1091 hint.x = x; hint.y = y;
1092 hint.width = width; hint.height = height;
1093 hint.flags = PPosition | PSize;
1094 XSetStandardProperties(mDisplay, x11->window, title, title, None, NULL, 0, &hint);
1095 vo_x11_sizehint(vo, x, y, width, height, 0);
1096 if (!vo_border) vo_x11_decoration(vo, 0);
1097 // map window
1098 XMapWindow(mDisplay, x11->window);
1099 XClearWindow(mDisplay, x11->window);
1100 // wait for map
1101 do {
1102 XNextEvent(mDisplay, &xev);
1103 } while (xev.type != MapNotify || xev.xmap.event != x11->window);
1104 XSelectInput(mDisplay, x11->window, NoEventMask);
1105 XSync(mDisplay, False);
1106 vo_x11_selectinput_witherr(mDisplay, x11->window,
1107 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1108 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1110 if (opts->vo_ontop) vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1111 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, width, height);
1112 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1113 vo_x11_fullscreen(vo);
1114 else if (vo_fs) {
1115 // if we are already in fullscreen do not switch back and forth, just
1116 // set the size values right.
1117 vo->dwidth = vo->opts->vo_screenwidth;
1118 vo->dheight = vo->opts->vo_screenheight;
1120 final:
1121 if (x11->vo_gc != None)
1122 XFreeGC(mDisplay, x11->vo_gc);
1123 x11->vo_gc = XCreateGC(mDisplay, x11->window, GCForeground, &xgcv);
1124 XSync(mDisplay, False);
1125 x11->vo_mouse_autohide = 1;
1128 void vo_x11_clearwindow_part(struct vo *vo, Window vo_window,
1129 int img_width, int img_height, int use_fs)
1131 struct vo_x11_state *x11 = vo->x11;
1132 struct MPOpts *opts = vo->opts;
1133 Display *mDisplay = vo->x11->display;
1134 int u_dheight, u_dwidth, left_ov, left_ov2;
1136 if (!x11->f_gc)
1137 return;
1139 u_dheight = use_fs ? opts->vo_screenheight : vo->dheight;
1140 u_dwidth = use_fs ? opts->vo_screenwidth : vo->dwidth;
1141 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1142 return;
1144 left_ov = (u_dheight - img_height) / 2;
1145 left_ov2 = (u_dwidth - img_width) / 2;
1147 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, 0, u_dwidth, left_ov);
1148 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, u_dheight - left_ov - 1,
1149 u_dwidth, left_ov + 1);
1151 if (u_dwidth > img_width)
1153 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, left_ov, left_ov2,
1154 img_height);
1155 XFillRectangle(mDisplay, vo_window, x11->f_gc, u_dwidth - left_ov2 - 1,
1156 left_ov, left_ov2 + 1, img_height);
1159 XFlush(mDisplay);
1162 void vo_x11_clearwindow(struct vo *vo, Window vo_window)
1164 struct vo_x11_state *x11 = vo->x11;
1165 struct MPOpts *opts = vo->opts;
1166 if (!x11->f_gc)
1167 return;
1168 XFillRectangle(x11->display, vo_window, x11->f_gc, 0, 0,
1169 opts->vo_screenwidth, opts->vo_screenheight);
1171 XFlush(x11->display);
1175 void vo_x11_setlayer(struct vo *vo, Window vo_window, int layer)
1177 struct vo_x11_state *x11 = vo->x11;
1178 if (WinID >= 0)
1179 return;
1181 if (x11->fs_type & vo_wm_LAYER)
1183 XClientMessageEvent xev;
1185 if (!x11->orig_layer)
1186 x11->orig_layer = vo_x11_get_gnome_layer(x11, vo_window);
1188 memset(&xev, 0, sizeof(xev));
1189 xev.type = ClientMessage;
1190 xev.display = x11->display;
1191 xev.window = vo_window;
1192 xev.message_type = x11->XA_WIN_LAYER;
1193 xev.format = 32;
1194 xev.data.l[0] = layer ? fs_layer : x11->orig_layer; // if not fullscreen, stay on default layer
1195 xev.data.l[1] = CurrentTime;
1196 mp_msg(MSGT_VO, MSGL_V,
1197 "[x11] Layered style stay on top (layer %ld).\n",
1198 xev.data.l[0]);
1199 XSendEvent(x11->display, x11->rootwin, False, SubstructureNotifyMask,
1200 (XEvent *) & xev);
1201 } else if (x11->fs_type & vo_wm_NETWM)
1203 XClientMessageEvent xev;
1204 char *state;
1206 memset(&xev, 0, sizeof(xev));
1207 xev.type = ClientMessage;
1208 xev.message_type = x11->XA_NET_WM_STATE;
1209 xev.display = x11->display;
1210 xev.window = vo_window;
1211 xev.format = 32;
1212 xev.data.l[0] = layer;
1214 if (x11->fs_type & vo_wm_STAYS_ON_TOP)
1215 xev.data.l[1] = x11->XA_NET_WM_STATE_STAYS_ON_TOP;
1216 else if (x11->fs_type & vo_wm_ABOVE)
1217 xev.data.l[1] = x11->XA_NET_WM_STATE_ABOVE;
1218 else if (x11->fs_type & vo_wm_FULLSCREEN)
1219 xev.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
1220 else if (x11->fs_type & vo_wm_BELOW)
1221 // This is not fallback. We can safely assume that the situation
1222 // where only NETWM_STATE_BELOW is supported doesn't exist.
1223 xev.data.l[1] = x11->XA_NET_WM_STATE_BELOW;
1225 XSendEvent(x11->display, x11->rootwin, False, SubstructureRedirectMask,
1226 (XEvent *) & xev);
1227 state = XGetAtomName(x11->display, xev.data.l[1]);
1228 mp_msg(MSGT_VO, MSGL_V,
1229 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1230 layer, state);
1231 XFree(state);
1235 static int vo_x11_get_fs_type(int supported)
1237 int i;
1238 int type = supported;
1240 if (vo_fstype_list)
1242 i = 0;
1243 for (i = 0; vo_fstype_list[i]; i++)
1245 int neg = 0;
1246 char *arg = vo_fstype_list[i];
1248 if (vo_fstype_list[i][0] == '-')
1250 neg = 1;
1251 arg = vo_fstype_list[i] + 1;
1254 if (!strncmp(arg, "layer", 5))
1256 if (!neg && (arg[5] == '='))
1258 char *endptr = NULL;
1259 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1261 if (endptr && *endptr == '\0' && layer >= 0
1262 && layer <= 15)
1263 fs_layer = layer;
1265 if (neg)
1266 type &= ~vo_wm_LAYER;
1267 else
1268 type |= vo_wm_LAYER;
1269 } else if (!strcmp(arg, "above"))
1271 if (neg)
1272 type &= ~vo_wm_ABOVE;
1273 else
1274 type |= vo_wm_ABOVE;
1275 } else if (!strcmp(arg, "fullscreen"))
1277 if (neg)
1278 type &= ~vo_wm_FULLSCREEN;
1279 else
1280 type |= vo_wm_FULLSCREEN;
1281 } else if (!strcmp(arg, "stays_on_top"))
1283 if (neg)
1284 type &= ~vo_wm_STAYS_ON_TOP;
1285 else
1286 type |= vo_wm_STAYS_ON_TOP;
1287 } else if (!strcmp(arg, "below"))
1289 if (neg)
1290 type &= ~vo_wm_BELOW;
1291 else
1292 type |= vo_wm_BELOW;
1293 } else if (!strcmp(arg, "netwm"))
1295 if (neg)
1296 type &= ~vo_wm_NETWM;
1297 else
1298 type |= vo_wm_NETWM;
1299 } else if (!strcmp(arg, "none"))
1300 return 0;
1304 return type;
1308 * \brief update vo->dx, vo->dy, vo->dwidth and vo->dheight with current values of vo->x11->window
1309 * \return returns current color depth of vo->x11->window
1311 int vo_x11_update_geometry(struct vo *vo)
1313 struct vo_x11_state *x11 = vo->x11;
1314 unsigned depth, w, h;
1315 int dummy_int;
1316 Window dummy_win;
1317 XGetGeometry(x11->display, x11->window, &dummy_win, &dummy_int, &dummy_int,
1318 &w, &h, &dummy_int, &depth);
1319 if (w <= INT_MAX && h <= INT_MAX) {
1320 vo->dwidth = w;
1321 vo->dheight = h;
1323 XTranslateCoordinates(x11->display, x11->window, x11->rootwin, 0, 0,
1324 &vo->dx, &vo->dy, &dummy_win);
1325 return depth <= INT_MAX ? depth : 0;
1328 void vo_x11_fullscreen(struct vo *vo)
1330 struct MPOpts *opts = vo->opts;
1331 struct vo_x11_state *x11 = vo->x11;
1332 int x, y, w, h;
1334 if (WinID >= 0 || x11->fs_flip)
1335 return;
1337 if (vo_fs)
1339 // fs->win
1340 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1342 x = x11->vo_old_x;
1343 y = x11->vo_old_y;
1344 w = x11->vo_old_width;
1345 h = x11->vo_old_height;
1348 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1349 vo_fs = VO_FALSE;
1350 } else
1352 // win->fs
1353 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1355 vo_fs = VO_TRUE;
1356 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1358 x11->vo_old_x = vo->dx;
1359 x11->vo_old_y = vo->dy;
1360 x11->vo_old_width = vo->dwidth;
1361 x11->vo_old_height = vo->dheight;
1363 update_xinerama_info(vo);
1364 x = xinerama_x;
1365 y = xinerama_y;
1366 w = opts->vo_screenwidth;
1367 h = opts->vo_screenheight;
1370 long dummy;
1372 XGetWMNormalHints(x11->display, x11->window, &x11->vo_hint, &dummy);
1373 if (!(x11->vo_hint.flags & PWinGravity))
1374 x11->old_gravity = NorthWestGravity;
1375 else
1376 x11->old_gravity = x11->vo_hint.win_gravity;
1378 if (x11->wm_type == 0 && !(vo_fsmode & 16))
1380 XUnmapWindow(x11->display, x11->window); // required for MWM
1381 XWithdrawWindow(x11->display, x11->window, x11->screen);
1382 x11->fs_flip = 1;
1385 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1387 vo_x11_decoration(vo, vo_border && !vo_fs);
1388 vo_x11_sizehint(vo, x, y, w, h, 0);
1389 vo_x11_setlayer(vo, x11->window, vo_fs);
1392 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1394 /* some WMs lose ontop after fullscreen */
1395 if ((!(vo_fs)) & opts->vo_ontop)
1396 vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1398 XMapRaised(x11->display, x11->window);
1399 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1400 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1401 XRaiseWindow(x11->display, x11->window);
1402 XFlush(x11->display);
1405 void vo_x11_ontop(struct vo *vo)
1407 struct MPOpts *opts = vo->opts;
1408 opts->vo_ontop = !opts->vo_ontop;
1410 vo_x11_setlayer(vo, vo->x11->window, opts->vo_ontop);
1413 void vo_x11_border(struct vo *vo)
1415 vo_border = !vo_border;
1416 vo_x11_decoration(vo, vo_border && !vo_fs);
1420 * XScreensaver stuff
1423 static int screensaver_off;
1424 static unsigned int time_last;
1426 void xscreensaver_heartbeat(struct vo_x11_state *x11)
1428 unsigned int time = GetTimerMS();
1430 if (x11->display && screensaver_off && (time - time_last) > 30000)
1432 time_last = time;
1434 XResetScreenSaver(x11->display);
1438 static int xss_suspend(Display *mDisplay, Bool suspend)
1440 #ifndef CONFIG_XSS
1441 return 0;
1442 #else
1443 int event, error, major, minor;
1444 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1445 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1446 return 0;
1447 if (major < 1 || (major == 1 && minor < 1))
1448 return 0;
1449 XScreenSaverSuspend(mDisplay, suspend);
1450 return 1;
1451 #endif
1455 * End of XScreensaver stuff
1458 static void saver_on(Display * mDisplay)
1461 if (!screensaver_off)
1462 return;
1463 screensaver_off = 0;
1464 if (xss_suspend(mDisplay, False))
1465 return;
1466 #ifdef CONFIG_XDPMS
1467 if (dpms_disabled)
1469 int nothing;
1470 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1472 if (!DPMSEnable(mDisplay))
1473 { // restoring power saving settings
1474 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1475 } else
1477 // DPMS does not seem to be enabled unless we call DPMSInfo
1478 BOOL onoff;
1479 CARD16 state;
1481 DPMSForceLevel(mDisplay, DPMSModeOn);
1482 DPMSInfo(mDisplay, &state, &onoff);
1483 if (onoff)
1485 mp_msg(MSGT_VO, MSGL_V,
1486 "Successfully enabled DPMS\n");
1487 } else
1489 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1493 dpms_disabled = 0;
1495 #endif
1498 static void saver_off(Display * mDisplay)
1500 int nothing;
1502 if (screensaver_off)
1503 return;
1504 screensaver_off = 1;
1505 if (xss_suspend(mDisplay, True))
1506 return;
1507 #ifdef CONFIG_XDPMS
1508 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1510 BOOL onoff;
1511 CARD16 state;
1513 DPMSInfo(mDisplay, &state, &onoff);
1514 if (onoff)
1516 Status stat;
1518 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1519 dpms_disabled = 1;
1520 stat = DPMSDisable(mDisplay); // monitor powersave off
1521 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1524 #endif
1527 static XErrorHandler old_handler = NULL;
1528 static int selectinput_err = 0;
1529 static int x11_selectinput_errorhandler(Display * display,
1530 XErrorEvent * event)
1532 if (event->error_code == BadAccess)
1534 selectinput_err = 1;
1535 mp_msg(MSGT_VO, MSGL_ERR,
1536 "X11 error: BadAccess during XSelectInput Call\n");
1537 mp_msg(MSGT_VO, MSGL_ERR,
1538 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1539 /* If you think MPlayer should shutdown with this error,
1540 * comment out the following line */
1541 return 0;
1543 if (old_handler != NULL)
1544 old_handler(display, event);
1545 else
1546 x11_errorhandler(display, event);
1547 return 0;
1550 void vo_x11_selectinput_witherr(Display * display, Window w,
1551 long event_mask)
1553 XSync(display, False);
1554 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1555 selectinput_err = 0;
1556 if (vo_nomouse_input)
1558 XSelectInput(display, w,
1559 event_mask &
1560 (~(ButtonPressMask | ButtonReleaseMask)));
1561 } else
1563 XSelectInput(display, w, event_mask);
1565 XSync(display, False);
1566 XSetErrorHandler(old_handler);
1567 if (selectinput_err)
1569 mp_msg(MSGT_VO, MSGL_ERR,
1570 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1571 XSelectInput(display, w,
1572 event_mask &
1574 (ButtonPressMask | ButtonReleaseMask |
1575 PointerMotionMask)));
1579 #ifdef CONFIG_XF86VM
1580 void vo_vm_switch(struct vo *vo)
1582 struct vo_x11_state *x11 = vo->x11;
1583 struct MPOpts *opts = vo->opts;
1584 Display *mDisplay = x11->display;
1585 int vm_event, vm_error;
1586 int vm_ver, vm_rev;
1587 int i, j, have_vm = 0;
1588 int X = vo->dwidth, Y = vo->dheight;
1589 int modeline_width, modeline_height;
1591 int modecount;
1593 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1595 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1596 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1597 vm_rev);
1598 have_vm = 1;
1599 } else {
1600 mp_msg(MSGT_VO, MSGL_WARN,
1601 "XF86VidMode extension not available.\n");
1604 if (have_vm)
1606 if (vidmodes == NULL)
1607 XF86VidModeGetAllModeLines(mDisplay, x11->screen, &modecount,
1608 &vidmodes);
1609 j = 0;
1610 modeline_width = vidmodes[0]->hdisplay;
1611 modeline_height = vidmodes[0]->vdisplay;
1613 for (i = 1; i < modecount; i++)
1614 if ((vidmodes[i]->hdisplay >= X)
1615 && (vidmodes[i]->vdisplay >= Y))
1616 if ((vidmodes[i]->hdisplay <= modeline_width)
1617 && (vidmodes[i]->vdisplay <= modeline_height))
1619 modeline_width = vidmodes[i]->hdisplay;
1620 modeline_height = vidmodes[i]->vdisplay;
1621 j = i;
1624 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_SelectedVideoMode,
1625 modeline_width, modeline_height, X, Y);
1626 XF86VidModeLockModeSwitch(mDisplay, x11->screen, 0);
1627 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1628 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1630 // FIXME: all this is more of a hack than proper solution
1631 X = (opts->vo_screenwidth - modeline_width) / 2;
1632 Y = (opts->vo_screenheight - modeline_height) / 2;
1633 XF86VidModeSetViewPort(mDisplay, x11->screen, X, Y);
1634 vo->dx = X;
1635 vo->dy = Y;
1636 vo->dwidth = modeline_width;
1637 vo->dheight = modeline_height;
1638 aspect_save_screenres(vo, modeline_width, modeline_height);
1642 void vo_vm_close(struct vo *vo)
1644 Display *dpy = vo->x11->display;
1645 struct MPOpts *opts = vo->opts;
1646 #ifdef CONFIG_GUI
1647 if (vidmodes != NULL && vo->x11->vo_window != None)
1648 #else
1649 if (vidmodes != NULL)
1650 #endif
1652 int i, modecount;
1654 free(vidmodes);
1655 vidmodes = NULL;
1656 XF86VidModeGetAllModeLines(dpy, vo->x11->screen, &modecount,
1657 &vidmodes);
1658 for (i = 0; i < modecount; i++)
1659 if ((vidmodes[i]->hdisplay == opts->vo_screenwidth)
1660 && (vidmodes[i]->vdisplay == opts->vo_screenheight))
1662 mp_msg(MSGT_VO, MSGL_INFO,
1663 "Returning to original mode %dx%d\n",
1664 opts->vo_screenwidth, opts->vo_screenheight);
1665 break;
1668 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1669 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1670 free(vidmodes);
1671 vidmodes = NULL;
1674 #endif
1676 #endif /* X11_FULLSCREEN */
1680 * Scan the available visuals on this Display/Screen. Try to find
1681 * the 'best' available TrueColor visual that has a decent color
1682 * depth (at least 15bit). If there are multiple visuals with depth
1683 * >= 15bit, we prefer visuals with a smaller color depth.
1685 int vo_find_depth_from_visuals(Display * dpy, int screen,
1686 Visual ** visual_return)
1688 XVisualInfo visual_tmpl;
1689 XVisualInfo *visuals;
1690 int nvisuals, i;
1691 int bestvisual = -1;
1692 int bestvisual_depth = -1;
1694 visual_tmpl.screen = screen;
1695 visual_tmpl.class = TrueColor;
1696 visuals = XGetVisualInfo(dpy,
1697 VisualScreenMask | VisualClassMask,
1698 &visual_tmpl, &nvisuals);
1699 if (visuals != NULL)
1701 for (i = 0; i < nvisuals; i++)
1703 mp_msg(MSGT_VO, MSGL_V,
1704 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1705 visuals[i].visualid, visuals[i].depth,
1706 visuals[i].red_mask, visuals[i].green_mask,
1707 visuals[i].blue_mask);
1709 * Save the visual index and its depth, if this is the first
1710 * truecolor visul, or a visual that is 'preferred' over the
1711 * previous 'best' visual.
1713 if (bestvisual_depth == -1
1714 || (visuals[i].depth >= 15
1715 && (visuals[i].depth < bestvisual_depth
1716 || bestvisual_depth < 15)))
1718 bestvisual = i;
1719 bestvisual_depth = visuals[i].depth;
1723 if (bestvisual != -1 && visual_return != NULL)
1724 *visual_return = visuals[bestvisual].visual;
1726 XFree(visuals);
1728 return bestvisual_depth;
1732 static Colormap cmap = None;
1733 static XColor cols[256];
1734 static int cm_size, red_mask, green_mask, blue_mask;
1737 Colormap vo_x11_create_colormap(struct vo *vo, XVisualInfo *vinfo)
1739 struct vo_x11_state *x11 = vo->x11;
1740 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1742 if (vinfo->class != DirectColor)
1743 return XCreateColormap(x11->display, x11->rootwin, vinfo->visual,
1744 AllocNone);
1746 /* can this function get called twice or more? */
1747 if (cmap)
1748 return cmap;
1749 cm_size = vinfo->colormap_size;
1750 red_mask = vinfo->red_mask;
1751 green_mask = vinfo->green_mask;
1752 blue_mask = vinfo->blue_mask;
1753 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1754 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1755 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1756 rvu = 65536ull * ru / (red_mask + ru);
1757 gvu = 65536ull * gu / (green_mask + gu);
1758 bvu = 65536ull * bu / (blue_mask + bu);
1759 r = g = b = 0;
1760 rv = gv = bv = 0;
1761 m = DoRed | DoGreen | DoBlue;
1762 for (k = 0; k < cm_size; k++)
1764 int t;
1766 cols[k].pixel = r | g | b;
1767 cols[k].red = rv;
1768 cols[k].green = gv;
1769 cols[k].blue = bv;
1770 cols[k].flags = m;
1771 t = (r + ru) & red_mask;
1772 if (t < r)
1773 m &= ~DoRed;
1774 r = t;
1775 t = (g + gu) & green_mask;
1776 if (t < g)
1777 m &= ~DoGreen;
1778 g = t;
1779 t = (b + bu) & blue_mask;
1780 if (t < b)
1781 m &= ~DoBlue;
1782 b = t;
1783 rv += rvu;
1784 gv += gvu;
1785 bv += bvu;
1787 cmap = XCreateColormap(x11->display, x11->rootwin, vinfo->visual, AllocAll);
1788 XStoreColors(x11->display, cmap, cols, cm_size);
1789 return cmap;
1793 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1794 * hue and red/green/blue intensity, but we cannot do saturation.
1795 * Currently only gamma, brightness and contrast are implemented.
1796 * Is there sufficient interest for hue and/or red/green/blue intensity?
1798 /* these values have range [-100,100] and are initially 0 */
1799 static int vo_gamma = 0;
1800 static int vo_brightness = 0;
1801 static int vo_contrast = 0;
1803 static int transform_color(float val,
1804 float brightness, float contrast, float gamma) {
1805 float s = pow(val, gamma);
1806 s = (s - 0.5) * contrast + 0.5;
1807 s += brightness;
1808 if (s < 0)
1809 s = 0;
1810 if (s > 1)
1811 s = 1;
1812 return (unsigned short) (s * 65535);
1815 uint32_t vo_x11_set_equalizer(struct vo *vo, char *name, int value)
1817 float gamma, brightness, contrast;
1818 float rf, gf, bf;
1819 int k;
1822 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1823 * of TrueColor-ed window but be careful:
1824 * Unlike the colormaps, which are private for the X client
1825 * who created them and thus automatically destroyed on client
1826 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1827 * setting and _must_ be restored before the process exits.
1828 * Unforunately when the process crashes (or gets killed
1829 * for some reason) it is impossible to restore the setting,
1830 * and such behaviour could be rather annoying for the users.
1832 if (cmap == None)
1833 return VO_NOTAVAIL;
1835 if (!strcasecmp(name, "brightness"))
1836 vo_brightness = value;
1837 else if (!strcasecmp(name, "contrast"))
1838 vo_contrast = value;
1839 else if (!strcasecmp(name, "gamma"))
1840 vo_gamma = value;
1841 else
1842 return VO_NOTIMPL;
1844 brightness = 0.01 * vo_brightness;
1845 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1846 gamma = pow(2, -0.02 * vo_gamma);
1848 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1849 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
1850 green_mask;
1851 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
1853 /* now recalculate the colormap using the newly set value */
1854 for (k = 0; k < cm_size; k++)
1856 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
1857 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
1858 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
1861 XStoreColors(vo->x11->display, cmap, cols, cm_size);
1862 XFlush(vo->x11->display);
1863 return VO_TRUE;
1866 uint32_t vo_x11_get_equalizer(char *name, int *value)
1868 if (cmap == None)
1869 return VO_NOTAVAIL;
1870 if (!strcasecmp(name, "brightness"))
1871 *value = vo_brightness;
1872 else if (!strcasecmp(name, "contrast"))
1873 *value = vo_contrast;
1874 else if (!strcasecmp(name, "gamma"))
1875 *value = vo_gamma;
1876 else
1877 return VO_NOTIMPL;
1878 return VO_TRUE;
1881 #ifdef CONFIG_XV
1882 int vo_xv_set_eq(struct vo *vo, uint32_t xv_port, char *name, int value)
1884 XvAttribute *attributes;
1885 int i, howmany, xv_atom;
1887 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
1889 /* get available attributes */
1890 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1891 for (i = 0; i < howmany && attributes; i++)
1892 if (attributes[i].flags & XvSettable)
1894 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1895 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1896 then trigger it if it's ok so that the other values are at default upon query */
1897 if (xv_atom != None)
1899 int hue = 0, port_value, port_min, port_max;
1901 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1902 (!strcasecmp(name, "brightness")))
1903 port_value = value;
1904 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1905 (!strcasecmp(name, "contrast")))
1906 port_value = value;
1907 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1908 (!strcasecmp(name, "saturation")))
1909 port_value = value;
1910 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1911 (!strcasecmp(name, "hue")))
1913 port_value = value;
1914 hue = 1;
1915 } else
1916 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1917 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1918 (!strcasecmp(name, "red_intensity")))
1919 port_value = value;
1920 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1921 && (!strcasecmp(name, "green_intensity")))
1922 port_value = value;
1923 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1924 && (!strcasecmp(name, "blue_intensity")))
1925 port_value = value;
1926 else
1927 continue;
1929 port_min = attributes[i].min_value;
1930 port_max = attributes[i].max_value;
1932 /* nvidia hue workaround */
1933 if (hue && port_min == 0 && port_max == 360)
1935 port_value =
1936 (port_value >=
1937 0) ? (port_value - 100) : (port_value + 100);
1939 // -100 -> min
1940 // 0 -> (max+min)/2
1941 // +100 -> max
1942 port_value =
1943 (port_value + 100) * (port_max - port_min) / 200 +
1944 port_min;
1945 XvSetPortAttribute(vo->x11->display, xv_port, xv_atom, port_value);
1946 return VO_TRUE;
1949 return VO_FALSE;
1952 int vo_xv_get_eq(struct vo *vo, uint32_t xv_port, char *name, int *value)
1955 XvAttribute *attributes;
1956 int i, howmany, xv_atom;
1958 /* get available attributes */
1959 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1960 for (i = 0; i < howmany && attributes; i++)
1961 if (attributes[i].flags & XvGettable)
1963 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1964 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1965 then trigger it if it's ok so that the other values are at default upon query */
1966 if (xv_atom != None)
1968 int val, port_value = 0, port_min, port_max;
1970 XvGetPortAttribute(vo->x11->display, xv_port, xv_atom,
1971 &port_value);
1973 port_min = attributes[i].min_value;
1974 port_max = attributes[i].max_value;
1975 val =
1976 (port_value - port_min) * 200 / (port_max - port_min) -
1977 100;
1979 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1980 (!strcasecmp(name, "brightness")))
1981 *value = val;
1982 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1983 (!strcasecmp(name, "contrast")))
1984 *value = val;
1985 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1986 (!strcasecmp(name, "saturation")))
1987 *value = val;
1988 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1989 (!strcasecmp(name, "hue")))
1991 /* nasty nvidia detect */
1992 if (port_min == 0 && port_max == 360)
1993 *value = (val >= 0) ? (val - 100) : (val + 100);
1994 else
1995 *value = val;
1996 } else
1997 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1998 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1999 (!strcasecmp(name, "red_intensity")))
2000 *value = val;
2001 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2002 && (!strcasecmp(name, "green_intensity")))
2003 *value = val;
2004 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2005 && (!strcasecmp(name, "blue_intensity")))
2006 *value = val;
2007 else
2008 continue;
2010 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2011 name, *value);
2012 return VO_TRUE;
2015 return VO_FALSE;
2019 * \brief Interns the requested atom if it is available.
2021 * \param atom_name String containing the name of the requested atom.
2023 * \return Returns the atom if available, else None is returned.
2026 static Atom xv_intern_atom_if_exists(struct vo_x11_state *x11,
2027 char const *atom_name)
2029 XvAttribute * attributes;
2030 int attrib_count,i;
2031 Atom xv_atom = None;
2033 attributes = XvQueryPortAttributes(x11->display, x11->xv_port, &attrib_count );
2034 if( attributes!=NULL )
2036 for ( i = 0; i < attrib_count; ++i )
2038 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2040 xv_atom = XInternAtom(x11->display, atom_name, False );
2041 break; // found what we want, break out
2044 XFree( attributes );
2047 return xv_atom;
2051 * \brief Try to enable vsync for xv.
2052 * \return Returns -1 if not available, 0 on failure and 1 on success.
2054 int vo_xv_enable_vsync(struct vo *vo)
2056 struct vo_x11_state *x11 = vo->x11;
2057 Atom xv_atom = xv_intern_atom_if_exists(x11, "XV_SYNC_TO_VBLANK");
2058 if (xv_atom == None)
2059 return -1;
2060 return XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1) == Success;
2064 * \brief Get maximum supported source image dimensions.
2066 * This function does not set the variables pointed to by
2067 * width and height if the information could not be retrieved,
2068 * so the caller is reponsible for properly initializing them.
2070 * \param width [out] The maximum width gets stored here.
2071 * \param height [out] The maximum height gets stored here.
2074 void vo_xv_get_max_img_dim(struct vo *vo, uint32_t * width, uint32_t * height)
2076 struct vo_x11_state *x11 = vo->x11;
2077 XvEncodingInfo * encodings;
2078 //unsigned long num_encodings, idx; to int or too long?!
2079 unsigned int num_encodings, idx;
2081 XvQueryEncodings(x11->display, x11->xv_port, &num_encodings, &encodings);
2083 if ( encodings )
2085 for ( idx = 0; idx < num_encodings; ++idx )
2087 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2089 *width = encodings[idx].width;
2090 *height = encodings[idx].height;
2091 break;
2096 mp_msg( MSGT_VO, MSGL_V,
2097 "[xv common] Maximum source image dimensions: %ux%u\n",
2098 *width, *height );
2100 XvFreeEncodingInfo( encodings );
2104 * \brief Print information about the colorkey method and source.
2106 * \param ck_handling Integer value containing the information about
2107 * colorkey handling (see x11_common.h).
2109 * Outputs the content of |ck_handling| as a readable message.
2112 static void vo_xv_print_ck_info(struct vo_x11_state *x11)
2114 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2116 switch ( x11->xv_ck_info.method )
2118 case CK_METHOD_NONE:
2119 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2120 case CK_METHOD_AUTOPAINT:
2121 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2122 case CK_METHOD_MANUALFILL:
2123 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2124 case CK_METHOD_BACKGROUND:
2125 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2128 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2130 switch ( x11->xv_ck_info.source )
2132 case CK_SRC_CUR:
2133 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2134 x11->xv_colorkey );
2135 break;
2136 case CK_SRC_USE:
2137 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2139 mp_msg( MSGT_VO, MSGL_V,
2140 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2141 x11->xv_colorkey );
2143 else
2145 mp_msg( MSGT_VO, MSGL_V,
2146 "Using colorkey from MPlayer (0x%06lx)."
2147 " Use -colorkey to change.\n",
2148 x11->xv_colorkey );
2150 break;
2151 case CK_SRC_SET:
2152 mp_msg( MSGT_VO, MSGL_V,
2153 "Setting and using colorkey from MPlayer (0x%06lx)."
2154 " Use -colorkey to change.\n",
2155 x11->xv_colorkey );
2156 break;
2160 * \brief Init colorkey depending on the settings in xv_ck_info.
2162 * \return Returns 0 on failure and 1 on success.
2164 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2165 * flags in xv_ck_info.
2167 * Possiblilities:
2168 * * Methods
2169 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2170 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2171 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2172 * * Sources
2173 * - use currently set colorkey ( CK_SRC_CUR )
2174 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2175 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2177 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2178 * we don't draw anything as this means it was forced to off.
2180 int vo_xv_init_colorkey(struct vo *vo)
2182 struct vo_x11_state *x11 = vo->x11;
2183 Atom xv_atom;
2184 int rez;
2186 /* check if colorkeying is needed */
2187 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_COLORKEY");
2189 /* if we have to deal with colorkeying ... */
2190 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2192 /* check if we should use the colorkey specified in vo_colorkey */
2193 if ( x11->xv_ck_info.source != CK_SRC_CUR )
2195 x11->xv_colorkey = vo_colorkey;
2197 /* check if we have to set the colorkey too */
2198 if ( x11->xv_ck_info.source == CK_SRC_SET )
2200 xv_atom = XInternAtom(x11->display, "XV_COLORKEY",False);
2202 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, vo_colorkey);
2203 if ( rez != Success )
2205 mp_msg( MSGT_VO, MSGL_FATAL,
2206 "[xv common] Couldn't set colorkey!\n" );
2207 return 0; // error setting colorkey
2211 else
2213 int colorkey_ret;
2215 rez=XvGetPortAttribute(x11->display,x11->xv_port, xv_atom, &colorkey_ret);
2216 if ( rez == Success )
2218 x11->xv_colorkey = colorkey_ret;
2220 else
2222 mp_msg( MSGT_VO, MSGL_FATAL,
2223 "[xv common] Couldn't get colorkey!"
2224 "Maybe the selected Xv port has no overlay.\n" );
2225 return 0; // error getting colorkey
2229 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_AUTOPAINT_COLORKEY");
2231 /* should we draw the colorkey ourselves or activate autopainting? */
2232 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2234 rez = !Success; // reset rez to something different than Success
2236 if ( xv_atom != None ) // autopaint is supported
2238 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1);
2241 if ( rez != Success )
2243 // fallback to manual colorkey drawing
2244 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2247 else // disable colorkey autopainting if supported
2249 if ( xv_atom != None ) // we have autopaint attribute
2251 XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 0);
2255 else // do no colorkey drawing at all
2257 x11->xv_ck_info.method = CK_METHOD_NONE;
2258 } /* end: should we draw colorkey */
2260 /* output information about the current colorkey settings */
2261 vo_xv_print_ck_info(x11);
2263 return 1; // success
2267 * \brief Draw the colorkey on the video window.
2269 * Draws the colorkey depending on the set method ( colorkey_handling ).
2271 * Also draws the black bars ( when the video doesn't fit the display in
2272 * fullscreen ) separately, so they don't overlap with the video area.
2273 * It doesn't call XFlush.
2276 void vo_xv_draw_colorkey(struct vo *vo, int32_t x, int32_t y,
2277 int32_t w, int32_t h)
2279 struct MPOpts *opts = vo->opts;
2280 struct vo_x11_state *x11 = vo->x11;
2281 if( x11->xv_ck_info.method == CK_METHOD_MANUALFILL ||
2282 x11->xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2284 XSetForeground(x11->display, x11->vo_gc, x11->xv_colorkey );
2285 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2286 x, y,
2287 w, h );
2290 /* draw black bars if needed */
2291 /* TODO! move this to vo_x11_clearwindow_part() */
2292 if ( vo_fs )
2294 XSetForeground(x11->display, x11->vo_gc, 0 );
2295 /* making non-overlap fills, requires 8 checks instead of 4 */
2296 if ( y > 0 )
2297 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2298 0, 0,
2299 opts->vo_screenwidth, y);
2300 if (x > 0)
2301 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2302 0, 0,
2303 x, opts->vo_screenheight);
2304 if (x + w < opts->vo_screenwidth)
2305 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2306 x + w, 0,
2307 opts->vo_screenwidth, opts->vo_screenheight);
2308 if (y + h < opts->vo_screenheight)
2309 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2310 0, y + h,
2311 opts->vo_screenwidth, opts->vo_screenheight);
2315 /** \brief Tests if a valid argument for the ck suboption was given. */
2316 int xv_test_ck( void * arg )
2318 strarg_t * strarg = (strarg_t *)arg;
2320 if ( strargcmp( strarg, "use" ) == 0 ||
2321 strargcmp( strarg, "set" ) == 0 ||
2322 strargcmp( strarg, "cur" ) == 0 )
2324 return 1;
2327 return 0;
2329 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2330 int xv_test_ckm( void * arg )
2332 strarg_t * strarg = (strarg_t *)arg;
2334 if ( strargcmp( strarg, "bg" ) == 0 ||
2335 strargcmp( strarg, "man" ) == 0 ||
2336 strargcmp( strarg, "auto" ) == 0 )
2338 return 1;
2341 return 0;
2345 * \brief Modify the colorkey_handling var according to str
2347 * Checks if a valid pointer ( not NULL ) to the string
2348 * was given. And in that case modifies the colorkey_handling
2349 * var to reflect the requested behaviour.
2350 * If nothing happens the content of colorkey_handling stays
2351 * the same.
2353 * \param str Pointer to the string or NULL
2356 void xv_setup_colorkeyhandling(struct vo *vo, const char *ck_method_str,
2357 const char *ck_str)
2359 struct vo_x11_state *x11 = vo->x11;
2360 /* check if a valid pointer to the string was passed */
2361 if ( ck_str )
2363 if ( strncmp( ck_str, "use", 3 ) == 0 )
2365 x11->xv_ck_info.source = CK_SRC_USE;
2367 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2369 x11->xv_ck_info.source = CK_SRC_SET;
2372 /* check if a valid pointer to the string was passed */
2373 if ( ck_method_str )
2375 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2377 x11->xv_ck_info.method = CK_METHOD_BACKGROUND;
2379 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2381 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2383 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2385 x11->xv_ck_info.method = CK_METHOD_AUTOPAINT;
2390 #endif
2392 struct vo_x11_state *vo_x11_init_state(void)
2394 struct vo_x11_state *s = talloc_ptrtype(NULL, s);
2395 *s = (struct vo_x11_state){
2396 .xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR },
2397 .olddecor = MWM_DECOR_ALL,
2398 .oldfuncs = MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
2399 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE,
2400 .old_gravity = NorthWestGravity,
2402 return s;