ypr0: Correct .scrobbler.log path for YPR0
[maemo-rb.git] / wps / wpsbuild.pl
blob6df35df58555de69d93011f7e08a0258c4773e36
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 $remoteviewericon;
68 my $lineselecttextcolor;
69 my $filetylecolor;
70 my $listviewport;
71 my $remotelistviewport;
73 # LCD sizes
74 my ($main_height, $main_width, $main_depth);
75 my ($remote_height, $remote_width, $remote_depth);
76 my $has_remote;
79 if(!$wpslist) {
80 print "Usage: wpsbuilds.pl <WPSLIST> <target>\n",
81 "Run this script in the root of the target build, and it will put all the\n",
82 "stuff in $tempdir/wps/\n and build the cfg according to $rbdir";
83 exit;
86 sub getlcdsizes
88 my ($remote) = @_;
90 open(GCC, ">gcctemp");
91 if($remote) {
92 # Get the remote LCD screen size
93 print GCC <<STOP
94 \#include "config.h"
95 #ifdef HAVE_REMOTE_LCD
96 Height: LCD_REMOTE_HEIGHT
97 Width: LCD_REMOTE_WIDTH
98 Depth: LCD_REMOTE_DEPTH
99 #endif
100 STOP
103 else {
104 print GCC <<STOP
105 \#include "config.h"
106 Height: LCD_HEIGHT
107 Width: LCD_WIDTH
108 Depth: LCD_DEPTH
109 STOP
112 close(GCC);
114 my $c="cat gcctemp | gcc $cppdef -I. -I$firmdir/export -E -P -";
116 #print "CMD $c\n";
118 open(GETSIZE, "$c|");
120 my ($height, $width, $depth);
121 while(<GETSIZE>) {
122 if($_ =~ /^Height: (\d*)/) {
123 $height = $1;
125 elsif($_ =~ /^Width: (\d*)/) {
126 $width = $1;
128 elsif($_ =~ /^Depth: (\d*)/) {
129 $depth = $1;
131 if($height && $width && $depth) {
132 last;
135 close(GETSIZE);
136 unlink("gcctemp");
138 return ($height, $width, $depth);
141 sub mkdirs
143 mkdir "$tempdir/wps", 0777;
144 mkdir "$tempdir/themes", 0777;
145 mkdir "$tempdir/icons", 0777;
147 if( -d "$tempdir/wps/$theme") {
148 #print STDERR "wpsbuild warning: directory wps/$theme already exists!\n";
150 else
152 mkdir "$tempdir/wps/$theme", 0777;
156 sub normalize
158 my $in = $_[0];
159 # strip resolution
160 $in =~ s/(\.[0-9]*x[0-9]*x[0-9]*)//;
161 return $in;
164 sub copybackdrop
166 #copy the backdrop file into the build dir
167 if ($backdrop ne '') {
168 my $dst = normalize($backdrop);
169 system("cp $ROOT/$backdrop $tempdir/$dst");
173 sub copythemefont
175 #copy the font specified by the theme
176 my $o = $_[0];
178 $o =~ s/\.fnt/\.bdf/;
179 mkdir "$tempdir/fonts";
180 system("$ROOT/tools/convbdf -f -o \"$tempdir/fonts/$_[0]\" \"$ROOT/fonts/$o\" ");
183 sub copythemeicon
185 my $i = $_[0];
186 #copy the icon specified by the theme
187 if ($i ne "-") {
188 my $tempicon = $tempdir . "/" . $i;
189 $tempicon =~ /\/.*icons\/(.*)/i;
190 system("cp $ROOT/icons/$1 $tempicon");
194 sub uniq {
195 my %seen = ();
196 my @r = ();
197 foreach my $a (@_) {
198 unless ($seen{$a}) {
199 push @r, $a;
200 $seen{$a} = 1;
203 return @r;
206 sub copywps
208 # we assume that we copy the WPS files from the same dir the WPSLIST
209 # file is located in
210 my $dir;
211 my %skinfiles = ("wps", $wps,
212 "sbs", $sbs,
213 "fms", $fms,
214 "rwps", $rwps,
215 "rsbs", $rsbs,
216 "rfms", $rfms);
217 my @filelist;
218 my $file;
220 if($wpslist =~ /(.*)\/WPSLIST/) {
221 $dir = $1;
223 # copy fully-fledged wps, sbs, etc. including graphics
224 foreach my $ext (keys %skinfiles) {
225 next unless ($skinfiles{$ext});
226 $file = $skinfiles{$ext};
227 system("cp $dir/$file $tempdir/wps/$theme.$ext");
228 open(SKIN, "$dir/$file");
229 while (<SKIN>) {
230 $filelist[$#filelist + 1] = $1 if (/[\(,]([^,]*?.bmp)[\),]/);
232 close(SKIN);
235 if ($#filelist >= 0) {
236 if (-e "$dir/$theme") {
237 foreach $file (uniq(@filelist)) {
238 system("cp $dir/$theme/$file $tempdir/wps/$theme/");
241 else {
242 print STDERR "beep, no dir to copy WPS from!\n";
245 } else {
246 print STDERR "No source directory!\n";
250 sub buildcfg {
251 my $cfg = $theme . ".cfg";
252 my @out;
254 push @out, <<MOO
256 \# $cfg generated by wpsbuild.pl
257 \# $wps is made by $author
262 my %skinfiles = ("wps" => $wps,
263 "sbs" => $sbs,
264 "fms" => $fms,
265 "rwps" => $rwps,
266 "rsbs" => $rsbs,
267 "rfms" => $rfms);
268 for my $skin (keys %skinfiles) {
269 my $val = $skinfiles{$skin};
270 if (!defined($val)) {
271 # dont put value if not defined (e.g. rwps when there's no remote)
272 next;
273 } elsif ($val eq '') {
274 # empty resets to built-in
275 push @out, "$skin: -\n";
276 } else {
277 # file name is always <theme>.{wps,rwps,...} (see copywps())
278 push @out, "$skin: $rbdir/wps/$theme.$skin\n";
282 push @out, "selector type: $selecttype\n" if (defined($selecttype));
283 push @out, "backdrop: $backdrop\n" if (defined($backdrop));
284 push @out, "filetype colours: $filetylecolor\n" if (defined($filetylecolor));
286 if ($main_depth > 2) {
287 push @out, "foreground color: $fgcolor\n" if($fgcolor);
288 push @out, "background color: $bgcolor\n" if($bgcolor);
289 push @out, "line selector start color: $lineselectstart\n" if($lineselectstart);
290 push @out, "line selector end color: $lineselectend\n" if($lineselectend);;
291 push @out, "line selector text color: $lineselecttextcolor\n" if($lineselecttextcolor);
294 push @out, "font: $font\n" if (defined($font));
295 push @out, "statusbar: $statusbar\n" if (defined($statusbar));
296 push @out, "iconset: $iconset\n" if (defined($iconset));
297 push @out, "viewers iconset: $viewericon\n" if (defined($viewericon));
298 push @out, "ui viewport: $listviewport\n" if (defined($listviewport));
300 if ($has_remote) {
301 push @out, "remote font: $remotefont\n" if (defined($remotefont));
302 push @out, "remote statusbar: $remotestatusbar\n" if (defined($remotestatusbar));
303 push @out, "remote iconset: $remoteiconset\n" if (defined($remoteiconset));
304 push @out, "remote viewers iconset: $remoteviewericon\n" if (defined($remoteviewericon));
305 push @out, "remote ui viewport: $remotelistviewport\n" if (defined($remotelistviewport));
308 if(-f "$tempdir/wps/$cfg") {
309 print STDERR "wpsbuild warning: wps/$cfg already exists!\n";
311 else {
312 open(CFG, ">$tempdir/themes/$cfg");
313 print CFG @out;
314 close(CFG);
318 # Get the LCD sizes first
319 ($main_height, $main_width, $main_depth) = getlcdsizes();
320 ($remote_height, $remote_width, $remote_depth) = getlcdsizes(1);
322 #print "LCD: ${main_width}x${main_height}x${main_depth}\n";
323 $has_remote = 1 if ($remote_height && $remote_width && $remote_depth);
326 # check if line matches the setting string or if it contains a regex
327 # that contains the targets resolution
328 sub check_res {
329 my ($line, $string, $remote) = @_;
330 if ($line =~ /^${string}: *(.*)/i) {
331 return $1;
333 elsif($line =~ /^${string}.(.*): *(.*)/i) {
334 # $1 is a resolution regex, $2 the filename incl. resolution
335 my $fn = $2;
336 my $size_str = "${main_width}x${main_height}x${main_depth}";
337 if ($remote) {
338 $size_str = "${remote_width}x${remote_height}x${remote_depth}";
340 if ($size_str =~ /$1$/) {
341 return $fn;
344 return "";
347 # check if <theme>.<model>.<ext> exists. If not, check if <theme>.<ext> exists
348 sub check_skinfile {
349 my $ext = $_[0];
350 my $req_skin = $theme . "." . $modelname . ".$ext";
351 if (-e "$wpsdir/$req_skin") {
352 return $req_skin;
353 } else {
354 $req_skin = $theme . ".$ext";
355 if (-e "$wpsdir/$req_skin") {
356 return $req_skin;
359 return '';
363 # Infer WPS (etc.) filename from the the if it wasnt given
364 $wpslist =~ /(.*)WPSLIST/;
365 $wpsdir = $1;
367 open(WPS, "<$wpslist");
368 while(<WPS>) {
369 my $l = $_;
371 # remove CR
372 $l =~ s/\r//g;
373 if($l =~ /^ *\#/) {
374 # skip comment
375 next;
378 # prefix $rbdir with / if needed (needed for the theme.cfg)
379 unless ($rbdir =~ m/^\/.*/) {
380 $rbdir = "/" . $rbdir;
383 if($l =~ /^ *<theme>/i) {
384 # undef is a unary operator (!)
385 undef $theme;
386 undef $has_wps;
387 undef $has_rwps;
388 undef $has_sbs;
389 undef $has_rsbs;
390 undef $has_fms;
391 undef $has_rfms;
392 undef $wps;
393 undef $rwps;
394 undef $sbs;
395 undef $rsbs;
396 undef $fms;
397 undef $rfms;
398 undef $width;
399 undef $height;
400 undef $font;
401 undef $remotefont;
402 undef $fgcolor;
403 undef $bgcolor;
404 undef $statusbar;
405 undef $remotestatusbar;
406 undef $author;
407 undef $backdrop;
408 undef $lineselectstart;
409 undef $lineselectend;
410 undef $selecttype;
411 undef $iconset;
412 undef $remoteiconset;
413 undef $viewericon;
414 undef $remoteviewericon;
415 undef $lineselecttextcolor;
416 undef $filetylecolor;
417 undef $listviewport;
418 undef $remotelistviewport;
420 elsif($l =~ /^Name: *(.*)/i) {
421 $theme = $1;
423 elsif($l =~ /^Authors: *(.*)/i) {
424 $author = $1;
426 elsif ($l =~ /^WPS: *(yes|no)/i) {
427 $has_wps = $1;
429 elsif ($l =~ /^RWPS: *(yes|no)/i) {
430 $has_rwps = $1;
432 elsif ($l =~ /^SBS: *(yes|no)/i) {
433 $has_sbs = $1;
435 elsif ($l =~ /^RSBS: *(yes|no)/i) {
436 $has_rsbs = $1;
438 elsif ($l =~ /^FMS: *(yes|no)/i) {
439 $has_fms = $1;
441 elsif ($l =~ /^RFMS: *(yes|no)/i) {
442 $has_rfms = $1;
444 elsif($l =~ /^ *<main>/i) {
445 # parse main unit settings
446 while(<WPS>) {
447 my $l = $_;
448 if ($l =~ /^ *<\/main>/i) {
449 last;
451 elsif($_ = check_res($l, "wps")) {
452 $wps = $_;
454 elsif($_ = check_res($l, "sbs")) {
455 $sbs = $_;
457 elsif($_ = check_res($l, "fms")) {
458 $fms = $_;
460 elsif($_ = check_res($l, "Font")) {
461 $font = $_;
463 elsif($_ = check_res($l, "Statusbar")) {
464 $statusbar = $_;
466 elsif($_ = check_res($l, "Backdrop")) {
467 $backdrop = $_;
469 elsif($l =~ /^Foreground Color: *(.*)/i) {
470 $fgcolor = $1;
472 elsif($l =~ /^Background Color: *(.*)/i) {
473 $bgcolor = $1;
475 elsif($l =~ /^line selector start color: *(.*)/i) {
476 $lineselectstart = $1;
478 elsif($l =~ /^line selector end color: *(.*)/i) {
479 $lineselectend = $1;
481 elsif($_ = check_res($l, "selector type")) {
482 $selecttype = $_;
484 elsif($_ = check_res($l, "iconset")) {
485 $iconset = $_;
487 elsif($_ = check_res($l, "viewers iconset")) {
488 $viewericon = $_;
490 elsif($l =~ /^line selector text color: *(.*)/i) {
491 $lineselecttextcolor = $1;
493 elsif($l =~ /^filetype colours: *(.*)/i) {
494 $filetylecolor = $1;
496 elsif($_ = check_res($l, "ui viewport")) {
497 $listviewport = $_;
501 elsif($l =~ /^ *<remote>/i) {
502 while(<WPS>) {
503 # parse remote settings
504 my $l = $_;
505 if ($l =~ /^ *<\/remote>/i) {
506 last;
508 elsif(!$has_remote) {
509 next; # dont parse <remote> section
511 elsif($_ = check_res($l, "rwps", 1)) {
512 $rwps = $_;
514 elsif($_ = check_res($l, "rsbs", 1)) {
515 $rsbs = $_;
517 elsif($_ = check_res($l, "rfms", 1)) {
518 $rfms = $_;
520 elsif($_ = check_res($l, "Font", 1)) {
521 $remotefont = $_;
523 elsif($_ = check_res($l, "iconset", 1)) {
524 $remoteiconset = $_;
526 elsif($_ = check_res($l, "viewers iconset", 1)) {
527 $remoteviewericon = $_;
529 elsif($_ = check_res($l, "statusbar", 1)) {
530 $remotestatusbar = $_;
532 elsif($_ = check_res($l, "ui viewport", 1)) {
533 $remotelistviewport = $_;
537 elsif($l =~ /^ *<\/theme>/i) {
538 # for each wps,sbs,fms (+ remote variants) check if <theme>[.<model>].wps
539 # exists if no filename was specified in WPSLIST
540 my $req_skin;
542 if ($has_wps eq "yes" && !$wps) {
543 $wps = check_skinfile("wps");
544 } elsif ($has_wps eq "no") {
545 $wps = '';
548 if ($has_sbs eq "yes" && !$sbs) {
549 $sbs = check_skinfile("sbs");
550 } elsif ($has_sbs eq "no") {
551 $sbs = '';
554 if ($has_fms eq "yes" && !$fms) {
555 $fms = check_skinfile("fms");
556 } elsif ($has_fms eq "no") {
557 $fms = '';
560 # now check for remote skin files (use main screen's extension)
561 if ($has_remote) {
562 if ($has_rwps eq "yes" && !$rwps) {
563 $rwps = check_skinfile("wps");
564 } elsif ($has_rwps eq "no") {
565 $rwps = '';
568 if ($has_rsbs eq "yes" && !$rsbs) {
569 $rsbs = check_skinfile("sbs");
570 } elsif ($has_rsbs eq "no") {
571 $rsbs = '';
574 if ($has_rfms eq "yes" && !$rfms) {
575 $rfms = check_skinfile("fms");
576 } elsif ($has_rfms eq "no") {
577 $rfms = '';
580 #print "LCD: $wps wants $width x $height\n";
583 # The target model has an LCD that is suitable for this
584 # WPS
586 #print "Size requirement is fine!\n";
587 mkdirs() if (-e "$wpsdir/$theme");
588 # Do the copying before building the .cfg - buildcfg()
589 # mangles some filenames
590 if (defined($backdrop) && $backdrop ne "-") {
591 copybackdrop();
592 $backdrop = "$rbdir/" . normalize($backdrop);
594 foreach my $i ($iconset, $viewericon, $remoteiconset, $remoteviewericon) {
595 if (defined($i) && $i ne "-") {
596 copythemeicon($i);
597 $i = "$rbdir/$i";
600 foreach my $i ($font, $remotefont) {
601 if (defined($i) && $i ne "-") {
602 copythemefont($font);
603 $i = "$rbdir/fonts/$font";
606 buildcfg();
607 copywps();
609 else{
610 #print "Unknown line: $l!\n";
614 close(WPS);