1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
28 #include <X11/Xutil.h>
29 #include <X11/extensions/shape.h>
31 #include "x11_display.hpp"
32 #include "../src/logger.hpp"
34 template<class type> type X11Display::putPixel(type r, type g, type b) const
36 return ( (r >> m_redRightShift) << m_redLeftShift ) |
37 ( (g >> m_greenRightShift) << m_greenLeftShift ) |
38 ( (b >> m_blueRightShift) << m_blueLeftShift );
42 type X11Display::blendPixel(type v,type r, type g, type b, type a) const
46 temp = ((uint8_t)((v >> m_redLeftShift) << m_redRightShift));
47 uint8_t red = r + ( temp * (255 - a) ) / 255;
48 temp = ((uint8_t)((v >> m_greenLeftShift) << m_greenRightShift));
49 uint8_t green = g + ( temp * (255 - a) ) / 255;
50 temp = ((uint8_t)((v >> m_blueLeftShift) << m_blueRightShift));
51 uint8_t blue = b + ( temp * (255 - a) ) / 255;
53 return putPixel<type>(red,green,blue);
56 X11Display::X11Display( intf_thread_t *pIntf ): SkinObject( pIntf ),
57 m_mainWindow( 0 ), m_gc( NULL ), m_colormap( 0 )
59 char *psz_display = var_CreateGetNonEmptyString( pIntf, "x11-display" );
60 // Open a connection to the X Server
61 m_pDisplay = XOpenDisplay( psz_display );
64 if( m_pDisplay == NULL )
66 MSG_ERR( "Cannot open display" );
70 // Load the XShape extension
72 XShapeQueryExtension( m_pDisplay, &event, &error );
74 // Get the display parameters
75 int screen = DefaultScreen( m_pDisplay );
76 int depth = DefaultDepth( m_pDisplay, screen );
77 int order = ImageByteOrder( m_pDisplay );
78 Window root = DefaultRootWindow( m_pDisplay );
80 // Template for looking up the XVisualInfo
81 XVisualInfo xVInfoTemplate;
82 xVInfoTemplate.screen = screen;
83 xVInfoTemplate.depth = depth;
85 XVisualInfo *pVInfo = NULL;
91 xVInfoTemplate.c_class = DirectColor;
92 // Get the DirectColor visual
93 pVInfo = XGetVisualInfo( m_pDisplay, VisualScreenMask |
94 VisualClassMask, &xVInfoTemplate,
98 msg_Err( getIntf(), "no DirectColor visual available" );
102 m_pVisual = pVInfo->visual;
104 // Compute the color shifts
105 getShifts( pVInfo->red_mask, m_redLeftShift, m_redRightShift );
106 getShifts( pVInfo->green_mask, m_greenLeftShift,
108 getShifts( pVInfo->blue_mask, m_blueLeftShift, m_blueRightShift );
110 // Create a color map
111 m_colormap = XCreateColormap( m_pDisplay, root,
112 DefaultVisual( m_pDisplay, screen ), AllocAll );
114 // Create the palette
116 for( uint16_t i = 0; i < 255; i++ )
118 // kludge: colors are indexed reversely because color 255 seems
119 // to bereserved for black even if we try to set it to white
120 pColors[i].pixel = 254-i;
122 pColors[i].flags = DoRed | DoGreen | DoBlue;
124 (i >> m_redLeftShift) << (m_redRightShift + 8);
126 (i >> m_greenLeftShift) << (m_greenRightShift + 8);
128 (i >> m_blueLeftShift) << (m_blueRightShift + 8);
130 XStoreColors( m_pDisplay, m_colormap, pColors, 255 );
131 blendPixelImpl = &X11Display::blendPixel8;
132 putPixelImpl = &X11Display::putPixel8;
140 // Get the TrueColor visual
141 xVInfoTemplate.c_class = TrueColor;
142 pVInfo = XGetVisualInfo( m_pDisplay, VisualScreenMask |
143 VisualDepthMask | VisualClassMask,
144 &xVInfoTemplate, &vCount );
147 msg_Err( getIntf(), "No TrueColor visual for depth %d", depth );
151 m_pVisual = pVInfo->visual;
153 // Compute the color shifts
154 getShifts( pVInfo->red_mask, m_redLeftShift, m_redRightShift );
155 getShifts( pVInfo->green_mask, m_greenLeftShift, m_greenRightShift );
156 getShifts( pVInfo->blue_mask, m_blueLeftShift, m_blueRightShift );
158 if( depth == 15 || depth == 16 )
160 if( order == MSBFirst )
162 blendPixelImpl = &X11Display::blendPixel16MSB;
163 putPixelImpl = &X11Display::putPixel16MSB;
167 blendPixelImpl = &X11Display::blendPixel16LSB;
168 putPixelImpl = &X11Display::putPixel16LSB;
174 if( order == MSBFirst )
176 blendPixelImpl = &X11Display::blendPixel32MSB;
177 putPixelImpl = &X11Display::putPixel32MSB;
181 blendPixelImpl = &X11Display::blendPixel32LSB;
182 putPixelImpl = &X11Display::putPixel32LSB;
189 msg_Err( getIntf(), "unsupported depth: %d bpp\n", depth );
194 // Free the visual info
200 // Create a graphics context that doesn't generate GraphicsExpose events
204 xgcvalues.graphics_exposures = False;
205 m_gc = XCreateGC( m_pDisplay, root, GCGraphicsExposures, &xgcvalues );
207 // Create a parent window to have a single task in the task bar
208 XSetWindowAttributes attr;
209 m_mainWindow = XCreateWindow( m_pDisplay, root, 0, 0, 1, 1, 0, 0,
210 InputOutput, CopyFromParent, 0, &attr );
212 // initialize WM_CLASS
213 XClassHint classhint;
214 classhint.res_name = (char*) "vlc";
215 classhint.res_class = (char*) "Vlc";
216 XSetClassHint( m_pDisplay, m_mainWindow, &classhint );
218 // Receive WM_DELETE_WINDOW
219 Atom wm_delete = XInternAtom( m_pDisplay, "WM_DELETE_WINDOW", False);
220 XSetWMProtocols( m_pDisplay, m_mainWindow, &wm_delete, 1);
222 // Changing decorations
225 unsigned long functions;
226 unsigned long decorations;
228 unsigned long status;
230 Atom hints_atom = XInternAtom( m_pDisplay, "_MOTIF_WM_HINTS", False );
231 motifWmHints.flags = 2; // MWM_HINTS_DECORATIONS;
232 motifWmHints.decorations = 0;
233 XChangeProperty( m_pDisplay, m_mainWindow, hints_atom, hints_atom, 32,
234 PropModeReplace, (unsigned char *)&motifWmHints,
235 sizeof( motifWmHints ) / sizeof( long ) );
237 // Change the window title
238 XStoreName( m_pDisplay, m_mainWindow, "VLC Media Player" );
240 // Receive map notify events
241 XSelectInput( m_pDisplay, m_mainWindow, StructureNotifyMask );
243 // Set an empty mask for the window
244 Region mask = XCreateRegion();
245 XShapeCombineRegion( m_pDisplay, m_mainWindow, ShapeBounding, 0, 0,
247 XDestroyRegion( mask );
249 XMapWindow( m_pDisplay, m_mainWindow);
251 // Move it outside the screen to avoid seeing it in workspace selector
252 XMoveWindow( m_pDisplay, m_mainWindow, -10, -10 );
254 // test EWMH capabilities
257 // Force _NET_WM_PID whatever the WM _NET_SUPPORTED says
258 m_net_wm_pid = XInternAtom( m_pDisplay, "_NET_WM_PID" , False );
263 X11Display::~X11Display()
265 if( m_mainWindow ) XDestroyWindow( m_pDisplay, m_mainWindow );
266 if( m_gc ) XFreeGC( m_pDisplay, m_gc );
267 if( m_colormap ) XFreeColormap( m_pDisplay, m_colormap );
268 if( m_pDisplay ) XCloseDisplay( m_pDisplay );
272 void X11Display::testEWMH()
275 unsigned long i_items, i_bytesafter;
276 union { Atom *p_atom; unsigned char *p_char; } p_args;
277 p_args.p_atom = NULL;
279 m_net_wm_supported = XInternAtom( m_pDisplay, "_NET_SUPPORTED", False );
280 msg_Dbg( getIntf(), "EWMH: supported %d", m_net_wm_supported != None ? 1 : 0 );
281 if( m_net_wm_supported == None )
284 i_ret = XGetWindowProperty( m_pDisplay, DefaultRootWindow( m_pDisplay ),
286 0, 16384, False, AnyPropertyType,
288 &i_format, &i_items, &i_bytesafter,
289 (unsigned char **)&p_args );
291 if( i_ret != Success || i_items == 0 )
292 return; /* Not supported */
294 #define TEST_EWMH( name, value ) \
296 name = XInternAtom( m_pDisplay, value, False );\
298 for( i = 0; i < i_items; i++ )\
300 if( p_args.p_atom[i] == name ) break;\
304 msg_Dbg( getIntf(), "%s support: no", value );\
308 msg_Dbg( getIntf(), "%s support: yes", value );\
311 TEST_EWMH( m_net_wm_state, "_NET_WM_STATE" )
312 TEST_EWMH( m_net_wm_state_fullscreen, "_NET_WM_STATE_FULLSCREEN" )
313 TEST_EWMH( m_net_wm_stays_on_top, "_NET_WM_STATE_STAYS_ON_TOP" )
314 TEST_EWMH( m_net_wm_state_above, "_NET_WM_STATE_ABOVE" )
316 TEST_EWMH( m_net_wm_window_opacity, "_NET_WM_WINDOW_OPACITY" )
318 TEST_EWMH( m_net_wm_pid, "_NET_WM_PID" )
322 XFree( p_args.p_atom );
327 void X11Display::getShifts( uint32_t mask, int &rLeftShift, int &rRightShift )
329 for( rLeftShift = 0; (rLeftShift < 32) && !(mask & 1); rLeftShift++ )
333 for( rRightShift = 8; (mask & 1) ; rRightShift--)
337 if( rRightShift < 0 )
339 rLeftShift -= rRightShift;
345 void X11Display::blendPixel8( uint8_t *pPixel, uint8_t r, uint8_t g,
346 uint8_t b, uint8_t a ) const
348 *pPixel = 255 - blendPixel<uint8_t>(255 - *pPixel,r,g,b,a);
352 void X11Display::blendPixel16MSB( uint8_t *pPixel, uint8_t r, uint8_t g,
353 uint8_t b, uint8_t a ) const
355 uint16_t value = pPixel[1] | pPixel[0] << 8;
357 value = blendPixel<uint16_t>(value,r,g,b,a);
359 pPixel[1] = value; value >>= 8;
364 void X11Display::blendPixel16LSB( uint8_t *pPixel, uint8_t r, uint8_t g,
365 uint8_t b, uint8_t a ) const
367 uint16_t value = pPixel[0] | pPixel[1] << 8;
369 value = blendPixel<uint16_t>(value,r,g,b,a);
371 pPixel[0] = value; value >>= 8;
376 void X11Display::blendPixel32MSB( uint8_t *pPixel, uint8_t r, uint8_t g,
377 uint8_t b, uint8_t a ) const
379 uint32_t value = pPixel[3] | pPixel[2] << 8 |
380 pPixel[1] << 16 | pPixel[0] << 24;
382 value = blendPixel<uint32_t>(value,r,g,b,a);
384 pPixel[3] = value; value >>= 8;
385 pPixel[2] = value; value >>= 8;
386 pPixel[1] = value; value >>= 8;
391 void X11Display::blendPixel32LSB( uint8_t *pPixel, uint8_t r, uint8_t g,
392 uint8_t b, uint8_t a ) const
394 uint32_t value = pPixel[0] | pPixel[1] << 8 |
395 pPixel[2] << 16 | pPixel[3] << 24;
397 value = blendPixel<uint32_t>(value,r,g,b,a);
399 pPixel[0] = value; value >>= 8;
400 pPixel[1] = value; value >>= 8;
401 pPixel[2] = value; value >>= 8;
406 void X11Display::putPixel8( uint8_t *pPixel, uint8_t r, uint8_t g,
407 uint8_t b, uint8_t a ) const
409 *pPixel = 255 - putPixel<uint8_t>(r,g,b);
413 void X11Display::putPixel16MSB( uint8_t *pPixel, uint8_t r, uint8_t g,
414 uint8_t b, uint8_t a ) const
416 uint16_t value = putPixel<uint16_t>(r, g, b);
418 pPixel[1] = value; value >>= 8;
423 void X11Display::putPixel16LSB( uint8_t *pPixel, uint8_t r, uint8_t g,
424 uint8_t b, uint8_t a ) const
426 uint16_t value = putPixel<uint16_t>(r,g,b);
427 pPixel[0] = value; value >>= 8;
432 void X11Display::putPixel32MSB( uint8_t *pPixel, uint8_t r, uint8_t g,
433 uint8_t b, uint8_t a ) const
435 uint32_t value = putPixel<uint32_t>(r,g,b);
437 pPixel[3] = value; value >>= 8;
438 pPixel[2] = value; value >>= 8;
439 pPixel[1] = value; value >>= 8;
444 void X11Display::putPixel32LSB( uint8_t *pPixel, uint8_t r, uint8_t g,
445 uint8_t b, uint8_t a ) const
447 uint32_t value = putPixel<uint32_t>(r,g,b);
449 pPixel[0] = value; value >>= 8;
450 pPixel[1] = value; value >>= 8;
451 pPixel[2] = value; value >>= 8;
456 unsigned long X11Display::getPixelValue( uint8_t r, uint8_t g, uint8_t b )
459 unsigned long value = putPixel<unsigned long>(r,g,b);
461 return m_pixelSize==1 ? 255 - value : value;