summaryrefslogtreecommitdiffstats
path: root/wps/wpsbuild.pl
blob: 52492f6b71d6fb923bc232cf52665ab07ae098c6 (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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
#!/usr/bin/perl
#             __________               __   ___.
#   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
#   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
#   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
#   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
#                     \/            \/     \/    \/            \/
# $Id$
#

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

my $ROOT="..";
my $wpsdir;
my $verbose;
my $rbdir=".rockbox";
my $tempdir=".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
         'tempdir=s'    => \$tempdir, # override .rockbox temporary dir
    );

($wpslist, $target) = @ARGV;

my $firmdir="$ROOT/firmware";
my $cppdef = $target;

# These parameters are filled in as we parse wpslist
my $req_t;
my $theme;
my $has_wps;
my $wps;
my $has_rwps;
my $rwps;
my $has_sbs;
my $sbs;
my $has_rsbs;
my $rsbs;
my $has_fms;
my $fms;
my $has_rfms;
my $rfms;
my $width;
my $height;
my $font;
my $remotefont;
my $fgcolor;
my $bgcolor;
my $sepcolor;
my $sep;
my $statusbar;
my $remotestatusbar;
my $author;
my $backdrop;
my $lineselectstart;
my $lineselectend;
my $selecttype;
my $iconset;
my $remoteiconset;
my $viewericon;
my $showicons;
my $remoteviewericon;
my $lineselecttextcolor;
my $filetylecolor;
my $listviewport;
my $remotelistviewport;

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


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

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

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

    my $cmd = "gcc $cppdef -I. -I$firmdir/export -E -P -";
    my $pid = open2(*COUT, *CIN, $cmd) or die "Could not spawn child: $!\n";

    print CIN $str;
    close(CIN);
    waitpid($pid, 0);

    my ($height, $width, $depth, $touch);
    while(<COUT>) {
        if($_ =~ /^Height: (\d*)/) {
            $height = $1;
        }
        elsif($_ =~ /^Width: (\d*)/) {
            $width = $1;
        }
        elsif($_ =~ /^Depth: (\d*)/) {
            $depth = $1;
        }
        if($_ =~ /^Touchscreen/) {
            $touch = 1;
        }
    }
    close(COUT);

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

