x11: allow unicode input
[mplayer.git] / libvo / x11_common.c
blob8cabd3dd0911f4924734866752f12a07fe5b6f24
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},
546 // cursor keys
547 {wsLeft, KEY_LEFT}, {wsRight, KEY_RIGHT}, {wsUp, KEY_UP}, {wsDown, KEY_DOWN},
549 // navigation block
550 {wsInsert, KEY_INSERT}, {wsDelete, KEY_DELETE}, {wsHome, KEY_HOME}, {wsEnd, KEY_END},
551 {wsPageUp, KEY_PAGE_UP}, {wsPageDown, KEY_PAGE_DOWN},
553 // F-keys
554 {wsF1, KEY_F+1}, {wsF2, KEY_F+2}, {wsF3, KEY_F+3}, {wsF4, KEY_F+4},
555 {wsF5, KEY_F+5}, {wsF6, KEY_F+6}, {wsF7, KEY_F+7}, {wsF8, KEY_F+8},
556 {wsF9, KEY_F+9}, {wsF10, KEY_F+10}, {wsF11, KEY_F+11}, {wsF12, KEY_F+12},
558 // numpad independent of numlock
559 {wsGrayMinus, '-'}, {wsGrayPlus, '+'}, {wsGrayMul, '*'}, {wsGrayDiv, '/'},
560 {wsGrayEnter, KEY_KPENTER},
562 // numpad with numlock
563 {wsGray0, KEY_KP0}, {wsGray1, KEY_KP1}, {wsGray2, KEY_KP2},
564 {wsGray3, KEY_KP3}, {wsGray4, KEY_KP4}, {wsGray5, KEY_KP5},
565 {wsGray6, KEY_KP6}, {wsGray7, KEY_KP7}, {wsGray8, KEY_KP8},
566 {wsGray9, KEY_KP9}, {wsGrayDecimal, KEY_KPDEC},
568 // numpad without numlock
569 {wsGrayInsert, KEY_KPINS}, {wsGrayEnd, KEY_KP1}, {wsGrayDown, KEY_KP2},
570 {wsGrayPgDn, KEY_KP3}, {wsGrayLeft, KEY_KP4}, {wsGray5Dup, KEY_KP5},
571 {wsGrayRight, KEY_KP6}, {wsGrayHome, KEY_KP7}, {wsGrayUp, KEY_KP8},
572 {wsGrayPgUp, KEY_KP9}, {wsGrayDelete, KEY_KPDEL},
574 #ifdef XF86XK_AudioPause
575 {XF86XK_MenuKB, KEY_MENU},
576 {XF86XK_AudioPlay, KEY_PLAY}, {XF86XK_AudioPause, KEY_PAUSE}, {XF86XK_AudioStop, KEY_STOP},
577 {XF86XK_AudioPrev, KEY_PREV}, {XF86XK_AudioNext, KEY_NEXT},
578 {XF86XK_AudioMute, KEY_MUTE}, {XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN}, {XF86XK_AudioRaiseVolume, KEY_VOLUME_UP},
579 #endif
581 {0, 0}
584 static int vo_x11_lookupkey(int key)
586 static const char *passthrough_keys = " -+*/<>`~!@#$%^&()_{}:;\"\',.?\\|=[]";
587 int mpkey = 0;
588 if ((key >= 'a' && key <= 'z') ||
589 (key >= 'A' && key <= 'Z') ||
590 (key >= '0' && key <= '9') ||
591 (key > 0 && key < 256 && strchr(passthrough_keys, key)))
592 mpkey = key;
594 if (!mpkey)
595 mpkey = lookup_keymap_table(keymap, key);
597 return mpkey;
601 // ----- Motif header: -------
603 #define MWM_HINTS_FUNCTIONS (1L << 0)
604 #define MWM_HINTS_DECORATIONS (1L << 1)
605 #define MWM_HINTS_INPUT_MODE (1L << 2)
606 #define MWM_HINTS_STATUS (1L << 3)
608 #define MWM_FUNC_ALL (1L << 0)
609 #define MWM_FUNC_RESIZE (1L << 1)
610 #define MWM_FUNC_MOVE (1L << 2)
611 #define MWM_FUNC_MINIMIZE (1L << 3)
612 #define MWM_FUNC_MAXIMIZE (1L << 4)
613 #define MWM_FUNC_CLOSE (1L << 5)
615 #define MWM_DECOR_ALL (1L << 0)
616 #define MWM_DECOR_BORDER (1L << 1)
617 #define MWM_DECOR_RESIZEH (1L << 2)
618 #define MWM_DECOR_TITLE (1L << 3)
619 #define MWM_DECOR_MENU (1L << 4)
620 #define MWM_DECOR_MINIMIZE (1L << 5)
621 #define MWM_DECOR_MAXIMIZE (1L << 6)
623 #define MWM_INPUT_MODELESS 0
624 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
625 #define MWM_INPUT_SYSTEM_MODAL 2
626 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
627 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
629 #define MWM_TEAROFF_WINDOW (1L<<0)
631 typedef struct
633 long flags;
634 long functions;
635 long decorations;
636 long input_mode;
637 long state;
638 } MotifWmHints;
640 static MotifWmHints vo_MotifWmHints;
641 static Atom vo_MotifHints = None;
643 void vo_x11_decoration(struct vo *vo, int d)
645 struct vo_x11_state *x11 = vo->x11;
646 Atom mtype;
647 int mformat;
648 unsigned long mn, mb;
650 if (!WinID)
651 return;
653 if (vo_fsmode & 8)
655 XSetTransientForHint(x11->display, x11->window,
656 RootWindow(x11->display, x11->screen));
659 vo_MotifHints = XInternAtom(x11->display, "_MOTIF_WM_HINTS", 0);
660 if (vo_MotifHints != None)
662 if (!d)
664 MotifWmHints *mhints = NULL;
666 XGetWindowProperty(x11->display, x11->window,
667 vo_MotifHints, 0, 20, False,
668 vo_MotifHints, &mtype, &mformat, &mn,
669 &mb, (unsigned char **) &mhints);
670 if (mhints)
672 if (mhints->flags & MWM_HINTS_DECORATIONS)
673 x11->olddecor = mhints->decorations;
674 if (mhints->flags & MWM_HINTS_FUNCTIONS)
675 x11->oldfuncs = mhints->functions;
676 XFree(mhints);
680 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
681 vo_MotifWmHints.flags =
682 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
683 if (d)
685 vo_MotifWmHints.functions = x11->oldfuncs;
686 d = x11->olddecor;
688 #if 0
689 vo_MotifWmHints.decorations =
690 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
691 #else
692 vo_MotifWmHints.decorations =
693 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
694 #endif
695 XChangeProperty(x11->display, x11->window, vo_MotifHints,
696 vo_MotifHints, 32,
697 PropModeReplace,
698 (unsigned char *) &vo_MotifWmHints,
699 (vo_fsmode & 4) ? 4 : 5);
703 void vo_x11_classhint(struct vo *vo, Window window, const char *name)
705 struct MPOpts *opts = vo->opts;
706 struct vo_x11_state *x11 = vo->x11;
707 XClassHint wmClass;
708 pid_t pid = getpid();
710 wmClass.res_name = opts->vo_winname ? opts->vo_winname : (char *)name;
711 wmClass.res_class = "mplayer2";
712 XSetClassHint(x11->display, window, &wmClass);
713 XChangeProperty(x11->display, window, x11->XA_NET_WM_PID, XA_CARDINAL,
714 32, PropModeReplace, (unsigned char *) &pid, 1);
717 void vo_x11_uninit(struct vo *vo)
719 struct vo_x11_state *x11 = vo->x11;
720 saver_on(x11->display);
721 if (x11->window != None)
722 vo_showcursor(x11->display, x11->window);
724 if (x11->f_gc != None)
726 XFreeGC(vo->x11->display, x11->f_gc);
727 x11->f_gc = None;
730 if (x11->vo_gc != None)
732 XFreeGC(vo->x11->display, x11->vo_gc);
733 x11->vo_gc = None;
735 if (x11->window != None)
737 XClearWindow(x11->display, x11->window);
738 if (WinID < 0)
740 XEvent xev;
742 if (x11->xic)
743 XDestroyIC(x11->xic);
744 x11->xic = NULL;
746 XUnmapWindow(x11->display, x11->window);
747 XSelectInput(x11->display, x11->window, StructureNotifyMask);
748 XDestroyWindow(x11->display, x11->window);
751 XNextEvent(x11->display, &xev);
753 while (xev.type != DestroyNotify
754 || xev.xdestroywindow.event != x11->window);
756 x11->window = None;
758 vo_fs = 0;
759 x11->vo_old_width = x11->vo_old_height = 0;
760 x11->last_video_width = 0;
761 x11->last_video_height = 0;
762 x11->size_changed_during_fs = false;
766 static int check_resize(struct vo *vo)
768 int old_w = vo->dwidth, old_h = vo->dheight;
769 int old_x = vo->dx, old_y = vo->dy;
770 int rc = 0;
771 vo_x11_update_geometry(vo, true);
772 if (vo->dwidth != old_w || vo->dheight != old_h)
773 rc |= VO_EVENT_RESIZE;
774 if (vo->dx != old_x || vo->dy != old_y)
775 rc |= VO_EVENT_MOVE;
776 return rc;
779 int vo_x11_check_events(struct vo *vo)
781 struct vo_x11_state *x11 = vo->x11;
782 Display *display = vo->x11->display;
783 int ret = 0;
784 XEvent Event;
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 if (WinID > 0)
793 ret |= check_resize(vo);
794 while (XPending(display))
796 XNextEvent(display, &Event);
797 // printf("\rEvent.type=%X \n",Event.type);
798 switch (Event.type)
800 case Expose:
801 ret |= VO_EVENT_EXPOSE;
802 break;
803 case ConfigureNotify:
804 if (x11->window == None)
805 break;
806 ret |= check_resize(vo);
807 break;
808 case KeyPress:
810 char buf[100];
811 KeySym keySym = 0;
812 int modifiers = 0;
813 if (Event.xkey.state & ShiftMask)
814 modifiers |= KEY_MODIFIER_SHIFT;
815 if (Event.xkey.state & ControlMask)
816 modifiers |= KEY_MODIFIER_CTRL;
817 if (Event.xkey.state & Mod1Mask)
818 modifiers |= KEY_MODIFIER_ALT;
819 if (Event.xkey.state & Mod4Mask)
820 modifiers |= KEY_MODIFIER_META;
821 if (x11->xic) {
822 Status status;
823 int len = Xutf8LookupString(x11->xic, &Event.xkey, buf,
824 sizeof(buf), &keySym,
825 &status);
826 int mpkey = vo_x11_lookupkey(keySym);
827 if (mpkey) {
828 mplayer_put_key(vo->key_fifo, mpkey | modifiers);
829 } else if (status == XLookupChars
830 || status == XLookupBoth)
832 struct bstr t = { buf, len };
833 mplayer_put_key_utf8(vo->key_fifo, modifiers, t);
835 } else {
836 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
837 &x11->compose_status);
838 int mpkey = vo_x11_lookupkey(keySym);
839 if (mpkey)
840 mplayer_put_key(vo->key_fifo, mpkey | modifiers);
842 ret |= VO_EVENT_KEYPRESS;
844 break;
845 case MotionNotify:
846 vo_mouse_movement(vo, Event.xmotion.x, Event.xmotion.y);
848 if (x11->vo_mouse_autohide)
850 vo_showcursor(display, x11->window);
851 x11->mouse_waiting_hide = 1;
852 x11->mouse_timer = GetTimerMS();
854 break;
855 case ButtonPress:
856 if (x11->vo_mouse_autohide)
858 vo_showcursor(display, x11->window);
859 x11->mouse_waiting_hide = 1;
860 x11->mouse_timer = GetTimerMS();
862 mplayer_put_key(vo->key_fifo,
863 (MOUSE_BTN0 + Event.xbutton.button - 1)
864 | MP_KEY_DOWN);
865 break;
866 case ButtonRelease:
867 if (x11->vo_mouse_autohide)
869 vo_showcursor(display, x11->window);
870 x11->mouse_waiting_hide = 1;
871 x11->mouse_timer = GetTimerMS();
873 mplayer_put_key(vo->key_fifo,
874 MOUSE_BTN0 + Event.xbutton.button - 1);
875 break;
876 case PropertyNotify:
878 char *name =
879 XGetAtomName(display, Event.xproperty.atom);
881 if (!name)
882 break;
884 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
886 XFree(name);
888 break;
889 case MapNotify:
890 x11->vo_hint.win_gravity = x11->old_gravity;
891 XSetWMNormalHints(display, x11->window, &x11->vo_hint);
892 x11->fs_flip = 0;
893 break;
894 case DestroyNotify:
895 mp_msg(MSGT_VO, MSGL_WARN, "Our window was destroyed, exiting\n");
896 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
897 break;
898 case ClientMessage:
899 if (Event.xclient.message_type == x11->XAWM_PROTOCOLS &&
900 Event.xclient.data.l[0] == x11->XAWM_DELETE_WINDOW)
901 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
902 break;
905 return ret;
909 * \brief sets the size and position of the non-fullscreen window.
911 static void vo_x11_nofs_sizepos(struct vo *vo, int x, int y,
912 int width, int height)
914 struct vo_x11_state *x11 = vo->x11;
915 if (width == x11->last_video_width && height == x11->last_video_height) {
916 if (!vo->opts->force_window_position && !x11->size_changed_during_fs)
917 return;
918 } else if (vo_fs)
919 x11->size_changed_during_fs = true;
920 x11->last_video_height = height;
921 x11->last_video_width = width;
922 vo_x11_sizehint(vo, x, y, width, height, 0);
923 if (vo_fs) {
924 x11->vo_old_x = x;
925 x11->vo_old_y = y;
926 x11->vo_old_width = width;
927 x11->vo_old_height = height;
929 else
931 vo->dwidth = width;
932 vo->dheight = height;
933 if (vo->opts->force_window_position)
934 XMoveResizeWindow(vo->x11->display, vo->x11->window, x, y, width,
935 height);
936 else
937 XResizeWindow(vo->x11->display, vo->x11->window, width, height);
941 void vo_x11_sizehint(struct vo *vo, int x, int y, int width, int height, int max)
943 struct vo_x11_state *x11 = vo->x11;
944 x11->vo_hint.flags = 0;
945 if (vo_keepaspect)
947 x11->vo_hint.flags |= PAspect;
948 x11->vo_hint.min_aspect.x = width;
949 x11->vo_hint.min_aspect.y = height;
950 x11->vo_hint.max_aspect.x = width;
951 x11->vo_hint.max_aspect.y = height;
954 x11->vo_hint.flags |= PPosition | PSize;
955 x11->vo_hint.x = x;
956 x11->vo_hint.y = y;
957 x11->vo_hint.width = width;
958 x11->vo_hint.height = height;
959 if (max)
961 x11->vo_hint.flags |= PMaxSize;
962 x11->vo_hint.max_width = width;
963 x11->vo_hint.max_height = height;
964 } else
966 x11->vo_hint.max_width = 0;
967 x11->vo_hint.max_height = 0;
970 // Set minimum height/width to 4 to avoid off-by-one errors
971 // and because mga_vid requires a minimal size of 4 pixels.
972 x11->vo_hint.flags |= PMinSize;
973 x11->vo_hint.min_width = x11->vo_hint.min_height = 4;
975 // Set the base size. A window manager might display the window
976 // size to the user relative to this.
977 // Setting these to width/height might be nice, but e.g. fluxbox can't handle it.
978 x11->vo_hint.flags |= PBaseSize;
979 x11->vo_hint.base_width = 0 /*width*/;
980 x11->vo_hint.base_height = 0 /*height*/;
982 x11->vo_hint.flags |= PWinGravity;
983 x11->vo_hint.win_gravity = StaticGravity;
984 XSetWMNormalHints(x11->display, x11->window, &x11->vo_hint);
987 static int vo_x11_get_gnome_layer(struct vo_x11_state *x11, Window win)
989 Atom type;
990 int format;
991 unsigned long nitems;
992 unsigned long bytesafter;
993 unsigned short *args = NULL;
995 if (XGetWindowProperty(x11->display, win, x11->XA_WIN_LAYER, 0, 16384,
996 False, AnyPropertyType, &type, &format, &nitems,
997 &bytesafter,
998 (unsigned char **) &args) == Success
999 && nitems > 0 && args)
1001 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
1002 *args);
1003 return *args;
1005 return WIN_LAYER_NORMAL;
1008 // set a X text property that expects a UTF8_STRING type
1009 static void vo_x11_set_property_utf8(struct vo *vo, Atom name, const char *t)
1011 struct vo_x11_state *x11 = vo->x11;
1013 XChangeProperty(x11->display, x11->window, name, x11->XAUTF8_STRING, 8,
1014 PropModeReplace, t, strlen(t));
1017 // set a X text property that expects a STRING or COMPOUND_TEXT type
1018 static void vo_x11_set_property_string(struct vo *vo, Atom name, const char *t)
1020 struct vo_x11_state *x11 = vo->x11;
1021 XTextProperty prop = {0};
1023 if (Xutf8TextListToTextProperty(x11->display, (char **)&t, 1,
1024 XStdICCTextStyle, &prop) == Success) {
1025 XSetTextProperty(x11->display, x11->window, &prop, name);
1026 } else {
1027 // Strictly speaking this violates the ICCCM, but there's no way we
1028 // can do this correctly.
1029 vo_x11_set_property_utf8(vo, name, t);
1032 if (prop.value)
1033 XFree(prop.value);
1036 static void vo_x11_update_window_title(struct vo *vo)
1038 struct vo_x11_state *x11 = vo->x11;
1040 const char *title = vo_get_window_title(vo);
1041 vo_x11_set_property_string(vo, XA_WM_NAME, title);
1042 vo_x11_set_property_string(vo, XA_WM_ICON_NAME, title);
1043 vo_x11_set_property_utf8(vo, x11->XA_NET_WM_NAME, title);
1044 vo_x11_set_property_utf8(vo, x11->XA_NET_WM_ICON_NAME, title);
1048 static Window vo_x11_create_smooth_window(struct vo_x11_state *x11, Window mRoot,
1049 Visual * vis, int x, int y,
1050 unsigned int width, unsigned int height,
1051 int depth, Colormap col_map)
1053 unsigned long xswamask = CWBorderPixel;
1054 XSetWindowAttributes xswa;
1055 Window ret_win;
1057 if (col_map != CopyFromParent)
1059 xswa.colormap = col_map;
1060 xswamask |= CWColormap;
1062 xswa.background_pixel = 0;
1063 xswa.border_pixel = 0;
1064 xswa.backing_store = NotUseful;
1065 xswa.bit_gravity = StaticGravity;
1067 ret_win =
1068 XCreateWindow(x11->display, x11->rootwin, x, y, width, height, 0, depth,
1069 CopyFromParent, vis, xswamask, &xswa);
1070 XSetWMProtocols(x11->display, ret_win, &x11->XAWM_DELETE_WINDOW, 1);
1071 if (x11->f_gc == None)
1072 x11->f_gc = XCreateGC(x11->display, ret_win, 0, 0);
1073 XSetForeground(x11->display, x11->f_gc, 0);
1075 return ret_win;
1079 * \brief create and setup a window suitable for display
1080 * \param vis Visual to use for creating the window
1081 * \param x x position of window
1082 * \param y y position of window
1083 * \param width width of window
1084 * \param height height of window
1085 * \param flags flags for window creation.
1086 * Only VOFLAG_FULLSCREEN is supported so far.
1087 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1088 * \param classname name to use for the classhint
1090 * This also does the grunt-work like setting Window Manager hints etc.
1091 * If vo_window is already set it just moves and resizes it.
1093 void vo_x11_create_vo_window(struct vo *vo, XVisualInfo *vis, int x, int y,
1094 unsigned int width, unsigned int height, int flags,
1095 Colormap col_map, const char *classname)
1097 struct MPOpts *opts = vo->opts;
1098 struct vo_x11_state *x11 = vo->x11;
1099 Display *mDisplay = vo->x11->display;
1100 if (WinID >= 0) {
1101 vo_fs = flags & VOFLAG_FULLSCREEN;
1102 x11->window = WinID ? (Window)WinID : x11->rootwin;
1103 if (col_map != CopyFromParent) {
1104 unsigned long xswamask = CWColormap;
1105 XSetWindowAttributes xswa;
1106 xswa.colormap = col_map;
1107 XChangeWindowAttributes(mDisplay, x11->window, xswamask, &xswa);
1108 XInstallColormap(mDisplay, col_map);
1110 if (WinID) {
1111 // Expose events can only really be handled by us, so request them.
1112 vo_x11_selectinput_witherr(mDisplay, x11->window, ExposureMask);
1113 } else
1114 // Do not capture events since it might break the parent application
1115 // if it relies on events being forwarded to the parent of WinID.
1116 // It also is consistent with the w32_common.c code.
1117 vo_x11_selectinput_witherr(mDisplay, x11->window,
1118 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1119 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1121 vo_x11_update_geometry(vo, true);
1122 goto final;
1124 if (x11->window == None) {
1125 vo_fs = 0;
1126 vo->dwidth = width;
1127 vo->dheight = height;
1128 x11->window = vo_x11_create_smooth_window(x11, x11->rootwin, vis->visual,
1129 x, y, width, height, vis->depth, col_map);
1130 x11->window_state = VOFLAG_HIDDEN;
1132 if (flags & VOFLAG_HIDDEN)
1133 goto final;
1134 if (x11->window_state & VOFLAG_HIDDEN) {
1135 XSizeHints hint;
1136 x11->window_state &= ~VOFLAG_HIDDEN;
1137 vo_x11_classhint(vo, x11->window, classname);
1138 vo_hidecursor(mDisplay, x11->window);
1139 XSelectInput(mDisplay, x11->window, StructureNotifyMask);
1140 hint.x = x; hint.y = y;
1141 hint.width = width; hint.height = height;
1142 hint.flags = PSize;
1143 if (geometry_xy_changed)
1144 hint.flags |= PPosition;
1145 XSetWMNormalHints(mDisplay, x11->window, &hint);
1146 if (!vo_border) vo_x11_decoration(vo, 0);
1147 // map window
1148 x11->xic = XCreateIC(x11->xim,
1149 XNInputStyle, XIMPreeditNone | XIMStatusNone,
1150 XNClientWindow, x11->window,
1151 XNFocusWindow, x11->window,
1152 NULL);
1153 XSelectInput(mDisplay, x11->window, NoEventMask);
1154 vo_x11_selectinput_witherr(mDisplay, x11->window,
1155 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1156 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1157 XMapWindow(mDisplay, x11->window);
1158 vo_x11_clearwindow(vo, x11->window);
1160 vo_x11_update_window_title(vo);
1161 if (opts->vo_ontop) vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1162 vo_x11_update_geometry(vo, !geometry_xy_changed);
1163 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, width, height);
1164 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1165 vo_x11_fullscreen(vo);
1166 else if (vo_fs) {
1167 // if we are already in fullscreen do not switch back and forth, just
1168 // set the size values right.
1169 vo->dwidth = vo->opts->vo_screenwidth;
1170 vo->dheight = vo->opts->vo_screenheight;
1172 final:
1173 if (x11->vo_gc != None)
1174 XFreeGC(mDisplay, x11->vo_gc);
1175 x11->vo_gc = XCreateGC(mDisplay, x11->window, 0, NULL);
1177 XSync(mDisplay, False);
1178 x11->vo_mouse_autohide = 1;
1179 vo->event_fd = ConnectionNumber(x11->display);
1182 void vo_x11_clearwindow_part(struct vo *vo, Window vo_window,
1183 int img_width, int img_height)
1185 struct vo_x11_state *x11 = vo->x11;
1186 Display *mDisplay = vo->x11->display;
1187 int u_dheight, u_dwidth, left_ov, left_ov2;
1189 if (x11->f_gc == None)
1190 return;
1192 u_dheight = vo->dheight;
1193 u_dwidth = vo->dwidth;
1194 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1195 return;
1197 left_ov = (u_dheight - img_height) / 2;
1198 left_ov2 = (u_dwidth - img_width) / 2;
1200 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, 0, u_dwidth, left_ov);
1201 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, u_dheight - left_ov - 1,
1202 u_dwidth, left_ov + 1);
1204 if (u_dwidth > img_width)
1206 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, left_ov, left_ov2,
1207 img_height);
1208 XFillRectangle(mDisplay, vo_window, x11->f_gc, u_dwidth - left_ov2 - 1,
1209 left_ov, left_ov2 + 1, img_height);
1212 XFlush(mDisplay);
1215 void vo_x11_clearwindow(struct vo *vo, Window vo_window)
1217 struct vo_x11_state *x11 = vo->x11;
1218 struct MPOpts *opts = vo->opts;
1219 if (x11->f_gc == None)
1220 return;
1221 XFillRectangle(x11->display, vo_window, x11->f_gc, 0, 0,
1222 opts->vo_screenwidth, opts->vo_screenheight);
1224 XFlush(x11->display);
1228 void vo_x11_setlayer(struct vo *vo, Window vo_window, int layer)
1230 struct vo_x11_state *x11 = vo->x11;
1231 if (WinID >= 0)
1232 return;
1234 if (x11->fs_type & vo_wm_LAYER)
1236 XClientMessageEvent xev;
1238 if (!x11->orig_layer)
1239 x11->orig_layer = vo_x11_get_gnome_layer(x11, vo_window);
1241 memset(&xev, 0, sizeof(xev));
1242 xev.type = ClientMessage;
1243 xev.display = x11->display;
1244 xev.window = vo_window;
1245 xev.message_type = x11->XA_WIN_LAYER;
1246 xev.format = 32;
1247 xev.data.l[0] = layer ? fs_layer : x11->orig_layer; // if not fullscreen, stay on default layer
1248 xev.data.l[1] = CurrentTime;
1249 mp_msg(MSGT_VO, MSGL_V,
1250 "[x11] Layered style stay on top (layer %ld).\n",
1251 xev.data.l[0]);
1252 XSendEvent(x11->display, x11->rootwin, False, SubstructureNotifyMask,
1253 (XEvent *) & xev);
1254 } else if (x11->fs_type & vo_wm_NETWM)
1256 XClientMessageEvent xev;
1257 char *state;
1259 memset(&xev, 0, sizeof(xev));
1260 xev.type = ClientMessage;
1261 xev.message_type = x11->XA_NET_WM_STATE;
1262 xev.display = x11->display;
1263 xev.window = vo_window;
1264 xev.format = 32;
1265 xev.data.l[0] = layer;
1267 if (x11->fs_type & vo_wm_STAYS_ON_TOP)
1268 xev.data.l[1] = x11->XA_NET_WM_STATE_STAYS_ON_TOP;
1269 else if (x11->fs_type & vo_wm_ABOVE)
1270 xev.data.l[1] = x11->XA_NET_WM_STATE_ABOVE;
1271 else if (x11->fs_type & vo_wm_FULLSCREEN)
1272 xev.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
1273 else if (x11->fs_type & vo_wm_BELOW)
1274 // This is not fallback. We can safely assume that the situation
1275 // where only NETWM_STATE_BELOW is supported doesn't exist.
1276 xev.data.l[1] = x11->XA_NET_WM_STATE_BELOW;
1278 XSendEvent(x11->display, x11->rootwin, False, SubstructureRedirectMask,
1279 (XEvent *) & xev);
1280 state = XGetAtomName(x11->display, xev.data.l[1]);
1281 mp_msg(MSGT_VO, MSGL_V,
1282 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1283 layer, state);
1284 XFree(state);
1288 static int vo_x11_get_fs_type(int supported)
1290 int i;
1291 int type = supported;
1293 if (vo_fstype_list)
1295 for (i = 0; vo_fstype_list[i]; i++)
1297 int neg = 0;
1298 char *arg = vo_fstype_list[i];
1300 if (vo_fstype_list[i][0] == '-')
1302 neg = 1;
1303 arg = vo_fstype_list[i] + 1;
1306 if (!strncmp(arg, "layer", 5))
1308 if (!neg && (arg[5] == '='))
1310 char *endptr = NULL;
1311 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1313 if (endptr && *endptr == '\0' && layer >= 0
1314 && layer <= 15)
1315 fs_layer = layer;
1317 if (neg)
1318 type &= ~vo_wm_LAYER;
1319 else
1320 type |= vo_wm_LAYER;
1321 } else if (!strcmp(arg, "above"))
1323 if (neg)
1324 type &= ~vo_wm_ABOVE;
1325 else
1326 type |= vo_wm_ABOVE;
1327 } else if (!strcmp(arg, "fullscreen"))
1329 if (neg)
1330 type &= ~vo_wm_FULLSCREEN;
1331 else
1332 type |= vo_wm_FULLSCREEN;
1333 } else if (!strcmp(arg, "stays_on_top"))
1335 if (neg)
1336 type &= ~vo_wm_STAYS_ON_TOP;
1337 else
1338 type |= vo_wm_STAYS_ON_TOP;
1339 } else if (!strcmp(arg, "below"))
1341 if (neg)
1342 type &= ~vo_wm_BELOW;
1343 else
1344 type |= vo_wm_BELOW;
1345 } else if (!strcmp(arg, "netwm"))
1347 if (neg)
1348 type &= ~vo_wm_NETWM;
1349 else
1350 type |= vo_wm_NETWM;
1351 } else if (!strcmp(arg, "none"))
1352 type = 0; // clear; keep parsing
1356 return type;
1360 * \brief update vo->dx, vo->dy, vo->dwidth and vo->dheight with current values of vo->x11->window
1361 * \return returns current color depth of vo->x11->window
1363 int vo_x11_update_geometry(struct vo *vo, bool update_pos)
1365 struct vo_x11_state *x11 = vo->x11;
1366 unsigned depth, w, h;
1367 int dummy_int;
1368 Window dummy_win;
1369 XGetGeometry(x11->display, x11->window, &dummy_win, &dummy_int, &dummy_int,
1370 &w, &h, &dummy_int, &depth);
1371 if (w <= INT_MAX && h <= INT_MAX) {
1372 vo->dwidth = w;
1373 vo->dheight = h;
1375 if (update_pos)
1376 XTranslateCoordinates(x11->display, x11->window, x11->rootwin, 0, 0,
1377 &vo->dx, &vo->dy, &dummy_win);
1379 return depth <= INT_MAX ? depth : 0;
1382 void vo_x11_fullscreen(struct vo *vo)
1384 struct MPOpts *opts = vo->opts;
1385 struct vo_x11_state *x11 = vo->x11;
1386 int x, y, w, h;
1387 x = x11->vo_old_x;
1388 y = x11->vo_old_y;
1389 w = x11->vo_old_width;
1390 h = x11->vo_old_height;
1392 if (WinID >= 0) {
1393 vo_fs = !vo_fs;
1394 return;
1396 if (x11->fs_flip)
1397 return;
1399 if (vo_fs)
1401 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1402 vo_fs = VO_FALSE;
1403 if (x11->size_changed_during_fs && (x11->fs_type & vo_wm_FULLSCREEN))
1404 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, x11->last_video_width,
1405 x11->last_video_height);
1406 x11->size_changed_during_fs = false;
1407 } else
1409 // win->fs
1410 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1412 vo_fs = VO_TRUE;
1413 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1415 x11->vo_old_x = vo->dx;
1416 x11->vo_old_y = vo->dy;
1417 x11->vo_old_width = vo->dwidth;
1418 x11->vo_old_height = vo->dheight;
1420 update_xinerama_info(vo);
1421 x = xinerama_x;
1422 y = xinerama_y;
1423 w = opts->vo_screenwidth;
1424 h = opts->vo_screenheight;
1427 long dummy;
1429 XGetWMNormalHints(x11->display, x11->window, &x11->vo_hint, &dummy);
1430 if (!(x11->vo_hint.flags & PWinGravity))
1431 x11->old_gravity = NorthWestGravity;
1432 else
1433 x11->old_gravity = x11->vo_hint.win_gravity;
1435 if (x11->wm_type == 0 && !(vo_fsmode & 16))
1437 XUnmapWindow(x11->display, x11->window); // required for MWM
1438 XWithdrawWindow(x11->display, x11->window, x11->screen);
1439 x11->fs_flip = 1;
1442 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1444 vo_x11_decoration(vo, vo_border && !vo_fs);
1445 vo_x11_sizehint(vo, x, y, w, h, 0);
1446 vo_x11_setlayer(vo, x11->window, vo_fs);
1449 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1451 /* some WMs lose ontop after fullscreen */
1452 if ((!(vo_fs)) & opts->vo_ontop)
1453 vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1455 XMapRaised(x11->display, x11->window);
1456 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1457 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1458 XRaiseWindow(x11->display, x11->window);
1459 XFlush(x11->display);
1462 void vo_x11_ontop(struct vo *vo)
1464 struct MPOpts *opts = vo->opts;
1465 opts->vo_ontop = !opts->vo_ontop;
1467 vo_x11_setlayer(vo, vo->x11->window, opts->vo_ontop);
1470 void vo_x11_border(struct vo *vo)
1472 vo_border = !vo_border;
1473 vo_x11_decoration(vo, vo_border && !vo_fs);
1477 * XScreensaver stuff
1480 static int screensaver_off;
1481 static unsigned int time_last;
1483 void xscreensaver_heartbeat(struct vo_x11_state *x11)
1485 unsigned int time = GetTimerMS();
1487 if (x11->display && screensaver_off && (time - time_last) > 30000)
1489 time_last = time;
1491 XResetScreenSaver(x11->display);
1495 static int xss_suspend(Display *mDisplay, Bool suspend)
1497 #ifndef CONFIG_XSS
1498 return 0;
1499 #else
1500 int event, error, major, minor;
1501 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1502 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1503 return 0;
1504 if (major < 1 || (major == 1 && minor < 1))
1505 return 0;
1506 XScreenSaverSuspend(mDisplay, suspend);
1507 return 1;
1508 #endif
1512 * End of XScreensaver stuff
1515 static void saver_on(Display * mDisplay)
1518 if (!screensaver_off)
1519 return;
1520 screensaver_off = 0;
1521 if (xss_suspend(mDisplay, False))
1522 return;
1523 #ifdef CONFIG_XDPMS
1524 if (dpms_disabled)
1526 int nothing;
1527 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1529 if (!DPMSEnable(mDisplay))
1530 { // restoring power saving settings
1531 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1532 } else
1534 // DPMS does not seem to be enabled unless we call DPMSInfo
1535 BOOL onoff;
1536 CARD16 state;
1538 DPMSForceLevel(mDisplay, DPMSModeOn);
1539 DPMSInfo(mDisplay, &state, &onoff);
1540 if (onoff)
1542 mp_msg(MSGT_VO, MSGL_V,
1543 "Successfully enabled DPMS\n");
1544 } else
1546 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1550 dpms_disabled = 0;
1552 #endif
1555 static void saver_off(Display * mDisplay)
1557 int nothing;
1559 if (screensaver_off)
1560 return;
1561 screensaver_off = 1;
1562 if (xss_suspend(mDisplay, True))
1563 return;
1564 #ifdef CONFIG_XDPMS
1565 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1567 BOOL onoff;
1568 CARD16 state;
1570 DPMSInfo(mDisplay, &state, &onoff);
1571 if (onoff)
1573 Status stat;
1575 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1576 dpms_disabled = 1;
1577 stat = DPMSDisable(mDisplay); // monitor powersave off
1578 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1581 #endif
1584 static XErrorHandler old_handler = NULL;
1585 static int selectinput_err = 0;
1586 static int x11_selectinput_errorhandler(Display * display,
1587 XErrorEvent * event)
1589 if (event->error_code == BadAccess)
1591 selectinput_err = 1;
1592 mp_msg(MSGT_VO, MSGL_ERR,
1593 "X11 error: BadAccess during XSelectInput Call\n");
1594 mp_msg(MSGT_VO, MSGL_ERR,
1595 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1596 /* If you think MPlayer should shutdown with this error,
1597 * comment out the following line */
1598 return 0;
1600 if (old_handler != NULL)
1601 old_handler(display, event);
1602 else
1603 x11_errorhandler(display, event);
1604 return 0;
1607 void vo_x11_selectinput_witherr(Display * display, Window w,
1608 long event_mask)
1610 XSync(display, False);
1611 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1612 selectinput_err = 0;
1613 if (vo_nomouse_input)
1615 XSelectInput(display, w,
1616 event_mask &
1617 (~(ButtonPressMask | ButtonReleaseMask)));
1618 } else
1620 XSelectInput(display, w, event_mask);
1622 XSync(display, False);
1623 XSetErrorHandler(old_handler);
1624 if (selectinput_err)
1626 mp_msg(MSGT_VO, MSGL_ERR,
1627 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1628 XSelectInput(display, w,
1629 event_mask &
1631 (ButtonPressMask | ButtonReleaseMask |
1632 PointerMotionMask)));
1636 #ifdef CONFIG_XF86VM
1637 void vo_vm_switch(struct vo *vo)
1639 struct vo_x11_state *x11 = vo->x11;
1640 struct MPOpts *opts = vo->opts;
1641 Display *mDisplay = x11->display;
1642 int vm_event, vm_error;
1643 int vm_ver, vm_rev;
1644 int i, j, have_vm = 0;
1645 int X = vo->dwidth, Y = vo->dheight;
1646 int modeline_width, modeline_height;
1648 int modecount;
1650 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1652 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1653 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1654 vm_rev);
1655 have_vm = 1;
1656 } else {
1657 mp_msg(MSGT_VO, MSGL_WARN,
1658 "XF86VidMode extension not available.\n");
1661 if (have_vm)
1663 if (vidmodes == NULL)
1664 XF86VidModeGetAllModeLines(mDisplay, x11->screen, &modecount,
1665 &vidmodes);
1666 j = 0;
1667 modeline_width = vidmodes[0]->hdisplay;
1668 modeline_height = vidmodes[0]->vdisplay;
1670 for (i = 1; i < modecount; i++)
1671 if ((vidmodes[i]->hdisplay >= X)
1672 && (vidmodes[i]->vdisplay >= Y))
1673 if ((vidmodes[i]->hdisplay <= modeline_width)
1674 && (vidmodes[i]->vdisplay <= modeline_height))
1676 modeline_width = vidmodes[i]->hdisplay;
1677 modeline_height = vidmodes[i]->vdisplay;
1678 j = i;
1681 mp_tmsg(MSGT_VO, MSGL_INFO, "XF86VM: Selected video mode %dx%d for image size %dx%d.\n",
1682 modeline_width, modeline_height, X, Y);
1683 XF86VidModeLockModeSwitch(mDisplay, x11->screen, 0);
1684 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1685 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1687 // FIXME: all this is more of a hack than proper solution
1688 X = (opts->vo_screenwidth - modeline_width) / 2;
1689 Y = (opts->vo_screenheight - modeline_height) / 2;
1690 XF86VidModeSetViewPort(mDisplay, x11->screen, X, Y);
1691 vo->dx = X;
1692 vo->dy = Y;
1693 vo->dwidth = modeline_width;
1694 vo->dheight = modeline_height;
1695 aspect_save_screenres(vo, modeline_width, modeline_height);
1699 void vo_vm_close(struct vo *vo)
1701 Display *dpy = vo->x11->display;
1702 struct MPOpts *opts = vo->opts;
1703 if (vidmodes != NULL)
1705 int i, modecount;
1707 free(vidmodes);
1708 vidmodes = NULL;
1709 XF86VidModeGetAllModeLines(dpy, vo->x11->screen, &modecount,
1710 &vidmodes);
1711 for (i = 0; i < modecount; i++)
1712 if ((vidmodes[i]->hdisplay == opts->vo_screenwidth)
1713 && (vidmodes[i]->vdisplay == opts->vo_screenheight))
1715 mp_msg(MSGT_VO, MSGL_INFO,
1716 "Returning to original mode %dx%d\n",
1717 opts->vo_screenwidth, opts->vo_screenheight);
1718 break;
1721 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1722 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1723 free(vidmodes);
1724 vidmodes = NULL;
1728 double vo_vm_get_fps(struct vo *vo)
1730 struct vo_x11_state *x11 = vo->x11;
1731 int clock;
1732 XF86VidModeModeLine modeline;
1733 if (!XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline))
1734 return 0;
1735 if (modeline.privsize)
1736 XFree(modeline.private);
1737 return 1e3 * clock / modeline.htotal / modeline.vtotal;
1739 #endif
1741 #endif /* X11_FULLSCREEN */
1745 * Scan the available visuals on this Display/Screen. Try to find
1746 * the 'best' available TrueColor visual that has a decent color
1747 * depth (at least 15bit). If there are multiple visuals with depth
1748 * >= 15bit, we prefer visuals with a smaller color depth.
1750 int vo_find_depth_from_visuals(Display * dpy, int screen,
1751 Visual ** visual_return)
1753 XVisualInfo visual_tmpl;
1754 XVisualInfo *visuals;
1755 int nvisuals, i;
1756 int bestvisual = -1;
1757 int bestvisual_depth = -1;
1759 visual_tmpl.screen = screen;
1760 visual_tmpl.class = TrueColor;
1761 visuals = XGetVisualInfo(dpy,
1762 VisualScreenMask | VisualClassMask,
1763 &visual_tmpl, &nvisuals);
1764 if (visuals != NULL)
1766 for (i = 0; i < nvisuals; i++)
1768 mp_msg(MSGT_VO, MSGL_V,
1769 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1770 visuals[i].visualid, visuals[i].depth,
1771 visuals[i].red_mask, visuals[i].green_mask,
1772 visuals[i].blue_mask);
1774 * Save the visual index and its depth, if this is the first
1775 * truecolor visul, or a visual that is 'preferred' over the
1776 * previous 'best' visual.
1778 if (bestvisual_depth == -1
1779 || (visuals[i].depth >= 15
1780 && (visuals[i].depth < bestvisual_depth
1781 || bestvisual_depth < 15)))
1783 bestvisual = i;
1784 bestvisual_depth = visuals[i].depth;
1788 if (bestvisual != -1 && visual_return != NULL)
1789 *visual_return = visuals[bestvisual].visual;
1791 XFree(visuals);
1793 return bestvisual_depth;
1797 static Colormap cmap = None;
1798 static XColor cols[256];
1799 static int cm_size, red_mask, green_mask, blue_mask;
1802 Colormap vo_x11_create_colormap(struct vo *vo, XVisualInfo *vinfo)
1804 struct vo_x11_state *x11 = vo->x11;
1805 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1807 if (vinfo->class != DirectColor)
1808 return XCreateColormap(x11->display, x11->rootwin, vinfo->visual,
1809 AllocNone);
1811 /* can this function get called twice or more? */
1812 if (cmap)
1813 return cmap;
1814 cm_size = vinfo->colormap_size;
1815 red_mask = vinfo->red_mask;
1816 green_mask = vinfo->green_mask;
1817 blue_mask = vinfo->blue_mask;
1818 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1819 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1820 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1821 rvu = 65536ull * ru / (red_mask + ru);
1822 gvu = 65536ull * gu / (green_mask + gu);
1823 bvu = 65536ull * bu / (blue_mask + bu);
1824 r = g = b = 0;
1825 rv = gv = bv = 0;
1826 m = DoRed | DoGreen | DoBlue;
1827 for (k = 0; k < cm_size; k++)
1829 int t;
1831 cols[k].pixel = r | g | b;
1832 cols[k].red = rv;
1833 cols[k].green = gv;
1834 cols[k].blue = bv;
1835 cols[k].flags = m;
1836 t = (r + ru) & red_mask;
1837 if (t < r)
1838 m &= ~DoRed;
1839 r = t;
1840 t = (g + gu) & green_mask;
1841 if (t < g)
1842 m &= ~DoGreen;
1843 g = t;
1844 t = (b + bu) & blue_mask;
1845 if (t < b)
1846 m &= ~DoBlue;
1847 b = t;
1848 rv += rvu;
1849 gv += gvu;
1850 bv += bvu;
1852 cmap = XCreateColormap(x11->display, x11->rootwin, vinfo->visual, AllocAll);
1853 XStoreColors(x11->display, cmap, cols, cm_size);
1854 return cmap;
1858 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1859 * hue and red/green/blue intensity, but we cannot do saturation.
1860 * Currently only gamma, brightness and contrast are implemented.
1861 * Is there sufficient interest for hue and/or red/green/blue intensity?
1863 /* these values have range [-100,100] and are initially 0 */
1864 static int vo_gamma = 0;
1865 static int vo_brightness = 0;
1866 static int vo_contrast = 0;
1868 static int transform_color(float val,
1869 float brightness, float contrast, float gamma) {
1870 float s = pow(val, gamma);
1871 s = (s - 0.5) * contrast + 0.5;
1872 s += brightness;
1873 if (s < 0)
1874 s = 0;
1875 if (s > 1)
1876 s = 1;
1877 return (unsigned short) (s * 65535);
1880 uint32_t vo_x11_set_equalizer(struct vo *vo, const char *name, int value)
1882 float gamma, brightness, contrast;
1883 float rf, gf, bf;
1884 int k;
1887 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1888 * of TrueColor-ed window but be careful:
1889 * Unlike the colormaps, which are private for the X client
1890 * who created them and thus automatically destroyed on client
1891 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1892 * setting and _must_ be restored before the process exits.
1893 * Unforunately when the process crashes (or gets killed
1894 * for some reason) it is impossible to restore the setting,
1895 * and such behaviour could be rather annoying for the users.
1897 if (cmap == None)
1898 return VO_NOTAVAIL;
1900 if (!strcasecmp(name, "brightness"))
1901 vo_brightness = value;
1902 else if (!strcasecmp(name, "contrast"))
1903 vo_contrast = value;
1904 else if (!strcasecmp(name, "gamma"))
1905 vo_gamma = value;
1906 else
1907 return VO_NOTIMPL;
1909 brightness = 0.01 * vo_brightness;
1910 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1911 gamma = pow(2, -0.02 * vo_gamma);
1913 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1914 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
1915 green_mask;
1916 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
1918 /* now recalculate the colormap using the newly set value */
1919 for (k = 0; k < cm_size; k++)
1921 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
1922 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
1923 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
1926 XStoreColors(vo->x11->display, cmap, cols, cm_size);
1927 XFlush(vo->x11->display);
1928 return VO_TRUE;
1931 uint32_t vo_x11_get_equalizer(const char *name, int *value)
1933 if (cmap == None)
1934 return VO_NOTAVAIL;
1935 if (!strcasecmp(name, "brightness"))
1936 *value = vo_brightness;
1937 else if (!strcasecmp(name, "contrast"))
1938 *value = vo_contrast;
1939 else if (!strcasecmp(name, "gamma"))
1940 *value = vo_gamma;
1941 else
1942 return VO_NOTIMPL;
1943 return VO_TRUE;
1946 #ifdef CONFIG_XV
1947 int vo_xv_set_eq(struct vo *vo, uint32_t xv_port, const char *name, int value)
1949 XvAttribute *attributes;
1950 int i, howmany, xv_atom;
1952 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
1954 /* get available attributes */
1955 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
1956 for (i = 0; i < howmany && attributes; i++)
1957 if (attributes[i].flags & XvSettable)
1959 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
1960 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1961 then trigger it if it's ok so that the other values are at default upon query */
1962 if (xv_atom != None)
1964 int hue = 0, port_value, port_min, port_max;
1966 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1967 (!strcasecmp(name, "brightness")))
1968 port_value = value;
1969 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1970 (!strcasecmp(name, "contrast")))
1971 port_value = value;
1972 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1973 (!strcasecmp(name, "saturation")))
1974 port_value = value;
1975 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1976 (!strcasecmp(name, "hue")))
1978 port_value = value;
1979 hue = 1;
1980 } else
1981 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1982 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1983 (!strcasecmp(name, "red_intensity")))
1984 port_value = value;
1985 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1986 && (!strcasecmp(name, "green_intensity")))
1987 port_value = value;
1988 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1989 && (!strcasecmp(name, "blue_intensity")))
1990 port_value = value;
1991 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
1992 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
1993 && (!strcasecmp(name, "bt_709")))
1994 port_value = value;
1995 else
1996 continue;
1998 port_min = attributes[i].min_value;
1999 port_max = attributes[i].max_value;
2001 /* nvidia hue workaround */
2002 if (hue && port_min == 0 && port_max == 360)
2004 port_value =
2005 (port_value >=
2006 0) ? (port_value - 100) : (port_value + 100);
2008 // -100 -> min
2009 // 0 -> (max+min)/2
2010 // +100 -> max
2011 port_value =
2012 (port_value + 100) * (port_max - port_min) / 200 +
2013 port_min;
2014 XvSetPortAttribute(vo->x11->display, xv_port, xv_atom, port_value);
2015 return VO_TRUE;
2018 return VO_FALSE;
2021 int vo_xv_get_eq(struct vo *vo, uint32_t xv_port, const char *name, int *value)
2024 XvAttribute *attributes;
2025 int i, howmany, xv_atom;
2027 /* get available attributes */
2028 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
2029 for (i = 0; i < howmany && attributes; i++)
2030 if (attributes[i].flags & XvGettable)
2032 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
2033 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2034 then trigger it if it's ok so that the other values are at default upon query */
2035 if (xv_atom != None)
2037 int val, port_value = 0, port_min, port_max;
2039 XvGetPortAttribute(vo->x11->display, xv_port, xv_atom,
2040 &port_value);
2042 port_min = attributes[i].min_value;
2043 port_max = attributes[i].max_value;
2044 val =
2045 (port_value - port_min) * 200 / (port_max - port_min) -
2046 100;
2048 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2049 (!strcasecmp(name, "brightness")))
2050 *value = val;
2051 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2052 (!strcasecmp(name, "contrast")))
2053 *value = val;
2054 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2055 (!strcasecmp(name, "saturation")))
2056 *value = val;
2057 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2058 (!strcasecmp(name, "hue")))
2060 /* nasty nvidia detect */
2061 if (port_min == 0 && port_max == 360)
2062 *value = (val >= 0) ? (val - 100) : (val + 100);
2063 else
2064 *value = val;
2065 } else
2066 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2067 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2068 (!strcasecmp(name, "red_intensity")))
2069 *value = val;
2070 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2071 && (!strcasecmp(name, "green_intensity")))
2072 *value = val;
2073 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2074 && (!strcasecmp(name, "blue_intensity")))
2075 *value = val;
2076 else if ((!strcmp(attributes[i].name, "XV_ITURBT_709") //NVIDIA
2077 || !strcmp(attributes[i].name, "XV_COLORSPACE"))//ATI
2078 && (!strcasecmp(name, "bt_709")))
2079 *value = val;
2080 else
2081 continue;
2083 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2084 name, *value);
2085 return VO_TRUE;
2088 return VO_FALSE;
2092 * \brief Interns the requested atom if it is available.
2094 * \param atom_name String containing the name of the requested atom.
2096 * \return Returns the atom if available, else None is returned.
2099 static Atom xv_intern_atom_if_exists(struct vo_x11_state *x11,
2100 char const *atom_name)
2102 XvAttribute * attributes;
2103 int attrib_count,i;
2104 Atom xv_atom = None;
2106 attributes = XvQueryPortAttributes(x11->display, x11->xv_port, &attrib_count );
2107 if( attributes!=NULL )
2109 for ( i = 0; i < attrib_count; ++i )
2111 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2113 xv_atom = XInternAtom(x11->display, atom_name, False );
2114 break; // found what we want, break out
2117 XFree( attributes );
2120 return xv_atom;
2124 * \brief Try to enable vsync for xv.
2125 * \return Returns -1 if not available, 0 on failure and 1 on success.
2127 int vo_xv_enable_vsync(struct vo *vo)
2129 struct vo_x11_state *x11 = vo->x11;
2130 Atom xv_atom = xv_intern_atom_if_exists(x11, "XV_SYNC_TO_VBLANK");
2131 if (xv_atom == None)
2132 return -1;
2133 return XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1) == Success;
2137 * \brief Get maximum supported source image dimensions.
2139 * This function does not set the variables pointed to by
2140 * width and height if the information could not be retrieved,
2141 * so the caller is reponsible for properly initializing them.
2143 * \param width [out] The maximum width gets stored here.
2144 * \param height [out] The maximum height gets stored here.
2147 void vo_xv_get_max_img_dim(struct vo *vo, uint32_t * width, uint32_t * height)
2149 struct vo_x11_state *x11 = vo->x11;
2150 XvEncodingInfo * encodings;
2151 //unsigned long num_encodings, idx; to int or too long?!
2152 unsigned int num_encodings, idx;
2154 XvQueryEncodings(x11->display, x11->xv_port, &num_encodings, &encodings);
2156 if ( encodings )
2158 for ( idx = 0; idx < num_encodings; ++idx )
2160 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2162 *width = encodings[idx].width;
2163 *height = encodings[idx].height;
2164 break;
2169 mp_msg( MSGT_VO, MSGL_V,
2170 "[xv common] Maximum source image dimensions: %ux%u\n",
2171 *width, *height );
2173 XvFreeEncodingInfo( encodings );
2177 * \brief Print information about the colorkey method and source.
2179 * \param ck_handling Integer value containing the information about
2180 * colorkey handling (see x11_common.h).
2182 * Outputs the content of |ck_handling| as a readable message.
2185 static void vo_xv_print_ck_info(struct vo_x11_state *x11)
2187 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2189 switch ( x11->xv_ck_info.method )
2191 case CK_METHOD_NONE:
2192 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2193 case CK_METHOD_AUTOPAINT:
2194 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2195 case CK_METHOD_MANUALFILL:
2196 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2197 case CK_METHOD_BACKGROUND:
2198 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2201 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2203 switch ( x11->xv_ck_info.source )
2205 case CK_SRC_CUR:
2206 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2207 x11->xv_colorkey );
2208 break;
2209 case CK_SRC_USE:
2210 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2212 mp_msg( MSGT_VO, MSGL_V,
2213 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2214 x11->xv_colorkey );
2216 else
2218 mp_msg( MSGT_VO, MSGL_V,
2219 "Using colorkey from MPlayer (0x%06lx)."
2220 " Use -colorkey to change.\n",
2221 x11->xv_colorkey );
2223 break;
2224 case CK_SRC_SET:
2225 mp_msg( MSGT_VO, MSGL_V,
2226 "Setting and using colorkey from MPlayer (0x%06lx)."
2227 " Use -colorkey to change.\n",
2228 x11->xv_colorkey );
2229 break;
2233 * \brief Init colorkey depending on the settings in xv_ck_info.
2235 * \return Returns 0 on failure and 1 on success.
2237 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2238 * flags in xv_ck_info.
2240 * Possiblilities:
2241 * * Methods
2242 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2243 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2244 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2245 * * Sources
2246 * - use currently set colorkey ( CK_SRC_CUR )
2247 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2248 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2250 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2251 * we don't draw anything as this means it was forced to off.
2253 int vo_xv_init_colorkey(struct vo *vo)
2255 struct vo_x11_state *x11 = vo->x11;
2256 Atom xv_atom;
2257 int rez;
2259 /* check if colorkeying is needed */
2260 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_COLORKEY");
2262 /* if we have to deal with colorkeying ... */
2263 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2265 /* check if we should use the colorkey specified in vo_colorkey */
2266 if ( x11->xv_ck_info.source != CK_SRC_CUR )
2268 x11->xv_colorkey = vo_colorkey;
2270 /* check if we have to set the colorkey too */
2271 if ( x11->xv_ck_info.source == CK_SRC_SET )
2273 xv_atom = XInternAtom(x11->display, "XV_COLORKEY",False);
2275 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, vo_colorkey);
2276 if ( rez != Success )
2278 mp_msg( MSGT_VO, MSGL_FATAL,
2279 "[xv common] Couldn't set colorkey!\n" );
2280 return 0; // error setting colorkey
2284 else
2286 int colorkey_ret;
2288 rez=XvGetPortAttribute(x11->display,x11->xv_port, xv_atom, &colorkey_ret);
2289 if ( rez == Success )
2291 x11->xv_colorkey = colorkey_ret;
2293 else
2295 mp_msg( MSGT_VO, MSGL_FATAL,
2296 "[xv common] Couldn't get colorkey!"
2297 "Maybe the selected Xv port has no overlay.\n" );
2298 return 0; // error getting colorkey
2302 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_AUTOPAINT_COLORKEY");
2304 /* should we draw the colorkey ourselves or activate autopainting? */
2305 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2307 rez = !Success; // reset rez to something different than Success
2309 if ( xv_atom != None ) // autopaint is supported
2311 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1);
2314 if ( rez != Success )
2316 // fallback to manual colorkey drawing
2317 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2320 else // disable colorkey autopainting if supported
2322 if ( xv_atom != None ) // we have autopaint attribute
2324 XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 0);
2328 else // do no colorkey drawing at all
2330 x11->xv_ck_info.method = CK_METHOD_NONE;
2331 } /* end: should we draw colorkey */
2333 /* output information about the current colorkey settings */
2334 vo_xv_print_ck_info(x11);
2336 return 1; // success
2340 * \brief Draw the colorkey on the video window.
2342 * Draws the colorkey depending on the set method ( colorkey_handling ).
2344 * Also draws the black bars ( when the video doesn't fit the display in
2345 * fullscreen ) separately, so they don't overlap with the video area.
2346 * It doesn't call XFlush.
2349 void vo_xv_draw_colorkey(struct vo *vo, int32_t x, int32_t y,
2350 int32_t w, int32_t h)
2352 struct vo_x11_state *x11 = vo->x11;
2353 if( x11->xv_ck_info.method == CK_METHOD_MANUALFILL ||
2354 x11->xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2356 XSetForeground(x11->display, x11->vo_gc, x11->xv_colorkey );
2357 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2358 x, y,
2359 w, h );
2363 /** \brief Tests if a valid argument for the ck suboption was given. */
2364 int xv_test_ck( void * arg )
2366 strarg_t * strarg = (strarg_t *)arg;
2368 if ( strargcmp( strarg, "use" ) == 0 ||
2369 strargcmp( strarg, "set" ) == 0 ||
2370 strargcmp( strarg, "cur" ) == 0 )
2372 return 1;
2375 return 0;
2377 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2378 int xv_test_ckm( void * arg )
2380 strarg_t * strarg = (strarg_t *)arg;
2382 if ( strargcmp( strarg, "bg" ) == 0 ||
2383 strargcmp( strarg, "man" ) == 0 ||
2384 strargcmp( strarg, "auto" ) == 0 )
2386 return 1;
2389 return 0;
2393 * \brief Modify the colorkey_handling var according to str
2395 * Checks if a valid pointer ( not NULL ) to the string
2396 * was given. And in that case modifies the colorkey_handling
2397 * var to reflect the requested behaviour.
2398 * If nothing happens the content of colorkey_handling stays
2399 * the same.
2401 * \param str Pointer to the string or NULL
2404 void xv_setup_colorkeyhandling(struct vo *vo, const char *ck_method_str,
2405 const char *ck_str)
2407 struct vo_x11_state *x11 = vo->x11;
2408 /* check if a valid pointer to the string was passed */
2409 if ( ck_str )
2411 if ( strncmp( ck_str, "use", 3 ) == 0 )
2413 x11->xv_ck_info.source = CK_SRC_USE;
2415 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2417 x11->xv_ck_info.source = CK_SRC_SET;
2420 /* check if a valid pointer to the string was passed */
2421 if ( ck_method_str )
2423 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2425 x11->xv_ck_info.method = CK_METHOD_BACKGROUND;
2427 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2429 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2431 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2433 x11->xv_ck_info.method = CK_METHOD_AUTOPAINT;
2438 #endif
2440 struct vo_x11_state *vo_x11_init_state(void)
2442 struct vo_x11_state *s = talloc_ptrtype(NULL, s);
2443 *s = (struct vo_x11_state){
2444 .xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR },
2445 .olddecor = MWM_DECOR_ALL,
2446 .oldfuncs = MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
2447 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE,
2448 .old_gravity = NorthWestGravity,
2450 return s;