Simplify, do not duplicate buffer size calculation
[mplayer/glamo.git] / libvo / x11_common.c
blobdcbac97bbc6ac5fa1cf7eb25e10bb61f8ee46eba
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 "mp_msg.h"
10 #include "mp_fifo.h"
11 #include "x11_common.h"
13 #ifdef X11_FULLSCREEN
15 #include <string.h>
16 #include <unistd.h>
17 #include <assert.h>
19 #include "video_out.h"
20 #include "aspect.h"
21 #include "geometry.h"
22 #include "help_mp.h"
23 #include "osdep/timer.h"
25 #include <X11/Xmd.h>
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <X11/Xatom.h>
30 #ifdef CONFIG_XSS
31 #include <X11/extensions/scrnsaver.h>
32 #endif
34 #ifdef CONFIG_XDPMS
35 #include <X11/extensions/dpms.h>
36 #endif
38 #ifdef CONFIG_XINERAMA
39 #include <X11/extensions/Xinerama.h>
40 #endif
42 #ifdef CONFIG_XF86VM
43 #include <X11/extensions/xf86vmode.h>
44 #endif
46 #ifdef CONFIG_XF86XK
47 #include <X11/XF86keysym.h>
48 #endif
50 #ifdef CONFIG_XV
51 #include <X11/extensions/Xv.h>
52 #include <X11/extensions/Xvlib.h>
54 #include "subopt-helper.h"
55 #endif
57 #include "input/input.h"
58 #include "input/mouse.h"
60 #ifdef CONFIG_GUI
61 #include "gui/interface.h"
62 #include "mplayer.h"
63 #endif
65 #define WIN_LAYER_ONBOTTOM 2
66 #define WIN_LAYER_NORMAL 4
67 #define WIN_LAYER_ONTOP 6
68 #define WIN_LAYER_ABOVE_DOCK 10
70 extern int enable_mouse_movements;
71 int fs_layer = WIN_LAYER_ABOVE_DOCK;
72 static int orig_layer = 0;
73 static int old_gravity = NorthWestGravity;
75 int stop_xscreensaver = 0;
77 static int dpms_disabled = 0;
79 char *mDisplayName = NULL;
80 Display *mDisplay = NULL;
81 Window mRootWin;
82 int mScreen;
83 int mLocalDisplay;
85 /* output window id */
86 int vo_mouse_autohide = 0;
87 int vo_wm_type = 0;
88 int vo_fs_type = 0; // needs to be accessible for GUI X11 code
89 static int vo_fs_flip = 0;
90 char **vo_fstype_list;
92 /* 1 means that the WM is metacity (broken as hell) */
93 int metacity_hack = 0;
95 static Atom XA_NET_SUPPORTED;
96 static Atom XA_NET_WM_STATE;
97 static Atom XA_NET_WM_STATE_FULLSCREEN;
98 static Atom XA_NET_WM_STATE_ABOVE;
99 static Atom XA_NET_WM_STATE_STAYS_ON_TOP;
100 static Atom XA_NET_WM_STATE_BELOW;
101 static Atom XA_NET_WM_PID;
102 static Atom XA_WIN_PROTOCOLS;
103 static Atom XA_WIN_LAYER;
104 static Atom XA_WIN_HINTS;
105 static Atom XAWM_PROTOCOLS;
106 static Atom XAWM_DELETE_WINDOW;
108 #define XA_INIT(x) XA##x = XInternAtom(mDisplay, #x, False)
110 static int vo_old_x = 0;
111 static int vo_old_y = 0;
112 static int vo_old_width = 0;
113 static int vo_old_height = 0;
115 #ifdef CONFIG_XF86VM
116 XF86VidModeModeInfo **vidmodes = NULL;
117 XF86VidModeModeLine modeline;
118 #endif
120 static int vo_x11_get_fs_type(int supported);
124 * Sends the EWMH fullscreen state event.
126 * action: could be one of _NET_WM_STATE_REMOVE -- remove state
127 * _NET_WM_STATE_ADD -- add state
128 * _NET_WM_STATE_TOGGLE -- toggle
130 void vo_x11_ewmh_fullscreen(int action)
132 assert(action == _NET_WM_STATE_REMOVE ||
133 action == _NET_WM_STATE_ADD || action == _NET_WM_STATE_TOGGLE);
135 if (vo_fs_type & vo_wm_FULLSCREEN)
137 XEvent xev;
139 /* init X event structure for _NET_WM_FULLSCREEN client message */
140 xev.xclient.type = ClientMessage;
141 xev.xclient.serial = 0;
142 xev.xclient.send_event = True;
143 xev.xclient.message_type = XA_NET_WM_STATE;
144 xev.xclient.window = vo_window;
145 xev.xclient.format = 32;
146 xev.xclient.data.l[0] = action;
147 xev.xclient.data.l[1] = XA_NET_WM_STATE_FULLSCREEN;
148 xev.xclient.data.l[2] = 0;
149 xev.xclient.data.l[3] = 0;
150 xev.xclient.data.l[4] = 0;
152 /* finally send that damn thing */
153 if (!XSendEvent(mDisplay, DefaultRootWindow(mDisplay), False,
154 SubstructureRedirectMask | SubstructureNotifyMask,
155 &xev))
157 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_EwmhFullscreenStateFailed);
162 void vo_hidecursor(Display * disp, Window win)
164 Cursor no_ptr;
165 Pixmap bm_no;
166 XColor black, dummy;
167 Colormap colormap;
168 static char bm_no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
170 if (WinID == 0)
171 return; // do not hide if playing on the root window
173 colormap = DefaultColormap(disp, DefaultScreen(disp));
174 if ( !XAllocNamedColor(disp, colormap, "black", &black, &dummy) )
176 return; // color alloc failed, give up
178 bm_no = XCreateBitmapFromData(disp, win, bm_no_data, 8, 8);
179 no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
180 XDefineCursor(disp, win, no_ptr);
181 XFreeCursor(disp, no_ptr);
182 if (bm_no != None)
183 XFreePixmap(disp, bm_no);
184 XFreeColors(disp,colormap,&black.pixel,1,0);
187 void vo_showcursor(Display * disp, Window win)
189 if (WinID == 0)
190 return;
191 XDefineCursor(disp, win, 0);
194 static int x11_errorhandler(Display * display, XErrorEvent * event)
196 #define MSGLEN 60
197 char msg[MSGLEN];
199 XGetErrorText(display, event->error_code, (char *) &msg, MSGLEN);
201 mp_msg(MSGT_VO, MSGL_ERR, "X11 error: %s\n", msg);
203 mp_msg(MSGT_VO, MSGL_V,
204 "Type: %x, display: %p, resourceid: %lx, serial: %lx\n",
205 event->type, event->display, event->resourceid, event->serial);
206 mp_msg(MSGT_VO, MSGL_V,
207 "Error code: %x, request code: %x, minor code: %x\n",
208 event->error_code, event->request_code, event->minor_code);
210 // abort();
211 //exit_player("X11 error");
212 return 0;
213 #undef MSGLEN
216 void fstype_help(void)
218 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_AvailableFsType);
219 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FULL_SCREEN_TYPES\n");
221 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "none",
222 "don't set fullscreen window layer");
223 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer",
224 "use _WIN_LAYER hint with default layer");
225 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer=<0..15>",
226 "use _WIN_LAYER hint with a given layer number");
227 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "netwm",
228 "force NETWM style");
229 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "above",
230 "use _NETWM_STATE_ABOVE hint if available");
231 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "below",
232 "use _NETWM_STATE_BELOW hint if available");
233 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "fullscreen",
234 "use _NETWM_STATE_FULLSCREEN hint if availale");
235 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "stays_on_top",
236 "use _NETWM_STATE_STAYS_ON_TOP hint if available");
237 mp_msg(MSGT_VO, MSGL_INFO,
238 "You can also negate the settings with simply putting '-' in the beginning");
239 mp_msg(MSGT_VO, MSGL_INFO, "\n");
242 static void fstype_dump(int fstype)
244 if (fstype)
246 mp_msg(MSGT_VO, MSGL_V, "[x11] Current fstype setting honours");
247 if (fstype & vo_wm_LAYER)
248 mp_msg(MSGT_VO, MSGL_V, " LAYER");
249 if (fstype & vo_wm_FULLSCREEN)
250 mp_msg(MSGT_VO, MSGL_V, " FULLSCREEN");
251 if (fstype & vo_wm_STAYS_ON_TOP)
252 mp_msg(MSGT_VO, MSGL_V, " STAYS_ON_TOP");
253 if (fstype & vo_wm_ABOVE)
254 mp_msg(MSGT_VO, MSGL_V, " ABOVE");
255 if (fstype & vo_wm_BELOW)
256 mp_msg(MSGT_VO, MSGL_V, " BELOW");
257 mp_msg(MSGT_VO, MSGL_V, " X atoms\n");
258 } else
259 mp_msg(MSGT_VO, MSGL_V,
260 "[x11] Current fstype setting doesn't honour any X atoms\n");
263 static int net_wm_support_state_test(Atom atom)
265 #define NET_WM_STATE_TEST(x) { if (atom == XA_NET_WM_STATE_##x) { mp_msg( MSGT_VO,MSGL_V, "[x11] Detected wm supports " #x " state.\n" ); return vo_wm_##x; } }
267 NET_WM_STATE_TEST(FULLSCREEN);
268 NET_WM_STATE_TEST(ABOVE);
269 NET_WM_STATE_TEST(STAYS_ON_TOP);
270 NET_WM_STATE_TEST(BELOW);
271 return 0;
274 static int x11_get_property(Atom type, Atom ** args, unsigned long *nitems)
276 int format;
277 unsigned long bytesafter;
279 return Success ==
280 XGetWindowProperty(mDisplay, mRootWin, type, 0, 16384, False,
281 AnyPropertyType, &type, &format, nitems,
282 &bytesafter, (unsigned char **) args)
283 && *nitems > 0;
286 static int vo_wm_detect(void)
288 int i;
289 int wm = 0;
290 unsigned long nitems;
291 Atom *args = NULL;
293 if (WinID >= 0)
294 return 0;
296 // -- supports layers
297 if (x11_get_property(XA_WIN_PROTOCOLS, &args, &nitems))
299 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports layers.\n");
300 for (i = 0; i < nitems; i++)
302 if (args[i] == XA_WIN_LAYER)
304 wm |= vo_wm_LAYER;
305 metacity_hack |= 1;
306 } else
307 /* metacity is the only window manager I know which reports
308 * supporting only the _WIN_LAYER hint in _WIN_PROTOCOLS.
309 * (what's more support for it is broken) */
310 metacity_hack |= 2;
312 XFree(args);
313 if (wm && (metacity_hack == 1))
315 // metacity claims to support layers, but it is not the truth :-)
316 wm ^= vo_wm_LAYER;
317 mp_msg(MSGT_VO, MSGL_V,
318 "[x11] Using workaround for Metacity bugs.\n");
321 // --- netwm
322 if (x11_get_property(XA_NET_SUPPORTED, &args, &nitems))
324 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports NetWM.\n");
325 for (i = 0; i < nitems; i++)
326 wm |= net_wm_support_state_test(args[i]);
327 XFree(args);
330 if (wm == 0)
331 mp_msg(MSGT_VO, MSGL_V, "[x11] Unknown wm type...\n");
332 return wm;
335 static void init_atoms(void)
337 XA_INIT(_NET_SUPPORTED);
338 XA_INIT(_NET_WM_STATE);
339 XA_INIT(_NET_WM_STATE_FULLSCREEN);
340 XA_INIT(_NET_WM_STATE_ABOVE);
341 XA_INIT(_NET_WM_STATE_STAYS_ON_TOP);
342 XA_INIT(_NET_WM_STATE_BELOW);
343 XA_INIT(_NET_WM_PID);
344 XA_INIT(_WIN_PROTOCOLS);
345 XA_INIT(_WIN_LAYER);
346 XA_INIT(_WIN_HINTS);
347 XA_INIT(WM_PROTOCOLS);
348 XA_INIT(WM_DELETE_WINDOW);
351 void update_xinerama_info(void) {
352 int screen = xinerama_screen;
353 xinerama_x = xinerama_y = 0;
354 #ifdef CONFIG_XINERAMA
355 if (screen >= -1 && XineramaIsActive(mDisplay))
357 XineramaScreenInfo *screens;
358 int num_screens;
360 screens = XineramaQueryScreens(mDisplay, &num_screens);
361 if (screen >= num_screens)
362 screen = num_screens - 1;
363 if (screen == -1) {
364 int x = vo_dx + vo_dwidth / 2;
365 int y = vo_dy + vo_dheight / 2;
366 for (screen = num_screens - 1; screen > 0; screen--) {
367 int left = screens[screen].x_org;
368 int right = left + screens[screen].width;
369 int top = screens[screen].y_org;
370 int bottom = top + screens[screen].height;
371 if (left <= x && x <= right && top <= y && y <= bottom)
372 break;
375 if (screen < 0)
376 screen = 0;
377 vo_screenwidth = screens[screen].width;
378 vo_screenheight = screens[screen].height;
379 xinerama_x = screens[screen].x_org;
380 xinerama_y = screens[screen].y_org;
382 XFree(screens);
384 #endif
385 aspect_save_screenres(vo_screenwidth, vo_screenheight);
388 int vo_init(void)
390 // int mScreen;
391 int depth, bpp;
392 unsigned int mask;
394 // char * DisplayName = ":0.0";
395 // Display * mDisplay;
396 XImage *mXImage = NULL;
398 // Window mRootWin;
399 XWindowAttributes attribs;
400 char *dispName;
402 if (vo_rootwin)
403 WinID = 0; // use root window
405 if (vo_depthonscreen)
407 saver_off(mDisplay);
408 return 1; // already called
411 XSetErrorHandler(x11_errorhandler);
413 #if 0
414 if (!mDisplayName)
415 if (!(mDisplayName = getenv("DISPLAY")))
416 mDisplayName = strdup(":0.0");
417 #else
418 dispName = XDisplayName(mDisplayName);
419 #endif
421 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
423 mDisplay = XOpenDisplay(dispName);
424 if (!mDisplay)
426 mp_msg(MSGT_VO, MSGL_ERR,
427 "vo: couldn't open the X11 display (%s)!\n", dispName);
428 return 0;
430 mScreen = DefaultScreen(mDisplay); // screen ID
431 mRootWin = RootWindow(mDisplay, mScreen); // root window ID
433 init_atoms();
435 #ifdef CONFIG_XF86VM
437 int clock;
439 XF86VidModeGetModeLine(mDisplay, mScreen, &clock, &modeline);
440 if (!vo_screenwidth)
441 vo_screenwidth = modeline.hdisplay;
442 if (!vo_screenheight)
443 vo_screenheight = modeline.vdisplay;
445 #endif
447 if (!vo_screenwidth)
448 vo_screenwidth = DisplayWidth(mDisplay, mScreen);
449 if (!vo_screenheight)
450 vo_screenheight = DisplayHeight(mDisplay, mScreen);
452 // get color depth (from root window, or the best visual):
453 XGetWindowAttributes(mDisplay, mRootWin, &attribs);
454 depth = attribs.depth;
456 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
458 Visual *visual;
460 depth = vo_find_depth_from_visuals(mDisplay, mScreen, &visual);
461 if (depth != -1)
462 mXImage = XCreateImage(mDisplay, visual, depth, ZPixmap,
463 0, NULL, 1, 1, 8, 1);
464 } else
465 mXImage =
466 XGetImage(mDisplay, mRootWin, 0, 0, 1, 1, AllPlanes, ZPixmap);
468 vo_depthonscreen = depth; // display depth on screen
470 // get bits/pixel from XImage structure:
471 if (mXImage == NULL)
473 mask = 0;
474 } else
477 * for the depth==24 case, the XImage structures might use
478 * 24 or 32 bits of data per pixel. The global variable
479 * vo_depthonscreen stores the amount of data per pixel in the
480 * XImage structure!
482 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
484 bpp = mXImage->bits_per_pixel;
485 if ((vo_depthonscreen + 7) / 8 != (bpp + 7) / 8)
486 vo_depthonscreen = bpp; // by A'rpi
487 mask =
488 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
489 mp_msg(MSGT_VO, MSGL_V,
490 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
491 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
492 XDestroyImage(mXImage);
494 if (((vo_depthonscreen + 7) / 8) == 2)
496 if (mask == 0x7FFF)
497 vo_depthonscreen = 15;
498 else if (mask == 0xFFFF)
499 vo_depthonscreen = 16;
501 // XCloseDisplay( mDisplay );
502 /* slightly improved local display detection AST */
503 if (strncmp(dispName, "unix:", 5) == 0)
504 dispName += 4;
505 else if (strncmp(dispName, "localhost:", 10) == 0)
506 dispName += 9;
507 if (*dispName == ':' && atoi(dispName + 1) < 10)
508 mLocalDisplay = 1;
509 else
510 mLocalDisplay = 0;
511 mp_msg(MSGT_VO, MSGL_V,
512 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
513 vo_screenwidth, vo_screenheight, depth, vo_depthonscreen,
514 dispName, mLocalDisplay ? "local" : "remote");
516 vo_wm_type = vo_wm_detect();
518 vo_fs_type = vo_x11_get_fs_type(vo_wm_type);
520 fstype_dump(vo_fs_type);
522 saver_off(mDisplay);
523 return 1;
526 void vo_uninit(void)
528 if (!mDisplay)
530 mp_msg(MSGT_VO, MSGL_V,
531 "vo: x11 uninit called but X11 not initialized..\n");
532 return;
534 // if( !vo_depthonscreen ) return;
535 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
536 XSetErrorHandler(NULL);
537 XCloseDisplay(mDisplay);
538 vo_depthonscreen = 0;
539 mDisplay = NULL;
542 #include "osdep/keycodes.h"
543 #include "wskeys.h"
545 #ifdef XF86XK_AudioPause
546 static void vo_x11_putkey_ext(int keysym)
548 switch (keysym)
550 case XF86XK_MenuKB:
551 mplayer_put_key(KEY_MENU);
552 break;
553 case XF86XK_AudioPlay:
554 mplayer_put_key(KEY_PLAY);
555 break;
556 case XF86XK_AudioPause:
557 mplayer_put_key(KEY_PAUSE);
558 break;
559 case XF86XK_AudioStop:
560 mplayer_put_key(KEY_STOP);
561 break;
562 case XF86XK_AudioPrev:
563 mplayer_put_key(KEY_PREV);
564 break;
565 case XF86XK_AudioNext:
566 mplayer_put_key(KEY_NEXT);
567 break;
568 case XF86XK_AudioMute:
569 mplayer_put_key(KEY_MUTE);
570 break;
571 case XF86XK_AudioLowerVolume:
572 mplayer_put_key(KEY_VOLUME_DOWN);
573 break;
574 case XF86XK_AudioRaiseVolume:
575 mplayer_put_key(KEY_VOLUME_UP);
576 break;
577 default:
578 break;
581 #endif
583 void vo_x11_putkey(int key)
585 switch (key)
587 case wsLeft:
588 mplayer_put_key(KEY_LEFT);
589 break;
590 case wsRight:
591 mplayer_put_key(KEY_RIGHT);
592 break;
593 case wsUp:
594 mplayer_put_key(KEY_UP);
595 break;
596 case wsDown:
597 mplayer_put_key(KEY_DOWN);
598 break;
599 case wsSpace:
600 mplayer_put_key(' ');
601 break;
602 case wsEscape:
603 mplayer_put_key(KEY_ESC);
604 break;
605 case wsTab:
606 mplayer_put_key(KEY_TAB);
607 break;
608 case wsEnter:
609 mplayer_put_key(KEY_ENTER);
610 break;
611 case wsBackSpace:
612 mplayer_put_key(KEY_BS);
613 break;
614 case wsDelete:
615 mplayer_put_key(KEY_DELETE);
616 break;
617 case wsInsert:
618 mplayer_put_key(KEY_INSERT);
619 break;
620 case wsHome:
621 mplayer_put_key(KEY_HOME);
622 break;
623 case wsEnd:
624 mplayer_put_key(KEY_END);
625 break;
626 case wsPageUp:
627 mplayer_put_key(KEY_PAGE_UP);
628 break;
629 case wsPageDown:
630 mplayer_put_key(KEY_PAGE_DOWN);
631 break;
632 case wsF1:
633 mplayer_put_key(KEY_F + 1);
634 break;
635 case wsF2:
636 mplayer_put_key(KEY_F + 2);
637 break;
638 case wsF3:
639 mplayer_put_key(KEY_F + 3);
640 break;
641 case wsF4:
642 mplayer_put_key(KEY_F + 4);
643 break;
644 case wsF5:
645 mplayer_put_key(KEY_F + 5);
646 break;
647 case wsF6:
648 mplayer_put_key(KEY_F + 6);
649 break;
650 case wsF7:
651 mplayer_put_key(KEY_F + 7);
652 break;
653 case wsF8:
654 mplayer_put_key(KEY_F + 8);
655 break;
656 case wsF9:
657 mplayer_put_key(KEY_F + 9);
658 break;
659 case wsF10:
660 mplayer_put_key(KEY_F + 10);
661 break;
662 case wsF11:
663 mplayer_put_key(KEY_F + 11);
664 break;
665 case wsF12:
666 mplayer_put_key(KEY_F + 12);
667 break;
668 case wsMinus:
669 case wsGrayMinus:
670 mplayer_put_key('-');
671 break;
672 case wsPlus:
673 case wsGrayPlus:
674 mplayer_put_key('+');
675 break;
676 case wsGrayMul:
677 case wsMul:
678 mplayer_put_key('*');
679 break;
680 case wsGrayDiv:
681 case wsDiv:
682 mplayer_put_key('/');
683 break;
684 case wsLess:
685 mplayer_put_key('<');
686 break;
687 case wsMore:
688 mplayer_put_key('>');
689 break;
690 case wsGray0:
691 mplayer_put_key(KEY_KP0);
692 break;
693 case wsGrayEnd:
694 case wsGray1:
695 mplayer_put_key(KEY_KP1);
696 break;
697 case wsGrayDown:
698 case wsGray2:
699 mplayer_put_key(KEY_KP2);
700 break;
701 case wsGrayPgDn:
702 case wsGray3:
703 mplayer_put_key(KEY_KP3);
704 break;
705 case wsGrayLeft:
706 case wsGray4:
707 mplayer_put_key(KEY_KP4);
708 break;
709 case wsGray5Dup:
710 case wsGray5:
711 mplayer_put_key(KEY_KP5);
712 break;
713 case wsGrayRight:
714 case wsGray6:
715 mplayer_put_key(KEY_KP6);
716 break;
717 case wsGrayHome:
718 case wsGray7:
719 mplayer_put_key(KEY_KP7);
720 break;
721 case wsGrayUp:
722 case wsGray8:
723 mplayer_put_key(KEY_KP8);
724 break;
725 case wsGrayPgUp:
726 case wsGray9:
727 mplayer_put_key(KEY_KP9);
728 break;
729 case wsGrayDecimal:
730 mplayer_put_key(KEY_KPDEC);
731 break;
732 case wsGrayInsert:
733 mplayer_put_key(KEY_KPINS);
734 break;
735 case wsGrayDelete:
736 mplayer_put_key(KEY_KPDEL);
737 break;
738 case wsGrayEnter:
739 mplayer_put_key(KEY_KPENTER);
740 break;
741 case wsGrave:
742 mplayer_put_key('`');
743 break;
744 case wsTilde:
745 mplayer_put_key('~');
746 break;
747 case wsExclSign:
748 mplayer_put_key('!');
749 break;
750 case wsAt:
751 mplayer_put_key('@');
752 break;
753 case wsHash:
754 mplayer_put_key('#');
755 break;
756 case wsDollar:
757 mplayer_put_key('$');
758 break;
759 case wsPercent:
760 mplayer_put_key('%');
761 break;
762 case wsCircumflex:
763 mplayer_put_key('^');
764 break;
765 case wsAmpersand:
766 mplayer_put_key('&');
767 break;
768 case wsobracket:
769 mplayer_put_key('(');
770 break;
771 case wscbracket:
772 mplayer_put_key(')');
773 break;
774 case wsUnder:
775 mplayer_put_key('_');
776 break;
777 case wsocbracket:
778 mplayer_put_key('{');
779 break;
780 case wsccbracket:
781 mplayer_put_key('}');
782 break;
783 case wsColon:
784 mplayer_put_key(':');
785 break;
786 case wsSemicolon:
787 mplayer_put_key(';');
788 break;
789 case wsDblQuote:
790 mplayer_put_key('\"');
791 break;
792 case wsAcute:
793 mplayer_put_key('\'');
794 break;
795 case wsComma:
796 mplayer_put_key(',');
797 break;
798 case wsPoint:
799 mplayer_put_key('.');
800 break;
801 case wsQuestSign:
802 mplayer_put_key('?');
803 break;
804 case wsBSlash:
805 mplayer_put_key('\\');
806 break;
807 case wsPipe:
808 mplayer_put_key('|');
809 break;
810 case wsEqual:
811 mplayer_put_key('=');
812 break;
813 case wsosbrackets:
814 mplayer_put_key('[');
815 break;
816 case wscsbrackets:
817 mplayer_put_key(']');
818 break;
821 default:
822 if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') ||
823 (key >= '0' && key <= '9'))
824 mplayer_put_key(key);
830 // ----- Motif header: -------
832 #define MWM_HINTS_FUNCTIONS (1L << 0)
833 #define MWM_HINTS_DECORATIONS (1L << 1)
834 #define MWM_HINTS_INPUT_MODE (1L << 2)
835 #define MWM_HINTS_STATUS (1L << 3)
837 #define MWM_FUNC_ALL (1L << 0)
838 #define MWM_FUNC_RESIZE (1L << 1)
839 #define MWM_FUNC_MOVE (1L << 2)
840 #define MWM_FUNC_MINIMIZE (1L << 3)
841 #define MWM_FUNC_MAXIMIZE (1L << 4)
842 #define MWM_FUNC_CLOSE (1L << 5)
844 #define MWM_DECOR_ALL (1L << 0)
845 #define MWM_DECOR_BORDER (1L << 1)
846 #define MWM_DECOR_RESIZEH (1L << 2)
847 #define MWM_DECOR_TITLE (1L << 3)
848 #define MWM_DECOR_MENU (1L << 4)
849 #define MWM_DECOR_MINIMIZE (1L << 5)
850 #define MWM_DECOR_MAXIMIZE (1L << 6)
852 #define MWM_INPUT_MODELESS 0
853 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
854 #define MWM_INPUT_SYSTEM_MODAL 2
855 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
856 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
858 #define MWM_TEAROFF_WINDOW (1L<<0)
860 typedef struct
862 long flags;
863 long functions;
864 long decorations;
865 long input_mode;
866 long state;
867 } MotifWmHints;
869 static MotifWmHints vo_MotifWmHints;
870 static Atom vo_MotifHints = None;
872 void vo_x11_decoration(Display * vo_Display, Window w, int d)
874 static unsigned int olddecor = MWM_DECOR_ALL;
875 static unsigned int oldfuncs =
876 MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
877 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE;
878 Atom mtype;
879 int mformat;
880 unsigned long mn, mb;
882 if (!WinID)
883 return;
885 if (vo_fsmode & 8)
887 XSetTransientForHint(vo_Display, w,
888 RootWindow(vo_Display, mScreen));
891 vo_MotifHints = XInternAtom(vo_Display, "_MOTIF_WM_HINTS", 0);
892 if (vo_MotifHints != None)
894 if (!d)
896 MotifWmHints *mhints = NULL;
898 XGetWindowProperty(vo_Display, w, vo_MotifHints, 0, 20, False,
899 vo_MotifHints, &mtype, &mformat, &mn,
900 &mb, (unsigned char **) &mhints);
901 if (mhints)
903 if (mhints->flags & MWM_HINTS_DECORATIONS)
904 olddecor = mhints->decorations;
905 if (mhints->flags & MWM_HINTS_FUNCTIONS)
906 oldfuncs = mhints->functions;
907 XFree(mhints);
911 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
912 vo_MotifWmHints.flags =
913 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
914 if (d)
916 vo_MotifWmHints.functions = oldfuncs;
917 d = olddecor;
919 #if 0
920 vo_MotifWmHints.decorations =
921 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
922 #else
923 vo_MotifWmHints.decorations =
924 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
925 #endif
926 XChangeProperty(vo_Display, w, vo_MotifHints, vo_MotifHints, 32,
927 PropModeReplace,
928 (unsigned char *) &vo_MotifWmHints,
929 (vo_fsmode & 4) ? 4 : 5);
933 void vo_x11_classhint(Display * display, Window window, char *name)
935 XClassHint wmClass;
936 pid_t pid = getpid();
938 wmClass.res_name = name;
939 wmClass.res_class = "MPlayer";
940 XSetClassHint(display, window, &wmClass);
941 XChangeProperty(display, window, XA_NET_WM_PID, XA_CARDINAL, 32,
942 PropModeReplace, (unsigned char *) &pid, 1);
945 Window vo_window = None;
946 GC vo_gc = NULL;
947 GC f_gc = NULL;
948 XSizeHints vo_hint;
950 #ifdef CONFIG_GUI
951 void vo_setwindow(Window w, GC g)
953 vo_window = w;
954 vo_gc = g;
956 #endif
958 void vo_x11_uninit(void)
960 saver_on(mDisplay);
961 if (vo_window != None)
962 vo_showcursor(mDisplay, vo_window);
964 if (f_gc)
966 XFreeGC(mDisplay, f_gc);
967 f_gc = NULL;
969 #ifdef CONFIG_GUI
970 /* destroy window only if it's not controlled by the GUI */
971 if (!use_gui)
972 #endif
974 if (vo_gc)
976 XSetBackground(mDisplay, vo_gc, 0);
977 XFreeGC(mDisplay, vo_gc);
978 vo_gc = NULL;
980 if (vo_window != None)
982 XClearWindow(mDisplay, vo_window);
983 if (WinID < 0)
985 XEvent xev;
987 XUnmapWindow(mDisplay, vo_window);
988 XDestroyWindow(mDisplay, vo_window);
991 XNextEvent(mDisplay, &xev);
993 while (xev.type != DestroyNotify
994 || xev.xdestroywindow.event != vo_window);
996 vo_window = None;
998 vo_fs = 0;
999 vo_old_width = vo_old_height = 0;
1003 static unsigned int mouse_timer;
1004 static int mouse_waiting_hide;
1006 int vo_x11_check_events(Display * mydisplay)
1008 int ret = 0;
1009 XEvent Event;
1010 char buf[100];
1011 KeySym keySym;
1012 static XComposeStatus stat;
1014 // unsigned long vo_KeyTable[512];
1016 if ((vo_mouse_autohide) && mouse_waiting_hide &&
1017 (GetTimerMS() - mouse_timer >= 1000)) {
1018 vo_hidecursor(mydisplay, vo_window);
1019 mouse_waiting_hide = 0;
1022 while (XPending(mydisplay))
1024 XNextEvent(mydisplay, &Event);
1025 #ifdef CONFIG_GUI
1026 if (use_gui)
1028 guiGetEvent(0, (char *) &Event);
1029 if (vo_window != Event.xany.window)
1030 continue;
1032 #endif
1033 // printf("\rEvent.type=%X \n",Event.type);
1034 switch (Event.type)
1036 case Expose:
1037 ret |= VO_EVENT_EXPOSE;
1038 break;
1039 case ConfigureNotify:
1040 // if (!vo_fs && (Event.xconfigure.width == vo_screenwidth || Event.xconfigure.height == vo_screenheight)) break;
1041 // if (vo_fs && Event.xconfigure.width != vo_screenwidth && Event.xconfigure.height != vo_screenheight) break;
1042 if (vo_window == None)
1043 break;
1044 vo_x11_update_geometry();
1045 ret |= VO_EVENT_RESIZE;
1046 break;
1047 case KeyPress:
1049 int key;
1051 #ifdef CONFIG_GUI
1052 if ( use_gui ) { break; }
1053 #endif
1055 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
1056 &stat);
1057 #ifdef XF86XK_AudioPause
1058 vo_x11_putkey_ext(keySym);
1059 #endif
1060 key =
1061 ((keySym & 0xff00) !=
1062 0 ? ((keySym & 0x00ff) + 256) : (keySym));
1063 vo_x11_putkey(key);
1064 ret |= VO_EVENT_KEYPRESS;
1066 break;
1067 case MotionNotify:
1068 if(enable_mouse_movements)
1070 char cmd_str[40];
1071 sprintf(cmd_str,"set_mouse_pos %i %i",Event.xmotion.x, Event.xmotion.y);
1072 mp_input_queue_cmd(mp_input_parse_cmd(cmd_str));
1075 if (vo_mouse_autohide)
1077 vo_showcursor(mydisplay, vo_window);
1078 mouse_waiting_hide = 1;
1079 mouse_timer = GetTimerMS();
1081 break;
1082 case ButtonPress:
1083 if (vo_mouse_autohide)
1085 vo_showcursor(mydisplay, vo_window);
1086 mouse_waiting_hide = 1;
1087 mouse_timer = GetTimerMS();
1089 #ifdef CONFIG_GUI
1090 // Ignore mouse button 1-3 under GUI.
1091 if (use_gui && (Event.xbutton.button >= 1)
1092 && (Event.xbutton.button <= 3))
1093 break;
1094 #endif
1095 mplayer_put_key((MOUSE_BTN0 + Event.xbutton.button -
1096 1) | MP_KEY_DOWN);
1097 break;
1098 case ButtonRelease:
1099 if (vo_mouse_autohide)
1101 vo_showcursor(mydisplay, vo_window);
1102 mouse_waiting_hide = 1;
1103 mouse_timer = GetTimerMS();
1105 #ifdef CONFIG_GUI
1106 // Ignore mouse button 1-3 under GUI.
1107 if (use_gui && (Event.xbutton.button >= 1)
1108 && (Event.xbutton.button <= 3))
1109 break;
1110 #endif
1111 mplayer_put_key(MOUSE_BTN0 + Event.xbutton.button - 1);
1112 break;
1113 case PropertyNotify:
1115 char *name =
1116 XGetAtomName(mydisplay, Event.xproperty.atom);
1118 if (!name)
1119 break;
1121 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
1123 XFree(name);
1125 break;
1126 case MapNotify:
1127 vo_hint.win_gravity = old_gravity;
1128 XSetWMNormalHints(mDisplay, vo_window, &vo_hint);
1129 vo_fs_flip = 0;
1130 break;
1131 case ClientMessage:
1132 if (Event.xclient.message_type == XAWM_PROTOCOLS &&
1133 Event.xclient.data.l[0] == XAWM_DELETE_WINDOW)
1134 mplayer_put_key(KEY_CLOSE_WIN);
1135 break;
1138 return ret;
1142 * \brief sets the size and position of the non-fullscreen window.
1144 void vo_x11_nofs_sizepos(int x, int y, int width, int height)
1146 vo_x11_sizehint(x, y, width, height, 0);
1147 if (vo_fs) {
1148 vo_old_x = x;
1149 vo_old_y = y;
1150 vo_old_width = width;
1151 vo_old_height = height;
1153 else
1155 vo_dwidth = width;
1156 vo_dheight = height;
1157 XMoveResizeWindow(mDisplay, vo_window, x, y, width, height);
1161 void vo_x11_sizehint(int x, int y, int width, int height, int max)
1163 vo_hint.flags = 0;
1164 if (vo_keepaspect)
1166 vo_hint.flags |= PAspect;
1167 vo_hint.min_aspect.x = width;
1168 vo_hint.min_aspect.y = height;
1169 vo_hint.max_aspect.x = width;
1170 vo_hint.max_aspect.y = height;
1173 vo_hint.flags |= PPosition | PSize;
1174 vo_hint.x = x;
1175 vo_hint.y = y;
1176 vo_hint.width = width;
1177 vo_hint.height = height;
1178 if (max)
1180 vo_hint.flags |= PMaxSize;
1181 vo_hint.max_width = width;
1182 vo_hint.max_height = height;
1183 } else
1185 vo_hint.max_width = 0;
1186 vo_hint.max_height = 0;
1189 // Set minimum height/width to 4 to avoid off-by-one errors
1190 // and because mga_vid requires a minimal size of 4 pixels.
1191 vo_hint.flags |= PMinSize;
1192 vo_hint.min_width = vo_hint.min_height = 4;
1194 // Set the base size. A window manager might display the window
1195 // size to the user relative to this.
1196 // Setting these to width/height might be nice, but e.g. fluxbox can't handle it.
1197 vo_hint.flags |= PBaseSize;
1198 vo_hint.base_width = 0 /*width*/;
1199 vo_hint.base_height = 0 /*height*/;
1201 vo_hint.flags |= PWinGravity;
1202 vo_hint.win_gravity = StaticGravity;
1203 XSetWMNormalHints(mDisplay, vo_window, &vo_hint);
1206 static int vo_x11_get_gnome_layer(Display * mDisplay, Window win)
1208 Atom type;
1209 int format;
1210 unsigned long nitems;
1211 unsigned long bytesafter;
1212 unsigned short *args = NULL;
1214 if (XGetWindowProperty(mDisplay, win, XA_WIN_LAYER, 0, 16384,
1215 False, AnyPropertyType, &type, &format, &nitems,
1216 &bytesafter,
1217 (unsigned char **) &args) == Success
1218 && nitems > 0 && args)
1220 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
1221 *args);
1222 return *args;
1224 return WIN_LAYER_NORMAL;
1228 Window vo_x11_create_smooth_window(Display * mDisplay, Window mRoot,
1229 Visual * vis, int x, int y,
1230 unsigned int width, unsigned int height,
1231 int depth, Colormap col_map)
1233 unsigned long xswamask = CWBorderPixel;
1234 XSetWindowAttributes xswa;
1235 Window ret_win;
1237 if (col_map != CopyFromParent)
1239 xswa.colormap = col_map;
1240 xswamask |= CWColormap;
1242 xswa.background_pixel = 0;
1243 xswa.border_pixel = 0;
1244 xswa.backing_store = NotUseful;
1245 xswa.bit_gravity = StaticGravity;
1247 ret_win =
1248 XCreateWindow(mDisplay, mRootWin, x, y, width, height, 0, depth,
1249 CopyFromParent, vis, xswamask, &xswa);
1250 XSetWMProtocols(mDisplay, ret_win, &XAWM_DELETE_WINDOW, 1);
1251 if (!f_gc)
1252 f_gc = XCreateGC(mDisplay, ret_win, 0, 0);
1253 XSetForeground(mDisplay, f_gc, 0);
1255 return ret_win;
1259 * \brief create and setup a window suitable for display
1260 * \param vis Visual to use for creating the window
1261 * \param x x position of window
1262 * \param y y position of window
1263 * \param width width of window
1264 * \param height height of window
1265 * \param flags flags for window creation.
1266 * Only VOFLAG_FULLSCREEN is supported so far.
1267 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1268 * \param classname name to use for the classhint
1269 * \param title title for the window
1271 * This also does the grunt-work like setting Window Manager hints etc.
1272 * If vo_window is already set it just moves and resizes it.
1274 void vo_x11_create_vo_window(XVisualInfo *vis, int x, int y,
1275 unsigned int width, unsigned int height, int flags,
1276 Colormap col_map,
1277 const char *classname, const char *title)
1279 XGCValues xgcv;
1280 if (WinID >= 0) {
1281 vo_window = WinID ? (Window)WinID : mRootWin;
1282 if (col_map != CopyFromParent) {
1283 unsigned long xswamask = CWColormap;
1284 XSetWindowAttributes xswa;
1285 xswa.colormap = col_map;
1286 XUnmapWindow(mDisplay, vo_window);
1287 XChangeWindowAttributes(mDisplay, vo_window, xswamask, &xswa);
1288 XMapWindow(mDisplay, vo_window);
1290 if (WinID) vo_x11_update_geometry();
1291 vo_x11_selectinput_witherr(mDisplay, vo_window,
1292 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1293 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1294 goto final;
1296 if (vo_window == None) {
1297 XSizeHints hint;
1298 XEvent xev;
1299 vo_fs = 0;
1300 vo_dwidth = width;
1301 vo_dheight = height;
1302 vo_window = vo_x11_create_smooth_window(mDisplay, mRootWin, vis->visual,
1303 x, y, width, height, vis->depth, col_map);
1304 vo_x11_classhint(mDisplay, vo_window, classname);
1305 XStoreName(mDisplay, vo_window, title);
1306 vo_hidecursor(mDisplay, vo_window);
1307 XSelectInput(mDisplay, vo_window, StructureNotifyMask);
1308 hint.x = x; hint.y = y;
1309 hint.width = width; hint.height = height;
1310 hint.flags = PPosition | PSize;
1311 XSetStandardProperties(mDisplay, vo_window, title, title, None, NULL, 0, &hint);
1312 vo_x11_sizehint(x, y, width, height, 0);
1313 if (!vo_border) vo_x11_decoration(mDisplay, vo_window, 0);
1314 // map window
1315 XMapWindow(mDisplay, vo_window);
1316 XClearWindow(mDisplay, vo_window);
1317 // wait for map
1318 do {
1319 XNextEvent(mDisplay, &xev);
1320 } while (xev.type != MapNotify || xev.xmap.event != vo_window);
1321 XSelectInput(mDisplay, vo_window, NoEventMask);
1322 XSync(mDisplay, False);
1323 vo_x11_selectinput_witherr(mDisplay, vo_window,
1324 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1325 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1327 if (vo_ontop) vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
1328 vo_x11_nofs_sizepos(vo_dx, vo_dy, width, height);
1329 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1330 vo_x11_fullscreen();
1331 final:
1332 if (vo_gc != None)
1333 XFreeGC(mDisplay, vo_gc);
1334 vo_gc = XCreateGC(mDisplay, vo_window, GCForeground, &xgcv);
1335 XSync(mDisplay, False);
1336 vo_mouse_autohide = 1;
1339 void vo_x11_clearwindow_part(Display * mDisplay, Window vo_window,
1340 int img_width, int img_height, int use_fs)
1342 int u_dheight, u_dwidth, left_ov, left_ov2;
1344 if (!f_gc)
1345 return;
1347 u_dheight = use_fs ? vo_screenheight : vo_dheight;
1348 u_dwidth = use_fs ? vo_screenwidth : vo_dwidth;
1349 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1350 return;
1352 left_ov = (u_dheight - img_height) / 2;
1353 left_ov2 = (u_dwidth - img_width) / 2;
1355 XFillRectangle(mDisplay, vo_window, f_gc, 0, 0, u_dwidth, left_ov);
1356 XFillRectangle(mDisplay, vo_window, f_gc, 0, u_dheight - left_ov - 1,
1357 u_dwidth, left_ov + 1);
1359 if (u_dwidth > img_width)
1361 XFillRectangle(mDisplay, vo_window, f_gc, 0, left_ov, left_ov2,
1362 img_height);
1363 XFillRectangle(mDisplay, vo_window, f_gc, u_dwidth - left_ov2 - 1,
1364 left_ov, left_ov2 + 1, img_height);
1367 XFlush(mDisplay);
1370 void vo_x11_clearwindow(Display * mDisplay, Window vo_window)
1372 if (!f_gc)
1373 return;
1374 XFillRectangle(mDisplay, vo_window, f_gc, 0, 0, vo_screenwidth,
1375 vo_screenheight);
1377 XFlush(mDisplay);
1381 void vo_x11_setlayer(Display * mDisplay, Window vo_window, int layer)
1383 if (WinID >= 0)
1384 return;
1386 if (vo_fs_type & vo_wm_LAYER)
1388 XClientMessageEvent xev;
1390 if (!orig_layer)
1391 orig_layer = vo_x11_get_gnome_layer(mDisplay, vo_window);
1393 memset(&xev, 0, sizeof(xev));
1394 xev.type = ClientMessage;
1395 xev.display = mDisplay;
1396 xev.window = vo_window;
1397 xev.message_type = XA_WIN_LAYER;
1398 xev.format = 32;
1399 xev.data.l[0] = layer ? fs_layer : orig_layer; // if not fullscreen, stay on default layer
1400 xev.data.l[1] = CurrentTime;
1401 mp_msg(MSGT_VO, MSGL_V,
1402 "[x11] Layered style stay on top (layer %ld).\n",
1403 xev.data.l[0]);
1404 XSendEvent(mDisplay, mRootWin, False, SubstructureNotifyMask,
1405 (XEvent *) & xev);
1406 } else if (vo_fs_type & vo_wm_NETWM)
1408 XClientMessageEvent xev;
1409 char *state;
1411 memset(&xev, 0, sizeof(xev));
1412 xev.type = ClientMessage;
1413 xev.message_type = XA_NET_WM_STATE;
1414 xev.display = mDisplay;
1415 xev.window = vo_window;
1416 xev.format = 32;
1417 xev.data.l[0] = layer;
1419 if (vo_fs_type & vo_wm_STAYS_ON_TOP)
1420 xev.data.l[1] = XA_NET_WM_STATE_STAYS_ON_TOP;
1421 else if (vo_fs_type & vo_wm_ABOVE)
1422 xev.data.l[1] = XA_NET_WM_STATE_ABOVE;
1423 else if (vo_fs_type & vo_wm_FULLSCREEN)
1424 xev.data.l[1] = XA_NET_WM_STATE_FULLSCREEN;
1425 else if (vo_fs_type & vo_wm_BELOW)
1426 // This is not fallback. We can safely assume that the situation
1427 // where only NETWM_STATE_BELOW is supported doesn't exist.
1428 xev.data.l[1] = XA_NET_WM_STATE_BELOW;
1430 XSendEvent(mDisplay, mRootWin, False, SubstructureRedirectMask,
1431 (XEvent *) & xev);
1432 state = XGetAtomName(mDisplay, xev.data.l[1]);
1433 mp_msg(MSGT_VO, MSGL_V,
1434 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1435 layer, state);
1436 XFree(state);
1440 static int vo_x11_get_fs_type(int supported)
1442 int i;
1443 int type = supported;
1445 if (vo_fstype_list)
1447 i = 0;
1448 for (i = 0; vo_fstype_list[i]; i++)
1450 int neg = 0;
1451 char *arg = vo_fstype_list[i];
1453 if (vo_fstype_list[i][0] == '-')
1455 neg = 1;
1456 arg = vo_fstype_list[i] + 1;
1459 if (!strncmp(arg, "layer", 5))
1461 if (!neg && (arg[5] == '='))
1463 char *endptr = NULL;
1464 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1466 if (endptr && *endptr == '\0' && layer >= 0
1467 && layer <= 15)
1468 fs_layer = layer;
1470 if (neg)
1471 type &= ~vo_wm_LAYER;
1472 else
1473 type |= vo_wm_LAYER;
1474 } else if (!strcmp(arg, "above"))
1476 if (neg)
1477 type &= ~vo_wm_ABOVE;
1478 else
1479 type |= vo_wm_ABOVE;
1480 } else if (!strcmp(arg, "fullscreen"))
1482 if (neg)
1483 type &= ~vo_wm_FULLSCREEN;
1484 else
1485 type |= vo_wm_FULLSCREEN;
1486 } else if (!strcmp(arg, "stays_on_top"))
1488 if (neg)
1489 type &= ~vo_wm_STAYS_ON_TOP;
1490 else
1491 type |= vo_wm_STAYS_ON_TOP;
1492 } else if (!strcmp(arg, "below"))
1494 if (neg)
1495 type &= ~vo_wm_BELOW;
1496 else
1497 type |= vo_wm_BELOW;
1498 } else if (!strcmp(arg, "netwm"))
1500 if (neg)
1501 type &= ~vo_wm_NETWM;
1502 else
1503 type |= vo_wm_NETWM;
1504 } else if (!strcmp(arg, "none"))
1505 return 0;
1509 return type;
1513 * \brief update vo_dx, vo_dy, vo_dwidth and vo_dheight with current values of vo_window
1514 * \return returns current color depth of vo_window
1516 int vo_x11_update_geometry(void) {
1517 unsigned depth, w, h;
1518 int dummy_int;
1519 Window dummy_win;
1520 XGetGeometry(mDisplay, vo_window, &dummy_win, &dummy_int, &dummy_int,
1521 &w, &h, &dummy_int, &depth);
1522 if (w <= INT_MAX && h <= INT_MAX) { vo_dwidth = w; vo_dheight = h; }
1523 XTranslateCoordinates(mDisplay, vo_window, mRootWin, 0, 0, &vo_dx, &vo_dy,
1524 &dummy_win);
1525 return depth <= INT_MAX ? depth : 0;
1528 void vo_x11_fullscreen(void)
1530 int x, y, w, h;
1532 if (WinID >= 0 || vo_fs_flip)
1533 return;
1535 if (vo_fs)
1537 // fs->win
1538 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1540 x = vo_old_x;
1541 y = vo_old_y;
1542 w = vo_old_width;
1543 h = vo_old_height;
1546 vo_x11_ewmh_fullscreen(_NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1547 vo_fs = VO_FALSE;
1548 } else
1550 // win->fs
1551 vo_x11_ewmh_fullscreen(_NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1553 vo_fs = VO_TRUE;
1554 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1556 vo_old_x = vo_dx;
1557 vo_old_y = vo_dy;
1558 vo_old_width = vo_dwidth;
1559 vo_old_height = vo_dheight;
1561 update_xinerama_info();
1562 x = xinerama_x;
1563 y = xinerama_y;
1564 w = vo_screenwidth;
1565 h = vo_screenheight;
1568 long dummy;
1570 XGetWMNormalHints(mDisplay, vo_window, &vo_hint, &dummy);
1571 if (!(vo_hint.flags & PWinGravity))
1572 old_gravity = NorthWestGravity;
1573 else
1574 old_gravity = vo_hint.win_gravity;
1576 if (vo_wm_type == 0 && !(vo_fsmode & 16))
1578 XUnmapWindow(mDisplay, vo_window); // required for MWM
1579 XWithdrawWindow(mDisplay, vo_window, mScreen);
1580 vo_fs_flip = 1;
1583 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1585 vo_x11_decoration(mDisplay, vo_window, vo_border && !vo_fs);
1586 vo_x11_sizehint(x, y, w, h, 0);
1587 vo_x11_setlayer(mDisplay, vo_window, vo_fs);
1590 XMoveResizeWindow(mDisplay, vo_window, x, y, w, h);
1592 /* some WMs lose ontop after fullscreen */
1593 if ((!(vo_fs)) & vo_ontop)
1594 vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
1596 XMapRaised(mDisplay, vo_window);
1597 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1598 XMoveResizeWindow(mDisplay, vo_window, x, y, w, h);
1599 XRaiseWindow(mDisplay, vo_window);
1600 XFlush(mDisplay);
1603 void vo_x11_ontop(void)
1605 vo_ontop = (!(vo_ontop));
1607 vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
1610 void vo_x11_border(void)
1612 vo_border = !vo_border;
1613 vo_x11_decoration(mDisplay, vo_window, vo_border && !vo_fs);
1617 * XScreensaver stuff
1620 static int screensaver_off;
1621 static unsigned int time_last;
1623 void xscreensaver_heartbeat(void)
1625 unsigned int time = GetTimerMS();
1627 if (mDisplay && screensaver_off && (time - time_last) > 30000)
1629 time_last = time;
1631 XResetScreenSaver(mDisplay);
1635 static int xss_suspend(Bool suspend)
1637 #ifndef CONFIG_XSS
1638 return 0;
1639 #else
1640 int event, error, major, minor;
1641 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1642 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1643 return 0;
1644 if (major < 1 || (major == 1 && minor < 1))
1645 return 0;
1646 XScreenSaverSuspend(mDisplay, suspend);
1647 return 1;
1648 #endif
1652 * End of XScreensaver stuff
1655 void saver_on(Display * mDisplay)
1658 if (!screensaver_off)
1659 return;
1660 screensaver_off = 0;
1661 if (xss_suspend(False))
1662 return;
1663 #ifdef CONFIG_XDPMS
1664 if (dpms_disabled)
1666 int nothing;
1667 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1669 if (!DPMSEnable(mDisplay))
1670 { // restoring power saving settings
1671 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1672 } else
1674 // DPMS does not seem to be enabled unless we call DPMSInfo
1675 BOOL onoff;
1676 CARD16 state;
1678 DPMSForceLevel(mDisplay, DPMSModeOn);
1679 DPMSInfo(mDisplay, &state, &onoff);
1680 if (onoff)
1682 mp_msg(MSGT_VO, MSGL_V,
1683 "Successfully enabled DPMS\n");
1684 } else
1686 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1690 dpms_disabled = 0;
1692 #endif
1695 void saver_off(Display * mDisplay)
1697 int nothing;
1699 if (screensaver_off)
1700 return;
1701 screensaver_off = 1;
1702 if (xss_suspend(True))
1703 return;
1704 #ifdef CONFIG_XDPMS
1705 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1707 BOOL onoff;
1708 CARD16 state;
1710 DPMSInfo(mDisplay, &state, &onoff);
1711 if (onoff)
1713 Status stat;
1715 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1716 dpms_disabled = 1;
1717 stat = DPMSDisable(mDisplay); // monitor powersave off
1718 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1721 #endif
1724 static XErrorHandler old_handler = NULL;
1725 static int selectinput_err = 0;
1726 static int x11_selectinput_errorhandler(Display * display,
1727 XErrorEvent * event)
1729 if (event->error_code == BadAccess)
1731 selectinput_err = 1;
1732 mp_msg(MSGT_VO, MSGL_ERR,
1733 "X11 error: BadAccess during XSelectInput Call\n");
1734 mp_msg(MSGT_VO, MSGL_ERR,
1735 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1736 /* If you think MPlayer should shutdown with this error,
1737 * comment out the following line */
1738 return 0;
1740 if (old_handler != NULL)
1741 old_handler(display, event);
1742 else
1743 x11_errorhandler(display, event);
1744 return 0;
1747 void vo_x11_selectinput_witherr(Display * display, Window w,
1748 long event_mask)
1750 XSync(display, False);
1751 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1752 selectinput_err = 0;
1753 if (vo_nomouse_input)
1755 XSelectInput(display, w,
1756 event_mask &
1757 (~(ButtonPressMask | ButtonReleaseMask)));
1758 } else
1760 XSelectInput(display, w, event_mask);
1762 XSync(display, False);
1763 XSetErrorHandler(old_handler);
1764 if (selectinput_err)
1766 mp_msg(MSGT_VO, MSGL_ERR,
1767 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1768 XSelectInput(display, w,
1769 event_mask &
1771 (ButtonPressMask | ButtonReleaseMask |
1772 PointerMotionMask)));
1776 #ifdef CONFIG_XF86VM
1777 void vo_vm_switch(void)
1779 int vm_event, vm_error;
1780 int vm_ver, vm_rev;
1781 int i, j, have_vm = 0;
1782 int X = vo_dwidth, Y = vo_dheight;
1783 int modeline_width, modeline_height;
1785 int modecount;
1787 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1789 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1790 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1791 vm_rev);
1792 have_vm = 1;
1793 } else {
1794 mp_msg(MSGT_VO, MSGL_WARN,
1795 "XF86VidMode extension not available.\n");
1798 if (have_vm)
1800 if (vidmodes == NULL)
1801 XF86VidModeGetAllModeLines(mDisplay, mScreen, &modecount,
1802 &vidmodes);
1803 j = 0;
1804 modeline_width = vidmodes[0]->hdisplay;
1805 modeline_height = vidmodes[0]->vdisplay;
1807 for (i = 1; i < modecount; i++)
1808 if ((vidmodes[i]->hdisplay >= X)
1809 && (vidmodes[i]->vdisplay >= Y))
1810 if ((vidmodes[i]->hdisplay <= modeline_width)
1811 && (vidmodes[i]->vdisplay <= modeline_height))
1813 modeline_width = vidmodes[i]->hdisplay;
1814 modeline_height = vidmodes[i]->vdisplay;
1815 j = i;
1818 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_SelectedVideoMode,
1819 modeline_width, modeline_height, X, Y);
1820 XF86VidModeLockModeSwitch(mDisplay, mScreen, 0);
1821 XF86VidModeSwitchToMode(mDisplay, mScreen, vidmodes[j]);
1822 XF86VidModeSwitchToMode(mDisplay, mScreen, vidmodes[j]);
1824 // FIXME: all this is more of a hack than proper solution
1825 X = (vo_screenwidth - modeline_width) / 2;
1826 Y = (vo_screenheight - modeline_height) / 2;
1827 XF86VidModeSetViewPort(mDisplay, mScreen, X, Y);
1828 vo_dx = X;
1829 vo_dy = Y;
1830 vo_dwidth = modeline_width;
1831 vo_dheight = modeline_height;
1832 aspect_save_screenres(modeline_width, modeline_height);
1836 void vo_vm_close(void)
1838 #ifdef CONFIG_GUI
1839 if (vidmodes != NULL && vo_window != None)
1840 #else
1841 if (vidmodes != NULL)
1842 #endif
1844 int i, modecount;
1846 free(vidmodes);
1847 vidmodes = NULL;
1848 XF86VidModeGetAllModeLines(mDisplay, mScreen, &modecount,
1849 &vidmodes);
1850 for (i = 0; i < modecount; i++)
1851 if ((vidmodes[i]->hdisplay == vo_screenwidth)
1852 && (vidmodes[i]->vdisplay == vo_screenheight))
1854 mp_msg(MSGT_VO, MSGL_INFO,
1855 "Returning to original mode %dx%d\n",
1856 vo_screenwidth, vo_screenheight);
1857 break;
1860 XF86VidModeSwitchToMode(mDisplay, mScreen, vidmodes[i]);
1861 XF86VidModeSwitchToMode(mDisplay, mScreen, vidmodes[i]);
1862 free(vidmodes);
1863 vidmodes = NULL;
1866 #endif
1868 #endif /* X11_FULLSCREEN */
1872 * Scan the available visuals on this Display/Screen. Try to find
1873 * the 'best' available TrueColor visual that has a decent color
1874 * depth (at least 15bit). If there are multiple visuals with depth
1875 * >= 15bit, we prefer visuals with a smaller color depth.
1877 int vo_find_depth_from_visuals(Display * dpy, int screen,
1878 Visual ** visual_return)
1880 XVisualInfo visual_tmpl;
1881 XVisualInfo *visuals;
1882 int nvisuals, i;
1883 int bestvisual = -1;
1884 int bestvisual_depth = -1;
1886 visual_tmpl.screen = screen;
1887 visual_tmpl.class = TrueColor;
1888 visuals = XGetVisualInfo(dpy,
1889 VisualScreenMask | VisualClassMask,
1890 &visual_tmpl, &nvisuals);
1891 if (visuals != NULL)
1893 for (i = 0; i < nvisuals; i++)
1895 mp_msg(MSGT_VO, MSGL_V,
1896 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1897 visuals[i].visualid, visuals[i].depth,
1898 visuals[i].red_mask, visuals[i].green_mask,
1899 visuals[i].blue_mask);
1901 * Save the visual index and its depth, if this is the first
1902 * truecolor visul, or a visual that is 'preferred' over the
1903 * previous 'best' visual.
1905 if (bestvisual_depth == -1
1906 || (visuals[i].depth >= 15
1907 && (visuals[i].depth < bestvisual_depth
1908 || bestvisual_depth < 15)))
1910 bestvisual = i;
1911 bestvisual_depth = visuals[i].depth;
1915 if (bestvisual != -1 && visual_return != NULL)
1916 *visual_return = visuals[bestvisual].visual;
1918 XFree(visuals);
1920 return bestvisual_depth;
1924 static Colormap cmap = None;
1925 static XColor cols[256];
1926 static int cm_size, red_mask, green_mask, blue_mask;
1929 Colormap vo_x11_create_colormap(XVisualInfo * vinfo)
1931 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1933 if (vinfo->class != DirectColor)
1934 return XCreateColormap(mDisplay, mRootWin, vinfo->visual,
1935 AllocNone);
1937 /* can this function get called twice or more? */
1938 if (cmap)
1939 return cmap;
1940 cm_size = vinfo->colormap_size;
1941 red_mask = vinfo->red_mask;
1942 green_mask = vinfo->green_mask;
1943 blue_mask = vinfo->blue_mask;
1944 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1945 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1946 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1947 rvu = 65536ull * ru / (red_mask + ru);
1948 gvu = 65536ull * gu / (green_mask + gu);
1949 bvu = 65536ull * bu / (blue_mask + bu);
1950 r = g = b = 0;
1951 rv = gv = bv = 0;
1952 m = DoRed | DoGreen | DoBlue;
1953 for (k = 0; k < cm_size; k++)
1955 int t;
1957 cols[k].pixel = r | g | b;
1958 cols[k].red = rv;
1959 cols[k].green = gv;
1960 cols[k].blue = bv;
1961 cols[k].flags = m;
1962 t = (r + ru) & red_mask;
1963 if (t < r)
1964 m &= ~DoRed;
1965 r = t;
1966 t = (g + gu) & green_mask;
1967 if (t < g)
1968 m &= ~DoGreen;
1969 g = t;
1970 t = (b + bu) & blue_mask;
1971 if (t < b)
1972 m &= ~DoBlue;
1973 b = t;
1974 rv += rvu;
1975 gv += gvu;
1976 bv += bvu;
1978 cmap = XCreateColormap(mDisplay, mRootWin, vinfo->visual, AllocAll);
1979 XStoreColors(mDisplay, cmap, cols, cm_size);
1980 return cmap;
1984 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1985 * hue and red/green/blue intensity, but we cannot do saturation.
1986 * Currently only gamma, brightness and contrast are implemented.
1987 * Is there sufficient interest for hue and/or red/green/blue intensity?
1989 /* these values have range [-100,100] and are initially 0 */
1990 static int vo_gamma = 0;
1991 static int vo_brightness = 0;
1992 static int vo_contrast = 0;
1994 static int transform_color(float val,
1995 float brightness, float contrast, float gamma) {
1996 float s = pow(val, gamma);
1997 s = (s - 0.5) * contrast + 0.5;
1998 s += brightness;
1999 if (s < 0)
2000 s = 0;
2001 if (s > 1)
2002 s = 1;
2003 return (unsigned short) (s * 65535);
2006 uint32_t vo_x11_set_equalizer(char *name, int value)
2008 float gamma, brightness, contrast;
2009 float rf, gf, bf;
2010 int k;
2013 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
2014 * of TrueColor-ed window but be careful:
2015 * Unlike the colormaps, which are private for the X client
2016 * who created them and thus automatically destroyed on client
2017 * disconnect, this gamma ramp is a system-wide (X-server-wide)
2018 * setting and _must_ be restored before the process exits.
2019 * Unforunately when the process crashes (or gets killed
2020 * for some reason) it is impossible to restore the setting,
2021 * and such behaviour could be rather annoying for the users.
2023 if (cmap == None)
2024 return VO_NOTAVAIL;
2026 if (!strcasecmp(name, "brightness"))
2027 vo_brightness = value;
2028 else if (!strcasecmp(name, "contrast"))
2029 vo_contrast = value;
2030 else if (!strcasecmp(name, "gamma"))
2031 vo_gamma = value;
2032 else
2033 return VO_NOTIMPL;
2035 brightness = 0.01 * vo_brightness;
2036 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
2037 gamma = pow(2, -0.02 * vo_gamma);
2039 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
2040 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
2041 green_mask;
2042 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
2044 /* now recalculate the colormap using the newly set value */
2045 for (k = 0; k < cm_size; k++)
2047 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
2048 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
2049 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
2052 XStoreColors(mDisplay, cmap, cols, cm_size);
2053 XFlush(mDisplay);
2054 return VO_TRUE;
2057 uint32_t vo_x11_get_equalizer(char *name, int *value)
2059 if (cmap == None)
2060 return VO_NOTAVAIL;
2061 if (!strcasecmp(name, "brightness"))
2062 *value = vo_brightness;
2063 else if (!strcasecmp(name, "contrast"))
2064 *value = vo_contrast;
2065 else if (!strcasecmp(name, "gamma"))
2066 *value = vo_gamma;
2067 else
2068 return VO_NOTIMPL;
2069 return VO_TRUE;
2072 #ifdef CONFIG_XV
2073 int vo_xv_set_eq(uint32_t xv_port, char *name, int value)
2075 XvAttribute *attributes;
2076 int i, howmany, xv_atom;
2078 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
2080 /* get available attributes */
2081 attributes = XvQueryPortAttributes(mDisplay, xv_port, &howmany);
2082 for (i = 0; i < howmany && attributes; i++)
2083 if (attributes[i].flags & XvSettable)
2085 xv_atom = XInternAtom(mDisplay, attributes[i].name, True);
2086 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2087 then trigger it if it's ok so that the other values are at default upon query */
2088 if (xv_atom != None)
2090 int hue = 0, port_value, port_min, port_max;
2092 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2093 (!strcasecmp(name, "brightness")))
2094 port_value = value;
2095 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2096 (!strcasecmp(name, "contrast")))
2097 port_value = value;
2098 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2099 (!strcasecmp(name, "saturation")))
2100 port_value = value;
2101 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2102 (!strcasecmp(name, "hue")))
2104 port_value = value;
2105 hue = 1;
2106 } else
2107 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2108 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2109 (!strcasecmp(name, "red_intensity")))
2110 port_value = value;
2111 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2112 && (!strcasecmp(name, "green_intensity")))
2113 port_value = value;
2114 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2115 && (!strcasecmp(name, "blue_intensity")))
2116 port_value = value;
2117 else
2118 continue;
2120 port_min = attributes[i].min_value;
2121 port_max = attributes[i].max_value;
2123 /* nvidia hue workaround */
2124 if (hue && port_min == 0 && port_max == 360)
2126 port_value =
2127 (port_value >=
2128 0) ? (port_value - 100) : (port_value + 100);
2130 // -100 -> min
2131 // 0 -> (max+min)/2
2132 // +100 -> max
2133 port_value =
2134 (port_value + 100) * (port_max - port_min) / 200 +
2135 port_min;
2136 XvSetPortAttribute(mDisplay, xv_port, xv_atom, port_value);
2137 return VO_TRUE;
2140 return VO_FALSE;
2143 int vo_xv_get_eq(uint32_t xv_port, char *name, int *value)
2146 XvAttribute *attributes;
2147 int i, howmany, xv_atom;
2149 /* get available attributes */
2150 attributes = XvQueryPortAttributes(mDisplay, xv_port, &howmany);
2151 for (i = 0; i < howmany && attributes; i++)
2152 if (attributes[i].flags & XvGettable)
2154 xv_atom = XInternAtom(mDisplay, attributes[i].name, True);
2155 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2156 then trigger it if it's ok so that the other values are at default upon query */
2157 if (xv_atom != None)
2159 int val, port_value = 0, port_min, port_max;
2161 XvGetPortAttribute(mDisplay, xv_port, xv_atom,
2162 &port_value);
2164 port_min = attributes[i].min_value;
2165 port_max = attributes[i].max_value;
2166 val =
2167 (port_value - port_min) * 200 / (port_max - port_min) -
2168 100;
2170 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2171 (!strcasecmp(name, "brightness")))
2172 *value = val;
2173 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2174 (!strcasecmp(name, "contrast")))
2175 *value = val;
2176 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2177 (!strcasecmp(name, "saturation")))
2178 *value = val;
2179 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2180 (!strcasecmp(name, "hue")))
2182 /* nasty nvidia detect */
2183 if (port_min == 0 && port_max == 360)
2184 *value = (val >= 0) ? (val - 100) : (val + 100);
2185 else
2186 *value = val;
2187 } else
2188 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2189 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2190 (!strcasecmp(name, "red_intensity")))
2191 *value = val;
2192 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2193 && (!strcasecmp(name, "green_intensity")))
2194 *value = val;
2195 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2196 && (!strcasecmp(name, "blue_intensity")))
2197 *value = val;
2198 else
2199 continue;
2201 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2202 name, *value);
2203 return VO_TRUE;
2206 return VO_FALSE;
2209 /** \brief contains flags changing the execution of the colorkeying code */
2210 xv_ck_info_t xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR };
2211 unsigned long xv_colorkey; ///< The color used for manual colorkeying.
2212 unsigned int xv_port; ///< The selected Xv port.
2215 * \brief Interns the requested atom if it is available.
2217 * \param atom_name String containing the name of the requested atom.
2219 * \return Returns the atom if available, else None is returned.
2222 static Atom xv_intern_atom_if_exists( char const * atom_name )
2224 XvAttribute * attributes;
2225 int attrib_count,i;
2226 Atom xv_atom = None;
2228 attributes = XvQueryPortAttributes( mDisplay, xv_port, &attrib_count );
2229 if( attributes!=NULL )
2231 for ( i = 0; i < attrib_count; ++i )
2233 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2235 xv_atom = XInternAtom( mDisplay, atom_name, False );
2236 break; // found what we want, break out
2239 XFree( attributes );
2242 return xv_atom;
2246 * \brief Try to enable vsync for xv.
2247 * \return Returns -1 if not available, 0 on failure and 1 on success.
2249 int vo_xv_enable_vsync(void)
2251 Atom xv_atom = xv_intern_atom_if_exists("XV_SYNC_TO_VBLANK");
2252 if (xv_atom == None)
2253 return -1;
2254 return XvSetPortAttribute(mDisplay, xv_port, xv_atom, 1) == Success;
2258 * \brief Get maximum supported source image dimensions.
2260 * This function does not set the variables pointed to by
2261 * width and height if the information could not be retrieved,
2262 * so the caller is reponsible for properly initializing them.
2264 * \param width [out] The maximum width gets stored here.
2265 * \param height [out] The maximum height gets stored here.
2268 void vo_xv_get_max_img_dim( uint32_t * width, uint32_t * height )
2270 XvEncodingInfo * encodings;
2271 //unsigned long num_encodings, idx; to int or too long?!
2272 unsigned int num_encodings, idx;
2274 XvQueryEncodings( mDisplay, xv_port, &num_encodings, &encodings);
2276 if ( encodings )
2278 for ( idx = 0; idx < num_encodings; ++idx )
2280 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2282 *width = encodings[idx].width;
2283 *height = encodings[idx].height;
2284 break;
2289 mp_msg( MSGT_VO, MSGL_V,
2290 "[xv common] Maximum source image dimensions: %ux%u\n",
2291 *width, *height );
2293 XvFreeEncodingInfo( encodings );
2297 * \brief Print information about the colorkey method and source.
2299 * \param ck_handling Integer value containing the information about
2300 * colorkey handling (see x11_common.h).
2302 * Outputs the content of |ck_handling| as a readable message.
2305 void vo_xv_print_ck_info(void)
2307 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2309 switch ( xv_ck_info.method )
2311 case CK_METHOD_NONE:
2312 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2313 case CK_METHOD_AUTOPAINT:
2314 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2315 case CK_METHOD_MANUALFILL:
2316 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2317 case CK_METHOD_BACKGROUND:
2318 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2321 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2323 switch ( xv_ck_info.source )
2325 case CK_SRC_CUR:
2326 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2327 xv_colorkey );
2328 break;
2329 case CK_SRC_USE:
2330 if ( xv_ck_info.method == CK_METHOD_AUTOPAINT )
2332 mp_msg( MSGT_VO, MSGL_V,
2333 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2334 xv_colorkey );
2336 else
2338 mp_msg( MSGT_VO, MSGL_V,
2339 "Using colorkey from MPlayer (0x%06lx)."
2340 " Use -colorkey to change.\n",
2341 xv_colorkey );
2343 break;
2344 case CK_SRC_SET:
2345 mp_msg( MSGT_VO, MSGL_V,
2346 "Setting and using colorkey from MPlayer (0x%06lx)."
2347 " Use -colorkey to change.\n",
2348 xv_colorkey );
2349 break;
2353 * \brief Init colorkey depending on the settings in xv_ck_info.
2355 * \return Returns 0 on failure and 1 on success.
2357 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2358 * flags in xv_ck_info.
2360 * Possiblilities:
2361 * * Methods
2362 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2363 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2364 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2365 * * Sources
2366 * - use currently set colorkey ( CK_SRC_CUR )
2367 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2368 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2370 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2371 * we don't draw anything as this means it was forced to off.
2373 int vo_xv_init_colorkey(void)
2375 Atom xv_atom;
2376 int rez;
2378 /* check if colorkeying is needed */
2379 xv_atom = xv_intern_atom_if_exists( "XV_COLORKEY" );
2381 /* if we have to deal with colorkeying ... */
2382 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2384 /* check if we should use the colorkey specified in vo_colorkey */
2385 if ( xv_ck_info.source != CK_SRC_CUR )
2387 xv_colorkey = vo_colorkey;
2389 /* check if we have to set the colorkey too */
2390 if ( xv_ck_info.source == CK_SRC_SET )
2392 xv_atom = XInternAtom(mDisplay, "XV_COLORKEY",False);
2394 rez = XvSetPortAttribute( mDisplay, xv_port, xv_atom, vo_colorkey );
2395 if ( rez != Success )
2397 mp_msg( MSGT_VO, MSGL_FATAL,
2398 "[xv common] Couldn't set colorkey!\n" );
2399 return 0; // error setting colorkey
2403 else
2405 int colorkey_ret;
2407 rez=XvGetPortAttribute(mDisplay,xv_port, xv_atom, &colorkey_ret);
2408 if ( rez == Success )
2410 xv_colorkey = colorkey_ret;
2412 else
2414 mp_msg( MSGT_VO, MSGL_FATAL,
2415 "[xv common] Couldn't get colorkey!"
2416 "Maybe the selected Xv port has no overlay.\n" );
2417 return 0; // error getting colorkey
2421 xv_atom = xv_intern_atom_if_exists( "XV_AUTOPAINT_COLORKEY" );
2423 /* should we draw the colorkey ourselves or activate autopainting? */
2424 if ( xv_ck_info.method == CK_METHOD_AUTOPAINT )
2426 rez = !Success; // reset rez to something different than Success
2428 if ( xv_atom != None ) // autopaint is supported
2430 rez = XvSetPortAttribute( mDisplay, xv_port, xv_atom, 1 );
2433 if ( rez != Success )
2435 // fallback to manual colorkey drawing
2436 xv_ck_info.method = CK_METHOD_MANUALFILL;
2439 else // disable colorkey autopainting if supported
2441 if ( xv_atom != None ) // we have autopaint attribute
2443 XvSetPortAttribute( mDisplay, xv_port, xv_atom, 0 );
2447 else // do no colorkey drawing at all
2449 xv_ck_info.method = CK_METHOD_NONE;
2450 } /* end: should we draw colorkey */
2452 /* output information about the current colorkey settings */
2453 vo_xv_print_ck_info();
2455 return 1; // success
2459 * \brief Draw the colorkey on the video window.
2461 * Draws the colorkey depending on the set method ( colorkey_handling ).
2463 * Also draws the black bars ( when the video doesn't fit the display in
2464 * fullscreen ) separately, so they don't overlap with the video area.
2465 * It doesn't call XFlush.
2468 void vo_xv_draw_colorkey( int32_t x, int32_t y,
2469 int32_t w, int32_t h )
2471 if( xv_ck_info.method == CK_METHOD_MANUALFILL ||
2472 xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2474 XSetForeground( mDisplay, vo_gc, xv_colorkey );
2475 XFillRectangle( mDisplay, vo_window, vo_gc,
2476 x, y,
2477 w, h );
2480 /* draw black bars if needed */
2481 /* TODO! move this to vo_x11_clearwindow_part() */
2482 if ( vo_fs )
2484 XSetForeground( mDisplay, vo_gc, 0 );
2485 /* making non-overlap fills, requires 8 checks instead of 4 */
2486 if ( y > 0 )
2487 XFillRectangle( mDisplay, vo_window, vo_gc,
2488 0, 0,
2489 vo_screenwidth, y);
2490 if (x > 0)
2491 XFillRectangle( mDisplay, vo_window, vo_gc,
2492 0, 0,
2493 x, vo_screenheight);
2494 if (x + w < vo_screenwidth)
2495 XFillRectangle( mDisplay, vo_window, vo_gc,
2496 x + w, 0,
2497 vo_screenwidth, vo_screenheight);
2498 if (y + h < vo_screenheight)
2499 XFillRectangle( mDisplay, vo_window, vo_gc,
2500 0, y + h,
2501 vo_screenwidth, vo_screenheight);
2505 /** \brief Tests if a valid argument for the ck suboption was given. */
2506 int xv_test_ck( void * arg )
2508 strarg_t * strarg = (strarg_t *)arg;
2510 if ( strargcmp( strarg, "use" ) == 0 ||
2511 strargcmp( strarg, "set" ) == 0 ||
2512 strargcmp( strarg, "cur" ) == 0 )
2514 return 1;
2517 return 0;
2519 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2520 int xv_test_ckm( void * arg )
2522 strarg_t * strarg = (strarg_t *)arg;
2524 if ( strargcmp( strarg, "bg" ) == 0 ||
2525 strargcmp( strarg, "man" ) == 0 ||
2526 strargcmp( strarg, "auto" ) == 0 )
2528 return 1;
2531 return 0;
2535 * \brief Modify the colorkey_handling var according to str
2537 * Checks if a valid pointer ( not NULL ) to the string
2538 * was given. And in that case modifies the colorkey_handling
2539 * var to reflect the requested behaviour.
2540 * If nothing happens the content of colorkey_handling stays
2541 * the same.
2543 * \param str Pointer to the string or NULL
2546 void xv_setup_colorkeyhandling( char const * ck_method_str,
2547 char const * ck_str )
2549 /* check if a valid pointer to the string was passed */
2550 if ( ck_str )
2552 if ( strncmp( ck_str, "use", 3 ) == 0 )
2554 xv_ck_info.source = CK_SRC_USE;
2556 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2558 xv_ck_info.source = CK_SRC_SET;
2561 /* check if a valid pointer to the string was passed */
2562 if ( ck_method_str )
2564 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2566 xv_ck_info.method = CK_METHOD_BACKGROUND;
2568 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2570 xv_ck_info.method = CK_METHOD_MANUALFILL;
2572 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2574 xv_ck_info.method = CK_METHOD_AUTOPAINT;
2579 #endif