1 /*
2 * This file is part of MPlayer.
3 *
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <math.h>
22 #include <inttypes.h>
23 #include <limits.h>
24 #include <locale.h>
25
26 #include "config.h"
27 #include "mp_msg.h"
28 #include "mp_fifo.h"
29 #include "libavutil/common.h"
30 #include "libavutil/avstring.h"
31 #include "x11_common.h"
32
33 #include <string.h>
34 #include <unistd.h>
35 #include <assert.h>
36
37 #include "video_out.h"
38 #include "aspect.h"
39 #include "geometry.h"
40 #include "help_mp.h"
41 #include "osdep/timer.h"
42
43 #include <X11/Xmd.h>
44 #include <X11/Xlib.h>
45 #include <X11/Xutil.h>
46 #include <X11/Xatom.h>
47
48 #ifdef CONFIG_XSS
49 #include <X11/extensions/scrnsaver.h>
50 #endif
51
52 #ifdef CONFIG_XDPMS
53 #include <X11/extensions/dpms.h>
54 #endif
55
56 #ifdef CONFIG_XINERAMA
57 #include <X11/extensions/Xinerama.h>
58 #endif
59
60 #ifdef CONFIG_XF86VM
61 #include <X11/extensions/xf86vmode.h>
62 #endif
63
64 #ifdef CONFIG_XF86XK
65 #include <X11/XF86keysym.h>
66 #endif
67
68 #ifdef CONFIG_XV
69 #include <X11/extensions/Xv.h>
70 #include <X11/extensions/Xvlib.h>
71
72 #include "subopt-helper.h"
73 #endif
74
75 #include "input/input.h"
76 #include "input/mouse.h"
77
78 #ifdef CONFIG_GUI
79 #include "gui/interface.h"
80 #include "mplayer.h"
81 #endif
82
83 #define WIN_LAYER_ONBOTTOM 2
84 #define WIN_LAYER_NORMAL 4
85 #define WIN_LAYER_ONTOP 6
86 #define WIN_LAYER_ABOVE_DOCK 10
87
88 static int fs_layer = WIN_LAYER_ABOVE_DOCK;
89 static int orig_layer = 0;
90 static int old_gravity = NorthWestGravity;
91
92 int stop_xscreensaver = 1;
93
94 static int dpms_disabled = 0;
95
96 char *mDisplayName;
97 Display *mDisplay;
98 Window mRootWin;
99 int mScreen;
100 int mLocalDisplay;
101
102 /* output window id */
103 int vo_mouse_autohide = 0;
104 int vo_wm_type = 0;
105 int vo_fs_type = 0; // needs to be accessible for GUI X11 code
106 static int window_state;
107 static int vo_fs_flip = 0;
108 char **vo_fstype_list;
109
110 /* 1 means that the WM is metacity (broken as hell) */
111 int metacity_hack = 0;
112
113 static Atom XA_NET_SUPPORTED;
114 static Atom XA_NET_WM_STATE;
115 static Atom XA_NET_WM_STATE_FULLSCREEN;
116 static Atom XA_NET_WM_STATE_ABOVE;
117 static Atom XA_NET_WM_STATE_STAYS_ON_TOP;
118 static Atom XA_NET_WM_STATE_BELOW;
119 static Atom XA_NET_WM_PID;
120 static Atom XA_NET_WM_NAME;
121 static Atom XA_WIN_PROTOCOLS;
122 static Atom XA_WIN_LAYER;
123 static Atom XA_WIN_HINTS;
124 static Atom XAWM_PROTOCOLS;
125 static Atom XAWM_DELETE_WINDOW;
126 static Atom XAUTF8_STRING;
127
128 #define XA_INIT(x) XA##x = XInternAtom(mDisplay, #x, False)
129
130 static int vo_old_x = 0;
131 static int vo_old_y = 0;
132 static int vo_old_width = 0;
133 static int vo_old_height = 0;
134
135 #ifdef CONFIG_XF86VM
136 static int modecount;
137 static XF86VidModeModeInfo **vidmodes;
138 static XF86VidModeModeLine modeline;
139 #endif
140
141 static int vo_x11_get_fs_type(int supported);
142
143
144 /*
145 * Sends the EWMH fullscreen state event.
146 *
147 * win: id of the window to which the event shall be sent
148 * action: could be one of _NET_WM_STATE_REMOVE -- remove state
149 * _NET_WM_STATE_ADD -- add state
150 * _NET_WM_STATE_TOGGLE -- toggle
151 */
vo_x11_ewmh_fullscreen(Window win,int action)152 void vo_x11_ewmh_fullscreen(Window win, int action)
153 {
154 assert(action == _NET_WM_STATE_REMOVE ||
155 action == _NET_WM_STATE_ADD || action == _NET_WM_STATE_TOGGLE);
156
157 if (vo_fs_type & vo_wm_FULLSCREEN)
158 {
159 XEvent xev;
160
161 /* init X event structure for _NET_WM_FULLSCREEN client message */
162 xev.xclient.type = ClientMessage;
163 xev.xclient.serial = 0;
164 xev.xclient.send_event = True;
165 xev.xclient.message_type = XA_NET_WM_STATE;
166 xev.xclient.window = win;
167 xev.xclient.format = 32;
168 xev.xclient.data.l[0] = action;
169 xev.xclient.data.l[1] = XA_NET_WM_STATE_FULLSCREEN;
170 xev.xclient.data.l[2] = 0;
171 xev.xclient.data.l[3] = 0;
172 xev.xclient.data.l[4] = 0;
173
174 /* finally send that damn thing */
175 if (!XSendEvent(mDisplay, DefaultRootWindow(mDisplay), False,
176 SubstructureRedirectMask | SubstructureNotifyMask,
177 &xev))
178 {
179 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_EwmhFullscreenStateFailed);
180 }
181 }
182 }
183
vo_hidecursor(Display * disp,Window win)184 static void vo_hidecursor(Display * disp, Window win)
185 {
186 Cursor no_ptr;
187 Pixmap bm_no;
188 XColor black, dummy;
189 Colormap colormap;
190 static char bm_no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
191
192 if (WinID >= 0)
193 return; // do not hide if attached to an existing window
194
195 colormap = DefaultColormap(disp, DefaultScreen(disp));
196 if ( !XAllocNamedColor(disp, colormap, "black", &black, &dummy) )
197 {
198 return; // color alloc failed, give up
199 }
200 bm_no = XCreateBitmapFromData(disp, win, bm_no_data, 8, 8);
201 no_ptr = XCreatePixmapCursor(disp, bm_no, bm_no, &black, &black, 0, 0);
202 XDefineCursor(disp, win, no_ptr);
203 XFreeCursor(disp, no_ptr);
204 if (bm_no != None)
205 XFreePixmap(disp, bm_no);
206 XFreeColors(disp,colormap,&black.pixel,1,0);
207 }
208
vo_showcursor(Display * disp,Window win)209 static void vo_showcursor(Display * disp, Window win)
210 {
211 if (WinID >= 0)
212 return; // do not show if attached to an existing window
213 XDefineCursor(disp, win, 0);
214 }
215
x11_errorhandler(Display * display,XErrorEvent * event)216 static int x11_errorhandler(Display * display, XErrorEvent * event)
217 {
218 #define MSGLEN 60
219 char msg[MSGLEN];
220
221 XGetErrorText(display, event->error_code, (char *) &msg, MSGLEN);
222
223 mp_msg(MSGT_VO, MSGL_ERR, MSGTR_X11Error, msg);
224
225 mp_msg(MSGT_VO, MSGL_V,
226 "Type: %x, display: %p, resourceid: %lx, serial: %lx\n",
227 event->type, event->display, event->resourceid, event->serial);
228 mp_msg(MSGT_VO, MSGL_V,
229 "Error code: %x, request code: %x, minor code: %x\n",
230 event->error_code, event->request_code, event->minor_code);
231
232 // abort();
233 return 0;
234 #undef MSGLEN
235 }
236
fstype_help(void)237 void fstype_help(void)
238 {
239 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_AvailableFsType);
240 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FULL_SCREEN_TYPES\n");
241
242 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "none",
243 "don't set fullscreen window layer");
244 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer",
245 "use _WIN_LAYER hint with default layer");
246 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "layer=<0..15>",
247 "use _WIN_LAYER hint with a given layer number");
248 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "netwm",
249 "force NETWM style");
250 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "above",
251 "use _NETWM_STATE_ABOVE hint if available");
252 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "below",
253 "use _NETWM_STATE_BELOW hint if available");
254 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "fullscreen",
255 "use _NETWM_STATE_FULLSCREEN hint if available");
256 mp_msg(MSGT_VO, MSGL_INFO, " %-15s %s\n", "stays_on_top",
257 "use _NETWM_STATE_STAYS_ON_TOP hint if available");
258 mp_msg(MSGT_VO, MSGL_INFO,
259 "You can also negate individual flags by preceding them with a '-' character");
260 mp_msg(MSGT_VO, MSGL_INFO, "\n");
261 }
262
fstype_dump(int fstype)263 static void fstype_dump(int fstype)
264 {
265 if (fstype)
266 {
267 mp_msg(MSGT_VO, MSGL_V, "[x11] Current fstype setting honours");
268 if (fstype & vo_wm_LAYER)
269 mp_msg(MSGT_VO, MSGL_V, " LAYER");
270 if (fstype & vo_wm_FULLSCREEN)
271 mp_msg(MSGT_VO, MSGL_V, " FULLSCREEN");
272 if (fstype & vo_wm_STAYS_ON_TOP)
273 mp_msg(MSGT_VO, MSGL_V, " STAYS_ON_TOP");
274 if (fstype & vo_wm_ABOVE)
275 mp_msg(MSGT_VO, MSGL_V, " ABOVE");
276 if (fstype & vo_wm_BELOW)
277 mp_msg(MSGT_VO, MSGL_V, " BELOW");
278 mp_msg(MSGT_VO, MSGL_V, " X atoms\n");
279 } else
280 mp_msg(MSGT_VO, MSGL_V,
281 "[x11] Current fstype setting doesn't honour any X atoms\n");
282 }
283
net_wm_support_state_test(Atom atom)284 static int net_wm_support_state_test(Atom atom)
285 {
286 #define NET_WM_STATE_TEST(x) { if (atom == XA_NET_WM_STATE_##x) { mp_msg( MSGT_VO,MSGL_V, "[x11] Detected wm supports " #x " state.\n" ); return vo_wm_##x; } }
287
288 NET_WM_STATE_TEST(FULLSCREEN);
289 NET_WM_STATE_TEST(ABOVE);
290 NET_WM_STATE_TEST(STAYS_ON_TOP);
291 NET_WM_STATE_TEST(BELOW);
292 return 0;
293 }
294
x11_get_property(Atom type,Atom ** args,unsigned long * nitems)295 static int x11_get_property(Atom type, Atom ** args, unsigned long *nitems)
296 {
297 int format;
298 unsigned long bytesafter;
299
300 return Success ==
301 XGetWindowProperty(mDisplay, mRootWin, type, 0, 16384, False,
302 AnyPropertyType, &type, &format, nitems,
303 &bytesafter, (unsigned char **) args)
304 && *nitems > 0;
305 }
306
vo_wm_detect(void)307 static int vo_wm_detect(void)
308 {
309 int i;
310 int wm = 0;
311 unsigned long nitems;
312 Atom *args = NULL;
313
314 if (WinID >= 0)
315 return 0;
316
317 // -- supports layers
318 if (x11_get_property(XA_WIN_PROTOCOLS, &args, &nitems))
319 {
320 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports layers.\n");
321 for (i = 0; i < nitems; i++)
322 {
323 if (args[i] == XA_WIN_LAYER)
324 {
325 wm |= vo_wm_LAYER;
326 metacity_hack |= 1;
327 } else
328 /* metacity is the only window manager I know which reports
329 * supporting only the _WIN_LAYER hint in _WIN_PROTOCOLS.
330 * (what's more support for it is broken) */
331 metacity_hack |= 2;
332 }
333 XFree(args);
334 if (wm && (metacity_hack == 1))
335 {
336 // metacity claims to support layers, but it is not the truth :-)
337 wm ^= vo_wm_LAYER;
338 mp_msg(MSGT_VO, MSGL_V,
339 "[x11] Using workaround for Metacity bugs.\n");
340 }
341 }
342 // --- netwm
343 if (x11_get_property(XA_NET_SUPPORTED, &args, &nitems))
344 {
345 mp_msg(MSGT_VO, MSGL_V, "[x11] Detected wm supports NetWM.\n");
346 for (i = 0; i < nitems; i++)
347 wm |= net_wm_support_state_test(args[i]);
348 XFree(args);
349 }
350
351 if (wm == 0)
352 mp_msg(MSGT_VO, MSGL_V, "[x11] Unknown wm type...\n");
353 return wm;
354 }
355
init_atoms(void)356 static void init_atoms(void)
357 {
358 XA_INIT(_NET_SUPPORTED);
359 XA_INIT(_NET_WM_STATE);
360 XA_INIT(_NET_WM_STATE_FULLSCREEN);
361 XA_INIT(_NET_WM_STATE_ABOVE);
362 XA_INIT(_NET_WM_STATE_STAYS_ON_TOP);
363 XA_INIT(_NET_WM_STATE_BELOW);
364 XA_INIT(_NET_WM_PID);
365 XA_INIT(_NET_WM_NAME);
366 XA_INIT(_WIN_PROTOCOLS);
367 XA_INIT(_WIN_LAYER);
368 XA_INIT(_WIN_HINTS);
369 XA_INIT(WM_PROTOCOLS);
370 XA_INIT(WM_DELETE_WINDOW);
371 XA_INIT(UTF8_STRING);
372 }
373
update_xinerama_info(void)374 void update_xinerama_info(void) {
375 xinerama_x = xinerama_y = 0;
376 #ifdef CONFIG_XINERAMA
377 if (xinerama_screen >= -1 && XineramaIsActive(mDisplay))
378 {
379 int screen = xinerama_screen;
380 XineramaScreenInfo *screens;
381 int num_screens;
382
383 screens = XineramaQueryScreens(mDisplay, &num_screens);
384 if (screen >= num_screens)
385 screen = num_screens - 1;
386 if (screen == -1) {
387 int x = vo_dx + vo_dwidth / 2;
388 int y = vo_dy + vo_dheight / 2;
389 for (screen = num_screens - 1; screen > 0; screen--) {
390 int left = screens[screen].x_org;
391 int right = left + screens[screen].width;
392 int top = screens[screen].y_org;
393 int bottom = top + screens[screen].height;
394 if (left <= x && x <= right && top <= y && y <= bottom)
395 break;
396 }
397 }
398 if (screen < 0)
399 screen = 0;
400 vo_screenwidth = screens[screen].width;
401 vo_screenheight = screens[screen].height;
402 xinerama_x = screens[screen].x_org;
403 xinerama_y = screens[screen].y_org;
404
405 XFree(screens);
406 }
407 #endif
408 aspect_save_screenres(vo_screenwidth, vo_screenheight);
409 }
410
vo_init(void)411 int vo_init(void)
412 {
413 // int mScreen;
414 int depth, bpp;
415 unsigned int mask;
416
417 XImage *mXImage = NULL;
418
419 // Window mRootWin;
420 XWindowAttributes attribs;
421 char *dispName;
422
423 if (vo_rootwin)
424 WinID = 0; // use root window
425
426 if (vo_depthonscreen)
427 {
428 saver_off(mDisplay);
429 return 1; // already called
430 }
431
432 // Required so that XLookupString returns UTF-8
433 if (!setlocale(LC_CTYPE, "en_US.UTF-8"))
434 mp_msg(MSGT_VO, MSGL_WARN, MSGTR_CouldntFindUTF8Locale);
435 XSetErrorHandler(x11_errorhandler);
436
437 dispName = XDisplayName(mDisplayName);
438
439 mp_msg(MSGT_VO, MSGL_V, "X11 opening display: %s\n", dispName);
440
441 mDisplay = XOpenDisplay(dispName);
442 if (!mDisplay)
443 {
444 mp_msg(MSGT_VO, MSGL_ERR,
445 MSGTR_CouldntOpenDisplay, dispName);
446 return 0;
447 }
448 mScreen = DefaultScreen(mDisplay); // screen ID
449 mRootWin = RootWindow(mDisplay, mScreen); // root window ID
450
451 init_atoms();
452
453 #ifdef CONFIG_XF86VM
454 {
455 int clock;
456
457 XF86VidModeGetModeLine(mDisplay, mScreen, &clock, &modeline);
458 if (!vo_screenwidth)
459 vo_screenwidth = modeline.hdisplay;
460 if (!vo_screenheight)
461 vo_screenheight = modeline.vdisplay;
462 }
463 #endif
464 {
465 if (!vo_screenwidth)
466 vo_screenwidth = DisplayWidth(mDisplay, mScreen);
467 if (!vo_screenheight)
468 vo_screenheight = DisplayHeight(mDisplay, mScreen);
469 }
470 // get color depth (from root window, or the best visual):
471 XGetWindowAttributes(mDisplay, mRootWin, &attribs);
472 depth = attribs.depth;
473
474 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
475 {
476 Visual *visual;
477
478 depth = vo_find_depth_from_visuals(mDisplay, mScreen, &visual);
479 if (depth != -1)
480 mXImage = XCreateImage(mDisplay, visual, depth, ZPixmap,
481 0, NULL, 1, 1, 8, 1);
482 } else
483 mXImage =
484 XGetImage(mDisplay, mRootWin, 0, 0, 1, 1, AllPlanes, ZPixmap);
485
486 vo_depthonscreen = depth; // display depth on screen
487
488 // get bits/pixel from XImage structure:
489 if (mXImage == NULL)
490 {
491 mask = 0;
492 } else
493 {
494 /*
495 * for the depth==24 case, the XImage structures might use
496 * 24 or 32 bits of data per pixel. The global variable
497 * vo_depthonscreen stores the amount of data per pixel in the
498 * XImage structure!
499 *
500 * Maybe we should rename vo_depthonscreen to (or add) vo_bpp?
501 */
502 bpp = mXImage->bits_per_pixel;
503 if ((vo_depthonscreen + 7) / 8 != (bpp + 7) / 8)
504 vo_depthonscreen = bpp; // by A'rpi
505 mask =
506 mXImage->red_mask | mXImage->green_mask | mXImage->blue_mask;
507 mp_msg(MSGT_VO, MSGL_V,
508 "vo: X11 color mask: %X (R:%lX G:%lX B:%lX)\n", mask,
509 mXImage->red_mask, mXImage->green_mask, mXImage->blue_mask);
510 XDestroyImage(mXImage);
511 }
512 if (((vo_depthonscreen + 7) / 8) == 2)
513 {
514 if (mask == 0x7FFF)
515 vo_depthonscreen = 15;
516 else if (mask == 0xFFFF)
517 vo_depthonscreen = 16;
518 }
519 // XCloseDisplay( mDisplay );
520 /* slightly improved local display detection AST */
521 if (strncmp(dispName, "unix:", 5) == 0)
522 dispName += 4;
523 else if (strncmp(dispName, "localhost:", 10) == 0)
524 dispName += 9;
525 if (*dispName == ':' && atoi(dispName + 1) < 10)
526 mLocalDisplay = 1;
527 else
528 mLocalDisplay = 0;
529 mp_msg(MSGT_VO, MSGL_V,
530 "vo: X11 running at %dx%d with depth %d and %d bpp (\"%s\" => %s display)\n",
531 vo_screenwidth, vo_screenheight, depth, vo_depthonscreen,
532 dispName, mLocalDisplay ? "local" : "remote");
533
534 vo_wm_type = vo_wm_detect();
535
536 vo_fs_type = vo_x11_get_fs_type(vo_wm_type);
537
538 fstype_dump(vo_fs_type);
539
540 saver_off(mDisplay);
541 return 1;
542 }
543
vo_uninit(void)544 void vo_uninit(void)
545 {
546 if (!mDisplay)
547 {
548 mp_msg(MSGT_VO, MSGL_V,
549 "vo: x11 uninit called but X11 not initialized..\n");
550 return;
551 }
552 // if( !vo_depthonscreen ) return;
553 mp_msg(MSGT_VO, MSGL_V, "vo: uninit ...\n");
554 XSetErrorHandler(NULL);
555 XCloseDisplay(mDisplay);
556 vo_depthonscreen = 0;
557 mDisplay = NULL;
558 }
559
560 #include "osdep/keycodes.h"
561 #include "wskeys.h"
562
563 static const struct mp_keymap keysym_map[] = {
564 #ifdef XF86XK_AudioPause
565 {XF86XK_MenuKB, KEY_MENU},
566 {XF86XK_AudioPlay, KEY_PLAY}, {XF86XK_AudioPause, KEY_PAUSE}, {XF86XK_AudioStop, KEY_STOP},
567 {XF86XK_AudioPrev, KEY_PREV}, {XF86XK_AudioNext, KEY_NEXT},
568 {XF86XK_AudioMute, KEY_MUTE}, {XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN}, {XF86XK_AudioRaiseVolume, KEY_VOLUME_UP},
569 #endif
570 {0, 0}
571 };
572
vo_x11_putkey_ext(int keysym)573 static int vo_x11_putkey_ext(int keysym)
574 {
575 int mpkey = lookup_keymap_table(keysym_map, keysym);
576 if (mpkey)
577 mplayer_put_key(mpkey);
578 return mpkey != 0;
579 }
580
581 static const struct mp_keymap keymap[] = {
582 // special keys
583 {wsPause, KEY_PAUSE}, {wsEscape, KEY_ESC}, {wsBackSpace, KEY_BS},
584 {wsTab, KEY_TAB}, {wsEnter, KEY_ENTER},
585
586 // cursor keys
587 {wsLeft, KEY_LEFT}, {wsRight, KEY_RIGHT}, {wsUp, KEY_UP}, {wsDown, KEY_DOWN},
588
589 // navigation block
590 {wsInsert, KEY_INSERT}, {wsDelete, KEY_DELETE}, {wsHome, KEY_HOME}, {wsEnd, KEY_END},
591 {wsPageUp, KEY_PAGE_UP}, {wsPageDown, KEY_PAGE_DOWN},
592
593 // F-keys
594 {wsF1, KEY_F+1}, {wsF2, KEY_F+2}, {wsF3, KEY_F+3}, {wsF4, KEY_F+4},
595 {wsF5, KEY_F+5}, {wsF6, KEY_F+6}, {wsF7, KEY_F+7}, {wsF8, KEY_F+8},
596 {wsF9, KEY_F+9}, {wsF10, KEY_F+10}, {wsF11, KEY_F+11}, {wsF12, KEY_F+12},
597
598 // numpad independent of numlock
599 {wsGrayMinus, '-'}, {wsGrayPlus, '+'}, {wsGrayMul, '*'}, {wsGrayDiv, '/'},
600 {wsGrayEnter, KEY_KPENTER},
601
602 // numpad with numlock
603 {wsGray0, KEY_KP0}, {wsGray1, KEY_KP1}, {wsGray2, KEY_KP2},
604 {wsGray3, KEY_KP3}, {wsGray4, KEY_KP4}, {wsGray5, KEY_KP5},
605 {wsGray6, KEY_KP6}, {wsGray7, KEY_KP7}, {wsGray8, KEY_KP8},
606 {wsGray9, KEY_KP9}, {wsGrayDecimal, KEY_KPDEC},
607
608 // numpad without numlock
609 {wsGrayInsert, KEY_KPINS}, {wsGrayEnd, KEY_KP1}, {wsGrayDown, KEY_KP2},
610 {wsGrayPgDn, KEY_KP3}, {wsGrayLeft, KEY_KP4}, {wsGray5Dup, KEY_KP5},
611 {wsGrayRight, KEY_KP6}, {wsGrayHome, KEY_KP7}, {wsGrayUp, KEY_KP8},
612 {wsGrayPgUp, KEY_KP9}, {wsGrayDelete, KEY_KPDEL},
613
614 {0, 0}
615 };
616
vo_x11_putkey(int key)617 void vo_x11_putkey(int key)
618 {
619 static const char *passthrough_keys = " -+*/<>`~!@#$%^&()_{}:;\"\',.?\\|=[]";
620 int mpkey = 0;
621 if ((key >= 'a' && key <= 'z') ||
622 (key >= 'A' && key <= 'Z') ||
623 (key >= '0' && key <= '9') ||
624 (key > 0 && key < 256 && strchr(passthrough_keys, key)))
625 mpkey = key;
626
627 if (!mpkey)
628 mpkey = lookup_keymap_table(keymap, key);
629
630 if (mpkey)
631 mplayer_put_key(mpkey);
632 }
633
634
635 // ----- Motif header: -------
636
637 #define MWM_HINTS_FUNCTIONS (1L << 0)
638 #define MWM_HINTS_DECORATIONS (1L << 1)
639 #define MWM_HINTS_INPUT_MODE (1L << 2)
640 #define MWM_HINTS_STATUS (1L << 3)
641
642 #define MWM_FUNC_ALL (1L << 0)
643 #define MWM_FUNC_RESIZE (1L << 1)
644 #define MWM_FUNC_MOVE (1L << 2)
645 #define MWM_FUNC_MINIMIZE (1L << 3)
646 #define MWM_FUNC_MAXIMIZE (1L << 4)
647 #define MWM_FUNC_CLOSE (1L << 5)
648
649 #define MWM_DECOR_ALL (1L << 0)
650 #define MWM_DECOR_BORDER (1L << 1)
651 #define MWM_DECOR_RESIZEH (1L << 2)
652 #define MWM_DECOR_TITLE (1L << 3)
653 #define MWM_DECOR_MENU (1L << 4)
654 #define MWM_DECOR_MINIMIZE (1L << 5)
655 #define MWM_DECOR_MAXIMIZE (1L << 6)
656
657 #define MWM_INPUT_MODELESS 0
658 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
659 #define MWM_INPUT_SYSTEM_MODAL 2
660 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
661 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
662
663 #define MWM_TEAROFF_WINDOW (1L<<0)
664
665 typedef struct
666 {
667 long flags;
668 long functions;
669 long decorations;
670 long input_mode;
671 long state;
672 } MotifWmHints;
673
674 static MotifWmHints vo_MotifWmHints;
675 static Atom vo_MotifHints = None;
676
vo_x11_decoration(Display * vo_Display,Window w,int d)677 void vo_x11_decoration(Display * vo_Display, Window w, int d)
678 {
679 static unsigned int olddecor = MWM_DECOR_ALL;
680 static unsigned int oldfuncs =
681 MWM_FUNC_MOVE | MWM_FUNC_CLOSE | MWM_FUNC_MINIMIZE |
682 MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE;
683 Atom mtype;
684 int mformat;
685 unsigned long mn, mb;
686
687 if (WinID >= 0)
688 return;
689
690 if (vo_fsmode & 8)
691 {
692 XSetTransientForHint(vo_Display, w,
693 RootWindow(vo_Display, mScreen));
694 }
695
696 vo_MotifHints = XInternAtom(vo_Display, "_MOTIF_WM_HINTS", 0);
697 if (vo_MotifHints != None)
698 {
699 if (!d)
700 {
701 MotifWmHints *mhints = NULL;
702
703 XGetWindowProperty(vo_Display, w, vo_MotifHints, 0, 20, False,
704 vo_MotifHints, &mtype, &mformat, &mn,
705 &mb, (unsigned char **) &mhints);
706 if (mhints)
707 {
708 if (mhints->flags & MWM_HINTS_DECORATIONS)
709 olddecor = mhints->decorations;
710 if (mhints->flags & MWM_HINTS_FUNCTIONS)
711 oldfuncs = mhints->functions;
712 XFree(mhints);
713 }
714 }
715
716 memset(&vo_MotifWmHints, 0, sizeof(MotifWmHints));
717 vo_MotifWmHints.flags =
718 MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
719 if (d)
720 {
721 vo_MotifWmHints.functions = oldfuncs;
722 d = olddecor;
723 }
724 #if 0
725 vo_MotifWmHints.decorations =
726 d | ((vo_fsmode & 2) ? 0 : MWM_DECOR_MENU);
727 #else
728 vo_MotifWmHints.decorations =
729 d | ((vo_fsmode & 2) ? MWM_DECOR_MENU : 0);
730 #endif
731 XChangeProperty(vo_Display, w, vo_MotifHints, vo_MotifHints, 32,
732 PropModeReplace,
733 (unsigned char *) &vo_MotifWmHints,
734 (vo_fsmode & 4) ? 4 : 5);
735 }
736 }
737
vo_x11_classhint(Display * display,Window window,const char * name)738 void vo_x11_classhint(Display * display, Window window, const char *name)
739 {
740 XClassHint wmClass;
741 pid_t pid = getpid();
742 long prop = pid & 0x7FFFFFFF;
743
744 wmClass.res_name = vo_winname ? vo_winname : name;
745 wmClass.res_class = "MPlayer";
746 XSetClassHint(display, window, &wmClass);
747
748 /* PID sizes other than 32-bit are not handled by the EWMH spec */
749 if ((pid_t)prop != pid)
750 return;
751
752 XChangeProperty(display, window, XA_NET_WM_PID, XA_CARDINAL, 32,
753 PropModeReplace, (unsigned char *)&prop, 1);
754 }
755
756 Window vo_window = None;
757 GC vo_gc = None;
758 GC f_gc = None;
759 XSizeHints vo_hint;
760
vo_x11_uninit(void)761 void vo_x11_uninit(void)
762 {
763 saver_on(mDisplay);
764 if (vo_window != None)
765 vo_showcursor(mDisplay, vo_window);
766
767 if (f_gc != None)
768 {
769 XFreeGC(mDisplay, f_gc);
770 f_gc = None;
771 }
772 {
773 if (vo_gc != None)
774 {
775 XFreeGC(mDisplay, vo_gc);
776 vo_gc = None;
777 }
778 if (vo_window != None)
779 {
780 XClearWindow(mDisplay, vo_window);
781 if (WinID < 0 && vo_window != mRootWin)
782 {
783 XEvent xev;
784
785 XUnmapWindow(mDisplay, vo_window);
786 XSelectInput(mDisplay, vo_window, StructureNotifyMask);
787 XDestroyWindow(mDisplay, vo_window);
788 do
789 {
790 XNextEvent(mDisplay, &xev);
791 }
792 while (xev.type != DestroyNotify
793 || xev.xdestroywindow.event != vo_window);
794 }
795 vo_window = None;
796 }
797 vo_fs = 0;
798 vo_old_width = vo_old_height = 0;
799 }
800 }
801
802 static unsigned int mouse_timer;
803 static int mouse_waiting_hide;
804
check_resize(void)805 static int check_resize(void)
806 {
807 int old_w = vo_dwidth, old_h = vo_dheight;
808 int old_x = vo_dx, old_y = vo_dy;
809 int rc = 0;
810 vo_x11_update_geometry();
811 if (vo_dwidth != old_w || vo_dheight != old_h)
812 rc |= VO_EVENT_RESIZE;
813 if (vo_dx != old_x || vo_dy != old_y)
814 rc |= VO_EVENT_MOVE;
815 return rc;
816 }
817
to_utf8(const uint8_t * in)818 static int to_utf8(const uint8_t *in)
819 {
820 uint32_t v = 0;
821 GET_UTF8(v, *in++, goto err;)
822 if (*in || v >= KEY_BASE)
823 goto err;
824 return v;
825 err:
826 return 0;
827 }
828
fixup_ctrl_state(int * ctrl_state,int state)829 static void fixup_ctrl_state(int *ctrl_state, int state)
830 {
831 // Attempt to fix if somehow our state got out of
832 // sync with reality.
833 // This usually happens when a shortcut involving CTRL
834 // was used to switch to a different window/workspace.
835 if (*ctrl_state != !!(state & 4)) {
836 *ctrl_state = !!(state & 4);
837 mplayer_put_key(KEY_CTRL |
838 (*ctrl_state ? MP_KEY_DOWN : 0));
839 }
840 }
841
handle_x11_event(Display * mydisplay,XEvent * event)842 static int handle_x11_event(Display *mydisplay, XEvent *event)
843 {
844 int key = 0;
845 uint8_t buf[16] = {0};
846 KeySym keySym;
847 static XComposeStatus stat;
848 static int ctrl_state;
849 #ifdef CONFIG_GUI
850 if (use_gui)
851 {
852 gui(GUI_HANDLE_X_EVENT, event);
853 if (vo_window != event->xany.window)
854 return 0;
855 }
856 #endif
857 switch (event->type)
858 {
859 case Expose:
860 return VO_EVENT_EXPOSE;
861 case ConfigureNotify:
862 if (vo_window == None)
863 break;
864 return check_resize();
865 case KeyPress:
866 case KeyRelease:
867 {
868 int key, utf8;
869
870 #ifdef CONFIG_GUI
871 if ( use_gui ) { break; }
872 #endif
873
874 XLookupString(&event->xkey, buf, sizeof(buf), &keySym,
875 &stat);
876 key =
877 ((keySym & 0xff00) !=
878 0 ? ((keySym & 0x00ff) + 256) : (keySym));
879 utf8 = buf[0] > 0xc0 ? to_utf8(buf) : 0;
880 if (utf8) key = 0;
881 if (key == wsLeftCtrl || key == wsRightCtrl) {
882 ctrl_state = event->type == KeyPress;
883 mplayer_put_key(KEY_CTRL |
884 (ctrl_state ? MP_KEY_DOWN : 0));
885 } else if (event->type == KeyRelease) {
886 break;
887 }
888 fixup_ctrl_state(&ctrl_state, event->xkey.state);
889 if (!vo_x11_putkey_ext(keySym)) {
890 if (utf8) mplayer_put_key(utf8);
891 else vo_x11_putkey(key);
892 }
893 return VO_EVENT_KEYPRESS;
894 }
895 break;
896 case MotionNotify:
897 vo_mouse_movement(event->xmotion.x, event->xmotion.y);
898
899 return VO_EVENT_MOUSE;
900 case ButtonPress:
901 key = MP_KEY_DOWN;
902 /* Fallthrough, treat like release otherwise */
903 case ButtonRelease:
904 fixup_ctrl_state(&ctrl_state, event->xbutton.state);
905 #ifdef CONFIG_GUI
906 // Ignore mouse button 1-3 under GUI.
907 if (use_gui && (event->xbutton.button >= 1)
908 && (event->xbutton.button <= 3))
909 return VO_EVENT_MOUSE;
910 #endif
911 key |= MOUSE_BTN0 + event->xbutton.button - 1;
912 mplayer_put_key(key);
913 return VO_EVENT_MOUSE;
914 case PropertyNotify:
915 {
916 char *name =
917 XGetAtomName(mydisplay, event->xproperty.atom);
918
919 if (!name)
920 break;
921
922 XFree(name);
923 }
924 break;
925 case MapNotify:
926 if (WinID < 0) {
927 vo_hint.win_gravity = old_gravity;
928 XSetWMNormalHints(mDisplay, vo_window, &vo_hint);
929 vo_fs_flip = 0;
930 }
931 break;
932 case DestroyNotify:
933 mp_msg(MSGT_VO, MSGL_WARN, MSGTR_WindowDestroyed);
934 mplayer_put_key(KEY_CLOSE_WIN);
935 break;
936 case ClientMessage:
937 if (event->xclient.message_type == XAWM_PROTOCOLS &&
938 event->xclient.data.l[0] == XAWM_DELETE_WINDOW)
939 mplayer_put_key(KEY_CLOSE_WIN);
940 break;
941 }
942 return 0;
943 }
944
vo_x11_check_events(Display * mydisplay)945 int vo_x11_check_events(Display * mydisplay)
946 {
947 int ret = 0;
948 XEvent Event;
949
950 if (vo_mouse_autohide && mouse_waiting_hide &&
951 (GetTimerMS() - mouse_timer >= 1000)) {
952 vo_hidecursor(mydisplay, vo_window);
953 mouse_waiting_hide = 0;
954 }
955
956 if (WinID > 0)
957 ret |= check_resize();
958 while (XPending(mydisplay))
959 {
960 XNextEvent(mydisplay, &Event);
961 ret |= handle_x11_event(mydisplay, &Event);
962 }
963 if (vo_mouse_autohide && (ret & VO_EVENT_MOUSE))
964 {
965 vo_showcursor(mydisplay, vo_window);
966 mouse_waiting_hide = 1;
967 mouse_timer = GetTimerMS();
968 }
969 return ret;
970 }
971
vo_x11_update_fs_borders(void)972 static void vo_x11_update_fs_borders(void)
973 {
974 if (!vo_fs)
975 return;
976 if (vo_dwidth <= vo_fs_border_l + vo_fs_border_r ||
977 vo_dheight <= vo_fs_border_t + vo_fs_border_b) {
978 mp_msg(MSGT_VO, MSGL_ERR, "[x11] borders too wide, ignored.\n");
979 return;
980 }
981 vo_dwidth -= vo_fs_border_l + vo_fs_border_r;
982 vo_dheight -= vo_fs_border_t + vo_fs_border_b;
983 }
984
985 /**
986 * \brief sets the size and position of the non-fullscreen window.
987 */
vo_x11_nofs_sizepos(int x,int y,int width,int height)988 void vo_x11_nofs_sizepos(int x, int y, int width, int height)
989 {
990 vo_x11_sizehint(x, y, width, height, 0);
991 if (vo_fs) {
992 vo_old_x = x;
993 vo_old_y = y;
994 vo_old_width = width;
995 vo_old_height = height;
996 }
997 else
998 {
999 vo_dwidth = width;
1000 vo_dheight = height;
1001 XMoveResizeWindow(mDisplay, vo_window, x, y, width, height);
1002 }
1003 }
1004
vo_x11_sizehint(int x,int y,int width,int height,int max)1005 void vo_x11_sizehint(int x, int y, int width, int height, int max)
1006 {
1007 vo_hint.flags = 0;
1008 if (vo_keepaspect)
1009 {
1010 vo_hint.flags |= PAspect;
1011 vo_hint.min_aspect.x = width;
1012 vo_hint.min_aspect.y = height;
1013 vo_hint.max_aspect.x = width;
1014 vo_hint.max_aspect.y = height;
1015 }
1016
1017 vo_hint.flags |= PPosition | PSize;
1018 vo_hint.x = x;
1019 vo_hint.y = y;
1020 vo_hint.width = width;
1021 vo_hint.height = height;
1022 if (max)
1023 {
1024 vo_hint.flags |= PMaxSize;
1025 vo_hint.max_width = width;
1026 vo_hint.max_height = height;
1027 } else
1028 {
1029 vo_hint.max_width = 0;
1030 vo_hint.max_height = 0;
1031 }
1032
1033 // Set minimum height/width to 4 to avoid off-by-one errors
1034 // and because mga_vid requires a minimal size of 4 pixels.
1035 vo_hint.flags |= PMinSize;
1036 vo_hint.min_width = vo_hint.min_height = 4;
1037
1038 // Set the base size. A window manager might display the window
1039 // size to the user relative to this.
1040 // Setting these to width/height might be nice, but e.g. fluxbox can't handle it.
1041 vo_hint.flags |= PBaseSize;
1042 vo_hint.base_width = 0 /*width*/;
1043 vo_hint.base_height = 0 /*height*/;
1044
1045 vo_hint.flags |= PWinGravity;
1046 vo_hint.win_gravity = StaticGravity;
1047 XSetWMNormalHints(mDisplay, vo_window, &vo_hint);
1048 }
1049
vo_x11_get_gnome_layer(Display * mDisplay,Window win)1050 static int vo_x11_get_gnome_layer(Display * mDisplay, Window win)
1051 {
1052 Atom type;
1053 int format;
1054 unsigned long nitems;
1055 unsigned long bytesafter;
1056 unsigned short *args = NULL;
1057
1058 if (XGetWindowProperty(mDisplay, win, XA_WIN_LAYER, 0, 16384,
1059 False, AnyPropertyType, &type, &format, &nitems,
1060 &bytesafter,
1061 (unsigned char **) &args) == Success
1062 && nitems > 0 && args)
1063 {
1064 mp_msg(MSGT_VO, MSGL_V, "[x11] original window layer is %d.\n",
1065 *args);
1066 return *args;
1067 }
1068 return WIN_LAYER_NORMAL;
1069 }
1070
1071 //
vo_x11_create_smooth_window(Display * mDisplay,Window mRoot,Visual * vis,int x,int y,unsigned int width,unsigned int height,int depth,Colormap col_map)1072 Window vo_x11_create_smooth_window(Display * mDisplay, Window mRoot,
1073 Visual * vis, int x, int y,
1074 unsigned int width, unsigned int height,
1075 int depth, Colormap col_map)
1076 {
1077 unsigned long xswamask = CWBorderPixel;
1078 XSetWindowAttributes xswa;
1079 Window ret_win;
1080
1081 if (col_map != CopyFromParent)
1082 {
1083 xswa.colormap = col_map;
1084 xswamask |= CWColormap;
1085 }
1086 xswa.background_pixel = 0;
1087 xswa.border_pixel = 0;
1088 xswa.backing_store = NotUseful;
1089 xswa.bit_gravity = StaticGravity;
1090
1091 ret_win =
1092 XCreateWindow(mDisplay, mRoot, x, y, width, height, 0, depth,
1093 CopyFromParent, vis, xswamask, &xswa);
1094 XSetWMProtocols(mDisplay, ret_win, &XAWM_DELETE_WINDOW, 1);
1095 if (f_gc == None)
1096 f_gc = XCreateGC(mDisplay, ret_win, 0, 0);
1097 XSetForeground(mDisplay, f_gc, 0);
1098
1099 return ret_win;
1100 }
1101
1102 /**
1103 * \brief create and setup a window suitable for display
1104 * \param vis Visual to use for creating the window
1105 * \param x x position of window
1106 * \param y y position of window
1107 * \param width width of window
1108 * \param height height of window
1109 * \param flags flags for window creation.
1110 * Only VOFLAG_FULLSCREEN is supported so far.
1111 * \param col_map Colourmap for window or CopyFromParent if a specific colormap isn't needed
1112 * \param classname name to use for the classhint
1113 * \param title title for the window
1114 *
1115 * This also does the grunt-work like setting Window Manager hints etc.
1116 * If vo_window is already set it just moves and resizes it.
1117 */
vo_x11_create_vo_window(XVisualInfo * vis,int x,int y,unsigned int width,unsigned int height,int flags,Colormap col_map,const char * classname,const char * title)1118 void vo_x11_create_vo_window(XVisualInfo *vis, int x, int y,
1119 unsigned int width, unsigned int height, int flags,
1120 Colormap col_map,
1121 const char *classname, const char *title)
1122 {
1123 if (flags & VOFLAG_HIDDEN) {
1124 // unmapped windows cause lots of issues, in particular
1125 // -geometry might be ignore when finally mapping them etc.
1126 if (vo_window == None)
1127 vo_window = mRootWin;
1128 window_state = VOFLAG_HIDDEN;
1129 goto final;
1130 } else if (vo_window == mRootWin && (window_state & VOFLAG_HIDDEN)) {
1131 vo_window = None;
1132 }
1133 if (vo_wintitle)
1134 title = vo_wintitle;
1135 if (WinID >= 0) {
1136 vo_fs = flags & VOFLAG_FULLSCREEN;
1137 vo_window = WinID ? (Window)WinID : mRootWin;
1138 if (col_map != CopyFromParent) {
1139 unsigned long xswamask = CWColormap;
1140 XSetWindowAttributes xswa;
1141 xswa.colormap = col_map;
1142 XChangeWindowAttributes(mDisplay, vo_window, xswamask, &xswa);
1143 XInstallColormap(mDisplay, col_map);
1144 }
1145 if (WinID) {
1146 // Expose events can only really be handled by us, so request them.
1147 // Do not remove existing masks so GUI keeps working.
1148 XWindowAttributes attribs;
1149 XGetWindowAttributes(mDisplay, vo_window, &attribs);
1150 vo_x11_selectinput_witherr(mDisplay, vo_window,
1151 attribs.your_event_mask | ExposureMask);
1152 } else
1153 // Do not capture events since it might break the parent application
1154 // if it relies on events being forwarded to the parent of WinID.
1155 // It also is consistent with the w32_common.c code.
1156 vo_x11_selectinput_witherr(mDisplay, vo_window,
1157 StructureNotifyMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
1158 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1159
1160 vo_x11_update_geometry();
1161 goto final;
1162 }
1163 if (vo_window == None) {
1164 vo_fs = 0;
1165 vo_dwidth = width;
1166 vo_dheight = height;
1167 vo_x11_update_fs_borders();
1168 vo_window = vo_x11_create_smooth_window(mDisplay, mRootWin, vis->visual,
1169 x, y, width, height, vis->depth, col_map);
1170 window_state = VOFLAG_HIDDEN;
1171 }
1172 XStoreName(mDisplay, vo_window, title);
1173 XChangeProperty(mDisplay, vo_window, XA_NET_WM_NAME, XAUTF8_STRING,
1174 8, PropModeReplace, title, strlen(title));
1175 if (window_state & VOFLAG_HIDDEN) {
1176 XSizeHints hint;
1177 XEvent xev;
1178 window_state &= ~VOFLAG_HIDDEN;
1179 vo_x11_classhint(mDisplay, vo_window, classname);
1180 vo_hidecursor(mDisplay, vo_window);
1181 XSelectInput(mDisplay, vo_window, StructureNotifyMask);
1182 hint.x = x; hint.y = y;
1183 hint.width = width; hint.height = height;
1184 hint.flags = PSize;
1185 if (geometry_xy_changed)
1186 hint.flags |= PPosition;
1187 XSetStandardProperties(mDisplay, vo_window, title, title, None, NULL, 0, &hint);
1188 if (!vo_border) vo_x11_decoration(mDisplay, vo_window, 0);
1189 // map window
1190 XMapWindow(mDisplay, vo_window);
1191 // wait for map
1192 do {
1193 XNextEvent(mDisplay, &xev);
1194 handle_x11_event(mDisplay, &xev);
1195 } while (xev.type != MapNotify || xev.xmap.event != vo_window);
1196 vo_x11_clearwindow(mDisplay, vo_window);
1197 vo_x11_selectinput_witherr(mDisplay, vo_window,
1198 StructureNotifyMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
1199 ButtonPressMask | ButtonReleaseMask | ExposureMask);
1200 }
1201 if (vo_ontop) vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
1202 if (!geometry_xy_changed)
1203 vo_x11_update_geometry();
1204 vo_x11_nofs_sizepos(vo_dx, vo_dy, width, height);
1205 if (!!vo_fs != !!(flags & VOFLAG_FULLSCREEN))
1206 vo_x11_fullscreen();
1207 else if (vo_fs) {
1208 // if we are already in fullscreen do not switch back and forth, just
1209 // set the size values right.
1210 vo_dwidth = vo_screenwidth;
1211 vo_dheight = vo_screenheight;
1212 vo_x11_update_fs_borders();
1213 }
1214 final:
1215 if (vo_gc != None)
1216 XFreeGC(mDisplay, vo_gc);
1217 vo_gc = XCreateGC(mDisplay, vo_window, 0, NULL);
1218
1219 XSync(mDisplay, False);
1220 vo_mouse_autohide = 1;
1221 }
1222
vo_x11_clearwindow_part(Display * mDisplay,Window vo_window,int img_width,int img_height,int use_fs)1223 void vo_x11_clearwindow_part(Display * mDisplay, Window vo_window,
1224 int img_width, int img_height, int use_fs)
1225 {
1226 int u_dheight, u_dwidth, left_ov, left_ov2;
1227
1228 if (f_gc == None)
1229 return;
1230
1231 u_dheight = use_fs ? vo_screenheight : vo_dheight;
1232 u_dwidth = use_fs ? vo_screenwidth : vo_dwidth;
1233 if ((u_dheight <= img_height) && (u_dwidth <= img_width))
1234 return;
1235
1236 left_ov = (u_dheight - img_height) / 2;
1237 left_ov2 = (u_dwidth - img_width) / 2;
1238
1239 XFillRectangle(mDisplay, vo_window, f_gc, 0, 0, u_dwidth, left_ov);
1240 XFillRectangle(mDisplay, vo_window, f_gc, 0, u_dheight - left_ov - 1,
1241 u_dwidth, left_ov + 1);
1242
1243 if (u_dwidth > img_width)
1244 {
1245 XFillRectangle(mDisplay, vo_window, f_gc, 0, left_ov, left_ov2,
1246 img_height);
1247 XFillRectangle(mDisplay, vo_window, f_gc, u_dwidth - left_ov2 - 1,
1248 left_ov, left_ov2 + 1, img_height);
1249 }
1250
1251 XFlush(mDisplay);
1252 }
1253
vo_x11_clearwindow(Display * mDisplay,Window vo_window)1254 void vo_x11_clearwindow(Display * mDisplay, Window vo_window)
1255 {
1256 if (f_gc == None)
1257 return;
1258 XFillRectangle(mDisplay, vo_window, f_gc, 0, 0, vo_screenwidth,
1259 vo_screenheight);
1260 //
1261 XFlush(mDisplay);
1262 }
1263
1264
vo_x11_setlayer(Display * mDisplay,Window vo_window,int layer)1265 void vo_x11_setlayer(Display * mDisplay, Window vo_window, int layer)
1266 {
1267 if (WinID >= 0)
1268 return;
1269
1270 if (vo_fs_type & vo_wm_LAYER)
1271 {
1272 XClientMessageEvent xev;
1273
1274 if (!orig_layer)
1275 orig_layer = vo_x11_get_gnome_layer(mDisplay, vo_window);
1276
1277 memset(&xev, 0, sizeof(xev));
1278 xev.type = ClientMessage;
1279 xev.display = mDisplay;
1280 xev.window = vo_window;
1281 xev.message_type = XA_WIN_LAYER;
1282 xev.format = 32;
1283 xev.data.l[0] = layer ? fs_layer : orig_layer; // if not fullscreen, stay on default layer
1284 xev.data.l[1] = CurrentTime;
1285 mp_msg(MSGT_VO, MSGL_V,
1286 "[x11] Layered style stay on top (layer %ld).\n",
1287 xev.data.l[0]);
1288 XSendEvent(mDisplay, mRootWin, False, SubstructureNotifyMask,
1289 (XEvent *) & xev);
1290 } else if (vo_fs_type & vo_wm_NETWM)
1291 {
1292 XClientMessageEvent xev;
1293 char *state;
1294
1295 memset(&xev, 0, sizeof(xev));
1296 xev.type = ClientMessage;
1297 xev.message_type = XA_NET_WM_STATE;
1298 xev.display = mDisplay;
1299 xev.window = vo_window;
1300 xev.format = 32;
1301 xev.data.l[0] = layer;
1302
1303 if (vo_fs_type & vo_wm_STAYS_ON_TOP)
1304 xev.data.l[1] = XA_NET_WM_STATE_STAYS_ON_TOP;
1305 else if (vo_fs_type & vo_wm_ABOVE)
1306 xev.data.l[1] = XA_NET_WM_STATE_ABOVE;
1307 else if (vo_fs_type & vo_wm_FULLSCREEN)
1308 xev.data.l[1] = XA_NET_WM_STATE_FULLSCREEN;
1309 else if (vo_fs_type & vo_wm_BELOW)
1310 // This is not fallback. We can safely assume that the situation
1311 // where only NETWM_STATE_BELOW is supported doesn't exist.
1312 xev.data.l[1] = XA_NET_WM_STATE_BELOW;
1313
1314 XSendEvent(mDisplay, mRootWin, False, SubstructureRedirectMask,
1315 (XEvent *) & xev);
1316 state = XGetAtomName(mDisplay, xev.data.l[1]);
1317 mp_msg(MSGT_VO, MSGL_V,
1318 "[x11] NET style stay on top (layer %d). Using state %s.\n",
1319 layer, state);
1320 XFree(state);
1321 }
1322 }
1323
vo_x11_get_fs_type(int supported)1324 static int vo_x11_get_fs_type(int supported)
1325 {
1326 int i;
1327 int type = supported;
1328
1329 if (vo_fstype_list)
1330 {
1331 for (i = 0; vo_fstype_list[i]; i++)
1332 {
1333 int neg = 0;
1334 char *arg = vo_fstype_list[i];
1335
1336 if (vo_fstype_list[i][0] == '-')
1337 {
1338 neg = 1;
1339 arg = vo_fstype_list[i] + 1;
1340 }
1341
1342 if (!strncmp(arg, "layer", 5))
1343 {
1344 if (!neg && (arg[5] == '='))
1345 {
1346 char *endptr = NULL;
1347 int layer = strtol(vo_fstype_list[i] + 6, &endptr, 10);
1348
1349 if (endptr && *endptr == '\0' && layer >= 0
1350 && layer <= 15)
1351 fs_layer = layer;
1352 }
1353 if (neg)
1354 type &= ~vo_wm_LAYER;
1355 else
1356 type |= vo_wm_LAYER;
1357 } else if (!strcmp(arg, "above"))
1358 {
1359 if (neg)
1360 type &= ~vo_wm_ABOVE;
1361 else
1362 type |= vo_wm_ABOVE;
1363 } else if (!strcmp(arg, "fullscreen"))
1364 {
1365 if (neg)
1366 type &= ~vo_wm_FULLSCREEN;
1367 else
1368 type |= vo_wm_FULLSCREEN;
1369 } else if (!strcmp(arg, "stays_on_top"))
1370 {
1371 if (neg)
1372 type &= ~vo_wm_STAYS_ON_TOP;
1373 else
1374 type |= vo_wm_STAYS_ON_TOP;
1375 } else if (!strcmp(arg, "below"))
1376 {
1377 if (neg)
1378 type &= ~vo_wm_BELOW;
1379 else
1380 type |= vo_wm_BELOW;
1381 } else if (!strcmp(arg, "netwm"))
1382 {
1383 if (neg)
1384 type &= ~vo_wm_NETWM;
1385 else
1386 type |= vo_wm_NETWM;
1387 } else if (!strcmp(arg, "none"))
1388 type = 0; // clear; keep parsing
1389 }
1390 }
1391
1392 return type;
1393 }
1394
1395 /**
1396 * \brief update vo_dx, vo_dy, vo_dwidth and vo_dheight with current values of vo_window
1397 * \return returns current color depth of vo_window
1398 */
vo_x11_update_geometry(void)1399 int vo_x11_update_geometry(void) {
1400 unsigned depth, w, h;
1401 int dummy_int;
1402 Window dummy_win;
1403 XGetGeometry(mDisplay, vo_window, &dummy_win, &dummy_int, &dummy_int,
1404 &w, &h, &dummy_int, &depth);
1405 if (w <= INT_MAX && h <= INT_MAX) {
1406 vo_dwidth = w;
1407 vo_dheight = h;
1408 vo_x11_update_fs_borders();
1409 }
1410 XTranslateCoordinates(mDisplay, vo_window, mRootWin, 0, 0, &vo_dx, &vo_dy,
1411 &dummy_win);
1412
1413 return depth <= INT_MAX ? depth : 0;
1414 }
1415
vo_x11_fullscreen(void)1416 void vo_x11_fullscreen(void)
1417 {
1418 int x, y, w, h;
1419 x = vo_old_x;
1420 y = vo_old_y;
1421 w = vo_old_width;
1422 h = vo_old_height;
1423
1424 if (WinID >= 0) {
1425 vo_fs = !vo_fs;
1426 return;
1427 }
1428 if (vo_fs_flip)
1429 return;
1430
1431 if (vo_fs)
1432 {
1433 vo_x11_ewmh_fullscreen(vo_window, _NET_WM_STATE_REMOVE); // removes fullscreen state if wm supports EWMH
1434 vo_fs = VO_FALSE;
1435 } else
1436 {
1437 // win->fs
1438 vo_x11_ewmh_fullscreen(vo_window, _NET_WM_STATE_ADD); // sends fullscreen state to be added if wm supports EWMH
1439
1440 vo_fs = VO_TRUE;
1441 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1442 {
1443 vo_old_x = vo_dx;
1444 vo_old_y = vo_dy;
1445 vo_old_width = vo_dwidth;
1446 vo_old_height = vo_dheight;
1447 }
1448 update_xinerama_info();
1449 x = xinerama_x;
1450 y = xinerama_y;
1451 w = vo_screenwidth;
1452 h = vo_screenheight;
1453 }
1454 {
1455 long dummy;
1456
1457 XGetWMNormalHints(mDisplay, vo_window, &vo_hint, &dummy);
1458 if (!(vo_hint.flags & PWinGravity))
1459 old_gravity = NorthWestGravity;
1460 else
1461 old_gravity = vo_hint.win_gravity;
1462 }
1463 if (vo_wm_type == 0 && !(vo_fsmode & 16))
1464 {
1465 XUnmapWindow(mDisplay, vo_window); // required for MWM
1466 XWithdrawWindow(mDisplay, vo_window, mScreen);
1467 vo_fs_flip = 1;
1468 }
1469
1470 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // not needed with EWMH fs
1471 {
1472 vo_x11_decoration(mDisplay, vo_window, vo_border && !vo_fs);
1473 vo_x11_sizehint(x, y, w, h, 0);
1474 vo_x11_setlayer(mDisplay, vo_window, vo_fs);
1475
1476
1477 XMoveResizeWindow(mDisplay, vo_window, x, y, w, h);
1478 }
1479 /* some WMs lose ontop after fullscreen */
1480 if ((!(vo_fs)) & vo_ontop)
1481 vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
1482
1483 XMapRaised(mDisplay, vo_window);
1484 if ( ! (vo_fs_type & vo_wm_FULLSCREEN) ) // some WMs change window pos on map
1485 XMoveResizeWindow(mDisplay, vo_window, x, y, w, h);
1486 XRaiseWindow(mDisplay, vo_window);
1487 XFlush(mDisplay);
1488 }
1489
vo_x11_ontop(void)1490 void vo_x11_ontop(void)
1491 {
1492 vo_ontop = (!(vo_ontop));
1493
1494 vo_x11_setlayer(mDisplay, vo_window, vo_ontop);
1495 }
1496
vo_x11_border(void)1497 void vo_x11_border(void)
1498 {
1499 vo_border = !vo_border;
1500 vo_x11_decoration(mDisplay, vo_window, vo_border && !vo_fs);
1501 }
1502
1503 /*
1504 * XScreensaver stuff
1505 */
1506
1507 static int screensaver_off;
1508 static unsigned int time_last;
1509
xscreensaver_heartbeat(void)1510 void xscreensaver_heartbeat(void)
1511 {
1512 unsigned int time = GetTimerMS();
1513
1514 if (mDisplay && screensaver_off && (time - time_last) > 30000)
1515 {
1516 time_last = time;
1517
1518 XResetScreenSaver(mDisplay);
1519 }
1520 }
1521
xss_suspend(Bool suspend)1522 static int xss_suspend(Bool suspend)
1523 {
1524 #ifndef CONFIG_XSS
1525 return 0;
1526 #else
1527 int event, error, major, minor;
1528 if (XScreenSaverQueryExtension(mDisplay, &event, &error) != True ||
1529 XScreenSaverQueryVersion(mDisplay, &major, &minor) != True)
1530 return 0;
1531 if (major < 1 || (major == 1 && minor < 1))
1532 return 0;
1533 XScreenSaverSuspend(mDisplay, suspend);
1534 return 1;
1535 #endif
1536 }
1537
1538 /*
1539 * End of XScreensaver stuff
1540 */
1541
saver_on(Display * mDisplay)1542 void saver_on(Display * mDisplay)
1543 {
1544
1545 if (!screensaver_off)
1546 return;
1547 screensaver_off = 0;
1548 if (xss_suspend(False))
1549 return;
1550 #ifdef CONFIG_XDPMS
1551 if (dpms_disabled)
1552 {
1553 int nothing;
1554 if (DPMSQueryExtension(mDisplay, ¬hing, ¬hing))
1555 {
1556 if (!DPMSEnable(mDisplay))
1557 { // restoring power saving settings
1558 mp_msg(MSGT_VO, MSGL_WARN, MSGTR_DPMSnotAvailable);
1559 } else
1560 {
1561 // DPMS does not seem to be enabled unless we call DPMSInfo
1562 BOOL onoff;
1563 CARD16 state;
1564
1565 DPMSForceLevel(mDisplay, DPMSModeOn);
1566 DPMSInfo(mDisplay, &state, &onoff);
1567 if (onoff)
1568 {
1569 mp_msg(MSGT_VO, MSGL_V,
1570 "Successfully enabled DPMS\n");
1571 } else
1572 {
1573 mp_msg(MSGT_VO, MSGL_WARN, MSGTR_DPMSnotEnabled);
1574 }
1575 }
1576 }
1577 dpms_disabled = 0;
1578 }
1579 #endif
1580 }
1581
saver_off(Display * mDisplay)1582 void saver_off(Display * mDisplay)
1583 {
1584 int nothing;
1585
1586 if (!stop_xscreensaver || screensaver_off)
1587 return;
1588 screensaver_off = 1;
1589 if (xss_suspend(True))
1590 return;
1591 #ifdef CONFIG_XDPMS
1592 if (DPMSQueryExtension(mDisplay, ¬hing, ¬hing))
1593 {
1594 BOOL onoff;
1595 CARD16 state;
1596
1597 DPMSInfo(mDisplay, &state, &onoff);
1598 if (onoff)
1599 {
1600 Status stat;
1601
1602 mp_msg(MSGT_VO, MSGL_V, "Disabling DPMS\n");
1603 dpms_disabled = 1;
1604 stat = DPMSDisable(mDisplay); // monitor powersave off
1605 mp_msg(MSGT_VO, MSGL_V, "DPMSDisable stat: %d\n", stat);
1606 }
1607 }
1608 #endif
1609 }
1610
1611 static XErrorHandler old_handler = NULL;
1612 static int selectinput_err = 0;
x11_selectinput_errorhandler(Display * display,XErrorEvent * event)1613 static int x11_selectinput_errorhandler(Display * display,
1614 XErrorEvent * event)
1615 {
1616 if (event->error_code == BadAccess)
1617 {
1618 selectinput_err = 1;
1619 mp_msg(MSGT_VO, MSGL_ERR,
1620 MSGTR_BadAccessXSelectInput);
1621 mp_msg(MSGT_VO, MSGL_ERR,
1622 MSGTR_ButtonPressMaskInUse);
1623 /* If you think MPlayer should shutdown with this error,
1624 * comment out the following line */
1625 return 0;
1626 }
1627 if (old_handler != NULL)
1628 old_handler(display, event);
1629 else
1630 x11_errorhandler(display, event);
1631 return 0;
1632 }
1633
vo_x11_selectinput_witherr(Display * display,Window w,long event_mask)1634 void vo_x11_selectinput_witherr(Display * display, Window w,
1635 long event_mask)
1636 {
1637 XSync(display, False);
1638 old_handler = XSetErrorHandler(x11_selectinput_errorhandler);
1639 selectinput_err = 0;
1640 if (vo_nomouse_input)
1641 {
1642 XSelectInput(display, w,
1643 event_mask &
1644 (~(ButtonPressMask | ButtonReleaseMask)));
1645 } else
1646 {
1647 XSelectInput(display, w, event_mask);
1648 }
1649 XSync(display, False);
1650 XSetErrorHandler(old_handler);
1651 if (selectinput_err)
1652 {
1653 mp_msg(MSGT_VO, MSGL_ERR,
1654 MSGTR_DiscardMouseControl);
1655 XSelectInput(display, w,
1656 event_mask &
1657 (~
1658 (ButtonPressMask | ButtonReleaseMask |
1659 PointerMotionMask)));
1660 }
1661 }
1662
1663 #ifdef CONFIG_XF86VM
vo_vm_switch(void)1664 void vo_vm_switch(void)
1665 {
1666 int vm_event, vm_error;
1667 int vm_ver, vm_rev;
1668 int i, j, have_vm = 0;
1669 int X = vo_dwidth, Y = vo_dheight;
1670 int modeline_width, modeline_height;
1671
1672 if (XF86VidModeQueryExtension(mDisplay, &vm_event, &vm_error))
1673 {
1674 XF86VidModeQueryVersion(mDisplay, &vm_ver, &vm_rev);
1675 mp_msg(MSGT_VO, MSGL_V, "XF86VidMode extension v%i.%i\n", vm_ver,
1676 vm_rev);
1677 have_vm = 1;
1678 } else {
1679 mp_msg(MSGT_VO, MSGL_WARN,
1680 MSGTR_NoXF86VidModeExtension);
1681 }
1682
1683 if (have_vm)
1684 {
1685 if (vidmodes == NULL)
1686 XF86VidModeGetAllModeLines(mDisplay, mScreen, &modecount,
1687 &vidmodes);
1688 j = 0;
1689 modeline_width = vidmodes[0]->hdisplay;
1690 modeline_height = vidmodes[0]->vdisplay;
1691
1692 for (i = 1; i < modecount; i++)
1693 if ((vidmodes[i]->hdisplay >= X)
1694 && (vidmodes[i]->vdisplay >= Y))
1695 if ((vidmodes[i]->hdisplay <= modeline_width)
1696 && (vidmodes[i]->vdisplay <= modeline_height))
1697 {
1698 modeline_width = vidmodes[i]->hdisplay;
1699 modeline_height = vidmodes[i]->vdisplay;
1700 j = i;
1701 }
1702
1703 mp_msg(MSGT_VO, MSGL_INFO, MSGTR_SelectedVideoMode,
1704 modeline_width, modeline_height, X, Y);
1705 XF86VidModeLockModeSwitch(mDisplay, mScreen, 0);
1706 XF86VidModeSwitchToMode(mDisplay, mScreen, vidmodes[j]);
1707 XF86VidModeSwitchToMode(mDisplay, mScreen, vidmodes[j]);
1708
1709 // FIXME: all this is more of a hack than proper solution
1710 X = (vo_screenwidth - modeline_width) / 2;
1711 Y = (vo_screenheight - modeline_height) / 2;
1712 XF86VidModeSetViewPort(mDisplay, mScreen, X, Y);
1713 vo_dx = X;
1714 vo_dy = Y;
1715 vo_dwidth = modeline_width;
1716 vo_dheight = modeline_height;
1717 aspect_save_screenres(modeline_width, modeline_height);
1718 }
1719 }
1720
vo_vm_close(void)1721 void vo_vm_close(void)
1722 {
1723 if (vidmodes != NULL)
1724 {
1725 int i;
1726
1727 free(vidmodes);
1728 vidmodes = NULL;
1729 XF86VidModeGetAllModeLines(mDisplay, mScreen, &modecount,
1730 &vidmodes);
1731 for (i = 0; i < modecount; i++)
1732 if ((vidmodes[i]->hdisplay == vo_screenwidth)
1733 && (vidmodes[i]->vdisplay == vo_screenheight))
1734 {
1735 mp_msg(MSGT_VO, MSGL_INFO,
1736 MSGTR_ReturningOriginalMode,
1737 vo_screenwidth, vo_screenheight);
1738 break;
1739 }
1740
1741 XF86VidModeSwitchToMode(mDisplay, mScreen, vidmodes[i]);
1742 XF86VidModeSwitchToMode(mDisplay, mScreen, vidmodes[i]);
1743 free(vidmodes);
1744 vidmodes = NULL;
1745 modecount = 0;
1746 }
1747 }
1748 #endif
1749
1750
1751 /*
1752 * Scan the available visuals on this Display/Screen. Try to find
1753 * the 'best' available TrueColor visual that has a decent color
1754 * depth (at least 15bit). If there are multiple visuals with depth
1755 * >= 15bit, we prefer visuals with a smaller color depth.
1756 */
vo_find_depth_from_visuals(Display * dpy,int screen,Visual ** visual_return)1757 int vo_find_depth_from_visuals(Display * dpy, int screen,
1758 Visual ** visual_return)
1759 {
1760 XVisualInfo visual_tmpl;
1761 XVisualInfo *visuals;
1762 int nvisuals, i;
1763 int bestvisual = -1;
1764 int bestvisual_depth = -1;
1765
1766 visual_tmpl.screen = screen;
1767 visual_tmpl.class = TrueColor;
1768 visuals = XGetVisualInfo(dpy,
1769 VisualScreenMask | VisualClassMask,
1770 &visual_tmpl, &nvisuals);
1771 if (visuals != NULL)
1772 {
1773 for (i = 0; i < nvisuals; i++)
1774 {
1775 mp_msg(MSGT_VO, MSGL_V,
1776 "vo: X11 truecolor visual %#lx, depth %d, R:%lX G:%lX B:%lX\n",
1777 visuals[i].visualid, visuals[i].depth,
1778 visuals[i].red_mask, visuals[i].green_mask,
1779 visuals[i].blue_mask);
1780 /*
1781 * Save the visual index and its depth, if this is the first
1782 * truecolor visual, or a visual that is 'preferred' over the
1783 * previous 'best' visual.
1784 */
1785 if (bestvisual_depth == -1
1786 || (visuals[i].depth >= 15
1787 && (visuals[i].depth < bestvisual_depth
1788 || bestvisual_depth < 15)))
1789 {
1790 bestvisual = i;
1791 bestvisual_depth = visuals[i].depth;
1792 }
1793 }
1794
1795 if (bestvisual != -1 && visual_return != NULL)
1796 *visual_return = visuals[bestvisual].visual;
1797
1798 XFree(visuals);
1799 }
1800 return bestvisual_depth;
1801 }
1802
1803
1804 static Colormap cmap = None;
1805 static XColor cols[256];
1806 static int cm_size, red_mask, green_mask, blue_mask;
1807
1808
vo_x11_create_colormap(XVisualInfo * vinfo)1809 Colormap vo_x11_create_colormap(XVisualInfo * vinfo)
1810 {
1811 unsigned k, r, g, b, ru, gu, bu, m, rv, gv, bv, rvu, gvu, bvu;
1812
1813 if (vinfo->class != DirectColor)
1814 return XCreateColormap(mDisplay, mRootWin, vinfo->visual,
1815 AllocNone);
1816
1817 /* can this function get called twice or more? */
1818 if (cmap)
1819 return cmap;
1820 cm_size = vinfo->colormap_size;
1821 red_mask = vinfo->red_mask;
1822 green_mask = vinfo->green_mask;
1823 blue_mask = vinfo->blue_mask;
1824 ru = (red_mask & (red_mask - 1)) ^ red_mask;
1825 gu = (green_mask & (green_mask - 1)) ^ green_mask;
1826 bu = (blue_mask & (blue_mask - 1)) ^ blue_mask;
1827 rvu = 65536ull * ru / (red_mask + ru);
1828 gvu = 65536ull * gu / (green_mask + gu);
1829 bvu = 65536ull * bu / (blue_mask + bu);
1830 r = g = b = 0;
1831 rv = gv = bv = 0;
1832 m = DoRed | DoGreen | DoBlue;
1833 for (k = 0; k < cm_size; k++)
1834 {
1835 int t;
1836
1837 cols[k].pixel = r | g | b;
1838 cols[k].red = rv;
1839 cols[k].green = gv;
1840 cols[k].blue = bv;
1841 cols[k].flags = m;
1842 t = (r + ru) & red_mask;
1843 if (t < r)
1844 m &= ~DoRed;
1845 r = t;
1846 t = (g + gu) & green_mask;
1847 if (t < g)
1848 m &= ~DoGreen;
1849 g = t;
1850 t = (b + bu) & blue_mask;
1851 if (t < b)
1852 m &= ~DoBlue;
1853 b = t;
1854 rv += rvu;
1855 gv += gvu;
1856 bv += bvu;
1857 }
1858 cmap = XCreateColormap(mDisplay, mRootWin, vinfo->visual, AllocAll);
1859 XStoreColors(mDisplay, cmap, cols, cm_size);
1860 return cmap;
1861 }
1862
1863 /*
1864 * Via colormaps/gamma ramps we can do gamma, brightness, contrast,
1865 * hue and red/green/blue intensity, but we cannot do saturation.
1866 * Currently only gamma, brightness and contrast are implemented.
1867 * Is there sufficient interest for hue and/or red/green/blue intensity?
1868 */
1869 /* these values have range [-100,100] and are initially 0 */
1870 static int vo_gamma = 0;
1871 static int vo_brightness = 0;
1872 static int vo_contrast = 0;
1873
transform_color(float val,float brightness,float contrast,float gamma)1874 static int transform_color(float val,
1875 float brightness, float contrast, float gamma) {
1876 float s = pow(val, gamma);
1877 s = (s - 0.5) * contrast + 0.5;
1878 s += brightness;
1879 if (s < 0)
1880 s = 0;
1881 if (s > 1)
1882 s = 1;
1883 return (unsigned short) (s * 65535);
1884 }
1885
vo_x11_set_equalizer(const char * name,int value)1886 uint32_t vo_x11_set_equalizer(const char *name, int value)
1887 {
1888 float gamma, brightness, contrast;
1889 float rf, gf, bf;
1890 int k;
1891
1892 /*
1893 * IMPLEMENTME: consider using XF86VidModeSetGammaRamp in the case
1894 * of TrueColor-ed window but be careful:
1895 * Unlike the colormaps, which are private for the X client
1896 * who created them and thus automatically destroyed on client
1897 * disconnect, this gamma ramp is a system-wide (X-server-wide)
1898 * setting and _must_ be restored before the process exits.
1899 * Unforunately when the process crashes (or gets killed
1900 * for some reason) it is impossible to restore the setting,
1901 * and such behaviour could be rather annoying for the users.
1902 */
1903 if (cmap == None)
1904 return VO_NOTAVAIL;
1905
1906 if (!av_strcasecmp(name, "brightness"))
1907 vo_brightness = value;
1908 else if (!av_strcasecmp(name, "contrast"))
1909 vo_contrast = value;
1910 else if (!av_strcasecmp(name, "gamma"))
1911 vo_gamma = value;
1912 else
1913 return VO_NOTIMPL;
1914
1915 brightness = 0.01 * vo_brightness;
1916 contrast = tan(0.0095 * (vo_contrast + 100) * M_PI / 4);
1917 gamma = pow(2, -0.02 * vo_gamma);
1918
1919 rf = (float) ((red_mask & (red_mask - 1)) ^ red_mask) / red_mask;
1920 gf = (float) ((green_mask & (green_mask - 1)) ^ green_mask) /
1921 green_mask;
1922 bf = (float) ((blue_mask & (blue_mask - 1)) ^ blue_mask) / blue_mask;
1923
1924 /* now recalculate the colormap using the newly set value */
1925 for (k = 0; k < cm_size; k++)
1926 {
1927 cols[k].red = transform_color(rf * k, brightness, contrast, gamma);
1928 cols[k].green = transform_color(gf * k, brightness, contrast, gamma);
1929 cols[k].blue = transform_color(bf * k, brightness, contrast, gamma);
1930 }
1931
1932 XStoreColors(mDisplay, cmap, cols, cm_size);
1933 XFlush(mDisplay);
1934 return VO_TRUE;
1935 }
1936
vo_x11_get_equalizer(const char * name,int * value)1937 uint32_t vo_x11_get_equalizer(const char *name, int *value)
1938 {
1939 if (cmap == None)
1940 return VO_NOTAVAIL;
1941 if (!av_strcasecmp(name, "brightness"))
1942 *value = vo_brightness;
1943 else if (!av_strcasecmp(name, "contrast"))
1944 *value = vo_contrast;
1945 else if (!av_strcasecmp(name, "gamma"))
1946 *value = vo_gamma;
1947 else
1948 return VO_NOTIMPL;
1949 return VO_TRUE;
1950 }
1951
1952 #ifdef CONFIG_XV
vo_xv_set_eq(uint32_t xv_port,const char * name,int value)1953 int vo_xv_set_eq(uint32_t xv_port, const char *name, int value)
1954 {
1955 XvAttribute *attributes;
1956 int i, howmany, xv_atom;
1957
1958 mp_dbg(MSGT_VO, MSGL_V, "xv_set_eq called! (%s, %d)\n", name, value);
1959
1960 /* get available attributes */
1961 attributes = XvQueryPortAttributes(mDisplay, xv_port, &howmany);
1962 for (i = 0; i < howmany && attributes; i++)
1963 if (attributes[i].flags & XvSettable)
1964 {
1965 xv_atom = XInternAtom(mDisplay, attributes[i].name, True);
1966 /* since we have SET_DEFAULTS first in our list, we can check if it's available
1967 then trigger it if it's ok so that the other values are at default upon query */
1968 if (xv_atom != None)
1969 {
1970 int hue = 0, port_value, port_min, port_max;
1971
1972 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
1973 (!av_strcasecmp(name, "brightness")))
1974 port_value = value;
1975 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
1976 (!av_strcasecmp(name, "contrast")))
1977 port_value = value;
1978 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
1979 (!av_strcasecmp(name, "saturation")))
1980 port_value = value;
1981 else if (!strcmp(attributes[i].name, "XV_HUE") &&
1982 (!av_strcasecmp(name, "hue")))
1983 {
1984 port_value = value;
1985 hue = 1;
1986 } else
1987 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
1988 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
1989 (!av_strcasecmp(name, "red_intensity")))
1990 port_value = value;
1991 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
1992 && (!av_strcasecmp(name, "green_intensity")))
1993 port_value = value;
1994 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
1995 && (!av_strcasecmp(name, "blue_intensity")))
1996 port_value = value;
1997 else
1998 continue;
1999
2000 port_min = attributes[i].min_value;
2001 port_max = attributes[i].max_value;
2002
2003 /* nvidia hue workaround */
2004 if (hue && port_min == 0 && port_max == 360)
2005 {
2006 port_value =
2007 (port_value >=
2008 0) ? (port_value - 100) : (port_value + 100);
2009 }
2010 // -100 -> min
2011 // 0 -> (max+min)/2
2012 // +100 -> max
2013 port_value =
2014 (port_value + 100) * (port_max - port_min) / 200 +
2015 port_min;
2016 XvSetPortAttribute(mDisplay, xv_port, xv_atom, port_value);
2017 return VO_TRUE;
2018 }
2019 }
2020 return VO_FALSE;
2021 }
2022
vo_xv_get_eq(uint32_t xv_port,const char * name,int * value)2023 int vo_xv_get_eq(uint32_t xv_port, const char *name, int *value)
2024 {
2025
2026 XvAttribute *attributes;
2027 int i, howmany, xv_atom;
2028
2029 /* get available attributes */
2030 attributes = XvQueryPortAttributes(mDisplay, xv_port, &howmany);
2031 for (i = 0; i < howmany && attributes; i++)
2032 if (attributes[i].flags & XvGettable)
2033 {
2034 xv_atom = XInternAtom(mDisplay, attributes[i].name, True);
2035 /* since we have SET_DEFAULTS first in our list, we can check if it's available
2036 then trigger it if it's ok so that the other values are at default upon query */
2037 if (xv_atom != None)
2038 {
2039 int val, port_value = 0, port_min, port_max;
2040
2041 XvGetPortAttribute(mDisplay, xv_port, xv_atom,
2042 &port_value);
2043
2044 port_min = attributes[i].min_value;
2045 port_max = attributes[i].max_value;
2046 val =
2047 (port_value - port_min) * 200 / (port_max - port_min) -
2048 100;
2049
2050 if (!strcmp(attributes[i].name, "XV_BRIGHTNESS") &&
2051 (!av_strcasecmp(name, "brightness")))
2052 *value = val;
2053 else if (!strcmp(attributes[i].name, "XV_CONTRAST") &&
2054 (!av_strcasecmp(name, "contrast")))
2055 *value = val;
2056 else if (!strcmp(attributes[i].name, "XV_SATURATION") &&
2057 (!av_strcasecmp(name, "saturation")))
2058 *value = val;
2059 else if (!strcmp(attributes[i].name, "XV_HUE") &&
2060 (!av_strcasecmp(name, "hue")))
2061 {
2062 /* nasty nvidia detect */
2063 if (port_min == 0 && port_max == 360)
2064 *value = (val >= 0) ? (val - 100) : (val + 100);
2065 else
2066 *value = val;
2067 } else
2068 /* Note: since 22.01.2002 GATOS supports these attrs for radeons (NK) */
2069 if (!strcmp(attributes[i].name, "XV_RED_INTENSITY") &&
2070 (!av_strcasecmp(name, "red_intensity")))
2071 *value = val;
2072 else if (!strcmp(attributes[i].name, "XV_GREEN_INTENSITY")
2073 && (!av_strcasecmp(name, "green_intensity")))
2074 *value = val;
2075 else if (!strcmp(attributes[i].name, "XV_BLUE_INTENSITY")
2076 && (!av_strcasecmp(name, "blue_intensity")))
2077 *value = val;
2078 else
2079 continue;
2080
2081 mp_dbg(MSGT_VO, MSGL_V, "xv_get_eq called! (%s, %d)\n",
2082 name, *value);
2083 return VO_TRUE;
2084 }
2085 }
2086 return VO_FALSE;
2087 }
2088
2089 /** \brief contains flags changing the execution of the colorkeying code */
2090 xv_ck_info_t xv_ck_info = { CK_METHOD_MANUALFILL, CK_SRC_CUR };
2091 unsigned long xv_colorkey; ///< The color used for manual colorkeying.
2092 unsigned int xv_port; ///< The selected Xv port.
2093
2094 /**
2095 * \brief Interns the requested atom if it is available.
2096 *
2097 * \param atom_name String containing the name of the requested atom.
2098 *
2099 * \return Returns the atom if available, else None is returned.
2100 *
2101 */
xv_intern_atom_if_exists(char const * atom_name)2102 static Atom xv_intern_atom_if_exists( char const * atom_name )
2103 {
2104 XvAttribute * attributes;
2105 int attrib_count,i;
2106 Atom xv_atom = None;
2107
2108 attributes = XvQueryPortAttributes( mDisplay, xv_port, &attrib_count );
2109 if( attributes!=NULL )
2110 {
2111 for ( i = 0; i < attrib_count; ++i )
2112 {
2113 if ( strcmp(attributes[i].name, atom_name ) == 0 )
2114 {
2115 xv_atom = XInternAtom( mDisplay, atom_name, False );
2116 break; // found what we want, break out
2117 }
2118 }
2119 XFree( attributes );
2120 }
2121
2122 return xv_atom;
2123 }
2124
2125 /**
2126 * \brief Try to enable vsync for xv.
2127 * \return Returns -1 if not available, 0 on failure and 1 on success.
2128 */
vo_xv_enable_vsync(void)2129 int vo_xv_enable_vsync(void)
2130 {
2131 Atom xv_atom = xv_intern_atom_if_exists("XV_SYNC_TO_VBLANK");
2132 if (xv_atom == None)
2133 return -1;
2134 return XvSetPortAttribute(mDisplay, xv_port, xv_atom, 1) == Success;
2135 }
2136
2137 /**
2138 * \brief Get maximum supported source image dimensions.
2139 *
2140 * This function does not set the variables pointed to by
2141 * width and height if the information could not be retrieved,
2142 * so the caller is reponsible for properly initializing them.
2143 *
2144 * \param width [out] The maximum width gets stored here.
2145 * \param height [out] The maximum height gets stored here.
2146 *
2147 */
vo_xv_get_max_img_dim(uint32_t * width,uint32_t * height)2148 void vo_xv_get_max_img_dim( uint32_t * width, uint32_t * height )
2149 {
2150 XvEncodingInfo * encodings;
2151 //unsigned long num_encodings, idx; to int or too long?!
2152 unsigned int num_encodings, idx;
2153
2154 XvQueryEncodings( mDisplay, xv_port, &num_encodings, &encodings);
2155
2156 if ( encodings )
2157 {
2158 for ( idx = 0; idx < num_encodings; ++idx )
2159 {
2160 if ( strcmp( encodings[idx].name, "XV_IMAGE" ) == 0 )
2161 {
2162 *width = encodings[idx].width;
2163 *height = encodings[idx].height;
2164 break;
2165 }
2166 }
2167 }
2168
2169 mp_msg( MSGT_VO, MSGL_V,
2170 "[xv common] Maximum source image dimensions: %ux%u\n",
2171 *width, *height );
2172
2173 XvFreeEncodingInfo( encodings );
2174 }
2175
2176 /**
2177 * \brief Print information about the colorkey method and source.
2178 *
2179 * \param ck_handling Integer value containing the information about
2180 * colorkey handling (see x11_common.h).
2181 *
2182 * Outputs the content of |ck_handling| as a readable message.
2183 *
2184 */
vo_xv_print_ck_info(void)2185 static void vo_xv_print_ck_info(void)
2186 {
2187 mp_msg( MSGT_VO, MSGL_V, "[xv common] " );
2188
2189 switch ( xv_ck_info.method )
2190 {
2191 case CK_METHOD_NONE:
2192 mp_msg( MSGT_VO, MSGL_V, "Drawing no colorkey.\n" ); return;
2193 case CK_METHOD_AUTOPAINT:
2194 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn by Xv." ); break;
2195 case CK_METHOD_MANUALFILL:
2196 mp_msg( MSGT_VO, MSGL_V, "Drawing colorkey manually." ); break;
2197 case CK_METHOD_BACKGROUND:
2198 mp_msg( MSGT_VO, MSGL_V, "Colorkey is drawn as window background." ); break;
2199 }
2200
2201 mp_msg( MSGT_VO, MSGL_V, "\n[xv common] " );
2202
2203 switch ( xv_ck_info.source )
2204 {
2205 case CK_SRC_CUR:
2206 mp_msg( MSGT_VO, MSGL_V, "Using colorkey from Xv (0x%06lx).\n",
2207 xv_colorkey );
2208 break;
2209 case CK_SRC_USE:
2210 if ( xv_ck_info.method == CK_METHOD_AUTOPAINT )
2211 {
2212 mp_msg( MSGT_VO, MSGL_V,
2213 "Ignoring colorkey from MPlayer (0x%06lx).\n",
2214 xv_colorkey );
2215 }
2216 else
2217 {
2218 mp_msg( MSGT_VO, MSGL_V,
2219 "Using colorkey from MPlayer (0x%06lx)."
2220 " Use -colorkey to change.\n",
2221 xv_colorkey );
2222 }
2223 break;
2224 case CK_SRC_SET:
2225 mp_msg( MSGT_VO, MSGL_V,
2226 "Setting and using colorkey from MPlayer (0x%06lx)."
2227 " Use -colorkey to change.\n",
2228 xv_colorkey );
2229 break;
2230 }
2231 }
2232 /**
2233 * \brief Init colorkey depending on the settings in xv_ck_info.
2234 *
2235 * \return Returns 0 on failure and 1 on success.
2236 *
2237 * Sets the colorkey variable according to the CK_SRC_* and CK_METHOD_*
2238 * flags in xv_ck_info.
2239 *
2240 * Possiblilities:
2241 * * Methods
2242 * - manual colorkey drawing ( CK_METHOD_MANUALFILL )
2243 * - set colorkey as window background ( CK_METHOD_BACKGROUND )
2244 * - let Xv paint the colorkey ( CK_METHOD_AUTOPAINT )
2245 * * Sources
2246 * - use currently set colorkey ( CK_SRC_CUR )
2247 * - use colorkey in vo_colorkey ( CK_SRC_USE )
2248 * - use and set colorkey in vo_colorkey ( CK_SRC_SET )
2249 *
2250 * NOTE: If vo_colorkey has bits set after the first 3 low order bytes
2251 * we don't draw anything as this means it was forced to off.
2252 */
vo_xv_init_colorkey(void)2253 int vo_xv_init_colorkey(void)
2254 {
2255 Atom xv_atom;
2256 int rez;
2257
2258 /* check if colorkeying is needed */
2259 xv_atom = xv_intern_atom_if_exists( "XV_COLORKEY" );
2260
2261 /* if we have to deal with colorkeying ... */
2262 if( xv_atom != None && !(vo_colorkey & 0xFF000000) )
2263 {
2264 /* check if we should use the colorkey specified in vo_colorkey */
2265 if ( xv_ck_info.source != CK_SRC_CUR )
2266 {
2267 xv_colorkey = vo_colorkey;
2268
2269 /* check if we have to set the colorkey too */
2270 if ( xv_ck_info.source == CK_SRC_SET )
2271 {
2272 xv_atom = XInternAtom(mDisplay, "XV_COLORKEY",False);
2273
2274 rez = XvSetPortAttribute( mDisplay, xv_port, xv_atom, vo_colorkey );
2275 if ( rez != Success )
2276 {
2277 mp_msg( MSGT_VO, MSGL_FATAL,
2278 MSGTR_CouldntSetColorkey );
2279 return 0; // error setting colorkey
2280 }
2281 }
2282 }
2283 else
2284 {
2285 int colorkey_ret;
2286
2287 rez=XvGetPortAttribute(mDisplay,xv_port, xv_atom, &colorkey_ret);
2288 if ( rez == Success )
2289 {
2290 xv_colorkey = colorkey_ret;
2291 }
2292 else
2293 {
2294 mp_msg( MSGT_VO, MSGL_FATAL,
2295 MSGTR_CouldntGetColorkey );
2296 return 0; // error getting colorkey
2297 }
2298 }
2299
2300 xv_atom = xv_intern_atom_if_exists( "XV_AUTOPAINT_COLORKEY" );
2301
2302 /* should we draw the colorkey ourselves or activate autopainting? */
2303 if ( xv_ck_info.method == CK_METHOD_AUTOPAINT )
2304 {
2305 rez = !Success; // reset rez to something different than Success
2306
2307 if ( xv_atom != None ) // autopaint is supported
2308 {
2309 rez = XvSetPortAttribute( mDisplay, xv_port, xv_atom, 1 );
2310 }
2311
2312 if ( rez != Success )
2313 {
2314 // fallback to manual colorkey drawing
2315 xv_ck_info.method = CK_METHOD_MANUALFILL;
2316 }
2317 }
2318 else // disable colorkey autopainting if supported
2319 {
2320 if ( xv_atom != None ) // we have autopaint attribute
2321 {
2322 XvSetPortAttribute( mDisplay, xv_port, xv_atom, 0 );
2323 }
2324 }
2325 }
2326 else // do no colorkey drawing at all
2327 {
2328 xv_ck_info.method = CK_METHOD_NONE;
2329 } /* end: should we draw colorkey */
2330
2331 /* output information about the current colorkey settings */
2332 vo_xv_print_ck_info();
2333
2334 return 1; // success
2335 }
2336
2337 /**
2338 * \brief Draw the colorkey on the video window.
2339 *
2340 * Draws the colorkey depending on the set method ( colorkey_handling ).
2341 *
2342 * Also draws the black bars ( when the video doesn't fit the display in
2343 * fullscreen ) separately, so they don't overlap with the video area.
2344 * It doesn't call XFlush.
2345 *
2346 */
vo_xv_draw_colorkey(int32_t x,int32_t y,int32_t w,int32_t h)2347 void vo_xv_draw_colorkey( int32_t x, int32_t y,
2348 int32_t w, int32_t h )
2349 {
2350 if( xv_ck_info.method == CK_METHOD_MANUALFILL ||
2351 xv_ck_info.method == CK_METHOD_BACKGROUND )//less tearing than XClearWindow()
2352 {
2353 XSetForeground( mDisplay, vo_gc, xv_colorkey );
2354 XFillRectangle( mDisplay, vo_window, vo_gc,
2355 x, y,
2356 w, h );
2357 }
2358
2359 /* draw black bars if needed */
2360 /* TODO! move this to vo_x11_clearwindow_part() */
2361 if ( vo_fs )
2362 {
2363 XSetForeground( mDisplay, vo_gc, 0 );
2364 /* making non-overlap fills, requires 8 checks instead of 4 */
2365 if ( y > 0 )
2366 XFillRectangle( mDisplay, vo_window, vo_gc,
2367 0, 0,
2368 vo_screenwidth, y);
2369 if (x > 0)
2370 XFillRectangle( mDisplay, vo_window, vo_gc,
2371 0, 0,
2372 x, vo_screenheight);
2373 if (x + w < vo_screenwidth)
2374 XFillRectangle( mDisplay, vo_window, vo_gc,
2375 x + w, 0,
2376 vo_screenwidth, vo_screenheight);
2377 if (y + h < vo_screenheight)
2378 XFillRectangle( mDisplay, vo_window, vo_gc,
2379 0, y + h,
2380 vo_screenwidth, vo_screenheight);
2381 }
2382 }
2383
2384 /** \brief Tests if a valid argument for the ck suboption was given. */
xv_test_ck(void * arg)2385 int xv_test_ck( void * arg )
2386 {
2387 strarg_t * strarg = (strarg_t *)arg;
2388
2389 if ( strargcmp( strarg, "use" ) == 0 ||
2390 strargcmp( strarg, "set" ) == 0 ||
2391 strargcmp( strarg, "cur" ) == 0 )
2392 {
2393 return 1;
2394 }
2395
2396 return 0;
2397 }
2398 /** \brief Tests if a valid arguments for the ck-method suboption was given. */
xv_test_ckm(void * arg)2399 int xv_test_ckm( void * arg )
2400 {
2401 strarg_t * strarg = (strarg_t *)arg;
2402
2403 if ( strargcmp( strarg, "bg" ) == 0 ||
2404 strargcmp( strarg, "man" ) == 0 ||
2405 strargcmp( strarg, "auto" ) == 0 )
2406 {
2407 return 1;
2408 }
2409
2410 return 0;
2411 }
2412
2413 /**
2414 * \brief Modify the colorkey_handling var according to str
2415 *
2416 * Checks if a valid pointer ( not NULL ) to the string
2417 * was given. And in that case modifies the colorkey_handling
2418 * var to reflect the requested behaviour.
2419 * If nothing happens the content of colorkey_handling stays
2420 * the same.
2421 *
2422 * \param str Pointer to the string or NULL
2423 *
2424 */
xv_setup_colorkeyhandling(char const * ck_method_str,char const * ck_str)2425 void xv_setup_colorkeyhandling( char const * ck_method_str,
2426 char const * ck_str )
2427 {
2428 /* check if a valid pointer to the string was passed */
2429 if ( ck_str )
2430 {
2431 if ( strncmp( ck_str, "use", 3 ) == 0 )
2432 {
2433 xv_ck_info.source = CK_SRC_USE;
2434 }
2435 else if ( strncmp( ck_str, "set", 3 ) == 0 )
2436 {
2437 xv_ck_info.source = CK_SRC_SET;
2438 }
2439 }
2440 /* check if a valid pointer to the string was passed */
2441 if ( ck_method_str )
2442 {
2443 if ( strncmp( ck_method_str, "bg", 2 ) == 0 )
2444 {
2445 xv_ck_info.method = CK_METHOD_BACKGROUND;
2446 }
2447 else if ( strncmp( ck_method_str, "man", 3 ) == 0 )
2448 {
2449 xv_ck_info.method = CK_METHOD_MANUALFILL;
2450 }
2451 else if ( strncmp( ck_method_str, "auto", 4 ) == 0 )
2452 {
2453 xv_ck_info.method = CK_METHOD_AUTOPAINT;
2454 }
2455 }
2456 }
2457
2458 #endif
2459