Annotation of /tags/mkinitrd-6_2_0/miscutils/fbsplash.c
Parent Directory | Revision Log
Revision 816 -
(hide annotations)
(download)
Fri Apr 24 18:33:46 2009 UTC (15 years, 1 month ago) by niro
Original Path: trunk/mkinitrd-magellan/busybox/miscutils/fbsplash.c
File MIME type: text/plain
File size: 10140 byte(s)
Fri Apr 24 18:33:46 2009 UTC (15 years, 1 month ago) by niro
Original Path: trunk/mkinitrd-magellan/busybox/miscutils/fbsplash.c
File MIME type: text/plain
File size: 10140 byte(s)
-updated to busybox-1.13.4
1 | niro | 816 | /* 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 ns[7]; // n-parameters | ||
42 | 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) | ||
47 | #define INIT_G() do { \ | ||
48 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ | ||
49 | } while (0) | ||
50 | |||
51 | #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 | ||
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 | */ | ||
99 | static void fb_drawrectangle(void) | ||
100 | { | ||
101 | int cnt; | ||
102 | DATA thispix; | ||
103 | DATA *ptr1, *ptr2; | ||
104 | unsigned char nred = G.nbar_colr/2; | ||
105 | unsigned char ngreen = G.nbar_colg/2; | ||
106 | unsigned char nblue = G.nbar_colb/2; | ||
107 | |||
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 | ||
114 | 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; | ||
117 | do { | ||
118 | *ptr1++ = thispix; | ||
119 | *ptr2++ = thispix; | ||
120 | } while (--cnt >= 0); | ||
121 | |||
122 | // vertical lines | ||
123 | 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; | ||
126 | 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 | ||
137 | * \param nred,ngreen,nblue rgb color | ||
138 | */ | ||
139 | static 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)); | ||
150 | |||
151 | 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); | ||
159 | |||
160 | nypos++; | ||
161 | } while (--cnt1 >= 0); | ||
162 | } | ||
163 | |||
164 | |||
165 | /** | ||
166 | * Draw a progress bar on framebuffer | ||
167 | * \param percent percentage of loading | ||
168 | */ | ||
169 | static void fb_drawprogressbar(unsigned percent) | ||
170 | { | ||
171 | int i, left_x, top_y, width, height; | ||
172 | |||
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 | |||
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 | |||
195 | if (percent > 0) { | ||
196 | // actual progress bar | ||
197 | width = width * percent / 100; | ||
198 | 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 | */ | ||
218 | static 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 | /** | ||
278 | * Parse configuration file | ||
279 | * \param *cfg_filename name of the configuration file | ||
280 | */ | ||
281 | static void init(const char *cfg_filename) | ||
282 | { | ||
283 | static const char const param_names[] ALIGN1 = | ||
284 | "BAR_WIDTH\0" "BAR_HEIGHT\0" | ||
285 | "BAR_LEFT\0" "BAR_TOP\0" | ||
286 | "BAR_R\0" "BAR_G\0" "BAR_B\0" | ||
287 | #if DEBUG | ||
288 | "DEBUG\0" | ||
289 | #endif | ||
290 | ; | ||
291 | char *token[2]; | ||
292 | parser_t *parser = config_open2(cfg_filename, xfopen_stdin); | ||
293 | while (config_read(parser, token, 2, 2, "#=", | ||
294 | (PARSE_NORMAL | PARSE_MIN_DIE) & ~(PARSE_TRIM | PARSE_COLLAPSE))) { | ||
295 | 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; | ||
301 | #if DEBUG | ||
302 | if (i == 7) { | ||
303 | G.bdebug_messages = val; | ||
304 | if (G.bdebug_messages) | ||
305 | G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log"); | ||
306 | } | ||
307 | #endif | ||
308 | } | ||
309 | config_close(parser); | ||
310 | } | ||
311 | |||
312 | |||
313 | int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
314 | int fbsplash_main(int argc UNUSED_PARAM, char **argv) | ||
315 | { | ||
316 | const char *fb_device, *cfg_filename, *fifo_filename; | ||
317 | FILE *fp = fp; // for compiler | ||
318 | char *num_buf; | ||
319 | unsigned num; | ||
320 | bool bCursorOff; | ||
321 | |||
322 | INIT_G(); | ||
323 | |||
324 | // parse command line options | ||
325 | fb_device = "/dev/fb0"; | ||
326 | cfg_filename = NULL; | ||
327 | fifo_filename = NULL; | ||
328 | bCursorOff = 1 & getopt32(argv, "cs:d:i:f:", | ||
329 | &G.image_filename, &fb_device, &cfg_filename, &fifo_filename); | ||
330 | |||
331 | // parse configuration file | ||
332 | if (cfg_filename) | ||
333 | init(cfg_filename); | ||
334 | |||
335 | // We must have -s IMG | ||
336 | if (!G.image_filename) | ||
337 | bb_show_usage(); | ||
338 | |||
339 | 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 | |||
348 | if (!fifo_filename) | ||
349 | return EXIT_SUCCESS; | ||
350 | |||
351 | fp = xfopen_stdin(fifo_filename); | ||
352 | if (fp != stdin) { | ||
353 | // 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 | ||
360 | // 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); | ||
391 | |||
392 | return EXIT_SUCCESS; | ||
393 | } |