1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 #include "skin_parser.hpp"
25 #include "../src/os_factory.hpp"
27 #include <libxml/catalog.h>
30 // Current DTD version
31 #define SKINS_DTD_VERSION "2.0"
33 // Static variable to avoid initializing catalogs twice
34 bool SkinParser::m_initialized = false;
37 SkinParser::SkinParser( intf_thread_t *pIntf, const string &rFileName,
38 const string &rPath ):
39 XMLParser( pIntf, rFileName ), m_xOffset( 0 ), m_yOffset( 0 ),
42 // Avoid duplicate initialization (mutex needed ?)
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;
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++ )
56 string catalog_path = (*it) + sep + "skin.catalog";
57 if( !stat( catalog_path.c_str(), &statBuf ) )
59 msg_Dbg( getIntf(), "Using catalog %s", catalog_path.c_str() );
60 xmlLoadCatalog( catalog_path.c_str() );
64 if( it == resPath.end() )
66 // Ok, try the default one
67 xmlInitializeCatalog();
70 for( it = resPath.begin(); it != resPath.end(); it++ )
72 string path = (*it) + sep + "skin.dtd";
73 if( !stat( path.c_str(), &statBuf ) )
76 msg_Dbg( getIntf(), "Using DTD %s", path.c_str() );
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() );
86 if( it == resPath.end() )
88 msg_Err( getIntf(), "Cannot find the skins DTD !");
95 void SkinParser::handleBeginElement( const string &rName, AttrList_t &attr )
97 if( rName == "Anchor" )
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 );
105 else if( rName == "Bitmap" )
107 const BuilderData::Bitmap bitmap( uniqueId( attr["id"] ),
108 convertFileName( attr["file"] ),
109 convertColor( attr["alphacolor"] ) );
110 m_data.m_listBitmap.push_back( bitmap );
113 else if( rName == "BitmapFont" )
115 const BuilderData::BitmapFont font( uniqueId( attr["id"] ),
116 convertFileName( attr["file"] ),
118 m_data.m_listBitmapFont.push_back( font );
121 else if( rName == "Button" )
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 );
129 m_data.m_listButton.push_back( button );
132 else if( rName == "Checkbox" )
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,
142 m_data.m_listCheckbox.push_back( checkbox );
145 else if( rName == "Font" )
147 const BuilderData::Font fontData( uniqueId( attr["id"] ),
148 convertFileName( attr["file"] ),
149 atoi( attr["size"] ) );
150 m_data.m_listFont.push_back( fontData );
153 else if( rName == "Group" )
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"] ) );
161 else if( rName == "Image" )
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 );
169 m_data.m_listImage.push_back( imageData );
172 else if( rName == "Layout" )
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 );
183 else if( rName == "Playlist" )
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 );
197 m_data.m_listList.push_back( listData );
200 else if( rName == "RadialSlider" )
202 const BuilderData::RadialSlider radial( uniqueId( attr["id"] ),
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,
211 m_data.m_listRadialSlider.push_back( radial );
214 else if( rName == "Slider" )
216 string newValue = attr["value"];
217 if( m_curListId != "" )
219 // Slider associated to a list
220 newValue = "playlist.slider";
222 const BuilderData::Slider slider( uniqueId( attr["id"] ),
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 );
230 m_data.m_listSlider.push_back( slider );
233 else if( rName == "Text" )
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 );
242 m_data.m_listText.push_back( textData );
245 else if( rName == "Theme" )
248 if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
250 msg_Err( getIntf(), "Bad theme version : %s (you need version %s)",
251 attr["version"], SKINS_DTD_VERSION );
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 );
262 else if( rName == "ThemeInfo" )
264 msg_Info( getIntf(), "skin: %s author: %s", attr["name"],
268 else if( rName == "Video" )
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 );
277 m_data.m_listVideo.push_back( videoData );
280 else if( rName == "Window" )
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 );
293 void SkinParser::handleEndElement( const string &rName )
295 if( rName == "Group" )
297 m_xOffset -= m_xOffsetList.back();
298 m_yOffset -= m_yOffsetList.back();
299 m_xOffsetList.pop_back();
300 m_yOffsetList.pop_back();
303 else if( rName == "Playlist" )
310 bool SkinParser::convertBoolean( const char *value ) const
312 return strcmp( value, "true" ) == 0;
316 int SkinParser::convertColor( const char *transcolor ) const
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 );
325 string SkinParser::convertFileName( const char *fileName ) const
327 return m_path + string( fileName );
331 int SkinParser::convertInRange( const char *value, int minValue, int maxValue,
332 const string &rAttribute ) const
334 int intValue = atoi( value );
336 if( intValue < minValue )
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 );
343 else if( intValue > maxValue )
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 );
357 const string SkinParser::generateId() const
362 snprintf( genId, 4, "%i", i++ );
364 string base = "_ReservedId_" + (string)genId;
370 const string SkinParser::uniqueId( const string &id )
374 if( m_idSet.find( id ) != m_idSet.end() )
376 // The id was already used
379 msg_Warn( getIntf(), "Non unique id: %s", id.c_str() );
381 newId = generateId();
385 // OK, this is a new id
389 // Add the id to the set
390 m_idSet.insert( newId );