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
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 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 // Macro to compute a pixel value
35 #define PUT_PIXEL(value, r, g, b, type) \
37 ( ((type)r >> m_redRightShift) << m_redLeftShift ) | \
38 ( ((type)g >> m_greenRightShift) << m_greenLeftShift ) | \
39 ( ((type)b >> m_blueRightShift) << m_blueLeftShift );
41 // Macro to blend a pixel with another color
42 #define BLEND_PIXEL(value, r, g, b, a, type) \
44 temp = ((uint8_t)((value >> m_redLeftShift) << m_redRightShift)); \
45 uint8_t red = r + ( temp * (255 - a) ) / 255; \
46 temp = ((uint8_t)((value >> m_greenLeftShift) << m_greenRightShift)); \
47 uint8_t green = g + ( temp * (255 - a) ) / 255; \
48 temp = ((uint8_t)((value >> m_blueLeftShift) << m_blueRightShift)); \
49 uint8_t blue = b + ( temp * (255 - a) ) / 255; \
50 PUT_PIXEL(value, red, green, blue, type)
53 X11Display::X11Display( intf_thread_t *pIntf ): SkinObject( pIntf ),
54 m_mainWindow( 0 ), m_gc( NULL ), m_colormap( 0 )
56 char *psz_display = var_CreateGetNonEmptyString( pIntf, "x11-display" );
57 // Open a connection to the X Server
58 m_pDisplay = XOpenDisplay( psz_display );
61 if( m_pDisplay == NULL )
63 MSG_ERR( "Cannot open display" );
67 // Load the XShape extension
69 XShapeQueryExtension( m_pDisplay, &event, &error );
71 // Get the display parameters
72 int screen = DefaultScreen( m_pDisplay );
73 int depth = DefaultDepth( m_pDisplay, screen );
74 int order = ImageByteOrder( m_pDisplay );
75 Window root = DefaultRootWindow( m_pDisplay );
77 // Template for looking up the XVisualInfo
78 XVisualInfo xVInfoTemplate;
79 xVInfoTemplate.screen = screen;
80 xVInfoTemplate.depth = depth;
82 XVisualInfo *pVInfo = NULL;
88 xVInfoTemplate.c_class = DirectColor;
89 // Get the DirectColor visual
90 pVInfo = XGetVisualInfo( m_pDisplay, VisualScreenMask |
91 VisualClassMask, &xVInfoTemplate,
95 msg_Err( getIntf(), "no DirectColor visual available" );
99 m_pVisual = pVInfo->visual;
101 // Compute the color shifts
102 getShifts( pVInfo->red_mask, m_redLeftShift, m_redRightShift );
103 getShifts( pVInfo->green_mask, m_greenLeftShift,
105 getShifts( pVInfo->blue_mask, m_blueLeftShift, m_blueRightShift );
107 // Create a color map
108 m_colormap = XCreateColormap( m_pDisplay, root,
109 DefaultVisual( m_pDisplay, screen ), AllocAll );
111 // Create the palette
113 for( uint16_t i = 0; i < 255; i++ )
115 // kludge: colors are indexed reversely because color 255 seems
116 // to bereserved for black even if we try to set it to white
117 pColors[i].pixel = 254-i;
119 pColors[i].flags = DoRed | DoGreen | DoBlue;
121 (i >> m_redLeftShift) << (m_redRightShift + 8);
123 (i >> m_greenLeftShift) << (m_greenRightShift + 8);
125 (i >> m_blueLeftShift) << (m_blueRightShift + 8);
127 XStoreColors( m_pDisplay, m_colormap, pColors, 255 );
128 blendPixelImpl = &X11Display::blendPixel8;
129 putPixelImpl = &X11Display::putPixel8;
137 // Get the TrueColor visual
138 xVInfoTemplate.c_class = TrueColor;
139 pVInfo = XGetVisualInfo( m_pDisplay, VisualScreenMask |
140 VisualDepthMask | VisualClassMask,
141 &xVInfoTemplate, &vCount );
144 msg_Err( getIntf(), "No TrueColor visual for depth %d",
149 m_pVisual = pVInfo->visual;
151 // Compute the color shifts
152 getShifts( pVInfo->red_mask, m_redLeftShift, m_redRightShift );
153 getShifts( pVInfo->green_mask, m_greenLeftShift,
155 getShifts( pVInfo->blue_mask, m_blueLeftShift, m_blueRightShift );
157 if( depth == 15 || depth == 16 )
159 if( order == MSBFirst )
161 blendPixelImpl = &X11Display::blendPixel16MSB;
162 putPixelImpl = &X11Display::putPixel16MSB;
166 blendPixelImpl = &X11Display::blendPixel16LSB;
167 putPixelImpl = &X11Display::putPixel16LSB;
173 if( order == MSBFirst )
175 blendPixelImpl = &X11Display::blendPixel32MSB;
176 putPixelImpl = &X11Display::putPixel32MSB;
180 blendPixelImpl = &X11Display::blendPixel32LSB;
181 putPixelImpl = &X11Display::putPixel32LSB;
188 msg_Err( getIntf(), "unsupported depth: %d bpp\n", depth );
193 // Free the visual info
199 // Create a graphics context that doesn't generate GraphicsExpose events
203 xgcvalues.graphics_exposures = False;
204 m_gc = XCreateGC( m_pDisplay, root, GCGraphicsExposures, &xgcvalues );
206 // Create a parent window to have a single task in the task bar
207 XSetWindowAttributes attr;
208 m_mainWindow = XCreateWindow( m_pDisplay, root, 0, 0, 1, 1, 0, 0,
209 InputOutput, CopyFromParent, 0, &attr );
211 // Changing decorations
214 unsigned long functions;
215 unsigned long decorations;
217 unsigned long status;
219 Atom hints_atom = XInternAtom( m_pDisplay, "_MOTIF_WM_HINTS", False );
220 motifWmHints.flags = 2; // MWM_HINTS_DECORATIONS;
221 motifWmHints.decorations = 0;
222 XChangeProperty( m_pDisplay, m_mainWindow, hints_atom, hints_atom, 32,
223 PropModeReplace, (unsigned char *)&motifWmHints,
224 sizeof( motifWmHints ) / sizeof( long ) );
226 // Change the window title
227 XStoreName( m_pDisplay, m_mainWindow, "VLC Media Player" );
229 // Receive map notify events
230 XSelectInput( m_pDisplay, m_mainWindow, StructureNotifyMask );
232 // Set an empty mask for the window
233 Region mask = XCreateRegion();
234 XShapeCombineRegion( m_pDisplay, m_mainWindow, ShapeBounding, 0, 0,
236 XDestroyRegion( mask );
239 XMapWindow( m_pDisplay, m_mainWindow);
241 // Move it outside the screen to avoid seeing it in workspace selector
242 XMoveWindow( m_pDisplay, m_mainWindow, -10, -10 );
247 X11Display::~X11Display()
251 XDestroyWindow( m_pDisplay, m_mainWindow );
255 XFreeGC( m_pDisplay, m_gc );
259 XFreeColormap( m_pDisplay, m_colormap );
263 XCloseDisplay( m_pDisplay );
268 void X11Display::getShifts( uint32_t mask, int &rLeftShift,
269 int &rRightShift ) const
271 for( rLeftShift = 0; (rLeftShift < 32) && !(mask & 1); rLeftShift++ )
275 for( rRightShift = 8; (mask & 1) ; rRightShift--)
279 if( rRightShift < 0 )
281 rLeftShift -= rRightShift;
287 void X11Display::blendPixel8( uint8_t *pPixel, uint8_t r, uint8_t g,
288 uint8_t b, uint8_t a ) const
290 uint8_t value = 255 - *pPixel;
292 BLEND_PIXEL(value, r, g, b, a, uint8_t)
294 *pPixel = 255 - value;
298 void X11Display::blendPixel16MSB( uint8_t *pPixel, uint8_t r, uint8_t g,
299 uint8_t b, uint8_t a ) const
301 uint16_t value = pPixel[1] | pPixel[0] << 8;
303 BLEND_PIXEL(value, r, g, b, a, uint16_t)
305 pPixel[1] = value; value >>= 8;
310 void X11Display::blendPixel16LSB( uint8_t *pPixel, uint8_t r, uint8_t g,
311 uint8_t b, uint8_t a ) const
313 uint16_t value = pPixel[0] | pPixel[1] << 8;
315 BLEND_PIXEL(value, r, g, b, a, uint16_t)
317 pPixel[0] = value; value >>= 8;
322 void X11Display::blendPixel32MSB( uint8_t *pPixel, uint8_t r, uint8_t g,
323 uint8_t b, uint8_t a ) const
325 uint32_t value = pPixel[3] | pPixel[2] << 8 | pPixel[1] << 16 |
328 BLEND_PIXEL(value, r, g, b, a, uint32_t)
330 pPixel[3] = value; value >>= 8;
331 pPixel[2] = value; value >>= 8;
332 pPixel[1] = value; value >>= 8;
337 void X11Display::blendPixel32LSB( uint8_t *pPixel, uint8_t r, uint8_t g,
338 uint8_t b, uint8_t a ) const
340 uint32_t value = pPixel[0] | pPixel[1] << 8 | pPixel[2] << 16 |
343 BLEND_PIXEL(value, r, g, b, a, uint32_t)
345 pPixel[0] = value; value >>= 8;
346 pPixel[1] = value; value >>= 8;
347 pPixel[2] = value; value >>= 8;
352 void X11Display::putPixel8( uint8_t *pPixel, uint8_t r, uint8_t g,
353 uint8_t b, uint8_t a ) const
355 uint8_t value = 255 - *pPixel;
357 PUT_PIXEL(value, r, g, b, uint8_t)
359 *pPixel = 255 - value;
363 void X11Display::putPixel16MSB( uint8_t *pPixel, uint8_t r, uint8_t g,
364 uint8_t b, uint8_t a ) const
366 uint16_t value = pPixel[1] | pPixel[0] << 8;
368 PUT_PIXEL(value, r, g, b, uint16_t)
370 pPixel[1] = value; value >>= 8;
375 void X11Display::putPixel16LSB( uint8_t *pPixel, uint8_t r, uint8_t g,
376 uint8_t b, uint8_t a ) const
378 uint16_t value = pPixel[0] | pPixel[1] << 8;
380 PUT_PIXEL(value, r, g, b, uint16_t)
382 pPixel[0] = value; value >>= 8;
387 void X11Display::putPixel32MSB( uint8_t *pPixel, uint8_t r, uint8_t g,
388 uint8_t b, uint8_t a ) const
390 uint32_t value = pPixel[3] | pPixel[2] << 8 | pPixel[1] << 16 |
393 PUT_PIXEL(value, r, g, b, uint32_t)
395 pPixel[3] = value; value >>= 8;
396 pPixel[2] = value; value >>= 8;
397 pPixel[1] = value; value >>= 8;
402 void X11Display::putPixel32LSB( uint8_t *pPixel, uint8_t r, uint8_t g,
403 uint8_t b, uint8_t a ) const
405 uint32_t value = pPixel[0] | pPixel[1] << 8 | pPixel[2] << 16 |
408 PUT_PIXEL(value, r, g, b, uint32_t)
410 pPixel[0] = value; value >>= 8;
411 pPixel[1] = value; value >>= 8;
412 pPixel[2] = value; value >>= 8;
417 unsigned long X11Display::getPixelValue( uint8_t r, uint8_t g, uint8_t b )
422 PUT_PIXEL(value, r, g, b, uint32_t)
424 if( m_pixelSize == 1 )