Fix vf_tcdump's compilation
[mplayer/kovensky.git] / libvo / x11_common.c
blob4cd1c9e67e3c931acf607ac8b131f29030083ba8
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <math.h>
22 #include <inttypes.h>
23 #include <limits.h>
25 #include "config.h"
26 #include "options.h"
27 #include "mp_msg.h"
28 #include "mp_fifo.h"
29 #include "libavutil/common.h"
30 #include "x11_common.h"
31 #include "talloc.h"
33 #ifdef X11_FULLSCREEN
35 #include <string.h>
36 #include <unistd.h>
37 #include <assert.h>
39 #include "video_out.h"
40 #include "aspect.h"
41 #include "geometry.h"
42 #include "osdep/timer.h"
44 #include <X11/Xmd.h>
45 #include <X11/Xlib.h>
46 #include <X11/Xutil.h>
47 #include <X11/Xatom.h>
49 #ifdef CONFIG_XSS
50 #include <X11/extensions/scrnsaver.h>
51 #endif
53 #ifdef CONFIG_XDPMS
54 #include <X11/extensions/dpms.h>
55 #endif
57 #ifdef CONFIG_XINERAMA
58 #include <X11/extensions/Xinerama.h>
59 #endif
61 #ifdef CONFIG_XF86VM
62 #include <X11/extensions/xf86vmode.h>
63 #endif
65 #ifdef CONFIG_XF86XK
66 #include <X11/XF86keysym.h>
67 #endif
69 #ifdef CONFIG_XV
70 #include <X11/extensions/Xv.h>
71 #include <X11/extensions/Xvlib.h>
73 #include "subopt-helper.h"
74 #endif
76 #include "input/input.h"
77 #include "input/mouse.h"
79 #define WIN_LAYER_ONBOTTOM 2
80 #define WIN_LAYER_NORMAL 4
81 #define WIN_LAYER_ONTOP 6
82 #define WIN_LAYER_ABOVE_DOCK 10
84 extern int enable_mouse_movements;
85 int fs_layer = WIN_LAYER_ABOVE_DOCK;
87 int stop_xscreensaver = 0;
89 static int dpms_disabled = 0;
91 char *mDisplayName = NULL;
93 char **vo_fstype_list;
95 /* 1 means that the WM is metacity (broken as hell) */
96 int metacity_hack = 0;
98 #ifdef CONFIG_XF86VM
99 XF86VidModeModeInfo **vidmodes = NULL;
100 XF86VidModeModeLine modeline;
101 #endif
103 static int vo_x11_get_fs_type(int supported);
104 static void saver_off(Display *);
105 static void saver_on(Display *);
108 * Sends the EWMH fullscreen state event.
110 * action: could be one of _NET_WM_STATE_REMOVE -- remove state
111 * _NET_WM_STATE_ADD -- add state
112 * _NET_WM_STATE_TOGGLE -- toggle
114 void vo_x11_ewmh_fullscreen(struct vo_x11_state *x11, int action)
116 assert(action == _NET_WM_STATE_REMOVE ||
117 action == _NET_WM_STATE_ADD || action == _NET_WM_STATE_TOGGLE);
119 if (x11->fs_type & vo_wm_FULLSCREEN)
121 XEvent xev;
123 /* init X event structure for _NET_WM_FULLSCREEN client message */
124 xev.xclient.type = ClientMessage;
125 xev.xclient.serial = 0;
126 xev.xclient.send_event = True;
127 xev.xclient.message_type = x11->XA_NET_WM_STATE;
128 xev.xclient.window = x11->window;
129 xev.xclient.format = 32;
130 xev.xclient.data.l[0] = action;
131 xev.xclient.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
132 xev.xclient.data.l[2] = 0;
133 xev.xclient.data.l[3] = 0;
134 xev.xclient.data.l[4] = 0;
136 /* finally send that damn thing */
137 if (!XSendEvent(x11->display, DefaultRootWindow(x11->display), False,
138 SubstructureRedirectMask | SubstructureNotifyMask,
139 &xev))
141 mp_tmsg(MSGT_VO, MSGL_ERR, "\nX11: Couldn't send EWMH fullscreen event!\n");
146 static void vo_hidecursor(Display * disp, Window win)
148 Cursor no_ptr;
149 Pixmap bm_no;
150 XColor black, dummy;
151 Colormap colormap;
152 const char bm_no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
154 if (WinID == 0)
155 return; // do not hide if playing on the root window
157 colormap = DefaultColormap(disp, DefaultScreen(disp));
158 if ( !XAllocNamedColor(disp, colormap, "black", &black, &dummy) )
160 return; // color alloc failed, give up
162 bm_no = XCreateBitmapFromData(disp, win, bm_no_data, 8, 8);
163 no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
164 XDefineCursor(disp, win, no_ptr);
165 XFreeCursor(disp, no_ptr);
166 if (bm_no != None)
167 XFreePixmap(disp, bm_no);
168 XFreeColors(disp,colormap,&black.pixel,1,0);
171 static void vo_showcursor(Display * disp, Window win)
173 if (WinID == 0)
174 return;
175 XDefineCursor(disp, win, 0);
178 static int x11_errorhandler(Display * display, XErrorEvent * event)
180 #define MSGLEN 60
181 char msg[MSGLEN];
183 XGetErrorText(display, event->error_code, (char *) &msg, MSGLEN);
185 mp_msg(MSGT_VO, MSGL_ERR, "X11 error: %s\n", msg);
187 mp_msg(MSGT_VO, MSGL_V,
188 "Type: %x, display: %p, resourceid: %lx, serial: %lx\n",
189 event->type, event->display, event->resourceid, event->serial);
190 mp_msg(MSGT_VO, MSGL_V,
191 "Error code: %x, request code: %x, minor code: %x\n",
192 event->error_code, event->request_code, event->minor_code);
194 // abort();
195 //exit_player("X11 error");
196 return 0;
197 #undef MSGLEN
200 void fstype_help(void)
202 mp_tmsg(MSGT_VO, MSGL_INFO, "Available fullscreen layer change modes:\n");
203 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FULL_SCREEN_TYPES\n");
205 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "none",
206 "don't set fullscreen window layer");
207 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer",
208 "use _WIN_LAYER hint with default layer");
209 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer=<0..15>",
210 "use _WIN_LAYER hint with a given layer number");
211 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "netwm",
212 "force NETWM style");
213 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "above",
214 "use _NETWM_STATE_ABOVE hint if available");
215 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "below",
216 "use _NETWM_STATE_BELOW hint if available");
217 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "fullscreen",
218 "use _NETWM_STATE_FULLSCREEN hint if availale");
219 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "stays_on_top",
220 "use _NETWM_STATE_STAYS_ON_TOP hint if available");
221 mp_msg(MSGT_VO, MSGL_INFO,
222 "You can also negate the settings with simply putting '-' in the beginning");
223 mp_msg(MSGT_VO, MSGL_INFO, "\n");
226 static void fstype_dump(int fstype)
228 if (fstype)
230 mp_msg(MSGT_VO, MSGL_V, "[x11] Current fstype setting honours");
231 if (fstype & vo_wm_LAYER)
232 mp_msg(MSGT_VO, MSGL_V, " LAYER");
233 if (fstype & vo_wm_FULLSCREEN)
234 mp_msg(MSGT_VO, MSGL_V, " FULLSCREEN");
235 if (fstype & vo_wm_STAYS_ON_TOP)
236 mp_msg(MSGT_VO, MSGL_V, " STAYS_ON_TOP");
237 if (fstype & vo_wm_ABOVE)
238 mp_msg(MSGT_VO, MSGL_V, " ABOVE");
239 if (fstype & vo_wm_BELOW)
240 mp_msg(MSGT_VO, MSGL_V, " BELOW");
241 mp_msg(MSGT_VO, MSGL_V, " X atoms\n");
242 } else
243 mp_msg(MSGT_VO, MSGL_V,
244 "[x11] Current fstype setting doesn't honour any X atoms\n");
247 static int net_wm_support_state_test(struct vo_x11_state *x11, Atom atom)
249 #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; } }
251 NET_WM_STATE_TEST(FULLSCREEN);
252 NET_WM_STATE_TEST(ABOVE);
253 NET_WM_STATE_TEST(STAYS_ON_TOP);
254 NET_WM_STATE_TEST(BELOW);
255 return 0;
258 static int x11_get_property(struct vo_x11_state *x11, Atom type, Atom ** args,
259 unsigned long *nitems)
261 int format;
262 unsigned long bytesafter;
264 return Success ==
265 XGetWindowProperty(x11->display, x11->rootwin, type, 0, 16384, False,
266 AnyPropertyType, &type, &format, nitems,
267 &bytesafter, (unsigned char **) args)
268 && *nitems > 0;
271 static int vo_wm_detect(struct vo *vo)
273 struct vo_x11_state *x11 = vo->x11;
274 int i;
275 int wm = 0;
276 unsigned long nitems;
277 Atom *args = NULL;
279 if (WinID >= 0)
280 return 0;
282 // -- supports layers
283 if (x11_get_property(x11, x11->XA_WIN_PROTOCOLS, &args, &nitems))
285 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports layers.\n");
286 for (i = 0; i < nitems; i++)
288 if (args[i] == x11->XA_WIN_LAYER)
290 wm |= vo_wm_LAYER;
291 metacity_hack |= 1;
292 } else
293 /* metacity is the only window manager I know which reports
294 * supporting only the _WIN_LAYER hint in _WIN_PROTOCOLS.
295 * (what's more support for it is broken) */
296 metacity_hack |= 2;
298 XFree(args);
299 if (wm && (metacity_hack == 1))
301 // metacity claims to support layers, but it is not the truth :-)
302 wm ^= vo_wm_LAYER;
303 mp_msg(MSGT_VO, MSGL_V,
304 "[x11] Using workaround for Metacity bugs.\n");
307 // --- netwm
308 if (x11_get_property(x11, x11->XA_NET_SUPPORTED, &args, &nitems))
310 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports NetWM.\n");
311 for (i = 0; i < nitems; i++)
312 wm |= net_wm_support_state_test(vo->x11, args[i]);
313 XFree(args);
316 if (wm == 0)
317 mp_msg(MSGT_VO, MSGL_V, "[x11] Unknown wm type...\n");
318 return wm;
321 #define XA_INIT(x) x11->XA##x = XInternAtom(x11->display, #x, False)
322 static void init_atoms(struct vo_x11_state *x11)
324 XA_INIT(_NET_SUPPORTED);
325 XA_INIT(_NET_WM_STATE);
326 XA_INIT(_NET_WM_STATE_FULLSCREEN);
327 XA_INIT(_NET_WM_STATE_ABOVE);
328 XA_INIT(_NET_WM_STATE_STAYS_ON_TOP);
329 XA_INIT(_NET_WM_STATE_BELOW);
330 XA_INIT(_NET_WM_PID);
331 XA_INIT(_WIN_PROTOCOLS);
332 XA_INIT(_WIN_LAYER);
333 XA_INIT(_WIN_HINTS);
334 XA_INIT(WM_PROTOCOLS);
335 XA_INIT(WM_DELETE_WINDOW);
338 void update_xinerama_info(struct vo *vo) {
339 struct MPOpts *opts = vo->opts;
340 int screen = xinerama_screen;
341 xinerama_x = xinerama_y = 0;
342 #ifdef CONFIG_XINERAMA
343 if (screen >= -1 && XineramaIsActive(vo->x11->display))
345 XineramaScreenInfo *screens;
346 int num_screens;
348 screens = XineramaQueryScreens(vo->x11->display, &num_screens);
349 if (screen >= num_screens)
350 screen = num_screens - 1;
351 if (screen == -1) {
352 int x = vo->dx + vo->dwidth / 2;
353 int y = vo->dy + vo->dheight / 2;
354 for (screen = num_screens - 1; screen > 0; screen--) {
355 int left = screens[screen].x_org;
356 int right = left + screens[screen].width;
357 int top = screens[screen].y_org;
358 int bottom = top + screens[screen].height;
359 if (left <= x && x <= right && top <= y && y <= bottom)
360 break;
363 if (screen < 0)
364 screen = 0;
365 opts->vo_screenwidth = screens[screen].width;
366 opts->vo_screenheight = screens[screen].height;
367 xinerama_x = screens[screen].x_org;
368 xinerama_y = screens[screen].y_org;
370 XFree(screens);
372 #endif
373 aspect_save_screenres(vo, opts->vo_screenwidth, opts->vo_screenheight);
376 int vo_init(struct vo *vo)
378 struct MPOpts *opts = vo->opts;
379 struct vo_x11_state *x11 = vo->x11;
380 // int mScreen;
381 int depth, bpp;
382 unsigned int mask;
384 // char * DisplayName = ":0.0";
385 // Display * mDisplay;
386 XImage *mXImage = NULL;
388 // Window mRootWin;
389 XWindowAttributes attribs;
390 char *dispName;
392 if (vo_rootwin)
393 WinID = 0; // use root window
395 if (x11->depthonscreen)
397 saver_off(x11->display);
398 return 1; // already called
401 XSetErrorHandler(x11_errorhandler);
403 #if 0
404 if (!mDisplayName)
405 if (!(mDisplayName = getenv("DISPLAY")))
406 mDisplayName = strdup(":0.0");
407 #else
408 dispName = XDisplayName(mDisplayName);
409 #endif
411 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
413 x11->display = XOpenDisplay(dispName);
414 if (!x11->display)
416 mp_msg(MSGT_VO, MSGL_ERR,
417 "vo: couldn't open the X11 display (%s)!\n", dispName);
418 return 0;
420 x11->screen = DefaultScreen(x11->display); // screen ID
421 x11->rootwin = RootWindow(x11->display, x11->screen); // root window ID
423 init_atoms(vo->x11);
425 #ifdef CONFIG_XF86VM
427 int clock;
429 XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline);
430 if (!opts->vo_screenwidth)
431 opts->vo_screenwidth = modeline.hdisplay;
432 if (!opts->vo_screenheight)
433 opts->vo_screenheight = modeline.vdisplay;
435 #endif
437 if (!opts->vo_screenwidth)
438 opts->vo_screenwidth = DisplayWidth(x11->display, x11->screen);
439 if (!opts->vo_screenheight)
440 opts->vo_screenheight = DisplayHeight(x11->display, x11->screen);
442 // get color depth (from root window, or the best visual):
443 XGetWindowAttributes(x11->display, x11->rootwin, &attribs);
444 depth = attribs.depth;
446 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
448 Visual *visual;
450 depth = vo_find_depth_from_visuals(x11->display, x11->screen, &visual);
451 if (depth != -1)
452 mXImage = XCreateImage(x11->display, visual, depth, ZPixmap,
453 0, NULL, 1, 1, 8, 1);
454 } else
455 mXImage =
456 XGetImage(x11->display, x11->rootwin, 0, 0, 1, 1, AllPlanes, ZPixmap);
458 x11->depthonscreen = depth; // display depth on screen
460 // get bits/pixel from XImage structure:
461 if (mXImage == NULL)
463 mask = 0;
464 } else
467 * for the depth==24 case, the XImage structures might use
468 * 24 or 32 bits of data per pixel. The x11->depthonscreen
469 * field stores the amount of data per pixel in the
470 * XImage structure!
472 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
474 bpp = mXImage->bits_per_pixel;
475 if ((x11->depthonscreen + 7) / 8 != (bpp + 7) / 8)
476 x11->depthonscreen = bpp; // by A'rpi
477 mask =
478 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
479 mp_msg(MSGT_VO, MSGL_V,
480 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
481 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
482 XDestroyImage(mXImage);
484 if (((x11->depthonscreen + 7) / 8) == 2)
486 if (mask == 0x7FFF)
487 x11->depthonscreen = 15;
488 else if (mask == 0xFFFF)
489 x11->depthonscreen = 16;
491 // XCloseDisplay( mDisplay );
492 /* slightly improved local display detection AST */
493 if (strncmp(dispName, "unix:", 5) == 0)
494 dispName += 4;
495 else if (strncmp(dispName, "localhost:", 10) == 0)
496 dispName += 9;
497 if (*dispName == ':' && atoi(dispName + 1) < 10)
498 x11->display_is_local = 1;
499 else
500 x11->display_is_local = 0;
501 mp_msg(MSGT_VO, MSGL_V,
502 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
503 opts->vo_screenwidth, opts->vo_screenheight, depth, x11->depthonscreen,
504 dispName, x11->display_is_local ? "local" : "remote");
506 x11->wm_type = vo_wm_detect(vo);
508 x11->fs_type = vo_x11_get_fs_type(x11->wm_type);
510 fstype_dump(x11->fs_type);
512 saver_off(x11->display);
513 return 1;
516 void vo_uninit(struct vo_x11_state *x11)
518 if (!x11->display)
520 mp_msg(MSGT_VO, MSGL_V,
521 "vo: x11 uninit called but X11 not initialized..\n");
522 } else {
523 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
524 XSetErrorHandler(NULL);
525 XCloseDisplay(x11->display);
526 x11->depthonscreen = 0;
527 x11->display = NULL;
529 talloc_free(x11);
532 #include "osdep/keycodes.h"
533 #include "wskeys.h"
535 #ifdef XF86XK_AudioPause
536 static const struct keymap keysym_map[] = {
537 {XF86XK_MenuKB, KEY_MENU},
538 {XF86XK_AudioPlay, KEY_PLAY}, {XF86XK_AudioPause, KEY_PAUSE}, {XF86XK_AudioStop, KEY_STOP},
539 {XF86XK_AudioPrev, KEY_PREV}, {XF86XK_AudioNext, KEY_NEXT},
540 {XF86XK_AudioMute, KEY_MUTE}, {XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN}, {XF86XK_AudioRaiseVolume, KEY_VOLUME_UP},
541 {0, 0}
544 static void vo_x11_putkey_ext(struct vo *vo, int keysym)
546 struct mp_fifo *f = vo->key_fifo;
547 int mpkey = lookup_keymap_table(keysym_map, keysym);
548 if (mpkey)
549 mplayer_put_key(f, mpkey);
551 #endif
553 static const struct keymap keymap[] = {
554 // special keys
555 {wsEscape, KEY_ESC}, {wsBackSpace, KEY_BS}, {wsTab, KEY_TAB}, {wsEnter, KEY_ENTER},
557 // cursor keys
558 {wsLeft, KEY_LEFT}, {wsRight, KEY_RIGHT}, {wsUp, KEY_UP}, {wsDown, KEY_DOWN},
560 // navigation block
561 {wsInsert, KEY_INSERT}, {wsDelete, KEY_DELETE}, {wsHome, KEY_HOME}, {wsEnd, KEY_END},
562 {wsPageUp, KEY_PAGE_UP}, {wsPageDown, KEY_PAGE_DOWN},
564 // F-keys
565 {wsF1, KEY_F+1}, {wsF2, KEY_F+2}, {wsF3, KEY_F+3}, {wsF4, KEY_F+4},
566 {wsF5, KEY_F+5}, {wsF6, KEY_F+6}, {wsF7, KEY_F+7}, {wsF8, KEY_F+8},
567 {wsF9, KEY_F+9}, {wsF10, KEY_F+10}, {wsF11, KEY_F+11}, {wsF12, KEY_F+12},
569 // numpad independent of numlock
570 {wsGrayMinus, '-'}, {wsGrayPlus, '+'}, {wsGrayMul, '*'}, {wsGrayDiv, '/'},
571 {wsGrayEnter, KEY_KPENTER},
573 // numpad with numlock
574 {wsGray0, KEY_KP0}, {wsGray1, KEY_KP1}, {wsGray2, KEY_KP2},
575 {wsGray3, KEY_KP3}, {wsGray4, KEY_KP4}, {wsGray5, KEY_KP5},
576 {wsGray6, KEY_KP6}, {wsGray7, KEY_KP7}, {wsGray8, KEY_KP8},
577 {wsGray9, KEY_KP9}, {wsGrayDecimal, KEY_KPDEC},
579 // numpad without numlock
580 {wsGrayInsert, KEY_KPINS}, {wsGrayEnd, KEY_KP1}, {wsGrayDown, KEY_KP2},
581 {wsGrayPgDn, KEY_KP3}, {wsGrayLeft, KEY_KP4}, {wsGray5Dup, KEY_KP5},
582 {wsGrayRight, KEY_KP6}, {wsGrayHome, KEY_KP7}, {wsGrayUp, KEY_KP8},
583 {wsGrayPgUp, KEY_KP9}, {wsGrayDelete, KEY_KPDEL},
585 {0, 0}
588 void vo_x11_putkey(struct vo *vo, int key)
590 static const char *passthrough_keys = " -+*/<>`~!@#$%^&()_{}:;\"\',.?\\|=[]";
591 int mpkey = 0;
592 if ((key >= 'a' && key <= 'z') ||
593 (key >= 'A' && key <= 'Z') ||
594 (key >= '0' && key <= '9') ||
595 (key > 0 && key < 256 && strchr(passthrough_keys, key)))
596 mpkey = key;
598 if (!mpkey)
599 mpkey = lookup_keymap_table(keymap, key);
601 if (mpkey)
602 mplayer_put_key(vo->key_fifo, mpkey);
606 // ----- Motif header: -------
608 #define MWM_HINTS_FUNCTIONS (1L << 0)
609 #define MWM_HINTS_DECORATIONS (1L << 1)
610 #define MWM_HINTS_INPUT_MODE (1L << 2)
611 #define MWM_HINTS_STATUS (1L << 3)
613 #define MWM_FUNC_ALL (1L << 0)
614 #define MWM_FUNC_RESIZE (1L << 1)
615 #define MWM_FUNC_MOVE (1L << 2)
616 #define MWM_FUNC_MINIMIZE (1L << 3)
617 #define MWM_FUNC_MAXIMIZE (1L << 4)
618 #define MWM_FUNC_CLOSE (1L << 5)
620 #define MWM_DECOR_ALL (1L << 0)
621 #define MWM_DECOR_BORDER (1L << 1)
622 #define MWM_DECOR_RESIZEH (1L << 2)
623 #define MWM_DECOR_TITLE (1L << 3)
624 #define MWM_DECOR_MENU (1L << 4)
625 #define MWM_DECOR_MINIMIZE (1L << 5)
626 #define MWM_DECOR_MAXIMIZE (1L << 6)
628 #define MWM_INPUT_MODELESS 0
629 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
630 #define MWM_INPUT_SYSTEM_MODAL 2
631 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
632 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
634 #define MWM_TEAROFF_WINDOW (1L<<0)
636 typedef struct
638 long flags;
639 long functions;
640 long decorations;
641 long input_mode;
642 long state;
643 } MotifWmHints;
645 static MotifWmHints vo_MotifWmHints;
646 static Atom vo_MotifHints = None;
648 void vo_x11_decoration(struct vo *vo, int d)
650 struct vo_x11_state *x11 = vo->x11;
651 Atom mtype;
652 int mformat;
653 unsigned long mn, mb;
655 if (!WinID)
656 return;
658 if (vo_fsmode & 8)
660 XSetTransientForHint(x11->display, x11->window,
661 RootWindow(x11->display, x11->screen));
664 vo_MotifHints = XInternAtom(x11->display, "_MOTIF_WM_HINTS", 0);
665 if (vo_MotifHints != None)
667 if (!d)
669 MotifWmHints *mhints = NULL;
671 XGetWindowProperty(x11->display, x11->window,
672 vo_MotifHints, 0, 20, False,
673 vo_MotifHints, &mtype, &mformat, &mn,
674 &mb, (unsigned char **) &mhints);
675 if (mhints)
677 if (mhints->flags & MWM_HINTS_DECORATIONS)
678 x11->olddecor = mhints->decorations;
679 if (mhints->flags & MWM_HINTS_FUNCTIONS)
680 x11->oldfuncs = mhints->functions;
681 XFree(mhints);
685 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
686 vo_MotifWmHints.flags =
687 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
688 if (d)
690 vo_MotifWmHints.functions = x11->oldfuncs;
691 d = x11->olddecor;
693 #if 0
694 vo_MotifWmHints.decorations =
695 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
696 #else
697 vo_MotifWmHints.decorations =
698 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
699 #endif
700 XChangeProperty(x11->display, x11->window, vo_MotifHints,
701 vo_MotifHints, 32,
702 PropModeReplace,
703 (unsigned char *) &vo_MotifWmHints,
704 (vo_fsmode & 4) ? 4 : 5);
708 void vo_x11_classhint(struct vo *vo, Window window, const char *name)
710 struct vo_x11_state *x11 = vo->x11;
711 XClassHint wmClass;
712 pid_t pid = getpid();
714 wmClass.res_name = vo_winname ? vo_winname : name;
715 wmClass.res_class = "MPlayer";
716 XSetClassHint(x11->display, window, &wmClass);
717 XChangeProperty(x11->display, window, x11->XA_NET_WM_PID, XA_CARDINAL,
718 32, PropModeReplace, (unsigned char *) &pid, 1);
721 void vo_x11_uninit(struct vo *vo)
723 struct vo_x11_state *x11 = vo->x11;
724 saver_on(x11->display);
725 if (x11->window != None)
726 vo_showcursor(x11->display, x11->window);
728 if (x11->f_gc)
730 XFreeGC(vo->x11->display, x11->f_gc);
731 x11->f_gc = NULL;
734 if (x11->vo_gc)
736 XSetBackground(vo->x11->display, x11->vo_gc, 0);
737 XFreeGC(vo->x11->display, x11->vo_gc);
738 x11->vo_gc = NULL;
740 if (x11->window != None)
742 XClearWindow(x11->display, x11->window);
743 if (WinID < 0)
745 XEvent xev;
747 XUnmapWindow(x11->display, x11->window);
748 XSelectInput(x11->display, x11->window, StructureNotifyMask);
749 XDestroyWindow(x11->display, x11->window);
752 XNextEvent(x11->display, &xev);
754 while (xev.type != DestroyNotify
755 || xev.xdestroywindow.event != x11->window);
757 x11->window = None;
759 vo_fs = 0;
760 x11->vo_old_width = x11->vo_old_height = 0;
764 int vo_x11_check_events(struct vo *vo)
766 struct vo_x11_state *x11 = vo->x11;
767 Display *display = vo->x11->display;
768 int ret = 0;
769 XEvent Event;
770 char buf[100];
771 KeySym keySym;
773 // unsigned long vo_KeyTable[512];
775 if ((x11->vo_mouse_autohide) && x11->mouse_waiting_hide &&
776 (GetTimerMS() - x11->mouse_timer >= 1000)) {
777 vo_hidecursor(display, x11->window);
778 x11->mouse_waiting_hide = 0;
781 while (XPending(display))
783 XNextEvent(display, &Event);
784 // printf("\rEvent.type=%X \n",Event.type);
785 switch (Event.type)
787 case Expose:
788 ret |= VO_EVENT_EXPOSE;
789 break;
790 case ConfigureNotify:
791 if (x11->window == None)
792 break;
794 int old_w = vo->dwidth, old_h = vo->dheight;
795 vo_x11_update_geometry(vo);
796 if (vo->dwidth != old_w || vo->dheight != old_h)
797 ret |= VO_EVENT_RESIZE;
799 break;
800 case KeyPress:
802 int key;
804 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
805 &x11->compose_status);
806 #ifdef XF86XK_AudioPause
807 vo_x11_putkey_ext(vo, keySym);
808 #endif
809 key =
810 ((keySym & 0xff00) !=
811 0 ? ((keySym & 0x00ff) + 256) : (keySym));
812 vo_x11_putkey(vo, key);
813 ret |= VO_EVENT_KEYPRESS;
815 break;
816 case MotionNotify:
817 if(enable_mouse_movements)
819 char cmd_str[40];
820 sprintf(cmd_str,"set_mouse_pos %i %i",Event.xmotion.x, Event.xmotion.y);
821 mp_input_queue_cmd(vo->input_ctx,
822 mp_input_parse_cmd(cmd_str));
825 if (x11->vo_mouse_autohide)
827 vo_showcursor(display, x11->window);
828 x11->mouse_waiting_hide = 1;
829 x11->mouse_timer = GetTimerMS();
831 break;
832 case ButtonPress:
833 if (x11->vo_mouse_autohide)
835 vo_showcursor(display, x11->window);
836 x11->mouse_waiting_hide = 1;
837 x11->mouse_timer = GetTimerMS();
839 mplayer_put_key(vo->key_fifo,
840 (MOUSE_BTN0 + Event.xbutton.button - 1)
841 | MP_KEY_DOWN);
842 break;
843 case ButtonRelease:
844 if (x11->vo_mouse_autohide)
846 vo_showcursor(display, x11->window);
847 x11->mouse_waiting_hide = 1;
848 x11->mouse_timer = GetTimerMS();
850 mplayer_put_key(vo->key_fifo,
851 MOUSE_BTN0 + Event.xbutton.button - 1);
852 break;
853 case PropertyNotify:
855 char *name =
856 XGetAtomName(display, Event.xproperty.atom);
858 if (!name)
859 break;
861 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
863 XFree(name);
865 break;
866 case MapNotify:
867 x11->vo_hint.win_gravity = x11->old_gravity;
868 XSetWMNormalHints(display, x11->window, &x11->vo_hint);
869 x11->fs_flip = 0;
870 break;
871 case ClientMessage:
872 if (Event.xclient.message_type == x11->XAWM_PROTOCOLS &&
873 Event.xclient.data.l[0] == x11->XAWM_DELETE_WINDOW)
874 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
875 break;
878 return ret;
882 * \brief sets the size and position of the non-fullscreen window.
884 static void vo_x11_nofs_sizepos(struct vo *vo, int x, int y,
885 int width, int height)
887 struct vo_x11_state *x11 = vo->x11;
888 vo_x11_sizehint(vo, x, y, width, height, 0);
889 if (vo_fs) {
890 x11->vo_old_x = x;
891 x11->vo_old_y = y;
892 x11->vo_old_width = width;
893 x11->vo_old_height = height;
895 else
897 vo->dwidth = width;
898 vo->dheight = height;
899 if (vo->opts->force_window_position)
900 XMoveResizeWindow(vo->x11->display, vo->x11->window, x, y, width,
901 height);
902 else
903 XResizeWindow(vo->x11->display, vo->x11->window, width, height);
907 void vo_x11_sizehint(struct vo *vo, int x, int y, int width, int height, int max)
909 struct vo_x11_state *x11 = vo->x11;
910 x11->vo_hint.flags = 0;
911 if (vo_keepaspect)
913 x11->vo_hint.flags |= PAspect;
914 x11->vo_hint.min_aspect.x = width;
915 x11->vo_hint.min_aspect.y = height;
916 x11->vo_hint.max_aspect.x = width;
917 x11->vo_hint.max_aspect.y = height;
920 x11->vo_hint.flags |= PPosition | PSize;
921 x11->vo_hint.x = x;
922 x11->vo_hint.y = y;
923 x11->vo_hint.width = width;
924 x11->vo_hint.height = height;
925 if (max)
927 x11->vo_hint.flags |= PMaxSize;
928 x11->vo_hint.max_width = width;
929 x11->vo_hint.max_height = height;
930 } else
932 x11->vo_hint.max_width = 0;
933 x11->vo_hint.max_height = 0;
936 // Set minimum height/width to 4 to avoid off-by-one errors
937 // and because mga_vid requires a minimal size of 4 pixels.
938 x11->vo_hint.flags |= PMinSize;
939 x11->vo_hint.min_width = x11->vo_hint.min_height = 4;
941 // Set the base size. A window manager might display the window
942 // size to the user relative to this.
943 // Setting these to width/height might be nice, but e.g. fluxbox can't handle it.
944 x11->vo_hint.flags |= PBaseSize;
945 x11->vo_hint.base_width = 0 /*width*/;
946 x11->vo_hint.base_height = 0 /*height*/;
948 x11->vo_hint.flags |= PWinGravity;
949 x11->vo_hint.win_gravity = StaticGravity;
950 XSetWMNormalHints(x11->display, x11->window, &x11->vo_hint);
953 static int vo_x11_get_gnome_layer(struct vo_x11_state *x11, Window win)
955 Atom type;
956 int format;
957 unsigned long nitems;
958 unsigned long bytesafter;
959 unsigned short *args = NULL;
961 if (XGetWindowProperty(x11->display, win, x11->XA_WIN_LAYER, 0, 16384,
962 False, AnyPropertyType, &type, &format, &nitems,
963 &bytesafter,
964 (unsigned char **) &args) == Success
965 && nitems > 0 && args)
967 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
968 *args);
969 return *args;
971 return WIN_LAYER_NORMAL;
975 static Window vo_x11_create_smooth_window(struct vo_x11_state *x11, Window mRoot,
976 Visual * vis, int x, int y,
977 unsigned int width, unsigned int height,
978 int depth, Colormap col_map)
980 unsigned long xswamask = CWBorderPixel;
981 XSetWindowAttributes xswa;
982 Window ret_win;
984 if (col_map != CopyFromParent)
986 xswa.colormap = col_map;
987 xswamask |= CWColormap;
989 xswa.background_pixel = 0;
990 xswa.border_pixel = 0;
991 xswa.backing_store = NotUseful;
992 xswa.bit_gravity = StaticGravity;
994 ret_win =
995 XCreateWindow(x11->display, x11->rootwin, x, y, width, height, 0, depth,
996 CopyFromParent, vis, xswamask, &xswa);
997 XSetWMProtocols(x11->display, ret_win, &x11->XAWM_DELETE_WINDOW, 1);
998 if (!x11->f_gc)
999 x11->f_gc = XCreateGC(x11->display, ret_win, 0, 0);
1000 XSetForeground(x11->display, x11->f_gc, 0);
1002 return ret_win;
1006 * \brief create and setup a window suitable for display
1007 * \param vis Visual to use for creating the window
1008 * \param x x position of window
1009 * \param y y position of window
1010 * \param width width of window
1011 * \param height height of window
1012 * \param flags flags for window creation.
1013 * Only VOFLAG_FULLSCREEN is supported so far.
1014 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1015 * \param classname name to use for the classhint
1016 * \param title title for the window
1018 * This also does the grunt-work like setting Window Manager hints etc.
1019 * If vo_window is already set it just moves and resizes it.
1021 void vo_x11_create_vo_window(struct vo *vo, XVisualInfo *vis, int x, int y,
1022 unsigned int width, unsigned int height, int flags,
1023 Colormap col_map,
1024 const char *classname, const char *title)
1026 struct MPOpts *opts = vo->opts;
1027 struct vo_x11_state *x11 = vo->x11;
1028 Display *mDisplay = vo->x11->display;
1029 XGCValues xgcv;
1030 if (WinID >= 0) {
1031 vo_fs = flags & VOFLAG_FULLSCREEN;
1032 x11->window = WinID ? (Window)WinID : x11->rootwin;
1033 if (col_map != CopyFromParent) {
1034 unsigned long xswamask = CWColormap;
1035 XSetWindowAttributes xswa;
1036 xswa.colormap = col_map;
1037 XChangeWindowAttributes(mDisplay, x11->window, xswamask, &xswa);
1038 XInstallColormap(mDisplay, col_map);
1040 if (WinID) vo_x11_update_geometry(vo);
1041 vo_x11_selectinput_witherr(mDisplay, x11->window,
1042 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1043 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1044 goto final;
1046 if (x11->window == None) {
1047 vo_fs = 0;
1048 vo->dwidth = width;
1049 vo->dheight = height;
1050 x11->window = vo_x11_create_smooth_window(x11, x11->rootwin, vis->visual,
1051 x, y, width, height, vis->depth, col_map);
1052 x11->window_state = VOFLAG_HIDDEN;
1054 if (flags & VOFLAG_HIDDEN)
1055 goto final;
1056 if (x11->window_state & VOFLAG_HIDDEN) {
1057 XSizeHints hint;
1058 XEvent xev;
1059 x11->window_state &= ~VOFLAG_HIDDEN;
1060 vo_x11_classhint(vo, x11->window, classname);
1061 XStoreName(mDisplay, x11->window, title);
1062 vo_hidecursor(mDisplay, x11->window);
1063 XSelectInput(mDisplay, x11->window, StructureNotifyMask);
1064 hint.x = x; hint.y = y;
1065 hint.width = width; hint.height = height;
1066 hint.flags = PPosition | PSize;
1067 XSetStandardProperties(mDisplay, x11->window, title, title, None, NULL, 0, &hint);
1068 if (!vo_border) vo_x11_decoration(vo, 0);
1069 // map window
1070 XMapWindow(mDisplay, x11->window);
1071 vo_x11_clearwindow(vo, x11->window);
1072 // wait for map
1073 do {
1074 XNextEvent(mDisplay, &xev);
1075 } while (xev.type != MapNotify || xev.xmap.event != x11->window);
1076 XSelectInput(mDisplay, x11->window, NoEventMask);
1077 XSync(mDisplay, False);
1078 vo_x11_selectinput_witherr(mDisplay, x11->window,
1079 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1080 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1082 if (opts->vo_ontop) vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1083 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, width, height);
1084 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1085 vo_x11_fullscreen(vo);
1086 else if (vo_fs) {
1087 // if we are already in fullscreen do not switch back and forth, just
1088 // set the size values right.
1089 vo->dwidth = vo->opts->vo_screenwidth;
1090 vo->dheight = vo->opts->vo_screenheight;
1092 final:
1093 if (x11->vo_gc != None)
1094 XFreeGC(mDisplay, x11->vo_gc);
1095 x11->vo_gc = XCreateGC(mDisplay, x11->window, GCForeground, &xgcv);
1096 XSync(mDisplay, False);
1097 x11->vo_mouse_autohide = 1;
1100 void vo_x11_clearwindow_part(struct vo *vo, Window vo_window,
1101 int img_width, int img_height, int use_fs)
1103 struct vo_x11_state *x11 = vo->x11;
1104 struct MPOpts *opts = vo->opts;
1105 Display *mDisplay = vo->x11->display;
1106 int u_dheight, u_dwidth, left_ov, left_ov2;
1108 if (!x11->f_gc)
1109 return;
1111 u_dheight = use_fs ? opts->vo_screenheight : vo->dheight;
1112 u_dwidth = use_fs ? opts->vo_screenwidth : vo->dwidth;
1113 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1114 return;
1116 left_ov = (u_dheight - img_height) / 2;
1117 left_ov2 = (u_dwidth - img_width) / 2;
1119 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, 0, u_dwidth, left_ov);
1120 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, u_dheight - left_ov - 1,
1121 u_dwidth, left_ov + 1);
1123 if (u_dwidth > img_width)
1125 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, left_ov, left_ov2,
1126 img_height);
1127 XFillRectangle(mDisplay, vo_window, x11->f_gc, u_dwidth - left_ov2 - 1,
1128 left_ov, left_ov2 + 1, img_height);
1131 XFlush(mDisplay);
1134 void vo_x11_clearwindow(struct vo *vo, Window vo_window)
1136 struct vo_x11_state *x11 = vo->x11;
1137 struct MPOpts *opts = vo->opts;
1138 if (!x11->f_gc)
1139 return;
1140 XFillRectangle(x11->display, vo_window, x11->f_gc, 0, 0,
1141 opts->vo_screenwidth, opts->vo_screenheight);
1143 XFlush(x11->display);
1147 void vo_x11_setlayer(struct vo *vo, Window vo_window, int layer)
1149 struct vo_x11_state *x11 = vo->x11;
1150 if (WinID >= 0)
1151 return;
1153 if (x11->fs_type & vo_wm_LAYER)
1155 XClientMessageEvent xev;
1157 if (!x11->orig_layer)
1158 x11->orig_layer = vo_x11_get_gnome_layer(x11, vo_window);
1160 memset(&xev, 0, sizeof(xev));
1161 xev.type = ClientMessage;
1162 xev.display = x11->display;
1163 xev.window = vo_window;
1164 xev.message_type = x11->XA_WIN_LAYER;
1165 xev.format = 32;
1166 xev.data.l[0] = layer ? fs_layer : x11->orig_layer; // if not fullscreen, stay on default layer
1167 xev.data.l[1] = CurrentTime;
1168 mp_msg(MSGT_VO, MSGL_V,
1169 "[x11] Layered style stay on top (layer %ld).\n",
1170 xev.data.l[0]);
1171 XSendEvent(x11->display, x11->rootwin, False, SubstructureNotifyMask,
1172 (XEvent *) & xev);
1173 } else if (x11->fs_type & vo_wm_NETWM)
1175 XClientMessageEvent xev;
1176 char *state;
1178 memset(&xev, 0, sizeof(xev));
1179 xev.type = ClientMessage;
1180 xev.message_type = x11->XA_NET_WM_STATE;
1181 xev.display = x11->display;
1182 xev.window = vo_window;
1183 xev.format = 32;
1184 xev.data.l[0] = layer;
1186 if (x11->fs_type & vo_wm_STAYS_ON_TOP)
1187 xev.data.l[1] = x11->XA_NET_WM_STATE_STAYS_ON_TOP;
1188 else if (x11->fs_type & vo_wm_ABOVE)
1189 xev.data.l[1] = x11->XA_NET_WM_STATE_ABOVE;
1190 else if (x11->fs_type & vo_wm_FULLSCREEN)
1191 xev.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
1192 else if (x11->fs_type & vo_wm_BELOW)
1193 // This is not fallback. We can safely assume that the situation
1194 // where only NETWM_STATE_BELOW is supported doesn't exist.
1195 xev.data.l[1] = x11->XA_NET_WM_STATE_BELOW;
1197 XSendEvent(x11->display, x11->rootwin, False, SubstructureRedirectMask,
1198 (XEvent *) & xev);
1199 state = XGetAtomName(x11->display, xev.data.l[1]);
1200 mp_msg(MSGT_VO, MSGL_V,
1201 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1202 layer, state);
1203 XFree(state);
1207 static int vo_x11_get_fs_type(int supported)
1209 int i;
1210 int type = supported;
1212 if (vo_fstype_list)
1214 for (i = 0; vo_fstype_list[i]; i++)
1216 int neg = 0;
1217 char *arg = vo_fstype_list[i];
1219 if (vo_fstype_list[i][0] == '-')
1221 neg = 1;
1222 arg = vo_fstype_list[i] + 1;
1225 if (!strncmp(arg, "layer", 5))
1227 if (!neg && (arg[5] == '='))
1229 char *endptr = NULL;
1230 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1232 if (endptr && *endptr == '\0' && layer >= 0
1233 && layer <= 15)
1234 fs_layer = layer;
1236 if (neg)
1237 type &= ~vo_wm_LAYER;
1238 else
1239 type |= vo_wm_LAYER;
1240 } else if (!strcmp(arg, "above"))
1242 if (neg)
1243 type &= ~vo_wm_ABOVE;
1244 else
1245 type |= vo_wm_ABOVE;
1246 } else if (!strcmp(arg, "fullscreen"))
1248 if (neg)
1249 type &= ~vo_wm_FULLSCREEN;
1250 else
1251 type |= vo_wm_FULLSCREEN;
1252 } else if (!strcmp(arg, "stays_on_top"))
1254 if (neg)
1255 type &= ~vo_wm_STAYS_ON_TOP;
1256 else
1257 type |= vo_wm_STAYS_ON_TOP;
1258 } else if (!strcmp(arg, "below"))
1260 if (neg)
1261 type &= ~vo_wm_BELOW;
1262 else
1263 type |= vo_wm_BELOW;
1264 } else if (!strcmp(arg, "netwm"))
1266 if (neg)
1267 type &= ~vo_wm_NETWM;
1268 else
1269 type |= vo_wm_NETWM;
1270 } else if (!strcmp(arg, "none"))
1271 type = 0; // clear; keep parsing
1275 return type;
1279 * \brief update vo->dx, vo->dy, vo->dwidth and vo->dheight with current values of vo->x11->window
1280 * \return returns current color depth of vo->x11->window
1282 int vo_x11_update_geometry(struct vo *vo)
1284 struct vo_x11_state *x11 = vo->x11;
1285 unsigned depth, w, h;
1286 int dummy_int;
1287 Window dummy_win;
1288 XGetGeometry(x11->display, x11->window, &dummy_win, &dummy_int, &dummy_int,
1289 &w, &h, &dummy_int, &depth);
1290 if (w <= INT_MAX && h <= INT_MAX) {
1291 vo->dwidth = w;
1292 vo->dheight = h;
1294 XTranslateCoordinates(x11->display, x11->window, x11->rootwin, 0, 0,
1295 &vo->dx, &vo->dy, &dummy_win);
1296 if (vo_wintitle)
1297 XStoreName(x11->display, x11->window, vo_wintitle);
1299 return depth <= INT_MAX ? depth : 0;
1302 void vo_x11_fullscreen(struct vo *vo)
1304 struct MPOpts *opts = vo->opts;
1305 struct vo_x11_state *x11 = vo->x11;
1306 int x, y, w, h;
1307 x = x11->vo_old_x;
1308 y = x11->vo_old_y;
1309 w = x11->vo_old_width;
1310 h = x11->vo_old_height;
1312 if (WinID >= 0) {
1313 vo_fs = !vo_fs;
1314 return;
1316 if (x11->fs_flip)
1317 return;
1319 if (vo_fs)
1321 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1322 vo_fs = VO_FALSE;
1323 } else
1325 // win->fs
1326 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1328 vo_fs = VO_TRUE;
1329 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1331 x11->vo_old_x = vo->dx;
1332 x11->vo_old_y = vo->dy;
1333 x11->vo_old_width = vo->dwidth;
1334 x11->vo_old_height = vo->dheight;
1336 update_xinerama_info(vo);
1337 x = xinerama_x;
1338 y = xinerama_y;
1339 w = opts->vo_screenwidth;
1340 h = opts->vo_screenheight;
1343 long dummy;
1345 XGetWMNormalHints(x11->display, x11->window, &x11->vo_hint, &dummy);
1346 if (!(x11->vo_hint.flags & PWinGravity))
1347 x11->old_gravity = NorthWestGravity;
1348 else
1349 x11->old_gravity = x11->vo_hint.win_gravity;
1351 if (x11->wm_type == 0 && !(vo_fsmode & 16))
1353 XUnmapWindow(x11->display, x11->window); // required for MWM
1354 XWithdrawWindow(x11->display, x11->window, x11->screen);
1355 x11->fs_flip = 1;
1358 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1360 vo_x11_decoration(vo, vo_border && !vo_fs);
1361 vo_x11_sizehint(vo, x, y, w, h, 0);
1362 vo_x11_setlayer(vo, x11->window, vo_fs);
1365 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1367 /* some WMs lose ontop after fullscreen */
1368 if ((!(vo_fs)) & opts->vo_ontop)
1369 vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1371 XMapRaised(x11->display, x11->window);
1372 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1373 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1374 XRaiseWindow(x11->display, x11->window);
1375 XFlush(x11->display);
1378 void vo_x11_ontop(struct vo *vo)
1380 struct MPOpts *opts = vo->opts;
1381 opts->vo_ontop = !opts->vo_ontop;
1383 vo_x11_setlayer(vo, vo->x11->window, opts->vo_ontop);
1386 void vo_x11_border(struct vo *vo)
1388 vo_border = !vo_border;
1389 vo_x11_decoration(vo, vo_border && !vo_fs);
1393 * XScreensaver stuff
1396 static int screensaver_off;
1397 static unsigned int time_last;
1399 void xscreensaver_heartbeat(struct vo_x11_state *x11)
1401 unsigned int time = GetTimerMS();
1403 if (x11->display && screensaver_off && (time - time_last) > 30000)
1405 time_last = time;
1407 XResetScreenSaver(x11->display);
1411 static int xss_suspend(Display *mDisplay, Bool suspend)
1413 #ifndef CONFIG_XSS
1414 return 0;
1415 #else
1416 int event, error, major, minor;
1417 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1418 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1419 return 0;
1420 if (major < 1 || (major == 1 && minor < 1))
1421 return 0;
1422 XScreenSaverSuspend(mDisplay, suspend);
1423 return 1;
1424 #endif
1428 * End of XScreensaver stuff
1431 static void saver_on(Display * mDisplay)
1434 if (!screensaver_off)
1435 return;
1436 screensaver_off = 0;
1437 if (xss_suspend(mDisplay, False))
1438 return;
1439 #ifdef CONFIG_XDPMS
1440 if (dpms_disabled)
1442 int nothing;
1443 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1445 if (!DPMSEnable(mDisplay))
1446 { // restoring power saving settings
1447 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1448 } else
1450 // DPMS does not seem to be enabled unless we call DPMSInfo
1451 BOOL onoff;
1452 CARD16 state;
1454 DPMSForceLevel(mDisplay, DPMSModeOn);
1455 DPMSInfo(mDisplay, &state, &onoff);
1456 if (onoff)
1458 mp_msg(MSGT_VO, MSGL_V,
1459 "Successfully enabled DPMS\n");
1460 } else
1462 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1466 dpms_disabled = 0;
1468 #endif
1471 static void saver_off(Display * mDisplay)
1473 int nothing;
1475 if (screensaver_off)
1476 return;
1477 screensaver_off = 1;
1478 if (xss_suspend(mDisplay, True))
1479 return;
1480 #ifdef CONFIG_XDPMS
1481 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1483 BOOL onoff;
1484 CARD16 state;
1486 DPMSInfo(mDisplay, &state, &onoff);
1487 if (onoff)
1489 Status stat;
1491 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1492 dpms_disabled = 1;
1493 stat = DPMSDisable(mDisplay); // monitor powersave off
1494 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1497 #endif
1500 static XErrorHandler old_handler = NULL;
1501 static int selectinput_err = 0;
1502 static int x11_selectinput_errorhandler(Display * display,
1503 XErrorEvent * event)
1505 if (event->error_code == BadAccess)
1507 selectinput_err = 1;
1508 mp_msg(MSGT_VO, MSGL_ERR,
1509 "X11 error: BadAccess during XSelectInput Call\n");
1510 mp_msg(MSGT_VO, MSGL_ERR,
1511 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1512 /* If you think MPlayer should shutdown with this error,
1513 * comment out the following line */
1514 return 0;
1516 if (old_handler != NULL)
1517 old_handler(display, event);
1518 else
1519 x11_errorhandler(display, event);
1520 return 0;
1523 void vo_x11_selectinput_witherr(Display * display, Window w,
1524 long event_mask)
1526 XSync(display, False);
1527 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1528 selectinput_err = 0;
1529 if (vo_nomouse_input)
1531 XSelectInput(display, w,
1532 event_mask &
1533 (~(ButtonPressMask | ButtonReleaseMask)));
1534 } else
1536 XSelectInput(display, w, event_mask);
1538 XSync(display, False);
1539 XSetErrorHandler(old_handler);
1540 if (selectinput_err)
1542 mp_msg(MSGT_VO, MSGL_ERR,
1543 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1544 XSelectInput(display, w,
1545 event_mask &
1547 (ButtonPressMask | ButtonReleaseMask |
1548 PointerMotionMask)));
1552 #ifdef CONFIG_XF86VM
1553 void vo_vm_switch(struct vo *vo)
1555 struct vo_x11_state *x11 = vo->x11;
1556 struct MPOpts *opts = vo->opts;
1557 Display *mDisplay = x11->display;
1558 int vm_event, vm_error;
1559 int vm_ver, vm_rev;
1560 int i, j, have_vm = 0;
1561 int X = vo->dwidth, Y = vo->dheight;
1562 int modeline_width, modeline_height;
1564 int modecount;
1566 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1568 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1569 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1570 vm_rev);
1571 have_vm = 1;
1572 } else {
1573 mp_msg(MSGT_VO, MSGL_WARN,
1574 "XF86VidMode extension not available.\n");
1577 if (have_vm)
1579 if (vidmodes == NULL)
1580 XF86VidModeGetAllModeLines(mDisplay, x11->screen, &modecount,
1581 &vidmodes);
1582 j = 0;
1583 modeline_width = vidmodes[0]->hdisplay;
1584 modeline_height = vidmodes[0]->vdisplay;
1586 for (i = 1; i < modecount; i++)
1587 if ((vidmodes[i]->hdisplay >= X)
1588 && (vidmodes[i]->vdisplay >= Y))
1589 if ((vidmodes[i]->hdisplay <= modeline_width)
1590 && (vidmodes[i]->vdisplay <= modeline_height))
1592 modeline_width = vidmodes[i]->hdisplay;
1593 modeline_height = vidmodes[i]->vdisplay;
1594 j = i;
1597 mp_tmsg(MSGT_VO, MSGL_INFO, "XF86VM: Selected video mode %dx%d for image size %dx%d.\n",
1598 modeline_width, modeline_height, X, Y);
1599 XF86VidModeLockModeSwitch(mDisplay, x11->screen, 0);
1600 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1601 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1603 // FIXME: all this is more of a hack than proper solution
1604 X = (opts->vo_screenwidth - modeline_width) / 2;
1605 Y = (opts->vo_screenheight - modeline_height) / 2;
1606 XF86VidModeSetViewPort(mDisplay, x11->screen, X, Y);
1607 vo->dx = X;
1608 vo->dy = Y;
1609 vo->dwidth = modeline_width;
1610 vo->dheight = modeline_height;
1611 aspect_save_screenres(vo, modeline_width, modeline_height);
1615 void vo_vm_close(struct vo *vo)
1617 Display *dpy = vo->x11->display;
1618 struct MPOpts *opts = vo->opts;
1619 if (vidmodes != NULL)
1621 int i, modecount;
1623 free(vidmodes);
1624 vidmodes = NULL;
1625 XF86VidModeGetAllModeLines(dpy, vo->x11->screen, &modecount,
1626 &vidmodes);
1627 for (i = 0; i < modecount; i++)
1628 if ((vidmodes[i]->hdisplay == opts->vo_screenwidth)
1629 && (vidmodes[i]->vdisplay == opts->vo_screenheight))
1631 mp_msg(MSGT_VO, MSGL_INFO,
1632 "Returning to original mode %dx%d\n",
1633 opts->vo_screenwidth, opts->vo_screenheight);
1634 break;
1637 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1638 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1639 free(vidmodes);
1640 vidmodes = NULL;
1644 double vo_vm_get_fps(struct vo *vo)
1646 struct vo_x11_state *x11 = vo->x11;
1647 int clock;
1648 XF86VidModeModeLine modeline;
1649 if (!XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline))
1650 return 0;
1651 if (modeline.privsize)
1652 XFree(modeline.private);
1653 return 1e3 * clock / modeline.htotal / modeline.vtotal;
1655 #endif
1657 #endif /* X11_FULLSCREEN */
1661 * Scan the available visuals on this Display/Screen. Try to find
1662 * the 'best' available TrueColor visual that has a decent color
1663 * depth (at least 15bit). If there are multiple visuals with depth
1664 * >= 15bit, we prefer visuals with a smaller color depth.
1666 int vo_find_depth_from_visuals(Display * dpy, int screen,
1667 Visual ** visual_return)
1669 XVisualInfo visual_tmpl;
1670 XVisualInfo *visuals;
1671 int nvisuals, i;
1672 int bestvisual = -1;
1673 int bestvisual_depth = -1;
1675 visual_tmpl.screen = screen;
1676 visual_tmpl.class = TrueColor;
1677 visuals = XGetVisualInfo(dpy,
1678 VisualScreenMask | VisualClassMask,
1679 &visual_tmpl, &nvisuals);
1680 if (visuals != NULL)
1682 for (i = 0; i < nvisuals; i++)
1684 mp_msg(MSGT_VO, MSGL_V,
1685 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1686 visuals[i].visualid, visuals[i].depth,
1687 visuals[i].red_mask, visuals[i].green_mask,
1688 visuals[i].blue_mask);
1690 * Save the visual index and its depth, if this is the first
1691 * truecolor visul, or a visual that is 'preferred' over the
1692 * previous 'best' visual.
1694 if (bestvisual_depth == -1
1695 || (visuals[i].depth >= 15
1696 && (visuals[i].depth < bestvisual_depth
1697 || bestvisual_depth < 15)))
1699 bestvisual = i;
1700 bestvisual_depth = visuals[i].depth;
1704 if (bestvisual != -1 && visual_return != NULL)
1705 *visual_return = visuals[bestvisual].visual;
1707 XFree(visuals);
1709 return bestvisual_depth;
1713 static Colormap cmap = None;
1714 static XColor cols[256];
1715 static int cm_size, red_mask, green_mask, blue_mask;
1718 Colormap vo_x11_create_colormap(struct vo *vo, XVisualInfo *vinfo)
1720 struct vo_x11_state *x11 = vo->x11;
1721 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1723 if (vinfo->class != DirectColor)
1724 return XCreateColormap(x11->display, x11->rootwin, vinfo->visual,
1725 AllocNone);
1727 /* can this function get called twice or more? */
1728 if (cmap)
1729 return cmap;
1730 cm_size = vinfo->colormap_size;
1731 red_mask = vinfo->red_mask;
1732 green_mask = vinfo->green_mask;
1733 blue_mask = vinfo->blue_mask;
1734 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1735 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1736 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1737 rvu = 65536ull * ru / (red_mask + ru);
1738 gvu = 65536ull * gu / (green_mask + gu);
1739 bvu = 65536ull * bu / (blue_mask + bu);
1740 r = g = b = 0;
1741 rv = gv = bv = 0;
1742 m = DoRed | DoGreen | DoBlue;
1743 for (k = 0; k < cm_size; k++)
1745 int t;
1747 cols[k].pixel = r | g | b;
1748 cols[k].red = rv;
1749 cols[k].green = gv;
1750 cols[k].blue = bv;
1751 cols[k].flags = m;
1752 t = (r + ru) & red_mask;
1753 if (t < r)
1754 m &= ~DoRed;
1755 r = t;
1756 t = (g + gu) & green_mask;
1757 if (t < g)
1758 m &= ~DoGreen;
1759 g = t;
1760 t = (b + bu) & blue_mask;
1761 if (t < b)
1762 m &= ~DoBlue;
1763 b = t;
1764 rv += rvu;
1765 gv += gvu;
1766 bv += bvu;
1768 cmap = XCreateColormap(x11->display, x11->rootwin, vinfo->visual, AllocAll);
1769 XStoreColors(x11->display, cmap, cols, cm_size);
1770 return cmap;
1774 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1775 * hue and red/green/blue intensity, but we cannot do saturation.
1776 * Currently only gamma, brightness and contrast are implemented.
1777 * Is there sufficient interest for hue and/or red/green/blue intensity?
1779 /* these values have range [-100,100] and are initially 0 */
1780 static int vo_gamma = 0;
1781 static int vo_brightness = 0;
1782 static int vo_contrast = 0;
1784 static int transform_color(float val,
1785 float brightness, float contrast, float gamma) {
1786 float s = pow(val, gamma);
1787 s = (s - 0.5) * contrast + 0.5;
1788 s += brightness;
1789 if (s < 0)
1790 s = 0;
1791 if (s > 1)
1792 s = 1;
1793 return (unsigned short) (s * 65535);
1796 uint32_t vo_x11_set_equalizer(struct vo *vo, char *name, int value)
1798 float gamma, brightness, contrast;
1799 float rf, gf, bf;
1800 int k;
1803 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1804 * of TrueColor-ed window but be careful:
1805 * Unlike the colormaps, which are private for the X client
1806 * who created them and thus automatically destroyed on client
1807 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1808 * setting and _must_ be restored before the process exits.
1809 * Unforunately when the process crashes (or gets killed
1810 * for some reason) it is impossible to restore the setting,
1811 * and such behaviour could be rather annoying for the users.
1813 if (cmap == None)
1814 return VO_NOTAVAIL;
1816 if (!strcasecmp(name, "brightness"))
1817 vo_brightness = value;
1818 else if (!strcasecmp(name, "contrast"))
1819 vo_contrast = value;
1820 else if (!strcasecmp(name, "gamma"))
1821 vo_gamma = value;
1822 else
1823 return VO_NOTIMPL;
1825 brightness = 0.01 * vo_brightness;
1826 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1827 gamma = pow(2, -0.02 * vo_gamma);
1829 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1830 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
1831 green_mask;
1832 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
1834 /* now recalculate the colormap using the newly set value */
1835 for (k = 0; k < cm_size; k++)
1837 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
1838 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
1839 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
1842 XStoreColors(vo->x11->display, cmap, cols, cm_size);
1843 XFlush(vo->x11->display);
1844 return VO_TRUE;
1847 uint32_t vo_x11_get_equalizer(char *name, int *value)
1849 if (cmap == None)
1850 return VO_NOTAVAIL;
1851 if (!strcasecmp(name, "brightness"))
1852 *value = vo_brightness;
1853 else if (!strcasecmp(name, "contrast"))
1854 *value = vo_contrast;
1855 else if (!strcasecmp(name, "gamma"))
1856 *value = vo_gamma;
1857 else
1858 return VO_NOTIMPL;
1859 return VO_TRUE;
1862 #ifdef CONFIG_XV
1863 int vo_xv_set_eq(struct vo *vo, uint32_t xv_port, char *name, int value)
1865 XvAttribute *attributes;
1866 int i, howmany, xv_atom;
1868 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
1870 /* get available attributes */
1871 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1872 for (i = 0; i < howmany && attributes; i++)
1873 if (attributes[i].flags & XvSettable)
1875 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1876 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1877 then trigger it if it's ok so that the other values are at default upon query */
1878 if (xv_atom != None)
1880 int hue = 0, port_value, port_min, port_max;
1882 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1883 (!strcasecmp(name, "brightness")))
1884 port_value = value;
1885 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1886 (!strcasecmp(name, "contrast")))
1887 port_value = value;
1888 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1889 (!strcasecmp(name, "saturation")))
1890 port_value = value;
1891 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1892 (!strcasecmp(name, "hue")))
1894 port_value = value;
1895 hue = 1;
1896 } else
1897 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1898 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1899 (!strcasecmp(name, "red_intensity")))
1900 port_value = value;
1901 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1902 && (!strcasecmp(name, "green_intensity")))
1903 port_value = value;
1904 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1905 && (!strcasecmp(name, "blue_intensity")))
1906 port_value = value;
1907 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
1908 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
1909 && (!strcasecmp(name, "bt_709")))
1910 port_value = value;
1911 else
1912 continue;
1914 port_min = attributes[i].min_value;
1915 port_max = attributes[i].max_value;
1917 /* nvidia hue workaround */
1918 if (hue && port_min == 0 && port_max == 360)
1920 port_value =
1921 (port_value >=
1922 0) ? (port_value - 100) : (port_value + 100);
1924 // -100 -> min
1925 // 0 -> (max+min)/2
1926 // +100 -> max
1927 port_value =
1928 (port_value + 100) * (port_max - port_min) / 200 +
1929 port_min;
1930 XvSetPortAttribute(vo->x11->display, xv_port, xv_atom, port_value);
1931 return VO_TRUE;
1934 return VO_FALSE;
1937 int vo_xv_get_eq(struct vo *vo, uint32_t xv_port, char *name, int *value)
1940 XvAttribute *attributes;
1941 int i, howmany, xv_atom;
1943 /* get available attributes */
1944 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1945 for (i = 0; i < howmany && attributes; i++)
1946 if (attributes[i].flags & XvGettable)
1948 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1949 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1950 then trigger it if it's ok so that the other values are at default upon query */
1951 if (xv_atom != None)
1953 int val, port_value = 0, port_min, port_max;
1955 XvGetPortAttribute(vo->x11->display, xv_port, xv_atom,
1956 &port_value);
1958 port_min = attributes[i].min_value;
1959 port_max = attributes[i].max_value;
1960 val =
1961 (port_value - port_min) * 200 / (port_max - port_min) -
1962 100;
1964 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1965 (!strcasecmp(name, "brightness")))
1966 *value = val;
1967 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1968 (!strcasecmp(name, "contrast")))
1969 *value = val;
1970 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1971 (!strcasecmp(name, "saturation")))
1972 *value = val;
1973 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1974 (!strcasecmp(name, "hue")))
1976 /* nasty nvidia detect */
1977 if (port_min == 0 && port_max == 360)
1978 *value = (val >= 0) ? (val - 100) : (val + 100);
1979 else
1980 *value = val;
1981 } else
1982 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1983 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1984 (!strcasecmp(name, "red_intensity")))
1985 *value = val;
1986 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1987 && (!strcasecmp(name, "green_intensity")))
1988 *value = val;
1989 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1990 && (!strcasecmp(name, "blue_intensity")))
1991 *value = val;
1992 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
1993 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
1994 && (!strcasecmp(name, "bt_709")))
1995 *value = val;
1996 else
1997 continue;
1999 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2000 name, *value);
2001 return VO_TRUE;
2004 return VO_FALSE;
2008 * \brief Interns the requested atom if it is available.
2010 * \param atom_name String containing the name of the requested atom.
2012 * \return Returns the atom if available, else None is returned.
2015 static Atom xv_intern_atom_if_exists(struct vo_x11_state *x11,
2016 char const *atom_name)
2018 XvAttribute * attributes;
2019 int attrib_count,i;
2020 Atom xv_atom = None;
2022 attributes = XvQueryPortAttributes(x11->display, x11->xv_port, &attrib_count );
2023 if( attributes!=NULL )
2025 for ( i = 0; i < attrib_count; ++i )
2027 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2029 xv_atom = XInternAtom(x11->display, atom_name, False );
2030 break; // found what we want, break out
2033 XFree( attributes );
2036 return xv_atom;
2040 * \brief Try to enable vsync for xv.
2041 * \return Returns -1 if not available, 0 on failure and 1 on success.
2043 int vo_xv_enable_vsync(struct vo *vo)
2045 struct vo_x11_state *x11 = vo->x11;
2046 Atom xv_atom = xv_intern_atom_if_exists(x11, "XV_SYNC_TO_VBLANK");
2047 if (xv_atom == None)
2048 return -1;
2049 return XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1) == Success;
2053 * \brief Get maximum supported source image dimensions.
2055 * This function does not set the variables pointed to by
2056 * width and height if the information could not be retrieved,
2057 * so the caller is reponsible for properly initializing them.
2059 * \param width [out] The maximum width gets stored here.
2060 * \param height [out] The maximum height gets stored here.
2063 void vo_xv_get_max_img_dim(struct vo *vo, uint32_t * width, uint32_t * height)
2065 struct vo_x11_state *x11 = vo->x11;
2066 XvEncodingInfo * encodings;
2067 //unsigned long num_encodings, idx; to int or too long?!
2068 unsigned int num_encodings, idx;
2070 XvQueryEncodings(x11->display, x11->xv_port, &num_encodings, &encodings);
2072 if ( encodings )
2074 for ( idx = 0; idx < num_encodings; ++idx )
2076 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2078 *width = encodings[idx].width;
2079 *height = encodings[idx].height;
2080 break;
2085 mp_msg( MSGT_VO, MSGL_V,
2086 "[xv common] Maximum source image dimensions: %ux%u\n",
2087 *width, *height );
2089 XvFreeEncodingInfo( encodings );
2093 * \brief Print information about the colorkey method and source.
2095 * \param ck_handling Integer value containing the information about
2096 * colorkey handling (see x11_common.h).
2098 * Outputs the content of |ck_handling| as a readable message.
2101 static void vo_xv_print_ck_info(struct vo_x11_state *x11)
2103 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2105 switch ( x11->xv_ck_info.method )
2107 case CK_METHOD_NONE:
2108 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2109 case CK_METHOD_AUTOPAINT:
2110 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2111 case CK_METHOD_MANUALFILL:
2112 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2113 case CK_METHOD_BACKGROUND:
2114 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2117 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2119 switch ( x11->xv_ck_info.source )
2121 case CK_SRC_CUR:
2122 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2123 x11->xv_colorkey );
2124 break;
2125 case CK_SRC_USE:
2126 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2128 mp_msg( MSGT_VO, MSGL_V,
2129 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2130 x11->xv_colorkey );
2132 else
2134 mp_msg( MSGT_VO, MSGL_V,
2135 "Using colorkey from MPlayer (0x%06lx)."
2136 " Use -colorkey to change.\n",
2137 x11->xv_colorkey );
2139 break;
2140 case CK_SRC_SET:
2141 mp_msg( MSGT_VO, MSGL_V,
2142 "Setting and using colorkey from MPlayer (0x%06lx)."
2143 " Use -colorkey to change.\n",
2144 x11->xv_colorkey );
2145 break;
2149 * \brief Init colorkey depending on the settings in xv_ck_info.
2151 * \return Returns 0 on failure and 1 on success.
2153 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2154 * flags in xv_ck_info.
2156 * Possiblilities:
2157 * * Methods
2158 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2159 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2160 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2161 * * Sources
2162 * - use currently set colorkey ( CK_SRC_CUR )
2163 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2164 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2166 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2167 * we don't draw anything as this means it was forced to off.
2169 int vo_xv_init_colorkey(struct vo *vo)
2171 struct vo_x11_state *x11 = vo->x11;
2172 Atom xv_atom;
2173 int rez;
2175 /* check if colorkeying is needed */
2176 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_COLORKEY");
2178 /* if we have to deal with colorkeying ... */
2179 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2181 /* check if we should use the colorkey specified in vo_colorkey */
2182 if ( x11->xv_ck_info.source != CK_SRC_CUR )
2184 x11->xv_colorkey = vo_colorkey;
2186 /* check if we have to set the colorkey too */
2187 if ( x11->xv_ck_info.source == CK_SRC_SET )
2189 xv_atom = XInternAtom(x11->display, "XV_COLORKEY",False);
2191 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, vo_colorkey);
2192 if ( rez != Success )
2194 mp_msg( MSGT_VO, MSGL_FATAL,
2195 "[xv common] Couldn't set colorkey!\n" );
2196 return 0; // error setting colorkey
2200 else
2202 int colorkey_ret;
2204 rez=XvGetPortAttribute(x11->display,x11->xv_port, xv_atom, &colorkey_ret);
2205 if ( rez == Success )
2207 x11->xv_colorkey = colorkey_ret;
2209 else
2211 mp_msg( MSGT_VO, MSGL_FATAL,
2212 "[xv common] Couldn't get colorkey!"
2213 "Maybe the selected Xv port has no overlay.\n" );
2214 return 0; // error getting colorkey
2218 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_AUTOPAINT_COLORKEY");
2220 /* should we draw the colorkey ourselves or activate autopainting? */
2221 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2223 rez = !Success; // reset rez to something different than Success
2225 if ( xv_atom != None ) // autopaint is supported
2227 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1);
2230 if ( rez != Success )
2232 // fallback to manual colorkey drawing
2233 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2236 else // disable colorkey autopainting if supported
2238 if ( xv_atom != None ) // we have autopaint attribute
2240 XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 0);
2244 else // do no colorkey drawing at all
2246 x11->xv_ck_info.method = CK_METHOD_NONE;
2247 } /* end: should we draw colorkey */
2249 /* output information about the current colorkey settings */
2250 vo_xv_print_ck_info(x11);
2252 return 1; // success
2256 * \brief Draw the colorkey on the video window.
2258 * Draws the colorkey depending on the set method ( colorkey_handling ).
2260 * Also draws the black bars ( when the video doesn't fit the display in
2261 * fullscreen ) separately, so they don't overlap with the video area.
2262 * It doesn't call XFlush.
2265 void vo_xv_draw_colorkey(struct vo *vo, int32_t x, int32_t y,
2266 int32_t w, int32_t h)
2268 struct MPOpts *opts = vo->opts;
2269 struct vo_x11_state *x11 = vo->x11;
2270 if( x11->xv_ck_info.method == CK_METHOD_MANUALFILL ||
2271 x11->xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2273 XSetForeground(x11->display, x11->vo_gc, x11->xv_colorkey );
2274 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2275 x, y,
2276 w, h );
2279 /* draw black bars if needed */
2280 /* TODO! move this to vo_x11_clearwindow_part() */
2281 if ( vo_fs )
2283 XSetForeground(x11->display, x11->vo_gc, 0 );
2284 /* making non-overlap fills, requires 8 checks instead of 4 */
2285 if ( y > 0 )
2286 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2287 0, 0,
2288 opts->vo_screenwidth, y);
2289 if (x > 0)
2290 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2291 0, 0,
2292 x, opts->vo_screenheight);
2293 if (x + w < opts->vo_screenwidth)
2294 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2295 x + w, 0,
2296 opts->vo_screenwidth, opts->vo_screenheight);
2297 if (y + h < opts->vo_screenheight)
2298 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2299 0, y + h,
2300 opts->vo_screenwidth, opts->vo_screenheight);
2304 /** \brief Tests if a valid argument for the ck suboption was given. */
2305 int xv_test_ck( void * arg )
2307 strarg_t * strarg = (strarg_t *)arg;
2309 if ( strargcmp( strarg, "use" ) == 0 ||
2310 strargcmp( strarg, "set" ) == 0 ||
2311 strargcmp( strarg, "cur" ) == 0 )
2313 return 1;
2316 return 0;
2318 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2319 int xv_test_ckm( void * arg )
2321 strarg_t * strarg = (strarg_t *)arg;
2323 if ( strargcmp( strarg, "bg" ) == 0 ||
2324 strargcmp( strarg, "man" ) == 0 ||
2325 strargcmp( strarg, "auto" ) == 0 )
2327 return 1;
2330 return 0;
2334 * \brief Modify the colorkey_handling var according to str
2336 * Checks if a valid pointer ( not NULL ) to the string
2337 * was given. And in that case modifies the colorkey_handling
2338 * var to reflect the requested behaviour.
2339 * If nothing happens the content of colorkey_handling stays
2340 * the same.
2342 * \param str Pointer to the string or NULL
2345 void xv_setup_colorkeyhandling(struct vo *vo, const char *ck_method_str,
2346 const char *ck_str)
2348 struct vo_x11_state *x11 = vo->x11;
2349 /* check if a valid pointer to the string was passed */
2350 if ( ck_str )
2352 if ( strncmp( ck_str, "use", 3 ) == 0 )
2354 x11->xv_ck_info.source = CK_SRC_USE;
2356 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2358 x11->xv_ck_info.source = CK_SRC_SET;
2361 /* check if a valid pointer to the string was passed */
2362 if ( ck_method_str )
2364 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2366 x11->xv_ck_info.method = CK_METHOD_BACKGROUND;
2368 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2370 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2372 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2374 x11->xv_ck_info.method = CK_METHOD_AUTOPAINT;
2379 #endif
2381 struct vo_x11_state *vo_x11_init_state(void)
2383 struct vo_x11_state *s = talloc_ptrtype(NULL, s);
2384 *s = (struct vo_x11_state){
2385 .xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR },
2386 .olddecor = MWM_DECOR_ALL,
2387 .oldfuncs = MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
2388 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE,
2389 .old_gravity = NorthWestGravity,
2391 return s;