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