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