]> git.sesse.net Git - vlc/blob - modules/gui/skins2/x11/x11_factory.cpp
skins2(Linux): display list of monitors and caracteristics for debug
[vlc] / modules / gui / skins2 / x11 / x11_factory.cpp
1 /*****************************************************************************
2  * x11_factory.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #ifdef X11_SKINS
26
27 #include <unistd.h>
28 #include <dirent.h>
29 #include <sys/stat.h>
30 #include <X11/Xlib.h>
31 #include <X11/extensions/Xinerama.h>
32
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"
41
42 #include "../src/generic_window.hpp"
43
44 #include <vlc_common.h>
45 #include <vlc_xlib.h>
46
47 X11Factory::X11Factory( intf_thread_t *pIntf ): OSFactory( pIntf ),
48     m_pDisplay( NULL ), m_pTimerLoop( NULL ), m_dirSep( "/" )
49 {
50     // see init()
51 }
52
53
54 X11Factory::~X11Factory()
55 {
56     delete m_pTimerLoop;
57     delete m_pDisplay;
58 }
59
60
61 bool X11Factory::init()
62 {
63     // make sure xlib is safe-thread
64     if( !vlc_xlib_init( VLC_OBJECT( getIntf() ) ) )
65     {
66         msg_Err( getIntf(), "initializing xlib for multi-threading failed" );
67         return false;
68     }
69
70     // Create the X11 display
71     m_pDisplay = new X11Display( getIntf() );
72
73     // Get the display
74     Display *pDisplay = m_pDisplay->getDisplay();
75     if( pDisplay == NULL )
76     {
77         // Initialization failed
78         return false;
79     }
80
81     // Create the timer loop
82     m_pTimerLoop = new X11TimerLoop( getIntf(),
83                                      ConnectionNumber( pDisplay ) );
84
85     // Initialize the resource path
86     char *datadir = config_GetUserDir( VLC_DATA_DIR );
87     m_resourcePath.push_back( (string)datadir + "/skins2" );
88     free( datadir );
89     m_resourcePath.push_back( (string)"share/skins2" );
90     datadir = config_GetDataDir();
91     m_resourcePath.push_back( (string)datadir + "/skins2" );
92     free( datadir );
93
94     // Determine the monitor geometry
95     getDefaultGeometry( &m_screenWidth, &m_screenHeight );
96
97     // list all available monitors
98     int num_screen;
99     XineramaScreenInfo* info = XineramaQueryScreens( pDisplay, &num_screen );
100     if( info )
101     {
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 );
107         XFree( info );
108     }
109
110     return true;
111 }
112
113
114 OSGraphics *X11Factory::createOSGraphics( int width, int height )
115 {
116     return new X11Graphics( getIntf(), *m_pDisplay, width, height );
117 }
118
119
120 OSLoop *X11Factory::getOSLoop()
121 {
122     return X11Loop::instance( getIntf(), *m_pDisplay );
123 }
124
125
126 void X11Factory::destroyOSLoop()
127 {
128     X11Loop::destroy( getIntf() );
129 }
130
131 void X11Factory::minimize()
132 {
133     XIconifyWindow( m_pDisplay->getDisplay(), m_pDisplay->getMainWindow(),
134                     DefaultScreen( m_pDisplay->getDisplay() ) );
135 }
136
137 void X11Factory::restore()
138 {
139     // TODO
140 }
141
142 void X11Factory::addInTray()
143 {
144     // TODO
145 }
146
147 void X11Factory::removeFromTray()
148 {
149     // TODO
150 }
151
152 void X11Factory::addInTaskBar()
153 {
154     // TODO
155 }
156
157 void X11Factory::removeFromTaskBar()
158 {
159     // TODO
160 }
161
162 OSTimer *X11Factory::createOSTimer( CmdGeneric &rCmd )
163 {
164     return new X11Timer( getIntf(), rCmd );
165 }
166
167
168 OSWindow *X11Factory::createOSWindow( GenericWindow &rWindow, bool dragDrop,
169                                       bool playOnDrop, OSWindow *pParent,
170                                       GenericWindow::WindowType_t type )
171 {
172     return new X11Window( getIntf(), rWindow, *m_pDisplay, dragDrop,
173                           playOnDrop, (X11Window*)pParent, type );
174 }
175
176
177 OSTooltip *X11Factory::createOSTooltip()
178 {
179     return new X11Tooltip( getIntf(), *m_pDisplay );
180 }
181
182
183 OSPopup *X11Factory::createOSPopup()
184 {
185     return new X11Popup( getIntf(), *m_pDisplay );
186 }
187
188
189 int X11Factory::getScreenWidth() const
190 {
191     return m_screenWidth;
192 }
193
194
195 int X11Factory::getScreenHeight() const
196 {
197     return m_screenHeight;
198 }
199
200
201 void X11Factory::getMonitorInfo( const GenericWindow &rWindow,
202                                  int* p_x, int* p_y,
203                                  int* p_width, int* p_height ) const
204 {
205     // initialize to default geometry
206     *p_x = 0;
207     *p_y = 0;
208     *p_width = getScreenWidth();
209     *p_height = getScreenHeight();
210
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 );
216     Window child_wnd;
217
218     int x, y;
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 );
222
223     int num;
224     XineramaScreenInfo* info = XineramaQueryScreens( pDisplay, &num );
225     if( info )
226     {
227         Region reg1 = XCreateRegion();
228         XRectangle rect1 = { x, y, w, h };
229         XUnionRectWithRegion( &rect1, reg1, reg1 );
230
231         unsigned int surface = 0;
232         for( int i = 0; i < num; i++ )
233         {
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 );
238
239             Region reg = XCreateRegion();
240             XIntersectRegion( reg1, reg2, reg );
241             XRectangle rect;
242             XClipBox( reg, &rect );
243             unsigned int surf = rect.width * rect.height;
244             if( surf > surface )
245             {
246                surface = surf;
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;
251             }
252             XDestroyRegion( reg );
253             XDestroyRegion( reg2 );
254         }
255         XDestroyRegion( reg1 );
256         XFree( info );
257     }
258 }
259
260
261 void X11Factory::getMonitorInfo( int numScreen,
262                                  int* p_x, int* p_y,
263                                  int* p_width, int* p_height ) const
264 {
265     // initialize to default geometry
266     *p_x = 0;
267     *p_y = 0;
268     *p_width = getScreenWidth();
269     *p_height = getScreenHeight();
270
271     // try to detect the requested screen via Xinerama
272     if( numScreen >= 0 )
273     {
274         int num;
275         Display *pDisplay = m_pDisplay->getDisplay();
276         XineramaScreenInfo* info = XineramaQueryScreens( pDisplay, &num );
277         if( info )
278         {
279             if( numScreen < num )
280             {
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;
285             }
286             XFree( info );
287         }
288     }
289 }
290
291
292 void X11Factory::getDefaultGeometry( int* p_width, int* p_height ) const
293 {
294     Display *pDisplay = m_pDisplay->getDisplay();
295
296     // Initialize to defaults
297     int screen = DefaultScreen( pDisplay );
298     *p_width = DisplayWidth( pDisplay, screen );
299     *p_height = DisplayHeight( pDisplay, screen );
300
301     // Use Xinerama to restrain to the first monitor instead of the full
302     // virtual screen
303     int num;
304     XineramaScreenInfo* info = XineramaQueryScreens( pDisplay, &num );
305     if( info )
306     {
307         for( int i = 0; i < num; i++ )
308         {
309             if( info[i].x_org == 0 && info[i].y_org == 0 )
310             {
311                 *p_width = info[i].width;
312                 *p_height = info[i].height;
313                 break;
314             }
315         }
316         XFree( info );
317     }
318 }
319
320
321 SkinsRect X11Factory::getWorkArea() const
322 {
323     // XXX
324     return SkinsRect( 0, 0, getScreenWidth(), getScreenHeight() );
325 }
326
327
328 void X11Factory::getMousePos( int &rXPos, int &rYPos ) const
329 {
330     Window rootReturn, childReturn;
331     int winx, winy;
332     unsigned int xmask;
333
334     Display *pDisplay = m_pDisplay->getDisplay();
335     Window root = DefaultRootWindow( pDisplay );
336     XQueryPointer( pDisplay, root, &rootReturn, &childReturn,
337                    &rXPos, &rYPos, &winx, &winy, &xmask );
338 }
339
340
341 void X11Factory::rmDir( const string &rPath )
342 {
343     struct dirent *file;
344     DIR *dir;
345
346     dir = opendir( rPath.c_str() );
347     if( !dir ) return;
348
349     // Parse the directory and remove everything it contains
350     while( (file = readdir( dir )) )
351     {
352         struct stat statbuf;
353         string filename = file->d_name;
354
355         // Skip "." and ".."
356         if( filename == "." || filename == ".." )
357         {
358             continue;
359         }
360
361         filename = rPath + "/" + filename;
362
363         if( !stat( filename.c_str(), &statbuf ) && statbuf.st_mode & S_IFDIR )
364         {
365             rmDir( filename );
366         }
367         else
368         {
369             unlink( filename.c_str() );
370         }
371     }
372
373     // Close the directory
374     closedir( dir );
375
376     // And delete it
377     rmdir( rPath.c_str() );
378 }
379
380 #endif