fbsplash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. };
  50. #define G (*ptr_to_globals)
  51. #define INIT_G() do { \
  52. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  53. } while (0)
  54. #define nbar_width ns[0] // progress bar width
  55. #define nbar_height ns[1] // progress bar height
  56. #define nbar_posx ns[2] // progress bar horizontal position
  57. #define nbar_posy ns[3] // progress bar vertical position
  58. #define nbar_colr ns[4] // progress bar color red component
  59. #define nbar_colg ns[5] // progress bar color green component
  60. #define nbar_colb ns[6] // progress bar color blue component
  61. #if DEBUG
  62. #define DEBUG_MESSAGE(strMessage, args...) \
  63. if (G.bdebug_messages) { \
  64. fprintf(G.logfile_fd, "[%s][%s] - %s\n", \
  65. __FILE__, __FUNCTION__, strMessage); \
  66. }
  67. #else
  68. #define DEBUG_MESSAGE(...) ((void)0)
  69. #endif
  70. /**
  71. * Open and initialize the framebuffer device
  72. * \param *strfb_device pointer to framebuffer device
  73. */
  74. static void fb_open(const char *strfb_device)
  75. {
  76. int fbfd = xopen(strfb_device, O_RDWR);
  77. // framebuffer properties
  78. xioctl(fbfd, FBIOGET_VSCREENINFO, &G.scr_var);
  79. xioctl(fbfd, FBIOGET_FSCREENINFO, &G.scr_fix);
  80. if (G.scr_var.bits_per_pixel < 16 || G.scr_var.bits_per_pixel > 32)
  81. bb_error_msg_and_die("unsupported %u bpp", (int)G.scr_var.bits_per_pixel);
  82. G.bytes_per_pixel = (G.scr_var.bits_per_pixel + 7) >> 3;
  83. // map the device in memory
  84. G.addr = mmap(NULL,
  85. G.scr_var.xres * G.scr_var.yres * G.bytes_per_pixel,
  86. PROT_WRITE, MAP_SHARED, fbfd, 0);
  87. if (G.addr == MAP_FAILED)
  88. bb_perror_msg_and_die("mmap");
  89. // point to the start of the visible screen
  90. G.addr += G.scr_var.yoffset * G.scr_fix.line_length + G.scr_var.xoffset * G.bytes_per_pixel;
  91. close(fbfd);
  92. }
  93. /**
  94. * Return pixel value of the passed RGB color
  95. */
  96. static unsigned fb_pixel_value(unsigned r, unsigned g, unsigned b)
  97. {
  98. if (G.bytes_per_pixel == 2) {
  99. r >>= 3; // 5-bit red
  100. g >>= 2; // 6-bit green
  101. b >>= 3; // 5-bit blue
  102. return b + (g << 5) + (r << (5+6));
  103. }
  104. // RGB 888
  105. return b + (g << 8) + (r << 16);
  106. }
  107. /**
  108. * Draw pixel on framebuffer
  109. */
  110. static void fb_write_pixel(unsigned char *addr, unsigned pixel)
  111. {
  112. switch (G.bytes_per_pixel) {
  113. case 2:
  114. *(uint16_t *)addr = pixel;
  115. break;
  116. case 4:
  117. *(uint32_t *)addr = pixel;
  118. break;
  119. default: // 24 bits per pixel
  120. addr[0] = pixel;
  121. addr[1] = pixel >> 8;
  122. addr[2] = pixel >> 16;
  123. }
  124. }
  125. /**
  126. * Draw hollow rectangle on framebuffer
  127. */
  128. static void fb_drawrectangle(void)
  129. {
  130. int cnt;
  131. unsigned thispix;
  132. unsigned char *ptr1, *ptr2;
  133. unsigned char nred = G.nbar_colr/2;
  134. unsigned char ngreen = G.nbar_colg/2;
  135. unsigned char nblue = G.nbar_colb/2;
  136. thispix = fb_pixel_value(nred, ngreen, nblue);
  137. // horizontal lines
  138. ptr1 = G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx) * G.bytes_per_pixel;
  139. ptr2 = G.addr + ((G.nbar_posy + G.nbar_height - 1) * G.scr_var.xres + G.nbar_posx) * G.bytes_per_pixel;
  140. cnt = G.nbar_width - 1;
  141. do {
  142. fb_write_pixel(ptr1, thispix);
  143. fb_write_pixel(ptr2, thispix);
  144. ptr1 += G.bytes_per_pixel;
  145. ptr2 += G.bytes_per_pixel;
  146. } while (--cnt >= 0);
  147. // vertical lines
  148. ptr1 = G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx) * G.bytes_per_pixel;
  149. ptr2 = G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx + G.nbar_width - 1) * G.bytes_per_pixel;
  150. cnt = G.nbar_height - 1;
  151. do {
  152. fb_write_pixel(ptr1, thispix);
  153. fb_write_pixel(ptr2, thispix);
  154. ptr1 += G.scr_var.xres * G.bytes_per_pixel;
  155. ptr2 += G.scr_var.xres * G.bytes_per_pixel;
  156. } while (--cnt >= 0);
  157. }
  158. /**
  159. * Draw filled rectangle on framebuffer
  160. * \param nx1pos,ny1pos upper left position
  161. * \param nx2pos,ny2pos down right position
  162. * \param nred,ngreen,nblue rgb color
  163. */
  164. static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
  165. unsigned char nred, unsigned char ngreen, unsigned char nblue)
  166. {
  167. int cnt1, cnt2, nypos;
  168. unsigned thispix;
  169. unsigned char *ptr;
  170. thispix = fb_pixel_value(nred, ngreen, nblue);
  171. cnt1 = ny2pos - ny1pos;
  172. nypos = ny1pos;
  173. do {
  174. ptr = G.addr + (nypos * G.scr_var.xres + nx1pos) * G.bytes_per_pixel;
  175. cnt2 = nx2pos - nx1pos;
  176. do {
  177. fb_write_pixel(ptr, thispix);
  178. ptr += G.bytes_per_pixel;
  179. } while (--cnt2 >= 0);
  180. nypos++;
  181. } while (--cnt1 >= 0);
  182. }
  183. /**
  184. * Draw a progress bar on framebuffer
  185. * \param percent percentage of loading
  186. */
  187. static void fb_drawprogressbar(unsigned percent)
  188. {
  189. int i, left_x, top_y, width, height;
  190. // outer box
  191. left_x = G.nbar_posx;
  192. top_y = G.nbar_posy;
  193. width = G.nbar_width - 1;
  194. height = G.nbar_height - 1;
  195. if ((height | width) < 0)
  196. return;
  197. // NB: "width" of 1 actually makes rect with width of 2!
  198. fb_drawrectangle();
  199. // inner "empty" rectangle
  200. left_x++;
  201. top_y++;
  202. width -= 2;
  203. height -= 2;
  204. if ((height | width) < 0)
  205. return;
  206. fb_drawfullrectangle(
  207. left_x, top_y,
  208. left_x + width, top_y + height,
  209. G.nbar_colr, G.nbar_colg, G.nbar_colb);
  210. if (percent > 0) {
  211. // actual progress bar
  212. width = width * percent / 100;
  213. i = height;
  214. if (height == 0)
  215. height++; // divide by 0 is bad
  216. while (i >= 0) {
  217. // draw one-line thick "rectangle"
  218. // top line will have gray lvl 200, bottom one 100
  219. unsigned gray_level = 100 + i*100/height;
  220. fb_drawfullrectangle(
  221. left_x, top_y, left_x + width, top_y,
  222. gray_level, gray_level, gray_level);
  223. top_y++;
  224. i--;
  225. }
  226. }
  227. }
  228. /**
  229. * Draw image from PPM file
  230. */
  231. static void fb_drawimage(void)
  232. {
  233. FILE *theme_file;
  234. char *read_ptr;
  235. unsigned char *pixline;
  236. unsigned i, j, width, height, line_size;
  237. if (LONE_DASH(G.image_filename)) {
  238. theme_file = stdin;
  239. } else {
  240. int fd = open_zipped(G.image_filename);
  241. if (fd < 0)
  242. bb_simple_perror_msg_and_die(G.image_filename);
  243. theme_file = xfdopen_for_read(fd);
  244. }
  245. /* Parse ppm header:
  246. * - Magic: two characters "P6".
  247. * - Whitespace (blanks, TABs, CRs, LFs).
  248. * - A width, formatted as ASCII characters in decimal.
  249. * - Whitespace.
  250. * - A height, ASCII decimal.
  251. * - Whitespace.
  252. * - The maximum color value, ASCII decimal, in 0..65535
  253. * - Newline or other single whitespace character.
  254. * (we support newline only)
  255. * - A raster of Width * Height pixels in triplets of rgb
  256. * in pure binary by 1 or 2 bytes. (we support only 1 byte)
  257. */
  258. #define concat_buf bb_common_bufsiz1
  259. read_ptr = concat_buf;
  260. while (1) {
  261. int w, h, max_color_val;
  262. int rem = concat_buf + sizeof(concat_buf) - read_ptr;
  263. if (rem < 2
  264. || fgets(read_ptr, rem, theme_file) == NULL
  265. ) {
  266. bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
  267. }
  268. read_ptr = strchrnul(read_ptr, '#');
  269. *read_ptr = '\0'; /* ignore #comments */
  270. if (sscanf(concat_buf, "P6 %u %u %u", &w, &h, &max_color_val) == 3
  271. && max_color_val <= 255
  272. ) {
  273. width = w; /* w is on stack, width may be in register */
  274. height = h;
  275. break;
  276. }
  277. }
  278. line_size = width*3;
  279. pixline = xmalloc(line_size);
  280. if (width > G.scr_var.xres)
  281. width = G.scr_var.xres;
  282. if (height > G.scr_var.yres)
  283. height = G.scr_var.yres;
  284. for (j = 0; j < height; j++) {
  285. unsigned char *pixel;
  286. unsigned char *src;
  287. if (fread(pixline, 1, line_size, theme_file) != line_size)
  288. bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
  289. pixel = pixline;
  290. src = G.addr + j * G.scr_fix.line_length;
  291. for (i = 0; i < width; i++) {
  292. unsigned thispix = fb_pixel_value(pixel[0], pixel[1], pixel[2]);
  293. fb_write_pixel(src, thispix);
  294. src += G.bytes_per_pixel;
  295. pixel += 3;
  296. }
  297. }
  298. free(pixline);
  299. fclose(theme_file);
  300. }
  301. /**
  302. * Parse configuration file
  303. * \param *cfg_filename name of the configuration file
  304. */
  305. static void init(const char *cfg_filename)
  306. {
  307. static const char param_names[] ALIGN1 =
  308. "BAR_WIDTH\0" "BAR_HEIGHT\0"
  309. "BAR_LEFT\0" "BAR_TOP\0"
  310. "BAR_R\0" "BAR_G\0" "BAR_B\0"
  311. #if DEBUG
  312. "DEBUG\0"
  313. #endif
  314. ;
  315. char *token[2];
  316. parser_t *parser = config_open2(cfg_filename, xfopen_stdin);
  317. while (config_read(parser, token, 2, 2, "#=",
  318. (PARSE_NORMAL | PARSE_MIN_DIE) & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
  319. unsigned val = xatoi_positive(token[1]);
  320. int i = index_in_strings(param_names, token[0]);
  321. if (i < 0)
  322. bb_error_msg_and_die("syntax error: %s", token[0]);
  323. if (i >= 0 && i < 7)
  324. G.ns[i] = val;
  325. #if DEBUG
  326. if (i == 7) {
  327. G.bdebug_messages = val;
  328. if (G.bdebug_messages)
  329. G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log");
  330. }
  331. #endif
  332. }
  333. config_close(parser);
  334. }
  335. int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  336. int fbsplash_main(int argc UNUSED_PARAM, char **argv)
  337. {
  338. const char *fb_device, *cfg_filename, *fifo_filename;
  339. FILE *fp = fp; // for compiler
  340. char *num_buf;
  341. unsigned num;
  342. bool bCursorOff;
  343. INIT_G();
  344. // parse command line options
  345. fb_device = "/dev/fb0";
  346. cfg_filename = NULL;
  347. fifo_filename = NULL;
  348. bCursorOff = 1 & getopt32(argv, "cs:d:i:f:",
  349. &G.image_filename, &fb_device, &cfg_filename, &fifo_filename);
  350. // parse configuration file
  351. if (cfg_filename)
  352. init(cfg_filename);
  353. // We must have -s IMG
  354. if (!G.image_filename)
  355. bb_show_usage();
  356. fb_open(fb_device);
  357. if (fifo_filename && bCursorOff) {
  358. // hide cursor (BEFORE any fb ops)
  359. full_write(STDOUT_FILENO, "\033[?25l", 6);
  360. }
  361. fb_drawimage();
  362. if (!fifo_filename)
  363. return EXIT_SUCCESS;
  364. fp = xfopen_stdin(fifo_filename);
  365. if (fp != stdin) {
  366. // For named pipes, we want to support this:
  367. // mkfifo cmd_pipe
  368. // fbsplash -f cmd_pipe .... &
  369. // ...
  370. // echo 33 >cmd_pipe
  371. // ...
  372. // echo 66 >cmd_pipe
  373. // This means that we don't want fbsplash to get EOF
  374. // when last writer closes input end.
  375. // The simplest way is to open fifo for writing too
  376. // and become an additional writer :)
  377. open(fifo_filename, O_WRONLY); // errors are ignored
  378. }
  379. fb_drawprogressbar(0);
  380. // Block on read, waiting for some input.
  381. // Use of <stdio.h> style I/O allows to correctly
  382. // handle a case when we have many buffered lines
  383. // already in the pipe
  384. while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
  385. if (strncmp(num_buf, "exit", 4) == 0) {
  386. DEBUG_MESSAGE("exit");
  387. break;
  388. }
  389. num = atoi(num_buf);
  390. if (isdigit(num_buf[0]) && (num <= 100)) {
  391. #if DEBUG
  392. DEBUG_MESSAGE(itoa(num));
  393. #endif
  394. fb_drawprogressbar(num);
  395. }
  396. free(num_buf);
  397. }
  398. if (bCursorOff) // restore cursor
  399. full_write(STDOUT_FILENO, "\033[?25h", 6);
  400. return EXIT_SUCCESS;
  401. }