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