1 /* vi: set sw=4 ts=4: */
3 * Copyright (C) 2008 Michele Sanges <michele.sanges@otomelara.it>,
4 * <michele.sanges@gmail.it>
6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9 * - use kernel option 'vga=xxx' or otherwise enable framebuffer device.
10 * - put somewhere fbsplash.cfg file and an image in .ppm format.
11 * - run applet: $ setsid fbsplash [params] &
13 * -d /dev/fbN: framebuffer device (if not /dev/fb0)
14 * -s path_to_image_file (can be "-" for stdin)
16 * -f path_to_fifo (can be "-" for stdin)
17 * - if you want to run it only in presence of a kernel parameter
18 * (for example fbsplash=on), use:
19 * grep -q "fbsplash=on" </proc/cmdline && setsid fbsplash [params]
20 * - commands for fifo:
21 * "NN" (ASCII decimal number) - percentage to show on progress bar.
22 * "exit" (or just close fifo) - well you guessed it.
28 /* If you want logging messages on /tmp/fbsplash.log... */
31 #define BYTES_PER_PIXEL 2
33 typedef unsigned short DATA;
37 bool bdebug_messages; // enable/disable logging
38 FILE *logfile_fd; // log file
40 unsigned char *addr; // pointer to framebuffer memory
41 unsigned ns[7]; // n-parameters
42 const char *image_filename;
43 struct fb_var_screeninfo scr_var;
44 struct fb_fix_screeninfo scr_fix;
46 #define G (*ptr_to_globals)
47 #define INIT_G() do { \
48 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
51 #define nbar_width ns[0] // progress bar width
52 #define nbar_height ns[1] // progress bar height
53 #define nbar_posx ns[2] // progress bar horizontal position
54 #define nbar_posy ns[3] // progress bar vertical position
55 #define nbar_colr ns[4] // progress bar color red component
56 #define nbar_colg ns[5] // progress bar color green component
57 #define nbar_colb ns[6] // progress bar color blue component
60 #define DEBUG_MESSAGE(strMessage, args...) \
61 if (G.bdebug_messages) { \
62 fprintf(G.logfile_fd, "[%s][%s] - %s\n", \
63 __FILE__, __FUNCTION__, strMessage); \
66 #define DEBUG_MESSAGE(...) ((void)0)
71 * Open and initialize the framebuffer device
72 * \param *strfb_device pointer to framebuffer device
74 static void fb_open(const char *strfb_device)
76 int fbfd = xopen(strfb_device, O_RDWR);
78 // framebuffer properties
79 xioctl(fbfd, FBIOGET_VSCREENINFO, &G.scr_var);
80 xioctl(fbfd, FBIOGET_FSCREENINFO, &G.scr_fix);
82 if (G.scr_var.bits_per_pixel != 16)
83 bb_error_msg_and_die("only 16 bpp is supported");
85 // map the device in memory
87 G.scr_var.xres * G.scr_var.yres
88 * BYTES_PER_PIXEL /*(G.scr_var.bits_per_pixel / 8)*/ ,
89 PROT_WRITE, MAP_SHARED, fbfd, 0);
90 if (G.addr == MAP_FAILED)
91 bb_perror_msg_and_die("can't mmap %s", strfb_device);
97 * Draw hollow rectangle on framebuffer
99 static void fb_drawrectangle(void)
104 unsigned char nred = G.nbar_colr/2;
105 unsigned char ngreen = G.nbar_colg/2;
106 unsigned char nblue = G.nbar_colb/2;
108 nred >>= 3; // 5-bit red
109 ngreen >>= 2; // 6-bit green
110 nblue >>= 3; // 5-bit blue
111 thispix = nblue + (ngreen << 5) + (nred << (5+6));
114 ptr1 = (DATA*)(G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx) * BYTES_PER_PIXEL);
115 ptr2 = (DATA*)(G.addr + ((G.nbar_posy + G.nbar_height - 1) * G.scr_var.xres + G.nbar_posx) * BYTES_PER_PIXEL);
116 cnt = G.nbar_width - 1;
120 } while (--cnt >= 0);
123 ptr1 = (DATA*)(G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx) * BYTES_PER_PIXEL);
124 ptr2 = (DATA*)(G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx + G.nbar_width - 1) * BYTES_PER_PIXEL);
125 cnt = G.nbar_posy + G.nbar_height - 1 - G.nbar_posy;
127 *ptr1 = thispix; ptr1 += G.scr_var.xres;
128 *ptr2 = thispix; ptr2 += G.scr_var.xres;
129 } while (--cnt >= 0);
134 * Draw filled rectangle on framebuffer
135 * \param nx1pos,ny1pos upper left position
136 * \param nx2pos,ny2pos down right position
137 * \param nred,ngreen,nblue rgb color
139 static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
140 unsigned char nred, unsigned char ngreen, unsigned char nblue)
142 int cnt1, cnt2, nypos;
146 nred >>= 3; // 5-bit red
147 ngreen >>= 2; // 6-bit green
148 nblue >>= 3; // 5-bit blue
149 thispix = nblue + (ngreen << 5) + (nred << (5+6));
151 cnt1 = ny2pos - ny1pos;
154 ptr = (DATA*)(G.addr + (nypos * G.scr_var.xres + nx1pos) * BYTES_PER_PIXEL);
155 cnt2 = nx2pos - nx1pos;
158 } while (--cnt2 >= 0);
161 } while (--cnt1 >= 0);
166 * Draw a progress bar on framebuffer
167 * \param percent percentage of loading
169 static void fb_drawprogressbar(unsigned percent)
171 int i, left_x, top_y, width, height;
174 left_x = G.nbar_posx;
176 width = G.nbar_width - 1;
177 height = G.nbar_height - 1;
178 if ((height | width) < 0)
180 // NB: "width" of 1 actually makes rect with width of 2!
183 // inner "empty" rectangle
188 if ((height | width) < 0)
190 fb_drawfullrectangle(
192 left_x + width, top_y + height,
193 G.nbar_colr, G.nbar_colg, G.nbar_colb);
196 // actual progress bar
197 width = width * percent / 100;
200 height++; // divide by 0 is bad
202 // draw one-line thick "rectangle"
203 // top line will have gray lvl 200, bottom one 100
204 unsigned gray_level = 100 + i*100/height;
205 fb_drawfullrectangle(
206 left_x, top_y, left_x + width, top_y,
207 gray_level, gray_level, gray_level);
216 * Draw image from PPM file
218 static void fb_drawimage(void)
223 unsigned char *pixline;
224 unsigned i, j, width, height, line_size;
226 memset(head, 0, sizeof(head));
227 theme_file = xfopen_stdin(G.image_filename);
231 if (fgets(s, sizeof(s), theme_file) == NULL)
232 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
237 if (strlen(head) + strlen(s) >= sizeof(head))
238 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
241 if (head[0] != 'P' || head[1] != '6')
242 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
244 // width, height, max_color_val
245 if (sscanf(head, "P6 %u %u %u", &width, &height, &i) == 3)
247 // TODO: i must be <= 255!
251 if (width > G.scr_var.xres)
252 width = G.scr_var.xres;
253 if (height > G.scr_var.yres)
254 height = G.scr_var.yres;
256 pixline = xmalloc(line_size);
257 for (j = 0; j < height; j++) {
258 unsigned char *pixel = pixline;
259 DATA *src = (DATA *)(G.addr + j * G.scr_fix.line_length);
261 if (fread(pixline, 1, line_size, theme_file) != line_size)
262 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
263 for (i = 0; i < width; i++) {
265 thispix = (((unsigned)pixel[0] << 8) & 0xf800)
266 | (((unsigned)pixel[1] << 3) & 0x07e0)
267 | (((unsigned)pixel[2] >> 3));
278 * Parse configuration file
279 * \param *cfg_filename name of the configuration file
281 static void init(const char *cfg_filename)
283 static const char const param_names[] ALIGN1 =
284 "BAR_WIDTH\0" "BAR_HEIGHT\0"
285 "BAR_LEFT\0" "BAR_TOP\0"
286 "BAR_R\0" "BAR_G\0" "BAR_B\0"
292 parser_t *parser = config_open2(cfg_filename, xfopen_stdin);
293 while (config_read(parser, token, 2, 2, "#=",
294 (PARSE_NORMAL | PARSE_MIN_DIE) & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
295 unsigned val = xatoi_u(token[1]);
296 int i = index_in_strings(param_names, token[0]);
298 bb_error_msg_and_die("syntax error: '%s'", token[0]);
303 G.bdebug_messages = val;
304 if (G.bdebug_messages)
305 G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log");
309 config_close(parser);
313 int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
314 int fbsplash_main(int argc UNUSED_PARAM, char **argv)
316 const char *fb_device, *cfg_filename, *fifo_filename;
317 FILE *fp = fp; // for compiler
324 // parse command line options
325 fb_device = "/dev/fb0";
327 fifo_filename = NULL;
328 bCursorOff = 1 & getopt32(argv, "cs:d:i:f:",
329 &G.image_filename, &fb_device, &cfg_filename, &fifo_filename);
331 // parse configuration file
335 // We must have -s IMG
336 if (!G.image_filename)
341 if (fifo_filename && bCursorOff) {
342 // hide cursor (BEFORE any fb ops)
343 full_write(STDOUT_FILENO, "\x1b" "[?25l", 6);
351 fp = xfopen_stdin(fifo_filename);
353 // For named pipes, we want to support this:
355 // fbsplash -f cmd_pipe .... &
360 // This means that we don't want fbsplash to get EOF
361 // when last writer closes input end.
362 // The simplest way is to open fifo for writing too
363 // and become an additional writer :)
364 open(fifo_filename, O_WRONLY); // errors are ignored
367 fb_drawprogressbar(0);
368 // Block on read, waiting for some input.
369 // Use of <stdio.h> style I/O allows to correctly
370 // handle a case when we have many buffered lines
371 // already in the pipe
372 while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
373 if (strncmp(num_buf, "exit", 4) == 0) {
374 DEBUG_MESSAGE("exit");
378 if (isdigit(num_buf[0]) && (num <= 100)) {
381 sprintf(strVal, "%d", num);
382 DEBUG_MESSAGE(strVal);
384 fb_drawprogressbar(num);
389 if (bCursorOff) // restore cursor
390 full_write(STDOUT_FILENO, "\x1b" "[?25h", 6);