libvo: Move aspect.c static variables to vo struct
[mplayer/glamo.git] / libvo / x11_common.c
blob6fefae61f3988f2d8bece64e89a2e720f7ace014
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"
12 #include "talloc.h"
14 #ifdef X11_FULLSCREEN
16 #include <string.h>
17 #include <unistd.h>
18 #include <assert.h>
20 #include "video_out.h"
21 #include "aspect.h"
22 #include "geometry.h"
23 #include "help_mp.h"
24 #include "osdep/timer.h"
26 #include <X11/Xmd.h>
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
29 #include <X11/Xatom.h>
31 #ifdef HAVE_XSS
32 #include <X11/extensions/scrnsaver.h>
33 #endif
35 #ifdef HAVE_XDPMS
36 #include <X11/extensions/dpms.h>
37 #endif
39 #ifdef HAVE_XINERAMA
40 #include <X11/extensions/Xinerama.h>
41 #endif
43 #ifdef HAVE_XF86VM
44 #include <X11/extensions/xf86vmode.h>
45 #endif
47 #ifdef HAVE_XF86XK
48 #include <X11/XF86keysym.h>
49 #endif
51 #ifdef HAVE_XV
52 #include <X11/extensions/Xv.h>
53 #include <X11/extensions/Xvlib.h>
55 #include "subopt-helper.h"
56 #endif
58 #include "input/input.h"
59 #include "input/mouse.h"
61 #ifdef HAVE_NEW_GUI
62 #include "gui/interface.h"
63 #include "mplayer.h"
64 #endif
66 #define WIN_LAYER_ONBOTTOM 2
67 #define WIN_LAYER_NORMAL 4
68 #define WIN_LAYER_ONTOP 6
69 #define WIN_LAYER_ABOVE_DOCK 10
71 extern int enable_mouse_movements;
72 int fs_layer = WIN_LAYER_ABOVE_DOCK;
74 int stop_xscreensaver = 0;
76 static int dpms_disabled = 0;
78 char *mDisplayName = NULL;
80 char **vo_fstype_list;
82 /* 1 means that the WM is metacity (broken as hell) */
83 int metacity_hack = 0;
85 #ifdef HAVE_XF86VM
86 XF86VidModeModeInfo **vidmodes = NULL;
87 XF86VidModeModeLine modeline;
88 #endif
90 static int vo_x11_get_fs_type(int supported);
91 static void saver_off(Display *);
92 static void saver_on(Display *);
95 * Sends the EWMH fullscreen state event.
97 * action: could be one of _NET_WM_STATE_REMOVE -- remove state
98 * _NET_WM_STATE_ADD -- add state
99 * _NET_WM_STATE_TOGGLE -- toggle
101 void vo_x11_ewmh_fullscreen(struct vo_x11_state *x11, int action)
103 assert(action == _NET_WM_STATE_REMOVE ||
104 action == _NET_WM_STATE_ADD || action == _NET_WM_STATE_TOGGLE);
106 if (x11->fs_type & vo_wm_FULLSCREEN)
108 XEvent xev;
110 /* init X event structure for _NET_WM_FULLSCREEN client message */
111 xev.xclient.type = ClientMessage;
112 xev.xclient.serial = 0;
113 xev.xclient.send_event = True;
114 xev.xclient.message_type = XInternAtom(x11->display,
115 "_NET_WM_STATE", False);
116 xev.xclient.window = x11->window;
117 xev.xclient.format = 32;
118 xev.xclient.data.l[0] = action;
119 xev.xclient.data.l[1] = XInternAtom(x11->display,
120 "_NET_WM_STATE_FULLSCREEN",
121 False);
122 xev.xclient.data.l[2] = 0;
123 xev.xclient.data.l[3] = 0;
124 xev.xclient.data.l[4] = 0;
126 /* finally send that damn thing */
127 if (!XSendEvent(x11->display, DefaultRootWindow(x11->display), False,
128 SubstructureRedirectMask | SubstructureNotifyMask,
129 &xev))
131 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_EwmhFullscreenStateFailed);
136 static void vo_hidecursor(Display * disp, Window win)
138 Cursor no_ptr;
139 Pixmap bm_no;
140 XColor black, dummy;
141 Colormap colormap;
142 const char bm_no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
144 if (WinID == 0)
145 return; // do not hide if playing on the root window
147 colormap = DefaultColormap(disp, DefaultScreen(disp));
148 if ( !XAllocNamedColor(disp, colormap, "black", &black, &dummy) )
150 return; // color alloc failed, give up
152 bm_no = XCreateBitmapFromData(disp, win, bm_no_data, 8, 8);
153 no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
154 XDefineCursor(disp, win, no_ptr);
155 XFreeCursor(disp, no_ptr);
156 if (bm_no != None)
157 XFreePixmap(disp, bm_no);
158 XFreeColors(disp,colormap,&black.pixel,1,0);
161 static void vo_showcursor(Display * disp, Window win)
163 if (WinID == 0)
164 return;
165 XDefineCursor(disp, win, 0);
168 static int x11_errorhandler(Display * display, XErrorEvent * event)
170 #define MSGLEN 60
171 char msg[MSGLEN];
173 XGetErrorText(display, event->error_code, (char *) &msg, MSGLEN);
175 mp_msg(MSGT_VO, MSGL_ERR, "X11 error: %s\n", msg);
177 mp_msg(MSGT_VO, MSGL_V,
178 "Type: %x, display: %p, resourceid: %lx, serial: %lx\n",
179 event->type, event->display, event->resourceid, event->serial);
180 mp_msg(MSGT_VO, MSGL_V,
181 "Error code: %x, request code: %x, minor code: %x\n",
182 event->error_code, event->request_code, event->minor_code);
184 // abort();
185 //exit_player("X11 error");
186 return 0;
187 #undef MSGLEN
190 void fstype_help(void)
192 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_AvailableFsType);
193 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FULL_SCREEN_TYPES\n");
195 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "none",
196 "don't set fullscreen window layer");
197 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer",
198 "use _WIN_LAYER hint with default layer");
199 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer=<0..15>",
200 "use _WIN_LAYER hint with a given layer number");
201 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "netwm",
202 "force NETWM style");
203 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "above",
204 "use _NETWM_STATE_ABOVE hint if available");
205 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "below",
206 "use _NETWM_STATE_BELOW hint if available");
207 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "fullscreen",
208 "use _NETWM_STATE_FULLSCREEN hint if availale");
209 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "stays_on_top",
210 "use _NETWM_STATE_STAYS_ON_TOP hint if available");
211 mp_msg(MSGT_VO, MSGL_INFO,
212 "You can also negate the settings with simply putting '-' in the beginning");
213 mp_msg(MSGT_VO, MSGL_INFO, "\n");
216 static void fstype_dump(int fstype)
218 if (fstype)
220 mp_msg(MSGT_VO, MSGL_V, "[x11] Current fstype setting honours");
221 if (fstype & vo_wm_LAYER)
222 mp_msg(MSGT_VO, MSGL_V, " LAYER");
223 if (fstype & vo_wm_FULLSCREEN)
224 mp_msg(MSGT_VO, MSGL_V, " FULLSCREEN");
225 if (fstype & vo_wm_STAYS_ON_TOP)
226 mp_msg(MSGT_VO, MSGL_V, " STAYS_ON_TOP");
227 if (fstype & vo_wm_ABOVE)
228 mp_msg(MSGT_VO, MSGL_V, " ABOVE");
229 if (fstype & vo_wm_BELOW)
230 mp_msg(MSGT_VO, MSGL_V, " BELOW");
231 mp_msg(MSGT_VO, MSGL_V, " X atoms\n");
232 } else
233 mp_msg(MSGT_VO, MSGL_V,
234 "[x11] Current fstype setting doesn't honour any X atoms\n");
237 static int net_wm_support_state_test(struct vo_x11_state *x11, Atom atom)
239 #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; } }
241 NET_WM_STATE_TEST(FULLSCREEN);
242 NET_WM_STATE_TEST(ABOVE);
243 NET_WM_STATE_TEST(STAYS_ON_TOP);
244 NET_WM_STATE_TEST(BELOW);
245 return 0;
248 static int x11_get_property(struct vo_x11_state *x11, Atom type, Atom ** args,
249 unsigned long *nitems)
251 int format;
252 unsigned long bytesafter;
254 return (Success ==
255 XGetWindowProperty(x11->display, x11->rootwin, type, 0, 16384, False,
256 AnyPropertyType, &type, &format, nitems,
257 &bytesafter, (unsigned char **) args)
258 && *nitems > 0);
261 static int vo_wm_detect(struct vo *vo)
263 struct vo_x11_state *x11 = vo->x11;
264 int i;
265 int wm = 0;
266 unsigned long nitems;
267 Atom *args = NULL;
269 if (WinID >= 0)
270 return 0;
272 // -- supports layers
273 if (x11_get_property(x11, x11->XA_WIN_PROTOCOLS, &args, &nitems))
275 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports layers.\n");
276 for (i = 0; i < nitems; i++)
278 if (args[i] == x11->XA_WIN_LAYER)
280 wm |= vo_wm_LAYER;
281 metacity_hack |= 1;
282 } else
283 /* metacity is the only window manager I know which reports
284 * supporting only the _WIN_LAYER hint in _WIN_PROTOCOLS.
285 * (what's more support for it is broken) */
286 metacity_hack |= 2;
288 XFree(args);
289 if (wm && (metacity_hack == 1))
291 // metacity claims to support layers, but it is not the truth :-)
292 wm ^= vo_wm_LAYER;
293 mp_msg(MSGT_VO, MSGL_V,
294 "[x11] Using workaround for Metacity bugs.\n");
297 // --- netwm
298 if (x11_get_property(x11, x11->XA_NET_SUPPORTED, &args, &nitems))
300 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports NetWM.\n");
301 for (i = 0; i < nitems; i++)
302 wm |= net_wm_support_state_test(vo->x11, args[i]);
303 XFree(args);
304 #if 0
305 // ugly hack for broken OpenBox _NET_WM_STATE_FULLSCREEN support
306 // (in their implementation it only changes internal window state, nothing more!!!)
307 if (wm & vo_wm_FULLSCREEN)
309 if (x11_get_property(x11, x11->XA_BLACKBOX_PID, &args, &nitems))
311 mp_msg(MSGT_VO, MSGL_V,
312 "[x11] Detected wm is a broken OpenBox.\n");
313 wm ^= vo_wm_FULLSCREEN;
315 XFree(args);
317 #endif
320 if (wm == 0)
321 mp_msg(MSGT_VO, MSGL_V, "[x11] Unknown wm type...\n");
322 return wm;
325 #define XA_INIT(x) x11->XA##x = XInternAtom(x11->display, #x, False)
326 static void init_atoms(struct vo_x11_state *x11)
328 XA_INIT(_NET_SUPPORTED);
329 XA_INIT(_NET_WM_STATE);
330 XA_INIT(_NET_WM_STATE_FULLSCREEN);
331 XA_INIT(_NET_WM_STATE_ABOVE);
332 XA_INIT(_NET_WM_STATE_STAYS_ON_TOP);
333 XA_INIT(_NET_WM_STATE_BELOW);
334 XA_INIT(_NET_WM_PID);
335 XA_INIT(_WIN_PROTOCOLS);
336 XA_INIT(_WIN_LAYER);
337 XA_INIT(_WIN_HINTS);
338 XA_INIT(_BLACKBOX_PID);
339 XA_INIT(WM_PROTOCOLS);
340 XA_INIT(WM_DELETE_WINDOW);
343 void update_xinerama_info(struct vo *vo) {
344 struct MPOpts *opts = vo->opts;
345 int screen = xinerama_screen;
346 xinerama_x = xinerama_y = 0;
347 #ifdef HAVE_XINERAMA
348 if (screen >= -1 && XineramaIsActive(vo->x11->display))
350 XineramaScreenInfo *screens;
351 int num_screens;
353 screens = XineramaQueryScreens(vo->x11->display, &num_screens);
354 if (screen >= num_screens)
355 screen = num_screens - 1;
356 if (screen == -1) {
357 int x = vo->dx + vo->dwidth / 2;
358 int y = vo->dy + vo->dheight / 2;
359 for (screen = num_screens - 1; screen > 0; screen--) {
360 int left = screens[screen].x_org;
361 int right = left + screens[screen].width;
362 int top = screens[screen].y_org;
363 int bottom = top + screens[screen].height;
364 if (left <= x && x <= right && top <= y && y <= bottom)
365 break;
368 if (screen < 0)
369 screen = 0;
370 opts->vo_screenwidth = screens[screen].width;
371 opts->vo_screenheight = screens[screen].height;
372 xinerama_x = screens[screen].x_org;
373 xinerama_y = screens[screen].y_org;
375 XFree(screens);
377 #endif
378 aspect_save_screenres(vo, opts->vo_screenwidth, opts->vo_screenheight);
381 int vo_init(struct vo *vo)
383 struct MPOpts *opts = vo->opts;
384 struct vo_x11_state *x11 = vo->x11;
385 // int mScreen;
386 int depth, bpp;
387 unsigned int mask;
389 // char * DisplayName = ":0.0";
390 // Display * mDisplay;
391 XImage *mXImage = NULL;
393 // Window mRootWin;
394 XWindowAttributes attribs;
395 char *dispName;
397 if (vo_rootwin)
398 WinID = 0; // use root window
400 if (x11->depthonscreen)
402 saver_off(x11->display);
403 return 1; // already called
406 XSetErrorHandler(x11_errorhandler);
408 #if 0
409 if (!mDisplayName)
410 if (!(mDisplayName = getenv("DISPLAY")))
411 mDisplayName = strdup(":0.0");
412 #else
413 dispName = XDisplayName(mDisplayName);
414 #endif
416 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
418 x11->display = XOpenDisplay(dispName);
419 if (!x11->display)
421 mp_msg(MSGT_VO, MSGL_ERR,
422 "vo: couldn't open the X11 display (%s)!\n", dispName);
423 return 0;
425 x11->screen = DefaultScreen(x11->display); // screen ID
426 x11->rootwin = RootWindow(x11->display, x11->screen); // root window ID
428 init_atoms(vo->x11);
430 #ifdef HAVE_XF86VM
432 int clock;
434 XF86VidModeGetModeLine(x11->display, x11->screen, &clock, &modeline);
435 if (!opts->vo_screenwidth)
436 opts->vo_screenwidth = modeline.hdisplay;
437 if (!opts->vo_screenheight)
438 opts->vo_screenheight = modeline.vdisplay;
440 #endif
442 if (!opts->vo_screenwidth)
443 opts->vo_screenwidth = DisplayWidth(x11->display, x11->screen);
444 if (!opts->vo_screenheight)
445 opts->vo_screenheight = DisplayHeight(x11->display, x11->screen);
447 // get color depth (from root window, or the best visual):
448 XGetWindowAttributes(x11->display, x11->rootwin, &attribs);
449 depth = attribs.depth;
451 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
453 Visual *visual;
455 depth = vo_find_depth_from_visuals(x11->display, x11->screen, &visual);
456 if (depth != -1)
457 mXImage = XCreateImage(x11->display, visual, depth, ZPixmap,
458 0, NULL, 1, 1, 8, 1);
459 } else
460 mXImage =
461 XGetImage(x11->display, x11->rootwin, 0, 0, 1, 1, AllPlanes, ZPixmap);
463 x11->depthonscreen = depth; // display depth on screen
465 // get bits/pixel from XImage structure:
466 if (mXImage == NULL)
468 mask = 0;
469 } else
472 * for the depth==24 case, the XImage structures might use
473 * 24 or 32 bits of data per pixel. The x11->depthonscreen
474 * field stores the amount of data per pixel in the
475 * XImage structure!
477 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
479 bpp = mXImage->bits_per_pixel;
480 if ((x11->depthonscreen + 7) / 8 != (bpp + 7) / 8)
481 x11->depthonscreen = bpp; // by A'rpi
482 mask =
483 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
484 mp_msg(MSGT_VO, MSGL_V,
485 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
486 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
487 XDestroyImage(mXImage);
489 if (((x11->depthonscreen + 7) / 8) == 2)
491 if (mask == 0x7FFF)
492 x11->depthonscreen = 15;
493 else if (mask == 0xFFFF)
494 x11->depthonscreen = 16;
496 // XCloseDisplay( mDisplay );
497 /* slightly improved local display detection AST */
498 if (strncmp(dispName, "unix:", 5) == 0)
499 dispName += 4;
500 else if (strncmp(dispName, "localhost:", 10) == 0)
501 dispName += 9;
502 if (*dispName == ':' && atoi(dispName + 1) < 10)
503 x11->display_is_local = 1;
504 else
505 x11->display_is_local = 0;
506 mp_msg(MSGT_VO, MSGL_V,
507 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
508 opts->vo_screenwidth, opts->vo_screenheight, depth, x11->depthonscreen,
509 dispName, x11->display_is_local ? "local" : "remote");
511 x11->wm_type = vo_wm_detect(vo);
513 x11->fs_type = vo_x11_get_fs_type(x11->wm_type);
515 fstype_dump(x11->fs_type);
517 saver_off(x11->display);
518 return 1;
521 void vo_uninit(struct vo_x11_state *x11)
523 if (!x11->display)
525 mp_msg(MSGT_VO, MSGL_V,
526 "vo: x11 uninit called but X11 not initialized..\n");
527 return;
529 // if( !vo_depthonscreen ) return;
530 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
531 XSetErrorHandler(NULL);
532 XCloseDisplay(x11->display);
533 x11->depthonscreen = 0;
534 x11->display = NULL;
535 talloc_free(x11);
538 #include "osdep/keycodes.h"
539 #include "wskeys.h"
541 #ifdef XF86XK_AudioPause
542 static void vo_x11_putkey_ext(struct vo *vo, int keysym)
544 struct mp_fifo *f = vo->key_fifo;
545 switch (keysym)
547 case XF86XK_AudioPause:
548 mplayer_put_key(f, KEY_PAUSE);
549 break;
550 case XF86XK_AudioStop:
551 mplayer_put_key(f, KEY_STOP);
552 break;
553 case XF86XK_AudioPrev:
554 mplayer_put_key(f, KEY_PREV);
555 break;
556 case XF86XK_AudioNext:
557 mplayer_put_key(f, KEY_NEXT);
558 break;
559 case XF86XK_AudioLowerVolume:
560 mplayer_put_key(f, KEY_VOLUME_DOWN);
561 break;
562 case XF86XK_AudioRaiseVolume:
563 mplayer_put_key(f, KEY_VOLUME_UP);
564 break;
565 default:
566 break;
569 #endif
571 void vo_x11_putkey(struct vo *vo, int key)
573 struct mp_fifo *f = vo->key_fifo;
574 switch (key)
576 case wsLeft:
577 mplayer_put_key(f, KEY_LEFT);
578 break;
579 case wsRight:
580 mplayer_put_key(f, KEY_RIGHT);
581 break;
582 case wsUp:
583 mplayer_put_key(f, KEY_UP);
584 break;
585 case wsDown:
586 mplayer_put_key(f, KEY_DOWN);
587 break;
588 case wsSpace:
589 mplayer_put_key(f, ' ');
590 break;
591 case wsEscape:
592 mplayer_put_key(f, KEY_ESC);
593 break;
594 case wsTab:
595 mplayer_put_key(f, KEY_TAB);
596 break;
597 case wsEnter:
598 mplayer_put_key(f, KEY_ENTER);
599 break;
600 case wsBackSpace:
601 mplayer_put_key(f, KEY_BS);
602 break;
603 case wsDelete:
604 mplayer_put_key(f, KEY_DELETE);
605 break;
606 case wsInsert:
607 mplayer_put_key(f, KEY_INSERT);
608 break;
609 case wsHome:
610 mplayer_put_key(f, KEY_HOME);
611 break;
612 case wsEnd:
613 mplayer_put_key(f, KEY_END);
614 break;
615 case wsPageUp:
616 mplayer_put_key(f, KEY_PAGE_UP);
617 break;
618 case wsPageDown:
619 mplayer_put_key(f, KEY_PAGE_DOWN);
620 break;
621 case wsF1:
622 mplayer_put_key(f, KEY_F + 1);
623 break;
624 case wsF2:
625 mplayer_put_key(f, KEY_F + 2);
626 break;
627 case wsF3:
628 mplayer_put_key(f, KEY_F + 3);
629 break;
630 case wsF4:
631 mplayer_put_key(f, KEY_F + 4);
632 break;
633 case wsF5:
634 mplayer_put_key(f, KEY_F + 5);
635 break;
636 case wsF6:
637 mplayer_put_key(f, KEY_F + 6);
638 break;
639 case wsF7:
640 mplayer_put_key(f, KEY_F + 7);
641 break;
642 case wsF8:
643 mplayer_put_key(f, KEY_F + 8);
644 break;
645 case wsF9:
646 mplayer_put_key(f, KEY_F + 9);
647 break;
648 case wsF10:
649 mplayer_put_key(f, KEY_F + 10);
650 break;
651 case wsF11:
652 mplayer_put_key(f, KEY_F + 11);
653 break;
654 case wsF12:
655 mplayer_put_key(f, KEY_F + 12);
656 break;
657 case wsMinus:
658 case wsGrayMinus:
659 mplayer_put_key(f, '-');
660 break;
661 case wsPlus:
662 case wsGrayPlus:
663 mplayer_put_key(f, '+');
664 break;
665 case wsGrayMul:
666 case wsMul:
667 mplayer_put_key(f, '*');
668 break;
669 case wsGrayDiv:
670 case wsDiv:
671 mplayer_put_key(f, '/');
672 break;
673 case wsLess:
674 mplayer_put_key(f, '<');
675 break;
676 case wsMore:
677 mplayer_put_key(f, '>');
678 break;
679 case wsGray0:
680 mplayer_put_key(f, KEY_KP0);
681 break;
682 case wsGrayEnd:
683 case wsGray1:
684 mplayer_put_key(f, KEY_KP1);
685 break;
686 case wsGrayDown:
687 case wsGray2:
688 mplayer_put_key(f, KEY_KP2);
689 break;
690 case wsGrayPgDn:
691 case wsGray3:
692 mplayer_put_key(f, KEY_KP3);
693 break;
694 case wsGrayLeft:
695 case wsGray4:
696 mplayer_put_key(f, KEY_KP4);
697 break;
698 case wsGray5Dup:
699 case wsGray5:
700 mplayer_put_key(f, KEY_KP5);
701 break;
702 case wsGrayRight:
703 case wsGray6:
704 mplayer_put_key(f, KEY_KP6);
705 break;
706 case wsGrayHome:
707 case wsGray7:
708 mplayer_put_key(f, KEY_KP7);
709 break;
710 case wsGrayUp:
711 case wsGray8:
712 mplayer_put_key(f, KEY_KP8);
713 break;
714 case wsGrayPgUp:
715 case wsGray9:
716 mplayer_put_key(f, KEY_KP9);
717 break;
718 case wsGrayDecimal:
719 mplayer_put_key(f, KEY_KPDEC);
720 break;
721 case wsGrayInsert:
722 mplayer_put_key(f, KEY_KPINS);
723 break;
724 case wsGrayDelete:
725 mplayer_put_key(f, KEY_KPDEL);
726 break;
727 case wsGrayEnter:
728 mplayer_put_key(f, KEY_KPENTER);
729 break;
730 case wsGrave:
731 mplayer_put_key(f, '`');
732 break;
733 case wsTilde:
734 mplayer_put_key(f, '~');
735 break;
736 case wsExclSign:
737 mplayer_put_key(f, '!');
738 break;
739 case wsAt:
740 mplayer_put_key(f, '@');
741 break;
742 case wsHash:
743 mplayer_put_key(f, '#');
744 break;
745 case wsDollar:
746 mplayer_put_key(f, '$');
747 break;
748 case wsPercent:
749 mplayer_put_key(f, '%');
750 break;
751 case wsCircumflex:
752 mplayer_put_key(f, '^');
753 break;
754 case wsAmpersand:
755 mplayer_put_key(f, '&');
756 break;
757 case wsobracket:
758 mplayer_put_key(f, '(');
759 break;
760 case wscbracket:
761 mplayer_put_key(f, ')');
762 break;
763 case wsUnder:
764 mplayer_put_key(f, '_');
765 break;
766 case wsocbracket:
767 mplayer_put_key(f, '{');
768 break;
769 case wsccbracket:
770 mplayer_put_key(f, '}');
771 break;
772 case wsColon:
773 mplayer_put_key(f, ':');
774 break;
775 case wsSemicolon:
776 mplayer_put_key(f, ';');
777 break;
778 case wsDblQuote:
779 mplayer_put_key(f, '\"');
780 break;
781 case wsAcute:
782 mplayer_put_key(f, '\'');
783 break;
784 case wsComma:
785 mplayer_put_key(f, ',');
786 break;
787 case wsPoint:
788 mplayer_put_key(f, '.');
789 break;
790 case wsQuestSign:
791 mplayer_put_key(f, '?');
792 break;
793 case wsBSlash:
794 mplayer_put_key(f, '\\');
795 break;
796 case wsPipe:
797 mplayer_put_key(f, '|');
798 break;
799 case wsEqual:
800 mplayer_put_key(f, '=');
801 break;
802 case wsosbrackets:
803 mplayer_put_key(f, '[');
804 break;
805 case wscsbrackets:
806 mplayer_put_key(f, ']');
807 break;
810 default:
811 if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') ||
812 (key >= '0' && key <= '9'))
813 mplayer_put_key(f, key);
819 // ----- Motif header: -------
821 #define MWM_HINTS_FUNCTIONS (1L << 0)
822 #define MWM_HINTS_DECORATIONS (1L << 1)
823 #define MWM_HINTS_INPUT_MODE (1L << 2)
824 #define MWM_HINTS_STATUS (1L << 3)
826 #define MWM_FUNC_ALL (1L << 0)
827 #define MWM_FUNC_RESIZE (1L << 1)
828 #define MWM_FUNC_MOVE (1L << 2)
829 #define MWM_FUNC_MINIMIZE (1L << 3)
830 #define MWM_FUNC_MAXIMIZE (1L << 4)
831 #define MWM_FUNC_CLOSE (1L << 5)
833 #define MWM_DECOR_ALL (1L << 0)
834 #define MWM_DECOR_BORDER (1L << 1)
835 #define MWM_DECOR_RESIZEH (1L << 2)
836 #define MWM_DECOR_TITLE (1L << 3)
837 #define MWM_DECOR_MENU (1L << 4)
838 #define MWM_DECOR_MINIMIZE (1L << 5)
839 #define MWM_DECOR_MAXIMIZE (1L << 6)
841 #define MWM_INPUT_MODELESS 0
842 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
843 #define MWM_INPUT_SYSTEM_MODAL 2
844 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
845 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
847 #define MWM_TEAROFF_WINDOW (1L<<0)
849 typedef struct
851 long flags;
852 long functions;
853 long decorations;
854 long input_mode;
855 long state;
856 } MotifWmHints;
858 static MotifWmHints vo_MotifWmHints;
859 static Atom vo_MotifHints = None;
861 void vo_x11_decoration(struct vo *vo, int d)
863 struct vo_x11_state *x11 = vo->x11;
864 Atom mtype;
865 int mformat;
866 unsigned long mn, mb;
868 if (!WinID)
869 return;
871 if (vo_fsmode & 8)
873 XSetTransientForHint(x11->display, x11->window,
874 RootWindow(x11->display, x11->screen));
877 vo_MotifHints = XInternAtom(x11->display, "_MOTIF_WM_HINTS", 0);
878 if (vo_MotifHints != None)
880 if (!d)
882 MotifWmHints *mhints = NULL;
884 XGetWindowProperty(x11->display, x11->window,
885 vo_MotifHints, 0, 20, False,
886 vo_MotifHints, &mtype, &mformat, &mn,
887 &mb, (unsigned char **) &mhints);
888 if (mhints)
890 if (mhints->flags & MWM_HINTS_DECORATIONS)
891 x11->olddecor = mhints->decorations;
892 if (mhints->flags & MWM_HINTS_FUNCTIONS)
893 x11->oldfuncs = mhints->functions;
894 XFree(mhints);
898 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
899 vo_MotifWmHints.flags =
900 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
901 if (d)
903 vo_MotifWmHints.functions = x11->oldfuncs;
904 d = x11->olddecor;
906 #if 0
907 vo_MotifWmHints.decorations =
908 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
909 #else
910 vo_MotifWmHints.decorations =
911 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
912 #endif
913 XChangeProperty(x11->display, x11->window, vo_MotifHints,
914 vo_MotifHints, 32,
915 PropModeReplace,
916 (unsigned char *) &vo_MotifWmHints,
917 (vo_fsmode & 4) ? 4 : 5);
921 void vo_x11_classhint(struct vo *vo, Window window, char *name)
923 struct vo_x11_state *x11 = vo->x11;
924 XClassHint wmClass;
925 pid_t pid = getpid();
927 wmClass.res_name = name;
928 wmClass.res_class = "MPlayer";
929 XSetClassHint(x11->display, window, &wmClass);
930 XChangeProperty(x11->display, window, x11->XA_NET_WM_PID, XA_CARDINAL,
931 32, PropModeReplace, (unsigned char *) &pid, 1);
934 void vo_x11_uninit(struct vo *vo)
936 struct vo_x11_state *x11 = vo->x11;
937 saver_on(x11->display);
938 if (x11->window != None)
939 vo_showcursor(x11->display, x11->window);
941 if (x11->f_gc)
943 XFreeGC(vo->x11->display, x11->f_gc);
944 x11->f_gc = NULL;
946 #ifdef HAVE_NEW_GUI
947 /* destroy window only if it's not controlled by the GUI */
948 if (!use_gui)
949 #endif
951 if (x11->vo_gc)
953 XSetBackground(vo->x11->display, x11->vo_gc, 0);
954 XFreeGC(vo->x11->display, x11->vo_gc);
955 x11->vo_gc = NULL;
957 if (x11->window != None)
959 XClearWindow(x11->display, x11->window);
960 if (WinID < 0)
962 XEvent xev;
964 XUnmapWindow(x11->display, x11->window);
965 XDestroyWindow(x11->display, x11->window);
968 XNextEvent(x11->display, &xev);
970 while (xev.type != DestroyNotify
971 || xev.xdestroywindow.event != x11->window);
973 x11->window = None;
975 vo_fs = 0;
976 x11->vo_old_width = x11->vo_old_height = 0;
980 int vo_x11_check_events(struct vo *vo)
982 struct vo_x11_state *x11 = vo->x11;
983 struct MPOpts *opts = vo->opts;
984 Display *display = vo->x11->display;
985 int ret = 0;
986 XEvent Event;
987 char buf[100];
988 KeySym keySym;
990 // unsigned long vo_KeyTable[512];
992 if ((x11->vo_mouse_autohide) && x11->mouse_waiting_hide &&
993 (GetTimerMS() - x11->mouse_timer >= 1000)) {
994 vo_hidecursor(display, x11->window);
995 x11->mouse_waiting_hide = 0;
998 while (XPending(display))
1000 XNextEvent(display, &Event);
1001 #ifdef HAVE_NEW_GUI
1002 if (use_gui)
1004 guiGetEvent(0, (char *) &Event);
1005 if (x11->window != Event.xany.window)
1006 continue;
1008 #endif
1009 // printf("\rEvent.type=%X \n",Event.type);
1010 switch (Event.type)
1012 case Expose:
1013 ret |= VO_EVENT_EXPOSE;
1014 break;
1015 case ConfigureNotify:
1016 // if (!vo_fs && (Event.xconfigure.width == opts->vo_screenwidth || Event.xconfigure.height == opts->vo_screenheight)) break;
1017 // if (vo_fs && Event.xconfigure.width != opts->vo_screenwidth && Event.xconfigure.height != opts->vo_screenheight) break;
1018 if (x11->window == None)
1019 break;
1020 vo->dwidth = Event.xconfigure.width;
1021 vo->dheight = Event.xconfigure.height;
1022 #if 0
1023 /* when resizing, x and y are zero :( */
1024 vo->dx = Event.xconfigure.x;
1025 vo->dy = Event.xconfigure.y;
1026 #else
1028 Window root;
1029 int foo;
1030 Window win;
1032 XGetGeometry(display, x11->window, &root, &foo, &foo,
1033 &foo /*width */ , &foo /*height */ , &foo,
1034 &foo);
1035 XTranslateCoordinates(display, x11->window, root, 0, 0,
1036 &vo->dx, &vo->dy, &win);
1038 #endif
1039 ret |= VO_EVENT_RESIZE;
1040 break;
1041 case KeyPress:
1043 int key;
1045 #ifdef HAVE_NEW_GUI
1046 if ( use_gui ) { break; }
1047 #endif
1049 XLookupString(&Event.xkey, buf, sizeof(buf), &keySym,
1050 &x11->compose_status);
1051 #ifdef XF86XK_AudioPause
1052 vo_x11_putkey_ext(vo, keySym);
1053 #endif
1054 key =
1055 ((keySym & 0xff00) !=
1056 0 ? ((keySym & 0x00ff) + 256) : (keySym));
1057 vo_x11_putkey(vo, key);
1058 ret |= VO_EVENT_KEYPRESS;
1060 break;
1061 case MotionNotify:
1062 if(enable_mouse_movements)
1064 char cmd_str[40];
1065 sprintf(cmd_str,"set_mouse_pos %i %i",Event.xmotion.x, Event.xmotion.y);
1066 mp_input_queue_cmd(vo->input_ctx,
1067 mp_input_parse_cmd(cmd_str));
1070 if (x11->vo_mouse_autohide)
1072 vo_showcursor(display, x11->window);
1073 x11->mouse_waiting_hide = 1;
1074 x11->mouse_timer = GetTimerMS();
1076 break;
1077 case ButtonPress:
1078 if (x11->vo_mouse_autohide)
1080 vo_showcursor(display, x11->window);
1081 x11->mouse_waiting_hide = 1;
1082 x11->mouse_timer = GetTimerMS();
1084 #ifdef HAVE_NEW_GUI
1085 // Ignore mouse button 1-3 under GUI.
1086 if (use_gui && (Event.xbutton.button >= 1)
1087 && (Event.xbutton.button <= 3))
1088 break;
1089 #endif
1090 mplayer_put_key(vo->key_fifo,
1091 (MOUSE_BTN0 + Event.xbutton.button - 1)
1092 | MP_KEY_DOWN);
1093 break;
1094 case ButtonRelease:
1095 if (x11->vo_mouse_autohide)
1097 vo_showcursor(display, x11->window);
1098 x11->mouse_waiting_hide = 1;
1099 x11->mouse_timer = GetTimerMS();
1101 #ifdef HAVE_NEW_GUI
1102 // Ignore mouse button 1-3 under GUI.
1103 if (use_gui && (Event.xbutton.button >= 1)
1104 && (Event.xbutton.button <= 3))
1105 break;
1106 #endif
1107 mplayer_put_key(vo->key_fifo,
1108 MOUSE_BTN0 + Event.xbutton.button - 1);
1109 break;
1110 case PropertyNotify:
1112 char *name =
1113 XGetAtomName(display, Event.xproperty.atom);
1115 if (!name)
1116 break;
1118 // fprintf(stderr,"[ws] PropertyNotify ( 0x%x ) %s ( 0x%x )\n",vo_window,name,Event.xproperty.atom );
1120 XFree(name);
1122 break;
1123 case MapNotify:
1124 x11->vo_hint.win_gravity = x11->old_gravity;
1125 XSetWMNormalHints(display, x11->window, &x11->vo_hint);
1126 x11->fs_flip = 0;
1127 break;
1128 case ClientMessage:
1129 if (Event.xclient.message_type == x11->XAWM_PROTOCOLS &&
1130 Event.xclient.data.l[0] == x11->XAWM_DELETE_WINDOW)
1131 mplayer_put_key(vo->key_fifo, KEY_CLOSE_WIN);
1132 break;
1135 return ret;
1139 * \brief sets the size and position of the non-fullscreen window.
1141 static void vo_x11_nofs_sizepos(struct vo *vo, int x, int y,
1142 int width, int height)
1144 struct vo_x11_state *x11 = vo->x11;
1145 vo_x11_sizehint(vo, x, y, width, height, 0);
1146 if (vo_fs) {
1147 x11->vo_old_x = x;
1148 x11->vo_old_y = y;
1149 x11->vo_old_width = width;
1150 x11->vo_old_height = height;
1152 else
1154 vo->dwidth = width;
1155 vo->dheight = height;
1156 XMoveResizeWindow(vo->x11->display, vo->x11->window, x, y, width, height);
1160 void vo_x11_sizehint(struct vo *vo, int x, int y, int width, int height, int max)
1162 struct vo_x11_state *x11 = vo->x11;
1163 x11->vo_hint.flags = 0;
1164 if (vo_keepaspect)
1166 x11->vo_hint.flags |= PAspect;
1167 x11->vo_hint.min_aspect.x = width;
1168 x11->vo_hint.min_aspect.y = height;
1169 x11->vo_hint.max_aspect.x = width;
1170 x11->vo_hint.max_aspect.y = height;
1173 x11->vo_hint.flags |= PPosition | PSize;
1174 x11->vo_hint.x = x;
1175 x11->vo_hint.y = y;
1176 x11->vo_hint.width = width;
1177 x11->vo_hint.height = height;
1178 if (max)
1180 x11->vo_hint.flags |= PMaxSize;
1181 x11->vo_hint.max_width = width;
1182 x11->vo_hint.max_height = height;
1183 } else
1185 x11->vo_hint.max_width = 0;
1186 x11->vo_hint.max_height = 0;
1189 // Set minimum height/width to 4 to avoid off-by-one errors
1190 // and because mga_vid requires a minimal size of 4 pixels.
1191 x11->vo_hint.flags |= PMinSize;
1192 x11->vo_hint.min_width = x11->vo_hint.min_height = 4;
1194 x11->vo_hint.flags |= PWinGravity;
1195 x11->vo_hint.win_gravity = StaticGravity;
1196 XSetWMNormalHints(x11->display, x11->window, &x11->vo_hint);
1199 static int vo_x11_get_gnome_layer(struct vo_x11_state *x11, Window win)
1201 Atom type;
1202 int format;
1203 unsigned long nitems;
1204 unsigned long bytesafter;
1205 unsigned short *args = NULL;
1207 if (XGetWindowProperty(x11->display, win, x11->XA_WIN_LAYER, 0, 16384,
1208 False, AnyPropertyType, &type, &format, &nitems,
1209 &bytesafter,
1210 (unsigned char **) &args) == Success
1211 && nitems > 0 && args)
1213 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
1214 *args);
1215 return *args;
1217 return WIN_LAYER_NORMAL;
1221 static Window vo_x11_create_smooth_window(struct vo_x11_state *x11, Window mRoot,
1222 Visual * vis, int x, int y,
1223 unsigned int width, unsigned int height,
1224 int depth, Colormap col_map)
1226 unsigned long xswamask = CWBackingStore | CWBorderPixel;
1227 XSetWindowAttributes xswa;
1228 Window ret_win;
1230 if (col_map != CopyFromParent)
1232 xswa.colormap = col_map;
1233 xswamask |= CWColormap;
1235 xswa.background_pixel = 0;
1236 xswa.border_pixel = 0;
1237 xswa.backing_store = Always;
1238 xswa.bit_gravity = StaticGravity;
1240 ret_win =
1241 XCreateWindow(x11->display, x11->rootwin, x, y, width, height, 0, depth,
1242 CopyFromParent, vis, xswamask, &xswa);
1243 XSetWMProtocols(x11->display, ret_win, &x11->XAWM_DELETE_WINDOW, 1);
1244 if (!x11->f_gc)
1245 x11->f_gc = XCreateGC(x11->display, ret_win, 0, 0);
1246 XSetForeground(x11->display, x11->f_gc, 0);
1248 return ret_win;
1252 * \brief create and setup a window suitable for display
1253 * \param vis Visual to use for creating the window
1254 * \param x x position of window
1255 * \param y y position of window
1256 * \param width width of window
1257 * \param height height of window
1258 * \param flags flags for window creation.
1259 * Only VOFLAG_FULLSCREEN is supported so far.
1260 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1261 * \param classname name to use for the classhint
1262 * \param title title for the window
1264 * This also does the grunt-work like setting Window Manager hints etc.
1265 * If vo_window is already set it just moves and resizes it.
1267 void vo_x11_create_vo_window(struct vo *vo, XVisualInfo *vis, int x, int y,
1268 unsigned int width, unsigned int height, int flags,
1269 Colormap col_map,
1270 const char *classname, const char *title)
1272 struct MPOpts *opts = vo->opts;
1273 struct vo_x11_state *x11 = vo->x11;
1274 Display *mDisplay = vo->x11->display;
1275 if (x11->window == None) {
1276 XSizeHints hint;
1277 XEvent xev;
1278 vo_fs = 0;
1279 vo->dwidth = width;
1280 vo->dheight = height;
1281 x11->window = vo_x11_create_smooth_window(x11, x11->rootwin, vis->visual,
1282 x, y, width, height, vis->depth, col_map);
1283 vo_x11_classhint(vo, x11->window, classname);
1284 XStoreName(mDisplay, x11->window, title);
1285 vo_hidecursor(mDisplay, x11->window);
1286 XSelectInput(mDisplay, x11->window, StructureNotifyMask);
1287 hint.x = x; hint.y = y;
1288 hint.width = width; hint.height = height;
1289 hint.flags = PPosition | PSize;
1290 XSetStandardProperties(mDisplay, x11->window, title, title, None, NULL, 0, &hint);
1291 vo_x11_sizehint(vo, x, y, width, height, 0);
1292 // map window
1293 XMapWindow(mDisplay, x11->window);
1294 XClearWindow(mDisplay, x11->window);
1295 // wait for map
1296 do {
1297 XNextEvent(mDisplay, &xev);
1298 } while (xev.type != MapNotify || xev.xmap.event != x11->window);
1299 XSelectInput(mDisplay, x11->window, NoEventMask);
1300 XSync(mDisplay, False);
1301 vo_x11_selectinput_witherr(mDisplay, x11->window,
1302 StructureNotifyMask | KeyPressMask | PointerMotionMask |
1303 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1305 if (opts->vo_ontop) vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1306 vo_x11_nofs_sizepos(vo, vo->dx, vo->dy, width, height);
1307 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1308 vo_x11_fullscreen(vo);
1311 void vo_x11_clearwindow_part(struct vo *vo, Window vo_window,
1312 int img_width, int img_height, int use_fs)
1314 struct vo_x11_state *x11 = vo->x11;
1315 struct MPOpts *opts = vo->opts;
1316 Display *mDisplay = vo->x11->display;
1317 int u_dheight, u_dwidth, left_ov, left_ov2;
1319 if (!x11->f_gc)
1320 return;
1322 u_dheight = use_fs ? opts->vo_screenheight : vo->dheight;
1323 u_dwidth = use_fs ? opts->vo_screenwidth : vo->dwidth;
1324 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1325 return;
1327 left_ov = (u_dheight - img_height) / 2;
1328 left_ov2 = (u_dwidth - img_width) / 2;
1330 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, 0, u_dwidth, left_ov);
1331 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, u_dheight - left_ov - 1,
1332 u_dwidth, left_ov + 1);
1334 if (u_dwidth > img_width)
1336 XFillRectangle(mDisplay, vo_window, x11->f_gc, 0, left_ov, left_ov2,
1337 img_height);
1338 XFillRectangle(mDisplay, vo_window, x11->f_gc, u_dwidth - left_ov2 - 1,
1339 left_ov, left_ov2 + 1, img_height);
1342 XFlush(mDisplay);
1345 void vo_x11_clearwindow(struct vo *vo, Window vo_window)
1347 struct vo_x11_state *x11 = vo->x11;
1348 struct MPOpts *opts = vo->opts;
1349 if (!x11->f_gc)
1350 return;
1351 XFillRectangle(x11->display, vo_window, x11->f_gc, 0, 0,
1352 opts->vo_screenwidth, opts->vo_screenheight);
1354 XFlush(x11->display);
1358 void vo_x11_setlayer(struct vo *vo, Window vo_window, int layer)
1360 struct vo_x11_state *x11 = vo->x11;
1361 if (WinID >= 0)
1362 return;
1364 if (x11->fs_type & vo_wm_LAYER)
1366 XClientMessageEvent xev;
1368 if (!x11->orig_layer)
1369 x11->orig_layer = vo_x11_get_gnome_layer(x11, vo_window);
1371 memset(&xev, 0, sizeof(xev));
1372 xev.type = ClientMessage;
1373 xev.display = x11->display;
1374 xev.window = vo_window;
1375 xev.message_type = x11->XA_WIN_LAYER;
1376 xev.format = 32;
1377 xev.data.l[0] = layer ? fs_layer : x11->orig_layer; // if not fullscreen, stay on default layer
1378 xev.data.l[1] = CurrentTime;
1379 mp_msg(MSGT_VO, MSGL_V,
1380 "[x11] Layered style stay on top (layer %ld).\n",
1381 xev.data.l[0]);
1382 XSendEvent(x11->display, x11->rootwin, False, SubstructureNotifyMask,
1383 (XEvent *) & xev);
1384 } else if (x11->fs_type & vo_wm_NETWM)
1386 XClientMessageEvent xev;
1387 char *state;
1389 memset(&xev, 0, sizeof(xev));
1390 xev.type = ClientMessage;
1391 xev.message_type = x11->XA_NET_WM_STATE;
1392 xev.display = x11->display;
1393 xev.window = vo_window;
1394 xev.format = 32;
1395 xev.data.l[0] = layer;
1397 if (x11->fs_type & vo_wm_STAYS_ON_TOP)
1398 xev.data.l[1] = x11->XA_NET_WM_STATE_STAYS_ON_TOP;
1399 else if (x11->fs_type & vo_wm_ABOVE)
1400 xev.data.l[1] = x11->XA_NET_WM_STATE_ABOVE;
1401 else if (x11->fs_type & vo_wm_FULLSCREEN)
1402 xev.data.l[1] = x11->XA_NET_WM_STATE_FULLSCREEN;
1403 else if (x11->fs_type & vo_wm_BELOW)
1404 // This is not fallback. We can safely assume that the situation
1405 // where only NETWM_STATE_BELOW is supported doesn't exist.
1406 xev.data.l[1] = x11->XA_NET_WM_STATE_BELOW;
1408 XSendEvent(x11->display, x11->rootwin, False, SubstructureRedirectMask,
1409 (XEvent *) & xev);
1410 state = XGetAtomName(x11->display, xev.data.l[1]);
1411 mp_msg(MSGT_VO, MSGL_V,
1412 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1413 layer, state);
1414 XFree(state);
1418 static int vo_x11_get_fs_type(int supported)
1420 int i;
1421 int type = supported;
1423 if (vo_fstype_list)
1425 i = 0;
1426 for (i = 0; vo_fstype_list[i]; i++)
1428 int neg = 0;
1429 char *arg = vo_fstype_list[i];
1431 if (vo_fstype_list[i][0] == '-')
1433 neg = 1;
1434 arg = vo_fstype_list[i] + 1;
1437 if (!strncmp(arg, "layer", 5))
1439 if (!neg && (arg[5] == '='))
1441 char *endptr = NULL;
1442 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1444 if (endptr && *endptr == '\0' && layer >= 0
1445 && layer <= 15)
1446 fs_layer = layer;
1448 if (neg)
1449 type &= ~vo_wm_LAYER;
1450 else
1451 type |= vo_wm_LAYER;
1452 } else if (!strcmp(arg, "above"))
1454 if (neg)
1455 type &= ~vo_wm_ABOVE;
1456 else
1457 type |= vo_wm_ABOVE;
1458 } else if (!strcmp(arg, "fullscreen"))
1460 if (neg)
1461 type &= ~vo_wm_FULLSCREEN;
1462 else
1463 type |= vo_wm_FULLSCREEN;
1464 } else if (!strcmp(arg, "stays_on_top"))
1466 if (neg)
1467 type &= ~vo_wm_STAYS_ON_TOP;
1468 else
1469 type |= vo_wm_STAYS_ON_TOP;
1470 } else if (!strcmp(arg, "below"))
1472 if (neg)
1473 type &= ~vo_wm_BELOW;
1474 else
1475 type |= vo_wm_BELOW;
1476 } else if (!strcmp(arg, "netwm"))
1478 if (neg)
1479 type &= ~vo_wm_NETWM;
1480 else
1481 type |= vo_wm_NETWM;
1482 } else if (!strcmp(arg, "none"))
1483 return 0;
1487 return type;
1490 void vo_x11_fullscreen(struct vo *vo)
1492 struct MPOpts *opts = vo->opts;
1493 struct vo_x11_state *x11 = vo->x11;
1494 int x, y, w, h;
1496 if (WinID >= 0 || x11->fs_flip)
1497 return;
1499 if (vo_fs)
1501 // fs->win
1502 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1504 x = x11->vo_old_x;
1505 y = x11->vo_old_y;
1506 w = x11->vo_old_width;
1507 h = x11->vo_old_height;
1510 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1511 vo_fs = VO_FALSE;
1512 } else
1514 // win->fs
1515 vo_x11_ewmh_fullscreen(x11, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1517 vo_fs = VO_TRUE;
1518 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1520 x11->vo_old_x = vo->dx;
1521 x11->vo_old_y = vo->dy;
1522 x11->vo_old_width = vo->dwidth;
1523 x11->vo_old_height = vo->dheight;
1525 update_xinerama_info(vo);
1526 x = xinerama_x;
1527 y = xinerama_y;
1528 w = opts->vo_screenwidth;
1529 h = opts->vo_screenheight;
1532 long dummy;
1534 XGetWMNormalHints(x11->display, x11->window, &x11->vo_hint, &dummy);
1535 if (!(x11->vo_hint.flags & PWinGravity))
1536 x11->old_gravity = NorthWestGravity;
1537 else
1538 x11->old_gravity = x11->vo_hint.win_gravity;
1540 if (x11->wm_type == 0 && !(vo_fsmode & 16))
1542 XUnmapWindow(x11->display, x11->window); // required for MWM
1543 XWithdrawWindow(x11->display, x11->window, x11->screen);
1544 x11->fs_flip = 1;
1547 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1549 vo_x11_decoration(vo, (vo_fs) ? 0 : 1);
1550 vo_x11_sizehint(vo, x, y, w, h, 0);
1551 vo_x11_setlayer(vo, x11->window, vo_fs);
1554 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1556 /* some WMs lose ontop after fullscreen */
1557 if ((!(vo_fs)) & opts->vo_ontop)
1558 vo_x11_setlayer(vo, x11->window, opts->vo_ontop);
1560 XMapRaised(x11->display, x11->window);
1561 if ( ! (x11->fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1562 XMoveResizeWindow(x11->display, x11->window, x, y, w, h);
1563 XRaiseWindow(x11->display, x11->window);
1564 XFlush(x11->display);
1567 void vo_x11_ontop(struct vo *vo)
1569 struct MPOpts *opts = vo->opts;
1570 opts->vo_ontop = !opts->vo_ontop;
1572 vo_x11_setlayer(vo, vo->x11->window, opts->vo_ontop);
1576 * XScreensaver stuff
1579 static int screensaver_off;
1580 static unsigned int time_last;
1582 void xscreensaver_heartbeat(struct vo_x11_state *x11)
1584 unsigned int time = GetTimerMS();
1586 if (x11->display && screensaver_off && (time - time_last) > 30000)
1588 time_last = time;
1590 XResetScreenSaver(x11->display);
1594 static int xss_suspend(Display *mDisplay, Bool suspend)
1596 #ifndef HAVE_XSS
1597 return 0;
1598 #else
1599 int event, error, major, minor;
1600 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1601 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1602 return 0;
1603 if (major < 1 || major == 1 && minor < 1)
1604 return 0;
1605 XScreenSaverSuspend(mDisplay, suspend);
1606 return 1;
1607 #endif
1611 * End of XScreensaver stuff
1614 static void saver_on(Display * mDisplay)
1617 if (!screensaver_off)
1618 return;
1619 screensaver_off = 0;
1620 if (xss_suspend(mDisplay, False))
1621 return;
1622 #ifdef HAVE_XDPMS
1623 if (dpms_disabled)
1625 int nothing;
1626 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1628 if (!DPMSEnable(mDisplay))
1629 { // restoring power saving settings
1630 mp_msg(MSGT_VO, MSGL_WARN, "DPMS not available?\n");
1631 } else
1633 // DPMS does not seem to be enabled unless we call DPMSInfo
1634 BOOL onoff;
1635 CARD16 state;
1637 DPMSForceLevel(mDisplay, DPMSModeOn);
1638 DPMSInfo(mDisplay, &state, &onoff);
1639 if (onoff)
1641 mp_msg(MSGT_VO, MSGL_V,
1642 "Successfully enabled DPMS\n");
1643 } else
1645 mp_msg(MSGT_VO, MSGL_WARN, "Could not enable DPMS\n");
1649 dpms_disabled = 0;
1651 #endif
1654 static void saver_off(Display * mDisplay)
1656 int nothing;
1658 if (screensaver_off)
1659 return;
1660 screensaver_off = 1;
1661 if (xss_suspend(mDisplay, True))
1662 return;
1663 #ifdef HAVE_XDPMS
1664 if (DPMSQueryExtension(mDisplay, &nothing, &nothing))
1666 BOOL onoff;
1667 CARD16 state;
1669 DPMSInfo(mDisplay, &state, &onoff);
1670 if (onoff)
1672 Status stat;
1674 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1675 dpms_disabled = 1;
1676 stat = DPMSDisable(mDisplay); // monitor powersave off
1677 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1680 #endif
1683 static XErrorHandler old_handler = NULL;
1684 static int selectinput_err = 0;
1685 static int x11_selectinput_errorhandler(Display * display,
1686 XErrorEvent * event)
1688 if (event->error_code == BadAccess)
1690 selectinput_err = 1;
1691 mp_msg(MSGT_VO, MSGL_ERR,
1692 "X11 error: BadAccess during XSelectInput Call\n");
1693 mp_msg(MSGT_VO, MSGL_ERR,
1694 "X11 error: The 'ButtonPressMask' mask of specified window has probably already used by another appication (see man XSelectInput)\n");
1695 /* If you think MPlayer should shutdown with this error,
1696 * comment out the following line */
1697 return 0;
1699 if (old_handler != NULL)
1700 old_handler(display, event);
1701 else
1702 x11_errorhandler(display, event);
1703 return 0;
1706 void vo_x11_selectinput_witherr(Display * display, Window w,
1707 long event_mask)
1709 XSync(display, False);
1710 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1711 selectinput_err = 0;
1712 if (vo_nomouse_input)
1714 XSelectInput(display, w,
1715 event_mask &
1716 (~(ButtonPressMask | ButtonReleaseMask)));
1717 } else
1719 XSelectInput(display, w, event_mask);
1721 XSync(display, False);
1722 XSetErrorHandler(old_handler);
1723 if (selectinput_err)
1725 mp_msg(MSGT_VO, MSGL_ERR,
1726 "X11 error: MPlayer discards mouse control (reconfiguring)\n");
1727 XSelectInput(display, w,
1728 event_mask &
1730 (ButtonPressMask | ButtonReleaseMask |
1731 PointerMotionMask)));
1735 #ifdef HAVE_XF86VM
1736 void vo_vm_switch(struct vo *vo, uint32_t X, uint32_t Y, int *modeline_width,
1737 int *modeline_height)
1739 struct vo_x11_state *x11 = vo->x11;
1740 struct MPOpts *opts = vo->opts;
1741 Display *mDisplay = x11->display;
1742 int vm_event, vm_error;
1743 int vm_ver, vm_rev;
1744 int i, j, have_vm = 0;
1746 int modecount;
1748 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1750 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1751 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1752 vm_rev);
1753 have_vm = 1;
1754 } else
1755 mp_msg(MSGT_VO, MSGL_WARN,
1756 "XF86VidMode extension not available.\n");
1758 if (have_vm)
1760 if (vidmodes == NULL)
1761 XF86VidModeGetAllModeLines(mDisplay, x11->screen, &modecount,
1762 &vidmodes);
1763 j = 0;
1764 *modeline_width = vidmodes[0]->hdisplay;
1765 *modeline_height = vidmodes[0]->vdisplay;
1767 for (i = 1; i < modecount; i++)
1768 if ((vidmodes[i]->hdisplay >= X)
1769 && (vidmodes[i]->vdisplay >= Y))
1770 if ((vidmodes[i]->hdisplay <= *modeline_width)
1771 && (vidmodes[i]->vdisplay <= *modeline_height))
1773 *modeline_width = vidmodes[i]->hdisplay;
1774 *modeline_height = vidmodes[i]->vdisplay;
1775 j = i;
1778 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_SelectedVideoMode,
1779 *modeline_width, *modeline_height, X, Y);
1780 XF86VidModeLockModeSwitch(mDisplay, x11->screen, 0);
1781 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1782 XF86VidModeSwitchToMode(mDisplay, x11->screen, vidmodes[j]);
1783 X = (opts->vo_screenwidth - *modeline_width) / 2;
1784 Y = (opts->vo_screenheight - *modeline_height) / 2;
1785 XF86VidModeSetViewPort(mDisplay, x11->screen, X, Y);
1789 void vo_vm_close(struct vo *vo)
1791 Display *dpy = vo->x11->display;
1792 struct MPOpts *opts = vo->opts;
1793 #ifdef HAVE_NEW_GUI
1794 if (vidmodes != NULL && vo->x11->vo_window != None)
1795 #else
1796 if (vidmodes != NULL)
1797 #endif
1799 int i, modecount;
1800 int screen;
1802 screen = DefaultScreen(dpy);
1804 free(vidmodes);
1805 vidmodes = NULL;
1806 XF86VidModeGetAllModeLines(dpy, vo->x11->screen, &modecount,
1807 &vidmodes);
1808 for (i = 0; i < modecount; i++)
1809 if ((vidmodes[i]->hdisplay == opts->vo_screenwidth)
1810 && (vidmodes[i]->vdisplay == opts->vo_screenheight))
1812 mp_msg(MSGT_VO, MSGL_INFO,
1813 "Returning to original mode %dx%d\n",
1814 opts->vo_screenwidth, opts->vo_screenheight);
1815 break;
1818 XF86VidModeSwitchToMode(dpy, screen, vidmodes[i]);
1819 XF86VidModeSwitchToMode(dpy, screen, vidmodes[i]);
1820 free(vidmodes);
1821 vidmodes = NULL;
1824 #endif
1826 #endif /* X11_FULLSCREEN */
1830 * Scan the available visuals on this Display/Screen. Try to find
1831 * the 'best' available TrueColor visual that has a decent color
1832 * depth (at least 15bit). If there are multiple visuals with depth
1833 * >= 15bit, we prefer visuals with a smaller color depth.
1835 int vo_find_depth_from_visuals(Display * dpy, int screen,
1836 Visual ** visual_return)
1838 XVisualInfo visual_tmpl;
1839 XVisualInfo *visuals;
1840 int nvisuals, i;
1841 int bestvisual = -1;
1842 int bestvisual_depth = -1;
1844 visual_tmpl.screen = screen;
1845 visual_tmpl.class = TrueColor;
1846 visuals = XGetVisualInfo(dpy,
1847 VisualScreenMask | VisualClassMask,
1848 &visual_tmpl, &nvisuals);
1849 if (visuals != NULL)
1851 for (i = 0; i < nvisuals; i++)
1853 mp_msg(MSGT_VO, MSGL_V,
1854 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1855 visuals[i].visualid, visuals[i].depth,
1856 visuals[i].red_mask, visuals[i].green_mask,
1857 visuals[i].blue_mask);
1859 * Save the visual index and its depth, if this is the first
1860 * truecolor visul, or a visual that is 'preferred' over the
1861 * previous 'best' visual.
1863 if (bestvisual_depth == -1
1864 || (visuals[i].depth >= 15
1865 && (visuals[i].depth < bestvisual_depth
1866 || bestvisual_depth < 15)))
1868 bestvisual = i;
1869 bestvisual_depth = visuals[i].depth;
1873 if (bestvisual != -1 && visual_return != NULL)
1874 *visual_return = visuals[bestvisual].visual;
1876 XFree(visuals);
1878 return bestvisual_depth;
1882 static Colormap cmap = None;
1883 static XColor cols[256];
1884 static int cm_size, red_mask, green_mask, blue_mask;
1887 Colormap vo_x11_create_colormap(struct vo *vo, XVisualInfo *vinfo)
1889 struct vo_x11_state *x11 = vo->x11;
1890 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1892 if (vinfo->class != DirectColor)
1893 return XCreateColormap(x11->display, x11->rootwin, vinfo->visual,
1894 AllocNone);
1896 /* can this function get called twice or more? */
1897 if (cmap)
1898 return cmap;
1899 cm_size = vinfo->colormap_size;
1900 red_mask = vinfo->red_mask;
1901 green_mask = vinfo->green_mask;
1902 blue_mask = vinfo->blue_mask;
1903 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1904 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1905 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1906 rvu = 65536ull * ru / (red_mask + ru);
1907 gvu = 65536ull * gu / (green_mask + gu);
1908 bvu = 65536ull * bu / (blue_mask + bu);
1909 r = g = b = 0;
1910 rv = gv = bv = 0;
1911 m = DoRed | DoGreen | DoBlue;
1912 for (k = 0; k < cm_size; k++)
1914 int t;
1916 cols[k].pixel = r | g | b;
1917 cols[k].red = rv;
1918 cols[k].green = gv;
1919 cols[k].blue = bv;
1920 cols[k].flags = m;
1921 t = (r + ru) & red_mask;
1922 if (t < r)
1923 m &= ~DoRed;
1924 r = t;
1925 t = (g + gu) & green_mask;
1926 if (t < g)
1927 m &= ~DoGreen;
1928 g = t;
1929 t = (b + bu) & blue_mask;
1930 if (t < b)
1931 m &= ~DoBlue;
1932 b = t;
1933 rv += rvu;
1934 gv += gvu;
1935 bv += bvu;
1937 cmap = XCreateColormap(x11->display, x11->rootwin, vinfo->visual, AllocAll);
1938 XStoreColors(x11->display, cmap, cols, cm_size);
1939 return cmap;
1943 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1944 * hue and red/green/blue intensity, but we cannot do saturation.
1945 * Currently only gamma, brightness and contrast are implemented.
1946 * Is there sufficient interest for hue and/or red/green/blue intensity?
1948 /* these values have range [-100,100] and are initially 0 */
1949 static int vo_gamma = 0;
1950 static int vo_brightness = 0;
1951 static int vo_contrast = 0;
1953 static int transform_color(float val,
1954 float brightness, float contrast, float gamma) {
1955 float s = pow(val, gamma);
1956 s = (s - 0.5) * contrast + 0.5;
1957 s += brightness;
1958 if (s < 0)
1959 s = 0;
1960 if (s > 1)
1961 s = 1;
1962 return (unsigned short) (s * 65535);
1965 uint32_t vo_x11_set_equalizer(struct vo *vo, char *name, int value)
1967 float gamma, brightness, contrast;
1968 float rf, gf, bf;
1969 int k;
1972 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1973 * of TrueColor-ed window but be careful:
1974 * Unlike the colormaps, which are private for the X client
1975 * who created them and thus automatically destroyed on client
1976 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1977 * setting and _must_ be restored before the process exits.
1978 * Unforunately when the process crashes (or gets killed
1979 * for some reason) it is impossible to restore the setting,
1980 * and such behaviour could be rather annoying for the users.
1982 if (cmap == None)
1983 return VO_NOTAVAIL;
1985 if (!strcasecmp(name, "brightness"))
1986 vo_brightness = value;
1987 else if (!strcasecmp(name, "contrast"))
1988 vo_contrast = value;
1989 else if (!strcasecmp(name, "gamma"))
1990 vo_gamma = value;
1991 else
1992 return VO_NOTIMPL;
1994 brightness = 0.01 * vo_brightness;
1995 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1996 gamma = pow(2, -0.02 * vo_gamma);
1998 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1999 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
2000 green_mask;
2001 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
2003 /* now recalculate the colormap using the newly set value */
2004 for (k = 0; k < cm_size; k++)
2006 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
2007 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
2008 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
2011 XStoreColors(vo->x11->display, cmap, cols, cm_size);
2012 XFlush(vo->x11->display);
2013 return VO_TRUE;
2016 uint32_t vo_x11_get_equalizer(char *name, int *value)
2018 if (cmap == None)
2019 return VO_NOTAVAIL;
2020 if (!strcasecmp(name, "brightness"))
2021 *value = vo_brightness;
2022 else if (!strcasecmp(name, "contrast"))
2023 *value = vo_contrast;
2024 else if (!strcasecmp(name, "gamma"))
2025 *value = vo_gamma;
2026 else
2027 return VO_NOTIMPL;
2028 return VO_TRUE;
2031 #ifdef HAVE_XV
2032 int vo_xv_set_eq(struct vo *vo, uint32_t xv_port, char *name, int value)
2034 XvAttribute *attributes;
2035 int i, howmany, xv_atom;
2037 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
2039 /* get available attributes */
2040 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
2041 for (i = 0; i < howmany && attributes; i++)
2042 if (attributes[i].flags & XvSettable)
2044 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
2045 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2046 then trigger it if it's ok so that the other values are at default upon query */
2047 if (xv_atom != None)
2049 int hue = 0, port_value, port_min, port_max;
2051 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2052 (!strcasecmp(name, "brightness")))
2053 port_value = value;
2054 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2055 (!strcasecmp(name, "contrast")))
2056 port_value = value;
2057 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2058 (!strcasecmp(name, "saturation")))
2059 port_value = value;
2060 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2061 (!strcasecmp(name, "hue")))
2063 port_value = value;
2064 hue = 1;
2065 } else
2066 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2067 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2068 (!strcasecmp(name, "red_intensity")))
2069 port_value = value;
2070 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2071 && (!strcasecmp(name, "green_intensity")))
2072 port_value = value;
2073 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2074 && (!strcasecmp(name, "blue_intensity")))
2075 port_value = value;
2076 else
2077 continue;
2079 port_min = attributes[i].min_value;
2080 port_max = attributes[i].max_value;
2082 /* nvidia hue workaround */
2083 if (hue && port_min == 0 && port_max == 360)
2085 port_value =
2086 (port_value >=
2087 0) ? (port_value - 100) : (port_value + 100);
2089 // -100 -> min
2090 // 0 -> (max+min)/2
2091 // +100 -> max
2092 port_value =
2093 (port_value + 100) * (port_max - port_min) / 200 +
2094 port_min;
2095 XvSetPortAttribute(vo->x11->display, xv_port, xv_atom, port_value);
2096 return (VO_TRUE);
2099 return (VO_FALSE);
2102 int vo_xv_get_eq(struct vo *vo, uint32_t xv_port, char *name, int *value)
2105 XvAttribute *attributes;
2106 int i, howmany, xv_atom;
2108 /* get available attributes */
2109 attributes = XvQueryPortAttributes(vo->x11->display, xv_port, &howmany);
2110 for (i = 0; i < howmany && attributes; i++)
2111 if (attributes[i].flags & XvGettable)
2113 xv_atom = XInternAtom(vo->x11->display, attributes[i].name, True);
2114 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2115 then trigger it if it's ok so that the other values are at default upon query */
2116 if (xv_atom != None)
2118 int val, port_value = 0, port_min, port_max;
2120 XvGetPortAttribute(vo->x11->display, xv_port, xv_atom,
2121 &port_value);
2123 port_min = attributes[i].min_value;
2124 port_max = attributes[i].max_value;
2125 val =
2126 (port_value - port_min) * 200 / (port_max - port_min) -
2127 100;
2129 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2130 (!strcasecmp(name, "brightness")))
2131 *value = val;
2132 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2133 (!strcasecmp(name, "contrast")))
2134 *value = val;
2135 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2136 (!strcasecmp(name, "saturation")))
2137 *value = val;
2138 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2139 (!strcasecmp(name, "hue")))
2141 /* nasty nvidia detect */
2142 if (port_min == 0 && port_max == 360)
2143 *value = (val >= 0) ? (val - 100) : (val + 100);
2144 else
2145 *value = val;
2146 } else
2147 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2148 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2149 (!strcasecmp(name, "red_intensity")))
2150 *value = val;
2151 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2152 && (!strcasecmp(name, "green_intensity")))
2153 *value = val;
2154 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2155 && (!strcasecmp(name, "blue_intensity")))
2156 *value = val;
2157 else
2158 continue;
2160 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2161 name, *value);
2162 return (VO_TRUE);
2165 return (VO_FALSE);
2169 * \brief Interns the requested atom if it is available.
2171 * \param atom_name String containing the name of the requested atom.
2173 * \return Returns the atom if available, else None is returned.
2176 static Atom xv_intern_atom_if_exists(struct vo_x11_state *x11,
2177 char const *atom_name)
2179 XvAttribute * attributes;
2180 int attrib_count,i;
2181 Atom xv_atom = None;
2183 attributes = XvQueryPortAttributes(x11->display, x11->xv_port, &attrib_count );
2184 if( attributes!=NULL )
2186 for ( i = 0; i < attrib_count; ++i )
2188 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2190 xv_atom = XInternAtom(x11->display, atom_name, False );
2191 break; // found what we want, break out
2194 XFree( attributes );
2197 return xv_atom;
2201 * \brief Try to enable vsync for xv.
2202 * \return Returns -1 if not available, 0 on failure and 1 on success.
2204 int vo_xv_enable_vsync(struct vo *vo)
2206 struct vo_x11_state *x11 = vo->x11;
2207 Atom xv_atom = xv_intern_atom_if_exists(x11, "XV_SYNC_TO_VBLANK");
2208 if (xv_atom == None)
2209 return -1;
2210 return XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1) == Success;
2214 * \brief Get maximum supported source image dimensions.
2216 * This function does not set the variables pointed to by
2217 * width and height if the information could not be retrieved,
2218 * so the caller is reponsible for properly initializing them.
2220 * \param width [out] The maximum width gets stored here.
2221 * \param height [out] The maximum height gets stored here.
2224 void vo_xv_get_max_img_dim(struct vo *vo, uint32_t * width, uint32_t * height)
2226 struct vo_x11_state *x11 = vo->x11;
2227 XvEncodingInfo * encodings;
2228 //unsigned long num_encodings, idx; to int or too long?!
2229 unsigned int num_encodings, idx;
2231 XvQueryEncodings(x11->display, x11->xv_port, &num_encodings, &encodings);
2233 if ( encodings )
2235 for ( idx = 0; idx < num_encodings; ++idx )
2237 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2239 *width = encodings[idx].width;
2240 *height = encodings[idx].height;
2241 break;
2246 mp_msg( MSGT_VO, MSGL_V,
2247 "[xv common] Maximum source image dimensions: %ux%u\n",
2248 *width, *height );
2250 XvFreeEncodingInfo( encodings );
2254 * \brief Print information about the colorkey method and source.
2256 * \param ck_handling Integer value containing the information about
2257 * colorkey handling (see x11_common.h).
2259 * Outputs the content of |ck_handling| as a readable message.
2262 static void vo_xv_print_ck_info(struct vo_x11_state *x11)
2264 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2266 switch ( x11->xv_ck_info.method )
2268 case CK_METHOD_NONE:
2269 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2270 case CK_METHOD_AUTOPAINT:
2271 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2272 case CK_METHOD_MANUALFILL:
2273 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2274 case CK_METHOD_BACKGROUND:
2275 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2278 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2280 switch ( x11->xv_ck_info.source )
2282 case CK_SRC_CUR:
2283 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2284 x11->xv_colorkey );
2285 break;
2286 case CK_SRC_USE:
2287 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2289 mp_msg( MSGT_VO, MSGL_V,
2290 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2291 x11->xv_colorkey );
2293 else
2295 mp_msg( MSGT_VO, MSGL_V,
2296 "Using colorkey from MPlayer (0x%06lx)."
2297 " Use -colorkey to change.\n",
2298 x11->xv_colorkey );
2300 break;
2301 case CK_SRC_SET:
2302 mp_msg( MSGT_VO, MSGL_V,
2303 "Setting and using colorkey from MPlayer (0x%06lx)."
2304 " Use -colorkey to change.\n",
2305 x11->xv_colorkey );
2306 break;
2310 * \brief Init colorkey depending on the settings in xv_ck_info.
2312 * \return Returns 0 on failure and 1 on success.
2314 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2315 * flags in xv_ck_info.
2317 * Possiblilities:
2318 * * Methods
2319 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2320 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2321 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2322 * * Sources
2323 * - use currently set colorkey ( CK_SRC_CUR )
2324 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2325 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2327 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2328 * we don't draw anything as this means it was forced to off.
2330 int vo_xv_init_colorkey(struct vo *vo)
2332 struct vo_x11_state *x11 = vo->x11;
2333 Atom xv_atom;
2334 int rez;
2336 /* check if colorkeying is needed */
2337 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_COLORKEY");
2339 /* if we have to deal with colorkeying ... */
2340 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2342 /* check if we should use the colorkey specified in vo_colorkey */
2343 if ( x11->xv_ck_info.source != CK_SRC_CUR )
2345 x11->xv_colorkey = vo_colorkey;
2347 /* check if we have to set the colorkey too */
2348 if ( x11->xv_ck_info.source == CK_SRC_SET )
2350 xv_atom = XInternAtom(x11->display, "XV_COLORKEY",False);
2352 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, vo_colorkey);
2353 if ( rez != Success )
2355 mp_msg( MSGT_VO, MSGL_FATAL,
2356 "[xv common] Couldn't set colorkey!\n" );
2357 return 0; // error setting colorkey
2361 else
2363 int colorkey_ret;
2365 rez=XvGetPortAttribute(x11->display,x11->xv_port, xv_atom, &colorkey_ret);
2366 if ( rez == Success )
2368 x11->xv_colorkey = colorkey_ret;
2370 else
2372 mp_msg( MSGT_VO, MSGL_FATAL,
2373 "[xv common] Couldn't get colorkey!"
2374 "Maybe the selected Xv port has no overlay.\n" );
2375 return 0; // error getting colorkey
2379 xv_atom = xv_intern_atom_if_exists(vo->x11, "XV_AUTOPAINT_COLORKEY");
2381 /* should we draw the colorkey ourselves or activate autopainting? */
2382 if ( x11->xv_ck_info.method == CK_METHOD_AUTOPAINT )
2384 rez = !Success; // reset rez to something different than Success
2386 if ( xv_atom != None ) // autopaint is supported
2388 rez = XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 1);
2391 if ( rez != Success )
2393 // fallback to manual colorkey drawing
2394 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2397 else // disable colorkey autopainting if supported
2399 if ( xv_atom != None ) // we have autopaint attribute
2401 XvSetPortAttribute(x11->display, x11->xv_port, xv_atom, 0);
2405 else // do no colorkey drawing at all
2407 x11->xv_ck_info.method = CK_METHOD_NONE;
2408 } /* end: should we draw colorkey */
2410 /* output information about the current colorkey settings */
2411 vo_xv_print_ck_info(x11);
2413 return 1; // success
2417 * \brief Draw the colorkey on the video window.
2419 * Draws the colorkey depending on the set method ( colorkey_handling ).
2421 * Also draws the black bars ( when the video doesn't fit the display in
2422 * fullscreen ) separately, so they don't overlap with the video area.
2423 * It doesn't call XFlush.
2426 void vo_xv_draw_colorkey(struct vo *vo, int32_t x, int32_t y,
2427 int32_t w, int32_t h)
2429 struct MPOpts *opts = vo->opts;
2430 struct vo_x11_state *x11 = vo->x11;
2431 if( x11->xv_ck_info.method == CK_METHOD_MANUALFILL ||
2432 x11->xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2434 XSetForeground(x11->display, x11->vo_gc, x11->xv_colorkey );
2435 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2436 x, y,
2437 w, h );
2440 /* draw black bars if needed */
2441 /* TODO! move this to vo_x11_clearwindow_part() */
2442 if ( vo_fs )
2444 XSetForeground(x11->display, x11->vo_gc, 0 );
2445 /* making non-overlap fills, requires 8 checks instead of 4 */
2446 if ( y > 0 )
2447 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2448 0, 0,
2449 opts->vo_screenwidth, y);
2450 if (x > 0)
2451 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2452 0, 0,
2453 x, opts->vo_screenheight);
2454 if (x + w < opts->vo_screenwidth)
2455 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2456 x + w, 0,
2457 opts->vo_screenwidth, opts->vo_screenheight);
2458 if (y + h < opts->vo_screenheight)
2459 XFillRectangle(x11->display, x11->window, x11->vo_gc,
2460 0, y + h,
2461 opts->vo_screenwidth, opts->vo_screenheight);
2465 /** \brief Tests if a valid argument for the ck suboption was given. */
2466 int xv_test_ck( void * arg )
2468 strarg_t * strarg = (strarg_t *)arg;
2470 if ( strargcmp( strarg, "use" ) == 0 ||
2471 strargcmp( strarg, "set" ) == 0 ||
2472 strargcmp( strarg, "cur" ) == 0 )
2474 return 1;
2477 return 0;
2479 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
2480 int xv_test_ckm( void * arg )
2482 strarg_t * strarg = (strarg_t *)arg;
2484 if ( strargcmp( strarg, "bg" ) == 0 ||
2485 strargcmp( strarg, "man" ) == 0 ||
2486 strargcmp( strarg, "auto" ) == 0 )
2488 return 1;
2491 return 0;
2495 * \brief Modify the colorkey_handling var according to str
2497 * Checks if a valid pointer ( not NULL ) to the string
2498 * was given. And in that case modifies the colorkey_handling
2499 * var to reflect the requested behaviour.
2500 * If nothing happens the content of colorkey_handling stays
2501 * the same.
2503 * \param str Pointer to the string or NULL
2506 void xv_setup_colorkeyhandling(struct vo *vo, const char *ck_method_str,
2507 const char *ck_str)
2509 struct vo_x11_state *x11 = vo->x11;
2510 /* check if a valid pointer to the string was passed */
2511 if ( ck_str )
2513 if ( strncmp( ck_str, "use", 3 ) == 0 )
2515 x11->xv_ck_info.source = CK_SRC_USE;
2517 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2519 x11->xv_ck_info.source = CK_SRC_SET;
2522 /* check if a valid pointer to the string was passed */
2523 if ( ck_method_str )
2525 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2527 x11->xv_ck_info.method = CK_METHOD_BACKGROUND;
2529 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2531 x11->xv_ck_info.method = CK_METHOD_MANUALFILL;
2533 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2535 x11->xv_ck_info.method = CK_METHOD_AUTOPAINT;
2540 #endif
2542 struct vo_x11_state *vo_x11_init_state(void)
2544 struct vo_x11_state *s = talloc_ptrtype(NULL, s);
2545 *s = (struct vo_x11_state){
2546 .xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR },
2547 .olddecor = MWM_DECOR_ALL,
2548 .oldfuncs = MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
2549 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE,
2550 .old_gravity = NorthWestGravity,
2552 return s;