playtree: make some char * function arguments const
[mplayer.git] / libvo / x11_common.c
blobc576839ddb36bc1b3f486049c8995ac24d273212
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <math.h>
22 #include <inttypes.h>
23 #include <limits.h>
25 #include "config.h"
26 #include "options.h"
27 #include "mp_msg.h"
28 #include "mp_fifo.h"
29 #include "libavutil/common.h"
30 #include "x11_common.h"
31 #include "talloc.h"
33 #ifdef X11_FULLSCREEN
35 #include <string.h>
36 #include <unistd.h>
37 #include <assert.h>
39 #include "video_out.h"
40 #include "aspect.h"
41 #include "geometry.h"
42 #include "osdep/timer.h"
44 #include <X11/Xmd.h>
45 #include <X11/Xlib.h>
46 #include <X11/Xutil.h>
47 #include <X11/Xatom.h>
49 #ifdef CONFIG_XSS
50 #include <X11/extensions/scrnsaver.h>
51 #endif
53 #ifdef CONFIG_XDPMS
54 #include <X11/extensions/dpms.h>
55 #endif
57 #ifdef CONFIG_XINERAMA
58 #include <X11/extensions/Xinerama.h>
59 #endif
61 #ifdef CONFIG_XF86VM
62 #include <X11/extensions/xf86vmode.h>
63 #endif
65 #ifdef CONFIG_XF86XK
66 #include <X11/XF86keysym.h>
67 #endif
69 #ifdef CONFIG_XV
70 #include <X11/extensions/Xv.h>
71 #include <X11/extensions/Xvlib.h>
73 #include "subopt-helper.h"
74 #endif
76 #include "input/input.h"
77 #include "input/keycodes.h"
79 #define WIN_LAYER_ONBOTTOM 2
80 #define WIN_LAYER_NORMAL 4
81 #define WIN_LAYER_ONTOP 6
82 #define WIN_LAYER_ABOVE_DOCK 10
84 int fs_layer = WIN_LAYER_ABOVE_DOCK;
86 int stop_xscreensaver = 0;
88 static int dpms_disabled = 0;
90 char *mDisplayName = NULL;
92 char **vo_fstype_list;
94 /* 1 means that the WM is metacity (broken as hell) */
95 int metacity_hack = 0;
97 #ifdef CONFIG_XF86VM
98 XF86VidModeModeInfo **vidmodes = NULL;
99 XF86VidModeModeLine modeline;
100 #endif
102 static int vo_x11_get_fs_type(int supported);
103 static void saver_off(Display *);
104 static void saver_on(Display *);
107 * Sends the EWMH fullscreen state event.
109 * action: could be one of _NET_WM_STATE_REMOVE -- remove state
110 * _NET_WM_STATE_ADD -- add state
111 * _NET_WM_STATE_TOGGLE -- toggle
113 void vo_x11_ewmh_fullscreen(struct vo_x11_state *x11, int action)
115 assert(action == _NET_WM_STATE_REMOVE ||
116 action == _NET_WM_STATE_ADD || action == _NET_WM_STATE_TOGGLE);
118 if (x11->fs_type & vo_wm_FULLSCREEN)
120 XEvent xev;
122 /* init X event structure for _NET_WM_FULLSCREEN client message */
123 xev.xclient.type = ClientMessage;
124 xev.xclient.serial = 0;
125 xev.xclient.send_event = True;
126 xev.xclient.message_type = x11->XA_NET_WM_STATE;
127 xev.xclient.window = x11->window;
128 xev.xclient.format = 32;
129 xev.xclient.data.l[0] = action;
130 xev.xclient.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
131 xev.xclient.data.l[2] = 0;
132 xev.xclient.data.l[3] = 0;
133 xev.xclient.data.l[4] = 0;
135 /* finally send that damn thing */
136 if (!XSendEvent(x11->display, DefaultRootWindow(x11->display), False,
137 SubstructureRedirectMask | SubstructureNotifyMask,
138 &xev))
140 mp_tmsg(MSGT_VO, MSGL_ERR, "\nX11: Couldn't send EWMH fullscreen event!\n");
145 static void vo_hidecursor(Display * disp, Window win)
147 Cursor no_ptr;
148 Pixmap bm_no;
149 XColor black, dummy;
150 Colormap colormap;
151 const char bm_no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
153 if (WinID == 0)
154 return; // do not hide if playing on the root window
156 colormap = DefaultColormap(disp, DefaultScreen(disp));
157 if ( !XAllocNamedColor(disp, colormap, "black", &black, &dummy) )
159 return; // color alloc failed, give up
161 bm_no = XCreateBitmapFromData(disp, win, bm_no_data, 8, 8);
162 no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
163 XDefineCursor(disp, win, no_ptr);
164 XFreeCursor(disp, no_ptr);
165 if (bm_no != None)
166 XFreePixmap(disp, bm_no);
167 XFreeColors(disp,colormap,&black.pixel,1,0);
170 static void vo_showcursor(Display * disp, Window win)
172 if (WinID == 0)
173 return;
174 XDefineCursor(disp, win, 0);
177 static int x11_errorhandler(Display * display, XErrorEvent * event)
179 #define MSGLEN 60
180 char msg[MSGLEN];
182 XGetErrorText(display, event->error_code, (char *) &msg, MSGLEN);
184 mp_msg(MSGT_VO, MSGL_ERR, "X11 error: %s\n", msg);
186 mp_msg(MSGT_VO, MSGL_V,
187 "Type: %x, display: %p, resourceid: %lx, serial: %lx\n",
188 event->type, event->display, event->resourceid, event->serial);
189 mp_msg(MSGT_VO, MSGL_V,
190 "Error code: %x, request code: %x, minor code: %x\n",
191 event->error_code, event->request_code, event->minor_code);
193 // abort();
194 //exit_player("X11 error");
195 return 0;
196 #undef MSGLEN
199 void fstype_help(void)
201 mp_tmsg(MSGT_VO, MSGL_INFO, "Available fullscreen layer change modes:\n");
202 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FULL_SCREEN_TYPES\n");
204 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "none",
205 "don't set fullscreen window layer");
206 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer",
207 "use _WIN_LAYER hint with default layer");
208 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer=<0..15>",
209 "use _WIN_LAYER hint with a given layer number");
210 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "netwm",
211 "force NETWM style");
212 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "above",
213 "use _NETWM_STATE_ABOVE hint if available");
214 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "below",
215 "use _NETWM_STATE_BELOW hint if available");
216 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "fullscreen",
217 "use _NETWM_STATE_FULLSCREEN hint if available");
218 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "stays_on_top",
219 "use _NETWM_STATE_STAYS_ON_TOP hint if available");
220 mp_msg(MSGT_VO, MSGL_INFO,
221 "You can also negate the settings with simply putting '-' in the beginning");
222 mp_msg(MSGT_VO, MSGL_INFO, "\n");
225 static void fstype_dump(int fstype)
227 if (fstype)
229 mp_msg(MSGT_VO, MSGL_V, "[x11] Current fstype setting honours");
230 if (fstype & vo_wm_LAYER)
231 mp_msg(MSGT_VO, MSGL_V, " LAYER");
232 if (fstype & vo_wm_FULLSCREEN)
233 mp_msg(MSGT_VO, MSGL_V, " FULLSCREEN");
234 if (fstype & vo_wm_STAYS_ON_TOP)
235 mp_msg(MSGT_VO, MSGL_V, " STAYS_ON_TOP");
236 if (fstype & vo_wm_ABOVE)
237 mp_msg(MSGT_VO, MSGL_V, " ABOVE");
238 if (fstype & vo_wm_BELOW)
239 mp_msg(MSGT_VO, MSGL_V, " BELOW");
240 mp_msg(MSGT_VO, MSGL_V, " X atoms\n");
241 } else
242 mp_msg(MSGT_VO, MSGL_V,
243 "[x11] Current fstype setting doesn't honour any X atoms\n");
246 static int net_wm_support_state_test(struct vo_x11_state *x11, Atom atom)
248 #define NET_WM_STATE_TEST(x) { if (atom == x11->XA_NET_WM_STATE_##x) { mp_msg( MSGT_VO,MSGL_V, "[x11] Detected wm supports " #x " state.\n" ); return vo_wm_##x; } }
250 NET_WM_STATE_TEST(FULLSCREEN);
251 NET_WM_STATE_TEST(ABOVE);
252 NET_WM_STATE_TEST(STAYS_ON_TOP);
253 NET_WM_STATE_TEST(BELOW);
254 return 0;
257 static int x11_get_property(struct vo_x11_state *x11, Atom type, Atom ** args,
258 unsigned long *nitems)
260 int format;
261 unsigned long bytesafter;
263 return Success ==
264 XGetWindowProperty(x11->display, x11->rootwin, type, 0, 16384, False,
265 AnyPropertyType, &type, &format, nitems,
266 &bytesafter, (unsigned char **) args)
267 && *nitems > 0;
270 static int vo_wm_detect(struct vo *vo)
272 struct vo_x11_state *x11 = vo->x11;
273 int i;
274 int wm = 0;
275 unsigned long nitems;
276 Atom *args = NULL;
278 if (WinID >= 0)
279 return 0;
281 // -- supports layers
282 if (x11_get_property(x11, x11->XA_WIN_PROTOCOLS, &args, &nitems))
284 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports layers.\n");
285 for (i = 0; i < nitems; i++)
287 if (args[i] == x11->XA_WIN_LAYER)
289 wm |= vo_wm_LAYER;
290 metacity_hack |= 1;
291 } else
292 /* metacity is the only window manager I know which reports
293 * supporting only the _WIN_LAYER hint in _WIN_PROTOCOLS.
294 * (what's more support for it is broken) */
295 metacity_hack |= 2;
297 XFree(args);
298 if (wm && (metacity_hack == 1))
300 // metacity claims to support layers, but it is not the truth :-)
301 wm ^= vo_wm_LAYER;
302 mp_msg(MSGT_VO, MSGL_V,
303 "[x11] Using workaround for Metacity bugs.\n");
306 // --- netwm
307 if (x11_get_property(x11, x11->XA_NET_SUPPORTED, &args, &nitems))
309 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports NetWM.\n");
310 for (i = 0; i < nitems; i++)
311 wm |= net_wm_support_state_test(vo->x11, args[i]);
312 XFree(args);
315 if (wm == 0)
316 mp_msg(MSGT_VO, MSGL_V, "[x11] Unknown wm type...\n");
317 return wm;
320 #define XA_INIT(x) x11->XA##x = XInternAtom(x11->display, #x, False)
321 static void init_atoms(struct vo_x11_state *x11)
323 XA_INIT(_NET_SUPPORTED);
324 XA_INIT(_NET_WM_STATE);
325 XA_INIT(_NET_WM_STATE_FULLSCREEN);
326 XA_INIT(_NET_WM_STATE_ABOVE);
327 XA_INIT(_NET_WM_STATE_STAYS_ON_TOP);
328 XA_INIT(_NET_WM_STATE_BELOW);
329 XA_INIT(_NET_WM_PID);
330 XA_INIT(_WIN_PROTOCOLS);
331 XA_INIT(_WIN_LAYER);
332 XA_INIT(_WIN_HINTS);
333 XA_INIT(WM_PROTOCOLS);
334 XA_INIT(WM_DELETE_WINDOW);
337 void update_xinerama_info(struct vo *vo) {
338 struct MPOpts *opts = vo->opts;
339 xinerama_x = xinerama_y = 0;
340 #ifdef CONFIG_XINERAMA
341 if (xinerama_screen >= -1 && XineramaIsActive(vo->x11->display))
343 int screen = xinerama_screen;
344 XineramaScreenInfo *screens;
345 int num_screens;
347 screens = XineramaQueryScreens(vo->x11->display, &num_screens);
348 if (screen >= num_screens)
349 screen = num_screens - 1;
350 if (screen == -1) {
351 int x = vo->dx + vo->dwidth / 2;
352 int y = vo->dy + vo->dheight / 2;
353 for (screen = num_screens - 1; screen > 0; screen--) {
354 int left = screens[screen].x_org;
355 int right = left + screens[screen].width;
356 int top = screens[screen].y_org;
357 int bottom = top + screens[screen].height;
358 if (left <= x && x <= right && top <= y && y <= bottom)
359 break;
362 if (screen < 0)
363 screen = 0;
364 opts->vo_screenwidth = screens[screen].width;
365 opts->vo_screenheight = screens[screen].height;
366 xinerama_x = screens[screen].x_org;
367 xinerama_y = screens[screen].y_org;
369 XFree(screens);
371 #endif
372 aspect_save_screenres(vo, opts->vo_screenwidth, opts->vo_screenheight);
375 int vo_init(struct vo *vo)
377 struct MPOpts *opts = vo->opts;
378 struct vo_x11_state *x11 = vo->x11;
379 // int mScreen;
380 int depth, bpp;
381 unsigned int mask;
383 // char * DisplayName = ":0.0";
384 // Display * mDisplay;
385 XImage *mXImage = NULL;
387 // Window mRootWin;
388 XWindowAttributes attribs;
389 char *dispName;
391 if (vo_rootwin)
392 WinID = 0; // use root window
394 if (x11->depthonscreen)
396 saver_off(x11->display);
397 return 1; // already called
400 XSetErrorHandler(x11_errorhandler);
402 #if 0
403 if (!mDisplayName)
404 if (!(mDisplayName = getenv("DISPLAY")))
405 mDisplayName = strdup(":0.0");
406 #else
407 dispName = XDisplayName(mDisplayName);
408 #endif
410 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
412 x11->display = XOpenDisplay(dispName);
413 if (!x11->display)
415 mp_msg(MSGT_VO, MSGL_ERR,
416 "vo: couldn't open the X11 display (%s)!\n", dispName);
417 return 0;
419 x11->screen = DefaultScreen(x11->display); // screen ID
420 x11->rootwin = RootWindow(x11->display, x11->screen); // root window ID
422 init_atoms(vo->x11);
424 #ifdef CONFIG_XF86VM
426 int clock;
428 XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline);
429 if (!opts->vo_screenwidth)
430 opts->vo_screenwidth = modeline.hdisplay;
431 if (!opts->vo_screenheight)
432 opts->vo_screenheight = modeline.vdisplay;
434 #endif
436 if (!opts->vo_screenwidth)
437 opts->vo_screenwidth = DisplayWidth(x11->display, x11->screen);
438 if (!opts->vo_screenheight)
439 opts->vo_screenheight = DisplayHeight(x11->display, x11->screen);
441 // get color depth (from root window, or the best visual):
442 XGetWindowAttributes(x11->display, x11->rootwin, &attribs);
443 depth = attribs.depth;
445 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
447 Visual *visual;
449 depth = vo_find_depth_from_visuals(x11->display, x11->screen, &visual);
450 if (depth != -1)
451 mXImage = XCreateImage(x11->display, visual, depth, ZPixmap,
452 0, NULL, 1, 1, 8, 1);
453 } else
454 mXImage =
455 XGetImage(x11->display, x11->rootwin, 0, 0, 1, 1, AllPlanes, ZPixmap);
457 x11->depthonscreen = depth; // display depth on screen
459 // get bits/pixel from XImage structure:
460 if (mXImage == NULL)
462 mask = 0;
463 } else
466 * for the depth==24 case, the XImage structures might use
467 * 24 or 32 bits of data per pixel. The x11->depthonscreen
468 * field stores the amount of data per pixel in the
469 * XImage structure!
471 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
473 bpp = mXImage->bits_per_pixel;
474 if ((x11->depthonscreen + 7) / 8 != (bpp + 7) / 8)
475 x11->depthonscreen = bpp; // by A'rpi
476 mask =
477 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
478 mp_msg(MSGT_VO, MSGL_V,
479 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
480 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
481 XDestroyImage(mXImage);
483 if (((x11->depthonscreen + 7) / 8) == 2)
485 if (mask == 0x7FFF)
486 x11->depthonscreen = 15;
487 else if (mask == 0xFFFF)
488 x11->depthonscreen = 16;
490 // XCloseDisplay( mDisplay );
491 /* slightly improved local display detection AST */
492 if (strncmp(dispName, "unix:", 5) == 0)
493 dispName += 4;
494 else if (strncmp(dispName, "localhost:", 10) == 0)
495 dispName += 9;
496 if (*dispName == ':' && atoi(dispName + 1) < 10)
497 x11->display_is_local = 1;
498 else
499 x11->display_is_local = 0;
500 mp_msg(MSGT_VO, MSGL_V,
501 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
502 opts->vo_screenwidth, opts->vo_screenheight, depth, x11->depthonscreen,
503 dispName, x11->display_is_local ? "local" : "remote");
505 x11->wm_type = vo_wm_detect(vo);
507 x11->fs_type = vo_x11_get_fs_type(x11->wm_type);
509 fstype_dump(x11->fs_type);
511 saver_off(x11->display);
512 return 1;
515 void vo_uninit(struct vo_x11_state *x11)
517 if (!x11->display)
519 mp_msg(MSGT_VO, MSGL_V,
520 "vo: x11 uninit called but X11 not initialized..\n");
521 } else {
522 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
523 XSetErrorHandler(NULL);
524 XCloseDisplay(x11->display);
525 x11->depthonscreen = 0;
526 x11->display = NULL;
528 talloc_free(x11);
531 #include "wskeys.h"
533 #ifdef XF86XK_AudioPause
534 static const struct mp_keymap keysym_map[] = {
535 {XF86XK_MenuKB, KEY_MENU},
536 {XF86XK_AudioPlay, KEY_PLAY}, {XF86XK_AudioPause, KEY_PAUSE}, {XF86XK_AudioStop, KEY_STOP},
537 {XF86XK_AudioPrev, KEY_PREV}, {XF86XK_AudioNext, KEY_NEXT},
538 {XF86XK_AudioMute, KEY_MUTE}, {XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN}, {XF86XK_AudioRaiseVolume, KEY_VOLUME_UP},
539 {0, 0}
542 static void vo_x11_putkey_ext(struct vo *vo, int keysym, int modifiers)
544 struct mp_fifo *f = vo->key_fifo;
545 int mpkey = lookup_keymap_table(keysym_map, keysym);
546 if (mpkey)
547 mplayer_put_key(f, mpkey + modifiers);
549 #endif
551 static const struct mp_keymap keymap[] = {
552 // special keys
553 {wsEscape, KEY_ESC}, {wsBackSpace, KEY_BS}, {wsTab, KEY_TAB}, {wsEnter, KEY_ENTER},
555 // cursor keys
556 {wsLeft, KEY_LEFT}, {wsRight, KEY_RIGHT}, {wsUp, KEY_UP}, {wsDown, KEY_DOWN},
558 // navigation block
559 {wsInsert, KEY_INSERT}, {wsDelete, KEY_DELETE}, {wsHome, KEY_HOME}, {wsEnd, KEY_END},
560 {wsPageUp, KEY_PAGE_UP}, {wsPageDown, KEY_PAGE_DOWN},
562 // F-keys
563 {wsF1, KEY_F+1}, {wsF2, KEY_F+2}, {wsF3, KEY_F+3}, {wsF4, KEY_F+4},
564 {wsF5, KEY_F+5}, {wsF6, KEY_F+6}, {wsF7, KEY_F+7}, {wsF8, KEY_F+8},
565 {wsF9, KEY_F+9}, {wsF10, KEY_F+10}, {wsF11, KEY_F+11}, {wsF12, KEY_F+12},
567 // numpad independent of numlock
568 {wsGrayMinus, '-'}, {wsGrayPlus, '+'}, {wsGrayMul, '*'}, {wsGrayDiv, '/'},
569 {wsGrayEnter, KEY_KPENTER},
571 // numpad with numlock
572 {wsGray0, KEY_KP0}, {wsGray1, KEY_KP1}, {wsGray2, KEY_KP2},
573 {wsGray3, KEY_KP3}, {wsGray4, KEY_KP4}, {wsGray5, KEY_KP5},
574 {wsGray6, KEY_KP6}, {wsGray7, KEY_KP7}, {wsGray8, KEY_KP8},
575 {wsGray9, KEY_KP9}, {wsGrayDecimal, KEY_KPDEC},
577 // numpad without numlock
578 {wsGrayInsert, KEY_KPINS}, {wsGrayEnd, KEY_KP1}, {wsGrayDown, KEY_KP2},
579 {wsGrayPgDn, KEY_KP3}, {wsGrayLeft, KEY_KP4}, {wsGray5Dup, KEY_KP5},
580 {wsGrayRight, KEY_KP6}, {wsGrayHome, KEY_KP7}, {wsGrayUp, KEY_KP8},
581 {wsGrayPgUp, KEY_KP9}, {wsGrayDelete, KEY_KPDEL},
583 {0, 0}
586 static void vo_x11_putkey(struct vo *vo, int key, int modifiers)
588 static const char *passthrough_keys = " -+*/<>`~!@#$%^&()_{}:;\"\',.?\\|=[]";
589 int mpkey = 0;
590 if ((key >= 'a' && key <= 'z') ||
591 (key >= 'A' && key <= 'Z') ||
592 (key >= '0' && key <= '9') ||
593 (key > 0 && key < 256 && strchr(passthrough_keys, key)))
594 mpkey = key;
596 if (!mpkey)
597 mpkey = lookup_keymap_table(keymap, key);
599 if (mpkey)
600 mplayer_put_key(vo->key_fifo, mpkey + modifiers);
604 // ----- Motif header: -------
606 #define MWM_HINTS_FUNCTIONS (1L << 0)
607 #define MWM_HINTS_DECORATIONS (1L << 1)
608 #define MWM_HINTS_INPUT_MODE (1L << 2)
609 #define MWM_HINTS_STATUS (1L << 3)
611 #define MWM_FUNC_ALL (1L << 0)
612 #define MWM_FUNC_RESIZE (1L << 1)
613 #define MWM_FUNC_MOVE (1L << 2)
614 #define MWM_FUNC_MINIMIZE (1L << 3)
615 #define MWM_FUNC_MAXIMIZE (1L << 4)
616 #define MWM_FUNC_CLOSE (1L << 5)
618 #define MWM_DECOR_ALL (1L << 0)
619 #define MWM_DECOR_BORDER (1L << 1)
620 #define MWM_DECOR_RESIZEH (1L << 2)
621 #define MWM_DECOR_TITLE (1L << 3)
622 #define MWM_DECOR_MENU (1L << 4)
623 #define MWM_DECOR_MINIMIZE (1L << 5)
624 #define MWM_DECOR_MAXIMIZE (1L << 6)
626 #define MWM_INPUT_MODELESS 0
627 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
628 #define MWM_INPUT_SYSTEM_MODAL 2
629 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
630 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
632 #define MWM_TEAROFF_WINDOW (1L<<0)
634 typedef struct
636 long flags;
637 long functions;
638 long decorations;
639 long input_mode;
640 long state;
641 } MotifWmHints;
643 static MotifWmHints vo_MotifWmHints;
644 static Atom vo_MotifHints = None;
646 void vo_x11_decoration(struct vo *vo, int d)
648 struct vo_x11_state *x11 = vo->x11;
649 Atom mtype;
650 int mformat;
651 unsigned long mn, mb;
653 if (!WinID)
654 return;
656 if (vo_fsmode & 8)
658 XSetTransientForHint(x11->display, x11->window,
659 RootWindow(x11->display, x11->screen));
662 vo_MotifHints = XInternAtom(x11->display, "_MOTIF_WM_HINTS", 0);
663 if (vo_MotifHints != None)
665 if (!d)
667 MotifWmHints *mhints = NULL;
669 XGetWindowProperty(x11->display, x11->window,
670 vo_MotifHints, 0, 20, False,
671 vo_MotifHints, &mtype, &mformat, &mn,
672 &mb, (unsigned char **) &mhints);
673 if (mhints)
675 if (mhints->flags & MWM_HINTS_DECORATIONS)
676 x11->olddecor = mhints->decorations;
677 if (mhints->flags & MWM_HINTS_FUNCTIONS)
678 x11->oldfuncs = mhints->functions;
679 XFree(mhints);
683 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
684 vo_MotifWmHints.flags =
685 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
686 if (d)
688 vo_MotifWmHints.functions = x11->oldfuncs;
689 d = x11->olddecor;
691 #if 0
692 vo_MotifWmHints.decorations =
693 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
694 #else
695 vo_MotifWmHints.decorations =
696 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
697 #endif
698 XChangeProperty(x11->display, x11->window, vo_MotifHints,
699 vo_MotifHints, 32,
700 PropModeReplace,
701 (unsigned char *) &vo_MotifWmHints,
702 (vo_fsmode & 4) ? 4 : 5);
706 void vo_x11_classhint(struct vo *vo, Window window, const char *name)
708 struct MPOpts *opts = vo->opts;
709 struct vo_x11_state *x11 = vo->x11;
710 XClassHint wmClass;
711 pid_t pid = getpid();
713 wmClass.res_name = opts->vo_winname ? opts->vo_winname : (char *)name;
714 wmClass.res_class = "MPlayer";
715 XSetClassHint(x11->display, window, &wmClass);
716 XChangeProperty(x11->display, window, x11->XA_NET_WM_PID, XA_CARDINAL,
717 32, PropModeReplace, (unsigned char *) &pid, 1);
720 void vo_x11_uninit(struct vo *vo)
722 struct vo_x11_state *x11 = vo->x11;
723 saver_on(x11->display);
724 if (x11->window != None)
725 vo_showcursor(x11->display, x11->window);
727 if (x11->f_gc != None)
729 XFreeGC(vo->x11->display, x11->f_gc);
730 x11->f_gc = None;
733 if (x11->vo_gc != None)
735 XFreeGC(vo->x11->display, x11->vo_gc);
736 x11->vo_gc = None;
738 if (x11->window != None)
740 XClearWindow(x11->display, x11->window);
741 if (WinID < 0)
743 XEvent xev;
745 XUnmapWindow(x11->display, x11->window);
746 XSelectInput(x11->display, x11->window, StructureNotifyMask);
747 XDestroyWindow(x11->display, x11->window);
750 XNextEvent(x11->display, &xev);
752 while (xev.type != DestroyNotify
753 || xev.xdestroywindow.event != x11->window);
755 x11->window = None;
757 vo_fs = 0;
758 x11->vo_old_width = x11->vo_old_height = 0;
759 x11->last_video_width = 0;
760 x11->last_video_height = 0;
761 x11->size_changed_during_fs = false;
765 static int check_resize(struct vo *vo)
767 int old_w = vo->dwidth, old_h = vo->dheight;
768 int old_x = vo->dx, old_y = vo->dy;
769 int rc = 0;
770 vo_x11_update_geometry(vo, true);
771 if (vo->dwidth != old_w || vo->dheight != old_h)
772 rc |= VO_EVENT_RESIZE;
773 if (vo->dx != old_x || vo->dy != old_y)
774 rc |= VO_EVENT_MOVE;
775 return rc;
778 int vo_x11_check_events(struct vo *vo)
780 struct vo_x11_state *x11 = vo->x11;
781 Display *display = vo->x11->display;
782 int ret = 0;
783 XEvent Event;
784 char buf[100];
785 KeySym keySym;
787 if (x11->vo_mouse_autohide && x11->mouse_waiting_hide &&
788 (GetTimerMS() - x11->mouse_timer >= 1000)) {
789 vo_hidecursor(display, x11->window);
790 x11->mouse_waiting_hide = 0;
793 if (WinID > 0)
794 ret |= check_resize(vo);
795 while (XPending(display))
797 XNextEvent(display, &Event);
798 // printf("\rEvent.type=%X \n",Event.type);
799 switch (Event.type)
801 case Expose:
802 ret |= VO_EVENT_EXPOSE;
803 break;
804 case ConfigureNotify:
805 if (x11->window == None)
806 break;
807 ret |= check_resize(vo);
808 break;
809 case KeyPress:
811 int key;
813 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
814 &x11->compose_status);
815 int modifiers = 0;
816 if (Event.xkey.state & ShiftMask)
817 modifiers |= KEY_MODIFIER_SHIFT;
818 if (Event.xkey.state & ControlMask)
819 modifiers |= KEY_MODIFIER_CTRL;
820 if (Event.xkey.state & Mod1Mask)
821 modifiers |= KEY_MODIFIER_ALT;
822 if (Event.xkey.state & Mod4Mask)
823 modifiers |= KEY_MODIFIER_META;
824 #ifdef XF86XK_AudioPause
825 vo_x11_putkey_ext(vo, keySym, modifiers);
826 #endif
827 key =
828 ((keySym & 0xff00) !=
829 0 ? ((keySym & 0x00ff) + 256) : (keySym));
830 vo_x11_putkey(vo, key, modifiers);
831 ret |= VO_EVENT_KEYPRESS;
833 break;
834 case MotionNotify:
835 vo_mouse_movement(vo, Event.xmotion.x, Event.xmotion.y);
837 if (x11->vo_mouse_autohide)
839 vo_showcursor(display, x11->window);
840 x11->mouse_waiting_hide = 1;
841 x11->mouse_timer = GetTimerMS();
843 break;
844 case ButtonPress:
845 if (x11->vo_mouse_autohide)
847 vo_showcursor(display, x11->window);
848 x11->mouse_waiting_hide = 1;
849 x11->mouse_timer = GetTimerMS();
851 mplayer_put_key(vo->key_fifo,
852 (MOUSE_BTN0 + Event.xbutton.button - 1)
853 | MP_KEY_DOWN);
854 break;
855 case ButtonRelease:
856 if (x11->vo_mouse_autohide)
858 vo_showcursor(display, x11->window);
859 x11->mouse_waiting_hide = 1;
860 x11->mouse_timer = GetTimerMS();
862 mplayer_put_key(vo->key_fifo,
863 MOUSE_BTN0 + Event.xbutton.button - 1);
864 break;
865 case PropertyNotify:
867 char *name =
868 XGetAtomName(display, Event.xproperty.atom);
870 if (!name)
871 break;
873 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
875 XFree(name);
877 break;
878 case MapNotify:
879 x11->vo_hint.win_gravity = x11->old_gravity;
880 XSetWMNormalHints(display, x11->window, &x11->vo_hint);
881 x11->fs_flip = 0;
882 break;
883 case DestroyNotify:
884 mp_msg(MSGT_VO, MSGL_WARN, "Our window was destroyed, exiting\n");
885 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
886 break;
887 case ClientMessage:
888 if (Event.xclient.message_type == x11->XAWM_PROTOCOLS &&
889 Event.xclient.data.l[0] == x11->XAWM_DELETE_WINDOW)
890 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
891 break;
894 return ret;
898 * \brief sets the size and position of the non-fullscreen window.
900 static void vo_x11_nofs_sizepos(struct vo *vo, int x, int y,
901 int width, int height)
903 struct vo_x11_state *x11 = vo->x11;
904 if (width == x11->last_video_width && height == x11->last_video_height) {
905 if (!vo->opts->force_window_position && !x11->size_changed_during_fs)
906 return;
907 } else if (vo_fs)
908 x11->size_changed_during_fs = true;
909 x11->last_video_height = height;
910 x11->last_video_width = width;
911 vo_x11_sizehint(vo, x, y, width, height, 0);
912 if (vo_fs) {
913 x11->vo_old_x = x;
914 x11->vo_old_y = y;
915 x11->vo_old_width = width;
916 x11->vo_old_height = height;
918 else
920 vo->dwidth = width;
921 vo->dheight = height;
922 if (vo->opts->force_window_position)
923 XMoveResizeWindow(vo->x11->display, vo->x11->window, x, y, width,
924 height);
925 else
926 XResizeWindow(vo->x11->display, vo->x11->window, width, height);
930 void vo_x11_sizehint(struct vo *vo, int x, int y, int width, int height, int max)
932 struct vo_x11_state *x11 = vo->x11;
933 x11->vo_hint.flags = 0;
934 if (vo_keepaspect)
936 x11->vo_hint.flags |= PAspect;
937 x11->vo_hint.min_aspect.x = width;
938 x11->vo_hint.min_aspect.y = height;
939 x11->vo_hint.max_aspect.x = width;
940 x11->vo_hint.max_aspect.y = height;
943 x11->vo_hint.flags |= PPosition | PSize;
944 x11->vo_hint.x = x;
945 x11->vo_hint.y = y;
946 x11->vo_hint.width = width;
947 x11->vo_hint.height = height;
948 if (max)
950 x11->vo_hint.flags |= PMaxSize;
951 x11->vo_hint.max_width = width;
952 x11->vo_hint.max_height = height;
953 } else
955 x11->vo_hint.max_width = 0;
956 x11->vo_hint.max_height = 0;
959 // Set minimum height/width to 4 to avoid off-by-one errors
960 // and because mga_vid requires a minimal size of 4 pixels.
961 x11->vo_hint.flags |= PMinSize;
962 x11->vo_hint.min_width = x11->vo_hint.min_height = 4;
964 // Set the base size. A window manager might display the window
965 // size to the user relative to this.
966 // Setting these to width/height might be nice, but e.g. fluxbox can't handle it.
967 x11->vo_hint.flags |= PBaseSize;
968 x11->vo_hint.base_width = 0 /*width*/;
969 x11->vo_hint.base_height = 0 /*height*/;
971 x11->vo_hint.flags |= PWinGravity;
972 x11->vo_hint.win_gravity = StaticGravity;
973 XSetWMNormalHints(x11->display, x11->window, &x11->vo_hint);
976 static int vo_x11_get_gnome_layer(struct vo_x11_state *x11, Window win)
978 Atom type;
979 int format;
980 unsigned long nitems;
981 unsigned long bytesafter;
982 unsigned short *args = NULL;
984 if (XGetWindowProperty(x11->display, win, x11->XA_WIN_LAYER, 0, 16384,
985 False, AnyPropertyType, &type, &format, &nitems,
986 &bytesafter,
987 (unsigned char **) &args) == Success
988 && nitems > 0 && args)
990 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
991 *args);
992 return *args;
994 return WIN_LAYER_NORMAL;
998 static Window vo_x11_create_smooth_window(struct vo_x11_state *x11, Window mRoot,
999 Visual * vis, int x, int y,
1000 unsigned int width, unsigned int height,
1001 int depth, Colormap col_map)
1003 unsigned long xswamask = CWBorderPixel;
1004 XSetWindowAttributes xswa;
1005 Window ret_win;
1007 if (col_map != CopyFromParent)
1009 xswa.colormap = col_map;
1010 xswamask |= CWColormap;
1012 xswa.background_pixel = 0;
1013 xswa.border_pixel = 0;
1014 xswa.backing_store = NotUseful;
1015 xswa.bit_gravity = StaticGravity;
1017 ret_win =
1018 XCreateWindow(x11->display, x11->rootwin, x, y, width, height, 0, depth,
1019 CopyFromParent, vis, xswamask, &xswa);
1020 XSetWMProtocols(x11->display, ret_win, &x11->XAWM_DELETE_WINDOW, 1);
1021 if (x11->f_gc == None)
1022 x11->f_gc = XCreateGC(x11->display, ret_win, 0, 0);
1023 XSetForeground(x11->display, x11->f_gc, 0);
1025 return ret_win;
1029 * \brief create and setup a window suitable for display
1030 * \param vis Visual to use for creating the window
1031 * \param x x position of window
1032 * \param y y position of window
1033 * \param width width of window
1034 * \param height height of window
1035 * \param flags flags for window creation.
1036 * Only VOFLAG_FULLSCREEN is supported so far.
1037 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1038 * \param classname name to use for the classhint
1039 * \param title title for the window
1041 * This also does the grunt-work like setting Window Manager hints etc.
1042 * If vo_window is already set it just moves and resizes it.
1044 void vo_x11_create_vo_window(struct vo *vo, XVisualInfo *vis, int x, int y,
1045 unsigned int width, unsigned int height, int flags,
1046 Colormap col_map,
1047 const char *classname, const char *title)
1049 struct MPOpts *opts = vo->opts;
1050 struct vo_x11_state *x11 = vo->x11;
1051 Display *mDisplay = vo->x11->display;
1052 if (WinID >= 0) {
1053 vo_fs = flags & VOFLAG_FULLSCREEN;
1054 x11->window = WinID ? (Window)WinID : x11->rootwin;
1055 if (col_map != CopyFromParent) {
1056 unsigned long xswamask = CWColormap;
1057 XSetWindowAttributes xswa;
1058 xswa.colormap = col_map;
1059 XChangeWindowAttributes(mDisplay, x11->window, xswamask, &xswa);
1060 XInstallColormap(mDisplay, col_map);
1062 if (WinID) {
1063 // Expose events can only really be handled by us, so request them.
1064 vo_x11_selectinput_witherr(mDisplay, x11->window, ExposureMask);
1065 } else
1066 // Do not capture events since it might break the parent application
1067 // if it relies on events being forwarded to the parent of WinID.
1068 // It also is consistent with the w32_common.c code.
1069 vo_x11_selectinput_witherr(mDisplay, x11->window,
1070 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1071 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1073 vo_x11_update_geometry(vo, true);
1074 goto final;
1076 if (x11->window == None) {
1077 vo_fs = 0;
1078 vo->dwidth = width;
1079 vo->dheight = height;
1080 x11->window = vo_x11_create_smooth_window(x11, x11->rootwin, vis->visual,
1081 x, y, width, height, vis->depth, col_map);
1082 x11->window_state = VOFLAG_HIDDEN;
1084 if (flags & VOFLAG_HIDDEN)
1085 goto final;
1086 if (x11->window_state & VOFLAG_HIDDEN) {
1087 XSizeHints hint;
1088 x11->window_state &= ~VOFLAG_HIDDEN;
1089 vo_x11_classhint(vo, x11->window, classname);
1090 XStoreName(mDisplay, x11->window, title);
1091 vo_hidecursor(mDisplay, x11->window);
1092 XSelectInput(mDisplay, x11->window, StructureNotifyMask);
1093 hint.x = x; hint.y = y;
1094 hint.width = width; hint.height = height;
1095 hint.flags = PSize;
1096 if (geometry_xy_changed)
1097 hint.flags |= PPosition;
1098 XSetStandardProperties(mDisplay, x11->window, title, title, None, NULL, 0, &hint);
1099 if (!vo_border) vo_x11_decoration(vo, 0);
1100 // map window
1101 XMapWindow(mDisplay, x11->window);
1102 vo_x11_clearwindow(vo, x11->window);
1103 XSelectInput(mDisplay, x11->window, NoEventMask);
1104 XSync(mDisplay, False);
1105 vo_x11_selectinput_witherr(mDisplay, x11->window,
1106 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1107 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1109 if (opts->vo_ontop) vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1110 vo_x11_update_geometry(vo, !geometry_xy_changed);
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, 0, NULL);
1125 XSync(mDisplay, False);
1126 x11->vo_mouse_autohide = 1;
1127 vo->event_fd = ConnectionNumber(x11->display);
1130 void vo_x11_clearwindow_part(struct vo *vo, Window vo_window,
1131 int img_width, int img_height, int use_fs)
1133 struct vo_x11_state *x11 = vo->x11;
1134 struct MPOpts *opts = vo->opts;
1135 Display *mDisplay = vo->x11->display;
1136 int u_dheight, u_dwidth, left_ov, left_ov2;
1138 if (x11->f_gc == None)
1139 return;
1141 u_dheight = use_fs ? opts->vo_screenheight : vo->dheight;
1142 u_dwidth = use_fs ? opts->vo_screenwidth : vo->dwidth;
1143 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1144 return;
1146 left_ov = (u_dheight - img_height) / 2;
1147 left_ov2 = (u_dwidth - img_width) / 2;
1149 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, 0, u_dwidth, left_ov);
1150 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, u_dheight - left_ov - 1,
1151 u_dwidth, left_ov + 1);
1153 if (u_dwidth > img_width)
1155 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, left_ov, left_ov2,
1156 img_height);
1157 XFillRectangle(mDisplay, vo_window, x11->f_gc, u_dwidth - left_ov2 - 1,
1158 left_ov, left_ov2 + 1, img_height);
1161 XFlush(mDisplay);
1164 void vo_x11_clearwindow(struct vo *vo, Window vo_window)
1166 struct vo_x11_state *x11 = vo->x11;
1167 struct MPOpts *opts = vo->opts;
1168 if (x11->f_gc == None)
1169 return;
1170 XFillRectangle(x11->display, vo_window, x11->f_gc, 0, 0,
1171 opts->vo_screenwidth, opts->vo_screenheight);
1173 XFlush(x11->display);
1177 void vo_x11_setlayer(struct vo *vo, Window vo_window, int layer)
1179 struct vo_x11_state *x11 = vo->x11;
1180 if (WinID >= 0)
1181 return;
1183 if (x11->fs_type & vo_wm_LAYER)
1185 XClientMessageEvent xev;
1187 if (!x11->orig_layer)
1188 x11->orig_layer = vo_x11_get_gnome_layer(x11, vo_window);
1190 memset(&xev, 0, sizeof(xev));
1191 xev.type = ClientMessage;
1192 xev.display = x11->display;
1193 xev.window = vo_window;
1194 xev.message_type = x11->XA_WIN_LAYER;
1195 xev.format = 32;
1196 xev.data.l[0] = layer ? fs_layer : x11->orig_layer; // if not fullscreen, stay on default layer
1197 xev.data.l[1] = CurrentTime;
1198 mp_msg(MSGT_VO, MSGL_V,
1199 "[x11] Layered style stay on top (layer %ld).\n",
1200 xev.data.l[0]);
1201 XSendEvent(x11->display, x11->rootwin, False, SubstructureNotifyMask,
1202 (XEvent *) & xev);
1203 } else if (x11->fs_type & vo_wm_NETWM)
1205 XClientMessageEvent xev;
1206 char *state;
1208 memset(&xev, 0, sizeof(xev));
1209 xev.type = ClientMessage;
1210 xev.message_type = x11->XA_NET_WM_STATE;
1211 xev.display = x11->display;
1212 xev.window = vo_window;
1213 xev.format = 32;
1214 xev.data.l[0] = layer;
1216 if (x11->fs_type & vo_wm_STAYS_ON_TOP)
1217 xev.data.l[1] = x11->XA_NET_WM_STATE_STAYS_ON_TOP;
1218 else if (x11->fs_type & vo_wm_ABOVE)
1219 xev.data.l[1] = x11->XA_NET_WM_STATE_ABOVE;
1220 else if (x11->fs_type & vo_wm_FULLSCREEN)
1221 xev.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
1222 else if (x11->fs_type & vo_wm_BELOW)
1223 // This is not fallback. We can safely assume that the situation
1224 // where only NETWM_STATE_BELOW is supported doesn't exist.
1225 xev.data.l[1] = x11->XA_NET_WM_STATE_BELOW;
1227 XSendEvent(x11->display, x11->rootwin, False, SubstructureRedirectMask,
1228 (XEvent *) & xev);
1229 state = XGetAtomName(x11->display, xev.data.l[1]);
1230 mp_msg(MSGT_VO, MSGL_V,
1231 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1232 layer, state);
1233 XFree(state);
1237 static int vo_x11_get_fs_type(int supported)
1239 int i;
1240 int type = supported;
1242 if (vo_fstype_list)
1244 for (i = 0; vo_fstype_list[i]; i++)
1246 int neg = 0;
1247 char *arg = vo_fstype_list[i];
1249 if (vo_fstype_list[i][0] == '-')
1251 neg = 1;
1252 arg = vo_fstype_list[i] + 1;
1255 if (!strncmp(arg, "layer", 5))
1257 if (!neg && (arg[5] == '='))
1259 char *endptr = NULL;
1260 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1262 if (endptr && *endptr == '\0' && layer >= 0
1263 && layer <= 15)
1264 fs_layer = layer;
1266 if (neg)
1267 type &= ~vo_wm_LAYER;
1268 else
1269 type |= vo_wm_LAYER;
1270 } else if (!strcmp(arg, "above"))
1272 if (neg)
1273 type &= ~vo_wm_ABOVE;
1274 else
1275 type |= vo_wm_ABOVE;
1276 } else if (!strcmp(arg, "fullscreen"))
1278 if (neg)
1279 type &= ~vo_wm_FULLSCREEN;
1280 else
1281 type |= vo_wm_FULLSCREEN;
1282 } else if (!strcmp(arg, "stays_on_top"))
1284 if (neg)
1285 type &= ~vo_wm_STAYS_ON_TOP;
1286 else
1287 type |= vo_wm_STAYS_ON_TOP;
1288 } else if (!strcmp(arg, "below"))
1290 if (neg)
1291 type &= ~vo_wm_BELOW;
1292 else
1293 type |= vo_wm_BELOW;
1294 } else if (!strcmp(arg, "netwm"))
1296 if (neg)
1297 type &= ~vo_wm_NETWM;
1298 else
1299 type |= vo_wm_NETWM;
1300 } else if (!strcmp(arg, "none"))
1301 type = 0; // clear; keep parsing
1305 return type;
1309 * \brief update vo->dx, vo->dy, vo->dwidth and vo->dheight with current values of vo->x11->window
1310 * \return returns current color depth of vo->x11->window
1312 int vo_x11_update_geometry(struct vo *vo, bool update_pos)
1314 struct MPOpts *opts = vo->opts;
1315 struct vo_x11_state *x11 = vo->x11;
1316 unsigned depth, w, h;
1317 int dummy_int;
1318 Window dummy_win;
1319 XGetGeometry(x11->display, x11->window, &dummy_win, &dummy_int, &dummy_int,
1320 &w, &h, &dummy_int, &depth);
1321 if (w <= INT_MAX && h <= INT_MAX) {
1322 vo->dwidth = w;
1323 vo->dheight = h;
1325 if (update_pos)
1326 XTranslateCoordinates(x11->display, x11->window, x11->rootwin, 0, 0,
1327 &vo->dx, &vo->dy, &dummy_win);
1328 if (opts->vo_wintitle)
1329 XStoreName(x11->display, x11->window, opts->vo_wintitle);
1331 return depth <= INT_MAX ? depth : 0;
1334 void vo_x11_fullscreen(struct vo *vo)
1336 struct MPOpts *opts = vo->opts;
1337 struct vo_x11_state *x11 = vo->x11;
1338 int x, y, w, h;
1339 x = x11->vo_old_x;
1340 y = x11->vo_old_y;
1341 w = x11->vo_old_width;
1342 h = x11->vo_old_height;
1344 if (WinID >= 0) {
1345 vo_fs = !vo_fs;
1346 return;
1348 if (x11->fs_flip)
1349 return;
1351 if (vo_fs)
1353 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1354 vo_fs = VO_FALSE;
1355 if (x11->size_changed_during_fs && (x11->fs_type & vo_wm_FULLSCREEN))
1356 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, x11->last_video_width,
1357 x11->last_video_height);
1358 x11->size_changed_during_fs = false;
1359 } else
1361 // win->fs
1362 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1364 vo_fs = VO_TRUE;
1365 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1367 x11->vo_old_x = vo->dx;
1368 x11->vo_old_y = vo->dy;
1369 x11->vo_old_width = vo->dwidth;
1370 x11->vo_old_height = vo->dheight;
1372 update_xinerama_info(vo);
1373 x = xinerama_x;
1374 y = xinerama_y;
1375 w = opts->vo_screenwidth;
1376 h = opts->vo_screenheight;
1379 long dummy;
1381 XGetWMNormalHints(x11->display, x11->window, &x11->vo_hint, &dummy);
1382 if (!(x11->vo_hint.flags & PWinGravity))
1383 x11->old_gravity = NorthWestGravity;
1384 else
1385 x11->old_gravity = x11->vo_hint.win_gravity;
1387 if (x11->wm_type == 0 && !(vo_fsmode & 16))
1389 XUnmapWindow(x11->display, x11->window); // required for MWM
1390 XWithdrawWindow(x11->display, x11->window, x11->screen);
1391 x11->fs_flip = 1;
1394 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1396 vo_x11_decoration(vo, vo_border && !vo_fs);
1397 vo_x11_sizehint(vo, x, y, w, h, 0);
1398 vo_x11_setlayer(vo, x11->window, vo_fs);
1401 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1403 /* some WMs lose ontop after fullscreen */
1404 if ((!(vo_fs)) & opts->vo_ontop)
1405 vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1407 XMapRaised(x11->display, x11->window);
1408 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1409 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1410 XRaiseWindow(x11->display, x11->window);
1411 XFlush(x11->display);
1414 void vo_x11_ontop(struct vo *vo)
1416 struct MPOpts *opts = vo->opts;
1417 opts->vo_ontop = !opts->vo_ontop;
1419 vo_x11_setlayer(vo, vo->x11->window, opts->vo_ontop);
1422 void vo_x11_border(struct vo *vo)
1424 vo_border = !vo_border;
1425 vo_x11_decoration(vo, vo_border && !vo_fs);
1429 * XScreensaver stuff
1432 static int screensaver_off;
1433 static unsigned int time_last;
1435 void xscreensaver_heartbeat(struct vo_x11_state *x11)
1437 unsigned int time = GetTimerMS();
1439 if (x11->display && screensaver_off && (time - time_last) > 30000)
1441 time_last = time;
1443 XResetScreenSaver(x11->display);
1447 static int xss_suspend(Display *mDisplay, Bool suspend)
1449 #ifndef CONFIG_XSS
1450 return 0;
1451 #else
1452 int event, error, major, minor;
1453 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1454 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1455 return 0;
1456 if (major < 1 || (major == 1 && minor < 1))
1457 return 0;
1458 XScreenSaverSuspend(mDisplay, suspend);
1459 return 1;
1460 #endif
1464 * End of XScreensaver stuff
1467 static void saver_on(Display * mDisplay)
1470 if (!screensaver_off)
1471 return;
1472 screensaver_off = 0;
1473 if (xss_suspend(mDisplay, False))
1474 return;
1475 #ifdef CONFIG_XDPMS
1476 if (dpms_disabled)
1478 int nothing;
1479 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1481 if (!DPMSEnable(mDisplay))
1482 { // restoring power saving settings
1483 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1484 } else
1486 // DPMS does not seem to be enabled unless we call DPMSInfo
1487 BOOL onoff;
1488 CARD16 state;
1490 DPMSForceLevel(mDisplay, DPMSModeOn);
1491 DPMSInfo(mDisplay, &state, &onoff);
1492 if (onoff)
1494 mp_msg(MSGT_VO, MSGL_V,
1495 "Successfully enabled DPMS\n");
1496 } else
1498 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1502 dpms_disabled = 0;
1504 #endif
1507 static void saver_off(Display * mDisplay)
1509 int nothing;
1511 if (screensaver_off)
1512 return;
1513 screensaver_off = 1;
1514 if (xss_suspend(mDisplay, True))
1515 return;
1516 #ifdef CONFIG_XDPMS
1517 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1519 BOOL onoff;
1520 CARD16 state;
1522 DPMSInfo(mDisplay, &state, &onoff);
1523 if (onoff)
1525 Status stat;
1527 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1528 dpms_disabled = 1;
1529 stat = DPMSDisable(mDisplay); // monitor powersave off
1530 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1533 #endif
1536 static XErrorHandler old_handler = NULL;
1537 static int selectinput_err = 0;
1538 static int x11_selectinput_errorhandler(Display * display,
1539 XErrorEvent * event)
1541 if (event->error_code == BadAccess)
1543 selectinput_err = 1;
1544 mp_msg(MSGT_VO, MSGL_ERR,
1545 "X11 error: BadAccess during XSelectInput Call\n");
1546 mp_msg(MSGT_VO, MSGL_ERR,
1547 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1548 /* If you think MPlayer should shutdown with this error,
1549 * comment out the following line */
1550 return 0;
1552 if (old_handler != NULL)
1553 old_handler(display, event);
1554 else
1555 x11_errorhandler(display, event);
1556 return 0;
1559 void vo_x11_selectinput_witherr(Display * display, Window w,
1560 long event_mask)
1562 XSync(display, False);
1563 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1564 selectinput_err = 0;
1565 if (vo_nomouse_input)
1567 XSelectInput(display, w,
1568 event_mask &
1569 (~(ButtonPressMask | ButtonReleaseMask)));
1570 } else
1572 XSelectInput(display, w, event_mask);
1574 XSync(display, False);
1575 XSetErrorHandler(old_handler);
1576 if (selectinput_err)
1578 mp_msg(MSGT_VO, MSGL_ERR,
1579 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1580 XSelectInput(display, w,
1581 event_mask &
1583 (ButtonPressMask | ButtonReleaseMask |
1584 PointerMotionMask)));
1588 #ifdef CONFIG_XF86VM
1589 void vo_vm_switch(struct vo *vo)
1591 struct vo_x11_state *x11 = vo->x11;
1592 struct MPOpts *opts = vo->opts;
1593 Display *mDisplay = x11->display;
1594 int vm_event, vm_error;
1595 int vm_ver, vm_rev;
1596 int i, j, have_vm = 0;
1597 int X = vo->dwidth, Y = vo->dheight;
1598 int modeline_width, modeline_height;
1600 int modecount;
1602 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1604 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1605 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1606 vm_rev);
1607 have_vm = 1;
1608 } else {
1609 mp_msg(MSGT_VO, MSGL_WARN,
1610 "XF86VidMode extension not available.\n");
1613 if (have_vm)
1615 if (vidmodes == NULL)
1616 XF86VidModeGetAllModeLines(mDisplay, x11->screen, &modecount,
1617 &vidmodes);
1618 j = 0;
1619 modeline_width = vidmodes[0]->hdisplay;
1620 modeline_height = vidmodes[0]->vdisplay;
1622 for (i = 1; i < modecount; i++)
1623 if ((vidmodes[i]->hdisplay >= X)
1624 && (vidmodes[i]->vdisplay >= Y))
1625 if ((vidmodes[i]->hdisplay <= modeline_width)
1626 && (vidmodes[i]->vdisplay <= modeline_height))
1628 modeline_width = vidmodes[i]->hdisplay;
1629 modeline_height = vidmodes[i]->vdisplay;
1630 j = i;
1633 mp_tmsg(MSGT_VO, MSGL_INFO, "XF86VM: Selected video mode %dx%d for image size %dx%d.\n",
1634 modeline_width, modeline_height, X, Y);
1635 XF86VidModeLockModeSwitch(mDisplay, x11->screen, 0);
1636 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1637 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1639 // FIXME: all this is more of a hack than proper solution
1640 X = (opts->vo_screenwidth - modeline_width) / 2;
1641 Y = (opts->vo_screenheight - modeline_height) / 2;
1642 XF86VidModeSetViewPort(mDisplay, x11->screen, X, Y);
1643 vo->dx = X;
1644 vo->dy = Y;
1645 vo->dwidth = modeline_width;
1646 vo->dheight = modeline_height;
1647 aspect_save_screenres(vo, modeline_width, modeline_height);
1651 void vo_vm_close(struct vo *vo)
1653 Display *dpy = vo->x11->display;
1654 struct MPOpts *opts = vo->opts;
1655 if (vidmodes != NULL)
1657 int i, modecount;
1659 free(vidmodes);
1660 vidmodes = NULL;
1661 XF86VidModeGetAllModeLines(dpy, vo->x11->screen, &modecount,
1662 &vidmodes);
1663 for (i = 0; i < modecount; i++)
1664 if ((vidmodes[i]->hdisplay == opts->vo_screenwidth)
1665 && (vidmodes[i]->vdisplay == opts->vo_screenheight))
1667 mp_msg(MSGT_VO, MSGL_INFO,
1668 "Returning to original mode %dx%d\n",
1669 opts->vo_screenwidth, opts->vo_screenheight);
1670 break;
1673 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1674 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1675 free(vidmodes);
1676 vidmodes = NULL;
1680 double vo_vm_get_fps(struct vo *vo)
1682 struct vo_x11_state *x11 = vo->x11;
1683 int clock;
1684 XF86VidModeModeLine modeline;
1685 if (!XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline))
1686 return 0;
1687 if (modeline.privsize)
1688 XFree(modeline.private);
1689 return 1e3 * clock / modeline.htotal / modeline.vtotal;
1691 #endif
1693 #endif /* X11_FULLSCREEN */
1697 * Scan the available visuals on this Display/Screen. Try to find
1698 * the 'best' available TrueColor visual that has a decent color
1699 * depth (at least 15bit). If there are multiple visuals with depth
1700 * >= 15bit, we prefer visuals with a smaller color depth.
1702 int vo_find_depth_from_visuals(Display * dpy, int screen,
1703 Visual ** visual_return)
1705 XVisualInfo visual_tmpl;
1706 XVisualInfo *visuals;
1707 int nvisuals, i;
1708 int bestvisual = -1;
1709 int bestvisual_depth = -1;
1711 visual_tmpl.screen = screen;
1712 visual_tmpl.class = TrueColor;
1713 visuals = XGetVisualInfo(dpy,
1714 VisualScreenMask | VisualClassMask,
1715 &visual_tmpl, &nvisuals);
1716 if (visuals != NULL)
1718 for (i = 0; i < nvisuals; i++)
1720 mp_msg(MSGT_VO, MSGL_V,
1721 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1722 visuals[i].visualid, visuals[i].depth,
1723 visuals[i].red_mask, visuals[i].green_mask,
1724 visuals[i].blue_mask);
1726 * Save the visual index and its depth, if this is the first
1727 * truecolor visul, or a visual that is 'preferred' over the
1728 * previous 'best' visual.
1730 if (bestvisual_depth == -1
1731 || (visuals[i].depth >= 15
1732 && (visuals[i].depth < bestvisual_depth
1733 || bestvisual_depth < 15)))
1735 bestvisual = i;
1736 bestvisual_depth = visuals[i].depth;
1740 if (bestvisual != -1 && visual_return != NULL)
1741 *visual_return = visuals[bestvisual].visual;
1743 XFree(visuals);
1745 return bestvisual_depth;
1749 static Colormap cmap = None;
1750 static XColor cols[256];
1751 static int cm_size, red_mask, green_mask, blue_mask;
1754 Colormap vo_x11_create_colormap(struct vo *vo, XVisualInfo *vinfo)
1756 struct vo_x11_state *x11 = vo->x11;
1757 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1759 if (vinfo->class != DirectColor)
1760 return XCreateColormap(x11->display, x11->rootwin, vinfo->visual,
1761 AllocNone);
1763 /* can this function get called twice or more? */
1764 if (cmap)
1765 return cmap;
1766 cm_size = vinfo->colormap_size;
1767 red_mask = vinfo->red_mask;
1768 green_mask = vinfo->green_mask;
1769 blue_mask = vinfo->blue_mask;
1770 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1771 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1772 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1773 rvu = 65536ull * ru / (red_mask + ru);
1774 gvu = 65536ull * gu / (green_mask + gu);
1775 bvu = 65536ull * bu / (blue_mask + bu);
1776 r = g = b = 0;
1777 rv = gv = bv = 0;
1778 m = DoRed | DoGreen | DoBlue;
1779 for (k = 0; k < cm_size; k++)
1781 int t;
1783 cols[k].pixel = r | g | b;
1784 cols[k].red = rv;
1785 cols[k].green = gv;
1786 cols[k].blue = bv;
1787 cols[k].flags = m;
1788 t = (r + ru) & red_mask;
1789 if (t < r)
1790 m &= ~DoRed;
1791 r = t;
1792 t = (g + gu) & green_mask;
1793 if (t < g)
1794 m &= ~DoGreen;
1795 g = t;
1796 t = (b + bu) & blue_mask;
1797 if (t < b)
1798 m &= ~DoBlue;
1799 b = t;
1800 rv += rvu;
1801 gv += gvu;
1802 bv += bvu;
1804 cmap = XCreateColormap(x11->display, x11->rootwin, vinfo->visual, AllocAll);
1805 XStoreColors(x11->display, cmap, cols, cm_size);
1806 return cmap;
1810 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1811 * hue and red/green/blue intensity, but we cannot do saturation.
1812 * Currently only gamma, brightness and contrast are implemented.
1813 * Is there sufficient interest for hue and/or red/green/blue intensity?
1815 /* these values have range [-100,100] and are initially 0 */
1816 static int vo_gamma = 0;
1817 static int vo_brightness = 0;
1818 static int vo_contrast = 0;
1820 static int transform_color(float val,
1821 float brightness, float contrast, float gamma) {
1822 float s = pow(val, gamma);
1823 s = (s - 0.5) * contrast + 0.5;
1824 s += brightness;
1825 if (s < 0)
1826 s = 0;
1827 if (s > 1)
1828 s = 1;
1829 return (unsigned short) (s * 65535);
1832 uint32_t vo_x11_set_equalizer(struct vo *vo, const char *name, int value)
1834 float gamma, brightness, contrast;
1835 float rf, gf, bf;
1836 int k;
1839 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1840 * of TrueColor-ed window but be careful:
1841 * Unlike the colormaps, which are private for the X client
1842 * who created them and thus automatically destroyed on client
1843 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1844 * setting and _must_ be restored before the process exits.
1845 * Unforunately when the process crashes (or gets killed
1846 * for some reason) it is impossible to restore the setting,
1847 * and such behaviour could be rather annoying for the users.
1849 if (cmap == None)
1850 return VO_NOTAVAIL;
1852 if (!strcasecmp(name, "brightness"))
1853 vo_brightness = value;
1854 else if (!strcasecmp(name, "contrast"))
1855 vo_contrast = value;
1856 else if (!strcasecmp(name, "gamma"))
1857 vo_gamma = value;
1858 else
1859 return VO_NOTIMPL;
1861 brightness = 0.01 * vo_brightness;
1862 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1863 gamma = pow(2, -0.02 * vo_gamma);
1865 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1866 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
1867 green_mask;
1868 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
1870 /* now recalculate the colormap using the newly set value */
1871 for (k = 0; k < cm_size; k++)
1873 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
1874 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
1875 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
1878 XStoreColors(vo->x11->display, cmap, cols, cm_size);
1879 XFlush(vo->x11->display);
1880 return VO_TRUE;
1883 uint32_t vo_x11_get_equalizer(const char *name, int *value)
1885 if (cmap == None)
1886 return VO_NOTAVAIL;
1887 if (!strcasecmp(name, "brightness"))
1888 *value = vo_brightness;
1889 else if (!strcasecmp(name, "contrast"))
1890 *value = vo_contrast;
1891 else if (!strcasecmp(name, "gamma"))
1892 *value = vo_gamma;
1893 else
1894 return VO_NOTIMPL;
1895 return VO_TRUE;
1898 #ifdef CONFIG_XV
1899 int vo_xv_set_eq(struct vo *vo, uint32_t xv_port, const char *name, int value)
1901 XvAttribute *attributes;
1902 int i, howmany, xv_atom;
1904 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
1906 /* get available attributes */
1907 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1908 for (i = 0; i < howmany && attributes; i++)
1909 if (attributes[i].flags & XvSettable)
1911 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1912 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1913 then trigger it if it's ok so that the other values are at default upon query */
1914 if (xv_atom != None)
1916 int hue = 0, port_value, port_min, port_max;
1918 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1919 (!strcasecmp(name, "brightness")))
1920 port_value = value;
1921 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1922 (!strcasecmp(name, "contrast")))
1923 port_value = value;
1924 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1925 (!strcasecmp(name, "saturation")))
1926 port_value = value;
1927 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1928 (!strcasecmp(name, "hue")))
1930 port_value = value;
1931 hue = 1;
1932 } else
1933 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1934 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1935 (!strcasecmp(name, "red_intensity")))
1936 port_value = value;
1937 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1938 && (!strcasecmp(name, "green_intensity")))
1939 port_value = value;
1940 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1941 && (!strcasecmp(name, "blue_intensity")))
1942 port_value = value;
1943 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
1944 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
1945 && (!strcasecmp(name, "bt_709")))
1946 port_value = value;
1947 else
1948 continue;
1950 port_min = attributes[i].min_value;
1951 port_max = attributes[i].max_value;
1953 /* nvidia hue workaround */
1954 if (hue && port_min == 0 && port_max == 360)
1956 port_value =
1957 (port_value >=
1958 0) ? (port_value - 100) : (port_value + 100);
1960 // -100 -> min
1961 // 0 -> (max+min)/2
1962 // +100 -> max
1963 port_value =
1964 (port_value + 100) * (port_max - port_min) / 200 +
1965 port_min;
1966 XvSetPortAttribute(vo->x11->display, xv_port, xv_atom, port_value);
1967 return VO_TRUE;
1970 return VO_FALSE;
1973 int vo_xv_get_eq(struct vo *vo, uint32_t xv_port, const char *name, int *value)
1976 XvAttribute *attributes;
1977 int i, howmany, xv_atom;
1979 /* get available attributes */
1980 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1981 for (i = 0; i < howmany && attributes; i++)
1982 if (attributes[i].flags & XvGettable)
1984 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1985 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1986 then trigger it if it's ok so that the other values are at default upon query */
1987 if (xv_atom != None)
1989 int val, port_value = 0, port_min, port_max;
1991 XvGetPortAttribute(vo->x11->display, xv_port, xv_atom,
1992 &port_value);
1994 port_min = attributes[i].min_value;
1995 port_max = attributes[i].max_value;
1996 val =
1997 (port_value - port_min) * 200 / (port_max - port_min) -
1998 100;
2000 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2001 (!strcasecmp(name, "brightness")))
2002 *value = val;
2003 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2004 (!strcasecmp(name, "contrast")))
2005 *value = val;
2006 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2007 (!strcasecmp(name, "saturation")))
2008 *value = val;
2009 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2010 (!strcasecmp(name, "hue")))
2012 /* nasty nvidia detect */
2013 if (port_min == 0 && port_max == 360)
2014 *value = (val >= 0) ? (val - 100) : (val + 100);
2015 else
2016 *value = val;
2017 } else
2018 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2019 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2020 (!strcasecmp(name, "red_intensity")))
2021 *value = val;
2022 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2023 && (!strcasecmp(name, "green_intensity")))
2024 *value = val;
2025 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2026 && (!strcasecmp(name, "blue_intensity")))
2027 *value = val;
2028 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
2029 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
2030 && (!strcasecmp(name, "bt_709")))
2031 *value = val;
2032 else
2033 continue;
2035 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2036 name, *value);
2037 return VO_TRUE;
2040 return VO_FALSE;
2044 * \brief Interns the requested atom if it is available.
2046 * \param atom_name String containing the name of the requested atom.
2048 * \return Returns the atom if available, else None is returned.
2051 static Atom xv_intern_atom_if_exists(struct vo_x11_state *x11,
2052 char const *atom_name)
2054 XvAttribute * attributes;
2055 int attrib_count,i;
2056 Atom xv_atom = None;
2058 attributes = XvQueryPortAttributes(x11->display, x11->xv_port, &attrib_count );
2059 if( attributes!=NULL )
2061 for ( i = 0; i < attrib_count; ++i )
2063 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2065 xv_atom = XInternAtom(x11->display, atom_name, False );
2066 break; // found what we want, break out
2069 XFree( attributes );
2072 return xv_atom;
2076 * \brief Try to enable vsync for xv.
2077 * \return Returns -1 if not available, 0 on failure and 1 on success.
2079 int vo_xv_enable_vsync(struct vo *vo)
2081 struct vo_x11_state *x11 = vo->x11;
2082 Atom xv_atom = xv_intern_atom_if_exists(x11, "XV_SYNC_TO_VBLANK");
2083 if (xv_atom == None)
2084 return -1;
2085 return XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1) == Success;
2089 * \brief Get maximum supported source image dimensions.
2091 * This function does not set the variables pointed to by
2092 * width and height if the information could not be retrieved,
2093 * so the caller is reponsible for properly initializing them.
2095 * \param width [out] The maximum width gets stored here.
2096 * \param height [out] The maximum height gets stored here.
2099 void vo_xv_get_max_img_dim(struct vo *vo, uint32_t * width, uint32_t * height)
2101 struct vo_x11_state *x11 = vo->x11;
2102 XvEncodingInfo * encodings;
2103 //unsigned long num_encodings, idx; to int or too long?!
2104 unsigned int num_encodings, idx;
2106 XvQueryEncodings(x11->display, x11->xv_port, &num_encodings, &encodings);
2108 if ( encodings )
2110 for ( idx = 0; idx < num_encodings; ++idx )
2112 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2114 *width = encodings[idx].width;
2115 *height = encodings[idx].height;
2116 break;
2121 mp_msg( MSGT_VO, MSGL_V,
2122 "[xv common] Maximum source image dimensions: %ux%u\n",
2123 *width, *height );
2125 XvFreeEncodingInfo( encodings );
2129 * \brief Print information about the colorkey method and source.
2131 * \param ck_handling Integer value containing the information about
2132 * colorkey handling (see x11_common.h).
2134 * Outputs the content of |ck_handling| as a readable message.
2137 static void vo_xv_print_ck_info(struct vo_x11_state *x11)
2139 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2141 switch ( x11->xv_ck_info.method )
2143 case CK_METHOD_NONE:
2144 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2145 case CK_METHOD_AUTOPAINT:
2146 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2147 case CK_METHOD_MANUALFILL:
2148 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2149 case CK_METHOD_BACKGROUND:
2150 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2153 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2155 switch ( x11->xv_ck_info.source )
2157 case CK_SRC_CUR:
2158 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2159 x11->xv_colorkey );
2160 break;
2161 case CK_SRC_USE:
2162 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2164 mp_msg( MSGT_VO, MSGL_V,
2165 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2166 x11->xv_colorkey );
2168 else
2170 mp_msg( MSGT_VO, MSGL_V,
2171 "Using colorkey from MPlayer (0x%06lx)."
2172 " Use -colorkey to change.\n",
2173 x11->xv_colorkey );
2175 break;
2176 case CK_SRC_SET:
2177 mp_msg( MSGT_VO, MSGL_V,
2178 "Setting and using colorkey from MPlayer (0x%06lx)."
2179 " Use -colorkey to change.\n",
2180 x11->xv_colorkey );
2181 break;
2185 * \brief Init colorkey depending on the settings in xv_ck_info.
2187 * \return Returns 0 on failure and 1 on success.
2189 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2190 * flags in xv_ck_info.
2192 * Possiblilities:
2193 * * Methods
2194 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2195 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2196 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2197 * * Sources
2198 * - use currently set colorkey ( CK_SRC_CUR )
2199 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2200 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2202 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2203 * we don't draw anything as this means it was forced to off.
2205 int vo_xv_init_colorkey(struct vo *vo)
2207 struct vo_x11_state *x11 = vo->x11;
2208 Atom xv_atom;
2209 int rez;
2211 /* check if colorkeying is needed */
2212 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_COLORKEY");
2214 /* if we have to deal with colorkeying ... */
2215 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2217 /* check if we should use the colorkey specified in vo_colorkey */
2218 if ( x11->xv_ck_info.source != CK_SRC_CUR )
2220 x11->xv_colorkey = vo_colorkey;
2222 /* check if we have to set the colorkey too */
2223 if ( x11->xv_ck_info.source == CK_SRC_SET )
2225 xv_atom = XInternAtom(x11->display, "XV_COLORKEY",False);
2227 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, vo_colorkey);
2228 if ( rez != Success )
2230 mp_msg( MSGT_VO, MSGL_FATAL,
2231 "[xv common] Couldn't set colorkey!\n" );
2232 return 0; // error setting colorkey
2236 else
2238 int colorkey_ret;
2240 rez=XvGetPortAttribute(x11->display,x11->xv_port, xv_atom, &colorkey_ret);
2241 if ( rez == Success )
2243 x11->xv_colorkey = colorkey_ret;
2245 else
2247 mp_msg( MSGT_VO, MSGL_FATAL,
2248 "[xv common] Couldn't get colorkey!"
2249 "Maybe the selected Xv port has no overlay.\n" );
2250 return 0; // error getting colorkey
2254 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_AUTOPAINT_COLORKEY");
2256 /* should we draw the colorkey ourselves or activate autopainting? */
2257 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2259 rez = !Success; // reset rez to something different than Success
2261 if ( xv_atom != None ) // autopaint is supported
2263 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1);
2266 if ( rez != Success )
2268 // fallback to manual colorkey drawing
2269 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2272 else // disable colorkey autopainting if supported
2274 if ( xv_atom != None ) // we have autopaint attribute
2276 XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 0);
2280 else // do no colorkey drawing at all
2282 x11->xv_ck_info.method = CK_METHOD_NONE;
2283 } /* end: should we draw colorkey */
2285 /* output information about the current colorkey settings */
2286 vo_xv_print_ck_info(x11);
2288 return 1; // success
2292 * \brief Draw the colorkey on the video window.
2294 * Draws the colorkey depending on the set method ( colorkey_handling ).
2296 * Also draws the black bars ( when the video doesn't fit the display in
2297 * fullscreen ) separately, so they don't overlap with the video area.
2298 * It doesn't call XFlush.
2301 void vo_xv_draw_colorkey(struct vo *vo, int32_t x, int32_t y,
2302 int32_t w, int32_t h)
2304 struct MPOpts *opts = vo->opts;
2305 struct vo_x11_state *x11 = vo->x11;
2306 if( x11->xv_ck_info.method == CK_METHOD_MANUALFILL ||
2307 x11->xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2309 XSetForeground(x11->display, x11->vo_gc, x11->xv_colorkey );
2310 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2311 x, y,
2312 w, h );
2315 /* draw black bars if needed */
2316 /* TODO! move this to vo_x11_clearwindow_part() */
2317 if ( vo_fs )
2319 XSetForeground(x11->display, x11->vo_gc, 0 );
2320 /* making non-overlap fills, requires 8 checks instead of 4 */
2321 if ( y > 0 )
2322 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2323 0, 0,
2324 opts->vo_screenwidth, y);
2325 if (x > 0)
2326 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2327 0, 0,
2328 x, opts->vo_screenheight);
2329 if (x + w < opts->vo_screenwidth)
2330 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2331 x + w, 0,
2332 opts->vo_screenwidth, opts->vo_screenheight);
2333 if (y + h < opts->vo_screenheight)
2334 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2335 0, y + h,
2336 opts->vo_screenwidth, opts->vo_screenheight);
2340 /** \brief Tests if a valid argument for the ck suboption was given. */
2341 int xv_test_ck( void * arg )
2343 strarg_t * strarg = (strarg_t *)arg;
2345 if ( strargcmp( strarg, "use" ) == 0 ||
2346 strargcmp( strarg, "set" ) == 0 ||
2347 strargcmp( strarg, "cur" ) == 0 )
2349 return 1;
2352 return 0;
2354 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2355 int xv_test_ckm( void * arg )
2357 strarg_t * strarg = (strarg_t *)arg;
2359 if ( strargcmp( strarg, "bg" ) == 0 ||
2360 strargcmp( strarg, "man" ) == 0 ||
2361 strargcmp( strarg, "auto" ) == 0 )
2363 return 1;
2366 return 0;
2370 * \brief Modify the colorkey_handling var according to str
2372 * Checks if a valid pointer ( not NULL ) to the string
2373 * was given. And in that case modifies the colorkey_handling
2374 * var to reflect the requested behaviour.
2375 * If nothing happens the content of colorkey_handling stays
2376 * the same.
2378 * \param str Pointer to the string or NULL
2381 void xv_setup_colorkeyhandling(struct vo *vo, const char *ck_method_str,
2382 const char *ck_str)
2384 struct vo_x11_state *x11 = vo->x11;
2385 /* check if a valid pointer to the string was passed */
2386 if ( ck_str )
2388 if ( strncmp( ck_str, "use", 3 ) == 0 )
2390 x11->xv_ck_info.source = CK_SRC_USE;
2392 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2394 x11->xv_ck_info.source = CK_SRC_SET;
2397 /* check if a valid pointer to the string was passed */
2398 if ( ck_method_str )
2400 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2402 x11->xv_ck_info.method = CK_METHOD_BACKGROUND;
2404 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2406 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2408 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2410 x11->xv_ck_info.method = CK_METHOD_AUTOPAINT;
2415 #endif
2417 struct vo_x11_state *vo_x11_init_state(void)
2419 struct vo_x11_state *s = talloc_ptrtype(NULL, s);
2420 *s = (struct vo_x11_state){
2421 .xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR },
2422 .olddecor = MWM_DECOR_ALL,
2423 .oldfuncs = MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
2424 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE,
2425 .old_gravity = NorthWestGravity,
2427 return s;