cocoa_common: refactor menu generation
[mplayer.git] / libvo / x11_common.c
blobcf8bb2dc4f7557ea835a8ed63dcc9084406fe925
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 "bstr.h"
27 #include "options.h"
28 #include "mp_msg.h"
29 #include "mp_fifo.h"
30 #include "libavutil/common.h"
31 #include "x11_common.h"
32 #include "talloc.h"
34 #ifdef X11_FULLSCREEN
36 #include <string.h>
37 #include <unistd.h>
38 #include <assert.h>
40 #include "video_out.h"
41 #include "aspect.h"
42 #include "geometry.h"
43 #include "osdep/timer.h"
45 #include <X11/Xmd.h>
46 #include <X11/Xlib.h>
47 #include <X11/Xutil.h>
48 #include <X11/Xatom.h>
49 #include <X11/keysym.h>
51 #ifdef CONFIG_XSS
52 #include <X11/extensions/scrnsaver.h>
53 #endif
55 #ifdef CONFIG_XDPMS
56 #include <X11/extensions/dpms.h>
57 #endif
59 #ifdef CONFIG_XINERAMA
60 #include <X11/extensions/Xinerama.h>
61 #endif
63 #ifdef CONFIG_XF86VM
64 #include <X11/extensions/xf86vmode.h>
65 #endif
67 #ifdef CONFIG_XF86XK
68 #include <X11/XF86keysym.h>
69 #endif
71 #ifdef CONFIG_XV
72 #include <X11/extensions/Xv.h>
73 #include <X11/extensions/Xvlib.h>
75 #include "subopt-helper.h"
76 #endif
78 #include "input/input.h"
79 #include "input/keycodes.h"
81 #define WIN_LAYER_ONBOTTOM 2
82 #define WIN_LAYER_NORMAL 4
83 #define WIN_LAYER_ONTOP 6
84 #define WIN_LAYER_ABOVE_DOCK 10
86 int fs_layer = WIN_LAYER_ABOVE_DOCK;
88 int stop_xscreensaver = 0;
90 static int dpms_disabled = 0;
92 char *mDisplayName = NULL;
94 char **vo_fstype_list;
96 /* 1 means that the WM is metacity (broken as hell) */
97 int metacity_hack = 0;
99 #ifdef CONFIG_XF86VM
100 XF86VidModeModeInfo **vidmodes = NULL;
101 XF86VidModeModeLine modeline;
102 #endif
104 static int vo_x11_get_fs_type(int supported);
105 static void saver_off(Display *);
106 static void saver_on(Display *);
109 * Sends the EWMH fullscreen state event.
111 * action: could be one of _NET_WM_STATE_REMOVE -- remove state
112 * _NET_WM_STATE_ADD -- add state
113 * _NET_WM_STATE_TOGGLE -- toggle
115 void vo_x11_ewmh_fullscreen(struct vo_x11_state *x11, int action)
117 assert(action == _NET_WM_STATE_REMOVE ||
118 action == _NET_WM_STATE_ADD || action == _NET_WM_STATE_TOGGLE);
120 if (x11->fs_type & vo_wm_FULLSCREEN)
122 XEvent xev;
124 /* init X event structure for _NET_WM_FULLSCREEN client message */
125 xev.xclient.type = ClientMessage;
126 xev.xclient.serial = 0;
127 xev.xclient.send_event = True;
128 xev.xclient.message_type = x11->XA_NET_WM_STATE;
129 xev.xclient.window = x11->window;
130 xev.xclient.format = 32;
131 xev.xclient.data.l[0] = action;
132 xev.xclient.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
133 xev.xclient.data.l[2] = 0;
134 xev.xclient.data.l[3] = 0;
135 xev.xclient.data.l[4] = 0;
137 /* finally send that damn thing */
138 if (!XSendEvent(x11->display, DefaultRootWindow(x11->display), False,
139 SubstructureRedirectMask | SubstructureNotifyMask,
140 &xev))
142 mp_tmsg(MSGT_VO, MSGL_ERR, "\nX11: Couldn't send EWMH fullscreen event!\n");
147 static void vo_hidecursor(Display * disp, Window win)
149 Cursor no_ptr;
150 Pixmap bm_no;
151 XColor black, dummy;
152 Colormap colormap;
153 const char bm_no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
155 if (WinID == 0)
156 return; // do not hide if playing on the root window
158 colormap = DefaultColormap(disp, DefaultScreen(disp));
159 if ( !XAllocNamedColor(disp, colormap, "black", &black, &dummy) )
161 return; // color alloc failed, give up
163 bm_no = XCreateBitmapFromData(disp, win, bm_no_data, 8, 8);
164 no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
165 XDefineCursor(disp, win, no_ptr);
166 XFreeCursor(disp, no_ptr);
167 if (bm_no != None)
168 XFreePixmap(disp, bm_no);
169 XFreeColors(disp,colormap,&black.pixel,1,0);
172 static void vo_showcursor(Display * disp, Window win)
174 if (WinID == 0)
175 return;
176 XDefineCursor(disp, win, 0);
179 static int x11_errorhandler(Display * display, XErrorEvent * event)
181 #define MSGLEN 60
182 char msg[MSGLEN];
184 XGetErrorText(display, event->error_code, (char *) &msg, MSGLEN);
186 mp_msg(MSGT_VO, MSGL_ERR, "X11 error: %s\n", msg);
188 mp_msg(MSGT_VO, MSGL_V,
189 "Type: %x, display: %p, resourceid: %lx, serial: %lx\n",
190 event->type, event->display, event->resourceid, event->serial);
191 mp_msg(MSGT_VO, MSGL_V,
192 "Error code: %x, request code: %x, minor code: %x\n",
193 event->error_code, event->request_code, event->minor_code);
195 // abort();
196 //exit_player("X11 error");
197 return 0;
198 #undef MSGLEN
201 void fstype_help(void)
203 mp_tmsg(MSGT_VO, MSGL_INFO, "Available fullscreen layer change modes:\n");
204 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FULL_SCREEN_TYPES\n");
206 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "none",
207 "don't set fullscreen window layer");
208 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer",
209 "use _WIN_LAYER hint with default layer");
210 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer=<0..15>",
211 "use _WIN_LAYER hint with a given layer number");
212 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "netwm",
213 "force NETWM style");
214 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "above",
215 "use _NETWM_STATE_ABOVE hint if available");
216 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "below",
217 "use _NETWM_STATE_BELOW hint if available");
218 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "fullscreen",
219 "use _NETWM_STATE_FULLSCREEN hint if available");
220 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "stays_on_top",
221 "use _NETWM_STATE_STAYS_ON_TOP hint if available");
222 mp_msg(MSGT_VO, MSGL_INFO,
223 "You can also negate the settings with simply putting '-' in the beginning");
224 mp_msg(MSGT_VO, MSGL_INFO, "\n");
227 static void fstype_dump(int fstype)
229 if (fstype)
231 mp_msg(MSGT_VO, MSGL_V, "[x11] Current fstype setting honours");
232 if (fstype & vo_wm_LAYER)
233 mp_msg(MSGT_VO, MSGL_V, " LAYER");
234 if (fstype & vo_wm_FULLSCREEN)
235 mp_msg(MSGT_VO, MSGL_V, " FULLSCREEN");
236 if (fstype & vo_wm_STAYS_ON_TOP)
237 mp_msg(MSGT_VO, MSGL_V, " STAYS_ON_TOP");
238 if (fstype & vo_wm_ABOVE)
239 mp_msg(MSGT_VO, MSGL_V, " ABOVE");
240 if (fstype & vo_wm_BELOW)
241 mp_msg(MSGT_VO, MSGL_V, " BELOW");
242 mp_msg(MSGT_VO, MSGL_V, " X atoms\n");
243 } else
244 mp_msg(MSGT_VO, MSGL_V,
245 "[x11] Current fstype setting doesn't honour any X atoms\n");
248 static int net_wm_support_state_test(struct vo_x11_state *x11, Atom atom)
250 #define NET_WM_STATE_TEST(x) { if (atom == x11->XA_NET_WM_STATE_##x) { mp_msg( MSGT_VO,MSGL_V, "[x11] Detected wm supports " #x " state.\n" ); return vo_wm_##x; } }
252 NET_WM_STATE_TEST(FULLSCREEN);
253 NET_WM_STATE_TEST(ABOVE);
254 NET_WM_STATE_TEST(STAYS_ON_TOP);
255 NET_WM_STATE_TEST(BELOW);
256 return 0;
259 static int x11_get_property(struct vo_x11_state *x11, Atom type, Atom ** args,
260 unsigned long *nitems)
262 int format;
263 unsigned long bytesafter;
265 return Success ==
266 XGetWindowProperty(x11->display, x11->rootwin, type, 0, 16384, False,
267 AnyPropertyType, &type, &format, nitems,
268 &bytesafter, (unsigned char **) args)
269 && *nitems > 0;
272 static int vo_wm_detect(struct vo *vo)
274 struct vo_x11_state *x11 = vo->x11;
275 int i;
276 int wm = 0;
277 unsigned long nitems;
278 Atom *args = NULL;
280 if (WinID >= 0)
281 return 0;
283 // -- supports layers
284 if (x11_get_property(x11, x11->XA_WIN_PROTOCOLS, &args, &nitems))
286 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports layers.\n");
287 for (i = 0; i < nitems; i++)
289 if (args[i] == x11->XA_WIN_LAYER)
291 wm |= vo_wm_LAYER;
292 metacity_hack |= 1;
293 } else
294 /* metacity is the only window manager I know which reports
295 * supporting only the _WIN_LAYER hint in _WIN_PROTOCOLS.
296 * (what's more support for it is broken) */
297 metacity_hack |= 2;
299 XFree(args);
300 if (wm && (metacity_hack == 1))
302 // metacity claims to support layers, but it is not the truth :-)
303 wm ^= vo_wm_LAYER;
304 mp_msg(MSGT_VO, MSGL_V,
305 "[x11] Using workaround for Metacity bugs.\n");
308 // --- netwm
309 if (x11_get_property(x11, x11->XA_NET_SUPPORTED, &args, &nitems))
311 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports NetWM.\n");
312 for (i = 0; i < nitems; i++)
313 wm |= net_wm_support_state_test(vo->x11, args[i]);
314 XFree(args);
317 if (wm == 0)
318 mp_msg(MSGT_VO, MSGL_V, "[x11] Unknown wm type...\n");
319 return wm;
322 #define XA_INIT(x) x11->XA##x = XInternAtom(x11->display, #x, False)
323 static void init_atoms(struct vo_x11_state *x11)
325 XA_INIT(_NET_SUPPORTED);
326 XA_INIT(_NET_WM_STATE);
327 XA_INIT(_NET_WM_STATE_FULLSCREEN);
328 XA_INIT(_NET_WM_STATE_ABOVE);
329 XA_INIT(_NET_WM_STATE_STAYS_ON_TOP);
330 XA_INIT(_NET_WM_STATE_BELOW);
331 XA_INIT(_NET_WM_PID);
332 XA_INIT(_NET_WM_NAME);
333 XA_INIT(_NET_WM_ICON_NAME);
334 XA_INIT(_WIN_PROTOCOLS);
335 XA_INIT(_WIN_LAYER);
336 XA_INIT(_WIN_HINTS);
337 XA_INIT(WM_PROTOCOLS);
338 XA_INIT(WM_DELETE_WINDOW);
339 XA_INIT(UTF8_STRING);
340 char buf[50];
341 sprintf(buf, "_NET_WM_CM_S%d", x11->screen);
342 x11->XA_NET_WM_CM = XInternAtom(x11->display, buf, False);
345 void update_xinerama_info(struct vo *vo) {
346 struct MPOpts *opts = vo->opts;
347 xinerama_x = xinerama_y = 0;
348 #ifdef CONFIG_XINERAMA
349 if (xinerama_screen >= -1 && XineramaIsActive(vo->x11->display))
351 int screen = xinerama_screen;
352 XineramaScreenInfo *screens;
353 int num_screens;
355 screens = XineramaQueryScreens(vo->x11->display, &num_screens);
356 if (screen >= num_screens)
357 screen = num_screens - 1;
358 if (screen == -1) {
359 int x = vo->dx + vo->dwidth / 2;
360 int y = vo->dy + vo->dheight / 2;
361 for (screen = num_screens - 1; screen > 0; screen--) {
362 int left = screens[screen].x_org;
363 int right = left + screens[screen].width;
364 int top = screens[screen].y_org;
365 int bottom = top + screens[screen].height;
366 if (left <= x && x <= right && top <= y && y <= bottom)
367 break;
370 if (screen < 0)
371 screen = 0;
372 opts->vo_screenwidth = screens[screen].width;
373 opts->vo_screenheight = screens[screen].height;
374 xinerama_x = screens[screen].x_org;
375 xinerama_y = screens[screen].y_org;
377 XFree(screens);
379 #endif
380 aspect_save_screenres(vo, opts->vo_screenwidth, opts->vo_screenheight);
383 int vo_init(struct vo *vo)
385 struct MPOpts *opts = vo->opts;
386 struct vo_x11_state *x11 = vo->x11;
387 // int mScreen;
388 int depth, bpp;
389 unsigned int mask;
391 // char * DisplayName = ":0.0";
392 // Display * mDisplay;
393 XImage *mXImage = NULL;
395 // Window mRootWin;
396 XWindowAttributes attribs;
397 char *dispName;
399 if (vo_rootwin)
400 WinID = 0; // use root window
402 if (x11->depthonscreen)
404 saver_off(x11->display);
405 return 1; // already called
408 XSetErrorHandler(x11_errorhandler);
410 #if 0
411 if (!mDisplayName)
412 if (!(mDisplayName = getenv("DISPLAY")))
413 mDisplayName = strdup(":0.0");
414 #else
415 dispName = XDisplayName(mDisplayName);
416 #endif
418 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
420 x11->display = XOpenDisplay(dispName);
421 if (!x11->display)
423 mp_msg(MSGT_VO, MSGL_ERR,
424 "vo: couldn't open the X11 display (%s)!\n", dispName);
425 return 0;
427 x11->screen = DefaultScreen(x11->display); // screen ID
428 x11->rootwin = RootWindow(x11->display, x11->screen); // root window ID
430 x11->xim = XOpenIM(x11->display, NULL, NULL, NULL);
432 init_atoms(vo->x11);
434 #ifdef CONFIG_XF86VM
436 int clock;
438 XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline);
439 if (!opts->vo_screenwidth)
440 opts->vo_screenwidth = modeline.hdisplay;
441 if (!opts->vo_screenheight)
442 opts->vo_screenheight = modeline.vdisplay;
444 #endif
446 if (!opts->vo_screenwidth)
447 opts->vo_screenwidth = DisplayWidth(x11->display, x11->screen);
448 if (!opts->vo_screenheight)
449 opts->vo_screenheight = DisplayHeight(x11->display, x11->screen);
451 // get color depth (from root window, or the best visual):
452 XGetWindowAttributes(x11->display, x11->rootwin, &attribs);
453 depth = attribs.depth;
455 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
457 Visual *visual;
459 depth = vo_find_depth_from_visuals(x11->display, x11->screen, &visual);
460 if (depth != -1)
461 mXImage = XCreateImage(x11->display, visual, depth, ZPixmap,
462 0, NULL, 1, 1, 8, 1);
463 } else
464 mXImage =
465 XGetImage(x11->display, x11->rootwin, 0, 0, 1, 1, AllPlanes, ZPixmap);
467 x11->depthonscreen = depth; // display depth on screen
469 // get bits/pixel from XImage structure:
470 if (mXImage == NULL)
472 mask = 0;
473 } else
476 * for the depth==24 case, the XImage structures might use
477 * 24 or 32 bits of data per pixel. The x11->depthonscreen
478 * field stores the amount of data per pixel in the
479 * XImage structure!
481 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
483 bpp = mXImage->bits_per_pixel;
484 if ((x11->depthonscreen + 7) / 8 != (bpp + 7) / 8)
485 x11->depthonscreen = bpp; // by A'rpi
486 mask =
487 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
488 mp_msg(MSGT_VO, MSGL_V,
489 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
490 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
491 XDestroyImage(mXImage);
493 if (((x11->depthonscreen + 7) / 8) == 2)
495 if (mask == 0x7FFF)
496 x11->depthonscreen = 15;
497 else if (mask == 0xFFFF)
498 x11->depthonscreen = 16;
500 // XCloseDisplay( mDisplay );
501 /* slightly improved local display detection AST */
502 if (strncmp(dispName, "unix:", 5) == 0)
503 dispName += 4;
504 else if (strncmp(dispName, "localhost:", 10) == 0)
505 dispName += 9;
506 if (*dispName == ':' && atoi(dispName + 1) < 10)
507 x11->display_is_local = 1;
508 else
509 x11->display_is_local = 0;
510 mp_msg(MSGT_VO, MSGL_V,
511 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
512 opts->vo_screenwidth, opts->vo_screenheight, depth, x11->depthonscreen,
513 dispName, x11->display_is_local ? "local" : "remote");
515 x11->wm_type = vo_wm_detect(vo);
517 x11->fs_type = vo_x11_get_fs_type(x11->wm_type);
519 fstype_dump(x11->fs_type);
521 saver_off(x11->display);
522 return 1;
525 void vo_uninit(struct vo_x11_state *x11)
527 if (!x11->display)
529 mp_msg(MSGT_VO, MSGL_V,
530 "vo: x11 uninit called but X11 not initialized..\n");
531 } else {
532 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
533 if (x11->xim)
534 XCloseIM(x11->xim);
535 XSetErrorHandler(NULL);
536 XCloseDisplay(x11->display);
537 x11->depthonscreen = 0;
538 x11->display = NULL;
540 talloc_free(x11);
543 static const struct mp_keymap keymap[] = {
544 // special keys
545 {XK_Pause, KEY_PAUSE}, {XK_Escape, KEY_ESC}, {XK_BackSpace, KEY_BS},
546 {XK_Tab, KEY_TAB}, {XK_Return, KEY_ENTER},
547 {XK_Menu, KEY_MENU}, {XK_Print, KEY_PRINT},
549 // cursor keys
550 {XK_Left, KEY_LEFT}, {XK_Right, KEY_RIGHT}, {XK_Up, KEY_UP}, {XK_Down, KEY_DOWN},
552 // navigation block
553 {XK_Insert, KEY_INSERT}, {XK_Delete, KEY_DELETE}, {XK_Home, KEY_HOME}, {XK_End, KEY_END},
554 {XK_Page_Up, KEY_PAGE_UP}, {XK_Page_Down, KEY_PAGE_DOWN},
556 // F-keys
557 {XK_F1, KEY_F+1}, {XK_F2, KEY_F+2}, {XK_F3, KEY_F+3}, {XK_F4, KEY_F+4},
558 {XK_F5, KEY_F+5}, {XK_F6, KEY_F+6}, {XK_F7, KEY_F+7}, {XK_F8, KEY_F+8},
559 {XK_F9, KEY_F+9}, {XK_F10, KEY_F+10}, {XK_F11, KEY_F+11}, {XK_F12, KEY_F+12},
561 // numpad independent of numlock
562 {XK_KP_Subtract, '-'}, {XK_KP_Add, '+'}, {XK_KP_Multiply, '*'}, {XK_KP_Divide, '/'},
563 {XK_KP_Enter, KEY_KPENTER},
565 // numpad with numlock
566 {XK_KP_0, KEY_KP0}, {XK_KP_1, KEY_KP1}, {XK_KP_2, KEY_KP2},
567 {XK_KP_3, KEY_KP3}, {XK_KP_4, KEY_KP4}, {XK_KP_5, KEY_KP5},
568 {XK_KP_6, KEY_KP6}, {XK_KP_7, KEY_KP7}, {XK_KP_8, KEY_KP8},
569 {XK_KP_9, KEY_KP9}, {XK_KP_Decimal, KEY_KPDEC},
570 {XK_KP_Separator, KEY_KPDEC},
572 // numpad without numlock
573 {XK_KP_Insert, KEY_KPINS}, {XK_KP_End, KEY_KP1}, {XK_KP_Down, KEY_KP2},
574 {XK_KP_Page_Down, KEY_KP3}, {XK_KP_Left, KEY_KP4}, {XK_KP_Begin, KEY_KP5},
575 {XK_KP_Right, KEY_KP6}, {XK_KP_Home, KEY_KP7}, {XK_KP_Up, KEY_KP8},
576 {XK_KP_Page_Up, KEY_KP9}, {XK_KP_Delete, KEY_KPDEL},
578 #ifdef XF86XK_AudioPause
579 {XF86XK_MenuKB, KEY_MENU},
580 {XF86XK_AudioPlay, KEY_PLAY}, {XF86XK_AudioPause, KEY_PAUSE}, {XF86XK_AudioStop, KEY_STOP},
581 {XF86XK_AudioPrev, KEY_PREV}, {XF86XK_AudioNext, KEY_NEXT},
582 {XF86XK_AudioMute, KEY_MUTE}, {XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN}, {XF86XK_AudioRaiseVolume, KEY_VOLUME_UP},
583 #endif
585 {0, 0}
588 static int vo_x11_lookupkey(int key)
590 static const char *passthrough_keys = " -+*/<>`~!@#$%^&()_{}:;\"\',.?\\|=[]";
591 int mpkey = 0;
592 if ((key >= 'a' && key <= 'z') ||
593 (key >= 'A' && key <= 'Z') ||
594 (key >= '0' && key <= '9') ||
595 (key > 0 && key < 256 && strchr(passthrough_keys, key)))
596 mpkey = key;
598 if (!mpkey)
599 mpkey = lookup_keymap_table(keymap, key);
601 return mpkey;
605 // ----- Motif header: -------
607 #define MWM_HINTS_FUNCTIONS (1L << 0)
608 #define MWM_HINTS_DECORATIONS (1L << 1)
609 #define MWM_HINTS_INPUT_MODE (1L << 2)
610 #define MWM_HINTS_STATUS (1L << 3)
612 #define MWM_FUNC_ALL (1L << 0)
613 #define MWM_FUNC_RESIZE (1L << 1)
614 #define MWM_FUNC_MOVE (1L << 2)
615 #define MWM_FUNC_MINIMIZE (1L << 3)
616 #define MWM_FUNC_MAXIMIZE (1L << 4)
617 #define MWM_FUNC_CLOSE (1L << 5)
619 #define MWM_DECOR_ALL (1L << 0)
620 #define MWM_DECOR_BORDER (1L << 1)
621 #define MWM_DECOR_RESIZEH (1L << 2)
622 #define MWM_DECOR_TITLE (1L << 3)
623 #define MWM_DECOR_MENU (1L << 4)
624 #define MWM_DECOR_MINIMIZE (1L << 5)
625 #define MWM_DECOR_MAXIMIZE (1L << 6)
627 #define MWM_INPUT_MODELESS 0
628 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
629 #define MWM_INPUT_SYSTEM_MODAL 2
630 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
631 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
633 #define MWM_TEAROFF_WINDOW (1L<<0)
635 typedef struct
637 long flags;
638 long functions;
639 long decorations;
640 long input_mode;
641 long state;
642 } MotifWmHints;
644 static MotifWmHints vo_MotifWmHints;
645 static Atom vo_MotifHints = None;
647 void vo_x11_decoration(struct vo *vo, int d)
649 struct vo_x11_state *x11 = vo->x11;
650 Atom mtype;
651 int mformat;
652 unsigned long mn, mb;
654 if (!WinID)
655 return;
657 if (vo_fsmode & 8)
659 XSetTransientForHint(x11->display, x11->window,
660 RootWindow(x11->display, x11->screen));
663 vo_MotifHints = XInternAtom(x11->display, "_MOTIF_WM_HINTS", 0);
664 if (vo_MotifHints != None)
666 if (!d)
668 MotifWmHints *mhints = NULL;
670 XGetWindowProperty(x11->display, x11->window,
671 vo_MotifHints, 0, 20, False,
672 vo_MotifHints, &mtype, &mformat, &mn,
673 &mb, (unsigned char **) &mhints);
674 if (mhints)
676 if (mhints->flags & MWM_HINTS_DECORATIONS)
677 x11->olddecor = mhints->decorations;
678 if (mhints->flags & MWM_HINTS_FUNCTIONS)
679 x11->oldfuncs = mhints->functions;
680 XFree(mhints);
684 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
685 vo_MotifWmHints.flags =
686 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
687 if (d)
689 vo_MotifWmHints.functions = x11->oldfuncs;
690 d = x11->olddecor;
692 #if 0
693 vo_MotifWmHints.decorations =
694 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
695 #else
696 vo_MotifWmHints.decorations =
697 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
698 #endif
699 XChangeProperty(x11->display, x11->window, vo_MotifHints,
700 vo_MotifHints, 32,
701 PropModeReplace,
702 (unsigned char *) &vo_MotifWmHints,
703 (vo_fsmode & 4) ? 4 : 5);
707 void vo_x11_classhint(struct vo *vo, Window window, const char *name)
709 struct MPOpts *opts = vo->opts;
710 struct vo_x11_state *x11 = vo->x11;
711 XClassHint wmClass;
712 pid_t pid = getpid();
714 wmClass.res_name = opts->vo_winname ? opts->vo_winname : (char *)name;
715 wmClass.res_class = "mplayer2";
716 XSetClassHint(x11->display, window, &wmClass);
717 XChangeProperty(x11->display, window, x11->XA_NET_WM_PID, XA_CARDINAL,
718 32, PropModeReplace, (unsigned char *) &pid, 1);
721 void vo_x11_uninit(struct vo *vo)
723 struct vo_x11_state *x11 = vo->x11;
724 saver_on(x11->display);
725 if (x11->window != None)
726 vo_showcursor(x11->display, x11->window);
728 if (x11->f_gc != None)
730 XFreeGC(vo->x11->display, x11->f_gc);
731 x11->f_gc = None;
734 if (x11->vo_gc != None)
736 XFreeGC(vo->x11->display, x11->vo_gc);
737 x11->vo_gc = None;
739 if (x11->window != None)
741 XClearWindow(x11->display, x11->window);
742 if (WinID < 0)
744 XEvent xev;
746 if (x11->xic)
747 XDestroyIC(x11->xic);
748 x11->xic = NULL;
750 XUnmapWindow(x11->display, x11->window);
751 XSelectInput(x11->display, x11->window, StructureNotifyMask);
752 XDestroyWindow(x11->display, x11->window);
755 XNextEvent(x11->display, &xev);
757 while (xev.type != DestroyNotify
758 || xev.xdestroywindow.event != x11->window);
760 x11->window = None;
762 vo_fs = 0;
763 x11->vo_old_width = x11->vo_old_height = 0;
764 x11->last_video_width = 0;
765 x11->last_video_height = 0;
766 x11->size_changed_during_fs = false;
770 static int check_resize(struct vo *vo)
772 int old_w = vo->dwidth, old_h = vo->dheight;
773 int old_x = vo->dx, old_y = vo->dy;
774 int rc = 0;
775 vo_x11_update_geometry(vo, true);
776 if (vo->dwidth != old_w || vo->dheight != old_h)
777 rc |= VO_EVENT_RESIZE;
778 if (vo->dx != old_x || vo->dy != old_y)
779 rc |= VO_EVENT_MOVE;
780 return rc;
783 int vo_x11_check_events(struct vo *vo)
785 struct vo_x11_state *x11 = vo->x11;
786 struct MPOpts *opts = vo->opts;
787 Display *display = vo->x11->display;
788 int ret = 0;
789 XEvent Event;
791 if (x11->mouse_waiting_hide && opts->cursor_autohide_delay != -1 &&
792 (GetTimerMS() - x11->mouse_timer >= opts->cursor_autohide_delay)) {
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 char buf[100];
816 KeySym keySym = 0;
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 if (x11->xic) {
827 Status status;
828 int len = Xutf8LookupString(x11->xic, &Event.xkey, buf,
829 sizeof(buf), &keySym,
830 &status);
831 int mpkey = vo_x11_lookupkey(keySym);
832 if (mpkey) {
833 mplayer_put_key(vo->key_fifo, mpkey | modifiers);
834 } else if (status == XLookupChars
835 || status == XLookupBoth)
837 struct bstr t = { buf, len };
838 mplayer_put_key_utf8(vo->key_fifo, modifiers, t);
840 } else {
841 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
842 &x11->compose_status);
843 int mpkey = vo_x11_lookupkey(keySym);
844 if (mpkey)
845 mplayer_put_key(vo->key_fifo, mpkey | modifiers);
847 ret |= VO_EVENT_KEYPRESS;
849 break;
850 case MotionNotify:
851 vo_mouse_movement(vo, Event.xmotion.x, Event.xmotion.y);
853 if (opts->cursor_autohide_delay > -2) {
854 vo_showcursor(display, x11->window);
855 x11->mouse_waiting_hide = 1;
856 x11->mouse_timer = GetTimerMS();
858 break;
859 case ButtonPress:
860 if (opts->cursor_autohide_delay > -2) {
861 vo_showcursor(display, x11->window);
862 x11->mouse_waiting_hide = 1;
863 x11->mouse_timer = GetTimerMS();
865 mplayer_put_key(vo->key_fifo,
866 (MOUSE_BTN0 + Event.xbutton.button - 1)
867 | MP_KEY_DOWN);
868 break;
869 case ButtonRelease:
870 if (opts->cursor_autohide_delay > -2) {
871 vo_showcursor(display, x11->window);
872 x11->mouse_waiting_hide = 1;
873 x11->mouse_timer = GetTimerMS();
875 mplayer_put_key(vo->key_fifo,
876 MOUSE_BTN0 + Event.xbutton.button - 1);
877 break;
878 case PropertyNotify:
880 char *name =
881 XGetAtomName(display, Event.xproperty.atom);
883 if (!name)
884 break;
886 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
888 XFree(name);
890 break;
891 case MapNotify:
892 x11->vo_hint.win_gravity = x11->old_gravity;
893 XSetWMNormalHints(display, x11->window, &x11->vo_hint);
894 x11->fs_flip = 0;
895 break;
896 case DestroyNotify:
897 mp_msg(MSGT_VO, MSGL_WARN, "Our window was destroyed, exiting\n");
898 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
899 break;
900 case ClientMessage:
901 if (Event.xclient.message_type == x11->XAWM_PROTOCOLS &&
902 Event.xclient.data.l[0] == x11->XAWM_DELETE_WINDOW)
903 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
904 break;
907 return ret;
911 * \brief sets the size and position of the non-fullscreen window.
913 static void vo_x11_nofs_sizepos(struct vo *vo, int x, int y,
914 int width, int height)
916 struct vo_x11_state *x11 = vo->x11;
917 if (width == x11->last_video_width && height == x11->last_video_height) {
918 if (!vo->opts->force_window_position && !x11->size_changed_during_fs)
919 return;
920 } else if (vo_fs)
921 x11->size_changed_during_fs = true;
922 x11->last_video_height = height;
923 x11->last_video_width = width;
924 vo_x11_sizehint(vo, x, y, width, height, 0);
925 if (vo_fs) {
926 x11->vo_old_x = x;
927 x11->vo_old_y = y;
928 x11->vo_old_width = width;
929 x11->vo_old_height = height;
931 else
933 vo->dwidth = width;
934 vo->dheight = height;
935 if (vo->opts->force_window_position)
936 XMoveResizeWindow(vo->x11->display, vo->x11->window, x, y, width,
937 height);
938 else
939 XResizeWindow(vo->x11->display, vo->x11->window, width, height);
943 void vo_x11_sizehint(struct vo *vo, int x, int y, int width, int height, int max)
945 struct vo_x11_state *x11 = vo->x11;
946 x11->vo_hint.flags = 0;
947 if (vo_keepaspect)
949 x11->vo_hint.flags |= PAspect;
950 x11->vo_hint.min_aspect.x = width;
951 x11->vo_hint.min_aspect.y = height;
952 x11->vo_hint.max_aspect.x = width;
953 x11->vo_hint.max_aspect.y = height;
956 x11->vo_hint.flags |= PPosition | PSize;
957 x11->vo_hint.x = x;
958 x11->vo_hint.y = y;
959 x11->vo_hint.width = width;
960 x11->vo_hint.height = height;
961 if (max)
963 x11->vo_hint.flags |= PMaxSize;
964 x11->vo_hint.max_width = width;
965 x11->vo_hint.max_height = height;
966 } else
968 x11->vo_hint.max_width = 0;
969 x11->vo_hint.max_height = 0;
972 // Set minimum height/width to 4 to avoid off-by-one errors
973 // and because mga_vid requires a minimal size of 4 pixels.
974 x11->vo_hint.flags |= PMinSize;
975 x11->vo_hint.min_width = x11->vo_hint.min_height = 4;
977 // Set the base size. A window manager might display the window
978 // size to the user relative to this.
979 // Setting these to width/height might be nice, but e.g. fluxbox can't handle it.
980 x11->vo_hint.flags |= PBaseSize;
981 x11->vo_hint.base_width = 0 /*width*/;
982 x11->vo_hint.base_height = 0 /*height*/;
984 x11->vo_hint.flags |= PWinGravity;
985 x11->vo_hint.win_gravity = StaticGravity;
986 XSetWMNormalHints(x11->display, x11->window, &x11->vo_hint);
989 static int vo_x11_get_gnome_layer(struct vo_x11_state *x11, Window win)
991 Atom type;
992 int format;
993 unsigned long nitems;
994 unsigned long bytesafter;
995 unsigned short *args = NULL;
997 if (XGetWindowProperty(x11->display, win, x11->XA_WIN_LAYER, 0, 16384,
998 False, AnyPropertyType, &type, &format, &nitems,
999 &bytesafter,
1000 (unsigned char **) &args) == Success
1001 && nitems > 0 && args)
1003 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
1004 *args);
1005 return *args;
1007 return WIN_LAYER_NORMAL;
1010 // set a X text property that expects a UTF8_STRING type
1011 static void vo_x11_set_property_utf8(struct vo *vo, Atom name, const char *t)
1013 struct vo_x11_state *x11 = vo->x11;
1015 XChangeProperty(x11->display, x11->window, name, x11->XAUTF8_STRING, 8,
1016 PropModeReplace, t, strlen(t));
1019 // set a X text property that expects a STRING or COMPOUND_TEXT type
1020 static void vo_x11_set_property_string(struct vo *vo, Atom name, const char *t)
1022 struct vo_x11_state *x11 = vo->x11;
1023 XTextProperty prop = {0};
1025 if (Xutf8TextListToTextProperty(x11->display, (char **)&t, 1,
1026 XStdICCTextStyle, &prop) == Success) {
1027 XSetTextProperty(x11->display, x11->window, &prop, name);
1028 } else {
1029 // Strictly speaking this violates the ICCCM, but there's no way we
1030 // can do this correctly.
1031 vo_x11_set_property_utf8(vo, name, t);
1034 if (prop.value)
1035 XFree(prop.value);
1038 static void vo_x11_update_window_title(struct vo *vo)
1040 struct vo_x11_state *x11 = vo->x11;
1042 const char *title = vo_get_window_title(vo);
1043 vo_x11_set_property_string(vo, XA_WM_NAME, title);
1044 vo_x11_set_property_string(vo, XA_WM_ICON_NAME, title);
1045 vo_x11_set_property_utf8(vo, x11->XA_NET_WM_NAME, title);
1046 vo_x11_set_property_utf8(vo, x11->XA_NET_WM_ICON_NAME, title);
1050 static Window vo_x11_create_smooth_window(struct vo_x11_state *x11, Window mRoot,
1051 Visual * vis, int x, int y,
1052 unsigned int width, unsigned int height,
1053 int depth, Colormap col_map)
1055 unsigned long xswamask = CWBorderPixel;
1056 XSetWindowAttributes xswa;
1057 Window ret_win;
1059 if (col_map != CopyFromParent)
1061 xswa.colormap = col_map;
1062 xswamask |= CWColormap;
1064 xswa.background_pixel = 0;
1065 xswa.border_pixel = 0;
1066 xswa.backing_store = NotUseful;
1067 xswa.bit_gravity = StaticGravity;
1069 ret_win =
1070 XCreateWindow(x11->display, x11->rootwin, x, y, width, height, 0, depth,
1071 CopyFromParent, vis, xswamask, &xswa);
1072 XSetWMProtocols(x11->display, ret_win, &x11->XAWM_DELETE_WINDOW, 1);
1073 if (x11->f_gc == None)
1074 x11->f_gc = XCreateGC(x11->display, ret_win, 0, 0);
1075 XSetForeground(x11->display, x11->f_gc, 0);
1077 return ret_win;
1081 * \brief create and setup a window suitable for display
1082 * \param vis Visual to use for creating the window
1083 * \param x x position of window
1084 * \param y y position of window
1085 * \param width width of window
1086 * \param height height of window
1087 * \param flags flags for window creation.
1088 * Only VOFLAG_FULLSCREEN is supported so far.
1089 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1090 * \param classname name to use for the classhint
1092 * This also does the grunt-work like setting Window Manager hints etc.
1093 * If vo_window is already set it just moves and resizes it.
1095 void vo_x11_create_vo_window(struct vo *vo, XVisualInfo *vis, int x, int y,
1096 unsigned int width, unsigned int height, int flags,
1097 Colormap col_map, const char *classname)
1099 struct MPOpts *opts = vo->opts;
1100 struct vo_x11_state *x11 = vo->x11;
1101 Display *mDisplay = vo->x11->display;
1102 if (WinID >= 0) {
1103 vo_fs = flags & VOFLAG_FULLSCREEN;
1104 x11->window = WinID ? (Window)WinID : x11->rootwin;
1105 if (col_map != CopyFromParent) {
1106 unsigned long xswamask = CWColormap;
1107 XSetWindowAttributes xswa;
1108 xswa.colormap = col_map;
1109 XChangeWindowAttributes(mDisplay, x11->window, xswamask, &xswa);
1110 XInstallColormap(mDisplay, col_map);
1112 if (WinID) {
1113 // Expose events can only really be handled by us, so request them.
1114 vo_x11_selectinput_witherr(mDisplay, x11->window, ExposureMask);
1115 } else
1116 // Do not capture events since it might break the parent application
1117 // if it relies on events being forwarded to the parent of WinID.
1118 // It also is consistent with the w32_common.c code.
1119 vo_x11_selectinput_witherr(mDisplay, x11->window,
1120 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1121 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1123 vo_x11_update_geometry(vo, true);
1124 goto final;
1126 if (x11->window == None) {
1127 vo_fs = 0;
1128 vo->dwidth = width;
1129 vo->dheight = height;
1130 x11->window = vo_x11_create_smooth_window(x11, x11->rootwin, vis->visual,
1131 x, y, width, height, vis->depth, col_map);
1132 x11->window_state = VOFLAG_HIDDEN;
1134 if (flags & VOFLAG_HIDDEN)
1135 goto final;
1136 if (x11->window_state & VOFLAG_HIDDEN) {
1137 XSizeHints hint;
1138 x11->window_state &= ~VOFLAG_HIDDEN;
1139 vo_x11_classhint(vo, x11->window, classname);
1140 vo_hidecursor(mDisplay, x11->window);
1141 XSelectInput(mDisplay, x11->window, StructureNotifyMask);
1142 hint.x = x; hint.y = y;
1143 hint.width = width; hint.height = height;
1144 hint.flags = PSize;
1145 if (geometry_xy_changed)
1146 hint.flags |= PPosition;
1147 XSetWMNormalHints(mDisplay, x11->window, &hint);
1148 if (!vo_border) vo_x11_decoration(vo, 0);
1149 // map window
1150 x11->xic = XCreateIC(x11->xim,
1151 XNInputStyle, XIMPreeditNone | XIMStatusNone,
1152 XNClientWindow, x11->window,
1153 XNFocusWindow, x11->window,
1154 NULL);
1155 XSelectInput(mDisplay, x11->window, NoEventMask);
1156 vo_x11_selectinput_witherr(mDisplay, x11->window,
1157 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1158 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1159 XMapWindow(mDisplay, x11->window);
1160 vo_x11_clearwindow(vo, x11->window);
1162 vo_x11_update_window_title(vo);
1163 if (opts->vo_ontop) vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1164 vo_x11_update_geometry(vo, !geometry_xy_changed);
1165 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, width, height);
1166 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1167 vo_x11_fullscreen(vo);
1168 else if (vo_fs) {
1169 // if we are already in fullscreen do not switch back and forth, just
1170 // set the size values right.
1171 vo->dwidth = vo->opts->vo_screenwidth;
1172 vo->dheight = vo->opts->vo_screenheight;
1174 final:
1175 if (x11->vo_gc != None)
1176 XFreeGC(mDisplay, x11->vo_gc);
1177 x11->vo_gc = XCreateGC(mDisplay, x11->window, 0, NULL);
1179 XSync(mDisplay, False);
1180 vo->event_fd = ConnectionNumber(x11->display);
1183 void vo_x11_clearwindow_part(struct vo *vo, Window vo_window,
1184 int img_width, int img_height)
1186 struct vo_x11_state *x11 = vo->x11;
1187 Display *mDisplay = vo->x11->display;
1188 int u_dheight, u_dwidth, left_ov, left_ov2;
1190 if (x11->f_gc == None)
1191 return;
1193 u_dheight = vo->dheight;
1194 u_dwidth = vo->dwidth;
1195 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1196 return;
1198 left_ov = (u_dheight - img_height) / 2;
1199 left_ov2 = (u_dwidth - img_width) / 2;
1201 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, 0, u_dwidth, left_ov);
1202 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, u_dheight - left_ov - 1,
1203 u_dwidth, left_ov + 1);
1205 if (u_dwidth > img_width)
1207 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, left_ov, left_ov2,
1208 img_height);
1209 XFillRectangle(mDisplay, vo_window, x11->f_gc, u_dwidth - left_ov2 - 1,
1210 left_ov, left_ov2 + 1, img_height);
1213 XFlush(mDisplay);
1216 void vo_x11_clearwindow(struct vo *vo, Window vo_window)
1218 struct vo_x11_state *x11 = vo->x11;
1219 struct MPOpts *opts = vo->opts;
1220 if (x11->f_gc == None)
1221 return;
1222 XFillRectangle(x11->display, vo_window, x11->f_gc, 0, 0,
1223 opts->vo_screenwidth, opts->vo_screenheight);
1225 XFlush(x11->display);
1229 void vo_x11_setlayer(struct vo *vo, Window vo_window, int layer)
1231 struct vo_x11_state *x11 = vo->x11;
1232 if (WinID >= 0)
1233 return;
1235 if (x11->fs_type & vo_wm_LAYER)
1237 XClientMessageEvent xev;
1239 if (!x11->orig_layer)
1240 x11->orig_layer = vo_x11_get_gnome_layer(x11, vo_window);
1242 memset(&xev, 0, sizeof(xev));
1243 xev.type = ClientMessage;
1244 xev.display = x11->display;
1245 xev.window = vo_window;
1246 xev.message_type = x11->XA_WIN_LAYER;
1247 xev.format = 32;
1248 xev.data.l[0] = layer ? fs_layer : x11->orig_layer; // if not fullscreen, stay on default layer
1249 xev.data.l[1] = CurrentTime;
1250 mp_msg(MSGT_VO, MSGL_V,
1251 "[x11] Layered style stay on top (layer %ld).\n",
1252 xev.data.l[0]);
1253 XSendEvent(x11->display, x11->rootwin, False, SubstructureNotifyMask,
1254 (XEvent *) & xev);
1255 } else if (x11->fs_type & vo_wm_NETWM)
1257 XClientMessageEvent xev;
1258 char *state;
1260 memset(&xev, 0, sizeof(xev));
1261 xev.type = ClientMessage;
1262 xev.message_type = x11->XA_NET_WM_STATE;
1263 xev.display = x11->display;
1264 xev.window = vo_window;
1265 xev.format = 32;
1266 xev.data.l[0] = layer;
1268 if (x11->fs_type & vo_wm_STAYS_ON_TOP)
1269 xev.data.l[1] = x11->XA_NET_WM_STATE_STAYS_ON_TOP;
1270 else if (x11->fs_type & vo_wm_ABOVE)
1271 xev.data.l[1] = x11->XA_NET_WM_STATE_ABOVE;
1272 else if (x11->fs_type & vo_wm_FULLSCREEN)
1273 xev.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
1274 else if (x11->fs_type & vo_wm_BELOW)
1275 // This is not fallback. We can safely assume that the situation
1276 // where only NETWM_STATE_BELOW is supported doesn't exist.
1277 xev.data.l[1] = x11->XA_NET_WM_STATE_BELOW;
1279 XSendEvent(x11->display, x11->rootwin, False, SubstructureRedirectMask,
1280 (XEvent *) & xev);
1281 state = XGetAtomName(x11->display, xev.data.l[1]);
1282 mp_msg(MSGT_VO, MSGL_V,
1283 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1284 layer, state);
1285 XFree(state);
1289 static int vo_x11_get_fs_type(int supported)
1291 int i;
1292 int type = supported;
1294 if (vo_fstype_list)
1296 for (i = 0; vo_fstype_list[i]; i++)
1298 int neg = 0;
1299 char *arg = vo_fstype_list[i];
1301 if (vo_fstype_list[i][0] == '-')
1303 neg = 1;
1304 arg = vo_fstype_list[i] + 1;
1307 if (!strncmp(arg, "layer", 5))
1309 if (!neg && (arg[5] == '='))
1311 char *endptr = NULL;
1312 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1314 if (endptr && *endptr == '\0' && layer >= 0
1315 && layer <= 15)
1316 fs_layer = layer;
1318 if (neg)
1319 type &= ~vo_wm_LAYER;
1320 else
1321 type |= vo_wm_LAYER;
1322 } else if (!strcmp(arg, "above"))
1324 if (neg)
1325 type &= ~vo_wm_ABOVE;
1326 else
1327 type |= vo_wm_ABOVE;
1328 } else if (!strcmp(arg, "fullscreen"))
1330 if (neg)
1331 type &= ~vo_wm_FULLSCREEN;
1332 else
1333 type |= vo_wm_FULLSCREEN;
1334 } else if (!strcmp(arg, "stays_on_top"))
1336 if (neg)
1337 type &= ~vo_wm_STAYS_ON_TOP;
1338 else
1339 type |= vo_wm_STAYS_ON_TOP;
1340 } else if (!strcmp(arg, "below"))
1342 if (neg)
1343 type &= ~vo_wm_BELOW;
1344 else
1345 type |= vo_wm_BELOW;
1346 } else if (!strcmp(arg, "netwm"))
1348 if (neg)
1349 type &= ~vo_wm_NETWM;
1350 else
1351 type |= vo_wm_NETWM;
1352 } else if (!strcmp(arg, "none"))
1353 type = 0; // clear; keep parsing
1357 return type;
1361 * \brief update vo->dx, vo->dy, vo->dwidth and vo->dheight with current values of vo->x11->window
1362 * \return returns current color depth of vo->x11->window
1364 int vo_x11_update_geometry(struct vo *vo, bool update_pos)
1366 struct vo_x11_state *x11 = vo->x11;
1367 unsigned depth, w, h;
1368 int dummy_int;
1369 Window dummy_win;
1370 XGetGeometry(x11->display, x11->window, &dummy_win, &dummy_int, &dummy_int,
1371 &w, &h, &dummy_int, &depth);
1372 if (w <= INT_MAX && h <= INT_MAX) {
1373 vo->dwidth = w;
1374 vo->dheight = h;
1376 if (update_pos)
1377 XTranslateCoordinates(x11->display, x11->window, x11->rootwin, 0, 0,
1378 &vo->dx, &vo->dy, &dummy_win);
1380 return depth <= INT_MAX ? depth : 0;
1383 void vo_x11_fullscreen(struct vo *vo)
1385 struct MPOpts *opts = vo->opts;
1386 struct vo_x11_state *x11 = vo->x11;
1387 int x, y, w, h;
1388 x = x11->vo_old_x;
1389 y = x11->vo_old_y;
1390 w = x11->vo_old_width;
1391 h = x11->vo_old_height;
1393 if (WinID >= 0) {
1394 vo_fs = !vo_fs;
1395 return;
1397 if (x11->fs_flip)
1398 return;
1400 if (vo_fs)
1402 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1403 vo_fs = VO_FALSE;
1404 if (x11->size_changed_during_fs && (x11->fs_type & vo_wm_FULLSCREEN))
1405 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, x11->last_video_width,
1406 x11->last_video_height);
1407 x11->size_changed_during_fs = false;
1408 } else
1410 // win->fs
1411 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1413 vo_fs = VO_TRUE;
1414 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1416 x11->vo_old_x = vo->dx;
1417 x11->vo_old_y = vo->dy;
1418 x11->vo_old_width = vo->dwidth;
1419 x11->vo_old_height = vo->dheight;
1421 update_xinerama_info(vo);
1422 x = xinerama_x;
1423 y = xinerama_y;
1424 w = opts->vo_screenwidth;
1425 h = opts->vo_screenheight;
1428 long dummy;
1430 XGetWMNormalHints(x11->display, x11->window, &x11->vo_hint, &dummy);
1431 if (!(x11->vo_hint.flags & PWinGravity))
1432 x11->old_gravity = NorthWestGravity;
1433 else
1434 x11->old_gravity = x11->vo_hint.win_gravity;
1436 if (x11->wm_type == 0 && !(vo_fsmode & 16))
1438 XUnmapWindow(x11->display, x11->window); // required for MWM
1439 XWithdrawWindow(x11->display, x11->window, x11->screen);
1440 x11->fs_flip = 1;
1443 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1445 vo_x11_decoration(vo, vo_border && !vo_fs);
1446 vo_x11_sizehint(vo, x, y, w, h, 0);
1447 vo_x11_setlayer(vo, x11->window, vo_fs);
1450 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1452 /* some WMs lose ontop after fullscreen */
1453 if ((!(vo_fs)) & opts->vo_ontop)
1454 vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1456 XMapRaised(x11->display, x11->window);
1457 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1458 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1459 XRaiseWindow(x11->display, x11->window);
1460 XFlush(x11->display);
1463 void vo_x11_ontop(struct vo *vo)
1465 struct MPOpts *opts = vo->opts;
1466 opts->vo_ontop = !opts->vo_ontop;
1468 vo_x11_setlayer(vo, vo->x11->window, opts->vo_ontop);
1471 void vo_x11_border(struct vo *vo)
1473 vo_border = !vo_border;
1474 vo_x11_decoration(vo, vo_border && !vo_fs);
1478 * XScreensaver stuff
1481 static int screensaver_off;
1482 static unsigned int time_last;
1484 void xscreensaver_heartbeat(struct vo_x11_state *x11)
1486 unsigned int time = GetTimerMS();
1488 if (x11->display && screensaver_off && (time - time_last) > 30000)
1490 time_last = time;
1492 XResetScreenSaver(x11->display);
1496 static int xss_suspend(Display *mDisplay, Bool suspend)
1498 #ifndef CONFIG_XSS
1499 return 0;
1500 #else
1501 int event, error, major, minor;
1502 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1503 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1504 return 0;
1505 if (major < 1 || (major == 1 && minor < 1))
1506 return 0;
1507 XScreenSaverSuspend(mDisplay, suspend);
1508 return 1;
1509 #endif
1513 * End of XScreensaver stuff
1516 static void saver_on(Display * mDisplay)
1519 if (!screensaver_off)
1520 return;
1521 screensaver_off = 0;
1522 if (xss_suspend(mDisplay, False))
1523 return;
1524 #ifdef CONFIG_XDPMS
1525 if (dpms_disabled)
1527 int nothing;
1528 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1530 if (!DPMSEnable(mDisplay))
1531 { // restoring power saving settings
1532 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1533 } else
1535 // DPMS does not seem to be enabled unless we call DPMSInfo
1536 BOOL onoff;
1537 CARD16 state;
1539 DPMSForceLevel(mDisplay, DPMSModeOn);
1540 DPMSInfo(mDisplay, &state, &onoff);
1541 if (onoff)
1543 mp_msg(MSGT_VO, MSGL_V,
1544 "Successfully enabled DPMS\n");
1545 } else
1547 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1551 dpms_disabled = 0;
1553 #endif
1556 static void saver_off(Display * mDisplay)
1558 int nothing;
1560 if (screensaver_off)
1561 return;
1562 screensaver_off = 1;
1563 if (xss_suspend(mDisplay, True))
1564 return;
1565 #ifdef CONFIG_XDPMS
1566 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1568 BOOL onoff;
1569 CARD16 state;
1571 DPMSInfo(mDisplay, &state, &onoff);
1572 if (onoff)
1574 Status stat;
1576 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1577 dpms_disabled = 1;
1578 stat = DPMSDisable(mDisplay); // monitor powersave off
1579 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1582 #endif
1585 static XErrorHandler old_handler = NULL;
1586 static int selectinput_err = 0;
1587 static int x11_selectinput_errorhandler(Display * display,
1588 XErrorEvent * event)
1590 if (event->error_code == BadAccess)
1592 selectinput_err = 1;
1593 mp_msg(MSGT_VO, MSGL_ERR,
1594 "X11 error: BadAccess during XSelectInput Call\n");
1595 mp_msg(MSGT_VO, MSGL_ERR,
1596 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1597 /* If you think MPlayer should shutdown with this error,
1598 * comment out the following line */
1599 return 0;
1601 if (old_handler != NULL)
1602 old_handler(display, event);
1603 else
1604 x11_errorhandler(display, event);
1605 return 0;
1608 void vo_x11_selectinput_witherr(Display * display, Window w,
1609 long event_mask)
1611 XSync(display, False);
1612 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1613 selectinput_err = 0;
1614 if (vo_nomouse_input)
1616 XSelectInput(display, w,
1617 event_mask &
1618 (~(ButtonPressMask | ButtonReleaseMask)));
1619 } else
1621 XSelectInput(display, w, event_mask);
1623 XSync(display, False);
1624 XSetErrorHandler(old_handler);
1625 if (selectinput_err)
1627 mp_msg(MSGT_VO, MSGL_ERR,
1628 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1629 XSelectInput(display, w,
1630 event_mask &
1632 (ButtonPressMask | ButtonReleaseMask |
1633 PointerMotionMask)));
1637 #ifdef CONFIG_XF86VM
1638 void vo_vm_switch(struct vo *vo)
1640 struct vo_x11_state *x11 = vo->x11;
1641 struct MPOpts *opts = vo->opts;
1642 Display *mDisplay = x11->display;
1643 int vm_event, vm_error;
1644 int vm_ver, vm_rev;
1645 int i, j, have_vm = 0;
1646 int X = vo->dwidth, Y = vo->dheight;
1647 int modeline_width, modeline_height;
1649 int modecount;
1651 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1653 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1654 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1655 vm_rev);
1656 have_vm = 1;
1657 } else {
1658 mp_msg(MSGT_VO, MSGL_WARN,
1659 "XF86VidMode extension not available.\n");
1662 if (have_vm)
1664 if (vidmodes == NULL)
1665 XF86VidModeGetAllModeLines(mDisplay, x11->screen, &modecount,
1666 &vidmodes);
1667 j = 0;
1668 modeline_width = vidmodes[0]->hdisplay;
1669 modeline_height = vidmodes[0]->vdisplay;
1671 for (i = 1; i < modecount; i++)
1672 if ((vidmodes[i]->hdisplay >= X)
1673 && (vidmodes[i]->vdisplay >= Y))
1674 if ((vidmodes[i]->hdisplay <= modeline_width)
1675 && (vidmodes[i]->vdisplay <= modeline_height))
1677 modeline_width = vidmodes[i]->hdisplay;
1678 modeline_height = vidmodes[i]->vdisplay;
1679 j = i;
1682 mp_tmsg(MSGT_VO, MSGL_INFO, "XF86VM: Selected video mode %dx%d for image size %dx%d.\n",
1683 modeline_width, modeline_height, X, Y);
1684 XF86VidModeLockModeSwitch(mDisplay, x11->screen, 0);
1685 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1686 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1688 // FIXME: all this is more of a hack than proper solution
1689 X = (opts->vo_screenwidth - modeline_width) / 2;
1690 Y = (opts->vo_screenheight - modeline_height) / 2;
1691 XF86VidModeSetViewPort(mDisplay, x11->screen, X, Y);
1692 vo->dx = X;
1693 vo->dy = Y;
1694 vo->dwidth = modeline_width;
1695 vo->dheight = modeline_height;
1696 aspect_save_screenres(vo, modeline_width, modeline_height);
1700 void vo_vm_close(struct vo *vo)
1702 Display *dpy = vo->x11->display;
1703 struct MPOpts *opts = vo->opts;
1704 if (vidmodes != NULL)
1706 int i, modecount;
1708 free(vidmodes);
1709 vidmodes = NULL;
1710 XF86VidModeGetAllModeLines(dpy, vo->x11->screen, &modecount,
1711 &vidmodes);
1712 for (i = 0; i < modecount; i++)
1713 if ((vidmodes[i]->hdisplay == opts->vo_screenwidth)
1714 && (vidmodes[i]->vdisplay == opts->vo_screenheight))
1716 mp_msg(MSGT_VO, MSGL_INFO,
1717 "Returning to original mode %dx%d\n",
1718 opts->vo_screenwidth, opts->vo_screenheight);
1719 break;
1722 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1723 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1724 free(vidmodes);
1725 vidmodes = NULL;
1729 double vo_vm_get_fps(struct vo *vo)
1731 struct vo_x11_state *x11 = vo->x11;
1732 int clock;
1733 XF86VidModeModeLine modeline;
1734 if (!XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline))
1735 return 0;
1736 if (modeline.privsize)
1737 XFree(modeline.private);
1738 return 1e3 * clock / modeline.htotal / modeline.vtotal;
1740 #endif
1742 #endif /* X11_FULLSCREEN */
1746 * Scan the available visuals on this Display/Screen. Try to find
1747 * the 'best' available TrueColor visual that has a decent color
1748 * depth (at least 15bit). If there are multiple visuals with depth
1749 * >= 15bit, we prefer visuals with a smaller color depth.
1751 int vo_find_depth_from_visuals(Display * dpy, int screen,
1752 Visual ** visual_return)
1754 XVisualInfo visual_tmpl;
1755 XVisualInfo *visuals;
1756 int nvisuals, i;
1757 int bestvisual = -1;
1758 int bestvisual_depth = -1;
1760 visual_tmpl.screen = screen;
1761 visual_tmpl.class = TrueColor;
1762 visuals = XGetVisualInfo(dpy,
1763 VisualScreenMask | VisualClassMask,
1764 &visual_tmpl, &nvisuals);
1765 if (visuals != NULL)
1767 for (i = 0; i < nvisuals; i++)
1769 mp_msg(MSGT_VO, MSGL_V,
1770 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1771 visuals[i].visualid, visuals[i].depth,
1772 visuals[i].red_mask, visuals[i].green_mask,
1773 visuals[i].blue_mask);
1775 * Save the visual index and its depth, if this is the first
1776 * truecolor visul, or a visual that is 'preferred' over the
1777 * previous 'best' visual.
1779 if (bestvisual_depth == -1
1780 || (visuals[i].depth >= 15
1781 && (visuals[i].depth < bestvisual_depth
1782 || bestvisual_depth < 15)))
1784 bestvisual = i;
1785 bestvisual_depth = visuals[i].depth;
1789 if (bestvisual != -1 && visual_return != NULL)
1790 *visual_return = visuals[bestvisual].visual;
1792 XFree(visuals);
1794 return bestvisual_depth;
1798 static Colormap cmap = None;
1799 static XColor cols[256];
1800 static int cm_size, red_mask, green_mask, blue_mask;
1803 Colormap vo_x11_create_colormap(struct vo *vo, XVisualInfo *vinfo)
1805 struct vo_x11_state *x11 = vo->x11;
1806 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1808 if (vinfo->class != DirectColor)
1809 return XCreateColormap(x11->display, x11->rootwin, vinfo->visual,
1810 AllocNone);
1812 /* can this function get called twice or more? */
1813 if (cmap)
1814 return cmap;
1815 cm_size = vinfo->colormap_size;
1816 red_mask = vinfo->red_mask;
1817 green_mask = vinfo->green_mask;
1818 blue_mask = vinfo->blue_mask;
1819 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1820 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1821 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1822 rvu = 65536ull * ru / (red_mask + ru);
1823 gvu = 65536ull * gu / (green_mask + gu);
1824 bvu = 65536ull * bu / (blue_mask + bu);
1825 r = g = b = 0;
1826 rv = gv = bv = 0;
1827 m = DoRed | DoGreen | DoBlue;
1828 for (k = 0; k < cm_size; k++)
1830 int t;
1832 cols[k].pixel = r | g | b;
1833 cols[k].red = rv;
1834 cols[k].green = gv;
1835 cols[k].blue = bv;
1836 cols[k].flags = m;
1837 t = (r + ru) & red_mask;
1838 if (t < r)
1839 m &= ~DoRed;
1840 r = t;
1841 t = (g + gu) & green_mask;
1842 if (t < g)
1843 m &= ~DoGreen;
1844 g = t;
1845 t = (b + bu) & blue_mask;
1846 if (t < b)
1847 m &= ~DoBlue;
1848 b = t;
1849 rv += rvu;
1850 gv += gvu;
1851 bv += bvu;
1853 cmap = XCreateColormap(x11->display, x11->rootwin, vinfo->visual, AllocAll);
1854 XStoreColors(x11->display, cmap, cols, cm_size);
1855 return cmap;
1859 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1860 * hue and red/green/blue intensity, but we cannot do saturation.
1861 * Currently only gamma, brightness and contrast are implemented.
1862 * Is there sufficient interest for hue and/or red/green/blue intensity?
1864 /* these values have range [-100,100] and are initially 0 */
1865 static int vo_gamma = 0;
1866 static int vo_brightness = 0;
1867 static int vo_contrast = 0;
1869 static int transform_color(float val,
1870 float brightness, float contrast, float gamma) {
1871 float s = pow(val, gamma);
1872 s = (s - 0.5) * contrast + 0.5;
1873 s += brightness;
1874 if (s < 0)
1875 s = 0;
1876 if (s > 1)
1877 s = 1;
1878 return (unsigned short) (s * 65535);
1881 uint32_t vo_x11_set_equalizer(struct vo *vo, const char *name, int value)
1883 float gamma, brightness, contrast;
1884 float rf, gf, bf;
1885 int k;
1888 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1889 * of TrueColor-ed window but be careful:
1890 * Unlike the colormaps, which are private for the X client
1891 * who created them and thus automatically destroyed on client
1892 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1893 * setting and _must_ be restored before the process exits.
1894 * Unforunately when the process crashes (or gets killed
1895 * for some reason) it is impossible to restore the setting,
1896 * and such behaviour could be rather annoying for the users.
1898 if (cmap == None)
1899 return VO_NOTAVAIL;
1901 if (!strcasecmp(name, "brightness"))
1902 vo_brightness = value;
1903 else if (!strcasecmp(name, "contrast"))
1904 vo_contrast = value;
1905 else if (!strcasecmp(name, "gamma"))
1906 vo_gamma = value;
1907 else
1908 return VO_NOTIMPL;
1910 brightness = 0.01 * vo_brightness;
1911 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1912 gamma = pow(2, -0.02 * vo_gamma);
1914 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1915 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
1916 green_mask;
1917 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
1919 /* now recalculate the colormap using the newly set value */
1920 for (k = 0; k < cm_size; k++)
1922 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
1923 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
1924 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
1927 XStoreColors(vo->x11->display, cmap, cols, cm_size);
1928 XFlush(vo->x11->display);
1929 return VO_TRUE;
1932 uint32_t vo_x11_get_equalizer(const char *name, int *value)
1934 if (cmap == None)
1935 return VO_NOTAVAIL;
1936 if (!strcasecmp(name, "brightness"))
1937 *value = vo_brightness;
1938 else if (!strcasecmp(name, "contrast"))
1939 *value = vo_contrast;
1940 else if (!strcasecmp(name, "gamma"))
1941 *value = vo_gamma;
1942 else
1943 return VO_NOTIMPL;
1944 return VO_TRUE;
1947 bool vo_x11_screen_is_composited(struct vo *vo)
1949 struct vo_x11_state *x11 = vo->x11;
1950 return XGetSelectionOwner(x11->display, x11->XA_NET_WM_CM) != None;
1953 #ifdef CONFIG_XV
1954 int vo_xv_set_eq(struct vo *vo, uint32_t xv_port, const char *name, int value)
1956 XvAttribute *attributes;
1957 int i, howmany, xv_atom;
1959 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
1961 /* get available attributes */
1962 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1963 for (i = 0; i < howmany && attributes; i++)
1964 if (attributes[i].flags & XvSettable)
1966 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1967 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1968 then trigger it if it's ok so that the other values are at default upon query */
1969 if (xv_atom != None)
1971 int hue = 0, port_value, port_min, port_max;
1973 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1974 (!strcasecmp(name, "brightness")))
1975 port_value = value;
1976 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1977 (!strcasecmp(name, "contrast")))
1978 port_value = value;
1979 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1980 (!strcasecmp(name, "saturation")))
1981 port_value = value;
1982 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1983 (!strcasecmp(name, "hue")))
1985 port_value = value;
1986 hue = 1;
1987 } else
1988 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1989 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1990 (!strcasecmp(name, "red_intensity")))
1991 port_value = value;
1992 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1993 && (!strcasecmp(name, "green_intensity")))
1994 port_value = value;
1995 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1996 && (!strcasecmp(name, "blue_intensity")))
1997 port_value = value;
1998 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
1999 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
2000 && (!strcasecmp(name, "bt_709")))
2001 port_value = value;
2002 else
2003 continue;
2005 port_min = attributes[i].min_value;
2006 port_max = attributes[i].max_value;
2008 /* nvidia hue workaround */
2009 if (hue && port_min == 0 && port_max == 360)
2011 port_value =
2012 (port_value >=
2013 0) ? (port_value - 100) : (port_value + 100);
2015 // -100 -> min
2016 // 0 -> (max+min)/2
2017 // +100 -> max
2018 port_value =
2019 (port_value + 100) * (port_max - port_min) / 200 +
2020 port_min;
2021 XvSetPortAttribute(vo->x11->display, xv_port, xv_atom, port_value);
2022 return VO_TRUE;
2025 return VO_FALSE;
2028 int vo_xv_get_eq(struct vo *vo, uint32_t xv_port, const char *name, int *value)
2031 XvAttribute *attributes;
2032 int i, howmany, xv_atom;
2034 /* get available attributes */
2035 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
2036 for (i = 0; i < howmany && attributes; i++)
2037 if (attributes[i].flags & XvGettable)
2039 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
2040 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2041 then trigger it if it's ok so that the other values are at default upon query */
2042 if (xv_atom != None)
2044 int val, port_value = 0, port_min, port_max;
2046 XvGetPortAttribute(vo->x11->display, xv_port, xv_atom,
2047 &port_value);
2049 port_min = attributes[i].min_value;
2050 port_max = attributes[i].max_value;
2051 val =
2052 (port_value - port_min) * 200 / (port_max - port_min) -
2053 100;
2055 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2056 (!strcasecmp(name, "brightness")))
2057 *value = val;
2058 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2059 (!strcasecmp(name, "contrast")))
2060 *value = val;
2061 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2062 (!strcasecmp(name, "saturation")))
2063 *value = val;
2064 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2065 (!strcasecmp(name, "hue")))
2067 /* nasty nvidia detect */
2068 if (port_min == 0 && port_max == 360)
2069 *value = (val >= 0) ? (val - 100) : (val + 100);
2070 else
2071 *value = val;
2072 } else
2073 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2074 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2075 (!strcasecmp(name, "red_intensity")))
2076 *value = val;
2077 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2078 && (!strcasecmp(name, "green_intensity")))
2079 *value = val;
2080 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2081 && (!strcasecmp(name, "blue_intensity")))
2082 *value = val;
2083 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
2084 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
2085 && (!strcasecmp(name, "bt_709")))
2086 *value = val;
2087 else
2088 continue;
2090 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2091 name, *value);
2092 return VO_TRUE;
2095 return VO_FALSE;
2099 * \brief Interns the requested atom if it is available.
2101 * \param atom_name String containing the name of the requested atom.
2103 * \return Returns the atom if available, else None is returned.
2106 static Atom xv_intern_atom_if_exists(struct vo_x11_state *x11,
2107 char const *atom_name)
2109 XvAttribute * attributes;
2110 int attrib_count,i;
2111 Atom xv_atom = None;
2113 attributes = XvQueryPortAttributes(x11->display, x11->xv_port, &attrib_count );
2114 if( attributes!=NULL )
2116 for ( i = 0; i < attrib_count; ++i )
2118 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2120 xv_atom = XInternAtom(x11->display, atom_name, False );
2121 break; // found what we want, break out
2124 XFree( attributes );
2127 return xv_atom;
2131 * \brief Try to enable vsync for xv.
2132 * \return Returns -1 if not available, 0 on failure and 1 on success.
2134 int vo_xv_enable_vsync(struct vo *vo)
2136 struct vo_x11_state *x11 = vo->x11;
2137 Atom xv_atom = xv_intern_atom_if_exists(x11, "XV_SYNC_TO_VBLANK");
2138 if (xv_atom == None)
2139 return -1;
2140 return XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1) == Success;
2144 * \brief Get maximum supported source image dimensions.
2146 * This function does not set the variables pointed to by
2147 * width and height if the information could not be retrieved,
2148 * so the caller is reponsible for properly initializing them.
2150 * \param width [out] The maximum width gets stored here.
2151 * \param height [out] The maximum height gets stored here.
2154 void vo_xv_get_max_img_dim(struct vo *vo, uint32_t * width, uint32_t * height)
2156 struct vo_x11_state *x11 = vo->x11;
2157 XvEncodingInfo * encodings;
2158 //unsigned long num_encodings, idx; to int or too long?!
2159 unsigned int num_encodings, idx;
2161 XvQueryEncodings(x11->display, x11->xv_port, &num_encodings, &encodings);
2163 if ( encodings )
2165 for ( idx = 0; idx < num_encodings; ++idx )
2167 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2169 *width = encodings[idx].width;
2170 *height = encodings[idx].height;
2171 break;
2176 mp_msg( MSGT_VO, MSGL_V,
2177 "[xv common] Maximum source image dimensions: %ux%u\n",
2178 *width, *height );
2180 XvFreeEncodingInfo( encodings );
2184 * \brief Print information about the colorkey method and source.
2186 * \param ck_handling Integer value containing the information about
2187 * colorkey handling (see x11_common.h).
2189 * Outputs the content of |ck_handling| as a readable message.
2192 static void vo_xv_print_ck_info(struct vo_x11_state *x11)
2194 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2196 switch ( x11->xv_ck_info.method )
2198 case CK_METHOD_NONE:
2199 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2200 case CK_METHOD_AUTOPAINT:
2201 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2202 case CK_METHOD_MANUALFILL:
2203 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2204 case CK_METHOD_BACKGROUND:
2205 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2208 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2210 switch ( x11->xv_ck_info.source )
2212 case CK_SRC_CUR:
2213 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2214 x11->xv_colorkey );
2215 break;
2216 case CK_SRC_USE:
2217 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2219 mp_msg( MSGT_VO, MSGL_V,
2220 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2221 x11->xv_colorkey );
2223 else
2225 mp_msg( MSGT_VO, MSGL_V,
2226 "Using colorkey from MPlayer (0x%06lx)."
2227 " Use -colorkey to change.\n",
2228 x11->xv_colorkey );
2230 break;
2231 case CK_SRC_SET:
2232 mp_msg( MSGT_VO, MSGL_V,
2233 "Setting and using colorkey from MPlayer (0x%06lx)."
2234 " Use -colorkey to change.\n",
2235 x11->xv_colorkey );
2236 break;
2240 * \brief Init colorkey depending on the settings in xv_ck_info.
2242 * \return Returns 0 on failure and 1 on success.
2244 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2245 * flags in xv_ck_info.
2247 * Possiblilities:
2248 * * Methods
2249 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2250 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2251 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2252 * * Sources
2253 * - use currently set colorkey ( CK_SRC_CUR )
2254 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2255 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2257 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2258 * we don't draw anything as this means it was forced to off.
2260 int vo_xv_init_colorkey(struct vo *vo)
2262 struct vo_x11_state *x11 = vo->x11;
2263 Atom xv_atom;
2264 int rez;
2266 /* check if colorkeying is needed */
2267 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_COLORKEY");
2269 /* if we have to deal with colorkeying ... */
2270 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2272 /* check if we should use the colorkey specified in vo_colorkey */
2273 if ( x11->xv_ck_info.source != CK_SRC_CUR )
2275 x11->xv_colorkey = vo_colorkey;
2277 /* check if we have to set the colorkey too */
2278 if ( x11->xv_ck_info.source == CK_SRC_SET )
2280 xv_atom = XInternAtom(x11->display, "XV_COLORKEY",False);
2282 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, vo_colorkey);
2283 if ( rez != Success )
2285 mp_msg( MSGT_VO, MSGL_FATAL,
2286 "[xv common] Couldn't set colorkey!\n" );
2287 return 0; // error setting colorkey
2291 else
2293 int colorkey_ret;
2295 rez=XvGetPortAttribute(x11->display,x11->xv_port, xv_atom, &colorkey_ret);
2296 if ( rez == Success )
2298 x11->xv_colorkey = colorkey_ret;
2300 else
2302 mp_msg( MSGT_VO, MSGL_FATAL,
2303 "[xv common] Couldn't get colorkey!"
2304 "Maybe the selected Xv port has no overlay.\n" );
2305 return 0; // error getting colorkey
2309 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_AUTOPAINT_COLORKEY");
2311 /* should we draw the colorkey ourselves or activate autopainting? */
2312 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2314 rez = !Success; // reset rez to something different than Success
2316 if ( xv_atom != None ) // autopaint is supported
2318 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1);
2321 if ( rez != Success )
2323 // fallback to manual colorkey drawing
2324 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2327 else // disable colorkey autopainting if supported
2329 if ( xv_atom != None ) // we have autopaint attribute
2331 XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 0);
2335 else // do no colorkey drawing at all
2337 x11->xv_ck_info.method = CK_METHOD_NONE;
2338 } /* end: should we draw colorkey */
2340 /* output information about the current colorkey settings */
2341 vo_xv_print_ck_info(x11);
2343 return 1; // success
2347 * \brief Draw the colorkey on the video window.
2349 * Draws the colorkey depending on the set method ( colorkey_handling ).
2351 * Also draws the black bars ( when the video doesn't fit the display in
2352 * fullscreen ) separately, so they don't overlap with the video area.
2353 * It doesn't call XFlush.
2356 void vo_xv_draw_colorkey(struct vo *vo, int32_t x, int32_t y,
2357 int32_t w, int32_t h)
2359 struct vo_x11_state *x11 = vo->x11;
2360 if( x11->xv_ck_info.method == CK_METHOD_MANUALFILL ||
2361 x11->xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2363 XSetForeground(x11->display, x11->vo_gc, x11->xv_colorkey );
2364 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2365 x, y,
2366 w, h );
2370 /** \brief Tests if a valid argument for the ck suboption was given. */
2371 int xv_test_ck( void * arg )
2373 strarg_t * strarg = (strarg_t *)arg;
2375 if ( strargcmp( strarg, "use" ) == 0 ||
2376 strargcmp( strarg, "set" ) == 0 ||
2377 strargcmp( strarg, "cur" ) == 0 )
2379 return 1;
2382 return 0;
2384 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2385 int xv_test_ckm( void * arg )
2387 strarg_t * strarg = (strarg_t *)arg;
2389 if ( strargcmp( strarg, "bg" ) == 0 ||
2390 strargcmp( strarg, "man" ) == 0 ||
2391 strargcmp( strarg, "auto" ) == 0 )
2393 return 1;
2396 return 0;
2400 * \brief Modify the colorkey_handling var according to str
2402 * Checks if a valid pointer ( not NULL ) to the string
2403 * was given. And in that case modifies the colorkey_handling
2404 * var to reflect the requested behaviour.
2405 * If nothing happens the content of colorkey_handling stays
2406 * the same.
2408 * \param str Pointer to the string or NULL
2411 void xv_setup_colorkeyhandling(struct vo *vo, const char *ck_method_str,
2412 const char *ck_str)
2414 struct vo_x11_state *x11 = vo->x11;
2415 /* check if a valid pointer to the string was passed */
2416 if ( ck_str )
2418 if ( strncmp( ck_str, "use", 3 ) == 0 )
2420 x11->xv_ck_info.source = CK_SRC_USE;
2422 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2424 x11->xv_ck_info.source = CK_SRC_SET;
2427 /* check if a valid pointer to the string was passed */
2428 if ( ck_method_str )
2430 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2432 x11->xv_ck_info.method = CK_METHOD_BACKGROUND;
2434 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2436 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2438 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2440 x11->xv_ck_info.method = CK_METHOD_AUTOPAINT;
2445 #endif
2447 struct vo_x11_state *vo_x11_init_state(void)
2449 struct vo_x11_state *s = talloc_ptrtype(NULL, s);
2450 *s = (struct vo_x11_state){
2451 .xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR },
2452 .olddecor = MWM_DECOR_ALL,
2453 .oldfuncs = MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
2454 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE,
2455 .old_gravity = NorthWestGravity,
2457 return s;