Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame^] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * splash implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2008 Michele Sanges <michele.sanges@otomelara.it>, |
| 6 | * <michele.sanges@gmail.it> |
| 7 | * |
| 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 9 | * |
| 10 | * Usage: |
| 11 | * - use kernel option 'vga=xxx' or otherwise enable framebuffer device. |
| 12 | * - put somewhere the fbsplash.ini file and image in .ppm format. |
| 13 | * - configure applet by editing .ini file. |
| 14 | * - run applet: $ setsid fbsplash [params] & |
| 15 | * -c: hide cursor |
| 16 | * -d /dev/fbN: framebuffer device (if not /dev/fb0) |
| 17 | * -s path_of_image_file |
| 18 | * -i path_of_ini_file |
| 19 | * -f path_of_fifo (can be "-" for stdin) |
| 20 | * - if you want to run the applet only in presence of a kernel parameter |
| 21 | * (for example fbsplash=on), type: |
| 22 | * grep -q "fbsplash=on" </proc/cmdline && setsid fbsplash [params] |
| 23 | * - commands for fifo: |
| 24 | * "NN" (ASCII decimal number) - percentage to show on progress bar. |
| 25 | * "exit" (or just close fifo) - well you guessed it. |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * Splash implementation for busybox |
| 30 | * \file: fbsplash.c |
| 31 | * \author Michele Sanges <michele.sanges@otomelara.it> <michele.sanges@gmail.com> |
| 32 | * \version 1.0.0 |
| 33 | * \date 07/03/2008 |
| 34 | */ |
| 35 | |
| 36 | #include "libbb.h" |
| 37 | #include <linux/fb.h> |
| 38 | |
| 39 | /* If you want logging messages on /tmp/fbsplash_log... */ |
| 40 | #define DEBUG 0 |
| 41 | |
| 42 | #define BYTES_PER_PIXEL 2 |
| 43 | |
| 44 | typedef unsigned short DATA; |
| 45 | |
| 46 | struct globals { |
| 47 | #if DEBUG |
| 48 | bool bdebug_messages; // enable/disable logging |
| 49 | FILE *logfile_fd; // log file |
| 50 | #endif |
| 51 | unsigned char *addr; // pointer to framebuffer memory |
| 52 | unsigned nbar_width; // progress bar width |
| 53 | unsigned nbar_height; // progress bar height |
| 54 | unsigned nbar_posx; // progress bar horizontal position |
| 55 | unsigned nbar_posy; // progress bar vertical position |
| 56 | unsigned char nbar_colr; // progress bar color red component |
| 57 | unsigned char nbar_colg; // progress bar color green component |
| 58 | unsigned char nbar_colb; // progress bar color blue component |
| 59 | const char *image_filename; |
| 60 | struct fb_var_screeninfo scr_var; |
| 61 | struct fb_fix_screeninfo scr_fix; |
| 62 | }; |
| 63 | #define G (*ptr_to_globals) |
| 64 | #define INIT_G() \ |
| 65 | do { \ |
| 66 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 67 | } while (0) |
| 68 | |
| 69 | |
| 70 | #if DEBUG |
| 71 | #define DEBUG_MESSAGE(strMessage, args...) \ |
| 72 | if (G.bdebug_messages) { \ |
| 73 | fprintf(G.logfile_fd, "[%s][%s] - %s\n", \ |
| 74 | __FILE__, __FUNCTION__, strMessage); \ |
| 75 | } |
| 76 | #else |
| 77 | #define DEBUG_MESSAGE(...) ((void)0) |
| 78 | #endif |
| 79 | |
| 80 | |
| 81 | /** |
| 82 | * Open and initialize the framebuffer device |
| 83 | * \param *strfb_device pointer to framebuffer device |
| 84 | */ |
| 85 | static void fb_open(const char *strfb_device) |
| 86 | { |
| 87 | int fbfd = xopen(strfb_device, O_RDWR); |
| 88 | |
| 89 | // framebuffer properties |
| 90 | xioctl(fbfd, FBIOGET_VSCREENINFO, &G.scr_var); |
| 91 | xioctl(fbfd, FBIOGET_FSCREENINFO, &G.scr_fix); |
| 92 | |
| 93 | if (G.scr_var.bits_per_pixel != 16) |
| 94 | bb_error_msg_and_die("only 16 bpp is supported"); |
| 95 | |
| 96 | // map the device in memory |
| 97 | G.addr = mmap(NULL, |
| 98 | G.scr_var.xres * G.scr_var.yres |
| 99 | * BYTES_PER_PIXEL /*(G.scr_var.bits_per_pixel / 8)*/ , |
| 100 | PROT_WRITE, MAP_SHARED, fbfd, 0); |
| 101 | if (G.addr == MAP_FAILED) |
| 102 | bb_perror_msg_and_die("can't mmap %s", strfb_device); |
| 103 | close(fbfd); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /** |
| 108 | * Draw hollow rectangle on framebuffer |
| 109 | * \param nx1pos,ny1pos upper left position |
| 110 | * \param nx2pos,ny2pos down right position |
| 111 | * \param nred24,ngreen24,nblue24 rgb color |
| 112 | */ |
| 113 | static void fb_drawrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos, |
| 114 | unsigned char nred, unsigned char ngreen, unsigned char nblue) |
| 115 | { |
| 116 | int cnt; |
| 117 | DATA thispix; |
| 118 | DATA *ptr1, *ptr2; |
| 119 | |
| 120 | nred >>= 3; // 5-bit red |
| 121 | ngreen >>= 2; // 6-bit green |
| 122 | nblue >>= 3; // 5-bit blue |
| 123 | thispix = nblue + (ngreen << 5) + (nred << (5+6)); |
| 124 | |
| 125 | // horizontal lines |
| 126 | ptr1 = (DATA*)(G.addr + (ny1pos * G.scr_var.xres + nx1pos) * BYTES_PER_PIXEL); |
| 127 | ptr2 = (DATA*)(G.addr + (ny2pos * G.scr_var.xres + nx1pos) * BYTES_PER_PIXEL); |
| 128 | cnt = nx2pos - nx1pos; |
| 129 | do { |
| 130 | *ptr1++ = thispix; |
| 131 | *ptr2++ = thispix; |
| 132 | } while (--cnt >= 0); |
| 133 | |
| 134 | // vertical lines |
| 135 | ptr1 = (DATA*)(G.addr + (ny1pos * G.scr_var.xres + nx1pos) * BYTES_PER_PIXEL); |
| 136 | ptr2 = (DATA*)(G.addr + (ny1pos * G.scr_var.xres + nx2pos) * BYTES_PER_PIXEL); |
| 137 | cnt = ny2pos - ny1pos; |
| 138 | do { |
| 139 | *ptr1 = thispix; ptr1 += G.scr_var.xres; |
| 140 | *ptr2 = thispix; ptr2 += G.scr_var.xres; |
| 141 | } while (--cnt >= 0); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | /** |
| 146 | * Draw filled rectangle on framebuffer |
| 147 | * \param nx1pos,ny1pos upper left position |
| 148 | * \param nx2pos,ny2pos down right position |
| 149 | * \param nred24,ngreen24,nblue24 rgb color |
| 150 | */ |
| 151 | static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos, |
| 152 | unsigned char nred, unsigned char ngreen, unsigned char nblue) |
| 153 | { |
| 154 | int cnt1, cnt2, nypos; |
| 155 | DATA thispix; |
| 156 | DATA *ptr; |
| 157 | |
| 158 | nred >>= 3; // 5-bit red |
| 159 | ngreen >>= 2; // 6-bit green |
| 160 | nblue >>= 3; // 5-bit blue |
| 161 | thispix = nblue + (ngreen << 5) + (nred << (5+6)); |
| 162 | |
| 163 | cnt1 = ny2pos - ny1pos; |
| 164 | nypos = ny1pos; |
| 165 | do { |
| 166 | ptr = (DATA*)(G.addr + (nypos * G.scr_var.xres + nx1pos) * BYTES_PER_PIXEL); |
| 167 | cnt2 = nx2pos - nx1pos; |
| 168 | do { |
| 169 | *ptr++ = thispix; |
| 170 | } while (--cnt2 >= 0); |
| 171 | |
| 172 | nypos++; |
| 173 | } while (--cnt1 >= 0); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /** |
| 178 | * Draw a progress bar on framebuffer |
| 179 | * \param nPercent percentage of loading |
| 180 | */ |
| 181 | static void fb_drawprogressbar(unsigned nPercent) |
| 182 | { |
| 183 | int i, left_x, top_y, width, height; |
| 184 | // outer box |
| 185 | left_x = G.nbar_posx; |
| 186 | top_y = G.nbar_posy; |
| 187 | width = G.nbar_width - 1; |
| 188 | height = G.nbar_height - 1; |
| 189 | if ((height | width) < 0) |
| 190 | return; |
| 191 | // NB: "width" of 1 actually makes rect with width of 2! |
| 192 | fb_drawrectangle( |
| 193 | left_x, top_y, |
| 194 | left_x + width, top_y + height, |
| 195 | G.nbar_colr/2, G.nbar_colg/2, G.nbar_colb/2); |
| 196 | |
| 197 | // inner "empty" rectangle |
| 198 | left_x++; |
| 199 | top_y++; |
| 200 | width -= 2; |
| 201 | height -= 2; |
| 202 | if ((height | width) < 0) |
| 203 | return; |
| 204 | fb_drawfullrectangle( |
| 205 | left_x, top_y, |
| 206 | left_x + width, top_y + height, |
| 207 | G.nbar_colr, G.nbar_colg, G.nbar_colb); |
| 208 | |
| 209 | if (nPercent > 0) { |
| 210 | // actual progress bar |
| 211 | width = width*nPercent/100; |
| 212 | i = height; |
| 213 | if (height == 0) |
| 214 | height++; // divide by 0 is bad |
| 215 | while (i >= 0) { |
| 216 | // draw one-line thick "rectangle" |
| 217 | // top line will have gray lvl 200, bottom one 100 |
| 218 | unsigned gray_level = 100 + i*100/height; |
| 219 | fb_drawfullrectangle( |
| 220 | left_x, top_y, left_x + width, top_y, |
| 221 | gray_level, gray_level, gray_level); |
| 222 | top_y++; |
| 223 | i--; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | |
| 229 | /** |
| 230 | * Draw image from PPM file |
| 231 | */ |
| 232 | static void fb_drawimage(void) |
| 233 | { |
| 234 | char head[256]; |
| 235 | char s[80]; |
| 236 | FILE *theme_file; |
| 237 | unsigned char *pixline; |
| 238 | unsigned i, j, width, height, line_size; |
| 239 | |
| 240 | memset(head, 0, sizeof(head)); |
| 241 | theme_file = xfopen_stdin(G.image_filename); |
| 242 | |
| 243 | // parse ppm header |
| 244 | while (1) { |
| 245 | if (fgets(s, sizeof(s), theme_file) == NULL) |
| 246 | bb_error_msg_and_die("bad PPM file '%s'", G.image_filename); |
| 247 | |
| 248 | if (s[0] == '#') |
| 249 | continue; |
| 250 | |
| 251 | if (strlen(head) + strlen(s) >= sizeof(head)) |
| 252 | bb_error_msg_and_die("bad PPM file '%s'", G.image_filename); |
| 253 | |
| 254 | strcat(head, s); |
| 255 | if (head[0] != 'P' || head[1] != '6') |
| 256 | bb_error_msg_and_die("bad PPM file '%s'", G.image_filename); |
| 257 | |
| 258 | // width, height, max_color_val |
| 259 | if (sscanf(head, "P6 %u %u %u", &width, &height, &i) == 3) |
| 260 | break; |
| 261 | // TODO: i must be <= 255! |
| 262 | } |
| 263 | |
| 264 | line_size = width*3; |
| 265 | if (width > G.scr_var.xres) |
| 266 | width = G.scr_var.xres; |
| 267 | if (height > G.scr_var.yres) |
| 268 | height = G.scr_var.yres; |
| 269 | |
| 270 | pixline = xmalloc(line_size); |
| 271 | for (j = 0; j < height; j++) { |
| 272 | unsigned char *pixel = pixline; |
| 273 | DATA *src = (DATA *)(G.addr + j * G.scr_fix.line_length); |
| 274 | |
| 275 | if (fread(pixline, 1, line_size, theme_file) != line_size) |
| 276 | bb_error_msg_and_die("bad PPM file '%s'", G.image_filename); |
| 277 | for (i = 0; i < width; i++) { |
| 278 | unsigned thispix; |
| 279 | thispix = (((unsigned)pixel[0] << 8) & 0xf800) |
| 280 | | (((unsigned)pixel[1] << 3) & 0x07e0) |
| 281 | | (((unsigned)pixel[2] >> 3)); |
| 282 | *src++ = thispix; |
| 283 | pixel += 3; |
| 284 | } |
| 285 | } |
| 286 | free(pixline); |
| 287 | fclose(theme_file); |
| 288 | } |
| 289 | |
| 290 | |
| 291 | /** |
| 292 | * Parse configuration file |
| 293 | */ |
| 294 | static void init(const char *ini_filename) |
| 295 | { |
| 296 | static const char const param_names[] ALIGN1 = |
| 297 | "BAR_LEFT\0" "BAR_TOP\0" |
| 298 | "BAR_WIDTH\0" "BAR_HEIGHT\0" |
| 299 | "BAR_R\0" "BAR_G\0" "BAR_B\0" |
| 300 | #if DEBUG |
| 301 | "DEBUG\0" |
| 302 | #endif |
| 303 | ; |
| 304 | |
| 305 | FILE *inifile; |
| 306 | char *buf; |
| 307 | |
| 308 | inifile = xfopen(ini_filename, "r"); |
| 309 | |
| 310 | while ((buf = xmalloc_getline(inifile)) != NULL) { |
| 311 | char *value_str; |
| 312 | int val; |
| 313 | |
| 314 | if (*buf == '#') { // it's a comment |
| 315 | free(buf); |
| 316 | continue; |
| 317 | } |
| 318 | |
| 319 | value_str = strchr(buf, '='); |
| 320 | if (!value_str) |
| 321 | goto err; |
| 322 | *value_str++ = '\0'; |
| 323 | val = xatoi_u(value_str); |
| 324 | |
| 325 | switch (index_in_strings(param_names, buf)) { |
| 326 | case 0: |
| 327 | // progress bar horizontal position |
| 328 | G.nbar_posx = val; |
| 329 | break; |
| 330 | case 1: |
| 331 | // progress bar vertical position |
| 332 | G.nbar_posy = val; |
| 333 | break; |
| 334 | case 2: |
| 335 | // progress bar width |
| 336 | G.nbar_width = val; |
| 337 | break; |
| 338 | case 3: |
| 339 | // progress bar height |
| 340 | G.nbar_height = val; |
| 341 | break; |
| 342 | case 4: |
| 343 | // progress bar color - red component |
| 344 | G.nbar_colr = val; |
| 345 | break; |
| 346 | case 5: |
| 347 | // progress bar color - green component |
| 348 | G.nbar_colg = val; |
| 349 | break; |
| 350 | case 6: |
| 351 | // progress bar color - blue component |
| 352 | G.nbar_colb = val; |
| 353 | break; |
| 354 | #if DEBUG |
| 355 | case 7: |
| 356 | G.bdebug_messages = val; |
| 357 | if (G.bdebug_messages) |
| 358 | G.logfile_fd = xfopen("/tmp/fbsplash_log", "w"); |
| 359 | break; |
| 360 | #endif |
| 361 | err: |
| 362 | default: |
| 363 | bb_error_msg_and_die("syntax error: '%s'", buf); |
| 364 | } |
| 365 | free(buf); |
| 366 | } |
| 367 | fclose(inifile); |
| 368 | } |
| 369 | |
| 370 | |
| 371 | int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 372 | int fbsplash_main(int argc ATTRIBUTE_UNUSED, char **argv) |
| 373 | { |
| 374 | char num_buf[16]; |
| 375 | const char *fb_device, *ini_filename, *fifo_filename; |
| 376 | int fd = fd; // for compiler |
| 377 | int len, num; |
| 378 | bool bCursorOff; |
| 379 | |
| 380 | INIT_G(); |
| 381 | |
| 382 | // parse command line options |
| 383 | fb_device = "/dev/fb0"; |
| 384 | ini_filename = NULL; |
| 385 | fifo_filename = NULL; |
| 386 | bCursorOff = 1 & getopt32(argv, "cs:d:i:f:", |
| 387 | &G.image_filename, &fb_device, &ini_filename, &fifo_filename); |
| 388 | |
| 389 | // parse configuration file |
| 390 | if (ini_filename) |
| 391 | init(ini_filename); |
| 392 | |
| 393 | // We must have -s IMG |
| 394 | if (!G.image_filename) |
| 395 | bb_show_usage(); |
| 396 | |
| 397 | if (fifo_filename) { |
| 398 | fd = STDIN_FILENO; |
| 399 | if (NOT_LONE_DASH(fifo_filename)) { |
| 400 | // open command fifo/pipe |
| 401 | fd = xopen(fifo_filename, O_RDONLY | O_NOCTTY); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | fb_open(fb_device); |
| 406 | |
| 407 | if (fifo_filename && bCursorOff) { |
| 408 | // hide cursor (BEFORE any fb ops) |
| 409 | full_write(STDOUT_FILENO, "\x1b" "[?25l", 6); |
| 410 | } |
| 411 | |
| 412 | fb_drawimage(); |
| 413 | |
| 414 | if (fifo_filename) { |
| 415 | num = 0; |
| 416 | goto draw_bar; |
| 417 | |
| 418 | while (1) { |
| 419 | // block on read, waiting for some input |
| 420 | len = safe_read(fd, num_buf, sizeof(num_buf) - 1); |
| 421 | if (len <= 0) // EOF/error |
| 422 | break; |
| 423 | num_buf[len] = '\0'; |
| 424 | // parse command |
| 425 | if (strncmp(num_buf, "exit", 4) == 0) { |
| 426 | DEBUG_MESSAGE("exit"); |
| 427 | break; |
| 428 | } |
| 429 | num = atoi(num_buf); |
| 430 | if (isdigit(num_buf[0]) && (num >= 0) && (num <= 100)) { |
| 431 | #if DEBUG |
| 432 | char strVal[10]; |
| 433 | sprintf(strVal, "%d", num); |
| 434 | DEBUG_MESSAGE(strVal); |
| 435 | #endif |
| 436 | draw_bar: |
| 437 | fb_drawprogressbar(num); |
| 438 | } |
| 439 | } |
| 440 | if (bCursorOff) { |
| 441 | // restore cursor |
| 442 | full_write(STDOUT_FILENO, "\x1b" "[?25h", 6); |
| 443 | } |
| 444 | if (ENABLE_FEATURE_CLEAN_UP) |
| 445 | close(fd); |
| 446 | } |
| 447 | |
| 448 | #if DEBUG |
| 449 | if (ENABLE_FEATURE_CLEAN_UP) |
| 450 | if (G.bdebug_messages) |
| 451 | fclose(G.logfile_fd); |
| 452 | #endif |
| 453 | |
| 454 | return EXIT_SUCCESS; |
| 455 | } |