summaryrefslogtreecommitdiffstats
path: root/tools/buildtheme.pl
blob: 171adf21764f495d608d86602804c3c194d9d0cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/usr/bin/perl
#             __________               __   ___.
#   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
#   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
#   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
#   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
#                     \/            \/     \/    \/            \/
# $Id: wpsbuild.pl 24813 2010-02-21 19:10:57Z kugel $
#

use strict;
use Getopt::Long qw(:config pass_through);	# pass_through so not confused by -DTYPE_STUFF

my $ROOT="..";
my $verbose;
my $rbdir=".rockbox";
my $wpslist;
my $target;
my $modelname;

# Get options
GetOptions ( 'r|root=s'		=> \$ROOT,
	     'm|modelname=s'	=> \$modelname,
	     'v|verbose'	=> \$verbose,
	     'rbdir=s'          => \$rbdir, # If we want to put in a different directory
    );

($wpslist, $target) = @ARGV;

my $firmdir="$ROOT/firmware";
my $cppdef = $target;
my @depthlist = ( 16, 8, 4, 2, 1 );


# LCD sizes
my ($main_height, $main_width, $main_depth);
my ($remote_height, $remote_width, $remote_depth);
my $has_remote;


if(!$wpslist) {
    print "Usage: buildtheme.pl <WPSLIST> <target>\n",
    "Run this script in the root of the target build, and it will put all the\n",
    "stuff in $rbdir/wps/\n";
    exit;
}

sub getlcdsizes
{
    my ($remote) = @_;

    open(GCC, ">gcctemp");
    if($remote) {
        # Get the remote LCD screen size
    print GCC <<STOP
\#include "config.h"
#ifdef HAVE_REMOTE_LCD
Height: LCD_REMOTE_HEIGHT
Width: LCD_REMOTE_WIDTH
Depth: LCD_REMOTE_DEPTH
#endif
STOP
;
    }
    else {
    print GCC <<STOP
\#include "config.h"
Height: LCD_HEIGHT
Width: LCD_WIDTH
Depth: LCD_DEPTH
STOP
;
}
    close(GCC);

    my $c="cat gcctemp | gcc $cppdef -I. -I$firmdir/export -E -P -";

    #print "CMD $c\n";

    open(GETSIZE, "$c|");

    my ($height, $width, $depth);
    while(<GETSIZE>) {
        if($_ =~ /^Height: (\d*)/) {
            $height = $1;
        }
        elsif($_ =~ /^Width: (\d*)/) {
            $width = $1;
        }
        elsif($_ =~ /^Depth: (\d*)/) {
            $depth = $1;
        }
        if($height && $width && $depth) {
            last;
        }
    }
    close(GETSIZE);
    unlink("gcctemp");

    return ($height, $width, $depth);
}

# Get the LCD sizes first
($main_height, $main_width, $main_depth) = getlcdsizes();
($remote_height, $remote_width, $remote_depth) = getlcdsizes(1);

#print "LCD: ${main_width}x${main_height}x${main_depth}\n";
$has_remote = 1 if ($remote_height && $remote_width && $remote_depth);

my $isrwps;
my $within;

my %theme;


sub match {
    my ($string, $pattern)=@_;

    $pattern =~ s/\*/.*/g;
    $pattern =~ s/\?/./g;

    return ($string =~ /^$pattern\z/);
}

sub matchdisplaystring {
    my ($string)=@_;
    return ($string =~ /${main_width}x${main_height}x${main_depth}/i) || 
            ($string =~ /r${remote_width}x${remote_height}x${remote_depth}/i);
}

sub mkdirs
{
    my ($themename) = @_;
    mkdir "$rbdir", 0777;
    mkdir "$rbdir/wps", 0777;
    mkdir "$rbdir/themes", 0777;
    mkdir "$rbdir/icons", 0777;
    mkdir "$rbdir/backdrops", 0777;

    if( -d "$rbdir/wps/$themename") {
  #      print STDERR "wpsbuild warning: directory wps/$themename already exists!\n";
    }
    else
    {
       mkdir "$rbdir/wps/$themename", 0777;
    }
}

