]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/skin_parser.cpp
815902ba3da497447a4be11d67fe7d8fe80d55fd
[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( 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( "tooltiptext", "" );
367         CheckDefault( "help", "" );
368
369         string newValue = attr["value"];
370         if( m_curListId != "" )
371         {
372             // Slider associated to a list
373             newValue = "playlist.slider";
374         }
375         else if( m_curTreeId != "" )
376         {
377             // Slider associated to a tree
378             newValue = "playtree.slider";
379         }
380         const BuilderData::Slider slider( uniqueId( attr["id"] ),
381                 attr["visible"], atoi( attr["x"] ) + m_xOffset,
382                 atoi( attr["y"] ) + m_yOffset, attr["lefttop"],
383                 attr["rightbottom"], attr["up"], attr["down"],
384                 attr["over"], attr["points"], atoi( attr["thickness"] ),
385                 newValue, "none", 0, 0, 0, 0, attr["tooltiptext"],
386                 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
387         m_curLayer++;
388         m_pData->m_listSlider.push_back( slider );
389     }
390
391     else if( rName == "SliderBackground" )
392     {
393         RequireDefault( "image" );
394         CheckDefault( "nbhoriz", "1" );
395         CheckDefault( "nbvert", "1" );
396         CheckDefault( "padhoriz", "0" );
397         CheckDefault( "padvert", "0" );
398
399         // Retrieve the current slider data
400         BuilderData::Slider &slider = m_pData->m_listSlider.back();
401
402         slider.m_imageId = attr["image"];
403         slider.m_nbHoriz = atoi( attr["nbhoriz"] );
404         slider.m_nbVert = atoi( attr["nbvert"] );
405         slider.m_padHoriz = atoi( attr["padhoriz"] );
406         slider.m_padVert = atoi( attr["padvert"] );
407     }
408
409     else if( rName == "Text" )
410     {
411         RequireDefault( "font" );
412         CheckDefault( "id", "none" );
413         CheckDefault( "visible", "true" );
414         CheckDefault( "x", "0" );
415         CheckDefault( "y", "0" );
416         CheckDefault( "text", "" );
417         CheckDefault( "color", "#000000" );
418         CheckDefault( "width", "0" );
419         CheckDefault( "lefttop", "lefttop" );
420         CheckDefault( "rightbottom", "lefttop" );
421         CheckDefault( "help", "" );
422
423         const BuilderData::Text textData( uniqueId( attr["id"] ),
424                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
425                 attr["visible"], attr["font"],
426                 attr["text"], atoi( attr["width"] ),
427                 attr["lefttop"], attr["rightbottom"],
428                 convertColor( attr["color"] ), attr["help"], m_curLayer,
429                 m_curWindowId, m_curLayoutId );
430         m_curLayer++;
431         m_pData->m_listText.push_back( textData );
432     }
433
434     else if( rName == "Theme" )
435     {
436         RequireDefault( "version" );
437         CheckDefault( "tooltipfont", "defaultfont" );
438         CheckDefault( "magnet", "15" );
439         CheckDefault( "alpha", "255" );
440         CheckDefault( "movealpha", "255" );
441
442         // Check the version
443         if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
444         {
445             msg_Err( getIntf(), "Bad theme version : %s (you need version %s)",
446                      attr["version"], SKINS_DTD_VERSION );
447             m_errors = true;
448             return;
449         }
450         const BuilderData::Theme theme( attr["tooltipfont"],
451                 atoi( attr["magnet"] ),
452                 convertInRange( attr["alpha"], 1, 255, "alpha" ),
453                 convertInRange( attr["movealpha"], 1, 255, "movealpha" ) );
454         m_pData->m_listTheme.push_back( theme );
455     }
456
457     else if( rName == "ThemeInfo" )
458     {
459         msg_Info( getIntf(), "skin: %s  author: %s", attr["name"],
460                   attr["author"] );
461     }
462
463     else if( rName == "Video" )
464     {
465         CheckDefault( "id", "none" );
466         CheckDefault( "visible", "true" );
467         CheckDefault( "x", "0" );
468         CheckDefault( "y", "0" );
469         CheckDefault( "width", "0" );
470         CheckDefault( "height", "0" );
471         CheckDefault( "lefttop", "lefttop" );
472         CheckDefault( "rightbottom", "lefttop" );
473         CheckDefault( "autoresize", "false" );
474         CheckDefault( "help", "" );
475
476         const BuilderData::Video videoData( uniqueId( attr["id"] ),
477                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
478                 atoi( attr["width"] ), atoi( attr["height" ]),
479                 attr["lefttop"], attr["rightbottom"],
480                 attr["visible"], convertBoolean( attr["autoresize"] ),
481                 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
482         m_curLayer++;
483         m_pData->m_listVideo.push_back( videoData );
484     }
485
486     else if( rName == "Window" )
487     {
488         CheckDefault( "id", "none" );
489         CheckDefault( "visible", "true" );
490         CheckDefault( "x", "0" );
491         CheckDefault( "y", "0" );
492         CheckDefault( "dragdrop", "true" );
493         CheckDefault( "playondrop", "true" );
494
495         m_curWindowId = uniqueId( attr["id"] );
496         const BuilderData::Window window( m_curWindowId,
497                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
498                 convertBoolean( attr["visible"] ),
499                 convertBoolean( attr["dragdrop"] ),
500                 convertBoolean( attr["playondrop"] ) );
501         m_pData->m_listWindow.push_back( window );
502     }
503 }
504
505
506 void SkinParser::handleEndElement( const string &rName )
507 {
508     if( rName == "Group" )
509     {
510         m_xOffset -= m_xOffsetList.back();
511         m_yOffset -= m_yOffsetList.back();
512         m_xOffsetList.pop_back();
513         m_yOffsetList.pop_back();
514     }
515     else if( rName == "Playlist" )
516     {
517         m_curListId = "";
518     }
519     else if( rName == "Playtree" )
520     {
521         m_curTreeId = "";
522     }
523 }
524
525
526 bool SkinParser::convertBoolean( const char *value ) const
527 {
528     return strcmp( value, "true" ) == 0;
529 }
530
531
532 int SkinParser::convertColor( const char *transcolor ) const
533 {
534     unsigned long iRed, iGreen, iBlue;
535     iRed = iGreen = iBlue = 0;
536     sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
537     return ( iRed << 16 | iGreen << 8 | iBlue );
538 }
539
540
541 int SkinParser::convertInRange( const char *value, int minValue, int maxValue,
542                                 const string &rAttribute ) const
543 {
544     int intValue = atoi( value );
545
546     if( intValue < minValue )
547     {
548         msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
549                   "expected range [%i, %i], using %i instead",
550                   rAttribute.c_str(), intValue, minValue, maxValue, minValue );
551         return minValue;
552     }
553     else if( intValue > maxValue )
554     {
555         msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
556                   "expected range [%i, %i], using %i instead",
557                   rAttribute.c_str(), intValue, minValue, maxValue, maxValue );
558         return maxValue;
559     }
560     else
561     {
562         return intValue;
563     }
564 }
565
566
567 const string SkinParser::generateId() const
568 {
569     static int i = 1;
570
571     char genId[5];
572     snprintf( genId, 4, "%i", i++ );
573
574     string base = "_ReservedId_" + (string)genId;
575
576     return base;
577 }
578
579
580 const string SkinParser::uniqueId( const string &id )
581 {
582     string newId;
583
584     if( m_idSet.find( id ) != m_idSet.end() )
585     {
586         // The id was already used
587         if( id != "none" )
588         {
589             msg_Warn( getIntf(), "Non unique id: %s", id.c_str() );
590         }
591         newId = generateId();
592     }
593     else
594     {
595         // OK, this is a new id
596         newId = id;
597     }
598
599     // Add the id to the set
600     m_idSet.insert( newId );
601
602     return newId;
603 }