Fix segfault if an 'strf' chunk couldn't be found in avi
[mplayer/glamo.git] / libvo / x11_common.c
blobba1edbcc4fb968a6705f7152801808519c63f282
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <math.h>
5 #include <inttypes.h>
7 #include "config.h"
8 #include "mp_msg.h"
9 #include "mp_fifo.h"
10 #include "x11_common.h"
12 #ifdef X11_FULLSCREEN
14 #include <string.h>
15 #include <unistd.h>
16 #include <signal.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 HAVE_XDPMS
31 #include <X11/extensions/dpms.h>
32 #endif
34 #ifdef HAVE_XINERAMA
35 #include <X11/extensions/Xinerama.h>
36 #endif
38 #ifdef HAVE_XF86VM
39 #include <X11/extensions/xf86vmode.h>
40 #endif
42 #ifdef HAVE_XF86XK
43 #include <X11/XF86keysym.h>
44 #endif
46 #ifdef HAVE_XV
47 #include <X11/extensions/Xv.h>
48 #include <X11/extensions/Xvlib.h>
50 #include "subopt-helper.h"
51 #endif
53 #include "input/input.h"
54 #include "input/mouse.h"
56 #ifdef HAVE_NEW_GUI
57 #include "gui/interface.h"
58 #include "mplayer.h"
59 #endif
61 #define WIN_LAYER_ONBOTTOM 2
62 #define WIN_LAYER_NORMAL 4
63 #define WIN_LAYER_ONTOP 6
64 #define WIN_LAYER_ABOVE_DOCK 10
66 extern int enable_mouse_movements;
67 int fs_layer = WIN_LAYER_ABOVE_DOCK;
68 static int orig_layer = 0;
69 static int old_gravity = NorthWestGravity;
71 int stop_xscreensaver = 0;
73 static int dpms_disabled = 0;
74 static int timeout_save = 0;
75 static int kdescreensaver_was_running = 0;
77 char *mDisplayName = NULL;
78 Display *mDisplay = NULL;
79 Window mRootWin;
80 int mScreen;
81 int mLocalDisplay;
83 /* output window id */
84 int vo_mouse_autohide = 0;
85 int vo_wm_type = 0;
86 int vo_fs_type = 0; // needs to be accessible for GUI X11 code
87 static int vo_fs_flip = 0;
88 char **vo_fstype_list;
90 /* 1 means that the WM is metacity (broken as hell) */
91 int metacity_hack = 0;
93 static Atom XA_NET_SUPPORTED;
94 static Atom XA_NET_WM_STATE;
95 static Atom XA_NET_WM_STATE_FULLSCREEN;
96 static Atom XA_NET_WM_STATE_ABOVE;
97 static Atom XA_NET_WM_STATE_STAYS_ON_TOP;
98 static Atom XA_NET_WM_STATE_BELOW;
99 static Atom XA_NET_WM_PID;
100 static Atom XA_WIN_PROTOCOLS;
101 static Atom XA_WIN_LAYER;
102 static Atom XA_WIN_HINTS;
103 static Atom XA_BLACKBOX_PID;
104 static Atom XAWM_PROTOCOLS;
105 static Atom XAWM_DELETE_WINDOW;
107 #define XA_INIT(x) XA##x = XInternAtom(mDisplay, #x, False)
109 static int vo_old_x = 0;
110 static int vo_old_y = 0;
111 static int vo_old_width = 0;
112 static int vo_old_height = 0;
114 #ifdef HAVE_XF86VM
115 XF86VidModeModeInfo **vidmodes = NULL;
116 XF86VidModeModeLine modeline;
117 #endif
119 static int vo_x11_get_fs_type(int supported);
123 * Sends the EWMH fullscreen state event.
125 * action: could be one of _NET_WM_STATE_REMOVE -- remove state
126 * _NET_WM_STATE_ADD -- add state
127 * _NET_WM_STATE_TOGGLE -- toggle
129 void vo_x11_ewmh_fullscreen(int action)
131 assert(action == _NET_WM_STATE_REMOVE ||
132 action == _NET_WM_STATE_ADD || action == _NET_WM_STATE_TOGGLE);
134 if (vo_fs_type & vo_wm_FULLSCREEN)
136 XEvent xev;
138 /* init X event structure for _NET_WM_FULLSCREEN client message */
139 xev.xclient.type = ClientMessage;
140 xev.xclient.serial = 0;
141 xev.xclient.send_event = True;
142 xev.xclient.message_type = XInternAtom(mDisplay,
143 "_NET_WM_STATE", False);
144 xev.xclient.window = vo_window;
145 xev.xclient.format = 32;
146 xev.xclient.data.l[0] = action;
147 xev.xclient.data.l[1] = XInternAtom(mDisplay,
148 "_NET_WM_STATE_FULLSCREEN",
149 False);
150 xev.xclient.data.l[2] = 0;
151 xev.xclient.data.l[3] = 0;
152 xev.xclient.data.l[4] = 0;
154 /* finally send that damn thing */
155 if (!XSendEvent(mDisplay, DefaultRootWindow(mDisplay), False,
156 SubstructureRedirectMask | SubstructureNotifyMask,
157 &xev))
159 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_EwmhFullscreenStateFailed);
164 void vo_hidecursor(Display * disp, Window win)
166 Cursor no_ptr;
167 Pixmap bm_no;
168 XColor black, dummy;
169 Colormap colormap;
170 static char bm_no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
172 if (WinID == 0)
173 return; // do not hide if playing on the root window
175 colormap = DefaultColormap(disp, DefaultScreen(disp));
176 if ( !XAllocNamedColor(disp, colormap, "black", &black, &dummy) )
178 return; // color alloc failed, give up
180 bm_no = XCreateBitmapFromData(disp, win, bm_no_data, 8, 8);
181 no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
182 XDefineCursor(disp, win, no_ptr);
183 XFreeCursor(disp, no_ptr);
184 if (bm_no != None)
185 XFreePixmap(disp, bm_no);
186 XFreeColors(disp,colormap,&black.pixel,1,0);
189 void vo_showcursor(Display * disp, Window win)
191 if (WinID == 0)
192 return;
193 XDefineCursor(disp, win, 0);
196 static int x11_errorhandler(Display * display, XErrorEvent * event)
198 #define MSGLEN 60
199 char msg[MSGLEN];
201 XGetErrorText(display, event->error_code, (char *) &msg, MSGLEN);
203 mp_msg(MSGT_VO, MSGL_ERR, "X11 error: %s\n", msg);
205 mp_msg(MSGT_VO, MSGL_V,
206 "Type: %x, display: %p, resourceid: %lx, serial: %lx\n",
207 event->type, event->display, event->resourceid, event->serial);
208 mp_msg(MSGT_VO, MSGL_V,
209 "Error code: %x, request code: %x, minor code: %x\n",
210 event->error_code, event->request_code, event->minor_code);
212 // abort();
213 //exit_player("X11 error");
214 return 0;
215 #undef MSGLEN
218 void fstype_help(void)
220 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_AvailableFsType);
221 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FULL_SCREEN_TYPES\n");
223 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "none",
224 "don't set fullscreen window layer");
225 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer",
226 "use _WIN_LAYER hint with default layer");
227 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer=<0..15>",
228 "use _WIN_LAYER hint with a given layer number");
229 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "netwm",
230 "force NETWM style");
231 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "above",
232 "use _NETWM_STATE_ABOVE hint if available");
233 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "below",
234 "use _NETWM_STATE_BELOW hint if available");
235 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "fullscreen",
236 "use _NETWM_STATE_FULLSCREEN hint if availale");
237 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "stays_on_top",
238 "use _NETWM_STATE_STAYS_ON_TOP hint if available");
239 mp_msg(MSGT_VO, MSGL_INFO,
240 "You can also negate the settings with simply putting '-' in the beginning");
241 mp_msg(MSGT_VO, MSGL_INFO, "\n");
244 static void fstype_dump(int fstype)
246 if (fstype)
248 mp_msg(MSGT_VO, MSGL_V, "[x11] Current fstype setting honours");
249 if (fstype & vo_wm_LAYER)
250 mp_msg(MSGT_VO, MSGL_V, " LAYER");
251 if (fstype & vo_wm_FULLSCREEN)
252 mp_msg(MSGT_VO, MSGL_V, " FULLSCREEN");
253 if (fstype & vo_wm_STAYS_ON_TOP)
254 mp_msg(MSGT_VO, MSGL_V, " STAYS_ON_TOP");
255 if (fstype & vo_wm_ABOVE)
256 mp_msg(MSGT_VO, MSGL_V, " ABOVE");
257 if (fstype & vo_wm_BELOW)
258 mp_msg(MSGT_VO, MSGL_V, " BELOW");
259 mp_msg(MSGT_VO, MSGL_V, " X atoms\n");
260 } else
261 mp_msg(MSGT_VO, MSGL_V,
262 "[x11] Current fstype setting doesn't honour any X atoms\n");
265 static int net_wm_support_state_test(Atom atom)
267 #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; } }
269 NET_WM_STATE_TEST(FULLSCREEN);
270 NET_WM_STATE_TEST(ABOVE);
271 NET_WM_STATE_TEST(STAYS_ON_TOP);
272 NET_WM_STATE_TEST(BELOW);
273 return 0;
276 static int x11_get_property(Atom type, Atom ** args, unsigned long *nitems)
278 int format;
279 unsigned long bytesafter;
281 return (Success ==
282 XGetWindowProperty(mDisplay, mRootWin, type, 0, 16384, False,
283 AnyPropertyType, &type, &format, nitems,
284 &bytesafter, (unsigned char **) args)
285 && *nitems > 0);
288 static int vo_wm_detect(void)
290 int i;
291 int wm = 0;
292 unsigned long nitems;
293 Atom *args = NULL;
295 if (WinID >= 0)
296 return 0;
298 // -- supports layers
299 if (x11_get_property(XA_WIN_PROTOCOLS, &args, &nitems))
301 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports layers.\n");
302 for (i = 0; i < nitems; i++)
304 if (args[i] == XA_WIN_LAYER)
306 wm |= vo_wm_LAYER;
307 metacity_hack |= 1;
308 } else
309 /* metacity is the only window manager I know which reports
310 * supporting only the _WIN_LAYER hint in _WIN_PROTOCOLS.
311 * (what's more support for it is broken) */
312 metacity_hack |= 2;
314 XFree(args);
315 if (wm && (metacity_hack == 1))
317 // metacity claims to support layers, but it is not the truth :-)
318 wm ^= vo_wm_LAYER;
319 mp_msg(MSGT_VO, MSGL_V,
320 "[x11] Using workaround for Metacity bugs.\n");
323 // --- netwm
324 if (x11_get_property(XA_NET_SUPPORTED, &args, &nitems))
326 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports NetWM.\n");
327 for (i = 0; i < nitems; i++)
328 wm |= net_wm_support_state_test(args[i]);
329 XFree(args);
330 #if 0
331 // ugly hack for broken OpenBox _NET_WM_STATE_FULLSCREEN support
332 // (in their implementation it only changes internal window state, nothing more!!!)
333 if (wm & vo_wm_FULLSCREEN)
335 if (x11_get_property(XA_BLACKBOX_PID, &args, &nitems))
337 mp_msg(MSGT_VO, MSGL_V,
338 "[x11] Detected wm is a broken OpenBox.\n");
339 wm ^= vo_wm_FULLSCREEN;
341 XFree(args);
343 #endif
346 if (wm == 0)
347 mp_msg(MSGT_VO, MSGL_V, "[x11] Unknown wm type...\n");
348 return wm;
351 static void init_atoms(void)
353 XA_INIT(_NET_SUPPORTED);
354 XA_INIT(_NET_WM_STATE);
355 XA_INIT(_NET_WM_STATE_FULLSCREEN);
356 XA_INIT(_NET_WM_STATE_ABOVE);
357 XA_INIT(_NET_WM_STATE_STAYS_ON_TOP);
358 XA_INIT(_NET_WM_STATE_BELOW);
359 XA_INIT(_NET_WM_PID);
360 XA_INIT(_WIN_PROTOCOLS);
361 XA_INIT(_WIN_LAYER);
362 XA_INIT(_WIN_HINTS);
363 XA_INIT(_BLACKBOX_PID);
364 XA_INIT(WM_PROTOCOLS);
365 XA_INIT(WM_DELETE_WINDOW);
368 void update_xinerama_info(void) {
369 int screen = xinerama_screen;
370 xinerama_x = xinerama_y = 0;
371 #ifdef HAVE_XINERAMA
372 if (screen >= -1 && XineramaIsActive(mDisplay))
374 XineramaScreenInfo *screens;
375 int num_screens;
377 screens = XineramaQueryScreens(mDisplay, &num_screens);
378 if (screen >= num_screens)
379 screen = num_screens - 1;
380 if (screen == -1) {
381 int x = vo_dx + vo_dwidth / 2;
382 int y = vo_dy + vo_dheight / 2;
383 for (screen = num_screens - 1; screen > 0; screen--) {
384 int left = screens[screen].x_org;
385 int right = left + screens[screen].width;
386 int top = screens[screen].y_org;
387 int bottom = top + screens[screen].height;
388 if (left <= x && x <= right && top <= y && y <= bottom)
389 break;
392 if (screen < 0)
393 screen = 0;
394 vo_screenwidth = screens[screen].width;
395 vo_screenheight = screens[screen].height;
396 xinerama_x = screens[screen].x_org;
397 xinerama_y = screens[screen].y_org;
399 XFree(screens);
401 #endif
402 aspect_save_screenres(vo_screenwidth, vo_screenheight);
405 int vo_init(void)
407 // int mScreen;
408 int depth, bpp;
409 unsigned int mask;
411 // char * DisplayName = ":0.0";
412 // Display * mDisplay;
413 XImage *mXImage = NULL;
415 // Window mRootWin;
416 XWindowAttributes attribs;
417 char *dispName;
419 if (vo_rootwin)
420 WinID = 0; // use root window
422 if (vo_depthonscreen)
424 saver_off(mDisplay);
425 return 1; // already called
428 XSetErrorHandler(x11_errorhandler);
430 #if 0
431 if (!mDisplayName)
432 if (!(mDisplayName = getenv("DISPLAY")))
433 mDisplayName = strdup(":0.0");
434 #else
435 dispName = XDisplayName(mDisplayName);
436 #endif
438 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
440 mDisplay = XOpenDisplay(dispName);
441 if (!mDisplay)
443 mp_msg(MSGT_VO, MSGL_ERR,
444 "vo: couldn't open the X11 display (%s)!\n", dispName);
445 return 0;
447 mScreen = DefaultScreen(mDisplay); // screen ID
448 mRootWin = RootWindow(mDisplay, mScreen); // root window ID
450 init_atoms();
452 #ifdef HAVE_XF86VM
454 int clock;
456 XF86VidModeGetModeLine(mDisplay, mScreen, &clock, &modeline);
457 if (!vo_screenwidth)
458 vo_screenwidth = modeline.hdisplay;
459 if (!vo_screenheight)
460 vo_screenheight = modeline.vdisplay;
462 #endif
464 if (!vo_screenwidth)
465 vo_screenwidth = DisplayWidth(mDisplay, mScreen);
466 if (!vo_screenheight)
467 vo_screenheight = DisplayHeight(mDisplay, mScreen);
469 // get color depth (from root window, or the best visual):
470 XGetWindowAttributes(mDisplay, mRootWin, &attribs);
471 depth = attribs.depth;
473 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
475 Visual *visual;
477 depth = vo_find_depth_from_visuals(mDisplay, mScreen, &visual);
478 if (depth != -1)
479 mXImage = XCreateImage(mDisplay, visual, depth, ZPixmap,
480 0, NULL, 1, 1, 8, 1);
481 } else
482 mXImage =
483 XGetImage(mDisplay, mRootWin, 0, 0, 1, 1, AllPlanes, ZPixmap);
485 vo_depthonscreen = depth; // display depth on screen
487 // get bits/pixel from XImage structure:
488 if (mXImage == NULL)
490 mask = 0;
491 } else
494 * for the depth==24 case, the XImage structures might use
495 * 24 or 32 bits of data per pixel. The global variable
496 * vo_depthonscreen stores the amount of data per pixel in the
497 * XImage structure!
499 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
501 bpp = mXImage->bits_per_pixel;
502 if ((vo_depthonscreen + 7) / 8 != (bpp + 7) / 8)
503 vo_depthonscreen = bpp; // by A'rpi
504 mask =
505 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
506 mp_msg(MSGT_VO, MSGL_V,
507 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
508 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
509 XDestroyImage(mXImage);
511 if (((vo_depthonscreen + 7) / 8) == 2)
513 if (mask == 0x7FFF)
514 vo_depthonscreen = 15;
515 else if (mask == 0xFFFF)
516 vo_depthonscreen = 16;
518 // XCloseDisplay( mDisplay );
519 /* slightly improved local display detection AST */
520 if (strncmp(dispName, "unix:", 5) == 0)
521 dispName += 4;
522 else if (strncmp(dispName, "localhost:", 10) == 0)
523 dispName += 9;
524 if (*dispName == ':' && atoi(dispName + 1) < 10)
525 mLocalDisplay = 1;
526 else
527 mLocalDisplay = 0;
528 mp_msg(MSGT_VO, MSGL_V,
529 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
530 vo_screenwidth, vo_screenheight, depth, vo_depthonscreen,
531 dispName, mLocalDisplay ? "local" : "remote");
533 vo_wm_type = vo_wm_detect();
535 vo_fs_type = vo_x11_get_fs_type(vo_wm_type);
537 fstype_dump(vo_fs_type);
539 saver_off(mDisplay);
540 return 1;
543 void vo_uninit(void)
545 if (!mDisplay)
547 mp_msg(MSGT_VO, MSGL_V,
548 "vo: x11 uninit called but X11 not inited..\n");
549 return;
551 // if( !vo_depthonscreen ) return;
552 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
553 XSetErrorHandler(NULL);
554 XCloseDisplay(mDisplay);
555 vo_depthonscreen = 0;
556 mDisplay = NULL;
559 #include "osdep/keycodes.h"
560 #include "wskeys.h"
562 #ifdef XF86XK_AudioPause
563 static void vo_x11_putkey_ext(int keysym)
565 switch (keysym)
567 case XF86XK_AudioPause:
568 mplayer_put_key(KEY_PAUSE);
569 break;
570 case XF86XK_AudioStop:
571 mplayer_put_key(KEY_STOP);
572 break;
573 case XF86XK_AudioPrev:
574 mplayer_put_key(KEY_PREV);
575 break;
576 case XF86XK_AudioNext:
577 mplayer_put_key(KEY_NEXT);
578 break;
579 default:
580 break;
583 #endif
585 void vo_x11_putkey(int key)
587 switch (key)
589 case wsLeft:
590 mplayer_put_key(KEY_LEFT);
591 break;
592 case wsRight:
593 mplayer_put_key(KEY_RIGHT);
594 break;
595 case wsUp:
596 mplayer_put_key(KEY_UP);
597 break;
598 case wsDown:
599 mplayer_put_key(KEY_DOWN);
600 break;
601 case wsSpace:
602 mplayer_put_key(' ');
603 break;
604 case wsEscape:
605 mplayer_put_key(KEY_ESC);
606 break;
607 case wsTab:
608 mplayer_put_key(KEY_TAB);
609 break;
610 case wsEnter:
611 mplayer_put_key(KEY_ENTER);
612 break;
613 case wsBackSpace:
614 mplayer_put_key(KEY_BS);
615 break;
616 case wsDelete:
617 mplayer_put_key(KEY_DELETE);
618 break;
619 case wsInsert:
620 mplayer_put_key(KEY_INSERT);
621 break;
622 case wsHome:
623 mplayer_put_key(KEY_HOME);
624 break;
625 case wsEnd:
626 mplayer_put_key(KEY_END);
627 break;
628 case wsPageUp:
629 mplayer_put_key(KEY_PAGE_UP);
630 break;
631 case wsPageDown:
632 mplayer_put_key(KEY_PAGE_DOWN);
633 break;
634 case wsF1:
635 mplayer_put_key(KEY_F + 1);
636 break;
637 case wsF2:
638 mplayer_put_key(KEY_F + 2);
639 break;
640 case wsF3:
641 mplayer_put_key(KEY_F + 3);
642 break;
643 case wsF4:
644 mplayer_put_key(KEY_F + 4);
645 break;
646 case wsF5:
647 mplayer_put_key(KEY_F + 5);
648 break;
649 case wsF6:
650 mplayer_put_key(KEY_F + 6);
651 break;
652 case wsF7:
653 mplayer_put_key(KEY_F + 7);
654 break;
655 case wsF8:
656 mplayer_put_key(KEY_F + 8);
657 break;
658 case wsF9:
659 mplayer_put_key(KEY_F + 9);
660 break;
661 case wsF10:
662 mplayer_put_key(KEY_F + 10);
663 break;
664 case wsF11:
665 mplayer_put_key(KEY_F + 11);
666 break;
667 case wsF12:
668 mplayer_put_key(KEY_F + 12);
669 break;
670 case wsMinus:
671 case wsGrayMinus:
672 mplayer_put_key('-');
673 break;
674 case wsPlus:
675 case wsGrayPlus:
676 mplayer_put_key('+');
677 break;
678 case wsGrayMul:
679 case wsMul:
680 mplayer_put_key('*');
681 break;
682 case wsGrayDiv:
683 case wsDiv:
684 mplayer_put_key('/');
685 break;
686 case wsLess:
687 mplayer_put_key('<');
688 break;
689 case wsMore:
690 mplayer_put_key('>');
691 break;
692 case wsGray0:
693 mplayer_put_key(KEY_KP0);
694 break;
695 case wsGrayEnd:
696 case wsGray1:
697 mplayer_put_key(KEY_KP1);
698 break;
699 case wsGrayDown:
700 case wsGray2:
701 mplayer_put_key(KEY_KP2);
702 break;
703 case wsGrayPgDn:
704 case wsGray3:
705 mplayer_put_key(KEY_KP3);
706 break;
707 case wsGrayLeft:
708 case wsGray4:
709 mplayer_put_key(KEY_KP4);
710 break;
711 case wsGray5Dup:
712 case wsGray5:
713 mplayer_put_key(KEY_KP5);
714 break;
715 case wsGrayRight:
716 case wsGray6:
717 mplayer_put_key(KEY_KP6);
718 break;
719 case wsGrayHome:
720 case wsGray7:
721 mplayer_put_key(KEY_KP7);
722 break;
723 case wsGrayUp:
724 case wsGray8:
725 mplayer_put_key(KEY_KP8);
726 break;
727 case wsGrayPgUp:
728 case wsGray9:
729 mplayer_put_key(KEY_KP9);
730 break;
731 case wsGrayDecimal:
732 mplayer_put_key(KEY_KPDEC);
733 break;
734 case wsGrayInsert:
735 mplayer_put_key(KEY_KPINS);
736 break;
737 case wsGrayDelete:
738 mplayer_put_key(KEY_KPDEL);
739 break;
740 case wsGrayEnter:
741 mplayer_put_key(KEY_KPENTER);
742 break;
743 case wsGrave:
744 mplayer_put_key('`');
745 break;
746 case wsTilde:
747 mplayer_put_key('~');
748 break;
749 case wsExclSign:
750 mplayer_put_key('!');
751 break;
752 case wsAt:
753 mplayer_put_key('@');
754 break;
755 case wsHash:
756 mplayer_put_key('#');
757 break;
758 case wsDollar:
759 mplayer_put_key('$');
760 break;
761 case wsPercent:
762 mplayer_put_key('%');
763 break;
764 case wsCircumflex:
765 mplayer_put_key('^');
766 break;
767 case wsAmpersand:
768 mplayer_put_key('&');
769 break;
770 case wsobracket:
771 mplayer_put_key('(');
772 break;
773 case wscbracket:
774 mplayer_put_key(')');
775 break;
776 case wsUnder:
777 mplayer_put_key('_');
778 break;
779 case wsocbracket:
780 mplayer_put_key('{');
781 break;
782 case wsccbracket:
783 mplayer_put_key('}');
784 break;
785 case wsColon:
786 mplayer_put_key(':');
787 break;
788 case wsSemicolon:
789 mplayer_put_key(';');
790 break;
791 case wsDblQuote:
792 mplayer_put_key('\"');
793 break;
794 case wsAcute:
795 mplayer_put_key('\'');
796 break;
797 case wsComma:
798 mplayer_put_key(',');
799 break;
800 case wsPoint:
801 mplayer_put_key('.');
802 break;
803 case wsQuestSign:
804 mplayer_put_key('?');
805 break;
806 case wsBSlash:
807 mplayer_put_key('\\');
808 break;
809 case wsPipe:
810 mplayer_put_key('|');
811 break;
812 case wsEqual:
813 mplayer_put_key('=');
814 break;
815 case wsosbrackets:
816 mplayer_put_key('[');
817 break;
818 case wscsbrackets:
819 mplayer_put_key(']');
820 break;
823 default:
824 if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') ||
825 (key >= '0' && key <= '9'))
826 mplayer_put_key(key);
832 // ----- Motif header: -------
834 #define MWM_HINTS_FUNCTIONS (1L << 0)
835 #define MWM_HINTS_DECORATIONS (1L << 1)
836 #define MWM_HINTS_INPUT_MODE (1L << 2)
837 #define MWM_HINTS_STATUS (1L << 3)
839 #define MWM_FUNC_ALL (1L << 0)
840 #define MWM_FUNC_RESIZE (1L << 1)
841 #define MWM_FUNC_MOVE (1L << 2)
842 #define MWM_FUNC_MINIMIZE (1L << 3)
843 #define MWM_FUNC_MAXIMIZE (1L << 4)
844 #define MWM_FUNC_CLOSE (1L << 5)
846 #define MWM_DECOR_ALL (1L << 0)
847 #define MWM_DECOR_BORDER (1L << 1)
848 #define MWM_DECOR_RESIZEH (1L << 2)
849 #define MWM_DECOR_TITLE (1L << 3)
850 #define MWM_DECOR_MENU (1L << 4)
851 #define MWM_DECOR_MINIMIZE (1L << 5)
852 #define MWM_DECOR_MAXIMIZE (1L << 6)
854 #define MWM_INPUT_MODELESS 0
855 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
856 #define MWM_INPUT_SYSTEM_MODAL 2
857 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
858 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
860 #define MWM_TEAROFF_WINDOW (1L<<0)
862 typedef struct
864 long flags;
865 long functions;
866 long decorations;
867 long input_mode;
868 long state;
869 } MotifWmHints;
871 static MotifWmHints vo_MotifWmHints;
872 static Atom vo_MotifHints = None;
874 void vo_x11_decoration(Display * vo_Display, Window w, int d)
876 static unsigned int olddecor = MWM_DECOR_ALL;
877 static unsigned int oldfuncs =
878 MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
879 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE;
880 Atom mtype;
881 int mformat;
882 unsigned long mn, mb;
884 if (!WinID)
885 return;
887 if (vo_fsmode & 8)
889 XSetTransientForHint(vo_Display, w,
890 RootWindow(vo_Display, mScreen));
893 vo_MotifHints = XInternAtom(vo_Display, "_MOTIF_WM_HINTS", 0);
894 if (vo_MotifHints != None)
896 if (!d)
898 MotifWmHints *mhints = NULL;
900 XGetWindowProperty(vo_Display, w, vo_MotifHints, 0, 20, False,
901 vo_MotifHints, &mtype, &mformat, &mn,
902 &mb, (unsigned char **) &mhints);
903 if (mhints)
905 if (mhints->flags & MWM_HINTS_DECORATIONS)
906 olddecor = mhints->decorations;
907 if (mhints->flags & MWM_HINTS_FUNCTIONS)
908 oldfuncs = mhints->functions;
909 XFree(mhints);
913 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
914 vo_MotifWmHints.flags =
915 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
916 if (d)
918 vo_MotifWmHints.functions = oldfuncs;
919 d = olddecor;
921 #if 0
922 vo_MotifWmHints.decorations =
923 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
924 #else
925 vo_MotifWmHints.decorations =
926 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
927 #endif
928 XChangeProperty(vo_Display, w, vo_MotifHints, vo_MotifHints, 32,
929 PropModeReplace,
930 (unsigned char *) &vo_MotifWmHints,
931 (vo_fsmode & 4) ? 4 : 5);
935 void vo_x11_classhint(Display * display, Window window, char *name)
937 XClassHint wmClass;
938 pid_t pid = getpid();
940 wmClass.res_name = name;
941 wmClass.res_class = "MPlayer";
942 XSetClassHint(display, window, &wmClass);
943 XChangeProperty(display, window, XA_NET_WM_PID, XA_CARDINAL, 32,
944 PropModeReplace, (unsigned char *) &pid, 1);
947 Window vo_window = None;
948 GC vo_gc = NULL;
949 GC f_gc = NULL;
950 XSizeHints vo_hint;
952 #ifdef HAVE_NEW_GUI
953 void vo_setwindow(Window w, GC g)
955 vo_window = w;
956 vo_gc = g;
958 #endif
960 void vo_x11_uninit(void)
962 saver_on(mDisplay);
963 if (vo_window != None)
964 vo_showcursor(mDisplay, vo_window);
966 if (f_gc)
968 XFreeGC(mDisplay, f_gc);
969 f_gc = NULL;
971 #ifdef HAVE_NEW_GUI
972 /* destroy window only if it's not controlled by the GUI */
973 if (!use_gui)
974 #endif
976 if (vo_gc)
978 XSetBackground(mDisplay, vo_gc, 0);
979 XFreeGC(mDisplay, vo_gc);
980 vo_gc = NULL;
982 if (vo_window != None)
984 XClearWindow(mDisplay, vo_window);
985 if (WinID < 0)
987 XEvent xev;
989 XUnmapWindow(mDisplay, vo_window);
990 XDestroyWindow(mDisplay, vo_window);
993 XNextEvent(mDisplay, &xev);
995 while (xev.type != DestroyNotify
996 || xev.xdestroywindow.event != vo_window);
998 vo_window = None;
1000 vo_fs = 0;
1001 vo_old_width = vo_old_height = 0;
1005 static unsigned int mouse_timer;
1006 static int mouse_waiting_hide;
1008 int vo_x11_check_events(Display * mydisplay)
1010 int ret = 0;
1011 XEvent Event;
1012 char buf[100];
1013 KeySym keySym;
1014 static XComposeStatus stat;
1016 // unsigned long vo_KeyTable[512];
1018 if ((vo_mouse_autohide) && mouse_waiting_hide &&
1019 (GetTimerMS() - mouse_timer >= 1000)) {
1020 vo_hidecursor(mydisplay, vo_window);
1021 mouse_waiting_hide = 0;
1024 while (XPending(mydisplay))
1026 XNextEvent(mydisplay, &Event);
1027 #ifdef HAVE_NEW_GUI
1028 if (use_gui)
1030 guiGetEvent(0, (char *) &Event);
1031 if (vo_window != Event.xany.window)
1032 continue;
1034 #endif
1035 // printf("\rEvent.type=%X \n",Event.type);
1036 switch (Event.type)
1038 case Expose:
1039 ret |= VO_EVENT_EXPOSE;
1040 break;
1041 case ConfigureNotify:
1042 // if (!vo_fs && (Event.xconfigure.width == vo_screenwidth || Event.xconfigure.height == vo_screenheight)) break;
1043 // if (vo_fs && Event.xconfigure.width != vo_screenwidth && Event.xconfigure.height != vo_screenheight) break;
1044 if (vo_window == None)
1045 break;
1046 vo_dwidth = Event.xconfigure.width;
1047 vo_dheight = Event.xconfigure.height;
1048 #if 0
1049 /* when resizing, x and y are zero :( */
1050 vo_dx = Event.xconfigure.x;
1051 vo_dy = Event.xconfigure.y;
1052 #else
1054 Window root;
1055 int foo;
1056 Window win;
1058 XGetGeometry(mydisplay, vo_window, &root, &foo, &foo,
1059 &foo /*width */ , &foo /*height */ , &foo,
1060 &foo);
1061 XTranslateCoordinates(mydisplay, vo_window, root, 0, 0,
1062 &vo_dx, &vo_dy, &win);
1064 #endif
1065 ret |= VO_EVENT_RESIZE;
1066 break;
1067 case KeyPress:
1069 int key;
1071 #ifdef HAVE_NEW_GUI
1072 if ( use_gui ) { break; }
1073 #endif
1075 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
1076 &stat);
1077 #ifdef XF86XK_AudioPause
1078 vo_x11_putkey_ext(keySym);
1079 #endif
1080 key =
1081 ((keySym & 0xff00) !=
1082 0 ? ((keySym & 0x00ff) + 256) : (keySym));
1083 vo_x11_putkey(key);
1084 ret |= VO_EVENT_KEYPRESS;
1086 break;
1087 case MotionNotify:
1088 if(enable_mouse_movements)
1090 char cmd_str[40];
1091 sprintf(cmd_str,"set_mouse_pos %i %i",Event.xmotion.x, Event.xmotion.y);
1092 mp_input_queue_cmd(mp_input_parse_cmd(cmd_str));
1095 if (vo_mouse_autohide)
1097 vo_showcursor(mydisplay, vo_window);
1098 mouse_waiting_hide = 1;
1099 mouse_timer = GetTimerMS();
1101 break;
1102 case ButtonPress:
1103 if (vo_mouse_autohide)
1105 vo_showcursor(mydisplay, vo_window);
1106 mouse_waiting_hide = 1;
1107 mouse_timer = GetTimerMS();
1109 #ifdef HAVE_NEW_GUI
1110 // Ignore mouse button 1-3 under GUI.
1111 if (use_gui && (Event.xbutton.button >= 1)
1112 && (Event.xbutton.button <= 3))
1113 break;
1114 #endif
1115 mplayer_put_key((MOUSE_BTN0 + Event.xbutton.button -
1116 1) | MP_KEY_DOWN);
1117 break;
1118 case ButtonRelease:
1119 if (vo_mouse_autohide)
1121 vo_showcursor(mydisplay, vo_window);
1122 mouse_waiting_hide = 1;
1123 mouse_timer = GetTimerMS();
1125 #ifdef HAVE_NEW_GUI
1126 // Ignore mouse button 1-3 under GUI.
1127 if (use_gui && (Event.xbutton.button >= 1)
1128 && (Event.xbutton.button <= 3))
1129 break;
1130 #endif
1131 mplayer_put_key(MOUSE_BTN0 + Event.xbutton.button - 1);
1132 break;
1133 case PropertyNotify:
1135 char *name =
1136 XGetAtomName(mydisplay, Event.xproperty.atom);
1138 if (!name)
1139 break;
1141 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
1143 XFree(name);
1145 break;
1146 case MapNotify:
1147 vo_hint.win_gravity = old_gravity;
1148 XSetWMNormalHints(mDisplay, vo_window, &vo_hint);
1149 vo_fs_flip = 0;
1150 break;
1151 case ClientMessage:
1152 if (Event.xclient.message_type == XAWM_PROTOCOLS &&
1153 Event.xclient.data.l[0] == XAWM_DELETE_WINDOW)
1154 mplayer_put_key(KEY_CLOSE_WIN);
1155 break;
1158 return ret;
1162 * \brief sets the size and position of the non-fullscreen window.
1164 void vo_x11_nofs_sizepos(int x, int y, int width, int height)
1166 if (vo_fs) {
1167 vo_old_x = x;
1168 vo_old_y = y;
1169 vo_old_width = width;
1170 vo_old_height = height;
1172 else
1174 vo_dwidth = width;
1175 vo_dheight = height;
1176 XMoveResizeWindow(mDisplay, vo_window, x, y, width, height);
1180 void vo_x11_sizehint(int x, int y, int width, int height, int max)
1182 vo_hint.flags = PPosition | PSize | PWinGravity;
1183 if (vo_keepaspect)
1185 vo_hint.flags |= PAspect;
1186 vo_hint.min_aspect.x = width;
1187 vo_hint.min_aspect.y = height;
1188 vo_hint.max_aspect.x = width;
1189 vo_hint.max_aspect.y = height;
1192 vo_hint.x = x;
1193 vo_hint.y = y;
1194 vo_hint.width = width;
1195 vo_hint.height = height;
1196 if (max)
1198 vo_hint.max_width = width;
1199 vo_hint.max_height = height;
1200 vo_hint.flags |= PMaxSize;
1201 } else
1203 vo_hint.max_width = 0;
1204 vo_hint.max_height = 0;
1207 // Set minimum height/width to 4 to avoid off-by-one errors
1208 // and because mga_vid requires a minimal size of 4 pixels.
1209 vo_hint.min_width = vo_hint.min_height = 4;
1210 vo_hint.flags |= PMinSize;
1212 vo_hint.win_gravity = StaticGravity;
1213 XSetWMNormalHints(mDisplay, vo_window, &vo_hint);
1216 static int vo_x11_get_gnome_layer(Display * mDisplay, Window win)
1218 Atom type;
1219 int format;
1220 unsigned long nitems;
1221 unsigned long bytesafter;
1222 unsigned short *args = NULL;
1224 if (XGetWindowProperty(mDisplay, win, XA_WIN_LAYER, 0, 16384,
1225 False, AnyPropertyType, &type, &format, &nitems,
1226 &bytesafter,
1227 (unsigned char **) &args) == Success
1228 && nitems > 0 && args)
1230 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
1231 *args);
1232 return *args;
1234 return WIN_LAYER_NORMAL;
1238 Window vo_x11_create_smooth_window(Display * mDisplay, Window mRoot,
1239 Visual * vis, int x, int y,
1240 unsigned int width, unsigned int height,
1241 int depth, Colormap col_map)
1243 unsigned long xswamask = CWBackingStore | CWBorderPixel;
1244 XSetWindowAttributes xswa;
1245 Window ret_win;
1247 if (col_map != CopyFromParent)
1249 xswa.colormap = col_map;
1250 xswamask |= CWColormap;
1252 xswa.background_pixel = 0;
1253 xswa.border_pixel = 0;
1254 xswa.backing_store = Always;
1255 xswa.bit_gravity = StaticGravity;
1257 ret_win =
1258 XCreateWindow(mDisplay, mRootWin, x, y, width, height, 0, depth,
1259 CopyFromParent, vis, xswamask, &xswa);
1260 XSetWMProtocols(mDisplay, ret_win, &XAWM_DELETE_WINDOW, 1);
1261 if (!f_gc)
1262 f_gc = XCreateGC(mDisplay, ret_win, 0, 0);
1263 XSetForeground(mDisplay, f_gc, 0);
1265 return ret_win;
1269 * \brief create and setup a window suitable for display
1270 * \param vis Visual to use for creating the window
1271 * \param x x position of window
1272 * \param y y position of window
1273 * \param width width of window
1274 * \param height height of window
1275 * \param flags flags for window creation.
1276 * Only VOFLAG_FULLSCREEN is supported so far.
1277 * \param col_map Colourmap for window
1278 * \param classname name to use for the classhint
1279 * \param title title for the window
1281 * This also does the grunt-work like setting Window Manager hints etc.
1282 * If vo_window is already set it just moves and resizes it.
1284 void vo_x11_create_vo_window(XVisualInfo *vis, int x, int y,
1285 unsigned int width, unsigned int height, int flags,
1286 Colormap col_map,
1287 const char *classname, const char *title)
1289 if (vo_window == None) {
1290 XSizeHints hint;
1291 XEvent xev;
1292 vo_fs = 0;
1293 vo_dwidth = width;
1294 vo_dheight = height;
1295 vo_window = vo_x11_create_smooth_window(mDisplay, mRootWin, vis->visual,
1296 x, y, width, height, vis->depth, col_map);
1297 vo_x11_classhint(mDisplay, vo_window, classname);
1298 XStoreName(mDisplay, vo_window, title);
1299 vo_hidecursor(mDisplay, vo_window);
1300 XSelectInput(mDisplay, vo_window, StructureNotifyMask);
1301 hint.x = x; hint.y = y;
1302 hint.width = width; hint.height = height;
1303 hint.flags = PPosition | PSize;
1304 XSetStandardProperties(mDisplay, vo_window, title, title, None, NULL, 0, &hint);
1305 vo_x11_sizehint(x, y, width, height, 0);
1306 // map window
1307 XMapWindow(mDisplay, vo_window);
1308 XClearWindow(mDisplay, vo_window);
1309 // wait for map
1310 do {
1311 XNextEvent(mDisplay, &xev);
1312 } while (xev.type != MapNotify || xev.xmap.event != vo_window);
1313 XSelectInput(mDisplay, vo_window, NoEventMask);
1314 XSync(mDisplay, False);
1315 vo_x11_selectinput_witherr(mDisplay, vo_window,
1316 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1317 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1319 if (vo_ontop) vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
1320 vo_x11_nofs_sizepos(vo_dx, vo_dy, width, height);
1321 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1322 vo_x11_fullscreen();
1325 void vo_x11_clearwindow_part(Display * mDisplay, Window vo_window,
1326 int img_width, int img_height, int use_fs)
1328 int u_dheight, u_dwidth, left_ov, left_ov2;
1330 if (!f_gc)
1331 return;
1333 u_dheight = use_fs ? vo_screenheight : vo_dheight;
1334 u_dwidth = use_fs ? vo_screenwidth : vo_dwidth;
1335 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1336 return;
1338 left_ov = (u_dheight - img_height) / 2;
1339 left_ov2 = (u_dwidth - img_width) / 2;
1341 XFillRectangle(mDisplay, vo_window, f_gc, 0, 0, u_dwidth, left_ov);
1342 XFillRectangle(mDisplay, vo_window, f_gc, 0, u_dheight - left_ov - 1,
1343 u_dwidth, left_ov + 1);
1345 if (u_dwidth > img_width)
1347 XFillRectangle(mDisplay, vo_window, f_gc, 0, left_ov, left_ov2,
1348 img_height);
1349 XFillRectangle(mDisplay, vo_window, f_gc, u_dwidth - left_ov2 - 1,
1350 left_ov, left_ov2 + 1, img_height);
1353 XFlush(mDisplay);
1356 void vo_x11_clearwindow(Display * mDisplay, Window vo_window)
1358 if (!f_gc)
1359 return;
1360 XFillRectangle(mDisplay, vo_window, f_gc, 0, 0, vo_screenwidth,
1361 vo_screenheight);
1363 XFlush(mDisplay);
1367 void vo_x11_setlayer(Display * mDisplay, Window vo_window, int layer)
1369 if (WinID >= 0)
1370 return;
1372 if (vo_fs_type & vo_wm_LAYER)
1374 XClientMessageEvent xev;
1376 if (!orig_layer)
1377 orig_layer = vo_x11_get_gnome_layer(mDisplay, vo_window);
1379 memset(&xev, 0, sizeof(xev));
1380 xev.type = ClientMessage;
1381 xev.display = mDisplay;
1382 xev.window = vo_window;
1383 xev.message_type = XA_WIN_LAYER;
1384 xev.format = 32;
1385 xev.data.l[0] = layer ? fs_layer : orig_layer; // if not fullscreen, stay on default layer
1386 xev.data.l[1] = CurrentTime;
1387 mp_msg(MSGT_VO, MSGL_V,
1388 "[x11] Layered style stay on top (layer %ld).\n",
1389 xev.data.l[0]);
1390 XSendEvent(mDisplay, mRootWin, False, SubstructureNotifyMask,
1391 (XEvent *) & xev);
1392 } else if (vo_fs_type & vo_wm_NETWM)
1394 XClientMessageEvent xev;
1395 char *state;
1397 memset(&xev, 0, sizeof(xev));
1398 xev.type = ClientMessage;
1399 xev.message_type = XA_NET_WM_STATE;
1400 xev.display = mDisplay;
1401 xev.window = vo_window;
1402 xev.format = 32;
1403 xev.data.l[0] = layer;
1405 if (vo_fs_type & vo_wm_STAYS_ON_TOP)
1406 xev.data.l[1] = XA_NET_WM_STATE_STAYS_ON_TOP;
1407 else if (vo_fs_type & vo_wm_ABOVE)
1408 xev.data.l[1] = XA_NET_WM_STATE_ABOVE;
1409 else if (vo_fs_type & vo_wm_FULLSCREEN)
1410 xev.data.l[1] = XA_NET_WM_STATE_FULLSCREEN;
1411 else if (vo_fs_type & vo_wm_BELOW)
1412 // This is not fallback. We can safely assume that the situation
1413 // where only NETWM_STATE_BELOW is supported doesn't exist.
1414 xev.data.l[1] = XA_NET_WM_STATE_BELOW;
1416 XSendEvent(mDisplay, mRootWin, False, SubstructureRedirectMask,
1417 (XEvent *) & xev);
1418 state = XGetAtomName(mDisplay, xev.data.l[1]);
1419 mp_msg(MSGT_VO, MSGL_V,
1420 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1421 layer, state);
1422 XFree(state);
1426 static int vo_x11_get_fs_type(int supported)
1428 int i;
1429 int type = supported;
1431 if (vo_fstype_list)
1433 i = 0;
1434 for (i = 0; vo_fstype_list[i]; i++)
1436 int neg = 0;
1437 char *arg = vo_fstype_list[i];
1439 if (vo_fstype_list[i][0] == '-')
1441 neg = 1;
1442 arg = vo_fstype_list[i] + 1;
1445 if (!strncmp(arg, "layer", 5))
1447 if (!neg && (arg[5] == '='))
1449 char *endptr = NULL;
1450 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1452 if (endptr && *endptr == '\0' && layer >= 0
1453 && layer <= 15)
1454 fs_layer = layer;
1456 if (neg)
1457 type &= ~vo_wm_LAYER;
1458 else
1459 type |= vo_wm_LAYER;
1460 } else if (!strcmp(arg, "above"))
1462 if (neg)
1463 type &= ~vo_wm_ABOVE;
1464 else
1465 type |= vo_wm_ABOVE;
1466 } else if (!strcmp(arg, "fullscreen"))
1468 if (neg)
1469 type &= ~vo_wm_FULLSCREEN;
1470 else
1471 type |= vo_wm_FULLSCREEN;
1472 } else if (!strcmp(arg, "stays_on_top"))
1474 if (neg)
1475 type &= ~vo_wm_STAYS_ON_TOP;
1476 else
1477 type |= vo_wm_STAYS_ON_TOP;
1478 } else if (!strcmp(arg, "below"))
1480 if (neg)
1481 type &= ~vo_wm_BELOW;
1482 else
1483 type |= vo_wm_BELOW;
1484 } else if (!strcmp(arg, "netwm"))
1486 if (neg)
1487 type &= ~vo_wm_NETWM;
1488 else
1489 type |= vo_wm_NETWM;
1490 } else if (!strcmp(arg, "none"))
1491 return 0;
1495 return type;
1498 void vo_x11_fullscreen(void)
1500 int x, y, w, h;
1502 if (WinID >= 0 || vo_fs_flip)
1503 return;
1505 if (vo_fs)
1507 // fs->win
1508 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1510 if (vo_dwidth != vo_screenwidth && vo_dheight != vo_screenheight)
1511 return;
1512 x = vo_old_x;
1513 y = vo_old_y;
1514 w = vo_old_width;
1515 h = vo_old_height;
1518 vo_x11_ewmh_fullscreen(_NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1519 vo_fs = VO_FALSE;
1520 } else
1522 // win->fs
1523 vo_x11_ewmh_fullscreen(_NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1525 vo_fs = VO_TRUE;
1526 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1528 if (vo_old_width &&
1529 (vo_dwidth == vo_screenwidth && vo_dwidth != vo_old_width) &&
1530 (vo_dheight == vo_screenheight && vo_dheight != vo_old_height))
1531 return;
1532 vo_old_x = vo_dx;
1533 vo_old_y = vo_dy;
1534 vo_old_width = vo_dwidth;
1535 vo_old_height = vo_dheight;
1536 update_xinerama_info();
1537 x = xinerama_x;
1538 y = xinerama_y;
1539 w = vo_screenwidth;
1540 h = vo_screenheight;
1544 long dummy;
1546 XGetWMNormalHints(mDisplay, vo_window, &vo_hint, &dummy);
1547 if (!(vo_hint.flags & PWinGravity))
1548 old_gravity = NorthWestGravity;
1549 else
1550 old_gravity = vo_hint.win_gravity;
1552 if (vo_wm_type == 0 && !(vo_fsmode & 16))
1554 XUnmapWindow(mDisplay, vo_window); // required for MWM
1555 XWithdrawWindow(mDisplay, vo_window, mScreen);
1556 vo_fs_flip = 1;
1559 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1561 vo_x11_decoration(mDisplay, vo_window, (vo_fs) ? 0 : 1);
1562 vo_x11_sizehint(x, y, w, h, 0);
1563 vo_x11_setlayer(mDisplay, vo_window, vo_fs);
1566 XMoveResizeWindow(mDisplay, vo_window, x, y, w, h);
1568 /* some WMs lose ontop after fullscreen */
1569 if ((!(vo_fs)) & vo_ontop)
1570 vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
1572 XMapRaised(mDisplay, vo_window);
1573 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1574 XMoveResizeWindow(mDisplay, vo_window, x, y, w, h);
1575 XRaiseWindow(mDisplay, vo_window);
1576 XFlush(mDisplay);
1579 void vo_x11_ontop(void)
1581 vo_ontop = (!(vo_ontop));
1583 vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
1587 * XScreensaver stuff
1590 static int got_badwindow;
1591 static XErrorHandler old_handler;
1593 static int badwindow_handler(Display * dpy, XErrorEvent * error)
1595 if (error->error_code != BadWindow)
1596 return (*old_handler) (dpy, error);
1598 got_badwindow = True;
1599 return 0;
1602 static Window find_xscreensaver_window(Display * dpy)
1604 int i;
1605 Window root = RootWindowOfScreen(DefaultScreenOfDisplay(dpy));
1606 Window root2, parent, *kids;
1607 Window retval = 0;
1608 Atom xs_version;
1609 unsigned int nkids = 0;
1611 xs_version = XInternAtom(dpy, "_SCREENSAVER_VERSION", True);
1613 if (!(xs_version != None &&
1614 XQueryTree(dpy, root, &root2, &parent, &kids, &nkids) &&
1615 kids && nkids))
1616 return 0;
1618 old_handler = XSetErrorHandler(badwindow_handler);
1620 for (i = 0; i < nkids; i++)
1622 Atom type;
1623 int format;
1624 unsigned long nitems, bytesafter;
1625 char *v;
1626 int status;
1628 got_badwindow = False;
1629 status =
1630 XGetWindowProperty(dpy, kids[i], xs_version, 0, 200, False,
1631 XA_STRING, &type, &format, &nitems,
1632 &bytesafter, (unsigned char **) &v);
1633 XSync(dpy, False);
1634 if (got_badwindow)
1635 status = BadWindow;
1637 if (status == Success && type != None)
1639 retval = kids[i];
1640 break;
1643 XFree(kids);
1644 XSetErrorHandler(old_handler);
1646 return retval;
1649 static Window xs_windowid = 0;
1650 static Atom deactivate;
1651 static Atom screensaver;
1653 static unsigned int time_last;
1655 void xscreensaver_heartbeat(void)
1657 unsigned int time = GetTimerMS();
1658 XEvent ev;
1660 if (mDisplay && xs_windowid && (time - time_last) > 30000)
1662 time_last = time;
1664 ev.xany.type = ClientMessage;
1665 ev.xclient.display = mDisplay;
1666 ev.xclient.window = xs_windowid;
1667 ev.xclient.message_type = screensaver;
1668 ev.xclient.format = 32;
1669 memset(&ev.xclient.data, 0, sizeof(ev.xclient.data));
1670 ev.xclient.data.l[0] = (long) deactivate;
1672 mp_msg(MSGT_VO, MSGL_DBG2, "Pinging xscreensaver.\n");
1673 old_handler = XSetErrorHandler(badwindow_handler);
1674 XSendEvent(mDisplay, xs_windowid, False, 0L, &ev);
1675 XSync(mDisplay, False);
1676 XSetErrorHandler(old_handler);
1680 static void xscreensaver_disable(Display * dpy)
1682 mp_msg(MSGT_VO, MSGL_DBG2, "xscreensaver_disable()\n");
1684 xs_windowid = find_xscreensaver_window(dpy);
1685 if (!xs_windowid)
1687 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_CouldNotFindXScreenSaver);
1688 return;
1690 mp_msg(MSGT_VO, MSGL_INFO,
1691 "xscreensaver_disable: xscreensaver wid=%ld.\n", xs_windowid);
1693 deactivate = XInternAtom(dpy, "DEACTIVATE", False);
1694 screensaver = XInternAtom(dpy, "SCREENSAVER", False);
1697 static void xscreensaver_enable(void)
1699 xs_windowid = 0;
1703 * End of XScreensaver stuff
1706 void saver_on(Display * mDisplay)
1709 #ifdef HAVE_XDPMS
1710 int nothing;
1712 if (dpms_disabled)
1714 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1716 if (!DPMSEnable(mDisplay))
1717 { // restoring power saving settings
1718 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1719 } else
1721 // DPMS does not seem to be enabled unless we call DPMSInfo
1722 BOOL onoff;
1723 CARD16 state;
1725 DPMSForceLevel(mDisplay, DPMSModeOn);
1726 DPMSInfo(mDisplay, &state, &onoff);
1727 if (onoff)
1729 mp_msg(MSGT_VO, MSGL_V,
1730 "Successfully enabled DPMS\n");
1731 } else
1733 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1737 dpms_disabled = 0;
1739 #endif
1741 if (timeout_save)
1743 int dummy, interval, prefer_blank, allow_exp;
1745 XGetScreenSaver(mDisplay, &dummy, &interval, &prefer_blank,
1746 &allow_exp);
1747 XSetScreenSaver(mDisplay, timeout_save, interval, prefer_blank,
1748 allow_exp);
1749 XGetScreenSaver(mDisplay, &timeout_save, &interval, &prefer_blank,
1750 &allow_exp);
1751 timeout_save = 0;
1754 if (stop_xscreensaver)
1755 xscreensaver_enable();
1756 if (kdescreensaver_was_running && stop_xscreensaver)
1758 system
1759 ("dcop kdesktop KScreensaverIface enable true 2>/dev/null >/dev/null");
1760 kdescreensaver_was_running = 0;
1766 void saver_off(Display * mDisplay)
1769 int interval, prefer_blank, allow_exp;
1771 #ifdef HAVE_XDPMS
1772 int nothing;
1774 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1776 BOOL onoff;
1777 CARD16 state;
1779 DPMSInfo(mDisplay, &state, &onoff);
1780 if (onoff)
1782 Status stat;
1784 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1785 dpms_disabled = 1;
1786 stat = DPMSDisable(mDisplay); // monitor powersave off
1787 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1790 #endif
1791 if (!timeout_save)
1793 XGetScreenSaver(mDisplay, &timeout_save, &interval, &prefer_blank,
1794 &allow_exp);
1795 if (timeout_save)
1796 XSetScreenSaver(mDisplay, 0, interval, prefer_blank,
1797 allow_exp);
1799 // turning off screensaver
1800 if (stop_xscreensaver)
1801 xscreensaver_disable(mDisplay);
1802 if (stop_xscreensaver && !kdescreensaver_was_running)
1804 kdescreensaver_was_running =
1805 (system
1806 ("dcop kdesktop KScreensaverIface isEnabled 2>/dev/null | sed 's/1/true/g' | grep true 2>/dev/null >/dev/null")
1807 == 0);
1808 if (kdescreensaver_was_running)
1809 system
1810 ("dcop kdesktop KScreensaverIface enable false 2>/dev/null >/dev/null");
1814 static XErrorHandler old_handler = NULL;
1815 static int selectinput_err = 0;
1816 static int x11_selectinput_errorhandler(Display * display,
1817 XErrorEvent * event)
1819 if (event->error_code == BadAccess)
1821 selectinput_err = 1;
1822 mp_msg(MSGT_VO, MSGL_ERR,
1823 "X11 error: BadAccess during XSelectInput Call\n");
1824 mp_msg(MSGT_VO, MSGL_ERR,
1825 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1826 /* If you think MPlayer should shutdown with this error,
1827 * comment out the following line */
1828 return 0;
1830 if (old_handler != NULL)
1831 old_handler(display, event);
1832 else
1833 x11_errorhandler(display, event);
1834 return 0;
1837 void vo_x11_selectinput_witherr(Display * display, Window w,
1838 long event_mask)
1840 XSync(display, False);
1841 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1842 selectinput_err = 0;
1843 if (vo_nomouse_input)
1845 XSelectInput(display, w,
1846 event_mask &
1847 (~(ButtonPressMask | ButtonReleaseMask)));
1848 } else
1850 XSelectInput(display, w, event_mask);
1852 XSync(display, False);
1853 XSetErrorHandler(old_handler);
1854 if (selectinput_err)
1856 mp_msg(MSGT_VO, MSGL_ERR,
1857 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1858 XSelectInput(display, w,
1859 event_mask &
1861 (ButtonPressMask | ButtonReleaseMask |
1862 PointerMotionMask)));
1866 #ifdef HAVE_XF86VM
1867 void vo_vm_switch(uint32_t X, uint32_t Y, int *modeline_width,
1868 int *modeline_height)
1870 int vm_event, vm_error;
1871 int vm_ver, vm_rev;
1872 int i, j, have_vm = 0;
1874 int modecount;
1876 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1878 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1879 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1880 vm_rev);
1881 have_vm = 1;
1882 } else
1883 mp_msg(MSGT_VO, MSGL_WARN,
1884 "XF86VidMode extension not available.\n");
1886 if (have_vm)
1888 if (vidmodes == NULL)
1889 XF86VidModeGetAllModeLines(mDisplay, mScreen, &modecount,
1890 &vidmodes);
1891 j = 0;
1892 *modeline_width = vidmodes[0]->hdisplay;
1893 *modeline_height = vidmodes[0]->vdisplay;
1895 for (i = 1; i < modecount; i++)
1896 if ((vidmodes[i]->hdisplay >= X)
1897 && (vidmodes[i]->vdisplay >= Y))
1898 if ((vidmodes[i]->hdisplay <= *modeline_width)
1899 && (vidmodes[i]->vdisplay <= *modeline_height))
1901 *modeline_width = vidmodes[i]->hdisplay;
1902 *modeline_height = vidmodes[i]->vdisplay;
1903 j = i;
1906 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_SelectedVideoMode,
1907 *modeline_width, *modeline_height, X, Y);
1908 XF86VidModeLockModeSwitch(mDisplay, mScreen, 0);
1909 XF86VidModeSwitchToMode(mDisplay, mScreen, vidmodes[j]);
1910 XF86VidModeSwitchToMode(mDisplay, mScreen, vidmodes[j]);
1911 X = (vo_screenwidth - *modeline_width) / 2;
1912 Y = (vo_screenheight - *modeline_height) / 2;
1913 XF86VidModeSetViewPort(mDisplay, mScreen, X, Y);
1917 void vo_vm_close(Display * dpy)
1919 #ifdef HAVE_NEW_GUI
1920 if (vidmodes != NULL && vo_window != None)
1921 #else
1922 if (vidmodes != NULL)
1923 #endif
1925 int i, modecount;
1926 int screen;
1928 screen = DefaultScreen(dpy);
1930 free(vidmodes);
1931 vidmodes = NULL;
1932 XF86VidModeGetAllModeLines(mDisplay, mScreen, &modecount,
1933 &vidmodes);
1934 for (i = 0; i < modecount; i++)
1935 if ((vidmodes[i]->hdisplay == vo_screenwidth)
1936 && (vidmodes[i]->vdisplay == vo_screenheight))
1938 mp_msg(MSGT_VO, MSGL_INFO,
1939 "Returning to original mode %dx%d\n",
1940 vo_screenwidth, vo_screenheight);
1941 break;
1944 XF86VidModeSwitchToMode(dpy, screen, vidmodes[i]);
1945 XF86VidModeSwitchToMode(dpy, screen, vidmodes[i]);
1946 free(vidmodes);
1947 vidmodes = NULL;
1950 #endif
1952 #endif /* X11_FULLSCREEN */
1956 * Scan the available visuals on this Display/Screen. Try to find
1957 * the 'best' available TrueColor visual that has a decent color
1958 * depth (at least 15bit). If there are multiple visuals with depth
1959 * >= 15bit, we prefer visuals with a smaller color depth.
1961 int vo_find_depth_from_visuals(Display * dpy, int screen,
1962 Visual ** visual_return)
1964 XVisualInfo visual_tmpl;
1965 XVisualInfo *visuals;
1966 int nvisuals, i;
1967 int bestvisual = -1;
1968 int bestvisual_depth = -1;
1970 visual_tmpl.screen = screen;
1971 visual_tmpl.class = TrueColor;
1972 visuals = XGetVisualInfo(dpy,
1973 VisualScreenMask | VisualClassMask,
1974 &visual_tmpl, &nvisuals);
1975 if (visuals != NULL)
1977 for (i = 0; i < nvisuals; i++)
1979 mp_msg(MSGT_VO, MSGL_V,
1980 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1981 visuals[i].visualid, visuals[i].depth,
1982 visuals[i].red_mask, visuals[i].green_mask,
1983 visuals[i].blue_mask);
1985 * Save the visual index and its depth, if this is the first
1986 * truecolor visul, or a visual that is 'preferred' over the
1987 * previous 'best' visual.
1989 if (bestvisual_depth == -1
1990 || (visuals[i].depth >= 15
1991 && (visuals[i].depth < bestvisual_depth
1992 || bestvisual_depth < 15)))
1994 bestvisual = i;
1995 bestvisual_depth = visuals[i].depth;
1999 if (bestvisual != -1 && visual_return != NULL)
2000 *visual_return = visuals[bestvisual].visual;
2002 XFree(visuals);
2004 return bestvisual_depth;
2008 static Colormap cmap = None;
2009 static XColor cols[256];
2010 static int cm_size, red_mask, green_mask, blue_mask;
2013 Colormap vo_x11_create_colormap(XVisualInfo * vinfo)
2015 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
2017 if (vinfo->class != DirectColor)
2018 return XCreateColormap(mDisplay, mRootWin, vinfo->visual,
2019 AllocNone);
2021 /* can this function get called twice or more? */
2022 if (cmap)
2023 return cmap;
2024 cm_size = vinfo->colormap_size;
2025 red_mask = vinfo->red_mask;
2026 green_mask = vinfo->green_mask;
2027 blue_mask = vinfo->blue_mask;
2028 ru = (red_mask & (red_mask - 1)) ^ red_mask;
2029 gu = (green_mask & (green_mask - 1)) ^ green_mask;
2030 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
2031 rvu = 65536ull * ru / (red_mask + ru);
2032 gvu = 65536ull * gu / (green_mask + gu);
2033 bvu = 65536ull * bu / (blue_mask + bu);
2034 r = g = b = 0;
2035 rv = gv = bv = 0;
2036 m = DoRed | DoGreen | DoBlue;
2037 for (k = 0; k < cm_size; k++)
2039 int t;
2041 cols[k].pixel = r | g | b;
2042 cols[k].red = rv;
2043 cols[k].green = gv;
2044 cols[k].blue = bv;
2045 cols[k].flags = m;
2046 t = (r + ru) & red_mask;
2047 if (t < r)
2048 m &= ~DoRed;
2049 r = t;
2050 t = (g + gu) & green_mask;
2051 if (t < g)
2052 m &= ~DoGreen;
2053 g = t;
2054 t = (b + bu) & blue_mask;
2055 if (t < b)
2056 m &= ~DoBlue;
2057 b = t;
2058 rv += rvu;
2059 gv += gvu;
2060 bv += bvu;
2062 cmap = XCreateColormap(mDisplay, mRootWin, vinfo->visual, AllocAll);
2063 XStoreColors(mDisplay, cmap, cols, cm_size);
2064 return cmap;
2068 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
2069 * hue and red/green/blue intensity, but we cannot do saturation.
2070 * Currently only gamma, brightness and contrast are implemented.
2071 * Is there sufficient interest for hue and/or red/green/blue intensity?
2073 /* these values have range [-100,100] and are initially 0 */
2074 static int vo_gamma = 0;
2075 static int vo_brightness = 0;
2076 static int vo_contrast = 0;
2079 uint32_t vo_x11_set_equalizer(char *name, int value)
2081 float gamma, brightness, contrast;
2082 float rf, gf, bf;
2083 int k;
2086 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
2087 * of TrueColor-ed window but be careful:
2088 * Unlike the colormaps, which are private for the X client
2089 * who created them and thus automatically destroyed on client
2090 * disconnect, this gamma ramp is a system-wide (X-server-wide)
2091 * setting and _must_ be restored before the process exits.
2092 * Unforunately when the process crashes (or gets killed
2093 * for some reason) it is impossible to restore the setting,
2094 * and such behaviour could be rather annoying for the users.
2096 if (cmap == None)
2097 return VO_NOTAVAIL;
2099 if (!strcasecmp(name, "brightness"))
2100 vo_brightness = value;
2101 else if (!strcasecmp(name, "contrast"))
2102 vo_contrast = value;
2103 else if (!strcasecmp(name, "gamma"))
2104 vo_gamma = value;
2105 else
2106 return VO_NOTIMPL;
2108 brightness = 0.01 * vo_brightness;
2109 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
2110 gamma = pow(2, -0.02 * vo_gamma);
2112 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
2113 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
2114 green_mask;
2115 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
2117 /* now recalculate the colormap using the newly set value */
2118 for (k = 0; k < cm_size; k++)
2120 float s;
2122 s = pow(rf * k, gamma);
2123 s = (s - 0.5) * contrast + 0.5;
2124 s += brightness;
2125 if (s < 0)
2126 s = 0;
2127 if (s > 1)
2128 s = 1;
2129 cols[k].red = (unsigned short) (s * 65535);
2131 s = pow(gf * k, gamma);
2132 s = (s - 0.5) * contrast + 0.5;
2133 s += brightness;
2134 if (s < 0)
2135 s = 0;
2136 if (s > 1)
2137 s = 1;
2138 cols[k].green = (unsigned short) (s * 65535);
2140 s = pow(bf * k, gamma);
2141 s = (s - 0.5) * contrast + 0.5;
2142 s += brightness;
2143 if (s < 0)
2144 s = 0;
2145 if (s > 1)
2146 s = 1;
2147 cols[k].blue = (unsigned short) (s * 65535);
2150 XStoreColors(mDisplay, cmap, cols, cm_size);
2151 XFlush(mDisplay);
2152 return VO_TRUE;
2155 uint32_t vo_x11_get_equalizer(char *name, int *value)
2157 if (cmap == None)
2158 return VO_NOTAVAIL;
2159 if (!strcasecmp(name, "brightness"))
2160 *value = vo_brightness;
2161 else if (!strcasecmp(name, "contrast"))
2162 *value = vo_contrast;
2163 else if (!strcasecmp(name, "gamma"))
2164 *value = vo_gamma;
2165 else
2166 return VO_NOTIMPL;
2167 return VO_TRUE;
2170 #ifdef HAVE_XV
2171 int vo_xv_set_eq(uint32_t xv_port, char *name, int value)
2173 XvAttribute *attributes;
2174 int i, howmany, xv_atom;
2176 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
2178 /* get available attributes */
2179 attributes = XvQueryPortAttributes(mDisplay, xv_port, &howmany);
2180 for (i = 0; i < howmany && attributes; i++)
2181 if (attributes[i].flags & XvSettable)
2183 xv_atom = XInternAtom(mDisplay, attributes[i].name, True);
2184 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2185 then trigger it if it's ok so that the other values are at default upon query */
2186 if (xv_atom != None)
2188 int hue = 0, port_value, port_min, port_max;
2190 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2191 (!strcasecmp(name, "brightness")))
2192 port_value = value;
2193 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2194 (!strcasecmp(name, "contrast")))
2195 port_value = value;
2196 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2197 (!strcasecmp(name, "saturation")))
2198 port_value = value;
2199 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2200 (!strcasecmp(name, "hue")))
2202 port_value = value;
2203 hue = 1;
2204 } else
2205 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2206 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2207 (!strcasecmp(name, "red_intensity")))
2208 port_value = value;
2209 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2210 && (!strcasecmp(name, "green_intensity")))
2211 port_value = value;
2212 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2213 && (!strcasecmp(name, "blue_intensity")))
2214 port_value = value;
2215 else
2216 continue;
2218 port_min = attributes[i].min_value;
2219 port_max = attributes[i].max_value;
2221 /* nvidia hue workaround */
2222 if (hue && port_min == 0 && port_max == 360)
2224 port_value =
2225 (port_value >=
2226 0) ? (port_value - 100) : (port_value + 100);
2228 // -100 -> min
2229 // 0 -> (max+min)/2
2230 // +100 -> max
2231 port_value =
2232 (port_value + 100) * (port_max - port_min) / 200 +
2233 port_min;
2234 XvSetPortAttribute(mDisplay, xv_port, xv_atom, port_value);
2235 return (VO_TRUE);
2238 return (VO_FALSE);
2241 int vo_xv_get_eq(uint32_t xv_port, char *name, int *value)
2244 XvAttribute *attributes;
2245 int i, howmany, xv_atom;
2247 /* get available attributes */
2248 attributes = XvQueryPortAttributes(mDisplay, xv_port, &howmany);
2249 for (i = 0; i < howmany && attributes; i++)
2250 if (attributes[i].flags & XvGettable)
2252 xv_atom = XInternAtom(mDisplay, attributes[i].name, True);
2253 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2254 then trigger it if it's ok so that the other values are at default upon query */
2255 if (xv_atom != None)
2257 int val, port_value = 0, port_min, port_max;
2259 XvGetPortAttribute(mDisplay, xv_port, xv_atom,
2260 &port_value);
2262 port_min = attributes[i].min_value;
2263 port_max = attributes[i].max_value;
2264 val =
2265 (port_value - port_min) * 200 / (port_max - port_min) -
2266 100;
2268 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2269 (!strcasecmp(name, "brightness")))
2270 *value = val;
2271 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2272 (!strcasecmp(name, "contrast")))
2273 *value = val;
2274 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2275 (!strcasecmp(name, "saturation")))
2276 *value = val;
2277 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2278 (!strcasecmp(name, "hue")))
2280 /* nasty nvidia detect */
2281 if (port_min == 0 && port_max == 360)
2282 *value = (val >= 0) ? (val - 100) : (val + 100);
2283 else
2284 *value = val;
2285 } else
2286 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2287 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2288 (!strcasecmp(name, "red_intensity")))
2289 *value = val;
2290 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2291 && (!strcasecmp(name, "green_intensity")))
2292 *value = val;
2293 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2294 && (!strcasecmp(name, "blue_intensity")))
2295 *value = val;
2296 else
2297 continue;
2299 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2300 name, *value);
2301 return (VO_TRUE);
2304 return (VO_FALSE);
2307 /** \brief contains flags changing the execution of the colorkeying code */
2308 xv_ck_info_t xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR };
2309 unsigned long xv_colorkey; ///< The color used for manual colorkeying.
2310 unsigned int xv_port; ///< The selected Xv port.
2313 * \brief Interns the requested atom if it is available.
2315 * \param atom_name String containing the name of the requested atom.
2317 * \return Returns the atom if available, else None is returned.
2320 static Atom xv_intern_atom_if_exists( char const * atom_name )
2322 XvAttribute * attributes;
2323 int attrib_count,i;
2324 Atom xv_atom = None;
2326 attributes = XvQueryPortAttributes( mDisplay, xv_port, &attrib_count );
2327 if( attributes!=NULL )
2329 for ( i = 0; i < attrib_count; ++i )
2331 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2333 xv_atom = XInternAtom( mDisplay, atom_name, False );
2334 break; // found what we want, break out
2337 XFree( attributes );
2340 return xv_atom;
2344 * \brief Try to enable vsync for xv.
2345 * \return Returns -1 if not available, 0 on failure and 1 on success.
2347 int vo_xv_enable_vsync(void)
2349 Atom xv_atom = xv_intern_atom_if_exists("XV_SYNC_TO_VBLANK");
2350 if (xv_atom == None)
2351 return -1;
2352 return XvSetPortAttribute(mDisplay, xv_port, xv_atom, 1) == Success;
2356 * \brief Get maximum supported source image dimensions.
2358 * This function does not set the variables pointed to by
2359 * width and height if the information could not be retrieved,
2360 * so the caller is reponsible for properly initializing them.
2362 * \param width [out] The maximum width gets stored here.
2363 * \param height [out] The maximum height gets stored here.
2366 void vo_xv_get_max_img_dim( uint32_t * width, uint32_t * height )
2368 XvEncodingInfo * encodings;
2369 //unsigned long num_encodings, idx; to int or too long?!
2370 unsigned int num_encodings, idx;
2372 XvQueryEncodings( mDisplay, xv_port, &num_encodings, &encodings);
2374 if ( encodings )
2376 for ( idx = 0; idx < num_encodings; ++idx )
2378 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2380 *width = encodings[idx].width;
2381 *height = encodings[idx].height;
2382 break;
2387 mp_msg( MSGT_VO, MSGL_V,
2388 "[xv common] Maximum source image dimensions: %ux%u\n",
2389 *width, *height );
2391 XvFreeEncodingInfo( encodings );
2395 * \brief Print information about the colorkey method and source.
2397 * \param ck_handling Integer value containing the information about
2398 * colorkey handling (see x11_common.h).
2400 * Outputs the content of |ck_handling| as a readable message.
2403 void vo_xv_print_ck_info(void)
2405 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2407 switch ( xv_ck_info.method )
2409 case CK_METHOD_NONE:
2410 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2411 case CK_METHOD_AUTOPAINT:
2412 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2413 case CK_METHOD_MANUALFILL:
2414 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2415 case CK_METHOD_BACKGROUND:
2416 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2419 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2421 switch ( xv_ck_info.source )
2423 case CK_SRC_CUR:
2424 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2425 xv_colorkey );
2426 break;
2427 case CK_SRC_USE:
2428 if ( xv_ck_info.method == CK_METHOD_AUTOPAINT )
2430 mp_msg( MSGT_VO, MSGL_V,
2431 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2432 xv_colorkey );
2434 else
2436 mp_msg( MSGT_VO, MSGL_V,
2437 "Using colorkey from MPlayer (0x%06lx)."
2438 " Use -colorkey to change.\n",
2439 xv_colorkey );
2441 break;
2442 case CK_SRC_SET:
2443 mp_msg( MSGT_VO, MSGL_V,
2444 "Setting and using colorkey from MPlayer (0x%06lx)."
2445 " Use -colorkey to change.\n",
2446 xv_colorkey );
2447 break;
2451 * \brief Init colorkey depending on the settings in xv_ck_info.
2453 * \return Returns 0 on failure and 1 on success.
2455 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2456 * flags in xv_ck_info.
2458 * Possiblilities:
2459 * * Methods
2460 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2461 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2462 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2463 * * Sources
2464 * - use currently set colorkey ( CK_SRC_CUR )
2465 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2466 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2468 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2469 * we don't draw anything as this means it was forced to off.
2471 int vo_xv_init_colorkey(void)
2473 Atom xv_atom;
2474 int rez;
2476 /* check if colorkeying is needed */
2477 xv_atom = xv_intern_atom_if_exists( "XV_COLORKEY" );
2479 /* if we have to deal with colorkeying ... */
2480 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2482 /* check if we should use the colorkey specified in vo_colorkey */
2483 if ( xv_ck_info.source != CK_SRC_CUR )
2485 xv_colorkey = vo_colorkey;
2487 /* check if we have to set the colorkey too */
2488 if ( xv_ck_info.source == CK_SRC_SET )
2490 xv_atom = XInternAtom(mDisplay, "XV_COLORKEY",False);
2492 rez = XvSetPortAttribute( mDisplay, xv_port, xv_atom, vo_colorkey );
2493 if ( rez != Success )
2495 mp_msg( MSGT_VO, MSGL_FATAL,
2496 "[xv common] Couldn't set colorkey!\n" );
2497 return 0; // error setting colorkey
2501 else
2503 int colorkey_ret;
2505 rez=XvGetPortAttribute(mDisplay,xv_port, xv_atom, &colorkey_ret);
2506 if ( rez == Success )
2508 xv_colorkey = colorkey_ret;
2510 else
2512 mp_msg( MSGT_VO, MSGL_FATAL,
2513 "[xv common] Couldn't get colorkey!"
2514 "Maybe the selected Xv port has no overlay.\n" );
2515 return 0; // error getting colorkey
2519 xv_atom = xv_intern_atom_if_exists( "XV_AUTOPAINT_COLORKEY" );
2521 /* should we draw the colorkey ourselves or activate autopainting? */
2522 if ( xv_ck_info.method == CK_METHOD_AUTOPAINT )
2524 rez = !Success; // reset rez to something different than Success
2526 if ( xv_atom != None ) // autopaint is supported
2528 rez = XvSetPortAttribute( mDisplay, xv_port, xv_atom, 1 );
2531 if ( rez != Success )
2533 // fallback to manual colorkey drawing
2534 xv_ck_info.method = CK_METHOD_MANUALFILL;
2537 else // disable colorkey autopainting if supported
2539 if ( xv_atom != None ) // we have autopaint attribute
2541 XvSetPortAttribute( mDisplay, xv_port, xv_atom, 0 );
2545 else // do no colorkey drawing at all
2547 xv_ck_info.method = CK_METHOD_NONE;
2548 } /* end: should we draw colorkey */
2550 /* output information about the current colorkey settings */
2551 vo_xv_print_ck_info();
2553 return 1; // success
2557 * \brief Draw the colorkey on the video window.
2559 * Draws the colorkey depending on the set method ( colorkey_handling ).
2561 * Also draws the black bars ( when the video doesn't fit the display in
2562 * fullscreen ) separately, so they don't overlap with the video area.
2563 * It doesn't call XFlush.
2566 inline void vo_xv_draw_colorkey( int32_t x, int32_t y,
2567 int32_t w, int32_t h )
2569 if( xv_ck_info.method == CK_METHOD_MANUALFILL ||
2570 xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2572 XSetForeground( mDisplay, vo_gc, xv_colorkey );
2573 XFillRectangle( mDisplay, vo_window, vo_gc,
2574 x, y,
2575 w, h );
2578 /* draw black bars if needed */
2579 /* TODO! move this to vo_x11_clearwindow_part() */
2580 if ( vo_fs )
2582 XSetForeground( mDisplay, vo_gc, 0 );
2583 /* making non-overlap fills, requires 8 checks instead of 4 */
2584 if ( y > 0 )
2585 XFillRectangle( mDisplay, vo_window, vo_gc,
2586 0, 0,
2587 vo_screenwidth, y);
2588 if (x > 0)
2589 XFillRectangle( mDisplay, vo_window, vo_gc,
2590 0, 0,
2591 x, vo_screenheight);
2592 if (x + w < vo_screenwidth)
2593 XFillRectangle( mDisplay, vo_window, vo_gc,
2594 x + w, 0,
2595 vo_screenwidth, vo_screenheight);
2596 if (y + h < vo_screenheight)
2597 XFillRectangle( mDisplay, vo_window, vo_gc,
2598 0, y + h,
2599 vo_screenwidth, vo_screenheight);
2603 /** \brief Tests if a valid argument for the ck suboption was given. */
2604 int xv_test_ck( void * arg )
2606 strarg_t * strarg = (strarg_t *)arg;
2608 if ( strargcmp( strarg, "use" ) == 0 ||
2609 strargcmp( strarg, "set" ) == 0 ||
2610 strargcmp( strarg, "cur" ) == 0 )
2612 return 1;
2615 return 0;
2617 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2618 int xv_test_ckm( void * arg )
2620 strarg_t * strarg = (strarg_t *)arg;
2622 if ( strargcmp( strarg, "bg" ) == 0 ||
2623 strargcmp( strarg, "man" ) == 0 ||
2624 strargcmp( strarg, "auto" ) == 0 )
2626 return 1;
2629 return 0;
2633 * \brief Modify the colorkey_handling var according to str
2635 * Checks if a valid pointer ( not NULL ) to the string
2636 * was given. And in that case modifies the colorkey_handling
2637 * var to reflect the requested behaviour.
2638 * If nothing happens the content of colorkey_handling stays
2639 * the same.
2641 * \param str Pointer to the string or NULL
2644 void xv_setup_colorkeyhandling( char const * ck_method_str,
2645 char const * ck_str )
2647 /* check if a valid pointer to the string was passed */
2648 if ( ck_str )
2650 if ( strncmp( ck_str, "use", 3 ) == 0 )
2652 xv_ck_info.source = CK_SRC_USE;
2654 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2656 xv_ck_info.source = CK_SRC_SET;
2659 /* check if a valid pointer to the string was passed */
2660 if ( ck_method_str )
2662 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2664 xv_ck_info.method = CK_METHOD_BACKGROUND;
2666 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2668 xv_ck_info.method = CK_METHOD_MANUALFILL;
2670 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2672 xv_ck_info.method = CK_METHOD_AUTOPAINT;
2677 #endif