Merge svn changes up to r27979
[mplayer.git] / libvo / x11_common.c
blob25bed4389a899eac60e9c25fc1eff16e3ef0962b
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <math.h>
5 #include <inttypes.h>
6 #include <limits.h>
8 #include "config.h"
9 #include "options.h"
10 #include "mp_msg.h"
11 #include "mp_fifo.h"
12 #include "x11_common.h"
13 #include "talloc.h"
15 #ifdef X11_FULLSCREEN
17 #include <string.h>
18 #include <unistd.h>
19 #include <assert.h>
21 #include "video_out.h"
22 #include "aspect.h"
23 #include "geometry.h"
24 #include "help_mp.h"
25 #include "osdep/timer.h"
27 #include <X11/Xmd.h>
28 #include <X11/Xlib.h>
29 #include <X11/Xutil.h>
30 #include <X11/Xatom.h>
32 #ifdef CONFIG_XSS
33 #include <X11/extensions/scrnsaver.h>
34 #endif
36 #ifdef CONFIG_XDPMS
37 #include <X11/extensions/dpms.h>
38 #endif
40 #ifdef CONFIG_XINERAMA
41 #include <X11/extensions/Xinerama.h>
42 #endif
44 #ifdef CONFIG_XF86VM
45 #include <X11/extensions/xf86vmode.h>
46 #endif
48 #ifdef CONFIG_XF86XK
49 #include <X11/XF86keysym.h>
50 #endif
52 #ifdef CONFIG_XV
53 #include <X11/extensions/Xv.h>
54 #include <X11/extensions/Xvlib.h>
56 #include "subopt-helper.h"
57 #endif
59 #include "input/input.h"
60 #include "input/mouse.h"
62 #ifdef CONFIG_GUI
63 #include "gui/interface.h"
64 #include "mplayer.h"
65 #endif
67 #define WIN_LAYER_ONBOTTOM 2
68 #define WIN_LAYER_NORMAL 4
69 #define WIN_LAYER_ONTOP 6
70 #define WIN_LAYER_ABOVE_DOCK 10
72 extern int enable_mouse_movements;
73 int fs_layer = WIN_LAYER_ABOVE_DOCK;
75 int stop_xscreensaver = 0;
77 static int dpms_disabled = 0;
79 char *mDisplayName = NULL;
81 char **vo_fstype_list;
83 /* 1 means that the WM is metacity (broken as hell) */
84 int metacity_hack = 0;
86 #ifdef CONFIG_XF86VM
87 XF86VidModeModeInfo **vidmodes = NULL;
88 XF86VidModeModeLine modeline;
89 #endif
91 static int vo_x11_get_fs_type(int supported);
92 static void saver_off(Display *);
93 static void saver_on(Display *);
96 * Sends the EWMH fullscreen state event.
98 * action: could be one of _NET_WM_STATE_REMOVE -- remove state
99 * _NET_WM_STATE_ADD -- add state
100 * _NET_WM_STATE_TOGGLE -- toggle
102 void vo_x11_ewmh_fullscreen(struct vo_x11_state *x11, int action)
104 assert(action == _NET_WM_STATE_REMOVE ||
105 action == _NET_WM_STATE_ADD || action == _NET_WM_STATE_TOGGLE);
107 if (x11->fs_type & vo_wm_FULLSCREEN)
109 XEvent xev;
111 /* init X event structure for _NET_WM_FULLSCREEN client message */
112 xev.xclient.type = ClientMessage;
113 xev.xclient.serial = 0;
114 xev.xclient.send_event = True;
115 xev.xclient.message_type = x11->XA_NET_WM_STATE;
116 xev.xclient.window = x11->window;
117 xev.xclient.format = 32;
118 xev.xclient.data.l[0] = action;
119 xev.xclient.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
120 xev.xclient.data.l[2] = 0;
121 xev.xclient.data.l[3] = 0;
122 xev.xclient.data.l[4] = 0;
124 /* finally send that damn thing */
125 if (!XSendEvent(x11->display, DefaultRootWindow(x11->display), False,
126 SubstructureRedirectMask | SubstructureNotifyMask,
127 &xev))
129 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_EwmhFullscreenStateFailed);
134 static void vo_hidecursor(Display * disp, Window win)
136 Cursor no_ptr;
137 Pixmap bm_no;
138 XColor black, dummy;
139 Colormap colormap;
140 const char bm_no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
142 if (WinID == 0)
143 return; // do not hide if playing on the root window
145 colormap = DefaultColormap(disp, DefaultScreen(disp));
146 if ( !XAllocNamedColor(disp, colormap, "black", &black, &dummy) )
148 return; // color alloc failed, give up
150 bm_no = XCreateBitmapFromData(disp, win, bm_no_data, 8, 8);
151 no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
152 XDefineCursor(disp, win, no_ptr);
153 XFreeCursor(disp, no_ptr);
154 if (bm_no != None)
155 XFreePixmap(disp, bm_no);
156 XFreeColors(disp,colormap,&black.pixel,1,0);
159 static void vo_showcursor(Display * disp, Window win)
161 if (WinID == 0)
162 return;
163 XDefineCursor(disp, win, 0);
166 static int x11_errorhandler(Display * display, XErrorEvent * event)
168 #define MSGLEN 60
169 char msg[MSGLEN];
171 XGetErrorText(display, event->error_code, (char *) &msg, MSGLEN);
173 mp_msg(MSGT_VO, MSGL_ERR, "X11 error: %s\n", msg);
175 mp_msg(MSGT_VO, MSGL_V,
176 "Type: %x, display: %p, resourceid: %lx, serial: %lx\n",
177 event->type, event->display, event->resourceid, event->serial);
178 mp_msg(MSGT_VO, MSGL_V,
179 "Error code: %x, request code: %x, minor code: %x\n",
180 event->error_code, event->request_code, event->minor_code);
182 // abort();
183 //exit_player("X11 error");
184 return 0;
185 #undef MSGLEN
188 void fstype_help(void)
190 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_AvailableFsType);
191 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FULL_SCREEN_TYPES\n");
193 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "none",
194 "don't set fullscreen window layer");
195 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer",
196 "use _WIN_LAYER hint with default layer");
197 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer=<0..15>",
198 "use _WIN_LAYER hint with a given layer number");
199 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "netwm",
200 "force NETWM style");
201 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "above",
202 "use _NETWM_STATE_ABOVE hint if available");
203 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "below",
204 "use _NETWM_STATE_BELOW hint if available");
205 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "fullscreen",
206 "use _NETWM_STATE_FULLSCREEN hint if availale");
207 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "stays_on_top",
208 "use _NETWM_STATE_STAYS_ON_TOP hint if available");
209 mp_msg(MSGT_VO, MSGL_INFO,
210 "You can also negate the settings with simply putting '-' in the beginning");
211 mp_msg(MSGT_VO, MSGL_INFO, "\n");
214 static void fstype_dump(int fstype)
216 if (fstype)
218 mp_msg(MSGT_VO, MSGL_V, "[x11] Current fstype setting honours");
219 if (fstype & vo_wm_LAYER)
220 mp_msg(MSGT_VO, MSGL_V, " LAYER");
221 if (fstype & vo_wm_FULLSCREEN)
222 mp_msg(MSGT_VO, MSGL_V, " FULLSCREEN");
223 if (fstype & vo_wm_STAYS_ON_TOP)
224 mp_msg(MSGT_VO, MSGL_V, " STAYS_ON_TOP");
225 if (fstype & vo_wm_ABOVE)
226 mp_msg(MSGT_VO, MSGL_V, " ABOVE");
227 if (fstype & vo_wm_BELOW)
228 mp_msg(MSGT_VO, MSGL_V, " BELOW");
229 mp_msg(MSGT_VO, MSGL_V, " X atoms\n");
230 } else
231 mp_msg(MSGT_VO, MSGL_V,
232 "[x11] Current fstype setting doesn't honour any X atoms\n");
235 static int net_wm_support_state_test(struct vo_x11_state *x11, Atom atom)
237 #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; } }
239 NET_WM_STATE_TEST(FULLSCREEN);
240 NET_WM_STATE_TEST(ABOVE);
241 NET_WM_STATE_TEST(STAYS_ON_TOP);
242 NET_WM_STATE_TEST(BELOW);
243 return 0;
246 static int x11_get_property(struct vo_x11_state *x11, Atom type, Atom ** args,
247 unsigned long *nitems)
249 int format;
250 unsigned long bytesafter;
252 return Success ==
253 XGetWindowProperty(x11->display, x11->rootwin, type, 0, 16384, False,
254 AnyPropertyType, &type, &format, nitems,
255 &bytesafter, (unsigned char **) args)
256 && *nitems > 0;
259 static int vo_wm_detect(struct vo *vo)
261 struct vo_x11_state *x11 = vo->x11;
262 int i;
263 int wm = 0;
264 unsigned long nitems;
265 Atom *args = NULL;
267 if (WinID >= 0)
268 return 0;
270 // -- supports layers
271 if (x11_get_property(x11, x11->XA_WIN_PROTOCOLS, &args, &nitems))
273 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports layers.\n");
274 for (i = 0; i < nitems; i++)
276 if (args[i] == x11->XA_WIN_LAYER)
278 wm |= vo_wm_LAYER;
279 metacity_hack |= 1;
280 } else
281 /* metacity is the only window manager I know which reports
282 * supporting only the _WIN_LAYER hint in _WIN_PROTOCOLS.
283 * (what's more support for it is broken) */
284 metacity_hack |= 2;
286 XFree(args);
287 if (wm && (metacity_hack == 1))
289 // metacity claims to support layers, but it is not the truth :-)
290 wm ^= vo_wm_LAYER;
291 mp_msg(MSGT_VO, MSGL_V,
292 "[x11] Using workaround for Metacity bugs.\n");
295 // --- netwm
296 if (x11_get_property(x11, x11->XA_NET_SUPPORTED, &args, &nitems))
298 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports NetWM.\n");
299 for (i = 0; i < nitems; i++)
300 wm |= net_wm_support_state_test(vo->x11, args[i]);
301 XFree(args);
304 if (wm == 0)
305 mp_msg(MSGT_VO, MSGL_V, "[x11] Unknown wm type...\n");
306 return wm;
309 #define XA_INIT(x) x11->XA##x = XInternAtom(x11->display, #x, False)
310 static void init_atoms(struct vo_x11_state *x11)
312 XA_INIT(_NET_SUPPORTED);
313 XA_INIT(_NET_WM_STATE);
314 XA_INIT(_NET_WM_STATE_FULLSCREEN);
315 XA_INIT(_NET_WM_STATE_ABOVE);
316 XA_INIT(_NET_WM_STATE_STAYS_ON_TOP);
317 XA_INIT(_NET_WM_STATE_BELOW);
318 XA_INIT(_NET_WM_PID);
319 XA_INIT(_WIN_PROTOCOLS);
320 XA_INIT(_WIN_LAYER);
321 XA_INIT(_WIN_HINTS);
322 XA_INIT(WM_PROTOCOLS);
323 XA_INIT(WM_DELETE_WINDOW);
326 void update_xinerama_info(struct vo *vo) {
327 struct MPOpts *opts = vo->opts;
328 int screen = xinerama_screen;
329 xinerama_x = xinerama_y = 0;
330 #ifdef CONFIG_XINERAMA
331 if (screen >= -1 && XineramaIsActive(vo->x11->display))
333 XineramaScreenInfo *screens;
334 int num_screens;
336 screens = XineramaQueryScreens(vo->x11->display, &num_screens);
337 if (screen >= num_screens)
338 screen = num_screens - 1;
339 if (screen == -1) {
340 int x = vo->dx + vo->dwidth / 2;
341 int y = vo->dy + vo->dheight / 2;
342 for (screen = num_screens - 1; screen > 0; screen--) {
343 int left = screens[screen].x_org;
344 int right = left + screens[screen].width;
345 int top = screens[screen].y_org;
346 int bottom = top + screens[screen].height;
347 if (left <= x && x <= right && top <= y && y <= bottom)
348 break;
351 if (screen < 0)
352 screen = 0;
353 opts->vo_screenwidth = screens[screen].width;
354 opts->vo_screenheight = screens[screen].height;
355 xinerama_x = screens[screen].x_org;
356 xinerama_y = screens[screen].y_org;
358 XFree(screens);
360 #endif
361 aspect_save_screenres(vo, opts->vo_screenwidth, opts->vo_screenheight);
364 int vo_init(struct vo *vo)
366 struct MPOpts *opts = vo->opts;
367 struct vo_x11_state *x11 = vo->x11;
368 // int mScreen;
369 int depth, bpp;
370 unsigned int mask;
372 // char * DisplayName = ":0.0";
373 // Display * mDisplay;
374 XImage *mXImage = NULL;
376 // Window mRootWin;
377 XWindowAttributes attribs;
378 char *dispName;
380 if (vo_rootwin)
381 WinID = 0; // use root window
383 if (x11->depthonscreen)
385 saver_off(x11->display);
386 return 1; // already called
389 XSetErrorHandler(x11_errorhandler);
391 #if 0
392 if (!mDisplayName)
393 if (!(mDisplayName = getenv("DISPLAY")))
394 mDisplayName = strdup(":0.0");
395 #else
396 dispName = XDisplayName(mDisplayName);
397 #endif
399 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
401 x11->display = XOpenDisplay(dispName);
402 if (!x11->display)
404 mp_msg(MSGT_VO, MSGL_ERR,
405 "vo: couldn't open the X11 display (%s)!\n", dispName);
406 return 0;
408 x11->screen = DefaultScreen(x11->display); // screen ID
409 x11->rootwin = RootWindow(x11->display, x11->screen); // root window ID
411 init_atoms(vo->x11);
413 #ifdef CONFIG_XF86VM
415 int clock;
417 XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline);
418 if (!opts->vo_screenwidth)
419 opts->vo_screenwidth = modeline.hdisplay;
420 if (!opts->vo_screenheight)
421 opts->vo_screenheight = modeline.vdisplay;
423 #endif
425 if (!opts->vo_screenwidth)
426 opts->vo_screenwidth = DisplayWidth(x11->display, x11->screen);
427 if (!opts->vo_screenheight)
428 opts->vo_screenheight = DisplayHeight(x11->display, x11->screen);
430 // get color depth (from root window, or the best visual):
431 XGetWindowAttributes(x11->display, x11->rootwin, &attribs);
432 depth = attribs.depth;
434 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
436 Visual *visual;
438 depth = vo_find_depth_from_visuals(x11->display, x11->screen, &visual);
439 if (depth != -1)
440 mXImage = XCreateImage(x11->display, visual, depth, ZPixmap,
441 0, NULL, 1, 1, 8, 1);
442 } else
443 mXImage =
444 XGetImage(x11->display, x11->rootwin, 0, 0, 1, 1, AllPlanes, ZPixmap);
446 x11->depthonscreen = depth; // display depth on screen
448 // get bits/pixel from XImage structure:
449 if (mXImage == NULL)
451 mask = 0;
452 } else
455 * for the depth==24 case, the XImage structures might use
456 * 24 or 32 bits of data per pixel. The x11->depthonscreen
457 * field stores the amount of data per pixel in the
458 * XImage structure!
460 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
462 bpp = mXImage->bits_per_pixel;
463 if ((x11->depthonscreen + 7) / 8 != (bpp + 7) / 8)
464 x11->depthonscreen = bpp; // by A'rpi
465 mask =
466 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
467 mp_msg(MSGT_VO, MSGL_V,
468 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
469 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
470 XDestroyImage(mXImage);
472 if (((x11->depthonscreen + 7) / 8) == 2)
474 if (mask == 0x7FFF)
475 x11->depthonscreen = 15;
476 else if (mask == 0xFFFF)
477 x11->depthonscreen = 16;
479 // XCloseDisplay( mDisplay );
480 /* slightly improved local display detection AST */
481 if (strncmp(dispName, "unix:", 5) == 0)
482 dispName += 4;
483 else if (strncmp(dispName, "localhost:", 10) == 0)
484 dispName += 9;
485 if (*dispName == ':' && atoi(dispName + 1) < 10)
486 x11->display_is_local = 1;
487 else
488 x11->display_is_local = 0;
489 mp_msg(MSGT_VO, MSGL_V,
490 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
491 opts->vo_screenwidth, opts->vo_screenheight, depth, x11->depthonscreen,
492 dispName, x11->display_is_local ? "local" : "remote");
494 x11->wm_type = vo_wm_detect(vo);
496 x11->fs_type = vo_x11_get_fs_type(x11->wm_type);
498 fstype_dump(x11->fs_type);
500 saver_off(x11->display);
501 return 1;
504 void vo_uninit(struct vo_x11_state *x11)
506 if (!x11->display)
508 mp_msg(MSGT_VO, MSGL_V,
509 "vo: x11 uninit called but X11 not initialized..\n");
510 return;
512 // if( !vo_depthonscreen ) return;
513 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
514 XSetErrorHandler(NULL);
515 XCloseDisplay(x11->display);
516 x11->depthonscreen = 0;
517 x11->display = NULL;
518 talloc_free(x11);
521 #include "osdep/keycodes.h"
522 #include "wskeys.h"
524 #ifdef XF86XK_AudioPause
525 static void vo_x11_putkey_ext(struct vo *vo, int keysym)
527 struct mp_fifo *f = vo->key_fifo;
528 switch (keysym)
530 case XF86XK_MenuKB:
531 mplayer_put_key(f, KEY_MENU);
532 break;
533 case XF86XK_AudioPlay:
534 mplayer_put_key(f, KEY_PLAY);
535 break;
536 case XF86XK_AudioPause:
537 mplayer_put_key(f, KEY_PAUSE);
538 break;
539 case XF86XK_AudioStop:
540 mplayer_put_key(f, KEY_STOP);
541 break;
542 case XF86XK_AudioPrev:
543 mplayer_put_key(f, KEY_PREV);
544 break;
545 case XF86XK_AudioNext:
546 mplayer_put_key(f, KEY_NEXT);
547 break;
548 case XF86XK_AudioMute:
549 mplayer_put_key(f, KEY_MUTE);
550 break;
551 case XF86XK_AudioLowerVolume:
552 mplayer_put_key(f, KEY_VOLUME_DOWN);
553 break;
554 case XF86XK_AudioRaiseVolume:
555 mplayer_put_key(f, KEY_VOLUME_UP);
556 break;
557 default:
558 break;
561 #endif
563 void vo_x11_putkey(struct vo *vo, int key)
565 struct mp_fifo *f = vo->key_fifo;
566 switch (key)
568 case wsLeft:
569 mplayer_put_key(f, KEY_LEFT);
570 break;
571 case wsRight:
572 mplayer_put_key(f, KEY_RIGHT);
573 break;
574 case wsUp:
575 mplayer_put_key(f, KEY_UP);
576 break;
577 case wsDown:
578 mplayer_put_key(f, KEY_DOWN);
579 break;
580 case wsSpace:
581 mplayer_put_key(f, ' ');
582 break;
583 case wsEscape:
584 mplayer_put_key(f, KEY_ESC);
585 break;
586 case wsTab:
587 mplayer_put_key(f, KEY_TAB);
588 break;
589 case wsEnter:
590 mplayer_put_key(f, KEY_ENTER);
591 break;
592 case wsBackSpace:
593 mplayer_put_key(f, KEY_BS);
594 break;
595 case wsDelete:
596 mplayer_put_key(f, KEY_DELETE);
597 break;
598 case wsInsert:
599 mplayer_put_key(f, KEY_INSERT);
600 break;
601 case wsHome:
602 mplayer_put_key(f, KEY_HOME);
603 break;
604 case wsEnd:
605 mplayer_put_key(f, KEY_END);
606 break;
607 case wsPageUp:
608 mplayer_put_key(f, KEY_PAGE_UP);
609 break;
610 case wsPageDown:
611 mplayer_put_key(f, KEY_PAGE_DOWN);
612 break;
613 case wsF1:
614 mplayer_put_key(f, KEY_F + 1);
615 break;
616 case wsF2:
617 mplayer_put_key(f, KEY_F + 2);
618 break;
619 case wsF3:
620 mplayer_put_key(f, KEY_F + 3);
621 break;
622 case wsF4:
623 mplayer_put_key(f, KEY_F + 4);
624 break;
625 case wsF5:
626 mplayer_put_key(f, KEY_F + 5);
627 break;
628 case wsF6:
629 mplayer_put_key(f, KEY_F + 6);
630 break;
631 case wsF7:
632 mplayer_put_key(f, KEY_F + 7);
633 break;
634 case wsF8:
635 mplayer_put_key(f, KEY_F + 8);
636 break;
637 case wsF9:
638 mplayer_put_key(f, KEY_F + 9);
639 break;
640 case wsF10:
641 mplayer_put_key(f, KEY_F + 10);
642 break;
643 case wsF11:
644 mplayer_put_key(f, KEY_F + 11);
645 break;
646 case wsF12:
647 mplayer_put_key(f, KEY_F + 12);
648 break;
649 case wsMinus:
650 case wsGrayMinus:
651 mplayer_put_key(f, '-');
652 break;
653 case wsPlus:
654 case wsGrayPlus:
655 mplayer_put_key(f, '+');
656 break;
657 case wsGrayMul:
658 case wsMul:
659 mplayer_put_key(f, '*');
660 break;
661 case wsGrayDiv:
662 case wsDiv:
663 mplayer_put_key(f, '/');
664 break;
665 case wsLess:
666 mplayer_put_key(f, '<');
667 break;
668 case wsMore:
669 mplayer_put_key(f, '>');
670 break;
671 case wsGray0:
672 mplayer_put_key(f, KEY_KP0);
673 break;
674 case wsGrayEnd:
675 case wsGray1:
676 mplayer_put_key(f, KEY_KP1);
677 break;
678 case wsGrayDown:
679 case wsGray2:
680 mplayer_put_key(f, KEY_KP2);
681 break;
682 case wsGrayPgDn:
683 case wsGray3:
684 mplayer_put_key(f, KEY_KP3);
685 break;
686 case wsGrayLeft:
687 case wsGray4:
688 mplayer_put_key(f, KEY_KP4);
689 break;
690 case wsGray5Dup:
691 case wsGray5:
692 mplayer_put_key(f, KEY_KP5);
693 break;
694 case wsGrayRight:
695 case wsGray6:
696 mplayer_put_key(f, KEY_KP6);
697 break;
698 case wsGrayHome:
699 case wsGray7:
700 mplayer_put_key(f, KEY_KP7);
701 break;
702 case wsGrayUp:
703 case wsGray8:
704 mplayer_put_key(f, KEY_KP8);
705 break;
706 case wsGrayPgUp:
707 case wsGray9:
708 mplayer_put_key(f, KEY_KP9);
709 break;
710 case wsGrayDecimal:
711 mplayer_put_key(f, KEY_KPDEC);
712 break;
713 case wsGrayInsert:
714 mplayer_put_key(f, KEY_KPINS);
715 break;
716 case wsGrayDelete:
717 mplayer_put_key(f, KEY_KPDEL);
718 break;
719 case wsGrayEnter:
720 mplayer_put_key(f, KEY_KPENTER);
721 break;
722 case wsGrave:
723 mplayer_put_key(f, '`');
724 break;
725 case wsTilde:
726 mplayer_put_key(f, '~');
727 break;
728 case wsExclSign:
729 mplayer_put_key(f, '!');
730 break;
731 case wsAt:
732 mplayer_put_key(f, '@');
733 break;
734 case wsHash:
735 mplayer_put_key(f, '#');
736 break;
737 case wsDollar:
738 mplayer_put_key(f, '$');
739 break;
740 case wsPercent:
741 mplayer_put_key(f, '%');
742 break;
743 case wsCircumflex:
744 mplayer_put_key(f, '^');
745 break;
746 case wsAmpersand:
747 mplayer_put_key(f, '&');
748 break;
749 case wsobracket:
750 mplayer_put_key(f, '(');
751 break;
752 case wscbracket:
753 mplayer_put_key(f, ')');
754 break;
755 case wsUnder:
756 mplayer_put_key(f, '_');
757 break;
758 case wsocbracket:
759 mplayer_put_key(f, '{');
760 break;
761 case wsccbracket:
762 mplayer_put_key(f, '}');
763 break;
764 case wsColon:
765 mplayer_put_key(f, ':');
766 break;
767 case wsSemicolon:
768 mplayer_put_key(f, ';');
769 break;
770 case wsDblQuote:
771 mplayer_put_key(f, '\"');
772 break;
773 case wsAcute:
774 mplayer_put_key(f, '\'');
775 break;
776 case wsComma:
777 mplayer_put_key(f, ',');
778 break;
779 case wsPoint:
780 mplayer_put_key(f, '.');
781 break;
782 case wsQuestSign:
783 mplayer_put_key(f, '?');
784 break;
785 case wsBSlash:
786 mplayer_put_key(f, '\\');
787 break;
788 case wsPipe:
789 mplayer_put_key(f, '|');
790 break;
791 case wsEqual:
792 mplayer_put_key(f, '=');
793 break;
794 case wsosbrackets:
795 mplayer_put_key(f, '[');
796 break;
797 case wscsbrackets:
798 mplayer_put_key(f, ']');
799 break;
802 default:
803 if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') ||
804 (key >= '0' && key <= '9'))
805 mplayer_put_key(f, key);
811 // ----- Motif header: -------
813 #define MWM_HINTS_FUNCTIONS (1L << 0)
814 #define MWM_HINTS_DECORATIONS (1L << 1)
815 #define MWM_HINTS_INPUT_MODE (1L << 2)
816 #define MWM_HINTS_STATUS (1L << 3)
818 #define MWM_FUNC_ALL (1L << 0)
819 #define MWM_FUNC_RESIZE (1L << 1)
820 #define MWM_FUNC_MOVE (1L << 2)
821 #define MWM_FUNC_MINIMIZE (1L << 3)
822 #define MWM_FUNC_MAXIMIZE (1L << 4)
823 #define MWM_FUNC_CLOSE (1L << 5)
825 #define MWM_DECOR_ALL (1L << 0)
826 #define MWM_DECOR_BORDER (1L << 1)
827 #define MWM_DECOR_RESIZEH (1L << 2)
828 #define MWM_DECOR_TITLE (1L << 3)
829 #define MWM_DECOR_MENU (1L << 4)
830 #define MWM_DECOR_MINIMIZE (1L << 5)
831 #define MWM_DECOR_MAXIMIZE (1L << 6)
833 #define MWM_INPUT_MODELESS 0
834 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
835 #define MWM_INPUT_SYSTEM_MODAL 2
836 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
837 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
839 #define MWM_TEAROFF_WINDOW (1L<<0)
841 typedef struct
843 long flags;
844 long functions;
845 long decorations;
846 long input_mode;
847 long state;
848 } MotifWmHints;
850 static MotifWmHints vo_MotifWmHints;
851 static Atom vo_MotifHints = None;
853 void vo_x11_decoration(struct vo *vo, int d)
855 struct vo_x11_state *x11 = vo->x11;
856 Atom mtype;
857 int mformat;
858 unsigned long mn, mb;
860 if (!WinID)
861 return;
863 if (vo_fsmode & 8)
865 XSetTransientForHint(x11->display, x11->window,
866 RootWindow(x11->display, x11->screen));
869 vo_MotifHints = XInternAtom(x11->display, "_MOTIF_WM_HINTS", 0);
870 if (vo_MotifHints != None)
872 if (!d)
874 MotifWmHints *mhints = NULL;
876 XGetWindowProperty(x11->display, x11->window,
877 vo_MotifHints, 0, 20, False,
878 vo_MotifHints, &mtype, &mformat, &mn,
879 &mb, (unsigned char **) &mhints);
880 if (mhints)
882 if (mhints->flags & MWM_HINTS_DECORATIONS)
883 x11->olddecor = mhints->decorations;
884 if (mhints->flags & MWM_HINTS_FUNCTIONS)
885 x11->oldfuncs = mhints->functions;
886 XFree(mhints);
890 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
891 vo_MotifWmHints.flags =
892 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
893 if (d)
895 vo_MotifWmHints.functions = x11->oldfuncs;
896 d = x11->olddecor;
898 #if 0
899 vo_MotifWmHints.decorations =
900 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
901 #else
902 vo_MotifWmHints.decorations =
903 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
904 #endif
905 XChangeProperty(x11->display, x11->window, vo_MotifHints,
906 vo_MotifHints, 32,
907 PropModeReplace,
908 (unsigned char *) &vo_MotifWmHints,
909 (vo_fsmode & 4) ? 4 : 5);
913 void vo_x11_classhint(struct vo *vo, Window window, char *name)
915 struct vo_x11_state *x11 = vo->x11;
916 XClassHint wmClass;
917 pid_t pid = getpid();
919 wmClass.res_name = name;
920 wmClass.res_class = "MPlayer";
921 XSetClassHint(x11->display, window, &wmClass);
922 XChangeProperty(x11->display, window, x11->XA_NET_WM_PID, XA_CARDINAL,
923 32, PropModeReplace, (unsigned char *) &pid, 1);
926 void vo_x11_uninit(struct vo *vo)
928 struct vo_x11_state *x11 = vo->x11;
929 saver_on(x11->display);
930 if (x11->window != None)
931 vo_showcursor(x11->display, x11->window);
933 if (x11->f_gc)
935 XFreeGC(vo->x11->display, x11->f_gc);
936 x11->f_gc = NULL;
938 #ifdef CONFIG_GUI
939 /* destroy window only if it's not controlled by the GUI */
940 if (!use_gui)
941 #endif
943 if (x11->vo_gc)
945 XSetBackground(vo->x11->display, x11->vo_gc, 0);
946 XFreeGC(vo->x11->display, x11->vo_gc);
947 x11->vo_gc = NULL;
949 if (x11->window != None)
951 XClearWindow(x11->display, x11->window);
952 if (WinID < 0)
954 XEvent xev;
956 XUnmapWindow(x11->display, x11->window);
957 XDestroyWindow(x11->display, x11->window);
960 XNextEvent(x11->display, &xev);
962 while (xev.type != DestroyNotify
963 || xev.xdestroywindow.event != x11->window);
965 x11->window = None;
967 vo_fs = 0;
968 x11->vo_old_width = x11->vo_old_height = 0;
972 int vo_x11_check_events(struct vo *vo)
974 struct vo_x11_state *x11 = vo->x11;
975 struct MPOpts *opts = vo->opts;
976 Display *display = vo->x11->display;
977 int ret = 0;
978 XEvent Event;
979 char buf[100];
980 KeySym keySym;
982 // unsigned long vo_KeyTable[512];
984 if ((x11->vo_mouse_autohide) && x11->mouse_waiting_hide &&
985 (GetTimerMS() - x11->mouse_timer >= 1000)) {
986 vo_hidecursor(display, x11->window);
987 x11->mouse_waiting_hide = 0;
990 while (XPending(display))
992 XNextEvent(display, &Event);
993 #ifdef CONFIG_GUI
994 if (use_gui)
996 guiGetEvent(0, (char *) &Event);
997 if (x11->window != Event.xany.window)
998 continue;
1000 #endif
1001 // printf("\rEvent.type=%X \n",Event.type);
1002 switch (Event.type)
1004 case Expose:
1005 ret |= VO_EVENT_EXPOSE;
1006 break;
1007 case ConfigureNotify:
1008 // if (!vo_fs && (Event.xconfigure.width == opts->vo_screenwidth || Event.xconfigure.height == opts->vo_screenheight)) break;
1009 // if (vo_fs && Event.xconfigure.width != opts->vo_screenwidth && Event.xconfigure.height != opts->vo_screenheight) break;
1010 if (x11->window == None)
1011 break;
1012 vo_x11_update_geometry(vo);
1013 ret |= VO_EVENT_RESIZE;
1014 break;
1015 case KeyPress:
1017 int key;
1019 #ifdef CONFIG_GUI
1020 if ( use_gui ) { break; }
1021 #endif
1023 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
1024 &x11->compose_status);
1025 #ifdef XF86XK_AudioPause
1026 vo_x11_putkey_ext(vo, keySym);
1027 #endif
1028 key =
1029 ((keySym & 0xff00) !=
1030 0 ? ((keySym & 0x00ff) + 256) : (keySym));
1031 vo_x11_putkey(vo, key);
1032 ret |= VO_EVENT_KEYPRESS;
1034 break;
1035 case MotionNotify:
1036 if(enable_mouse_movements)
1038 char cmd_str[40];
1039 sprintf(cmd_str,"set_mouse_pos %i %i",Event.xmotion.x, Event.xmotion.y);
1040 mp_input_queue_cmd(vo->input_ctx,
1041 mp_input_parse_cmd(cmd_str));
1044 if (x11->vo_mouse_autohide)
1046 vo_showcursor(display, x11->window);
1047 x11->mouse_waiting_hide = 1;
1048 x11->mouse_timer = GetTimerMS();
1050 break;
1051 case ButtonPress:
1052 if (x11->vo_mouse_autohide)
1054 vo_showcursor(display, x11->window);
1055 x11->mouse_waiting_hide = 1;
1056 x11->mouse_timer = GetTimerMS();
1058 #ifdef CONFIG_GUI
1059 // Ignore mouse button 1-3 under GUI.
1060 if (use_gui && (Event.xbutton.button >= 1)
1061 && (Event.xbutton.button <= 3))
1062 break;
1063 #endif
1064 mplayer_put_key(vo->key_fifo,
1065 (MOUSE_BTN0 + Event.xbutton.button - 1)
1066 | MP_KEY_DOWN);
1067 break;
1068 case ButtonRelease:
1069 if (x11->vo_mouse_autohide)
1071 vo_showcursor(display, x11->window);
1072 x11->mouse_waiting_hide = 1;
1073 x11->mouse_timer = GetTimerMS();
1075 #ifdef CONFIG_GUI
1076 // Ignore mouse button 1-3 under GUI.
1077 if (use_gui && (Event.xbutton.button >= 1)
1078 && (Event.xbutton.button <= 3))
1079 break;
1080 #endif
1081 mplayer_put_key(vo->key_fifo,
1082 MOUSE_BTN0 + Event.xbutton.button - 1);
1083 break;
1084 case PropertyNotify:
1086 char *name =
1087 XGetAtomName(display, Event.xproperty.atom);
1089 if (!name)
1090 break;
1092 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
1094 XFree(name);
1096 break;
1097 case MapNotify:
1098 x11->vo_hint.win_gravity = x11->old_gravity;
1099 XSetWMNormalHints(display, x11->window, &x11->vo_hint);
1100 x11->fs_flip = 0;
1101 break;
1102 case ClientMessage:
1103 if (Event.xclient.message_type == x11->XAWM_PROTOCOLS &&
1104 Event.xclient.data.l[0] == x11->XAWM_DELETE_WINDOW)
1105 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
1106 break;
1109 return ret;
1113 * \brief sets the size and position of the non-fullscreen window.
1115 static void vo_x11_nofs_sizepos(struct vo *vo, int x, int y,
1116 int width, int height)
1118 struct vo_x11_state *x11 = vo->x11;
1119 vo_x11_sizehint(vo, x, y, width, height, 0);
1120 if (vo_fs) {
1121 x11->vo_old_x = x;
1122 x11->vo_old_y = y;
1123 x11->vo_old_width = width;
1124 x11->vo_old_height = height;
1126 else
1128 vo->dwidth = width;
1129 vo->dheight = height;
1130 XMoveResizeWindow(vo->x11->display, vo->x11->window, x, y, width, height);
1134 void vo_x11_sizehint(struct vo *vo, int x, int y, int width, int height, int max)
1136 struct vo_x11_state *x11 = vo->x11;
1137 x11->vo_hint.flags = 0;
1138 if (vo_keepaspect)
1140 x11->vo_hint.flags |= PAspect;
1141 x11->vo_hint.min_aspect.x = width;
1142 x11->vo_hint.min_aspect.y = height;
1143 x11->vo_hint.max_aspect.x = width;
1144 x11->vo_hint.max_aspect.y = height;
1147 x11->vo_hint.flags |= PPosition | PSize;
1148 x11->vo_hint.x = x;
1149 x11->vo_hint.y = y;
1150 x11->vo_hint.width = width;
1151 x11->vo_hint.height = height;
1152 if (max)
1154 x11->vo_hint.flags |= PMaxSize;
1155 x11->vo_hint.max_width = width;
1156 x11->vo_hint.max_height = height;
1157 } else
1159 x11->vo_hint.max_width = 0;
1160 x11->vo_hint.max_height = 0;
1163 // Set minimum height/width to 4 to avoid off-by-one errors
1164 // and because mga_vid requires a minimal size of 4 pixels.
1165 x11->vo_hint.flags |= PMinSize;
1166 x11->vo_hint.min_width = x11->vo_hint.min_height = 4;
1168 x11->vo_hint.flags |= PWinGravity;
1169 x11->vo_hint.win_gravity = StaticGravity;
1170 XSetWMNormalHints(x11->display, x11->window, &x11->vo_hint);
1173 static int vo_x11_get_gnome_layer(struct vo_x11_state *x11, Window win)
1175 Atom type;
1176 int format;
1177 unsigned long nitems;
1178 unsigned long bytesafter;
1179 unsigned short *args = NULL;
1181 if (XGetWindowProperty(x11->display, win, x11->XA_WIN_LAYER, 0, 16384,
1182 False, AnyPropertyType, &type, &format, &nitems,
1183 &bytesafter,
1184 (unsigned char **) &args) == Success
1185 && nitems > 0 && args)
1187 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
1188 *args);
1189 return *args;
1191 return WIN_LAYER_NORMAL;
1195 static Window vo_x11_create_smooth_window(struct vo_x11_state *x11, Window mRoot,
1196 Visual * vis, int x, int y,
1197 unsigned int width, unsigned int height,
1198 int depth, Colormap col_map)
1200 unsigned long xswamask = CWBorderPixel;
1201 XSetWindowAttributes xswa;
1202 Window ret_win;
1204 if (col_map != CopyFromParent)
1206 xswa.colormap = col_map;
1207 xswamask |= CWColormap;
1209 xswa.background_pixel = 0;
1210 xswa.border_pixel = 0;
1211 xswa.backing_store = NotUseful;
1212 xswa.bit_gravity = StaticGravity;
1214 ret_win =
1215 XCreateWindow(x11->display, x11->rootwin, x, y, width, height, 0, depth,
1216 CopyFromParent, vis, xswamask, &xswa);
1217 XSetWMProtocols(x11->display, ret_win, &x11->XAWM_DELETE_WINDOW, 1);
1218 if (!x11->f_gc)
1219 x11->f_gc = XCreateGC(x11->display, ret_win, 0, 0);
1220 XSetForeground(x11->display, x11->f_gc, 0);
1222 return ret_win;
1226 * \brief create and setup a window suitable for display
1227 * \param vis Visual to use for creating the window
1228 * \param x x position of window
1229 * \param y y position of window
1230 * \param width width of window
1231 * \param height height of window
1232 * \param flags flags for window creation.
1233 * Only VOFLAG_FULLSCREEN is supported so far.
1234 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1235 * \param classname name to use for the classhint
1236 * \param title title for the window
1238 * This also does the grunt-work like setting Window Manager hints etc.
1239 * If vo_window is already set it just moves and resizes it.
1241 void vo_x11_create_vo_window(struct vo *vo, XVisualInfo *vis, int x, int y,
1242 unsigned int width, unsigned int height, int flags,
1243 Colormap col_map,
1244 const char *classname, const char *title)
1246 struct MPOpts *opts = vo->opts;
1247 struct vo_x11_state *x11 = vo->x11;
1248 Display *mDisplay = vo->x11->display;
1249 XGCValues xgcv;
1250 if (WinID >= 0) {
1251 x11->window = WinID ? (Window)WinID : x11->rootwin;
1252 if (col_map != CopyFromParent) {
1253 unsigned long xswamask = CWColormap;
1254 XSetWindowAttributes xswa;
1255 xswa.colormap = col_map;
1256 XUnmapWindow(mDisplay, x11->window);
1257 XChangeWindowAttributes(mDisplay, x11->window, xswamask, &xswa);
1258 XMapWindow(mDisplay, x11->window);
1260 if (WinID) vo_x11_update_geometry(vo);
1261 vo_x11_selectinput_witherr(mDisplay, x11->window,
1262 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1263 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1264 goto final;
1266 if (x11->window == None) {
1267 XSizeHints hint;
1268 XEvent xev;
1269 vo_fs = 0;
1270 vo->dwidth = width;
1271 vo->dheight = height;
1272 x11->window = vo_x11_create_smooth_window(x11, x11->rootwin, vis->visual,
1273 x, y, width, height, vis->depth, col_map);
1274 vo_x11_classhint(vo, x11->window, classname);
1275 XStoreName(mDisplay, x11->window, title);
1276 vo_hidecursor(mDisplay, x11->window);
1277 XSelectInput(mDisplay, x11->window, StructureNotifyMask);
1278 hint.x = x; hint.y = y;
1279 hint.width = width; hint.height = height;
1280 hint.flags = PPosition | PSize;
1281 XSetStandardProperties(mDisplay, x11->window, title, title, None, NULL, 0, &hint);
1282 vo_x11_sizehint(vo, x, y, width, height, 0);
1283 if (!vo_border) vo_x11_decoration(vo, 0);
1284 // map window
1285 XMapWindow(mDisplay, x11->window);
1286 XClearWindow(mDisplay, x11->window);
1287 // wait for map
1288 do {
1289 XNextEvent(mDisplay, &xev);
1290 } while (xev.type != MapNotify || xev.xmap.event != x11->window);
1291 XSelectInput(mDisplay, x11->window, NoEventMask);
1292 XSync(mDisplay, False);
1293 vo_x11_selectinput_witherr(mDisplay, x11->window,
1294 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1295 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1297 if (opts->vo_ontop) vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1298 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, width, height);
1299 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1300 vo_x11_fullscreen(vo);
1301 final:
1302 if (x11->vo_gc != None)
1303 XFreeGC(mDisplay, x11->vo_gc);
1304 x11->vo_gc = XCreateGC(mDisplay, x11->window, GCForeground, &xgcv);
1305 XSync(mDisplay, False);
1306 x11->vo_mouse_autohide = 1;
1309 void vo_x11_clearwindow_part(struct vo *vo, Window vo_window,
1310 int img_width, int img_height, int use_fs)
1312 struct vo_x11_state *x11 = vo->x11;
1313 struct MPOpts *opts = vo->opts;
1314 Display *mDisplay = vo->x11->display;
1315 int u_dheight, u_dwidth, left_ov, left_ov2;
1317 if (!x11->f_gc)
1318 return;
1320 u_dheight = use_fs ? opts->vo_screenheight : vo->dheight;
1321 u_dwidth = use_fs ? opts->vo_screenwidth : vo->dwidth;
1322 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1323 return;
1325 left_ov = (u_dheight - img_height) / 2;
1326 left_ov2 = (u_dwidth - img_width) / 2;
1328 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, 0, u_dwidth, left_ov);
1329 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, u_dheight - left_ov - 1,
1330 u_dwidth, left_ov + 1);
1332 if (u_dwidth > img_width)
1334 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, left_ov, left_ov2,
1335 img_height);
1336 XFillRectangle(mDisplay, vo_window, x11->f_gc, u_dwidth - left_ov2 - 1,
1337 left_ov, left_ov2 + 1, img_height);
1340 XFlush(mDisplay);
1343 void vo_x11_clearwindow(struct vo *vo, Window vo_window)
1345 struct vo_x11_state *x11 = vo->x11;
1346 struct MPOpts *opts = vo->opts;
1347 if (!x11->f_gc)
1348 return;
1349 XFillRectangle(x11->display, vo_window, x11->f_gc, 0, 0,
1350 opts->vo_screenwidth, opts->vo_screenheight);
1352 XFlush(x11->display);
1356 void vo_x11_setlayer(struct vo *vo, Window vo_window, int layer)
1358 struct vo_x11_state *x11 = vo->x11;
1359 if (WinID >= 0)
1360 return;
1362 if (x11->fs_type & vo_wm_LAYER)
1364 XClientMessageEvent xev;
1366 if (!x11->orig_layer)
1367 x11->orig_layer = vo_x11_get_gnome_layer(x11, vo_window);
1369 memset(&xev, 0, sizeof(xev));
1370 xev.type = ClientMessage;
1371 xev.display = x11->display;
1372 xev.window = vo_window;
1373 xev.message_type = x11->XA_WIN_LAYER;
1374 xev.format = 32;
1375 xev.data.l[0] = layer ? fs_layer : x11->orig_layer; // if not fullscreen, stay on default layer
1376 xev.data.l[1] = CurrentTime;
1377 mp_msg(MSGT_VO, MSGL_V,
1378 "[x11] Layered style stay on top (layer %ld).\n",
1379 xev.data.l[0]);
1380 XSendEvent(x11->display, x11->rootwin, False, SubstructureNotifyMask,
1381 (XEvent *) & xev);
1382 } else if (x11->fs_type & vo_wm_NETWM)
1384 XClientMessageEvent xev;
1385 char *state;
1387 memset(&xev, 0, sizeof(xev));
1388 xev.type = ClientMessage;
1389 xev.message_type = x11->XA_NET_WM_STATE;
1390 xev.display = x11->display;
1391 xev.window = vo_window;
1392 xev.format = 32;
1393 xev.data.l[0] = layer;
1395 if (x11->fs_type & vo_wm_STAYS_ON_TOP)
1396 xev.data.l[1] = x11->XA_NET_WM_STATE_STAYS_ON_TOP;
1397 else if (x11->fs_type & vo_wm_ABOVE)
1398 xev.data.l[1] = x11->XA_NET_WM_STATE_ABOVE;
1399 else if (x11->fs_type & vo_wm_FULLSCREEN)
1400 xev.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
1401 else if (x11->fs_type & vo_wm_BELOW)
1402 // This is not fallback. We can safely assume that the situation
1403 // where only NETWM_STATE_BELOW is supported doesn't exist.
1404 xev.data.l[1] = x11->XA_NET_WM_STATE_BELOW;
1406 XSendEvent(x11->display, x11->rootwin, False, SubstructureRedirectMask,
1407 (XEvent *) & xev);
1408 state = XGetAtomName(x11->display, xev.data.l[1]);
1409 mp_msg(MSGT_VO, MSGL_V,
1410 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1411 layer, state);
1412 XFree(state);
1416 static int vo_x11_get_fs_type(int supported)
1418 int i;
1419 int type = supported;
1421 if (vo_fstype_list)
1423 i = 0;
1424 for (i = 0; vo_fstype_list[i]; i++)
1426 int neg = 0;
1427 char *arg = vo_fstype_list[i];
1429 if (vo_fstype_list[i][0] == '-')
1431 neg = 1;
1432 arg = vo_fstype_list[i] + 1;
1435 if (!strncmp(arg, "layer", 5))
1437 if (!neg && (arg[5] == '='))
1439 char *endptr = NULL;
1440 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1442 if (endptr && *endptr == '\0' && layer >= 0
1443 && layer <= 15)
1444 fs_layer = layer;
1446 if (neg)
1447 type &= ~vo_wm_LAYER;
1448 else
1449 type |= vo_wm_LAYER;
1450 } else if (!strcmp(arg, "above"))
1452 if (neg)
1453 type &= ~vo_wm_ABOVE;
1454 else
1455 type |= vo_wm_ABOVE;
1456 } else if (!strcmp(arg, "fullscreen"))
1458 if (neg)
1459 type &= ~vo_wm_FULLSCREEN;
1460 else
1461 type |= vo_wm_FULLSCREEN;
1462 } else if (!strcmp(arg, "stays_on_top"))
1464 if (neg)
1465 type &= ~vo_wm_STAYS_ON_TOP;
1466 else
1467 type |= vo_wm_STAYS_ON_TOP;
1468 } else if (!strcmp(arg, "below"))
1470 if (neg)
1471 type &= ~vo_wm_BELOW;
1472 else
1473 type |= vo_wm_BELOW;
1474 } else if (!strcmp(arg, "netwm"))
1476 if (neg)
1477 type &= ~vo_wm_NETWM;
1478 else
1479 type |= vo_wm_NETWM;
1480 } else if (!strcmp(arg, "none"))
1481 return 0;
1485 return type;
1489 * \brief update vo->dx, vo->dy, vo->dwidth and vo->dheight with current values of vo->x11->window
1490 * \return returns current color depth of vo->x11->window
1492 int vo_x11_update_geometry(struct vo *vo)
1494 struct vo_x11_state *x11 = vo->x11;
1495 unsigned depth, w, h;
1496 int dummy_int;
1497 Window dummy_win;
1498 XGetGeometry(x11->display, x11->window, &dummy_win, &dummy_int, &dummy_int,
1499 &w, &h, &dummy_int, &depth);
1500 if (w <= INT_MAX && h <= INT_MAX) {
1501 vo->dwidth = w;
1502 vo->dheight = h;
1504 XTranslateCoordinates(x11->display, x11->window, x11->rootwin, 0, 0,
1505 &vo->dx, &vo->dy, &dummy_win);
1506 return depth <= INT_MAX ? depth : 0;
1509 void vo_x11_fullscreen(struct vo *vo)
1511 struct MPOpts *opts = vo->opts;
1512 struct vo_x11_state *x11 = vo->x11;
1513 int x, y, w, h;
1515 if (WinID >= 0 || x11->fs_flip)
1516 return;
1518 if (vo_fs)
1520 // fs->win
1521 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1523 x = x11->vo_old_x;
1524 y = x11->vo_old_y;
1525 w = x11->vo_old_width;
1526 h = x11->vo_old_height;
1529 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1530 vo_fs = VO_FALSE;
1531 } else
1533 // win->fs
1534 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1536 vo_fs = VO_TRUE;
1537 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1539 x11->vo_old_x = vo->dx;
1540 x11->vo_old_y = vo->dy;
1541 x11->vo_old_width = vo->dwidth;
1542 x11->vo_old_height = vo->dheight;
1544 update_xinerama_info(vo);
1545 x = xinerama_x;
1546 y = xinerama_y;
1547 w = opts->vo_screenwidth;
1548 h = opts->vo_screenheight;
1551 long dummy;
1553 XGetWMNormalHints(x11->display, x11->window, &x11->vo_hint, &dummy);
1554 if (!(x11->vo_hint.flags & PWinGravity))
1555 x11->old_gravity = NorthWestGravity;
1556 else
1557 x11->old_gravity = x11->vo_hint.win_gravity;
1559 if (x11->wm_type == 0 && !(vo_fsmode & 16))
1561 XUnmapWindow(x11->display, x11->window); // required for MWM
1562 XWithdrawWindow(x11->display, x11->window, x11->screen);
1563 x11->fs_flip = 1;
1566 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1568 vo_x11_decoration(vo, vo_border && !vo_fs);
1569 vo_x11_sizehint(vo, x, y, w, h, 0);
1570 vo_x11_setlayer(vo, x11->window, vo_fs);
1573 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1575 /* some WMs lose ontop after fullscreen */
1576 if ((!(vo_fs)) & opts->vo_ontop)
1577 vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1579 XMapRaised(x11->display, x11->window);
1580 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1581 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1582 XRaiseWindow(x11->display, x11->window);
1583 XFlush(x11->display);
1586 void vo_x11_ontop(struct vo *vo)
1588 struct MPOpts *opts = vo->opts;
1589 opts->vo_ontop = !opts->vo_ontop;
1591 vo_x11_setlayer(vo, vo->x11->window, opts->vo_ontop);
1594 void vo_x11_border(struct vo *vo)
1596 vo_border = !vo_border;
1597 vo_x11_decoration(vo, vo_border && !vo_fs);
1601 * XScreensaver stuff
1604 static int screensaver_off;
1605 static unsigned int time_last;
1607 void xscreensaver_heartbeat(struct vo_x11_state *x11)
1609 unsigned int time = GetTimerMS();
1611 if (x11->display && screensaver_off && (time - time_last) > 30000)
1613 time_last = time;
1615 XResetScreenSaver(x11->display);
1619 static int xss_suspend(Display *mDisplay, Bool suspend)
1621 #ifndef CONFIG_XSS
1622 return 0;
1623 #else
1624 int event, error, major, minor;
1625 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1626 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1627 return 0;
1628 if (major < 1 || (major == 1 && minor < 1))
1629 return 0;
1630 XScreenSaverSuspend(mDisplay, suspend);
1631 return 1;
1632 #endif
1636 * End of XScreensaver stuff
1639 static void saver_on(Display * mDisplay)
1642 if (!screensaver_off)
1643 return;
1644 screensaver_off = 0;
1645 if (xss_suspend(mDisplay, False))
1646 return;
1647 #ifdef CONFIG_XDPMS
1648 if (dpms_disabled)
1650 int nothing;
1651 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1653 if (!DPMSEnable(mDisplay))
1654 { // restoring power saving settings
1655 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1656 } else
1658 // DPMS does not seem to be enabled unless we call DPMSInfo
1659 BOOL onoff;
1660 CARD16 state;
1662 DPMSForceLevel(mDisplay, DPMSModeOn);
1663 DPMSInfo(mDisplay, &state, &onoff);
1664 if (onoff)
1666 mp_msg(MSGT_VO, MSGL_V,
1667 "Successfully enabled DPMS\n");
1668 } else
1670 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1674 dpms_disabled = 0;
1676 #endif
1679 static void saver_off(Display * mDisplay)
1681 int nothing;
1683 if (screensaver_off)
1684 return;
1685 screensaver_off = 1;
1686 if (xss_suspend(mDisplay, True))
1687 return;
1688 #ifdef CONFIG_XDPMS
1689 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1691 BOOL onoff;
1692 CARD16 state;
1694 DPMSInfo(mDisplay, &state, &onoff);
1695 if (onoff)
1697 Status stat;
1699 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1700 dpms_disabled = 1;
1701 stat = DPMSDisable(mDisplay); // monitor powersave off
1702 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1705 #endif
1708 static XErrorHandler old_handler = NULL;
1709 static int selectinput_err = 0;
1710 static int x11_selectinput_errorhandler(Display * display,
1711 XErrorEvent * event)
1713 if (event->error_code == BadAccess)
1715 selectinput_err = 1;
1716 mp_msg(MSGT_VO, MSGL_ERR,
1717 "X11 error: BadAccess during XSelectInput Call\n");
1718 mp_msg(MSGT_VO, MSGL_ERR,
1719 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1720 /* If you think MPlayer should shutdown with this error,
1721 * comment out the following line */
1722 return 0;
1724 if (old_handler != NULL)
1725 old_handler(display, event);
1726 else
1727 x11_errorhandler(display, event);
1728 return 0;
1731 void vo_x11_selectinput_witherr(Display * display, Window w,
1732 long event_mask)
1734 XSync(display, False);
1735 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1736 selectinput_err = 0;
1737 if (vo_nomouse_input)
1739 XSelectInput(display, w,
1740 event_mask &
1741 (~(ButtonPressMask | ButtonReleaseMask)));
1742 } else
1744 XSelectInput(display, w, event_mask);
1746 XSync(display, False);
1747 XSetErrorHandler(old_handler);
1748 if (selectinput_err)
1750 mp_msg(MSGT_VO, MSGL_ERR,
1751 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1752 XSelectInput(display, w,
1753 event_mask &
1755 (ButtonPressMask | ButtonReleaseMask |
1756 PointerMotionMask)));
1760 #ifdef CONFIG_XF86VM
1761 void vo_vm_switch(struct vo *vo)
1763 struct vo_x11_state *x11 = vo->x11;
1764 struct MPOpts *opts = vo->opts;
1765 Display *mDisplay = x11->display;
1766 int vm_event, vm_error;
1767 int vm_ver, vm_rev;
1768 int i, j, have_vm = 0;
1769 int X = vo->dwidth, Y = vo->dheight;
1770 int modeline_width, modeline_height;
1772 int modecount;
1774 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1776 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1777 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1778 vm_rev);
1779 have_vm = 1;
1780 } else {
1781 mp_msg(MSGT_VO, MSGL_WARN,
1782 "XF86VidMode extension not available.\n");
1785 if (have_vm)
1787 if (vidmodes == NULL)
1788 XF86VidModeGetAllModeLines(mDisplay, x11->screen, &modecount,
1789 &vidmodes);
1790 j = 0;
1791 modeline_width = vidmodes[0]->hdisplay;
1792 modeline_height = vidmodes[0]->vdisplay;
1794 for (i = 1; i < modecount; i++)
1795 if ((vidmodes[i]->hdisplay >= X)
1796 && (vidmodes[i]->vdisplay >= Y))
1797 if ((vidmodes[i]->hdisplay <= modeline_width)
1798 && (vidmodes[i]->vdisplay <= modeline_height))
1800 modeline_width = vidmodes[i]->hdisplay;
1801 modeline_height = vidmodes[i]->vdisplay;
1802 j = i;
1805 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_SelectedVideoMode,
1806 modeline_width, modeline_height, X, Y);
1807 XF86VidModeLockModeSwitch(mDisplay, x11->screen, 0);
1808 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1809 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1811 // FIXME: all this is more of a hack than proper solution
1812 X = (opts->vo_screenwidth - modeline_width) / 2;
1813 Y = (opts->vo_screenheight - modeline_height) / 2;
1814 XF86VidModeSetViewPort(mDisplay, x11->screen, X, Y);
1815 vo->dx = X;
1816 vo->dy = Y;
1817 vo->dwidth = modeline_width;
1818 vo->dheight = modeline_height;
1819 aspect_save_screenres(vo, modeline_width, modeline_height);
1823 void vo_vm_close(struct vo *vo)
1825 Display *dpy = vo->x11->display;
1826 struct MPOpts *opts = vo->opts;
1827 #ifdef CONFIG_GUI
1828 if (vidmodes != NULL && vo->x11->vo_window != None)
1829 #else
1830 if (vidmodes != NULL)
1831 #endif
1833 int i, modecount;
1835 free(vidmodes);
1836 vidmodes = NULL;
1837 XF86VidModeGetAllModeLines(dpy, vo->x11->screen, &modecount,
1838 &vidmodes);
1839 for (i = 0; i < modecount; i++)
1840 if ((vidmodes[i]->hdisplay == opts->vo_screenwidth)
1841 && (vidmodes[i]->vdisplay == opts->vo_screenheight))
1843 mp_msg(MSGT_VO, MSGL_INFO,
1844 "Returning to original mode %dx%d\n",
1845 opts->vo_screenwidth, opts->vo_screenheight);
1846 break;
1849 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1850 XF86VidModeSwitchToMode(dpy, vo->x11->screen, vidmodes[i]);
1851 free(vidmodes);
1852 vidmodes = NULL;
1855 #endif
1857 #endif /* X11_FULLSCREEN */
1861 * Scan the available visuals on this Display/Screen. Try to find
1862 * the 'best' available TrueColor visual that has a decent color
1863 * depth (at least 15bit). If there are multiple visuals with depth
1864 * >= 15bit, we prefer visuals with a smaller color depth.
1866 int vo_find_depth_from_visuals(Display * dpy, int screen,
1867 Visual ** visual_return)
1869 XVisualInfo visual_tmpl;
1870 XVisualInfo *visuals;
1871 int nvisuals, i;
1872 int bestvisual = -1;
1873 int bestvisual_depth = -1;
1875 visual_tmpl.screen = screen;
1876 visual_tmpl.class = TrueColor;
1877 visuals = XGetVisualInfo(dpy,
1878 VisualScreenMask | VisualClassMask,
1879 &visual_tmpl, &nvisuals);
1880 if (visuals != NULL)
1882 for (i = 0; i < nvisuals; i++)
1884 mp_msg(MSGT_VO, MSGL_V,
1885 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1886 visuals[i].visualid, visuals[i].depth,
1887 visuals[i].red_mask, visuals[i].green_mask,
1888 visuals[i].blue_mask);
1890 * Save the visual index and its depth, if this is the first
1891 * truecolor visul, or a visual that is 'preferred' over the
1892 * previous 'best' visual.
1894 if (bestvisual_depth == -1
1895 || (visuals[i].depth >= 15
1896 && (visuals[i].depth < bestvisual_depth
1897 || bestvisual_depth < 15)))
1899 bestvisual = i;
1900 bestvisual_depth = visuals[i].depth;
1904 if (bestvisual != -1 && visual_return != NULL)
1905 *visual_return = visuals[bestvisual].visual;
1907 XFree(visuals);
1909 return bestvisual_depth;
1913 static Colormap cmap = None;
1914 static XColor cols[256];
1915 static int cm_size, red_mask, green_mask, blue_mask;
1918 Colormap vo_x11_create_colormap(struct vo *vo, XVisualInfo *vinfo)
1920 struct vo_x11_state *x11 = vo->x11;
1921 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1923 if (vinfo->class != DirectColor)
1924 return XCreateColormap(x11->display, x11->rootwin, vinfo->visual,
1925 AllocNone);
1927 /* can this function get called twice or more? */
1928 if (cmap)
1929 return cmap;
1930 cm_size = vinfo->colormap_size;
1931 red_mask = vinfo->red_mask;
1932 green_mask = vinfo->green_mask;
1933 blue_mask = vinfo->blue_mask;
1934 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1935 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1936 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1937 rvu = 65536ull * ru / (red_mask + ru);
1938 gvu = 65536ull * gu / (green_mask + gu);
1939 bvu = 65536ull * bu / (blue_mask + bu);
1940 r = g = b = 0;
1941 rv = gv = bv = 0;
1942 m = DoRed | DoGreen | DoBlue;
1943 for (k = 0; k < cm_size; k++)
1945 int t;
1947 cols[k].pixel = r | g | b;
1948 cols[k].red = rv;
1949 cols[k].green = gv;
1950 cols[k].blue = bv;
1951 cols[k].flags = m;
1952 t = (r + ru) & red_mask;
1953 if (t < r)
1954 m &= ~DoRed;
1955 r = t;
1956 t = (g + gu) & green_mask;
1957 if (t < g)
1958 m &= ~DoGreen;
1959 g = t;
1960 t = (b + bu) & blue_mask;
1961 if (t < b)
1962 m &= ~DoBlue;
1963 b = t;
1964 rv += rvu;
1965 gv += gvu;
1966 bv += bvu;
1968 cmap = XCreateColormap(x11->display, x11->rootwin, vinfo->visual, AllocAll);
1969 XStoreColors(x11->display, cmap, cols, cm_size);
1970 return cmap;
1974 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1975 * hue and red/green/blue intensity, but we cannot do saturation.
1976 * Currently only gamma, brightness and contrast are implemented.
1977 * Is there sufficient interest for hue and/or red/green/blue intensity?
1979 /* these values have range [-100,100] and are initially 0 */
1980 static int vo_gamma = 0;
1981 static int vo_brightness = 0;
1982 static int vo_contrast = 0;
1984 static int transform_color(float val,
1985 float brightness, float contrast, float gamma) {
1986 float s = pow(val, gamma);
1987 s = (s - 0.5) * contrast + 0.5;
1988 s += brightness;
1989 if (s < 0)
1990 s = 0;
1991 if (s > 1)
1992 s = 1;
1993 return (unsigned short) (s * 65535);
1996 uint32_t vo_x11_set_equalizer(struct vo *vo, char *name, int value)
1998 float gamma, brightness, contrast;
1999 float rf, gf, bf;
2000 int k;
2003 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
2004 * of TrueColor-ed window but be careful:
2005 * Unlike the colormaps, which are private for the X client
2006 * who created them and thus automatically destroyed on client
2007 * disconnect, this gamma ramp is a system-wide (X-server-wide)
2008 * setting and _must_ be restored before the process exits.
2009 * Unforunately when the process crashes (or gets killed
2010 * for some reason) it is impossible to restore the setting,
2011 * and such behaviour could be rather annoying for the users.
2013 if (cmap == None)
2014 return VO_NOTAVAIL;
2016 if (!strcasecmp(name, "brightness"))
2017 vo_brightness = value;
2018 else if (!strcasecmp(name, "contrast"))
2019 vo_contrast = value;
2020 else if (!strcasecmp(name, "gamma"))
2021 vo_gamma = value;
2022 else
2023 return VO_NOTIMPL;
2025 brightness = 0.01 * vo_brightness;
2026 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
2027 gamma = pow(2, -0.02 * vo_gamma);
2029 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
2030 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
2031 green_mask;
2032 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
2034 /* now recalculate the colormap using the newly set value */
2035 for (k = 0; k < cm_size; k++)
2037 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
2038 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
2039 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
2042 XStoreColors(vo->x11->display, cmap, cols, cm_size);
2043 XFlush(vo->x11->display);
2044 return VO_TRUE;
2047 uint32_t vo_x11_get_equalizer(char *name, int *value)
2049 if (cmap == None)
2050 return VO_NOTAVAIL;
2051 if (!strcasecmp(name, "brightness"))
2052 *value = vo_brightness;
2053 else if (!strcasecmp(name, "contrast"))
2054 *value = vo_contrast;
2055 else if (!strcasecmp(name, "gamma"))
2056 *value = vo_gamma;
2057 else
2058 return VO_NOTIMPL;
2059 return VO_TRUE;
2062 #ifdef CONFIG_XV
2063 int vo_xv_set_eq(struct vo *vo, uint32_t xv_port, char *name, int value)
2065 XvAttribute *attributes;
2066 int i, howmany, xv_atom;
2068 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
2070 /* get available attributes */
2071 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
2072 for (i = 0; i < howmany && attributes; i++)
2073 if (attributes[i].flags & XvSettable)
2075 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
2076 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2077 then trigger it if it's ok so that the other values are at default upon query */
2078 if (xv_atom != None)
2080 int hue = 0, port_value, port_min, port_max;
2082 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2083 (!strcasecmp(name, "brightness")))
2084 port_value = value;
2085 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2086 (!strcasecmp(name, "contrast")))
2087 port_value = value;
2088 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2089 (!strcasecmp(name, "saturation")))
2090 port_value = value;
2091 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2092 (!strcasecmp(name, "hue")))
2094 port_value = value;
2095 hue = 1;
2096 } else
2097 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2098 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2099 (!strcasecmp(name, "red_intensity")))
2100 port_value = value;
2101 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2102 && (!strcasecmp(name, "green_intensity")))
2103 port_value = value;
2104 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2105 && (!strcasecmp(name, "blue_intensity")))
2106 port_value = value;
2107 else
2108 continue;
2110 port_min = attributes[i].min_value;
2111 port_max = attributes[i].max_value;
2113 /* nvidia hue workaround */
2114 if (hue && port_min == 0 && port_max == 360)
2116 port_value =
2117 (port_value >=
2118 0) ? (port_value - 100) : (port_value + 100);
2120 // -100 -> min
2121 // 0 -> (max+min)/2
2122 // +100 -> max
2123 port_value =
2124 (port_value + 100) * (port_max - port_min) / 200 +
2125 port_min;
2126 XvSetPortAttribute(vo->x11->display, xv_port, xv_atom, port_value);
2127 return VO_TRUE;
2130 return VO_FALSE;
2133 int vo_xv_get_eq(struct vo *vo, uint32_t xv_port, char *name, int *value)
2136 XvAttribute *attributes;
2137 int i, howmany, xv_atom;
2139 /* get available attributes */
2140 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
2141 for (i = 0; i < howmany && attributes; i++)
2142 if (attributes[i].flags & XvGettable)
2144 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
2145 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2146 then trigger it if it's ok so that the other values are at default upon query */
2147 if (xv_atom != None)
2149 int val, port_value = 0, port_min, port_max;
2151 XvGetPortAttribute(vo->x11->display, xv_port, xv_atom,
2152 &port_value);
2154 port_min = attributes[i].min_value;
2155 port_max = attributes[i].max_value;
2156 val =
2157 (port_value - port_min) * 200 / (port_max - port_min) -
2158 100;
2160 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2161 (!strcasecmp(name, "brightness")))
2162 *value = val;
2163 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2164 (!strcasecmp(name, "contrast")))
2165 *value = val;
2166 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2167 (!strcasecmp(name, "saturation")))
2168 *value = val;
2169 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2170 (!strcasecmp(name, "hue")))
2172 /* nasty nvidia detect */
2173 if (port_min == 0 && port_max == 360)
2174 *value = (val >= 0) ? (val - 100) : (val + 100);
2175 else
2176 *value = val;
2177 } else
2178 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2179 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2180 (!strcasecmp(name, "red_intensity")))
2181 *value = val;
2182 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2183 && (!strcasecmp(name, "green_intensity")))
2184 *value = val;
2185 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2186 && (!strcasecmp(name, "blue_intensity")))
2187 *value = val;
2188 else
2189 continue;
2191 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2192 name, *value);
2193 return VO_TRUE;
2196 return VO_FALSE;
2200 * \brief Interns the requested atom if it is available.
2202 * \param atom_name String containing the name of the requested atom.
2204 * \return Returns the atom if available, else None is returned.
2207 static Atom xv_intern_atom_if_exists(struct vo_x11_state *x11,
2208 char const *atom_name)
2210 XvAttribute * attributes;
2211 int attrib_count,i;
2212 Atom xv_atom = None;
2214 attributes = XvQueryPortAttributes(x11->display, x11->xv_port, &attrib_count );
2215 if( attributes!=NULL )
2217 for ( i = 0; i < attrib_count; ++i )
2219 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2221 xv_atom = XInternAtom(x11->display, atom_name, False );
2222 break; // found what we want, break out
2225 XFree( attributes );
2228 return xv_atom;
2232 * \brief Try to enable vsync for xv.
2233 * \return Returns -1 if not available, 0 on failure and 1 on success.
2235 int vo_xv_enable_vsync(struct vo *vo)
2237 struct vo_x11_state *x11 = vo->x11;
2238 Atom xv_atom = xv_intern_atom_if_exists(x11, "XV_SYNC_TO_VBLANK");
2239 if (xv_atom == None)
2240 return -1;
2241 return XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1) == Success;
2245 * \brief Get maximum supported source image dimensions.
2247 * This function does not set the variables pointed to by
2248 * width and height if the information could not be retrieved,
2249 * so the caller is reponsible for properly initializing them.
2251 * \param width [out] The maximum width gets stored here.
2252 * \param height [out] The maximum height gets stored here.
2255 void vo_xv_get_max_img_dim(struct vo *vo, uint32_t * width, uint32_t * height)
2257 struct vo_x11_state *x11 = vo->x11;
2258 XvEncodingInfo * encodings;
2259 //unsigned long num_encodings, idx; to int or too long?!
2260 unsigned int num_encodings, idx;
2262 XvQueryEncodings(x11->display, x11->xv_port, &num_encodings, &encodings);
2264 if ( encodings )
2266 for ( idx = 0; idx < num_encodings; ++idx )
2268 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2270 *width = encodings[idx].width;
2271 *height = encodings[idx].height;
2272 break;
2277 mp_msg( MSGT_VO, MSGL_V,
2278 "[xv common] Maximum source image dimensions: %ux%u\n",
2279 *width, *height );
2281 XvFreeEncodingInfo( encodings );
2285 * \brief Print information about the colorkey method and source.
2287 * \param ck_handling Integer value containing the information about
2288 * colorkey handling (see x11_common.h).
2290 * Outputs the content of |ck_handling| as a readable message.
2293 static void vo_xv_print_ck_info(struct vo_x11_state *x11)
2295 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2297 switch ( x11->xv_ck_info.method )
2299 case CK_METHOD_NONE:
2300 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2301 case CK_METHOD_AUTOPAINT:
2302 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2303 case CK_METHOD_MANUALFILL:
2304 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2305 case CK_METHOD_BACKGROUND:
2306 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2309 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2311 switch ( x11->xv_ck_info.source )
2313 case CK_SRC_CUR:
2314 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2315 x11->xv_colorkey );
2316 break;
2317 case CK_SRC_USE:
2318 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2320 mp_msg( MSGT_VO, MSGL_V,
2321 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2322 x11->xv_colorkey );
2324 else
2326 mp_msg( MSGT_VO, MSGL_V,
2327 "Using colorkey from MPlayer (0x%06lx)."
2328 " Use -colorkey to change.\n",
2329 x11->xv_colorkey );
2331 break;
2332 case CK_SRC_SET:
2333 mp_msg( MSGT_VO, MSGL_V,
2334 "Setting and using colorkey from MPlayer (0x%06lx)."
2335 " Use -colorkey to change.\n",
2336 x11->xv_colorkey );
2337 break;
2341 * \brief Init colorkey depending on the settings in xv_ck_info.
2343 * \return Returns 0 on failure and 1 on success.
2345 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2346 * flags in xv_ck_info.
2348 * Possiblilities:
2349 * * Methods
2350 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2351 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2352 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2353 * * Sources
2354 * - use currently set colorkey ( CK_SRC_CUR )
2355 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2356 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2358 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2359 * we don't draw anything as this means it was forced to off.
2361 int vo_xv_init_colorkey(struct vo *vo)
2363 struct vo_x11_state *x11 = vo->x11;
2364 Atom xv_atom;
2365 int rez;
2367 /* check if colorkeying is needed */
2368 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_COLORKEY");
2370 /* if we have to deal with colorkeying ... */
2371 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2373 /* check if we should use the colorkey specified in vo_colorkey */
2374 if ( x11->xv_ck_info.source != CK_SRC_CUR )
2376 x11->xv_colorkey = vo_colorkey;
2378 /* check if we have to set the colorkey too */
2379 if ( x11->xv_ck_info.source == CK_SRC_SET )
2381 xv_atom = XInternAtom(x11->display, "XV_COLORKEY",False);
2383 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, vo_colorkey);
2384 if ( rez != Success )
2386 mp_msg( MSGT_VO, MSGL_FATAL,
2387 "[xv common] Couldn't set colorkey!\n" );
2388 return 0; // error setting colorkey
2392 else
2394 int colorkey_ret;
2396 rez=XvGetPortAttribute(x11->display,x11->xv_port, xv_atom, &colorkey_ret);
2397 if ( rez == Success )
2399 x11->xv_colorkey = colorkey_ret;
2401 else
2403 mp_msg( MSGT_VO, MSGL_FATAL,
2404 "[xv common] Couldn't get colorkey!"
2405 "Maybe the selected Xv port has no overlay.\n" );
2406 return 0; // error getting colorkey
2410 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_AUTOPAINT_COLORKEY");
2412 /* should we draw the colorkey ourselves or activate autopainting? */
2413 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2415 rez = !Success; // reset rez to something different than Success
2417 if ( xv_atom != None ) // autopaint is supported
2419 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1);
2422 if ( rez != Success )
2424 // fallback to manual colorkey drawing
2425 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2428 else // disable colorkey autopainting if supported
2430 if ( xv_atom != None ) // we have autopaint attribute
2432 XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 0);
2436 else // do no colorkey drawing at all
2438 x11->xv_ck_info.method = CK_METHOD_NONE;
2439 } /* end: should we draw colorkey */
2441 /* output information about the current colorkey settings */
2442 vo_xv_print_ck_info(x11);
2444 return 1; // success
2448 * \brief Draw the colorkey on the video window.
2450 * Draws the colorkey depending on the set method ( colorkey_handling ).
2452 * Also draws the black bars ( when the video doesn't fit the display in
2453 * fullscreen ) separately, so they don't overlap with the video area.
2454 * It doesn't call XFlush.
2457 void vo_xv_draw_colorkey(struct vo *vo, int32_t x, int32_t y,
2458 int32_t w, int32_t h)
2460 struct MPOpts *opts = vo->opts;
2461 struct vo_x11_state *x11 = vo->x11;
2462 if( x11->xv_ck_info.method == CK_METHOD_MANUALFILL ||
2463 x11->xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2465 XSetForeground(x11->display, x11->vo_gc, x11->xv_colorkey );
2466 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2467 x, y,
2468 w, h );
2471 /* draw black bars if needed */
2472 /* TODO! move this to vo_x11_clearwindow_part() */
2473 if ( vo_fs )
2475 XSetForeground(x11->display, x11->vo_gc, 0 );
2476 /* making non-overlap fills, requires 8 checks instead of 4 */
2477 if ( y > 0 )
2478 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2479 0, 0,
2480 opts->vo_screenwidth, y);
2481 if (x > 0)
2482 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2483 0, 0,
2484 x, opts->vo_screenheight);
2485 if (x + w < opts->vo_screenwidth)
2486 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2487 x + w, 0,
2488 opts->vo_screenwidth, opts->vo_screenheight);
2489 if (y + h < opts->vo_screenheight)
2490 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2491 0, y + h,
2492 opts->vo_screenwidth, opts->vo_screenheight);
2496 /** \brief Tests if a valid argument for the ck suboption was given. */
2497 int xv_test_ck( void * arg )
2499 strarg_t * strarg = (strarg_t *)arg;
2501 if ( strargcmp( strarg, "use" ) == 0 ||
2502 strargcmp( strarg, "set" ) == 0 ||
2503 strargcmp( strarg, "cur" ) == 0 )
2505 return 1;
2508 return 0;
2510 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2511 int xv_test_ckm( void * arg )
2513 strarg_t * strarg = (strarg_t *)arg;
2515 if ( strargcmp( strarg, "bg" ) == 0 ||
2516 strargcmp( strarg, "man" ) == 0 ||
2517 strargcmp( strarg, "auto" ) == 0 )
2519 return 1;
2522 return 0;
2526 * \brief Modify the colorkey_handling var according to str
2528 * Checks if a valid pointer ( not NULL ) to the string
2529 * was given. And in that case modifies the colorkey_handling
2530 * var to reflect the requested behaviour.
2531 * If nothing happens the content of colorkey_handling stays
2532 * the same.
2534 * \param str Pointer to the string or NULL
2537 void xv_setup_colorkeyhandling(struct vo *vo, const char *ck_method_str,
2538 const char *ck_str)
2540 struct vo_x11_state *x11 = vo->x11;
2541 /* check if a valid pointer to the string was passed */
2542 if ( ck_str )
2544 if ( strncmp( ck_str, "use", 3 ) == 0 )
2546 x11->xv_ck_info.source = CK_SRC_USE;
2548 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2550 x11->xv_ck_info.source = CK_SRC_SET;
2553 /* check if a valid pointer to the string was passed */
2554 if ( ck_method_str )
2556 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2558 x11->xv_ck_info.method = CK_METHOD_BACKGROUND;
2560 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2562 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2564 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2566 x11->xv_ck_info.method = CK_METHOD_AUTOPAINT;
2571 #endif
2573 struct vo_x11_state *vo_x11_init_state(void)
2575 struct vo_x11_state *s = talloc_ptrtype(NULL, s);
2576 *s = (struct vo_x11_state){
2577 .xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR },
2578 .olddecor = MWM_DECOR_ALL,
2579 .oldfuncs = MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
2580 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE,
2581 .old_gravity = NorthWestGravity,
2583 return s;