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 *****************************************************************************/
31 #include <X11/extensions/Xinerama.h>
33 #include "x11_factory.hpp"
34 #include "x11_display.hpp"
35 #include "x11_graphics.hpp"
36 #include "x11_loop.hpp"
37 #include "x11_popup.hpp"
38 #include "x11_timer.hpp"
39 #include "x11_window.hpp"
40 #include "x11_tooltip.hpp"
42 #include "../src/generic_window.hpp"
44 #include <vlc_common.h>
47 X11Factory::X11Factory( intf_thread_t *pIntf ): OSFactory( pIntf ),
48 m_pDisplay( NULL ), m_pTimerLoop( NULL ), m_dirSep( "/" )
54 X11Factory::~X11Factory()
61 bool X11Factory::init()
63 // make sure xlib is safe-thread
64 if( !vlc_xlib_init( VLC_OBJECT( getIntf() ) ) )
66 msg_Err( getIntf(), "initializing xlib for multi-threading failed" );
70 // Create the X11 display
71 m_pDisplay = new X11Display( getIntf() );
74 Display *pDisplay = m_pDisplay->getDisplay();
75 if( pDisplay == NULL )
77 // Initialization failed
81 // Create the timer loop
82 m_pTimerLoop = new X11TimerLoop( getIntf(),
83 ConnectionNumber( pDisplay ) );
85 // Initialize the resource path
86 char *datadir = config_GetUserDir( VLC_DATA_DIR );
87 m_resourcePath.push_back( (string)datadir + "/skins2" );
89 m_resourcePath.push_back( (string)"share/skins2" );
90 datadir = config_GetDataDir();
91 m_resourcePath.push_back( (string)datadir + "/skins2" );
94 // Determine the monitor geometry
95 getDefaultGeometry( &m_screenWidth, &m_screenHeight );
97 // list all available monitors
99 XineramaScreenInfo* info = XineramaQueryScreens( pDisplay, &num_screen );
102 msg_Dbg( getIntf(), "number of monitors detected : %i", num_screen );
103 for( int i = 0; i < num_screen; i++ )
104 msg_Dbg( getIntf(), " monitor #%i : %ix%i at +%i+%i",
105 i, info[i].width, info[i].height,
106 info[i].x_org, info[i].y_org );
114 OSGraphics *X11Factory::createOSGraphics( int width, int height )
116 return new X11Graphics( getIntf(), *m_pDisplay, width, height );
120 OSLoop *X11Factory::getOSLoop()
122 return X11Loop::instance( getIntf(), *m_pDisplay );
126 void X11Factory::destroyOSLoop()
128 X11Loop::destroy( getIntf() );
131 void X11Factory::minimize()
133 XIconifyWindow( m_pDisplay->getDisplay(), m_pDisplay->getMainWindow(),
134 DefaultScreen( m_pDisplay->getDisplay() ) );
137 void X11Factory::restore()
142 void X11Factory::addInTray()
147 void X11Factory::removeFromTray()
152 void X11Factory::addInTaskBar()
157 void X11Factory::removeFromTaskBar()
162 OSTimer *X11Factory::createOSTimer( CmdGeneric &rCmd )
164 return new X11Timer( getIntf(), rCmd );
168 OSWindow *X11Factory::createOSWindow( GenericWindow &rWindow, bool dragDrop,
169 bool playOnDrop, OSWindow *pParent,
170 GenericWindow::WindowType_t type )
172 return new X11Window( getIntf(), rWindow, *m_pDisplay, dragDrop,
173 playOnDrop, (X11Window*)pParent, type );
177 OSTooltip *X11Factory::createOSTooltip()
179 return new X11Tooltip( getIntf(), *m_pDisplay );
183 OSPopup *X11Factory::createOSPopup()
185 return new X11Popup( getIntf(), *m_pDisplay );
189 int X11Factory::getScreenWidth() const
191 return m_screenWidth;
195 int X11Factory::getScreenHeight() const
197 return m_screenHeight;
201 void X11Factory::getMonitorInfo( const GenericWindow &rWindow,
203 int* p_width, int* p_height ) const
205 // initialize to default geometry
208 *p_width = getScreenWidth();
209 *p_height = getScreenHeight();
211 // Use Xinerama to determine the monitor where the video
212 // mostly resides (biggest surface)
213 Display *pDisplay = m_pDisplay->getDisplay();
214 Window wnd = (Window)rWindow.getOSHandle();
215 Window root = DefaultRootWindow( pDisplay );
219 unsigned int w, h, border, depth;
220 XGetGeometry( pDisplay, wnd, &root, &x, &y, &w, &h, &border, &depth );
221 XTranslateCoordinates( pDisplay, wnd, root, 0, 0, &x, &y, &child_wnd );
224 XineramaScreenInfo* info = XineramaQueryScreens( pDisplay, &num );
227 Region reg1 = XCreateRegion();
228 XRectangle rect1 = { x, y, w, h };
229 XUnionRectWithRegion( &rect1, reg1, reg1 );
231 unsigned int surface = 0;
232 for( int i = 0; i < num; i++ )
234 Region reg2 = XCreateRegion();
235 XRectangle rect2 = { info[i].x_org, info[i].y_org,
236 info[i].width, info[i].height };
237 XUnionRectWithRegion( &rect2, reg2, reg2 );
239 Region reg = XCreateRegion();
240 XIntersectRegion( reg1, reg2, reg );
242 XClipBox( reg, &rect );
243 unsigned int surf = rect.width * rect.height;
247 *p_x = info[i].x_org;
248 *p_y = info[i].y_org;
249 *p_width = info[i].width;
250 *p_height = info[i].height;
252 XDestroyRegion( reg );
253 XDestroyRegion( reg2 );
255 XDestroyRegion( reg1 );
261 void X11Factory::getMonitorInfo( int numScreen,
263 int* p_width, int* p_height ) const
265 // initialize to default geometry
268 *p_width = getScreenWidth();
269 *p_height = getScreenHeight();
271 // try to detect the requested screen via Xinerama
275 Display *pDisplay = m_pDisplay->getDisplay();
276 XineramaScreenInfo* info = XineramaQueryScreens( pDisplay, &num );
279 if( numScreen < num )
281 *p_x = info[numScreen].x_org;
282 *p_y = info[numScreen].y_org;
283 *p_width = info[numScreen].width;
284 *p_height = info[numScreen].height;
292 void X11Factory::getDefaultGeometry( int* p_width, int* p_height ) const
294 Display *pDisplay = m_pDisplay->getDisplay();
296 // Initialize to defaults
297 int screen = DefaultScreen( pDisplay );
298 *p_width = DisplayWidth( pDisplay, screen );
299 *p_height = DisplayHeight( pDisplay, screen );
301 // Use Xinerama to restrain to the first monitor instead of the full
304 XineramaScreenInfo* info = XineramaQueryScreens( pDisplay, &num );
307 for( int i = 0; i < num; i++ )
309 if( info[i].x_org == 0 && info[i].y_org == 0 )
311 *p_width = info[i].width;
312 *p_height = info[i].height;
321 SkinsRect X11Factory::getWorkArea() const
324 return SkinsRect( 0, 0, getScreenWidth(), getScreenHeight() );
328 void X11Factory::getMousePos( int &rXPos, int &rYPos ) const
330 Window rootReturn, childReturn;
334 Display *pDisplay = m_pDisplay->getDisplay();
335 Window root = DefaultRootWindow( pDisplay );
336 XQueryPointer( pDisplay, root, &rootReturn, &childReturn,
337 &rXPos, &rYPos, &winx, &winy, &xmask );
341 void X11Factory::rmDir( const string &rPath )
346 dir = opendir( rPath.c_str() );
349 // Parse the directory and remove everything it contains
350 while( (file = readdir( dir )) )
353 string filename = file->d_name;
356 if( filename == "." || filename == ".." )
361 filename = rPath + "/" + filename;
363 if( !stat( filename.c_str(), &statbuf ) && statbuf.st_mode & S_IFDIR )
369 unlink( filename.c_str() );
373 // Close the directory
377 rmdir( rPath.c_str() );