summaryrefslogtreecommitdiffstats
path: root/wps/wpsbuild.pl
blob: 548b46974fbe750fc989afda1b44daedc6706b1d (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
#!/usr/bin/perl
#             __________               __   ___.
#   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
#   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
#   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
#   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
#                     \/            \/     \/    \/            \/
# $Id$
#

$ROOT="..";

if($ARGV[0] eq "-r") {
    $ROOT=$ARGV[1];
    shift @ARGV;
    shift @ARGV;    
}

my $verbose;
if($ARGV[0] eq "-v") {
    $verbose =1;
    shift @ARGV;
}

my $firmdir="$ROOT/firmware";

my $wpslist=$ARGV[0];

my $target = $ARGV[1];
my $cppdef = $target;


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 .rockbox/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
#endif
STOP
;
    }
    else {
    print GCC <<STOP
\#include "config.h"
#ifdef HAVE_LCD_BITMAP
Height: LCD_HEIGHT
Width: LCD_WIDTH
#endif
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);
    while(<GETSIZE>) {
        if($_ =~ /^Height: (\d*)/) {
            $height = $1;
        }
        elsif($_ =~ /^Width: (\d*)/) {
            $width = $1;
        }
        if($height && $width) {
            last;
        }
    }
    close(GETSIZE);
    unlink("gcctemp");

    return ($height, $width);
}

sub mkdirs {        
    my $wpsdir = $wps;
    $wpsdir =~ s/\.(r|)wps//;
    mkdir ".rockbox/wps", 0777;
    mkdir ".rockbox/themes", 0777;

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

sub copywps {
    # we assume that we copy the WPS files from the same dir the WPSLIST
    # file is located in
    my $dir;

    if($wpslist =~ /(.*)WPSLIST/) {
        $dir = $1;
        my $wpsdir = $wps;
        $wpsdir =~ s/\.(r|)wps//;
        system("cp $dir/$wps .rockbox/wps/");

        if (-e "$dir/$wpsdir") {
           system("cp $dir/$wpsdir/*.bmp .rockbox/wps/$wpsdir/");
        }
    }
    else {
        print STDERR "beep, no dir to copy WPS from!\n";
    }
}

sub buildcfg {
    my $cfg = $wps;
    my @out;

    $cfg =~ s/\.(r|)wps/.cfg/;

    push @out, <<MOO
\#
\# $cfg generated by wpsbuild.pl
\# $wps is made by $author
\#
wps: /.rockbox/wps/$wps
MOO
;
    if($font) {
        push @out, "font: /.rockbox/fonts/$font\n";
    }
    if($statusbar) {
        push @out, "statusbar: $statusbar\n";
    }
    if($rwps && $has_remote ) {
        push @out, "rwps: /.rockbox/wps/$rwps\n";
    }

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

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

$has_remote = true if ($remote_height && $remote_width);

open(WPS, "<$wpslist");
while(<WPS>) {
    my $l = $_;
    if($l =~ /^ *\#/) {
        # skip comment
        next;
    }
    if($l =~ /^ *<(r|)wps>/i) {
        $isrwps = $1;
        $within = 1;
        # undef is a unary operator (!)
        undef $wps;
        undef $rwps;
        undef $width; 
        undef $height;
        undef $font;
        undef $statusbar;
        undef $author;
        next;
    }
    if($within) {
        if($l =~ /^ *<\/${isrwps}wps>/i) {
            # Get the required width and height
            my ($rheight, $rwidth);
            if($isrwps) {
                ($rheight, $rwidth) = ($remote_height, $remote_width);
            }
            else {
                ($rheight, $rwidth) = ($main_height, $main_width);
            }

            if(!$rheight || !$rwidth) {
                #printf STDERR "wpsbuild notice: No %sLCD size, skipping $wps\n",
                $isrwps?"remote ":"";
                $within = 0;
                next;
            }

            #print "LCD: $wps wants $height x $width\n";
            #print "LCD: is $rheight x $rwidth\n";

            if(($height <= $rheight) && ($width <= $rwidth)) {
                #
                # The target model has an LCD that is suitable for this
                # WPS
                #
                #print "Size requirement is fine!\n";
                mkdirs();
                if(!$isrwps) {
                    # We only make .cfg files for <wps> sections:
                    buildcfg();
                }
                copywps();
            }
            else {
                #print "Skip $wps due to size restraints\n";
            }
            $within = 0;
        }
        elsif($l =~ /^Name: (.*)/i) {
            # Note that in the case this is within <rwps>, $wps will contain the
            # name of the rwps. Use $isrwps to figure out what type it is.
            $wps = $1;
        }
        elsif($l =~ /^RWPS: (.*)/i) {
            $rwps = $1;
        }
        elsif($l =~ /^Author: (.*)/i) {
            $author = $1;
        }
        elsif($l =~ /^Width: (.*)/i) {
            $width = $1;
        }
        elsif($l =~ /^Height: (.*)/i) {
            $height = $1;
        }
        elsif($l =~ /^Font: (.*)/i) {
            $font = $1;
        }
        elsif($l =~ /^Statusbar: (.*)/i) {
            $statusbar = $1;
        }
    }
}

close(WPS)