VO: Don't force window position in X11 VOs
[mplayer.git] / libvo / x11_common.c
blob434cdbd04cd4f102c0fb3244c42d45c2f595f501
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <math.h>
22 #include <inttypes.h>
23 #include <limits.h>
25 #include "config.h"
26 #include "options.h"
27 #include "mp_msg.h"
28 #include "mp_fifo.h"
29 #include "libavutil/common.h"
30 #include "x11_common.h"
31 #include "talloc.h"
33 #ifdef X11_FULLSCREEN
35 #include <string.h>
36 #include <unistd.h>
37 #include <assert.h>
39 #include "video_out.h"
40 #include "aspect.h"
41 #include "geometry.h"
42 #include "help_mp.h"
43 #include "osdep/timer.h"
45 #include <X11/Xmd.h>
46 #include <X11/Xlib.h>
47 #include <X11/Xutil.h>
48 #include <X11/Xatom.h>
50 #ifdef CONFIG_XSS
51 #include <X11/extensions/scrnsaver.h>
52 #endif
54 #ifdef CONFIG_XDPMS
55 #include <X11/extensions/dpms.h>
56 #endif
58 #ifdef CONFIG_XINERAMA
59 #include <X11/extensions/Xinerama.h>
60 #endif
62 #ifdef CONFIG_XF86VM
63 #include <X11/extensions/xf86vmode.h>
64 #endif
66 #ifdef CONFIG_XF86XK
67 #include <X11/XF86keysym.h>
68 #endif
70 #ifdef CONFIG_XV
71 #include <X11/extensions/Xv.h>
72 #include <X11/extensions/Xvlib.h>
74 #include "subopt-helper.h"
75 #endif
77 #include "input/input.h"
78 #include "input/mouse.h"
80 #ifdef CONFIG_GUI
81 #include "gui/interface.h"
82 #include "mplayer.h"
83 #endif
85 #define WIN_LAYER_ONBOTTOM 2
86 #define WIN_LAYER_NORMAL 4
87 #define WIN_LAYER_ONTOP 6
88 #define WIN_LAYER_ABOVE_DOCK 10
90 extern int enable_mouse_movements;
91 int fs_layer = WIN_LAYER_ABOVE_DOCK;
93 int stop_xscreensaver = 0;
95 static int dpms_disabled = 0;
97 char *mDisplayName = NULL;
99 char **vo_fstype_list;
101 /* 1 means that the WM is metacity (broken as hell) */
102 int metacity_hack = 0;
104 #ifdef CONFIG_XF86VM
105 XF86VidModeModeInfo **vidmodes = NULL;
106 XF86VidModeModeLine modeline;
107 #endif
109 static int vo_x11_get_fs_type(int supported);
110 static void saver_off(Display *);
111 static void saver_on(Display *);
114 * Sends the EWMH fullscreen state event.
116 * action: could be one of _NET_WM_STATE_REMOVE -- remove state
117 * _NET_WM_STATE_ADD -- add state
118 * _NET_WM_STATE_TOGGLE -- toggle
120 void vo_x11_ewmh_fullscreen(struct vo_x11_state *x11, int action)
122 assert(action == _NET_WM_STATE_REMOVE ||
123 action == _NET_WM_STATE_ADD || action == _NET_WM_STATE_TOGGLE);
125 if (x11->fs_type & vo_wm_FULLSCREEN)
127 XEvent xev;
129 /* init X event structure for _NET_WM_FULLSCREEN client message */
130 xev.xclient.type = ClientMessage;
131 xev.xclient.serial = 0;
132 xev.xclient.send_event = True;
133 xev.xclient.message_type = x11->XA_NET_WM_STATE;
134 xev.xclient.window = x11->window;
135 xev.xclient.format = 32;
136 xev.xclient.data.l[0] = action;
137 xev.xclient.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
138 xev.xclient.data.l[2] = 0;
139 xev.xclient.data.l[3] = 0;
140 xev.xclient.data.l[4] = 0;
142 /* finally send that damn thing */
143 if (!XSendEvent(x11->display, DefaultRootWindow(x11->display), False,
144 SubstructureRedirectMask | SubstructureNotifyMask,
145 &xev))
147 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_EwmhFullscreenStateFailed);
152 static void vo_hidecursor(Display * disp, Window win)
154 Cursor no_ptr;
155 Pixmap bm_no;
156 XColor black, dummy;
157 Colormap colormap;
158 const char bm_no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
160 if (WinID == 0)
161 return; // do not hide if playing on the root window
163 colormap = DefaultColormap(disp, DefaultScreen(disp));
164 if ( !XAllocNamedColor(disp, colormap, "black", &black, &dummy) )
166 return; // color alloc failed, give up
168 bm_no = XCreateBitmapFromData(disp, win, bm_no_data, 8, 8);
169 no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
170 XDefineCursor(disp, win, no_ptr);
171 XFreeCursor(disp, no_ptr);
172 if (bm_no != None)
173 XFreePixmap(disp, bm_no);
174 XFreeColors(disp,colormap,&black.pixel,1,0);
177 static void vo_showcursor(Display * disp, Window win)
179 if (WinID == 0)
180 return;
181 XDefineCursor(disp, win, 0);
184 static int x11_errorhandler(Display * display, XErrorEvent * event)
186 #define MSGLEN 60
187 char msg[MSGLEN];
189 XGetErrorText(display, event->error_code, (char *) &msg, MSGLEN);
191 mp_msg(MSGT_VO, MSGL_ERR, "X11 error: %s\n", msg);
193 mp_msg(MSGT_VO, MSGL_V,
194 "Type: %x, display: %p, resourceid: %lx, serial: %lx\n",
195 event->type, event->display, event->resourceid, event->serial);
196 mp_msg(MSGT_VO, MSGL_V,
197 "Error code: %x, request code: %x, minor code: %x\n",
198 event->error_code, event->request_code, event->minor_code);
200 // abort();
201 //exit_player("X11 error");
202 return 0;
203 #undef MSGLEN
206 void fstype_help(void)
208 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_AvailableFsType);
209 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FULL_SCREEN_TYPES\n");
211 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "none",
212 "don't set fullscreen window layer");
213 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer",
214 "use _WIN_LAYER hint with default layer");
215 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer=<0..15>",
216 "use _WIN_LAYER hint with a given layer number");
217 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "netwm",
218 "force NETWM style");
219 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "above",
220 "use _NETWM_STATE_ABOVE hint if available");
221 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "below",
222 "use _NETWM_STATE_BELOW hint if available");
223 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "fullscreen",
224 "use _NETWM_STATE_FULLSCREEN hint if availale");
225 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "stays_on_top",
226 "use _NETWM_STATE_STAYS_ON_TOP hint if available");
227 mp_msg(MSGT_VO, MSGL_INFO,
228 "You can also negate the settings with simply putting '-' in the beginning");
229 mp_msg(MSGT_VO, MSGL_INFO, "\n");
232 static void fstype_dump(int fstype)
234 if (fstype)
236 mp_msg(MSGT_VO, MSGL_V, "[x11] Current fstype setting honours");
237 if (fstype & vo_wm_LAYER)
238 mp_msg(MSGT_VO, MSGL_V, " LAYER");
239 if (fstype & vo_wm_FULLSCREEN)
240 mp_msg(MSGT_VO, MSGL_V, " FULLSCREEN");
241 if (fstype & vo_wm_STAYS_ON_TOP)
242 mp_msg(MSGT_VO, MSGL_V, " STAYS_ON_TOP");
243 if (fstype & vo_wm_ABOVE)
244 mp_msg(MSGT_VO, MSGL_V, " ABOVE");
245 if (fstype & vo_wm_BELOW)
246 mp_msg(MSGT_VO, MSGL_V, " BELOW");
247 mp_msg(MSGT_VO, MSGL_V, " X atoms\n");
248 } else
249 mp_msg(MSGT_VO, MSGL_V,
250 "[x11] Current fstype setting doesn't honour any X atoms\n");
253 static int net_wm_support_state_test(struct vo_x11_state *x11, Atom atom)
255 #define NET_WM_STATE_TEST(x) { if (atom == x11->XA_NET_WM_STATE_##x) { mp_msg( MSGT_VO,MSGL_V, "[x11] Detected wm supports " #x " state.\n" ); return vo_wm_##x; } }
257 NET_WM_STATE_TEST(FULLSCREEN);
258 NET_WM_STATE_TEST(ABOVE);
259 NET_WM_STATE_TEST(STAYS_ON_TOP);
260 NET_WM_STATE_TEST(BELOW);
261 return 0;
264 static int x11_get_property(struct vo_x11_state *x11, Atom type, Atom ** args,
265 unsigned long *nitems)
267 int format;
268 unsigned long bytesafter;
270 return Success ==
271 XGetWindowProperty(x11->display, x11->rootwin, type, 0, 16384, False,
272 AnyPropertyType, &type, &format, nitems,
273 &bytesafter, (unsigned char **) args)
274 && *nitems > 0;
277 static int vo_wm_detect(struct vo *vo)
279 struct vo_x11_state *x11 = vo->x11;
280 int i;
281 int wm = 0;
282 unsigned long nitems;
283 Atom *args = NULL;
285 if (WinID >= 0)
286 return 0;
288 // -- supports layers
289 if (x11_get_property(x11, x11->XA_WIN_PROTOCOLS, &args, &nitems))
291 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports layers.\n");
292 for (i = 0; i < nitems; i++)
294 if (args[i] == x11->XA_WIN_LAYER)
296 wm |= vo_wm_LAYER;
297 metacity_hack |= 1;
298 } else
299 /* metacity is the only window manager I know which reports
300 * supporting only the _WIN_LAYER hint in _WIN_PROTOCOLS.
301 * (what's more support for it is broken) */
302 metacity_hack |= 2;
304 XFree(args);
305 if (wm && (metacity_hack == 1))
307 // metacity claims to support layers, but it is not the truth :-)
308 wm ^= vo_wm_LAYER;
309 mp_msg(MSGT_VO, MSGL_V,
310 "[x11] Using workaround for Metacity bugs.\n");
313 // --- netwm
314 if (x11_get_property(x11, x11->XA_NET_SUPPORTED, &args, &nitems))
316 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports NetWM.\n");
317 for (i = 0; i < nitems; i++)
318 wm |= net_wm_support_state_test(vo->x11, args[i]);
319 XFree(args);
322 if (wm == 0)
323 mp_msg(MSGT_VO, MSGL_V, "[x11] Unknown wm type...\n");
324 return wm;
327 #define XA_INIT(x) x11->XA##x = XInternAtom(x11->display, #x, False)
328 static void init_atoms(struct vo_x11_state *x11)
330 XA_INIT(_NET_SUPPORTED);
331 XA_INIT(_NET_WM_STATE);
332 XA_INIT(_NET_WM_STATE_FULLSCREEN);
333 XA_INIT(_NET_WM_STATE_ABOVE);
334 XA_INIT(_NET_WM_STATE_STAYS_ON_TOP);
335 XA_INIT(_NET_WM_STATE_BELOW);
336 XA_INIT(_NET_WM_PID);
337 XA_INIT(_WIN_PROTOCOLS);
338 XA_INIT(_WIN_LAYER);
339 XA_INIT(_WIN_HINTS);
340 XA_INIT(WM_PROTOCOLS);
341 XA_INIT(WM_DELETE_WINDOW);
344 void update_xinerama_info(struct vo *vo) {
345 struct MPOpts *opts = vo->opts;
346 int screen = xinerama_screen;
347 xinerama_x = xinerama_y = 0;
348 #ifdef CONFIG_XINERAMA
349 if (screen >= -1 && XineramaIsActive(vo->x11->display))
351 XineramaScreenInfo *screens;
352 int num_screens;
354 screens = XineramaQueryScreens(vo->x11->display, &num_screens);
355 if (screen >= num_screens)
356 screen = num_screens - 1;
357 if (screen == -1) {
358 int x = vo->dx + vo->dwidth / 2;
359 int y = vo->dy + vo->dheight / 2;
360 for (screen = num_screens - 1; screen > 0; screen--) {
361 int left = screens[screen].x_org;
362 int right = left + screens[screen].width;
363 int top = screens[screen].y_org;
364 int bottom = top + screens[screen].height;
365 if (left <= x && x <= right && top <= y && y <= bottom)
366 break;
369 if (screen < 0)
370 screen = 0;
371 opts->vo_screenwidth = screens[screen].width;
372 opts->vo_screenheight = screens[screen].height;
373 xinerama_x = screens[screen].x_org;
374 xinerama_y = screens[screen].y_org;
376 XFree(screens);
378 #endif
379 aspect_save_screenres(vo, opts->vo_screenwidth, opts->vo_screenheight);
382 int vo_init(struct vo *vo)
384 struct MPOpts *opts = vo->opts;
385 struct vo_x11_state *x11 = vo->x11;
386 // int mScreen;
387 int depth, bpp;
388 unsigned int mask;
390 // char * DisplayName = ":0.0";
391 // Display * mDisplay;
392 XImage *mXImage = NULL;
394 // Window mRootWin;
395 XWindowAttributes attribs;
396 char *dispName;
398 if (vo_rootwin)
399 WinID = 0; // use root window
401 if (x11->depthonscreen)
403 saver_off(x11->display);
404 return 1; // already called
407 XSetErrorHandler(x11_errorhandler);
409 #if 0
410 if (!mDisplayName)
411 if (!(mDisplayName = getenv("DISPLAY")))
412 mDisplayName = strdup(":0.0");
413 #else
414 dispName = XDisplayName(mDisplayName);
415 #endif
417 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
419 x11->display = XOpenDisplay(dispName);
420 if (!x11->display)
422 mp_msg(MSGT_VO, MSGL_ERR,
423 "vo: couldn't open the X11 display (%s)!\n", dispName);
424 return 0;
426 x11->screen = DefaultScreen(x11->display); // screen ID
427 x11->rootwin = RootWindow(x11->display, x11->screen); // root window ID
429 init_atoms(vo->x11);
431 #ifdef CONFIG_XF86VM
433 int clock;
435 XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline);
436 if (!opts->vo_screenwidth)
437 opts->vo_screenwidth = modeline.hdisplay;
438 if (!opts->vo_screenheight)
439 opts->vo_screenheight = modeline.vdisplay;
441 #endif
443 if (!opts->vo_screenwidth)
444 opts->vo_screenwidth = DisplayWidth(x11->display, x11->screen);
445 if (!opts->vo_screenheight)
446 opts->vo_screenheight = DisplayHeight(x11->display, x11->screen);
448 // get color depth (from root window, or the best visual):
449 XGetWindowAttributes(x11->display, x11->rootwin, &attribs);
450 depth = attribs.depth;
452 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
454 Visual *visual;
456 depth = vo_find_depth_from_visuals(x11->display, x11->screen, &visual);
457 if (depth != -1)
458 mXImage = XCreateImage(x11->display, visual, depth, ZPixmap,
459 0, NULL, 1, 1, 8, 1);
460 } else
461 mXImage =
462 XGetImage(x11->display, x11->rootwin, 0, 0, 1, 1, AllPlanes, ZPixmap);
464 x11->depthonscreen = depth; // display depth on screen
466 // get bits/pixel from XImage structure:
467 if (mXImage == NULL)
469 mask = 0;
470 } else
473 * for the depth==24 case, the XImage structures might use
474 * 24 or 32 bits of data per pixel. The x11->depthonscreen
475 * field stores the amount of data per pixel in the
476 * XImage structure!
478 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
480 bpp = mXImage->bits_per_pixel;
481 if ((x11->depthonscreen + 7) / 8 != (bpp + 7) / 8)
482 x11->depthonscreen = bpp; // by A'rpi
483 mask =
484 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
485 mp_msg(MSGT_VO, MSGL_V,
486 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
487 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
488 XDestroyImage(mXImage);
490 if (((x11->depthonscreen + 7) / 8) == 2)
492 if (mask == 0x7FFF)
493 x11->depthonscreen = 15;
494 else if (mask == 0xFFFF)
495 x11->depthonscreen = 16;
497 // XCloseDisplay( mDisplay );
498 /* slightly improved local display detection AST */
499 if (strncmp(dispName, "unix:", 5) == 0)
500 dispName += 4;
501 else if (strncmp(dispName, "localhost:", 10) == 0)
502 dispName += 9;
503 if (*dispName == ':' && atoi(dispName + 1) < 10)
504 x11->display_is_local = 1;
505 else
506 x11->display_is_local = 0;
507 mp_msg(MSGT_VO, MSGL_V,
508 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
509 opts->vo_screenwidth, opts->vo_screenheight, depth, x11->depthonscreen,
510 dispName, x11->display_is_local ? "local" : "remote");
512 x11->wm_type = vo_wm_detect(vo);
514 x11->fs_type = vo_x11_get_fs_type(x11->wm_type);
516 fstype_dump(x11->fs_type);
518 saver_off(x11->display);
519 return 1;
522 void vo_uninit(struct vo_x11_state *x11)
524 if (!x11->display)
526 mp_msg(MSGT_VO, MSGL_V,
527 "vo: x11 uninit called but X11 not initialized..\n");
528 return;
530 // if( !vo_depthonscreen ) return;
531 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
532 XSetErrorHandler(NULL);
533 XCloseDisplay(x11->display);
534 x11->depthonscreen = 0;
535 x11->display = NULL;
536 talloc_free(x11);
539 #include "osdep/keycodes.h"
540 #include "wskeys.h"
542 #ifdef XF86XK_AudioPause
543 static const struct keymap keysym_map[] = {
544 {XF86XK_MenuKB, KEY_MENU},
545 {XF86XK_AudioPlay, KEY_PLAY}, {XF86XK_AudioPause, KEY_PAUSE}, {XF86XK_AudioStop, KEY_STOP},
546 {XF86XK_AudioPrev, KEY_PREV}, {XF86XK_AudioNext, KEY_NEXT},
547 {XF86XK_AudioMute, KEY_MUTE}, {XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN}, {XF86XK_AudioRaiseVolume, KEY_VOLUME_UP},
548 {0, 0}
551 static void vo_x11_putkey_ext(struct vo *vo, int keysym)
553 struct mp_fifo *f = vo->key_fifo;
554 int mpkey = lookup_keymap_table(keysym_map, keysym);
555 if (mpkey)
556 mplayer_put_key(f, mpkey);
558 #endif
560 static const struct keymap keymap[] = {
561 // special keys
562 {wsEscape, KEY_ESC}, {wsBackSpace, KEY_BS}, {wsTab, KEY_TAB}, {wsEnter, KEY_ENTER},
564 // cursor keys
565 {wsLeft, KEY_LEFT}, {wsRight, KEY_RIGHT}, {wsUp, KEY_UP}, {wsDown, KEY_DOWN},
567 // navigation block
568 {wsInsert, KEY_INSERT}, {wsDelete, KEY_DELETE}, {wsHome, KEY_HOME}, {wsEnd, KEY_END},
569 {wsPageUp, KEY_PAGE_UP}, {wsPageDown, KEY_PAGE_DOWN},
571 // F-keys
572 {wsF1, KEY_F+1}, {wsF2, KEY_F+2}, {wsF3, KEY_F+3}, {wsF4, KEY_F+4},
573 {wsF5, KEY_F+5}, {wsF6, KEY_F+6}, {wsF7, KEY_F+7}, {wsF8, KEY_F+8},
574 {wsF9, KEY_F+9}, {wsF10, KEY_F+10}, {wsF11, KEY_F+11}, {wsF12, KEY_F+12},
576 // numpad independent of numlock
577 {wsGrayMinus, '-'}, {wsGrayPlus, '+'}, {wsGrayMul, '*'}, {wsGrayDiv, '/'},
578 {wsGrayEnter, KEY_KPENTER},
580 // numpad with numlock
581 {wsGray0, KEY_KP0}, {wsGray1, KEY_KP1}, {wsGray2, KEY_KP2},
582 {wsGray3, KEY_KP3}, {wsGray4, KEY_KP4}, {wsGray5, KEY_KP5},
583 {wsGray6, KEY_KP6}, {wsGray7, KEY_KP7}, {wsGray8, KEY_KP8},
584 {wsGray9, KEY_KP9}, {wsGrayDecimal, KEY_KPDEC},
586 // numpad without numlock
587 {wsGrayInsert, KEY_KPINS}, {wsGrayEnd, KEY_KP1}, {wsGrayDown, KEY_KP2},
588 {wsGrayPgDn, KEY_KP3}, {wsGrayLeft, KEY_KP4}, {wsGray5Dup, KEY_KP5},
589 {wsGrayRight, KEY_KP6}, {wsGrayHome, KEY_KP7}, {wsGrayUp, KEY_KP8},
590 {wsGrayPgUp, KEY_KP9}, {wsGrayDelete, KEY_KPDEL},
592 {0, 0}
595 void vo_x11_putkey(struct vo *vo, int key)
597 static const char *passthrough_keys = " -+*/<>`~!@#$%^&()_{}:;\"\',.?\\|=[]";
598 int mpkey = 0;
599 if ((key >= 'a' && key <= 'z') ||
600 (key >= 'A' && key <= 'Z') ||
601 (key >= '0' && key <= '9') ||
602 (key > 0 && key < 256 && strchr(passthrough_keys, key)))
603 mpkey = key;
605 if (!mpkey)
606 mpkey = lookup_keymap_table(keymap, key);
608 if (mpkey)
609 mplayer_put_key(vo->key_fifo, mpkey);
613 // ----- Motif header: -------
615 #define MWM_HINTS_FUNCTIONS (1L << 0)
616 #define MWM_HINTS_DECORATIONS (1L << 1)
617 #define MWM_HINTS_INPUT_MODE (1L << 2)
618 #define MWM_HINTS_STATUS (1L << 3)
620 #define MWM_FUNC_ALL (1L << 0)
621 #define MWM_FUNC_RESIZE (1L << 1)
622 #define MWM_FUNC_MOVE (1L << 2)
623 #define MWM_FUNC_MINIMIZE (1L << 3)
624 #define MWM_FUNC_MAXIMIZE (1L << 4)
625 #define MWM_FUNC_CLOSE (1L << 5)
627 #define MWM_DECOR_ALL (1L << 0)
628 #define MWM_DECOR_BORDER (1L << 1)
629 #define MWM_DECOR_RESIZEH (1L << 2)
630 #define MWM_DECOR_TITLE (1L << 3)
631 #define MWM_DECOR_MENU (1L << 4)
632 #define MWM_DECOR_MINIMIZE (1L << 5)
633 #define MWM_DECOR_MAXIMIZE (1L << 6)
635 #define MWM_INPUT_MODELESS 0
636 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
637 #define MWM_INPUT_SYSTEM_MODAL 2
638 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
639 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
641 #define MWM_TEAROFF_WINDOW (1L<<0)
643 typedef struct
645 long flags;
646 long functions;
647 long decorations;
648 long input_mode;
649 long state;
650 } MotifWmHints;
652 static MotifWmHints vo_MotifWmHints;
653 static Atom vo_MotifHints = None;
655 void vo_x11_decoration(struct vo *vo, int d)
657 struct vo_x11_state *x11 = vo->x11;
658 Atom mtype;
659 int mformat;
660 unsigned long mn, mb;
662 if (!WinID)
663 return;
665 if (vo_fsmode & 8)
667 XSetTransientForHint(x11->display, x11->window,
668 RootWindow(x11->display, x11->screen));
671 vo_MotifHints = XInternAtom(x11->display, "_MOTIF_WM_HINTS", 0);
672 if (vo_MotifHints != None)
674 if (!d)
676 MotifWmHints *mhints = NULL;
678 XGetWindowProperty(x11->display, x11->window,
679 vo_MotifHints, 0, 20, False,
680 vo_MotifHints, &mtype, &mformat, &mn,
681 &mb, (unsigned char **) &mhints);
682 if (mhints)
684 if (mhints->flags & MWM_HINTS_DECORATIONS)
685 x11->olddecor = mhints->decorations;
686 if (mhints->flags & MWM_HINTS_FUNCTIONS)
687 x11->oldfuncs = mhints->functions;
688 XFree(mhints);
692 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
693 vo_MotifWmHints.flags =
694 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
695 if (d)
697 vo_MotifWmHints.functions = x11->oldfuncs;
698 d = x11->olddecor;
700 #if 0
701 vo_MotifWmHints.decorations =
702 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
703 #else
704 vo_MotifWmHints.decorations =
705 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
706 #endif
707 XChangeProperty(x11->display, x11->window, vo_MotifHints,
708 vo_MotifHints, 32,
709 PropModeReplace,
710 (unsigned char *) &vo_MotifWmHints,
711 (vo_fsmode & 4) ? 4 : 5);
715 void vo_x11_classhint(struct vo *vo, Window window, char *name)
717 struct vo_x11_state *x11 = vo->x11;
718 XClassHint wmClass;
719 pid_t pid = getpid();
721 wmClass.res_name = name;
722 wmClass.res_class = "MPlayer";
723 XSetClassHint(x11->display, window, &wmClass);
724 XChangeProperty(x11->display, window, x11->XA_NET_WM_PID, XA_CARDINAL,
725 32, PropModeReplace, (unsigned char *) &pid, 1);
728 void vo_x11_uninit(struct vo *vo)
730 struct vo_x11_state *x11 = vo->x11;
731 saver_on(x11->display);
732 if (x11->window != None)
733 vo_showcursor(x11->display, x11->window);
735 if (x11->f_gc)
737 XFreeGC(vo->x11->display, x11->f_gc);
738 x11->f_gc = NULL;
740 #ifdef CONFIG_GUI
741 /* destroy window only if it's not controlled by the GUI */
742 if (!use_gui)
743 #endif
745 if (x11->vo_gc)
747 XSetBackground(vo->x11->display, x11->vo_gc, 0);
748 XFreeGC(vo->x11->display, x11->vo_gc);
749 x11->vo_gc = NULL;
751 if (x11->window != None)
753 XClearWindow(x11->display, x11->window);
754 if (WinID < 0)
756 XEvent xev;
758 XUnmapWindow(x11->display, x11->window);
759 XDestroyWindow(x11->display, x11->window);
762 XNextEvent(x11->display, &xev);
764 while (xev.type != DestroyNotify
765 || xev.xdestroywindow.event != x11->window);
767 x11->window = None;
769 vo_fs = 0;
770 x11->vo_old_width = x11->vo_old_height = 0;
774 int vo_x11_check_events(struct vo *vo)
776 struct vo_x11_state *x11 = vo->x11;
777 struct MPOpts *opts = vo->opts;
778 Display *display = vo->x11->display;
779 int ret = 0;
780 XEvent Event;
781 char buf[100];
782 KeySym keySym;
784 // unsigned long vo_KeyTable[512];
786 if ((x11->vo_mouse_autohide) && x11->mouse_waiting_hide &&
787 (GetTimerMS() - x11->mouse_timer >= 1000)) {
788 vo_hidecursor(display, x11->window);
789 x11->mouse_waiting_hide = 0;
792 while (XPending(display))
794 XNextEvent(display, &Event);
795 #ifdef CONFIG_GUI
796 if (use_gui)
798 guiGetEvent(0, (char *) &Event);
799 if (x11->window != Event.xany.window)
800 continue;
802 #endif
803 // printf("\rEvent.type=%X \n",Event.type);
804 switch (Event.type)
806 case Expose:
807 ret |= VO_EVENT_EXPOSE;
808 break;
809 case ConfigureNotify:
810 // if (!vo_fs && (Event.xconfigure.width == opts->vo_screenwidth || Event.xconfigure.height == opts->vo_screenheight)) break;
811 // if (vo_fs && Event.xconfigure.width != opts->vo_screenwidth && Event.xconfigure.height != opts->vo_screenheight) break;
812 if (x11->window == None)
813 break;
815 int old_w = vo->dwidth, old_h = vo->dheight;
816 vo_x11_update_geometry(vo);
817 if (vo->dwidth != old_w || vo->dheight != old_h)
818 ret |= VO_EVENT_RESIZE;
820 break;
821 case KeyPress:
823 int key;
825 #ifdef CONFIG_GUI
826 if ( use_gui ) { break; }
827 #endif
829 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
830 &x11->compose_status);
831 #ifdef XF86XK_AudioPause
832 vo_x11_putkey_ext(vo, keySym);
833 #endif
834 key =
835 ((keySym & 0xff00) !=
836 0 ? ((keySym & 0x00ff) + 256) : (keySym));
837 vo_x11_putkey(vo, key);
838 ret |= VO_EVENT_KEYPRESS;
840 break;
841 case MotionNotify:
842 if(enable_mouse_movements)
844 char cmd_str[40];
845 sprintf(cmd_str,"set_mouse_pos %i %i",Event.xmotion.x, Event.xmotion.y);
846 mp_input_queue_cmd(vo->input_ctx,
847 mp_input_parse_cmd(cmd_str));
850 if (x11->vo_mouse_autohide)
852 vo_showcursor(display, x11->window);
853 x11->mouse_waiting_hide = 1;
854 x11->mouse_timer = GetTimerMS();
856 break;
857 case ButtonPress:
858 if (x11->vo_mouse_autohide)
860 vo_showcursor(display, x11->window);
861 x11->mouse_waiting_hide = 1;
862 x11->mouse_timer = GetTimerMS();
864 #ifdef CONFIG_GUI
865 // Ignore mouse button 1-3 under GUI.
866 if (use_gui && (Event.xbutton.button >= 1)
867 && (Event.xbutton.button <= 3))
868 break;
869 #endif
870 mplayer_put_key(vo->key_fifo,
871 (MOUSE_BTN0 + Event.xbutton.button - 1)
872 | MP_KEY_DOWN);
873 break;
874 case ButtonRelease:
875 if (x11->vo_mouse_autohide)
877 vo_showcursor(display, x11->window);
878 x11->mouse_waiting_hide = 1;
879 x11->mouse_timer = GetTimerMS();
881 #ifdef CONFIG_GUI
882 // Ignore mouse button 1-3 under GUI.
883 if (use_gui && (Event.xbutton.button >= 1)
884 && (Event.xbutton.button <= 3))
885 break;
886 #endif
887 mplayer_put_key(vo->key_fifo,
888 MOUSE_BTN0 + Event.xbutton.button - 1);
889 break;
890 case PropertyNotify:
892 char *name =
893 XGetAtomName(display, Event.xproperty.atom);
895 if (!name)
896 break;
898 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
900 XFree(name);
902 break;
903 case MapNotify:
904 x11->vo_hint.win_gravity = x11->old_gravity;
905 XSetWMNormalHints(display, x11->window, &x11->vo_hint);
906 x11->fs_flip = 0;
907 break;
908 case ClientMessage:
909 if (Event.xclient.message_type == x11->XAWM_PROTOCOLS &&
910 Event.xclient.data.l[0] == x11->XAWM_DELETE_WINDOW)
911 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
912 break;
915 return ret;
919 * \brief sets the size and position of the non-fullscreen window.
921 static void vo_x11_nofs_sizepos(struct vo *vo, int x, int y,
922 int width, int height)
924 struct vo_x11_state *x11 = vo->x11;
925 vo_x11_sizehint(vo, x, y, width, height, 0);
926 if (vo_fs) {
927 x11->vo_old_x = x;
928 x11->vo_old_y = y;
929 x11->vo_old_width = width;
930 x11->vo_old_height = height;
932 else
934 vo->dwidth = width;
935 vo->dheight = height;
936 if (vo->opts->force_window_position)
937 XMoveResizeWindow(vo->x11->display, vo->x11->window, x, y, width,
938 height);
939 else
940 XResizeWindow(vo->x11->display, vo->x11->window, width, height);
944 void vo_x11_sizehint(struct vo *vo, int x, int y, int width, int height, int max)
946 struct vo_x11_state *x11 = vo->x11;
947 x11->vo_hint.flags = 0;
948 if (vo_keepaspect)
950 x11->vo_hint.flags |= PAspect;
951 x11->vo_hint.min_aspect.x = width;
952 x11->vo_hint.min_aspect.y = height;
953 x11->vo_hint.max_aspect.x = width;
954 x11->vo_hint.max_aspect.y = height;
957 x11->vo_hint.flags |= PPosition | PSize;
958 x11->vo_hint.x = x;
959 x11->vo_hint.y = y;
960 x11->vo_hint.width = width;
961 x11->vo_hint.height = height;
962 if (max)
964 x11->vo_hint.flags |= PMaxSize;
965 x11->vo_hint.max_width = width;
966 x11->vo_hint.max_height = height;
967 } else
969 x11->vo_hint.max_width = 0;
970 x11->vo_hint.max_height = 0;
973 // Set minimum height/width to 4 to avoid off-by-one errors
974 // and because mga_vid requires a minimal size of 4 pixels.
975 x11->vo_hint.flags |= PMinSize;
976 x11->vo_hint.min_width = x11->vo_hint.min_height = 4;
978 // Set the base size. A window manager might display the window
979 // size to the user relative to this.
980 // Setting these to width/height might be nice, but e.g. fluxbox can't handle it.
981 x11->vo_hint.flags |= PBaseSize;
982 x11->vo_hint.base_width = 0 /*width*/;
983 x11->vo_hint.base_height = 0 /*height*/;
985 x11->vo_hint.flags |= PWinGravity;
986 x11->vo_hint.win_gravity = StaticGravity;
987 XSetWMNormalHints(x11->display, x11->window, &x11->vo_hint);
990 static int vo_x11_get_gnome_layer(struct vo_x11_state *x11, Window win)
992 Atom type;
993 int format;
994 unsigned long nitems;
995 unsigned long bytesafter;
996 unsigned short *args = NULL;
998 if (XGetWindowProperty(x11->display, win, x11->XA_WIN_LAYER, 0, 16384,
999 False, AnyPropertyType, &type, &format, &nitems,
1000 &bytesafter,
1001 (unsigned char **) &args) == Success
1002 && nitems > 0 && args)
1004 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
1005 *args);
1006 return *args;
1008 return WIN_LAYER_NORMAL;
1012 static Window vo_x11_create_smooth_window(struct vo_x11_state *x11, Window mRoot,
1013 Visual * vis, int x, int y,
1014 unsigned int width, unsigned int height,
1015 int depth, Colormap col_map)
1017 unsigned long xswamask = CWBorderPixel;
1018 XSetWindowAttributes xswa;
1019 Window ret_win;
1021 if (col_map != CopyFromParent)
1023 xswa.colormap = col_map;
1024 xswamask |= CWColormap;
1026 xswa.background_pixel = 0;
1027 xswa.border_pixel = 0;
1028 xswa.backing_store = NotUseful;
1029 xswa.bit_gravity = StaticGravity;
1031 ret_win =
1032 XCreateWindow(x11->display, x11->rootwin, x, y, width, height, 0, depth,
1033 CopyFromParent, vis, xswamask, &xswa);
1034 XSetWMProtocols(x11->display, ret_win, &x11->XAWM_DELETE_WINDOW, 1);
1035 if (!x11->f_gc)
1036 x11->f_gc = XCreateGC(x11->display, ret_win, 0, 0);
1037 XSetForeground(x11->display, x11->f_gc, 0);
1039 return ret_win;
1043 * \brief create and setup a window suitable for display
1044 * \param vis Visual to use for creating the window
1045 * \param x x position of window
1046 * \param y y position of window
1047 * \param width width of window
1048 * \param height height of window
1049 * \param flags flags for window creation.
1050 * Only VOFLAG_FULLSCREEN is supported so far.
1051 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1052 * \param classname name to use for the classhint
1053 * \param title title for the window
1055 * This also does the grunt-work like setting Window Manager hints etc.
1056 * If vo_window is already set it just moves and resizes it.
1058 void vo_x11_create_vo_window(struct vo *vo, XVisualInfo *vis, int x, int y,
1059 unsigned int width, unsigned int height, int flags,
1060 Colormap col_map,
1061 const char *classname, const char *title)
1063 struct MPOpts *opts = vo->opts;
1064 struct vo_x11_state *x11 = vo->x11;
1065 Display *mDisplay = vo->x11->display;
1066 XGCValues xgcv;
1067 if (WinID >= 0) {
1068 x11->window = WinID ? (Window)WinID : x11->rootwin;
1069 if (col_map != CopyFromParent) {
1070 unsigned long xswamask = CWColormap;
1071 XSetWindowAttributes xswa;
1072 xswa.colormap = col_map;
1073 XUnmapWindow(mDisplay, x11->window);
1074 XChangeWindowAttributes(mDisplay, x11->window, xswamask, &xswa);
1075 XMapWindow(mDisplay, x11->window);
1077 if (WinID) vo_x11_update_geometry(vo);
1078 vo_x11_selectinput_witherr(mDisplay, x11->window,
1079 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1080 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1081 goto final;
1083 if (x11->window == None) {
1084 XSizeHints hint;
1085 XEvent xev;
1086 vo_fs = 0;
1087 vo->dwidth = width;
1088 vo->dheight = height;
1089 x11->window = vo_x11_create_smooth_window(x11, x11->rootwin, vis->visual,
1090 x, y, width, height, vis->depth, col_map);
1091 vo_x11_classhint(vo, x11->window, classname);
1092 XStoreName(mDisplay, x11->window, title);
1093 vo_hidecursor(mDisplay, x11->window);
1094 XSelectInput(mDisplay, x11->window, StructureNotifyMask);
1095 hint.x = x; hint.y = y;
1096 hint.width = width; hint.height = height;
1097 hint.flags = PPosition | PSize;
1098 XSetStandardProperties(mDisplay, x11->window, title, title, None, NULL, 0, &hint);
1099 vo_x11_sizehint(vo, x, y, width, height, 0);
1100 if (!vo_border) vo_x11_decoration(vo, 0);
1101 // map window
1102 XMapWindow(mDisplay, x11->window);
1103 XClearWindow(mDisplay, x11->window);
1104 // wait for map
1105 do {
1106 XNextEvent(mDisplay, &xev);
1107 } while (xev.type != MapNotify || xev.xmap.event != x11->window);
1108 XSelectInput(mDisplay, x11->window, NoEventMask);
1109 XSync(mDisplay, False);
1110 vo_x11_selectinput_witherr(mDisplay, x11->window,
1111 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1112 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1114 if (opts->vo_ontop) vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1115 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, width, height);
1116 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1117 vo_x11_fullscreen(vo);
1118 else if (vo_fs) {
1119 // if we are already in fullscreen do not switch back and forth, just
1120 // set the size values right.
1121 vo->dwidth = vo->opts->vo_screenwidth;
1122 vo->dheight = vo->opts->vo_screenheight;
1124 final:
1125 if (x11->vo_gc != None)
1126 XFreeGC(mDisplay, x11->vo_gc);
1127 x11->vo_gc = XCreateGC(mDisplay, x11->window, GCForeground, &xgcv);
1128 XSync(mDisplay, False);
1129 x11->vo_mouse_autohide = 1;
1132 void vo_x11_clearwindow_part(struct vo *vo, Window vo_window,
1133 int img_width, int img_height, int use_fs)
1135 struct vo_x11_state *x11 = vo->x11;
1136 struct MPOpts *opts = vo->opts;
1137 Display *mDisplay = vo->x11->display;
1138 int u_dheight, u_dwidth, left_ov, left_ov2;
1140 if (!x11->f_gc)
1141 return;
1143 u_dheight = use_fs ? opts->vo_screenheight : vo->dheight;
1144 u_dwidth = use_fs ? opts->vo_screenwidth : vo->dwidth;
1145 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1146 return;
1148 left_ov = (u_dheight - img_height) / 2;
1149 left_ov2 = (u_dwidth - img_width) / 2;
1151 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, 0, u_dwidth, left_ov);
1152 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, u_dheight - left_ov - 1,
1153 u_dwidth, left_ov + 1);
1155 if (u_dwidth > img_width)
1157 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, left_ov, left_ov2,
1158 img_height);
1159 XFillRectangle(mDisplay, vo_window, x11->f_gc, u_dwidth - left_ov2 - 1,
1160 left_ov, left_ov2 + 1, img_height);
1163 XFlush(mDisplay);
1166 void vo_x11_clearwindow(struct vo *vo, Window vo_window)
1168 struct vo_x11_state *x11 = vo->x11;
1169 struct MPOpts *opts = vo->opts;
1170 if (!x11->f_gc)
1171 return;
1172 XFillRectangle(x11->display, vo_window, x11->f_gc, 0, 0,
1173 opts->vo_screenwidth, opts->vo_screenheight);
1175 XFlush(x11->display);
1179 void vo_x11_setlayer(struct vo *vo, Window vo_window, int layer)
1181 struct vo_x11_state *x11 = vo->x11;
1182 if (WinID >= 0)
1183 return;
1185 if (x11->fs_type & vo_wm_LAYER)
1187 XClientMessageEvent xev;
1189 if (!x11->orig_layer)
1190 x11->orig_layer = vo_x11_get_gnome_layer(x11, vo_window);
1192 memset(&xev, 0, sizeof(xev));
1193 xev.type = ClientMessage;
1194 xev.display = x11->display;
1195 xev.window = vo_window;
1196 xev.message_type = x11->XA_WIN_LAYER;
1197 xev.format = 32;
1198 xev.data.l[0] = layer ? fs_layer : x11->orig_layer; // if not fullscreen, stay on default layer
1199 xev.data.l[1] = CurrentTime;
1200 mp_msg(MSGT_VO, MSGL_V,
1201 "[x11] Layered style stay on top (layer %ld).\n",
1202 xev.data.l[0]);
1203 XSendEvent(x11->display, x11->rootwin, False, SubstructureNotifyMask,
1204 (XEvent *) & xev);
1205 } else if (x11->fs_type & vo_wm_NETWM)
1207 XClientMessageEvent xev;
1208 char *state;
1210 memset(&xev, 0, sizeof(xev));
1211 xev.type = ClientMessage;
1212 xev.message_type = x11->XA_NET_WM_STATE;
1213 xev.display = x11->display;
1214 xev.window = vo_window;
1215 xev.format = 32;
1216 xev.data.l[0] = layer;
1218 if (x11->fs_type & vo_wm_STAYS_ON_TOP)
1219 xev.data.l[1] = x11->XA_NET_WM_STATE_STAYS_ON_TOP;
1220 else if (x11->fs_type & vo_wm_ABOVE)
1221 xev.data.l[1] = x11->XA_NET_WM_STATE_ABOVE;
1222 else if (x11->fs_type & vo_wm_FULLSCREEN)
1223 xev.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
1224 else if (x11->fs_type & vo_wm_BELOW)
1225 // This is not fallback. We can safely assume that the situation
1226 // where only NETWM_STATE_BELOW is supported doesn't exist.
1227 xev.data.l[1] = x11->XA_NET_WM_STATE_BELOW;
1229 XSendEvent(x11->display, x11->rootwin, False, SubstructureRedirectMask,
1230 (XEvent *) & xev);
1231 state = XGetAtomName(x11->display, xev.data.l[1]);
1232 mp_msg(MSGT_VO, MSGL_V,
1233 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1234 layer, state);
1235 XFree(state);
1239 static int vo_x11_get_fs_type(int supported)
1241 int i;
1242 int type = supported;
1244 if (vo_fstype_list)
1246 i = 0;
1247 for (i = 0; vo_fstype_list[i]; i++)
1249 int neg = 0;
1250 char *arg = vo_fstype_list[i];
1252 if (vo_fstype_list[i][0] == '-')
1254 neg = 1;
1255 arg = vo_fstype_list[i] + 1;
1258 if (!strncmp(arg, "layer", 5))
1260 if (!neg && (arg[5] == '='))
1262 char *endptr = NULL;
1263 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1265 if (endptr && *endptr == '\0' && layer >= 0
1266 && layer <= 15)
1267 fs_layer = layer;
1269 if (neg)
1270 type &= ~vo_wm_LAYER;
1271 else
1272 type |= vo_wm_LAYER;
1273 } else if (!strcmp(arg, "above"))
1275 if (neg)
1276 type &= ~vo_wm_ABOVE;
1277 else
1278 type |= vo_wm_ABOVE;
1279 } else if (!strcmp(arg, "fullscreen"))
1281 if (neg)
1282 type &= ~vo_wm_FULLSCREEN;
1283 else
1284 type |= vo_wm_FULLSCREEN;
1285 } else if (!strcmp(arg, "stays_on_top"))
1287 if (neg)
1288 type &= ~vo_wm_STAYS_ON_TOP;
1289 else
1290 type |= vo_wm_STAYS_ON_TOP;
1291 } else if (!strcmp(arg, "below"))
1293 if (neg)
1294 type &= ~vo_wm_BELOW;
1295 else
1296 type |= vo_wm_BELOW;
1297 } else if (!strcmp(arg, "netwm"))
1299 if (neg)
1300 type &= ~vo_wm_NETWM;
1301 else
1302 type |= vo_wm_NETWM;
1303 } else if (!strcmp(arg, "none"))
1304 return 0;
1308 return type;
1312 * \brief update vo->dx, vo->dy, vo->dwidth and vo->dheight with current values of vo->x11->window
1313 * \return returns current color depth of vo->x11->window
1315 int vo_x11_update_geometry(struct vo *vo)
1317 struct vo_x11_state *x11 = vo->x11;
1318 unsigned depth, w, h;
1319 int dummy_int;
1320 Window dummy_win;
1321 XGetGeometry(x11->display, x11->window, &dummy_win, &dummy_int, &dummy_int,
1322 &w, &h, &dummy_int, &depth);
1323 if (w <= INT_MAX && h <= INT_MAX) {
1324 vo->dwidth = w;
1325 vo->dheight = h;
1327 XTranslateCoordinates(x11->display, x11->window, x11->rootwin, 0, 0,
1328 &vo->dx, &vo->dy, &dummy_win);
1329 return depth <= INT_MAX ? depth : 0;
1332 void vo_x11_fullscreen(struct vo *vo)
1334 struct MPOpts *opts = vo->opts;
1335 struct vo_x11_state *x11 = vo->x11;
1336 int x, y, w, h;
1338 if (WinID >= 0 || x11->fs_flip)
1339 return;
1341 if (vo_fs)
1343 // fs->win
1344 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1346 x = x11->vo_old_x;
1347 y = x11->vo_old_y;
1348 w = x11->vo_old_width;
1349 h = x11->vo_old_height;
1352 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1353 vo_fs = VO_FALSE;
1354 } else
1356 // win->fs
1357 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1359 vo_fs = VO_TRUE;
1360 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1362 x11->vo_old_x = vo->dx;
1363 x11->vo_old_y = vo->dy;
1364 x11->vo_old_width = vo->dwidth;
1365 x11->vo_old_height = vo->dheight;
1367 update_xinerama_info(vo);
1368 x = xinerama_x;
1369 y = xinerama_y;
1370 w = opts->vo_screenwidth;
1371 h = opts->vo_screenheight;
1374 long dummy;
1376 XGetWMNormalHints(x11->display, x11->window, &x11->vo_hint, &dummy);
1377 if (!(x11->vo_hint.flags & PWinGravity))
1378 x11->old_gravity = NorthWestGravity;
1379 else
1380 x11->old_gravity = x11->vo_hint.win_gravity;
1382 if (x11->wm_type == 0 && !(vo_fsmode & 16))
1384 XUnmapWindow(x11->display, x11->window); // required for MWM
1385 XWithdrawWindow(x11->display, x11->window, x11->screen);
1386 x11->fs_flip = 1;
1389 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1391 vo_x11_decoration(vo, vo_border && !vo_fs);
1392 vo_x11_sizehint(vo, x, y, w, h, 0);
1393 vo_x11_setlayer(vo, x11->window, vo_fs);
1396 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1398 /* some WMs lose ontop after fullscreen */
1399 if ((!(vo_fs)) & opts->vo_ontop)
1400 vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1402 XMapRaised(x11->display, x11->window);
1403 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1404 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1405 XRaiseWindow(x11->display, x11->window);
1406 XFlush(x11->display);
1409 void vo_x11_ontop(struct vo *vo)
1411 struct MPOpts *opts = vo->opts;
1412 opts->vo_ontop = !opts->vo_ontop;
1414 vo_x11_setlayer(vo, vo->x11->window, opts->vo_ontop);
1417 void vo_x11_border(struct vo *vo)
1419 vo_border = !vo_border;
1420 vo_x11_decoration(vo, vo_border && !vo_fs);
1424 * XScreensaver stuff
1427 static int screensaver_off;
1428 static unsigned int time_last;
1430 void xscreensaver_heartbeat(struct vo_x11_state *x11)
1432 unsigned int time = GetTimerMS();
1434 if (x11->display && screensaver_off && (time - time_last) > 30000)
1436 time_last = time;
1438 XResetScreenSaver(x11->display);
1442 static int xss_suspend(Display *mDisplay, Bool suspend)
1444 #ifndef CONFIG_XSS
1445 return 0;
1446 #else
1447 int event, error, major, minor;
1448 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1449 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1450 return 0;
1451 if (major < 1 || (major == 1 && minor < 1))
1452 return 0;
1453 XScreenSaverSuspend(mDisplay, suspend);
1454 return 1;
1455 #endif
1459 * End of XScreensaver stuff
1462 static void saver_on(Display * mDisplay)
1465 if (!screensaver_off)
1466 return;
1467 screensaver_off = 0;
1468 if (xss_suspend(mDisplay, False))
1469 return;
1470 #ifdef CONFIG_XDPMS
1471 if (dpms_disabled)
1473 int nothing;
1474 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1476 if (!DPMSEnable(mDisplay))
1477 { // restoring power saving settings
1478 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1479 } else
1481 // DPMS does not seem to be enabled unless we call DPMSInfo
1482 BOOL onoff;
1483 CARD16 state;
1485 DPMSForceLevel(mDisplay, DPMSModeOn);
1486 DPMSInfo(mDisplay, &state, &onoff);
1487 if (onoff)
1489 mp_msg(MSGT_VO, MSGL_V,
1490 "Successfully enabled DPMS\n");
1491 } else
1493 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1497 dpms_disabled = 0;
1499 #endif
1502 static void saver_off(Display * mDisplay)
1504 int nothing;
1506 if (screensaver_off)
1507 return;
1508 screensaver_off = 1;
1509 if (xss_suspend(mDisplay, True))
1510 return;
1511 #ifdef CONFIG_XDPMS
1512 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1514 BOOL onoff;
1515 CARD16 state;
1517 DPMSInfo(mDisplay, &state, &onoff);
1518 if (onoff)
1520 Status stat;
1522 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1523 dpms_disabled = 1;
1524 stat = DPMSDisable(mDisplay); // monitor powersave off
1525 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1528 #endif
1531 static XErrorHandler old_handler = NULL;
1532 static int selectinput_err = 0;
1533 static int x11_selectinput_errorhandler(Display * display,
1534 XErrorEvent * event)
1536 if (event->error_code == BadAccess)
1538 selectinput_err = 1;
1539 mp_msg(MSGT_VO, MSGL_ERR,
1540 "X11 error: BadAccess during XSelectInput Call\n");
1541 mp_msg(MSGT_VO, MSGL_ERR,
1542 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1543 /* If you think MPlayer should shutdown with this error,
1544 * comment out the following line */
1545 return 0;
1547 if (old_handler != NULL)
1548 old_handler(display, event);
1549 else
1550 x11_errorhandler(display, event);
1551 return 0;
1554 void vo_x11_selectinput_witherr(Display * display, Window w,
1555 long event_mask)
1557 XSync(display, False);
1558 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1559 selectinput_err = 0;
1560 if (vo_nomouse_input)
1562 XSelectInput(display, w,
1563 event_mask &
1564 (~(ButtonPressMask | ButtonReleaseMask)));
1565 } else
1567 XSelectInput(display, w, event_mask);
1569 XSync(display, False);
1570 XSetErrorHandler(old_handler);
1571 if (selectinput_err)
1573 mp_msg(MSGT_VO, MSGL_ERR,
1574 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1575 XSelectInput(display, w,
1576 event_mask &
1578 (ButtonPressMask | ButtonReleaseMask |
1579 PointerMotionMask)));
1583 #ifdef CONFIG_XF86VM
1584 void vo_vm_switch(struct vo *vo)
1586 struct vo_x11_state *x11 = vo->x11;
1587 struct MPOpts *opts = vo->opts;
1588 Display *mDisplay = x11->display;
1589 int vm_event, vm_error;
1590 int vm_ver, vm_rev;
1591 int i, j, have_vm = 0;
1592 int X = vo->dwidth, Y = vo->dheight;
1593 int modeline_width, modeline_height;
1595 int modecount;
1597 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1599 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1600 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1601 vm_rev);
1602 have_vm = 1;
1603 } else {
1604 mp_msg(MSGT_VO, MSGL_WARN,
1605 "XF86VidMode extension not available.\n");
1608 if (have_vm)
1610 if (vidmodes == NULL)
1611 XF86VidModeGetAllModeLines(mDisplay, x11->screen, &modecount,
1612 &vidmodes);
1613 j = 0;
1614 modeline_width = vidmodes[0]->hdisplay;
1615 modeline_height = vidmodes[0]->vdisplay;
1617 for (i = 1; i < modecount; i++)
1618 if ((vidmodes[i]->hdisplay >= X)
1619 && (vidmodes[i]->vdisplay >= Y))
1620 if ((vidmodes[i]->hdisplay <= modeline_width)
1621 && (vidmodes[i]->vdisplay <= modeline_height))
1623 modeline_width = vidmodes[i]->hdisplay;
1624 modeline_height = vidmodes[i]->vdisplay;
1625 j = i;
1628 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_SelectedVideoMode,
1629 modeline_width, modeline_height, X, Y);
1630 XF86VidModeLockModeSwitch(mDisplay, x11->screen, 0);
1631 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1632 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1634 // FIXME: all this is more of a hack than proper solution
1635 X = (opts->vo_screenwidth - modeline_width) / 2;
1636 Y = (opts->vo_screenheight - modeline_height) / 2;
1637 XF86VidModeSetViewPort(mDisplay, x11->screen, X, Y);
1638 vo->dx = X;
1639 vo->dy = Y;
1640 vo->dwidth = modeline_width;
1641 vo->dheight = modeline_height;
1642 aspect_save_screenres(vo, modeline_width, modeline_height);
1646 void vo_vm_close(struct vo *vo)
1648 Display *dpy = vo->x11->display;
1649 struct MPOpts *opts = vo->opts;
1650 #ifdef CONFIG_GUI
1651 if (vidmodes != NULL && vo->x11->vo_window != None)
1652 #else
1653 if (vidmodes != NULL)
1654 #endif
1656 int i, modecount;
1658 free(vidmodes);
1659 vidmodes = NULL;
1660 XF86VidModeGetAllModeLines(dpy, vo->x11->screen, &modecount,
1661 &vidmodes);
1662 for (i = 0; i < modecount; i++)
1663 if ((vidmodes[i]->hdisplay == opts->vo_screenwidth)
1664 && (vidmodes[i]->vdisplay == opts->vo_screenheight))
1666 mp_msg(MSGT_VO, MSGL_INFO,
1667 "Returning to original mode %dx%d\n",
1668 opts->vo_screenwidth, opts->vo_screenheight);
1669 break;
1672 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1673 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1674 free(vidmodes);
1675 vidmodes = NULL;
1678 #endif
1680 #endif /* X11_FULLSCREEN */
1684 * Scan the available visuals on this Display/Screen. Try to find
1685 * the 'best' available TrueColor visual that has a decent color
1686 * depth (at least 15bit). If there are multiple visuals with depth
1687 * >= 15bit, we prefer visuals with a smaller color depth.
1689 int vo_find_depth_from_visuals(Display * dpy, int screen,
1690 Visual ** visual_return)
1692 XVisualInfo visual_tmpl;
1693 XVisualInfo *visuals;
1694 int nvisuals, i;
1695 int bestvisual = -1;
1696 int bestvisual_depth = -1;
1698 visual_tmpl.screen = screen;
1699 visual_tmpl.class = TrueColor;
1700 visuals = XGetVisualInfo(dpy,
1701 VisualScreenMask | VisualClassMask,
1702 &visual_tmpl, &nvisuals);
1703 if (visuals != NULL)
1705 for (i = 0; i < nvisuals; i++)
1707 mp_msg(MSGT_VO, MSGL_V,
1708 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1709 visuals[i].visualid, visuals[i].depth,
1710 visuals[i].red_mask, visuals[i].green_mask,
1711 visuals[i].blue_mask);
1713 * Save the visual index and its depth, if this is the first
1714 * truecolor visul, or a visual that is 'preferred' over the
1715 * previous 'best' visual.
1717 if (bestvisual_depth == -1
1718 || (visuals[i].depth >= 15
1719 && (visuals[i].depth < bestvisual_depth
1720 || bestvisual_depth < 15)))
1722 bestvisual = i;
1723 bestvisual_depth = visuals[i].depth;
1727 if (bestvisual != -1 && visual_return != NULL)
1728 *visual_return = visuals[bestvisual].visual;
1730 XFree(visuals);
1732 return bestvisual_depth;
1736 static Colormap cmap = None;
1737 static XColor cols[256];
1738 static int cm_size, red_mask, green_mask, blue_mask;
1741 Colormap vo_x11_create_colormap(struct vo *vo, XVisualInfo *vinfo)
1743 struct vo_x11_state *x11 = vo->x11;
1744 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1746 if (vinfo->class != DirectColor)
1747 return XCreateColormap(x11->display, x11->rootwin, vinfo->visual,
1748 AllocNone);
1750 /* can this function get called twice or more? */
1751 if (cmap)
1752 return cmap;
1753 cm_size = vinfo->colormap_size;
1754 red_mask = vinfo->red_mask;
1755 green_mask = vinfo->green_mask;
1756 blue_mask = vinfo->blue_mask;
1757 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1758 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1759 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1760 rvu = 65536ull * ru / (red_mask + ru);
1761 gvu = 65536ull * gu / (green_mask + gu);
1762 bvu = 65536ull * bu / (blue_mask + bu);
1763 r = g = b = 0;
1764 rv = gv = bv = 0;
1765 m = DoRed | DoGreen | DoBlue;
1766 for (k = 0; k < cm_size; k++)
1768 int t;
1770 cols[k].pixel = r | g | b;
1771 cols[k].red = rv;
1772 cols[k].green = gv;
1773 cols[k].blue = bv;
1774 cols[k].flags = m;
1775 t = (r + ru) & red_mask;
1776 if (t < r)
1777 m &= ~DoRed;
1778 r = t;
1779 t = (g + gu) & green_mask;
1780 if (t < g)
1781 m &= ~DoGreen;
1782 g = t;
1783 t = (b + bu) & blue_mask;
1784 if (t < b)
1785 m &= ~DoBlue;
1786 b = t;
1787 rv += rvu;
1788 gv += gvu;
1789 bv += bvu;
1791 cmap = XCreateColormap(x11->display, x11->rootwin, vinfo->visual, AllocAll);
1792 XStoreColors(x11->display, cmap, cols, cm_size);
1793 return cmap;
1797 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1798 * hue and red/green/blue intensity, but we cannot do saturation.
1799 * Currently only gamma, brightness and contrast are implemented.
1800 * Is there sufficient interest for hue and/or red/green/blue intensity?
1802 /* these values have range [-100,100] and are initially 0 */
1803 static int vo_gamma = 0;
1804 static int vo_brightness = 0;
1805 static int vo_contrast = 0;
1807 static int transform_color(float val,
1808 float brightness, float contrast, float gamma) {
1809 float s = pow(val, gamma);
1810 s = (s - 0.5) * contrast + 0.5;
1811 s += brightness;
1812 if (s < 0)
1813 s = 0;
1814 if (s > 1)
1815 s = 1;
1816 return (unsigned short) (s * 65535);
1819 uint32_t vo_x11_set_equalizer(struct vo *vo, char *name, int value)
1821 float gamma, brightness, contrast;
1822 float rf, gf, bf;
1823 int k;
1826 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1827 * of TrueColor-ed window but be careful:
1828 * Unlike the colormaps, which are private for the X client
1829 * who created them and thus automatically destroyed on client
1830 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1831 * setting and _must_ be restored before the process exits.
1832 * Unforunately when the process crashes (or gets killed
1833 * for some reason) it is impossible to restore the setting,
1834 * and such behaviour could be rather annoying for the users.
1836 if (cmap == None)
1837 return VO_NOTAVAIL;
1839 if (!strcasecmp(name, "brightness"))
1840 vo_brightness = value;
1841 else if (!strcasecmp(name, "contrast"))
1842 vo_contrast = value;
1843 else if (!strcasecmp(name, "gamma"))
1844 vo_gamma = value;
1845 else
1846 return VO_NOTIMPL;
1848 brightness = 0.01 * vo_brightness;
1849 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1850 gamma = pow(2, -0.02 * vo_gamma);
1852 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1853 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
1854 green_mask;
1855 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
1857 /* now recalculate the colormap using the newly set value */
1858 for (k = 0; k < cm_size; k++)
1860 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
1861 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
1862 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
1865 XStoreColors(vo->x11->display, cmap, cols, cm_size);
1866 XFlush(vo->x11->display);
1867 return VO_TRUE;
1870 uint32_t vo_x11_get_equalizer(char *name, int *value)
1872 if (cmap == None)
1873 return VO_NOTAVAIL;
1874 if (!strcasecmp(name, "brightness"))
1875 *value = vo_brightness;
1876 else if (!strcasecmp(name, "contrast"))
1877 *value = vo_contrast;
1878 else if (!strcasecmp(name, "gamma"))
1879 *value = vo_gamma;
1880 else
1881 return VO_NOTIMPL;
1882 return VO_TRUE;
1885 #ifdef CONFIG_XV
1886 int vo_xv_set_eq(struct vo *vo, uint32_t xv_port, char *name, int value)
1888 XvAttribute *attributes;
1889 int i, howmany, xv_atom;
1891 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
1893 /* get available attributes */
1894 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1895 for (i = 0; i < howmany && attributes; i++)
1896 if (attributes[i].flags & XvSettable)
1898 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1899 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1900 then trigger it if it's ok so that the other values are at default upon query */
1901 if (xv_atom != None)
1903 int hue = 0, port_value, port_min, port_max;
1905 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1906 (!strcasecmp(name, "brightness")))
1907 port_value = value;
1908 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1909 (!strcasecmp(name, "contrast")))
1910 port_value = value;
1911 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1912 (!strcasecmp(name, "saturation")))
1913 port_value = value;
1914 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1915 (!strcasecmp(name, "hue")))
1917 port_value = value;
1918 hue = 1;
1919 } else
1920 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1921 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1922 (!strcasecmp(name, "red_intensity")))
1923 port_value = value;
1924 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1925 && (!strcasecmp(name, "green_intensity")))
1926 port_value = value;
1927 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1928 && (!strcasecmp(name, "blue_intensity")))
1929 port_value = value;
1930 else
1931 continue;
1933 port_min = attributes[i].min_value;
1934 port_max = attributes[i].max_value;
1936 /* nvidia hue workaround */
1937 if (hue && port_min == 0 && port_max == 360)
1939 port_value =
1940 (port_value >=
1941 0) ? (port_value - 100) : (port_value + 100);
1943 // -100 -> min
1944 // 0 -> (max+min)/2
1945 // +100 -> max
1946 port_value =
1947 (port_value + 100) * (port_max - port_min) / 200 +
1948 port_min;
1949 XvSetPortAttribute(vo->x11->display, xv_port, xv_atom, port_value);
1950 return VO_TRUE;
1953 return VO_FALSE;
1956 int vo_xv_get_eq(struct vo *vo, uint32_t xv_port, char *name, int *value)
1959 XvAttribute *attributes;
1960 int i, howmany, xv_atom;
1962 /* get available attributes */
1963 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1964 for (i = 0; i < howmany && attributes; i++)
1965 if (attributes[i].flags & XvGettable)
1967 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1968 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1969 then trigger it if it's ok so that the other values are at default upon query */
1970 if (xv_atom != None)
1972 int val, port_value = 0, port_min, port_max;
1974 XvGetPortAttribute(vo->x11->display, xv_port, xv_atom,
1975 &port_value);
1977 port_min = attributes[i].min_value;
1978 port_max = attributes[i].max_value;
1979 val =
1980 (port_value - port_min) * 200 / (port_max - port_min) -
1981 100;
1983 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1984 (!strcasecmp(name, "brightness")))
1985 *value = val;
1986 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1987 (!strcasecmp(name, "contrast")))
1988 *value = val;
1989 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1990 (!strcasecmp(name, "saturation")))
1991 *value = val;
1992 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1993 (!strcasecmp(name, "hue")))
1995 /* nasty nvidia detect */
1996 if (port_min == 0 && port_max == 360)
1997 *value = (val >= 0) ? (val - 100) : (val + 100);
1998 else
1999 *value = val;
2000 } else
2001 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2002 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2003 (!strcasecmp(name, "red_intensity")))
2004 *value = val;
2005 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2006 && (!strcasecmp(name, "green_intensity")))
2007 *value = val;
2008 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2009 && (!strcasecmp(name, "blue_intensity")))
2010 *value = val;
2011 else
2012 continue;
2014 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2015 name, *value);
2016 return VO_TRUE;
2019 return VO_FALSE;
2023 * \brief Interns the requested atom if it is available.
2025 * \param atom_name String containing the name of the requested atom.
2027 * \return Returns the atom if available, else None is returned.
2030 static Atom xv_intern_atom_if_exists(struct vo_x11_state *x11,
2031 char const *atom_name)
2033 XvAttribute * attributes;
2034 int attrib_count,i;
2035 Atom xv_atom = None;
2037 attributes = XvQueryPortAttributes(x11->display, x11->xv_port, &attrib_count );
2038 if( attributes!=NULL )
2040 for ( i = 0; i < attrib_count; ++i )
2042 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2044 xv_atom = XInternAtom(x11->display, atom_name, False );
2045 break; // found what we want, break out
2048 XFree( attributes );
2051 return xv_atom;
2055 * \brief Try to enable vsync for xv.
2056 * \return Returns -1 if not available, 0 on failure and 1 on success.
2058 int vo_xv_enable_vsync(struct vo *vo)
2060 struct vo_x11_state *x11 = vo->x11;
2061 Atom xv_atom = xv_intern_atom_if_exists(x11, "XV_SYNC_TO_VBLANK");
2062 if (xv_atom == None)
2063 return -1;
2064 return XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1) == Success;
2068 * \brief Get maximum supported source image dimensions.
2070 * This function does not set the variables pointed to by
2071 * width and height if the information could not be retrieved,
2072 * so the caller is reponsible for properly initializing them.
2074 * \param width [out] The maximum width gets stored here.
2075 * \param height [out] The maximum height gets stored here.
2078 void vo_xv_get_max_img_dim(struct vo *vo, uint32_t * width, uint32_t * height)
2080 struct vo_x11_state *x11 = vo->x11;
2081 XvEncodingInfo * encodings;
2082 //unsigned long num_encodings, idx; to int or too long?!
2083 unsigned int num_encodings, idx;
2085 XvQueryEncodings(x11->display, x11->xv_port, &num_encodings, &encodings);
2087 if ( encodings )
2089 for ( idx = 0; idx < num_encodings; ++idx )
2091 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2093 *width = encodings[idx].width;
2094 *height = encodings[idx].height;
2095 break;
2100 mp_msg( MSGT_VO, MSGL_V,
2101 "[xv common] Maximum source image dimensions: %ux%u\n",
2102 *width, *height );
2104 XvFreeEncodingInfo( encodings );
2108 * \brief Print information about the colorkey method and source.
2110 * \param ck_handling Integer value containing the information about
2111 * colorkey handling (see x11_common.h).
2113 * Outputs the content of |ck_handling| as a readable message.
2116 static void vo_xv_print_ck_info(struct vo_x11_state *x11)
2118 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2120 switch ( x11->xv_ck_info.method )
2122 case CK_METHOD_NONE:
2123 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2124 case CK_METHOD_AUTOPAINT:
2125 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2126 case CK_METHOD_MANUALFILL:
2127 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2128 case CK_METHOD_BACKGROUND:
2129 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2132 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2134 switch ( x11->xv_ck_info.source )
2136 case CK_SRC_CUR:
2137 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2138 x11->xv_colorkey );
2139 break;
2140 case CK_SRC_USE:
2141 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2143 mp_msg( MSGT_VO, MSGL_V,
2144 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2145 x11->xv_colorkey );
2147 else
2149 mp_msg( MSGT_VO, MSGL_V,
2150 "Using colorkey from MPlayer (0x%06lx)."
2151 " Use -colorkey to change.\n",
2152 x11->xv_colorkey );
2154 break;
2155 case CK_SRC_SET:
2156 mp_msg( MSGT_VO, MSGL_V,
2157 "Setting and using colorkey from MPlayer (0x%06lx)."
2158 " Use -colorkey to change.\n",
2159 x11->xv_colorkey );
2160 break;
2164 * \brief Init colorkey depending on the settings in xv_ck_info.
2166 * \return Returns 0 on failure and 1 on success.
2168 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2169 * flags in xv_ck_info.
2171 * Possiblilities:
2172 * * Methods
2173 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2174 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2175 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2176 * * Sources
2177 * - use currently set colorkey ( CK_SRC_CUR )
2178 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2179 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2181 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2182 * we don't draw anything as this means it was forced to off.
2184 int vo_xv_init_colorkey(struct vo *vo)
2186 struct vo_x11_state *x11 = vo->x11;
2187 Atom xv_atom;
2188 int rez;
2190 /* check if colorkeying is needed */
2191 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_COLORKEY");
2193 /* if we have to deal with colorkeying ... */
2194 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2196 /* check if we should use the colorkey specified in vo_colorkey */
2197 if ( x11->xv_ck_info.source != CK_SRC_CUR )
2199 x11->xv_colorkey = vo_colorkey;
2201 /* check if we have to set the colorkey too */
2202 if ( x11->xv_ck_info.source == CK_SRC_SET )
2204 xv_atom = XInternAtom(x11->display, "XV_COLORKEY",False);
2206 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, vo_colorkey);
2207 if ( rez != Success )
2209 mp_msg( MSGT_VO, MSGL_FATAL,
2210 "[xv common] Couldn't set colorkey!\n" );
2211 return 0; // error setting colorkey
2215 else
2217 int colorkey_ret;
2219 rez=XvGetPortAttribute(x11->display,x11->xv_port, xv_atom, &colorkey_ret);
2220 if ( rez == Success )
2222 x11->xv_colorkey = colorkey_ret;
2224 else
2226 mp_msg( MSGT_VO, MSGL_FATAL,
2227 "[xv common] Couldn't get colorkey!"
2228 "Maybe the selected Xv port has no overlay.\n" );
2229 return 0; // error getting colorkey
2233 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_AUTOPAINT_COLORKEY");
2235 /* should we draw the colorkey ourselves or activate autopainting? */
2236 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2238 rez = !Success; // reset rez to something different than Success
2240 if ( xv_atom != None ) // autopaint is supported
2242 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1);
2245 if ( rez != Success )
2247 // fallback to manual colorkey drawing
2248 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2251 else // disable colorkey autopainting if supported
2253 if ( xv_atom != None ) // we have autopaint attribute
2255 XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 0);
2259 else // do no colorkey drawing at all
2261 x11->xv_ck_info.method = CK_METHOD_NONE;
2262 } /* end: should we draw colorkey */
2264 /* output information about the current colorkey settings */
2265 vo_xv_print_ck_info(x11);
2267 return 1; // success
2271 * \brief Draw the colorkey on the video window.
2273 * Draws the colorkey depending on the set method ( colorkey_handling ).
2275 * Also draws the black bars ( when the video doesn't fit the display in
2276 * fullscreen ) separately, so they don't overlap with the video area.
2277 * It doesn't call XFlush.
2280 void vo_xv_draw_colorkey(struct vo *vo, int32_t x, int32_t y,
2281 int32_t w, int32_t h)
2283 struct MPOpts *opts = vo->opts;
2284 struct vo_x11_state *x11 = vo->x11;
2285 if( x11->xv_ck_info.method == CK_METHOD_MANUALFILL ||
2286 x11->xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2288 XSetForeground(x11->display, x11->vo_gc, x11->xv_colorkey );
2289 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2290 x, y,
2291 w, h );
2294 /* draw black bars if needed */
2295 /* TODO! move this to vo_x11_clearwindow_part() */
2296 if ( vo_fs )
2298 XSetForeground(x11->display, x11->vo_gc, 0 );
2299 /* making non-overlap fills, requires 8 checks instead of 4 */
2300 if ( y > 0 )
2301 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2302 0, 0,
2303 opts->vo_screenwidth, y);
2304 if (x > 0)
2305 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2306 0, 0,
2307 x, opts->vo_screenheight);
2308 if (x + w < opts->vo_screenwidth)
2309 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2310 x + w, 0,
2311 opts->vo_screenwidth, opts->vo_screenheight);
2312 if (y + h < opts->vo_screenheight)
2313 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2314 0, y + h,
2315 opts->vo_screenwidth, opts->vo_screenheight);
2319 /** \brief Tests if a valid argument for the ck suboption was given. */
2320 int xv_test_ck( void * arg )
2322 strarg_t * strarg = (strarg_t *)arg;
2324 if ( strargcmp( strarg, "use" ) == 0 ||
2325 strargcmp( strarg, "set" ) == 0 ||
2326 strargcmp( strarg, "cur" ) == 0 )
2328 return 1;
2331 return 0;
2333 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2334 int xv_test_ckm( void * arg )
2336 strarg_t * strarg = (strarg_t *)arg;
2338 if ( strargcmp( strarg, "bg" ) == 0 ||
2339 strargcmp( strarg, "man" ) == 0 ||
2340 strargcmp( strarg, "auto" ) == 0 )
2342 return 1;
2345 return 0;
2349 * \brief Modify the colorkey_handling var according to str
2351 * Checks if a valid pointer ( not NULL ) to the string
2352 * was given. And in that case modifies the colorkey_handling
2353 * var to reflect the requested behaviour.
2354 * If nothing happens the content of colorkey_handling stays
2355 * the same.
2357 * \param str Pointer to the string or NULL
2360 void xv_setup_colorkeyhandling(struct vo *vo, const char *ck_method_str,
2361 const char *ck_str)
2363 struct vo_x11_state *x11 = vo->x11;
2364 /* check if a valid pointer to the string was passed */
2365 if ( ck_str )
2367 if ( strncmp( ck_str, "use", 3 ) == 0 )
2369 x11->xv_ck_info.source = CK_SRC_USE;
2371 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2373 x11->xv_ck_info.source = CK_SRC_SET;
2376 /* check if a valid pointer to the string was passed */
2377 if ( ck_method_str )
2379 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2381 x11->xv_ck_info.method = CK_METHOD_BACKGROUND;
2383 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2385 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2387 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2389 x11->xv_ck_info.method = CK_METHOD_AUTOPAINT;
2394 #endif
2396 struct vo_x11_state *vo_x11_init_state(void)
2398 struct vo_x11_state *s = talloc_ptrtype(NULL, s);
2399 *s = (struct vo_x11_state){
2400 .xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR },
2401 .olddecor = MWM_DECOR_ALL,
2402 .oldfuncs = MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
2403 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE,
2404 .old_gravity = NorthWestGravity,
2406 return s;