Prepare new maemo release
[maemo-rb.git] / wps / wpsbuild.pl
bloba420faddd88c92cb1588ed2371ff83c7c63d3c75
1 #!/usr/bin/perl
2 # __________ __ ___.
3 # Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 # \/ \/ \/ \/ \/
8 # $Id$
11 use strict;
12 use Getopt::Long qw(:config pass_through); # pass_through so not confused by -DTYPE_STUFF
14 my $ROOT="..";
15 my $wpsdir;
16 my $verbose;
17 my $rbdir=".rockbox";
18 my $tempdir=".rockbox";
19 my $wpslist;
20 my $target;
21 my $modelname;
23 # Get options
24 GetOptions ( 'r|root=s' => \$ROOT,
25 'm|modelname=s' => \$modelname,
26 'v|verbose' => \$verbose,
27 'rbdir=s' => \$rbdir, # If we want to put in a different directory
28 'tempdir=s' => \$tempdir, # override .rockbox temporary dir
31 ($wpslist, $target) = @ARGV;
33 my $firmdir="$ROOT/firmware";
34 my $cppdef = $target;
36 # These parameters are filled in as we parse wpslist
37 my $req_t;
38 my $theme;
39 my $has_wps;
40 my $wps;
41 my $has_rwps;
42 my $rwps;
43 my $has_sbs;
44 my $sbs;
45 my $has_rsbs;
46 my $rsbs;
47 my $has_fms;
48 my $fms;
49 my $has_rfms;
50 my $rfms;
51 my $width;
52 my $height;
53 my $font;
54 my $remotefont;
55 my $fgcolor;
56 my $bgcolor;
57 my $statusbar;
58 my $remotestatusbar;
59 my $author;
60 my $backdrop;
61 my $lineselectstart;
62 my $lineselectend;
63 my $selecttype;
64 my $iconset;
65 my $remoteiconset;
66 my $viewericon;
67 my $showicons;
68 my $remoteviewericon;
69 my $lineselecttextcolor;
70 my $filetylecolor;
71 my $listviewport;
72 my $remotelistviewport;
74 # LCD sizes
75 my ($main_height, $main_width, $main_depth);
76 my ($remote_height, $remote_width, $remote_depth);
77 my $has_remote;
80 if(!$wpslist) {
81 print "Usage: wpsbuilds.pl <WPSLIST> <target>\n",
82 "Run this script in the root of the target build, and it will put all the\n",
83 "stuff in $tempdir/wps/\n and build the cfg according to $rbdir";
84 exit;
87 sub getlcdsizes
89 my ($remote) = @_;
91 open(GCC, ">gcctemp");
92 if($remote) {
93 # Get the remote LCD screen size
94 print GCC <<STOP
95 \#include "config.h"
96 #ifdef HAVE_REMOTE_LCD
97 Height: LCD_REMOTE_HEIGHT
98 Width: LCD_REMOTE_WIDTH
99 Depth: LCD_REMOTE_DEPTH
100 #endif
101 STOP
104 else {
105 print GCC <<STOP
106 \#include "config.h"
107 Height: LCD_HEIGHT
108 Width: LCD_WIDTH
109 Depth: LCD_DEPTH
110 STOP
113 close(GCC);
115 my $c="cat gcctemp | gcc $cppdef -I. -I$firmdir/export -E -P -";
117 #print "CMD $c\n";
119 open(GETSIZE, "$c|");
121 my ($height, $width, $depth);
122 while(<GETSIZE>) {
123 if($_ =~ /^Height: (\d*)/) {
124 $height = $1;
126 elsif($_ =~ /^Width: (\d*)/) {
127 $width = $1;
129 elsif($_ =~ /^Depth: (\d*)/) {
130 $depth = $1;
132 if($height && $width && $depth) {
133 last;
136 close(GETSIZE);
137 unlink("gcctemp");
139 return ($height, $width, $depth);
142 sub mkdirs
144 mkdir "$tempdir/wps", 0777;
145 mkdir "$tempdir/themes", 0777;
146 mkdir "$tempdir/icons", 0777;
148 if( -d "$tempdir/wps/$theme") {
149 #print STDERR "wpsbuild warning: directory wps/$theme already exists!\n";
151 else
153 mkdir "$tempdir/wps/$theme", 0777;
157 sub normalize
159 my $in = $_[0];
160 # strip resolution
161 $in =~ s/(\.[0-9]*x[0-9]*x[0-9]*)//;
162 return $in;
165 sub copybackdrop
167 #copy the backdrop file into the build dir
168 if ($backdrop ne '') {
169 my $dst = normalize($backdrop);
170 system("cp $ROOT/$backdrop $tempdir/$dst");
174 sub copythemefont
176 #copy the font specified by the theme
177 my $o = $_[0];
179 $o =~ s/\.fnt/\.bdf/;
180 mkdir "$tempdir/fonts";
181 system("$ROOT/tools/convbdf -f -o \"$tempdir/fonts/$_[0]\" \"$ROOT/fonts/$o\" ");
184 sub copythemeicon
186 my $i = $_[0];
187 #copy the icon specified by the theme
188 if ($i ne "-") {
189 my $tempicon = $tempdir . "/" . $i;
190 $tempicon =~ /\/.*icons\/(.*)/i;
191 system("cp $ROOT/icons/$1 $tempicon");
195 sub uniq {
196 my %seen = ();
197 my @r = ();
198 foreach my $a (@_) {
199 unless ($seen{$a}) {
200 push @r, $a;
201 $seen{$a} = 1;
204 return @r;
207 sub copywps
209 # we assume that we copy the WPS files from the same dir the WPSLIST
210 # file is located in
211 my $dir;
212 my %skinfiles = ("wps", $wps,
213 "sbs", $sbs,
214 "fms", $fms,
215 "rwps", $rwps,
216 "rsbs", $rsbs,
217 "rfms", $rfms);
218 my @filelist;
219 my $file;
221 if($wpslist =~ /(.*)\/WPSLIST/) {
222 $dir = $1;
224 # copy fully-fledged wps, sbs, etc. including graphics
225 foreach my $ext (keys %skinfiles) {
226 next unless ($skinfiles{$ext});
227 $file = $skinfiles{$ext};
228 system("cp $dir/$file $tempdir/wps/$theme.$ext");
229 open(SKIN, "$dir/$file");
230 while (<SKIN>) {
231 $filelist[$#filelist + 1] = $1 if (/[\(,]([^,]*?.bmp)[\),]/);
233 close(SKIN);
236 if ($#filelist >= 0) {
237 if (-e "$dir/$theme") {
238 foreach $file (uniq(@filelist)) {
239 system("cp $dir/$theme/$file $tempdir/wps/$theme/");
242 else {
243 print STDERR "beep, no dir to copy WPS from!\n";
246 } else {
247 print STDERR "No source directory!\n";
251 sub buildcfg {
252 my $cfg = $theme . ".cfg";
253 my @out;
255 push @out, <<MOO
257 \# $cfg generated by wpsbuild.pl
258 \# $wps is made by $author
263 my %skinfiles = ("wps" => $wps,
264 "sbs" => $sbs,
265 "fms" => $fms,
266 "rwps" => $rwps,
267 "rsbs" => $rsbs,
268 "rfms" => $rfms);
269 for my $skin (keys %skinfiles) {
270 my $val = $skinfiles{$skin};
271 if (!defined($val)) {
272 # dont put value if not defined (e.g. rwps when there's no remote)
273 next;
274 } elsif ($val eq '') {
275 # empty resets to built-in
276 push @out, "$skin: -\n";
277 } else {
278 # file name is always <theme>.{wps,rwps,...} (see copywps())
279 push @out, "$skin: $rbdir/wps/$theme.$skin\n";
283 push @out, "selector type: $selecttype\n" if (defined($selecttype));
284 push @out, "backdrop: $backdrop\n" if (defined($backdrop));
285 push @out, "filetype colours: $filetylecolor\n" if (defined($filetylecolor));
287 if ($main_depth > 2) {
288 push @out, "foreground color: $fgcolor\n" if($fgcolor);
289 push @out, "background color: $bgcolor\n" if($bgcolor);
290 push @out, "line selector start color: $lineselectstart\n" if($lineselectstart);
291 push @out, "line selector end color: $lineselectend\n" if($lineselectend);;
292 push @out, "line selector text color: $lineselecttextcolor\n" if($lineselecttextcolor);
295 push @out, "font: $font\n" if (defined($font));
296 push @out, "statusbar: $statusbar\n" if (defined($statusbar));
297 push @out, "iconset: $iconset\n" if (defined($iconset));
298 push @out, "viewers iconset: $viewericon\n" if (defined($viewericon));
299 push @out, "show icons: $showicons\n" if (defined($viewericon) && defined($showicons));
300 push @out, "ui viewport: $listviewport\n" if (defined($listviewport));
302 if ($has_remote) {
303 push @out, "remote font: $remotefont\n" if (defined($remotefont));
304 push @out, "remote statusbar: $remotestatusbar\n" if (defined($remotestatusbar));
305 push @out, "remote iconset: $remoteiconset\n" if (defined($remoteiconset));
306 push @out, "remote viewers iconset: $remoteviewericon\n" if (defined($remoteviewericon));
307 push @out, "remote ui viewport: $remotelistviewport\n" if (defined($remotelistviewport));
310 if(-f "$tempdir/wps/$cfg") {
311 print STDERR "wpsbuild warning: wps/$cfg already exists!\n";
313 else {
314 open(CFG, ">$tempdir/themes/$cfg");
315 print CFG @out;
316 close(CFG);
320 # Get the LCD sizes first
321 ($main_height, $main_width, $main_depth) = getlcdsizes();
322 ($remote_height, $remote_width, $remote_depth) = getlcdsizes(1);
324 #print "LCD: ${main_width}x${main_height}x${main_depth}\n";
325 $has_remote = 1 if ($remote_height && $remote_width && $remote_depth);
328 # check if line matches the setting string or if it contains a regex
329 # that contains the targets resolution
330 sub check_res {
331 my ($line, $string, $remote) = @_;
332 if ($line =~ /^${string}: *(.*)/i) {
333 return $1;
335 elsif($line =~ /^${string}.(.*): *(.*)/i) {
336 # $1 is a resolution regex, $2 the filename incl. resolution
337 my $fn = $2;
338 my $size_str = "${main_width}x${main_height}x${main_depth}";
339 if ($remote) {
340 $size_str = "${remote_width}x${remote_height}x${remote_depth}";
342 if ($size_str =~ /$1$/) {
343 return $fn;
346 return "";
349 # check if <theme>.<model>.<ext> exists. If not, check if <theme>.<ext> exists
350 sub check_skinfile {
351 my $ext = $_[0];
352 my $req_skin = $theme . "." . $modelname . ".$ext";
353 if (-e "$wpsdir/$req_skin") {
354 return $req_skin;
355 } else {
356 $req_skin = $theme . ".$ext";
357 if (-e "$wpsdir/$req_skin") {
358 return $req_skin;
361 return '';
365 # Infer WPS (etc.) filename from the the if it wasnt given
366 $wpslist =~ /(.*)WPSLIST/;
367 $wpsdir = $1;
369 open(WPS, "<$wpslist");
370 while(<WPS>) {
371 my $l = $_;
373 # remove CR
374 $l =~ s/\r//g;
375 if($l =~ /^ *\#/) {
376 # skip comment
377 next;
380 # prefix $rbdir with / if needed (needed for the theme.cfg)
381 unless ($rbdir =~ m/^\/.*/) {
382 $rbdir = "/" . $rbdir;
385 if($l =~ /^ *<theme>/i) {
386 # undef is a unary operator (!)
387 undef $theme;
388 undef $has_wps;
389 undef $has_rwps;
390 undef $has_sbs;
391 undef $has_rsbs;
392 undef $has_fms;
393 undef $has_rfms;
394 undef $wps;
395 undef $rwps;
396 undef $sbs;
397 undef $rsbs;
398 undef $fms;
399 undef $rfms;
400 undef $width;
401 undef $height;
402 undef $font;
403 undef $remotefont;
404 undef $fgcolor;
405 undef $bgcolor;
406 undef $statusbar;
407 undef $remotestatusbar;
408 undef $author;
409 undef $backdrop;
410 undef $lineselectstart;
411 undef $lineselectend;
412 undef $selecttype;
413 undef $iconset;
414 undef $remoteiconset;
415 undef $viewericon;
416 undef $showicons;
417 undef $remoteviewericon;
418 undef $lineselecttextcolor;
419 undef $filetylecolor;
420 undef $listviewport;
421 undef $remotelistviewport;
423 elsif($l =~ /^Name: *(.*)/i) {
424 $theme = $1;
426 elsif($l =~ /^Authors: *(.*)/i) {
427 $author = $1;
429 elsif ($l =~ /^WPS: *(yes|no)/i) {
430 $has_wps = $1;
432 elsif ($l =~ /^RWPS: *(yes|no)/i) {
433 $has_rwps = $1;
435 elsif ($l =~ /^SBS: *(yes|no)/i) {
436 $has_sbs = $1;
438 elsif ($l =~ /^RSBS: *(yes|no)/i) {
439 $has_rsbs = $1;
441 elsif ($l =~ /^FMS: *(yes|no)/i) {
442 $has_fms = $1;
444 elsif ($l =~ /^RFMS: *(yes|no)/i) {
445 $has_rfms = $1;
447 elsif($l =~ /^ *<main>/i) {
448 # parse main unit settings
449 while(<WPS>) {
450 my $l = $_;
451 if ($l =~ /^ *<\/main>/i) {
452 last;
454 elsif($_ = check_res($l, "wps")) {
455 $wps = $_;
457 elsif($_ = check_res($l, "sbs")) {
458 $sbs = $_;
460 elsif($_ = check_res($l, "fms")) {
461 $fms = $_;
463 elsif($_ = check_res($l, "Font")) {
464 $font = $_;
466 elsif($_ = check_res($l, "Statusbar")) {
467 $statusbar = $_;
469 elsif($_ = check_res($l, "Backdrop")) {
470 $backdrop = $_;
472 elsif($l =~ /^Foreground Color: *(.*)/i) {
473 $fgcolor = $1;
475 elsif($l =~ /^Background Color: *(.*)/i) {
476 $bgcolor = $1;
478 elsif($l =~ /^line selector start color: *(.*)/i) {
479 $lineselectstart = $1;
481 elsif($l =~ /^line selector end color: *(.*)/i) {
482 $lineselectend = $1;
484 elsif($_ = check_res($l, "selector type")) {
485 $selecttype = $_;
487 elsif($_ = check_res($l, "iconset")) {
488 $iconset = $_;
490 elsif($_ = check_res($l, "viewers iconset")) {
491 $viewericon = $_;
493 elsif($l =~ /^show icons: *(.*)/i) {
494 $showicons = $1;
496 elsif($l =~ /^line selector text color: *(.*)/i) {
497 $lineselecttextcolor = $1;
499 elsif($l =~ /^filetype colours: *(.*)/i) {
500 $filetylecolor = $1;
502 elsif($_ = check_res($l, "ui viewport")) {
503 $listviewport = $_;
507 elsif($l =~ /^ *<remote>/i) {
508 while(<WPS>) {
509 # parse remote settings
510 my $l = $_;
511 if ($l =~ /^ *<\/remote>/i) {
512 last;
514 elsif(!$has_remote) {
515 next; # dont parse <remote> section
517 elsif($_ = check_res($l, "rwps", 1)) {
518 $rwps = $_;
520 elsif($_ = check_res($l, "rsbs", 1)) {
521 $rsbs = $_;
523 elsif($_ = check_res($l, "rfms", 1)) {
524 $rfms = $_;
526 elsif($_ = check_res($l, "Font", 1)) {
527 $remotefont = $_;
529 elsif($_ = check_res($l, "iconset", 1)) {
530 $remoteiconset = $_;
532 elsif($_ = check_res($l, "viewers iconset", 1)) {
533 $remoteviewericon = $_;
535 elsif($_ = check_res($l, "statusbar", 1)) {
536 $remotestatusbar = $_;
538 elsif($_ = check_res($l, "ui viewport", 1)) {
539 $remotelistviewport = $_;
543 elsif($l =~ /^ *<\/theme>/i) {
544 # for each wps,sbs,fms (+ remote variants) check if <theme>[.<model>].wps
545 # exists if no filename was specified in WPSLIST
546 my $req_skin;
548 if ($has_wps eq "yes" && !$wps) {
549 $wps = check_skinfile("wps");
550 } elsif ($has_wps eq "no") {
551 $wps = '';
554 if ($has_sbs eq "yes" && !$sbs) {
555 $sbs = check_skinfile("sbs");
556 } elsif ($has_sbs eq "no") {
557 $sbs = '';
560 if ($has_fms eq "yes" && !$fms) {
561 $fms = check_skinfile("fms");
562 } elsif ($has_fms eq "no") {
563 $fms = '';
566 # now check for remote skin files (use main screen's extension)
567 if ($has_remote) {
568 if ($has_rwps eq "yes" && !$rwps) {
569 $rwps = check_skinfile("wps");
570 } elsif ($has_rwps eq "no") {
571 $rwps = '';
574 if ($has_rsbs eq "yes" && !$rsbs) {
575 $rsbs = check_skinfile("sbs");
576 } elsif ($has_rsbs eq "no") {
577 $rsbs = '';
580 if ($has_rfms eq "yes" && !$rfms) {
581 $rfms = check_skinfile("fms");
582 } elsif ($has_rfms eq "no") {
583 $rfms = '';
586 #print "LCD: $wps wants $width x $height\n";
589 # The target model has an LCD that is suitable for this
590 # WPS
592 #print "Size requirement is fine!\n";
593 mkdirs() if (-e "$wpsdir/$theme");
594 # Do the copying before building the .cfg - buildcfg()
595 # mangles some filenames
596 if (defined($backdrop) && $backdrop ne "-") {
597 copybackdrop();
598 $backdrop = "$rbdir/" . normalize($backdrop);
600 foreach my $i ($iconset, $viewericon, $remoteiconset, $remoteviewericon) {
601 if (defined($i) && $i ne "-") {
602 copythemeicon($i);
603 $i = "$rbdir/$i";
606 foreach my $i ($font, $remotefont) {
607 if (defined($i) && $i ne "-") {
608 copythemefont($i);
609 $i = "$rbdir/fonts/$i";
612 buildcfg();
613 copywps();
615 else{
616 #print "Unknown line: $l!\n";
620 close(WPS);