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