blob: 2934d8eb7bd2c8a408823270280aba9096b65266 [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 Vlasenkob097a842018-12-28 03:20:17 +010024//config: bool "fbsplash (26 kb)"
Denys Vlasenkofb4da162016-11-22 23:14:24 +010025//config: default y
Denys Vlasenkofb4da162016-11-22 23:14:24 +010026//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020027//config: Shows splash image and progress bar on framebuffer device.
28//config: Can be used during boot phase of an embedded device.
29//config: Usage:
30//config: - use kernel option 'vga=xxx' or otherwise enable fb device.
31//config: - put somewhere fbsplash.cfg file and an image in .ppm format.
32//config: - $ setsid fbsplash [params] &
Denys Vlasenkofb4da162016-11-22 23:14:24 +010033//config: -c: hide cursor
34//config: -d /dev/fbN: framebuffer device (if not /dev/fb0)
35//config: -s path_to_image_file (can be "-" for stdin)
36//config: -i path_to_cfg_file (can be "-" for stdin)
37//config: -f path_to_fifo (can be "-" for stdin)
Denys Vlasenko72089cf2017-07-21 09:50:55 +020038//config: - if you want to run it only in presence of kernel parameter:
Denys Vlasenkofb4da162016-11-22 23:14:24 +010039//config: grep -q "fbsplash=on" </proc/cmdline && setsid fbsplash [params] &
Denys Vlasenko72089cf2017-07-21 09:50:55 +020040//config: - commands for fifo:
Denys Vlasenkofb4da162016-11-22 23:14:24 +010041//config: "NN" (ASCII decimal number) - percentage to show on progress bar
42//config: "exit" - well you guessed it
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000043
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010044//applet:IF_FBSPLASH(APPLET(fbsplash, BB_DIR_SBIN, BB_SUID_DROP))
45
46//kbuild:lib-$(CONFIG_FBSPLASH) += fbsplash.o
47
Pere Orga5bc8c002011-04-11 03:29:49 +020048//usage:#define fbsplash_trivial_usage
49//usage: "-s IMGFILE [-c] [-d DEV] [-i INIFILE] [-f CMD]"
50//usage:#define fbsplash_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020051//usage: " -s Image"
Pere Orga5bc8c002011-04-11 03:29:49 +020052//usage: "\n -c Hide cursor"
53//usage: "\n -d Framebuffer device (default /dev/fb0)"
54//usage: "\n -i Config file (var=value):"
55//usage: "\n BAR_LEFT,BAR_TOP,BAR_WIDTH,BAR_HEIGHT"
Peter Korsgaarda82fe672018-03-29 13:35:01 +020056//usage: "\n BAR_R,BAR_G,BAR_B,IMG_LEFT,IMG_TOP"
Pere Orga5bc8c002011-04-11 03:29:49 +020057//usage: "\n -f Control pipe (else exit after drawing image)"
58//usage: "\n commands: 'NN' (% for progress bar) or 'exit'"
59
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000060#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020061#include "common_bufsiz.h"
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000062#include <linux/fb.h>
63
Denis Vlasenko25a9c172008-03-26 15:12:11 +000064/* If you want logging messages on /tmp/fbsplash.log... */
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000065#define DEBUG 0
66
Denys Vlasenko8187e012017-09-13 22:48:30 +020067#define ESC "\033"
68
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000069struct globals {
70#if DEBUG
71 bool bdebug_messages; // enable/disable logging
72 FILE *logfile_fd; // log file
73#endif
74 unsigned char *addr; // pointer to framebuffer memory
Peter Korsgaarda82fe672018-03-29 13:35:01 +020075 unsigned ns[9]; // n-parameters
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000076 const char *image_filename;
77 struct fb_var_screeninfo scr_var;
78 struct fb_fix_screeninfo scr_fix;
Nuno Lucas0e89fd92011-03-28 15:30:59 +020079 unsigned bytes_per_pixel;
Linus Walleij6d463de2012-09-06 16:52:31 +020080 // cached (8 - scr_var.COLOR.length):
81 unsigned red_shift;
82 unsigned green_shift;
83 unsigned blue_shift;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000084};
85#define G (*ptr_to_globals)
Denis Vlasenko7049ff82008-06-25 09:53:17 +000086#define INIT_G() do { \
87 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
88} while (0)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000089
Denis Vlasenko0f99d492008-07-24 23:38:04 +000090#define nbar_width ns[0] // progress bar width
91#define nbar_height ns[1] // progress bar height
92#define nbar_posx ns[2] // progress bar horizontal position
93#define nbar_posy ns[3] // progress bar vertical position
94#define nbar_colr ns[4] // progress bar color red component
95#define nbar_colg ns[5] // progress bar color green component
96#define nbar_colb ns[6] // progress bar color blue component
Peter Korsgaarda82fe672018-03-29 13:35:01 +020097#define img_posx ns[7] // image horizontal position
98#define img_posy ns[8] // image vertical position
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000099
100#if DEBUG
101#define DEBUG_MESSAGE(strMessage, args...) \
102 if (G.bdebug_messages) { \
103 fprintf(G.logfile_fd, "[%s][%s] - %s\n", \
104 __FILE__, __FUNCTION__, strMessage); \
105 }
106#else
107#define DEBUG_MESSAGE(...) ((void)0)
108#endif
109
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200110/**
111 * Configure palette for RGB:332
112 */
113static void fb_setpal(int fd)
114{
115 struct fb_cmap cmap;
116 /* fb colors are 16 bit */
117 unsigned short red[256], green[256], blue[256];
118 unsigned i;
119
120 /* RGB:332 */
121 for (i = 0; i < 256; i++) {
122 /* Color is encoded in pixel value as rrrgggbb.
123 * 3-bit color is mapped to 16-bit one as:
124 * 000 -> 00000000 00000000
125 * 001 -> 00100100 10010010
126 * ...
127 * 011 -> 01101101 10110110
128 * 100 -> 10010010 01001001
129 * ...
130 * 111 -> 11111111 11111111
131 */
132 red[i] = (( i >> 5 ) * 0x9249) >> 2; // rrr * 00 10010010 01001001 >> 2
133 green[i] = (((i >> 2) & 0x7) * 0x9249) >> 2; // ggg * 00 10010010 01001001 >> 2
134 /* 2-bit color is easier: */
135 blue[i] = ( i & 0x3) * 0x5555; // bb * 01010101 01010101
136 }
137
138 cmap.start = 0;
139 cmap.len = 256;
140 cmap.red = red;
141 cmap.green = green;
142 cmap.blue = blue;
143 cmap.transp = 0;
144
145 xioctl(fd, FBIOPUTCMAP, &cmap);
146}
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000147
148/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200149 * Open and initialize the framebuffer device
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000150 * \param *strfb_device pointer to framebuffer device
151 */
152static void fb_open(const char *strfb_device)
153{
154 int fbfd = xopen(strfb_device, O_RDWR);
155
156 // framebuffer properties
157 xioctl(fbfd, FBIOGET_VSCREENINFO, &G.scr_var);
158 xioctl(fbfd, FBIOGET_FSCREENINFO, &G.scr_fix);
159
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200160 switch (G.scr_var.bits_per_pixel) {
161 case 8:
162 fb_setpal(fbfd);
163 break;
164
165 case 16:
166 case 24:
167 case 32:
168 break;
169
170 default:
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200171 bb_error_msg_and_die("unsupported %u bpp", (int)G.scr_var.bits_per_pixel);
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200172 break;
173 }
174
Linus Walleij6d463de2012-09-06 16:52:31 +0200175 G.red_shift = 8 - G.scr_var.red.length;
176 G.green_shift = 8 - G.scr_var.green.length;
177 G.blue_shift = 8 - G.scr_var.blue.length;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200178 G.bytes_per_pixel = (G.scr_var.bits_per_pixel + 7) >> 3;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000179
180 // map the device in memory
181 G.addr = mmap(NULL,
Timo Teräs82c2fad2015-10-26 09:40:31 +0200182 (G.scr_var.yres_virtual ?: G.scr_var.yres) * G.scr_fix.line_length,
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000183 PROT_WRITE, MAP_SHARED, fbfd, 0);
184 if (G.addr == MAP_FAILED)
James Byrne69374872019-07-02 11:35:03 +0200185 bb_simple_perror_msg_and_die("mmap");
Dan Fandrich07078c22011-01-26 11:30:34 -0800186
187 // point to the start of the visible screen
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200188 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 +0000189 close(fbfd);
190}
191
192
193/**
Linus Walleij6d463de2012-09-06 16:52:31 +0200194 * Return pixel value of the passed RGB color.
195 * This is performance critical fn.
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200196 */
197static unsigned fb_pixel_value(unsigned r, unsigned g, unsigned b)
198{
Linus Walleij6d463de2012-09-06 16:52:31 +0200199 /* We assume that the r,g,b values are <= 255 */
200
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200201 if (G.bytes_per_pixel == 1) {
202 r = r & 0xe0; // 3-bit red
203 g = (g >> 3) & 0x1c; // 3-bit green
204 b = b >> 6; // 2-bit blue
205 return r + g + b;
206 }
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200207 if (G.bytes_per_pixel == 2) {
Linus Walleij6d463de2012-09-06 16:52:31 +0200208 // ARM PL110 on Integrator/CP has RGBA5551 bit arrangement.
209 // We want to support bit locations like that.
210 //
211 // First shift out unused bits
212 r = r >> G.red_shift;
213 g = g >> G.green_shift;
214 b = b >> G.blue_shift;
215 // Then shift the remaining bits to their offset
216 return (r << G.scr_var.red.offset) +
217 (g << G.scr_var.green.offset) +
218 (b << G.scr_var.blue.offset);
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200219 }
220 // RGB 888
221 return b + (g << 8) + (r << 16);
222}
223
224/**
225 * Draw pixel on framebuffer
226 */
227static void fb_write_pixel(unsigned char *addr, unsigned pixel)
228{
229 switch (G.bytes_per_pixel) {
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200230 case 1:
231 *addr = pixel;
232 break;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200233 case 2:
234 *(uint16_t *)addr = pixel;
235 break;
236 case 4:
237 *(uint32_t *)addr = pixel;
238 break;
239 default: // 24 bits per pixel
240 addr[0] = pixel;
241 addr[1] = pixel >> 8;
242 addr[2] = pixel >> 16;
243 }
244}
245
246
247/**
248 * Draw hollow rectangle on framebuffer
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000249 */
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000250static void fb_drawrectangle(void)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000251{
252 int cnt;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200253 unsigned thispix;
254 unsigned char *ptr1, *ptr2;
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000255 unsigned char nred = G.nbar_colr/2;
256 unsigned char ngreen = G.nbar_colg/2;
257 unsigned char nblue = G.nbar_colb/2;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000258
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200259 thispix = fb_pixel_value(nred, ngreen, nblue);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000260
261 // horizontal lines
Yin Kangkai3ac066c2012-02-23 15:56:36 +0800262 ptr1 = G.addr + G.nbar_posy * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
263 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 +0000264 cnt = G.nbar_width - 1;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000265 do {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200266 fb_write_pixel(ptr1, thispix);
267 fb_write_pixel(ptr2, thispix);
268 ptr1 += G.bytes_per_pixel;
269 ptr2 += G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000270 } while (--cnt >= 0);
271
272 // vertical lines
Yin Kangkai3ac066c2012-02-23 15:56:36 +0800273 ptr1 = G.addr + G.nbar_posy * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
274 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 +0100275 cnt = G.nbar_height - 1;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000276 do {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200277 fb_write_pixel(ptr1, thispix);
278 fb_write_pixel(ptr2, thispix);
Yin Kangkai3ac066c2012-02-23 15:56:36 +0800279 ptr1 += G.scr_fix.line_length;
280 ptr2 += G.scr_fix.line_length;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000281 } while (--cnt >= 0);
282}
283
284
285/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200286 * Draw filled rectangle on framebuffer
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000287 * \param nx1pos,ny1pos upper left position
288 * \param nx2pos,ny2pos down right position
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000289 * \param nred,ngreen,nblue rgb color
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000290 */
291static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
292 unsigned char nred, unsigned char ngreen, unsigned char nblue)
293{
294 int cnt1, cnt2, nypos;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200295 unsigned thispix;
296 unsigned char *ptr;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000297
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200298 thispix = fb_pixel_value(nred, ngreen, nblue);
Denis Vlasenko1f228982008-04-22 00:16:29 +0000299
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000300 cnt1 = ny2pos - ny1pos;
301 nypos = ny1pos;
302 do {
Yin Kangkai3ac066c2012-02-23 15:56:36 +0800303 ptr = G.addr + nypos * G.scr_fix.line_length + nx1pos * G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000304 cnt2 = nx2pos - nx1pos;
305 do {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200306 fb_write_pixel(ptr, thispix);
307 ptr += G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000308 } while (--cnt2 >= 0);
Denis Vlasenko1f228982008-04-22 00:16:29 +0000309
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000310 nypos++;
311 } while (--cnt1 >= 0);
312}
313
314
315/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200316 * Draw a progress bar on framebuffer
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000317 * \param percent percentage of loading
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000318 */
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000319static void fb_drawprogressbar(unsigned percent)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000320{
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200321 int left_x, top_y, pos_x;
322 unsigned width, height;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000323
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000324 // outer box
325 left_x = G.nbar_posx;
326 top_y = G.nbar_posy;
327 width = G.nbar_width - 1;
328 height = G.nbar_height - 1;
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200329 if ((int)(height | width) < 0)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000330 return;
331 // NB: "width" of 1 actually makes rect with width of 2!
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000332 fb_drawrectangle();
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000333
334 // inner "empty" rectangle
335 left_x++;
336 top_y++;
337 width -= 2;
338 height -= 2;
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200339 if ((int)(height | width) < 0)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000340 return;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000341
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200342 pos_x = left_x;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000343 if (percent > 0) {
Timo Teräs67dc7b22012-10-17 19:39:34 +0200344 int i, y;
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200345
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000346 // actual progress bar
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200347 pos_x += (unsigned)(width * percent) / 100;
348
349 y = top_y;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000350 i = height;
351 if (height == 0)
352 height++; // divide by 0 is bad
353 while (i >= 0) {
354 // draw one-line thick "rectangle"
355 // top line will have gray lvl 200, bottom one 100
Timo Teräs67dc7b22012-10-17 19:39:34 +0200356 unsigned gray_level = 100 + (unsigned)i*100 / height;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000357 fb_drawfullrectangle(
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200358 left_x, y, pos_x, y,
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000359 gray_level, gray_level, gray_level);
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200360 y++;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000361 i--;
362 }
363 }
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200364
365 fb_drawfullrectangle(
366 pos_x, top_y,
367 left_x + width, top_y + height,
368 G.nbar_colr, G.nbar_colg, G.nbar_colb);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000369}
370
371
372/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200373 * Draw image from PPM file
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000374 */
375static void fb_drawimage(void)
376{
Bernhard Reutner-Fischer051fdb92009-02-16 12:36:50 +0000377 FILE *theme_file;
Denys Vlasenko814da222010-03-14 23:59:54 +0100378 char *read_ptr;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000379 unsigned char *pixline;
380 unsigned i, j, width, height, line_size;
381
Denys Vlasenko814da222010-03-14 23:59:54 +0100382 if (LONE_DASH(G.image_filename)) {
Vladimir Dronnikov6eaac022009-11-07 04:14:50 +0100383 theme_file = stdin;
Denys Vlasenko814da222010-03-14 23:59:54 +0100384 } else {
Denys Vlasenko640ce3d2014-02-02 02:06:38 +0100385 int fd = open_zipped(G.image_filename, /*fail_if_not_compressed:*/ 0);
Vladimir Dronnikov6eaac022009-11-07 04:14:50 +0100386 if (fd < 0)
387 bb_simple_perror_msg_and_die(G.image_filename);
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +0100388 theme_file = xfdopen_for_read(fd);
Vladimir Dronnikov6eaac022009-11-07 04:14:50 +0100389 }
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000390
Denys Vlasenko814da222010-03-14 23:59:54 +0100391 /* Parse ppm header:
392 * - Magic: two characters "P6".
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000393 * - Whitespace (blanks, TABs, CRs, LFs).
394 * - A width, formatted as ASCII characters in decimal.
395 * - Whitespace.
Denys Vlasenko814da222010-03-14 23:59:54 +0100396 * - A height, ASCII decimal.
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000397 * - Whitespace.
Denys Vlasenko814da222010-03-14 23:59:54 +0100398 * - The maximum color value, ASCII decimal, in 0..65535
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000399 * - Newline or other single whitespace character.
Denys Vlasenko814da222010-03-14 23:59:54 +0100400 * (we support newline only)
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000401 * - A raster of Width * Height pixels in triplets of rgb
Denys Vlasenko814da222010-03-14 23:59:54 +0100402 * in pure binary by 1 or 2 bytes. (we support only 1 byte)
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000403 */
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200404#define concat_buf bb_common_bufsiz1
405 setup_common_bufsiz();
406
Denys Vlasenko814da222010-03-14 23:59:54 +0100407 read_ptr = concat_buf;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000408 while (1) {
Denys Vlasenko814da222010-03-14 23:59:54 +0100409 int w, h, max_color_val;
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200410 int rem = concat_buf + COMMON_BUFSIZE - read_ptr;
Denys Vlasenko814da222010-03-14 23:59:54 +0100411 if (rem < 2
412 || fgets(read_ptr, rem, theme_file) == NULL
413 ) {
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000414 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
Denys Vlasenko814da222010-03-14 23:59:54 +0100415 }
416 read_ptr = strchrnul(read_ptr, '#');
417 *read_ptr = '\0'; /* ignore #comments */
418 if (sscanf(concat_buf, "P6 %u %u %u", &w, &h, &max_color_val) == 3
419 && max_color_val <= 255
420 ) {
421 width = w; /* w is on stack, width may be in register */
422 height = h;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000423 break;
Denys Vlasenko814da222010-03-14 23:59:54 +0100424 }
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000425 }
426
427 line_size = width*3;
Denys Vlasenko814da222010-03-14 23:59:54 +0100428 pixline = xmalloc(line_size);
429
Peter Korsgaarda82fe672018-03-29 13:35:01 +0200430 if ((width + G.img_posx) > G.scr_var.xres)
431 width = G.scr_var.xres - G.img_posx;
432 if ((height + G.img_posy) > G.scr_var.yres)
433 height = G.scr_var.yres - G.img_posy;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000434 for (j = 0; j < height; j++) {
Denys Vlasenko814da222010-03-14 23:59:54 +0100435 unsigned char *pixel;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200436 unsigned char *src;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000437
Bernhard Reutner-Fischer051fdb92009-02-16 12:36:50 +0000438 if (fread(pixline, 1, line_size, theme_file) != line_size)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000439 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
Denys Vlasenko814da222010-03-14 23:59:54 +0100440 pixel = pixline;
Peter Korsgaarda82fe672018-03-29 13:35:01 +0200441 src = G.addr + (G.img_posy + j) * G.scr_fix.line_length + G.img_posx * G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000442 for (i = 0; i < width; i++) {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200443 unsigned thispix = fb_pixel_value(pixel[0], pixel[1], pixel[2]);
444 fb_write_pixel(src, thispix);
445 src += G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000446 pixel += 3;
447 }
448 }
Denys Vlasenko814da222010-03-14 23:59:54 +0100449 free(pixline);
Bernhard Reutner-Fischer051fdb92009-02-16 12:36:50 +0000450 fclose(theme_file);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000451}
452
453
454/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200455 * Parse configuration file
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000456 * \param *cfg_filename name of the configuration file
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000457 */
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000458static void init(const char *cfg_filename)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000459{
Denys Vlasenko10544a82010-02-09 00:26:10 +0100460 static const char param_names[] ALIGN1 =
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000461 "BAR_WIDTH\0" "BAR_HEIGHT\0"
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000462 "BAR_LEFT\0" "BAR_TOP\0"
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000463 "BAR_R\0" "BAR_G\0" "BAR_B\0"
Peter Korsgaarda82fe672018-03-29 13:35:01 +0200464 "IMG_LEFT\0" "IMG_TOP\0"
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000465#if DEBUG
466 "DEBUG\0"
467#endif
468 ;
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000469 char *token[2];
470 parser_t *parser = config_open2(cfg_filename, xfopen_stdin);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000471 while (config_read(parser, token, 2, 2, "#=",
Denys Vlasenko814da222010-03-14 23:59:54 +0100472 (PARSE_NORMAL | PARSE_MIN_DIE) & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
Denys Vlasenko77832482010-08-12 14:14:45 +0200473 unsigned val = xatoi_positive(token[1]);
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000474 int i = index_in_strings(param_names, token[0]);
475 if (i < 0)
Bernhard Reutner-Fischer4efcec92009-02-14 12:36:16 +0000476 bb_error_msg_and_die("syntax error: %s", token[0]);
Peter Korsgaarda82fe672018-03-29 13:35:01 +0200477 if (i >= 0 && i < 9)
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000478 G.ns[i] = val;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000479#if DEBUG
Peter Korsgaarda82fe672018-03-29 13:35:01 +0200480 if (i == 9) {
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000481 G.bdebug_messages = val;
482 if (G.bdebug_messages)
Denis Vlasenko5415c852008-07-21 23:05:26 +0000483 G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log");
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000484 }
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000485#endif
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000486 }
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000487 config_close(parser);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000488}
489
490
491int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000492int fbsplash_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000493{
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000494 const char *fb_device, *cfg_filename, *fifo_filename;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000495 FILE *fp = fp; // for compiler
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000496 char *num_buf;
497 unsigned num;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000498 bool bCursorOff;
499
500 INIT_G();
501
502 // parse command line options
503 fb_device = "/dev/fb0";
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000504 cfg_filename = NULL;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000505 fifo_filename = NULL;
506 bCursorOff = 1 & getopt32(argv, "cs:d:i:f:",
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000507 &G.image_filename, &fb_device, &cfg_filename, &fifo_filename);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000508
509 // parse configuration file
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000510 if (cfg_filename)
511 init(cfg_filename);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000512
513 // We must have -s IMG
514 if (!G.image_filename)
515 bb_show_usage();
516
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000517 fb_open(fb_device);
518
519 if (fifo_filename && bCursorOff) {
520 // hide cursor (BEFORE any fb ops)
Denys Vlasenko8187e012017-09-13 22:48:30 +0200521 full_write(STDOUT_FILENO, ESC"[?25l", 6);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000522 }
523
524 fb_drawimage();
525
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000526 if (!fifo_filename)
527 return EXIT_SUCCESS;
528
529 fp = xfopen_stdin(fifo_filename);
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000530 if (fp != stdin) {
Denis Vlasenko72b34422008-03-27 13:14:29 +0000531 // For named pipes, we want to support this:
532 // mkfifo cmd_pipe
533 // fbsplash -f cmd_pipe .... &
534 // ...
535 // echo 33 >cmd_pipe
536 // ...
537 // echo 66 >cmd_pipe
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000538 // This means that we don't want fbsplash to get EOF
539 // when last writer closes input end.
540 // The simplest way is to open fifo for writing too
541 // and become an additional writer :)
542 open(fifo_filename, O_WRONLY); // errors are ignored
543 }
544
545 fb_drawprogressbar(0);
546 // Block on read, waiting for some input.
547 // Use of <stdio.h> style I/O allows to correctly
548 // handle a case when we have many buffered lines
549 // already in the pipe
550 while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100551 if (is_prefixed_with(num_buf, "exit")) {
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000552 DEBUG_MESSAGE("exit");
553 break;
554 }
555 num = atoi(num_buf);
556 if (isdigit(num_buf[0]) && (num <= 100)) {
557#if DEBUG
Denys Vlasenko7bb346f2009-10-06 22:09:50 +0200558 DEBUG_MESSAGE(itoa(num));
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000559#endif
560 fb_drawprogressbar(num);
561 }
562 free(num_buf);
563 }
564
565 if (bCursorOff) // restore cursor
Denys Vlasenko8187e012017-09-13 22:48:30 +0200566 full_write(STDOUT_FILENO, ESC"[?25h", 6);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000567
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000568 return EXIT_SUCCESS;
569}