blob: 07350b0cc0355cec5349923749f6e5c20593e449 [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
41 unsigned nbar_width; // progress bar width
42 unsigned nbar_height; // progress bar height
43 unsigned nbar_posx; // progress bar horizontal position
44 unsigned nbar_posy; // progress bar vertical position
45 unsigned char nbar_colr; // progress bar color red component
46 unsigned char nbar_colg; // progress bar color green component
47 unsigned char nbar_colb; // progress bar color blue component
48 const char *image_filename;
49 struct fb_var_screeninfo scr_var;
50 struct fb_fix_screeninfo scr_fix;
51};
52#define G (*ptr_to_globals)
Denis Vlasenko7049ff82008-06-25 09:53:17 +000053#define INIT_G() do { \
54 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
55} while (0)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000056
57
58#if DEBUG
59#define DEBUG_MESSAGE(strMessage, args...) \
60 if (G.bdebug_messages) { \
61 fprintf(G.logfile_fd, "[%s][%s] - %s\n", \
62 __FILE__, __FUNCTION__, strMessage); \
63 }
64#else
65#define DEBUG_MESSAGE(...) ((void)0)
66#endif
67
68
69/**
70 * Open and initialize the framebuffer device
71 * \param *strfb_device pointer to framebuffer device
72 */
73static void fb_open(const char *strfb_device)
74{
75 int fbfd = xopen(strfb_device, O_RDWR);
76
77 // framebuffer properties
78 xioctl(fbfd, FBIOGET_VSCREENINFO, &G.scr_var);
79 xioctl(fbfd, FBIOGET_FSCREENINFO, &G.scr_fix);
80
81 if (G.scr_var.bits_per_pixel != 16)
82 bb_error_msg_and_die("only 16 bpp is supported");
83
84 // map the device in memory
85 G.addr = mmap(NULL,
86 G.scr_var.xres * G.scr_var.yres
87 * BYTES_PER_PIXEL /*(G.scr_var.bits_per_pixel / 8)*/ ,
88 PROT_WRITE, MAP_SHARED, fbfd, 0);
89 if (G.addr == MAP_FAILED)
90 bb_perror_msg_and_die("can't mmap %s", strfb_device);
91 close(fbfd);
92}
93
94
95/**
96 * Draw hollow rectangle on framebuffer
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000097 */
Denis Vlasenkoa38ba592008-03-28 11:17:35 +000098static void fb_drawrectangle(void)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +000099{
100 int cnt;
101 DATA thispix;
102 DATA *ptr1, *ptr2;
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000103 unsigned char nred = G.nbar_colr/2;
104 unsigned char ngreen = G.nbar_colg/2;
105 unsigned char nblue = G.nbar_colb/2;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000106
107 nred >>= 3; // 5-bit red
108 ngreen >>= 2; // 6-bit green
109 nblue >>= 3; // 5-bit blue
110 thispix = nblue + (ngreen << 5) + (nred << (5+6));
111
112 // horizontal lines
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000113 ptr1 = (DATA*)(G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx) * BYTES_PER_PIXEL);
114 ptr2 = (DATA*)(G.addr + ((G.nbar_posy + G.nbar_height - 1) * G.scr_var.xres + G.nbar_posx) * BYTES_PER_PIXEL);
115 cnt = G.nbar_width - 1;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000116 do {
117 *ptr1++ = thispix;
118 *ptr2++ = thispix;
119 } while (--cnt >= 0);
120
121 // vertical lines
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000122 ptr1 = (DATA*)(G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx) * BYTES_PER_PIXEL);
123 ptr2 = (DATA*)(G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx + G.nbar_width - 1) * BYTES_PER_PIXEL);
124 cnt = G.nbar_posy + G.nbar_height - 1 - G.nbar_posy;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000125 do {
126 *ptr1 = thispix; ptr1 += G.scr_var.xres;
127 *ptr2 = thispix; ptr2 += G.scr_var.xres;
128 } while (--cnt >= 0);
129}
130
131
132/**
133 * Draw filled rectangle on framebuffer
134 * \param nx1pos,ny1pos upper left position
135 * \param nx2pos,ny2pos down right position
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000136 * \param nred,ngreen,nblue rgb color
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000137 */
138static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
139 unsigned char nred, unsigned char ngreen, unsigned char nblue)
140{
141 int cnt1, cnt2, nypos;
142 DATA thispix;
143 DATA *ptr;
144
145 nred >>= 3; // 5-bit red
146 ngreen >>= 2; // 6-bit green
147 nblue >>= 3; // 5-bit blue
148 thispix = nblue + (ngreen << 5) + (nred << (5+6));
Denis Vlasenko1f228982008-04-22 00:16:29 +0000149
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000150 cnt1 = ny2pos - ny1pos;
151 nypos = ny1pos;
152 do {
153 ptr = (DATA*)(G.addr + (nypos * G.scr_var.xres + nx1pos) * BYTES_PER_PIXEL);
154 cnt2 = nx2pos - nx1pos;
155 do {
156 *ptr++ = thispix;
157 } while (--cnt2 >= 0);
Denis Vlasenko1f228982008-04-22 00:16:29 +0000158
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000159 nypos++;
160 } while (--cnt1 >= 0);
161}
162
163
164/**
165 * Draw a progress bar on framebuffer
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000166 * \param percent percentage of loading
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000167 */
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000168static void fb_drawprogressbar(unsigned percent)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000169{
170 int i, left_x, top_y, width, height;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000171
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000172 // outer box
173 left_x = G.nbar_posx;
174 top_y = G.nbar_posy;
175 width = G.nbar_width - 1;
176 height = G.nbar_height - 1;
177 if ((height | width) < 0)
178 return;
179 // NB: "width" of 1 actually makes rect with width of 2!
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000180 fb_drawrectangle();
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000181
182 // inner "empty" rectangle
183 left_x++;
184 top_y++;
185 width -= 2;
186 height -= 2;
187 if ((height | width) < 0)
188 return;
189 fb_drawfullrectangle(
190 left_x, top_y,
191 left_x + width, top_y + height,
192 G.nbar_colr, G.nbar_colg, G.nbar_colb);
193
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000194 if (percent > 0) {
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000195 // actual progress bar
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000196 width = width * percent / 100;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000197 i = height;
198 if (height == 0)
199 height++; // divide by 0 is bad
200 while (i >= 0) {
201 // draw one-line thick "rectangle"
202 // top line will have gray lvl 200, bottom one 100
203 unsigned gray_level = 100 + i*100/height;
204 fb_drawfullrectangle(
205 left_x, top_y, left_x + width, top_y,
206 gray_level, gray_level, gray_level);
207 top_y++;
208 i--;
209 }
210 }
211}
212
213
214/**
215 * Draw image from PPM file
216 */
217static void fb_drawimage(void)
218{
219 char head[256];
220 char s[80];
221 FILE *theme_file;
222 unsigned char *pixline;
223 unsigned i, j, width, height, line_size;
224
225 memset(head, 0, sizeof(head));
226 theme_file = xfopen_stdin(G.image_filename);
227
228 // parse ppm header
229 while (1) {
230 if (fgets(s, sizeof(s), theme_file) == NULL)
231 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
232
233 if (s[0] == '#')
234 continue;
235
236 if (strlen(head) + strlen(s) >= sizeof(head))
237 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
238
239 strcat(head, s);
240 if (head[0] != 'P' || head[1] != '6')
241 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
242
243 // width, height, max_color_val
244 if (sscanf(head, "P6 %u %u %u", &width, &height, &i) == 3)
245 break;
246// TODO: i must be <= 255!
247 }
248
249 line_size = width*3;
250 if (width > G.scr_var.xres)
251 width = G.scr_var.xres;
252 if (height > G.scr_var.yres)
253 height = G.scr_var.yres;
254
255 pixline = xmalloc(line_size);
256 for (j = 0; j < height; j++) {
257 unsigned char *pixel = pixline;
258 DATA *src = (DATA *)(G.addr + j * G.scr_fix.line_length);
259
260 if (fread(pixline, 1, line_size, theme_file) != line_size)
261 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
262 for (i = 0; i < width; i++) {
263 unsigned thispix;
264 thispix = (((unsigned)pixel[0] << 8) & 0xf800)
265 | (((unsigned)pixel[1] << 3) & 0x07e0)
266 | (((unsigned)pixel[2] >> 3));
267 *src++ = thispix;
268 pixel += 3;
269 }
270 }
271 free(pixline);
272 fclose(theme_file);
273}
274
275
276/**
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000277 * Parse configuration file
278 * \param *cfg_filename name of the configuration file
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000279 */
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000280static void init(const char *cfg_filename)
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000281{
282 static const char const param_names[] ALIGN1 =
283 "BAR_LEFT\0" "BAR_TOP\0"
284 "BAR_WIDTH\0" "BAR_HEIGHT\0"
285 "BAR_R\0" "BAR_G\0" "BAR_B\0"
286#if DEBUG
287 "DEBUG\0"
288#endif
289 ;
290
291 FILE *inifile;
292 char *buf;
293
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000294 inifile = xfopen_stdin(cfg_filename);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000295
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000296 while ((buf = xmalloc_fgetline(inifile)) != NULL) {
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000297 char *value_str;
298 int val;
299
300 if (*buf == '#') { // it's a comment
301 free(buf);
302 continue;
303 }
304
305 value_str = strchr(buf, '=');
306 if (!value_str)
307 goto err;
308 *value_str++ = '\0';
309 val = xatoi_u(value_str);
310
311 switch (index_in_strings(param_names, buf)) {
312 case 0:
313 // progress bar horizontal position
314 G.nbar_posx = val;
315 break;
316 case 1:
317 // progress bar vertical position
318 G.nbar_posy = val;
319 break;
320 case 2:
321 // progress bar width
322 G.nbar_width = val;
323 break;
324 case 3:
325 // progress bar height
326 G.nbar_height = val;
327 break;
328 case 4:
329 // progress bar color - red component
330 G.nbar_colr = val;
331 break;
332 case 5:
333 // progress bar color - green component
334 G.nbar_colg = val;
335 break;
336 case 6:
337 // progress bar color - blue component
338 G.nbar_colb = val;
339 break;
340#if DEBUG
341 case 7:
342 G.bdebug_messages = val;
343 if (G.bdebug_messages)
Denis Vlasenko1f228982008-04-22 00:16:29 +0000344 G.logfile_fd = xfopen("/tmp/fbsplash.log", "w");
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000345 break;
346#endif
347 err:
348 default:
349 bb_error_msg_and_die("syntax error: '%s'", buf);
350 }
351 free(buf);
352 }
353 fclose(inifile);
354}
355
356
357int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
358int fbsplash_main(int argc ATTRIBUTE_UNUSED, char **argv)
359{
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000360 const char *fb_device, *cfg_filename, *fifo_filename;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000361 FILE *fp = fp; // for compiler
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000362 char *num_buf;
363 unsigned num;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000364 bool bCursorOff;
365
366 INIT_G();
367
368 // parse command line options
369 fb_device = "/dev/fb0";
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000370 cfg_filename = NULL;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000371 fifo_filename = NULL;
372 bCursorOff = 1 & getopt32(argv, "cs:d:i:f:",
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000373 &G.image_filename, &fb_device, &cfg_filename, &fifo_filename);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000374
375 // parse configuration file
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000376 if (cfg_filename)
377 init(cfg_filename);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000378
379 // We must have -s IMG
380 if (!G.image_filename)
381 bb_show_usage();
382
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000383 fb_open(fb_device);
384
385 if (fifo_filename && bCursorOff) {
386 // hide cursor (BEFORE any fb ops)
387 full_write(STDOUT_FILENO, "\x1b" "[?25l", 6);
388 }
389
390 fb_drawimage();
391
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000392 if (!fifo_filename)
393 return EXIT_SUCCESS;
394
395 fp = xfopen_stdin(fifo_filename);
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000396 if (fp != stdin) {
Denis Vlasenko72b34422008-03-27 13:14:29 +0000397 // For named pipes, we want to support this:
398 // mkfifo cmd_pipe
399 // fbsplash -f cmd_pipe .... &
400 // ...
401 // echo 33 >cmd_pipe
402 // ...
403 // echo 66 >cmd_pipe
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000404 // This means that we don't want fbsplash to get EOF
405 // when last writer closes input end.
406 // The simplest way is to open fifo for writing too
407 // and become an additional writer :)
408 open(fifo_filename, O_WRONLY); // errors are ignored
409 }
410
411 fb_drawprogressbar(0);
412 // Block on read, waiting for some input.
413 // Use of <stdio.h> style I/O allows to correctly
414 // handle a case when we have many buffered lines
415 // already in the pipe
416 while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
417 if (strncmp(num_buf, "exit", 4) == 0) {
418 DEBUG_MESSAGE("exit");
419 break;
420 }
421 num = atoi(num_buf);
422 if (isdigit(num_buf[0]) && (num <= 100)) {
423#if DEBUG
424 char strVal[10];
425 sprintf(strVal, "%d", num);
426 DEBUG_MESSAGE(strVal);
427#endif
428 fb_drawprogressbar(num);
429 }
430 free(num_buf);
431 }
432
433 if (bCursorOff) // restore cursor
434 full_write(STDOUT_FILENO, "\x1b" "[?25h", 6);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000435
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000436 return EXIT_SUCCESS;
437}