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