]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/skin_parser.cpp
dbf2e76282b7aea30026c89d6aa72ceefcd7ad4f
[vlc] / modules / gui / skins2 / parser / skin_parser.cpp
1 /*****************************************************************************
2  * skin_parser.cpp
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include "skin_parser.hpp"
25 #include "../src/os_factory.hpp"
26 #include <math.h>
27 #include <libxml/catalog.h>
28 #include <sys/stat.h>
29
30 // Current DTD version
31 #define SKINS_DTD_VERSION "2.0"
32
33 // Static variable to avoid initializing catalogs twice
34 bool SkinParser::m_initialized = false;
35
36
37 SkinParser::SkinParser( intf_thread_t *pIntf, const string &rFileName,
38                         const string &rPath ):
39     XMLParser( pIntf, rFileName ), m_xOffset( 0 ), m_yOffset( 0 ),
40     m_path( rPath )
41 {
42     // Avoid duplicate initialization (mutex needed ?)
43     if( !m_initialized )
44     {
45         // Get the resource path and look for the DTD
46         OSFactory *pOSFactory = OSFactory::instance( getIntf() );
47         const list<string> &resPath = pOSFactory->getResourcePath();
48         const string &sep = pOSFactory->getDirSeparator();
49         list<string>::const_iterator it;
50         struct stat statBuf;
51
52         // Try to load the catalog first (needed at least on win32 where
53         // we don't have a default catalog)
54         for( it = resPath.begin(); it != resPath.end(); it++ )
55         {
56             string catalog_path = (*it) + sep + "skin.catalog";
57             if( !stat( catalog_path.c_str(), &statBuf ) )
58             {
59                 msg_Dbg( getIntf(), "Using catalog %s", catalog_path.c_str() );
60                 xmlLoadCatalog( catalog_path.c_str() );
61                 break;
62             }
63         }
64         if( it == resPath.end() )
65         {
66             // Ok, try the default one
67             xmlInitializeCatalog();
68         }
69
70         for( it = resPath.begin(); it != resPath.end(); it++ )
71         {
72             string path = (*it) + sep + "skin.dtd";
73             if( !stat( path.c_str(), &statBuf ) )
74             {
75                 // DTD found
76                 msg_Dbg( getIntf(), "Using DTD %s", path.c_str() );
77
78                 // Add an entry in the default catalog
79                 xmlCatalogAdd( (xmlChar*)"public",
80                                (xmlChar*)("-//VideoLAN//DTD VLC Skins V"
81                                           SKINS_DTD_VERSION "//EN"),
82                                (xmlChar*)path.c_str() );
83                 break;
84             }
85         }
86         if( it == resPath.end() )
87         {
88             msg_Err( getIntf(), "Cannot find the skins DTD !");
89         }
90         m_initialized = true;
91     }
92 }
93
94
95 void SkinParser::handleBeginElement( const string &rName, AttrList_t &attr )
96 {
97     if( rName == "Anchor" )
98     {
99         const BuilderData::Anchor anchor( atoi( attr["x"] ) + m_xOffset,
100                 atoi( attr["y"] ) + m_yOffset, atoi( attr["range"] ),
101                 atoi( attr["priority"] ), attr["points"], m_curLayoutId );
102         m_data.m_listAnchor.push_back( anchor );
103     }
104
105     else if( rName == "Bitmap" )
106     {
107         const BuilderData::Bitmap bitmap( uniqueId( attr["id"] ),
108                 convertFileName( attr["file"] ),
109                 convertColor( attr["alphacolor"] ) );
110         m_data.m_listBitmap.push_back( bitmap );
111     }
112
113     else if( rName == "BitmapFont" )
114     {
115         const BuilderData::BitmapFont font( uniqueId( attr["id"] ),
116                 convertFileName( attr["file"] ),
117                 attr["type"] );
118         m_data.m_listBitmapFont.push_back( font );
119     }
120
121     else if( rName == "Button" )
122     {
123         const BuilderData::Button button( uniqueId( attr["id"] ), atoi( attr["x"] ) +
124                 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["lefttop"],
125                 attr["rightbottom"], attr["visible"], attr["up"], attr["down"],
126                 attr["over"], attr["action"], attr["tooltiptext"], attr["help"],
127                 m_curLayer, m_curWindowId, m_curLayoutId );
128         m_curLayer++;
129         m_data.m_listButton.push_back( button );
130     }
131
132     else if( rName == "Checkbox" )
133     {
134         const BuilderData::Checkbox checkbox( uniqueId( attr["id"] ), atoi( attr["x"] ) +
135                 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["lefttop"],
136                 attr["rightbottom"], attr["visible"], attr["up1"], attr["down1"], attr["over1"],
137                 attr["up2"], attr["down2"], attr["over2"], attr["state"],
138                 attr["action1"], attr["action2"], attr["tooltiptext1"],
139                 attr["tooltiptext2"], attr["help"], m_curLayer, m_curWindowId,
140                 m_curLayoutId );
141         m_curLayer++;
142         m_data.m_listCheckbox.push_back( checkbox );
143     }
144
145     else if( rName == "Font" )
146     {
147         const BuilderData::Font fontData( uniqueId( attr["id"] ),
148                 convertFileName( attr["file"] ),
149                 atoi( attr["size"] ) );
150         m_data.m_listFont.push_back( fontData );
151     }
152
153     else if( rName == "Group" )
154     {
155         m_xOffset += atoi( attr["x"] );
156         m_yOffset += atoi( attr["y"] );
157         m_xOffsetList.push_back( atoi( attr["x"] ) );
158         m_yOffsetList.push_back( atoi( attr["y"] ) );
159     }
160
161     else if( rName == "Image" )
162     {
163         const BuilderData::Image imageData( uniqueId( attr["id"] ), atoi( attr["x"] ) +
164                 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["lefttop"],
165                 attr["rightbottom"], attr["visible"],
166                 attr["image"], attr["action"], attr["help"], m_curLayer,
167                 m_curWindowId, m_curLayoutId );
168         m_curLayer++;
169         m_data.m_listImage.push_back( imageData );
170     }
171
172     else if( rName == "Layout" )
173     {
174         m_curLayoutId = uniqueId( attr["id"] );
175         const BuilderData::Layout layout( m_curLayoutId, atoi( attr["width"] ),
176                 atoi( attr["height"] ), atoi( attr["minwidth"] ),
177                 atoi( attr["maxwidth"] ), atoi( attr["minheight"] ),
178                 atoi( attr["maxheight"] ), m_curWindowId );
179         m_data.m_listLayout.push_back( layout );
180         m_curLayer = 0;
181     }
182
183     else if( rName == "Playlist" )
184     {
185         m_curListId = uniqueId( attr["id"] );
186         const BuilderData::List listData( m_curListId, atoi( attr["x"] ) +
187                 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
188                 atoi( attr["width"]), atoi( attr["height"] ),
189                 attr["lefttop"], attr["rightbottom"],
190                 attr["font"], "playlist", convertColor( attr["fgcolor"] ),
191                 convertColor( attr["playcolor"] ),
192                 convertColor( attr["bgcolor1"] ),
193                 convertColor( attr["bgcolor2"] ),
194                 convertColor( attr["selcolor"] ), attr["help"],
195                 m_curLayer, m_curWindowId, m_curLayoutId );
196         m_curLayer++;
197         m_data.m_listList.push_back( listData );
198     }
199
200     else if( rName == "RadialSlider" )
201     {
202         const BuilderData::RadialSlider radial( uniqueId( attr["id"] ),
203                 attr["visible"],
204                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
205                 attr["lefttop"], attr["rightbottom"], attr["sequence"],
206                 atoi( attr["nbImages"] ), atof( attr["minAngle"] ) * M_PI / 180,
207                 atof( attr["maxAngle"] ) * M_PI / 180, attr["value"],
208                 attr["tooltiptext"], attr["help"], m_curLayer, m_curWindowId,
209                 m_curLayoutId );
210         m_curLayer++;
211         m_data.m_listRadialSlider.push_back( radial );
212     }
213
214     else if( rName == "Slider" )
215     {
216         string newValue = attr["value"];
217         if( m_curListId != "" )
218         {
219             // Slider associated to a list
220             newValue = "playlist.slider";
221         }
222         const BuilderData::Slider slider( uniqueId( attr["id"] ),
223                 attr["visible"],
224                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
225                 attr["lefttop"], attr["rightbottom"], attr["up"], attr["down"],
226                 attr["over"], attr["points"], atoi( attr["thickness"] ),
227                 newValue, attr["tooltiptext"], attr["help"], m_curLayer,
228                 m_curWindowId, m_curLayoutId );
229         m_curLayer++;
230         m_data.m_listSlider.push_back( slider );
231     }
232
233     else if( rName == "Text" )
234     {
235         const BuilderData::Text textData( uniqueId( attr["id"] ),
236                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
237                 attr["visible"], attr["font"],
238                 attr["text"], atoi( attr["width"] ),
239                 convertColor( attr["color"] ), attr["help"], m_curLayer,
240                 m_curWindowId, m_curLayoutId );
241         m_curLayer++;
242         m_data.m_listText.push_back( textData );
243     }
244
245     else if( rName == "Theme" )
246     {
247         // Check the version
248         if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
249         {
250             msg_Err( getIntf(), "Bad theme version : %s (you need version %s)",
251                      attr["version"], SKINS_DTD_VERSION );
252             m_errors = true;
253             return;
254         }
255         const BuilderData::Theme theme( attr["tooltipfont"],
256                 atoi( attr["magnet"] ),
257                 convertInRange( attr["alpha"], 1, 255, "alpha" ),
258                 convertInRange( attr["movealpha"], 1, 255, "movealpha" ) );
259         m_data.m_listTheme.push_back( theme );
260     }
261
262     else if( rName == "ThemeInfo" )
263     {
264         msg_Info( getIntf(), "skin: %s  author: %s", attr["name"],
265                   attr["author"] );
266     }
267
268     else if( rName == "Video" )
269     {
270         const BuilderData::Video videoData( uniqueId( attr["id"] ),
271                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
272                 atoi( attr["width"] ), atoi( attr["height" ]),
273                 attr["lefttop"], attr["rightbottom"],
274                 attr["visible"], attr["help"], m_curLayer,
275                 m_curWindowId, m_curLayoutId );
276         m_curLayer++;
277         m_data.m_listVideo.push_back( videoData );
278     }
279
280     else if( rName == "Window" )
281     {
282         m_curWindowId = uniqueId( attr["id"] );
283         const BuilderData::Window window( m_curWindowId,
284                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
285                 convertBoolean( attr["visible"] ),
286                 convertBoolean( attr["dragdrop"] ),
287                 convertBoolean( attr["playondrop"] ) );
288         m_data.m_listWindow.push_back( window );
289     }
290 }
291
292
293 void SkinParser::handleEndElement( const string &rName )
294 {
295     if( rName == "Group" )
296     {
297         m_xOffset -= m_xOffsetList.back();
298         m_yOffset -= m_yOffsetList.back();
299         m_xOffsetList.pop_back();
300         m_yOffsetList.pop_back();
301     }
302
303     else if( rName == "Playlist" )
304     {
305         m_curListId = "";
306     }
307 }
308
309
310 bool SkinParser::convertBoolean( const char *value ) const
311 {
312     return strcmp( value, "true" ) == 0;
313 }
314
315
316 int SkinParser::convertColor( const char *transcolor ) const
317 {
318     unsigned long iRed, iGreen, iBlue;
319     iRed = iGreen = iBlue = 0;
320     sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
321     return ( iRed << 16 | iGreen << 8 | iBlue );
322 }
323
324
325 string SkinParser::convertFileName( const char *fileName ) const
326 {
327     return m_path + string( fileName );
328 }
329
330
331 int SkinParser::convertInRange( const char *value, int minValue, int maxValue,
332                                 const string &rAttribute ) const
333 {
334     int intValue = atoi( value );
335
336     if( intValue < minValue )
337     {
338         msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
339                   "expected range [%i, %i], using %i instead",
340                   rAttribute.c_str(), intValue, minValue, maxValue, minValue );
341         return minValue;
342     }
343     else if( intValue > maxValue )
344     {
345         msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
346                   "expected range [%i, %i], using %i instead",
347                   rAttribute.c_str(), intValue, minValue, maxValue, maxValue );
348         return maxValue;
349     }
350     else
351     {
352         return intValue;
353     }
354 }
355
356
357 const string SkinParser::generateId() const
358 {
359     static int i = 1;
360
361     char genId[5];
362     snprintf( genId, 4, "%i", i++ );
363
364     string base = "_ReservedId_" + (string)genId;
365
366     return base;
367 }
368
369
370 const string SkinParser::uniqueId( const string &id )
371 {
372     string newId;
373
374     if( m_idSet.find( id ) != m_idSet.end() )
375     {
376         // The id was already used
377         if( id != "none" )
378         {
379             msg_Warn( getIntf(), "Non unique id: %s", id.c_str() );
380         }
381         newId = generateId();
382     }
383     else
384     {
385         // OK, this is a new id
386         newId = id;
387     }
388
389     // Add the id to the set
390     m_idSet.insert( newId );
391
392     return newId;
393 }
394