blob: a40c585a01461607694b155fd437ac44ad1a3fb4 [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] &
12 * -c: hide cursor
13 * -d /dev/fbN: framebuffer device (if not /dev/fb0)
Denis Vlasenko25a9c172008-03-26 15:12:11 +000014 * -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:
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
98 * \param nx1pos,ny1pos upper left position
99 * \param nx2pos,ny2pos down right position
100 * \param nred24,ngreen24,nblue24 rgb color
101 */
102static 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 nred24,ngreen24,nblue24 rgb color
139 */
140static 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 nPercent percentage of loading
169 */
170static void fb_drawprogressbar(unsigned nPercent)
171{
172 int i, left_x, top_y, width, height;
173 // 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!
181 fb_drawrectangle(
182 left_x, top_y,
183 left_x + width, top_y + height,
184 G.nbar_colr/2, G.nbar_colg/2, G.nbar_colb/2);
185
186 // inner "empty" rectangle
187 left_x++;
188 top_y++;
189 width -= 2;
190 height -= 2;
191 if ((height | width) < 0)
192 return;
193 fb_drawfullrectangle(
194 left_x, top_y,
195 left_x + width, top_y + height,
196 G.nbar_colr, G.nbar_colg, G.nbar_colb);
197
198 if (nPercent > 0) {
199 // actual progress bar
200 width = width*nPercent/100;
201 i = height;
202 if (height == 0)
203 height++; // divide by 0 is bad
204 while (i >= 0) {
205 // draw one-line thick "rectangle"
206 // top line will have gray lvl 200, bottom one 100
207 unsigned gray_level = 100 + i*100/height;
208 fb_drawfullrectangle(
209 left_x, top_y, left_x + width, top_y,
210 gray_level, gray_level, gray_level);
211 top_y++;
212 i--;
213 }
214 }
215}
216
217
218/**
219 * Draw image from PPM file
220 */
221static void fb_drawimage(void)
222{
223 char head[256];
224 char s[80];
225 FILE *theme_file;
226 unsigned char *pixline;
227 unsigned i, j, width, height, line_size;
228
229 memset(head, 0, sizeof(head));
230 theme_file = xfopen_stdin(G.image_filename);
231
232 // parse ppm header
233 while (1) {
234 if (fgets(s, sizeof(s), theme_file) == NULL)
235 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
236
237 if (s[0] == '#')
238 continue;
239
240 if (strlen(head) + strlen(s) >= sizeof(head))
241 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
242
243 strcat(head, s);
244 if (head[0] != 'P' || head[1] != '6')
245 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
246
247 // width, height, max_color_val
248 if (sscanf(head, "P6 %u %u %u", &width, &height, &i) == 3)
249 break;
250// TODO: i must be <= 255!
251 }
252
253 line_size = width*3;
254 if (width > G.scr_var.xres)
255 width = G.scr_var.xres;
256 if (height > G.scr_var.yres)
257 height = G.scr_var.yres;
258
259 pixline = xmalloc(line_size);
260 for (j = 0; j < height; j++) {
261 unsigned char *pixel = pixline;
262 DATA *src = (DATA *)(G.addr + j * G.scr_fix.line_length);
263
264 if (fread(pixline, 1, line_size, theme_file) != line_size)
265 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
266 for (i = 0; i < width; i++) {
267 unsigned thispix;
268 thispix = (((unsigned)pixel[0] << 8) & 0xf800)
269 | (((unsigned)pixel[1] << 3) & 0x07e0)
270 | (((unsigned)pixel[2] >> 3));
271 *src++ = thispix;
272 pixel += 3;
273 }
274 }
275 free(pixline);
276 fclose(theme_file);
277}
278
279
280/**
281 * Parse configuration file
282 */
283static void init(const char *ini_filename)
284{
285 static const char const param_names[] ALIGN1 =
286 "BAR_LEFT\0" "BAR_TOP\0"
287 "BAR_WIDTH\0" "BAR_HEIGHT\0"
288 "BAR_R\0" "BAR_G\0" "BAR_B\0"
289#if DEBUG
290 "DEBUG\0"
291#endif
292 ;
293
294 FILE *inifile;
295 char *buf;
296
297 inifile = xfopen(ini_filename, "r");
298
299 while ((buf = xmalloc_getline(inifile)) != NULL) {
300 char *value_str;
301 int val;
302
303 if (*buf == '#') { // it's a comment
304 free(buf);
305 continue;
306 }
307
308 value_str = strchr(buf, '=');
309 if (!value_str)
310 goto err;
311 *value_str++ = '\0';
312 val = xatoi_u(value_str);
313
314 switch (index_in_strings(param_names, buf)) {
315 case 0:
316 // progress bar horizontal position
317 G.nbar_posx = val;
318 break;
319 case 1:
320 // progress bar vertical position
321 G.nbar_posy = val;
322 break;
323 case 2:
324 // progress bar width
325 G.nbar_width = val;
326 break;
327 case 3:
328 // progress bar height
329 G.nbar_height = val;
330 break;
331 case 4:
332 // progress bar color - red component
333 G.nbar_colr = val;
334 break;
335 case 5:
336 // progress bar color - green component
337 G.nbar_colg = val;
338 break;
339 case 6:
340 // progress bar color - blue component
341 G.nbar_colb = val;
342 break;
343#if DEBUG
344 case 7:
345 G.bdebug_messages = val;
346 if (G.bdebug_messages)
Denis Vlasenko25a9c172008-03-26 15:12:11 +0000347 G.logfile_fd = xfopen("/tmp/fbsplash.log", "w");
Denis Vlasenkoc6dbb852008-03-26 14:57:49 +0000348 break;
349#endif
350 err:
351 default:
352 bb_error_msg_and_die("syntax error: '%s'", buf);
353 }
354 free(buf);
355 }
356 fclose(inifile);
357}
358
359
360int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
361int fbsplash_main(int argc ATTRIBUTE_UNUSED, char **argv)
362{
363 char num_buf[16];
364 const char *fb_device, *ini_filename, *fifo_filename;
365 int fd = fd; // for compiler
366 int len, num;
367 bool bCursorOff;
368
369 INIT_G();
370
371 // parse command line options
372 fb_device = "/dev/fb0";
373 ini_filename = NULL;
374 fifo_filename = NULL;
375 bCursorOff = 1 & getopt32(argv, "cs:d:i:f:",
376 &G.image_filename, &fb_device, &ini_filename, &fifo_filename);
377
378 // parse configuration file
379 if (ini_filename)
380 init(ini_filename);
381
382 // We must have -s IMG
383 if (!G.image_filename)
384 bb_show_usage();
385
386 if (fifo_filename) {
387 fd = STDIN_FILENO;
388 if (NOT_LONE_DASH(fifo_filename)) {
389 // open command fifo/pipe
390 fd = xopen(fifo_filename, O_RDONLY | O_NOCTTY);
391 }
392 }
393
394 fb_open(fb_device);
395
396 if (fifo_filename && bCursorOff) {
397 // hide cursor (BEFORE any fb ops)
398 full_write(STDOUT_FILENO, "\x1b" "[?25l", 6);
399 }
400
401 fb_drawimage();
402
403 if (fifo_filename) {
404 num = 0;
405 goto draw_bar;
406
407 while (1) {
408 // block on read, waiting for some input
409 len = safe_read(fd, num_buf, sizeof(num_buf) - 1);
410 if (len <= 0) // EOF/error
411 break;
412 num_buf[len] = '\0';
413 // parse command
414 if (strncmp(num_buf, "exit", 4) == 0) {
415 DEBUG_MESSAGE("exit");
416 break;
417 }
418 num = atoi(num_buf);
419 if (isdigit(num_buf[0]) && (num >= 0) && (num <= 100)) {
420#if DEBUG
421 char strVal[10];
422 sprintf(strVal, "%d", num);
423 DEBUG_MESSAGE(strVal);
424#endif
425 draw_bar:
426 fb_drawprogressbar(num);
427 }
428 }
429 if (bCursorOff) {
430 // restore cursor
431 full_write(STDOUT_FILENO, "\x1b" "[?25h", 6);
432 }
433 if (ENABLE_FEATURE_CLEAN_UP)
434 close(fd);
435 }
436
437#if DEBUG
438 if (ENABLE_FEATURE_CLEAN_UP)
439 if (G.bdebug_messages)
440 fclose(G.logfile_fd);
441#endif
442
443 return EXIT_SUCCESS;
444}