blob: 626cefb2823cd717bb615994ace048f997ccc9e6 [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)
53#define INIT_G() \
54 do { \
55 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
56 } while (0)
57
58
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 =
284 "BAR_LEFT\0" "BAR_TOP\0"
285 "BAR_WIDTH\0" "BAR_HEIGHT\0"
286 "BAR_R\0" "BAR_G\0" "BAR_B\0"
287#if DEBUG
288 "DEBUG\0"
289#endif
290 ;
291
292 FILE *inifile;
293 char *buf;
294
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000295 inifile = xfopen_stdin(cfg_filename);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000296
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000297 while ((buf = xmalloc_fgetline(inifile)) != NULL) {
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000298 char *value_str;
299 int val;
300
301 if (*buf == '#') { // it's a comment
302 free(buf);
303 continue;
304 }
305
306 value_str = strchr(buf, '=');
307 if (!value_str)
308 goto err;
309 *value_str++ = '\0';
310 val = xatoi_u(value_str);
311
312 switch (index_in_strings(param_names, buf)) {
313 case 0:
314 // progress bar horizontal position
315 G.nbar_posx = val;
316 break;
317 case 1:
318 // progress bar vertical position
319 G.nbar_posy = val;
320 break;
321 case 2:
322 // progress bar width
323 G.nbar_width = val;
324 break;
325 case 3:
326 // progress bar height
327 G.nbar_height = val;
328 break;
329 case 4:
330 // progress bar color - red component
331 G.nbar_colr = val;
332 break;
333 case 5:
334 // progress bar color - green component
335 G.nbar_colg = val;
336 break;
337 case 6:
338 // progress bar color - blue component
339 G.nbar_colb = val;
340 break;
341#if DEBUG
342 case 7:
343 G.bdebug_messages = val;
344 if (G.bdebug_messages)
Denis Vlasenko1f228982008-04-22 00:16:29 +0000345 G.logfile_fd = xfopen("/tmp/fbsplash.log", "w");
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000346 break;
347#endif
348 err:
349 default:
350 bb_error_msg_and_die("syntax error: '%s'", buf);
351 }
352 free(buf);
353 }
354 fclose(inifile);
355}
356
357
358int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
359int fbsplash_main(int argc ATTRIBUTE_UNUSED, char **argv)
360{
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000361 const char *fb_device, *cfg_filename, *fifo_filename;
Denis Vlasenko11b9f262008-03-26 20:06:24 +0000362 FILE *fp = fp; // for compiler
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000363 char *num_buf;
364 unsigned num;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000365 bool bCursorOff;
366
367 INIT_G();
368
369 // parse command line options
370 fb_device = "/dev/fb0";
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000371 cfg_filename = NULL;
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000372 fifo_filename = NULL;
373 bCursorOff = 1 & getopt32(argv, "cs:d:i:f:",
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000374 &G.image_filename, &fb_device, &cfg_filename, &fifo_filename);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000375
376 // parse configuration file
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000377 if (cfg_filename)
378 init(cfg_filename);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000379
380 // We must have -s IMG
381 if (!G.image_filename)
382 bb_show_usage();
383
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000384 fb_open(fb_device);
385
386 if (fifo_filename && bCursorOff) {
387 // hide cursor (BEFORE any fb ops)
388 full_write(STDOUT_FILENO, "\x1b" "[?25l", 6);
389 }
390
391 fb_drawimage();
392
Denis Vlasenkoa38ba592008-03-28 11:17:35 +0000393 if (!fifo_filename)
394 return EXIT_SUCCESS;
395
396 fp = xfopen_stdin(fifo_filename);
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000397 if (fp != stdin) {
Denis Vlasenko72b34422008-03-27 13:14:29 +0000398 // For named pipes, we want to support this:
399 // mkfifo cmd_pipe
400 // fbsplash -f cmd_pipe .... &
401 // ...
402 // echo 33 >cmd_pipe
403 // ...
404 // echo 66 >cmd_pipe
Denis Vlasenko7f8f0fa2008-04-04 20:38:49 +0000405 // This means that we don't want fbsplash to get EOF
406 // when last writer closes input end.
407 // The simplest way is to open fifo for writing too
408 // and become an additional writer :)
409 open(fifo_filename, O_WRONLY); // errors are ignored
410 }
411
412 fb_drawprogressbar(0);
413 // Block on read, waiting for some input.
414 // Use of <stdio.h> style I/O allows to correctly
415 // handle a case when we have many buffered lines
416 // already in the pipe
417 while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
418 if (strncmp(num_buf, "exit", 4) == 0) {
419 DEBUG_MESSAGE("exit");
420 break;
421 }
422 num = atoi(num_buf);
423 if (isdigit(num_buf[0]) && (num <= 100)) {
424#if DEBUG
425 char strVal[10];
426 sprintf(strVal, "%d", num);
427 DEBUG_MESSAGE(strVal);
428#endif
429 fb_drawprogressbar(num);
430 }
431 free(num_buf);
432 }
433
434 if (bCursorOff) // restore cursor
435 full_write(STDOUT_FILENO, "\x1b" "[?25h", 6);
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000436
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000437 return EXIT_SUCCESS;
438}