blob: 9ac91e01e1a81072cd80e619b38eae3638565eff [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
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000068struct globals {
69#if DEBUG
70 bool bdebug_messages; // enable/disable logging
71 FILE *logfile_fd; // log file
72#endif
73 unsigned char *addr; // pointer to framebuffer memory
Denis Vlasenko0f99d492008-07-24 23:38:04 +000074 unsigned ns[7]; // n-parameters
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000075 const char *image_filename;
76 struct fb_var_screeninfo scr_var;
77 struct fb_fix_screeninfo scr_fix;
Nuno Lucas0e89fd92011-03-28 15:30:59 +020078 unsigned bytes_per_pixel;
Linus Walleij6d463de2012-09-06 16:52:31 +020079 // cached (8 - scr_var.COLOR.length):
80 unsigned red_shift;
81 unsigned green_shift;
82 unsigned blue_shift;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000083};
84#define G (*ptr_to_globals)
Denis Vlasenko7049ff82008-06-25 09:53:17 +000085#define INIT_G() do { \
86 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
87} while (0)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000088
Denis Vlasenko0f99d492008-07-24 23:38:04 +000089#define nbar_width ns[0] // progress bar width
90#define nbar_height ns[1] // progress bar height
91#define nbar_posx ns[2] // progress bar horizontal position
92#define nbar_posy ns[3] // progress bar vertical position
93#define nbar_colr ns[4] // progress bar color red component
94#define nbar_colg ns[5] // progress bar color green component
95#define nbar_colb ns[6] // progress bar color blue component
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000096
97#if DEBUG
98#define DEBUG_MESSAGE(strMessage, args...) \
99 if (G.bdebug_messages) { \
100 fprintf(G.logfile_fd, "[%s][%s] - %s\n", \
101 __FILE__, __FUNCTION__, strMessage); \
102 }
103#else
104#define DEBUG_MESSAGE(...) ((void)0)
105#endif
106
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200107/**
108 * Configure palette for RGB:332
109 */
110static void fb_setpal(int fd)
111{
112 struct fb_cmap cmap;
113 /* fb colors are 16 bit */
114 unsigned short red[256], green[256], blue[256];
115 unsigned i;
116
117 /* RGB:332 */
118 for (i = 0; i < 256; i++) {
119 /* Color is encoded in pixel value as rrrgggbb.
120 * 3-bit color is mapped to 16-bit one as:
121 * 000 -> 00000000 00000000
122 * 001 -> 00100100 10010010
123 * ...
124 * 011 -> 01101101 10110110
125 * 100 -> 10010010 01001001
126 * ...
127 * 111 -> 11111111 11111111
128 */
129 red[i] = (( i >> 5 ) * 0x9249) >> 2; // rrr * 00 10010010 01001001 >> 2
130 green[i] = (((i >> 2) & 0x7) * 0x9249) >> 2; // ggg * 00 10010010 01001001 >> 2
131 /* 2-bit color is easier: */
132 blue[i] = ( i & 0x3) * 0x5555; // bb * 01010101 01010101
133 }
134
135 cmap.start = 0;
136 cmap.len = 256;
137 cmap.red = red;
138 cmap.green = green;
139 cmap.blue = blue;
140 cmap.transp = 0;
141
142 xioctl(fd, FBIOPUTCMAP, &cmap);
143}
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000144
145/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200146 * Open and initialize the framebuffer device
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000147 * \param *strfb_device pointer to framebuffer device
148 */
149static void fb_open(const char *strfb_device)
150{
151 int fbfd = xopen(strfb_device, O_RDWR);
152
153 // framebuffer properties
154 xioctl(fbfd, FBIOGET_VSCREENINFO, &G.scr_var);
155 xioctl(fbfd, FBIOGET_FSCREENINFO, &G.scr_fix);
156
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200157 switch (G.scr_var.bits_per_pixel) {
158 case 8:
159 fb_setpal(fbfd);
160 break;
161
162 case 16:
163 case 24:
164 case 32:
165 break;
166
167 default:
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200168 bb_error_msg_and_die("unsupported %u bpp", (int)G.scr_var.bits_per_pixel);
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200169 break;
170 }
171
Linus Walleij6d463de2012-09-06 16:52:31 +0200172 G.red_shift = 8 - G.scr_var.red.length;
173 G.green_shift = 8 - G.scr_var.green.length;
174 G.blue_shift = 8 - G.scr_var.blue.length;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200175 G.bytes_per_pixel = (G.scr_var.bits_per_pixel + 7) >> 3;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000176
177 // map the device in memory
178 G.addr = mmap(NULL,
Timo Teräs82c2fad2015-10-26 09:40:31 +0200179 (G.scr_var.yres_virtual ?: G.scr_var.yres) * G.scr_fix.line_length,
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000180 PROT_WRITE, MAP_SHARED, fbfd, 0);
181 if (G.addr == MAP_FAILED)
Bernhard Reutner-Fischer4efcec92009-02-14 12:36:16 +0000182 bb_perror_msg_and_die("mmap");
Dan Fandrich07078c22011-01-26 11:30:34 -0800183
184 // point to the start of the visible screen
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200185 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 +0000186 close(fbfd);
187}
188
189
190/**
Linus Walleij6d463de2012-09-06 16:52:31 +0200191 * Return pixel value of the passed RGB color.
192 * This is performance critical fn.
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200193 */
194static unsigned fb_pixel_value(unsigned r, unsigned g, unsigned b)
195{
Linus Walleij6d463de2012-09-06 16:52:31 +0200196 /* We assume that the r,g,b values are <= 255 */
197
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200198 if (G.bytes_per_pixel == 1) {
199 r = r & 0xe0; // 3-bit red
200 g = (g >> 3) & 0x1c; // 3-bit green
201 b = b >> 6; // 2-bit blue
202 return r + g + b;
203 }
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200204 if (G.bytes_per_pixel == 2) {
Linus Walleij6d463de2012-09-06 16:52:31 +0200205 // ARM PL110 on Integrator/CP has RGBA5551 bit arrangement.
206 // We want to support bit locations like that.
207 //
208 // First shift out unused bits
209 r = r >> G.red_shift;
210 g = g >> G.green_shift;
211 b = b >> G.blue_shift;
212 // Then shift the remaining bits to their offset
213 return (r << G.scr_var.red.offset) +
214 (g << G.scr_var.green.offset) +
215 (b << G.scr_var.blue.offset);
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200216 }
217 // RGB 888
218 return b + (g << 8) + (r << 16);
219}
220
221/**
222 * Draw pixel on framebuffer
223 */
224static void fb_write_pixel(unsigned char *addr, unsigned pixel)
225{
226 switch (G.bytes_per_pixel) {
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200227 case 1:
228 *addr = pixel;
229 break;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200230 case 2:
231 *(uint16_t *)addr = pixel;
232 break;
233 case 4:
234 *(uint32_t *)addr = pixel;
235 break;
236 default: // 24 bits per pixel
237 addr[0] = pixel;
238 addr[1] = pixel >> 8;
239 addr[2] = pixel >> 16;
240 }
241}
242
243
244/**
245 * Draw hollow rectangle on framebuffer
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000246 */
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000247static void fb_drawrectangle(void)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000248{
249 int cnt;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200250 unsigned thispix;
251 unsigned char *ptr1, *ptr2;
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000252 unsigned char nred = G.nbar_colr/2;
253 unsigned char ngreen = G.nbar_colg/2;
254 unsigned char nblue = G.nbar_colb/2;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000255
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200256 thispix = fb_pixel_value(nred, ngreen, nblue);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000257
258 // horizontal lines
Yin Kangkai3ac066c2012-02-23 15:56:36 +0800259 ptr1 = G.addr + G.nbar_posy * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
260 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 +0000261 cnt = G.nbar_width - 1;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000262 do {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200263 fb_write_pixel(ptr1, thispix);
264 fb_write_pixel(ptr2, thispix);
265 ptr1 += G.bytes_per_pixel;
266 ptr2 += G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000267 } while (--cnt >= 0);
268
269 // vertical lines
Yin Kangkai3ac066c2012-02-23 15:56:36 +0800270 ptr1 = G.addr + G.nbar_posy * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
271 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 +0100272 cnt = G.nbar_height - 1;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000273 do {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200274 fb_write_pixel(ptr1, thispix);
275 fb_write_pixel(ptr2, thispix);
Yin Kangkai3ac066c2012-02-23 15:56:36 +0800276 ptr1 += G.scr_fix.line_length;
277 ptr2 += G.scr_fix.line_length;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000278 } while (--cnt >= 0);
279}
280
281
282/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200283 * Draw filled rectangle on framebuffer
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000284 * \param nx1pos,ny1pos upper left position
285 * \param nx2pos,ny2pos down right position
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000286 * \param nred,ngreen,nblue rgb color
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000287 */
288static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
289 unsigned char nred, unsigned char ngreen, unsigned char nblue)
290{
291 int cnt1, cnt2, nypos;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200292 unsigned thispix;
293 unsigned char *ptr;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000294
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200295 thispix = fb_pixel_value(nred, ngreen, nblue);
Denis Vlasenko1f228982008-04-22 00:16:29 +0000296
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000297 cnt1 = ny2pos - ny1pos;
298 nypos = ny1pos;
299 do {
Yin Kangkai3ac066c2012-02-23 15:56:36 +0800300 ptr = G.addr + nypos * G.scr_fix.line_length + nx1pos * G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000301 cnt2 = nx2pos - nx1pos;
302 do {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200303 fb_write_pixel(ptr, thispix);
304 ptr += G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000305 } while (--cnt2 >= 0);
Denis Vlasenko1f228982008-04-22 00:16:29 +0000306
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000307 nypos++;
308 } while (--cnt1 >= 0);
309}
310
311
312/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200313 * Draw a progress bar on framebuffer
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000314 * \param percent percentage of loading
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000315 */
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000316static void fb_drawprogressbar(unsigned percent)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000317{
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200318 int left_x, top_y, pos_x;
319 unsigned width, height;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000320
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000321 // outer box
322 left_x = G.nbar_posx;
323 top_y = G.nbar_posy;
324 width = G.nbar_width - 1;
325 height = G.nbar_height - 1;
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200326 if ((int)(height | width) < 0)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000327 return;
328 // NB: "width" of 1 actually makes rect with width of 2!
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000329 fb_drawrectangle();
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000330
331 // inner "empty" rectangle
332 left_x++;
333 top_y++;
334 width -= 2;
335 height -= 2;
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200336 if ((int)(height | width) < 0)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000337 return;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000338
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200339 pos_x = left_x;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000340 if (percent > 0) {
Timo Teräs67dc7b22012-10-17 19:39:34 +0200341 int i, y;
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200342
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000343 // actual progress bar
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200344 pos_x += (unsigned)(width * percent) / 100;
345
346 y = top_y;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000347 i = height;
348 if (height == 0)
349 height++; // divide by 0 is bad
350 while (i >= 0) {
351 // draw one-line thick "rectangle"
352 // top line will have gray lvl 200, bottom one 100
Timo Teräs67dc7b22012-10-17 19:39:34 +0200353 unsigned gray_level = 100 + (unsigned)i*100 / height;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000354 fb_drawfullrectangle(
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200355 left_x, y, pos_x, y,
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000356 gray_level, gray_level, gray_level);
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200357 y++;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000358 i--;
359 }
360 }
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200361
362 fb_drawfullrectangle(
363 pos_x, top_y,
364 left_x + width, top_y + height,
365 G.nbar_colr, G.nbar_colg, G.nbar_colb);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000366}
367
368
369/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200370 * Draw image from PPM file
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000371 */
372static void fb_drawimage(void)
373{
Bernhard Reutner-Fischer051fdb92009-02-16 12:36:50 +0000374 FILE *theme_file;
Denys Vlasenko814da222010-03-14 23:59:54 +0100375 char *read_ptr;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000376 unsigned char *pixline;
377 unsigned i, j, width, height, line_size;
378
Denys Vlasenko814da222010-03-14 23:59:54 +0100379 if (LONE_DASH(G.image_filename)) {
Vladimir Dronnikov6eaac022009-11-07 04:14:50 +0100380 theme_file = stdin;
Denys Vlasenko814da222010-03-14 23:59:54 +0100381 } else {
Denys Vlasenko640ce3d2014-02-02 02:06:38 +0100382 int fd = open_zipped(G.image_filename, /*fail_if_not_compressed:*/ 0);
Vladimir Dronnikov6eaac022009-11-07 04:14:50 +0100383 if (fd < 0)
384 bb_simple_perror_msg_and_die(G.image_filename);
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +0100385 theme_file = xfdopen_for_read(fd);
Vladimir Dronnikov6eaac022009-11-07 04:14:50 +0100386 }
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000387
Denys Vlasenko814da222010-03-14 23:59:54 +0100388 /* Parse ppm header:
389 * - Magic: two characters "P6".
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000390 * - Whitespace (blanks, TABs, CRs, LFs).
391 * - A width, formatted as ASCII characters in decimal.
392 * - Whitespace.
Denys Vlasenko814da222010-03-14 23:59:54 +0100393 * - A height, ASCII decimal.
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000394 * - Whitespace.
Denys Vlasenko814da222010-03-14 23:59:54 +0100395 * - The maximum color value, ASCII decimal, in 0..65535
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000396 * - Newline or other single whitespace character.
Denys Vlasenko814da222010-03-14 23:59:54 +0100397 * (we support newline only)
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000398 * - A raster of Width * Height pixels in triplets of rgb
Denys Vlasenko814da222010-03-14 23:59:54 +0100399 * in pure binary by 1 or 2 bytes. (we support only 1 byte)
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000400 */
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200401#define concat_buf bb_common_bufsiz1
402 setup_common_bufsiz();
403
Denys Vlasenko814da222010-03-14 23:59:54 +0100404 read_ptr = concat_buf;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000405 while (1) {
Denys Vlasenko814da222010-03-14 23:59:54 +0100406 int w, h, max_color_val;
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200407 int rem = concat_buf + COMMON_BUFSIZE - read_ptr;
Denys Vlasenko814da222010-03-14 23:59:54 +0100408 if (rem < 2
409 || fgets(read_ptr, rem, theme_file) == NULL
410 ) {
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000411 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
Denys Vlasenko814da222010-03-14 23:59:54 +0100412 }
413 read_ptr = strchrnul(read_ptr, '#');
414 *read_ptr = '\0'; /* ignore #comments */
415 if (sscanf(concat_buf, "P6 %u %u %u", &w, &h, &max_color_val) == 3
416 && max_color_val <= 255
417 ) {
418 width = w; /* w is on stack, width may be in register */
419 height = h;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000420 break;
Denys Vlasenko814da222010-03-14 23:59:54 +0100421 }
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000422 }
423
424 line_size = width*3;
Denys Vlasenko814da222010-03-14 23:59:54 +0100425 pixline = xmalloc(line_size);
426
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000427 if (width > G.scr_var.xres)
428 width = G.scr_var.xres;
429 if (height > G.scr_var.yres)
430 height = G.scr_var.yres;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000431 for (j = 0; j < height; j++) {
Denys Vlasenko814da222010-03-14 23:59:54 +0100432 unsigned char *pixel;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200433 unsigned char *src;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000434
Bernhard Reutner-Fischer051fdb92009-02-16 12:36:50 +0000435 if (fread(pixline, 1, line_size, theme_file) != line_size)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000436 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
Denys Vlasenko814da222010-03-14 23:59:54 +0100437 pixel = pixline;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200438 src = G.addr + j * G.scr_fix.line_length;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000439 for (i = 0; i < width; i++) {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200440 unsigned thispix = fb_pixel_value(pixel[0], pixel[1], pixel[2]);
441 fb_write_pixel(src, thispix);
442 src += G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000443 pixel += 3;
444 }
445 }
Denys Vlasenko814da222010-03-14 23:59:54 +0100446 free(pixline);
Bernhard Reutner-Fischer051fdb92009-02-16 12:36:50 +0000447 fclose(theme_file);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000448}
449
450
451/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200452 * Parse configuration file
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000453 * \param *cfg_filename name of the configuration file
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000454 */
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000455static void init(const char *cfg_filename)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000456{
Denys Vlasenko10544a82010-02-09 00:26:10 +0100457 static const char param_names[] ALIGN1 =
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000458 "BAR_WIDTH\0" "BAR_HEIGHT\0"
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000459 "BAR_LEFT\0" "BAR_TOP\0"
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000460 "BAR_R\0" "BAR_G\0" "BAR_B\0"
461#if DEBUG
462 "DEBUG\0"
463#endif
464 ;
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000465 char *token[2];
466 parser_t *parser = config_open2(cfg_filename, xfopen_stdin);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000467 while (config_read(parser, token, 2, 2, "#=",
Denys Vlasenko814da222010-03-14 23:59:54 +0100468 (PARSE_NORMAL | PARSE_MIN_DIE) & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
Denys Vlasenko77832482010-08-12 14:14:45 +0200469 unsigned val = xatoi_positive(token[1]);
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000470 int i = index_in_strings(param_names, token[0]);
471 if (i < 0)
Bernhard Reutner-Fischer4efcec92009-02-14 12:36:16 +0000472 bb_error_msg_and_die("syntax error: %s", token[0]);
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000473 if (i >= 0 && i < 7)
474 G.ns[i] = val;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000475#if DEBUG
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000476 if (i == 7) {
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000477 G.bdebug_messages = val;
478 if (G.bdebug_messages)
Denis Vlasenko5415c852008-07-21 23:05:26 +0000479 G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log");
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000480 }
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000481#endif
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000482 }
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000483 config_close(parser);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000484}
485
486
487int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000488int fbsplash_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000489{
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000490 const char *fb_device, *cfg_filename, *fifo_filename;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000491 FILE *fp = fp; // for compiler
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000492 char *num_buf;
493 unsigned num;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000494 bool bCursorOff;
495
496 INIT_G();
497
498 // parse command line options
499 fb_device = "/dev/fb0";
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000500 cfg_filename = NULL;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000501 fifo_filename = NULL;
502 bCursorOff = 1 & getopt32(argv, "cs:d:i:f:",
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000503 &G.image_filename, &fb_device, &cfg_filename, &fifo_filename);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000504
505 // parse configuration file
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000506 if (cfg_filename)
507 init(cfg_filename);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000508
509 // We must have -s IMG
510 if (!G.image_filename)
511 bb_show_usage();
512
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000513 fb_open(fb_device);
514
515 if (fifo_filename && bCursorOff) {
516 // hide cursor (BEFORE any fb ops)
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200517 full_write(STDOUT_FILENO, "\033[?25l", 6);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000518 }
519
520 fb_drawimage();
521
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000522 if (!fifo_filename)
523 return EXIT_SUCCESS;
524
525 fp = xfopen_stdin(fifo_filename);
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000526 if (fp != stdin) {
Denis Vlasenko72b34422008-03-27 13:14:29 +0000527 // For named pipes, we want to support this:
528 // mkfifo cmd_pipe
529 // fbsplash -f cmd_pipe .... &
530 // ...
531 // echo 33 >cmd_pipe
532 // ...
533 // echo 66 >cmd_pipe
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000534 // This means that we don't want fbsplash to get EOF
535 // when last writer closes input end.
536 // The simplest way is to open fifo for writing too
537 // and become an additional writer :)
538 open(fifo_filename, O_WRONLY); // errors are ignored
539 }
540
541 fb_drawprogressbar(0);
542 // Block on read, waiting for some input.
543 // Use of <stdio.h> style I/O allows to correctly
544 // handle a case when we have many buffered lines
545 // already in the pipe
546 while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100547 if (is_prefixed_with(num_buf, "exit")) {
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000548 DEBUG_MESSAGE("exit");
549 break;
550 }
551 num = atoi(num_buf);
552 if (isdigit(num_buf[0]) && (num <= 100)) {
553#if DEBUG
Denys Vlasenko7bb346f2009-10-06 22:09:50 +0200554 DEBUG_MESSAGE(itoa(num));
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000555#endif
556 fb_drawprogressbar(num);
557 }
558 free(num_buf);
559 }
560
561 if (bCursorOff) // restore cursor
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200562 full_write(STDOUT_FILENO, "\033[?25h", 6);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000563
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000564 return EXIT_SUCCESS;
565}