x11: fix setting UTF-8 window titles for some special cases
[mplayer.git] / libvo / x11_common.c
blob3df029e242b2645800bbe2730c74ee3c8c380ae4
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(_NET_WM_NAME);
331 XA_INIT(_NET_WM_ICON_NAME);
332 XA_INIT(_WIN_PROTOCOLS);
333 XA_INIT(_WIN_LAYER);
334 XA_INIT(_WIN_HINTS);
335 XA_INIT(WM_PROTOCOLS);
336 XA_INIT(WM_DELETE_WINDOW);
337 XA_INIT(UTF8_STRING);
340 void update_xinerama_info(struct vo *vo) {
341 struct MPOpts *opts = vo->opts;
342 xinerama_x = xinerama_y = 0;
343 #ifdef CONFIG_XINERAMA
344 if (xinerama_screen >= -1 && XineramaIsActive(vo->x11->display))
346 int screen = xinerama_screen;
347 XineramaScreenInfo *screens;
348 int num_screens;
350 screens = XineramaQueryScreens(vo->x11->display, &num_screens);
351 if (screen >= num_screens)
352 screen = num_screens - 1;
353 if (screen == -1) {
354 int x = vo->dx + vo->dwidth / 2;
355 int y = vo->dy + vo->dheight / 2;
356 for (screen = num_screens - 1; screen > 0; screen--) {
357 int left = screens[screen].x_org;
358 int right = left + screens[screen].width;
359 int top = screens[screen].y_org;
360 int bottom = top + screens[screen].height;
361 if (left <= x && x <= right && top <= y && y <= bottom)
362 break;
365 if (screen < 0)
366 screen = 0;
367 opts->vo_screenwidth = screens[screen].width;
368 opts->vo_screenheight = screens[screen].height;
369 xinerama_x = screens[screen].x_org;
370 xinerama_y = screens[screen].y_org;
372 XFree(screens);
374 #endif
375 aspect_save_screenres(vo, opts->vo_screenwidth, opts->vo_screenheight);
378 int vo_init(struct vo *vo)
380 struct MPOpts *opts = vo->opts;
381 struct vo_x11_state *x11 = vo->x11;
382 // int mScreen;
383 int depth, bpp;
384 unsigned int mask;
386 // char * DisplayName = ":0.0";
387 // Display * mDisplay;
388 XImage *mXImage = NULL;
390 // Window mRootWin;
391 XWindowAttributes attribs;
392 char *dispName;
394 if (vo_rootwin)
395 WinID = 0; // use root window
397 if (x11->depthonscreen)
399 saver_off(x11->display);
400 return 1; // already called
403 XSetErrorHandler(x11_errorhandler);
405 #if 0
406 if (!mDisplayName)
407 if (!(mDisplayName = getenv("DISPLAY")))
408 mDisplayName = strdup(":0.0");
409 #else
410 dispName = XDisplayName(mDisplayName);
411 #endif
413 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
415 x11->display = XOpenDisplay(dispName);
416 if (!x11->display)
418 mp_msg(MSGT_VO, MSGL_ERR,
419 "vo: couldn't open the X11 display (%s)!\n", dispName);
420 return 0;
422 x11->screen = DefaultScreen(x11->display); // screen ID
423 x11->rootwin = RootWindow(x11->display, x11->screen); // root window ID
425 init_atoms(vo->x11);
427 #ifdef CONFIG_XF86VM
429 int clock;
431 XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline);
432 if (!opts->vo_screenwidth)
433 opts->vo_screenwidth = modeline.hdisplay;
434 if (!opts->vo_screenheight)
435 opts->vo_screenheight = modeline.vdisplay;
437 #endif
439 if (!opts->vo_screenwidth)
440 opts->vo_screenwidth = DisplayWidth(x11->display, x11->screen);
441 if (!opts->vo_screenheight)
442 opts->vo_screenheight = DisplayHeight(x11->display, x11->screen);
444 // get color depth (from root window, or the best visual):
445 XGetWindowAttributes(x11->display, x11->rootwin, &attribs);
446 depth = attribs.depth;
448 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
450 Visual *visual;
452 depth = vo_find_depth_from_visuals(x11->display, x11->screen, &visual);
453 if (depth != -1)
454 mXImage = XCreateImage(x11->display, visual, depth, ZPixmap,
455 0, NULL, 1, 1, 8, 1);
456 } else
457 mXImage =
458 XGetImage(x11->display, x11->rootwin, 0, 0, 1, 1, AllPlanes, ZPixmap);
460 x11->depthonscreen = depth; // display depth on screen
462 // get bits/pixel from XImage structure:
463 if (mXImage == NULL)
465 mask = 0;
466 } else
469 * for the depth==24 case, the XImage structures might use
470 * 24 or 32 bits of data per pixel. The x11->depthonscreen
471 * field stores the amount of data per pixel in the
472 * XImage structure!
474 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
476 bpp = mXImage->bits_per_pixel;
477 if ((x11->depthonscreen + 7) / 8 != (bpp + 7) / 8)
478 x11->depthonscreen = bpp; // by A'rpi
479 mask =
480 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
481 mp_msg(MSGT_VO, MSGL_V,
482 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
483 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
484 XDestroyImage(mXImage);
486 if (((x11->depthonscreen + 7) / 8) == 2)
488 if (mask == 0x7FFF)
489 x11->depthonscreen = 15;
490 else if (mask == 0xFFFF)
491 x11->depthonscreen = 16;
493 // XCloseDisplay( mDisplay );
494 /* slightly improved local display detection AST */
495 if (strncmp(dispName, "unix:", 5) == 0)
496 dispName += 4;
497 else if (strncmp(dispName, "localhost:", 10) == 0)
498 dispName += 9;
499 if (*dispName == ':' && atoi(dispName + 1) < 10)
500 x11->display_is_local = 1;
501 else
502 x11->display_is_local = 0;
503 mp_msg(MSGT_VO, MSGL_V,
504 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
505 opts->vo_screenwidth, opts->vo_screenheight, depth, x11->depthonscreen,
506 dispName, x11->display_is_local ? "local" : "remote");
508 x11->wm_type = vo_wm_detect(vo);
510 x11->fs_type = vo_x11_get_fs_type(x11->wm_type);
512 fstype_dump(x11->fs_type);
514 saver_off(x11->display);
515 return 1;
518 void vo_uninit(struct vo_x11_state *x11)
520 if (!x11->display)
522 mp_msg(MSGT_VO, MSGL_V,
523 "vo: x11 uninit called but X11 not initialized..\n");
524 } else {
525 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
526 XSetErrorHandler(NULL);
527 XCloseDisplay(x11->display);
528 x11->depthonscreen = 0;
529 x11->display = NULL;
531 talloc_free(x11);
534 #include "wskeys.h"
536 #ifdef XF86XK_AudioPause
537 static const struct mp_keymap keysym_map[] = {
538 {XF86XK_MenuKB, KEY_MENU},
539 {XF86XK_AudioPlay, KEY_PLAY}, {XF86XK_AudioPause, KEY_PAUSE}, {XF86XK_AudioStop, KEY_STOP},
540 {XF86XK_AudioPrev, KEY_PREV}, {XF86XK_AudioNext, KEY_NEXT},
541 {XF86XK_AudioMute, KEY_MUTE}, {XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN}, {XF86XK_AudioRaiseVolume, KEY_VOLUME_UP},
542 {0, 0}
545 static void vo_x11_putkey_ext(struct vo *vo, int keysym, int modifiers)
547 struct mp_fifo *f = vo->key_fifo;
548 int mpkey = lookup_keymap_table(keysym_map, keysym);
549 if (mpkey)
550 mplayer_put_key(f, mpkey + modifiers);
552 #endif
554 static const struct mp_keymap keymap[] = {
555 // special keys
556 {wsPause, KEY_PAUSE}, {wsEscape, KEY_ESC}, {wsBackSpace, KEY_BS},
557 {wsTab, KEY_TAB}, {wsEnter, KEY_ENTER},
559 // cursor keys
560 {wsLeft, KEY_LEFT}, {wsRight, KEY_RIGHT}, {wsUp, KEY_UP}, {wsDown, KEY_DOWN},
562 // navigation block
563 {wsInsert, KEY_INSERT}, {wsDelete, KEY_DELETE}, {wsHome, KEY_HOME}, {wsEnd, KEY_END},
564 {wsPageUp, KEY_PAGE_UP}, {wsPageDown, KEY_PAGE_DOWN},
566 // F-keys
567 {wsF1, KEY_F+1}, {wsF2, KEY_F+2}, {wsF3, KEY_F+3}, {wsF4, KEY_F+4},
568 {wsF5, KEY_F+5}, {wsF6, KEY_F+6}, {wsF7, KEY_F+7}, {wsF8, KEY_F+8},
569 {wsF9, KEY_F+9}, {wsF10, KEY_F+10}, {wsF11, KEY_F+11}, {wsF12, KEY_F+12},
571 // numpad independent of numlock
572 {wsGrayMinus, '-'}, {wsGrayPlus, '+'}, {wsGrayMul, '*'}, {wsGrayDiv, '/'},
573 {wsGrayEnter, KEY_KPENTER},
575 // numpad with numlock
576 {wsGray0, KEY_KP0}, {wsGray1, KEY_KP1}, {wsGray2, KEY_KP2},
577 {wsGray3, KEY_KP3}, {wsGray4, KEY_KP4}, {wsGray5, KEY_KP5},
578 {wsGray6, KEY_KP6}, {wsGray7, KEY_KP7}, {wsGray8, KEY_KP8},
579 {wsGray9, KEY_KP9}, {wsGrayDecimal, KEY_KPDEC},
581 // numpad without numlock
582 {wsGrayInsert, KEY_KPINS}, {wsGrayEnd, KEY_KP1}, {wsGrayDown, KEY_KP2},
583 {wsGrayPgDn, KEY_KP3}, {wsGrayLeft, KEY_KP4}, {wsGray5Dup, KEY_KP5},
584 {wsGrayRight, KEY_KP6}, {wsGrayHome, KEY_KP7}, {wsGrayUp, KEY_KP8},
585 {wsGrayPgUp, KEY_KP9}, {wsGrayDelete, KEY_KPDEL},
587 {0, 0}
590 static void vo_x11_putkey(struct vo *vo, int key, int modifiers)
592 static const char *passthrough_keys = " -+*/<>`~!@#$%^&()_{}:;\"\',.?\\|=[]";
593 int mpkey = 0;
594 if ((key >= 'a' && key <= 'z') ||
595 (key >= 'A' && key <= 'Z') ||
596 (key >= '0' && key <= '9') ||
597 (key > 0 && key < 256 && strchr(passthrough_keys, key)))
598 mpkey = key;
600 if (!mpkey)
601 mpkey = lookup_keymap_table(keymap, key);
603 if (mpkey)
604 mplayer_put_key(vo->key_fifo, mpkey + modifiers);
608 // ----- Motif header: -------
610 #define MWM_HINTS_FUNCTIONS (1L << 0)
611 #define MWM_HINTS_DECORATIONS (1L << 1)
612 #define MWM_HINTS_INPUT_MODE (1L << 2)
613 #define MWM_HINTS_STATUS (1L << 3)
615 #define MWM_FUNC_ALL (1L << 0)
616 #define MWM_FUNC_RESIZE (1L << 1)
617 #define MWM_FUNC_MOVE (1L << 2)
618 #define MWM_FUNC_MINIMIZE (1L << 3)
619 #define MWM_FUNC_MAXIMIZE (1L << 4)
620 #define MWM_FUNC_CLOSE (1L << 5)
622 #define MWM_DECOR_ALL (1L << 0)
623 #define MWM_DECOR_BORDER (1L << 1)
624 #define MWM_DECOR_RESIZEH (1L << 2)
625 #define MWM_DECOR_TITLE (1L << 3)
626 #define MWM_DECOR_MENU (1L << 4)
627 #define MWM_DECOR_MINIMIZE (1L << 5)
628 #define MWM_DECOR_MAXIMIZE (1L << 6)
630 #define MWM_INPUT_MODELESS 0
631 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
632 #define MWM_INPUT_SYSTEM_MODAL 2
633 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
634 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
636 #define MWM_TEAROFF_WINDOW (1L<<0)
638 typedef struct
640 long flags;
641 long functions;
642 long decorations;
643 long input_mode;
644 long state;
645 } MotifWmHints;
647 static MotifWmHints vo_MotifWmHints;
648 static Atom vo_MotifHints = None;
650 void vo_x11_decoration(struct vo *vo, int d)
652 struct vo_x11_state *x11 = vo->x11;
653 Atom mtype;
654 int mformat;
655 unsigned long mn, mb;
657 if (!WinID)
658 return;
660 if (vo_fsmode & 8)
662 XSetTransientForHint(x11->display, x11->window,
663 RootWindow(x11->display, x11->screen));
666 vo_MotifHints = XInternAtom(x11->display, "_MOTIF_WM_HINTS", 0);
667 if (vo_MotifHints != None)
669 if (!d)
671 MotifWmHints *mhints = NULL;
673 XGetWindowProperty(x11->display, x11->window,
674 vo_MotifHints, 0, 20, False,
675 vo_MotifHints, &mtype, &mformat, &mn,
676 &mb, (unsigned char **) &mhints);
677 if (mhints)
679 if (mhints->flags & MWM_HINTS_DECORATIONS)
680 x11->olddecor = mhints->decorations;
681 if (mhints->flags & MWM_HINTS_FUNCTIONS)
682 x11->oldfuncs = mhints->functions;
683 XFree(mhints);
687 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
688 vo_MotifWmHints.flags =
689 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
690 if (d)
692 vo_MotifWmHints.functions = x11->oldfuncs;
693 d = x11->olddecor;
695 #if 0
696 vo_MotifWmHints.decorations =
697 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
698 #else
699 vo_MotifWmHints.decorations =
700 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
701 #endif
702 XChangeProperty(x11->display, x11->window, vo_MotifHints,
703 vo_MotifHints, 32,
704 PropModeReplace,
705 (unsigned char *) &vo_MotifWmHints,
706 (vo_fsmode & 4) ? 4 : 5);
710 void vo_x11_classhint(struct vo *vo, Window window, const char *name)
712 struct MPOpts *opts = vo->opts;
713 struct vo_x11_state *x11 = vo->x11;
714 XClassHint wmClass;
715 pid_t pid = getpid();
717 wmClass.res_name = opts->vo_winname ? opts->vo_winname : (char *)name;
718 wmClass.res_class = "mplayer2";
719 XSetClassHint(x11->display, window, &wmClass);
720 XChangeProperty(x11->display, window, x11->XA_NET_WM_PID, XA_CARDINAL,
721 32, PropModeReplace, (unsigned char *) &pid, 1);
724 void vo_x11_uninit(struct vo *vo)
726 struct vo_x11_state *x11 = vo->x11;
727 saver_on(x11->display);
728 if (x11->window != None)
729 vo_showcursor(x11->display, x11->window);
731 if (x11->f_gc != None)
733 XFreeGC(vo->x11->display, x11->f_gc);
734 x11->f_gc = None;
737 if (x11->vo_gc != None)
739 XFreeGC(vo->x11->display, x11->vo_gc);
740 x11->vo_gc = None;
742 if (x11->window != None)
744 XClearWindow(x11->display, x11->window);
745 if (WinID < 0)
747 XEvent xev;
749 XUnmapWindow(x11->display, x11->window);
750 XSelectInput(x11->display, x11->window, StructureNotifyMask);
751 XDestroyWindow(x11->display, x11->window);
754 XNextEvent(x11->display, &xev);
756 while (xev.type != DestroyNotify
757 || xev.xdestroywindow.event != x11->window);
759 x11->window = None;
761 vo_fs = 0;
762 x11->vo_old_width = x11->vo_old_height = 0;
763 x11->last_video_width = 0;
764 x11->last_video_height = 0;
765 x11->size_changed_during_fs = false;
769 static int check_resize(struct vo *vo)
771 int old_w = vo->dwidth, old_h = vo->dheight;
772 int old_x = vo->dx, old_y = vo->dy;
773 int rc = 0;
774 vo_x11_update_geometry(vo, true);
775 if (vo->dwidth != old_w || vo->dheight != old_h)
776 rc |= VO_EVENT_RESIZE;
777 if (vo->dx != old_x || vo->dy != old_y)
778 rc |= VO_EVENT_MOVE;
779 return rc;
782 int vo_x11_check_events(struct vo *vo)
784 struct vo_x11_state *x11 = vo->x11;
785 Display *display = vo->x11->display;
786 int ret = 0;
787 XEvent Event;
788 char buf[100];
789 KeySym keySym;
791 if (x11->vo_mouse_autohide && x11->mouse_waiting_hide &&
792 (GetTimerMS() - x11->mouse_timer >= 1000)) {
793 vo_hidecursor(display, x11->window);
794 x11->mouse_waiting_hide = 0;
797 if (WinID > 0)
798 ret |= check_resize(vo);
799 while (XPending(display))
801 XNextEvent(display, &Event);
802 // printf("\rEvent.type=%X \n",Event.type);
803 switch (Event.type)
805 case Expose:
806 ret |= VO_EVENT_EXPOSE;
807 break;
808 case ConfigureNotify:
809 if (x11->window == None)
810 break;
811 ret |= check_resize(vo);
812 break;
813 case KeyPress:
815 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
816 &x11->compose_status);
817 int modifiers = 0;
818 if (Event.xkey.state & ShiftMask)
819 modifiers |= KEY_MODIFIER_SHIFT;
820 if (Event.xkey.state & ControlMask)
821 modifiers |= KEY_MODIFIER_CTRL;
822 if (Event.xkey.state & Mod1Mask)
823 modifiers |= KEY_MODIFIER_ALT;
824 if (Event.xkey.state & Mod4Mask)
825 modifiers |= KEY_MODIFIER_META;
826 #ifdef XF86XK_AudioPause
827 vo_x11_putkey_ext(vo, keySym, modifiers);
828 #endif
829 vo_x11_putkey(vo, keySym, modifiers);
830 ret |= VO_EVENT_KEYPRESS;
832 break;
833 case MotionNotify:
834 vo_mouse_movement(vo, Event.xmotion.x, Event.xmotion.y);
836 if (x11->vo_mouse_autohide)
838 vo_showcursor(display, x11->window);
839 x11->mouse_waiting_hide = 1;
840 x11->mouse_timer = GetTimerMS();
842 break;
843 case ButtonPress:
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 | MP_KEY_DOWN);
853 break;
854 case ButtonRelease:
855 if (x11->vo_mouse_autohide)
857 vo_showcursor(display, x11->window);
858 x11->mouse_waiting_hide = 1;
859 x11->mouse_timer = GetTimerMS();
861 mplayer_put_key(vo->key_fifo,
862 MOUSE_BTN0 + Event.xbutton.button - 1);
863 break;
864 case PropertyNotify:
866 char *name =
867 XGetAtomName(display, Event.xproperty.atom);
869 if (!name)
870 break;
872 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
874 XFree(name);
876 break;
877 case MapNotify:
878 x11->vo_hint.win_gravity = x11->old_gravity;
879 XSetWMNormalHints(display, x11->window, &x11->vo_hint);
880 x11->fs_flip = 0;
881 break;
882 case DestroyNotify:
883 mp_msg(MSGT_VO, MSGL_WARN, "Our window was destroyed, exiting\n");
884 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
885 break;
886 case ClientMessage:
887 if (Event.xclient.message_type == x11->XAWM_PROTOCOLS &&
888 Event.xclient.data.l[0] == x11->XAWM_DELETE_WINDOW)
889 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
890 break;
893 return ret;
897 * \brief sets the size and position of the non-fullscreen window.
899 static void vo_x11_nofs_sizepos(struct vo *vo, int x, int y,
900 int width, int height)
902 struct vo_x11_state *x11 = vo->x11;
903 if (width == x11->last_video_width && height == x11->last_video_height) {
904 if (!vo->opts->force_window_position && !x11->size_changed_during_fs)
905 return;
906 } else if (vo_fs)
907 x11->size_changed_during_fs = true;
908 x11->last_video_height = height;
909 x11->last_video_width = width;
910 vo_x11_sizehint(vo, x, y, width, height, 0);
911 if (vo_fs) {
912 x11->vo_old_x = x;
913 x11->vo_old_y = y;
914 x11->vo_old_width = width;
915 x11->vo_old_height = height;
917 else
919 vo->dwidth = width;
920 vo->dheight = height;
921 if (vo->opts->force_window_position)
922 XMoveResizeWindow(vo->x11->display, vo->x11->window, x, y, width,
923 height);
924 else
925 XResizeWindow(vo->x11->display, vo->x11->window, width, height);
929 void vo_x11_sizehint(struct vo *vo, int x, int y, int width, int height, int max)
931 struct vo_x11_state *x11 = vo->x11;
932 x11->vo_hint.flags = 0;
933 if (vo_keepaspect)
935 x11->vo_hint.flags |= PAspect;
936 x11->vo_hint.min_aspect.x = width;
937 x11->vo_hint.min_aspect.y = height;
938 x11->vo_hint.max_aspect.x = width;
939 x11->vo_hint.max_aspect.y = height;
942 x11->vo_hint.flags |= PPosition | PSize;
943 x11->vo_hint.x = x;
944 x11->vo_hint.y = y;
945 x11->vo_hint.width = width;
946 x11->vo_hint.height = height;
947 if (max)
949 x11->vo_hint.flags |= PMaxSize;
950 x11->vo_hint.max_width = width;
951 x11->vo_hint.max_height = height;
952 } else
954 x11->vo_hint.max_width = 0;
955 x11->vo_hint.max_height = 0;
958 // Set minimum height/width to 4 to avoid off-by-one errors
959 // and because mga_vid requires a minimal size of 4 pixels.
960 x11->vo_hint.flags |= PMinSize;
961 x11->vo_hint.min_width = x11->vo_hint.min_height = 4;
963 // Set the base size. A window manager might display the window
964 // size to the user relative to this.
965 // Setting these to width/height might be nice, but e.g. fluxbox can't handle it.
966 x11->vo_hint.flags |= PBaseSize;
967 x11->vo_hint.base_width = 0 /*width*/;
968 x11->vo_hint.base_height = 0 /*height*/;
970 x11->vo_hint.flags |= PWinGravity;
971 x11->vo_hint.win_gravity = StaticGravity;
972 XSetWMNormalHints(x11->display, x11->window, &x11->vo_hint);
975 static int vo_x11_get_gnome_layer(struct vo_x11_state *x11, Window win)
977 Atom type;
978 int format;
979 unsigned long nitems;
980 unsigned long bytesafter;
981 unsigned short *args = NULL;
983 if (XGetWindowProperty(x11->display, win, x11->XA_WIN_LAYER, 0, 16384,
984 False, AnyPropertyType, &type, &format, &nitems,
985 &bytesafter,
986 (unsigned char **) &args) == Success
987 && nitems > 0 && args)
989 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
990 *args);
991 return *args;
993 return WIN_LAYER_NORMAL;
996 // set a X text property that expects a UTF8_STRING type
997 static void vo_x11_set_property_utf8(struct vo *vo, Atom name, const char *t)
999 struct vo_x11_state *x11 = vo->x11;
1001 XChangeProperty(x11->display, x11->window, name, x11->XAUTF8_STRING, 8,
1002 PropModeReplace, t, strlen(t));
1005 // set a X text property that expects a STRING or COMPOUND_TEXT type
1006 static void vo_x11_set_property_string(struct vo *vo, Atom name, const char *t)
1008 struct vo_x11_state *x11 = vo->x11;
1009 XTextProperty prop = {0};
1011 if (Xutf8TextListToTextProperty(x11->display, (char **)&t, 1,
1012 XStdICCTextStyle, &prop) == Success) {
1013 XSetTextProperty(x11->display, x11->window, &prop, name);
1014 } else {
1015 // Strictly speaking this violates the ICCCM, but there's no way we
1016 // can do this correctly.
1017 vo_x11_set_property_utf8(vo, name, t);
1020 if (prop.value)
1021 XFree(prop.value);
1024 static void vo_x11_update_window_title(struct vo *vo)
1026 struct vo_x11_state *x11 = vo->x11;
1028 const char *title = vo_get_window_title(vo);
1029 vo_x11_set_property_string(vo, XA_WM_NAME, title);
1030 vo_x11_set_property_string(vo, XA_WM_ICON_NAME, title);
1031 vo_x11_set_property_utf8(vo, x11->XA_NET_WM_NAME, title);
1032 vo_x11_set_property_utf8(vo, x11->XA_NET_WM_ICON_NAME, title);
1036 static Window vo_x11_create_smooth_window(struct vo_x11_state *x11, Window mRoot,
1037 Visual * vis, int x, int y,
1038 unsigned int width, unsigned int height,
1039 int depth, Colormap col_map)
1041 unsigned long xswamask = CWBorderPixel;
1042 XSetWindowAttributes xswa;
1043 Window ret_win;
1045 if (col_map != CopyFromParent)
1047 xswa.colormap = col_map;
1048 xswamask |= CWColormap;
1050 xswa.background_pixel = 0;
1051 xswa.border_pixel = 0;
1052 xswa.backing_store = NotUseful;
1053 xswa.bit_gravity = StaticGravity;
1055 ret_win =
1056 XCreateWindow(x11->display, x11->rootwin, x, y, width, height, 0, depth,
1057 CopyFromParent, vis, xswamask, &xswa);
1058 XSetWMProtocols(x11->display, ret_win, &x11->XAWM_DELETE_WINDOW, 1);
1059 if (x11->f_gc == None)
1060 x11->f_gc = XCreateGC(x11->display, ret_win, 0, 0);
1061 XSetForeground(x11->display, x11->f_gc, 0);
1063 return ret_win;
1067 * \brief create and setup a window suitable for display
1068 * \param vis Visual to use for creating the window
1069 * \param x x position of window
1070 * \param y y position of window
1071 * \param width width of window
1072 * \param height height of window
1073 * \param flags flags for window creation.
1074 * Only VOFLAG_FULLSCREEN is supported so far.
1075 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1076 * \param classname name to use for the classhint
1078 * This also does the grunt-work like setting Window Manager hints etc.
1079 * If vo_window is already set it just moves and resizes it.
1081 void vo_x11_create_vo_window(struct vo *vo, XVisualInfo *vis, int x, int y,
1082 unsigned int width, unsigned int height, int flags,
1083 Colormap col_map, const char *classname)
1085 struct MPOpts *opts = vo->opts;
1086 struct vo_x11_state *x11 = vo->x11;
1087 Display *mDisplay = vo->x11->display;
1088 if (WinID >= 0) {
1089 vo_fs = flags & VOFLAG_FULLSCREEN;
1090 x11->window = WinID ? (Window)WinID : x11->rootwin;
1091 if (col_map != CopyFromParent) {
1092 unsigned long xswamask = CWColormap;
1093 XSetWindowAttributes xswa;
1094 xswa.colormap = col_map;
1095 XChangeWindowAttributes(mDisplay, x11->window, xswamask, &xswa);
1096 XInstallColormap(mDisplay, col_map);
1098 if (WinID) {
1099 // Expose events can only really be handled by us, so request them.
1100 vo_x11_selectinput_witherr(mDisplay, x11->window, ExposureMask);
1101 } else
1102 // Do not capture events since it might break the parent application
1103 // if it relies on events being forwarded to the parent of WinID.
1104 // It also is consistent with the w32_common.c code.
1105 vo_x11_selectinput_witherr(mDisplay, x11->window,
1106 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1107 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1109 vo_x11_update_geometry(vo, true);
1110 goto final;
1112 if (x11->window == None) {
1113 vo_fs = 0;
1114 vo->dwidth = width;
1115 vo->dheight = height;
1116 x11->window = vo_x11_create_smooth_window(x11, x11->rootwin, vis->visual,
1117 x, y, width, height, vis->depth, col_map);
1118 x11->window_state = VOFLAG_HIDDEN;
1120 if (flags & VOFLAG_HIDDEN)
1121 goto final;
1122 if (x11->window_state & VOFLAG_HIDDEN) {
1123 XSizeHints hint;
1124 x11->window_state &= ~VOFLAG_HIDDEN;
1125 vo_x11_classhint(vo, x11->window, classname);
1126 vo_hidecursor(mDisplay, x11->window);
1127 XSelectInput(mDisplay, x11->window, StructureNotifyMask);
1128 hint.x = x; hint.y = y;
1129 hint.width = width; hint.height = height;
1130 hint.flags = PSize;
1131 if (geometry_xy_changed)
1132 hint.flags |= PPosition;
1133 XSetWMNormalHints(mDisplay, x11->window, &hint);
1134 if (!vo_border) vo_x11_decoration(vo, 0);
1135 // map window
1136 XSelectInput(mDisplay, x11->window, NoEventMask);
1137 vo_x11_selectinput_witherr(mDisplay, x11->window,
1138 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1139 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1140 XMapWindow(mDisplay, x11->window);
1141 vo_x11_clearwindow(vo, x11->window);
1143 vo_x11_update_window_title(vo);
1144 if (opts->vo_ontop) vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1145 vo_x11_update_geometry(vo, !geometry_xy_changed);
1146 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, width, height);
1147 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1148 vo_x11_fullscreen(vo);
1149 else if (vo_fs) {
1150 // if we are already in fullscreen do not switch back and forth, just
1151 // set the size values right.
1152 vo->dwidth = vo->opts->vo_screenwidth;
1153 vo->dheight = vo->opts->vo_screenheight;
1155 final:
1156 if (x11->vo_gc != None)
1157 XFreeGC(mDisplay, x11->vo_gc);
1158 x11->vo_gc = XCreateGC(mDisplay, x11->window, 0, NULL);
1160 XSync(mDisplay, False);
1161 x11->vo_mouse_autohide = 1;
1162 vo->event_fd = ConnectionNumber(x11->display);
1165 void vo_x11_clearwindow_part(struct vo *vo, Window vo_window,
1166 int img_width, int img_height)
1168 struct vo_x11_state *x11 = vo->x11;
1169 Display *mDisplay = vo->x11->display;
1170 int u_dheight, u_dwidth, left_ov, left_ov2;
1172 if (x11->f_gc == None)
1173 return;
1175 u_dheight = vo->dheight;
1176 u_dwidth = vo->dwidth;
1177 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1178 return;
1180 left_ov = (u_dheight - img_height) / 2;
1181 left_ov2 = (u_dwidth - img_width) / 2;
1183 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, 0, u_dwidth, left_ov);
1184 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, u_dheight - left_ov - 1,
1185 u_dwidth, left_ov + 1);
1187 if (u_dwidth > img_width)
1189 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, left_ov, left_ov2,
1190 img_height);
1191 XFillRectangle(mDisplay, vo_window, x11->f_gc, u_dwidth - left_ov2 - 1,
1192 left_ov, left_ov2 + 1, img_height);
1195 XFlush(mDisplay);
1198 void vo_x11_clearwindow(struct vo *vo, Window vo_window)
1200 struct vo_x11_state *x11 = vo->x11;
1201 struct MPOpts *opts = vo->opts;
1202 if (x11->f_gc == None)
1203 return;
1204 XFillRectangle(x11->display, vo_window, x11->f_gc, 0, 0,
1205 opts->vo_screenwidth, opts->vo_screenheight);
1207 XFlush(x11->display);
1211 void vo_x11_setlayer(struct vo *vo, Window vo_window, int layer)
1213 struct vo_x11_state *x11 = vo->x11;
1214 if (WinID >= 0)
1215 return;
1217 if (x11->fs_type & vo_wm_LAYER)
1219 XClientMessageEvent xev;
1221 if (!x11->orig_layer)
1222 x11->orig_layer = vo_x11_get_gnome_layer(x11, vo_window);
1224 memset(&xev, 0, sizeof(xev));
1225 xev.type = ClientMessage;
1226 xev.display = x11->display;
1227 xev.window = vo_window;
1228 xev.message_type = x11->XA_WIN_LAYER;
1229 xev.format = 32;
1230 xev.data.l[0] = layer ? fs_layer : x11->orig_layer; // if not fullscreen, stay on default layer
1231 xev.data.l[1] = CurrentTime;
1232 mp_msg(MSGT_VO, MSGL_V,
1233 "[x11] Layered style stay on top (layer %ld).\n",
1234 xev.data.l[0]);
1235 XSendEvent(x11->display, x11->rootwin, False, SubstructureNotifyMask,
1236 (XEvent *) & xev);
1237 } else if (x11->fs_type & vo_wm_NETWM)
1239 XClientMessageEvent xev;
1240 char *state;
1242 memset(&xev, 0, sizeof(xev));
1243 xev.type = ClientMessage;
1244 xev.message_type = x11->XA_NET_WM_STATE;
1245 xev.display = x11->display;
1246 xev.window = vo_window;
1247 xev.format = 32;
1248 xev.data.l[0] = layer;
1250 if (x11->fs_type & vo_wm_STAYS_ON_TOP)
1251 xev.data.l[1] = x11->XA_NET_WM_STATE_STAYS_ON_TOP;
1252 else if (x11->fs_type & vo_wm_ABOVE)
1253 xev.data.l[1] = x11->XA_NET_WM_STATE_ABOVE;
1254 else if (x11->fs_type & vo_wm_FULLSCREEN)
1255 xev.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
1256 else if (x11->fs_type & vo_wm_BELOW)
1257 // This is not fallback. We can safely assume that the situation
1258 // where only NETWM_STATE_BELOW is supported doesn't exist.
1259 xev.data.l[1] = x11->XA_NET_WM_STATE_BELOW;
1261 XSendEvent(x11->display, x11->rootwin, False, SubstructureRedirectMask,
1262 (XEvent *) & xev);
1263 state = XGetAtomName(x11->display, xev.data.l[1]);
1264 mp_msg(MSGT_VO, MSGL_V,
1265 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1266 layer, state);
1267 XFree(state);
1271 static int vo_x11_get_fs_type(int supported)
1273 int i;
1274 int type = supported;
1276 if (vo_fstype_list)
1278 for (i = 0; vo_fstype_list[i]; i++)
1280 int neg = 0;
1281 char *arg = vo_fstype_list[i];
1283 if (vo_fstype_list[i][0] == '-')
1285 neg = 1;
1286 arg = vo_fstype_list[i] + 1;
1289 if (!strncmp(arg, "layer", 5))
1291 if (!neg && (arg[5] == '='))
1293 char *endptr = NULL;
1294 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1296 if (endptr && *endptr == '\0' && layer >= 0
1297 && layer <= 15)
1298 fs_layer = layer;
1300 if (neg)
1301 type &= ~vo_wm_LAYER;
1302 else
1303 type |= vo_wm_LAYER;
1304 } else if (!strcmp(arg, "above"))
1306 if (neg)
1307 type &= ~vo_wm_ABOVE;
1308 else
1309 type |= vo_wm_ABOVE;
1310 } else if (!strcmp(arg, "fullscreen"))
1312 if (neg)
1313 type &= ~vo_wm_FULLSCREEN;
1314 else
1315 type |= vo_wm_FULLSCREEN;
1316 } else if (!strcmp(arg, "stays_on_top"))
1318 if (neg)
1319 type &= ~vo_wm_STAYS_ON_TOP;
1320 else
1321 type |= vo_wm_STAYS_ON_TOP;
1322 } else if (!strcmp(arg, "below"))
1324 if (neg)
1325 type &= ~vo_wm_BELOW;
1326 else
1327 type |= vo_wm_BELOW;
1328 } else if (!strcmp(arg, "netwm"))
1330 if (neg)
1331 type &= ~vo_wm_NETWM;
1332 else
1333 type |= vo_wm_NETWM;
1334 } else if (!strcmp(arg, "none"))
1335 type = 0; // clear; keep parsing
1339 return type;
1343 * \brief update vo->dx, vo->dy, vo->dwidth and vo->dheight with current values of vo->x11->window
1344 * \return returns current color depth of vo->x11->window
1346 int vo_x11_update_geometry(struct vo *vo, bool update_pos)
1348 struct vo_x11_state *x11 = vo->x11;
1349 unsigned depth, w, h;
1350 int dummy_int;
1351 Window dummy_win;
1352 XGetGeometry(x11->display, x11->window, &dummy_win, &dummy_int, &dummy_int,
1353 &w, &h, &dummy_int, &depth);
1354 if (w <= INT_MAX && h <= INT_MAX) {
1355 vo->dwidth = w;
1356 vo->dheight = h;
1358 if (update_pos)
1359 XTranslateCoordinates(x11->display, x11->window, x11->rootwin, 0, 0,
1360 &vo->dx, &vo->dy, &dummy_win);
1362 return depth <= INT_MAX ? depth : 0;
1365 void vo_x11_fullscreen(struct vo *vo)
1367 struct MPOpts *opts = vo->opts;
1368 struct vo_x11_state *x11 = vo->x11;
1369 int x, y, w, h;
1370 x = x11->vo_old_x;
1371 y = x11->vo_old_y;
1372 w = x11->vo_old_width;
1373 h = x11->vo_old_height;
1375 if (WinID >= 0) {
1376 vo_fs = !vo_fs;
1377 return;
1379 if (x11->fs_flip)
1380 return;
1382 if (vo_fs)
1384 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1385 vo_fs = VO_FALSE;
1386 if (x11->size_changed_during_fs && (x11->fs_type & vo_wm_FULLSCREEN))
1387 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, x11->last_video_width,
1388 x11->last_video_height);
1389 x11->size_changed_during_fs = false;
1390 } else
1392 // win->fs
1393 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1395 vo_fs = VO_TRUE;
1396 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1398 x11->vo_old_x = vo->dx;
1399 x11->vo_old_y = vo->dy;
1400 x11->vo_old_width = vo->dwidth;
1401 x11->vo_old_height = vo->dheight;
1403 update_xinerama_info(vo);
1404 x = xinerama_x;
1405 y = xinerama_y;
1406 w = opts->vo_screenwidth;
1407 h = opts->vo_screenheight;
1410 long dummy;
1412 XGetWMNormalHints(x11->display, x11->window, &x11->vo_hint, &dummy);
1413 if (!(x11->vo_hint.flags & PWinGravity))
1414 x11->old_gravity = NorthWestGravity;
1415 else
1416 x11->old_gravity = x11->vo_hint.win_gravity;
1418 if (x11->wm_type == 0 && !(vo_fsmode & 16))
1420 XUnmapWindow(x11->display, x11->window); // required for MWM
1421 XWithdrawWindow(x11->display, x11->window, x11->screen);
1422 x11->fs_flip = 1;
1425 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1427 vo_x11_decoration(vo, vo_border && !vo_fs);
1428 vo_x11_sizehint(vo, x, y, w, h, 0);
1429 vo_x11_setlayer(vo, x11->window, vo_fs);
1432 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1434 /* some WMs lose ontop after fullscreen */
1435 if ((!(vo_fs)) & opts->vo_ontop)
1436 vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1438 XMapRaised(x11->display, x11->window);
1439 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1440 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1441 XRaiseWindow(x11->display, x11->window);
1442 XFlush(x11->display);
1445 void vo_x11_ontop(struct vo *vo)
1447 struct MPOpts *opts = vo->opts;
1448 opts->vo_ontop = !opts->vo_ontop;
1450 vo_x11_setlayer(vo, vo->x11->window, opts->vo_ontop);
1453 void vo_x11_border(struct vo *vo)
1455 vo_border = !vo_border;
1456 vo_x11_decoration(vo, vo_border && !vo_fs);
1460 * XScreensaver stuff
1463 static int screensaver_off;
1464 static unsigned int time_last;
1466 void xscreensaver_heartbeat(struct vo_x11_state *x11)
1468 unsigned int time = GetTimerMS();
1470 if (x11->display && screensaver_off && (time - time_last) > 30000)
1472 time_last = time;
1474 XResetScreenSaver(x11->display);
1478 static int xss_suspend(Display *mDisplay, Bool suspend)
1480 #ifndef CONFIG_XSS
1481 return 0;
1482 #else
1483 int event, error, major, minor;
1484 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1485 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1486 return 0;
1487 if (major < 1 || (major == 1 && minor < 1))
1488 return 0;
1489 XScreenSaverSuspend(mDisplay, suspend);
1490 return 1;
1491 #endif
1495 * End of XScreensaver stuff
1498 static void saver_on(Display * mDisplay)
1501 if (!screensaver_off)
1502 return;
1503 screensaver_off = 0;
1504 if (xss_suspend(mDisplay, False))
1505 return;
1506 #ifdef CONFIG_XDPMS
1507 if (dpms_disabled)
1509 int nothing;
1510 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1512 if (!DPMSEnable(mDisplay))
1513 { // restoring power saving settings
1514 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1515 } else
1517 // DPMS does not seem to be enabled unless we call DPMSInfo
1518 BOOL onoff;
1519 CARD16 state;
1521 DPMSForceLevel(mDisplay, DPMSModeOn);
1522 DPMSInfo(mDisplay, &state, &onoff);
1523 if (onoff)
1525 mp_msg(MSGT_VO, MSGL_V,
1526 "Successfully enabled DPMS\n");
1527 } else
1529 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1533 dpms_disabled = 0;
1535 #endif
1538 static void saver_off(Display * mDisplay)
1540 int nothing;
1542 if (screensaver_off)
1543 return;
1544 screensaver_off = 1;
1545 if (xss_suspend(mDisplay, True))
1546 return;
1547 #ifdef CONFIG_XDPMS
1548 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1550 BOOL onoff;
1551 CARD16 state;
1553 DPMSInfo(mDisplay, &state, &onoff);
1554 if (onoff)
1556 Status stat;
1558 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1559 dpms_disabled = 1;
1560 stat = DPMSDisable(mDisplay); // monitor powersave off
1561 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1564 #endif
1567 static XErrorHandler old_handler = NULL;
1568 static int selectinput_err = 0;
1569 static int x11_selectinput_errorhandler(Display * display,
1570 XErrorEvent * event)
1572 if (event->error_code == BadAccess)
1574 selectinput_err = 1;
1575 mp_msg(MSGT_VO, MSGL_ERR,
1576 "X11 error: BadAccess during XSelectInput Call\n");
1577 mp_msg(MSGT_VO, MSGL_ERR,
1578 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1579 /* If you think MPlayer should shutdown with this error,
1580 * comment out the following line */
1581 return 0;
1583 if (old_handler != NULL)
1584 old_handler(display, event);
1585 else
1586 x11_errorhandler(display, event);
1587 return 0;
1590 void vo_x11_selectinput_witherr(Display * display, Window w,
1591 long event_mask)
1593 XSync(display, False);
1594 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1595 selectinput_err = 0;
1596 if (vo_nomouse_input)
1598 XSelectInput(display, w,
1599 event_mask &
1600 (~(ButtonPressMask | ButtonReleaseMask)));
1601 } else
1603 XSelectInput(display, w, event_mask);
1605 XSync(display, False);
1606 XSetErrorHandler(old_handler);
1607 if (selectinput_err)
1609 mp_msg(MSGT_VO, MSGL_ERR,
1610 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1611 XSelectInput(display, w,
1612 event_mask &
1614 (ButtonPressMask | ButtonReleaseMask |
1615 PointerMotionMask)));
1619 #ifdef CONFIG_XF86VM
1620 void vo_vm_switch(struct vo *vo)
1622 struct vo_x11_state *x11 = vo->x11;
1623 struct MPOpts *opts = vo->opts;
1624 Display *mDisplay = x11->display;
1625 int vm_event, vm_error;
1626 int vm_ver, vm_rev;
1627 int i, j, have_vm = 0;
1628 int X = vo->dwidth, Y = vo->dheight;
1629 int modeline_width, modeline_height;
1631 int modecount;
1633 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1635 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1636 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1637 vm_rev);
1638 have_vm = 1;
1639 } else {
1640 mp_msg(MSGT_VO, MSGL_WARN,
1641 "XF86VidMode extension not available.\n");
1644 if (have_vm)
1646 if (vidmodes == NULL)
1647 XF86VidModeGetAllModeLines(mDisplay, x11->screen, &modecount,
1648 &vidmodes);
1649 j = 0;
1650 modeline_width = vidmodes[0]->hdisplay;
1651 modeline_height = vidmodes[0]->vdisplay;
1653 for (i = 1; i < modecount; i++)
1654 if ((vidmodes[i]->hdisplay >= X)
1655 && (vidmodes[i]->vdisplay >= Y))
1656 if ((vidmodes[i]->hdisplay <= modeline_width)
1657 && (vidmodes[i]->vdisplay <= modeline_height))
1659 modeline_width = vidmodes[i]->hdisplay;
1660 modeline_height = vidmodes[i]->vdisplay;
1661 j = i;
1664 mp_tmsg(MSGT_VO, MSGL_INFO, "XF86VM: Selected video mode %dx%d for image size %dx%d.\n",
1665 modeline_width, modeline_height, X, Y);
1666 XF86VidModeLockModeSwitch(mDisplay, x11->screen, 0);
1667 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1668 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1670 // FIXME: all this is more of a hack than proper solution
1671 X = (opts->vo_screenwidth - modeline_width) / 2;
1672 Y = (opts->vo_screenheight - modeline_height) / 2;
1673 XF86VidModeSetViewPort(mDisplay, x11->screen, X, Y);
1674 vo->dx = X;
1675 vo->dy = Y;
1676 vo->dwidth = modeline_width;
1677 vo->dheight = modeline_height;
1678 aspect_save_screenres(vo, modeline_width, modeline_height);
1682 void vo_vm_close(struct vo *vo)
1684 Display *dpy = vo->x11->display;
1685 struct MPOpts *opts = vo->opts;
1686 if (vidmodes != NULL)
1688 int i, modecount;
1690 free(vidmodes);
1691 vidmodes = NULL;
1692 XF86VidModeGetAllModeLines(dpy, vo->x11->screen, &modecount,
1693 &vidmodes);
1694 for (i = 0; i < modecount; i++)
1695 if ((vidmodes[i]->hdisplay == opts->vo_screenwidth)
1696 && (vidmodes[i]->vdisplay == opts->vo_screenheight))
1698 mp_msg(MSGT_VO, MSGL_INFO,
1699 "Returning to original mode %dx%d\n",
1700 opts->vo_screenwidth, opts->vo_screenheight);
1701 break;
1704 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1705 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1706 free(vidmodes);
1707 vidmodes = NULL;
1711 double vo_vm_get_fps(struct vo *vo)
1713 struct vo_x11_state *x11 = vo->x11;
1714 int clock;
1715 XF86VidModeModeLine modeline;
1716 if (!XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline))
1717 return 0;
1718 if (modeline.privsize)
1719 XFree(modeline.private);
1720 return 1e3 * clock / modeline.htotal / modeline.vtotal;
1722 #endif
1724 #endif /* X11_FULLSCREEN */
1728 * Scan the available visuals on this Display/Screen. Try to find
1729 * the 'best' available TrueColor visual that has a decent color
1730 * depth (at least 15bit). If there are multiple visuals with depth
1731 * >= 15bit, we prefer visuals with a smaller color depth.
1733 int vo_find_depth_from_visuals(Display * dpy, int screen,
1734 Visual ** visual_return)
1736 XVisualInfo visual_tmpl;
1737 XVisualInfo *visuals;
1738 int nvisuals, i;
1739 int bestvisual = -1;
1740 int bestvisual_depth = -1;
1742 visual_tmpl.screen = screen;
1743 visual_tmpl.class = TrueColor;
1744 visuals = XGetVisualInfo(dpy,
1745 VisualScreenMask | VisualClassMask,
1746 &visual_tmpl, &nvisuals);
1747 if (visuals != NULL)
1749 for (i = 0; i < nvisuals; i++)
1751 mp_msg(MSGT_VO, MSGL_V,
1752 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1753 visuals[i].visualid, visuals[i].depth,
1754 visuals[i].red_mask, visuals[i].green_mask,
1755 visuals[i].blue_mask);
1757 * Save the visual index and its depth, if this is the first
1758 * truecolor visul, or a visual that is 'preferred' over the
1759 * previous 'best' visual.
1761 if (bestvisual_depth == -1
1762 || (visuals[i].depth >= 15
1763 && (visuals[i].depth < bestvisual_depth
1764 || bestvisual_depth < 15)))
1766 bestvisual = i;
1767 bestvisual_depth = visuals[i].depth;
1771 if (bestvisual != -1 && visual_return != NULL)
1772 *visual_return = visuals[bestvisual].visual;
1774 XFree(visuals);
1776 return bestvisual_depth;
1780 static Colormap cmap = None;
1781 static XColor cols[256];
1782 static int cm_size, red_mask, green_mask, blue_mask;
1785 Colormap vo_x11_create_colormap(struct vo *vo, XVisualInfo *vinfo)
1787 struct vo_x11_state *x11 = vo->x11;
1788 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1790 if (vinfo->class != DirectColor)
1791 return XCreateColormap(x11->display, x11->rootwin, vinfo->visual,
1792 AllocNone);
1794 /* can this function get called twice or more? */
1795 if (cmap)
1796 return cmap;
1797 cm_size = vinfo->colormap_size;
1798 red_mask = vinfo->red_mask;
1799 green_mask = vinfo->green_mask;
1800 blue_mask = vinfo->blue_mask;
1801 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1802 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1803 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1804 rvu = 65536ull * ru / (red_mask + ru);
1805 gvu = 65536ull * gu / (green_mask + gu);
1806 bvu = 65536ull * bu / (blue_mask + bu);
1807 r = g = b = 0;
1808 rv = gv = bv = 0;
1809 m = DoRed | DoGreen | DoBlue;
1810 for (k = 0; k < cm_size; k++)
1812 int t;
1814 cols[k].pixel = r | g | b;
1815 cols[k].red = rv;
1816 cols[k].green = gv;
1817 cols[k].blue = bv;
1818 cols[k].flags = m;
1819 t = (r + ru) & red_mask;
1820 if (t < r)
1821 m &= ~DoRed;
1822 r = t;
1823 t = (g + gu) & green_mask;
1824 if (t < g)
1825 m &= ~DoGreen;
1826 g = t;
1827 t = (b + bu) & blue_mask;
1828 if (t < b)
1829 m &= ~DoBlue;
1830 b = t;
1831 rv += rvu;
1832 gv += gvu;
1833 bv += bvu;
1835 cmap = XCreateColormap(x11->display, x11->rootwin, vinfo->visual, AllocAll);
1836 XStoreColors(x11->display, cmap, cols, cm_size);
1837 return cmap;
1841 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1842 * hue and red/green/blue intensity, but we cannot do saturation.
1843 * Currently only gamma, brightness and contrast are implemented.
1844 * Is there sufficient interest for hue and/or red/green/blue intensity?
1846 /* these values have range [-100,100] and are initially 0 */
1847 static int vo_gamma = 0;
1848 static int vo_brightness = 0;
1849 static int vo_contrast = 0;
1851 static int transform_color(float val,
1852 float brightness, float contrast, float gamma) {
1853 float s = pow(val, gamma);
1854 s = (s - 0.5) * contrast + 0.5;
1855 s += brightness;
1856 if (s < 0)
1857 s = 0;
1858 if (s > 1)
1859 s = 1;
1860 return (unsigned short) (s * 65535);
1863 uint32_t vo_x11_set_equalizer(struct vo *vo, const char *name, int value)
1865 float gamma, brightness, contrast;
1866 float rf, gf, bf;
1867 int k;
1870 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1871 * of TrueColor-ed window but be careful:
1872 * Unlike the colormaps, which are private for the X client
1873 * who created them and thus automatically destroyed on client
1874 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1875 * setting and _must_ be restored before the process exits.
1876 * Unforunately when the process crashes (or gets killed
1877 * for some reason) it is impossible to restore the setting,
1878 * and such behaviour could be rather annoying for the users.
1880 if (cmap == None)
1881 return VO_NOTAVAIL;
1883 if (!strcasecmp(name, "brightness"))
1884 vo_brightness = value;
1885 else if (!strcasecmp(name, "contrast"))
1886 vo_contrast = value;
1887 else if (!strcasecmp(name, "gamma"))
1888 vo_gamma = value;
1889 else
1890 return VO_NOTIMPL;
1892 brightness = 0.01 * vo_brightness;
1893 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1894 gamma = pow(2, -0.02 * vo_gamma);
1896 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1897 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
1898 green_mask;
1899 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
1901 /* now recalculate the colormap using the newly set value */
1902 for (k = 0; k < cm_size; k++)
1904 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
1905 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
1906 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
1909 XStoreColors(vo->x11->display, cmap, cols, cm_size);
1910 XFlush(vo->x11->display);
1911 return VO_TRUE;
1914 uint32_t vo_x11_get_equalizer(const char *name, int *value)
1916 if (cmap == None)
1917 return VO_NOTAVAIL;
1918 if (!strcasecmp(name, "brightness"))
1919 *value = vo_brightness;
1920 else if (!strcasecmp(name, "contrast"))
1921 *value = vo_contrast;
1922 else if (!strcasecmp(name, "gamma"))
1923 *value = vo_gamma;
1924 else
1925 return VO_NOTIMPL;
1926 return VO_TRUE;
1929 #ifdef CONFIG_XV
1930 int vo_xv_set_eq(struct vo *vo, uint32_t xv_port, const char *name, int value)
1932 XvAttribute *attributes;
1933 int i, howmany, xv_atom;
1935 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
1937 /* get available attributes */
1938 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1939 for (i = 0; i < howmany && attributes; i++)
1940 if (attributes[i].flags & XvSettable)
1942 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1943 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1944 then trigger it if it's ok so that the other values are at default upon query */
1945 if (xv_atom != None)
1947 int hue = 0, port_value, port_min, port_max;
1949 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1950 (!strcasecmp(name, "brightness")))
1951 port_value = value;
1952 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1953 (!strcasecmp(name, "contrast")))
1954 port_value = value;
1955 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1956 (!strcasecmp(name, "saturation")))
1957 port_value = value;
1958 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1959 (!strcasecmp(name, "hue")))
1961 port_value = value;
1962 hue = 1;
1963 } else
1964 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1965 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1966 (!strcasecmp(name, "red_intensity")))
1967 port_value = value;
1968 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1969 && (!strcasecmp(name, "green_intensity")))
1970 port_value = value;
1971 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1972 && (!strcasecmp(name, "blue_intensity")))
1973 port_value = value;
1974 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
1975 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
1976 && (!strcasecmp(name, "bt_709")))
1977 port_value = value;
1978 else
1979 continue;
1981 port_min = attributes[i].min_value;
1982 port_max = attributes[i].max_value;
1984 /* nvidia hue workaround */
1985 if (hue && port_min == 0 && port_max == 360)
1987 port_value =
1988 (port_value >=
1989 0) ? (port_value - 100) : (port_value + 100);
1991 // -100 -> min
1992 // 0 -> (max+min)/2
1993 // +100 -> max
1994 port_value =
1995 (port_value + 100) * (port_max - port_min) / 200 +
1996 port_min;
1997 XvSetPortAttribute(vo->x11->display, xv_port, xv_atom, port_value);
1998 return VO_TRUE;
2001 return VO_FALSE;
2004 int vo_xv_get_eq(struct vo *vo, uint32_t xv_port, const char *name, int *value)
2007 XvAttribute *attributes;
2008 int i, howmany, xv_atom;
2010 /* get available attributes */
2011 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
2012 for (i = 0; i < howmany && attributes; i++)
2013 if (attributes[i].flags & XvGettable)
2015 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
2016 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2017 then trigger it if it's ok so that the other values are at default upon query */
2018 if (xv_atom != None)
2020 int val, port_value = 0, port_min, port_max;
2022 XvGetPortAttribute(vo->x11->display, xv_port, xv_atom,
2023 &port_value);
2025 port_min = attributes[i].min_value;
2026 port_max = attributes[i].max_value;
2027 val =
2028 (port_value - port_min) * 200 / (port_max - port_min) -
2029 100;
2031 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2032 (!strcasecmp(name, "brightness")))
2033 *value = val;
2034 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2035 (!strcasecmp(name, "contrast")))
2036 *value = val;
2037 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2038 (!strcasecmp(name, "saturation")))
2039 *value = val;
2040 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2041 (!strcasecmp(name, "hue")))
2043 /* nasty nvidia detect */
2044 if (port_min == 0 && port_max == 360)
2045 *value = (val >= 0) ? (val - 100) : (val + 100);
2046 else
2047 *value = val;
2048 } else
2049 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2050 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2051 (!strcasecmp(name, "red_intensity")))
2052 *value = val;
2053 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2054 && (!strcasecmp(name, "green_intensity")))
2055 *value = val;
2056 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2057 && (!strcasecmp(name, "blue_intensity")))
2058 *value = val;
2059 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
2060 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
2061 && (!strcasecmp(name, "bt_709")))
2062 *value = val;
2063 else
2064 continue;
2066 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2067 name, *value);
2068 return VO_TRUE;
2071 return VO_FALSE;
2075 * \brief Interns the requested atom if it is available.
2077 * \param atom_name String containing the name of the requested atom.
2079 * \return Returns the atom if available, else None is returned.
2082 static Atom xv_intern_atom_if_exists(struct vo_x11_state *x11,
2083 char const *atom_name)
2085 XvAttribute * attributes;
2086 int attrib_count,i;
2087 Atom xv_atom = None;
2089 attributes = XvQueryPortAttributes(x11->display, x11->xv_port, &attrib_count );
2090 if( attributes!=NULL )
2092 for ( i = 0; i < attrib_count; ++i )
2094 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2096 xv_atom = XInternAtom(x11->display, atom_name, False );
2097 break; // found what we want, break out
2100 XFree( attributes );
2103 return xv_atom;
2107 * \brief Try to enable vsync for xv.
2108 * \return Returns -1 if not available, 0 on failure and 1 on success.
2110 int vo_xv_enable_vsync(struct vo *vo)
2112 struct vo_x11_state *x11 = vo->x11;
2113 Atom xv_atom = xv_intern_atom_if_exists(x11, "XV_SYNC_TO_VBLANK");
2114 if (xv_atom == None)
2115 return -1;
2116 return XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1) == Success;
2120 * \brief Get maximum supported source image dimensions.
2122 * This function does not set the variables pointed to by
2123 * width and height if the information could not be retrieved,
2124 * so the caller is reponsible for properly initializing them.
2126 * \param width [out] The maximum width gets stored here.
2127 * \param height [out] The maximum height gets stored here.
2130 void vo_xv_get_max_img_dim(struct vo *vo, uint32_t * width, uint32_t * height)
2132 struct vo_x11_state *x11 = vo->x11;
2133 XvEncodingInfo * encodings;
2134 //unsigned long num_encodings, idx; to int or too long?!
2135 unsigned int num_encodings, idx;
2137 XvQueryEncodings(x11->display, x11->xv_port, &num_encodings, &encodings);
2139 if ( encodings )
2141 for ( idx = 0; idx < num_encodings; ++idx )
2143 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2145 *width = encodings[idx].width;
2146 *height = encodings[idx].height;
2147 break;
2152 mp_msg( MSGT_VO, MSGL_V,
2153 "[xv common] Maximum source image dimensions: %ux%u\n",
2154 *width, *height );
2156 XvFreeEncodingInfo( encodings );
2160 * \brief Print information about the colorkey method and source.
2162 * \param ck_handling Integer value containing the information about
2163 * colorkey handling (see x11_common.h).
2165 * Outputs the content of |ck_handling| as a readable message.
2168 static void vo_xv_print_ck_info(struct vo_x11_state *x11)
2170 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2172 switch ( x11->xv_ck_info.method )
2174 case CK_METHOD_NONE:
2175 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2176 case CK_METHOD_AUTOPAINT:
2177 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2178 case CK_METHOD_MANUALFILL:
2179 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2180 case CK_METHOD_BACKGROUND:
2181 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2184 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2186 switch ( x11->xv_ck_info.source )
2188 case CK_SRC_CUR:
2189 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2190 x11->xv_colorkey );
2191 break;
2192 case CK_SRC_USE:
2193 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2195 mp_msg( MSGT_VO, MSGL_V,
2196 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2197 x11->xv_colorkey );
2199 else
2201 mp_msg( MSGT_VO, MSGL_V,
2202 "Using colorkey from MPlayer (0x%06lx)."
2203 " Use -colorkey to change.\n",
2204 x11->xv_colorkey );
2206 break;
2207 case CK_SRC_SET:
2208 mp_msg( MSGT_VO, MSGL_V,
2209 "Setting and using colorkey from MPlayer (0x%06lx)."
2210 " Use -colorkey to change.\n",
2211 x11->xv_colorkey );
2212 break;
2216 * \brief Init colorkey depending on the settings in xv_ck_info.
2218 * \return Returns 0 on failure and 1 on success.
2220 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2221 * flags in xv_ck_info.
2223 * Possiblilities:
2224 * * Methods
2225 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2226 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2227 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2228 * * Sources
2229 * - use currently set colorkey ( CK_SRC_CUR )
2230 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2231 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2233 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2234 * we don't draw anything as this means it was forced to off.
2236 int vo_xv_init_colorkey(struct vo *vo)
2238 struct vo_x11_state *x11 = vo->x11;
2239 Atom xv_atom;
2240 int rez;
2242 /* check if colorkeying is needed */
2243 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_COLORKEY");
2245 /* if we have to deal with colorkeying ... */
2246 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2248 /* check if we should use the colorkey specified in vo_colorkey */
2249 if ( x11->xv_ck_info.source != CK_SRC_CUR )
2251 x11->xv_colorkey = vo_colorkey;
2253 /* check if we have to set the colorkey too */
2254 if ( x11->xv_ck_info.source == CK_SRC_SET )
2256 xv_atom = XInternAtom(x11->display, "XV_COLORKEY",False);
2258 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, vo_colorkey);
2259 if ( rez != Success )
2261 mp_msg( MSGT_VO, MSGL_FATAL,
2262 "[xv common] Couldn't set colorkey!\n" );
2263 return 0; // error setting colorkey
2267 else
2269 int colorkey_ret;
2271 rez=XvGetPortAttribute(x11->display,x11->xv_port, xv_atom, &colorkey_ret);
2272 if ( rez == Success )
2274 x11->xv_colorkey = colorkey_ret;
2276 else
2278 mp_msg( MSGT_VO, MSGL_FATAL,
2279 "[xv common] Couldn't get colorkey!"
2280 "Maybe the selected Xv port has no overlay.\n" );
2281 return 0; // error getting colorkey
2285 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_AUTOPAINT_COLORKEY");
2287 /* should we draw the colorkey ourselves or activate autopainting? */
2288 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2290 rez = !Success; // reset rez to something different than Success
2292 if ( xv_atom != None ) // autopaint is supported
2294 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1);
2297 if ( rez != Success )
2299 // fallback to manual colorkey drawing
2300 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2303 else // disable colorkey autopainting if supported
2305 if ( xv_atom != None ) // we have autopaint attribute
2307 XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 0);
2311 else // do no colorkey drawing at all
2313 x11->xv_ck_info.method = CK_METHOD_NONE;
2314 } /* end: should we draw colorkey */
2316 /* output information about the current colorkey settings */
2317 vo_xv_print_ck_info(x11);
2319 return 1; // success
2323 * \brief Draw the colorkey on the video window.
2325 * Draws the colorkey depending on the set method ( colorkey_handling ).
2327 * Also draws the black bars ( when the video doesn't fit the display in
2328 * fullscreen ) separately, so they don't overlap with the video area.
2329 * It doesn't call XFlush.
2332 void vo_xv_draw_colorkey(struct vo *vo, int32_t x, int32_t y,
2333 int32_t w, int32_t h)
2335 struct vo_x11_state *x11 = vo->x11;
2336 if( x11->xv_ck_info.method == CK_METHOD_MANUALFILL ||
2337 x11->xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2339 XSetForeground(x11->display, x11->vo_gc, x11->xv_colorkey );
2340 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2341 x, y,
2342 w, h );
2346 /** \brief Tests if a valid argument for the ck suboption was given. */
2347 int xv_test_ck( void * arg )
2349 strarg_t * strarg = (strarg_t *)arg;
2351 if ( strargcmp( strarg, "use" ) == 0 ||
2352 strargcmp( strarg, "set" ) == 0 ||
2353 strargcmp( strarg, "cur" ) == 0 )
2355 return 1;
2358 return 0;
2360 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2361 int xv_test_ckm( void * arg )
2363 strarg_t * strarg = (strarg_t *)arg;
2365 if ( strargcmp( strarg, "bg" ) == 0 ||
2366 strargcmp( strarg, "man" ) == 0 ||
2367 strargcmp( strarg, "auto" ) == 0 )
2369 return 1;
2372 return 0;
2376 * \brief Modify the colorkey_handling var according to str
2378 * Checks if a valid pointer ( not NULL ) to the string
2379 * was given. And in that case modifies the colorkey_handling
2380 * var to reflect the requested behaviour.
2381 * If nothing happens the content of colorkey_handling stays
2382 * the same.
2384 * \param str Pointer to the string or NULL
2387 void xv_setup_colorkeyhandling(struct vo *vo, const char *ck_method_str,
2388 const char *ck_str)
2390 struct vo_x11_state *x11 = vo->x11;
2391 /* check if a valid pointer to the string was passed */
2392 if ( ck_str )
2394 if ( strncmp( ck_str, "use", 3 ) == 0 )
2396 x11->xv_ck_info.source = CK_SRC_USE;
2398 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2400 x11->xv_ck_info.source = CK_SRC_SET;
2403 /* check if a valid pointer to the string was passed */
2404 if ( ck_method_str )
2406 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2408 x11->xv_ck_info.method = CK_METHOD_BACKGROUND;
2410 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2412 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2414 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2416 x11->xv_ck_info.method = CK_METHOD_AUTOPAINT;
2421 #endif
2423 struct vo_x11_state *vo_x11_init_state(void)
2425 struct vo_x11_state *s = talloc_ptrtype(NULL, s);
2426 *s = (struct vo_x11_state){
2427 .xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR },
2428 .olddecor = MWM_DECOR_ALL,
2429 .oldfuncs = MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
2430 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE,
2431 .old_gravity = NorthWestGravity,
2433 return s;