]> git.sesse.net Git - vlc/blob - modules/gui/skins2/x11/x11_factory.cpp
* all: fixed segfaults when initialization fails
[vlc] / modules / gui / skins2 / x11 / x11_factory.cpp
1 /*****************************************************************************
2  * x11_factory.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: x11_factory.cpp,v 1.2 2004/01/25 13:59:33 asmax Exp $
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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
32 #include "x11_factory.hpp"
33 #include "x11_display.hpp"
34 #include "x11_graphics.hpp"
35 #include "x11_loop.hpp"
36 #include "x11_timer.hpp"
37 #include "x11_window.hpp"
38 #include "x11_tooltip.hpp"
39
40
41 X11Factory::X11Factory( intf_thread_t *pIntf ): OSFactory( pIntf ),
42     m_pDisplay( NULL ), m_pTimerLoop( NULL )
43 {
44     // see init()
45 }
46
47
48 X11Factory::~X11Factory()
49 {
50     delete m_pTimerLoop;
51     delete m_pDisplay;
52 }
53
54
55 bool X11Factory::init()
56 {
57     // Create the X11 display
58     m_pDisplay = new X11Display( getIntf() );
59
60     // Get the display
61     Display *pDisplay = m_pDisplay->getDisplay();
62     if( pDisplay == NULL )
63     {
64         // Initialization failed
65         return false;
66     }
67
68     // Create the timer loop
69     m_pTimerLoop = new X11TimerLoop( getIntf(),
70                                      ConnectionNumber( pDisplay ) );
71
72     return true;
73 }
74
75
76 OSGraphics *X11Factory::createOSGraphics( int width, int height )
77 {
78     return new X11Graphics( getIntf(), *m_pDisplay, width, height );
79 }
80
81
82 OSLoop *X11Factory::getOSLoop()
83 {
84     return X11Loop::instance( getIntf(), *m_pDisplay );
85 }
86
87
88 void X11Factory::destroyOSLoop()
89 {
90     X11Loop::destroy( getIntf() );
91 }
92
93
94 OSTimer *X11Factory::createOSTimer( const Callback &rCallback )
95 {
96     return new X11Timer( getIntf(), rCallback );
97 }
98
99
100 OSWindow *X11Factory::createOSWindow( GenericWindow &rWindow, bool dragDrop,
101                                       bool playOnDrop )
102 {
103     return new X11Window( getIntf(), rWindow, *m_pDisplay, dragDrop,
104                           playOnDrop );
105 }
106
107
108 OSTooltip *X11Factory::createOSTooltip()
109 {
110     return new X11Tooltip( getIntf(), *m_pDisplay );
111 }
112
113
114 const string X11Factory::getDirSeparator() const
115 {
116     return "/";
117 }
118
119
120 int X11Factory::getScreenWidth() const
121 {
122     Display *pDisplay = m_pDisplay->getDisplay();
123     int screen = DefaultScreen( pDisplay );
124     return DisplayWidth( pDisplay, screen );
125 }
126
127
128 int X11Factory::getScreenHeight() const
129 {
130     Display *pDisplay = m_pDisplay->getDisplay();
131     int screen = DefaultScreen( pDisplay );
132     return DisplayHeight( pDisplay, screen );
133 }
134
135
136 Rect X11Factory::getWorkArea() const
137 {
138     // XXX
139     return Rect( 0, 0, getScreenWidth(), getScreenHeight() );
140 }
141
142
143 void X11Factory::getMousePos( int &rXPos, int &rYPos ) const
144 {
145     Window rootReturn, childReturn;
146     int winx, winy;
147     unsigned int xmask;
148
149     Display *pDisplay = m_pDisplay->getDisplay();
150     Window root = DefaultRootWindow( pDisplay );
151     XQueryPointer( pDisplay, root, &rootReturn, &childReturn,
152                    &rXPos, &rYPos, &winx, &winy, &xmask );
153 }
154
155
156 void X11Factory::rmDir( const string &rPath )
157 {
158     struct dirent *file;
159     DIR *dir;
160
161     dir = opendir( rPath.c_str() );
162     if( !dir ) return;
163
164     // Parse the directory and remove everything it contains
165     while( (file = readdir( dir )) )
166     {
167         struct stat statbuf;
168         string filename = file->d_name;
169
170         // Skip "." and ".."
171         if( filename == "." || filename == ".." )
172         {
173             continue;
174         }
175
176         filename = rPath + "/" + filename;
177
178         if( !stat( filename.c_str(), &statbuf ) && statbuf.st_mode & S_IFDIR )
179         {
180             rmDir( filename );
181         }
182         else
183         {
184             unlink( filename.c_str() );
185         }
186     }
187
188     // Close the directory
189     closedir( dir );
190
191     // And delete it
192     rmdir( rPath.c_str() );
193 }
194
195
196 #endif