blob: 77033c258cff354bf30ec331583676381affa477 [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"
37#include <linux/fb.h>
38
Denis Vlasenko25a9c172008-03-26 15:12:11 +000039/* If you want logging messages on /tmp/fbsplash.log... */
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000040#define DEBUG 0
41
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000042struct globals {
43#if DEBUG
44 bool bdebug_messages; // enable/disable logging
45 FILE *logfile_fd; // log file
46#endif
47 unsigned char *addr; // pointer to framebuffer memory
Denis Vlasenko0f99d492008-07-24 23:38:04 +000048 unsigned ns[7]; // n-parameters
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000049 const char *image_filename;
50 struct fb_var_screeninfo scr_var;
51 struct fb_fix_screeninfo scr_fix;
Nuno Lucas0e89fd92011-03-28 15:30:59 +020052 unsigned bytes_per_pixel;
Linus Walleij6d463de2012-09-06 16:52:31 +020053 // cached (8 - scr_var.COLOR.length):
54 unsigned red_shift;
55 unsigned green_shift;
56 unsigned blue_shift;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000057};
58#define G (*ptr_to_globals)
Denis Vlasenko7049ff82008-06-25 09:53:17 +000059#define INIT_G() do { \
60 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
61} while (0)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000062
Denis Vlasenko0f99d492008-07-24 23:38:04 +000063#define nbar_width ns[0] // progress bar width
64#define nbar_height ns[1] // progress bar height
65#define nbar_posx ns[2] // progress bar horizontal position
66#define nbar_posy ns[3] // progress bar vertical position
67#define nbar_colr ns[4] // progress bar color red component
68#define nbar_colg ns[5] // progress bar color green component
69#define nbar_colb ns[6] // progress bar color blue component
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000070
71#if DEBUG
72#define DEBUG_MESSAGE(strMessage, args...) \
73 if (G.bdebug_messages) { \
74 fprintf(G.logfile_fd, "[%s][%s] - %s\n", \
75 __FILE__, __FUNCTION__, strMessage); \
76 }
77#else
78#define DEBUG_MESSAGE(...) ((void)0)
79#endif
80
Peter Korsgaardcd06e062011-10-17 05:31:52 +020081/**
82 * Configure palette for RGB:332
83 */
84static void fb_setpal(int fd)
85{
86 struct fb_cmap cmap;
87 /* fb colors are 16 bit */
88 unsigned short red[256], green[256], blue[256];
89 unsigned i;
90
91 /* RGB:332 */
92 for (i = 0; i < 256; i++) {
93 /* Color is encoded in pixel value as rrrgggbb.
94 * 3-bit color is mapped to 16-bit one as:
95 * 000 -> 00000000 00000000
96 * 001 -> 00100100 10010010
97 * ...
98 * 011 -> 01101101 10110110
99 * 100 -> 10010010 01001001
100 * ...
101 * 111 -> 11111111 11111111
102 */
103 red[i] = (( i >> 5 ) * 0x9249) >> 2; // rrr * 00 10010010 01001001 >> 2
104 green[i] = (((i >> 2) & 0x7) * 0x9249) >> 2; // ggg * 00 10010010 01001001 >> 2
105 /* 2-bit color is easier: */
106 blue[i] = ( i & 0x3) * 0x5555; // bb * 01010101 01010101
107 }
108
109 cmap.start = 0;
110 cmap.len = 256;
111 cmap.red = red;
112 cmap.green = green;
113 cmap.blue = blue;
114 cmap.transp = 0;
115
116 xioctl(fd, FBIOPUTCMAP, &cmap);
117}
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000118
119/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200120 * Open and initialize the framebuffer device
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000121 * \param *strfb_device pointer to framebuffer device
122 */
123static void fb_open(const char *strfb_device)
124{
125 int fbfd = xopen(strfb_device, O_RDWR);
126
127 // framebuffer properties
128 xioctl(fbfd, FBIOGET_VSCREENINFO, &G.scr_var);
129 xioctl(fbfd, FBIOGET_FSCREENINFO, &G.scr_fix);
130
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200131 switch (G.scr_var.bits_per_pixel) {
132 case 8:
133 fb_setpal(fbfd);
134 break;
135
136 case 16:
137 case 24:
138 case 32:
139 break;
140
141 default:
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200142 bb_error_msg_and_die("unsupported %u bpp", (int)G.scr_var.bits_per_pixel);
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200143 break;
144 }
145
Linus Walleij6d463de2012-09-06 16:52:31 +0200146 G.red_shift = 8 - G.scr_var.red.length;
147 G.green_shift = 8 - G.scr_var.green.length;
148 G.blue_shift = 8 - G.scr_var.blue.length;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200149 G.bytes_per_pixel = (G.scr_var.bits_per_pixel + 7) >> 3;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000150
151 // map the device in memory
152 G.addr = mmap(NULL,
Denys Vlasenko69675782013-01-14 01:34:48 +0100153 G.scr_var.yres * G.scr_fix.line_length,
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000154 PROT_WRITE, MAP_SHARED, fbfd, 0);
155 if (G.addr == MAP_FAILED)
Bernhard Reutner-Fischer4efcec92009-02-14 12:36:16 +0000156 bb_perror_msg_and_die("mmap");
Dan Fandrich07078c22011-01-26 11:30:34 -0800157
158 // point to the start of the visible screen
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200159 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 +0000160 close(fbfd);
161}
162
163
164/**
Linus Walleij6d463de2012-09-06 16:52:31 +0200165 * Return pixel value of the passed RGB color.
166 * This is performance critical fn.
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200167 */
168static unsigned fb_pixel_value(unsigned r, unsigned g, unsigned b)
169{
Linus Walleij6d463de2012-09-06 16:52:31 +0200170 /* We assume that the r,g,b values are <= 255 */
171
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200172 if (G.bytes_per_pixel == 1) {
173 r = r & 0xe0; // 3-bit red
174 g = (g >> 3) & 0x1c; // 3-bit green
175 b = b >> 6; // 2-bit blue
176 return r + g + b;
177 }
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200178 if (G.bytes_per_pixel == 2) {
Linus Walleij6d463de2012-09-06 16:52:31 +0200179 // ARM PL110 on Integrator/CP has RGBA5551 bit arrangement.
180 // We want to support bit locations like that.
181 //
182 // First shift out unused bits
183 r = r >> G.red_shift;
184 g = g >> G.green_shift;
185 b = b >> G.blue_shift;
186 // Then shift the remaining bits to their offset
187 return (r << G.scr_var.red.offset) +
188 (g << G.scr_var.green.offset) +
189 (b << G.scr_var.blue.offset);
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200190 }
191 // RGB 888
192 return b + (g << 8) + (r << 16);
193}
194
195/**
196 * Draw pixel on framebuffer
197 */
198static void fb_write_pixel(unsigned char *addr, unsigned pixel)
199{
200 switch (G.bytes_per_pixel) {
Peter Korsgaardcd06e062011-10-17 05:31:52 +0200201 case 1:
202 *addr = pixel;
203 break;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200204 case 2:
205 *(uint16_t *)addr = pixel;
206 break;
207 case 4:
208 *(uint32_t *)addr = pixel;
209 break;
210 default: // 24 bits per pixel
211 addr[0] = pixel;
212 addr[1] = pixel >> 8;
213 addr[2] = pixel >> 16;
214 }
215}
216
217
218/**
219 * Draw hollow rectangle on framebuffer
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000220 */
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000221static void fb_drawrectangle(void)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000222{
223 int cnt;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200224 unsigned thispix;
225 unsigned char *ptr1, *ptr2;
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000226 unsigned char nred = G.nbar_colr/2;
227 unsigned char ngreen = G.nbar_colg/2;
228 unsigned char nblue = G.nbar_colb/2;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000229
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200230 thispix = fb_pixel_value(nred, ngreen, nblue);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000231
232 // horizontal lines
Yin Kangkai3ac066c2012-02-23 15:56:36 +0800233 ptr1 = G.addr + G.nbar_posy * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
234 ptr2 = G.addr + (G.nbar_posy + G.nbar_height - 1) * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000235 cnt = G.nbar_width - 1;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000236 do {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200237 fb_write_pixel(ptr1, thispix);
238 fb_write_pixel(ptr2, thispix);
239 ptr1 += G.bytes_per_pixel;
240 ptr2 += G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000241 } while (--cnt >= 0);
242
243 // vertical lines
Yin Kangkai3ac066c2012-02-23 15:56:36 +0800244 ptr1 = G.addr + G.nbar_posy * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
245 ptr2 = G.addr + G.nbar_posy * G.scr_fix.line_length + (G.nbar_posx + G.nbar_width - 1) * G.bytes_per_pixel;
Denys Vlasenko814da222010-03-14 23:59:54 +0100246 cnt = G.nbar_height - 1;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000247 do {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200248 fb_write_pixel(ptr1, thispix);
249 fb_write_pixel(ptr2, thispix);
Yin Kangkai3ac066c2012-02-23 15:56:36 +0800250 ptr1 += G.scr_fix.line_length;
251 ptr2 += G.scr_fix.line_length;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000252 } while (--cnt >= 0);
253}
254
255
256/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200257 * Draw filled rectangle on framebuffer
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000258 * \param nx1pos,ny1pos upper left position
259 * \param nx2pos,ny2pos down right position
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000260 * \param nred,ngreen,nblue rgb color
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000261 */
262static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
263 unsigned char nred, unsigned char ngreen, unsigned char nblue)
264{
265 int cnt1, cnt2, nypos;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200266 unsigned thispix;
267 unsigned char *ptr;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000268
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200269 thispix = fb_pixel_value(nred, ngreen, nblue);
Denis Vlasenko1f228982008-04-22 00:16:29 +0000270
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000271 cnt1 = ny2pos - ny1pos;
272 nypos = ny1pos;
273 do {
Yin Kangkai3ac066c2012-02-23 15:56:36 +0800274 ptr = G.addr + nypos * G.scr_fix.line_length + nx1pos * G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000275 cnt2 = nx2pos - nx1pos;
276 do {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200277 fb_write_pixel(ptr, thispix);
278 ptr += G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000279 } while (--cnt2 >= 0);
Denis Vlasenko1f228982008-04-22 00:16:29 +0000280
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000281 nypos++;
282 } while (--cnt1 >= 0);
283}
284
285
286/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200287 * Draw a progress bar on framebuffer
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000288 * \param percent percentage of loading
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000289 */
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000290static void fb_drawprogressbar(unsigned percent)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000291{
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200292 int left_x, top_y, pos_x;
293 unsigned width, height;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000294
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000295 // outer box
296 left_x = G.nbar_posx;
297 top_y = G.nbar_posy;
298 width = G.nbar_width - 1;
299 height = G.nbar_height - 1;
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200300 if ((int)(height | width) < 0)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000301 return;
302 // NB: "width" of 1 actually makes rect with width of 2!
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000303 fb_drawrectangle();
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000304
305 // inner "empty" rectangle
306 left_x++;
307 top_y++;
308 width -= 2;
309 height -= 2;
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200310 if ((int)(height | width) < 0)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000311 return;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000312
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200313 pos_x = left_x;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000314 if (percent > 0) {
Timo Teräs67dc7b22012-10-17 19:39:34 +0200315 int i, y;
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200316
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000317 // actual progress bar
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200318 pos_x += (unsigned)(width * percent) / 100;
319
320 y = top_y;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000321 i = height;
322 if (height == 0)
323 height++; // divide by 0 is bad
324 while (i >= 0) {
325 // draw one-line thick "rectangle"
326 // top line will have gray lvl 200, bottom one 100
Timo Teräs67dc7b22012-10-17 19:39:34 +0200327 unsigned gray_level = 100 + (unsigned)i*100 / height;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000328 fb_drawfullrectangle(
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200329 left_x, y, pos_x, y,
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000330 gray_level, gray_level, gray_level);
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200331 y++;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000332 i--;
333 }
334 }
Peter Korsgaarde4fa7b72011-10-17 04:35:23 +0200335
336 fb_drawfullrectangle(
337 pos_x, top_y,
338 left_x + width, top_y + height,
339 G.nbar_colr, G.nbar_colg, G.nbar_colb);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000340}
341
342
343/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200344 * Draw image from PPM file
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000345 */
346static void fb_drawimage(void)
347{
Bernhard Reutner-Fischer051fdb92009-02-16 12:36:50 +0000348 FILE *theme_file;
Denys Vlasenko814da222010-03-14 23:59:54 +0100349 char *read_ptr;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000350 unsigned char *pixline;
351 unsigned i, j, width, height, line_size;
352
Denys Vlasenko814da222010-03-14 23:59:54 +0100353 if (LONE_DASH(G.image_filename)) {
Vladimir Dronnikov6eaac022009-11-07 04:14:50 +0100354 theme_file = stdin;
Denys Vlasenko814da222010-03-14 23:59:54 +0100355 } else {
Denys Vlasenko640ce3d2014-02-02 02:06:38 +0100356 int fd = open_zipped(G.image_filename, /*fail_if_not_compressed:*/ 0);
Vladimir Dronnikov6eaac022009-11-07 04:14:50 +0100357 if (fd < 0)
358 bb_simple_perror_msg_and_die(G.image_filename);
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +0100359 theme_file = xfdopen_for_read(fd);
Vladimir Dronnikov6eaac022009-11-07 04:14:50 +0100360 }
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000361
Denys Vlasenko814da222010-03-14 23:59:54 +0100362 /* Parse ppm header:
363 * - Magic: two characters "P6".
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000364 * - Whitespace (blanks, TABs, CRs, LFs).
365 * - A width, formatted as ASCII characters in decimal.
366 * - Whitespace.
Denys Vlasenko814da222010-03-14 23:59:54 +0100367 * - A height, ASCII decimal.
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000368 * - Whitespace.
Denys Vlasenko814da222010-03-14 23:59:54 +0100369 * - The maximum color value, ASCII decimal, in 0..65535
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000370 * - Newline or other single whitespace character.
Denys Vlasenko814da222010-03-14 23:59:54 +0100371 * (we support newline only)
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000372 * - A raster of Width * Height pixels in triplets of rgb
Denys Vlasenko814da222010-03-14 23:59:54 +0100373 * in pure binary by 1 or 2 bytes. (we support only 1 byte)
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000374 */
Denys Vlasenko814da222010-03-14 23:59:54 +0100375#define concat_buf bb_common_bufsiz1
376 read_ptr = concat_buf;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000377 while (1) {
Denys Vlasenko814da222010-03-14 23:59:54 +0100378 int w, h, max_color_val;
379 int rem = concat_buf + sizeof(concat_buf) - read_ptr;
380 if (rem < 2
381 || fgets(read_ptr, rem, theme_file) == NULL
382 ) {
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000383 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
Denys Vlasenko814da222010-03-14 23:59:54 +0100384 }
385 read_ptr = strchrnul(read_ptr, '#');
386 *read_ptr = '\0'; /* ignore #comments */
387 if (sscanf(concat_buf, "P6 %u %u %u", &w, &h, &max_color_val) == 3
388 && max_color_val <= 255
389 ) {
390 width = w; /* w is on stack, width may be in register */
391 height = h;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000392 break;
Denys Vlasenko814da222010-03-14 23:59:54 +0100393 }
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000394 }
395
396 line_size = width*3;
Denys Vlasenko814da222010-03-14 23:59:54 +0100397 pixline = xmalloc(line_size);
398
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000399 if (width > G.scr_var.xres)
400 width = G.scr_var.xres;
401 if (height > G.scr_var.yres)
402 height = G.scr_var.yres;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000403 for (j = 0; j < height; j++) {
Denys Vlasenko814da222010-03-14 23:59:54 +0100404 unsigned char *pixel;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200405 unsigned char *src;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000406
Bernhard Reutner-Fischer051fdb92009-02-16 12:36:50 +0000407 if (fread(pixline, 1, line_size, theme_file) != line_size)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000408 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
Denys Vlasenko814da222010-03-14 23:59:54 +0100409 pixel = pixline;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200410 src = G.addr + j * G.scr_fix.line_length;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000411 for (i = 0; i < width; i++) {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200412 unsigned thispix = fb_pixel_value(pixel[0], pixel[1], pixel[2]);
413 fb_write_pixel(src, thispix);
414 src += G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000415 pixel += 3;
416 }
417 }
Denys Vlasenko814da222010-03-14 23:59:54 +0100418 free(pixline);
Bernhard Reutner-Fischer051fdb92009-02-16 12:36:50 +0000419 fclose(theme_file);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000420}
421
422
423/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200424 * Parse configuration file
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000425 * \param *cfg_filename name of the configuration file
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000426 */
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000427static void init(const char *cfg_filename)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000428{
Denys Vlasenko10544a82010-02-09 00:26:10 +0100429 static const char param_names[] ALIGN1 =
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000430 "BAR_WIDTH\0" "BAR_HEIGHT\0"
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000431 "BAR_LEFT\0" "BAR_TOP\0"
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000432 "BAR_R\0" "BAR_G\0" "BAR_B\0"
433#if DEBUG
434 "DEBUG\0"
435#endif
436 ;
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000437 char *token[2];
438 parser_t *parser = config_open2(cfg_filename, xfopen_stdin);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000439 while (config_read(parser, token, 2, 2, "#=",
Denys Vlasenko814da222010-03-14 23:59:54 +0100440 (PARSE_NORMAL | PARSE_MIN_DIE) & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
Denys Vlasenko77832482010-08-12 14:14:45 +0200441 unsigned val = xatoi_positive(token[1]);
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000442 int i = index_in_strings(param_names, token[0]);
443 if (i < 0)
Bernhard Reutner-Fischer4efcec92009-02-14 12:36:16 +0000444 bb_error_msg_and_die("syntax error: %s", token[0]);
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000445 if (i >= 0 && i < 7)
446 G.ns[i] = val;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000447#if DEBUG
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000448 if (i == 7) {
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000449 G.bdebug_messages = val;
450 if (G.bdebug_messages)
Denis Vlasenko5415c852008-07-21 23:05:26 +0000451 G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log");
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000452 }
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000453#endif
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000454 }
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000455 config_close(parser);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000456}
457
458
459int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000460int fbsplash_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000461{
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000462 const char *fb_device, *cfg_filename, *fifo_filename;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000463 FILE *fp = fp; // for compiler
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000464 char *num_buf;
465 unsigned num;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000466 bool bCursorOff;
467
468 INIT_G();
469
470 // parse command line options
471 fb_device = "/dev/fb0";
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000472 cfg_filename = NULL;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000473 fifo_filename = NULL;
474 bCursorOff = 1 & getopt32(argv, "cs:d:i:f:",
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000475 &G.image_filename, &fb_device, &cfg_filename, &fifo_filename);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000476
477 // parse configuration file
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000478 if (cfg_filename)
479 init(cfg_filename);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000480
481 // We must have -s IMG
482 if (!G.image_filename)
483 bb_show_usage();
484
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000485 fb_open(fb_device);
486
487 if (fifo_filename && bCursorOff) {
488 // hide cursor (BEFORE any fb ops)
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200489 full_write(STDOUT_FILENO, "\033[?25l", 6);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000490 }
491
492 fb_drawimage();
493
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000494 if (!fifo_filename)
495 return EXIT_SUCCESS;
496
497 fp = xfopen_stdin(fifo_filename);
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000498 if (fp != stdin) {
Denis Vlasenko72b34422008-03-27 13:14:29 +0000499 // For named pipes, we want to support this:
500 // mkfifo cmd_pipe
501 // fbsplash -f cmd_pipe .... &
502 // ...
503 // echo 33 >cmd_pipe
504 // ...
505 // echo 66 >cmd_pipe
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000506 // This means that we don't want fbsplash to get EOF
507 // when last writer closes input end.
508 // The simplest way is to open fifo for writing too
509 // and become an additional writer :)
510 open(fifo_filename, O_WRONLY); // errors are ignored
511 }
512
513 fb_drawprogressbar(0);
514 // Block on read, waiting for some input.
515 // Use of <stdio.h> style I/O allows to correctly
516 // handle a case when we have many buffered lines
517 // already in the pipe
518 while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100519 if (is_prefixed_with(num_buf, "exit")) {
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000520 DEBUG_MESSAGE("exit");
521 break;
522 }
523 num = atoi(num_buf);
524 if (isdigit(num_buf[0]) && (num <= 100)) {
525#if DEBUG
Denys Vlasenko7bb346f2009-10-06 22:09:50 +0200526 DEBUG_MESSAGE(itoa(num));
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000527#endif
528 fb_drawprogressbar(num);
529 }
530 free(num_buf);
531 }
532
533 if (bCursorOff) // restore cursor
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200534 full_write(STDOUT_FILENO, "\033[?25h", 6);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000535
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000536 return EXIT_SUCCESS;
537}