fbsplash.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2008 Michele Sanges <michele.sanges@gmail.com>
  4. *
  5. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  6. *
  7. * Usage:
  8. * - use kernel option 'vga=xxx' or otherwise enable framebuffer device.
  9. * - put somewhere fbsplash.cfg file and an image in .ppm format.
  10. * - run applet: $ setsid fbsplash [params] &
  11. * -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)
  16. * - if you want to run it only in presence of a kernel parameter
  17. * (for example fbsplash=on), use:
  18. * 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. //usage:#define fbsplash_trivial_usage
  24. //usage: "-s IMGFILE [-c] [-d DEV] [-i INIFILE] [-f CMD]"
  25. //usage:#define fbsplash_full_usage "\n\n"
  26. //usage: " -s Image"
  27. //usage: "\n -c Hide cursor"
  28. //usage: "\n -d Framebuffer device (default /dev/fb0)"
  29. //usage: "\n -i Config file (var=value):"
  30. //usage: "\n BAR_LEFT,BAR_TOP,BAR_WIDTH,BAR_HEIGHT"
  31. //usage: "\n BAR_R,BAR_G,BAR_B"
  32. //usage: "\n -f Control pipe (else exit after drawing image)"
  33. //usage: "\n commands: 'NN' (% for progress bar) or 'exit'"
  34. #include "libbb.h"
  35. #include <linux/fb.h>
  36. /* If you want logging messages on /tmp/fbsplash.log... */
  37. #define DEBUG 0
  38. struct globals {
  39. #if DEBUG
  40. bool bdebug_messages; // enable/disable logging
  41. FILE *logfile_fd; // log file
  42. #endif
  43. unsigned char *addr; // pointer to framebuffer memory
  44. unsigned ns[7]; // n-parameters
  45. const char *image_filename;
  46. struct fb_var_screeninfo scr_var;
  47. struct fb_fix_screeninfo scr_fix;
  48. unsigned bytes_per_pixel;
  49. // cached (8 - scr_var.COLOR.length):
  50. unsigned red_shift;
  51. unsigned green_shift;
  52. unsigned blue_shift;
  53. };
  54. #define G (*ptr_to_globals)
  55. #define INIT_G() do { \
  56. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  57. } while (0)
  58. #define nbar_width ns[0] // progress bar width
  59. #define nbar_height ns[1] // progress bar height
  60. #define nbar_posx ns[2] // progress bar horizontal position
  61. #define nbar_posy ns[3] // progress bar vertical position
  62. #define nbar_colr ns[4] // progress bar color red component
  63. #define nbar_colg ns[5] // progress bar color green component
  64. #define nbar_colb ns[6] // progress bar color blue component
  65. #if DEBUG
  66. #define DEBUG_MESSAGE(strMessage, args...) \
  67. if (G.bdebug_messages) { \
  68. fprintf(G.logfile_fd, "[%s][%s] - %s\n", \
  69. __FILE__, __FUNCTION__, strMessage); \
  70. }
  71. #else
  72. #define DEBUG_MESSAGE(...) ((void)0)
  73. #endif
  74. /**
  75. * Configure palette for RGB:332
  76. */
  77. static void fb_setpal(int fd)
  78. {
  79. struct fb_cmap cmap;
  80. /* fb colors are 16 bit */
  81. unsigned short red[256], green[256], blue[256];
  82. unsigned i;
  83. /* RGB:332 */
  84. for (i = 0; i < 256; i++) {
  85. /* Color is encoded in pixel value as rrrgggbb.
  86. * 3-bit color is mapped to 16-bit one as:
  87. * 000 -> 00000000 00000000
  88. * 001 -> 00100100 10010010
  89. * ...
  90. * 011 -> 01101101 10110110
  91. * 100 -> 10010010 01001001
  92. * ...
  93. * 111 -> 11111111 11111111
  94. */
  95. red[i] = (( i >> 5 ) * 0x9249) >> 2; // rrr * 00 10010010 01001001 >> 2
  96. green[i] = (((i >> 2) & 0x7) * 0x9249) >> 2; // ggg * 00 10010010 01001001 >> 2
  97. /* 2-bit color is easier: */
  98. blue[i] = ( i & 0x3) * 0x5555; // bb * 01010101 01010101
  99. }
  100. cmap.start = 0;
  101. cmap.len = 256;
  102. cmap.red = red;
  103. cmap.green = green;
  104. cmap.blue = blue;
  105. cmap.transp = 0;
  106. xioctl(fd, FBIOPUTCMAP, &cmap);
  107. }
  108. /**
  109. * Open and initialize the framebuffer device
  110. * \param *strfb_device pointer to framebuffer device
  111. */
  112. static void fb_open(const char *strfb_device)
  113. {
  114. int fbfd = xopen(strfb_device, O_RDWR);
  115. // framebuffer properties
  116. xioctl(fbfd, FBIOGET_VSCREENINFO, &G.scr_var);
  117. xioctl(fbfd, FBIOGET_FSCREENINFO, &G.scr_fix);
  118. switch (G.scr_var.bits_per_pixel) {
  119. case 8:
  120. fb_setpal(fbfd);
  121. break;
  122. case 16:
  123. case 24:
  124. case 32:
  125. break;
  126. default:
  127. bb_error_msg_and_die("unsupported %u bpp", (int)G.scr_var.bits_per_pixel);
  128. break;
  129. }
  130. G.red_shift = 8 - G.scr_var.red.length;
  131. G.green_shift = 8 - G.scr_var.green.length;
  132. G.blue_shift = 8 - G.scr_var.blue.length;
  133. G.bytes_per_pixel = (G.scr_var.bits_per_pixel + 7) >> 3;
  134. // map the device in memory
  135. G.addr = mmap(NULL,
  136. G.scr_var.yres * G.scr_fix.line_length,
  137. PROT_WRITE, MAP_SHARED, fbfd, 0);
  138. if (G.addr == MAP_FAILED)
  139. bb_perror_msg_and_die("mmap");
  140. // point to the start of the visible screen
  141. G.addr += G.scr_var.yoffset * G.scr_fix.line_length + G.scr_var.xoffset * G.bytes_per_pixel;
  142. close(fbfd);
  143. }
  144. /**
  145. * Return pixel value of the passed RGB color.
  146. * This is performance critical fn.
  147. */
  148. static unsigned fb_pixel_value(unsigned r, unsigned g, unsigned b)
  149. {
  150. /* We assume that the r,g,b values are <= 255 */
  151. if (G.bytes_per_pixel == 1) {
  152. r = r & 0xe0; // 3-bit red
  153. g = (g >> 3) & 0x1c; // 3-bit green
  154. b = b >> 6; // 2-bit blue
  155. return r + g + b;
  156. }
  157. if (G.bytes_per_pixel == 2) {
  158. // ARM PL110 on Integrator/CP has RGBA5551 bit arrangement.
  159. // We want to support bit locations like that.
  160. //
  161. // First shift out unused bits
  162. r = r >> G.red_shift;
  163. g = g >> G.green_shift;
  164. b = b >> G.blue_shift;
  165. // Then shift the remaining bits to their offset
  166. return (r << G.scr_var.red.offset) +
  167. (g << G.scr_var.green.offset) +
  168. (b << G.scr_var.blue.offset);
  169. }
  170. // RGB 888
  171. return b + (g << 8) + (r << 16);
  172. }
  173. /**
  174. * Draw pixel on framebuffer
  175. */
  176. static void fb_write_pixel(unsigned char *addr, unsigned pixel)
  177. {
  178. switch (G.bytes_per_pixel) {
  179. case 1:
  180. *addr = pixel;
  181. break;
  182. case 2:
  183. *(uint16_t *)addr = pixel;
  184. break;
  185. case 4:
  186. *(uint32_t *)addr = pixel;
  187. break;
  188. default: // 24 bits per pixel
  189. addr[0] = pixel;
  190. addr[1] = pixel >> 8;
  191. addr[2] = pixel >> 16;
  192. }
  193. }
  194. /**
  195. * Draw hollow rectangle on framebuffer
  196. */
  197. static void fb_drawrectangle(void)
  198. {
  199. int cnt;
  200. unsigned thispix;
  201. unsigned char *ptr1, *ptr2;
  202. unsigned char nred = G.nbar_colr/2;
  203. unsigned char ngreen = G.nbar_colg/2;
  204. unsigned char nblue = G.nbar_colb/2;
  205. thispix = fb_pixel_value(nred, ngreen, nblue);
  206. // horizontal lines
  207. ptr1 = G.addr + G.nbar_posy * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
  208. ptr2 = G.addr + (G.nbar_posy + G.nbar_height - 1) * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
  209. cnt = G.nbar_width - 1;
  210. do {
  211. fb_write_pixel(ptr1, thispix);
  212. fb_write_pixel(ptr2, thispix);
  213. ptr1 += G.bytes_per_pixel;
  214. ptr2 += G.bytes_per_pixel;
  215. } while (--cnt >= 0);
  216. // vertical lines
  217. ptr1 = G.addr + G.nbar_posy * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
  218. ptr2 = G.addr + G.nbar_posy * G.scr_fix.line_length + (G.nbar_posx + G.nbar_width - 1) * G.bytes_per_pixel;
  219. cnt = G.nbar_height - 1;
  220. do {
  221. fb_write_pixel(ptr1, thispix);
  222. fb_write_pixel(ptr2, thispix);
  223. ptr1 += G.scr_fix.line_length;
  224. ptr2 += G.scr_fix.line_length;
  225. } while (--cnt >= 0);
  226. }
  227. /**
  228. * Draw filled rectangle on framebuffer
  229. * \param nx1pos,ny1pos upper left position
  230. * \param nx2pos,ny2pos down right position
  231. * \param nred,ngreen,nblue rgb color
  232. */
  233. static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
  234. unsigned char nred, unsigned char ngreen, unsigned char nblue)
  235. {
  236. int cnt1, cnt2, nypos;
  237. unsigned thispix;
  238. unsigned char *ptr;
  239. thispix = fb_pixel_value(nred, ngreen, nblue);
  240. cnt1 = ny2pos - ny1pos;
  241. nypos = ny1pos;
  242. do {
  243. ptr = G.addr + nypos * G.scr_fix.line_length + nx1pos * G.bytes_per_pixel;
  244. cnt2 = nx2pos - nx1pos;
  245. do {
  246. fb_write_pixel(ptr, thispix);
  247. ptr += G.bytes_per_pixel;
  248. } while (--cnt2 >= 0);
  249. nypos++;
  250. } while (--cnt1 >= 0);
  251. }
  252. /**
  253. * Draw a progress bar on framebuffer
  254. * \param percent percentage of loading
  255. */
  256. static void fb_drawprogressbar(unsigned percent)
  257. {
  258. int left_x, top_y, pos_x;
  259. unsigned width, height;
  260. // outer box
  261. left_x = G.nbar_posx;
  262. top_y = G.nbar_posy;
  263. width = G.nbar_width - 1;
  264. height = G.nbar_height - 1;
  265. if ((int)(height | width) < 0)
  266. return;
  267. // NB: "width" of 1 actually makes rect with width of 2!
  268. fb_drawrectangle();
  269. // inner "empty" rectangle
  270. left_x++;
  271. top_y++;
  272. width -= 2;
  273. height -= 2;
  274. if ((int)(height | width) < 0)
  275. return;
  276. pos_x = left_x;
  277. if (percent > 0) {
  278. int i, y;
  279. // actual progress bar
  280. pos_x += (unsigned)(width * percent) / 100;
  281. y = top_y;
  282. i = height;
  283. if (height == 0)
  284. height++; // divide by 0 is bad
  285. while (i >= 0) {
  286. // draw one-line thick "rectangle"
  287. // top line will have gray lvl 200, bottom one 100
  288. unsigned gray_level = 100 + (unsigned)i*100 / height;
  289. fb_drawfullrectangle(
  290. left_x, y, pos_x, y,
  291. gray_level, gray_level, gray_level);
  292. y++;
  293. i--;
  294. }
  295. }
  296. fb_drawfullrectangle(
  297. pos_x, top_y,
  298. left_x + width, top_y + height,
  299. G.nbar_colr, G.nbar_colg, G.nbar_colb);
  300. }
  301. /**
  302. * Draw image from PPM file
  303. */
  304. static void fb_drawimage(void)
  305. {
  306. FILE *theme_file;
  307. char *read_ptr;
  308. unsigned char *pixline;
  309. unsigned i, j, width, height, line_size;
  310. if (LONE_DASH(G.image_filename)) {
  311. theme_file = stdin;
  312. } else {
  313. int fd = open_zipped(G.image_filename, /*fail_if_not_compressed:*/ 0);
  314. if (fd < 0)
  315. bb_simple_perror_msg_and_die(G.image_filename);
  316. theme_file = xfdopen_for_read(fd);
  317. }
  318. /* Parse ppm header:
  319. * - Magic: two characters "P6".
  320. * - Whitespace (blanks, TABs, CRs, LFs).
  321. * - A width, formatted as ASCII characters in decimal.
  322. * - Whitespace.
  323. * - A height, ASCII decimal.
  324. * - Whitespace.
  325. * - The maximum color value, ASCII decimal, in 0..65535
  326. * - Newline or other single whitespace character.
  327. * (we support newline only)
  328. * - A raster of Width * Height pixels in triplets of rgb
  329. * in pure binary by 1 or 2 bytes. (we support only 1 byte)
  330. */
  331. #define concat_buf bb_common_bufsiz1
  332. read_ptr = concat_buf;
  333. while (1) {
  334. int w, h, max_color_val;
  335. int rem = concat_buf + sizeof(concat_buf) - read_ptr;
  336. if (rem < 2
  337. || fgets(read_ptr, rem, theme_file) == NULL
  338. ) {
  339. bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
  340. }
  341. read_ptr = strchrnul(read_ptr, '#');
  342. *read_ptr = '\0'; /* ignore #comments */
  343. if (sscanf(concat_buf, "P6 %u %u %u", &w, &h, &max_color_val) == 3
  344. && max_color_val <= 255
  345. ) {
  346. width = w; /* w is on stack, width may be in register */
  347. height = h;
  348. break;
  349. }
  350. }
  351. line_size = width*3;
  352. pixline = xmalloc(line_size);
  353. if (width > G.scr_var.xres)
  354. width = G.scr_var.xres;
  355. if (height > G.scr_var.yres)
  356. height = G.scr_var.yres;
  357. for (j = 0; j < height; j++) {
  358. unsigned char *pixel;
  359. unsigned char *src;
  360. if (fread(pixline, 1, line_size, theme_file) != line_size)
  361. bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
  362. pixel = pixline;
  363. src = G.addr + j * G.scr_fix.line_length;
  364. for (i = 0; i < width; i++) {
  365. unsigned thispix = fb_pixel_value(pixel[0], pixel[1], pixel[2]);
  366. fb_write_pixel(src, thispix);
  367. src += G.bytes_per_pixel;
  368. pixel += 3;
  369. }
  370. }
  371. free(pixline);
  372. fclose(theme_file);
  373. }
  374. /**
  375. * Parse configuration file
  376. * \param *cfg_filename name of the configuration file
  377. */
  378. static void init(const char *cfg_filename)
  379. {
  380. static const char param_names[] ALIGN1 =
  381. "BAR_WIDTH\0" "BAR_HEIGHT\0"
  382. "BAR_LEFT\0" "BAR_TOP\0"
  383. "BAR_R\0" "BAR_G\0" "BAR_B\0"
  384. #if DEBUG
  385. "DEBUG\0"
  386. #endif
  387. ;
  388. char *token[2];
  389. parser_t *parser = config_open2(cfg_filename, xfopen_stdin);
  390. while (config_read(parser, token, 2, 2, "#=",
  391. (PARSE_NORMAL | PARSE_MIN_DIE) & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
  392. unsigned val = xatoi_positive(token[1]);
  393. int i = index_in_strings(param_names, token[0]);
  394. if (i < 0)
  395. bb_error_msg_and_die("syntax error: %s", token[0]);
  396. if (i >= 0 && i < 7)
  397. G.ns[i] = val;
  398. #if DEBUG
  399. if (i == 7) {
  400. G.bdebug_messages = val;
  401. if (G.bdebug_messages)
  402. G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log");
  403. }
  404. #endif
  405. }
  406. config_close(parser);
  407. }
  408. int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  409. int fbsplash_main(int argc UNUSED_PARAM, char **argv)
  410. {
  411. const char *fb_device, *cfg_filename, *fifo_filename;
  412. FILE *fp = fp; // for compiler
  413. char *num_buf;
  414. unsigned num;
  415. bool bCursorOff;
  416. INIT_G();
  417. // parse command line options
  418. fb_device = "/dev/fb0";
  419. cfg_filename = NULL;
  420. fifo_filename = NULL;
  421. bCursorOff = 1 & getopt32(argv, "cs:d:i:f:",
  422. &G.image_filename, &fb_device, &cfg_filename, &fifo_filename);
  423. // parse configuration file
  424. if (cfg_filename)
  425. init(cfg_filename);
  426. // We must have -s IMG
  427. if (!G.image_filename)
  428. bb_show_usage();
  429. fb_open(fb_device);
  430. if (fifo_filename && bCursorOff) {
  431. // hide cursor (BEFORE any fb ops)
  432. full_write(STDOUT_FILENO, "\033[?25l", 6);
  433. }
  434. fb_drawimage();
  435. if (!fifo_filename)
  436. return EXIT_SUCCESS;
  437. fp = xfopen_stdin(fifo_filename);
  438. if (fp != stdin) {
  439. // For named pipes, we want to support this:
  440. // mkfifo cmd_pipe
  441. // fbsplash -f cmd_pipe .... &
  442. // ...
  443. // echo 33 >cmd_pipe
  444. // ...
  445. // echo 66 >cmd_pipe
  446. // This means that we don't want fbsplash to get EOF
  447. // when last writer closes input end.
  448. // The simplest way is to open fifo for writing too
  449. // and become an additional writer :)
  450. open(fifo_filename, O_WRONLY); // errors are ignored
  451. }
  452. fb_drawprogressbar(0);
  453. // Block on read, waiting for some input.
  454. // Use of <stdio.h> style I/O allows to correctly
  455. // handle a case when we have many buffered lines
  456. // already in the pipe
  457. while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
  458. if (is_prefixed_with(num_buf, "exit")) {
  459. DEBUG_MESSAGE("exit");
  460. break;
  461. }
  462. num = atoi(num_buf);
  463. if (isdigit(num_buf[0]) && (num <= 100)) {
  464. #if DEBUG
  465. DEBUG_MESSAGE(itoa(num));
  466. #endif
  467. fb_drawprogressbar(num);
  468. }
  469. free(num_buf);
  470. }
  471. if (bCursorOff) // restore cursor
  472. full_write(STDOUT_FILENO, "\033[?25h", 6);
  473. return EXIT_SUCCESS;
  474. }