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 nbar_width; // progress bar width
42 unsigned nbar_height; // progress bar height
43 unsigned nbar_posx; // progress bar horizontal position
44 unsigned nbar_posy; // progress bar vertical position
45 unsigned char nbar_colr; // progress bar color red component
46 unsigned char nbar_colg; // progress bar color green component
47 unsigned char nbar_colb; // progress bar color blue component
48 const char *image_filename;
49 struct fb_var_screeninfo scr_var;
50 struct fb_fix_screeninfo scr_fix;
52 #define G (*ptr_to_globals)
55 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
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
98 * \param nx1pos,ny1pos upper left position
99 * \param nx2pos,ny2pos down right position
100 * \param nred,ngreen,nblue rgb color
102 static void fb_drawrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
103 unsigned char nred, unsigned char ngreen, unsigned char nblue)
109 nred >>= 3; // 5-bit red
110 ngreen >>= 2; // 6-bit green
111 nblue >>= 3; // 5-bit blue
112 thispix = nblue + (ngreen << 5) + (nred << (5+6));
115 ptr1 = (DATA*)(G.addr + (ny1pos * G.scr_var.xres + nx1pos) * BYTES_PER_PIXEL);
116 ptr2 = (DATA*)(G.addr + (ny2pos * G.scr_var.xres + nx1pos) * BYTES_PER_PIXEL);
117 cnt = nx2pos - nx1pos;
121 } while (--cnt >= 0);
124 ptr1 = (DATA*)(G.addr + (ny1pos * G.scr_var.xres + nx1pos) * BYTES_PER_PIXEL);
125 ptr2 = (DATA*)(G.addr + (ny1pos * G.scr_var.xres + nx2pos) * BYTES_PER_PIXEL);
126 cnt = ny2pos - ny1pos;
128 *ptr1 = thispix; ptr1 += G.scr_var.xres;
129 *ptr2 = thispix; ptr2 += G.scr_var.xres;
130 } while (--cnt >= 0);
135 * Draw filled rectangle on framebuffer
136 * \param nx1pos,ny1pos upper left position
137 * \param nx2pos,ny2pos down right position
138 * \param nred,ngreen,nblue rgb color
140 static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
141 unsigned char nred, unsigned char ngreen, unsigned char nblue)
143 int cnt1, cnt2, nypos;
147 nred >>= 3; // 5-bit red
148 ngreen >>= 2; // 6-bit green
149 nblue >>= 3; // 5-bit blue
150 thispix = nblue + (ngreen << 5) + (nred << (5+6));
152 cnt1 = ny2pos - ny1pos;
155 ptr = (DATA*)(G.addr + (nypos * G.scr_var.xres + nx1pos) * BYTES_PER_PIXEL);
156 cnt2 = nx2pos - nx1pos;
159 } while (--cnt2 >= 0);
162 } while (--cnt1 >= 0);
167 * Draw a progress bar on framebuffer
168 * \param percent percentage of loading
170 static void fb_drawprogressbar(unsigned percent)
172 int i, left_x, top_y, width, height;
175 left_x = G.nbar_posx;
177 width = G.nbar_width - 1;
178 height = G.nbar_height - 1;
179 if ((height | width) < 0)
181 // NB: "width" of 1 actually makes rect with width of 2!
184 left_x + width, top_y + height,
185 G.nbar_colr/2, G.nbar_colg/2, G.nbar_colb/2);
187 // inner "empty" rectangle
192 if ((height | width) < 0)
194 fb_drawfullrectangle(
196 left_x + width, top_y + height,
197 G.nbar_colr, G.nbar_colg, G.nbar_colb);
200 // actual progress bar
201 width = width * percent / 100;
204 height++; // divide by 0 is bad
206 // draw one-line thick "rectangle"
207 // top line will have gray lvl 200, bottom one 100
208 unsigned gray_level = 100 + i*100/height;
209 fb_drawfullrectangle(
210 left_x, top_y, left_x + width, top_y,
211 gray_level, gray_level, gray_level);
220 * Draw image from PPM file
222 static void fb_drawimage(void)
227 unsigned char *pixline;
228 unsigned i, j, width, height, line_size;
230 memset(head, 0, sizeof(head));
231 theme_file = xfopen_stdin(G.image_filename);
235 if (fgets(s, sizeof(s), theme_file) == NULL)
236 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
241 if (strlen(head) + strlen(s) >= sizeof(head))
242 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
245 if (head[0] != 'P' || head[1] != '6')
246 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
248 // width, height, max_color_val
249 if (sscanf(head, "P6 %u %u %u", &width, &height, &i) == 3)
251 // TODO: i must be <= 255!
255 if (width > G.scr_var.xres)
256 width = G.scr_var.xres;
257 if (height > G.scr_var.yres)
258 height = G.scr_var.yres;
260 pixline = xmalloc(line_size);
261 for (j = 0; j < height; j++) {
262 unsigned char *pixel = pixline;
263 DATA *src = (DATA *)(G.addr + j * G.scr_fix.line_length);
265 if (fread(pixline, 1, line_size, theme_file) != line_size)
266 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
267 for (i = 0; i < width; i++) {
269 thispix = (((unsigned)pixel[0] << 8) & 0xf800)
270 | (((unsigned)pixel[1] << 3) & 0x07e0)
271 | (((unsigned)pixel[2] >> 3));
282 * Parse configuration file
284 static void init(const char *ini_filename)
286 static const char const param_names[] ALIGN1 =
287 "BAR_LEFT\0" "BAR_TOP\0"
288 "BAR_WIDTH\0" "BAR_HEIGHT\0"
289 "BAR_R\0" "BAR_G\0" "BAR_B\0"
298 inifile = xfopen_stdin(ini_filename);
300 while ((buf = xmalloc_fgetline(inifile)) != NULL) {
304 if (*buf == '#') { // it's a comment
309 value_str = strchr(buf, '=');
313 val = xatoi_u(value_str);
315 switch (index_in_strings(param_names, buf)) {
317 // progress bar horizontal position
321 // progress bar vertical position
325 // progress bar width
329 // progress bar height
333 // progress bar color - red component
337 // progress bar color - green component
341 // progress bar color - blue component
346 G.bdebug_messages = val;
347 if (G.bdebug_messages)
348 G.logfile_fd = xfopen("/tmp/fbsplash.log", "w");
353 bb_error_msg_and_die("syntax error: '%s'", buf);
361 int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
362 int fbsplash_main(int argc ATTRIBUTE_UNUSED, char **argv)
364 const char *fb_device, *ini_filename, *fifo_filename;
365 FILE *fp = fp; // for compiler
370 // parse command line options
371 fb_device = "/dev/fb0";
373 fifo_filename = NULL;
374 bCursorOff = 1 & getopt32(argv, "cs:d:i:f:",
375 &G.image_filename, &fb_device, &ini_filename, &fifo_filename);
377 // parse configuration file
381 // We must have -s IMG
382 if (!G.image_filename)
386 fp = xfopen_stdin(fifo_filename);
390 if (fifo_filename && bCursorOff) {
391 // hide cursor (BEFORE any fb ops)
392 full_write(STDOUT_FILENO, "\x1b" "[?25l", 6);
401 fb_drawprogressbar(0);
402 // Block on read, waiting for some input.
403 // Use of <stdio.h> style I/O allows to correctly
404 // handle a case when we have many buffered lines
405 // already in the pipe.
406 while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
407 if (strncmp(num_buf, "exit", 4) == 0) {
408 DEBUG_MESSAGE("exit");
412 if (isdigit(num_buf[0]) && (num <= 100)) {
415 sprintf(strVal, "%d", num);
416 DEBUG_MESSAGE(strVal);
418 fb_drawprogressbar(num);
424 full_write(STDOUT_FILENO, "\x1b" "[?25h", 6);
426 if (ENABLE_FEATURE_CLEAN_UP)