blob: 51ba4729e64885e651b772ea3f1c41a34530b72f [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;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000053};
54#define G (*ptr_to_globals)
Denis Vlasenko7049ff82008-06-25 09:53:17 +000055#define INIT_G() do { \
56 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
57} while (0)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000058
Denis Vlasenko0f99d492008-07-24 23:38:04 +000059#define nbar_width ns[0] // progress bar width
60#define nbar_height ns[1] // progress bar height
61#define nbar_posx ns[2] // progress bar horizontal position
62#define nbar_posy ns[3] // progress bar vertical position
63#define nbar_colr ns[4] // progress bar color red component
64#define nbar_colg ns[5] // progress bar color green component
65#define nbar_colb ns[6] // progress bar color blue component
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000066
67#if DEBUG
68#define DEBUG_MESSAGE(strMessage, args...) \
69 if (G.bdebug_messages) { \
70 fprintf(G.logfile_fd, "[%s][%s] - %s\n", \
71 __FILE__, __FUNCTION__, strMessage); \
72 }
73#else
74#define DEBUG_MESSAGE(...) ((void)0)
75#endif
76
77
78/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +020079 * Open and initialize the framebuffer device
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000080 * \param *strfb_device pointer to framebuffer device
81 */
82static void fb_open(const char *strfb_device)
83{
84 int fbfd = xopen(strfb_device, O_RDWR);
85
86 // framebuffer properties
87 xioctl(fbfd, FBIOGET_VSCREENINFO, &G.scr_var);
88 xioctl(fbfd, FBIOGET_FSCREENINFO, &G.scr_fix);
89
Nuno Lucas0e89fd92011-03-28 15:30:59 +020090 if (G.scr_var.bits_per_pixel < 16 || G.scr_var.bits_per_pixel > 32)
91 bb_error_msg_and_die("unsupported %u bpp", (int)G.scr_var.bits_per_pixel);
92 G.bytes_per_pixel = (G.scr_var.bits_per_pixel + 7) >> 3;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000093
94 // map the device in memory
95 G.addr = mmap(NULL,
Nuno Lucas0e89fd92011-03-28 15:30:59 +020096 G.scr_var.xres * G.scr_var.yres * G.bytes_per_pixel,
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000097 PROT_WRITE, MAP_SHARED, fbfd, 0);
98 if (G.addr == MAP_FAILED)
Bernhard Reutner-Fischer4efcec92009-02-14 12:36:16 +000099 bb_perror_msg_and_die("mmap");
Dan Fandrich07078c22011-01-26 11:30:34 -0800100
101 // point to the start of the visible screen
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200102 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 +0000103 close(fbfd);
104}
105
106
107/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200108 * Return pixel value of the passed RGB color
109 */
110static unsigned fb_pixel_value(unsigned r, unsigned g, unsigned b)
111{
112 if (G.bytes_per_pixel == 2) {
113 r >>= 3; // 5-bit red
114 g >>= 2; // 6-bit green
115 b >>= 3; // 5-bit blue
116 return b + (g << 5) + (r << (5+6));
117 }
118 // RGB 888
119 return b + (g << 8) + (r << 16);
120}
121
122/**
123 * Draw pixel on framebuffer
124 */
125static void fb_write_pixel(unsigned char *addr, unsigned pixel)
126{
127 switch (G.bytes_per_pixel) {
128 case 2:
129 *(uint16_t *)addr = pixel;
130 break;
131 case 4:
132 *(uint32_t *)addr = pixel;
133 break;
134 default: // 24 bits per pixel
135 addr[0] = pixel;
136 addr[1] = pixel >> 8;
137 addr[2] = pixel >> 16;
138 }
139}
140
141
142/**
143 * Draw hollow rectangle on framebuffer
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000144 */
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000145static void fb_drawrectangle(void)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000146{
147 int cnt;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200148 unsigned thispix;
149 unsigned char *ptr1, *ptr2;
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000150 unsigned char nred = G.nbar_colr/2;
151 unsigned char ngreen = G.nbar_colg/2;
152 unsigned char nblue = G.nbar_colb/2;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000153
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200154 thispix = fb_pixel_value(nred, ngreen, nblue);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000155
156 // horizontal lines
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200157 ptr1 = G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx) * G.bytes_per_pixel;
158 ptr2 = G.addr + ((G.nbar_posy + G.nbar_height - 1) * G.scr_var.xres + G.nbar_posx) * G.bytes_per_pixel;
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000159 cnt = G.nbar_width - 1;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000160 do {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200161 fb_write_pixel(ptr1, thispix);
162 fb_write_pixel(ptr2, thispix);
163 ptr1 += G.bytes_per_pixel;
164 ptr2 += G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000165 } while (--cnt >= 0);
166
167 // vertical lines
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200168 ptr1 = G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx) * G.bytes_per_pixel;
169 ptr2 = G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx + G.nbar_width - 1) * G.bytes_per_pixel;
Denys Vlasenko814da222010-03-14 23:59:54 +0100170 cnt = G.nbar_height - 1;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000171 do {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200172 fb_write_pixel(ptr1, thispix);
173 fb_write_pixel(ptr2, thispix);
174 ptr1 += G.scr_var.xres * G.bytes_per_pixel;
175 ptr2 += G.scr_var.xres * G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000176 } while (--cnt >= 0);
177}
178
179
180/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200181 * Draw filled rectangle on framebuffer
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000182 * \param nx1pos,ny1pos upper left position
183 * \param nx2pos,ny2pos down right position
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000184 * \param nred,ngreen,nblue rgb color
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000185 */
186static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
187 unsigned char nred, unsigned char ngreen, unsigned char nblue)
188{
189 int cnt1, cnt2, nypos;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200190 unsigned thispix;
191 unsigned char *ptr;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000192
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200193 thispix = fb_pixel_value(nred, ngreen, nblue);
Denis Vlasenko1f228982008-04-22 00:16:29 +0000194
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000195 cnt1 = ny2pos - ny1pos;
196 nypos = ny1pos;
197 do {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200198 ptr = G.addr + (nypos * G.scr_var.xres + nx1pos) * G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000199 cnt2 = nx2pos - nx1pos;
200 do {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200201 fb_write_pixel(ptr, thispix);
202 ptr += G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000203 } while (--cnt2 >= 0);
Denis Vlasenko1f228982008-04-22 00:16:29 +0000204
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000205 nypos++;
206 } while (--cnt1 >= 0);
207}
208
209
210/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200211 * Draw a progress bar on framebuffer
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000212 * \param percent percentage of loading
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000213 */
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000214static void fb_drawprogressbar(unsigned percent)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000215{
216 int i, left_x, top_y, width, height;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000217
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000218 // outer box
219 left_x = G.nbar_posx;
220 top_y = G.nbar_posy;
221 width = G.nbar_width - 1;
222 height = G.nbar_height - 1;
223 if ((height | width) < 0)
224 return;
225 // NB: "width" of 1 actually makes rect with width of 2!
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000226 fb_drawrectangle();
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000227
228 // inner "empty" rectangle
229 left_x++;
230 top_y++;
231 width -= 2;
232 height -= 2;
233 if ((height | width) < 0)
234 return;
235 fb_drawfullrectangle(
236 left_x, top_y,
237 left_x + width, top_y + height,
238 G.nbar_colr, G.nbar_colg, G.nbar_colb);
239
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000240 if (percent > 0) {
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000241 // actual progress bar
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000242 width = width * percent / 100;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000243 i = height;
244 if (height == 0)
245 height++; // divide by 0 is bad
246 while (i >= 0) {
247 // draw one-line thick "rectangle"
248 // top line will have gray lvl 200, bottom one 100
249 unsigned gray_level = 100 + i*100/height;
250 fb_drawfullrectangle(
251 left_x, top_y, left_x + width, top_y,
252 gray_level, gray_level, gray_level);
253 top_y++;
254 i--;
255 }
256 }
257}
258
259
260/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200261 * Draw image from PPM file
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000262 */
263static void fb_drawimage(void)
264{
Bernhard Reutner-Fischer051fdb92009-02-16 12:36:50 +0000265 FILE *theme_file;
Denys Vlasenko814da222010-03-14 23:59:54 +0100266 char *read_ptr;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000267 unsigned char *pixline;
268 unsigned i, j, width, height, line_size;
269
Denys Vlasenko814da222010-03-14 23:59:54 +0100270 if (LONE_DASH(G.image_filename)) {
Vladimir Dronnikov6eaac022009-11-07 04:14:50 +0100271 theme_file = stdin;
Denys Vlasenko814da222010-03-14 23:59:54 +0100272 } else {
Vladimir Dronnikov6eaac022009-11-07 04:14:50 +0100273 int fd = open_zipped(G.image_filename);
274 if (fd < 0)
275 bb_simple_perror_msg_and_die(G.image_filename);
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +0100276 theme_file = xfdopen_for_read(fd);
Vladimir Dronnikov6eaac022009-11-07 04:14:50 +0100277 }
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000278
Denys Vlasenko814da222010-03-14 23:59:54 +0100279 /* Parse ppm header:
280 * - Magic: two characters "P6".
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000281 * - Whitespace (blanks, TABs, CRs, LFs).
282 * - A width, formatted as ASCII characters in decimal.
283 * - Whitespace.
Denys Vlasenko814da222010-03-14 23:59:54 +0100284 * - A height, ASCII decimal.
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000285 * - Whitespace.
Denys Vlasenko814da222010-03-14 23:59:54 +0100286 * - The maximum color value, ASCII decimal, in 0..65535
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000287 * - Newline or other single whitespace character.
Denys Vlasenko814da222010-03-14 23:59:54 +0100288 * (we support newline only)
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000289 * - A raster of Width * Height pixels in triplets of rgb
Denys Vlasenko814da222010-03-14 23:59:54 +0100290 * in pure binary by 1 or 2 bytes. (we support only 1 byte)
Bernhard Reutner-Fischer8dcb33c2009-02-18 15:13:05 +0000291 */
Denys Vlasenko814da222010-03-14 23:59:54 +0100292#define concat_buf bb_common_bufsiz1
293 read_ptr = concat_buf;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000294 while (1) {
Denys Vlasenko814da222010-03-14 23:59:54 +0100295 int w, h, max_color_val;
296 int rem = concat_buf + sizeof(concat_buf) - read_ptr;
297 if (rem < 2
298 || fgets(read_ptr, rem, theme_file) == NULL
299 ) {
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000300 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
Denys Vlasenko814da222010-03-14 23:59:54 +0100301 }
302 read_ptr = strchrnul(read_ptr, '#');
303 *read_ptr = '\0'; /* ignore #comments */
304 if (sscanf(concat_buf, "P6 %u %u %u", &w, &h, &max_color_val) == 3
305 && max_color_val <= 255
306 ) {
307 width = w; /* w is on stack, width may be in register */
308 height = h;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000309 break;
Denys Vlasenko814da222010-03-14 23:59:54 +0100310 }
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000311 }
312
313 line_size = width*3;
Denys Vlasenko814da222010-03-14 23:59:54 +0100314 pixline = xmalloc(line_size);
315
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000316 if (width > G.scr_var.xres)
317 width = G.scr_var.xres;
318 if (height > G.scr_var.yres)
319 height = G.scr_var.yres;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000320 for (j = 0; j < height; j++) {
Denys Vlasenko814da222010-03-14 23:59:54 +0100321 unsigned char *pixel;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200322 unsigned char *src;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000323
Bernhard Reutner-Fischer051fdb92009-02-16 12:36:50 +0000324 if (fread(pixline, 1, line_size, theme_file) != line_size)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000325 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
Denys Vlasenko814da222010-03-14 23:59:54 +0100326 pixel = pixline;
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200327 src = G.addr + j * G.scr_fix.line_length;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000328 for (i = 0; i < width; i++) {
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200329 unsigned thispix = fb_pixel_value(pixel[0], pixel[1], pixel[2]);
330 fb_write_pixel(src, thispix);
331 src += G.bytes_per_pixel;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000332 pixel += 3;
333 }
334 }
Denys Vlasenko814da222010-03-14 23:59:54 +0100335 free(pixline);
Bernhard Reutner-Fischer051fdb92009-02-16 12:36:50 +0000336 fclose(theme_file);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000337}
338
339
340/**
Nuno Lucas0e89fd92011-03-28 15:30:59 +0200341 * Parse configuration file
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000342 * \param *cfg_filename name of the configuration file
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000343 */
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000344static void init(const char *cfg_filename)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000345{
Denys Vlasenko10544a82010-02-09 00:26:10 +0100346 static const char param_names[] ALIGN1 =
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000347 "BAR_WIDTH\0" "BAR_HEIGHT\0"
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000348 "BAR_LEFT\0" "BAR_TOP\0"
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000349 "BAR_R\0" "BAR_G\0" "BAR_B\0"
350#if DEBUG
351 "DEBUG\0"
352#endif
353 ;
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000354 char *token[2];
355 parser_t *parser = config_open2(cfg_filename, xfopen_stdin);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000356 while (config_read(parser, token, 2, 2, "#=",
Denys Vlasenko814da222010-03-14 23:59:54 +0100357 (PARSE_NORMAL | PARSE_MIN_DIE) & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
Denys Vlasenko77832482010-08-12 14:14:45 +0200358 unsigned val = xatoi_positive(token[1]);
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000359 int i = index_in_strings(param_names, token[0]);
360 if (i < 0)
Bernhard Reutner-Fischer4efcec92009-02-14 12:36:16 +0000361 bb_error_msg_and_die("syntax error: %s", token[0]);
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000362 if (i >= 0 && i < 7)
363 G.ns[i] = val;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000364#if DEBUG
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000365 if (i == 7) {
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000366 G.bdebug_messages = val;
367 if (G.bdebug_messages)
Denis Vlasenko5415c852008-07-21 23:05:26 +0000368 G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log");
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000369 }
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000370#endif
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000371 }
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000372 config_close(parser);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000373}
374
375
376int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000377int fbsplash_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000378{
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000379 const char *fb_device, *cfg_filename, *fifo_filename;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000380 FILE *fp = fp; // for compiler
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000381 char *num_buf;
382 unsigned num;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000383 bool bCursorOff;
384
385 INIT_G();
386
387 // parse command line options
388 fb_device = "/dev/fb0";
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000389 cfg_filename = NULL;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000390 fifo_filename = NULL;
391 bCursorOff = 1 & getopt32(argv, "cs:d:i:f:",
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000392 &G.image_filename, &fb_device, &cfg_filename, &fifo_filename);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000393
394 // parse configuration file
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000395 if (cfg_filename)
396 init(cfg_filename);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000397
398 // We must have -s IMG
399 if (!G.image_filename)
400 bb_show_usage();
401
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000402 fb_open(fb_device);
403
404 if (fifo_filename && bCursorOff) {
405 // hide cursor (BEFORE any fb ops)
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200406 full_write(STDOUT_FILENO, "\033[?25l", 6);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000407 }
408
409 fb_drawimage();
410
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000411 if (!fifo_filename)
412 return EXIT_SUCCESS;
413
414 fp = xfopen_stdin(fifo_filename);
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000415 if (fp != stdin) {
Denis Vlasenko72b34422008-03-27 13:14:29 +0000416 // For named pipes, we want to support this:
417 // mkfifo cmd_pipe
418 // fbsplash -f cmd_pipe .... &
419 // ...
420 // echo 33 >cmd_pipe
421 // ...
422 // echo 66 >cmd_pipe
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000423 // This means that we don't want fbsplash to get EOF
424 // when last writer closes input end.
425 // The simplest way is to open fifo for writing too
426 // and become an additional writer :)
427 open(fifo_filename, O_WRONLY); // errors are ignored
428 }
429
430 fb_drawprogressbar(0);
431 // Block on read, waiting for some input.
432 // Use of <stdio.h> style I/O allows to correctly
433 // handle a case when we have many buffered lines
434 // already in the pipe
435 while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
436 if (strncmp(num_buf, "exit", 4) == 0) {
437 DEBUG_MESSAGE("exit");
438 break;
439 }
440 num = atoi(num_buf);
441 if (isdigit(num_buf[0]) && (num <= 100)) {
442#if DEBUG
Denys Vlasenko7bb346f2009-10-06 22:09:50 +0200443 DEBUG_MESSAGE(itoa(num));
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000444#endif
445 fb_drawprogressbar(num);
446 }
447 free(num_buf);
448 }
449
450 if (bCursorOff) // restore cursor
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200451 full_write(STDOUT_FILENO, "\033[?25h", 6);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000452
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000453 return EXIT_SUCCESS;
454}