sub mkdirs
{
    mkdir "$tempdir/wps", 0777;
    mkdir "$tempdir/themes", 0777;
    mkdir "$tempdir/icons", 0777;

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

sub normalize
{
    my $in = $_[0];
    # strip resolution
    $in =~ s/(\.[0-9]*x[0-9]*x[0-9]*)//;
    return $in;
}

sub copybackdrop
{
    #copy the backdrop file into the build dir
    if ($backdrop ne '') {
        my $dst = normalize($backdrop);
        system("cp $ROOT/$backdrop $tempdir/$dst");
    }
}

sub copythemefont
{
    #copy the font specified by the theme
    my $o = $_[0];

    $o =~ s/\.fnt/\.bdf/;
    mkdir "$tempdir/fonts";
    system("$ROOT/tools/convbdf -f -o \"$tempdir/fonts/$_[0]\" \"$ROOT/fonts/$o\" ");
}

sub copythemeicon
{
    my $i = $_[0];
    #copy the icon specified by the theme
    if ($i ne "-") {
        my $tempicon = $tempdir . "/" . $i;
        $tempicon =~ /\/.*icons\/(.*)/i;
        system("cp $ROOT/icons/$1 $tempicon");
    }
}

sub uniq {
    my %seen = ();
    my @r = ();
    foreach my $a (@_) {
        unless ($seen{$a}) {
            push @r, $a;
            $seen{$a} = 1;
        }
    }
    return @r;
}

sub copywps
{
    # we assume that we copy the WPS files from the same dir the WPSLIST
    # file is located in
    my $dir;
    my %skinfiles = ("wps", $wps,
                     "sbs", $sbs,
                     "fms", $fms,
                     "rwps", $rwps,
                     "rsbs", $rsbs,
                     "rfms", $rfms);
    my @filelist;
    my $file;

    if($wpslist =~ /(.*)\/WPSLIST/) {
        $dir = $1;

        # copy fully-fledged wps, sbs, etc. including graphics
        foreach my $ext (keys %skinfiles) {
            next unless ($skinfiles{$ext});
            $file = $skinfiles{$ext};
            system("cp $dir/$file $tempdir/wps/$theme.$ext");
            open(SKIN, "$dir/$file");
            while (<SKIN>) {
                $filelist[$#filelist + 1] = $1 if (/[\(,]([^,]*?.bmp)[\),]/);
            }
            close(SKIN);
        }

        if ($#filelist >= 0) {
            if (-e "$dir/$theme") {
                foreach $file (uniq(@filelist)) {
                    system("cp $dir/$theme/$file $tempdir/wps/$theme/");
                }
            }
            else {
                print STDERR "beep, no dir to copy WPS from!\n";
            }
        }
    } else {
        print STDERR "No source directory!\n";
    }
}

sub buildcfg {
    my $cfg = $theme . ".cfg";
    my @out;

    push @out, <<MOO
\#
\# $cfg generated by wpsbuild.pl
\# $wps is made by $author
\#
MOO
;

    my %skinfiles = ("wps" => $wps,
                     "sbs" => $sbs,
                     "fms" => $fms,
                     "rwps" => $rwps,
                     "rsbs" => $rsbs,
                     "rfms" => $rfms);
    for my $skin (keys %skinfiles) {
        my $val = $skinfiles{$skin};
        if (!defined($val)) {
            # dont put value if not defined (e.g. rwps when there's no remote)
            next;
        } elsif ($val eq '') {
            # empty resets to built-in
            push @out, "$skin: -\n";
        } else {
            # file name is always <theme>.{wps,rwps,...} (see copywps())
            push @out, "$skin: $rbdir/wps/$theme.$skin\n";
        }
    }

    push @out, "selector type: $selecttype\n"   if (defined($selecttype));
    push @out, "backdrop: $backdrop\n"          if (defined($backdrop));
    push @out, "filetype colours: $filetylecolor\n" if (defined($filetylecolor));

    if ($main_depth > 2) {
        push @out, "foreground color: $fgcolor\n"                     if($fgcolor);
        push @out, "background color: $bgcolor\n"                     if($bgcolor);
        push @out, "line selector start color: $lineselectstart\n"    if($lineselectstart);
        push @out, "line selector end color: $lineselectend\n"        if($lineselectend);;
        push @out, "line selector text color: $lineselecttextcolor\n" if($lineselecttextcolor);
        # list separator actually depends on HAVE_TOUCSCREEN
        push @out, "list separator height: $sep\n"                    if($sep);
        push @out, "list separator color: $sepcolor\n"                if($sepcolor);
    }

    push @out, "font: $font\n"                  if (defined($font));
    push @out, "statusbar: $statusbar\n"        if (defined($statusbar));
    push @out, "iconset: $iconset\n"            if (defined($iconset));
    push @out, "viewers iconset: $viewericon\n" if (defined($viewericon));
    push @out, "show icons: $showicons\n"       if (defined($viewericon) && defined($showicons));
    push @out, "ui viewport: $listviewport\n"   if (defined($listviewport));

    if ($has_remote) {
        push @out, "remote font: $remotefont\n"                  if (defined($remotefont));
        push @out, "remote statusbar: $remotestatusbar\n"        if (defined($remotestatusbar));
        push @out, "remote iconset: $remoteiconset\n"            if (defined($remoteiconset));
        push @out, "remote viewers iconset: $remoteviewericon\n" if (defined($remoteviewericon));
        push @out, "remote ui viewport: $remotelistviewport\n"   if (defined($remotelistviewport));
    }

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

# 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);


# check if line matches the setting string or if it contains a regex
# that contains the targets resolution
#
# the line can be of the from
# 1) setting: value (handled above)
# 2) setting.128x96x2: value
# 3) setting.128x96x2&touchscreen: value
# The resolution pattern can be a perl-comatible regex
sub check_res_feature {
    my ($line, $string, $remote) = @_;
    if ($line =~ /^$string: *(.*)/i) {
        return $1;
    }
    elsif($line =~ /^$string\.(.*): *(.*)/i) {
    # $1 is a resolution/feature regex, $2 the filename incl. resolution
        my $substr = $1;
        my $fn = $2;
        my $feature;
        my $size_str = "${main_width}x${main_height}x${main_depth}";
        if ($remote) {
            $size_str = "${remote_width}x${remote_height}x${remote_depth}";
        }

        # extract feature constraint, if any
        if ($substr =~ /&([a-z0-9_]+)/) {
            $feature = $1;
            $substr =~ s/&$feature//;
        }

        if ($size_str =~ /$substr$/) {
            if ($feature) {
                # check feature constrait
                open(FEAT, "<apps/features");
                chomp(my @lines = <FEAT>);
                close(FEAT);
                my $matches = (grep { /$feature/ } @lines);
                return $fn if $matches > 0;
            }
            else {
                # no feature constraint
                return $fn;
            }
        }
    }
    return "";
}

# check if <theme>.<model>.<ext> exists. If not, check if <theme>.<ext> exists
sub check_skinfile {
    my $ext = $_[0];
    my $req_skin = $theme . "." . $modelname . ".$ext";
    if (-e "$wpsdir/$req_skin") {
        return $req_skin;
    } else {
        $req_skin = $theme . ".$ext";
        if (-e "$wpsdir/$req_skin") {
            return $req_skin;
        }
    }
    return '';
}


# Infer WPS (etc.) filename from the the if it wasnt given
$wpslist =~ /(.*)WPSLIST/;
$wpsdir = $1;

open(WPS, "<$wpslist");
while(<WPS>) {
    my $l = $_;
    
    # remove CR
    $l =~ s/\r//g;
    if($l =~ /^ *\#/) {
        # skip comment
        next;
    }

    # prefix $rbdir with / if needed (needed for the theme.cfg)
    unless ($rbdir =~ m/^\/.*/) {
        $rbdir = "/" . $rbdir;
    }

    if($l =~ /^ *<theme>/i) {
        # undef is a unary operator (!)
        undef $theme;
        undef $has_wps;
        undef $has_rwps;
        undef $has_sbs;
        undef $has_rsbs;
        undef $has_fms;
        undef $has_rfms;
        undef $wps;
        undef $rwps;
        undef $sbs;
        undef $rsbs;
        undef $fms;
        undef $rfms;
        undef $width;
        undef $height;
        undef $font;
        undef $remotefont;
        undef $fgcolor;
        undef $bgcolor;
        undef $sepcolor;
        undef $sep;
        undef $statusbar;
        undef $remotestatusbar;
        undef $author;
        undef $backdrop;
        undef $lineselectstart;
        undef $lineselectend;
        undef $selecttype;
        undef $iconset;
        undef $remoteiconset;
        undef $viewericon;
        undef $showicons;
        undef $remoteviewericon;
        undef $lineselecttextcolor;
        undef $filetylecolor;
        undef $listviewport;
        undef $remotelistviewport;
    }
    elsif($l =~ /^Name: *(.*)/i) {
        $theme = $1;
    }
    elsif($l =~ /^Authors: *(.*)/i) {
        $author = $1;
    }
    elsif ($l =~ /^WPS: *(yes|no)/i) {
        $has_wps = $1;
    }
    elsif ($l =~ /^RWPS: *(yes|no)/i) {
        $has_rwps = $1;
    }
    elsif ($l =~ /^SBS: *(yes|no)/i) {
        $has_sbs = $1;
    }
    elsif ($l =~ /^RSBS: *(yes|no)/i) {
        $has_rsbs = $1;
    }
    elsif ($l =~ /^FMS: *(yes|no)/i) {
        $has_fms = $1;
    }
    elsif ($l =~ /^RFMS: *(yes|no)/i) {
        $has_rfms = $1;
    }
    elsif($l =~ /^ *<main>/i) {
        # parse main unit settings
        while(<WPS>) {
            my $l = $_;
            if ($l =~ /^ *<\/main>/i) {
                last;
            }
            elsif($_ = check_res_feature($l, "wps")) {
                $wps = $_;
            }
            elsif($_ = check_res_feature($l, "sbs")) {
                $sbs = $_;
            }
            elsif($_ = check_res_feature($l, "fms")) {
                $fms = $_;
            }
            elsif($_ = check_res_feature($l, "Font")) {
                $font = $_;
            }
            elsif($_ = check_res_feature($l, "Statusbar")) {
                $statusbar = $_;
            }
            elsif($_ = check_res_feature($l, "Backdrop")) {
                $backdrop = $_;
            }
            elsif($l =~ /^Foreground Color: *(.*)/i) {
                $fgcolor = $1;
            }
            elsif($l =~ /^Background Color: *(.*)/i) {
                $bgcolor = $1;
            }
            elsif($_ = check_res_feature($l, "list separator color")) {
                $sepcolor = $_;
            }
            elsif($_ = check_res_feature($l, "list separator height")) {
                $sep = $_;
            }
            elsif($l =~ /^line selector start color: *(.*)/i) {
                $lineselectstart = $1;
            }
            elsif($l =~ /^line selector end color: *(.*)/i) {
                $lineselectend = $1;
            }
            elsif($_ = check_res_feature($l, "selector type")) {
                $selecttype = $_;
            }
            elsif($_ = check_res_feature($l, "iconset")) {
                $iconset = $_;
            }
            elsif($_ = check_res_feature($l, "viewers iconset")) {
                $viewericon = $_;
            }
            elsif($l =~ /^show icons: *(.*)/i) {
                $showicons = $1;
            }
            elsif($l =~ /^line selector text color: *(.*)/i) {
                $lineselecttextcolor = $1;
            }
            elsif($l =~ /^filetype colours: *(.*)/i) {
                $filetylecolor = $1;
            }
            elsif($_ = check_res_feature($l, "ui viewport")) {
                $listviewport = $_;
            }
        }
    }
    elsif($l =~ /^ *<remote>/i) {
        while(<WPS>) {
            # parse remote settings
            my $l = $_;
            if ($l =~ /^ *<\/remote>/i) {
                last;
            }
            elsif(!$has_remote) {
                next; # dont parse <remote> section
            }
            elsif($_ = check_res_feature($l, "rwps", 1)) {
                $rwps = $_;
            }
            elsif($_ = check_res_feature($l, "rsbs", 1)) {
                $rsbs = $_;
            }
            elsif($_ = check_res_feature($l, "rfms", 1)) {
                $rfms = $_;
            }
            elsif($_ = check_res_feature($l, "Font", 1)) {
                $remotefont = $_;
            }
            elsif($_ = check_res_feature($l, "iconset", 1)) {
                $remoteiconset = $_;
            }
            elsif($_ = check_res_feature($l, "viewers iconset", 1)) {
                $remoteviewericon = $_;
            }
            elsif($_ = check_res_feature($l, "statusbar", 1)) {
                $remotestatusbar = $_;
            }
            elsif($_ = check_res_feature($l, "ui viewport", 1)) {
                $remotelistviewport = $_;
            }
        }
    }
    elsif($l =~ /^ *<\/theme>/i) {
        # for each wps,sbs,fms (+ remote variants) check if <theme>[.<model>].wps
        # exists if no filename was specified in WPSLIST
        my $req_skin;

        if ($has_wps eq "yes" && !$wps) {
            $wps = check_skinfile("wps");
        } elsif ($has_wps eq "no") {
            $wps = '';
        }

        if ($has_sbs eq "yes" && !$sbs) {
            $sbs = check_skinfile("sbs");
        } elsif ($has_sbs eq "no") {
            $sbs = '';
        }

        if ($has_fms eq "yes" && !$fms) {
            $fms = check_skinfile("fms");
        } elsif ($has_fms eq "no") {
            $fms = '';
        }

        # now check for remote skin files (use main screen's extension)
        if ($has_remote) {
            if ($has_rwps eq "yes" && !$rwps) {
                $rwps = check_skinfile("wps");
            } elsif ($has_rwps eq "no") {
                $rwps = '';
            }

            if ($has_rsbs eq "yes" && !$rsbs) {
                $rsbs = check_skinfile("sbs");
            } elsif ($has_rsbs eq "no") {
                $rsbs = '';
            }

            if ($has_rfms eq "yes" && !$rfms) {
                $rfms = check_skinfile("fms");
            } elsif ($has_rfms eq "no") {
                $rfms = '';
            }
        }
        #print "LCD: $wps wants $width x $height\n";

        #
        # The target model has an LCD that is suitable for this
        # WPS
        #
        #print "Size requirement is fine!\n";
        mkdirs() if (-e "$wpsdir/$theme");
        # Do the copying before building the .cfg - buildcfg()
        # mangles some filenames
        if (defined($backdrop) && $backdrop ne "-") {
            copybackdrop();
            $backdrop = "$rbdir/" . normalize($backdrop);
        }
        foreach my $i ($iconset, $viewericon, $remoteiconset, $remoteviewericon) {
            if (defined($i) && $i ne "-") {
                copythemeicon($i);
                $i = "$rbdir/$i";
            }
        }
        foreach my $i ($font, $remotefont) {
            if (defined($i) && $i ne "-") {
                copythemefont($i);
                $i = "$rbdir/fonts/$i";
            }
        }
        buildcfg();
        copywps();
    }
    else{
        #print "Unknown line:  $l!\n";
    }
}

close(WPS);