sub buildcfg {
    my ($themename) = @_;
    my @out;    

    push @out, <<MOO
\#
\# generated by buildtheme.pl
\# $themename is made by $theme{"Author"}
\#
MOO
;
    my %configs = (
                "Name" => "# Theme Name",
                "WPS" => "wps", "RWPS" => "rwps",
                "FMS" => "fms", "RFMS" => "rfms",
                "SBS" => "sbs", "RSBS" => "rsbs",
                "Font" => "font", "Remote Font" => "remote font",
                "Statusbar" => "statusbar", "Remote Statusbar" => "remote statusbar",
                "selector type" => "selector type", "Remote Selector Type" => "remote selector type",  
                "backdrop" => "backdrop", "iconset" => "iconset", "viewers iconset" => "viewers iconset",
                "remote iconset" => "remote iconset", "remote viewers iconset" => "remote viewers iconset",
                "Foreground Color" => "foreground color", "Background Color" => "background color", 
                "backdrop" => "backdrop"
                );
                
    while( my ($k, $v) = each %configs )
    {
        if ($k =~ "Name")
        {
            # do nothing
        }
        elsif ($k =~ /WPS|RWPS|FMS|RFMS|SBS|RSBS/ && exists($theme{$k}))
        {
            push (@out, "$v: $themename.$v\n");
        }
        elsif ($k =~ /backdrop/ )
        {
            if (exists($theme{$k}))
            {
                my $dst = $theme{$k};
                $dst =~ s/(\.[0-9]*x[0-9]*x[0-9]*)//;
                push (@out, "$v: $theme{$k}\n");
            }
            else
            {
                push (@out, "$v: -\n");
            }
        }
        elsif (exists($theme{$k}))
        {
            push (@out, "$v: $theme{$k}\n");
        }
        else
        {
            push (@out, "$v: -\n");
        }
    }

  #  if(-f "$rbdir/themes/$themename.cfg") {
  #      print STDERR "wpsbuild warning: $themename.cfg already exists!\n";
  #  }
  #  else {
        open(CFG, ">$rbdir/themes/$themename.cfg");
        print CFG @out;
        close(CFG);
  #  }
}


sub copythemefont
{
    my ($font) = @_;
    #copy the font specified by the theme

    my $o=$font;
    $o =~ s/\.fnt/\.bdf/;
    mkdir "$rbdir/fonts";
    my $cmd ="$ROOT/tools/convbdf -f -o \"$rbdir/fonts/$font\" \"$ROOT/fonts/$o\" ";
    # print "$cmd\n";
    `$cmd`;
}

sub copyiconset
{
    my ($iconset) = @_;
    #copy the icon specified by the theme

    if ($iconset ne '') {
        $iconset =~ s/.rockbox/$rbdir/;
        $iconset =~ /\/(.*icons\/(.*))/i;
        `cp $ROOT/icons/$2 $1`;
    }
}

sub copybackdrop
{
    my ($backdrop) = @_;
    #copy the backdrop file into the build dir
    if ($backdrop ne '') 
    {
        my $dst = $backdrop;
        $dst =~ s/(\.[0-9]*x[0-9]*x[0-9]*)//;
        my $cmd = "cp $ROOT/$backdrop $rbdir/$dst";
        # print "$cmd\n";
        `$cmd`;
    }
}


