fbsplash: fix broken handling of buffered case:
[platform/upstream/busybox.git] / miscutils / fbsplash.c
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
25 #include "libbb.h"
26 #include <linux/fb.h>
27
28 /* If you want logging messages on /tmp/fbsplash.log... */
29 #define DEBUG 0
30
31 #define BYTES_PER_PIXEL 2
32
33 typedef unsigned short DATA;
34
35 struct 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  */
74 static 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
98  * \param nx1pos,ny1pos upper left position
99  * \param nx2pos,ny2pos down right position
100  * \param nred,ngreen,nblue rgb color
101  */
102 static void fb_drawrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
103         unsigned char nred, unsigned char ngreen, unsigned char nblue)
104 {
105         int cnt;
106         DATA thispix;
107         DATA *ptr1, *ptr2;
108
109         nred   >>= 3;  // 5-bit red
110         ngreen >>= 2;  // 6-bit green
111         nblue  >>= 3;  // 5-bit blue
112         thispix = nblue + (ngreen << 5) + (nred << (5+6));
113
114         // horizontal lines
115         ptr1 = (DATA*)(G.addr + (ny1pos * G.scr_var.xres + nx1pos) * BYTES_PER_PIXEL);
116         ptr2 = (DATA*)(G.addr + (ny2pos * G.scr_var.xres + nx1pos) * BYTES_PER_PIXEL);
117         cnt = nx2pos - nx1pos;
118         do {
119                 *ptr1++ = thispix;
120                 *ptr2++ = thispix;
121         } while (--cnt >= 0);
122
123         // vertical lines
124         ptr1 = (DATA*)(G.addr + (ny1pos * G.scr_var.xres + nx1pos) * BYTES_PER_PIXEL);
125         ptr2 = (DATA*)(G.addr + (ny1pos * G.scr_var.xres + nx2pos) * BYTES_PER_PIXEL);
126         cnt = ny2pos - ny1pos;
127         do {
128                 *ptr1 = thispix; ptr1 += G.scr_var.xres;
129                 *ptr2 = thispix; ptr2 += G.scr_var.xres;
130         } while (--cnt >= 0);
131 }
132
133
134 /**
135  *      Draw filled rectangle on framebuffer
136  * \param nx1pos,ny1pos upper left position
137  * \param nx2pos,ny2pos down right position
138  * \param nred,ngreen,nblue rgb color
139  */
140 static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
141         unsigned char nred, unsigned char ngreen, unsigned char nblue)
142 {
143         int cnt1, cnt2, nypos;
144         DATA thispix;
145         DATA *ptr;
146
147         nred   >>= 3;  // 5-bit red
148         ngreen >>= 2;  // 6-bit green
149         nblue  >>= 3;  // 5-bit blue
150         thispix = nblue + (ngreen << 5) + (nred << (5+6));
151         
152         cnt1 = ny2pos - ny1pos;
153         nypos = ny1pos;
154         do {
155                 ptr = (DATA*)(G.addr + (nypos * G.scr_var.xres + nx1pos) * BYTES_PER_PIXEL);
156                 cnt2 = nx2pos - nx1pos;
157                 do {
158                         *ptr++ = thispix;
159                 } while (--cnt2 >= 0);
160                 
161                 nypos++;
162         } while (--cnt1 >= 0);
163 }
164
165
166 /**
167  *      Draw a progress bar on framebuffer
168  * \param percent percentage of loading
169  */
170 static void fb_drawprogressbar(unsigned percent)
171 {
172         int i, left_x, top_y, width, height;
173
174         // outer box
175         left_x = G.nbar_posx;
176         top_y = G.nbar_posy;
177         width = G.nbar_width - 1;
178         height = G.nbar_height - 1;
179         if ((height | width) < 0)
180                 return;
181         // NB: "width" of 1 actually makes rect with width of 2!
182         fb_drawrectangle(
183                         left_x, top_y,
184                                         left_x + width, top_y + height,
185                         G.nbar_colr/2, G.nbar_colg/2, G.nbar_colb/2);
186
187         // inner "empty" rectangle
188         left_x++;
189         top_y++;
190         width -= 2;
191         height -= 2;
192         if ((height | width) < 0)
193                 return;
194         fb_drawfullrectangle(
195                         left_x, top_y,
196                                         left_x + width, top_y + height,
197                         G.nbar_colr, G.nbar_colg, G.nbar_colb);
198
199         if (percent > 0) {
200                 // actual progress bar
201                 width = width * percent / 100;
202                 i = height;
203                 if (height == 0)
204                         height++; // divide by 0 is bad
205                 while (i >= 0) {
206                         // draw one-line thick "rectangle"
207                         // top line will have gray lvl 200, bottom one 100
208                         unsigned gray_level = 100 + i*100/height;
209                         fb_drawfullrectangle(
210                                         left_x, top_y, left_x + width, top_y,
211                                         gray_level, gray_level, gray_level);
212                         top_y++;
213                         i--;
214                 }
215         }
216 }
217
218
219 /**
220  *      Draw image from PPM file
221  */
222 static void fb_drawimage(void)
223 {
224         char head[256];
225         char s[80];
226         FILE *theme_file;
227         unsigned char *pixline;
228         unsigned i, j, width, height, line_size;
229
230         memset(head, 0, sizeof(head));
231         theme_file = xfopen_stdin(G.image_filename);
232
233         // parse ppm header
234         while (1) {
235                 if (fgets(s, sizeof(s), theme_file) == NULL)
236                         bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
237
238                 if (s[0] == '#')
239                         continue;
240
241                 if (strlen(head) + strlen(s) >= sizeof(head))
242                         bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
243
244                 strcat(head, s);
245                 if (head[0] != 'P' || head[1] != '6')
246                         bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
247
248                 // width, height, max_color_val
249                 if (sscanf(head, "P6 %u %u %u", &width, &height, &i) == 3)
250                         break;
251 // TODO: i must be <= 255!
252         }
253
254         line_size = width*3;
255         if (width > G.scr_var.xres)
256                 width = G.scr_var.xres;
257         if (height > G.scr_var.yres)
258                 height = G.scr_var.yres;
259
260         pixline = xmalloc(line_size);
261         for (j = 0; j < height; j++) {
262                 unsigned char *pixel = pixline;
263                 DATA *src = (DATA *)(G.addr + j * G.scr_fix.line_length);
264
265                 if (fread(pixline, 1, line_size, theme_file) != line_size)
266                         bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
267                 for (i = 0; i < width; i++) {
268                         unsigned thispix;
269                         thispix = (((unsigned)pixel[0] << 8) & 0xf800)
270                                 | (((unsigned)pixel[1] << 3) & 0x07e0)
271                                 | (((unsigned)pixel[2] >> 3));
272                         *src++ = thispix;
273                         pixel += 3;
274                 }
275         }
276         free(pixline);
277         fclose(theme_file);
278 }
279
280
281 /**
282  * Parse configuration file
283  */
284 static void init(const char *ini_filename)
285 {
286         static const char const param_names[] ALIGN1 =
287                 "BAR_LEFT\0" "BAR_TOP\0"
288                 "BAR_WIDTH\0" "BAR_HEIGHT\0"
289                 "BAR_R\0" "BAR_G\0" "BAR_B\0"
290 #if DEBUG
291                 "DEBUG\0"
292 #endif
293                 ;
294
295         FILE *inifile;
296         char *buf;
297
298         inifile = xfopen_stdin(ini_filename);
299
300         while ((buf = xmalloc_fgetline(inifile)) != NULL) {
301                 char *value_str;
302                 int val;
303
304                 if (*buf == '#') {  // it's a comment
305                         free(buf);
306                         continue;
307                 }
308
309                 value_str = strchr(buf, '=');
310                 if (!value_str)
311                         goto err;
312                 *value_str++ = '\0';
313                 val = xatoi_u(value_str);
314
315                 switch (index_in_strings(param_names, buf)) {
316                 case 0:
317                         // progress bar horizontal position
318                         G.nbar_posx = val;
319                         break;
320                 case 1:
321                         // progress bar vertical position
322                         G.nbar_posy = val;
323                         break;
324                 case 2:
325                         // progress bar width
326                         G.nbar_width = val;
327                         break;
328                 case 3:
329                         // progress bar height
330                         G.nbar_height = val;
331                         break;
332                 case 4:
333                         // progress bar color - red component
334                         G.nbar_colr = val;
335                         break;
336                 case 5:
337                         // progress bar color - green component
338                         G.nbar_colg = val;
339                         break;
340                 case 6:
341                         // progress bar color - blue component
342                         G.nbar_colb = val;
343                         break;
344 #if DEBUG
345                 case 7:
346                         G.bdebug_messages = val;
347                         if (G.bdebug_messages)
348                                 G.logfile_fd = xfopen("/tmp/fbsplash.log", "w");
349                         break;
350 #endif
351  err:
352                 default:
353                         bb_error_msg_and_die("syntax error: '%s'", buf);
354                 }
355                 free(buf);
356         }
357         fclose(inifile);
358 }
359
360
361 int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
362 int fbsplash_main(int argc ATTRIBUTE_UNUSED, char **argv)
363 {
364         const char *fb_device, *ini_filename, *fifo_filename;
365         FILE *fp = fp; // for compiler
366         bool bCursorOff;
367
368         INIT_G();
369
370         // parse command line options
371         fb_device = "/dev/fb0";
372         ini_filename = NULL;
373         fifo_filename = NULL;
374         bCursorOff = 1 & getopt32(argv, "cs:d:i:f:",
375                         &G.image_filename, &fb_device, &ini_filename, &fifo_filename);
376
377         // parse configuration file
378         if (ini_filename)
379                 init(ini_filename);
380
381         // We must have -s IMG
382         if (!G.image_filename)
383                 bb_show_usage();
384
385         if (fifo_filename)
386                 fp = xfopen_stdin(fifo_filename);
387
388         fb_open(fb_device);
389
390         if (fifo_filename && bCursorOff) {
391                 // hide cursor (BEFORE any fb ops)
392                 full_write(STDOUT_FILENO, "\x1b" "[?25l", 6);
393         }
394
395         fb_drawimage();
396
397         if (fifo_filename) {
398                 unsigned num;
399                 char *num_buf;
400
401                 fb_drawprogressbar(0);
402                 // Block on read, waiting for some input.
403                 // Use of <stdio.h> style I/O allows to correctly
404                 // handle a case when we have many buffered lines
405                 // already in the pipe.
406                 while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
407                         if (strncmp(num_buf, "exit", 4) == 0) {
408                                 DEBUG_MESSAGE("exit");
409                                 break;
410                         }
411                         num = atoi(num_buf);
412                         if (isdigit(num_buf[0]) && (num <= 100)) {
413 #if DEBUG
414                                 char strVal[10];
415                                 sprintf(strVal, "%d", num);
416                                 DEBUG_MESSAGE(strVal);
417 #endif
418                                 fb_drawprogressbar(num);
419                         }
420                         free(num_buf);
421                 }
422                 if (bCursorOff) {
423                         // restore cursor
424                         full_write(STDOUT_FILENO, "\x1b" "[?25h", 6);
425                 }
426                 if (ENABLE_FEATURE_CLEAN_UP)
427                         fclose(fp);
428         }
429
430         return EXIT_SUCCESS;
431 }