1 /* vi: set sw=4 ts=4: */
3 * Copyright (C) 2008 Michele Sanges <michele.sanges@gmail.com>
5 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 * - use kernel option 'vga=xxx' or otherwise enable framebuffer device.
9 * - put somewhere fbsplash.cfg file and an image in .ppm format.
10 * - run applet: $ setsid fbsplash [params] &
12 * -d /dev/fbN: framebuffer device (if not /dev/fb0)
13 * -s path_to_image_file (can be "-" for stdin)
15 * -f path_to_fifo (can be "-" for stdin)
16 * - if you want to run it only in presence of a kernel parameter
17 * (for example fbsplash=on), use:
18 * grep -q "fbsplash=on" </proc/cmdline && setsid fbsplash [params]
19 * - commands for fifo:
20 * "NN" (ASCII decimal number) - percentage to show on progress bar.
21 * "exit" (or just close fifo) - well you guessed it.
24 //usage:#define fbsplash_trivial_usage
25 //usage: "-s IMGFILE [-c] [-d DEV] [-i INIFILE] [-f CMD]"
26 //usage:#define fbsplash_full_usage "\n\n"
28 //usage: "\n -c Hide cursor"
29 //usage: "\n -d Framebuffer device (default /dev/fb0)"
30 //usage: "\n -i Config file (var=value):"
31 //usage: "\n BAR_LEFT,BAR_TOP,BAR_WIDTH,BAR_HEIGHT"
32 //usage: "\n BAR_R,BAR_G,BAR_B"
33 //usage: "\n -f Control pipe (else exit after drawing image)"
34 //usage: "\n commands: 'NN' (% for progress bar) or 'exit'"
39 /* If you want logging messages on /tmp/fbsplash.log... */
44 bool bdebug_messages
; // enable/disable logging
45 FILE *logfile_fd
; // log file
47 unsigned char *addr
; // pointer to framebuffer memory
48 unsigned ns
[7]; // n-parameters
49 const char *image_filename
;
50 struct fb_var_screeninfo scr_var
;
51 struct fb_fix_screeninfo scr_fix
;
52 unsigned bytes_per_pixel
;
53 // cached (8 - scr_var.COLOR.length):
58 #define G (*ptr_to_globals)
59 #define INIT_G() do { \
60 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
63 #define nbar_width ns[0] // progress bar width
64 #define nbar_height ns[1] // progress bar height
65 #define nbar_posx ns[2] // progress bar horizontal position
66 #define nbar_posy ns[3] // progress bar vertical position
67 #define nbar_colr ns[4] // progress bar color red component
68 #define nbar_colg ns[5] // progress bar color green component
69 #define nbar_colb ns[6] // progress bar color blue component
72 #define DEBUG_MESSAGE(strMessage, args...) \
73 if (G.bdebug_messages) { \
74 fprintf(G.logfile_fd, "[%s][%s] - %s\n", \
75 __FILE__, __FUNCTION__, strMessage); \
78 #define DEBUG_MESSAGE(...) ((void)0)
82 * Configure palette for RGB:332
84 static void fb_setpal(int fd
)
87 /* fb colors are 16 bit */
88 unsigned short red
[256], green
[256], blue
[256];
92 for (i
= 0; i
< 256; i
++) {
93 /* Color is encoded in pixel value as rrrgggbb.
94 * 3-bit color is mapped to 16-bit one as:
95 * 000 -> 00000000 00000000
96 * 001 -> 00100100 10010010
98 * 011 -> 01101101 10110110
99 * 100 -> 10010010 01001001
101 * 111 -> 11111111 11111111
103 red
[i
] = (( i
>> 5 ) * 0x9249) >> 2; // rrr * 00 10010010 01001001 >> 2
104 green
[i
] = (((i
>> 2) & 0x7) * 0x9249) >> 2; // ggg * 00 10010010 01001001 >> 2
105 /* 2-bit color is easier: */
106 blue
[i
] = ( i
& 0x3) * 0x5555; // bb * 01010101 01010101
116 xioctl(fd
, FBIOPUTCMAP
, &cmap
);
120 * Open and initialize the framebuffer device
121 * \param *strfb_device pointer to framebuffer device
123 static void fb_open(const char *strfb_device
)
125 int fbfd
= xopen(strfb_device
, O_RDWR
);
127 // framebuffer properties
128 xioctl(fbfd
, FBIOGET_VSCREENINFO
, &G
.scr_var
);
129 xioctl(fbfd
, FBIOGET_FSCREENINFO
, &G
.scr_fix
);
131 switch (G
.scr_var
.bits_per_pixel
) {
142 bb_error_msg_and_die("unsupported %u bpp", (int)G
.scr_var
.bits_per_pixel
);
146 G
.red_shift
= 8 - G
.scr_var
.red
.length
;
147 G
.green_shift
= 8 - G
.scr_var
.green
.length
;
148 G
.blue_shift
= 8 - G
.scr_var
.blue
.length
;
149 G
.bytes_per_pixel
= (G
.scr_var
.bits_per_pixel
+ 7) >> 3;
151 // map the device in memory
153 G
.scr_var
.yres
* G
.scr_fix
.line_length
,
154 PROT_WRITE
, MAP_SHARED
, fbfd
, 0);
155 if (G
.addr
== MAP_FAILED
)
156 bb_perror_msg_and_die("mmap");
158 // point to the start of the visible screen
159 G
.addr
+= G
.scr_var
.yoffset
* G
.scr_fix
.line_length
+ G
.scr_var
.xoffset
* G
.bytes_per_pixel
;
165 * Return pixel value of the passed RGB color.
166 * This is performance critical fn.
168 static unsigned fb_pixel_value(unsigned r
, unsigned g
, unsigned b
)
170 /* We assume that the r,g,b values are <= 255 */
172 if (G
.bytes_per_pixel
== 1) {
173 r
= r
& 0xe0; // 3-bit red
174 g
= (g
>> 3) & 0x1c; // 3-bit green
175 b
= b
>> 6; // 2-bit blue
178 if (G
.bytes_per_pixel
== 2) {
179 // ARM PL110 on Integrator/CP has RGBA5551 bit arrangement.
180 // We want to support bit locations like that.
182 // First shift out unused bits
183 r
= r
>> G
.red_shift
;
184 g
= g
>> G
.green_shift
;
185 b
= b
>> G
.blue_shift
;
186 // Then shift the remaining bits to their offset
187 return (r
<< G
.scr_var
.red
.offset
) +
188 (g
<< G
.scr_var
.green
.offset
) +
189 (b
<< G
.scr_var
.blue
.offset
);
192 return b
+ (g
<< 8) + (r
<< 16);
196 * Draw pixel on framebuffer
198 static void fb_write_pixel(unsigned char *addr
, unsigned pixel
)
200 switch (G
.bytes_per_pixel
) {
205 *(uint16_t *)addr
= pixel
;
208 *(uint32_t *)addr
= pixel
;
210 default: // 24 bits per pixel
212 addr
[1] = pixel
>> 8;
213 addr
[2] = pixel
>> 16;
219 * Draw hollow rectangle on framebuffer
221 static void fb_drawrectangle(void)
225 unsigned char *ptr1
, *ptr2
;
226 unsigned char nred
= G
.nbar_colr
/2;
227 unsigned char ngreen
= G
.nbar_colg
/2;
228 unsigned char nblue
= G
.nbar_colb
/2;
230 thispix
= fb_pixel_value(nred
, ngreen
, nblue
);
233 ptr1
= G
.addr
+ G
.nbar_posy
* G
.scr_fix
.line_length
+ G
.nbar_posx
* G
.bytes_per_pixel
;
234 ptr2
= G
.addr
+ (G
.nbar_posy
+ G
.nbar_height
- 1) * G
.scr_fix
.line_length
+ G
.nbar_posx
* G
.bytes_per_pixel
;
235 cnt
= G
.nbar_width
- 1;
237 fb_write_pixel(ptr1
, thispix
);
238 fb_write_pixel(ptr2
, thispix
);
239 ptr1
+= G
.bytes_per_pixel
;
240 ptr2
+= G
.bytes_per_pixel
;
241 } while (--cnt
>= 0);
244 ptr1
= G
.addr
+ G
.nbar_posy
* G
.scr_fix
.line_length
+ G
.nbar_posx
* G
.bytes_per_pixel
;
245 ptr2
= G
.addr
+ G
.nbar_posy
* G
.scr_fix
.line_length
+ (G
.nbar_posx
+ G
.nbar_width
- 1) * G
.bytes_per_pixel
;
246 cnt
= G
.nbar_height
- 1;
248 fb_write_pixel(ptr1
, thispix
);
249 fb_write_pixel(ptr2
, thispix
);
250 ptr1
+= G
.scr_fix
.line_length
;
251 ptr2
+= G
.scr_fix
.line_length
;
252 } while (--cnt
>= 0);
257 * Draw filled rectangle on framebuffer
258 * \param nx1pos,ny1pos upper left position
259 * \param nx2pos,ny2pos down right position
260 * \param nred,ngreen,nblue rgb color
262 static void fb_drawfullrectangle(int nx1pos
, int ny1pos
, int nx2pos
, int ny2pos
,
263 unsigned char nred
, unsigned char ngreen
, unsigned char nblue
)
265 int cnt1
, cnt2
, nypos
;
269 thispix
= fb_pixel_value(nred
, ngreen
, nblue
);
271 cnt1
= ny2pos
- ny1pos
;
274 ptr
= G
.addr
+ nypos
* G
.scr_fix
.line_length
+ nx1pos
* G
.bytes_per_pixel
;
275 cnt2
= nx2pos
- nx1pos
;
277 fb_write_pixel(ptr
, thispix
);
278 ptr
+= G
.bytes_per_pixel
;
279 } while (--cnt2
>= 0);
282 } while (--cnt1
>= 0);
287 * Draw a progress bar on framebuffer
288 * \param percent percentage of loading
290 static void fb_drawprogressbar(unsigned percent
)
292 int left_x
, top_y
, pos_x
;
293 unsigned width
, height
;
296 left_x
= G
.nbar_posx
;
298 width
= G
.nbar_width
- 1;
299 height
= G
.nbar_height
- 1;
300 if ((int)(height
| width
) < 0)
302 // NB: "width" of 1 actually makes rect with width of 2!
305 // inner "empty" rectangle
310 if ((int)(height
| width
) < 0)
317 // actual progress bar
318 pos_x
+= (unsigned)(width
* percent
) / 100;
323 height
++; // divide by 0 is bad
325 // draw one-line thick "rectangle"
326 // top line will have gray lvl 200, bottom one 100
327 unsigned gray_level
= 100 + (unsigned)i
*100 / height
;
328 fb_drawfullrectangle(
330 gray_level
, gray_level
, gray_level
);
336 fb_drawfullrectangle(
338 left_x
+ width
, top_y
+ height
,
339 G
.nbar_colr
, G
.nbar_colg
, G
.nbar_colb
);
344 * Draw image from PPM file
346 static void fb_drawimage(void)
350 unsigned char *pixline
;
351 unsigned i
, j
, width
, height
, line_size
;
353 if (LONE_DASH(G
.image_filename
)) {
356 int fd
= open_zipped(G
.image_filename
);
358 bb_simple_perror_msg_and_die(G
.image_filename
);
359 theme_file
= xfdopen_for_read(fd
);
363 * - Magic: two characters "P6".
364 * - Whitespace (blanks, TABs, CRs, LFs).
365 * - A width, formatted as ASCII characters in decimal.
367 * - A height, ASCII decimal.
369 * - The maximum color value, ASCII decimal, in 0..65535
370 * - Newline or other single whitespace character.
371 * (we support newline only)
372 * - A raster of Width * Height pixels in triplets of rgb
373 * in pure binary by 1 or 2 bytes. (we support only 1 byte)
375 #define concat_buf bb_common_bufsiz1
376 read_ptr
= concat_buf
;
378 int w
, h
, max_color_val
;
379 int rem
= concat_buf
+ sizeof(concat_buf
) - read_ptr
;
381 || fgets(read_ptr
, rem
, theme_file
) == NULL
383 bb_error_msg_and_die("bad PPM file '%s'", G
.image_filename
);
385 read_ptr
= strchrnul(read_ptr
, '#');
386 *read_ptr
= '\0'; /* ignore #comments */
387 if (sscanf(concat_buf
, "P6 %u %u %u", &w
, &h
, &max_color_val
) == 3
388 && max_color_val
<= 255
390 width
= w
; /* w is on stack, width may be in register */
397 pixline
= xmalloc(line_size
);
399 if (width
> G
.scr_var
.xres
)
400 width
= G
.scr_var
.xres
;
401 if (height
> G
.scr_var
.yres
)
402 height
= G
.scr_var
.yres
;
403 for (j
= 0; j
< height
; j
++) {
404 unsigned char *pixel
;
407 if (fread(pixline
, 1, line_size
, theme_file
) != line_size
)
408 bb_error_msg_and_die("bad PPM file '%s'", G
.image_filename
);
410 src
= G
.addr
+ j
* G
.scr_fix
.line_length
;
411 for (i
= 0; i
< width
; i
++) {
412 unsigned thispix
= fb_pixel_value(pixel
[0], pixel
[1], pixel
[2]);
413 fb_write_pixel(src
, thispix
);
414 src
+= G
.bytes_per_pixel
;
424 * Parse configuration file
425 * \param *cfg_filename name of the configuration file
427 static void init(const char *cfg_filename
)
429 static const char param_names
[] ALIGN1
=
430 "BAR_WIDTH\0" "BAR_HEIGHT\0"
431 "BAR_LEFT\0" "BAR_TOP\0"
432 "BAR_R\0" "BAR_G\0" "BAR_B\0"
438 parser_t
*parser
= config_open2(cfg_filename
, xfopen_stdin
);
439 while (config_read(parser
, token
, 2, 2, "#=",
440 (PARSE_NORMAL
| PARSE_MIN_DIE
) & ~(PARSE_TRIM
| PARSE_COLLAPSE
))) {
441 unsigned val
= xatoi_positive(token
[1]);
442 int i
= index_in_strings(param_names
, token
[0]);
444 bb_error_msg_and_die("syntax error: %s", token
[0]);
449 G
.bdebug_messages
= val
;
450 if (G
.bdebug_messages
)
451 G
.logfile_fd
= xfopen_for_write("/tmp/fbsplash.log");
455 config_close(parser
);
459 int fbsplash_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
460 int fbsplash_main(int argc UNUSED_PARAM
, char **argv
)
462 const char *fb_device
, *cfg_filename
, *fifo_filename
;
463 FILE *fp
= fp
; // for compiler
470 // parse command line options
471 fb_device
= "/dev/fb0";
473 fifo_filename
= NULL
;
474 bCursorOff
= 1 & getopt32(argv
, "cs:d:i:f:",
475 &G
.image_filename
, &fb_device
, &cfg_filename
, &fifo_filename
);
477 // parse configuration file
481 // We must have -s IMG
482 if (!G
.image_filename
)
487 if (fifo_filename
&& bCursorOff
) {
488 // hide cursor (BEFORE any fb ops)
489 full_write(STDOUT_FILENO
, "\033[?25l", 6);
497 fp
= xfopen_stdin(fifo_filename
);
499 // For named pipes, we want to support this:
501 // fbsplash -f cmd_pipe .... &
506 // This means that we don't want fbsplash to get EOF
507 // when last writer closes input end.
508 // The simplest way is to open fifo for writing too
509 // and become an additional writer :)
510 open(fifo_filename
, O_WRONLY
); // errors are ignored
513 fb_drawprogressbar(0);
514 // Block on read, waiting for some input.
515 // Use of <stdio.h> style I/O allows to correctly
516 // handle a case when we have many buffered lines
517 // already in the pipe
518 while ((num_buf
= xmalloc_fgetline(fp
)) != NULL
) {
519 if (strncmp(num_buf
, "exit", 4) == 0) {
520 DEBUG_MESSAGE("exit");
524 if (isdigit(num_buf
[0]) && (num
<= 100)) {
526 DEBUG_MESSAGE(itoa(num
));
528 fb_drawprogressbar(num
);
533 if (bCursorOff
) // restore cursor
534 full_write(STDOUT_FILENO
, "\033[?25h", 6);