sub copyskin
{
    my ($themename, $skin, $ext) = @_;
    # we assume that we copy the WPS files from the same dir the WPSLIST
    # file is located in
    my $dir;
    my @filelist;
    my $src;
    my $dest;
    my $sizestring;
        
    if($wpslist =~ /(.*)WPSFILE/) {
        $dir = $1;
        
        # first try the actual filename given to us
        # then $skin.widthxheightxdepths.ext
        # then $skin.ext
        $src = "${dir}$skin.$ext";
        if ( -e $src )
        {
            if ($skin =~ /\w\.(\d*x\d*x\d*).*/)
            {
                $sizestring = $1;
            }
            my $cmd = "cp $src $rbdir/wps/$themename.$ext";
            `$cmd`;
        }
        else
        {
            my $is_remote = ($ext =~ /^r.../i);
            my $width = $is_remote ? $remote_width : $main_width;
            my $height = $is_remote ? $remote_height : $main_height;
            my $depth = $is_remote ? $remote_depth : $main_depth;
            
            foreach my $d (@depthlist)
            {
                next if ($d > $depth);
                $sizestring = "${width}x${height}x${d}";
                $src = "${dir}$skin.${sizestring}.$ext";
                last if (-e $src);
            }
            if (-e $src)
            {
                my $cmd = "cp $src $rbdir/wps/$themename.$ext";
                `$cmd`;
            }
            elsif (-e "${dir}$skin.$ext")
            {
                my $cmd = "cp ${dir}$skin.$ext $rbdir/wps/$themename.$ext";
                `$cmd`;
            }
            else
            {
                #print STDERR "buildtheme warning: No suitable skin file for $ext\n";
                return;
            }
        }
        
        open(WPSFILE, "$rbdir/wps/$themename.$ext");
        while (<WPSFILE>) {
           $filelist[$#filelist + 1] = $1 if (/\|([^|]*?.bmp)\|/);
        }
        close(WPSFILE);
        if ($#filelist >= 0)
        {
            my $file;
            if ($sizestring && -e "$dir/$themename/$sizestring") 
            {
                foreach $file (@filelist) 
                {
                    system("cp $dir/$themename/$sizestring/$file $rbdir/wps/$themename/");
                }
            }
            elsif (-e "$dir/$themename") 
            {
                foreach $file (@filelist) 
                {
                    system("cp $dir/$themename/$file $rbdir/wps/$themename/");
                }
            }
            else
            {
                print STDERR "beep, no dir to copy WPS from!\n";
            }
        }
    }
}

open(WPS, "<$wpslist");
while(<WPS>) {
    my $l = $_;
    
    # remove CR
    $l =~ s/\r//g;
    if($l =~ /^ *\#/) {
        # skip comment
        next;
    }
    if($l =~ /^ *<(r|)wps>/i) {
        $isrwps = $1;
        $within = 1;
        undef %theme;
        next;
    }
    if($within) {
        if($l =~ /^ *<\/${isrwps}wps>/i) {
            #get the skin directory
            $wpslist =~ /(.*)WPSLIST/;
            my $wpsdir = $1;
            $within = 0;
            
            next if (!exists($theme{Name}));
            mkdirs($theme{Name});
            buildcfg($theme{Name});
            
            copyskin($theme{"Name"}, $theme{"WPS"}, "wps") if exists($theme{"WPS"});
            copyskin($theme{"Name"}, $theme{"RWPS"}, "rwps") if exists($theme{"RWPS"});
            copyskin($theme{"Name"}, $theme{"FMS"}, "fms") if exists($theme{"FMS"});
            copyskin($theme{"Name"}, $theme{"RFMS"}, "rfms") if exists($theme{"RFMS"});
            copyskin($theme{"Name"}, $theme{"SBS"}, "sbs") if exists($theme{"SBS"});
            copyskin($theme{"Name"}, $theme{"RSBS"}, "rsbs") if exists($theme{"RSBS"});
            
            copyiconset($theme{"iconset"}) if exists($theme{"iconset"});
            copyiconset($theme{"remote iconset"}) if exists($theme{"remote iconset"});
            copyiconset($theme{"viewers iconset"}) if exists($theme{"viewers iconset"});
            copyiconset($theme{"remote viewers iconset"}) if exists($theme{"remote viewers iconset"});
            
            copythemefont($theme{"Font"}) if exists($theme{"Font"});
            copythemefont($theme{"Remote Font"}) if exists($theme{"Remote Font"});
            
            copybackdrop($theme{"backdrop"}) if exists($theme{"backdrop"});
            
            
            
        }
        elsif($l =~ /^([\w ]*)\.?(.*):\s*(.*)/) {
            my $var = $1;
            my $extra = $2;
            my $value = $3;
            if (!exists($theme{$var}))
            {
                if (!$extra || 
                    ($extra && (match($target, $extra) || matchdisplaystring($extra))))
                {
                    $theme{$var} = $value;
                    #print "\'$var\': $value\n";
                }
            }
        }
        else{
            #print "Unknown line:  $l!\n";
        }
    }
}

close(WPS);