blob: 3ddf8a242767e6f902ae55c33a8f163953b2f1e5 [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 */
23
Pere Orga5bc8c002011-04-11 03:29:49 +020024//usage:#define fbsplash_trivial_usage
25//usage: "-s IMGFILE [-c] [-d DEV] [-i INIFILE] [-f CMD]"
26//usage:#define fbsplash_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020027//usage: " -s Image"
Pere Orga5bc8c002011-04-11 03:29:49 +020028//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'"
35
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000036#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020037#include "common_bufsiz.h"
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000038#include <linux/fb.h>
39
Denis Vlasenko25a9c172008-03-26 15:12:11 +000040/* If you want logging messages on /tmp/fbsplash.log... */
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000041#define DEBUG 0
42
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000043struct globals {
44#if DEBUG
45 bool bdebug_messages; // enable/disable logging
46 FILE *logfile_fd; // log file
47#endif
48 unsigned char *addr; // pointer to framebuffer memory
Denis Vlasenko0f99d492008-07-24 23:38:04 +000049 unsigned ns[7]; // n-parameters
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000050 const char *image_filename;
51 struct fb_var_screeninfo scr_var;
52 struct fb_fix_screeninfo scr_fix;
Nuno Lucas0e89fd92011-03-28 15:30:59 +020053 unsigned bytes_per_pixel;
Linus Walleij6d463de2012-09-06 16:52:31 +020054 // cached (8 - scr_var.COLOR.length):
55 unsigned red_shift;
56 unsigned green_shift;
57 unsigned blue_shift;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000058};
59#define G (*ptr_to_globals)
Denis Vlasenko7049ff82008-06-25 09:53:17 +000060#define INIT_G() do { \
61 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
62} while (0)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000063
Denis Vlasenko0f99d492008-07-24 23:38:04 +000064#define nbar_width ns[0] // progress bar width
65#define nbar_height ns[1] // progress bar height
66#define nbar_posx ns[2] // progress bar horizontal position
67#define nbar_posy ns[3] // progress bar vertical position
68#define nbar_colr ns[4] // progress bar color red component
69#define nbar_colg ns[5] // progress bar color green component
70#define nbar_colb ns[6] // progress bar color blue component
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000071
72#if DEBUG
73#define DEBUG_MESSAGE(strMessage, args...) \
74 if (G.bdebug_messages) { \
75 fprintf(G.logfile_fd, "[%s][%s] - %s\n", \
76 __FILE__, __FUNCTION__, strMessage); \
77 }
78#else
79#define DEBUG_MESSAGE(...) ((void)0)
80#endif
81
Peter Korsgaardcd06e062011-10-17 05:31:52 +020082/**
83 * Configure palette for RGB:332
84 */
85static void fb_setpal(int fd)
86{
87 struct fb_cmap cmap;
88 /* fb colors are 16 bit */
89 unsigned short red[256], green[256], blue[256];
90 unsigned i;
91
92 /* RGB:332 */
93 for (i = 0; i < 256; i++) {
94 /* Color is encoded in pixel value as rrrgggbb.
95 * 3-bit color is mapped to 16-bit one as:
96 * 000 -> 00000000 00000000
97 * 001 -> 00100100 10010010
98 * ...
99 * 011 -> 01101101 10110110
100 * 100 -> 10010010 01001001
101 * ...
102 * 111 -> 11111111 11111111
103 */
104 red[i] = (( i >> 5 ) * 0x9249) >> 2; // rrr * 00 10010010 01001001 >> 2
105 green[i] = (((i >> 2) & 0x7) * 0x9249) >> 2; // ggg * 00 10010010 01001001 >> 2
106 /* 2-bit color is easier: */
107 blue[i] = ( i & 0x3) * 0x5555; // bb * 01010101 01010101
108 }
109
110 cmap.start = 0;
111 cmap.len = 256;
112 cmap.red = red;
113 cmap.green = green;
114 cmap.blue = blue;
115 cmap.transp = 0;
116
117 xioctl(fd, FBIOPUTCMAP, &cmap);
118}
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000119
120/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200121 * Open and initialize the framebuffer device
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000122 * \param *strfb_device pointer to framebuffer device
123 */
124static void fb_open(const char *strfb_device)
125{
126 int fbfd = xopen(strfb_device, O_RDWR);
127
128 // framebuffer properties
129 xioctl(fbfd, FBIOGET_VSCREENINFO, &G.scr_var);
130 xioctl(fbfd, FBIOGET_FSCREENINFO, &G.scr_fix);
131
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200132 switch (G.scr_var.bits_per_pixel) {
133 case 8:
134 fb_setpal(fbfd);
135 break;
136
137 case 16:
138 case 24:
139 case 32:
140 break;
141
142 default:
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200143 bb_error_msg_and_die("unsupported %u bpp", (int)G.scr_var.bits_per_pixel);
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200144 break;
145 }
146
Linus Walleij6d463de2012-09-06 16:52:31 +0200147 G.red_shift = 8 - G.scr_var.red.length;
148 G.green_shift = 8 - G.scr_var.green.length;
149 G.blue_shift = 8 - G.scr_var.blue.length;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200150 G.bytes_per_pixel = (G.scr_var.bits_per_pixel + 7) >> 3;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000151
152 // map the device in memory
153 G.addr = mmap(NULL,
Timo Teräs82c2fad2015-10-26 09:40:31 +0200154 (G.scr_var.yres_virtual ?: G.scr_var.yres) * G.scr_fix.line_length,
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000155 PROT_WRITE, MAP_SHARED, fbfd, 0);
156 if (G.addr == MAP_FAILED)
Bernhard Reutner-Fischer4efcec92009-02-14 12:36:16 +0000157 bb_perror_msg_and_die("mmap");
Dan Fandrich07078c22011-01-26 11:30:34 -0800158
159 // point to the start of the visible screen
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200160 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 +0000161 close(fbfd);
162}
163
164
165/**
Linus Walleij6d463de2012-09-06 16:52:31 +0200166 * Return pixel value of the passed RGB color.
167 * This is performance critical fn.
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200168 */
169static unsigned fb_pixel_value(unsigned r, unsigned g, unsigned b)
170{
Linus Walleij6d463de2012-09-06 16:52:31 +0200171 /* We assume that the r,g,b values are <= 255 */
172
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200173 if (G.bytes_per_pixel == 1) {
174 r = r & 0xe0; // 3-bit red
175 g = (g >> 3) & 0x1c; // 3-bit green
176 b = b >> 6; // 2-bit blue
177 return r + g + b;
178 }
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200179 if (G.bytes_per_pixel == 2) {
Linus Walleij6d463de2012-09-06 16:52:31 +0200180 // ARM PL110 on Integrator/CP has RGBA5551 bit arrangement.
181 // We want to support bit locations like that.
182 //
183 // First shift out unused bits
184 r = r >> G.red_shift;
185 g = g >> G.green_shift;
186 b = b >> G.blue_shift;
187 // Then shift the remaining bits to their offset
188 return (r << G.scr_var.red.offset) +
189 (g << G.scr_var.green.offset) +
190 (b << G.scr_var.blue.offset);
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200191 }
192 // RGB 888
193 return b + (g << 8) + (r << 16);
194}
195
196/**
197 * Draw pixel on framebuffer
198 */
199static void fb_write_pixel(unsigned char *addr, unsigned pixel)
200{
201 switch (G.bytes_per_pixel) {
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200202 case 1:
203 *addr = pixel;
204 break;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200205 case 2:
206 *(uint16_t *)addr = pixel;
207 break;
208 case 4:
209 *(uint32_t *)addr = pixel;
210 break;
211 default: // 24 bits per pixel
212 addr[0] = pixel;
213 addr[1] = pixel >> 8;
214 addr[2] = pixel >> 16;
215 }
216}
217
218
219/**
220 * Draw hollow rectangle on framebuffer
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000221 */
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000222static void fb_drawrectangle(void)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000223{
224 int cnt;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200225 unsigned thispix;
226 unsigned char *ptr1, *ptr2;
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000227 unsigned char nred = G.nbar_colr/2;
228 unsigned char ngreen = G.nbar_colg/2;
229 unsigned char nblue = G.nbar_colb/2;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000230
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200231 thispix = fb_pixel_value(nred, ngreen, nblue);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000232
233 // horizontal lines
Yin Kangkai3ac066c2012-02-23 15:56:36 +0800234 ptr1 = G.addr + G.nbar_posy * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
235 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 +0000236 cnt = G.nbar_width - 1;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000237 do {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200238 fb_write_pixel(ptr1, thispix);
239 fb_write_pixel(ptr2, thispix);
240 ptr1 += G.bytes_per_pixel;
241 ptr2 += G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000242 } while (--cnt >= 0);
243
244 // vertical lines
Yin Kangkai3ac066c2012-02-23 15:56:36 +0800245 ptr1 = G.addr + G.nbar_posy * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
246 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 +0100247 cnt = G.nbar_height - 1;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000248 do {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200249 fb_write_pixel(ptr1, thispix);
250 fb_write_pixel(ptr2, thispix);
Yin Kangkai3ac066c2012-02-23 15:56:36 +0800251 ptr1 += G.scr_fix.line_length;
252 ptr2 += G.scr_fix.line_length;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000253 } while (--cnt >= 0);
254}
255
256
257/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200258 * Draw filled rectangle on framebuffer
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000259 * \param nx1pos,ny1pos upper left position
260 * \param nx2pos,ny2pos down right position
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000261 * \param nred,ngreen,nblue rgb color
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000262 */
263static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
264 unsigned char nred, unsigned char ngreen, unsigned char nblue)
265{
266 int cnt1, cnt2, nypos;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200267 unsigned thispix;
268 unsigned char *ptr;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000269
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200270 thispix = fb_pixel_value(nred, ngreen, nblue);
Denis Vlasenko1f228982008-04-22 00:16:29 +0000271
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000272 cnt1 = ny2pos - ny1pos;
273 nypos = ny1pos;
274 do {
Yin Kangkai3ac066c2012-02-23 15:56:36 +0800275 ptr = G.addr + nypos * G.scr_fix.line_length + nx1pos * G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000276 cnt2 = nx2pos - nx1pos;
277 do {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200278 fb_write_pixel(ptr, thispix);
279 ptr += G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000280 } while (--cnt2 >= 0);
Denis Vlasenko1f228982008-04-22 00:16:29 +0000281
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000282 nypos++;
283 } while (--cnt1 >= 0);
284}
285
286
287/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200288 * Draw a progress bar on framebuffer
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000289 * \param percent percentage of loading
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000290 */
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000291static void fb_drawprogressbar(unsigned percent)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000292{
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200293 int left_x, top_y, pos_x;
294 unsigned width, height;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000295
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000296 // outer box
297 left_x = G.nbar_posx;
298 top_y = G.nbar_posy;
299 width = G.nbar_width - 1;
300 height = G.nbar_height - 1;
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200301 if ((int)(height | width) < 0)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000302 return;
303 // NB: "width" of 1 actually makes rect with width of 2!
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000304 fb_drawrectangle();
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000305
306 // inner "empty" rectangle
307 left_x++;
308 top_y++;
309 width -= 2;
310 height -= 2;
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200311 if ((int)(height | width) < 0)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000312 return;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000313
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200314 pos_x = left_x;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000315 if (percent > 0) {
Timo Teräs67dc7b22012-10-17 19:39:34 +0200316 int i, y;
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200317
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000318 // actual progress bar
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200319 pos_x += (unsigned)(width * percent) / 100;
320
321 y = top_y;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000322 i = height;
323 if (height == 0)
324 height++; // divide by 0 is bad
325 while (i >= 0) {
326 // draw one-line thick "rectangle"
327 // top line will have gray lvl 200, bottom one 100
Timo Teräs67dc7b22012-10-17 19:39:34 +0200328 unsigned gray_level = 100 + (unsigned)i*100 / height;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000329 fb_drawfullrectangle(
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200330 left_x, y, pos_x, y,
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000331 gray_level, gray_level, gray_level);
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200332 y++;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000333 i--;
334 }
335 }
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200336
337 fb_drawfullrectangle(
338 pos_x, top_y,
339 left_x + width, top_y + height,
340 G.nbar_colr, G.nbar_colg, G.nbar_colb);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000341}
342
343
344/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200345 * Draw image from PPM file
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000346 */
347static void fb_drawimage(void)
348{
Bernhard Reutner-Fischer051fdb92009-02-16 12:36:50 +0000349 FILE *theme_file;
Denys Vlasenko814da222010-03-14 23:59:54 +0100350 char *read_ptr;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000351 unsigned char *pixline;
352 unsigned i, j, width, height, line_size;
353
Denys Vlasenko814da222010-03-14 23:59:54 +0100354 if (LONE_DASH(G.image_filename)) {
Vladimir Dronnikov6eaac022009-11-07 04:14:50 +0100355 theme_file = stdin;
Denys Vlasenko814da222010-03-14 23:59:54 +0100356 } else {
Denys Vlasenko640ce3d2014-02-02 02:06:38 +0100357 int fd = open_zipped(G.image_filename, /*fail_if_not_compressed:*/ 0);
Vladimir Dronnikov6eaac022009-11-07 04:14:50 +0100358 if (fd < 0)
359 bb_simple_perror_msg_and_die(G.image_filename);
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +0100360 theme_file = xfdopen_for_read(fd);
Vladimir Dronnikov6eaac022009-11-07 04:14:50 +0100361 }
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000362
Denys Vlasenko814da222010-03-14 23:59:54 +0100363 /* Parse ppm header:
364 * - Magic: two characters "P6".
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000365 * - Whitespace (blanks, TABs, CRs, LFs).
366 * - A width, formatted as ASCII characters in decimal.
367 * - Whitespace.
Denys Vlasenko814da222010-03-14 23:59:54 +0100368 * - A height, ASCII decimal.
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000369 * - Whitespace.
Denys Vlasenko814da222010-03-14 23:59:54 +0100370 * - The maximum color value, ASCII decimal, in 0..65535
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000371 * - Newline or other single whitespace character.
Denys Vlasenko814da222010-03-14 23:59:54 +0100372 * (we support newline only)
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000373 * - A raster of Width * Height pixels in triplets of rgb
Denys Vlasenko814da222010-03-14 23:59:54 +0100374 * in pure binary by 1 or 2 bytes. (we support only 1 byte)
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000375 */
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200376#define concat_buf bb_common_bufsiz1
377 setup_common_bufsiz();
378
Denys Vlasenko814da222010-03-14 23:59:54 +0100379 read_ptr = concat_buf;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000380 while (1) {
Denys Vlasenko814da222010-03-14 23:59:54 +0100381 int w, h, max_color_val;
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200382 int rem = concat_buf + COMMON_BUFSIZE - read_ptr;
Denys Vlasenko814da222010-03-14 23:59:54 +0100383 if (rem < 2
384 || fgets(read_ptr, rem, theme_file) == NULL
385 ) {
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000386 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
Denys Vlasenko814da222010-03-14 23:59:54 +0100387 }
388 read_ptr = strchrnul(read_ptr, '#');
389 *read_ptr = '\0'; /* ignore #comments */
390 if (sscanf(concat_buf, "P6 %u %u %u", &w, &h, &max_color_val) == 3
391 && max_color_val <= 255
392 ) {
393 width = w; /* w is on stack, width may be in register */
394 height = h;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000395 break;
Denys Vlasenko814da222010-03-14 23:59:54 +0100396 }
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000397 }
398
399 line_size = width*3;
Denys Vlasenko814da222010-03-14 23:59:54 +0100400 pixline = xmalloc(line_size);
401
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000402 if (width > G.scr_var.xres)
403 width = G.scr_var.xres;
404 if (height > G.scr_var.yres)
405 height = G.scr_var.yres;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000406 for (j = 0; j < height; j++) {
Denys Vlasenko814da222010-03-14 23:59:54 +0100407 unsigned char *pixel;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200408 unsigned char *src;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000409
Bernhard Reutner-Fischer051fdb92009-02-16 12:36:50 +0000410 if (fread(pixline, 1, line_size, theme_file) != line_size)
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 pixel = pixline;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200413 src = G.addr + j * G.scr_fix.line_length;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000414 for (i = 0; i < width; i++) {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200415 unsigned thispix = fb_pixel_value(pixel[0], pixel[1], pixel[2]);
416 fb_write_pixel(src, thispix);
417 src += G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000418 pixel += 3;
419 }
420 }
Denys Vlasenko814da222010-03-14 23:59:54 +0100421 free(pixline);
Bernhard Reutner-Fischer051fdb92009-02-16 12:36:50 +0000422 fclose(theme_file);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000423}
424
425
426/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200427 * Parse configuration file
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000428 * \param *cfg_filename name of the configuration file
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000429 */
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000430static void init(const char *cfg_filename)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000431{
Denys Vlasenko10544a82010-02-09 00:26:10 +0100432 static const char param_names[] ALIGN1 =
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000433 "BAR_WIDTH\0" "BAR_HEIGHT\0"
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000434 "BAR_LEFT\0" "BAR_TOP\0"
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000435 "BAR_R\0" "BAR_G\0" "BAR_B\0"
436#if DEBUG
437 "DEBUG\0"
438#endif
439 ;
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000440 char *token[2];
441 parser_t *parser = config_open2(cfg_filename, xfopen_stdin);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000442 while (config_read(parser, token, 2, 2, "#=",
Denys Vlasenko814da222010-03-14 23:59:54 +0100443 (PARSE_NORMAL | PARSE_MIN_DIE) & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
Denys Vlasenko77832482010-08-12 14:14:45 +0200444 unsigned val = xatoi_positive(token[1]);
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000445 int i = index_in_strings(param_names, token[0]);
446 if (i < 0)
Bernhard Reutner-Fischer4efcec92009-02-14 12:36:16 +0000447 bb_error_msg_and_die("syntax error: %s", token[0]);
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000448 if (i >= 0 && i < 7)
449 G.ns[i] = val;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000450#if DEBUG
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000451 if (i == 7) {
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000452 G.bdebug_messages = val;
453 if (G.bdebug_messages)
Denis Vlasenko5415c852008-07-21 23:05:26 +0000454 G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log");
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000455 }
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000456#endif
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000457 }
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000458 config_close(parser);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000459}
460
461
462int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000463int fbsplash_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000464{
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000465 const char *fb_device, *cfg_filename, *fifo_filename;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000466 FILE *fp = fp; // for compiler
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000467 char *num_buf;
468 unsigned num;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000469 bool bCursorOff;
470
471 INIT_G();
472
473 // parse command line options
474 fb_device = "/dev/fb0";
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000475 cfg_filename = NULL;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000476 fifo_filename = NULL;
477 bCursorOff = 1 & getopt32(argv, "cs:d:i:f:",
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000478 &G.image_filename, &fb_device, &cfg_filename, &fifo_filename);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000479
480 // parse configuration file
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000481 if (cfg_filename)
482 init(cfg_filename);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000483
484 // We must have -s IMG
485 if (!G.image_filename)
486 bb_show_usage();
487
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000488 fb_open(fb_device);
489
490 if (fifo_filename && bCursorOff) {
491 // hide cursor (BEFORE any fb ops)
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200492 full_write(STDOUT_FILENO, "\033[?25l", 6);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000493 }
494
495 fb_drawimage();
496
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000497 if (!fifo_filename)
498 return EXIT_SUCCESS;
499
500 fp = xfopen_stdin(fifo_filename);
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000501 if (fp != stdin) {
Denis Vlasenko72b34422008-03-27 13:14:29 +0000502 // For named pipes, we want to support this:
503 // mkfifo cmd_pipe
504 // fbsplash -f cmd_pipe .... &
505 // ...
506 // echo 33 >cmd_pipe
507 // ...
508 // echo 66 >cmd_pipe
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000509 // This means that we don't want fbsplash to get EOF
510 // when last writer closes input end.
511 // The simplest way is to open fifo for writing too
512 // and become an additional writer :)
513 open(fifo_filename, O_WRONLY); // errors are ignored
514 }
515
516 fb_drawprogressbar(0);
517 // Block on read, waiting for some input.
518 // Use of <stdio.h> style I/O allows to correctly
519 // handle a case when we have many buffered lines
520 // already in the pipe
521 while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100522 if (is_prefixed_with(num_buf, "exit")) {
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000523 DEBUG_MESSAGE("exit");
524 break;
525 }
526 num = atoi(num_buf);
527 if (isdigit(num_buf[0]) && (num <= 100)) {
528#if DEBUG
Denys Vlasenko7bb346f2009-10-06 22:09:50 +0200529 DEBUG_MESSAGE(itoa(num));
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000530#endif
531 fb_drawprogressbar(num);
532 }
533 free(num_buf);
534 }
535
536 if (bCursorOff) // restore cursor
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200537 full_write(STDOUT_FILENO, "\033[?25h", 6);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000538
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000539 return EXIT_SUCCESS;
540}