blob: f8289c3d11832ce1c1bb22e929bd71eedbf2d3e9 [file] [log] [blame]
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +00001/* vi: set sw=4 ts=4: */
2/*
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +00003 * 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.
Denis Vlasenko25a9c172008-03-26 15:12:11 +000010 * - put somewhere fbsplash.cfg file and an image in .ppm format.
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000011 * - run applet: $ setsid fbsplash [params] &
Denis Vlasenko1f228982008-04-22 00:16:29 +000012 * -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)
Denis Vlasenko25a9c172008-03-26 15:12:11 +000017 * - if you want to run it only in presence of a kernel parameter
18 * (for example fbsplash=on), use:
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000019 * 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
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000025#include "libbb.h"
26#include <linux/fb.h>
27
Denis Vlasenko25a9c172008-03-26 15:12:11 +000028/* If you want logging messages on /tmp/fbsplash.log... */
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000029#define DEBUG 0
30
31#define BYTES_PER_PIXEL 2
32
33typedef unsigned short DATA;
34
35struct globals {
36#if DEBUG
37 bool bdebug_messages; // enable/disable logging
38 FILE *logfile_fd; // log file
39#endif
40 unsigned char *addr; // pointer to framebuffer memory
Denis Vlasenko0f99d492008-07-24 23:38:04 +000041 unsigned ns[7]; // n-parameters
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000042 const char *image_filename;
43 struct fb_var_screeninfo scr_var;
44 struct fb_fix_screeninfo scr_fix;
45};
46#define G (*ptr_to_globals)
Denis Vlasenko7049ff82008-06-25 09:53:17 +000047#define INIT_G() do { \
48 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
49} while (0)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000050
Denis Vlasenko0f99d492008-07-24 23:38:04 +000051#define nbar_width ns[0] // progress bar width
52#define nbar_height ns[1] // progress bar height
53#define nbar_posx ns[2] // progress bar horizontal position
54#define nbar_posy ns[3] // progress bar vertical position
55#define nbar_colr ns[4] // progress bar color red component
56#define nbar_colg ns[5] // progress bar color green component
57#define nbar_colb ns[6] // progress bar color blue component
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000058
59#if DEBUG
60#define DEBUG_MESSAGE(strMessage, args...) \
61 if (G.bdebug_messages) { \
62 fprintf(G.logfile_fd, "[%s][%s] - %s\n", \
63 __FILE__, __FUNCTION__, strMessage); \
64 }
65#else
66#define DEBUG_MESSAGE(...) ((void)0)
67#endif
68
69
70/**
71 * Open and initialize the framebuffer device
72 * \param *strfb_device pointer to framebuffer device
73 */
74static void fb_open(const char *strfb_device)
75{
76 int fbfd = xopen(strfb_device, O_RDWR);
77
78 // framebuffer properties
79 xioctl(fbfd, FBIOGET_VSCREENINFO, &G.scr_var);
80 xioctl(fbfd, FBIOGET_FSCREENINFO, &G.scr_fix);
81
82 if (G.scr_var.bits_per_pixel != 16)
83 bb_error_msg_and_die("only 16 bpp is supported");
84
85 // map the device in memory
86 G.addr = mmap(NULL,
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);
92 close(fbfd);
93}
94
95
96/**
97 * Draw hollow rectangle on framebuffer
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000098 */
Denis Vlasenkoa38ba592008-03-28 11:17:35 +000099static void fb_drawrectangle(void)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000100{
101 int cnt;
102 DATA thispix;
103 DATA *ptr1, *ptr2;
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000104 unsigned char nred = G.nbar_colr/2;
105 unsigned char ngreen = G.nbar_colg/2;
106 unsigned char nblue = G.nbar_colb/2;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000107
108 nred >>= 3; // 5-bit red
109 ngreen >>= 2; // 6-bit green
110 nblue >>= 3; // 5-bit blue
111 thispix = nblue + (ngreen << 5) + (nred << (5+6));
112
113 // horizontal lines
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000114 ptr1 = (DATA*)(G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx) * BYTES_PER_PIXEL);
115 ptr2 = (DATA*)(G.addr + ((G.nbar_posy + G.nbar_height - 1) * G.scr_var.xres + G.nbar_posx) * BYTES_PER_PIXEL);
116 cnt = G.nbar_width - 1;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000117 do {
118 *ptr1++ = thispix;
119 *ptr2++ = thispix;
120 } while (--cnt >= 0);
121
122 // vertical lines
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000123 ptr1 = (DATA*)(G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx) * BYTES_PER_PIXEL);
124 ptr2 = (DATA*)(G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx + G.nbar_width - 1) * BYTES_PER_PIXEL);
125 cnt = G.nbar_posy + G.nbar_height - 1 - G.nbar_posy;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000126 do {
127 *ptr1 = thispix; ptr1 += G.scr_var.xres;
128 *ptr2 = thispix; ptr2 += G.scr_var.xres;
129 } while (--cnt >= 0);
130}
131
132
133/**
134 * Draw filled rectangle on framebuffer
135 * \param nx1pos,ny1pos upper left position
136 * \param nx2pos,ny2pos down right position
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000137 * \param nred,ngreen,nblue rgb color
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000138 */
139static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
140 unsigned char nred, unsigned char ngreen, unsigned char nblue)
141{
142 int cnt1, cnt2, nypos;
143 DATA thispix;
144 DATA *ptr;
145
146 nred >>= 3; // 5-bit red
147 ngreen >>= 2; // 6-bit green
148 nblue >>= 3; // 5-bit blue
149 thispix = nblue + (ngreen << 5) + (nred << (5+6));
Denis Vlasenko1f228982008-04-22 00:16:29 +0000150
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000151 cnt1 = ny2pos - ny1pos;
152 nypos = ny1pos;
153 do {
154 ptr = (DATA*)(G.addr + (nypos * G.scr_var.xres + nx1pos) * BYTES_PER_PIXEL);
155 cnt2 = nx2pos - nx1pos;
156 do {
157 *ptr++ = thispix;
158 } while (--cnt2 >= 0);
Denis Vlasenko1f228982008-04-22 00:16:29 +0000159
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000160 nypos++;
161 } while (--cnt1 >= 0);
162}
163
164
165/**
166 * Draw a progress bar on framebuffer
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000167 * \param percent percentage of loading
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000168 */
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000169static void fb_drawprogressbar(unsigned percent)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000170{
171 int i, left_x, top_y, width, height;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000172
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000173 // outer box
174 left_x = G.nbar_posx;
175 top_y = G.nbar_posy;
176 width = G.nbar_width - 1;
177 height = G.nbar_height - 1;
178 if ((height | width) < 0)
179 return;
180 // NB: "width" of 1 actually makes rect with width of 2!
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000181 fb_drawrectangle();
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000182
183 // inner "empty" rectangle
184 left_x++;
185 top_y++;
186 width -= 2;
187 height -= 2;
188 if ((height | width) < 0)
189 return;
190 fb_drawfullrectangle(
191 left_x, top_y,
192 left_x + width, top_y + height,
193 G.nbar_colr, G.nbar_colg, G.nbar_colb);
194
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000195 if (percent > 0) {
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000196 // actual progress bar
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000197 width = width * percent / 100;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000198 i = height;
199 if (height == 0)
200 height++; // divide by 0 is bad
201 while (i >= 0) {
202 // draw one-line thick "rectangle"
203 // top line will have gray lvl 200, bottom one 100
204 unsigned gray_level = 100 + i*100/height;
205 fb_drawfullrectangle(
206 left_x, top_y, left_x + width, top_y,
207 gray_level, gray_level, gray_level);
208 top_y++;
209 i--;
210 }
211 }
212}
213
214
215/**
216 * Draw image from PPM file
217 */
218static void fb_drawimage(void)
219{
220 char head[256];
221 char s[80];
222 FILE *theme_file;
223 unsigned char *pixline;
224 unsigned i, j, width, height, line_size;
225
226 memset(head, 0, sizeof(head));
227 theme_file = xfopen_stdin(G.image_filename);
228
229 // parse ppm header
230 while (1) {
231 if (fgets(s, sizeof(s), theme_file) == NULL)
232 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
233
234 if (s[0] == '#')
235 continue;
236
237 if (strlen(head) + strlen(s) >= sizeof(head))
238 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
239
240 strcat(head, s);
241 if (head[0] != 'P' || head[1] != '6')
242 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
243
244 // width, height, max_color_val
245 if (sscanf(head, "P6 %u %u %u", &width, &height, &i) == 3)
246 break;
247// TODO: i must be <= 255!
248 }
249
250 line_size = width*3;
251 if (width > G.scr_var.xres)
252 width = G.scr_var.xres;
253 if (height > G.scr_var.yres)
254 height = G.scr_var.yres;
255
256 pixline = xmalloc(line_size);
257 for (j = 0; j < height; j++) {
258 unsigned char *pixel = pixline;
259 DATA *src = (DATA *)(G.addr + j * G.scr_fix.line_length);
260
261 if (fread(pixline, 1, line_size, theme_file) != line_size)
262 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
263 for (i = 0; i < width; i++) {
264 unsigned thispix;
265 thispix = (((unsigned)pixel[0] << 8) & 0xf800)
266 | (((unsigned)pixel[1] << 3) & 0x07e0)
267 | (((unsigned)pixel[2] >> 3));
268 *src++ = thispix;
269 pixel += 3;
270 }
271 }
272 free(pixline);
273 fclose(theme_file);
274}
275
276
277/**
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000278 * Parse configuration file
279 * \param *cfg_filename name of the configuration file
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000280 */
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000281static void init(const char *cfg_filename)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000282{
283 static const char const param_names[] ALIGN1 =
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000284 "BAR_WIDTH\0" "BAR_HEIGHT\0"
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000285 "BAR_LEFT\0" "BAR_TOP\0"
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000286 "BAR_R\0" "BAR_G\0" "BAR_B\0"
287#if DEBUG
288 "DEBUG\0"
289#endif
290 ;
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000291 char *token[2];
292 parser_t *parser = config_open2(cfg_filename, xfopen_stdin);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000293 while (config_read(parser, token, 2, 2, "#=",
294 (PARSE_NORMAL | PARSE_MIN_DIE) & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000295 unsigned val = xatoi_u(token[1]);
296 int i = index_in_strings(param_names, token[0]);
297 if (i < 0)
298 bb_error_msg_and_die("syntax error: '%s'", token[0]);
299 if (i >= 0 && i < 7)
300 G.ns[i] = val;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000301#if DEBUG
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000302 if (i == 7) {
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000303 G.bdebug_messages = val;
304 if (G.bdebug_messages)
Denis Vlasenko5415c852008-07-21 23:05:26 +0000305 G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log");
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000306 }
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000307#endif
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000308 }
Denis Vlasenko0f99d492008-07-24 23:38:04 +0000309 config_close(parser);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000310}
311
312
313int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000314int fbsplash_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000315{
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000316 const char *fb_device, *cfg_filename, *fifo_filename;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000317 FILE *fp = fp; // for compiler
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000318 char *num_buf;
319 unsigned num;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000320 bool bCursorOff;
321
322 INIT_G();
323
324 // parse command line options
325 fb_device = "/dev/fb0";
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000326 cfg_filename = NULL;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000327 fifo_filename = NULL;
328 bCursorOff = 1 & getopt32(argv, "cs:d:i:f:",
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000329 &G.image_filename, &fb_device, &cfg_filename, &fifo_filename);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000330
331 // parse configuration file
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000332 if (cfg_filename)
333 init(cfg_filename);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000334
335 // We must have -s IMG
336 if (!G.image_filename)
337 bb_show_usage();
338
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000339 fb_open(fb_device);
340
341 if (fifo_filename && bCursorOff) {
342 // hide cursor (BEFORE any fb ops)
343 full_write(STDOUT_FILENO, "\x1b" "[?25l", 6);
344 }
345
346 fb_drawimage();
347
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000348 if (!fifo_filename)
349 return EXIT_SUCCESS;
350
351 fp = xfopen_stdin(fifo_filename);
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000352 if (fp != stdin) {
Denis Vlasenko72b34422008-03-27 13:14:29 +0000353 // For named pipes, we want to support this:
354 // mkfifo cmd_pipe
355 // fbsplash -f cmd_pipe .... &
356 // ...
357 // echo 33 >cmd_pipe
358 // ...
359 // echo 66 >cmd_pipe
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000360 // This means that we don't want fbsplash to get EOF
361 // when last writer closes input end.
362 // The simplest way is to open fifo for writing too
363 // and become an additional writer :)
364 open(fifo_filename, O_WRONLY); // errors are ignored
365 }
366
367 fb_drawprogressbar(0);
368 // Block on read, waiting for some input.
369 // Use of <stdio.h> style I/O allows to correctly
370 // handle a case when we have many buffered lines
371 // already in the pipe
372 while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
373 if (strncmp(num_buf, "exit", 4) == 0) {
374 DEBUG_MESSAGE("exit");
375 break;
376 }
377 num = atoi(num_buf);
378 if (isdigit(num_buf[0]) && (num <= 100)) {
379#if DEBUG
380 char strVal[10];
381 sprintf(strVal, "%d", num);
382 DEBUG_MESSAGE(strVal);
383#endif
384 fb_drawprogressbar(num);
385 }
386 free(num_buf);
387 }
388
389 if (bCursorOff) // restore cursor
390 full_write(STDOUT_FILENO, "\x1b" "[?25h", 6);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000391
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000392 return EXIT_SUCCESS;
393}