1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#include "libbb.h"
37#include <linux/fb.h>
38
39
40#define DEBUG 0
41
42struct globals {
43#if DEBUG
44 bool bdebug_messages;
45 FILE *logfile_fd;
46#endif
47 unsigned char *addr;
48 unsigned ns[7];
49 const char *image_filename;
50 struct fb_var_screeninfo scr_var;
51 struct fb_fix_screeninfo scr_fix;
52 unsigned bytes_per_pixel;
53
54 unsigned red_shift;
55 unsigned green_shift;
56 unsigned blue_shift;
57};
58#define G (*ptr_to_globals)
59#define INIT_G() do { \
60 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
61} while (0)
62
63#define nbar_width ns[0]
64#define nbar_height ns[1]
65#define nbar_posx ns[2]
66#define nbar_posy ns[3]
67#define nbar_colr ns[4]
68#define nbar_colg ns[5]
69#define nbar_colb ns[6]
70
71#if DEBUG
72#define DEBUG_MESSAGE(strMessage, args...) \
73 if (G.bdebug_messages) { \
74 fprintf(G.logfile_fd, "[%s][%s] - %s\n", \
75 __FILE__, __FUNCTION__, strMessage); \
76 }
77#else
78#define DEBUG_MESSAGE(...) ((void)0)
79#endif
80
81
82
83
84static void fb_setpal(int fd)
85{
86 struct fb_cmap cmap;
87
88 unsigned short red[256], green[256], blue[256];
89 unsigned i;
90
91
92 for (i = 0; i < 256; i++) {
93
94
95
96
97
98
99
100
101
102
103 red[i] = (( i >> 5 ) * 0x9249) >> 2;
104 green[i] = (((i >> 2) & 0x7) * 0x9249) >> 2;
105
106 blue[i] = ( i & 0x3) * 0x5555;
107 }
108
109 cmap.start = 0;
110 cmap.len = 256;
111 cmap.red = red;
112 cmap.green = green;
113 cmap.blue = blue;
114 cmap.transp = 0;
115
116 xioctl(fd, FBIOPUTCMAP, &cmap);
117}
118
119
120
121
122
123static void fb_open(const char *strfb_device)
124{
125 int fbfd = xopen(strfb_device, O_RDWR);
126
127
128 xioctl(fbfd, FBIOGET_VSCREENINFO, &G.scr_var);
129 xioctl(fbfd, FBIOGET_FSCREENINFO, &G.scr_fix);
130
131 switch (G.scr_var.bits_per_pixel) {
132 case 8:
133 fb_setpal(fbfd);
134 break;
135
136 case 16:
137 case 24:
138 case 32:
139 break;
140
141 default:
142 bb_error_msg_and_die("unsupported %u bpp", (int)G.scr_var.bits_per_pixel);
143 break;
144 }
145
146 G.red_shift = 8 - G.scr_var.red.length;
147 G.green_shift = 8 - G.scr_var.green.length;
148 G.blue_shift = 8 - G.scr_var.blue.length;
149 G.bytes_per_pixel = (G.scr_var.bits_per_pixel + 7) >> 3;
150
151
152 G.addr = mmap(NULL,
153 G.scr_var.yres * G.scr_fix.line_length,
154 PROT_WRITE, MAP_SHARED, fbfd, 0);
155 if (G.addr == MAP_FAILED)
156 bb_perror_msg_and_die("mmap");
157
158
159 G.addr += G.scr_var.yoffset * G.scr_fix.line_length + G.scr_var.xoffset * G.bytes_per_pixel;
160 close(fbfd);
161}
162
163
164
165
166
167
168static unsigned fb_pixel_value(unsigned r, unsigned g, unsigned b)
169{
170
171
172 if (G.bytes_per_pixel == 1) {
173 r = r & 0xe0;
174 g = (g >> 3) & 0x1c;
175 b = b >> 6;
176 return r + g + b;
177 }
178 if (G.bytes_per_pixel == 2) {
179
180
181
182
183 r = r >> G.red_shift;
184 g = g >> G.green_shift;
185 b = b >> G.blue_shift;
186
187 return (r << G.scr_var.red.offset) +
188 (g << G.scr_var.green.offset) +
189 (b << G.scr_var.blue.offset);
190 }
191
192 return b + (g << 8) + (r << 16);
193}
194
195
196
197
198static void fb_write_pixel(unsigned char *addr, unsigned pixel)
199{
200 switch (G.bytes_per_pixel) {
201 case 1:
202 *addr = pixel;
203 break;
204 case 2:
205 *(uint16_t *)addr = pixel;
206 break;
207 case 4:
208 *(uint32_t *)addr = pixel;
209 break;
210 default:
211 addr[0] = pixel;
212 addr[1] = pixel >> 8;
213 addr[2] = pixel >> 16;
214 }
215}
216
217
218
219
220
221static void fb_drawrectangle(void)
222{
223 int cnt;
224 unsigned thispix;
225 unsigned char *ptr1, *ptr2;
226 unsigned char nred = G.nbar_colr/2;
227 unsigned char ngreen = G.nbar_colg/2;
228 unsigned char nblue = G.nbar_colb/2;
229
230 thispix = fb_pixel_value(nred, ngreen, nblue);
231
232
233 ptr1 = G.addr + G.nbar_posy * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
234 ptr2 = G.addr + (G.nbar_posy + G.nbar_height - 1) * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
235 cnt = G.nbar_width - 1;
236 do {
237 fb_write_pixel(ptr1, thispix);
238 fb_write_pixel(ptr2, thispix);
239 ptr1 += G.bytes_per_pixel;
240 ptr2 += G.bytes_per_pixel;
241 } while (--cnt >= 0);
242
243
244 ptr1 = G.addr + G.nbar_posy * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
245 ptr2 = G.addr + G.nbar_posy * G.scr_fix.line_length + (G.nbar_posx + G.nbar_width - 1) * G.bytes_per_pixel;
246 cnt = G.nbar_height - 1;
247 do {
248 fb_write_pixel(ptr1, thispix);
249 fb_write_pixel(ptr2, thispix);
250 ptr1 += G.scr_fix.line_length;
251 ptr2 += G.scr_fix.line_length;
252 } while (--cnt >= 0);
253}
254
255
256
257
258
259
260
261
262static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
263 unsigned char nred, unsigned char ngreen, unsigned char nblue)
264{
265 int cnt1, cnt2, nypos;
266 unsigned thispix;
267 unsigned char *ptr;
268
269 thispix = fb_pixel_value(nred, ngreen, nblue);
270
271 cnt1 = ny2pos - ny1pos;
272 nypos = ny1pos;
273 do {
274 ptr = G.addr + nypos * G.scr_fix.line_length + nx1pos * G.bytes_per_pixel;
275 cnt2 = nx2pos - nx1pos;
276 do {
277 fb_write_pixel(ptr, thispix);
278 ptr += G.bytes_per_pixel;
279 } while (--cnt2 >= 0);
280
281 nypos++;
282 } while (--cnt1 >= 0);
283}
284
285
286
287
288
289
290static void fb_drawprogressbar(unsigned percent)
291{
292 int left_x, top_y, pos_x;
293 unsigned width, height;
294
295
296 left_x = G.nbar_posx;
297 top_y = G.nbar_posy;
298 width = G.nbar_width - 1;
299 height = G.nbar_height - 1;
300 if ((int)(height | width) < 0)
301 return;
302
303 fb_drawrectangle();
304
305
306 left_x++;
307 top_y++;
308 width -= 2;
309 height -= 2;
310 if ((int)(height | width) < 0)
311 return;
312
313 pos_x = left_x;
314 if (percent > 0) {
315 int i, y;
316
317
318 pos_x += (unsigned)(width * percent) / 100;
319
320 y = top_y;
321 i = height;
322 if (height == 0)
323 height++;
324 while (i >= 0) {
325
326
327 unsigned gray_level = 100 + (unsigned)i*100 / height;
328 fb_drawfullrectangle(
329 left_x, y, pos_x, y,
330 gray_level, gray_level, gray_level);
331 y++;
332 i--;
333 }
334 }
335
336 fb_drawfullrectangle(
337 pos_x, top_y,
338 left_x + width, top_y + height,
339 G.nbar_colr, G.nbar_colg, G.nbar_colb);
340}
341
342
343
344
345
346static void fb_drawimage(void)
347{
348 FILE *theme_file;
349 char *read_ptr;
350 unsigned char *pixline;
351 unsigned i, j, width, height, line_size;
352
353 if (LONE_DASH(G.image_filename)) {
354 theme_file = stdin;
355 } else {
356 int fd = open_zipped(G.image_filename);
357 if (fd < 0)
358 bb_simple_perror_msg_and_die(G.image_filename);
359 theme_file = xfdopen_for_read(fd);
360 }
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375#define concat_buf bb_common_bufsiz1
376 read_ptr = concat_buf;
377 while (1) {
378 int w, h, max_color_val;
379 int rem = concat_buf + sizeof(concat_buf) - read_ptr;
380 if (rem < 2
381 || fgets(read_ptr, rem, theme_file) == NULL
382 ) {
383 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
384 }
385 read_ptr = strchrnul(read_ptr, '#');
386 *read_ptr = '\0';
387 if (sscanf(concat_buf, "P6 %u %u %u", &w, &h, &max_color_val) == 3
388 && max_color_val <= 255
389 ) {
390 width = w;
391 height = h;
392 break;
393 }
394 }
395
396 line_size = width*3;
397 pixline = xmalloc(line_size);
398
399 if (width > G.scr_var.xres)
400 width = G.scr_var.xres;
401 if (height > G.scr_var.yres)
402 height = G.scr_var.yres;
403 for (j = 0; j < height; j++) {
404 unsigned char *pixel;
405 unsigned char *src;
406
407 if (fread(pixline, 1, line_size, theme_file) != line_size)
408 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
409 pixel = pixline;
410 src = G.addr + j * G.scr_fix.line_length;
411 for (i = 0; i < width; i++) {
412 unsigned thispix = fb_pixel_value(pixel[0], pixel[1], pixel[2]);
413 fb_write_pixel(src, thispix);
414 src += G.bytes_per_pixel;
415 pixel += 3;
416 }
417 }
418 free(pixline);
419 fclose(theme_file);
420}
421
422
423
424
425
426
427static void init(const char *cfg_filename)
428{
429 static const char param_names[] ALIGN1 =
430 "BAR_WIDTH\0" "BAR_HEIGHT\0"
431 "BAR_LEFT\0" "BAR_TOP\0"
432 "BAR_R\0" "BAR_G\0" "BAR_B\0"
433#if DEBUG
434 "DEBUG\0"
435#endif
436 ;
437 char *token[2];
438 parser_t *parser = config_open2(cfg_filename, xfopen_stdin);
439 while (config_read(parser, token, 2, 2, "#=",
440 (PARSE_NORMAL | PARSE_MIN_DIE) & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
441 unsigned val = xatoi_positive(token[1]);
442 int i = index_in_strings(param_names, token[0]);
443 if (i < 0)
444 bb_error_msg_and_die("syntax error: %s", token[0]);
445 if (i >= 0 && i < 7)
446 G.ns[i] = val;
447#if DEBUG
448 if (i == 7) {
449 G.bdebug_messages = val;
450 if (G.bdebug_messages)
451 G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log");
452 }
453#endif
454 }
455 config_close(parser);
456}
457
458
459int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
460int fbsplash_main(int argc UNUSED_PARAM, char **argv)
461{
462 const char *fb_device, *cfg_filename, *fifo_filename;
463 FILE *fp = fp;
464 char *num_buf;
465 unsigned num;
466 bool bCursorOff;
467
468 INIT_G();
469
470
471 fb_device = "/dev/fb0";
472 cfg_filename = NULL;
473 fifo_filename = NULL;
474 bCursorOff = 1 & getopt32(argv, "cs:d:i:f:",
475 &G.image_filename, &fb_device, &cfg_filename, &fifo_filename);
476
477
478 if (cfg_filename)
479 init(cfg_filename);
480
481
482 if (!G.image_filename)
483 bb_show_usage();
484
485 fb_open(fb_device);
486
487 if (fifo_filename && bCursorOff) {
488
489 full_write(STDOUT_FILENO, "\033[?25l", 6);
490 }
491
492 fb_drawimage();
493
494 if (!fifo_filename)
495 return EXIT_SUCCESS;
496
497 fp = xfopen_stdin(fifo_filename);
498 if (fp != stdin) {
499
500
501
502
503
504
505
506
507
508
509
510 open(fifo_filename, O_WRONLY);
511 }
512
513 fb_drawprogressbar(0);
514
515
516
517
518 while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
519 if (strncmp(num_buf, "exit", 4) == 0) {
520 DEBUG_MESSAGE("exit");
521 break;
522 }
523 num = atoi(num_buf);
524 if (isdigit(num_buf[0]) && (num <= 100)) {
525#if DEBUG
526 DEBUG_MESSAGE(itoa(num));
527#endif
528 fb_drawprogressbar(num);
529 }
530 free(num_buf);
531 }
532
533 if (bCursorOff)
534 full_write(STDOUT_FILENO, "\033[?25h", 6);
535
536 return EXIT_SUCCESS;
537}
538