x11: input: add print and menu keys
[mplayer.git] / libvo / x11_common.c
blob9f19910234abd02f101596000f516240a1fa31aa
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>
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/keycodes.h"
80 #define WIN_LAYER_ONBOTTOM 2
81 #define WIN_LAYER_NORMAL 4
82 #define WIN_LAYER_ONTOP 6
83 #define WIN_LAYER_ABOVE_DOCK 10
85 int fs_layer = WIN_LAYER_ABOVE_DOCK;
87 int stop_xscreensaver = 0;
89 static int dpms_disabled = 0;
91 char *mDisplayName = NULL;
93 char **vo_fstype_list;
95 /* 1 means that the WM is metacity (broken as hell) */
96 int metacity_hack = 0;
98 #ifdef CONFIG_XF86VM
99 XF86VidModeModeInfo **vidmodes = NULL;
100 XF86VidModeModeLine modeline;
101 #endif
103 static int vo_x11_get_fs_type(int supported);
104 static void saver_off(Display *);
105 static void saver_on(Display *);
108 * Sends the EWMH fullscreen state event.
110 * action: could be one of _NET_WM_STATE_REMOVE -- remove state
111 * _NET_WM_STATE_ADD -- add state
112 * _NET_WM_STATE_TOGGLE -- toggle
114 void vo_x11_ewmh_fullscreen(struct vo_x11_state *x11, int action)
116 assert(action == _NET_WM_STATE_REMOVE ||
117 action == _NET_WM_STATE_ADD || action == _NET_WM_STATE_TOGGLE);
119 if (x11->fs_type & vo_wm_FULLSCREEN)
121 XEvent xev;
123 /* init X event structure for _NET_WM_FULLSCREEN client message */
124 xev.xclient.type = ClientMessage;
125 xev.xclient.serial = 0;
126 xev.xclient.send_event = True;
127 xev.xclient.message_type = x11->XA_NET_WM_STATE;
128 xev.xclient.window = x11->window;
129 xev.xclient.format = 32;
130 xev.xclient.data.l[0] = action;
131 xev.xclient.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
132 xev.xclient.data.l[2] = 0;
133 xev.xclient.data.l[3] = 0;
134 xev.xclient.data.l[4] = 0;
136 /* finally send that damn thing */
137 if (!XSendEvent(x11->display, DefaultRootWindow(x11->display), False,
138 SubstructureRedirectMask | SubstructureNotifyMask,
139 &xev))
141 mp_tmsg(MSGT_VO, MSGL_ERR, "\nX11: Couldn't send EWMH fullscreen event!\n");
146 static void vo_hidecursor(Display * disp, Window win)
148 Cursor no_ptr;
149 Pixmap bm_no;
150 XColor black, dummy;
151 Colormap colormap;
152 const char bm_no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
154 if (WinID == 0)
155 return; // do not hide if playing on the root window
157 colormap = DefaultColormap(disp, DefaultScreen(disp));
158 if ( !XAllocNamedColor(disp, colormap, "black", &black, &dummy) )
160 return; // color alloc failed, give up
162 bm_no = XCreateBitmapFromData(disp, win, bm_no_data, 8, 8);
163 no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
164 XDefineCursor(disp, win, no_ptr);
165 XFreeCursor(disp, no_ptr);
166 if (bm_no != None)
167 XFreePixmap(disp, bm_no);
168 XFreeColors(disp,colormap,&black.pixel,1,0);
171 static void vo_showcursor(Display * disp, Window win)
173 if (WinID == 0)
174 return;
175 XDefineCursor(disp, win, 0);
178 static int x11_errorhandler(Display * display, XErrorEvent * event)
180 #define MSGLEN 60
181 char msg[MSGLEN];
183 XGetErrorText(display, event->error_code, (char *) &msg, MSGLEN);
185 mp_msg(MSGT_VO, MSGL_ERR, "X11 error: %s\n", msg);
187 mp_msg(MSGT_VO, MSGL_V,
188 "Type: %x, display: %p, resourceid: %lx, serial: %lx\n",
189 event->type, event->display, event->resourceid, event->serial);
190 mp_msg(MSGT_VO, MSGL_V,
191 "Error code: %x, request code: %x, minor code: %x\n",
192 event->error_code, event->request_code, event->minor_code);
194 // abort();
195 //exit_player("X11 error");
196 return 0;
197 #undef MSGLEN
200 void fstype_help(void)
202 mp_tmsg(MSGT_VO, MSGL_INFO, "Available fullscreen layer change modes:\n");
203 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FULL_SCREEN_TYPES\n");
205 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "none",
206 "don't set fullscreen window layer");
207 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer",
208 "use _WIN_LAYER hint with default layer");
209 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer=<0..15>",
210 "use _WIN_LAYER hint with a given layer number");
211 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "netwm",
212 "force NETWM style");
213 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "above",
214 "use _NETWM_STATE_ABOVE hint if available");
215 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "below",
216 "use _NETWM_STATE_BELOW hint if available");
217 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "fullscreen",
218 "use _NETWM_STATE_FULLSCREEN hint if available");
219 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "stays_on_top",
220 "use _NETWM_STATE_STAYS_ON_TOP hint if available");
221 mp_msg(MSGT_VO, MSGL_INFO,
222 "You can also negate the settings with simply putting '-' in the beginning");
223 mp_msg(MSGT_VO, MSGL_INFO, "\n");
226 static void fstype_dump(int fstype)
228 if (fstype)
230 mp_msg(MSGT_VO, MSGL_V, "[x11] Current fstype setting honours");
231 if (fstype & vo_wm_LAYER)
232 mp_msg(MSGT_VO, MSGL_V, " LAYER");
233 if (fstype & vo_wm_FULLSCREEN)
234 mp_msg(MSGT_VO, MSGL_V, " FULLSCREEN");
235 if (fstype & vo_wm_STAYS_ON_TOP)
236 mp_msg(MSGT_VO, MSGL_V, " STAYS_ON_TOP");
237 if (fstype & vo_wm_ABOVE)
238 mp_msg(MSGT_VO, MSGL_V, " ABOVE");
239 if (fstype & vo_wm_BELOW)
240 mp_msg(MSGT_VO, MSGL_V, " BELOW");
241 mp_msg(MSGT_VO, MSGL_V, " X atoms\n");
242 } else
243 mp_msg(MSGT_VO, MSGL_V,
244 "[x11] Current fstype setting doesn't honour any X atoms\n");
247 static int net_wm_support_state_test(struct vo_x11_state *x11, Atom atom)
249 #define NET_WM_STATE_TEST(x) { if (atom == x11->XA_NET_WM_STATE_##x) { mp_msg( MSGT_VO,MSGL_V, "[x11] Detected wm supports " #x " state.\n" ); return vo_wm_##x; } }
251 NET_WM_STATE_TEST(FULLSCREEN);
252 NET_WM_STATE_TEST(ABOVE);
253 NET_WM_STATE_TEST(STAYS_ON_TOP);
254 NET_WM_STATE_TEST(BELOW);
255 return 0;
258 static int x11_get_property(struct vo_x11_state *x11, Atom type, Atom ** args,
259 unsigned long *nitems)
261 int format;
262 unsigned long bytesafter;
264 return Success ==
265 XGetWindowProperty(x11->display, x11->rootwin, type, 0, 16384, False,
266 AnyPropertyType, &type, &format, nitems,
267 &bytesafter, (unsigned char **) args)
268 && *nitems > 0;
271 static int vo_wm_detect(struct vo *vo)
273 struct vo_x11_state *x11 = vo->x11;
274 int i;
275 int wm = 0;
276 unsigned long nitems;
277 Atom *args = NULL;
279 if (WinID >= 0)
280 return 0;
282 // -- supports layers
283 if (x11_get_property(x11, x11->XA_WIN_PROTOCOLS, &args, &nitems))
285 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports layers.\n");
286 for (i = 0; i < nitems; i++)
288 if (args[i] == x11->XA_WIN_LAYER)
290 wm |= vo_wm_LAYER;
291 metacity_hack |= 1;
292 } else
293 /* metacity is the only window manager I know which reports
294 * supporting only the _WIN_LAYER hint in _WIN_PROTOCOLS.
295 * (what's more support for it is broken) */
296 metacity_hack |= 2;
298 XFree(args);
299 if (wm && (metacity_hack == 1))
301 // metacity claims to support layers, but it is not the truth :-)
302 wm ^= vo_wm_LAYER;
303 mp_msg(MSGT_VO, MSGL_V,
304 "[x11] Using workaround for Metacity bugs.\n");
307 // --- netwm
308 if (x11_get_property(x11, x11->XA_NET_SUPPORTED, &args, &nitems))
310 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports NetWM.\n");
311 for (i = 0; i < nitems; i++)
312 wm |= net_wm_support_state_test(vo->x11, args[i]);
313 XFree(args);
316 if (wm == 0)
317 mp_msg(MSGT_VO, MSGL_V, "[x11] Unknown wm type...\n");
318 return wm;
321 #define XA_INIT(x) x11->XA##x = XInternAtom(x11->display, #x, False)
322 static void init_atoms(struct vo_x11_state *x11)
324 XA_INIT(_NET_SUPPORTED);
325 XA_INIT(_NET_WM_STATE);
326 XA_INIT(_NET_WM_STATE_FULLSCREEN);
327 XA_INIT(_NET_WM_STATE_ABOVE);
328 XA_INIT(_NET_WM_STATE_STAYS_ON_TOP);
329 XA_INIT(_NET_WM_STATE_BELOW);
330 XA_INIT(_NET_WM_PID);
331 XA_INIT(_NET_WM_NAME);
332 XA_INIT(_NET_WM_ICON_NAME);
333 XA_INIT(_WIN_PROTOCOLS);
334 XA_INIT(_WIN_LAYER);
335 XA_INIT(_WIN_HINTS);
336 XA_INIT(WM_PROTOCOLS);
337 XA_INIT(WM_DELETE_WINDOW);
338 XA_INIT(UTF8_STRING);
341 void update_xinerama_info(struct vo *vo) {
342 struct MPOpts *opts = vo->opts;
343 xinerama_x = xinerama_y = 0;
344 #ifdef CONFIG_XINERAMA
345 if (xinerama_screen >= -1 && XineramaIsActive(vo->x11->display))
347 int screen = xinerama_screen;
348 XineramaScreenInfo *screens;
349 int num_screens;
351 screens = XineramaQueryScreens(vo->x11->display, &num_screens);
352 if (screen >= num_screens)
353 screen = num_screens - 1;
354 if (screen == -1) {
355 int x = vo->dx + vo->dwidth / 2;
356 int y = vo->dy + vo->dheight / 2;
357 for (screen = num_screens - 1; screen > 0; screen--) {
358 int left = screens[screen].x_org;
359 int right = left + screens[screen].width;
360 int top = screens[screen].y_org;
361 int bottom = top + screens[screen].height;
362 if (left <= x && x <= right && top <= y && y <= bottom)
363 break;
366 if (screen < 0)
367 screen = 0;
368 opts->vo_screenwidth = screens[screen].width;
369 opts->vo_screenheight = screens[screen].height;
370 xinerama_x = screens[screen].x_org;
371 xinerama_y = screens[screen].y_org;
373 XFree(screens);
375 #endif
376 aspect_save_screenres(vo, opts->vo_screenwidth, opts->vo_screenheight);
379 int vo_init(struct vo *vo)
381 struct MPOpts *opts = vo->opts;
382 struct vo_x11_state *x11 = vo->x11;
383 // int mScreen;
384 int depth, bpp;
385 unsigned int mask;
387 // char * DisplayName = ":0.0";
388 // Display * mDisplay;
389 XImage *mXImage = NULL;
391 // Window mRootWin;
392 XWindowAttributes attribs;
393 char *dispName;
395 if (vo_rootwin)
396 WinID = 0; // use root window
398 if (x11->depthonscreen)
400 saver_off(x11->display);
401 return 1; // already called
404 XSetErrorHandler(x11_errorhandler);
406 #if 0
407 if (!mDisplayName)
408 if (!(mDisplayName = getenv("DISPLAY")))
409 mDisplayName = strdup(":0.0");
410 #else
411 dispName = XDisplayName(mDisplayName);
412 #endif
414 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
416 x11->display = XOpenDisplay(dispName);
417 if (!x11->display)
419 mp_msg(MSGT_VO, MSGL_ERR,
420 "vo: couldn't open the X11 display (%s)!\n", dispName);
421 return 0;
423 x11->screen = DefaultScreen(x11->display); // screen ID
424 x11->rootwin = RootWindow(x11->display, x11->screen); // root window ID
426 x11->xim = XOpenIM(x11->display, NULL, NULL, NULL);
428 init_atoms(vo->x11);
430 #ifdef CONFIG_XF86VM
432 int clock;
434 XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline);
435 if (!opts->vo_screenwidth)
436 opts->vo_screenwidth = modeline.hdisplay;
437 if (!opts->vo_screenheight)
438 opts->vo_screenheight = modeline.vdisplay;
440 #endif
442 if (!opts->vo_screenwidth)
443 opts->vo_screenwidth = DisplayWidth(x11->display, x11->screen);
444 if (!opts->vo_screenheight)
445 opts->vo_screenheight = DisplayHeight(x11->display, x11->screen);
447 // get color depth (from root window, or the best visual):
448 XGetWindowAttributes(x11->display, x11->rootwin, &attribs);
449 depth = attribs.depth;
451 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
453 Visual *visual;
455 depth = vo_find_depth_from_visuals(x11->display, x11->screen, &visual);
456 if (depth != -1)
457 mXImage = XCreateImage(x11->display, visual, depth, ZPixmap,
458 0, NULL, 1, 1, 8, 1);
459 } else
460 mXImage =
461 XGetImage(x11->display, x11->rootwin, 0, 0, 1, 1, AllPlanes, ZPixmap);
463 x11->depthonscreen = depth; // display depth on screen
465 // get bits/pixel from XImage structure:
466 if (mXImage == NULL)
468 mask = 0;
469 } else
472 * for the depth==24 case, the XImage structures might use
473 * 24 or 32 bits of data per pixel. The x11->depthonscreen
474 * field stores the amount of data per pixel in the
475 * XImage structure!
477 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
479 bpp = mXImage->bits_per_pixel;
480 if ((x11->depthonscreen + 7) / 8 != (bpp + 7) / 8)
481 x11->depthonscreen = bpp; // by A'rpi
482 mask =
483 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
484 mp_msg(MSGT_VO, MSGL_V,
485 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
486 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
487 XDestroyImage(mXImage);
489 if (((x11->depthonscreen + 7) / 8) == 2)
491 if (mask == 0x7FFF)
492 x11->depthonscreen = 15;
493 else if (mask == 0xFFFF)
494 x11->depthonscreen = 16;
496 // XCloseDisplay( mDisplay );
497 /* slightly improved local display detection AST */
498 if (strncmp(dispName, "unix:", 5) == 0)
499 dispName += 4;
500 else if (strncmp(dispName, "localhost:", 10) == 0)
501 dispName += 9;
502 if (*dispName == ':' && atoi(dispName + 1) < 10)
503 x11->display_is_local = 1;
504 else
505 x11->display_is_local = 0;
506 mp_msg(MSGT_VO, MSGL_V,
507 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
508 opts->vo_screenwidth, opts->vo_screenheight, depth, x11->depthonscreen,
509 dispName, x11->display_is_local ? "local" : "remote");
511 x11->wm_type = vo_wm_detect(vo);
513 x11->fs_type = vo_x11_get_fs_type(x11->wm_type);
515 fstype_dump(x11->fs_type);
517 saver_off(x11->display);
518 return 1;
521 void vo_uninit(struct vo_x11_state *x11)
523 if (!x11->display)
525 mp_msg(MSGT_VO, MSGL_V,
526 "vo: x11 uninit called but X11 not initialized..\n");
527 } else {
528 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
529 if (x11->xim)
530 XCloseIM(x11->xim);
531 XSetErrorHandler(NULL);
532 XCloseDisplay(x11->display);
533 x11->depthonscreen = 0;
534 x11->display = NULL;
536 talloc_free(x11);
539 #include "wskeys.h"
541 static const struct mp_keymap keymap[] = {
542 // special keys
543 {wsPause, KEY_PAUSE}, {wsEscape, KEY_ESC}, {wsBackSpace, KEY_BS},
544 {wsTab, KEY_TAB}, {wsEnter, KEY_ENTER},
545 {XK_Menu, KEY_MENU}, {XK_Print, KEY_PRINT},
547 // cursor keys
548 {wsLeft, KEY_LEFT}, {wsRight, KEY_RIGHT}, {wsUp, KEY_UP}, {wsDown, KEY_DOWN},
550 // navigation block
551 {wsInsert, KEY_INSERT}, {wsDelete, KEY_DELETE}, {wsHome, KEY_HOME}, {wsEnd, KEY_END},
552 {wsPageUp, KEY_PAGE_UP}, {wsPageDown, KEY_PAGE_DOWN},
554 // F-keys
555 {wsF1, KEY_F+1}, {wsF2, KEY_F+2}, {wsF3, KEY_F+3}, {wsF4, KEY_F+4},
556 {wsF5, KEY_F+5}, {wsF6, KEY_F+6}, {wsF7, KEY_F+7}, {wsF8, KEY_F+8},
557 {wsF9, KEY_F+9}, {wsF10, KEY_F+10}, {wsF11, KEY_F+11}, {wsF12, KEY_F+12},
559 // numpad independent of numlock
560 {wsGrayMinus, '-'}, {wsGrayPlus, '+'}, {wsGrayMul, '*'}, {wsGrayDiv, '/'},
561 {wsGrayEnter, KEY_KPENTER},
563 // numpad with numlock
564 {wsGray0, KEY_KP0}, {wsGray1, KEY_KP1}, {wsGray2, KEY_KP2},
565 {wsGray3, KEY_KP3}, {wsGray4, KEY_KP4}, {wsGray5, KEY_KP5},
566 {wsGray6, KEY_KP6}, {wsGray7, KEY_KP7}, {wsGray8, KEY_KP8},
567 {wsGray9, KEY_KP9}, {wsGrayDecimal, KEY_KPDEC},
568 {wsGraySeparator, KEY_KPDEC},
570 // numpad without numlock
571 {wsGrayInsert, KEY_KPINS}, {wsGrayEnd, KEY_KP1}, {wsGrayDown, KEY_KP2},
572 {wsGrayPgDn, KEY_KP3}, {wsGrayLeft, KEY_KP4}, {wsGray5Dup, KEY_KP5},
573 {wsGrayRight, KEY_KP6}, {wsGrayHome, KEY_KP7}, {wsGrayUp, KEY_KP8},
574 {wsGrayPgUp, KEY_KP9}, {wsGrayDelete, KEY_KPDEL},
576 #ifdef XF86XK_AudioPause
577 {XF86XK_MenuKB, KEY_MENU},
578 {XF86XK_AudioPlay, KEY_PLAY}, {XF86XK_AudioPause, KEY_PAUSE}, {XF86XK_AudioStop, KEY_STOP},
579 {XF86XK_AudioPrev, KEY_PREV}, {XF86XK_AudioNext, KEY_NEXT},
580 {XF86XK_AudioMute, KEY_MUTE}, {XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN}, {XF86XK_AudioRaiseVolume, KEY_VOLUME_UP},
581 #endif
583 {0, 0}
586 static int vo_x11_lookupkey(int key)
588 static const char *passthrough_keys = " -+*/<>`~!@#$%^&()_{}:;\"\',.?\\|=[]";
589 int mpkey = 0;
590 if ((key >= 'a' && key <= 'z') ||
591 (key >= 'A' && key <= 'Z') ||
592 (key >= '0' && key <= '9') ||
593 (key > 0 && key < 256 && strchr(passthrough_keys, key)))
594 mpkey = key;
596 if (!mpkey)
597 mpkey = lookup_keymap_table(keymap, key);
599 return mpkey;
603 // ----- Motif header: -------
605 #define MWM_HINTS_FUNCTIONS (1L << 0)
606 #define MWM_HINTS_DECORATIONS (1L << 1)
607 #define MWM_HINTS_INPUT_MODE (1L << 2)
608 #define MWM_HINTS_STATUS (1L << 3)
610 #define MWM_FUNC_ALL (1L << 0)
611 #define MWM_FUNC_RESIZE (1L << 1)
612 #define MWM_FUNC_MOVE (1L << 2)
613 #define MWM_FUNC_MINIMIZE (1L << 3)
614 #define MWM_FUNC_MAXIMIZE (1L << 4)
615 #define MWM_FUNC_CLOSE (1L << 5)
617 #define MWM_DECOR_ALL (1L << 0)
618 #define MWM_DECOR_BORDER (1L << 1)
619 #define MWM_DECOR_RESIZEH (1L << 2)
620 #define MWM_DECOR_TITLE (1L << 3)
621 #define MWM_DECOR_MENU (1L << 4)
622 #define MWM_DECOR_MINIMIZE (1L << 5)
623 #define MWM_DECOR_MAXIMIZE (1L << 6)
625 #define MWM_INPUT_MODELESS 0
626 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
627 #define MWM_INPUT_SYSTEM_MODAL 2
628 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
629 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
631 #define MWM_TEAROFF_WINDOW (1L<<0)
633 typedef struct
635 long flags;
636 long functions;
637 long decorations;
638 long input_mode;
639 long state;
640 } MotifWmHints;
642 static MotifWmHints vo_MotifWmHints;
643 static Atom vo_MotifHints = None;
645 void vo_x11_decoration(struct vo *vo, int d)
647 struct vo_x11_state *x11 = vo->x11;
648 Atom mtype;
649 int mformat;
650 unsigned long mn, mb;
652 if (!WinID)
653 return;
655 if (vo_fsmode & 8)
657 XSetTransientForHint(x11->display, x11->window,
658 RootWindow(x11->display, x11->screen));
661 vo_MotifHints = XInternAtom(x11->display, "_MOTIF_WM_HINTS", 0);
662 if (vo_MotifHints != None)
664 if (!d)
666 MotifWmHints *mhints = NULL;
668 XGetWindowProperty(x11->display, x11->window,
669 vo_MotifHints, 0, 20, False,
670 vo_MotifHints, &mtype, &mformat, &mn,
671 &mb, (unsigned char **) &mhints);
672 if (mhints)
674 if (mhints->flags & MWM_HINTS_DECORATIONS)
675 x11->olddecor = mhints->decorations;
676 if (mhints->flags & MWM_HINTS_FUNCTIONS)
677 x11->oldfuncs = mhints->functions;
678 XFree(mhints);
682 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
683 vo_MotifWmHints.flags =
684 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
685 if (d)
687 vo_MotifWmHints.functions = x11->oldfuncs;
688 d = x11->olddecor;
690 #if 0
691 vo_MotifWmHints.decorations =
692 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
693 #else
694 vo_MotifWmHints.decorations =
695 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
696 #endif
697 XChangeProperty(x11->display, x11->window, vo_MotifHints,
698 vo_MotifHints, 32,
699 PropModeReplace,
700 (unsigned char *) &vo_MotifWmHints,
701 (vo_fsmode & 4) ? 4 : 5);
705 void vo_x11_classhint(struct vo *vo, Window window, const char *name)
707 struct MPOpts *opts = vo->opts;
708 struct vo_x11_state *x11 = vo->x11;
709 XClassHint wmClass;
710 pid_t pid = getpid();
712 wmClass.res_name = opts->vo_winname ? opts->vo_winname : (char *)name;
713 wmClass.res_class = "mplayer2";
714 XSetClassHint(x11->display, window, &wmClass);
715 XChangeProperty(x11->display, window, x11->XA_NET_WM_PID, XA_CARDINAL,
716 32, PropModeReplace, (unsigned char *) &pid, 1);
719 void vo_x11_uninit(struct vo *vo)
721 struct vo_x11_state *x11 = vo->x11;
722 saver_on(x11->display);
723 if (x11->window != None)
724 vo_showcursor(x11->display, x11->window);
726 if (x11->f_gc != None)
728 XFreeGC(vo->x11->display, x11->f_gc);
729 x11->f_gc = None;
732 if (x11->vo_gc != None)
734 XFreeGC(vo->x11->display, x11->vo_gc);
735 x11->vo_gc = None;
737 if (x11->window != None)
739 XClearWindow(x11->display, x11->window);
740 if (WinID < 0)
742 XEvent xev;
744 if (x11->xic)
745 XDestroyIC(x11->xic);
746 x11->xic = NULL;
748 XUnmapWindow(x11->display, x11->window);
749 XSelectInput(x11->display, x11->window, StructureNotifyMask);
750 XDestroyWindow(x11->display, x11->window);
753 XNextEvent(x11->display, &xev);
755 while (xev.type != DestroyNotify
756 || xev.xdestroywindow.event != x11->window);
758 x11->window = None;
760 vo_fs = 0;
761 x11->vo_old_width = x11->vo_old_height = 0;
762 x11->last_video_width = 0;
763 x11->last_video_height = 0;
764 x11->size_changed_during_fs = false;
768 static int check_resize(struct vo *vo)
770 int old_w = vo->dwidth, old_h = vo->dheight;
771 int old_x = vo->dx, old_y = vo->dy;
772 int rc = 0;
773 vo_x11_update_geometry(vo, true);
774 if (vo->dwidth != old_w || vo->dheight != old_h)
775 rc |= VO_EVENT_RESIZE;
776 if (vo->dx != old_x || vo->dy != old_y)
777 rc |= VO_EVENT_MOVE;
778 return rc;
781 int vo_x11_check_events(struct vo *vo)
783 struct vo_x11_state *x11 = vo->x11;
784 Display *display = vo->x11->display;
785 int ret = 0;
786 XEvent Event;
788 if (x11->vo_mouse_autohide && x11->mouse_waiting_hide &&
789 (GetTimerMS() - x11->mouse_timer >= 1000)) {
790 vo_hidecursor(display, x11->window);
791 x11->mouse_waiting_hide = 0;
794 if (WinID > 0)
795 ret |= check_resize(vo);
796 while (XPending(display))
798 XNextEvent(display, &Event);
799 // printf("\rEvent.type=%X \n",Event.type);
800 switch (Event.type)
802 case Expose:
803 ret |= VO_EVENT_EXPOSE;
804 break;
805 case ConfigureNotify:
806 if (x11->window == None)
807 break;
808 ret |= check_resize(vo);
809 break;
810 case KeyPress:
812 char buf[100];
813 KeySym keySym = 0;
814 int modifiers = 0;
815 if (Event.xkey.state & ShiftMask)
816 modifiers |= KEY_MODIFIER_SHIFT;
817 if (Event.xkey.state & ControlMask)
818 modifiers |= KEY_MODIFIER_CTRL;
819 if (Event.xkey.state & Mod1Mask)
820 modifiers |= KEY_MODIFIER_ALT;
821 if (Event.xkey.state & Mod4Mask)
822 modifiers |= KEY_MODIFIER_META;
823 if (x11->xic) {
824 Status status;
825 int len = Xutf8LookupString(x11->xic, &Event.xkey, buf,
826 sizeof(buf), &keySym,
827 &status);
828 int mpkey = vo_x11_lookupkey(keySym);
829 if (mpkey) {
830 mplayer_put_key(vo->key_fifo, mpkey | modifiers);
831 } else if (status == XLookupChars
832 || status == XLookupBoth)
834 struct bstr t = { buf, len };
835 mplayer_put_key_utf8(vo->key_fifo, modifiers, t);
837 } else {
838 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
839 &x11->compose_status);
840 int mpkey = vo_x11_lookupkey(keySym);
841 if (mpkey)
842 mplayer_put_key(vo->key_fifo, mpkey | modifiers);
844 ret |= VO_EVENT_KEYPRESS;
846 break;
847 case MotionNotify:
848 vo_mouse_movement(vo, Event.xmotion.x, Event.xmotion.y);
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 mplayer_put_key(vo->key_fifo,
865 (MOUSE_BTN0 + Event.xbutton.button - 1)
866 | MP_KEY_DOWN);
867 break;
868 case ButtonRelease:
869 if (x11->vo_mouse_autohide)
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 x11->vo_mouse_autohide = 1;
1181 vo->event_fd = ConnectionNumber(x11->display);
1184 void vo_x11_clearwindow_part(struct vo *vo, Window vo_window,
1185 int img_width, int img_height)
1187 struct vo_x11_state *x11 = vo->x11;
1188 Display *mDisplay = vo->x11->display;
1189 int u_dheight, u_dwidth, left_ov, left_ov2;
1191 if (x11->f_gc == None)
1192 return;
1194 u_dheight = vo->dheight;
1195 u_dwidth = vo->dwidth;
1196 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1197 return;
1199 left_ov = (u_dheight - img_height) / 2;
1200 left_ov2 = (u_dwidth - img_width) / 2;
1202 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, 0, u_dwidth, left_ov);
1203 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, u_dheight - left_ov - 1,
1204 u_dwidth, left_ov + 1);
1206 if (u_dwidth > img_width)
1208 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, left_ov, left_ov2,
1209 img_height);
1210 XFillRectangle(mDisplay, vo_window, x11->f_gc, u_dwidth - left_ov2 - 1,
1211 left_ov, left_ov2 + 1, img_height);
1214 XFlush(mDisplay);
1217 void vo_x11_clearwindow(struct vo *vo, Window vo_window)
1219 struct vo_x11_state *x11 = vo->x11;
1220 struct MPOpts *opts = vo->opts;
1221 if (x11->f_gc == None)
1222 return;
1223 XFillRectangle(x11->display, vo_window, x11->f_gc, 0, 0,
1224 opts->vo_screenwidth, opts->vo_screenheight);
1226 XFlush(x11->display);
1230 void vo_x11_setlayer(struct vo *vo, Window vo_window, int layer)
1232 struct vo_x11_state *x11 = vo->x11;
1233 if (WinID >= 0)
1234 return;
1236 if (x11->fs_type & vo_wm_LAYER)
1238 XClientMessageEvent xev;
1240 if (!x11->orig_layer)
1241 x11->orig_layer = vo_x11_get_gnome_layer(x11, vo_window);
1243 memset(&xev, 0, sizeof(xev));
1244 xev.type = ClientMessage;
1245 xev.display = x11->display;
1246 xev.window = vo_window;
1247 xev.message_type = x11->XA_WIN_LAYER;
1248 xev.format = 32;
1249 xev.data.l[0] = layer ? fs_layer : x11->orig_layer; // if not fullscreen, stay on default layer
1250 xev.data.l[1] = CurrentTime;
1251 mp_msg(MSGT_VO, MSGL_V,
1252 "[x11] Layered style stay on top (layer %ld).\n",
1253 xev.data.l[0]);
1254 XSendEvent(x11->display, x11->rootwin, False, SubstructureNotifyMask,
1255 (XEvent *) & xev);
1256 } else if (x11->fs_type & vo_wm_NETWM)
1258 XClientMessageEvent xev;
1259 char *state;
1261 memset(&xev, 0, sizeof(xev));
1262 xev.type = ClientMessage;
1263 xev.message_type = x11->XA_NET_WM_STATE;
1264 xev.display = x11->display;
1265 xev.window = vo_window;
1266 xev.format = 32;
1267 xev.data.l[0] = layer;
1269 if (x11->fs_type & vo_wm_STAYS_ON_TOP)
1270 xev.data.l[1] = x11->XA_NET_WM_STATE_STAYS_ON_TOP;
1271 else if (x11->fs_type & vo_wm_ABOVE)
1272 xev.data.l[1] = x11->XA_NET_WM_STATE_ABOVE;
1273 else if (x11->fs_type & vo_wm_FULLSCREEN)
1274 xev.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
1275 else if (x11->fs_type & vo_wm_BELOW)
1276 // This is not fallback. We can safely assume that the situation
1277 // where only NETWM_STATE_BELOW is supported doesn't exist.
1278 xev.data.l[1] = x11->XA_NET_WM_STATE_BELOW;
1280 XSendEvent(x11->display, x11->rootwin, False, SubstructureRedirectMask,
1281 (XEvent *) & xev);
1282 state = XGetAtomName(x11->display, xev.data.l[1]);
1283 mp_msg(MSGT_VO, MSGL_V,
1284 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1285 layer, state);
1286 XFree(state);
1290 static int vo_x11_get_fs_type(int supported)
1292 int i;
1293 int type = supported;
1295 if (vo_fstype_list)
1297 for (i = 0; vo_fstype_list[i]; i++)
1299 int neg = 0;
1300 char *arg = vo_fstype_list[i];
1302 if (vo_fstype_list[i][0] == '-')
1304 neg = 1;
1305 arg = vo_fstype_list[i] + 1;
1308 if (!strncmp(arg, "layer", 5))
1310 if (!neg && (arg[5] == '='))
1312 char *endptr = NULL;
1313 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1315 if (endptr && *endptr == '\0' && layer >= 0
1316 && layer <= 15)
1317 fs_layer = layer;
1319 if (neg)
1320 type &= ~vo_wm_LAYER;
1321 else
1322 type |= vo_wm_LAYER;
1323 } else if (!strcmp(arg, "above"))
1325 if (neg)
1326 type &= ~vo_wm_ABOVE;
1327 else
1328 type |= vo_wm_ABOVE;
1329 } else if (!strcmp(arg, "fullscreen"))
1331 if (neg)
1332 type &= ~vo_wm_FULLSCREEN;
1333 else
1334 type |= vo_wm_FULLSCREEN;
1335 } else if (!strcmp(arg, "stays_on_top"))
1337 if (neg)
1338 type &= ~vo_wm_STAYS_ON_TOP;
1339 else
1340 type |= vo_wm_STAYS_ON_TOP;
1341 } else if (!strcmp(arg, "below"))
1343 if (neg)
1344 type &= ~vo_wm_BELOW;
1345 else
1346 type |= vo_wm_BELOW;
1347 } else if (!strcmp(arg, "netwm"))
1349 if (neg)
1350 type &= ~vo_wm_NETWM;
1351 else
1352 type |= vo_wm_NETWM;
1353 } else if (!strcmp(arg, "none"))
1354 type = 0; // clear; keep parsing
1358 return type;
1362 * \brief update vo->dx, vo->dy, vo->dwidth and vo->dheight with current values of vo->x11->window
1363 * \return returns current color depth of vo->x11->window
1365 int vo_x11_update_geometry(struct vo *vo, bool update_pos)
1367 struct vo_x11_state *x11 = vo->x11;
1368 unsigned depth, w, h;
1369 int dummy_int;
1370 Window dummy_win;
1371 XGetGeometry(x11->display, x11->window, &dummy_win, &dummy_int, &dummy_int,
1372 &w, &h, &dummy_int, &depth);
1373 if (w <= INT_MAX && h <= INT_MAX) {
1374 vo->dwidth = w;
1375 vo->dheight = h;
1377 if (update_pos)
1378 XTranslateCoordinates(x11->display, x11->window, x11->rootwin, 0, 0,
1379 &vo->dx, &vo->dy, &dummy_win);
1381 return depth <= INT_MAX ? depth : 0;
1384 void vo_x11_fullscreen(struct vo *vo)
1386 struct MPOpts *opts = vo->opts;
1387 struct vo_x11_state *x11 = vo->x11;
1388 int x, y, w, h;
1389 x = x11->vo_old_x;
1390 y = x11->vo_old_y;
1391 w = x11->vo_old_width;
1392 h = x11->vo_old_height;
1394 if (WinID >= 0) {
1395 vo_fs = !vo_fs;
1396 return;
1398 if (x11->fs_flip)
1399 return;
1401 if (vo_fs)
1403 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1404 vo_fs = VO_FALSE;
1405 if (x11->size_changed_during_fs && (x11->fs_type & vo_wm_FULLSCREEN))
1406 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, x11->last_video_width,
1407 x11->last_video_height);
1408 x11->size_changed_during_fs = false;
1409 } else
1411 // win->fs
1412 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1414 vo_fs = VO_TRUE;
1415 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1417 x11->vo_old_x = vo->dx;
1418 x11->vo_old_y = vo->dy;
1419 x11->vo_old_width = vo->dwidth;
1420 x11->vo_old_height = vo->dheight;
1422 update_xinerama_info(vo);
1423 x = xinerama_x;
1424 y = xinerama_y;
1425 w = opts->vo_screenwidth;
1426 h = opts->vo_screenheight;
1429 long dummy;
1431 XGetWMNormalHints(x11->display, x11->window, &x11->vo_hint, &dummy);
1432 if (!(x11->vo_hint.flags & PWinGravity))
1433 x11->old_gravity = NorthWestGravity;
1434 else
1435 x11->old_gravity = x11->vo_hint.win_gravity;
1437 if (x11->wm_type == 0 && !(vo_fsmode & 16))
1439 XUnmapWindow(x11->display, x11->window); // required for MWM
1440 XWithdrawWindow(x11->display, x11->window, x11->screen);
1441 x11->fs_flip = 1;
1444 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1446 vo_x11_decoration(vo, vo_border && !vo_fs);
1447 vo_x11_sizehint(vo, x, y, w, h, 0);
1448 vo_x11_setlayer(vo, x11->window, vo_fs);
1451 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1453 /* some WMs lose ontop after fullscreen */
1454 if ((!(vo_fs)) & opts->vo_ontop)
1455 vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1457 XMapRaised(x11->display, x11->window);
1458 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1459 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1460 XRaiseWindow(x11->display, x11->window);
1461 XFlush(x11->display);
1464 void vo_x11_ontop(struct vo *vo)
1466 struct MPOpts *opts = vo->opts;
1467 opts->vo_ontop = !opts->vo_ontop;
1469 vo_x11_setlayer(vo, vo->x11->window, opts->vo_ontop);
1472 void vo_x11_border(struct vo *vo)
1474 vo_border = !vo_border;
1475 vo_x11_decoration(vo, vo_border && !vo_fs);
1479 * XScreensaver stuff
1482 static int screensaver_off;
1483 static unsigned int time_last;
1485 void xscreensaver_heartbeat(struct vo_x11_state *x11)
1487 unsigned int time = GetTimerMS();
1489 if (x11->display && screensaver_off && (time - time_last) > 30000)
1491 time_last = time;
1493 XResetScreenSaver(x11->display);
1497 static int xss_suspend(Display *mDisplay, Bool suspend)
1499 #ifndef CONFIG_XSS
1500 return 0;
1501 #else
1502 int event, error, major, minor;
1503 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1504 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1505 return 0;
1506 if (major < 1 || (major == 1 && minor < 1))
1507 return 0;
1508 XScreenSaverSuspend(mDisplay, suspend);
1509 return 1;
1510 #endif
1514 * End of XScreensaver stuff
1517 static void saver_on(Display * mDisplay)
1520 if (!screensaver_off)
1521 return;
1522 screensaver_off = 0;
1523 if (xss_suspend(mDisplay, False))
1524 return;
1525 #ifdef CONFIG_XDPMS
1526 if (dpms_disabled)
1528 int nothing;
1529 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1531 if (!DPMSEnable(mDisplay))
1532 { // restoring power saving settings
1533 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1534 } else
1536 // DPMS does not seem to be enabled unless we call DPMSInfo
1537 BOOL onoff;
1538 CARD16 state;
1540 DPMSForceLevel(mDisplay, DPMSModeOn);
1541 DPMSInfo(mDisplay, &state, &onoff);
1542 if (onoff)
1544 mp_msg(MSGT_VO, MSGL_V,
1545 "Successfully enabled DPMS\n");
1546 } else
1548 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1552 dpms_disabled = 0;
1554 #endif
1557 static void saver_off(Display * mDisplay)
1559 int nothing;
1561 if (screensaver_off)
1562 return;
1563 screensaver_off = 1;
1564 if (xss_suspend(mDisplay, True))
1565 return;
1566 #ifdef CONFIG_XDPMS
1567 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1569 BOOL onoff;
1570 CARD16 state;
1572 DPMSInfo(mDisplay, &state, &onoff);
1573 if (onoff)
1575 Status stat;
1577 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1578 dpms_disabled = 1;
1579 stat = DPMSDisable(mDisplay); // monitor powersave off
1580 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1583 #endif
1586 static XErrorHandler old_handler = NULL;
1587 static int selectinput_err = 0;
1588 static int x11_selectinput_errorhandler(Display * display,
1589 XErrorEvent * event)
1591 if (event->error_code == BadAccess)
1593 selectinput_err = 1;
1594 mp_msg(MSGT_VO, MSGL_ERR,
1595 "X11 error: BadAccess during XSelectInput Call\n");
1596 mp_msg(MSGT_VO, MSGL_ERR,
1597 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1598 /* If you think MPlayer should shutdown with this error,
1599 * comment out the following line */
1600 return 0;
1602 if (old_handler != NULL)
1603 old_handler(display, event);
1604 else
1605 x11_errorhandler(display, event);
1606 return 0;
1609 void vo_x11_selectinput_witherr(Display * display, Window w,
1610 long event_mask)
1612 XSync(display, False);
1613 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1614 selectinput_err = 0;
1615 if (vo_nomouse_input)
1617 XSelectInput(display, w,
1618 event_mask &
1619 (~(ButtonPressMask | ButtonReleaseMask)));
1620 } else
1622 XSelectInput(display, w, event_mask);
1624 XSync(display, False);
1625 XSetErrorHandler(old_handler);
1626 if (selectinput_err)
1628 mp_msg(MSGT_VO, MSGL_ERR,
1629 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1630 XSelectInput(display, w,
1631 event_mask &
1633 (ButtonPressMask | ButtonReleaseMask |
1634 PointerMotionMask)));
1638 #ifdef CONFIG_XF86VM
1639 void vo_vm_switch(struct vo *vo)
1641 struct vo_x11_state *x11 = vo->x11;
1642 struct MPOpts *opts = vo->opts;
1643 Display *mDisplay = x11->display;
1644 int vm_event, vm_error;
1645 int vm_ver, vm_rev;
1646 int i, j, have_vm = 0;
1647 int X = vo->dwidth, Y = vo->dheight;
1648 int modeline_width, modeline_height;
1650 int modecount;
1652 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1654 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1655 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1656 vm_rev);
1657 have_vm = 1;
1658 } else {
1659 mp_msg(MSGT_VO, MSGL_WARN,
1660 "XF86VidMode extension not available.\n");
1663 if (have_vm)
1665 if (vidmodes == NULL)
1666 XF86VidModeGetAllModeLines(mDisplay, x11->screen, &modecount,
1667 &vidmodes);
1668 j = 0;
1669 modeline_width = vidmodes[0]->hdisplay;
1670 modeline_height = vidmodes[0]->vdisplay;
1672 for (i = 1; i < modecount; i++)
1673 if ((vidmodes[i]->hdisplay >= X)
1674 && (vidmodes[i]->vdisplay >= Y))
1675 if ((vidmodes[i]->hdisplay <= modeline_width)
1676 && (vidmodes[i]->vdisplay <= modeline_height))
1678 modeline_width = vidmodes[i]->hdisplay;
1679 modeline_height = vidmodes[i]->vdisplay;
1680 j = i;
1683 mp_tmsg(MSGT_VO, MSGL_INFO, "XF86VM: Selected video mode %dx%d for image size %dx%d.\n",
1684 modeline_width, modeline_height, X, Y);
1685 XF86VidModeLockModeSwitch(mDisplay, x11->screen, 0);
1686 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1687 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1689 // FIXME: all this is more of a hack than proper solution
1690 X = (opts->vo_screenwidth - modeline_width) / 2;
1691 Y = (opts->vo_screenheight - modeline_height) / 2;
1692 XF86VidModeSetViewPort(mDisplay, x11->screen, X, Y);
1693 vo->dx = X;
1694 vo->dy = Y;
1695 vo->dwidth = modeline_width;
1696 vo->dheight = modeline_height;
1697 aspect_save_screenres(vo, modeline_width, modeline_height);
1701 void vo_vm_close(struct vo *vo)
1703 Display *dpy = vo->x11->display;
1704 struct MPOpts *opts = vo->opts;
1705 if (vidmodes != NULL)
1707 int i, modecount;
1709 free(vidmodes);
1710 vidmodes = NULL;
1711 XF86VidModeGetAllModeLines(dpy, vo->x11->screen, &modecount,
1712 &vidmodes);
1713 for (i = 0; i < modecount; i++)
1714 if ((vidmodes[i]->hdisplay == opts->vo_screenwidth)
1715 && (vidmodes[i]->vdisplay == opts->vo_screenheight))
1717 mp_msg(MSGT_VO, MSGL_INFO,
1718 "Returning to original mode %dx%d\n",
1719 opts->vo_screenwidth, opts->vo_screenheight);
1720 break;
1723 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1724 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1725 free(vidmodes);
1726 vidmodes = NULL;
1730 double vo_vm_get_fps(struct vo *vo)
1732 struct vo_x11_state *x11 = vo->x11;
1733 int clock;
1734 XF86VidModeModeLine modeline;
1735 if (!XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline))
1736 return 0;
1737 if (modeline.privsize)
1738 XFree(modeline.private);
1739 return 1e3 * clock / modeline.htotal / modeline.vtotal;
1741 #endif
1743 #endif /* X11_FULLSCREEN */
1747 * Scan the available visuals on this Display/Screen. Try to find
1748 * the 'best' available TrueColor visual that has a decent color
1749 * depth (at least 15bit). If there are multiple visuals with depth
1750 * >= 15bit, we prefer visuals with a smaller color depth.
1752 int vo_find_depth_from_visuals(Display * dpy, int screen,
1753 Visual ** visual_return)
1755 XVisualInfo visual_tmpl;
1756 XVisualInfo *visuals;
1757 int nvisuals, i;
1758 int bestvisual = -1;
1759 int bestvisual_depth = -1;
1761 visual_tmpl.screen = screen;
1762 visual_tmpl.class = TrueColor;
1763 visuals = XGetVisualInfo(dpy,
1764 VisualScreenMask | VisualClassMask,
1765 &visual_tmpl, &nvisuals);
1766 if (visuals != NULL)
1768 for (i = 0; i < nvisuals; i++)
1770 mp_msg(MSGT_VO, MSGL_V,
1771 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1772 visuals[i].visualid, visuals[i].depth,
1773 visuals[i].red_mask, visuals[i].green_mask,
1774 visuals[i].blue_mask);
1776 * Save the visual index and its depth, if this is the first
1777 * truecolor visul, or a visual that is 'preferred' over the
1778 * previous 'best' visual.
1780 if (bestvisual_depth == -1
1781 || (visuals[i].depth >= 15
1782 && (visuals[i].depth < bestvisual_depth
1783 || bestvisual_depth < 15)))
1785 bestvisual = i;
1786 bestvisual_depth = visuals[i].depth;
1790 if (bestvisual != -1 && visual_return != NULL)
1791 *visual_return = visuals[bestvisual].visual;
1793 XFree(visuals);
1795 return bestvisual_depth;
1799 static Colormap cmap = None;
1800 static XColor cols[256];
1801 static int cm_size, red_mask, green_mask, blue_mask;
1804 Colormap vo_x11_create_colormap(struct vo *vo, XVisualInfo *vinfo)
1806 struct vo_x11_state *x11 = vo->x11;
1807 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1809 if (vinfo->class != DirectColor)
1810 return XCreateColormap(x11->display, x11->rootwin, vinfo->visual,
1811 AllocNone);
1813 /* can this function get called twice or more? */
1814 if (cmap)
1815 return cmap;
1816 cm_size = vinfo->colormap_size;
1817 red_mask = vinfo->red_mask;
1818 green_mask = vinfo->green_mask;
1819 blue_mask = vinfo->blue_mask;
1820 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1821 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1822 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1823 rvu = 65536ull * ru / (red_mask + ru);
1824 gvu = 65536ull * gu / (green_mask + gu);
1825 bvu = 65536ull * bu / (blue_mask + bu);
1826 r = g = b = 0;
1827 rv = gv = bv = 0;
1828 m = DoRed | DoGreen | DoBlue;
1829 for (k = 0; k < cm_size; k++)
1831 int t;
1833 cols[k].pixel = r | g | b;
1834 cols[k].red = rv;
1835 cols[k].green = gv;
1836 cols[k].blue = bv;
1837 cols[k].flags = m;
1838 t = (r + ru) & red_mask;
1839 if (t < r)
1840 m &= ~DoRed;
1841 r = t;
1842 t = (g + gu) & green_mask;
1843 if (t < g)
1844 m &= ~DoGreen;
1845 g = t;
1846 t = (b + bu) & blue_mask;
1847 if (t < b)
1848 m &= ~DoBlue;
1849 b = t;
1850 rv += rvu;
1851 gv += gvu;
1852 bv += bvu;
1854 cmap = XCreateColormap(x11->display, x11->rootwin, vinfo->visual, AllocAll);
1855 XStoreColors(x11->display, cmap, cols, cm_size);
1856 return cmap;
1860 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1861 * hue and red/green/blue intensity, but we cannot do saturation.
1862 * Currently only gamma, brightness and contrast are implemented.
1863 * Is there sufficient interest for hue and/or red/green/blue intensity?
1865 /* these values have range [-100,100] and are initially 0 */
1866 static int vo_gamma = 0;
1867 static int vo_brightness = 0;
1868 static int vo_contrast = 0;
1870 static int transform_color(float val,
1871 float brightness, float contrast, float gamma) {
1872 float s = pow(val, gamma);
1873 s = (s - 0.5) * contrast + 0.5;
1874 s += brightness;
1875 if (s < 0)
1876 s = 0;
1877 if (s > 1)
1878 s = 1;
1879 return (unsigned short) (s * 65535);
1882 uint32_t vo_x11_set_equalizer(struct vo *vo, const char *name, int value)
1884 float gamma, brightness, contrast;
1885 float rf, gf, bf;
1886 int k;
1889 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1890 * of TrueColor-ed window but be careful:
1891 * Unlike the colormaps, which are private for the X client
1892 * who created them and thus automatically destroyed on client
1893 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1894 * setting and _must_ be restored before the process exits.
1895 * Unforunately when the process crashes (or gets killed
1896 * for some reason) it is impossible to restore the setting,
1897 * and such behaviour could be rather annoying for the users.
1899 if (cmap == None)
1900 return VO_NOTAVAIL;
1902 if (!strcasecmp(name, "brightness"))
1903 vo_brightness = value;
1904 else if (!strcasecmp(name, "contrast"))
1905 vo_contrast = value;
1906 else if (!strcasecmp(name, "gamma"))
1907 vo_gamma = value;
1908 else
1909 return VO_NOTIMPL;
1911 brightness = 0.01 * vo_brightness;
1912 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1913 gamma = pow(2, -0.02 * vo_gamma);
1915 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1916 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
1917 green_mask;
1918 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
1920 /* now recalculate the colormap using the newly set value */
1921 for (k = 0; k < cm_size; k++)
1923 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
1924 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
1925 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
1928 XStoreColors(vo->x11->display, cmap, cols, cm_size);
1929 XFlush(vo->x11->display);
1930 return VO_TRUE;
1933 uint32_t vo_x11_get_equalizer(const char *name, int *value)
1935 if (cmap == None)
1936 return VO_NOTAVAIL;
1937 if (!strcasecmp(name, "brightness"))
1938 *value = vo_brightness;
1939 else if (!strcasecmp(name, "contrast"))
1940 *value = vo_contrast;
1941 else if (!strcasecmp(name, "gamma"))
1942 *value = vo_gamma;
1943 else
1944 return VO_NOTIMPL;
1945 return VO_TRUE;
1948 #ifdef CONFIG_XV
1949 int vo_xv_set_eq(struct vo *vo, uint32_t xv_port, const char *name, int value)
1951 XvAttribute *attributes;
1952 int i, howmany, xv_atom;
1954 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
1956 /* get available attributes */
1957 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1958 for (i = 0; i < howmany && attributes; i++)
1959 if (attributes[i].flags & XvSettable)
1961 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1962 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1963 then trigger it if it's ok so that the other values are at default upon query */
1964 if (xv_atom != None)
1966 int hue = 0, port_value, port_min, port_max;
1968 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1969 (!strcasecmp(name, "brightness")))
1970 port_value = value;
1971 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1972 (!strcasecmp(name, "contrast")))
1973 port_value = value;
1974 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1975 (!strcasecmp(name, "saturation")))
1976 port_value = value;
1977 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1978 (!strcasecmp(name, "hue")))
1980 port_value = value;
1981 hue = 1;
1982 } else
1983 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1984 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1985 (!strcasecmp(name, "red_intensity")))
1986 port_value = value;
1987 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1988 && (!strcasecmp(name, "green_intensity")))
1989 port_value = value;
1990 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1991 && (!strcasecmp(name, "blue_intensity")))
1992 port_value = value;
1993 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
1994 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
1995 && (!strcasecmp(name, "bt_709")))
1996 port_value = value;
1997 else
1998 continue;
2000 port_min = attributes[i].min_value;
2001 port_max = attributes[i].max_value;
2003 /* nvidia hue workaround */
2004 if (hue && port_min == 0 && port_max == 360)
2006 port_value =
2007 (port_value >=
2008 0) ? (port_value - 100) : (port_value + 100);
2010 // -100 -> min
2011 // 0 -> (max+min)/2
2012 // +100 -> max
2013 port_value =
2014 (port_value + 100) * (port_max - port_min) / 200 +
2015 port_min;
2016 XvSetPortAttribute(vo->x11->display, xv_port, xv_atom, port_value);
2017 return VO_TRUE;
2020 return VO_FALSE;
2023 int vo_xv_get_eq(struct vo *vo, uint32_t xv_port, const char *name, int *value)
2026 XvAttribute *attributes;
2027 int i, howmany, xv_atom;
2029 /* get available attributes */
2030 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
2031 for (i = 0; i < howmany && attributes; i++)
2032 if (attributes[i].flags & XvGettable)
2034 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
2035 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2036 then trigger it if it's ok so that the other values are at default upon query */
2037 if (xv_atom != None)
2039 int val, port_value = 0, port_min, port_max;
2041 XvGetPortAttribute(vo->x11->display, xv_port, xv_atom,
2042 &port_value);
2044 port_min = attributes[i].min_value;
2045 port_max = attributes[i].max_value;
2046 val =
2047 (port_value - port_min) * 200 / (port_max - port_min) -
2048 100;
2050 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2051 (!strcasecmp(name, "brightness")))
2052 *value = val;
2053 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2054 (!strcasecmp(name, "contrast")))
2055 *value = val;
2056 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2057 (!strcasecmp(name, "saturation")))
2058 *value = val;
2059 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2060 (!strcasecmp(name, "hue")))
2062 /* nasty nvidia detect */
2063 if (port_min == 0 && port_max == 360)
2064 *value = (val >= 0) ? (val - 100) : (val + 100);
2065 else
2066 *value = val;
2067 } else
2068 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2069 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2070 (!strcasecmp(name, "red_intensity")))
2071 *value = val;
2072 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2073 && (!strcasecmp(name, "green_intensity")))
2074 *value = val;
2075 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2076 && (!strcasecmp(name, "blue_intensity")))
2077 *value = val;
2078 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
2079 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
2080 && (!strcasecmp(name, "bt_709")))
2081 *value = val;
2082 else
2083 continue;
2085 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2086 name, *value);
2087 return VO_TRUE;
2090 return VO_FALSE;
2094 * \brief Interns the requested atom if it is available.
2096 * \param atom_name String containing the name of the requested atom.
2098 * \return Returns the atom if available, else None is returned.
2101 static Atom xv_intern_atom_if_exists(struct vo_x11_state *x11,
2102 char const *atom_name)
2104 XvAttribute * attributes;
2105 int attrib_count,i;
2106 Atom xv_atom = None;
2108 attributes = XvQueryPortAttributes(x11->display, x11->xv_port, &attrib_count );
2109 if( attributes!=NULL )
2111 for ( i = 0; i < attrib_count; ++i )
2113 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2115 xv_atom = XInternAtom(x11->display, atom_name, False );
2116 break; // found what we want, break out
2119 XFree( attributes );
2122 return xv_atom;
2126 * \brief Try to enable vsync for xv.
2127 * \return Returns -1 if not available, 0 on failure and 1 on success.
2129 int vo_xv_enable_vsync(struct vo *vo)
2131 struct vo_x11_state *x11 = vo->x11;
2132 Atom xv_atom = xv_intern_atom_if_exists(x11, "XV_SYNC_TO_VBLANK");
2133 if (xv_atom == None)
2134 return -1;
2135 return XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1) == Success;
2139 * \brief Get maximum supported source image dimensions.
2141 * This function does not set the variables pointed to by
2142 * width and height if the information could not be retrieved,
2143 * so the caller is reponsible for properly initializing them.
2145 * \param width [out] The maximum width gets stored here.
2146 * \param height [out] The maximum height gets stored here.
2149 void vo_xv_get_max_img_dim(struct vo *vo, uint32_t * width, uint32_t * height)
2151 struct vo_x11_state *x11 = vo->x11;
2152 XvEncodingInfo * encodings;
2153 //unsigned long num_encodings, idx; to int or too long?!
2154 unsigned int num_encodings, idx;
2156 XvQueryEncodings(x11->display, x11->xv_port, &num_encodings, &encodings);
2158 if ( encodings )
2160 for ( idx = 0; idx < num_encodings; ++idx )
2162 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2164 *width = encodings[idx].width;
2165 *height = encodings[idx].height;
2166 break;
2171 mp_msg( MSGT_VO, MSGL_V,
2172 "[xv common] Maximum source image dimensions: %ux%u\n",
2173 *width, *height );
2175 XvFreeEncodingInfo( encodings );
2179 * \brief Print information about the colorkey method and source.
2181 * \param ck_handling Integer value containing the information about
2182 * colorkey handling (see x11_common.h).
2184 * Outputs the content of |ck_handling| as a readable message.
2187 static void vo_xv_print_ck_info(struct vo_x11_state *x11)
2189 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2191 switch ( x11->xv_ck_info.method )
2193 case CK_METHOD_NONE:
2194 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2195 case CK_METHOD_AUTOPAINT:
2196 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2197 case CK_METHOD_MANUALFILL:
2198 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2199 case CK_METHOD_BACKGROUND:
2200 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2203 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2205 switch ( x11->xv_ck_info.source )
2207 case CK_SRC_CUR:
2208 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2209 x11->xv_colorkey );
2210 break;
2211 case CK_SRC_USE:
2212 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2214 mp_msg( MSGT_VO, MSGL_V,
2215 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2216 x11->xv_colorkey );
2218 else
2220 mp_msg( MSGT_VO, MSGL_V,
2221 "Using colorkey from MPlayer (0x%06lx)."
2222 " Use -colorkey to change.\n",
2223 x11->xv_colorkey );
2225 break;
2226 case CK_SRC_SET:
2227 mp_msg( MSGT_VO, MSGL_V,
2228 "Setting and using colorkey from MPlayer (0x%06lx)."
2229 " Use -colorkey to change.\n",
2230 x11->xv_colorkey );
2231 break;
2235 * \brief Init colorkey depending on the settings in xv_ck_info.
2237 * \return Returns 0 on failure and 1 on success.
2239 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2240 * flags in xv_ck_info.
2242 * Possiblilities:
2243 * * Methods
2244 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2245 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2246 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2247 * * Sources
2248 * - use currently set colorkey ( CK_SRC_CUR )
2249 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2250 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2252 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2253 * we don't draw anything as this means it was forced to off.
2255 int vo_xv_init_colorkey(struct vo *vo)
2257 struct vo_x11_state *x11 = vo->x11;
2258 Atom xv_atom;
2259 int rez;
2261 /* check if colorkeying is needed */
2262 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_COLORKEY");
2264 /* if we have to deal with colorkeying ... */
2265 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2267 /* check if we should use the colorkey specified in vo_colorkey */
2268 if ( x11->xv_ck_info.source != CK_SRC_CUR )
2270 x11->xv_colorkey = vo_colorkey;
2272 /* check if we have to set the colorkey too */
2273 if ( x11->xv_ck_info.source == CK_SRC_SET )
2275 xv_atom = XInternAtom(x11->display, "XV_COLORKEY",False);
2277 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, vo_colorkey);
2278 if ( rez != Success )
2280 mp_msg( MSGT_VO, MSGL_FATAL,
2281 "[xv common] Couldn't set colorkey!\n" );
2282 return 0; // error setting colorkey
2286 else
2288 int colorkey_ret;
2290 rez=XvGetPortAttribute(x11->display,x11->xv_port, xv_atom, &colorkey_ret);
2291 if ( rez == Success )
2293 x11->xv_colorkey = colorkey_ret;
2295 else
2297 mp_msg( MSGT_VO, MSGL_FATAL,
2298 "[xv common] Couldn't get colorkey!"
2299 "Maybe the selected Xv port has no overlay.\n" );
2300 return 0; // error getting colorkey
2304 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_AUTOPAINT_COLORKEY");
2306 /* should we draw the colorkey ourselves or activate autopainting? */
2307 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2309 rez = !Success; // reset rez to something different than Success
2311 if ( xv_atom != None ) // autopaint is supported
2313 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1);
2316 if ( rez != Success )
2318 // fallback to manual colorkey drawing
2319 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2322 else // disable colorkey autopainting if supported
2324 if ( xv_atom != None ) // we have autopaint attribute
2326 XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 0);
2330 else // do no colorkey drawing at all
2332 x11->xv_ck_info.method = CK_METHOD_NONE;
2333 } /* end: should we draw colorkey */
2335 /* output information about the current colorkey settings */
2336 vo_xv_print_ck_info(x11);
2338 return 1; // success
2342 * \brief Draw the colorkey on the video window.
2344 * Draws the colorkey depending on the set method ( colorkey_handling ).
2346 * Also draws the black bars ( when the video doesn't fit the display in
2347 * fullscreen ) separately, so they don't overlap with the video area.
2348 * It doesn't call XFlush.
2351 void vo_xv_draw_colorkey(struct vo *vo, int32_t x, int32_t y,
2352 int32_t w, int32_t h)
2354 struct vo_x11_state *x11 = vo->x11;
2355 if( x11->xv_ck_info.method == CK_METHOD_MANUALFILL ||
2356 x11->xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2358 XSetForeground(x11->display, x11->vo_gc, x11->xv_colorkey );
2359 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2360 x, y,
2361 w, h );
2365 /** \brief Tests if a valid argument for the ck suboption was given. */
2366 int xv_test_ck( void * arg )
2368 strarg_t * strarg = (strarg_t *)arg;
2370 if ( strargcmp( strarg, "use" ) == 0 ||
2371 strargcmp( strarg, "set" ) == 0 ||
2372 strargcmp( strarg, "cur" ) == 0 )
2374 return 1;
2377 return 0;
2379 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2380 int xv_test_ckm( void * arg )
2382 strarg_t * strarg = (strarg_t *)arg;
2384 if ( strargcmp( strarg, "bg" ) == 0 ||
2385 strargcmp( strarg, "man" ) == 0 ||
2386 strargcmp( strarg, "auto" ) == 0 )
2388 return 1;
2391 return 0;
2395 * \brief Modify the colorkey_handling var according to str
2397 * Checks if a valid pointer ( not NULL ) to the string
2398 * was given. And in that case modifies the colorkey_handling
2399 * var to reflect the requested behaviour.
2400 * If nothing happens the content of colorkey_handling stays
2401 * the same.
2403 * \param str Pointer to the string or NULL
2406 void xv_setup_colorkeyhandling(struct vo *vo, const char *ck_method_str,
2407 const char *ck_str)
2409 struct vo_x11_state *x11 = vo->x11;
2410 /* check if a valid pointer to the string was passed */
2411 if ( ck_str )
2413 if ( strncmp( ck_str, "use", 3 ) == 0 )
2415 x11->xv_ck_info.source = CK_SRC_USE;
2417 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2419 x11->xv_ck_info.source = CK_SRC_SET;
2422 /* check if a valid pointer to the string was passed */
2423 if ( ck_method_str )
2425 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2427 x11->xv_ck_info.method = CK_METHOD_BACKGROUND;
2429 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2431 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2433 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2435 x11->xv_ck_info.method = CK_METHOD_AUTOPAINT;
2440 #endif
2442 struct vo_x11_state *vo_x11_init_state(void)
2444 struct vo_x11_state *s = talloc_ptrtype(NULL, s);
2445 *s = (struct vo_x11_state){
2446 .xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR },
2447 .olddecor = MWM_DECOR_ALL,
2448 .oldfuncs = MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
2449 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE,
2450 .old_gravity = NorthWestGravity,
2452 return s;