Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Bernhard Reutner-Fischer | dd76b79 | 2009-01-27 12:56:33 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008 Michele Sanges <michele.sanges@gmail.com> |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 4 | * |
| 5 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 6 | * |
| 7 | * Usage: |
| 8 | * - use kernel option 'vga=xxx' or otherwise enable framebuffer device. |
Denis Vlasenko | 25a9c17 | 2008-03-26 15:12:11 +0000 | [diff] [blame] | 9 | * - put somewhere fbsplash.cfg file and an image in .ppm format. |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 10 | * - run applet: $ setsid fbsplash [params] & |
Denis Vlasenko | 1f22898 | 2008-04-22 00:16:29 +0000 | [diff] [blame] | 11 | * -c: hide cursor |
| 12 | * -d /dev/fbN: framebuffer device (if not /dev/fb0) |
| 13 | * -s path_to_image_file (can be "-" for stdin) |
| 14 | * -i path_to_cfg_file |
| 15 | * -f path_to_fifo (can be "-" for stdin) |
Denis Vlasenko | 25a9c17 | 2008-03-26 15:12:11 +0000 | [diff] [blame] | 16 | * - if you want to run it only in presence of a kernel parameter |
| 17 | * (for example fbsplash=on), use: |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 18 | * grep -q "fbsplash=on" </proc/cmdline && setsid fbsplash [params] |
| 19 | * - commands for fifo: |
| 20 | * "NN" (ASCII decimal number) - percentage to show on progress bar. |
| 21 | * "exit" (or just close fifo) - well you guessed it. |
| 22 | */ |
| 23 | |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 24 | #include "libbb.h" |
| 25 | #include <linux/fb.h> |
| 26 | |
Denis Vlasenko | 25a9c17 | 2008-03-26 15:12:11 +0000 | [diff] [blame] | 27 | /* If you want logging messages on /tmp/fbsplash.log... */ |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 28 | #define DEBUG 0 |
| 29 | |
| 30 | #define BYTES_PER_PIXEL 2 |
| 31 | |
| 32 | typedef unsigned short DATA; |
| 33 | |
| 34 | struct globals { |
| 35 | #if DEBUG |
| 36 | bool bdebug_messages; // enable/disable logging |
| 37 | FILE *logfile_fd; // log file |
| 38 | #endif |
| 39 | unsigned char *addr; // pointer to framebuffer memory |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 40 | unsigned ns[7]; // n-parameters |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 41 | const char *image_filename; |
| 42 | struct fb_var_screeninfo scr_var; |
| 43 | struct fb_fix_screeninfo scr_fix; |
| 44 | }; |
| 45 | #define G (*ptr_to_globals) |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 46 | #define INIT_G() do { \ |
| 47 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 48 | } while (0) |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 49 | |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 50 | #define nbar_width ns[0] // progress bar width |
| 51 | #define nbar_height ns[1] // progress bar height |
| 52 | #define nbar_posx ns[2] // progress bar horizontal position |
| 53 | #define nbar_posy ns[3] // progress bar vertical position |
| 54 | #define nbar_colr ns[4] // progress bar color red component |
| 55 | #define nbar_colg ns[5] // progress bar color green component |
| 56 | #define nbar_colb ns[6] // progress bar color blue component |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 57 | |
| 58 | #if DEBUG |
| 59 | #define DEBUG_MESSAGE(strMessage, args...) \ |
| 60 | if (G.bdebug_messages) { \ |
| 61 | fprintf(G.logfile_fd, "[%s][%s] - %s\n", \ |
| 62 | __FILE__, __FUNCTION__, strMessage); \ |
| 63 | } |
| 64 | #else |
| 65 | #define DEBUG_MESSAGE(...) ((void)0) |
| 66 | #endif |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * Open and initialize the framebuffer device |
| 71 | * \param *strfb_device pointer to framebuffer device |
| 72 | */ |
| 73 | static void fb_open(const char *strfb_device) |
| 74 | { |
| 75 | int fbfd = xopen(strfb_device, O_RDWR); |
| 76 | |
| 77 | // framebuffer properties |
| 78 | xioctl(fbfd, FBIOGET_VSCREENINFO, &G.scr_var); |
| 79 | xioctl(fbfd, FBIOGET_FSCREENINFO, &G.scr_fix); |
| 80 | |
| 81 | if (G.scr_var.bits_per_pixel != 16) |
| 82 | bb_error_msg_and_die("only 16 bpp is supported"); |
| 83 | |
| 84 | // map the device in memory |
| 85 | G.addr = mmap(NULL, |
| 86 | G.scr_var.xres * G.scr_var.yres |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 87 | * BYTES_PER_PIXEL /*(G.scr_var.bits_per_pixel / 8)*/, |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 88 | PROT_WRITE, MAP_SHARED, fbfd, 0); |
| 89 | if (G.addr == MAP_FAILED) |
Bernhard Reutner-Fischer | 4efcec9 | 2009-02-14 12:36:16 +0000 | [diff] [blame] | 90 | bb_perror_msg_and_die("mmap"); |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 91 | close(fbfd); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /** |
| 96 | * Draw hollow rectangle on framebuffer |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 97 | */ |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 98 | static void fb_drawrectangle(void) |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 99 | { |
| 100 | int cnt; |
| 101 | DATA thispix; |
| 102 | DATA *ptr1, *ptr2; |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 103 | unsigned char nred = G.nbar_colr/2; |
| 104 | unsigned char ngreen = G.nbar_colg/2; |
| 105 | unsigned char nblue = G.nbar_colb/2; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 106 | |
| 107 | nred >>= 3; // 5-bit red |
| 108 | ngreen >>= 2; // 6-bit green |
| 109 | nblue >>= 3; // 5-bit blue |
| 110 | thispix = nblue + (ngreen << 5) + (nred << (5+6)); |
| 111 | |
| 112 | // horizontal lines |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 113 | ptr1 = (DATA*)(G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx) * BYTES_PER_PIXEL); |
| 114 | ptr2 = (DATA*)(G.addr + ((G.nbar_posy + G.nbar_height - 1) * G.scr_var.xres + G.nbar_posx) * BYTES_PER_PIXEL); |
| 115 | cnt = G.nbar_width - 1; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 116 | do { |
| 117 | *ptr1++ = thispix; |
| 118 | *ptr2++ = thispix; |
| 119 | } while (--cnt >= 0); |
| 120 | |
| 121 | // vertical lines |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 122 | ptr1 = (DATA*)(G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx) * BYTES_PER_PIXEL); |
| 123 | ptr2 = (DATA*)(G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx + G.nbar_width - 1) * BYTES_PER_PIXEL); |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 124 | cnt = G.nbar_height - 1; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 125 | do { |
| 126 | *ptr1 = thispix; ptr1 += G.scr_var.xres; |
| 127 | *ptr2 = thispix; ptr2 += G.scr_var.xres; |
| 128 | } while (--cnt >= 0); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /** |
| 133 | * Draw filled rectangle on framebuffer |
| 134 | * \param nx1pos,ny1pos upper left position |
| 135 | * \param nx2pos,ny2pos down right position |
Denis Vlasenko | 11b9f26 | 2008-03-26 20:06:24 +0000 | [diff] [blame] | 136 | * \param nred,ngreen,nblue rgb color |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 137 | */ |
| 138 | static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos, |
| 139 | unsigned char nred, unsigned char ngreen, unsigned char nblue) |
| 140 | { |
| 141 | int cnt1, cnt2, nypos; |
| 142 | DATA thispix; |
| 143 | DATA *ptr; |
| 144 | |
| 145 | nred >>= 3; // 5-bit red |
| 146 | ngreen >>= 2; // 6-bit green |
| 147 | nblue >>= 3; // 5-bit blue |
| 148 | thispix = nblue + (ngreen << 5) + (nred << (5+6)); |
Denis Vlasenko | 1f22898 | 2008-04-22 00:16:29 +0000 | [diff] [blame] | 149 | |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 150 | cnt1 = ny2pos - ny1pos; |
| 151 | nypos = ny1pos; |
| 152 | do { |
| 153 | ptr = (DATA*)(G.addr + (nypos * G.scr_var.xres + nx1pos) * BYTES_PER_PIXEL); |
| 154 | cnt2 = nx2pos - nx1pos; |
| 155 | do { |
| 156 | *ptr++ = thispix; |
| 157 | } while (--cnt2 >= 0); |
Denis Vlasenko | 1f22898 | 2008-04-22 00:16:29 +0000 | [diff] [blame] | 158 | |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 159 | nypos++; |
| 160 | } while (--cnt1 >= 0); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /** |
| 165 | * Draw a progress bar on framebuffer |
Denis Vlasenko | 11b9f26 | 2008-03-26 20:06:24 +0000 | [diff] [blame] | 166 | * \param percent percentage of loading |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 167 | */ |
Denis Vlasenko | 11b9f26 | 2008-03-26 20:06:24 +0000 | [diff] [blame] | 168 | static void fb_drawprogressbar(unsigned percent) |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 169 | { |
| 170 | int i, left_x, top_y, width, height; |
Denis Vlasenko | 11b9f26 | 2008-03-26 20:06:24 +0000 | [diff] [blame] | 171 | |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 172 | // outer box |
| 173 | left_x = G.nbar_posx; |
| 174 | top_y = G.nbar_posy; |
| 175 | width = G.nbar_width - 1; |
| 176 | height = G.nbar_height - 1; |
| 177 | if ((height | width) < 0) |
| 178 | return; |
| 179 | // NB: "width" of 1 actually makes rect with width of 2! |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 180 | fb_drawrectangle(); |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 181 | |
| 182 | // inner "empty" rectangle |
| 183 | left_x++; |
| 184 | top_y++; |
| 185 | width -= 2; |
| 186 | height -= 2; |
| 187 | if ((height | width) < 0) |
| 188 | return; |
| 189 | fb_drawfullrectangle( |
| 190 | left_x, top_y, |
| 191 | left_x + width, top_y + height, |
| 192 | G.nbar_colr, G.nbar_colg, G.nbar_colb); |
| 193 | |
Denis Vlasenko | 11b9f26 | 2008-03-26 20:06:24 +0000 | [diff] [blame] | 194 | if (percent > 0) { |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 195 | // actual progress bar |
Denis Vlasenko | 11b9f26 | 2008-03-26 20:06:24 +0000 | [diff] [blame] | 196 | width = width * percent / 100; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 197 | i = height; |
| 198 | if (height == 0) |
| 199 | height++; // divide by 0 is bad |
| 200 | while (i >= 0) { |
| 201 | // draw one-line thick "rectangle" |
| 202 | // top line will have gray lvl 200, bottom one 100 |
| 203 | unsigned gray_level = 100 + i*100/height; |
| 204 | fb_drawfullrectangle( |
| 205 | left_x, top_y, left_x + width, top_y, |
| 206 | gray_level, gray_level, gray_level); |
| 207 | top_y++; |
| 208 | i--; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | |
| 214 | /** |
| 215 | * Draw image from PPM file |
| 216 | */ |
| 217 | static void fb_drawimage(void) |
| 218 | { |
Bernhard Reutner-Fischer | 051fdb9 | 2009-02-16 12:36:50 +0000 | [diff] [blame] | 219 | FILE *theme_file; |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 220 | char *read_ptr; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 221 | unsigned char *pixline; |
| 222 | unsigned i, j, width, height, line_size; |
| 223 | |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 224 | if (LONE_DASH(G.image_filename)) { |
Vladimir Dronnikov | 6eaac02 | 2009-11-07 04:14:50 +0100 | [diff] [blame] | 225 | theme_file = stdin; |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 226 | } else { |
Vladimir Dronnikov | 6eaac02 | 2009-11-07 04:14:50 +0100 | [diff] [blame] | 227 | int fd = open_zipped(G.image_filename); |
| 228 | if (fd < 0) |
| 229 | bb_simple_perror_msg_and_die(G.image_filename); |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 230 | theme_file = xfdopen_for_read(fd); |
Vladimir Dronnikov | 6eaac02 | 2009-11-07 04:14:50 +0100 | [diff] [blame] | 231 | } |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 232 | |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 233 | /* Parse ppm header: |
| 234 | * - Magic: two characters "P6". |
Bernhard Reutner-Fischer | 8dcb33c | 2009-02-18 15:13:05 +0000 | [diff] [blame] | 235 | * - Whitespace (blanks, TABs, CRs, LFs). |
| 236 | * - A width, formatted as ASCII characters in decimal. |
| 237 | * - Whitespace. |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 238 | * - A height, ASCII decimal. |
Bernhard Reutner-Fischer | 8dcb33c | 2009-02-18 15:13:05 +0000 | [diff] [blame] | 239 | * - Whitespace. |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 240 | * - The maximum color value, ASCII decimal, in 0..65535 |
Bernhard Reutner-Fischer | 8dcb33c | 2009-02-18 15:13:05 +0000 | [diff] [blame] | 241 | * - Newline or other single whitespace character. |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 242 | * (we support newline only) |
Bernhard Reutner-Fischer | 8dcb33c | 2009-02-18 15:13:05 +0000 | [diff] [blame] | 243 | * - A raster of Width * Height pixels in triplets of rgb |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 244 | * in pure binary by 1 or 2 bytes. (we support only 1 byte) |
Bernhard Reutner-Fischer | 8dcb33c | 2009-02-18 15:13:05 +0000 | [diff] [blame] | 245 | */ |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 246 | #define concat_buf bb_common_bufsiz1 |
| 247 | read_ptr = concat_buf; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 248 | while (1) { |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 249 | int w, h, max_color_val; |
| 250 | int rem = concat_buf + sizeof(concat_buf) - read_ptr; |
| 251 | if (rem < 2 |
| 252 | || fgets(read_ptr, rem, theme_file) == NULL |
| 253 | ) { |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 254 | bb_error_msg_and_die("bad PPM file '%s'", G.image_filename); |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 255 | } |
| 256 | read_ptr = strchrnul(read_ptr, '#'); |
| 257 | *read_ptr = '\0'; /* ignore #comments */ |
| 258 | if (sscanf(concat_buf, "P6 %u %u %u", &w, &h, &max_color_val) == 3 |
| 259 | && max_color_val <= 255 |
| 260 | ) { |
| 261 | width = w; /* w is on stack, width may be in register */ |
| 262 | height = h; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 263 | break; |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 264 | } |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | line_size = width*3; |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 268 | pixline = xmalloc(line_size); |
| 269 | |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 270 | if (width > G.scr_var.xres) |
| 271 | width = G.scr_var.xres; |
| 272 | if (height > G.scr_var.yres) |
| 273 | height = G.scr_var.yres; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 274 | for (j = 0; j < height; j++) { |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 275 | unsigned char *pixel; |
| 276 | DATA *src; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 277 | |
Bernhard Reutner-Fischer | 051fdb9 | 2009-02-16 12:36:50 +0000 | [diff] [blame] | 278 | if (fread(pixline, 1, line_size, theme_file) != line_size) |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 279 | bb_error_msg_and_die("bad PPM file '%s'", G.image_filename); |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 280 | pixel = pixline; |
| 281 | src = (DATA *)(G.addr + j * G.scr_fix.line_length); |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 282 | for (i = 0; i < width; i++) { |
| 283 | unsigned thispix; |
| 284 | thispix = (((unsigned)pixel[0] << 8) & 0xf800) |
| 285 | | (((unsigned)pixel[1] << 3) & 0x07e0) |
| 286 | | (((unsigned)pixel[2] >> 3)); |
| 287 | *src++ = thispix; |
| 288 | pixel += 3; |
| 289 | } |
| 290 | } |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 291 | free(pixline); |
Bernhard Reutner-Fischer | 051fdb9 | 2009-02-16 12:36:50 +0000 | [diff] [blame] | 292 | fclose(theme_file); |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | |
| 296 | /** |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 297 | * Parse configuration file |
| 298 | * \param *cfg_filename name of the configuration file |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 299 | */ |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 300 | static void init(const char *cfg_filename) |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 301 | { |
Denys Vlasenko | 10544a8 | 2010-02-09 00:26:10 +0100 | [diff] [blame] | 302 | static const char param_names[] ALIGN1 = |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 303 | "BAR_WIDTH\0" "BAR_HEIGHT\0" |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 304 | "BAR_LEFT\0" "BAR_TOP\0" |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 305 | "BAR_R\0" "BAR_G\0" "BAR_B\0" |
| 306 | #if DEBUG |
| 307 | "DEBUG\0" |
| 308 | #endif |
| 309 | ; |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 310 | char *token[2]; |
| 311 | parser_t *parser = config_open2(cfg_filename, xfopen_stdin); |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 312 | while (config_read(parser, token, 2, 2, "#=", |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 313 | (PARSE_NORMAL | PARSE_MIN_DIE) & ~(PARSE_TRIM | PARSE_COLLAPSE))) { |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 314 | unsigned val = xatoi_u(token[1]); |
| 315 | int i = index_in_strings(param_names, token[0]); |
| 316 | if (i < 0) |
Bernhard Reutner-Fischer | 4efcec9 | 2009-02-14 12:36:16 +0000 | [diff] [blame] | 317 | bb_error_msg_and_die("syntax error: %s", token[0]); |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 318 | if (i >= 0 && i < 7) |
| 319 | G.ns[i] = val; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 320 | #if DEBUG |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 321 | if (i == 7) { |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 322 | G.bdebug_messages = val; |
| 323 | if (G.bdebug_messages) |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 324 | G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log"); |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 325 | } |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 326 | #endif |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 327 | } |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 328 | config_close(parser); |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | |
| 332 | int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 333 | int fbsplash_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 334 | { |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 335 | const char *fb_device, *cfg_filename, *fifo_filename; |
Denis Vlasenko | 11b9f26 | 2008-03-26 20:06:24 +0000 | [diff] [blame] | 336 | FILE *fp = fp; // for compiler |
Denis Vlasenko | 7f8f0fa | 2008-04-04 20:38:49 +0000 | [diff] [blame] | 337 | char *num_buf; |
| 338 | unsigned num; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 339 | bool bCursorOff; |
| 340 | |
| 341 | INIT_G(); |
| 342 | |
| 343 | // parse command line options |
| 344 | fb_device = "/dev/fb0"; |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 345 | cfg_filename = NULL; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 346 | fifo_filename = NULL; |
| 347 | bCursorOff = 1 & getopt32(argv, "cs:d:i:f:", |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 348 | &G.image_filename, &fb_device, &cfg_filename, &fifo_filename); |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 349 | |
| 350 | // parse configuration file |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 351 | if (cfg_filename) |
| 352 | init(cfg_filename); |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 353 | |
| 354 | // We must have -s IMG |
| 355 | if (!G.image_filename) |
| 356 | bb_show_usage(); |
| 357 | |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 358 | fb_open(fb_device); |
| 359 | |
| 360 | if (fifo_filename && bCursorOff) { |
| 361 | // hide cursor (BEFORE any fb ops) |
Denys Vlasenko | d9a3e89 | 2010-05-16 23:42:13 +0200 | [diff] [blame] | 362 | full_write(STDOUT_FILENO, "\033[?25l", 6); |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | fb_drawimage(); |
| 366 | |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 367 | if (!fifo_filename) |
| 368 | return EXIT_SUCCESS; |
| 369 | |
| 370 | fp = xfopen_stdin(fifo_filename); |
Denis Vlasenko | 7f8f0fa | 2008-04-04 20:38:49 +0000 | [diff] [blame] | 371 | if (fp != stdin) { |
Denis Vlasenko | 72b3442 | 2008-03-27 13:14:29 +0000 | [diff] [blame] | 372 | // For named pipes, we want to support this: |
| 373 | // mkfifo cmd_pipe |
| 374 | // fbsplash -f cmd_pipe .... & |
| 375 | // ... |
| 376 | // echo 33 >cmd_pipe |
| 377 | // ... |
| 378 | // echo 66 >cmd_pipe |
Denis Vlasenko | 7f8f0fa | 2008-04-04 20:38:49 +0000 | [diff] [blame] | 379 | // This means that we don't want fbsplash to get EOF |
| 380 | // when last writer closes input end. |
| 381 | // The simplest way is to open fifo for writing too |
| 382 | // and become an additional writer :) |
| 383 | open(fifo_filename, O_WRONLY); // errors are ignored |
| 384 | } |
| 385 | |
| 386 | fb_drawprogressbar(0); |
| 387 | // Block on read, waiting for some input. |
| 388 | // Use of <stdio.h> style I/O allows to correctly |
| 389 | // handle a case when we have many buffered lines |
| 390 | // already in the pipe |
| 391 | while ((num_buf = xmalloc_fgetline(fp)) != NULL) { |
| 392 | if (strncmp(num_buf, "exit", 4) == 0) { |
| 393 | DEBUG_MESSAGE("exit"); |
| 394 | break; |
| 395 | } |
| 396 | num = atoi(num_buf); |
| 397 | if (isdigit(num_buf[0]) && (num <= 100)) { |
| 398 | #if DEBUG |
Denys Vlasenko | 7bb346f | 2009-10-06 22:09:50 +0200 | [diff] [blame] | 399 | DEBUG_MESSAGE(itoa(num)); |
Denis Vlasenko | 7f8f0fa | 2008-04-04 20:38:49 +0000 | [diff] [blame] | 400 | #endif |
| 401 | fb_drawprogressbar(num); |
| 402 | } |
| 403 | free(num_buf); |
| 404 | } |
| 405 | |
| 406 | if (bCursorOff) // restore cursor |
Denys Vlasenko | d9a3e89 | 2010-05-16 23:42:13 +0200 | [diff] [blame] | 407 | full_write(STDOUT_FILENO, "\033[?25h", 6); |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 408 | |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 409 | return EXIT_SUCCESS; |
| 410 | } |