Replace some Hungarian comments, thanks to Denes Balatoni for the translation.
[mplayer/greg.git] / libvo / x11_common.c
blob7f22a0c59d9fd0b03e75f4948b53a01992f27129
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 case XF86XK_AudioLowerVolume:
580 mplayer_put_key(KEY_VOLUME_DOWN);
581 break;
582 case XF86XK_AudioRaiseVolume:
583 mplayer_put_key(KEY_VOLUME_UP);
584 break;
585 default:
586 break;
589 #endif
591 void vo_x11_putkey(int key)
593 switch (key)
595 case wsLeft:
596 mplayer_put_key(KEY_LEFT);
597 break;
598 case wsRight:
599 mplayer_put_key(KEY_RIGHT);
600 break;
601 case wsUp:
602 mplayer_put_key(KEY_UP);
603 break;
604 case wsDown:
605 mplayer_put_key(KEY_DOWN);
606 break;
607 case wsSpace:
608 mplayer_put_key(' ');
609 break;
610 case wsEscape:
611 mplayer_put_key(KEY_ESC);
612 break;
613 case wsTab:
614 mplayer_put_key(KEY_TAB);
615 break;
616 case wsEnter:
617 mplayer_put_key(KEY_ENTER);
618 break;
619 case wsBackSpace:
620 mplayer_put_key(KEY_BS);
621 break;
622 case wsDelete:
623 mplayer_put_key(KEY_DELETE);
624 break;
625 case wsInsert:
626 mplayer_put_key(KEY_INSERT);
627 break;
628 case wsHome:
629 mplayer_put_key(KEY_HOME);
630 break;
631 case wsEnd:
632 mplayer_put_key(KEY_END);
633 break;
634 case wsPageUp:
635 mplayer_put_key(KEY_PAGE_UP);
636 break;
637 case wsPageDown:
638 mplayer_put_key(KEY_PAGE_DOWN);
639 break;
640 case wsF1:
641 mplayer_put_key(KEY_F + 1);
642 break;
643 case wsF2:
644 mplayer_put_key(KEY_F + 2);
645 break;
646 case wsF3:
647 mplayer_put_key(KEY_F + 3);
648 break;
649 case wsF4:
650 mplayer_put_key(KEY_F + 4);
651 break;
652 case wsF5:
653 mplayer_put_key(KEY_F + 5);
654 break;
655 case wsF6:
656 mplayer_put_key(KEY_F + 6);
657 break;
658 case wsF7:
659 mplayer_put_key(KEY_F + 7);
660 break;
661 case wsF8:
662 mplayer_put_key(KEY_F + 8);
663 break;
664 case wsF9:
665 mplayer_put_key(KEY_F + 9);
666 break;
667 case wsF10:
668 mplayer_put_key(KEY_F + 10);
669 break;
670 case wsF11:
671 mplayer_put_key(KEY_F + 11);
672 break;
673 case wsF12:
674 mplayer_put_key(KEY_F + 12);
675 break;
676 case wsMinus:
677 case wsGrayMinus:
678 mplayer_put_key('-');
679 break;
680 case wsPlus:
681 case wsGrayPlus:
682 mplayer_put_key('+');
683 break;
684 case wsGrayMul:
685 case wsMul:
686 mplayer_put_key('*');
687 break;
688 case wsGrayDiv:
689 case wsDiv:
690 mplayer_put_key('/');
691 break;
692 case wsLess:
693 mplayer_put_key('<');
694 break;
695 case wsMore:
696 mplayer_put_key('>');
697 break;
698 case wsGray0:
699 mplayer_put_key(KEY_KP0);
700 break;
701 case wsGrayEnd:
702 case wsGray1:
703 mplayer_put_key(KEY_KP1);
704 break;
705 case wsGrayDown:
706 case wsGray2:
707 mplayer_put_key(KEY_KP2);
708 break;
709 case wsGrayPgDn:
710 case wsGray3:
711 mplayer_put_key(KEY_KP3);
712 break;
713 case wsGrayLeft:
714 case wsGray4:
715 mplayer_put_key(KEY_KP4);
716 break;
717 case wsGray5Dup:
718 case wsGray5:
719 mplayer_put_key(KEY_KP5);
720 break;
721 case wsGrayRight:
722 case wsGray6:
723 mplayer_put_key(KEY_KP6);
724 break;
725 case wsGrayHome:
726 case wsGray7:
727 mplayer_put_key(KEY_KP7);
728 break;
729 case wsGrayUp:
730 case wsGray8:
731 mplayer_put_key(KEY_KP8);
732 break;
733 case wsGrayPgUp:
734 case wsGray9:
735 mplayer_put_key(KEY_KP9);
736 break;
737 case wsGrayDecimal:
738 mplayer_put_key(KEY_KPDEC);
739 break;
740 case wsGrayInsert:
741 mplayer_put_key(KEY_KPINS);
742 break;
743 case wsGrayDelete:
744 mplayer_put_key(KEY_KPDEL);
745 break;
746 case wsGrayEnter:
747 mplayer_put_key(KEY_KPENTER);
748 break;
749 case wsGrave:
750 mplayer_put_key('`');
751 break;
752 case wsTilde:
753 mplayer_put_key('~');
754 break;
755 case wsExclSign:
756 mplayer_put_key('!');
757 break;
758 case wsAt:
759 mplayer_put_key('@');
760 break;
761 case wsHash:
762 mplayer_put_key('#');
763 break;
764 case wsDollar:
765 mplayer_put_key('$');
766 break;
767 case wsPercent:
768 mplayer_put_key('%');
769 break;
770 case wsCircumflex:
771 mplayer_put_key('^');
772 break;
773 case wsAmpersand:
774 mplayer_put_key('&');
775 break;
776 case wsobracket:
777 mplayer_put_key('(');
778 break;
779 case wscbracket:
780 mplayer_put_key(')');
781 break;
782 case wsUnder:
783 mplayer_put_key('_');
784 break;
785 case wsocbracket:
786 mplayer_put_key('{');
787 break;
788 case wsccbracket:
789 mplayer_put_key('}');
790 break;
791 case wsColon:
792 mplayer_put_key(':');
793 break;
794 case wsSemicolon:
795 mplayer_put_key(';');
796 break;
797 case wsDblQuote:
798 mplayer_put_key('\"');
799 break;
800 case wsAcute:
801 mplayer_put_key('\'');
802 break;
803 case wsComma:
804 mplayer_put_key(',');
805 break;
806 case wsPoint:
807 mplayer_put_key('.');
808 break;
809 case wsQuestSign:
810 mplayer_put_key('?');
811 break;
812 case wsBSlash:
813 mplayer_put_key('\\');
814 break;
815 case wsPipe:
816 mplayer_put_key('|');
817 break;
818 case wsEqual:
819 mplayer_put_key('=');
820 break;
821 case wsosbrackets:
822 mplayer_put_key('[');
823 break;
824 case wscsbrackets:
825 mplayer_put_key(']');
826 break;
829 default:
830 if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') ||
831 (key >= '0' && key <= '9'))
832 mplayer_put_key(key);
838 // ----- Motif header: -------
840 #define MWM_HINTS_FUNCTIONS (1L << 0)
841 #define MWM_HINTS_DECORATIONS (1L << 1)
842 #define MWM_HINTS_INPUT_MODE (1L << 2)
843 #define MWM_HINTS_STATUS (1L << 3)
845 #define MWM_FUNC_ALL (1L << 0)
846 #define MWM_FUNC_RESIZE (1L << 1)
847 #define MWM_FUNC_MOVE (1L << 2)
848 #define MWM_FUNC_MINIMIZE (1L << 3)
849 #define MWM_FUNC_MAXIMIZE (1L << 4)
850 #define MWM_FUNC_CLOSE (1L << 5)
852 #define MWM_DECOR_ALL (1L << 0)
853 #define MWM_DECOR_BORDER (1L << 1)
854 #define MWM_DECOR_RESIZEH (1L << 2)
855 #define MWM_DECOR_TITLE (1L << 3)
856 #define MWM_DECOR_MENU (1L << 4)
857 #define MWM_DECOR_MINIMIZE (1L << 5)
858 #define MWM_DECOR_MAXIMIZE (1L << 6)
860 #define MWM_INPUT_MODELESS 0
861 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
862 #define MWM_INPUT_SYSTEM_MODAL 2
863 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
864 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
866 #define MWM_TEAROFF_WINDOW (1L<<0)
868 typedef struct
870 long flags;
871 long functions;
872 long decorations;
873 long input_mode;
874 long state;
875 } MotifWmHints;
877 static MotifWmHints vo_MotifWmHints;
878 static Atom vo_MotifHints = None;
880 void vo_x11_decoration(Display * vo_Display, Window w, int d)
882 static unsigned int olddecor = MWM_DECOR_ALL;
883 static unsigned int oldfuncs =
884 MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
885 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE;
886 Atom mtype;
887 int mformat;
888 unsigned long mn, mb;
890 if (!WinID)
891 return;
893 if (vo_fsmode & 8)
895 XSetTransientForHint(vo_Display, w,
896 RootWindow(vo_Display, mScreen));
899 vo_MotifHints = XInternAtom(vo_Display, "_MOTIF_WM_HINTS", 0);
900 if (vo_MotifHints != None)
902 if (!d)
904 MotifWmHints *mhints = NULL;
906 XGetWindowProperty(vo_Display, w, vo_MotifHints, 0, 20, False,
907 vo_MotifHints, &mtype, &mformat, &mn,
908 &mb, (unsigned char **) &mhints);
909 if (mhints)
911 if (mhints->flags & MWM_HINTS_DECORATIONS)
912 olddecor = mhints->decorations;
913 if (mhints->flags & MWM_HINTS_FUNCTIONS)
914 oldfuncs = mhints->functions;
915 XFree(mhints);
919 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
920 vo_MotifWmHints.flags =
921 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
922 if (d)
924 vo_MotifWmHints.functions = oldfuncs;
925 d = olddecor;
927 #if 0
928 vo_MotifWmHints.decorations =
929 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
930 #else
931 vo_MotifWmHints.decorations =
932 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
933 #endif
934 XChangeProperty(vo_Display, w, vo_MotifHints, vo_MotifHints, 32,
935 PropModeReplace,
936 (unsigned char *) &vo_MotifWmHints,
937 (vo_fsmode & 4) ? 4 : 5);
941 void vo_x11_classhint(Display * display, Window window, char *name)
943 XClassHint wmClass;
944 pid_t pid = getpid();
946 wmClass.res_name = name;
947 wmClass.res_class = "MPlayer";
948 XSetClassHint(display, window, &wmClass);
949 XChangeProperty(display, window, XA_NET_WM_PID, XA_CARDINAL, 32,
950 PropModeReplace, (unsigned char *) &pid, 1);
953 Window vo_window = None;
954 GC vo_gc = NULL;
955 GC f_gc = NULL;
956 XSizeHints vo_hint;
958 #ifdef HAVE_NEW_GUI
959 void vo_setwindow(Window w, GC g)
961 vo_window = w;
962 vo_gc = g;
964 #endif
966 void vo_x11_uninit(void)
968 saver_on(mDisplay);
969 if (vo_window != None)
970 vo_showcursor(mDisplay, vo_window);
972 if (f_gc)
974 XFreeGC(mDisplay, f_gc);
975 f_gc = NULL;
977 #ifdef HAVE_NEW_GUI
978 /* destroy window only if it's not controlled by the GUI */
979 if (!use_gui)
980 #endif
982 if (vo_gc)
984 XSetBackground(mDisplay, vo_gc, 0);
985 XFreeGC(mDisplay, vo_gc);
986 vo_gc = NULL;
988 if (vo_window != None)
990 XClearWindow(mDisplay, vo_window);
991 if (WinID < 0)
993 XEvent xev;
995 XUnmapWindow(mDisplay, vo_window);
996 XDestroyWindow(mDisplay, vo_window);
999 XNextEvent(mDisplay, &xev);
1001 while (xev.type != DestroyNotify
1002 || xev.xdestroywindow.event != vo_window);
1004 vo_window = None;
1006 vo_fs = 0;
1007 vo_old_width = vo_old_height = 0;
1011 static unsigned int mouse_timer;
1012 static int mouse_waiting_hide;
1014 int vo_x11_check_events(Display * mydisplay)
1016 int ret = 0;
1017 XEvent Event;
1018 char buf[100];
1019 KeySym keySym;
1020 static XComposeStatus stat;
1022 // unsigned long vo_KeyTable[512];
1024 if ((vo_mouse_autohide) && mouse_waiting_hide &&
1025 (GetTimerMS() - mouse_timer >= 1000)) {
1026 vo_hidecursor(mydisplay, vo_window);
1027 mouse_waiting_hide = 0;
1030 while (XPending(mydisplay))
1032 XNextEvent(mydisplay, &Event);
1033 #ifdef HAVE_NEW_GUI
1034 if (use_gui)
1036 guiGetEvent(0, (char *) &Event);
1037 if (vo_window != Event.xany.window)
1038 continue;
1040 #endif
1041 // printf("\rEvent.type=%X \n",Event.type);
1042 switch (Event.type)
1044 case Expose:
1045 ret |= VO_EVENT_EXPOSE;
1046 break;
1047 case ConfigureNotify:
1048 // if (!vo_fs && (Event.xconfigure.width == vo_screenwidth || Event.xconfigure.height == vo_screenheight)) break;
1049 // if (vo_fs && Event.xconfigure.width != vo_screenwidth && Event.xconfigure.height != vo_screenheight) break;
1050 if (vo_window == None)
1051 break;
1052 vo_dwidth = Event.xconfigure.width;
1053 vo_dheight = Event.xconfigure.height;
1054 #if 0
1055 /* when resizing, x and y are zero :( */
1056 vo_dx = Event.xconfigure.x;
1057 vo_dy = Event.xconfigure.y;
1058 #else
1060 Window root;
1061 int foo;
1062 Window win;
1064 XGetGeometry(mydisplay, vo_window, &root, &foo, &foo,
1065 &foo /*width */ , &foo /*height */ , &foo,
1066 &foo);
1067 XTranslateCoordinates(mydisplay, vo_window, root, 0, 0,
1068 &vo_dx, &vo_dy, &win);
1070 #endif
1071 ret |= VO_EVENT_RESIZE;
1072 break;
1073 case KeyPress:
1075 int key;
1077 #ifdef HAVE_NEW_GUI
1078 if ( use_gui ) { break; }
1079 #endif
1081 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
1082 &stat);
1083 #ifdef XF86XK_AudioPause
1084 vo_x11_putkey_ext(keySym);
1085 #endif
1086 key =
1087 ((keySym & 0xff00) !=
1088 0 ? ((keySym & 0x00ff) + 256) : (keySym));
1089 vo_x11_putkey(key);
1090 ret |= VO_EVENT_KEYPRESS;
1092 break;
1093 case MotionNotify:
1094 if(enable_mouse_movements)
1096 char cmd_str[40];
1097 sprintf(cmd_str,"set_mouse_pos %i %i",Event.xmotion.x, Event.xmotion.y);
1098 mp_input_queue_cmd(mp_input_parse_cmd(cmd_str));
1101 if (vo_mouse_autohide)
1103 vo_showcursor(mydisplay, vo_window);
1104 mouse_waiting_hide = 1;
1105 mouse_timer = GetTimerMS();
1107 break;
1108 case ButtonPress:
1109 if (vo_mouse_autohide)
1111 vo_showcursor(mydisplay, vo_window);
1112 mouse_waiting_hide = 1;
1113 mouse_timer = GetTimerMS();
1115 #ifdef HAVE_NEW_GUI
1116 // Ignore mouse button 1-3 under GUI.
1117 if (use_gui && (Event.xbutton.button >= 1)
1118 && (Event.xbutton.button <= 3))
1119 break;
1120 #endif
1121 mplayer_put_key((MOUSE_BTN0 + Event.xbutton.button -
1122 1) | MP_KEY_DOWN);
1123 break;
1124 case ButtonRelease:
1125 if (vo_mouse_autohide)
1127 vo_showcursor(mydisplay, vo_window);
1128 mouse_waiting_hide = 1;
1129 mouse_timer = GetTimerMS();
1131 #ifdef HAVE_NEW_GUI
1132 // Ignore mouse button 1-3 under GUI.
1133 if (use_gui && (Event.xbutton.button >= 1)
1134 && (Event.xbutton.button <= 3))
1135 break;
1136 #endif
1137 mplayer_put_key(MOUSE_BTN0 + Event.xbutton.button - 1);
1138 break;
1139 case PropertyNotify:
1141 char *name =
1142 XGetAtomName(mydisplay, Event.xproperty.atom);
1144 if (!name)
1145 break;
1147 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
1149 XFree(name);
1151 break;
1152 case MapNotify:
1153 vo_hint.win_gravity = old_gravity;
1154 XSetWMNormalHints(mDisplay, vo_window, &vo_hint);
1155 vo_fs_flip = 0;
1156 break;
1157 case ClientMessage:
1158 if (Event.xclient.message_type == XAWM_PROTOCOLS &&
1159 Event.xclient.data.l[0] == XAWM_DELETE_WINDOW)
1160 mplayer_put_key(KEY_CLOSE_WIN);
1161 break;
1164 return ret;
1168 * \brief sets the size and position of the non-fullscreen window.
1170 void vo_x11_nofs_sizepos(int x, int y, int width, int height)
1172 vo_x11_sizehint(x, y, width, height, 0);
1173 if (vo_fs) {
1174 vo_old_x = x;
1175 vo_old_y = y;
1176 vo_old_width = width;
1177 vo_old_height = height;
1179 else
1181 vo_dwidth = width;
1182 vo_dheight = height;
1183 XMoveResizeWindow(mDisplay, vo_window, x, y, width, height);
1187 void vo_x11_sizehint(int x, int y, int width, int height, int max)
1189 vo_hint.flags = 0;
1190 if (vo_keepaspect)
1192 vo_hint.flags |= PAspect;
1193 vo_hint.min_aspect.x = width;
1194 vo_hint.min_aspect.y = height;
1195 vo_hint.max_aspect.x = width;
1196 vo_hint.max_aspect.y = height;
1199 vo_hint.flags |= PPosition | PSize;
1200 vo_hint.x = x;
1201 vo_hint.y = y;
1202 vo_hint.width = width;
1203 vo_hint.height = height;
1204 if (max)
1206 vo_hint.flags |= PMaxSize;
1207 vo_hint.max_width = width;
1208 vo_hint.max_height = height;
1209 } else
1211 vo_hint.max_width = 0;
1212 vo_hint.max_height = 0;
1215 // Set minimum height/width to 4 to avoid off-by-one errors
1216 // and because mga_vid requires a minimal size of 4 pixels.
1217 vo_hint.flags |= PMinSize;
1218 vo_hint.min_width = vo_hint.min_height = 4;
1220 vo_hint.flags |= PWinGravity;
1221 vo_hint.win_gravity = StaticGravity;
1222 XSetWMNormalHints(mDisplay, vo_window, &vo_hint);
1225 static int vo_x11_get_gnome_layer(Display * mDisplay, Window win)
1227 Atom type;
1228 int format;
1229 unsigned long nitems;
1230 unsigned long bytesafter;
1231 unsigned short *args = NULL;
1233 if (XGetWindowProperty(mDisplay, win, XA_WIN_LAYER, 0, 16384,
1234 False, AnyPropertyType, &type, &format, &nitems,
1235 &bytesafter,
1236 (unsigned char **) &args) == Success
1237 && nitems > 0 && args)
1239 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
1240 *args);
1241 return *args;
1243 return WIN_LAYER_NORMAL;
1247 Window vo_x11_create_smooth_window(Display * mDisplay, Window mRoot,
1248 Visual * vis, int x, int y,
1249 unsigned int width, unsigned int height,
1250 int depth, Colormap col_map)
1252 unsigned long xswamask = CWBackingStore | CWBorderPixel;
1253 XSetWindowAttributes xswa;
1254 Window ret_win;
1256 if (col_map != CopyFromParent)
1258 xswa.colormap = col_map;
1259 xswamask |= CWColormap;
1261 xswa.background_pixel = 0;
1262 xswa.border_pixel = 0;
1263 xswa.backing_store = Always;
1264 xswa.bit_gravity = StaticGravity;
1266 ret_win =
1267 XCreateWindow(mDisplay, mRootWin, x, y, width, height, 0, depth,
1268 CopyFromParent, vis, xswamask, &xswa);
1269 XSetWMProtocols(mDisplay, ret_win, &XAWM_DELETE_WINDOW, 1);
1270 if (!f_gc)
1271 f_gc = XCreateGC(mDisplay, ret_win, 0, 0);
1272 XSetForeground(mDisplay, f_gc, 0);
1274 return ret_win;
1278 * \brief create and setup a window suitable for display
1279 * \param vis Visual to use for creating the window
1280 * \param x x position of window
1281 * \param y y position of window
1282 * \param width width of window
1283 * \param height height of window
1284 * \param flags flags for window creation.
1285 * Only VOFLAG_FULLSCREEN is supported so far.
1286 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1287 * \param classname name to use for the classhint
1288 * \param title title for the window
1290 * This also does the grunt-work like setting Window Manager hints etc.
1291 * If vo_window is already set it just moves and resizes it.
1293 void vo_x11_create_vo_window(XVisualInfo *vis, int x, int y,
1294 unsigned int width, unsigned int height, int flags,
1295 Colormap col_map,
1296 const char *classname, const char *title)
1298 if (vo_window == None) {
1299 XSizeHints hint;
1300 XEvent xev;
1301 vo_fs = 0;
1302 vo_dwidth = width;
1303 vo_dheight = height;
1304 vo_window = vo_x11_create_smooth_window(mDisplay, mRootWin, vis->visual,
1305 x, y, width, height, vis->depth, col_map);
1306 vo_x11_classhint(mDisplay, vo_window, classname);
1307 XStoreName(mDisplay, vo_window, title);
1308 vo_hidecursor(mDisplay, vo_window);
1309 XSelectInput(mDisplay, vo_window, StructureNotifyMask);
1310 hint.x = x; hint.y = y;
1311 hint.width = width; hint.height = height;
1312 hint.flags = PPosition | PSize;
1313 XSetStandardProperties(mDisplay, vo_window, title, title, None, NULL, 0, &hint);
1314 vo_x11_sizehint(x, y, width, height, 0);
1315 // map window
1316 XMapWindow(mDisplay, vo_window);
1317 XClearWindow(mDisplay, vo_window);
1318 // wait for map
1319 do {
1320 XNextEvent(mDisplay, &xev);
1321 } while (xev.type != MapNotify || xev.xmap.event != vo_window);
1322 XSelectInput(mDisplay, vo_window, NoEventMask);
1323 XSync(mDisplay, False);
1324 vo_x11_selectinput_witherr(mDisplay, vo_window,
1325 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1326 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1328 if (vo_ontop) vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
1329 vo_x11_nofs_sizepos(vo_dx, vo_dy, width, height);
1330 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1331 vo_x11_fullscreen();
1334 void vo_x11_clearwindow_part(Display * mDisplay, Window vo_window,
1335 int img_width, int img_height, int use_fs)
1337 int u_dheight, u_dwidth, left_ov, left_ov2;
1339 if (!f_gc)
1340 return;
1342 u_dheight = use_fs ? vo_screenheight : vo_dheight;
1343 u_dwidth = use_fs ? vo_screenwidth : vo_dwidth;
1344 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1345 return;
1347 left_ov = (u_dheight - img_height) / 2;
1348 left_ov2 = (u_dwidth - img_width) / 2;
1350 XFillRectangle(mDisplay, vo_window, f_gc, 0, 0, u_dwidth, left_ov);
1351 XFillRectangle(mDisplay, vo_window, f_gc, 0, u_dheight - left_ov - 1,
1352 u_dwidth, left_ov + 1);
1354 if (u_dwidth > img_width)
1356 XFillRectangle(mDisplay, vo_window, f_gc, 0, left_ov, left_ov2,
1357 img_height);
1358 XFillRectangle(mDisplay, vo_window, f_gc, u_dwidth - left_ov2 - 1,
1359 left_ov, left_ov2 + 1, img_height);
1362 XFlush(mDisplay);
1365 void vo_x11_clearwindow(Display * mDisplay, Window vo_window)
1367 if (!f_gc)
1368 return;
1369 XFillRectangle(mDisplay, vo_window, f_gc, 0, 0, vo_screenwidth,
1370 vo_screenheight);
1372 XFlush(mDisplay);
1376 void vo_x11_setlayer(Display * mDisplay, Window vo_window, int layer)
1378 if (WinID >= 0)
1379 return;
1381 if (vo_fs_type & vo_wm_LAYER)
1383 XClientMessageEvent xev;
1385 if (!orig_layer)
1386 orig_layer = vo_x11_get_gnome_layer(mDisplay, vo_window);
1388 memset(&xev, 0, sizeof(xev));
1389 xev.type = ClientMessage;
1390 xev.display = mDisplay;
1391 xev.window = vo_window;
1392 xev.message_type = XA_WIN_LAYER;
1393 xev.format = 32;
1394 xev.data.l[0] = layer ? fs_layer : orig_layer; // if not fullscreen, stay on default layer
1395 xev.data.l[1] = CurrentTime;
1396 mp_msg(MSGT_VO, MSGL_V,
1397 "[x11] Layered style stay on top (layer %ld).\n",
1398 xev.data.l[0]);
1399 XSendEvent(mDisplay, mRootWin, False, SubstructureNotifyMask,
1400 (XEvent *) & xev);
1401 } else if (vo_fs_type & vo_wm_NETWM)
1403 XClientMessageEvent xev;
1404 char *state;
1406 memset(&xev, 0, sizeof(xev));
1407 xev.type = ClientMessage;
1408 xev.message_type = XA_NET_WM_STATE;
1409 xev.display = mDisplay;
1410 xev.window = vo_window;
1411 xev.format = 32;
1412 xev.data.l[0] = layer;
1414 if (vo_fs_type & vo_wm_STAYS_ON_TOP)
1415 xev.data.l[1] = XA_NET_WM_STATE_STAYS_ON_TOP;
1416 else if (vo_fs_type & vo_wm_ABOVE)
1417 xev.data.l[1] = XA_NET_WM_STATE_ABOVE;
1418 else if (vo_fs_type & vo_wm_FULLSCREEN)
1419 xev.data.l[1] = XA_NET_WM_STATE_FULLSCREEN;
1420 else if (vo_fs_type & vo_wm_BELOW)
1421 // This is not fallback. We can safely assume that the situation
1422 // where only NETWM_STATE_BELOW is supported doesn't exist.
1423 xev.data.l[1] = XA_NET_WM_STATE_BELOW;
1425 XSendEvent(mDisplay, mRootWin, False, SubstructureRedirectMask,
1426 (XEvent *) & xev);
1427 state = XGetAtomName(mDisplay, xev.data.l[1]);
1428 mp_msg(MSGT_VO, MSGL_V,
1429 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1430 layer, state);
1431 XFree(state);
1435 static int vo_x11_get_fs_type(int supported)
1437 int i;
1438 int type = supported;
1440 if (vo_fstype_list)
1442 i = 0;
1443 for (i = 0; vo_fstype_list[i]; i++)
1445 int neg = 0;
1446 char *arg = vo_fstype_list[i];
1448 if (vo_fstype_list[i][0] == '-')
1450 neg = 1;
1451 arg = vo_fstype_list[i] + 1;
1454 if (!strncmp(arg, "layer", 5))
1456 if (!neg && (arg[5] == '='))
1458 char *endptr = NULL;
1459 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1461 if (endptr && *endptr == '\0' && layer >= 0
1462 && layer <= 15)
1463 fs_layer = layer;
1465 if (neg)
1466 type &= ~vo_wm_LAYER;
1467 else
1468 type |= vo_wm_LAYER;
1469 } else if (!strcmp(arg, "above"))
1471 if (neg)
1472 type &= ~vo_wm_ABOVE;
1473 else
1474 type |= vo_wm_ABOVE;
1475 } else if (!strcmp(arg, "fullscreen"))
1477 if (neg)
1478 type &= ~vo_wm_FULLSCREEN;
1479 else
1480 type |= vo_wm_FULLSCREEN;
1481 } else if (!strcmp(arg, "stays_on_top"))
1483 if (neg)
1484 type &= ~vo_wm_STAYS_ON_TOP;
1485 else
1486 type |= vo_wm_STAYS_ON_TOP;
1487 } else if (!strcmp(arg, "below"))
1489 if (neg)
1490 type &= ~vo_wm_BELOW;
1491 else
1492 type |= vo_wm_BELOW;
1493 } else if (!strcmp(arg, "netwm"))
1495 if (neg)
1496 type &= ~vo_wm_NETWM;
1497 else
1498 type |= vo_wm_NETWM;
1499 } else if (!strcmp(arg, "none"))
1500 return 0;
1504 return type;
1507 void vo_x11_fullscreen(void)
1509 int x, y, w, h;
1511 if (WinID >= 0 || vo_fs_flip)
1512 return;
1514 if (vo_fs)
1516 // fs->win
1517 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1519 x = vo_old_x;
1520 y = vo_old_y;
1521 w = vo_old_width;
1522 h = vo_old_height;
1525 vo_x11_ewmh_fullscreen(_NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1526 vo_fs = VO_FALSE;
1527 } else
1529 // win->fs
1530 vo_x11_ewmh_fullscreen(_NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1532 vo_fs = VO_TRUE;
1533 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1535 vo_old_x = vo_dx;
1536 vo_old_y = vo_dy;
1537 vo_old_width = vo_dwidth;
1538 vo_old_height = vo_dheight;
1540 update_xinerama_info();
1541 x = xinerama_x;
1542 y = xinerama_y;
1543 w = vo_screenwidth;
1544 h = vo_screenheight;
1547 long dummy;
1549 XGetWMNormalHints(mDisplay, vo_window, &vo_hint, &dummy);
1550 if (!(vo_hint.flags & PWinGravity))
1551 old_gravity = NorthWestGravity;
1552 else
1553 old_gravity = vo_hint.win_gravity;
1555 if (vo_wm_type == 0 && !(vo_fsmode & 16))
1557 XUnmapWindow(mDisplay, vo_window); // required for MWM
1558 XWithdrawWindow(mDisplay, vo_window, mScreen);
1559 vo_fs_flip = 1;
1562 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1564 vo_x11_decoration(mDisplay, vo_window, (vo_fs) ? 0 : 1);
1565 vo_x11_sizehint(x, y, w, h, 0);
1566 vo_x11_setlayer(mDisplay, vo_window, vo_fs);
1569 XMoveResizeWindow(mDisplay, vo_window, x, y, w, h);
1571 /* some WMs lose ontop after fullscreen */
1572 if ((!(vo_fs)) & vo_ontop)
1573 vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
1575 XMapRaised(mDisplay, vo_window);
1576 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1577 XMoveResizeWindow(mDisplay, vo_window, x, y, w, h);
1578 XRaiseWindow(mDisplay, vo_window);
1579 XFlush(mDisplay);
1582 void vo_x11_ontop(void)
1584 vo_ontop = (!(vo_ontop));
1586 vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
1590 * XScreensaver stuff
1593 static int got_badwindow;
1594 static XErrorHandler old_handler;
1596 static int badwindow_handler(Display * dpy, XErrorEvent * error)
1598 if (error->error_code != BadWindow)
1599 return (*old_handler) (dpy, error);
1601 got_badwindow = True;
1602 return 0;
1605 static Window find_xscreensaver_window(Display * dpy)
1607 int i;
1608 Window root = RootWindowOfScreen(DefaultScreenOfDisplay(dpy));
1609 Window root2, parent, *kids;
1610 Window retval = 0;
1611 Atom xs_version;
1612 unsigned int nkids = 0;
1614 xs_version = XInternAtom(dpy, "_SCREENSAVER_VERSION", True);
1616 if (!(xs_version != None &&
1617 XQueryTree(dpy, root, &root2, &parent, &kids, &nkids) &&
1618 kids && nkids))
1619 return 0;
1621 old_handler = XSetErrorHandler(badwindow_handler);
1623 for (i = 0; i < nkids; i++)
1625 Atom type;
1626 int format;
1627 unsigned long nitems, bytesafter;
1628 char *v;
1629 int status;
1631 got_badwindow = False;
1632 status =
1633 XGetWindowProperty(dpy, kids[i], xs_version, 0, 200, False,
1634 XA_STRING, &type, &format, &nitems,
1635 &bytesafter, (unsigned char **) &v);
1636 XSync(dpy, False);
1637 if (got_badwindow)
1638 status = BadWindow;
1640 if (status == Success && type != None)
1642 retval = kids[i];
1643 break;
1646 XFree(kids);
1647 XSetErrorHandler(old_handler);
1649 return retval;
1652 static Window xs_windowid = 0;
1653 static Atom deactivate;
1654 static Atom screensaver;
1656 static unsigned int time_last;
1658 void xscreensaver_heartbeat(void)
1660 unsigned int time = GetTimerMS();
1661 XEvent ev;
1663 if (mDisplay && xs_windowid && (time - time_last) > 30000)
1665 time_last = time;
1667 ev.xany.type = ClientMessage;
1668 ev.xclient.display = mDisplay;
1669 ev.xclient.window = xs_windowid;
1670 ev.xclient.message_type = screensaver;
1671 ev.xclient.format = 32;
1672 memset(&ev.xclient.data, 0, sizeof(ev.xclient.data));
1673 ev.xclient.data.l[0] = (long) deactivate;
1675 mp_msg(MSGT_VO, MSGL_DBG2, "Pinging xscreensaver.\n");
1676 old_handler = XSetErrorHandler(badwindow_handler);
1677 XSendEvent(mDisplay, xs_windowid, False, 0L, &ev);
1678 XSync(mDisplay, False);
1679 XSetErrorHandler(old_handler);
1683 static void xscreensaver_disable(Display * dpy)
1685 mp_msg(MSGT_VO, MSGL_DBG2, "xscreensaver_disable()\n");
1687 xs_windowid = find_xscreensaver_window(dpy);
1688 if (!xs_windowid)
1690 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_CouldNotFindXScreenSaver);
1691 return;
1693 mp_msg(MSGT_VO, MSGL_INFO,
1694 "xscreensaver_disable: xscreensaver wid=%ld.\n", xs_windowid);
1696 deactivate = XInternAtom(dpy, "DEACTIVATE", False);
1697 screensaver = XInternAtom(dpy, "SCREENSAVER", False);
1700 static void xscreensaver_enable(void)
1702 xs_windowid = 0;
1706 * End of XScreensaver stuff
1709 void saver_on(Display * mDisplay)
1712 #ifdef HAVE_XDPMS
1713 int nothing;
1715 if (dpms_disabled)
1717 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1719 if (!DPMSEnable(mDisplay))
1720 { // restoring power saving settings
1721 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1722 } else
1724 // DPMS does not seem to be enabled unless we call DPMSInfo
1725 BOOL onoff;
1726 CARD16 state;
1728 DPMSForceLevel(mDisplay, DPMSModeOn);
1729 DPMSInfo(mDisplay, &state, &onoff);
1730 if (onoff)
1732 mp_msg(MSGT_VO, MSGL_V,
1733 "Successfully enabled DPMS\n");
1734 } else
1736 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1740 dpms_disabled = 0;
1742 #endif
1744 if (timeout_save)
1746 int dummy, interval, prefer_blank, allow_exp;
1748 XGetScreenSaver(mDisplay, &dummy, &interval, &prefer_blank,
1749 &allow_exp);
1750 XSetScreenSaver(mDisplay, timeout_save, interval, prefer_blank,
1751 allow_exp);
1752 XGetScreenSaver(mDisplay, &timeout_save, &interval, &prefer_blank,
1753 &allow_exp);
1754 timeout_save = 0;
1757 if (stop_xscreensaver)
1758 xscreensaver_enable();
1759 if (kdescreensaver_was_running && stop_xscreensaver)
1761 system
1762 ("dcop kdesktop KScreensaverIface enable true 2>/dev/null >/dev/null");
1763 kdescreensaver_was_running = 0;
1769 void saver_off(Display * mDisplay)
1772 int interval, prefer_blank, allow_exp;
1774 #ifdef HAVE_XDPMS
1775 int nothing;
1777 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1779 BOOL onoff;
1780 CARD16 state;
1782 DPMSInfo(mDisplay, &state, &onoff);
1783 if (onoff)
1785 Status stat;
1787 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1788 dpms_disabled = 1;
1789 stat = DPMSDisable(mDisplay); // monitor powersave off
1790 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1793 #endif
1794 if (!timeout_save)
1796 XGetScreenSaver(mDisplay, &timeout_save, &interval, &prefer_blank,
1797 &allow_exp);
1798 if (timeout_save)
1799 XSetScreenSaver(mDisplay, 0, interval, prefer_blank,
1800 allow_exp);
1802 // turning off screensaver
1803 if (stop_xscreensaver)
1804 xscreensaver_disable(mDisplay);
1805 if (stop_xscreensaver && !kdescreensaver_was_running)
1807 kdescreensaver_was_running =
1808 (system
1809 ("dcop kdesktop KScreensaverIface isEnabled 2>/dev/null | sed 's/1/true/g' | grep true 2>/dev/null >/dev/null")
1810 == 0);
1811 if (kdescreensaver_was_running)
1812 system
1813 ("dcop kdesktop KScreensaverIface enable false 2>/dev/null >/dev/null");
1817 static XErrorHandler old_handler = NULL;
1818 static int selectinput_err = 0;
1819 static int x11_selectinput_errorhandler(Display * display,
1820 XErrorEvent * event)
1822 if (event->error_code == BadAccess)
1824 selectinput_err = 1;
1825 mp_msg(MSGT_VO, MSGL_ERR,
1826 "X11 error: BadAccess during XSelectInput Call\n");
1827 mp_msg(MSGT_VO, MSGL_ERR,
1828 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1829 /* If you think MPlayer should shutdown with this error,
1830 * comment out the following line */
1831 return 0;
1833 if (old_handler != NULL)
1834 old_handler(display, event);
1835 else
1836 x11_errorhandler(display, event);
1837 return 0;
1840 void vo_x11_selectinput_witherr(Display * display, Window w,
1841 long event_mask)
1843 XSync(display, False);
1844 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1845 selectinput_err = 0;
1846 if (vo_nomouse_input)
1848 XSelectInput(display, w,
1849 event_mask &
1850 (~(ButtonPressMask | ButtonReleaseMask)));
1851 } else
1853 XSelectInput(display, w, event_mask);
1855 XSync(display, False);
1856 XSetErrorHandler(old_handler);
1857 if (selectinput_err)
1859 mp_msg(MSGT_VO, MSGL_ERR,
1860 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1861 XSelectInput(display, w,
1862 event_mask &
1864 (ButtonPressMask | ButtonReleaseMask |
1865 PointerMotionMask)));
1869 #ifdef HAVE_XF86VM
1870 void vo_vm_switch(uint32_t X, uint32_t Y, int *modeline_width,
1871 int *modeline_height)
1873 int vm_event, vm_error;
1874 int vm_ver, vm_rev;
1875 int i, j, have_vm = 0;
1877 int modecount;
1879 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1881 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1882 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1883 vm_rev);
1884 have_vm = 1;
1885 } else
1886 mp_msg(MSGT_VO, MSGL_WARN,
1887 "XF86VidMode extension not available.\n");
1889 if (have_vm)
1891 if (vidmodes == NULL)
1892 XF86VidModeGetAllModeLines(mDisplay, mScreen, &modecount,
1893 &vidmodes);
1894 j = 0;
1895 *modeline_width = vidmodes[0]->hdisplay;
1896 *modeline_height = vidmodes[0]->vdisplay;
1898 for (i = 1; i < modecount; i++)
1899 if ((vidmodes[i]->hdisplay >= X)
1900 && (vidmodes[i]->vdisplay >= Y))
1901 if ((vidmodes[i]->hdisplay <= *modeline_width)
1902 && (vidmodes[i]->vdisplay <= *modeline_height))
1904 *modeline_width = vidmodes[i]->hdisplay;
1905 *modeline_height = vidmodes[i]->vdisplay;
1906 j = i;
1909 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_SelectedVideoMode,
1910 *modeline_width, *modeline_height, X, Y);
1911 XF86VidModeLockModeSwitch(mDisplay, mScreen, 0);
1912 XF86VidModeSwitchToMode(mDisplay, mScreen, vidmodes[j]);
1913 XF86VidModeSwitchToMode(mDisplay, mScreen, vidmodes[j]);
1914 X = (vo_screenwidth - *modeline_width) / 2;
1915 Y = (vo_screenheight - *modeline_height) / 2;
1916 XF86VidModeSetViewPort(mDisplay, mScreen, X, Y);
1920 void vo_vm_close(Display * dpy)
1922 #ifdef HAVE_NEW_GUI
1923 if (vidmodes != NULL && vo_window != None)
1924 #else
1925 if (vidmodes != NULL)
1926 #endif
1928 int i, modecount;
1929 int screen;
1931 screen = DefaultScreen(dpy);
1933 free(vidmodes);
1934 vidmodes = NULL;
1935 XF86VidModeGetAllModeLines(mDisplay, mScreen, &modecount,
1936 &vidmodes);
1937 for (i = 0; i < modecount; i++)
1938 if ((vidmodes[i]->hdisplay == vo_screenwidth)
1939 && (vidmodes[i]->vdisplay == vo_screenheight))
1941 mp_msg(MSGT_VO, MSGL_INFO,
1942 "Returning to original mode %dx%d\n",
1943 vo_screenwidth, vo_screenheight);
1944 break;
1947 XF86VidModeSwitchToMode(dpy, screen, vidmodes[i]);
1948 XF86VidModeSwitchToMode(dpy, screen, vidmodes[i]);
1949 free(vidmodes);
1950 vidmodes = NULL;
1953 #endif
1955 #endif /* X11_FULLSCREEN */
1959 * Scan the available visuals on this Display/Screen. Try to find
1960 * the 'best' available TrueColor visual that has a decent color
1961 * depth (at least 15bit). If there are multiple visuals with depth
1962 * >= 15bit, we prefer visuals with a smaller color depth.
1964 int vo_find_depth_from_visuals(Display * dpy, int screen,
1965 Visual ** visual_return)
1967 XVisualInfo visual_tmpl;
1968 XVisualInfo *visuals;
1969 int nvisuals, i;
1970 int bestvisual = -1;
1971 int bestvisual_depth = -1;
1973 visual_tmpl.screen = screen;
1974 visual_tmpl.class = TrueColor;
1975 visuals = XGetVisualInfo(dpy,
1976 VisualScreenMask | VisualClassMask,
1977 &visual_tmpl, &nvisuals);
1978 if (visuals != NULL)
1980 for (i = 0; i < nvisuals; i++)
1982 mp_msg(MSGT_VO, MSGL_V,
1983 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1984 visuals[i].visualid, visuals[i].depth,
1985 visuals[i].red_mask, visuals[i].green_mask,
1986 visuals[i].blue_mask);
1988 * Save the visual index and its depth, if this is the first
1989 * truecolor visul, or a visual that is 'preferred' over the
1990 * previous 'best' visual.
1992 if (bestvisual_depth == -1
1993 || (visuals[i].depth >= 15
1994 && (visuals[i].depth < bestvisual_depth
1995 || bestvisual_depth < 15)))
1997 bestvisual = i;
1998 bestvisual_depth = visuals[i].depth;
2002 if (bestvisual != -1 && visual_return != NULL)
2003 *visual_return = visuals[bestvisual].visual;
2005 XFree(visuals);
2007 return bestvisual_depth;
2011 static Colormap cmap = None;
2012 static XColor cols[256];
2013 static int cm_size, red_mask, green_mask, blue_mask;
2016 Colormap vo_x11_create_colormap(XVisualInfo * vinfo)
2018 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
2020 if (vinfo->class != DirectColor)
2021 return XCreateColormap(mDisplay, mRootWin, vinfo->visual,
2022 AllocNone);
2024 /* can this function get called twice or more? */
2025 if (cmap)
2026 return cmap;
2027 cm_size = vinfo->colormap_size;
2028 red_mask = vinfo->red_mask;
2029 green_mask = vinfo->green_mask;
2030 blue_mask = vinfo->blue_mask;
2031 ru = (red_mask & (red_mask - 1)) ^ red_mask;
2032 gu = (green_mask & (green_mask - 1)) ^ green_mask;
2033 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
2034 rvu = 65536ull * ru / (red_mask + ru);
2035 gvu = 65536ull * gu / (green_mask + gu);
2036 bvu = 65536ull * bu / (blue_mask + bu);
2037 r = g = b = 0;
2038 rv = gv = bv = 0;
2039 m = DoRed | DoGreen | DoBlue;
2040 for (k = 0; k < cm_size; k++)
2042 int t;
2044 cols[k].pixel = r | g | b;
2045 cols[k].red = rv;
2046 cols[k].green = gv;
2047 cols[k].blue = bv;
2048 cols[k].flags = m;
2049 t = (r + ru) & red_mask;
2050 if (t < r)
2051 m &= ~DoRed;
2052 r = t;
2053 t = (g + gu) & green_mask;
2054 if (t < g)
2055 m &= ~DoGreen;
2056 g = t;
2057 t = (b + bu) & blue_mask;
2058 if (t < b)
2059 m &= ~DoBlue;
2060 b = t;
2061 rv += rvu;
2062 gv += gvu;
2063 bv += bvu;
2065 cmap = XCreateColormap(mDisplay, mRootWin, vinfo->visual, AllocAll);
2066 XStoreColors(mDisplay, cmap, cols, cm_size);
2067 return cmap;
2071 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
2072 * hue and red/green/blue intensity, but we cannot do saturation.
2073 * Currently only gamma, brightness and contrast are implemented.
2074 * Is there sufficient interest for hue and/or red/green/blue intensity?
2076 /* these values have range [-100,100] and are initially 0 */
2077 static int vo_gamma = 0;
2078 static int vo_brightness = 0;
2079 static int vo_contrast = 0;
2082 uint32_t vo_x11_set_equalizer(char *name, int value)
2084 float gamma, brightness, contrast;
2085 float rf, gf, bf;
2086 int k;
2089 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
2090 * of TrueColor-ed window but be careful:
2091 * Unlike the colormaps, which are private for the X client
2092 * who created them and thus automatically destroyed on client
2093 * disconnect, this gamma ramp is a system-wide (X-server-wide)
2094 * setting and _must_ be restored before the process exits.
2095 * Unforunately when the process crashes (or gets killed
2096 * for some reason) it is impossible to restore the setting,
2097 * and such behaviour could be rather annoying for the users.
2099 if (cmap == None)
2100 return VO_NOTAVAIL;
2102 if (!strcasecmp(name, "brightness"))
2103 vo_brightness = value;
2104 else if (!strcasecmp(name, "contrast"))
2105 vo_contrast = value;
2106 else if (!strcasecmp(name, "gamma"))
2107 vo_gamma = value;
2108 else
2109 return VO_NOTIMPL;
2111 brightness = 0.01 * vo_brightness;
2112 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
2113 gamma = pow(2, -0.02 * vo_gamma);
2115 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
2116 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
2117 green_mask;
2118 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
2120 /* now recalculate the colormap using the newly set value */
2121 for (k = 0; k < cm_size; k++)
2123 float s;
2125 s = pow(rf * k, gamma);
2126 s = (s - 0.5) * contrast + 0.5;
2127 s += brightness;
2128 if (s < 0)
2129 s = 0;
2130 if (s > 1)
2131 s = 1;
2132 cols[k].red = (unsigned short) (s * 65535);
2134 s = pow(gf * k, gamma);
2135 s = (s - 0.5) * contrast + 0.5;
2136 s += brightness;
2137 if (s < 0)
2138 s = 0;
2139 if (s > 1)
2140 s = 1;
2141 cols[k].green = (unsigned short) (s * 65535);
2143 s = pow(bf * k, gamma);
2144 s = (s - 0.5) * contrast + 0.5;
2145 s += brightness;
2146 if (s < 0)
2147 s = 0;
2148 if (s > 1)
2149 s = 1;
2150 cols[k].blue = (unsigned short) (s * 65535);
2153 XStoreColors(mDisplay, cmap, cols, cm_size);
2154 XFlush(mDisplay);
2155 return VO_TRUE;
2158 uint32_t vo_x11_get_equalizer(char *name, int *value)
2160 if (cmap == None)
2161 return VO_NOTAVAIL;
2162 if (!strcasecmp(name, "brightness"))
2163 *value = vo_brightness;
2164 else if (!strcasecmp(name, "contrast"))
2165 *value = vo_contrast;
2166 else if (!strcasecmp(name, "gamma"))
2167 *value = vo_gamma;
2168 else
2169 return VO_NOTIMPL;
2170 return VO_TRUE;
2173 #ifdef HAVE_XV
2174 int vo_xv_set_eq(uint32_t xv_port, char *name, int value)
2176 XvAttribute *attributes;
2177 int i, howmany, xv_atom;
2179 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
2181 /* get available attributes */
2182 attributes = XvQueryPortAttributes(mDisplay, xv_port, &howmany);
2183 for (i = 0; i < howmany && attributes; i++)
2184 if (attributes[i].flags & XvSettable)
2186 xv_atom = XInternAtom(mDisplay, attributes[i].name, True);
2187 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2188 then trigger it if it's ok so that the other values are at default upon query */
2189 if (xv_atom != None)
2191 int hue = 0, port_value, port_min, port_max;
2193 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2194 (!strcasecmp(name, "brightness")))
2195 port_value = value;
2196 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2197 (!strcasecmp(name, "contrast")))
2198 port_value = value;
2199 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2200 (!strcasecmp(name, "saturation")))
2201 port_value = value;
2202 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2203 (!strcasecmp(name, "hue")))
2205 port_value = value;
2206 hue = 1;
2207 } else
2208 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2209 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2210 (!strcasecmp(name, "red_intensity")))
2211 port_value = value;
2212 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2213 && (!strcasecmp(name, "green_intensity")))
2214 port_value = value;
2215 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2216 && (!strcasecmp(name, "blue_intensity")))
2217 port_value = value;
2218 else
2219 continue;
2221 port_min = attributes[i].min_value;
2222 port_max = attributes[i].max_value;
2224 /* nvidia hue workaround */
2225 if (hue && port_min == 0 && port_max == 360)
2227 port_value =
2228 (port_value >=
2229 0) ? (port_value - 100) : (port_value + 100);
2231 // -100 -> min
2232 // 0 -> (max+min)/2
2233 // +100 -> max
2234 port_value =
2235 (port_value + 100) * (port_max - port_min) / 200 +
2236 port_min;
2237 XvSetPortAttribute(mDisplay, xv_port, xv_atom, port_value);
2238 return (VO_TRUE);
2241 return (VO_FALSE);
2244 int vo_xv_get_eq(uint32_t xv_port, char *name, int *value)
2247 XvAttribute *attributes;
2248 int i, howmany, xv_atom;
2250 /* get available attributes */
2251 attributes = XvQueryPortAttributes(mDisplay, xv_port, &howmany);
2252 for (i = 0; i < howmany && attributes; i++)
2253 if (attributes[i].flags & XvGettable)
2255 xv_atom = XInternAtom(mDisplay, attributes[i].name, True);
2256 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2257 then trigger it if it's ok so that the other values are at default upon query */
2258 if (xv_atom != None)
2260 int val, port_value = 0, port_min, port_max;
2262 XvGetPortAttribute(mDisplay, xv_port, xv_atom,
2263 &port_value);
2265 port_min = attributes[i].min_value;
2266 port_max = attributes[i].max_value;
2267 val =
2268 (port_value - port_min) * 200 / (port_max - port_min) -
2269 100;
2271 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2272 (!strcasecmp(name, "brightness")))
2273 *value = val;
2274 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2275 (!strcasecmp(name, "contrast")))
2276 *value = val;
2277 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2278 (!strcasecmp(name, "saturation")))
2279 *value = val;
2280 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2281 (!strcasecmp(name, "hue")))
2283 /* nasty nvidia detect */
2284 if (port_min == 0 && port_max == 360)
2285 *value = (val >= 0) ? (val - 100) : (val + 100);
2286 else
2287 *value = val;
2288 } else
2289 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2290 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2291 (!strcasecmp(name, "red_intensity")))
2292 *value = val;
2293 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2294 && (!strcasecmp(name, "green_intensity")))
2295 *value = val;
2296 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2297 && (!strcasecmp(name, "blue_intensity")))
2298 *value = val;
2299 else
2300 continue;
2302 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2303 name, *value);
2304 return (VO_TRUE);
2307 return (VO_FALSE);
2310 /** \brief contains flags changing the execution of the colorkeying code */
2311 xv_ck_info_t xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR };
2312 unsigned long xv_colorkey; ///< The color used for manual colorkeying.
2313 unsigned int xv_port; ///< The selected Xv port.
2316 * \brief Interns the requested atom if it is available.
2318 * \param atom_name String containing the name of the requested atom.
2320 * \return Returns the atom if available, else None is returned.
2323 static Atom xv_intern_atom_if_exists( char const * atom_name )
2325 XvAttribute * attributes;
2326 int attrib_count,i;
2327 Atom xv_atom = None;
2329 attributes = XvQueryPortAttributes( mDisplay, xv_port, &attrib_count );
2330 if( attributes!=NULL )
2332 for ( i = 0; i < attrib_count; ++i )
2334 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2336 xv_atom = XInternAtom( mDisplay, atom_name, False );
2337 break; // found what we want, break out
2340 XFree( attributes );
2343 return xv_atom;
2347 * \brief Try to enable vsync for xv.
2348 * \return Returns -1 if not available, 0 on failure and 1 on success.
2350 int vo_xv_enable_vsync(void)
2352 Atom xv_atom = xv_intern_atom_if_exists("XV_SYNC_TO_VBLANK");
2353 if (xv_atom == None)
2354 return -1;
2355 return XvSetPortAttribute(mDisplay, xv_port, xv_atom, 1) == Success;
2359 * \brief Get maximum supported source image dimensions.
2361 * This function does not set the variables pointed to by
2362 * width and height if the information could not be retrieved,
2363 * so the caller is reponsible for properly initializing them.
2365 * \param width [out] The maximum width gets stored here.
2366 * \param height [out] The maximum height gets stored here.
2369 void vo_xv_get_max_img_dim( uint32_t * width, uint32_t * height )
2371 XvEncodingInfo * encodings;
2372 //unsigned long num_encodings, idx; to int or too long?!
2373 unsigned int num_encodings, idx;
2375 XvQueryEncodings( mDisplay, xv_port, &num_encodings, &encodings);
2377 if ( encodings )
2379 for ( idx = 0; idx < num_encodings; ++idx )
2381 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2383 *width = encodings[idx].width;
2384 *height = encodings[idx].height;
2385 break;
2390 mp_msg( MSGT_VO, MSGL_V,
2391 "[xv common] Maximum source image dimensions: %ux%u\n",
2392 *width, *height );
2394 XvFreeEncodingInfo( encodings );
2398 * \brief Print information about the colorkey method and source.
2400 * \param ck_handling Integer value containing the information about
2401 * colorkey handling (see x11_common.h).
2403 * Outputs the content of |ck_handling| as a readable message.
2406 void vo_xv_print_ck_info(void)
2408 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2410 switch ( xv_ck_info.method )
2412 case CK_METHOD_NONE:
2413 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2414 case CK_METHOD_AUTOPAINT:
2415 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2416 case CK_METHOD_MANUALFILL:
2417 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2418 case CK_METHOD_BACKGROUND:
2419 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2422 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2424 switch ( xv_ck_info.source )
2426 case CK_SRC_CUR:
2427 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2428 xv_colorkey );
2429 break;
2430 case CK_SRC_USE:
2431 if ( xv_ck_info.method == CK_METHOD_AUTOPAINT )
2433 mp_msg( MSGT_VO, MSGL_V,
2434 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2435 xv_colorkey );
2437 else
2439 mp_msg( MSGT_VO, MSGL_V,
2440 "Using colorkey from MPlayer (0x%06lx)."
2441 " Use -colorkey to change.\n",
2442 xv_colorkey );
2444 break;
2445 case CK_SRC_SET:
2446 mp_msg( MSGT_VO, MSGL_V,
2447 "Setting and using colorkey from MPlayer (0x%06lx)."
2448 " Use -colorkey to change.\n",
2449 xv_colorkey );
2450 break;
2454 * \brief Init colorkey depending on the settings in xv_ck_info.
2456 * \return Returns 0 on failure and 1 on success.
2458 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2459 * flags in xv_ck_info.
2461 * Possiblilities:
2462 * * Methods
2463 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2464 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2465 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2466 * * Sources
2467 * - use currently set colorkey ( CK_SRC_CUR )
2468 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2469 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2471 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2472 * we don't draw anything as this means it was forced to off.
2474 int vo_xv_init_colorkey(void)
2476 Atom xv_atom;
2477 int rez;
2479 /* check if colorkeying is needed */
2480 xv_atom = xv_intern_atom_if_exists( "XV_COLORKEY" );
2482 /* if we have to deal with colorkeying ... */
2483 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2485 /* check if we should use the colorkey specified in vo_colorkey */
2486 if ( xv_ck_info.source != CK_SRC_CUR )
2488 xv_colorkey = vo_colorkey;
2490 /* check if we have to set the colorkey too */
2491 if ( xv_ck_info.source == CK_SRC_SET )
2493 xv_atom = XInternAtom(mDisplay, "XV_COLORKEY",False);
2495 rez = XvSetPortAttribute( mDisplay, xv_port, xv_atom, vo_colorkey );
2496 if ( rez != Success )
2498 mp_msg( MSGT_VO, MSGL_FATAL,
2499 "[xv common] Couldn't set colorkey!\n" );
2500 return 0; // error setting colorkey
2504 else
2506 int colorkey_ret;
2508 rez=XvGetPortAttribute(mDisplay,xv_port, xv_atom, &colorkey_ret);
2509 if ( rez == Success )
2511 xv_colorkey = colorkey_ret;
2513 else
2515 mp_msg( MSGT_VO, MSGL_FATAL,
2516 "[xv common] Couldn't get colorkey!"
2517 "Maybe the selected Xv port has no overlay.\n" );
2518 return 0; // error getting colorkey
2522 xv_atom = xv_intern_atom_if_exists( "XV_AUTOPAINT_COLORKEY" );
2524 /* should we draw the colorkey ourselves or activate autopainting? */
2525 if ( xv_ck_info.method == CK_METHOD_AUTOPAINT )
2527 rez = !Success; // reset rez to something different than Success
2529 if ( xv_atom != None ) // autopaint is supported
2531 rez = XvSetPortAttribute( mDisplay, xv_port, xv_atom, 1 );
2534 if ( rez != Success )
2536 // fallback to manual colorkey drawing
2537 xv_ck_info.method = CK_METHOD_MANUALFILL;
2540 else // disable colorkey autopainting if supported
2542 if ( xv_atom != None ) // we have autopaint attribute
2544 XvSetPortAttribute( mDisplay, xv_port, xv_atom, 0 );
2548 else // do no colorkey drawing at all
2550 xv_ck_info.method = CK_METHOD_NONE;
2551 } /* end: should we draw colorkey */
2553 /* output information about the current colorkey settings */
2554 vo_xv_print_ck_info();
2556 return 1; // success
2560 * \brief Draw the colorkey on the video window.
2562 * Draws the colorkey depending on the set method ( colorkey_handling ).
2564 * Also draws the black bars ( when the video doesn't fit the display in
2565 * fullscreen ) separately, so they don't overlap with the video area.
2566 * It doesn't call XFlush.
2569 void vo_xv_draw_colorkey( int32_t x, int32_t y,
2570 int32_t w, int32_t h )
2572 if( xv_ck_info.method == CK_METHOD_MANUALFILL ||
2573 xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2575 XSetForeground( mDisplay, vo_gc, xv_colorkey );
2576 XFillRectangle( mDisplay, vo_window, vo_gc,
2577 x, y,
2578 w, h );
2581 /* draw black bars if needed */
2582 /* TODO! move this to vo_x11_clearwindow_part() */
2583 if ( vo_fs )
2585 XSetForeground( mDisplay, vo_gc, 0 );
2586 /* making non-overlap fills, requires 8 checks instead of 4 */
2587 if ( y > 0 )
2588 XFillRectangle( mDisplay, vo_window, vo_gc,
2589 0, 0,
2590 vo_screenwidth, y);
2591 if (x > 0)
2592 XFillRectangle( mDisplay, vo_window, vo_gc,
2593 0, 0,
2594 x, vo_screenheight);
2595 if (x + w < vo_screenwidth)
2596 XFillRectangle( mDisplay, vo_window, vo_gc,
2597 x + w, 0,
2598 vo_screenwidth, vo_screenheight);
2599 if (y + h < vo_screenheight)
2600 XFillRectangle( mDisplay, vo_window, vo_gc,
2601 0, y + h,
2602 vo_screenwidth, vo_screenheight);
2606 /** \brief Tests if a valid argument for the ck suboption was given. */
2607 int xv_test_ck( void * arg )
2609 strarg_t * strarg = (strarg_t *)arg;
2611 if ( strargcmp( strarg, "use" ) == 0 ||
2612 strargcmp( strarg, "set" ) == 0 ||
2613 strargcmp( strarg, "cur" ) == 0 )
2615 return 1;
2618 return 0;
2620 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2621 int xv_test_ckm( void * arg )
2623 strarg_t * strarg = (strarg_t *)arg;
2625 if ( strargcmp( strarg, "bg" ) == 0 ||
2626 strargcmp( strarg, "man" ) == 0 ||
2627 strargcmp( strarg, "auto" ) == 0 )
2629 return 1;
2632 return 0;
2636 * \brief Modify the colorkey_handling var according to str
2638 * Checks if a valid pointer ( not NULL ) to the string
2639 * was given. And in that case modifies the colorkey_handling
2640 * var to reflect the requested behaviour.
2641 * If nothing happens the content of colorkey_handling stays
2642 * the same.
2644 * \param str Pointer to the string or NULL
2647 void xv_setup_colorkeyhandling( char const * ck_method_str,
2648 char const * ck_str )
2650 /* check if a valid pointer to the string was passed */
2651 if ( ck_str )
2653 if ( strncmp( ck_str, "use", 3 ) == 0 )
2655 xv_ck_info.source = CK_SRC_USE;
2657 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2659 xv_ck_info.source = CK_SRC_SET;
2662 /* check if a valid pointer to the string was passed */
2663 if ( ck_method_str )
2665 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2667 xv_ck_info.method = CK_METHOD_BACKGROUND;
2669 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2671 xv_ck_info.method = CK_METHOD_MANUALFILL;
2673 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2675 xv_ck_info.method = CK_METHOD_AUTOPAINT;
2680 #endif