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 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 24 | //usage:#define fbsplash_trivial_usage |
| 25 | //usage: "-s IMGFILE [-c] [-d DEV] [-i INIFILE] [-f CMD]" |
| 26 | //usage:#define fbsplash_full_usage "\n\n" |
Denys Vlasenko | 6642676 | 2011-06-05 03:58:28 +0200 | [diff] [blame] | 27 | //usage: " -s Image" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 28 | //usage: "\n -c Hide cursor" |
| 29 | //usage: "\n -d Framebuffer device (default /dev/fb0)" |
| 30 | //usage: "\n -i Config file (var=value):" |
| 31 | //usage: "\n BAR_LEFT,BAR_TOP,BAR_WIDTH,BAR_HEIGHT" |
| 32 | //usage: "\n BAR_R,BAR_G,BAR_B" |
| 33 | //usage: "\n -f Control pipe (else exit after drawing image)" |
| 34 | //usage: "\n commands: 'NN' (% for progress bar) or 'exit'" |
| 35 | |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 36 | #include "libbb.h" |
| 37 | #include <linux/fb.h> |
| 38 | |
Denis Vlasenko | 25a9c17 | 2008-03-26 15:12:11 +0000 | [diff] [blame] | 39 | /* If you want logging messages on /tmp/fbsplash.log... */ |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 40 | #define DEBUG 0 |
| 41 | |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 42 | struct globals { |
| 43 | #if DEBUG |
| 44 | bool bdebug_messages; // enable/disable logging |
| 45 | FILE *logfile_fd; // log file |
| 46 | #endif |
| 47 | unsigned char *addr; // pointer to framebuffer memory |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 48 | unsigned ns[7]; // n-parameters |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 49 | const char *image_filename; |
| 50 | struct fb_var_screeninfo scr_var; |
| 51 | struct fb_fix_screeninfo scr_fix; |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 52 | unsigned bytes_per_pixel; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 53 | }; |
| 54 | #define G (*ptr_to_globals) |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 55 | #define INIT_G() do { \ |
| 56 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 57 | } while (0) |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 58 | |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 59 | #define nbar_width ns[0] // progress bar width |
| 60 | #define nbar_height ns[1] // progress bar height |
| 61 | #define nbar_posx ns[2] // progress bar horizontal position |
| 62 | #define nbar_posy ns[3] // progress bar vertical position |
| 63 | #define nbar_colr ns[4] // progress bar color red component |
| 64 | #define nbar_colg ns[5] // progress bar color green component |
| 65 | #define nbar_colb ns[6] // progress bar color blue component |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 66 | |
| 67 | #if DEBUG |
| 68 | #define DEBUG_MESSAGE(strMessage, args...) \ |
| 69 | if (G.bdebug_messages) { \ |
| 70 | fprintf(G.logfile_fd, "[%s][%s] - %s\n", \ |
| 71 | __FILE__, __FUNCTION__, strMessage); \ |
| 72 | } |
| 73 | #else |
| 74 | #define DEBUG_MESSAGE(...) ((void)0) |
| 75 | #endif |
| 76 | |
Peter Korsgaard | cd06e06 | 2011-10-17 05:31:52 +0200 | [diff] [blame] | 77 | /** |
| 78 | * Configure palette for RGB:332 |
| 79 | */ |
| 80 | static void fb_setpal(int fd) |
| 81 | { |
| 82 | struct fb_cmap cmap; |
| 83 | /* fb colors are 16 bit */ |
| 84 | unsigned short red[256], green[256], blue[256]; |
| 85 | unsigned i; |
| 86 | |
| 87 | /* RGB:332 */ |
| 88 | for (i = 0; i < 256; i++) { |
| 89 | /* Color is encoded in pixel value as rrrgggbb. |
| 90 | * 3-bit color is mapped to 16-bit one as: |
| 91 | * 000 -> 00000000 00000000 |
| 92 | * 001 -> 00100100 10010010 |
| 93 | * ... |
| 94 | * 011 -> 01101101 10110110 |
| 95 | * 100 -> 10010010 01001001 |
| 96 | * ... |
| 97 | * 111 -> 11111111 11111111 |
| 98 | */ |
| 99 | red[i] = (( i >> 5 ) * 0x9249) >> 2; // rrr * 00 10010010 01001001 >> 2 |
| 100 | green[i] = (((i >> 2) & 0x7) * 0x9249) >> 2; // ggg * 00 10010010 01001001 >> 2 |
| 101 | /* 2-bit color is easier: */ |
| 102 | blue[i] = ( i & 0x3) * 0x5555; // bb * 01010101 01010101 |
| 103 | } |
| 104 | |
| 105 | cmap.start = 0; |
| 106 | cmap.len = 256; |
| 107 | cmap.red = red; |
| 108 | cmap.green = green; |
| 109 | cmap.blue = blue; |
| 110 | cmap.transp = 0; |
| 111 | |
| 112 | xioctl(fd, FBIOPUTCMAP, &cmap); |
| 113 | } |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 114 | |
| 115 | /** |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 116 | * Open and initialize the framebuffer device |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 117 | * \param *strfb_device pointer to framebuffer device |
| 118 | */ |
| 119 | static void fb_open(const char *strfb_device) |
| 120 | { |
| 121 | int fbfd = xopen(strfb_device, O_RDWR); |
| 122 | |
| 123 | // framebuffer properties |
| 124 | xioctl(fbfd, FBIOGET_VSCREENINFO, &G.scr_var); |
| 125 | xioctl(fbfd, FBIOGET_FSCREENINFO, &G.scr_fix); |
| 126 | |
Peter Korsgaard | cd06e06 | 2011-10-17 05:31:52 +0200 | [diff] [blame] | 127 | switch (G.scr_var.bits_per_pixel) { |
| 128 | case 8: |
| 129 | fb_setpal(fbfd); |
| 130 | break; |
| 131 | |
| 132 | case 16: |
| 133 | case 24: |
| 134 | case 32: |
| 135 | break; |
| 136 | |
| 137 | default: |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 138 | bb_error_msg_and_die("unsupported %u bpp", (int)G.scr_var.bits_per_pixel); |
Peter Korsgaard | cd06e06 | 2011-10-17 05:31:52 +0200 | [diff] [blame] | 139 | break; |
| 140 | } |
| 141 | |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 142 | G.bytes_per_pixel = (G.scr_var.bits_per_pixel + 7) >> 3; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 143 | |
| 144 | // map the device in memory |
| 145 | G.addr = mmap(NULL, |
Yin Kangkai | 3ac066c | 2012-02-23 15:56:36 +0800 | [diff] [blame] | 146 | G.scr_var.yres * G.scr_fix.line_length, |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 147 | PROT_WRITE, MAP_SHARED, fbfd, 0); |
| 148 | if (G.addr == MAP_FAILED) |
Bernhard Reutner-Fischer | 4efcec9 | 2009-02-14 12:36:16 +0000 | [diff] [blame] | 149 | bb_perror_msg_and_die("mmap"); |
Dan Fandrich | 07078c2 | 2011-01-26 11:30:34 -0800 | [diff] [blame] | 150 | |
| 151 | // point to the start of the visible screen |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 152 | G.addr += G.scr_var.yoffset * G.scr_fix.line_length + G.scr_var.xoffset * G.bytes_per_pixel; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 153 | close(fbfd); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /** |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 158 | * Return pixel value of the passed RGB color |
| 159 | */ |
| 160 | static unsigned fb_pixel_value(unsigned r, unsigned g, unsigned b) |
| 161 | { |
Peter Korsgaard | cd06e06 | 2011-10-17 05:31:52 +0200 | [diff] [blame] | 162 | if (G.bytes_per_pixel == 1) { |
| 163 | r = r & 0xe0; // 3-bit red |
| 164 | g = (g >> 3) & 0x1c; // 3-bit green |
| 165 | b = b >> 6; // 2-bit blue |
| 166 | return r + g + b; |
| 167 | } |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 168 | if (G.bytes_per_pixel == 2) { |
Peter Korsgaard | cd06e06 | 2011-10-17 05:31:52 +0200 | [diff] [blame] | 169 | r = (r & 0xf8) << 8; // 5-bit red |
| 170 | g = (g & 0xfc) << 3; // 6-bit green |
| 171 | b = b >> 3; // 5-bit blue |
| 172 | return r + g + b; |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 173 | } |
| 174 | // RGB 888 |
| 175 | return b + (g << 8) + (r << 16); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Draw pixel on framebuffer |
| 180 | */ |
| 181 | static void fb_write_pixel(unsigned char *addr, unsigned pixel) |
| 182 | { |
| 183 | switch (G.bytes_per_pixel) { |
Peter Korsgaard | cd06e06 | 2011-10-17 05:31:52 +0200 | [diff] [blame] | 184 | case 1: |
| 185 | *addr = pixel; |
| 186 | break; |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 187 | case 2: |
| 188 | *(uint16_t *)addr = pixel; |
| 189 | break; |
| 190 | case 4: |
| 191 | *(uint32_t *)addr = pixel; |
| 192 | break; |
| 193 | default: // 24 bits per pixel |
| 194 | addr[0] = pixel; |
| 195 | addr[1] = pixel >> 8; |
| 196 | addr[2] = pixel >> 16; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /** |
| 202 | * Draw hollow rectangle on framebuffer |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 203 | */ |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 204 | static void fb_drawrectangle(void) |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 205 | { |
| 206 | int cnt; |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 207 | unsigned thispix; |
| 208 | unsigned char *ptr1, *ptr2; |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 209 | unsigned char nred = G.nbar_colr/2; |
| 210 | unsigned char ngreen = G.nbar_colg/2; |
| 211 | unsigned char nblue = G.nbar_colb/2; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 212 | |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 213 | thispix = fb_pixel_value(nred, ngreen, nblue); |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 214 | |
| 215 | // horizontal lines |
Yin Kangkai | 3ac066c | 2012-02-23 15:56:36 +0800 | [diff] [blame] | 216 | ptr1 = G.addr + G.nbar_posy * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel; |
| 217 | ptr2 = G.addr + (G.nbar_posy + G.nbar_height - 1) * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel; |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 218 | cnt = G.nbar_width - 1; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 219 | do { |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 220 | fb_write_pixel(ptr1, thispix); |
| 221 | fb_write_pixel(ptr2, thispix); |
| 222 | ptr1 += G.bytes_per_pixel; |
| 223 | ptr2 += G.bytes_per_pixel; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 224 | } while (--cnt >= 0); |
| 225 | |
| 226 | // vertical lines |
Yin Kangkai | 3ac066c | 2012-02-23 15:56:36 +0800 | [diff] [blame] | 227 | ptr1 = G.addr + G.nbar_posy * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel; |
| 228 | ptr2 = G.addr + G.nbar_posy * G.scr_fix.line_length + (G.nbar_posx + G.nbar_width - 1) * G.bytes_per_pixel; |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 229 | cnt = G.nbar_height - 1; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 230 | do { |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 231 | fb_write_pixel(ptr1, thispix); |
| 232 | fb_write_pixel(ptr2, thispix); |
Yin Kangkai | 3ac066c | 2012-02-23 15:56:36 +0800 | [diff] [blame] | 233 | ptr1 += G.scr_fix.line_length; |
| 234 | ptr2 += G.scr_fix.line_length; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 235 | } while (--cnt >= 0); |
| 236 | } |
| 237 | |
| 238 | |
| 239 | /** |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 240 | * Draw filled rectangle on framebuffer |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 241 | * \param nx1pos,ny1pos upper left position |
| 242 | * \param nx2pos,ny2pos down right position |
Denis Vlasenko | 11b9f26 | 2008-03-26 20:06:24 +0000 | [diff] [blame] | 243 | * \param nred,ngreen,nblue rgb color |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 244 | */ |
| 245 | static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos, |
| 246 | unsigned char nred, unsigned char ngreen, unsigned char nblue) |
| 247 | { |
| 248 | int cnt1, cnt2, nypos; |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 249 | unsigned thispix; |
| 250 | unsigned char *ptr; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 251 | |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 252 | thispix = fb_pixel_value(nred, ngreen, nblue); |
Denis Vlasenko | 1f22898 | 2008-04-22 00:16:29 +0000 | [diff] [blame] | 253 | |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 254 | cnt1 = ny2pos - ny1pos; |
| 255 | nypos = ny1pos; |
| 256 | do { |
Yin Kangkai | 3ac066c | 2012-02-23 15:56:36 +0800 | [diff] [blame] | 257 | ptr = G.addr + nypos * G.scr_fix.line_length + nx1pos * G.bytes_per_pixel; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 258 | cnt2 = nx2pos - nx1pos; |
| 259 | do { |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 260 | fb_write_pixel(ptr, thispix); |
| 261 | ptr += G.bytes_per_pixel; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 262 | } while (--cnt2 >= 0); |
Denis Vlasenko | 1f22898 | 2008-04-22 00:16:29 +0000 | [diff] [blame] | 263 | |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 264 | nypos++; |
| 265 | } while (--cnt1 >= 0); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | /** |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 270 | * Draw a progress bar on framebuffer |
Denis Vlasenko | 11b9f26 | 2008-03-26 20:06:24 +0000 | [diff] [blame] | 271 | * \param percent percentage of loading |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 272 | */ |
Denis Vlasenko | 11b9f26 | 2008-03-26 20:06:24 +0000 | [diff] [blame] | 273 | static void fb_drawprogressbar(unsigned percent) |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 274 | { |
Peter Korsgaard | e4fa7b7 | 2011-10-17 04:35:23 +0200 | [diff] [blame] | 275 | int left_x, top_y, pos_x; |
| 276 | unsigned width, height; |
Denis Vlasenko | 11b9f26 | 2008-03-26 20:06:24 +0000 | [diff] [blame] | 277 | |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 278 | // outer box |
| 279 | left_x = G.nbar_posx; |
| 280 | top_y = G.nbar_posy; |
| 281 | width = G.nbar_width - 1; |
| 282 | height = G.nbar_height - 1; |
Peter Korsgaard | e4fa7b7 | 2011-10-17 04:35:23 +0200 | [diff] [blame] | 283 | if ((int)(height | width) < 0) |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 284 | return; |
| 285 | // NB: "width" of 1 actually makes rect with width of 2! |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 286 | fb_drawrectangle(); |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 287 | |
| 288 | // inner "empty" rectangle |
| 289 | left_x++; |
| 290 | top_y++; |
| 291 | width -= 2; |
| 292 | height -= 2; |
Peter Korsgaard | e4fa7b7 | 2011-10-17 04:35:23 +0200 | [diff] [blame] | 293 | if ((int)(height | width) < 0) |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 294 | return; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 295 | |
Peter Korsgaard | e4fa7b7 | 2011-10-17 04:35:23 +0200 | [diff] [blame] | 296 | pos_x = left_x; |
Denis Vlasenko | 11b9f26 | 2008-03-26 20:06:24 +0000 | [diff] [blame] | 297 | if (percent > 0) { |
Peter Korsgaard | e4fa7b7 | 2011-10-17 04:35:23 +0200 | [diff] [blame] | 298 | int y; |
| 299 | unsigned i; |
| 300 | |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 301 | // actual progress bar |
Peter Korsgaard | e4fa7b7 | 2011-10-17 04:35:23 +0200 | [diff] [blame] | 302 | pos_x += (unsigned)(width * percent) / 100; |
| 303 | |
| 304 | y = top_y; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 305 | i = height; |
| 306 | if (height == 0) |
| 307 | height++; // divide by 0 is bad |
| 308 | while (i >= 0) { |
| 309 | // draw one-line thick "rectangle" |
| 310 | // top line will have gray lvl 200, bottom one 100 |
Peter Korsgaard | e4fa7b7 | 2011-10-17 04:35:23 +0200 | [diff] [blame] | 311 | unsigned gray_level = 100 + i*100 / height; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 312 | fb_drawfullrectangle( |
Peter Korsgaard | e4fa7b7 | 2011-10-17 04:35:23 +0200 | [diff] [blame] | 313 | left_x, y, pos_x, y, |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 314 | gray_level, gray_level, gray_level); |
Peter Korsgaard | e4fa7b7 | 2011-10-17 04:35:23 +0200 | [diff] [blame] | 315 | y++; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 316 | i--; |
| 317 | } |
| 318 | } |
Peter Korsgaard | e4fa7b7 | 2011-10-17 04:35:23 +0200 | [diff] [blame] | 319 | |
| 320 | fb_drawfullrectangle( |
| 321 | pos_x, top_y, |
| 322 | left_x + width, top_y + height, |
| 323 | G.nbar_colr, G.nbar_colg, G.nbar_colb); |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | |
| 327 | /** |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 328 | * Draw image from PPM file |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 329 | */ |
| 330 | static void fb_drawimage(void) |
| 331 | { |
Bernhard Reutner-Fischer | 051fdb9 | 2009-02-16 12:36:50 +0000 | [diff] [blame] | 332 | FILE *theme_file; |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 333 | char *read_ptr; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 334 | unsigned char *pixline; |
| 335 | unsigned i, j, width, height, line_size; |
| 336 | |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 337 | if (LONE_DASH(G.image_filename)) { |
Vladimir Dronnikov | 6eaac02 | 2009-11-07 04:14:50 +0100 | [diff] [blame] | 338 | theme_file = stdin; |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 339 | } else { |
Vladimir Dronnikov | 6eaac02 | 2009-11-07 04:14:50 +0100 | [diff] [blame] | 340 | int fd = open_zipped(G.image_filename); |
| 341 | if (fd < 0) |
| 342 | bb_simple_perror_msg_and_die(G.image_filename); |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 343 | theme_file = xfdopen_for_read(fd); |
Vladimir Dronnikov | 6eaac02 | 2009-11-07 04:14:50 +0100 | [diff] [blame] | 344 | } |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 345 | |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 346 | /* Parse ppm header: |
| 347 | * - Magic: two characters "P6". |
Bernhard Reutner-Fischer | 8dcb33c | 2009-02-18 15:13:05 +0000 | [diff] [blame] | 348 | * - Whitespace (blanks, TABs, CRs, LFs). |
| 349 | * - A width, formatted as ASCII characters in decimal. |
| 350 | * - Whitespace. |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 351 | * - A height, ASCII decimal. |
Bernhard Reutner-Fischer | 8dcb33c | 2009-02-18 15:13:05 +0000 | [diff] [blame] | 352 | * - Whitespace. |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 353 | * - The maximum color value, ASCII decimal, in 0..65535 |
Bernhard Reutner-Fischer | 8dcb33c | 2009-02-18 15:13:05 +0000 | [diff] [blame] | 354 | * - Newline or other single whitespace character. |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 355 | * (we support newline only) |
Bernhard Reutner-Fischer | 8dcb33c | 2009-02-18 15:13:05 +0000 | [diff] [blame] | 356 | * - A raster of Width * Height pixels in triplets of rgb |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 357 | * 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] | 358 | */ |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 359 | #define concat_buf bb_common_bufsiz1 |
| 360 | read_ptr = concat_buf; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 361 | while (1) { |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 362 | int w, h, max_color_val; |
| 363 | int rem = concat_buf + sizeof(concat_buf) - read_ptr; |
| 364 | if (rem < 2 |
| 365 | || fgets(read_ptr, rem, theme_file) == NULL |
| 366 | ) { |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 367 | bb_error_msg_and_die("bad PPM file '%s'", G.image_filename); |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 368 | } |
| 369 | read_ptr = strchrnul(read_ptr, '#'); |
| 370 | *read_ptr = '\0'; /* ignore #comments */ |
| 371 | if (sscanf(concat_buf, "P6 %u %u %u", &w, &h, &max_color_val) == 3 |
| 372 | && max_color_val <= 255 |
| 373 | ) { |
| 374 | width = w; /* w is on stack, width may be in register */ |
| 375 | height = h; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 376 | break; |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 377 | } |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | line_size = width*3; |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 381 | pixline = xmalloc(line_size); |
| 382 | |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 383 | if (width > G.scr_var.xres) |
| 384 | width = G.scr_var.xres; |
| 385 | if (height > G.scr_var.yres) |
| 386 | height = G.scr_var.yres; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 387 | for (j = 0; j < height; j++) { |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 388 | unsigned char *pixel; |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 389 | unsigned char *src; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 390 | |
Bernhard Reutner-Fischer | 051fdb9 | 2009-02-16 12:36:50 +0000 | [diff] [blame] | 391 | if (fread(pixline, 1, line_size, theme_file) != line_size) |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 392 | bb_error_msg_and_die("bad PPM file '%s'", G.image_filename); |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 393 | pixel = pixline; |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 394 | src = G.addr + j * G.scr_fix.line_length; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 395 | for (i = 0; i < width; i++) { |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 396 | unsigned thispix = fb_pixel_value(pixel[0], pixel[1], pixel[2]); |
| 397 | fb_write_pixel(src, thispix); |
| 398 | src += G.bytes_per_pixel; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 399 | pixel += 3; |
| 400 | } |
| 401 | } |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 402 | free(pixline); |
Bernhard Reutner-Fischer | 051fdb9 | 2009-02-16 12:36:50 +0000 | [diff] [blame] | 403 | fclose(theme_file); |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | |
| 407 | /** |
Nuno Lucas | 0e89fd9 | 2011-03-28 15:30:59 +0200 | [diff] [blame] | 408 | * Parse configuration file |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 409 | * \param *cfg_filename name of the configuration file |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 410 | */ |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 411 | static void init(const char *cfg_filename) |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 412 | { |
Denys Vlasenko | 10544a8 | 2010-02-09 00:26:10 +0100 | [diff] [blame] | 413 | static const char param_names[] ALIGN1 = |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 414 | "BAR_WIDTH\0" "BAR_HEIGHT\0" |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 415 | "BAR_LEFT\0" "BAR_TOP\0" |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 416 | "BAR_R\0" "BAR_G\0" "BAR_B\0" |
| 417 | #if DEBUG |
| 418 | "DEBUG\0" |
| 419 | #endif |
| 420 | ; |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 421 | char *token[2]; |
| 422 | parser_t *parser = config_open2(cfg_filename, xfopen_stdin); |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 423 | while (config_read(parser, token, 2, 2, "#=", |
Denys Vlasenko | 814da22 | 2010-03-14 23:59:54 +0100 | [diff] [blame] | 424 | (PARSE_NORMAL | PARSE_MIN_DIE) & ~(PARSE_TRIM | PARSE_COLLAPSE))) { |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 425 | unsigned val = xatoi_positive(token[1]); |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 426 | int i = index_in_strings(param_names, token[0]); |
| 427 | if (i < 0) |
Bernhard Reutner-Fischer | 4efcec9 | 2009-02-14 12:36:16 +0000 | [diff] [blame] | 428 | bb_error_msg_and_die("syntax error: %s", token[0]); |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 429 | if (i >= 0 && i < 7) |
| 430 | G.ns[i] = val; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 431 | #if DEBUG |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 432 | if (i == 7) { |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 433 | G.bdebug_messages = val; |
| 434 | if (G.bdebug_messages) |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 435 | G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log"); |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 436 | } |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 437 | #endif |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 438 | } |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 439 | config_close(parser); |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | |
| 443 | int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 444 | int fbsplash_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 445 | { |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 446 | const char *fb_device, *cfg_filename, *fifo_filename; |
Denis Vlasenko | 11b9f26 | 2008-03-26 20:06:24 +0000 | [diff] [blame] | 447 | FILE *fp = fp; // for compiler |
Denis Vlasenko | 7f8f0fa | 2008-04-04 20:38:49 +0000 | [diff] [blame] | 448 | char *num_buf; |
| 449 | unsigned num; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 450 | bool bCursorOff; |
| 451 | |
| 452 | INIT_G(); |
| 453 | |
| 454 | // parse command line options |
| 455 | fb_device = "/dev/fb0"; |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 456 | cfg_filename = NULL; |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 457 | fifo_filename = NULL; |
| 458 | bCursorOff = 1 & getopt32(argv, "cs:d:i:f:", |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 459 | &G.image_filename, &fb_device, &cfg_filename, &fifo_filename); |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 460 | |
| 461 | // parse configuration file |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 462 | if (cfg_filename) |
| 463 | init(cfg_filename); |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 464 | |
| 465 | // We must have -s IMG |
| 466 | if (!G.image_filename) |
| 467 | bb_show_usage(); |
| 468 | |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 469 | fb_open(fb_device); |
| 470 | |
| 471 | if (fifo_filename && bCursorOff) { |
| 472 | // hide cursor (BEFORE any fb ops) |
Denys Vlasenko | d9a3e89 | 2010-05-16 23:42:13 +0200 | [diff] [blame] | 473 | full_write(STDOUT_FILENO, "\033[?25l", 6); |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | fb_drawimage(); |
| 477 | |
Denis Vlasenko | a38ba59 | 2008-03-28 11:17:35 +0000 | [diff] [blame] | 478 | if (!fifo_filename) |
| 479 | return EXIT_SUCCESS; |
| 480 | |
| 481 | fp = xfopen_stdin(fifo_filename); |
Denis Vlasenko | 7f8f0fa | 2008-04-04 20:38:49 +0000 | [diff] [blame] | 482 | if (fp != stdin) { |
Denis Vlasenko | 72b3442 | 2008-03-27 13:14:29 +0000 | [diff] [blame] | 483 | // For named pipes, we want to support this: |
| 484 | // mkfifo cmd_pipe |
| 485 | // fbsplash -f cmd_pipe .... & |
| 486 | // ... |
| 487 | // echo 33 >cmd_pipe |
| 488 | // ... |
| 489 | // echo 66 >cmd_pipe |
Denis Vlasenko | 7f8f0fa | 2008-04-04 20:38:49 +0000 | [diff] [blame] | 490 | // This means that we don't want fbsplash to get EOF |
| 491 | // when last writer closes input end. |
| 492 | // The simplest way is to open fifo for writing too |
| 493 | // and become an additional writer :) |
| 494 | open(fifo_filename, O_WRONLY); // errors are ignored |
| 495 | } |
| 496 | |
| 497 | fb_drawprogressbar(0); |
| 498 | // Block on read, waiting for some input. |
| 499 | // Use of <stdio.h> style I/O allows to correctly |
| 500 | // handle a case when we have many buffered lines |
| 501 | // already in the pipe |
| 502 | while ((num_buf = xmalloc_fgetline(fp)) != NULL) { |
| 503 | if (strncmp(num_buf, "exit", 4) == 0) { |
| 504 | DEBUG_MESSAGE("exit"); |
| 505 | break; |
| 506 | } |
| 507 | num = atoi(num_buf); |
| 508 | if (isdigit(num_buf[0]) && (num <= 100)) { |
| 509 | #if DEBUG |
Denys Vlasenko | 7bb346f | 2009-10-06 22:09:50 +0200 | [diff] [blame] | 510 | DEBUG_MESSAGE(itoa(num)); |
Denis Vlasenko | 7f8f0fa | 2008-04-04 20:38:49 +0000 | [diff] [blame] | 511 | #endif |
| 512 | fb_drawprogressbar(num); |
| 513 | } |
| 514 | free(num_buf); |
| 515 | } |
| 516 | |
| 517 | if (bCursorOff) // restore cursor |
Denys Vlasenko | d9a3e89 | 2010-05-16 23:42:13 +0200 | [diff] [blame] | 518 | full_write(STDOUT_FILENO, "\033[?25h", 6); |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 519 | |
Denis Vlasenko | c6dbb85 | 2008-03-26 14:57:49 +0000 | [diff] [blame] | 520 | return EXIT_SUCCESS; |
| 521 | } |