fbsplash.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2008 Michele Sanges <michele.sanges@otomelara.it>,
  4. * <michele.sanges@gmail.it>
  5. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  7. *
  8. * Usage:
  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] &
  12. * -c: hide cursor
  13. * -d /dev/fbN: framebuffer device (if not /dev/fb0)
  14. * -s path_to_image_file (can be "-" for stdin)
  15. * -i path_to_cfg_file
  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.
  23. */
  24. #include "libbb.h"
  25. #include <linux/fb.h>
  26. /* If you want logging messages on /tmp/fbsplash.log... */
  27. #define DEBUG 0
  28. #define BYTES_PER_PIXEL 2
  29. typedef unsigned short DATA;
  30. struct globals {
  31. #if DEBUG
  32. bool bdebug_messages; // enable/disable logging
  33. FILE *logfile_fd; // log file
  34. #endif
  35. unsigned char *addr; // pointer to framebuffer memory
  36. unsigned ns[7]; // n-parameters
  37. const char *image_filename;
  38. struct fb_var_screeninfo scr_var;
  39. struct fb_fix_screeninfo scr_fix;
  40. };
  41. #define G (*ptr_to_globals)
  42. #define INIT_G() do { \
  43. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  44. } while (0)
  45. #define nbar_width ns[0] // progress bar width
  46. #define nbar_height ns[1] // progress bar height
  47. #define nbar_posx ns[2] // progress bar horizontal position
  48. #define nbar_posy ns[3] // progress bar vertical position
  49. #define nbar_colr ns[4] // progress bar color red component
  50. #define nbar_colg ns[5] // progress bar color green component
  51. #define nbar_colb ns[6] // progress bar color blue component
  52. #if DEBUG
  53. #define DEBUG_MESSAGE(strMessage, args...) \
  54. if (G.bdebug_messages) { \
  55. fprintf(G.logfile_fd, "[%s][%s] - %s\n", \
  56. __FILE__, __FUNCTION__, strMessage); \
  57. }
  58. #else
  59. #define DEBUG_MESSAGE(...) ((void)0)
  60. #endif
  61. /**
  62. * Open and initialize the framebuffer device
  63. * \param *strfb_device pointer to framebuffer device
  64. */
  65. static void fb_open(const char *strfb_device)
  66. {
  67. int fbfd = xopen(strfb_device, O_RDWR);
  68. // framebuffer properties
  69. xioctl(fbfd, FBIOGET_VSCREENINFO, &G.scr_var);
  70. xioctl(fbfd, FBIOGET_FSCREENINFO, &G.scr_fix);
  71. if (G.scr_var.bits_per_pixel != 16)
  72. bb_error_msg_and_die("only 16 bpp is supported");
  73. // map the device in memory
  74. G.addr = mmap(NULL,
  75. G.scr_var.xres * G.scr_var.yres
  76. * BYTES_PER_PIXEL /*(G.scr_var.bits_per_pixel / 8)*/ ,
  77. PROT_WRITE, MAP_SHARED, fbfd, 0);
  78. if (G.addr == MAP_FAILED)
  79. bb_perror_msg_and_die("can't mmap %s", strfb_device);
  80. close(fbfd);
  81. }
  82. /**
  83. * Draw hollow rectangle on framebuffer
  84. */
  85. static void fb_drawrectangle(void)
  86. {
  87. int cnt;
  88. DATA thispix;
  89. DATA *ptr1, *ptr2;
  90. unsigned char nred = G.nbar_colr/2;
  91. unsigned char ngreen = G.nbar_colg/2;
  92. unsigned char nblue = G.nbar_colb/2;
  93. nred >>= 3; // 5-bit red
  94. ngreen >>= 2; // 6-bit green
  95. nblue >>= 3; // 5-bit blue
  96. thispix = nblue + (ngreen << 5) + (nred << (5+6));
  97. // horizontal lines
  98. ptr1 = (DATA*)(G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx) * BYTES_PER_PIXEL);
  99. ptr2 = (DATA*)(G.addr + ((G.nbar_posy + G.nbar_height - 1) * G.scr_var.xres + G.nbar_posx) * BYTES_PER_PIXEL);
  100. cnt = G.nbar_width - 1;
  101. do {
  102. *ptr1++ = thispix;
  103. *ptr2++ = thispix;
  104. } while (--cnt >= 0);
  105. // vertical lines
  106. ptr1 = (DATA*)(G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx) * BYTES_PER_PIXEL);
  107. ptr2 = (DATA*)(G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx + G.nbar_width - 1) * BYTES_PER_PIXEL);
  108. cnt = G.nbar_posy + G.nbar_height - 1 - G.nbar_posy;
  109. do {
  110. *ptr1 = thispix; ptr1 += G.scr_var.xres;
  111. *ptr2 = thispix; ptr2 += G.scr_var.xres;
  112. } while (--cnt >= 0);
  113. }
  114. /**
  115. * Draw filled rectangle on framebuffer
  116. * \param nx1pos,ny1pos upper left position
  117. * \param nx2pos,ny2pos down right position
  118. * \param nred,ngreen,nblue rgb color
  119. */
  120. static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
  121. unsigned char nred, unsigned char ngreen, unsigned char nblue)
  122. {
  123. int cnt1, cnt2, nypos;
  124. DATA thispix;
  125. DATA *ptr;
  126. nred >>= 3; // 5-bit red
  127. ngreen >>= 2; // 6-bit green
  128. nblue >>= 3; // 5-bit blue
  129. thispix = nblue + (ngreen << 5) + (nred << (5+6));
  130. cnt1 = ny2pos - ny1pos;
  131. nypos = ny1pos;
  132. do {
  133. ptr = (DATA*)(G.addr + (nypos * G.scr_var.xres + nx1pos) * BYTES_PER_PIXEL);
  134. cnt2 = nx2pos - nx1pos;
  135. do {
  136. *ptr++ = thispix;
  137. } while (--cnt2 >= 0);
  138. nypos++;
  139. } while (--cnt1 >= 0);
  140. }
  141. /**
  142. * Draw a progress bar on framebuffer
  143. * \param percent percentage of loading
  144. */
  145. static void fb_drawprogressbar(unsigned percent)
  146. {
  147. int i, left_x, top_y, width, height;
  148. // outer box
  149. left_x = G.nbar_posx;
  150. top_y = G.nbar_posy;
  151. width = G.nbar_width - 1;
  152. height = G.nbar_height - 1;
  153. if ((height | width) < 0)
  154. return;
  155. // NB: "width" of 1 actually makes rect with width of 2!
  156. fb_drawrectangle();
  157. // inner "empty" rectangle
  158. left_x++;
  159. top_y++;
  160. width -= 2;
  161. height -= 2;
  162. if ((height | width) < 0)
  163. return;
  164. fb_drawfullrectangle(
  165. left_x, top_y,
  166. left_x + width, top_y + height,
  167. G.nbar_colr, G.nbar_colg, G.nbar_colb);
  168. if (percent > 0) {
  169. // actual progress bar
  170. width = width * percent / 100;
  171. i = height;
  172. if (height == 0)
  173. height++; // divide by 0 is bad
  174. while (i >= 0) {
  175. // draw one-line thick "rectangle"
  176. // top line will have gray lvl 200, bottom one 100
  177. unsigned gray_level = 100 + i*100/height;
  178. fb_drawfullrectangle(
  179. left_x, top_y, left_x + width, top_y,
  180. gray_level, gray_level, gray_level);
  181. top_y++;
  182. i--;
  183. }
  184. }
  185. }
  186. /**
  187. * Draw image from PPM file
  188. */
  189. static void fb_drawimage(void)
  190. {
  191. char head[256];
  192. char s[80];
  193. FILE *theme_file;
  194. unsigned char *pixline;
  195. unsigned i, j, width, height, line_size;
  196. memset(head, 0, sizeof(head));
  197. theme_file = xfopen_stdin(G.image_filename);
  198. // parse ppm header
  199. while (1) {
  200. if (fgets(s, sizeof(s), theme_file) == NULL)
  201. bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
  202. if (s[0] == '#')
  203. continue;
  204. if (strlen(head) + strlen(s) >= sizeof(head))
  205. bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
  206. strcat(head, s);
  207. if (head[0] != 'P' || head[1] != '6')
  208. bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
  209. // width, height, max_color_val
  210. if (sscanf(head, "P6 %u %u %u", &width, &height, &i) == 3)
  211. break;
  212. // TODO: i must be <= 255!
  213. }
  214. line_size = width*3;
  215. if (width > G.scr_var.xres)
  216. width = G.scr_var.xres;
  217. if (height > G.scr_var.yres)
  218. height = G.scr_var.yres;
  219. pixline = xmalloc(line_size);
  220. for (j = 0; j < height; j++) {
  221. unsigned char *pixel = pixline;
  222. DATA *src = (DATA *)(G.addr + j * G.scr_fix.line_length);
  223. if (fread(pixline, 1, line_size, theme_file) != line_size)
  224. bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
  225. for (i = 0; i < width; i++) {
  226. unsigned thispix;
  227. thispix = (((unsigned)pixel[0] << 8) & 0xf800)
  228. | (((unsigned)pixel[1] << 3) & 0x07e0)
  229. | (((unsigned)pixel[2] >> 3));
  230. *src++ = thispix;
  231. pixel += 3;
  232. }
  233. }
  234. free(pixline);
  235. fclose(theme_file);
  236. }
  237. /**
  238. * Parse configuration file
  239. * \param *cfg_filename name of the configuration file
  240. */
  241. static void init(const char *cfg_filename)
  242. {
  243. static const char const param_names[] ALIGN1 =
  244. "BAR_WIDTH\0" "BAR_HEIGHT\0"
  245. "BAR_LEFT\0" "BAR_TOP\0"
  246. "BAR_R\0" "BAR_G\0" "BAR_B\0"
  247. #if DEBUG
  248. "DEBUG\0"
  249. #endif
  250. ;
  251. char *token[2];
  252. parser_t *parser = config_open2(cfg_filename, xfopen_stdin);
  253. while (config_read(parser, token, 2, 2, "#=",
  254. (PARSE_NORMAL | PARSE_MIN_DIE) & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
  255. unsigned val = xatoi_u(token[1]);
  256. int i = index_in_strings(param_names, token[0]);
  257. if (i < 0)
  258. bb_error_msg_and_die("syntax error: '%s'", token[0]);
  259. if (i >= 0 && i < 7)
  260. G.ns[i] = val;
  261. #if DEBUG
  262. if (i == 7) {
  263. G.bdebug_messages = val;
  264. if (G.bdebug_messages)
  265. G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log");
  266. }
  267. #endif
  268. }
  269. config_close(parser);
  270. }
  271. int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  272. int fbsplash_main(int argc UNUSED_PARAM, char **argv)
  273. {
  274. const char *fb_device, *cfg_filename, *fifo_filename;
  275. FILE *fp = fp; // for compiler
  276. char *num_buf;
  277. unsigned num;
  278. bool bCursorOff;
  279. INIT_G();
  280. // parse command line options
  281. fb_device = "/dev/fb0";
  282. cfg_filename = NULL;
  283. fifo_filename = NULL;
  284. bCursorOff = 1 & getopt32(argv, "cs:d:i:f:",
  285. &G.image_filename, &fb_device, &cfg_filename, &fifo_filename);
  286. // parse configuration file
  287. if (cfg_filename)
  288. init(cfg_filename);
  289. // We must have -s IMG
  290. if (!G.image_filename)
  291. bb_show_usage();
  292. fb_open(fb_device);
  293. if (fifo_filename && bCursorOff) {
  294. // hide cursor (BEFORE any fb ops)
  295. full_write(STDOUT_FILENO, "\x1b" "[?25l", 6);
  296. }
  297. fb_drawimage();
  298. if (!fifo_filename)
  299. return EXIT_SUCCESS;
  300. fp = xfopen_stdin(fifo_filename);
  301. if (fp != stdin) {
  302. // For named pipes, we want to support this:
  303. // mkfifo cmd_pipe
  304. // fbsplash -f cmd_pipe .... &
  305. // ...
  306. // echo 33 >cmd_pipe
  307. // ...
  308. // echo 66 >cmd_pipe
  309. // This means that we don't want fbsplash to get EOF
  310. // when last writer closes input end.
  311. // The simplest way is to open fifo for writing too
  312. // and become an additional writer :)
  313. open(fifo_filename, O_WRONLY); // errors are ignored
  314. }
  315. fb_drawprogressbar(0);
  316. // Block on read, waiting for some input.
  317. // Use of <stdio.h> style I/O allows to correctly
  318. // handle a case when we have many buffered lines
  319. // already in the pipe
  320. while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
  321. if (strncmp(num_buf, "exit", 4) == 0) {
  322. DEBUG_MESSAGE("exit");
  323. break;
  324. }
  325. num = atoi(num_buf);
  326. if (isdigit(num_buf[0]) && (num <= 100)) {
  327. #if DEBUG
  328. char strVal[10];
  329. sprintf(strVal, "%d", num);
  330. DEBUG_MESSAGE(strVal);
  331. #endif
  332. fb_drawprogressbar(num);
  333. }
  334. free(num_buf);
  335. }
  336. if (bCursorOff) // restore cursor
  337. full_write(STDOUT_FILENO, "\x1b" "[?25h", 6);
  338. return EXIT_SUCCESS;
  339. }