]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/skin_parser.cpp
7f5a7e8c1c2db1df3c23e4fdab1078ff08d35f9b
[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 <math.h>
26
27 SkinParser::SkinParser( intf_thread_t *pIntf, const string &rFileName,
28                         const string &rPath ):
29     XMLParser( pIntf, rFileName ), m_xOffset( 0 ), m_yOffset( 0 ),
30     m_path( rPath )
31 {
32 }
33
34
35 void SkinParser::handleBeginElement( const string &rName, AttrList_t &attr )
36 {
37 #define CheckDefault( a, b ) \
38     if( attr.find(a) == attr.end() ) attr[strdup(a)] = strdup(b);
39 #define RequireDefault( a ) \
40     if( attr.find(a) == attr.end() ) \
41     { \
42         msg_Err( getIntf(), "Bad theme (element: %s, missing attribute: %s)", \
43                  rName.c_str(), a ); \
44         m_errors = true; return; \
45     }
46
47     if( rName == "Anchor" )
48     {
49         RequireDefault( "priority" );
50         CheckDefault( "x", "0" );
51         CheckDefault( "y", "0" );
52         CheckDefault( "points", "(0,0)" );
53         CheckDefault( "range", "10" );
54
55         const BuilderData::Anchor anchor( atoi( attr["x"] ) + m_xOffset,
56                 atoi( attr["y"] ) + m_yOffset, atoi( attr["range"] ),
57                 atoi( attr["priority"] ), attr["points"], m_curLayoutId );
58         m_data.m_listAnchor.push_back( anchor );
59     }
60
61     else if( rName == "Bitmap" )
62     {
63         RequireDefault( "id" );
64         RequireDefault( "file" );
65         RequireDefault( "alphacolor" );
66
67         const BuilderData::Bitmap bitmap( uniqueId( attr["id"] ),
68                 convertFileName( attr["file"] ),
69                 convertColor( attr["alphacolor"] ) );
70         m_data.m_listBitmap.push_back( bitmap );
71     }
72
73     else if( rName == "BitmapFont" )
74     {
75         RequireDefault( "id" );
76         RequireDefault( "file" );
77         CheckDefault( "type", "digits" );
78
79         const BuilderData::BitmapFont font( uniqueId( attr["id"] ),
80                 convertFileName( attr["file"] ),
81                 attr["type"] );
82         m_data.m_listBitmapFont.push_back( font );
83     }
84
85     else if( rName == "Button" )
86     {
87         RequireDefault( "up" );
88         CheckDefault( "id", "none" );
89         CheckDefault( "visible", "true" );
90         CheckDefault( "x", "0" );
91         CheckDefault( "y", "0" );
92         CheckDefault( "lefttop", "lefttop" );
93         CheckDefault( "rightbottom", "lefttop" );
94         CheckDefault( "down", "none" );
95         CheckDefault( "over", "none" );
96         CheckDefault( "action", "none" );
97         CheckDefault( "tooltiptext", "" );
98         CheckDefault( "help", "" );
99
100         const BuilderData::Button button( uniqueId( attr["id"] ),
101                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
102                 attr["lefttop"], attr["rightbottom"], attr["visible"],
103                 attr["up"], attr["down"], attr["over"], attr["action"],
104                 attr["tooltiptext"], attr["help"],
105                 m_curLayer, m_curWindowId, m_curLayoutId );
106         m_curLayer++;
107         m_data.m_listButton.push_back( button );
108     }
109
110     else if( rName == "Checkbox" )
111     {
112         RequireDefault( "up1" );
113         RequireDefault( "up2" );
114         RequireDefault( "state" );
115         CheckDefault( "id", "none" );
116         CheckDefault( "visible", "true" );
117         CheckDefault( "x", "0" );
118         CheckDefault( "y", "0" );
119         CheckDefault( "lefttop", "lefttop" );
120         CheckDefault( "rightbottom", "lefttop" );
121         CheckDefault( "down1", "none" );
122         CheckDefault( "over1", "none" );
123         CheckDefault( "down2", "none" );
124         CheckDefault( "over2", "none" );
125         CheckDefault( "action1", "none" );
126         CheckDefault( "action2", "none" );
127         CheckDefault( "tooltiptext1", "" );
128         CheckDefault( "tooltiptext2", "" );
129         CheckDefault( "help", "" );
130
131         const BuilderData::Checkbox checkbox( uniqueId( attr["id"] ),
132                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
133                 attr["lefttop"], attr["rightbottom"], attr["visible"],
134                 attr["up1"], attr["down1"], attr["over1"],
135                 attr["up2"], attr["down2"], attr["over2"], attr["state"],
136                 attr["action1"], attr["action2"], attr["tooltiptext1"],
137                 attr["tooltiptext2"], attr["help"], m_curLayer, m_curWindowId,
138                 m_curLayoutId );
139         m_curLayer++;
140         m_data.m_listCheckbox.push_back( checkbox );
141     }
142
143     else if( rName == "Font" )
144     {
145         RequireDefault( "id" );
146         RequireDefault( "file" );
147         CheckDefault( "size", "12" );
148
149         const BuilderData::Font fontData( uniqueId( attr["id"] ),
150                 convertFileName( attr["file"] ),
151                 atoi( attr["size"] ) );
152         m_data.m_listFont.push_back( fontData );
153     }
154
155     else if( rName == "Group" )
156     {
157         CheckDefault( "x", "0" );
158         CheckDefault( "y", "0" );
159
160         m_xOffset += atoi( attr["x"] );
161         m_yOffset += atoi( attr["y"] );
162         m_xOffsetList.push_back( atoi( attr["x"] ) );
163         m_yOffsetList.push_back( atoi( attr["y"] ) );
164     }
165
166     else if( rName == "Image" )
167     {
168         RequireDefault( "image" );
169         CheckDefault( "id", "none" );
170         CheckDefault( "visible", "true" );
171         CheckDefault( "x", "0" );
172         CheckDefault( "y", "0" );
173         CheckDefault( "lefttop", "lefttop" );
174         CheckDefault( "rightbottom", "lefttop" );
175         CheckDefault( "action", "none" );
176         CheckDefault( "help", "" );
177
178         const BuilderData::Image imageData( uniqueId( attr["id"] ),
179                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
180                 attr["lefttop"], attr["rightbottom"], attr["visible"],
181                 attr["image"], attr["action"], attr["help"], m_curLayer,
182                 m_curWindowId, m_curLayoutId );
183         m_curLayer++;
184         m_data.m_listImage.push_back( imageData );
185     }
186
187     else if( rName == "Layout" )
188     {
189         RequireDefault( "width" );
190         RequireDefault( "height" );
191         CheckDefault( "id", "none" );
192         CheckDefault( "minwidth", "-1" );
193         CheckDefault( "maxwidth", "-1" );
194         CheckDefault( "minheight", "-1" );
195         CheckDefault( "maxheight", "-1" );
196
197         m_curLayoutId = uniqueId( attr["id"] );
198         const BuilderData::Layout layout( m_curLayoutId, atoi( attr["width"] ),
199                 atoi( attr["height"] ), atoi( attr["minwidth"] ),
200                 atoi( attr["maxwidth"] ), atoi( attr["minheight"] ),
201                 atoi( attr["maxheight"] ), m_curWindowId );
202         m_data.m_listLayout.push_back( layout );
203         m_curLayer = 0;
204     }
205
206     else if( rName == "Playlist" )
207     {
208         RequireDefault( "id" );
209         RequireDefault( "font" );
210         CheckDefault( "visible", "true" );
211         CheckDefault( "x", "0" );
212         CheckDefault( "y", "0" );
213         CheckDefault( "width", "0" );
214         CheckDefault( "height", "0" );
215         CheckDefault( "lefttop", "lefttop" );
216         CheckDefault( "rightbottom", "lefttop" );
217         CheckDefault( "fgcolor", "#000000" );
218         CheckDefault( "playcolor", "#FF0000" );
219         CheckDefault( "bgcolor1", "#FFFFFF" );
220         CheckDefault( "bgcolor2", "#FFFFFF" );
221         CheckDefault( "selcolor", "#0000FF" );
222         CheckDefault( "help", "" );
223
224         m_curListId = uniqueId( attr["id"] );
225         const BuilderData::List listData( m_curListId, atoi( attr["x"] ) +
226                 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
227                 atoi( attr["width"]), atoi( attr["height"] ),
228                 attr["lefttop"], attr["rightbottom"],
229                 attr["font"], "playlist", convertColor( attr["fgcolor"] ),
230                 convertColor( attr["playcolor"] ),
231                 convertColor( attr["bgcolor1"] ),
232                 convertColor( attr["bgcolor2"] ),
233                 convertColor( attr["selcolor"] ), attr["help"],
234                 m_curLayer, m_curWindowId, m_curLayoutId );
235         m_curLayer++;
236         m_data.m_listList.push_back( listData );
237     }
238
239     else if( rName == "RadialSlider" )
240     {
241         RequireDefault( "sequence" );
242         RequireDefault( "nbimages" );
243         CheckDefault( "id", "none" );
244         CheckDefault( "visible", "true" );
245         CheckDefault( "x", "0" );
246         CheckDefault( "y", "0" );
247         CheckDefault( "lefttop", "lefttop" );
248         CheckDefault( "rightbottom", "lefttop" );
249         CheckDefault( "minangle", "0" );
250         CheckDefault( "maxangle", "360" );
251         CheckDefault( "value", "none" );
252         CheckDefault( "tooltiptext", "" );
253         CheckDefault( "help", "" );
254
255         const BuilderData::RadialSlider radial( uniqueId( attr["id"] ),
256                 attr["visible"],
257                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
258                 attr["lefttop"], attr["rightbottom"], attr["sequence"],
259                 atoi( attr["nbImages"] ), atof( attr["minAngle"] ) * M_PI /180,
260                 atof( attr["maxAngle"] ) * M_PI / 180, attr["value"],
261                 attr["tooltiptext"], attr["help"], m_curLayer, m_curWindowId,
262                 m_curLayoutId );
263         m_curLayer++;
264         m_data.m_listRadialSlider.push_back( radial );
265     }
266
267     else if( rName == "Slider" )
268     {
269         RequireDefault( "up" );
270         RequireDefault( "points" );
271         CheckDefault( "id", "none" );
272         CheckDefault( "visible", "true" );
273         CheckDefault( "x", "0" );
274         CheckDefault( "y", "0" );
275         CheckDefault( "width", "0" );
276         CheckDefault( "height", "0" );
277         CheckDefault( "lefttop", "lefttop" );
278         CheckDefault( "rightbottom", "lefttop" );
279         CheckDefault( "down", "none" );
280         CheckDefault( "over", "none" );
281         CheckDefault( "thickness", "10" );
282         CheckDefault( "value", "none" );
283         CheckDefault( "tooltiptext", "" );
284         CheckDefault( "help", "" );
285
286         string newValue = attr["value"];
287         if( m_curListId != "" )
288         {
289             // Slider associated to a list
290             newValue = "playlist.slider";
291         }
292         const BuilderData::Slider slider( uniqueId( attr["id"] ),
293                 attr["visible"],
294                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
295                 attr["lefttop"], attr["rightbottom"], attr["up"], attr["down"],
296                 attr["over"], attr["points"], atoi( attr["thickness"] ),
297                 newValue, attr["tooltiptext"], attr["help"], m_curLayer,
298                 m_curWindowId, m_curLayoutId );
299         m_curLayer++;
300         m_data.m_listSlider.push_back( slider );
301     }
302
303     else if( rName == "Text" )
304     {
305         RequireDefault( "font" );
306         CheckDefault( "id", "none" );
307         CheckDefault( "visible", "true" );
308         CheckDefault( "x", "0" );
309         CheckDefault( "y", "0" );
310         CheckDefault( "text", "" );
311         CheckDefault( "color", "#000000" );
312         CheckDefault( "width", "0" );
313         CheckDefault( "lefttop", "lefttop" );
314         CheckDefault( "rightbottom", "lefttop" );
315         CheckDefault( "help", "" );
316
317         const BuilderData::Text textData( uniqueId( attr["id"] ),
318                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
319                 attr["visible"], attr["font"],
320                 attr["text"], atoi( attr["width"] ),
321                 attr["lefttop"], attr["rightbottom"],
322                 convertColor( attr["color"] ), attr["help"], m_curLayer,
323                 m_curWindowId, m_curLayoutId );
324         m_curLayer++;
325         m_data.m_listText.push_back( textData );
326     }
327
328     else if( rName == "Theme" )
329     {
330         RequireDefault( "version" );
331         CheckDefault( "tooltipfont", "defaultfont" );
332         CheckDefault( "magnet", "15" );
333         CheckDefault( "alpha", "255" );
334         CheckDefault( "movealpha", "255" );
335
336         // Check the version
337         if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
338         {
339             msg_Err( getIntf(), "Bad theme version : %s (you need version %s)",
340                      attr["version"], SKINS_DTD_VERSION );
341             m_errors = true;
342             return;
343         }
344         const BuilderData::Theme theme( attr["tooltipfont"],
345                 atoi( attr["magnet"] ),
346                 convertInRange( attr["alpha"], 1, 255, "alpha" ),
347                 convertInRange( attr["movealpha"], 1, 255, "movealpha" ) );
348         m_data.m_listTheme.push_back( theme );
349     }
350
351     else if( rName == "ThemeInfo" )
352     {
353         msg_Info( getIntf(), "skin: %s  author: %s", attr["name"],
354                   attr["author"] );
355     }
356
357     else if( rName == "Video" )
358     {
359         CheckDefault( "id", "none" );
360         CheckDefault( "visible", "true" );
361         CheckDefault( "x", "0" );
362         CheckDefault( "y", "0" );
363         CheckDefault( "width", "0" );
364         CheckDefault( "height", "0" );
365         CheckDefault( "lefttop", "lefttop" );
366         CheckDefault( "rightbottom", "lefttop" );
367         CheckDefault( "help", "" );
368
369         const BuilderData::Video videoData( uniqueId( attr["id"] ),
370                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
371                 atoi( attr["width"] ), atoi( attr["height" ]),
372                 attr["lefttop"], attr["rightbottom"],
373                 attr["visible"], attr["help"], m_curLayer,
374                 m_curWindowId, m_curLayoutId );
375         m_curLayer++;
376         m_data.m_listVideo.push_back( videoData );
377     }
378
379     else if( rName == "Window" )
380     {
381         CheckDefault( "id", "none" );
382         CheckDefault( "visible", "true" );
383         CheckDefault( "x", "0" );
384         CheckDefault( "y", "0" );
385         CheckDefault( "dragdrop", "true" );
386         CheckDefault( "playondrop", "true" );
387
388         m_curWindowId = uniqueId( attr["id"] );
389         const BuilderData::Window window( m_curWindowId,
390                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
391                 convertBoolean( attr["visible"] ),
392                 convertBoolean( attr["dragdrop"] ),
393                 convertBoolean( attr["playondrop"] ) );
394         m_data.m_listWindow.push_back( window );
395     }
396 }
397
398
399 void SkinParser::handleEndElement( const string &rName )
400 {
401     if( rName == "Group" )
402     {
403         m_xOffset -= m_xOffsetList.back();
404         m_yOffset -= m_yOffsetList.back();
405         m_xOffsetList.pop_back();
406         m_yOffsetList.pop_back();
407     }
408
409     else if( rName == "Playlist" )
410     {
411         m_curListId = "";
412     }
413 }
414
415
416 bool SkinParser::convertBoolean( const char *value ) const
417 {
418     return strcmp( value, "true" ) == 0;
419 }
420
421
422 int SkinParser::convertColor( const char *transcolor ) const
423 {
424     unsigned long iRed, iGreen, iBlue;
425     iRed = iGreen = iBlue = 0;
426     sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
427     return ( iRed << 16 | iGreen << 8 | iBlue );
428 }
429
430
431 string SkinParser::convertFileName( const char *fileName ) const
432 {
433     return m_path + string( fileName );
434 }
435
436
437 int SkinParser::convertInRange( const char *value, int minValue, int maxValue,
438                                 const string &rAttribute ) const
439 {
440     int intValue = atoi( value );
441
442     if( intValue < minValue )
443     {
444         msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
445                   "expected range [%i, %i], using %i instead",
446                   rAttribute.c_str(), intValue, minValue, maxValue, minValue );
447         return minValue;
448     }
449     else if( intValue > maxValue )
450     {
451         msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
452                   "expected range [%i, %i], using %i instead",
453                   rAttribute.c_str(), intValue, minValue, maxValue, maxValue );
454         return maxValue;
455     }
456     else
457     {
458         return intValue;
459     }
460 }
461
462
463 const string SkinParser::generateId() const
464 {
465     static int i = 1;
466
467     char genId[5];
468     snprintf( genId, 4, "%i", i++ );
469
470     string base = "_ReservedId_" + (string)genId;
471
472     return base;
473 }
474
475
476 const string SkinParser::uniqueId( const string &id )
477 {
478     string newId;
479
480     if( m_idSet.find( id ) != m_idSet.end() )
481     {
482         // The id was already used
483         if( id != "none" )
484         {
485             msg_Warn( getIntf(), "Non unique id: %s", id.c_str() );
486         }
487         newId = generateId();
488     }
489     else
490     {
491         // OK, this is a new id
492         newId = id;
493     }
494
495     // Add the id to the set
496     m_idSet.insert( newId );
497
498     return newId;
499 }