typo fixes
[mplayer/greg.git] / libvo / x11_common.c
blob9faeb741b8e3815b1743efabd5606c3e22af7f6b
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 "x11_common.h"
11 #ifdef X11_FULLSCREEN
13 #include <string.h>
14 #include <unistd.h>
15 #include <sys/mman.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 int fs_layer = WIN_LAYER_ABOVE_DOCK;
67 static int orig_layer = 0;
68 static int old_gravity = NorthWestGravity;
70 int stop_xscreensaver = 0;
72 static int dpms_disabled = 0;
73 static int timeout_save = 0;
74 static int kdescreensaver_was_running = 0;
76 char *mDisplayName = NULL;
77 Display *mDisplay = NULL;
78 Window mRootWin;
79 int mScreen;
80 int mLocalDisplay;
82 /* output window id */
83 int vo_mouse_autohide = 0;
84 int vo_wm_type = 0;
85 int vo_fs_type = 0; // needs to be accessible for GUI X11 code
86 static int vo_fs_flip = 0;
87 char **vo_fstype_list;
89 /* if equal to 1 means that WM is a metacity (broken as hell) */
90 int metacity_hack = 0;
92 static Atom XA_NET_SUPPORTED;
93 static Atom XA_NET_WM_STATE;
94 static Atom XA_NET_WM_STATE_FULLSCREEN;
95 static Atom XA_NET_WM_STATE_ABOVE;
96 static Atom XA_NET_WM_STATE_STAYS_ON_TOP;
97 static Atom XA_NET_WM_STATE_BELOW;
98 static Atom XA_NET_WM_PID;
99 static Atom XA_WIN_PROTOCOLS;
100 static Atom XA_WIN_LAYER;
101 static Atom XA_WIN_HINTS;
102 static Atom XA_BLACKBOX_PID;
103 static Atom XAWM_PROTOCOLS;
104 static Atom XAWM_DELETE_WINDOW;
106 #define XA_INIT(x) XA##x = XInternAtom(mDisplay, #x, False)
108 static int vo_old_x = 0;
109 static int vo_old_y = 0;
110 static int vo_old_width = 0;
111 static int vo_old_height = 0;
113 #ifdef HAVE_XF86VM
114 XF86VidModeModeInfo **vidmodes = NULL;
115 XF86VidModeModeLine modeline;
116 #endif
118 static int vo_x11_get_fs_type(int supported);
122 * Sends the EWMH fullscreen state event.
124 * action: could be on of _NET_WM_STATE_REMOVE -- remove state
125 * _NET_WM_STATE_ADD -- add state
126 * _NET_WM_STATE_TOGGLE -- toggle
128 void vo_x11_ewmh_fullscreen(int action)
130 assert(action == _NET_WM_STATE_REMOVE ||
131 action == _NET_WM_STATE_ADD || action == _NET_WM_STATE_TOGGLE);
133 if (vo_fs_type & vo_wm_FULLSCREEN)
135 XEvent xev;
137 /* init X event structure for _NET_WM_FULLSCREEN client msg */
138 xev.xclient.type = ClientMessage;
139 xev.xclient.serial = 0;
140 xev.xclient.send_event = True;
141 xev.xclient.message_type = XInternAtom(mDisplay,
142 "_NET_WM_STATE", False);
143 xev.xclient.window = vo_window;
144 xev.xclient.format = 32;
145 xev.xclient.data.l[0] = action;
146 xev.xclient.data.l[1] = XInternAtom(mDisplay,
147 "_NET_WM_STATE_FULLSCREEN",
148 False);
149 xev.xclient.data.l[2] = 0;
150 xev.xclient.data.l[3] = 0;
151 xev.xclient.data.l[4] = 0;
153 /* finally send that damn thing */
154 if (!XSendEvent(mDisplay, DefaultRootWindow(mDisplay), False,
155 SubstructureRedirectMask | SubstructureNotifyMask,
156 &xev))
158 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_EwmhFullscreenStateFailed);
163 void vo_hidecursor(Display * disp, Window win)
165 Cursor no_ptr;
166 Pixmap bm_no;
167 XColor black, dummy;
168 Colormap colormap;
169 static char bm_no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
171 if (WinID == 0)
172 return; // do not hide, if we're playing at rootwin
174 colormap = DefaultColormap(disp, DefaultScreen(disp));
175 if ( !XAllocNamedColor(disp, colormap, "black", &black, &dummy) )
177 return; // color alloc failed, give up
179 bm_no = XCreateBitmapFromData(disp, win, bm_no_data, 8, 8);
180 no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
181 XDefineCursor(disp, win, no_ptr);
182 XFreeCursor(disp, no_ptr);
183 if (bm_no != None)
184 XFreePixmap(disp, bm_no);
185 XFreeColors(disp,colormap,&black.pixel,1,0);
188 void vo_showcursor(Display * disp, Window win)
190 if (WinID == 0)
191 return;
192 XDefineCursor(disp, win, 0);
195 static int x11_errorhandler(Display * display, XErrorEvent * event)
197 #define MSGLEN 60
198 char msg[MSGLEN];
200 XGetErrorText(display, event->error_code, (char *) &msg, MSGLEN);
202 mp_msg(MSGT_VO, MSGL_ERR, "X11 error: %s\n", msg);
204 mp_msg(MSGT_VO, MSGL_V,
205 "Type: %x, display: %p, resourceid: %lx, serial: %lx\n",
206 event->type, event->display, event->resourceid, event->serial);
207 mp_msg(MSGT_VO, MSGL_V,
208 "Error code: %x, request code: %x, minor code: %x\n",
209 event->error_code, event->request_code, event->minor_code);
211 abort();
212 //exit_player("X11 error");
213 #undef MSGLEN
216 void fstype_help(void)
218 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_AvailableFsType);
219 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FULL_SCREEN_TYPES\n");
221 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "none",
222 "don't set fullscreen window layer");
223 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer",
224 "use _WIN_LAYER hint with default layer");
225 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer=<0..15>",
226 "use _WIN_LAYER hint with a given layer number");
227 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "netwm",
228 "force NETWM style");
229 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "above",
230 "use _NETWM_STATE_ABOVE hint if available");
231 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "below",
232 "use _NETWM_STATE_BELOW hint if available");
233 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "fullscreen",
234 "use _NETWM_STATE_FULLSCREEN hint if availale");
235 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "stays_on_top",
236 "use _NETWM_STATE_STAYS_ON_TOP hint if available");
237 mp_msg(MSGT_VO, MSGL_INFO,
238 "You can also negate the settings with simply putting '-' in the beginning");
239 mp_msg(MSGT_VO, MSGL_INFO, "\n");
242 static void fstype_dump(int fstype)
244 if (fstype)
246 mp_msg(MSGT_VO, MSGL_V, "[x11] Current fstype setting honours");
247 if (fstype & vo_wm_LAYER)
248 mp_msg(MSGT_VO, MSGL_V, " LAYER");
249 if (fstype & vo_wm_FULLSCREEN)
250 mp_msg(MSGT_VO, MSGL_V, " FULLSCREEN");
251 if (fstype & vo_wm_STAYS_ON_TOP)
252 mp_msg(MSGT_VO, MSGL_V, " STAYS_ON_TOP");
253 if (fstype & vo_wm_ABOVE)
254 mp_msg(MSGT_VO, MSGL_V, " ABOVE");
255 if (fstype & vo_wm_BELOW)
256 mp_msg(MSGT_VO, MSGL_V, " BELOW");
257 mp_msg(MSGT_VO, MSGL_V, " X atoms\n");
258 } else
259 mp_msg(MSGT_VO, MSGL_V,
260 "[x11] Current fstype setting doesn't honour any X atoms\n");
263 static int net_wm_support_state_test(Atom atom)
265 #define NET_WM_STATE_TEST(x) { if (atom == XA_NET_WM_STATE_##x) { mp_msg( MSGT_VO,MSGL_V, "[x11] Detected wm supports " #x " state.\n" ); return vo_wm_##x; } }
267 NET_WM_STATE_TEST(FULLSCREEN);
268 NET_WM_STATE_TEST(ABOVE);
269 NET_WM_STATE_TEST(STAYS_ON_TOP);
270 NET_WM_STATE_TEST(BELOW);
271 return 0;
274 static int x11_get_property(Atom type, Atom ** args, unsigned long *nitems)
276 int format;
277 unsigned long bytesafter;
279 return (Success ==
280 XGetWindowProperty(mDisplay, mRootWin, type, 0, 16384, False,
281 AnyPropertyType, &type, &format, nitems,
282 &bytesafter, (unsigned char **) args)
283 && *nitems > 0);
286 static int vo_wm_detect(void)
288 int i;
289 int wm = 0;
290 unsigned long nitems;
291 Atom *args = NULL;
293 if (WinID >= 0)
294 return 0;
296 // -- supports layers
297 if (x11_get_property(XA_WIN_PROTOCOLS, &args, &nitems))
299 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports layers.\n");
300 for (i = 0; i < nitems; i++)
302 if (args[i] == XA_WIN_LAYER)
304 wm |= vo_wm_LAYER;
305 metacity_hack |= 1;
306 } else
307 // metacity is the only manager I know which reports support only for _WIN_LAYER
308 // hint in _WIN_PROTOCOLS (what's more support for it is broken)
309 metacity_hack |= 2;
311 XFree(args);
312 if (wm && (metacity_hack == 1))
314 // metacity reports that it supports layers, but it is not really truth :-)
315 wm ^= vo_wm_LAYER;
316 mp_msg(MSGT_VO, MSGL_V,
317 "[x11] Using workaround for Metacity bugs.\n");
320 // --- netwm
321 if (x11_get_property(XA_NET_SUPPORTED, &args, &nitems))
323 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports NetWM.\n");
324 for (i = 0; i < nitems; i++)
325 wm |= net_wm_support_state_test(args[i]);
326 XFree(args);
327 #if 0
328 // ugly hack for broken OpenBox _NET_WM_STATE_FULLSCREEN support
329 // (in their implementation it only changes internal state of window, nothing more!!!)
330 if (wm & vo_wm_FULLSCREEN)
332 if (x11_get_property(XA_BLACKBOX_PID, &args, &nitems))
334 mp_msg(MSGT_VO, MSGL_V,
335 "[x11] Detected wm is a broken OpenBox.\n");
336 wm ^= vo_wm_FULLSCREEN;
338 XFree(args);
340 #endif
343 if (wm == 0)
344 mp_msg(MSGT_VO, MSGL_V, "[x11] Unknown wm type...\n");
345 return wm;
348 static void init_atoms(void)
350 XA_INIT(_NET_SUPPORTED);
351 XA_INIT(_NET_WM_STATE);
352 XA_INIT(_NET_WM_STATE_FULLSCREEN);
353 XA_INIT(_NET_WM_STATE_ABOVE);
354 XA_INIT(_NET_WM_STATE_STAYS_ON_TOP);
355 XA_INIT(_NET_WM_STATE_BELOW);
356 XA_INIT(_NET_WM_PID);
357 XA_INIT(_WIN_PROTOCOLS);
358 XA_INIT(_WIN_LAYER);
359 XA_INIT(_WIN_HINTS);
360 XA_INIT(_BLACKBOX_PID);
361 XA_INIT(WM_PROTOCOLS);
362 XA_INIT(WM_DELETE_WINDOW);
365 void update_xinerama_info(void) {
366 int screen = xinerama_screen;
367 xinerama_x = xinerama_y = 0;
368 #ifdef HAVE_XINERAMA
369 if (screen >= -1 && XineramaIsActive(mDisplay))
371 XineramaScreenInfo *screens;
372 int num_screens;
374 screens = XineramaQueryScreens(mDisplay, &num_screens);
375 if (screen >= num_screens)
376 screen = num_screens - 1;
377 if (screen == -1) {
378 int x = vo_dx + vo_dwidth / 2;
379 int y = vo_dy + vo_dheight / 2;
380 for (screen = num_screens - 1; screen > 0; screen--) {
381 int left = screens[screen].x_org;
382 int right = left + screens[screen].width;
383 int top = screens[screen].y_org;
384 int bottom = top + screens[screen].height;
385 if (left <= x && x <= right && top <= y && y <= bottom)
386 break;
389 if (screen < 0)
390 screen = 0;
391 vo_screenwidth = screens[screen].width;
392 vo_screenheight = screens[screen].height;
393 xinerama_x = screens[screen].x_org;
394 xinerama_y = screens[screen].y_org;
396 XFree(screens);
398 #endif
399 aspect_save_screenres(vo_screenwidth, vo_screenheight);
402 int vo_init(void)
404 // int mScreen;
405 int depth, bpp;
406 unsigned int mask;
408 // char * DisplayName = ":0.0";
409 // Display * mDisplay;
410 XImage *mXImage = NULL;
412 // Window mRootWin;
413 XWindowAttributes attribs;
414 char *dispName;
416 if (vo_rootwin)
417 WinID = 0; // use root win
419 if (vo_depthonscreen)
421 saver_off(mDisplay);
422 return 1; // already called
425 XSetErrorHandler(x11_errorhandler);
427 #if 0
428 if (!mDisplayName)
429 if (!(mDisplayName = getenv("DISPLAY")))
430 mDisplayName = strdup(":0.0");
431 #else
432 dispName = XDisplayName(mDisplayName);
433 #endif
435 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
437 mDisplay = XOpenDisplay(dispName);
438 if (!mDisplay)
440 mp_msg(MSGT_VO, MSGL_ERR,
441 "vo: couldn't open the X11 display (%s)!\n", dispName);
442 return 0;
444 mScreen = DefaultScreen(mDisplay); // Screen ID.
445 mRootWin = RootWindow(mDisplay, mScreen); // Root window ID.
447 init_atoms();
449 #ifdef HAVE_XF86VM
451 int clock;
453 XF86VidModeGetModeLine(mDisplay, mScreen, &clock, &modeline);
454 if (!vo_screenwidth)
455 vo_screenwidth = modeline.hdisplay;
456 if (!vo_screenheight)
457 vo_screenheight = modeline.vdisplay;
459 #endif
461 if (!vo_screenwidth)
462 vo_screenwidth = DisplayWidth(mDisplay, mScreen);
463 if (!vo_screenheight)
464 vo_screenheight = DisplayHeight(mDisplay, mScreen);
466 // get color depth (from root window, or the best visual):
467 XGetWindowAttributes(mDisplay, mRootWin, &attribs);
468 depth = attribs.depth;
470 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
472 Visual *visual;
474 depth = vo_find_depth_from_visuals(mDisplay, mScreen, &visual);
475 if (depth != -1)
476 mXImage = XCreateImage(mDisplay, visual, depth, ZPixmap,
477 0, NULL, 1, 1, 8, 1);
478 } else
479 mXImage =
480 XGetImage(mDisplay, mRootWin, 0, 0, 1, 1, AllPlanes, ZPixmap);
482 vo_depthonscreen = depth; // display depth on screen
484 // get bits/pixel from XImage structure:
485 if (mXImage == NULL)
487 mask = 0;
488 } else
491 * for the depth==24 case, the XImage structures might use
492 * 24 or 32 bits of data per pixel. The global variable
493 * vo_depthonscreen stores the amount of data per pixel in the
494 * XImage structure!
496 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
498 bpp = mXImage->bits_per_pixel;
499 if ((vo_depthonscreen + 7) / 8 != (bpp + 7) / 8)
500 vo_depthonscreen = bpp; // by A'rpi
501 mask =
502 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
503 mp_msg(MSGT_VO, MSGL_V,
504 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
505 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
506 XDestroyImage(mXImage);
508 if (((vo_depthonscreen + 7) / 8) == 2)
510 if (mask == 0x7FFF)
511 vo_depthonscreen = 15;
512 else if (mask == 0xFFFF)
513 vo_depthonscreen = 16;
515 // XCloseDisplay( mDisplay );
516 /* slightly improved local display detection AST */
517 if (strncmp(dispName, "unix:", 5) == 0)
518 dispName += 4;
519 else if (strncmp(dispName, "localhost:", 10) == 0)
520 dispName += 9;
521 if (*dispName == ':' && atoi(dispName + 1) < 10)
522 mLocalDisplay = 1;
523 else
524 mLocalDisplay = 0;
525 mp_msg(MSGT_VO, MSGL_V,
526 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
527 vo_screenwidth, vo_screenheight, depth, vo_depthonscreen,
528 dispName, mLocalDisplay ? "local" : "remote");
530 vo_wm_type = vo_wm_detect();
532 vo_fs_type = vo_x11_get_fs_type(vo_wm_type);
534 fstype_dump(vo_fs_type);
536 saver_off(mDisplay);
537 return 1;
540 void vo_uninit(void)
542 if (!mDisplay)
544 mp_msg(MSGT_VO, MSGL_V,
545 "vo: x11 uninit called but X11 not inited..\n");
546 return;
548 // if( !vo_depthonscreen ) return;
549 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
550 XSetErrorHandler(NULL);
551 XCloseDisplay(mDisplay);
552 vo_depthonscreen = 0;
553 mDisplay = NULL;
556 #include "osdep/keycodes.h"
557 #include "wskeys.h"
559 extern void mplayer_put_key(int code);
561 #ifdef XF86XK_AudioPause
562 void vo_x11_putkey_ext(int keysym)
564 switch (keysym)
566 case XF86XK_AudioPause:
567 mplayer_put_key(KEY_PAUSE);
568 break;
569 case XF86XK_AudioStop:
570 mplayer_put_key(KEY_STOP);
571 break;
572 case XF86XK_AudioPrev:
573 mplayer_put_key(KEY_PREV);
574 break;
575 case XF86XK_AudioNext:
576 mplayer_put_key(KEY_NEXT);
577 break;
578 default:
579 break;
582 #endif
584 void vo_x11_putkey(int key)
586 switch (key)
588 case wsLeft:
589 mplayer_put_key(KEY_LEFT);
590 break;
591 case wsRight:
592 mplayer_put_key(KEY_RIGHT);
593 break;
594 case wsUp:
595 mplayer_put_key(KEY_UP);
596 break;
597 case wsDown:
598 mplayer_put_key(KEY_DOWN);
599 break;
600 case wsSpace:
601 mplayer_put_key(' ');
602 break;
603 case wsEscape:
604 mplayer_put_key(KEY_ESC);
605 break;
606 case wsTab:
607 mplayer_put_key(KEY_TAB);
608 break;
609 case wsEnter:
610 mplayer_put_key(KEY_ENTER);
611 break;
612 case wsBackSpace:
613 mplayer_put_key(KEY_BS);
614 break;
615 case wsDelete:
616 mplayer_put_key(KEY_DELETE);
617 break;
618 case wsInsert:
619 mplayer_put_key(KEY_INSERT);
620 break;
621 case wsHome:
622 mplayer_put_key(KEY_HOME);
623 break;
624 case wsEnd:
625 mplayer_put_key(KEY_END);
626 break;
627 case wsPageUp:
628 mplayer_put_key(KEY_PAGE_UP);
629 break;
630 case wsPageDown:
631 mplayer_put_key(KEY_PAGE_DOWN);
632 break;
633 case wsF1:
634 mplayer_put_key(KEY_F + 1);
635 break;
636 case wsF2:
637 mplayer_put_key(KEY_F + 2);
638 break;
639 case wsF3:
640 mplayer_put_key(KEY_F + 3);
641 break;
642 case wsF4:
643 mplayer_put_key(KEY_F + 4);
644 break;
645 case wsF5:
646 mplayer_put_key(KEY_F + 5);
647 break;
648 case wsF6:
649 mplayer_put_key(KEY_F + 6);
650 break;
651 case wsF7:
652 mplayer_put_key(KEY_F + 7);
653 break;
654 case wsF8:
655 mplayer_put_key(KEY_F + 8);
656 break;
657 case wsF9:
658 mplayer_put_key(KEY_F + 9);
659 break;
660 case wsF10:
661 mplayer_put_key(KEY_F + 10);
662 break;
663 case wsF11:
664 mplayer_put_key(KEY_F + 11);
665 break;
666 case wsF12:
667 mplayer_put_key(KEY_F + 12);
668 break;
669 case wsMinus:
670 case wsGrayMinus:
671 mplayer_put_key('-');
672 break;
673 case wsPlus:
674 case wsGrayPlus:
675 mplayer_put_key('+');
676 break;
677 case wsGrayMul:
678 case wsMul:
679 mplayer_put_key('*');
680 break;
681 case wsGrayDiv:
682 case wsDiv:
683 mplayer_put_key('/');
684 break;
685 case wsLess:
686 mplayer_put_key('<');
687 break;
688 case wsMore:
689 mplayer_put_key('>');
690 break;
691 case wsGray0:
692 mplayer_put_key(KEY_KP0);
693 break;
694 case wsGrayEnd:
695 case wsGray1:
696 mplayer_put_key(KEY_KP1);
697 break;
698 case wsGrayDown:
699 case wsGray2:
700 mplayer_put_key(KEY_KP2);
701 break;
702 case wsGrayPgDn:
703 case wsGray3:
704 mplayer_put_key(KEY_KP3);
705 break;
706 case wsGrayLeft:
707 case wsGray4:
708 mplayer_put_key(KEY_KP4);
709 break;
710 case wsGray5Dup:
711 case wsGray5:
712 mplayer_put_key(KEY_KP5);
713 break;
714 case wsGrayRight:
715 case wsGray6:
716 mplayer_put_key(KEY_KP6);
717 break;
718 case wsGrayHome:
719 case wsGray7:
720 mplayer_put_key(KEY_KP7);
721 break;
722 case wsGrayUp:
723 case wsGray8:
724 mplayer_put_key(KEY_KP8);
725 break;
726 case wsGrayPgUp:
727 case wsGray9:
728 mplayer_put_key(KEY_KP9);
729 break;
730 case wsGrayDecimal:
731 mplayer_put_key(KEY_KPDEC);
732 break;
733 case wsGrayInsert:
734 mplayer_put_key(KEY_KPINS);
735 break;
736 case wsGrayDelete:
737 mplayer_put_key(KEY_KPDEL);
738 break;
739 case wsGrayEnter:
740 mplayer_put_key(KEY_KPENTER);
741 break;
742 case wsGrave:
743 mplayer_put_key('`');
744 break;
745 case wsTilde:
746 mplayer_put_key('~');
747 break;
748 case wsExclSign:
749 mplayer_put_key('!');
750 break;
751 case wsAt:
752 mplayer_put_key('@');
753 break;
754 case wsHash:
755 mplayer_put_key('#');
756 break;
757 case wsDollar:
758 mplayer_put_key('$');
759 break;
760 case wsPercent:
761 mplayer_put_key('%');
762 break;
763 case wsCircumflex:
764 mplayer_put_key('^');
765 break;
766 case wsAmpersand:
767 mplayer_put_key('&');
768 break;
769 case wsobracket:
770 mplayer_put_key('(');
771 break;
772 case wscbracket:
773 mplayer_put_key(')');
774 break;
775 case wsUnder:
776 mplayer_put_key('_');
777 break;
778 case wsocbracket:
779 mplayer_put_key('{');
780 break;
781 case wsccbracket:
782 mplayer_put_key('}');
783 break;
784 case wsColon:
785 mplayer_put_key(':');
786 break;
787 case wsSemicolon:
788 mplayer_put_key(';');
789 break;
790 case wsDblQuote:
791 mplayer_put_key('\"');
792 break;
793 case wsAcute:
794 mplayer_put_key('\'');
795 break;
796 case wsComma:
797 mplayer_put_key(',');
798 break;
799 case wsPoint:
800 mplayer_put_key('.');
801 break;
802 case wsQuestSign:
803 mplayer_put_key('?');
804 break;
805 case wsBSlash:
806 mplayer_put_key('\\');
807 break;
808 case wsPipe:
809 mplayer_put_key('|');
810 break;
811 case wsEqual:
812 mplayer_put_key('=');
813 break;
814 case wsosbrackets:
815 mplayer_put_key('[');
816 break;
817 case wscsbrackets:
818 mplayer_put_key(']');
819 break;
822 default:
823 if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') ||
824 (key >= '0' && key <= '9'))
825 mplayer_put_key(key);
831 // ----- Motif header: -------
833 #define MWM_HINTS_FUNCTIONS (1L << 0)
834 #define MWM_HINTS_DECORATIONS (1L << 1)
835 #define MWM_HINTS_INPUT_MODE (1L << 2)
836 #define MWM_HINTS_STATUS (1L << 3)
838 #define MWM_FUNC_ALL (1L << 0)
839 #define MWM_FUNC_RESIZE (1L << 1)
840 #define MWM_FUNC_MOVE (1L << 2)
841 #define MWM_FUNC_MINIMIZE (1L << 3)
842 #define MWM_FUNC_MAXIMIZE (1L << 4)
843 #define MWM_FUNC_CLOSE (1L << 5)
845 #define MWM_DECOR_ALL (1L << 0)
846 #define MWM_DECOR_BORDER (1L << 1)
847 #define MWM_DECOR_RESIZEH (1L << 2)
848 #define MWM_DECOR_TITLE (1L << 3)
849 #define MWM_DECOR_MENU (1L << 4)
850 #define MWM_DECOR_MINIMIZE (1L << 5)
851 #define MWM_DECOR_MAXIMIZE (1L << 6)
853 #define MWM_INPUT_MODELESS 0
854 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
855 #define MWM_INPUT_SYSTEM_MODAL 2
856 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
857 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
859 #define MWM_TEAROFF_WINDOW (1L<<0)
861 typedef struct
863 long flags;
864 long functions;
865 long decorations;
866 long input_mode;
867 long state;
868 } MotifWmHints;
870 static MotifWmHints vo_MotifWmHints;
871 static Atom vo_MotifHints = None;
873 void vo_x11_decoration(Display * vo_Display, Window w, int d)
875 static unsigned int olddecor = MWM_DECOR_ALL;
876 static unsigned int oldfuncs =
877 MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
878 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE;
879 Atom mtype;
880 int mformat;
881 unsigned long mn, mb;
883 if (!WinID)
884 return;
886 if (vo_fsmode & 8)
888 XSetTransientForHint(vo_Display, w,
889 RootWindow(vo_Display, mScreen));
892 vo_MotifHints = XInternAtom(vo_Display, "_MOTIF_WM_HINTS", 0);
893 if (vo_MotifHints != None)
895 if (!d)
897 MotifWmHints *mhints = NULL;
899 XGetWindowProperty(vo_Display, w, vo_MotifHints, 0, 20, False,
900 vo_MotifHints, &mtype, &mformat, &mn,
901 &mb, (unsigned char **) &mhints);
902 if (mhints)
904 if (mhints->flags & MWM_HINTS_DECORATIONS)
905 olddecor = mhints->decorations;
906 if (mhints->flags & MWM_HINTS_FUNCTIONS)
907 oldfuncs = mhints->functions;
908 XFree(mhints);
912 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
913 vo_MotifWmHints.flags =
914 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
915 if (d)
917 vo_MotifWmHints.functions = oldfuncs;
918 d = olddecor;
920 #if 0
921 vo_MotifWmHints.decorations =
922 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
923 #else
924 vo_MotifWmHints.decorations =
925 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
926 #endif
927 XChangeProperty(vo_Display, w, vo_MotifHints, vo_MotifHints, 32,
928 PropModeReplace,
929 (unsigned char *) &vo_MotifWmHints,
930 (vo_fsmode & 4) ? 4 : 5);
934 void vo_x11_classhint(Display * display, Window window, char *name)
936 XClassHint wmClass;
937 pid_t pid = getpid();
939 wmClass.res_name = name;
940 wmClass.res_class = "MPlayer";
941 XSetClassHint(display, window, &wmClass);
942 XChangeProperty(display, window, XA_NET_WM_PID, XA_CARDINAL, 32,
943 PropModeReplace, (unsigned char *) &pid, 1);
946 Window vo_window = None;
947 GC vo_gc = NULL;
948 GC f_gc = NULL;
949 XSizeHints vo_hint;
951 #ifdef HAVE_NEW_GUI
952 void vo_setwindow(Window w, GC g)
954 vo_window = w;
955 vo_gc = g;
957 #endif
959 void vo_x11_uninit(void)
961 saver_on(mDisplay);
962 if (vo_window != None)
963 vo_showcursor(mDisplay, vo_window);
965 if (f_gc)
967 XFreeGC(mDisplay, f_gc);
968 f_gc = NULL;
970 #ifdef HAVE_NEW_GUI
971 /* destroy window only if it's not controlled by GUI */
972 if (!use_gui)
973 #endif
975 if (vo_gc)
977 XSetBackground(mDisplay, vo_gc, 0);
978 XFreeGC(mDisplay, vo_gc);
979 vo_gc = NULL;
981 if (vo_window != None)
983 XClearWindow(mDisplay, vo_window);
984 if (WinID < 0)
986 XEvent xev;
988 XUnmapWindow(mDisplay, vo_window);
989 XDestroyWindow(mDisplay, vo_window);
992 XNextEvent(mDisplay, &xev);
994 while (xev.type != DestroyNotify
995 || xev.xdestroywindow.event != vo_window);
997 vo_window = None;
999 vo_fs = 0;
1000 vo_old_width = vo_old_height = 0;
1004 static unsigned int mouse_timer;
1005 static int mouse_waiting_hide;
1007 int vo_x11_check_events(Display * mydisplay)
1009 int ret = 0;
1010 XEvent Event;
1011 char buf[100];
1012 KeySym keySym;
1013 static XComposeStatus stat;
1015 // unsigned long vo_KeyTable[512];
1017 if ((vo_mouse_autohide) && mouse_waiting_hide &&
1018 (GetTimerMS() - mouse_timer >= 1000)) {
1019 vo_hidecursor(mydisplay, vo_window);
1020 mouse_waiting_hide = 0;
1023 while (XPending(mydisplay))
1025 XNextEvent(mydisplay, &Event);
1026 #ifdef HAVE_NEW_GUI
1027 if (use_gui)
1029 guiGetEvent(0, (char *) &Event);
1030 if (vo_window != Event.xany.window)
1031 continue;
1033 #endif
1034 // printf("\rEvent.type=%X \n",Event.type);
1035 switch (Event.type)
1037 case Expose:
1038 ret |= VO_EVENT_EXPOSE;
1039 break;
1040 case ConfigureNotify:
1041 // if (!vo_fs && (Event.xconfigure.width == vo_screenwidth || Event.xconfigure.height == vo_screenheight)) break;
1042 // if (vo_fs && Event.xconfigure.width != vo_screenwidth && Event.xconfigure.height != vo_screenheight) break;
1043 if (vo_window == None)
1044 break;
1045 vo_dwidth = Event.xconfigure.width;
1046 vo_dheight = Event.xconfigure.height;
1047 #if 0
1048 /* when resizing, x and y are zero :( */
1049 vo_dx = Event.xconfigure.x;
1050 vo_dy = Event.xconfigure.y;
1051 #else
1053 Window root;
1054 int foo;
1055 Window win;
1057 XGetGeometry(mydisplay, vo_window, &root, &foo, &foo,
1058 &foo /*width */ , &foo /*height */ , &foo,
1059 &foo);
1060 XTranslateCoordinates(mydisplay, vo_window, root, 0, 0,
1061 &vo_dx, &vo_dy, &win);
1063 #endif
1064 ret |= VO_EVENT_RESIZE;
1065 break;
1066 case KeyPress:
1068 int key;
1070 #ifdef HAVE_NEW_GUI
1071 if ( use_gui ) { break; }
1072 #endif
1074 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
1075 &stat);
1076 #ifdef XF86XK_AudioPause
1077 vo_x11_putkey_ext(keySym);
1078 #endif
1079 key =
1080 ((keySym & 0xff00) !=
1081 0 ? ((keySym & 0x00ff) + 256) : (keySym));
1082 vo_x11_putkey(key);
1083 ret |= VO_EVENT_KEYPRESS;
1085 break;
1086 case MotionNotify:
1087 if (vo_mouse_autohide)
1089 vo_showcursor(mydisplay, vo_window);
1090 mouse_waiting_hide = 1;
1091 mouse_timer = GetTimerMS();
1093 break;
1094 case ButtonPress:
1095 if (vo_mouse_autohide)
1097 vo_showcursor(mydisplay, vo_window);
1098 mouse_waiting_hide = 1;
1099 mouse_timer = GetTimerMS();
1101 // Ignore mouse whell press event
1102 if (Event.xbutton.button > 3)
1104 mplayer_put_key(MOUSE_BTN0 + Event.xbutton.button - 1);
1105 break;
1107 #ifdef HAVE_NEW_GUI
1108 // Ignor mouse button 1 - 3 under gui
1109 if (use_gui && (Event.xbutton.button >= 1)
1110 && (Event.xbutton.button <= 3))
1111 break;
1112 #endif
1113 mplayer_put_key((MOUSE_BTN0 + Event.xbutton.button -
1114 1) | MP_KEY_DOWN);
1115 break;
1116 case ButtonRelease:
1117 if (vo_mouse_autohide)
1119 vo_showcursor(mydisplay, vo_window);
1120 mouse_waiting_hide = 1;
1121 mouse_timer = GetTimerMS();
1123 #ifdef HAVE_NEW_GUI
1124 // Ignor mouse button 1 - 3 under gui
1125 if (use_gui && (Event.xbutton.button >= 1)
1126 && (Event.xbutton.button <= 3))
1127 break;
1128 #endif
1129 mplayer_put_key(MOUSE_BTN0 + Event.xbutton.button - 1);
1130 break;
1131 case PropertyNotify:
1133 char *name =
1134 XGetAtomName(mydisplay, Event.xproperty.atom);
1136 if (!name)
1137 break;
1139 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
1141 XFree(name);
1143 break;
1144 case MapNotify:
1145 vo_hint.win_gravity = old_gravity;
1146 XSetWMNormalHints(mDisplay, vo_window, &vo_hint);
1147 vo_fs_flip = 0;
1148 break;
1149 case ClientMessage:
1150 if (Event.xclient.message_type == XAWM_PROTOCOLS &&
1151 Event.xclient.data.l[0] == XAWM_DELETE_WINDOW)
1152 mplayer_put_key(KEY_CLOSE_WIN);
1153 break;
1156 return ret;
1160 * \brief sets the size and position of the non-fullscreen window.
1162 void vo_x11_nofs_sizepos(int x, int y, int width, int height)
1164 if (vo_fs) {
1165 vo_old_x = x;
1166 vo_old_y = y;
1167 vo_old_width = width;
1168 vo_old_height = height;
1170 else
1172 vo_dwidth = width;
1173 vo_dheight = height;
1174 XMoveResizeWindow(mDisplay, vo_window, x, y, width, height);
1178 void vo_x11_sizehint(int x, int y, int width, int height, int max)
1180 vo_hint.flags = PPosition | PSize | PWinGravity;
1181 if (vo_keepaspect)
1183 vo_hint.flags |= PAspect;
1184 vo_hint.min_aspect.x = width;
1185 vo_hint.min_aspect.y = height;
1186 vo_hint.max_aspect.x = width;
1187 vo_hint.max_aspect.y = height;
1190 vo_hint.x = x;
1191 vo_hint.y = y;
1192 vo_hint.width = width;
1193 vo_hint.height = height;
1194 if (max)
1196 vo_hint.max_width = width;
1197 vo_hint.max_height = height;
1198 vo_hint.flags |= PMaxSize;
1199 } else
1201 vo_hint.max_width = 0;
1202 vo_hint.max_height = 0;
1205 // set min height/width to 4 to avoid off by one errors
1206 // and because mga_vid requires a minial size of 4 pixel
1207 vo_hint.min_width = vo_hint.min_height = 4;
1208 vo_hint.flags |= PMinSize;
1210 vo_hint.win_gravity = StaticGravity;
1211 XSetWMNormalHints(mDisplay, vo_window, &vo_hint);
1214 static int vo_x11_get_gnome_layer(Display * mDisplay, Window win)
1216 Atom type;
1217 int format;
1218 unsigned long nitems;
1219 unsigned long bytesafter;
1220 unsigned short *args = NULL;
1222 if (XGetWindowProperty(mDisplay, win, XA_WIN_LAYER, 0, 16384,
1223 False, AnyPropertyType, &type, &format, &nitems,
1224 &bytesafter,
1225 (unsigned char **) &args) == Success
1226 && nitems > 0 && args)
1228 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
1229 *args);
1230 return *args;
1232 return WIN_LAYER_NORMAL;
1236 Window vo_x11_create_smooth_window(Display * mDisplay, Window mRoot,
1237 Visual * vis, int x, int y,
1238 unsigned int width, unsigned int height,
1239 int depth, Colormap col_map)
1241 unsigned long xswamask = CWBackingStore | CWBorderPixel;
1242 XSetWindowAttributes xswa;
1243 Window ret_win;
1245 if (col_map != CopyFromParent)
1247 xswa.colormap = col_map;
1248 xswamask |= CWColormap;
1250 xswa.background_pixel = 0;
1251 xswa.border_pixel = 0;
1252 xswa.backing_store = Always;
1253 xswa.bit_gravity = StaticGravity;
1255 ret_win =
1256 XCreateWindow(mDisplay, mRootWin, x, y, width, height, 0, depth,
1257 CopyFromParent, vis, xswamask, &xswa);
1258 XSetWMProtocols(mDisplay, ret_win, &XAWM_DELETE_WINDOW, 1);
1259 if (!f_gc)
1260 f_gc = XCreateGC(mDisplay, ret_win, 0, 0);
1261 XSetForeground(mDisplay, f_gc, 0);
1263 return ret_win;
1267 void vo_x11_clearwindow_part(Display * mDisplay, Window vo_window,
1268 int img_width, int img_height, int use_fs)
1270 int u_dheight, u_dwidth, left_ov, left_ov2;
1272 if (!f_gc)
1273 return;
1275 u_dheight = use_fs ? vo_screenheight : vo_dheight;
1276 u_dwidth = use_fs ? vo_screenwidth : vo_dwidth;
1277 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1278 return;
1280 left_ov = (u_dheight - img_height) / 2;
1281 left_ov2 = (u_dwidth - img_width) / 2;
1283 XFillRectangle(mDisplay, vo_window, f_gc, 0, 0, u_dwidth, left_ov);
1284 XFillRectangle(mDisplay, vo_window, f_gc, 0, u_dheight - left_ov - 1,
1285 u_dwidth, left_ov + 1);
1287 if (u_dwidth > img_width)
1289 XFillRectangle(mDisplay, vo_window, f_gc, 0, left_ov, left_ov2,
1290 img_height);
1291 XFillRectangle(mDisplay, vo_window, f_gc, u_dwidth - left_ov2 - 1,
1292 left_ov, left_ov2, img_height);
1295 XFlush(mDisplay);
1298 void vo_x11_clearwindow(Display * mDisplay, Window vo_window)
1300 if (!f_gc)
1301 return;
1302 XFillRectangle(mDisplay, vo_window, f_gc, 0, 0, vo_screenwidth,
1303 vo_screenheight);
1305 XFlush(mDisplay);
1309 void vo_x11_setlayer(Display * mDisplay, Window vo_window, int layer)
1311 if (WinID >= 0)
1312 return;
1314 if (vo_fs_type & vo_wm_LAYER)
1316 XClientMessageEvent xev;
1318 if (!orig_layer)
1319 orig_layer = vo_x11_get_gnome_layer(mDisplay, vo_window);
1321 memset(&xev, 0, sizeof(xev));
1322 xev.type = ClientMessage;
1323 xev.display = mDisplay;
1324 xev.window = vo_window;
1325 xev.message_type = XA_WIN_LAYER;
1326 xev.format = 32;
1327 xev.data.l[0] = layer ? fs_layer : orig_layer; // if not fullscreen, stay on default layer
1328 xev.data.l[1] = CurrentTime;
1329 mp_msg(MSGT_VO, MSGL_V,
1330 "[x11] Layered style stay on top (layer %ld).\n",
1331 xev.data.l[0]);
1332 XSendEvent(mDisplay, mRootWin, False, SubstructureNotifyMask,
1333 (XEvent *) & xev);
1334 } else if (vo_fs_type & vo_wm_NETWM)
1336 XClientMessageEvent xev;
1337 char *state;
1339 memset(&xev, 0, sizeof(xev));
1340 xev.type = ClientMessage;
1341 xev.message_type = XA_NET_WM_STATE;
1342 xev.display = mDisplay;
1343 xev.window = vo_window;
1344 xev.format = 32;
1345 xev.data.l[0] = layer;
1347 if (vo_fs_type & vo_wm_STAYS_ON_TOP)
1348 xev.data.l[1] = XA_NET_WM_STATE_STAYS_ON_TOP;
1349 else if (vo_fs_type & vo_wm_ABOVE)
1350 xev.data.l[1] = XA_NET_WM_STATE_ABOVE;
1351 else if (vo_fs_type & vo_wm_FULLSCREEN)
1352 xev.data.l[1] = XA_NET_WM_STATE_FULLSCREEN;
1353 else if (vo_fs_type & vo_wm_BELOW)
1354 // This is not fallback. We can safely assume that situation where
1355 // only NETWM_STATE_BELOW is supported and others not, doesn't exist.
1356 xev.data.l[1] = XA_NET_WM_STATE_BELOW;
1358 XSendEvent(mDisplay, mRootWin, False, SubstructureRedirectMask,
1359 (XEvent *) & xev);
1360 state = XGetAtomName(mDisplay, xev.data.l[1]);
1361 mp_msg(MSGT_VO, MSGL_V,
1362 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1363 layer, state);
1364 XFree(state);
1368 static int vo_x11_get_fs_type(int supported)
1370 int i;
1371 int type = supported;
1373 if (vo_fstype_list)
1375 i = 0;
1376 for (i = 0; vo_fstype_list[i]; i++)
1378 int neg = 0;
1379 char *arg = vo_fstype_list[i];
1381 if (vo_fstype_list[i][0] == '-')
1383 neg = 1;
1384 arg = vo_fstype_list[i] + 1;
1387 if (!strncmp(arg, "layer", 5))
1389 if (!neg && (arg[5] == '='))
1391 char *endptr = NULL;
1392 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1394 if (endptr && *endptr == '\0' && layer >= 0
1395 && layer <= 15)
1396 fs_layer = layer;
1398 if (neg)
1399 type &= ~vo_wm_LAYER;
1400 else
1401 type |= vo_wm_LAYER;
1402 } else if (!strcmp(arg, "above"))
1404 if (neg)
1405 type &= ~vo_wm_ABOVE;
1406 else
1407 type |= vo_wm_ABOVE;
1408 } else if (!strcmp(arg, "fullscreen"))
1410 if (neg)
1411 type &= ~vo_wm_FULLSCREEN;
1412 else
1413 type |= vo_wm_FULLSCREEN;
1414 } else if (!strcmp(arg, "stays_on_top"))
1416 if (neg)
1417 type &= ~vo_wm_STAYS_ON_TOP;
1418 else
1419 type |= vo_wm_STAYS_ON_TOP;
1420 } else if (!strcmp(arg, "below"))
1422 if (neg)
1423 type &= ~vo_wm_BELOW;
1424 else
1425 type |= vo_wm_BELOW;
1426 } else if (!strcmp(arg, "netwm"))
1428 if (neg)
1429 type &= ~vo_wm_NETWM;
1430 else
1431 type |= vo_wm_NETWM;
1432 } else if (!strcmp(arg, "none"))
1433 return 0;
1437 return type;
1440 void vo_x11_fullscreen(void)
1442 int x, y, w, h;
1444 if (WinID >= 0 || vo_fs_flip)
1445 return;
1447 if (vo_fs)
1449 // fs->win
1450 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1452 if (vo_dwidth != vo_screenwidth && vo_dheight != vo_screenheight)
1453 return;
1454 x = vo_old_x;
1455 y = vo_old_y;
1456 w = vo_old_width;
1457 h = vo_old_height;
1460 vo_x11_ewmh_fullscreen(_NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1461 vo_fs = VO_FALSE;
1462 } else
1464 // win->fs
1465 vo_x11_ewmh_fullscreen(_NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1467 vo_fs = VO_TRUE;
1468 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1470 if (vo_old_width &&
1471 (vo_dwidth == vo_screenwidth && vo_dwidth != vo_old_width) &&
1472 (vo_dheight == vo_screenheight && vo_dheight != vo_old_height))
1473 return;
1474 vo_old_x = vo_dx;
1475 vo_old_y = vo_dy;
1476 vo_old_width = vo_dwidth;
1477 vo_old_height = vo_dheight;
1478 update_xinerama_info();
1479 x = xinerama_x;
1480 y = xinerama_y;
1481 w = vo_screenwidth;
1482 h = vo_screenheight;
1486 long dummy;
1488 XGetWMNormalHints(mDisplay, vo_window, &vo_hint, &dummy);
1489 if (!(vo_hint.flags & PWinGravity))
1490 old_gravity = NorthWestGravity;
1491 else
1492 old_gravity = vo_hint.win_gravity;
1494 if (vo_wm_type == 0 && !(vo_fsmode & 16))
1496 XUnmapWindow(mDisplay, vo_window); // required for MWM
1497 XWithdrawWindow(mDisplay, vo_window, mScreen);
1498 vo_fs_flip = 1;
1501 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1503 vo_x11_decoration(mDisplay, vo_window, (vo_fs) ? 0 : 1);
1504 vo_x11_sizehint(x, y, w, h, 0);
1505 vo_x11_setlayer(mDisplay, vo_window, vo_fs);
1508 XMoveResizeWindow(mDisplay, vo_window, x, y, w, h);
1510 /* some WMs lose ontop after fullscreeen */
1511 if ((!(vo_fs)) & vo_ontop)
1512 vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
1514 XMapRaised(mDisplay, vo_window);
1515 XRaiseWindow(mDisplay, vo_window);
1516 XFlush(mDisplay);
1519 void vo_x11_ontop(void)
1521 vo_ontop = (!(vo_ontop));
1523 vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
1527 * XScreensaver stuff
1530 static int got_badwindow;
1531 static XErrorHandler old_handler;
1533 static int badwindow_handler(Display * dpy, XErrorEvent * error)
1535 if (error->error_code != BadWindow)
1536 return (*old_handler) (dpy, error);
1538 got_badwindow = True;
1539 return 0;
1542 static Window find_xscreensaver_window(Display * dpy)
1544 int i;
1545 Window root = RootWindowOfScreen(DefaultScreenOfDisplay(dpy));
1546 Window root2, parent, *kids;
1547 Window retval = 0;
1548 Atom xs_version;
1549 unsigned int nkids = 0;
1551 xs_version = XInternAtom(dpy, "_SCREENSAVER_VERSION", True);
1553 if (!(xs_version != None &&
1554 XQueryTree(dpy, root, &root2, &parent, &kids, &nkids) &&
1555 kids && nkids))
1556 return 0;
1558 old_handler = XSetErrorHandler(badwindow_handler);
1560 for (i = 0; i < nkids; i++)
1562 Atom type;
1563 int format;
1564 unsigned long nitems, bytesafter;
1565 char *v;
1566 int status;
1568 got_badwindow = False;
1569 status =
1570 XGetWindowProperty(dpy, kids[i], xs_version, 0, 200, False,
1571 XA_STRING, &type, &format, &nitems,
1572 &bytesafter, (unsigned char **) &v);
1573 XSync(dpy, False);
1574 if (got_badwindow)
1575 status = BadWindow;
1577 if (status == Success && type != None)
1579 retval = kids[i];
1580 break;
1583 XFree(kids);
1584 XSetErrorHandler(old_handler);
1586 return retval;
1589 static Window xs_windowid = 0;
1590 static Atom deactivate;
1591 static Atom screensaver;
1593 static unsigned int time_last;
1595 void xscreensaver_heartbeat(void)
1597 unsigned int time = GetTimerMS();
1598 XEvent ev;
1600 if (mDisplay && xs_windowid && (time - time_last) > 30000)
1602 time_last = time;
1604 ev.xany.type = ClientMessage;
1605 ev.xclient.display = mDisplay;
1606 ev.xclient.window = xs_windowid;
1607 ev.xclient.message_type = screensaver;
1608 ev.xclient.format = 32;
1609 memset(&ev.xclient.data, 0, sizeof(ev.xclient.data));
1610 ev.xclient.data.l[0] = (long) deactivate;
1612 mp_msg(MSGT_VO, MSGL_DBG2, "Pinging xscreensaver.\n");
1613 old_handler = XSetErrorHandler(badwindow_handler);
1614 XSendEvent(mDisplay, xs_windowid, False, 0L, &ev);
1615 XSync(mDisplay, False);
1616 XSetErrorHandler(old_handler);
1620 static void xscreensaver_disable(Display * dpy)
1622 mp_msg(MSGT_VO, MSGL_DBG2, "xscreensaver_disable()\n");
1624 xs_windowid = find_xscreensaver_window(dpy);
1625 if (!xs_windowid)
1627 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_CouldNotFindXScreenSaver);
1628 return;
1630 mp_msg(MSGT_VO, MSGL_INFO,
1631 "xscreensaver_disable: xscreensaver wid=%ld.\n", xs_windowid);
1633 deactivate = XInternAtom(dpy, "DEACTIVATE", False);
1634 screensaver = XInternAtom(dpy, "SCREENSAVER", False);
1637 static void xscreensaver_enable(void)
1639 xs_windowid = 0;
1643 * End of XScreensaver stuff
1646 void saver_on(Display * mDisplay)
1649 #ifdef HAVE_XDPMS
1650 int nothing;
1652 if (dpms_disabled)
1654 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1656 if (!DPMSEnable(mDisplay))
1657 { // restoring power saving settings
1658 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1659 } else
1661 // DPMS does not seem to be enabled unless we call DPMSInfo
1662 BOOL onoff;
1663 CARD16 state;
1665 DPMSForceLevel(mDisplay, DPMSModeOn);
1666 DPMSInfo(mDisplay, &state, &onoff);
1667 if (onoff)
1669 mp_msg(MSGT_VO, MSGL_V,
1670 "Successfully enabled DPMS\n");
1671 } else
1673 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1677 dpms_disabled = 0;
1679 #endif
1681 if (timeout_save)
1683 int dummy, interval, prefer_blank, allow_exp;
1685 XGetScreenSaver(mDisplay, &dummy, &interval, &prefer_blank,
1686 &allow_exp);
1687 XSetScreenSaver(mDisplay, timeout_save, interval, prefer_blank,
1688 allow_exp);
1689 XGetScreenSaver(mDisplay, &timeout_save, &interval, &prefer_blank,
1690 &allow_exp);
1691 timeout_save = 0;
1694 if (stop_xscreensaver)
1695 xscreensaver_enable();
1696 if (kdescreensaver_was_running && stop_xscreensaver)
1698 system
1699 ("dcop kdesktop KScreensaverIface enable true 2>/dev/null >/dev/null");
1700 kdescreensaver_was_running = 0;
1706 void saver_off(Display * mDisplay)
1709 int interval, prefer_blank, allow_exp;
1711 #ifdef HAVE_XDPMS
1712 int nothing;
1714 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1716 BOOL onoff;
1717 CARD16 state;
1719 DPMSInfo(mDisplay, &state, &onoff);
1720 if (onoff)
1722 Status stat;
1724 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1725 dpms_disabled = 1;
1726 stat = DPMSDisable(mDisplay); // monitor powersave off
1727 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1730 #endif
1731 if (!timeout_save)
1733 XGetScreenSaver(mDisplay, &timeout_save, &interval, &prefer_blank,
1734 &allow_exp);
1735 if (timeout_save)
1736 XSetScreenSaver(mDisplay, 0, interval, prefer_blank,
1737 allow_exp);
1739 // turning off screensaver
1740 if (stop_xscreensaver)
1741 xscreensaver_disable(mDisplay);
1742 if (stop_xscreensaver && !kdescreensaver_was_running)
1744 kdescreensaver_was_running =
1745 (system
1746 ("dcop kdesktop KScreensaverIface isEnabled 2>/dev/null | sed 's/1/true/g' | grep true 2>/dev/null >/dev/null")
1747 == 0);
1748 if (kdescreensaver_was_running)
1749 system
1750 ("dcop kdesktop KScreensaverIface enable false 2>/dev/null >/dev/null");
1754 static XErrorHandler old_handler = NULL;
1755 static int selectinput_err = 0;
1756 static int x11_selectinput_errorhandler(Display * display,
1757 XErrorEvent * event)
1759 if (event->error_code == BadAccess)
1761 selectinput_err = 1;
1762 mp_msg(MSGT_VO, MSGL_ERR,
1763 "X11 error: BadAccess during XSelectInput Call\n");
1764 mp_msg(MSGT_VO, MSGL_ERR,
1765 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1766 /* If you think mplayer should shutdown with this error, comments out following line */
1767 return 0;
1769 if (old_handler != NULL)
1770 old_handler(display, event);
1771 else
1772 x11_errorhandler(display, event);
1773 return 0;
1776 void vo_x11_selectinput_witherr(Display * display, Window w,
1777 long event_mask)
1779 XSync(display, False);
1780 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1781 selectinput_err = 0;
1782 if (vo_nomouse_input)
1784 XSelectInput(display, w,
1785 event_mask &
1786 (~(ButtonPressMask | ButtonReleaseMask)));
1787 } else
1789 XSelectInput(display, w, event_mask);
1791 XSync(display, False);
1792 XSetErrorHandler(old_handler);
1793 if (selectinput_err)
1795 mp_msg(MSGT_VO, MSGL_ERR,
1796 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1797 XSelectInput(display, w,
1798 event_mask &
1800 (ButtonPressMask | ButtonReleaseMask |
1801 PointerMotionMask)));
1805 #ifdef HAVE_XF86VM
1806 void vo_vm_switch(uint32_t X, uint32_t Y, int *modeline_width,
1807 int *modeline_height)
1809 int vm_event, vm_error;
1810 int vm_ver, vm_rev;
1811 int i, j, have_vm = 0;
1813 int modecount;
1815 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1817 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1818 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1819 vm_rev);
1820 have_vm = 1;
1821 } else
1822 mp_msg(MSGT_VO, MSGL_WARN,
1823 "XF86VidMode extension not available.\n");
1825 if (have_vm)
1827 if (vidmodes == NULL)
1828 XF86VidModeGetAllModeLines(mDisplay, mScreen, &modecount,
1829 &vidmodes);
1830 j = 0;
1831 *modeline_width = vidmodes[0]->hdisplay;
1832 *modeline_height = vidmodes[0]->vdisplay;
1834 for (i = 1; i < modecount; i++)
1835 if ((vidmodes[i]->hdisplay >= X)
1836 && (vidmodes[i]->vdisplay >= Y))
1837 if ((vidmodes[i]->hdisplay <= *modeline_width)
1838 && (vidmodes[i]->vdisplay <= *modeline_height))
1840 *modeline_width = vidmodes[i]->hdisplay;
1841 *modeline_height = vidmodes[i]->vdisplay;
1842 j = i;
1845 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_SelectedVideoMode,
1846 *modeline_width, *modeline_height, X, Y);
1847 XF86VidModeLockModeSwitch(mDisplay, mScreen, 0);
1848 XF86VidModeSwitchToMode(mDisplay, mScreen, vidmodes[j]);
1849 XF86VidModeSwitchToMode(mDisplay, mScreen, vidmodes[j]);
1850 X = (vo_screenwidth - *modeline_width) / 2;
1851 Y = (vo_screenheight - *modeline_height) / 2;
1852 XF86VidModeSetViewPort(mDisplay, mScreen, X, Y);
1856 void vo_vm_close(Display * dpy)
1858 #ifdef HAVE_NEW_GUI
1859 if (vidmodes != NULL && vo_window != None)
1860 #else
1861 if (vidmodes != NULL)
1862 #endif
1864 int i, modecount;
1865 int screen;
1867 screen = DefaultScreen(dpy);
1869 free(vidmodes);
1870 vidmodes = NULL;
1871 XF86VidModeGetAllModeLines(mDisplay, mScreen, &modecount,
1872 &vidmodes);
1873 for (i = 0; i < modecount; i++)
1874 if ((vidmodes[i]->hdisplay == vo_screenwidth)
1875 && (vidmodes[i]->vdisplay == vo_screenheight))
1877 mp_msg(MSGT_VO, MSGL_INFO,
1878 "Returning to original mode %dx%d\n",
1879 vo_screenwidth, vo_screenheight);
1880 break;
1883 XF86VidModeSwitchToMode(dpy, screen, vidmodes[i]);
1884 XF86VidModeSwitchToMode(dpy, screen, vidmodes[i]);
1885 free(vidmodes);
1886 vidmodes = NULL;
1889 #endif
1891 #endif /* X11_FULLSCREEN */
1895 * Scan the available visuals on this Display/Screen. Try to find
1896 * the 'best' available TrueColor visual that has a decent color
1897 * depth (at least 15bit). If there are multiple visuals with depth
1898 * >= 15bit, we prefer visuals with a smaller color depth.
1900 int vo_find_depth_from_visuals(Display * dpy, int screen,
1901 Visual ** visual_return)
1903 XVisualInfo visual_tmpl;
1904 XVisualInfo *visuals;
1905 int nvisuals, i;
1906 int bestvisual = -1;
1907 int bestvisual_depth = -1;
1909 visual_tmpl.screen = screen;
1910 visual_tmpl.class = TrueColor;
1911 visuals = XGetVisualInfo(dpy,
1912 VisualScreenMask | VisualClassMask,
1913 &visual_tmpl, &nvisuals);
1914 if (visuals != NULL)
1916 for (i = 0; i < nvisuals; i++)
1918 mp_msg(MSGT_VO, MSGL_V,
1919 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1920 visuals[i].visualid, visuals[i].depth,
1921 visuals[i].red_mask, visuals[i].green_mask,
1922 visuals[i].blue_mask);
1924 * save the visual index and it's depth, if this is the first
1925 * truecolor visul, or a visual that is 'preferred' over the
1926 * previous 'best' visual
1928 if (bestvisual_depth == -1
1929 || (visuals[i].depth >= 15
1930 && (visuals[i].depth < bestvisual_depth
1931 || bestvisual_depth < 15)))
1933 bestvisual = i;
1934 bestvisual_depth = visuals[i].depth;
1938 if (bestvisual != -1 && visual_return != NULL)
1939 *visual_return = visuals[bestvisual].visual;
1941 XFree(visuals);
1943 return bestvisual_depth;
1947 static Colormap cmap = None;
1948 static XColor cols[256];
1949 static int cm_size, red_mask, green_mask, blue_mask;
1952 Colormap vo_x11_create_colormap(XVisualInfo * vinfo)
1954 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1956 if (vinfo->class != DirectColor)
1957 return XCreateColormap(mDisplay, mRootWin, vinfo->visual,
1958 AllocNone);
1960 /* can this function get called twice or more? */
1961 if (cmap)
1962 return cmap;
1963 cm_size = vinfo->colormap_size;
1964 red_mask = vinfo->red_mask;
1965 green_mask = vinfo->green_mask;
1966 blue_mask = vinfo->blue_mask;
1967 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1968 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1969 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1970 rvu = 65536ull * ru / (red_mask + ru);
1971 gvu = 65536ull * gu / (green_mask + gu);
1972 bvu = 65536ull * bu / (blue_mask + bu);
1973 r = g = b = 0;
1974 rv = gv = bv = 0;
1975 m = DoRed | DoGreen | DoBlue;
1976 for (k = 0; k < cm_size; k++)
1978 int t;
1980 cols[k].pixel = r | g | b;
1981 cols[k].red = rv;
1982 cols[k].green = gv;
1983 cols[k].blue = bv;
1984 cols[k].flags = m;
1985 t = (r + ru) & red_mask;
1986 if (t < r)
1987 m &= ~DoRed;
1988 r = t;
1989 t = (g + gu) & green_mask;
1990 if (t < g)
1991 m &= ~DoGreen;
1992 g = t;
1993 t = (b + bu) & blue_mask;
1994 if (t < b)
1995 m &= ~DoBlue;
1996 b = t;
1997 rv += rvu;
1998 gv += gvu;
1999 bv += bvu;
2001 cmap = XCreateColormap(mDisplay, mRootWin, vinfo->visual, AllocAll);
2002 XStoreColors(mDisplay, cmap, cols, cm_size);
2003 return cmap;
2007 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
2008 * hue and red/green/blue intensity, but we cannot do saturation.
2009 * Currently only gamma, brightness and contrast are implemented.
2010 * Is there sufficient interest for hue and/or red/green/blue intensity?
2012 /* these values have range [-100,100] and are initially 0 */
2013 static int vo_gamma = 0;
2014 static int vo_brightness = 0;
2015 static int vo_contrast = 0;
2018 uint32_t vo_x11_set_equalizer(char *name, int value)
2020 float gamma, brightness, contrast;
2021 float rf, gf, bf;
2022 int k;
2025 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
2026 * of TrueColor-ed window but be careful:
2027 * unlike the colormaps, which are private for the X client
2028 * who created them and thus automatically destroyed on client
2029 * disconnect, this gamma ramp is a system-wide (X-server-wide)
2030 * setting and _must_ be restored before the process exit.
2031 * Unforunately when the process crashes (or get killed
2032 * for some reason) it is impossible to restore the setting,
2033 * and such behaviour could be rather annoying for the users.
2035 if (cmap == None)
2036 return VO_NOTAVAIL;
2038 if (!strcasecmp(name, "brightness"))
2039 vo_brightness = value;
2040 else if (!strcasecmp(name, "contrast"))
2041 vo_contrast = value;
2042 else if (!strcasecmp(name, "gamma"))
2043 vo_gamma = value;
2044 else
2045 return VO_NOTIMPL;
2047 brightness = 0.01 * vo_brightness;
2048 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
2049 gamma = pow(2, -0.02 * vo_gamma);
2051 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
2052 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
2053 green_mask;
2054 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
2056 /* now recalculate the colormap using the newly set value */
2057 for (k = 0; k < cm_size; k++)
2059 float s;
2061 s = pow(rf * k, gamma);
2062 s = (s - 0.5) * contrast + 0.5;
2063 s += brightness;
2064 if (s < 0)
2065 s = 0;
2066 if (s > 1)
2067 s = 1;
2068 cols[k].red = (unsigned short) (s * 65535);
2070 s = pow(gf * k, gamma);
2071 s = (s - 0.5) * contrast + 0.5;
2072 s += brightness;
2073 if (s < 0)
2074 s = 0;
2075 if (s > 1)
2076 s = 1;
2077 cols[k].green = (unsigned short) (s * 65535);
2079 s = pow(bf * k, gamma);
2080 s = (s - 0.5) * contrast + 0.5;
2081 s += brightness;
2082 if (s < 0)
2083 s = 0;
2084 if (s > 1)
2085 s = 1;
2086 cols[k].blue = (unsigned short) (s * 65535);
2089 XStoreColors(mDisplay, cmap, cols, cm_size);
2090 XFlush(mDisplay);
2091 return VO_TRUE;
2094 uint32_t vo_x11_get_equalizer(char *name, int *value)
2096 if (cmap == None)
2097 return VO_NOTAVAIL;
2098 if (!strcasecmp(name, "brightness"))
2099 *value = vo_brightness;
2100 else if (!strcasecmp(name, "contrast"))
2101 *value = vo_contrast;
2102 else if (!strcasecmp(name, "gamma"))
2103 *value = vo_gamma;
2104 else
2105 return VO_NOTIMPL;
2106 return VO_TRUE;
2109 #ifdef HAVE_XV
2110 int vo_xv_set_eq(uint32_t xv_port, char *name, int value)
2112 XvAttribute *attributes;
2113 int i, howmany, xv_atom;
2115 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
2117 /* get available attributes */
2118 attributes = XvQueryPortAttributes(mDisplay, xv_port, &howmany);
2119 for (i = 0; i < howmany && attributes; i++)
2120 if (attributes[i].flags & XvSettable)
2122 xv_atom = XInternAtom(mDisplay, attributes[i].name, True);
2123 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2124 then trigger it if it's ok so that the other values are at default upon query */
2125 if (xv_atom != None)
2127 int hue = 0, port_value, port_min, port_max;
2129 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2130 (!strcasecmp(name, "brightness")))
2131 port_value = value;
2132 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2133 (!strcasecmp(name, "contrast")))
2134 port_value = value;
2135 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2136 (!strcasecmp(name, "saturation")))
2137 port_value = value;
2138 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2139 (!strcasecmp(name, "hue")))
2141 port_value = value;
2142 hue = 1;
2143 } else
2144 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2145 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2146 (!strcasecmp(name, "red_intensity")))
2147 port_value = value;
2148 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2149 && (!strcasecmp(name, "green_intensity")))
2150 port_value = value;
2151 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2152 && (!strcasecmp(name, "blue_intensity")))
2153 port_value = value;
2154 else
2155 continue;
2157 port_min = attributes[i].min_value;
2158 port_max = attributes[i].max_value;
2160 /* nvidia hue workaround */
2161 if (hue && port_min == 0 && port_max == 360)
2163 port_value =
2164 (port_value >=
2165 0) ? (port_value - 100) : (port_value + 100);
2167 // -100 -> min
2168 // 0 -> (max+min)/2
2169 // +100 -> max
2170 port_value =
2171 (port_value + 100) * (port_max - port_min) / 200 +
2172 port_min;
2173 XvSetPortAttribute(mDisplay, xv_port, xv_atom, port_value);
2174 return (VO_TRUE);
2177 return (VO_FALSE);
2180 int vo_xv_get_eq(uint32_t xv_port, char *name, int *value)
2183 XvAttribute *attributes;
2184 int i, howmany, xv_atom;
2186 /* get available attributes */
2187 attributes = XvQueryPortAttributes(mDisplay, xv_port, &howmany);
2188 for (i = 0; i < howmany && attributes; i++)
2189 if (attributes[i].flags & XvGettable)
2191 xv_atom = XInternAtom(mDisplay, attributes[i].name, True);
2192 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2193 then trigger it if it's ok so that the other values are at default upon query */
2194 if (xv_atom != None)
2196 int val, port_value = 0, port_min, port_max;
2198 XvGetPortAttribute(mDisplay, xv_port, xv_atom,
2199 &port_value);
2201 port_min = attributes[i].min_value;
2202 port_max = attributes[i].max_value;
2203 val =
2204 (port_value - port_min) * 200 / (port_max - port_min) -
2205 100;
2207 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2208 (!strcasecmp(name, "brightness")))
2209 *value = val;
2210 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2211 (!strcasecmp(name, "contrast")))
2212 *value = val;
2213 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2214 (!strcasecmp(name, "saturation")))
2215 *value = val;
2216 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2217 (!strcasecmp(name, "hue")))
2219 /* nasty nvidia detect */
2220 if (port_min == 0 && port_max == 360)
2221 *value = (val >= 0) ? (val - 100) : (val + 100);
2222 else
2223 *value = val;
2224 } else
2225 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2226 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2227 (!strcasecmp(name, "red_intensity")))
2228 *value = val;
2229 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2230 && (!strcasecmp(name, "green_intensity")))
2231 *value = val;
2232 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2233 && (!strcasecmp(name, "blue_intensity")))
2234 *value = val;
2235 else
2236 continue;
2238 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2239 name, *value);
2240 return (VO_TRUE);
2243 return (VO_FALSE);
2246 /** \brief contains flags changing the execution of the colorkeying code */
2247 xv_ck_info_t xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR };
2248 unsigned long xv_colorkey; ///< The color used for manual colorkeying.
2249 unsigned int xv_port; ///< The selected Xv port.
2252 * \brief Interns the requested atom if it is available.
2254 * \param atom_name String containing the name of the requested atom.
2256 * \return Returns the atom if available, else None is returned.
2259 static Atom xv_intern_atom_if_exists( char const * atom_name )
2261 XvAttribute * attributes;
2262 int attrib_count,i;
2263 Atom xv_atom = None;
2265 attributes = XvQueryPortAttributes( mDisplay, xv_port, &attrib_count );
2266 if( attributes!=NULL )
2268 for ( i = 0; i < attrib_count; ++i )
2270 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2272 xv_atom = XInternAtom( mDisplay, atom_name, False );
2273 break; // found what we want, break out
2276 XFree( attributes );
2279 return xv_atom;
2283 * \brief Try to enable vsync for xv.
2284 * \return Returns -1 if not available, 0 on failure and 1 on success.
2286 int vo_xv_enable_vsync()
2288 Atom xv_atom = xv_intern_atom_if_exists("XV_SYNC_TO_VBLANK");
2289 if (xv_atom == None)
2290 return -1;
2291 return XvSetPortAttribute(mDisplay, xv_port, xv_atom, 1) == Success;
2295 * \brief Get maximum supported source image dimensions.
2297 * This function does not set the variables pointed to by
2298 * width and height if the information could not be retreived.
2299 * So the caller is reponsible for initing them properly.
2301 * \param width [out] The maximum width gets stored here.
2302 * \param height [out] The maximum height gets stored here.
2305 void vo_xv_get_max_img_dim( uint32_t * width, uint32_t * height )
2307 XvEncodingInfo * encodings;
2308 //unsigned long num_encodings, idx; to int or too long?!
2309 unsigned int num_encodings, idx;
2311 XvQueryEncodings( mDisplay, xv_port, &num_encodings, &encodings);
2313 if ( encodings )
2315 for ( idx = 0; idx < num_encodings; ++idx )
2317 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2319 *width = encodings[idx].width;
2320 *height = encodings[idx].height;
2321 break;
2326 mp_msg( MSGT_VO, MSGL_V,
2327 "[xv common] Maximum source image dimensions: %ux%u\n",
2328 *width, *height );
2330 XvFreeEncodingInfo( encodings );
2334 * \brief Print information about the colorkey method and source.
2336 * \param ck_handling Integer value containing the information about
2337 * colorkey handling (see x11_common.h).
2339 * Outputs the content of |ck_handling| as a readable message.
2342 void vo_xv_print_ck_info()
2344 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2346 switch ( xv_ck_info.method )
2348 case CK_METHOD_NONE:
2349 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2350 case CK_METHOD_AUTOPAINT:
2351 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2352 case CK_METHOD_MANUALFILL:
2353 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2354 case CK_METHOD_BACKGROUND:
2355 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2358 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2360 switch ( xv_ck_info.source )
2362 case CK_SRC_CUR:
2363 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2364 xv_colorkey );
2365 break;
2366 case CK_SRC_USE:
2367 if ( xv_ck_info.method == CK_METHOD_AUTOPAINT )
2369 mp_msg( MSGT_VO, MSGL_V,
2370 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2371 xv_colorkey );
2373 else
2375 mp_msg( MSGT_VO, MSGL_V,
2376 "Using colorkey from MPlayer (0x%06lx)."
2377 " Use -colorkey to change.\n",
2378 xv_colorkey );
2380 break;
2381 case CK_SRC_SET:
2382 mp_msg( MSGT_VO, MSGL_V,
2383 "Setting and using colorkey from MPlayer (0x%06lx)."
2384 " Use -colorkey to change.\n",
2385 xv_colorkey );
2386 break;
2390 * \brief Init colorkey depending on the settings in xv_ck_info.
2392 * \return Returns 0 on failure and 1 on success.
2394 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2395 * flags in xv_ck_info.
2397 * Possiblilities:
2398 * * Methods
2399 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2400 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2401 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2402 * * Sources
2403 * - use currently set colorkey ( CK_SRC_CUR )
2404 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2405 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2407 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2408 * we don't draw anything as this means it was forced to off.
2410 int vo_xv_init_colorkey()
2412 Atom xv_atom;
2413 int rez;
2415 /* check if colorkeying is needed */
2416 xv_atom = xv_intern_atom_if_exists( "XV_COLORKEY" );
2418 /* if we have to deal with colorkeying ... */
2419 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2421 /* check if we should use the colorkey specified in vo_colorkey */
2422 if ( xv_ck_info.source != CK_SRC_CUR )
2424 xv_colorkey = vo_colorkey;
2426 /* check if we have to set the colorkey too */
2427 if ( xv_ck_info.source == CK_SRC_SET )
2429 xv_atom = XInternAtom(mDisplay, "XV_COLORKEY",False);
2431 rez = XvSetPortAttribute( mDisplay, xv_port, xv_atom, vo_colorkey );
2432 if ( rez != Success )
2434 mp_msg( MSGT_VO, MSGL_FATAL,
2435 "[xv common] Couldn't set colorkey!\n" );
2436 return 0; // error setting colorkey
2440 else
2442 int colorkey_ret;
2444 rez=XvGetPortAttribute(mDisplay,xv_port, xv_atom, &colorkey_ret);
2445 if ( rez == Success )
2447 xv_colorkey = colorkey_ret;
2449 else
2451 mp_msg( MSGT_VO, MSGL_FATAL,
2452 "[xv common] Couldn't get colorkey!"
2453 "Maybe the selected Xv port has no overlay.\n" );
2454 return 0; // error getting colorkey
2458 xv_atom = xv_intern_atom_if_exists( "XV_AUTOPAINT_COLORKEY" );
2460 /* should we draw the colorkey ourselves or activate autopainting? */
2461 if ( xv_ck_info.method == CK_METHOD_AUTOPAINT )
2463 rez = !Success; // reset rez to something different than Success
2465 if ( xv_atom != None ) // autopaint is supported
2467 rez = XvSetPortAttribute( mDisplay, xv_port, xv_atom, 1 );
2470 if ( rez != Success )
2472 // fallback to manual colorkey drawing
2473 xv_ck_info.method = CK_METHOD_MANUALFILL;
2476 else // disable colorkey autopainting if supported
2478 if ( xv_atom != None ) // we have autopaint attribute
2480 XvSetPortAttribute( mDisplay, xv_port, xv_atom, 0 );
2484 else // do no colorkey drawing at all
2486 xv_ck_info.method = CK_METHOD_NONE;
2487 } /* end: should we draw colorkey */
2489 /* output information about the curren colorkey settings */
2490 vo_xv_print_ck_info();
2492 return 1; // success
2496 * \brief Draw the colorkey on the video window.
2498 * Draws the colorkey depending on the set method ( colorkey_handling ).
2500 * It also draws the black bars ( when the video doesn't fit to the
2501 * display in full screen ) seperately, so they don't overlap with the
2502 * video area.
2503 * It doesn't call XFlush
2506 inline void vo_xv_draw_colorkey( int32_t x, int32_t y,
2507 int32_t w, int32_t h )
2509 if( xv_ck_info.method == CK_METHOD_MANUALFILL ||
2510 xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2512 XSetForeground( mDisplay, vo_gc, xv_colorkey );
2513 XFillRectangle( mDisplay, vo_window, vo_gc,
2514 x, y,
2515 w, h );
2518 /* draw black bars if needed */
2519 /* TODO! move this to vo_x11_clearwindow_part() */
2520 if ( vo_fs )
2522 XSetForeground( mDisplay, vo_gc, 0 );
2523 /* making non overlap fills, requiare 8 checks instead of 4*/
2524 if ( y > 0 )
2525 XFillRectangle( mDisplay, vo_window, vo_gc,
2526 0, 0,
2527 vo_screenwidth, y);
2528 if (x > 0)
2529 XFillRectangle( mDisplay, vo_window, vo_gc,
2530 0, 0,
2531 x, vo_screenheight);
2532 if (x + w < vo_screenwidth)
2533 XFillRectangle( mDisplay, vo_window, vo_gc,
2534 x + w, 0,
2535 vo_screenwidth, vo_screenheight);
2536 if (y + h < vo_screenheight)
2537 XFillRectangle( mDisplay, vo_window, vo_gc,
2538 0, y + h,
2539 vo_screenwidth, vo_screenheight);
2543 /** \brief tests if a valid arg for the ck suboption was given */
2544 int xv_test_ck( void * arg )
2546 strarg_t * strarg = (strarg_t *)arg;
2548 if ( strargcmp( strarg, "use" ) == 0 ||
2549 strargcmp( strarg, "set" ) == 0 ||
2550 strargcmp( strarg, "cur" ) == 0 )
2552 return 1;
2555 return 0;
2557 /** \brief tests if a valid arg for the ck-method suboption was given */
2558 int xv_test_ckm( void * arg )
2560 strarg_t * strarg = (strarg_t *)arg;
2562 if ( strargcmp( strarg, "bg" ) == 0 ||
2563 strargcmp( strarg, "man" ) == 0 ||
2564 strargcmp( strarg, "auto" ) == 0 )
2566 return 1;
2569 return 0;
2573 * \brief Modify the colorkey_handling var according to str
2575 * Checks if a valid pointer ( not NULL ) to the string
2576 * was given. And in that case modifies the colorkey_handling
2577 * var to reflect the requested behaviour.
2578 * If nothing happens the content of colorkey_handling stays
2579 * the same.
2581 * \param str Pointer to the string or NULL
2584 void xv_setup_colorkeyhandling( char const * ck_method_str,
2585 char const * ck_str )
2587 /* check if a valid pointer to the string was passed */
2588 if ( ck_str )
2590 if ( strncmp( ck_str, "use", 3 ) == 0 )
2592 xv_ck_info.source = CK_SRC_USE;
2594 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2596 xv_ck_info.source = CK_SRC_SET;
2599 /* check if a valid pointer to the string was passed */
2600 if ( ck_method_str )
2602 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2604 xv_ck_info.method = CK_METHOD_BACKGROUND;
2606 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2608 xv_ck_info.method = CK_METHOD_MANUALFILL;
2610 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2612 xv_ck_info.method = CK_METHOD_AUTOPAINT;
2617 #endif