osdep: Remove cruft
[mplayer.git] / libvo / x11_common.c
blob617c6be0e9ad98f4fe62bf230ed7481a07c54944
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <math.h>
5 #include <inttypes.h>
7 #include "config.h"
8 #include "options.h"
9 #include "mp_msg.h"
10 #include "mp_fifo.h"
11 #include "x11_common.h"
13 #ifdef X11_FULLSCREEN
15 #include <string.h>
16 #include <unistd.h>
17 #include <assert.h>
19 #include "video_out.h"
20 #include "aspect.h"
21 #include "geometry.h"
22 #include "help_mp.h"
23 #include "osdep/timer.h"
25 #include <X11/Xmd.h>
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <X11/Xatom.h>
30 #ifdef HAVE_XSS
31 #include <X11/extensions/scrnsaver.h>
32 #endif
34 #ifdef HAVE_XDPMS
35 #include <X11/extensions/dpms.h>
36 #endif
38 #ifdef HAVE_XINERAMA
39 #include <X11/extensions/Xinerama.h>
40 #endif
42 #ifdef HAVE_XF86VM
43 #include <X11/extensions/xf86vmode.h>
44 #endif
46 #ifdef HAVE_XF86XK
47 #include <X11/XF86keysym.h>
48 #endif
50 #ifdef HAVE_XV
51 #include <X11/extensions/Xv.h>
52 #include <X11/extensions/Xvlib.h>
54 #include "subopt-helper.h"
55 #endif
57 #include "input/input.h"
58 #include "input/mouse.h"
60 #ifdef HAVE_NEW_GUI
61 #include "gui/interface.h"
62 #include "mplayer.h"
63 #endif
65 #define WIN_LAYER_ONBOTTOM 2
66 #define WIN_LAYER_NORMAL 4
67 #define WIN_LAYER_ONTOP 6
68 #define WIN_LAYER_ABOVE_DOCK 10
70 extern int enable_mouse_movements;
71 int fs_layer = WIN_LAYER_ABOVE_DOCK;
73 int stop_xscreensaver = 0;
75 static int dpms_disabled = 0;
77 char *mDisplayName = NULL;
79 char **vo_fstype_list;
81 /* 1 means that the WM is metacity (broken as hell) */
82 int metacity_hack = 0;
84 #ifdef HAVE_XF86VM
85 XF86VidModeModeInfo **vidmodes = NULL;
86 XF86VidModeModeLine modeline;
87 #endif
89 static int vo_x11_get_fs_type(int supported);
90 static void saver_off(Display *);
91 static void saver_on(Display *);
94 * Sends the EWMH fullscreen state event.
96 * action: could be one of _NET_WM_STATE_REMOVE -- remove state
97 * _NET_WM_STATE_ADD -- add state
98 * _NET_WM_STATE_TOGGLE -- toggle
100 void vo_x11_ewmh_fullscreen(struct vo_x11_state *x11, int action)
102 assert(action == _NET_WM_STATE_REMOVE ||
103 action == _NET_WM_STATE_ADD || action == _NET_WM_STATE_TOGGLE);
105 if (x11->fs_type & vo_wm_FULLSCREEN)
107 XEvent xev;
109 /* init X event structure for _NET_WM_FULLSCREEN client message */
110 xev.xclient.type = ClientMessage;
111 xev.xclient.serial = 0;
112 xev.xclient.send_event = True;
113 xev.xclient.message_type = XInternAtom(x11->display,
114 "_NET_WM_STATE", False);
115 xev.xclient.window = x11->window;
116 xev.xclient.format = 32;
117 xev.xclient.data.l[0] = action;
118 xev.xclient.data.l[1] = XInternAtom(x11->display,
119 "_NET_WM_STATE_FULLSCREEN",
120 False);
121 xev.xclient.data.l[2] = 0;
122 xev.xclient.data.l[3] = 0;
123 xev.xclient.data.l[4] = 0;
125 /* finally send that damn thing */
126 if (!XSendEvent(x11->display, DefaultRootWindow(x11->display), False,
127 SubstructureRedirectMask | SubstructureNotifyMask,
128 &xev))
130 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_EwmhFullscreenStateFailed);
135 static void vo_hidecursor(Display * disp, Window win)
137 Cursor no_ptr;
138 Pixmap bm_no;
139 XColor black, dummy;
140 Colormap colormap;
141 const char bm_no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
143 if (WinID == 0)
144 return; // do not hide if playing on the root window
146 colormap = DefaultColormap(disp, DefaultScreen(disp));
147 if ( !XAllocNamedColor(disp, colormap, "black", &black, &dummy) )
149 return; // color alloc failed, give up
151 bm_no = XCreateBitmapFromData(disp, win, bm_no_data, 8, 8);
152 no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
153 XDefineCursor(disp, win, no_ptr);
154 XFreeCursor(disp, no_ptr);
155 if (bm_no != None)
156 XFreePixmap(disp, bm_no);
157 XFreeColors(disp,colormap,&black.pixel,1,0);
160 static void vo_showcursor(Display * disp, Window win)
162 if (WinID == 0)
163 return;
164 XDefineCursor(disp, win, 0);
167 static int x11_errorhandler(Display * display, XErrorEvent * event)
169 #define MSGLEN 60
170 char msg[MSGLEN];
172 XGetErrorText(display, event->error_code, (char *) &msg, MSGLEN);
174 mp_msg(MSGT_VO, MSGL_ERR, "X11 error: %s\n", msg);
176 mp_msg(MSGT_VO, MSGL_V,
177 "Type: %x, display: %p, resourceid: %lx, serial: %lx\n",
178 event->type, event->display, event->resourceid, event->serial);
179 mp_msg(MSGT_VO, MSGL_V,
180 "Error code: %x, request code: %x, minor code: %x\n",
181 event->error_code, event->request_code, event->minor_code);
183 // abort();
184 //exit_player("X11 error");
185 return 0;
186 #undef MSGLEN
189 void fstype_help(void)
191 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_AvailableFsType);
192 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FULL_SCREEN_TYPES\n");
194 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "none",
195 "don't set fullscreen window layer");
196 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer",
197 "use _WIN_LAYER hint with default layer");
198 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer=<0..15>",
199 "use _WIN_LAYER hint with a given layer number");
200 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "netwm",
201 "force NETWM style");
202 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "above",
203 "use _NETWM_STATE_ABOVE hint if available");
204 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "below",
205 "use _NETWM_STATE_BELOW hint if available");
206 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "fullscreen",
207 "use _NETWM_STATE_FULLSCREEN hint if availale");
208 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "stays_on_top",
209 "use _NETWM_STATE_STAYS_ON_TOP hint if available");
210 mp_msg(MSGT_VO, MSGL_INFO,
211 "You can also negate the settings with simply putting '-' in the beginning");
212 mp_msg(MSGT_VO, MSGL_INFO, "\n");
215 static void fstype_dump(int fstype)
217 if (fstype)
219 mp_msg(MSGT_VO, MSGL_V, "[x11] Current fstype setting honours");
220 if (fstype & vo_wm_LAYER)
221 mp_msg(MSGT_VO, MSGL_V, " LAYER");
222 if (fstype & vo_wm_FULLSCREEN)
223 mp_msg(MSGT_VO, MSGL_V, " FULLSCREEN");
224 if (fstype & vo_wm_STAYS_ON_TOP)
225 mp_msg(MSGT_VO, MSGL_V, " STAYS_ON_TOP");
226 if (fstype & vo_wm_ABOVE)
227 mp_msg(MSGT_VO, MSGL_V, " ABOVE");
228 if (fstype & vo_wm_BELOW)
229 mp_msg(MSGT_VO, MSGL_V, " BELOW");
230 mp_msg(MSGT_VO, MSGL_V, " X atoms\n");
231 } else
232 mp_msg(MSGT_VO, MSGL_V,
233 "[x11] Current fstype setting doesn't honour any X atoms\n");
236 static int net_wm_support_state_test(struct vo_x11_state *x11, Atom atom)
238 #define NET_WM_STATE_TEST(x) { if (atom == x11->XA_NET_WM_STATE_##x) { mp_msg( MSGT_VO,MSGL_V, "[x11] Detected wm supports " #x " state.\n" ); return vo_wm_##x; } }
240 NET_WM_STATE_TEST(FULLSCREEN);
241 NET_WM_STATE_TEST(ABOVE);
242 NET_WM_STATE_TEST(STAYS_ON_TOP);
243 NET_WM_STATE_TEST(BELOW);
244 return 0;
247 static int x11_get_property(struct vo_x11_state *x11, Atom type, Atom ** args,
248 unsigned long *nitems)
250 int format;
251 unsigned long bytesafter;
253 return (Success ==
254 XGetWindowProperty(x11->display, x11->rootwin, type, 0, 16384, False,
255 AnyPropertyType, &type, &format, nitems,
256 &bytesafter, (unsigned char **) args)
257 && *nitems > 0);
260 static int vo_wm_detect(struct vo *vo)
262 struct vo_x11_state *x11 = vo->x11;
263 int i;
264 int wm = 0;
265 unsigned long nitems;
266 Atom *args = NULL;
268 if (WinID >= 0)
269 return 0;
271 // -- supports layers
272 if (x11_get_property(x11, x11->XA_WIN_PROTOCOLS, &args, &nitems))
274 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports layers.\n");
275 for (i = 0; i < nitems; i++)
277 if (args[i] == x11->XA_WIN_LAYER)
279 wm |= vo_wm_LAYER;
280 metacity_hack |= 1;
281 } else
282 /* metacity is the only window manager I know which reports
283 * supporting only the _WIN_LAYER hint in _WIN_PROTOCOLS.
284 * (what's more support for it is broken) */
285 metacity_hack |= 2;
287 XFree(args);
288 if (wm && (metacity_hack == 1))
290 // metacity claims to support layers, but it is not the truth :-)
291 wm ^= vo_wm_LAYER;
292 mp_msg(MSGT_VO, MSGL_V,
293 "[x11] Using workaround for Metacity bugs.\n");
296 // --- netwm
297 if (x11_get_property(x11, x11->XA_NET_SUPPORTED, &args, &nitems))
299 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports NetWM.\n");
300 for (i = 0; i < nitems; i++)
301 wm |= net_wm_support_state_test(vo->x11, args[i]);
302 XFree(args);
303 #if 0
304 // ugly hack for broken OpenBox _NET_WM_STATE_FULLSCREEN support
305 // (in their implementation it only changes internal window state, nothing more!!!)
306 if (wm & vo_wm_FULLSCREEN)
308 if (x11_get_property(x11, x11->XA_BLACKBOX_PID, &args, &nitems))
310 mp_msg(MSGT_VO, MSGL_V,
311 "[x11] Detected wm is a broken OpenBox.\n");
312 wm ^= vo_wm_FULLSCREEN;
314 XFree(args);
316 #endif
319 if (wm == 0)
320 mp_msg(MSGT_VO, MSGL_V, "[x11] Unknown wm type...\n");
321 return wm;
324 #define XA_INIT(x) x11->XA##x = XInternAtom(x11->display, #x, False)
325 static void init_atoms(struct vo_x11_state *x11)
327 XA_INIT(_NET_SUPPORTED);
328 XA_INIT(_NET_WM_STATE);
329 XA_INIT(_NET_WM_STATE_FULLSCREEN);
330 XA_INIT(_NET_WM_STATE_ABOVE);
331 XA_INIT(_NET_WM_STATE_STAYS_ON_TOP);
332 XA_INIT(_NET_WM_STATE_BELOW);
333 XA_INIT(_NET_WM_PID);
334 XA_INIT(_WIN_PROTOCOLS);
335 XA_INIT(_WIN_LAYER);
336 XA_INIT(_WIN_HINTS);
337 XA_INIT(_BLACKBOX_PID);
338 XA_INIT(WM_PROTOCOLS);
339 XA_INIT(WM_DELETE_WINDOW);
342 void update_xinerama_info(struct vo *vo) {
343 struct MPOpts *opts = vo->opts;
344 int screen = xinerama_screen;
345 xinerama_x = xinerama_y = 0;
346 #ifdef HAVE_XINERAMA
347 if (screen >= -1 && XineramaIsActive(vo->x11->display))
349 XineramaScreenInfo *screens;
350 int num_screens;
352 screens = XineramaQueryScreens(vo->x11->display, &num_screens);
353 if (screen >= num_screens)
354 screen = num_screens - 1;
355 if (screen == -1) {
356 int x = vo->dx + vo->dwidth / 2;
357 int y = vo->dy + vo->dheight / 2;
358 for (screen = num_screens - 1; screen > 0; screen--) {
359 int left = screens[screen].x_org;
360 int right = left + screens[screen].width;
361 int top = screens[screen].y_org;
362 int bottom = top + screens[screen].height;
363 if (left <= x && x <= right && top <= y && y <= bottom)
364 break;
367 if (screen < 0)
368 screen = 0;
369 opts->vo_screenwidth = screens[screen].width;
370 opts->vo_screenheight = screens[screen].height;
371 xinerama_x = screens[screen].x_org;
372 xinerama_y = screens[screen].y_org;
374 XFree(screens);
376 #endif
377 aspect_save_screenres(opts->vo_screenwidth, opts->vo_screenheight);
380 int vo_init(struct vo *vo)
382 struct MPOpts *opts = vo->opts;
383 struct vo_x11_state *x11 = vo->x11;
384 // int mScreen;
385 int depth, bpp;
386 unsigned int mask;
388 // char * DisplayName = ":0.0";
389 // Display * mDisplay;
390 XImage *mXImage = NULL;
392 // Window mRootWin;
393 XWindowAttributes attribs;
394 char *dispName;
396 if (vo_rootwin)
397 WinID = 0; // use root window
399 if (x11->depthonscreen)
401 saver_off(x11->display);
402 return 1; // already called
405 XSetErrorHandler(x11_errorhandler);
407 #if 0
408 if (!mDisplayName)
409 if (!(mDisplayName = getenv("DISPLAY")))
410 mDisplayName = strdup(":0.0");
411 #else
412 dispName = XDisplayName(mDisplayName);
413 #endif
415 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
417 x11->display = XOpenDisplay(dispName);
418 if (!x11->display)
420 mp_msg(MSGT_VO, MSGL_ERR,
421 "vo: couldn't open the X11 display (%s)!\n", dispName);
422 return 0;
424 x11->screen = DefaultScreen(x11->display); // screen ID
425 x11->rootwin = RootWindow(x11->display, x11->screen); // root window ID
427 init_atoms(vo->x11);
429 #ifdef HAVE_XF86VM
431 int clock;
433 XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline);
434 if (!opts->vo_screenwidth)
435 opts->vo_screenwidth = modeline.hdisplay;
436 if (!opts->vo_screenheight)
437 opts->vo_screenheight = modeline.vdisplay;
439 #endif
441 if (!opts->vo_screenwidth)
442 opts->vo_screenwidth = DisplayWidth(x11->display, x11->screen);
443 if (!opts->vo_screenheight)
444 opts->vo_screenheight = DisplayHeight(x11->display, x11->screen);
446 // get color depth (from root window, or the best visual):
447 XGetWindowAttributes(x11->display, x11->rootwin, &attribs);
448 depth = attribs.depth;
450 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
452 Visual *visual;
454 depth = vo_find_depth_from_visuals(x11->display, x11->screen, &visual);
455 if (depth != -1)
456 mXImage = XCreateImage(x11->display, visual, depth, ZPixmap,
457 0, NULL, 1, 1, 8, 1);
458 } else
459 mXImage =
460 XGetImage(x11->display, x11->rootwin, 0, 0, 1, 1, AllPlanes, ZPixmap);
462 x11->depthonscreen = depth; // display depth on screen
464 // get bits/pixel from XImage structure:
465 if (mXImage == NULL)
467 mask = 0;
468 } else
471 * for the depth==24 case, the XImage structures might use
472 * 24 or 32 bits of data per pixel. The x11->depthonscreen
473 * field stores the amount of data per pixel in the
474 * XImage structure!
476 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
478 bpp = mXImage->bits_per_pixel;
479 if ((x11->depthonscreen + 7) / 8 != (bpp + 7) / 8)
480 x11->depthonscreen = bpp; // by A'rpi
481 mask =
482 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
483 mp_msg(MSGT_VO, MSGL_V,
484 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
485 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
486 XDestroyImage(mXImage);
488 if (((x11->depthonscreen + 7) / 8) == 2)
490 if (mask == 0x7FFF)
491 x11->depthonscreen = 15;
492 else if (mask == 0xFFFF)
493 x11->depthonscreen = 16;
495 // XCloseDisplay( mDisplay );
496 /* slightly improved local display detection AST */
497 if (strncmp(dispName, "unix:", 5) == 0)
498 dispName += 4;
499 else if (strncmp(dispName, "localhost:", 10) == 0)
500 dispName += 9;
501 if (*dispName == ':' && atoi(dispName + 1) < 10)
502 x11->display_is_local = 1;
503 else
504 x11->display_is_local = 0;
505 mp_msg(MSGT_VO, MSGL_V,
506 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
507 opts->vo_screenwidth, opts->vo_screenheight, depth, x11->depthonscreen,
508 dispName, x11->display_is_local ? "local" : "remote");
510 x11->wm_type = vo_wm_detect(vo);
512 x11->fs_type = vo_x11_get_fs_type(x11->wm_type);
514 fstype_dump(x11->fs_type);
516 saver_off(x11->display);
517 return 1;
520 void vo_uninit(struct vo_x11_state *x11)
522 if (!x11->display)
524 mp_msg(MSGT_VO, MSGL_V,
525 "vo: x11 uninit called but X11 not initialized..\n");
526 return;
528 // if( !vo_depthonscreen ) return;
529 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
530 XSetErrorHandler(NULL);
531 XCloseDisplay(x11->display);
532 x11->depthonscreen = 0;
533 x11->display = NULL;
536 #include "osdep/keycodes.h"
537 #include "wskeys.h"
539 #ifdef XF86XK_AudioPause
540 static void vo_x11_putkey_ext(int keysym)
542 switch (keysym)
544 case XF86XK_AudioPause:
545 mplayer_put_key(KEY_PAUSE);
546 break;
547 case XF86XK_AudioStop:
548 mplayer_put_key(KEY_STOP);
549 break;
550 case XF86XK_AudioPrev:
551 mplayer_put_key(KEY_PREV);
552 break;
553 case XF86XK_AudioNext:
554 mplayer_put_key(KEY_NEXT);
555 break;
556 case XF86XK_AudioLowerVolume:
557 mplayer_put_key(KEY_VOLUME_DOWN);
558 break;
559 case XF86XK_AudioRaiseVolume:
560 mplayer_put_key(KEY_VOLUME_UP);
561 break;
562 default:
563 break;
566 #endif
568 void vo_x11_putkey(int key)
570 switch (key)
572 case wsLeft:
573 mplayer_put_key(KEY_LEFT);
574 break;
575 case wsRight:
576 mplayer_put_key(KEY_RIGHT);
577 break;
578 case wsUp:
579 mplayer_put_key(KEY_UP);
580 break;
581 case wsDown:
582 mplayer_put_key(KEY_DOWN);
583 break;
584 case wsSpace:
585 mplayer_put_key(' ');
586 break;
587 case wsEscape:
588 mplayer_put_key(KEY_ESC);
589 break;
590 case wsTab:
591 mplayer_put_key(KEY_TAB);
592 break;
593 case wsEnter:
594 mplayer_put_key(KEY_ENTER);
595 break;
596 case wsBackSpace:
597 mplayer_put_key(KEY_BS);
598 break;
599 case wsDelete:
600 mplayer_put_key(KEY_DELETE);
601 break;
602 case wsInsert:
603 mplayer_put_key(KEY_INSERT);
604 break;
605 case wsHome:
606 mplayer_put_key(KEY_HOME);
607 break;
608 case wsEnd:
609 mplayer_put_key(KEY_END);
610 break;
611 case wsPageUp:
612 mplayer_put_key(KEY_PAGE_UP);
613 break;
614 case wsPageDown:
615 mplayer_put_key(KEY_PAGE_DOWN);
616 break;
617 case wsF1:
618 mplayer_put_key(KEY_F + 1);
619 break;
620 case wsF2:
621 mplayer_put_key(KEY_F + 2);
622 break;
623 case wsF3:
624 mplayer_put_key(KEY_F + 3);
625 break;
626 case wsF4:
627 mplayer_put_key(KEY_F + 4);
628 break;
629 case wsF5:
630 mplayer_put_key(KEY_F + 5);
631 break;
632 case wsF6:
633 mplayer_put_key(KEY_F + 6);
634 break;
635 case wsF7:
636 mplayer_put_key(KEY_F + 7);
637 break;
638 case wsF8:
639 mplayer_put_key(KEY_F + 8);
640 break;
641 case wsF9:
642 mplayer_put_key(KEY_F + 9);
643 break;
644 case wsF10:
645 mplayer_put_key(KEY_F + 10);
646 break;
647 case wsF11:
648 mplayer_put_key(KEY_F + 11);
649 break;
650 case wsF12:
651 mplayer_put_key(KEY_F + 12);
652 break;
653 case wsMinus:
654 case wsGrayMinus:
655 mplayer_put_key('-');
656 break;
657 case wsPlus:
658 case wsGrayPlus:
659 mplayer_put_key('+');
660 break;
661 case wsGrayMul:
662 case wsMul:
663 mplayer_put_key('*');
664 break;
665 case wsGrayDiv:
666 case wsDiv:
667 mplayer_put_key('/');
668 break;
669 case wsLess:
670 mplayer_put_key('<');
671 break;
672 case wsMore:
673 mplayer_put_key('>');
674 break;
675 case wsGray0:
676 mplayer_put_key(KEY_KP0);
677 break;
678 case wsGrayEnd:
679 case wsGray1:
680 mplayer_put_key(KEY_KP1);
681 break;
682 case wsGrayDown:
683 case wsGray2:
684 mplayer_put_key(KEY_KP2);
685 break;
686 case wsGrayPgDn:
687 case wsGray3:
688 mplayer_put_key(KEY_KP3);
689 break;
690 case wsGrayLeft:
691 case wsGray4:
692 mplayer_put_key(KEY_KP4);
693 break;
694 case wsGray5Dup:
695 case wsGray5:
696 mplayer_put_key(KEY_KP5);
697 break;
698 case wsGrayRight:
699 case wsGray6:
700 mplayer_put_key(KEY_KP6);
701 break;
702 case wsGrayHome:
703 case wsGray7:
704 mplayer_put_key(KEY_KP7);
705 break;
706 case wsGrayUp:
707 case wsGray8:
708 mplayer_put_key(KEY_KP8);
709 break;
710 case wsGrayPgUp:
711 case wsGray9:
712 mplayer_put_key(KEY_KP9);
713 break;
714 case wsGrayDecimal:
715 mplayer_put_key(KEY_KPDEC);
716 break;
717 case wsGrayInsert:
718 mplayer_put_key(KEY_KPINS);
719 break;
720 case wsGrayDelete:
721 mplayer_put_key(KEY_KPDEL);
722 break;
723 case wsGrayEnter:
724 mplayer_put_key(KEY_KPENTER);
725 break;
726 case wsGrave:
727 mplayer_put_key('`');
728 break;
729 case wsTilde:
730 mplayer_put_key('~');
731 break;
732 case wsExclSign:
733 mplayer_put_key('!');
734 break;
735 case wsAt:
736 mplayer_put_key('@');
737 break;
738 case wsHash:
739 mplayer_put_key('#');
740 break;
741 case wsDollar:
742 mplayer_put_key('$');
743 break;
744 case wsPercent:
745 mplayer_put_key('%');
746 break;
747 case wsCircumflex:
748 mplayer_put_key('^');
749 break;
750 case wsAmpersand:
751 mplayer_put_key('&');
752 break;
753 case wsobracket:
754 mplayer_put_key('(');
755 break;
756 case wscbracket:
757 mplayer_put_key(')');
758 break;
759 case wsUnder:
760 mplayer_put_key('_');
761 break;
762 case wsocbracket:
763 mplayer_put_key('{');
764 break;
765 case wsccbracket:
766 mplayer_put_key('}');
767 break;
768 case wsColon:
769 mplayer_put_key(':');
770 break;
771 case wsSemicolon:
772 mplayer_put_key(';');
773 break;
774 case wsDblQuote:
775 mplayer_put_key('\"');
776 break;
777 case wsAcute:
778 mplayer_put_key('\'');
779 break;
780 case wsComma:
781 mplayer_put_key(',');
782 break;
783 case wsPoint:
784 mplayer_put_key('.');
785 break;
786 case wsQuestSign:
787 mplayer_put_key('?');
788 break;
789 case wsBSlash:
790 mplayer_put_key('\\');
791 break;
792 case wsPipe:
793 mplayer_put_key('|');
794 break;
795 case wsEqual:
796 mplayer_put_key('=');
797 break;
798 case wsosbrackets:
799 mplayer_put_key('[');
800 break;
801 case wscsbrackets:
802 mplayer_put_key(']');
803 break;
806 default:
807 if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') ||
808 (key >= '0' && key <= '9'))
809 mplayer_put_key(key);
815 // ----- Motif header: -------
817 #define MWM_HINTS_FUNCTIONS (1L << 0)
818 #define MWM_HINTS_DECORATIONS (1L << 1)
819 #define MWM_HINTS_INPUT_MODE (1L << 2)
820 #define MWM_HINTS_STATUS (1L << 3)
822 #define MWM_FUNC_ALL (1L << 0)
823 #define MWM_FUNC_RESIZE (1L << 1)
824 #define MWM_FUNC_MOVE (1L << 2)
825 #define MWM_FUNC_MINIMIZE (1L << 3)
826 #define MWM_FUNC_MAXIMIZE (1L << 4)
827 #define MWM_FUNC_CLOSE (1L << 5)
829 #define MWM_DECOR_ALL (1L << 0)
830 #define MWM_DECOR_BORDER (1L << 1)
831 #define MWM_DECOR_RESIZEH (1L << 2)
832 #define MWM_DECOR_TITLE (1L << 3)
833 #define MWM_DECOR_MENU (1L << 4)
834 #define MWM_DECOR_MINIMIZE (1L << 5)
835 #define MWM_DECOR_MAXIMIZE (1L << 6)
837 #define MWM_INPUT_MODELESS 0
838 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
839 #define MWM_INPUT_SYSTEM_MODAL 2
840 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
841 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
843 #define MWM_TEAROFF_WINDOW (1L<<0)
845 typedef struct
847 long flags;
848 long functions;
849 long decorations;
850 long input_mode;
851 long state;
852 } MotifWmHints;
854 static MotifWmHints vo_MotifWmHints;
855 static Atom vo_MotifHints = None;
857 void vo_x11_decoration(struct vo *vo, int d)
859 struct vo_x11_state *x11 = vo->x11;
860 Atom mtype;
861 int mformat;
862 unsigned long mn, mb;
864 if (!WinID)
865 return;
867 if (vo_fsmode & 8)
869 XSetTransientForHint(x11->display, x11->window,
870 RootWindow(x11->display, x11->screen));
873 vo_MotifHints = XInternAtom(x11->display, "_MOTIF_WM_HINTS", 0);
874 if (vo_MotifHints != None)
876 if (!d)
878 MotifWmHints *mhints = NULL;
880 XGetWindowProperty(x11->display, x11->window,
881 vo_MotifHints, 0, 20, False,
882 vo_MotifHints, &mtype, &mformat, &mn,
883 &mb, (unsigned char **) &mhints);
884 if (mhints)
886 if (mhints->flags & MWM_HINTS_DECORATIONS)
887 x11->olddecor = mhints->decorations;
888 if (mhints->flags & MWM_HINTS_FUNCTIONS)
889 x11->oldfuncs = mhints->functions;
890 XFree(mhints);
894 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
895 vo_MotifWmHints.flags =
896 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
897 if (d)
899 vo_MotifWmHints.functions = x11->oldfuncs;
900 d = x11->olddecor;
902 #if 0
903 vo_MotifWmHints.decorations =
904 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
905 #else
906 vo_MotifWmHints.decorations =
907 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
908 #endif
909 XChangeProperty(x11->display, x11->window, vo_MotifHints,
910 vo_MotifHints, 32,
911 PropModeReplace,
912 (unsigned char *) &vo_MotifWmHints,
913 (vo_fsmode & 4) ? 4 : 5);
917 void vo_x11_classhint(struct vo *vo, Window window, char *name)
919 struct vo_x11_state *x11 = vo->x11;
920 XClassHint wmClass;
921 pid_t pid = getpid();
923 wmClass.res_name = name;
924 wmClass.res_class = "MPlayer";
925 XSetClassHint(x11->display, window, &wmClass);
926 XChangeProperty(x11->display, window, x11->XA_NET_WM_PID, XA_CARDINAL,
927 32, PropModeReplace, (unsigned char *) &pid, 1);
930 void vo_x11_uninit(struct vo *vo)
932 struct vo_x11_state *x11 = vo->x11;
933 saver_on(x11->display);
934 if (x11->window != None)
935 vo_showcursor(x11->display, x11->window);
937 if (x11->f_gc)
939 XFreeGC(vo->x11->display, x11->f_gc);
940 x11->f_gc = NULL;
942 #ifdef HAVE_NEW_GUI
943 /* destroy window only if it's not controlled by the GUI */
944 if (!use_gui)
945 #endif
947 if (x11->vo_gc)
949 XSetBackground(vo->x11->display, x11->vo_gc, 0);
950 XFreeGC(vo->x11->display, x11->vo_gc);
951 x11->vo_gc = NULL;
953 if (x11->window != None)
955 XClearWindow(x11->display, x11->window);
956 if (WinID < 0)
958 XEvent xev;
960 XUnmapWindow(x11->display, x11->window);
961 XDestroyWindow(x11->display, x11->window);
964 XNextEvent(x11->display, &xev);
966 while (xev.type != DestroyNotify
967 || xev.xdestroywindow.event != x11->window);
969 x11->window = None;
971 vo_fs = 0;
972 x11->vo_old_width = x11->vo_old_height = 0;
976 int vo_x11_check_events(struct vo *vo)
978 struct vo_x11_state *x11 = vo->x11;
979 struct MPOpts *opts = vo->opts;
980 Display *display = vo->x11->display;
981 int ret = 0;
982 XEvent Event;
983 char buf[100];
984 KeySym keySym;
986 // unsigned long vo_KeyTable[512];
988 if ((x11->vo_mouse_autohide) && x11->mouse_waiting_hide &&
989 (GetTimerMS() - x11->mouse_timer >= 1000)) {
990 vo_hidecursor(display, x11->window);
991 x11->mouse_waiting_hide = 0;
994 while (XPending(display))
996 XNextEvent(display, &Event);
997 #ifdef HAVE_NEW_GUI
998 if (use_gui)
1000 guiGetEvent(0, (char *) &Event);
1001 if (x11->window != Event.xany.window)
1002 continue;
1004 #endif
1005 // printf("\rEvent.type=%X \n",Event.type);
1006 switch (Event.type)
1008 case Expose:
1009 ret |= VO_EVENT_EXPOSE;
1010 break;
1011 case ConfigureNotify:
1012 // if (!vo_fs && (Event.xconfigure.width == opts->vo_screenwidth || Event.xconfigure.height == opts->vo_screenheight)) break;
1013 // if (vo_fs && Event.xconfigure.width != opts->vo_screenwidth && Event.xconfigure.height != opts->vo_screenheight) break;
1014 if (x11->window == None)
1015 break;
1016 vo->dwidth = Event.xconfigure.width;
1017 vo->dheight = Event.xconfigure.height;
1018 #if 0
1019 /* when resizing, x and y are zero :( */
1020 vo->dx = Event.xconfigure.x;
1021 vo->dy = Event.xconfigure.y;
1022 #else
1024 Window root;
1025 int foo;
1026 Window win;
1028 XGetGeometry(display, x11->window, &root, &foo, &foo,
1029 &foo /*width */ , &foo /*height */ , &foo,
1030 &foo);
1031 XTranslateCoordinates(display, x11->window, root, 0, 0,
1032 &vo->dx, &vo->dy, &win);
1034 #endif
1035 ret |= VO_EVENT_RESIZE;
1036 break;
1037 case KeyPress:
1039 int key;
1041 #ifdef HAVE_NEW_GUI
1042 if ( use_gui ) { break; }
1043 #endif
1045 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
1046 &x11->compose_status);
1047 #ifdef XF86XK_AudioPause
1048 vo_x11_putkey_ext(keySym);
1049 #endif
1050 key =
1051 ((keySym & 0xff00) !=
1052 0 ? ((keySym & 0x00ff) + 256) : (keySym));
1053 vo_x11_putkey(key);
1054 ret |= VO_EVENT_KEYPRESS;
1056 break;
1057 case MotionNotify:
1058 if(enable_mouse_movements)
1060 char cmd_str[40];
1061 sprintf(cmd_str,"set_mouse_pos %i %i",Event.xmotion.x, Event.xmotion.y);
1062 mp_input_queue_cmd(mp_input_parse_cmd(cmd_str));
1065 if (x11->vo_mouse_autohide)
1067 vo_showcursor(display, x11->window);
1068 x11->mouse_waiting_hide = 1;
1069 x11->mouse_timer = GetTimerMS();
1071 break;
1072 case ButtonPress:
1073 if (x11->vo_mouse_autohide)
1075 vo_showcursor(display, x11->window);
1076 x11->mouse_waiting_hide = 1;
1077 x11->mouse_timer = GetTimerMS();
1079 #ifdef HAVE_NEW_GUI
1080 // Ignore mouse button 1-3 under GUI.
1081 if (use_gui && (Event.xbutton.button >= 1)
1082 && (Event.xbutton.button <= 3))
1083 break;
1084 #endif
1085 mplayer_put_key((MOUSE_BTN0 + Event.xbutton.button -
1086 1) | MP_KEY_DOWN);
1087 break;
1088 case ButtonRelease:
1089 if (x11->vo_mouse_autohide)
1091 vo_showcursor(display, x11->window);
1092 x11->mouse_waiting_hide = 1;
1093 x11->mouse_timer = GetTimerMS();
1095 #ifdef HAVE_NEW_GUI
1096 // Ignore mouse button 1-3 under GUI.
1097 if (use_gui && (Event.xbutton.button >= 1)
1098 && (Event.xbutton.button <= 3))
1099 break;
1100 #endif
1101 mplayer_put_key(MOUSE_BTN0 + Event.xbutton.button - 1);
1102 break;
1103 case PropertyNotify:
1105 char *name =
1106 XGetAtomName(display, Event.xproperty.atom);
1108 if (!name)
1109 break;
1111 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
1113 XFree(name);
1115 break;
1116 case MapNotify:
1117 x11->vo_hint.win_gravity = x11->old_gravity;
1118 XSetWMNormalHints(display, x11->window, &x11->vo_hint);
1119 x11->fs_flip = 0;
1120 break;
1121 case ClientMessage:
1122 if (Event.xclient.message_type == x11->XAWM_PROTOCOLS &&
1123 Event.xclient.data.l[0] == x11->XAWM_DELETE_WINDOW)
1124 mplayer_put_key(KEY_CLOSE_WIN);
1125 break;
1128 return ret;
1132 * \brief sets the size and position of the non-fullscreen window.
1134 static void vo_x11_nofs_sizepos(struct vo *vo, int x, int y,
1135 int width, int height)
1137 struct vo_x11_state *x11 = vo->x11;
1138 vo_x11_sizehint(vo, x, y, width, height, 0);
1139 if (vo_fs) {
1140 x11->vo_old_x = x;
1141 x11->vo_old_y = y;
1142 x11->vo_old_width = width;
1143 x11->vo_old_height = height;
1145 else
1147 vo->dwidth = width;
1148 vo->dheight = height;
1149 XMoveResizeWindow(vo->x11->display, vo->x11->window, x, y, width, height);
1153 void vo_x11_sizehint(struct vo *vo, int x, int y, int width, int height, int max)
1155 struct vo_x11_state *x11 = vo->x11;
1156 x11->vo_hint.flags = 0;
1157 if (vo_keepaspect)
1159 x11->vo_hint.flags |= PAspect;
1160 x11->vo_hint.min_aspect.x = width;
1161 x11->vo_hint.min_aspect.y = height;
1162 x11->vo_hint.max_aspect.x = width;
1163 x11->vo_hint.max_aspect.y = height;
1166 x11->vo_hint.flags |= PPosition | PSize;
1167 x11->vo_hint.x = x;
1168 x11->vo_hint.y = y;
1169 x11->vo_hint.width = width;
1170 x11->vo_hint.height = height;
1171 if (max)
1173 x11->vo_hint.flags |= PMaxSize;
1174 x11->vo_hint.max_width = width;
1175 x11->vo_hint.max_height = height;
1176 } else
1178 x11->vo_hint.max_width = 0;
1179 x11->vo_hint.max_height = 0;
1182 // Set minimum height/width to 4 to avoid off-by-one errors
1183 // and because mga_vid requires a minimal size of 4 pixels.
1184 x11->vo_hint.flags |= PMinSize;
1185 x11->vo_hint.min_width = x11->vo_hint.min_height = 4;
1187 x11->vo_hint.flags |= PWinGravity;
1188 x11->vo_hint.win_gravity = StaticGravity;
1189 XSetWMNormalHints(x11->display, x11->window, &x11->vo_hint);
1192 static int vo_x11_get_gnome_layer(struct vo_x11_state *x11, Window win)
1194 Atom type;
1195 int format;
1196 unsigned long nitems;
1197 unsigned long bytesafter;
1198 unsigned short *args = NULL;
1200 if (XGetWindowProperty(x11->display, win, x11->XA_WIN_LAYER, 0, 16384,
1201 False, AnyPropertyType, &type, &format, &nitems,
1202 &bytesafter,
1203 (unsigned char **) &args) == Success
1204 && nitems > 0 && args)
1206 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
1207 *args);
1208 return *args;
1210 return WIN_LAYER_NORMAL;
1214 static Window vo_x11_create_smooth_window(struct vo_x11_state *x11, Window mRoot,
1215 Visual * vis, int x, int y,
1216 unsigned int width, unsigned int height,
1217 int depth, Colormap col_map)
1219 unsigned long xswamask = CWBackingStore | CWBorderPixel;
1220 XSetWindowAttributes xswa;
1221 Window ret_win;
1223 if (col_map != CopyFromParent)
1225 xswa.colormap = col_map;
1226 xswamask |= CWColormap;
1228 xswa.background_pixel = 0;
1229 xswa.border_pixel = 0;
1230 xswa.backing_store = Always;
1231 xswa.bit_gravity = StaticGravity;
1233 ret_win =
1234 XCreateWindow(x11->display, x11->rootwin, x, y, width, height, 0, depth,
1235 CopyFromParent, vis, xswamask, &xswa);
1236 XSetWMProtocols(x11->display, ret_win, &x11->XAWM_DELETE_WINDOW, 1);
1237 if (!x11->f_gc)
1238 x11->f_gc = XCreateGC(x11->display, ret_win, 0, 0);
1239 XSetForeground(x11->display, x11->f_gc, 0);
1241 return ret_win;
1245 * \brief create and setup a window suitable for display
1246 * \param vis Visual to use for creating the window
1247 * \param x x position of window
1248 * \param y y position of window
1249 * \param width width of window
1250 * \param height height of window
1251 * \param flags flags for window creation.
1252 * Only VOFLAG_FULLSCREEN is supported so far.
1253 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1254 * \param classname name to use for the classhint
1255 * \param title title for the window
1257 * This also does the grunt-work like setting Window Manager hints etc.
1258 * If vo_window is already set it just moves and resizes it.
1260 void vo_x11_create_vo_window(struct vo *vo, XVisualInfo *vis, int x, int y,
1261 unsigned int width, unsigned int height, int flags,
1262 Colormap col_map,
1263 const char *classname, const char *title)
1265 struct MPOpts *opts = vo->opts;
1266 struct vo_x11_state *x11 = vo->x11;
1267 Display *mDisplay = vo->x11->display;
1268 if (x11->window == None) {
1269 XSizeHints hint;
1270 XEvent xev;
1271 vo_fs = 0;
1272 vo->dwidth = width;
1273 vo->dheight = height;
1274 x11->window = vo_x11_create_smooth_window(x11, x11->rootwin, vis->visual,
1275 x, y, width, height, vis->depth, col_map);
1276 vo_x11_classhint(vo, x11->window, classname);
1277 XStoreName(mDisplay, x11->window, title);
1278 vo_hidecursor(mDisplay, x11->window);
1279 XSelectInput(mDisplay, x11->window, StructureNotifyMask);
1280 hint.x = x; hint.y = y;
1281 hint.width = width; hint.height = height;
1282 hint.flags = PPosition | PSize;
1283 XSetStandardProperties(mDisplay, x11->window, title, title, None, NULL, 0, &hint);
1284 vo_x11_sizehint(vo, x, y, width, height, 0);
1285 // map window
1286 XMapWindow(mDisplay, x11->window);
1287 XClearWindow(mDisplay, x11->window);
1288 // wait for map
1289 do {
1290 XNextEvent(mDisplay, &xev);
1291 } while (xev.type != MapNotify || xev.xmap.event != x11->window);
1292 XSelectInput(mDisplay, x11->window, NoEventMask);
1293 XSync(mDisplay, False);
1294 vo_x11_selectinput_witherr(mDisplay, x11->window,
1295 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1296 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1298 if (opts->vo_ontop) vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1299 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, width, height);
1300 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1301 vo_x11_fullscreen(vo);
1304 void vo_x11_clearwindow_part(struct vo *vo, Window vo_window,
1305 int img_width, int img_height, int use_fs)
1307 struct vo_x11_state *x11 = vo->x11;
1308 struct MPOpts *opts = vo->opts;
1309 Display *mDisplay = vo->x11->display;
1310 int u_dheight, u_dwidth, left_ov, left_ov2;
1312 if (!x11->f_gc)
1313 return;
1315 u_dheight = use_fs ? opts->vo_screenheight : vo->dheight;
1316 u_dwidth = use_fs ? opts->vo_screenwidth : vo->dwidth;
1317 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1318 return;
1320 left_ov = (u_dheight - img_height) / 2;
1321 left_ov2 = (u_dwidth - img_width) / 2;
1323 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, 0, u_dwidth, left_ov);
1324 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, u_dheight - left_ov - 1,
1325 u_dwidth, left_ov + 1);
1327 if (u_dwidth > img_width)
1329 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, left_ov, left_ov2,
1330 img_height);
1331 XFillRectangle(mDisplay, vo_window, x11->f_gc, u_dwidth - left_ov2 - 1,
1332 left_ov, left_ov2 + 1, img_height);
1335 XFlush(mDisplay);
1338 void vo_x11_clearwindow(struct vo *vo, Window vo_window)
1340 struct vo_x11_state *x11 = vo->x11;
1341 struct MPOpts *opts = vo->opts;
1342 if (!x11->f_gc)
1343 return;
1344 XFillRectangle(x11->display, vo_window, x11->f_gc, 0, 0,
1345 opts->vo_screenwidth, opts->vo_screenheight);
1347 XFlush(x11->display);
1351 void vo_x11_setlayer(struct vo *vo, Window vo_window, int layer)
1353 struct vo_x11_state *x11 = vo->x11;
1354 if (WinID >= 0)
1355 return;
1357 if (x11->fs_type & vo_wm_LAYER)
1359 XClientMessageEvent xev;
1361 if (!x11->orig_layer)
1362 x11->orig_layer = vo_x11_get_gnome_layer(x11, vo_window);
1364 memset(&xev, 0, sizeof(xev));
1365 xev.type = ClientMessage;
1366 xev.display = x11->display;
1367 xev.window = vo_window;
1368 xev.message_type = x11->XA_WIN_LAYER;
1369 xev.format = 32;
1370 xev.data.l[0] = layer ? fs_layer : x11->orig_layer; // if not fullscreen, stay on default layer
1371 xev.data.l[1] = CurrentTime;
1372 mp_msg(MSGT_VO, MSGL_V,
1373 "[x11] Layered style stay on top (layer %ld).\n",
1374 xev.data.l[0]);
1375 XSendEvent(x11->display, x11->rootwin, False, SubstructureNotifyMask,
1376 (XEvent *) & xev);
1377 } else if (x11->fs_type & vo_wm_NETWM)
1379 XClientMessageEvent xev;
1380 char *state;
1382 memset(&xev, 0, sizeof(xev));
1383 xev.type = ClientMessage;
1384 xev.message_type = x11->XA_NET_WM_STATE;
1385 xev.display = x11->display;
1386 xev.window = vo_window;
1387 xev.format = 32;
1388 xev.data.l[0] = layer;
1390 if (x11->fs_type & vo_wm_STAYS_ON_TOP)
1391 xev.data.l[1] = x11->XA_NET_WM_STATE_STAYS_ON_TOP;
1392 else if (x11->fs_type & vo_wm_ABOVE)
1393 xev.data.l[1] = x11->XA_NET_WM_STATE_ABOVE;
1394 else if (x11->fs_type & vo_wm_FULLSCREEN)
1395 xev.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
1396 else if (x11->fs_type & vo_wm_BELOW)
1397 // This is not fallback. We can safely assume that the situation
1398 // where only NETWM_STATE_BELOW is supported doesn't exist.
1399 xev.data.l[1] = x11->XA_NET_WM_STATE_BELOW;
1401 XSendEvent(x11->display, x11->rootwin, False, SubstructureRedirectMask,
1402 (XEvent *) & xev);
1403 state = XGetAtomName(x11->display, xev.data.l[1]);
1404 mp_msg(MSGT_VO, MSGL_V,
1405 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1406 layer, state);
1407 XFree(state);
1411 static int vo_x11_get_fs_type(int supported)
1413 int i;
1414 int type = supported;
1416 if (vo_fstype_list)
1418 i = 0;
1419 for (i = 0; vo_fstype_list[i]; i++)
1421 int neg = 0;
1422 char *arg = vo_fstype_list[i];
1424 if (vo_fstype_list[i][0] == '-')
1426 neg = 1;
1427 arg = vo_fstype_list[i] + 1;
1430 if (!strncmp(arg, "layer", 5))
1432 if (!neg && (arg[5] == '='))
1434 char *endptr = NULL;
1435 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1437 if (endptr && *endptr == '\0' && layer >= 0
1438 && layer <= 15)
1439 fs_layer = layer;
1441 if (neg)
1442 type &= ~vo_wm_LAYER;
1443 else
1444 type |= vo_wm_LAYER;
1445 } else if (!strcmp(arg, "above"))
1447 if (neg)
1448 type &= ~vo_wm_ABOVE;
1449 else
1450 type |= vo_wm_ABOVE;
1451 } else if (!strcmp(arg, "fullscreen"))
1453 if (neg)
1454 type &= ~vo_wm_FULLSCREEN;
1455 else
1456 type |= vo_wm_FULLSCREEN;
1457 } else if (!strcmp(arg, "stays_on_top"))
1459 if (neg)
1460 type &= ~vo_wm_STAYS_ON_TOP;
1461 else
1462 type |= vo_wm_STAYS_ON_TOP;
1463 } else if (!strcmp(arg, "below"))
1465 if (neg)
1466 type &= ~vo_wm_BELOW;
1467 else
1468 type |= vo_wm_BELOW;
1469 } else if (!strcmp(arg, "netwm"))
1471 if (neg)
1472 type &= ~vo_wm_NETWM;
1473 else
1474 type |= vo_wm_NETWM;
1475 } else if (!strcmp(arg, "none"))
1476 return 0;
1480 return type;
1483 void vo_x11_fullscreen(struct vo *vo)
1485 struct MPOpts *opts = vo->opts;
1486 struct vo_x11_state *x11 = vo->x11;
1487 int x, y, w, h;
1489 if (WinID >= 0 || x11->fs_flip)
1490 return;
1492 if (vo_fs)
1494 // fs->win
1495 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1497 x = x11->vo_old_x;
1498 y = x11->vo_old_y;
1499 w = x11->vo_old_width;
1500 h = x11->vo_old_height;
1503 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1504 vo_fs = VO_FALSE;
1505 } else
1507 // win->fs
1508 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1510 vo_fs = VO_TRUE;
1511 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1513 x11->vo_old_x = vo->dx;
1514 x11->vo_old_y = vo->dy;
1515 x11->vo_old_width = vo->dwidth;
1516 x11->vo_old_height = vo->dheight;
1518 update_xinerama_info(vo);
1519 x = xinerama_x;
1520 y = xinerama_y;
1521 w = opts->vo_screenwidth;
1522 h = opts->vo_screenheight;
1525 long dummy;
1527 XGetWMNormalHints(x11->display, x11->window, &x11->vo_hint, &dummy);
1528 if (!(x11->vo_hint.flags & PWinGravity))
1529 x11->old_gravity = NorthWestGravity;
1530 else
1531 x11->old_gravity = x11->vo_hint.win_gravity;
1533 if (x11->wm_type == 0 && !(vo_fsmode & 16))
1535 XUnmapWindow(x11->display, x11->window); // required for MWM
1536 XWithdrawWindow(x11->display, x11->window, x11->screen);
1537 x11->fs_flip = 1;
1540 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1542 vo_x11_decoration(vo, (vo_fs) ? 0 : 1);
1543 vo_x11_sizehint(vo, x, y, w, h, 0);
1544 vo_x11_setlayer(vo, x11->window, vo_fs);
1547 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1549 /* some WMs lose ontop after fullscreen */
1550 if ((!(vo_fs)) & opts->vo_ontop)
1551 vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1553 XMapRaised(x11->display, x11->window);
1554 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1555 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1556 XRaiseWindow(x11->display, x11->window);
1557 XFlush(x11->display);
1560 void vo_x11_ontop(struct vo *vo)
1562 struct MPOpts *opts = vo->opts;
1563 opts->vo_ontop = !opts->vo_ontop;
1565 vo_x11_setlayer(vo, vo->x11->window, opts->vo_ontop);
1569 * XScreensaver stuff
1572 static int screensaver_off;
1573 static unsigned int time_last;
1575 void xscreensaver_heartbeat(struct vo_x11_state *x11)
1577 unsigned int time = GetTimerMS();
1579 if (x11->display && screensaver_off && (time - time_last) > 30000)
1581 time_last = time;
1583 XResetScreenSaver(x11->display);
1587 static int xss_suspend(Display *mDisplay, Bool suspend)
1589 #ifndef HAVE_XSS
1590 return 0;
1591 #else
1592 int event, error, major, minor;
1593 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1594 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1595 return 0;
1596 if (major < 1 || major == 1 && minor < 1)
1597 return 0;
1598 XScreenSaverSuspend(mDisplay, suspend);
1599 return 1;
1600 #endif
1604 * End of XScreensaver stuff
1607 static void saver_on(Display * mDisplay)
1610 if (!screensaver_off)
1611 return;
1612 screensaver_off = 0;
1613 if (xss_suspend(mDisplay, False))
1614 return;
1615 #ifdef HAVE_XDPMS
1616 if (dpms_disabled)
1618 int nothing;
1619 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1621 if (!DPMSEnable(mDisplay))
1622 { // restoring power saving settings
1623 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1624 } else
1626 // DPMS does not seem to be enabled unless we call DPMSInfo
1627 BOOL onoff;
1628 CARD16 state;
1630 DPMSForceLevel(mDisplay, DPMSModeOn);
1631 DPMSInfo(mDisplay, &state, &onoff);
1632 if (onoff)
1634 mp_msg(MSGT_VO, MSGL_V,
1635 "Successfully enabled DPMS\n");
1636 } else
1638 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1642 dpms_disabled = 0;
1644 #endif
1647 static void saver_off(Display * mDisplay)
1649 int nothing;
1651 if (screensaver_off)
1652 return;
1653 screensaver_off = 1;
1654 if (xss_suspend(mDisplay, True))
1655 return;
1656 #ifdef HAVE_XDPMS
1657 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1659 BOOL onoff;
1660 CARD16 state;
1662 DPMSInfo(mDisplay, &state, &onoff);
1663 if (onoff)
1665 Status stat;
1667 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1668 dpms_disabled = 1;
1669 stat = DPMSDisable(mDisplay); // monitor powersave off
1670 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1673 #endif
1676 static XErrorHandler old_handler = NULL;
1677 static int selectinput_err = 0;
1678 static int x11_selectinput_errorhandler(Display * display,
1679 XErrorEvent * event)
1681 if (event->error_code == BadAccess)
1683 selectinput_err = 1;
1684 mp_msg(MSGT_VO, MSGL_ERR,
1685 "X11 error: BadAccess during XSelectInput Call\n");
1686 mp_msg(MSGT_VO, MSGL_ERR,
1687 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1688 /* If you think MPlayer should shutdown with this error,
1689 * comment out the following line */
1690 return 0;
1692 if (old_handler != NULL)
1693 old_handler(display, event);
1694 else
1695 x11_errorhandler(display, event);
1696 return 0;
1699 void vo_x11_selectinput_witherr(Display * display, Window w,
1700 long event_mask)
1702 XSync(display, False);
1703 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1704 selectinput_err = 0;
1705 if (vo_nomouse_input)
1707 XSelectInput(display, w,
1708 event_mask &
1709 (~(ButtonPressMask | ButtonReleaseMask)));
1710 } else
1712 XSelectInput(display, w, event_mask);
1714 XSync(display, False);
1715 XSetErrorHandler(old_handler);
1716 if (selectinput_err)
1718 mp_msg(MSGT_VO, MSGL_ERR,
1719 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1720 XSelectInput(display, w,
1721 event_mask &
1723 (ButtonPressMask | ButtonReleaseMask |
1724 PointerMotionMask)));
1728 #ifdef HAVE_XF86VM
1729 void vo_vm_switch(struct vo *vo, uint32_t X, uint32_t Y, int *modeline_width,
1730 int *modeline_height)
1732 struct vo_x11_state *x11 = vo->x11;
1733 struct MPOpts *opts = vo->opts;
1734 Display *mDisplay = x11->display;
1735 int vm_event, vm_error;
1736 int vm_ver, vm_rev;
1737 int i, j, have_vm = 0;
1739 int modecount;
1741 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1743 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1744 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1745 vm_rev);
1746 have_vm = 1;
1747 } else
1748 mp_msg(MSGT_VO, MSGL_WARN,
1749 "XF86VidMode extension not available.\n");
1751 if (have_vm)
1753 if (vidmodes == NULL)
1754 XF86VidModeGetAllModeLines(mDisplay, x11->screen, &modecount,
1755 &vidmodes);
1756 j = 0;
1757 *modeline_width = vidmodes[0]->hdisplay;
1758 *modeline_height = vidmodes[0]->vdisplay;
1760 for (i = 1; i < modecount; i++)
1761 if ((vidmodes[i]->hdisplay >= X)
1762 && (vidmodes[i]->vdisplay >= Y))
1763 if ((vidmodes[i]->hdisplay <= *modeline_width)
1764 && (vidmodes[i]->vdisplay <= *modeline_height))
1766 *modeline_width = vidmodes[i]->hdisplay;
1767 *modeline_height = vidmodes[i]->vdisplay;
1768 j = i;
1771 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_SelectedVideoMode,
1772 *modeline_width, *modeline_height, X, Y);
1773 XF86VidModeLockModeSwitch(mDisplay, x11->screen, 0);
1774 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1775 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1776 X = (opts->vo_screenwidth - *modeline_width) / 2;
1777 Y = (opts->vo_screenheight - *modeline_height) / 2;
1778 XF86VidModeSetViewPort(mDisplay, x11->screen, X, Y);
1782 void vo_vm_close(struct vo *vo)
1784 Display *dpy = vo->x11->display;
1785 struct MPOpts *opts = vo->opts;
1786 #ifdef HAVE_NEW_GUI
1787 if (vidmodes != NULL && vo->x11->vo_window != None)
1788 #else
1789 if (vidmodes != NULL)
1790 #endif
1792 int i, modecount;
1793 int screen;
1795 screen = DefaultScreen(dpy);
1797 free(vidmodes);
1798 vidmodes = NULL;
1799 XF86VidModeGetAllModeLines(dpy, vo->x11->screen, &modecount,
1800 &vidmodes);
1801 for (i = 0; i < modecount; i++)
1802 if ((vidmodes[i]->hdisplay == opts->vo_screenwidth)
1803 && (vidmodes[i]->vdisplay == opts->vo_screenheight))
1805 mp_msg(MSGT_VO, MSGL_INFO,
1806 "Returning to original mode %dx%d\n",
1807 opts->vo_screenwidth, opts->vo_screenheight);
1808 break;
1811 XF86VidModeSwitchToMode(dpy, screen, vidmodes[i]);
1812 XF86VidModeSwitchToMode(dpy, screen, vidmodes[i]);
1813 free(vidmodes);
1814 vidmodes = NULL;
1817 #endif
1819 #endif /* X11_FULLSCREEN */
1823 * Scan the available visuals on this Display/Screen. Try to find
1824 * the 'best' available TrueColor visual that has a decent color
1825 * depth (at least 15bit). If there are multiple visuals with depth
1826 * >= 15bit, we prefer visuals with a smaller color depth.
1828 int vo_find_depth_from_visuals(Display * dpy, int screen,
1829 Visual ** visual_return)
1831 XVisualInfo visual_tmpl;
1832 XVisualInfo *visuals;
1833 int nvisuals, i;
1834 int bestvisual = -1;
1835 int bestvisual_depth = -1;
1837 visual_tmpl.screen = screen;
1838 visual_tmpl.class = TrueColor;
1839 visuals = XGetVisualInfo(dpy,
1840 VisualScreenMask | VisualClassMask,
1841 &visual_tmpl, &nvisuals);
1842 if (visuals != NULL)
1844 for (i = 0; i < nvisuals; i++)
1846 mp_msg(MSGT_VO, MSGL_V,
1847 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1848 visuals[i].visualid, visuals[i].depth,
1849 visuals[i].red_mask, visuals[i].green_mask,
1850 visuals[i].blue_mask);
1852 * Save the visual index and its depth, if this is the first
1853 * truecolor visul, or a visual that is 'preferred' over the
1854 * previous 'best' visual.
1856 if (bestvisual_depth == -1
1857 || (visuals[i].depth >= 15
1858 && (visuals[i].depth < bestvisual_depth
1859 || bestvisual_depth < 15)))
1861 bestvisual = i;
1862 bestvisual_depth = visuals[i].depth;
1866 if (bestvisual != -1 && visual_return != NULL)
1867 *visual_return = visuals[bestvisual].visual;
1869 XFree(visuals);
1871 return bestvisual_depth;
1875 static Colormap cmap = None;
1876 static XColor cols[256];
1877 static int cm_size, red_mask, green_mask, blue_mask;
1880 Colormap vo_x11_create_colormap(struct vo *vo, XVisualInfo *vinfo)
1882 struct vo_x11_state *x11 = vo->x11;
1883 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1885 if (vinfo->class != DirectColor)
1886 return XCreateColormap(x11->display, x11->rootwin, vinfo->visual,
1887 AllocNone);
1889 /* can this function get called twice or more? */
1890 if (cmap)
1891 return cmap;
1892 cm_size = vinfo->colormap_size;
1893 red_mask = vinfo->red_mask;
1894 green_mask = vinfo->green_mask;
1895 blue_mask = vinfo->blue_mask;
1896 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1897 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1898 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1899 rvu = 65536ull * ru / (red_mask + ru);
1900 gvu = 65536ull * gu / (green_mask + gu);
1901 bvu = 65536ull * bu / (blue_mask + bu);
1902 r = g = b = 0;
1903 rv = gv = bv = 0;
1904 m = DoRed | DoGreen | DoBlue;
1905 for (k = 0; k < cm_size; k++)
1907 int t;
1909 cols[k].pixel = r | g | b;
1910 cols[k].red = rv;
1911 cols[k].green = gv;
1912 cols[k].blue = bv;
1913 cols[k].flags = m;
1914 t = (r + ru) & red_mask;
1915 if (t < r)
1916 m &= ~DoRed;
1917 r = t;
1918 t = (g + gu) & green_mask;
1919 if (t < g)
1920 m &= ~DoGreen;
1921 g = t;
1922 t = (b + bu) & blue_mask;
1923 if (t < b)
1924 m &= ~DoBlue;
1925 b = t;
1926 rv += rvu;
1927 gv += gvu;
1928 bv += bvu;
1930 cmap = XCreateColormap(x11->display, x11->rootwin, vinfo->visual, AllocAll);
1931 XStoreColors(x11->display, cmap, cols, cm_size);
1932 return cmap;
1936 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1937 * hue and red/green/blue intensity, but we cannot do saturation.
1938 * Currently only gamma, brightness and contrast are implemented.
1939 * Is there sufficient interest for hue and/or red/green/blue intensity?
1941 /* these values have range [-100,100] and are initially 0 */
1942 static int vo_gamma = 0;
1943 static int vo_brightness = 0;
1944 static int vo_contrast = 0;
1946 static int transform_color(float val,
1947 float brightness, float contrast, float gamma) {
1948 float s = pow(val, gamma);
1949 s = (s - 0.5) * contrast + 0.5;
1950 s += brightness;
1951 if (s < 0)
1952 s = 0;
1953 if (s > 1)
1954 s = 1;
1955 return (unsigned short) (s * 65535);
1958 uint32_t vo_x11_set_equalizer(struct vo *vo, char *name, int value)
1960 float gamma, brightness, contrast;
1961 float rf, gf, bf;
1962 int k;
1965 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1966 * of TrueColor-ed window but be careful:
1967 * Unlike the colormaps, which are private for the X client
1968 * who created them and thus automatically destroyed on client
1969 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1970 * setting and _must_ be restored before the process exits.
1971 * Unforunately when the process crashes (or gets killed
1972 * for some reason) it is impossible to restore the setting,
1973 * and such behaviour could be rather annoying for the users.
1975 if (cmap == None)
1976 return VO_NOTAVAIL;
1978 if (!strcasecmp(name, "brightness"))
1979 vo_brightness = value;
1980 else if (!strcasecmp(name, "contrast"))
1981 vo_contrast = value;
1982 else if (!strcasecmp(name, "gamma"))
1983 vo_gamma = value;
1984 else
1985 return VO_NOTIMPL;
1987 brightness = 0.01 * vo_brightness;
1988 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1989 gamma = pow(2, -0.02 * vo_gamma);
1991 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1992 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
1993 green_mask;
1994 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
1996 /* now recalculate the colormap using the newly set value */
1997 for (k = 0; k < cm_size; k++)
1999 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
2000 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
2001 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
2004 XStoreColors(vo->x11->display, cmap, cols, cm_size);
2005 XFlush(vo->x11->display);
2006 return VO_TRUE;
2009 uint32_t vo_x11_get_equalizer(char *name, int *value)
2011 if (cmap == None)
2012 return VO_NOTAVAIL;
2013 if (!strcasecmp(name, "brightness"))
2014 *value = vo_brightness;
2015 else if (!strcasecmp(name, "contrast"))
2016 *value = vo_contrast;
2017 else if (!strcasecmp(name, "gamma"))
2018 *value = vo_gamma;
2019 else
2020 return VO_NOTIMPL;
2021 return VO_TRUE;
2024 #ifdef HAVE_XV
2025 int vo_xv_set_eq(struct vo *vo, uint32_t xv_port, char *name, int value)
2027 XvAttribute *attributes;
2028 int i, howmany, xv_atom;
2030 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
2032 /* get available attributes */
2033 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
2034 for (i = 0; i < howmany && attributes; i++)
2035 if (attributes[i].flags & XvSettable)
2037 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
2038 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2039 then trigger it if it's ok so that the other values are at default upon query */
2040 if (xv_atom != None)
2042 int hue = 0, port_value, port_min, port_max;
2044 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2045 (!strcasecmp(name, "brightness")))
2046 port_value = value;
2047 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2048 (!strcasecmp(name, "contrast")))
2049 port_value = value;
2050 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2051 (!strcasecmp(name, "saturation")))
2052 port_value = value;
2053 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2054 (!strcasecmp(name, "hue")))
2056 port_value = value;
2057 hue = 1;
2058 } else
2059 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2060 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2061 (!strcasecmp(name, "red_intensity")))
2062 port_value = value;
2063 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2064 && (!strcasecmp(name, "green_intensity")))
2065 port_value = value;
2066 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2067 && (!strcasecmp(name, "blue_intensity")))
2068 port_value = value;
2069 else
2070 continue;
2072 port_min = attributes[i].min_value;
2073 port_max = attributes[i].max_value;
2075 /* nvidia hue workaround */
2076 if (hue && port_min == 0 && port_max == 360)
2078 port_value =
2079 (port_value >=
2080 0) ? (port_value - 100) : (port_value + 100);
2082 // -100 -> min
2083 // 0 -> (max+min)/2
2084 // +100 -> max
2085 port_value =
2086 (port_value + 100) * (port_max - port_min) / 200 +
2087 port_min;
2088 XvSetPortAttribute(vo->x11->display, xv_port, xv_atom, port_value);
2089 return (VO_TRUE);
2092 return (VO_FALSE);
2095 int vo_xv_get_eq(struct vo *vo, uint32_t xv_port, char *name, int *value)
2098 XvAttribute *attributes;
2099 int i, howmany, xv_atom;
2101 /* get available attributes */
2102 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
2103 for (i = 0; i < howmany && attributes; i++)
2104 if (attributes[i].flags & XvGettable)
2106 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
2107 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2108 then trigger it if it's ok so that the other values are at default upon query */
2109 if (xv_atom != None)
2111 int val, port_value = 0, port_min, port_max;
2113 XvGetPortAttribute(vo->x11->display, xv_port, xv_atom,
2114 &port_value);
2116 port_min = attributes[i].min_value;
2117 port_max = attributes[i].max_value;
2118 val =
2119 (port_value - port_min) * 200 / (port_max - port_min) -
2120 100;
2122 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2123 (!strcasecmp(name, "brightness")))
2124 *value = val;
2125 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2126 (!strcasecmp(name, "contrast")))
2127 *value = val;
2128 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2129 (!strcasecmp(name, "saturation")))
2130 *value = val;
2131 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2132 (!strcasecmp(name, "hue")))
2134 /* nasty nvidia detect */
2135 if (port_min == 0 && port_max == 360)
2136 *value = (val >= 0) ? (val - 100) : (val + 100);
2137 else
2138 *value = val;
2139 } else
2140 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2141 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2142 (!strcasecmp(name, "red_intensity")))
2143 *value = val;
2144 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2145 && (!strcasecmp(name, "green_intensity")))
2146 *value = val;
2147 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2148 && (!strcasecmp(name, "blue_intensity")))
2149 *value = val;
2150 else
2151 continue;
2153 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2154 name, *value);
2155 return (VO_TRUE);
2158 return (VO_FALSE);
2162 * \brief Interns the requested atom if it is available.
2164 * \param atom_name String containing the name of the requested atom.
2166 * \return Returns the atom if available, else None is returned.
2169 static Atom xv_intern_atom_if_exists(struct vo_x11_state *x11,
2170 char const *atom_name)
2172 XvAttribute * attributes;
2173 int attrib_count,i;
2174 Atom xv_atom = None;
2176 attributes = XvQueryPortAttributes(x11->display, x11->xv_port, &attrib_count );
2177 if( attributes!=NULL )
2179 for ( i = 0; i < attrib_count; ++i )
2181 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2183 xv_atom = XInternAtom(x11->display, atom_name, False );
2184 break; // found what we want, break out
2187 XFree( attributes );
2190 return xv_atom;
2194 * \brief Try to enable vsync for xv.
2195 * \return Returns -1 if not available, 0 on failure and 1 on success.
2197 int vo_xv_enable_vsync(struct vo *vo)
2199 struct vo_x11_state *x11 = vo->x11;
2200 Atom xv_atom = xv_intern_atom_if_exists(x11, "XV_SYNC_TO_VBLANK");
2201 if (xv_atom == None)
2202 return -1;
2203 return XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1) == Success;
2207 * \brief Get maximum supported source image dimensions.
2209 * This function does not set the variables pointed to by
2210 * width and height if the information could not be retrieved,
2211 * so the caller is reponsible for properly initializing them.
2213 * \param width [out] The maximum width gets stored here.
2214 * \param height [out] The maximum height gets stored here.
2217 void vo_xv_get_max_img_dim(struct vo *vo, uint32_t * width, uint32_t * height)
2219 struct vo_x11_state *x11 = vo->x11;
2220 XvEncodingInfo * encodings;
2221 //unsigned long num_encodings, idx; to int or too long?!
2222 unsigned int num_encodings, idx;
2224 XvQueryEncodings(x11->display, x11->xv_port, &num_encodings, &encodings);
2226 if ( encodings )
2228 for ( idx = 0; idx < num_encodings; ++idx )
2230 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2232 *width = encodings[idx].width;
2233 *height = encodings[idx].height;
2234 break;
2239 mp_msg( MSGT_VO, MSGL_V,
2240 "[xv common] Maximum source image dimensions: %ux%u\n",
2241 *width, *height );
2243 XvFreeEncodingInfo( encodings );
2247 * \brief Print information about the colorkey method and source.
2249 * \param ck_handling Integer value containing the information about
2250 * colorkey handling (see x11_common.h).
2252 * Outputs the content of |ck_handling| as a readable message.
2255 static void vo_xv_print_ck_info(struct vo_x11_state *x11)
2257 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2259 switch ( x11->xv_ck_info.method )
2261 case CK_METHOD_NONE:
2262 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2263 case CK_METHOD_AUTOPAINT:
2264 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2265 case CK_METHOD_MANUALFILL:
2266 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2267 case CK_METHOD_BACKGROUND:
2268 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2271 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2273 switch ( x11->xv_ck_info.source )
2275 case CK_SRC_CUR:
2276 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2277 x11->xv_colorkey );
2278 break;
2279 case CK_SRC_USE:
2280 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2282 mp_msg( MSGT_VO, MSGL_V,
2283 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2284 x11->xv_colorkey );
2286 else
2288 mp_msg( MSGT_VO, MSGL_V,
2289 "Using colorkey from MPlayer (0x%06lx)."
2290 " Use -colorkey to change.\n",
2291 x11->xv_colorkey );
2293 break;
2294 case CK_SRC_SET:
2295 mp_msg( MSGT_VO, MSGL_V,
2296 "Setting and using colorkey from MPlayer (0x%06lx)."
2297 " Use -colorkey to change.\n",
2298 x11->xv_colorkey );
2299 break;
2303 * \brief Init colorkey depending on the settings in xv_ck_info.
2305 * \return Returns 0 on failure and 1 on success.
2307 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2308 * flags in xv_ck_info.
2310 * Possiblilities:
2311 * * Methods
2312 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2313 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2314 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2315 * * Sources
2316 * - use currently set colorkey ( CK_SRC_CUR )
2317 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2318 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2320 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2321 * we don't draw anything as this means it was forced to off.
2323 int vo_xv_init_colorkey(struct vo *vo)
2325 struct vo_x11_state *x11 = vo->x11;
2326 Atom xv_atom;
2327 int rez;
2329 /* check if colorkeying is needed */
2330 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_COLORKEY");
2332 /* if we have to deal with colorkeying ... */
2333 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2335 /* check if we should use the colorkey specified in vo_colorkey */
2336 if ( x11->xv_ck_info.source != CK_SRC_CUR )
2338 x11->xv_colorkey = vo_colorkey;
2340 /* check if we have to set the colorkey too */
2341 if ( x11->xv_ck_info.source == CK_SRC_SET )
2343 xv_atom = XInternAtom(x11->display, "XV_COLORKEY",False);
2345 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, vo_colorkey);
2346 if ( rez != Success )
2348 mp_msg( MSGT_VO, MSGL_FATAL,
2349 "[xv common] Couldn't set colorkey!\n" );
2350 return 0; // error setting colorkey
2354 else
2356 int colorkey_ret;
2358 rez=XvGetPortAttribute(x11->display,x11->xv_port, xv_atom, &colorkey_ret);
2359 if ( rez == Success )
2361 x11->xv_colorkey = colorkey_ret;
2363 else
2365 mp_msg( MSGT_VO, MSGL_FATAL,
2366 "[xv common] Couldn't get colorkey!"
2367 "Maybe the selected Xv port has no overlay.\n" );
2368 return 0; // error getting colorkey
2372 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_AUTOPAINT_COLORKEY");
2374 /* should we draw the colorkey ourselves or activate autopainting? */
2375 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2377 rez = !Success; // reset rez to something different than Success
2379 if ( xv_atom != None ) // autopaint is supported
2381 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1);
2384 if ( rez != Success )
2386 // fallback to manual colorkey drawing
2387 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2390 else // disable colorkey autopainting if supported
2392 if ( xv_atom != None ) // we have autopaint attribute
2394 XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 0);
2398 else // do no colorkey drawing at all
2400 x11->xv_ck_info.method = CK_METHOD_NONE;
2401 } /* end: should we draw colorkey */
2403 /* output information about the current colorkey settings */
2404 vo_xv_print_ck_info(x11);
2406 return 1; // success
2410 * \brief Draw the colorkey on the video window.
2412 * Draws the colorkey depending on the set method ( colorkey_handling ).
2414 * Also draws the black bars ( when the video doesn't fit the display in
2415 * fullscreen ) separately, so they don't overlap with the video area.
2416 * It doesn't call XFlush.
2419 void vo_xv_draw_colorkey(struct vo *vo, int32_t x, int32_t y,
2420 int32_t w, int32_t h)
2422 struct MPOpts *opts = vo->opts;
2423 struct vo_x11_state *x11 = vo->x11;
2424 if( x11->xv_ck_info.method == CK_METHOD_MANUALFILL ||
2425 x11->xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2427 XSetForeground(x11->display, x11->vo_gc, x11->xv_colorkey );
2428 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2429 x, y,
2430 w, h );
2433 /* draw black bars if needed */
2434 /* TODO! move this to vo_x11_clearwindow_part() */
2435 if ( vo_fs )
2437 XSetForeground(x11->display, x11->vo_gc, 0 );
2438 /* making non-overlap fills, requires 8 checks instead of 4 */
2439 if ( y > 0 )
2440 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2441 0, 0,
2442 opts->vo_screenwidth, y);
2443 if (x > 0)
2444 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2445 0, 0,
2446 x, opts->vo_screenheight);
2447 if (x + w < opts->vo_screenwidth)
2448 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2449 x + w, 0,
2450 opts->vo_screenwidth, opts->vo_screenheight);
2451 if (y + h < opts->vo_screenheight)
2452 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2453 0, y + h,
2454 opts->vo_screenwidth, opts->vo_screenheight);
2458 /** \brief Tests if a valid argument for the ck suboption was given. */
2459 int xv_test_ck( void * arg )
2461 strarg_t * strarg = (strarg_t *)arg;
2463 if ( strargcmp( strarg, "use" ) == 0 ||
2464 strargcmp( strarg, "set" ) == 0 ||
2465 strargcmp( strarg, "cur" ) == 0 )
2467 return 1;
2470 return 0;
2472 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2473 int xv_test_ckm( void * arg )
2475 strarg_t * strarg = (strarg_t *)arg;
2477 if ( strargcmp( strarg, "bg" ) == 0 ||
2478 strargcmp( strarg, "man" ) == 0 ||
2479 strargcmp( strarg, "auto" ) == 0 )
2481 return 1;
2484 return 0;
2488 * \brief Modify the colorkey_handling var according to str
2490 * Checks if a valid pointer ( not NULL ) to the string
2491 * was given. And in that case modifies the colorkey_handling
2492 * var to reflect the requested behaviour.
2493 * If nothing happens the content of colorkey_handling stays
2494 * the same.
2496 * \param str Pointer to the string or NULL
2499 void xv_setup_colorkeyhandling(struct vo *vo, const char *ck_method_str,
2500 const char *ck_str)
2502 struct vo_x11_state *x11 = vo->x11;
2503 /* check if a valid pointer to the string was passed */
2504 if ( ck_str )
2506 if ( strncmp( ck_str, "use", 3 ) == 0 )
2508 x11->xv_ck_info.source = CK_SRC_USE;
2510 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2512 x11->xv_ck_info.source = CK_SRC_SET;
2515 /* check if a valid pointer to the string was passed */
2516 if ( ck_method_str )
2518 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2520 x11->xv_ck_info.method = CK_METHOD_BACKGROUND;
2522 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2524 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2526 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2528 x11->xv_ck_info.method = CK_METHOD_AUTOPAINT;
2533 #endif
2535 void vo_x11_init_state(struct vo_x11_state *s)
2537 *s = (struct vo_x11_state){
2538 .xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR },
2539 .olddecor = MWM_DECOR_ALL,
2540 .oldfuncs = MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
2541 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE,
2542 .old_gravity = NorthWestGravity,