blob: 5b2e5ac56f4e88a8dc83baf3fdc649bc32b5f0e6 [file] [log] [blame]
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +00001/* vi: set sw=4 ts=4: */
2/*
Bernhard Reutner-Fischerdd76b792009-01-27 12:56:33 +00003 * Copyright (C) 2008 Michele Sanges <michele.sanges@gmail.com>
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +00004 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +00006 *
7 * Usage:
8 * - use kernel option 'vga=xxx' or otherwise enable framebuffer device.
Denis Vlasenko25a9c172008-03-26 15:12:11 +00009 * - put somewhere fbsplash.cfg file and an image in .ppm format.
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000010 * - run applet: $ setsid fbsplash [params] &
Denis Vlasenko1f228982008-04-22 00:16:29 +000011 * -c: hide cursor
12 * -d /dev/fbN: framebuffer device (if not /dev/fb0)
13 * -s path_to_image_file (can be "-" for stdin)
14 * -i path_to_cfg_file
15 * -f path_to_fifo (can be "-" for stdin)
Denis Vlasenko25a9c172008-03-26 15:12:11 +000016 * - if you want to run it only in presence of a kernel parameter
17 * (for example fbsplash=on), use:
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000018 * 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.
22 */
Denys Vlasenkofb4da162016-11-22 23:14:24 +010023//config:config FBSPLASH
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020024//config: bool "fbsplash (27 kb)"
Denys Vlasenkofb4da162016-11-22 23:14:24 +010025//config: default y
26//config: select PLATFORM_LINUX
27//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020028//config: Shows splash image and progress bar on framebuffer device.
29//config: Can be used during boot phase of an embedded device.
30//config: Usage:
31//config: - use kernel option 'vga=xxx' or otherwise enable fb device.
32//config: - put somewhere fbsplash.cfg file and an image in .ppm format.
33//config: - $ setsid fbsplash [params] &
Denys Vlasenkofb4da162016-11-22 23:14:24 +010034//config: -c: hide cursor
35//config: -d /dev/fbN: framebuffer device (if not /dev/fb0)
36//config: -s path_to_image_file (can be "-" for stdin)
37//config: -i path_to_cfg_file (can be "-" for stdin)
38//config: -f path_to_fifo (can be "-" for stdin)
Denys Vlasenko72089cf2017-07-21 09:50:55 +020039//config: - if you want to run it only in presence of kernel parameter:
Denys Vlasenkofb4da162016-11-22 23:14:24 +010040//config: grep -q "fbsplash=on" </proc/cmdline && setsid fbsplash [params] &
Denys Vlasenko72089cf2017-07-21 09:50:55 +020041//config: - commands for fifo:
Denys Vlasenkofb4da162016-11-22 23:14:24 +010042//config: "NN" (ASCII decimal number) - percentage to show on progress bar
43//config: "exit" - well you guessed it
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000044
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010045//applet:IF_FBSPLASH(APPLET(fbsplash, BB_DIR_SBIN, BB_SUID_DROP))
46
47//kbuild:lib-$(CONFIG_FBSPLASH) += fbsplash.o
48
Pere Orga5bc8c002011-04-11 03:29:49 +020049//usage:#define fbsplash_trivial_usage
50//usage: "-s IMGFILE [-c] [-d DEV] [-i INIFILE] [-f CMD]"
51//usage:#define fbsplash_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020052//usage: " -s Image"
Pere Orga5bc8c002011-04-11 03:29:49 +020053//usage: "\n -c Hide cursor"
54//usage: "\n -d Framebuffer device (default /dev/fb0)"
55//usage: "\n -i Config file (var=value):"
56//usage: "\n BAR_LEFT,BAR_TOP,BAR_WIDTH,BAR_HEIGHT"
57//usage: "\n BAR_R,BAR_G,BAR_B"
58//usage: "\n -f Control pipe (else exit after drawing image)"
59//usage: "\n commands: 'NN' (% for progress bar) or 'exit'"
60
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000061#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020062#include "common_bufsiz.h"
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000063#include <linux/fb.h>
64
Denis Vlasenko25a9c172008-03-26 15:12:11 +000065/* If you want logging messages on /tmp/fbsplash.log... */
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000066#define DEBUG 0
67
Denys Vlasenko8187e012017-09-13 22:48:30 +020068#define ESC "\033"
69
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000070struct globals {
71#if DEBUG
72 bool bdebug_messages; // enable/disable logging
73 FILE *logfile_fd; // log file
74#endif
75 unsigned char *addr; // pointer to framebuffer memory
Denis Vlasenko0f99d492008-07-24 23:38:04 +000076 unsigned ns[7]; // n-parameters
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000077 const char *image_filename;
78 struct fb_var_screeninfo scr_var;
79 struct fb_fix_screeninfo scr_fix;
Nuno Lucas0e89fd92011-03-28 15:30:59 +020080 unsigned bytes_per_pixel;
Linus Walleij6d463de2012-09-06 16:52:31 +020081 // cached (8 - scr_var.COLOR.length):
82 unsigned red_shift;
83 unsigned green_shift;
84 unsigned blue_shift;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000085};
86#define G (*ptr_to_globals)
Denis Vlasenko7049ff82008-06-25 09:53:17 +000087#define INIT_G() do { \
88 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
89} while (0)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000090
Denis Vlasenko0f99d492008-07-24 23:38:04 +000091#define nbar_width ns[0] // progress bar width
92#define nbar_height ns[1] // progress bar height
93#define nbar_posx ns[2] // progress bar horizontal position
94#define nbar_posy ns[3] // progress bar vertical position
95#define nbar_colr ns[4] // progress bar color red component
96#define nbar_colg ns[5] // progress bar color green component
97#define nbar_colb ns[6] // progress bar color blue component
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000098
99#if DEBUG
100#define DEBUG_MESSAGE(strMessage, args...) \
101 if (G.bdebug_messages) { \
102 fprintf(G.logfile_fd, "[%s][%s] - %s\n", \
103 __FILE__, __FUNCTION__, strMessage); \
104 }
105#else
106#define DEBUG_MESSAGE(...) ((void)0)
107#endif
108
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200109/**
110 * Configure palette for RGB:332
111 */
112static void fb_setpal(int fd)
113{
114 struct fb_cmap cmap;
115 /* fb colors are 16 bit */
116 unsigned short red[256], green[256], blue[256];
117 unsigned i;
118
119 /* RGB:332 */
120 for (i = 0; i < 256; i++) {
121 /* Color is encoded in pixel value as rrrgggbb.
122 * 3-bit color is mapped to 16-bit one as:
123 * 000 -> 00000000 00000000
124 * 001 -> 00100100 10010010
125 * ...
126 * 011 -> 01101101 10110110
127 * 100 -> 10010010 01001001
128 * ...
129 * 111 -> 11111111 11111111
130 */
131 red[i] = (( i >> 5 ) * 0x9249) >> 2; // rrr * 00 10010010 01001001 >> 2
132 green[i] = (((i >> 2) & 0x7) * 0x9249) >> 2; // ggg * 00 10010010 01001001 >> 2
133 /* 2-bit color is easier: */
134 blue[i] = ( i & 0x3) * 0x5555; // bb * 01010101 01010101
135 }
136
137 cmap.start = 0;
138 cmap.len = 256;
139 cmap.red = red;
140 cmap.green = green;
141 cmap.blue = blue;
142 cmap.transp = 0;
143
144 xioctl(fd, FBIOPUTCMAP, &cmap);
145}
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000146
147/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200148 * Open and initialize the framebuffer device
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000149 * \param *strfb_device pointer to framebuffer device
150 */
151static void fb_open(const char *strfb_device)
152{
153 int fbfd = xopen(strfb_device, O_RDWR);
154
155 // framebuffer properties
156 xioctl(fbfd, FBIOGET_VSCREENINFO, &G.scr_var);
157 xioctl(fbfd, FBIOGET_FSCREENINFO, &G.scr_fix);
158
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200159 switch (G.scr_var.bits_per_pixel) {
160 case 8:
161 fb_setpal(fbfd);
162 break;
163
164 case 16:
165 case 24:
166 case 32:
167 break;
168
169 default:
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200170 bb_error_msg_and_die("unsupported %u bpp", (int)G.scr_var.bits_per_pixel);
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200171 break;
172 }
173
Linus Walleij6d463de2012-09-06 16:52:31 +0200174 G.red_shift = 8 - G.scr_var.red.length;
175 G.green_shift = 8 - G.scr_var.green.length;
176 G.blue_shift = 8 - G.scr_var.blue.length;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200177 G.bytes_per_pixel = (G.scr_var.bits_per_pixel + 7) >> 3;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000178
179 // map the device in memory
180 G.addr = mmap(NULL,
Timo Teräs82c2fad2015-10-26 09:40:31 +0200181 (G.scr_var.yres_virtual ?: G.scr_var.yres) * G.scr_fix.line_length,
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000182 PROT_WRITE, MAP_SHARED, fbfd, 0);
183 if (G.addr == MAP_FAILED)
Bernhard Reutner-Fischer4efcec92009-02-14 12:36:16 +0000184 bb_perror_msg_and_die("mmap");
Dan Fandrich07078c22011-01-26 11:30:34 -0800185
186 // point to the start of the visible screen
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200187 G.addr += G.scr_var.yoffset * G.scr_fix.line_length + G.scr_var.xoffset * G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000188 close(fbfd);
189}
190
191
192/**
Linus Walleij6d463de2012-09-06 16:52:31 +0200193 * Return pixel value of the passed RGB color.
194 * This is performance critical fn.
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200195 */
196static unsigned fb_pixel_value(unsigned r, unsigned g, unsigned b)
197{
Linus Walleij6d463de2012-09-06 16:52:31 +0200198 /* We assume that the r,g,b values are <= 255 */
199
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200200 if (G.bytes_per_pixel == 1) {
201 r = r & 0xe0; // 3-bit red
202 g = (g >> 3) & 0x1c; // 3-bit green
203 b = b >> 6; // 2-bit blue
204 return r + g + b;
205 }
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200206 if (G.bytes_per_pixel == 2) {
Linus Walleij6d463de2012-09-06 16:52:31 +0200207 // ARM PL110 on Integrator/CP has RGBA5551 bit arrangement.
208 // We want to support bit locations like that.
209 //
210 // First shift out unused bits
211 r = r >> G.red_shift;
212 g = g >> G.green_shift;
213 b = b >> G.blue_shift;
214 // Then shift the remaining bits to their offset
215 return (r << G.scr_var.red.offset) +
216 (g << G.scr_var.green.offset) +
217 (b << G.scr_var.blue.offset);
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200218 }
219 // RGB 888
220 return b + (g << 8) + (r << 16);
221}
222
223/**
224 * Draw pixel on framebuffer
225 */
226static void fb_write_pixel(unsigned char *addr, unsigned pixel)
227{
228 switch (G.bytes_per_pixel) {
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200229 case 1:
230 *addr = pixel;
231 break;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200232 case 2:
233 *(uint16_t *)addr = pixel;
234 break;
235 case 4:
236 *(uint32_t *)addr = pixel;
237 break;
238 default: // 24 bits per pixel
239 addr[0] = pixel;
240 addr[1] = pixel >> 8;
241 addr[2] = pixel >> 16;
242 }
243}
244
245
246/**
247 * Draw hollow rectangle on framebuffer
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000248 */
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000249static void fb_drawrectangle(void)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000250{
251 int cnt;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200252 unsigned thispix;
253 unsigned char *ptr1, *ptr2;
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000254 unsigned char nred = G.nbar_colr/2;
255 unsigned char ngreen = G.nbar_colg/2;
256 unsigned char nblue = G.nbar_colb/2;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000257
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200258 thispix = fb_pixel_value(nred, ngreen, nblue);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000259
260 // horizontal lines
Yin Kangkai3ac066c2012-02-23 15:56:36 +0800261 ptr1 = G.addr + G.nbar_posy * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
262 ptr2 = G.addr + (G.nbar_posy + G.nbar_height - 1) * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000263 cnt = G.nbar_width - 1;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000264 do {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200265 fb_write_pixel(ptr1, thispix);
266 fb_write_pixel(ptr2, thispix);
267 ptr1 += G.bytes_per_pixel;
268 ptr2 += G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000269 } while (--cnt >= 0);
270
271 // vertical lines
Yin Kangkai3ac066c2012-02-23 15:56:36 +0800272 ptr1 = G.addr + G.nbar_posy * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
273 ptr2 = G.addr + G.nbar_posy * G.scr_fix.line_length + (G.nbar_posx + G.nbar_width - 1) * G.bytes_per_pixel;
Denys Vlasenko814da222010-03-14 23:59:54 +0100274 cnt = G.nbar_height - 1;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000275 do {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200276 fb_write_pixel(ptr1, thispix);
277 fb_write_pixel(ptr2, thispix);
Yin Kangkai3ac066c2012-02-23 15:56:36 +0800278 ptr1 += G.scr_fix.line_length;
279 ptr2 += G.scr_fix.line_length;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000280 } while (--cnt >= 0);
281}
282
283
284/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200285 * Draw filled rectangle on framebuffer
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000286 * \param nx1pos,ny1pos upper left position
287 * \param nx2pos,ny2pos down right position
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000288 * \param nred,ngreen,nblue rgb color
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000289 */
290static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
291 unsigned char nred, unsigned char ngreen, unsigned char nblue)
292{
293 int cnt1, cnt2, nypos;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200294 unsigned thispix;
295 unsigned char *ptr;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000296
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200297 thispix = fb_pixel_value(nred, ngreen, nblue);
Denis Vlasenko1f228982008-04-22 00:16:29 +0000298
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000299 cnt1 = ny2pos - ny1pos;
300 nypos = ny1pos;
301 do {
Yin Kangkai3ac066c2012-02-23 15:56:36 +0800302 ptr = G.addr + nypos * G.scr_fix.line_length + nx1pos * G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000303 cnt2 = nx2pos - nx1pos;
304 do {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200305 fb_write_pixel(ptr, thispix);
306 ptr += G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000307 } while (--cnt2 >= 0);
Denis Vlasenko1f228982008-04-22 00:16:29 +0000308
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000309 nypos++;
310 } while (--cnt1 >= 0);
311}
312
313
314/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200315 * Draw a progress bar on framebuffer
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000316 * \param percent percentage of loading
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000317 */
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000318static void fb_drawprogressbar(unsigned percent)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000319{
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200320 int left_x, top_y, pos_x;
321 unsigned width, height;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000322
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000323 // outer box
324 left_x = G.nbar_posx;
325 top_y = G.nbar_posy;
326 width = G.nbar_width - 1;
327 height = G.nbar_height - 1;
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200328 if ((int)(height | width) < 0)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000329 return;
330 // NB: "width" of 1 actually makes rect with width of 2!
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000331 fb_drawrectangle();
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000332
333 // inner "empty" rectangle
334 left_x++;
335 top_y++;
336 width -= 2;
337 height -= 2;
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200338 if ((int)(height | width) < 0)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000339 return;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000340
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200341 pos_x = left_x;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000342 if (percent > 0) {
Timo Teräs67dc7b22012-10-17 19:39:34 +0200343 int i, y;
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200344
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000345 // actual progress bar
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200346 pos_x += (unsigned)(width * percent) / 100;
347
348 y = top_y;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000349 i = height;
350 if (height == 0)
351 height++; // divide by 0 is bad
352 while (i >= 0) {
353 // draw one-line thick "rectangle"
354 // top line will have gray lvl 200, bottom one 100
Timo Teräs67dc7b22012-10-17 19:39:34 +0200355 unsigned gray_level = 100 + (unsigned)i*100 / height;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000356 fb_drawfullrectangle(
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200357 left_x, y, pos_x, y,
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000358 gray_level, gray_level, gray_level);
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200359 y++;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000360 i--;
361 }
362 }
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200363
364 fb_drawfullrectangle(
365 pos_x, top_y,
366 left_x + width, top_y + height,
367 G.nbar_colr, G.nbar_colg, G.nbar_colb);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000368}
369
370
371/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200372 * Draw image from PPM file
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000373 */
374static void fb_drawimage(void)
375{
Bernhard Reutner-Fischer051fdb92009-02-16 12:36:50 +0000376 FILE *theme_file;
Denys Vlasenko814da222010-03-14 23:59:54 +0100377 char *read_ptr;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000378 unsigned char *pixline;
379 unsigned i, j, width, height, line_size;
380
Denys Vlasenko814da222010-03-14 23:59:54 +0100381 if (LONE_DASH(G.image_filename)) {
Vladimir Dronnikov6eaac022009-11-07 04:14:50 +0100382 theme_file = stdin;
Denys Vlasenko814da222010-03-14 23:59:54 +0100383 } else {
Denys Vlasenko640ce3d2014-02-02 02:06:38 +0100384 int fd = open_zipped(G.image_filename, /*fail_if_not_compressed:*/ 0);
Vladimir Dronnikov6eaac022009-11-07 04:14:50 +0100385 if (fd < 0)
386 bb_simple_perror_msg_and_die(G.image_filename);
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +0100387 theme_file = xfdopen_for_read(fd);
Vladimir Dronnikov6eaac022009-11-07 04:14:50 +0100388 }
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000389
Denys Vlasenko814da222010-03-14 23:59:54 +0100390 /* Parse ppm header:
391 * - Magic: two characters "P6".
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000392 * - Whitespace (blanks, TABs, CRs, LFs).
393 * - A width, formatted as ASCII characters in decimal.
394 * - Whitespace.
Denys Vlasenko814da222010-03-14 23:59:54 +0100395 * - A height, ASCII decimal.
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000396 * - Whitespace.
Denys Vlasenko814da222010-03-14 23:59:54 +0100397 * - The maximum color value, ASCII decimal, in 0..65535
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000398 * - Newline or other single whitespace character.
Denys Vlasenko814da222010-03-14 23:59:54 +0100399 * (we support newline only)
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000400 * - A raster of Width * Height pixels in triplets of rgb
Denys Vlasenko814da222010-03-14 23:59:54 +0100401 * in pure binary by 1 or 2 bytes. (we support only 1 byte)
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000402 */
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200403#define concat_buf bb_common_bufsiz1
404 setup_common_bufsiz();
405
Denys Vlasenko814da222010-03-14 23:59:54 +0100406 read_ptr = concat_buf;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000407 while (1) {
Denys Vlasenko814da222010-03-14 23:59:54 +0100408 int w, h, max_color_val;
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200409 int rem = concat_buf + COMMON_BUFSIZE - read_ptr;
Denys Vlasenko814da222010-03-14 23:59:54 +0100410 if (rem < 2
411 || fgets(read_ptr, rem, theme_file) == NULL
412 ) {
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000413 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
Denys Vlasenko814da222010-03-14 23:59:54 +0100414 }
415 read_ptr = strchrnul(read_ptr, '#');
416 *read_ptr = '\0'; /* ignore #comments */
417 if (sscanf(concat_buf, "P6 %u %u %u", &w, &h, &max_color_val) == 3
418 && max_color_val <= 255
419 ) {
420 width = w; /* w is on stack, width may be in register */
421 height = h;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000422 break;
Denys Vlasenko814da222010-03-14 23:59:54 +0100423 }
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000424 }
425
426 line_size = width*3;
Denys Vlasenko814da222010-03-14 23:59:54 +0100427 pixline = xmalloc(line_size);
428
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000429 if (width > G.scr_var.xres)
430 width = G.scr_var.xres;
431 if (height > G.scr_var.yres)
432 height = G.scr_var.yres;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000433 for (j = 0; j < height; j++) {
Denys Vlasenko814da222010-03-14 23:59:54 +0100434 unsigned char *pixel;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200435 unsigned char *src;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000436
Bernhard Reutner-Fischer051fdb92009-02-16 12:36:50 +0000437 if (fread(pixline, 1, line_size, theme_file) != line_size)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000438 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
Denys Vlasenko814da222010-03-14 23:59:54 +0100439 pixel = pixline;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200440 src = G.addr + j * G.scr_fix.line_length;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000441 for (i = 0; i < width; i++) {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200442 unsigned thispix = fb_pixel_value(pixel[0], pixel[1], pixel[2]);
443 fb_write_pixel(src, thispix);
444 src += G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000445 pixel += 3;
446 }
447 }
Denys Vlasenko814da222010-03-14 23:59:54 +0100448 free(pixline);
Bernhard Reutner-Fischer051fdb92009-02-16 12:36:50 +0000449 fclose(theme_file);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000450}
451
452
453/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200454 * Parse configuration file
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000455 * \param *cfg_filename name of the configuration file
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000456 */
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000457static void init(const char *cfg_filename)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000458{
Denys Vlasenko10544a82010-02-09 00:26:10 +0100459 static const char param_names[] ALIGN1 =
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000460 "BAR_WIDTH\0" "BAR_HEIGHT\0"
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000461 "BAR_LEFT\0" "BAR_TOP\0"
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000462 "BAR_R\0" "BAR_G\0" "BAR_B\0"
463#if DEBUG
464 "DEBUG\0"
465#endif
466 ;
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000467 char *token[2];
468 parser_t *parser = config_open2(cfg_filename, xfopen_stdin);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000469 while (config_read(parser, token, 2, 2, "#=",
Denys Vlasenko814da222010-03-14 23:59:54 +0100470 (PARSE_NORMAL | PARSE_MIN_DIE) & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
Denys Vlasenko77832482010-08-12 14:14:45 +0200471 unsigned val = xatoi_positive(token[1]);
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000472 int i = index_in_strings(param_names, token[0]);
473 if (i < 0)
Bernhard Reutner-Fischer4efcec92009-02-14 12:36:16 +0000474 bb_error_msg_and_die("syntax error: %s", token[0]);
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000475 if (i >= 0 && i < 7)
476 G.ns[i] = val;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000477#if DEBUG
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000478 if (i == 7) {
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000479 G.bdebug_messages = val;
480 if (G.bdebug_messages)
Denis Vlasenko5415c852008-07-21 23:05:26 +0000481 G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log");
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000482 }
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000483#endif
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000484 }
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000485 config_close(parser);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000486}
487
488
489int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000490int fbsplash_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000491{
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000492 const char *fb_device, *cfg_filename, *fifo_filename;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000493 FILE *fp = fp; // for compiler
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000494 char *num_buf;
495 unsigned num;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000496 bool bCursorOff;
497
498 INIT_G();
499
500 // parse command line options
501 fb_device = "/dev/fb0";
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000502 cfg_filename = NULL;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000503 fifo_filename = NULL;
504 bCursorOff = 1 & getopt32(argv, "cs:d:i:f:",
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000505 &G.image_filename, &fb_device, &cfg_filename, &fifo_filename);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000506
507 // parse configuration file
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000508 if (cfg_filename)
509 init(cfg_filename);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000510
511 // We must have -s IMG
512 if (!G.image_filename)
513 bb_show_usage();
514
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000515 fb_open(fb_device);
516
517 if (fifo_filename && bCursorOff) {
518 // hide cursor (BEFORE any fb ops)
Denys Vlasenko8187e012017-09-13 22:48:30 +0200519 full_write(STDOUT_FILENO, ESC"[?25l", 6);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000520 }
521
522 fb_drawimage();
523
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000524 if (!fifo_filename)
525 return EXIT_SUCCESS;
526
527 fp = xfopen_stdin(fifo_filename);
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000528 if (fp != stdin) {
Denis Vlasenko72b34422008-03-27 13:14:29 +0000529 // For named pipes, we want to support this:
530 // mkfifo cmd_pipe
531 // fbsplash -f cmd_pipe .... &
532 // ...
533 // echo 33 >cmd_pipe
534 // ...
535 // echo 66 >cmd_pipe
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000536 // This means that we don't want fbsplash to get EOF
537 // when last writer closes input end.
538 // The simplest way is to open fifo for writing too
539 // and become an additional writer :)
540 open(fifo_filename, O_WRONLY); // errors are ignored
541 }
542
543 fb_drawprogressbar(0);
544 // Block on read, waiting for some input.
545 // Use of <stdio.h> style I/O allows to correctly
546 // handle a case when we have many buffered lines
547 // already in the pipe
548 while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100549 if (is_prefixed_with(num_buf, "exit")) {
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000550 DEBUG_MESSAGE("exit");
551 break;
552 }
553 num = atoi(num_buf);
554 if (isdigit(num_buf[0]) && (num <= 100)) {
555#if DEBUG
Denys Vlasenko7bb346f2009-10-06 22:09:50 +0200556 DEBUG_MESSAGE(itoa(num));
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000557#endif
558 fb_drawprogressbar(num);
559 }
560 free(num_buf);
561 }
562
563 if (bCursorOff) // restore cursor
Denys Vlasenko8187e012017-09-13 22:48:30 +0200564 full_write(STDOUT_FILENO, ESC"[?25h", 6);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000565
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000566 return EXIT_SUCCESS;
567}