| 1 | /* $Id$ |
|---|
| 2 | * $URL$ |
|---|
| 3 | * |
|---|
| 4 | * generic driver helper for graphic displays |
|---|
| 5 | * |
|---|
| 6 | * Copyright (C) 1999, 2000 Michael Reinelt <reinelt@eunet.at> |
|---|
| 7 | * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net> |
|---|
| 8 | * |
|---|
| 9 | * This file is part of LCD4Linux. |
|---|
| 10 | * |
|---|
| 11 | * LCD4Linux is free software; you can redistribute it and/or modify |
|---|
| 12 | * it under the terms of the GNU General Public License as published by |
|---|
| 13 | * the Free Software Foundation; either version 2, or (at your option) |
|---|
| 14 | * any later version. |
|---|
| 15 | * |
|---|
| 16 | * LCD4Linux is distributed in the hope that it will be useful, |
|---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 19 | * GNU General Public License for more details. |
|---|
| 20 | * |
|---|
| 21 | * You should have received a copy of the GNU General Public License |
|---|
| 22 | * along with this program; if not, write to the Free Software |
|---|
| 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 24 | * |
|---|
| 25 | */ |
|---|
| 26 | |
|---|
| 27 | /* |
|---|
| 28 | * |
|---|
| 29 | * exported functions: |
|---|
| 30 | * |
|---|
| 31 | * int drv_generic_graphic_init (char *section, char *driver); |
|---|
| 32 | * initializes the generic graphic driver |
|---|
| 33 | * |
|---|
| 34 | * int drv_generic_graphic_draw (WIDGET *W); |
|---|
| 35 | * renders Text widget into framebuffer |
|---|
| 36 | * calls drv_generic_graphic_real_blit() |
|---|
| 37 | * |
|---|
| 38 | * int drv_generic_graphic_icon_draw (WIDGET *W); |
|---|
| 39 | * renders Icon widget into framebuffer |
|---|
| 40 | * calls drv_generic_graphic_real_blit() |
|---|
| 41 | * |
|---|
| 42 | * int drv_generic_graphic_bar_draw (WIDGET *W); |
|---|
| 43 | * renders Bar widget into framebuffer |
|---|
| 44 | * calls drv_generic_graphic_real_blit() |
|---|
| 45 | * |
|---|
| 46 | * int drv_generic_graphic_quit (void); |
|---|
| 47 | * closes generic graphic driver |
|---|
| 48 | * |
|---|
| 49 | */ |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | #include "config.h" |
|---|
| 53 | |
|---|
| 54 | #include <stdlib.h> |
|---|
| 55 | #include <stdio.h> |
|---|
| 56 | #include <string.h> |
|---|
| 57 | #include <errno.h> |
|---|
| 58 | #include <unistd.h> |
|---|
| 59 | #include <termios.h> |
|---|
| 60 | #include <fcntl.h> |
|---|
| 61 | |
|---|
| 62 | #include "debug.h" |
|---|
| 63 | #include "cfg.h" |
|---|
| 64 | #include "plugin.h" |
|---|
| 65 | #include "layout.h" |
|---|
| 66 | #include "widget.h" |
|---|
| 67 | #include "property.h" |
|---|
| 68 | #include "widget_text.h" |
|---|
| 69 | #include "widget_icon.h" |
|---|
| 70 | #include "widget_bar.h" |
|---|
| 71 | #include "widget_image.h" |
|---|
| 72 | #include "rgb.h" |
|---|
| 73 | #include "drv.h" |
|---|
| 74 | #include "drv_generic.h" |
|---|
| 75 | #include "drv_generic_graphic.h" |
|---|
| 76 | #include "font_6x8.h" |
|---|
| 77 | #include "font_6x8_bold.h" |
|---|
| 78 | |
|---|
| 79 | #ifdef WITH_DMALLOC |
|---|
| 80 | #include <dmalloc.h> |
|---|
| 81 | #endif |
|---|
| 82 | |
|---|
| 83 | /* pixel colors */ |
|---|
| 84 | RGBA FG_COL = {.R = 0x00,.G = 0x00,.B = 0x00,.A = 0xff }; |
|---|
| 85 | RGBA BG_COL = {.R = 0xff,.G = 0xff,.B = 0xff,.A = 0xff }; |
|---|
| 86 | RGBA BL_COL = {.R = 0xff,.G = 0xff,.B = 0xff,.A = 0x00 }; |
|---|
| 87 | RGBA NO_COL = {.R = 0x00,.G = 0x00,.B = 0x00,.A = 0x00 }; |
|---|
| 88 | |
|---|
| 89 | static char *Section = NULL; |
|---|
| 90 | static char *Driver = NULL; |
|---|
| 91 | |
|---|
| 92 | /* framebuffer */ |
|---|
| 93 | static RGBA *drv_generic_graphic_FB[LAYERS] = { NULL, }; |
|---|
| 94 | |
|---|
| 95 | /* inverted colors */ |
|---|
| 96 | static int INVERTED = 0; |
|---|
| 97 | |
|---|
| 98 | /* must be implemented by the real driver */ |
|---|
| 99 | void (*drv_generic_graphic_real_blit) () = NULL; |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | /****************************************/ |
|---|
| 103 | /*** generic Framebuffer stuff ***/ |
|---|
| 104 | /****************************************/ |
|---|
| 105 | |
|---|
| 106 | static void drv_generic_graphic_resizeFB(int rows, int cols) |
|---|
| 107 | { |
|---|
| 108 | RGBA *newFB; |
|---|
| 109 | int i, l, row, col; |
|---|
| 110 | |
|---|
| 111 | /* Layout FB is large enough */ |
|---|
| 112 | if (rows <= LROWS && cols <= LCOLS) |
|---|
| 113 | return; |
|---|
| 114 | |
|---|
| 115 | /* get maximum values */ |
|---|
| 116 | if (rows < LROWS) |
|---|
| 117 | rows = LROWS; |
|---|
| 118 | if (cols < LCOLS) |
|---|
| 119 | cols = LCOLS; |
|---|
| 120 | |
|---|
| 121 | for (l = 0; l < LAYERS; l++) { |
|---|
| 122 | |
|---|
| 123 | /* allocate and initialize new Layout FB */ |
|---|
| 124 | newFB = malloc(cols * rows * sizeof(*newFB)); |
|---|
| 125 | for (i = 0; i < rows * cols; i++) |
|---|
| 126 | newFB[i] = (l == 0) ? BG_COL : NO_COL; |
|---|
| 127 | |
|---|
| 128 | /* transfer contents */ |
|---|
| 129 | if (drv_generic_graphic_FB[l] != NULL) { |
|---|
| 130 | for (row = 0; row < LROWS; row++) { |
|---|
| 131 | for (col = 0; col < LCOLS; col++) { |
|---|
| 132 | newFB[row * cols + col] = drv_generic_graphic_FB[l][row * LCOLS + col]; |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | free(drv_generic_graphic_FB[l]); |
|---|
| 136 | } |
|---|
| 137 | drv_generic_graphic_FB[l] = newFB; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | LCOLS = cols; |
|---|
| 141 | LROWS = rows; |
|---|
| 142 | |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | static void drv_generic_graphic_window(int pos, int size, int max, int *wpos, int *wsize) |
|---|
| 146 | { |
|---|
| 147 | int p1 = pos; |
|---|
| 148 | int p2 = pos + size; |
|---|
| 149 | |
|---|
| 150 | *wpos = 0; |
|---|
| 151 | *wsize = 0; |
|---|
| 152 | |
|---|
| 153 | if (p1 > max || p2 < 0 || size < 1) |
|---|
| 154 | return; |
|---|
| 155 | |
|---|
| 156 | if (p1 < 0) |
|---|
| 157 | p1 = 0; |
|---|
| 158 | |
|---|
| 159 | if (p2 > max) |
|---|
| 160 | p2 = max; |
|---|
| 161 | |
|---|
| 162 | *wpos = p1; |
|---|
| 163 | *wsize = p2 - p1; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | static void drv_generic_graphic_blit(const int row, const int col, const int height, const int width) |
|---|
| 167 | { |
|---|
| 168 | if (drv_generic_graphic_real_blit) { |
|---|
| 169 | int r, c, h, w; |
|---|
| 170 | drv_generic_graphic_window(row, height, DROWS, &r, &h); |
|---|
| 171 | drv_generic_graphic_window(col, width, DCOLS, &c, &w); |
|---|
| 172 | if (h > 0 && w > 0) { |
|---|
| 173 | drv_generic_graphic_real_blit(r, c, h, w); |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | static RGBA drv_generic_graphic_blend(const int row, const int col) |
|---|
| 179 | { |
|---|
| 180 | int l; |
|---|
| 181 | int opaqueBottomLayer = LAYERS - 1; |
|---|
| 182 | RGBA p; |
|---|
| 183 | RGBA ret; |
|---|
| 184 | |
|---|
| 185 | ret.R = BL_COL.R; |
|---|
| 186 | ret.G = BL_COL.G; |
|---|
| 187 | ret.B = BL_COL.B; |
|---|
| 188 | ret.A = 0xff; |
|---|
| 189 | /* Layer 0 is topmost: Find lowest opaque layer */ |
|---|
| 190 | for (l = 0; l < LAYERS; l++) { |
|---|
| 191 | if (drv_generic_graphic_FB[l][row * LCOLS + col].A >= 255) { |
|---|
| 192 | /* Layers below are fully covered */ |
|---|
| 193 | opaqueBottomLayer = l; |
|---|
| 194 | break; |
|---|
| 195 | } |
|---|
| 196 | } |
|---|
| 197 | for (l = opaqueBottomLayer; l >= 0; l--) { |
|---|
| 198 | p = drv_generic_graphic_FB[l][row * LCOLS + col]; |
|---|
| 199 | if (p.A > 0) { |
|---|
| 200 | ret.R = (p.R * p.A + ret.R * (255 - p.A)) / 255; |
|---|
| 201 | ret.G = (p.G * p.A + ret.G * (255 - p.A)) / 255; |
|---|
| 202 | ret.B = (p.B * p.A + ret.B * (255 - p.A)) / 255; |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | if (INVERTED) { |
|---|
| 206 | ret.R = 255 - ret.R; |
|---|
| 207 | ret.G = 255 - ret.G; |
|---|
| 208 | ret.B = 255 - ret.B; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | return ret; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | /****************************************/ |
|---|
| 216 | /*** generic text handling ***/ |
|---|
| 217 | /****************************************/ |
|---|
| 218 | |
|---|
| 219 | static void drv_generic_graphic_render(const int layer, const int row, const int col, const RGBA fg, const RGBA bg, |
|---|
| 220 | const char *style, const char *txt) |
|---|
| 221 | { |
|---|
| 222 | int c, r, x, y, len; |
|---|
| 223 | |
|---|
| 224 | /* sanity checks */ |
|---|
| 225 | if (layer < 0 || layer >= LAYERS) { |
|---|
| 226 | error("%s: layer %d out of bounds (0..%d)", Driver, layer, LAYERS - 1); |
|---|
| 227 | return; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | len = strlen(txt); |
|---|
| 231 | |
|---|
| 232 | /* maybe grow layout framebuffer */ |
|---|
| 233 | drv_generic_graphic_resizeFB(row + YRES, col + XRES * len); |
|---|
| 234 | |
|---|
| 235 | r = row; |
|---|
| 236 | c = col; |
|---|
| 237 | |
|---|
| 238 | /* render text into layout FB */ |
|---|
| 239 | while (*txt != '\0') { |
|---|
| 240 | unsigned char *chr; |
|---|
| 241 | |
|---|
| 242 | if (strcasestr(style, "bold") != NULL) { |
|---|
| 243 | chr = Font_6x8_bold[(int) *(unsigned char *) txt]; |
|---|
| 244 | } else { |
|---|
| 245 | chr = Font_6x8[(int) *(unsigned char *) txt]; |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | for (y = 0; y < YRES; y++) { |
|---|
| 249 | int mask = 1 << XRES; |
|---|
| 250 | for (x = 0; x < XRES; x++) { |
|---|
| 251 | mask >>= 1; |
|---|
| 252 | if (chr[y] & mask) |
|---|
| 253 | drv_generic_graphic_FB[layer][(r + y) * LCOLS + c + x] = fg; |
|---|
| 254 | else |
|---|
| 255 | drv_generic_graphic_FB[layer][(r + y) * LCOLS + c + x] = bg; |
|---|
| 256 | } |
|---|
| 257 | } |
|---|
| 258 | c += XRES; |
|---|
| 259 | txt++; |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | /* flush area */ |
|---|
| 263 | drv_generic_graphic_blit(row, col, YRES, XRES * len); |
|---|
| 264 | |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | /* say hello to the user */ |
|---|
| 269 | int drv_generic_graphic_greet(const char *msg1, const char *msg2) |
|---|
| 270 | { |
|---|
| 271 | char *line1[] = { "* LCD4Linux " VERSION " *", |
|---|
| 272 | "LCD4Linux " VERSION, |
|---|
| 273 | "* LCD4Linux *", |
|---|
| 274 | "LCD4Linux", |
|---|
| 275 | "L4Linux", |
|---|
| 276 | NULL |
|---|
| 277 | }; |
|---|
| 278 | |
|---|
| 279 | char *line2[] = { "http://lcd4linux.bulix.org", |
|---|
| 280 | "lcd4linux.bulix.org", |
|---|
| 281 | NULL |
|---|
| 282 | }; |
|---|
| 283 | |
|---|
| 284 | int i; |
|---|
| 285 | int flag = 0; |
|---|
| 286 | |
|---|
| 287 | unsigned int cols = DCOLS / XRES; |
|---|
| 288 | unsigned int rows = DROWS / YRES; |
|---|
| 289 | |
|---|
| 290 | for (i = 0; line1[i]; i++) { |
|---|
| 291 | if (strlen(line1[i]) <= cols) { |
|---|
| 292 | drv_generic_graphic_render(0, YRES * 0, XRES * ((cols - strlen(line1[i])) / 2), FG_COL, BG_COL, "norm", |
|---|
| 293 | line1[i]); |
|---|
| 294 | flag = 1; |
|---|
| 295 | break; |
|---|
| 296 | } |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | if (rows >= 2) { |
|---|
| 300 | for (i = 0; line2[i]; i++) { |
|---|
| 301 | if (strlen(line2[i]) <= cols) { |
|---|
| 302 | drv_generic_graphic_render(0, YRES * 1, XRES * ((cols - strlen(line2[i])) / 2), FG_COL, BG_COL, "norm", |
|---|
| 303 | line2[i]); |
|---|
| 304 | flag = 1; |
|---|
| 305 | break; |
|---|
| 306 | } |
|---|
| 307 | } |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | if (msg1 && rows >= 3) { |
|---|
| 311 | unsigned int len = strlen(msg1); |
|---|
| 312 | if (len <= cols) { |
|---|
| 313 | drv_generic_graphic_render(0, YRES * 2, XRES * ((cols - len) / 2), FG_COL, BG_COL, "norm", msg1); |
|---|
| 314 | flag = 1; |
|---|
| 315 | } |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | if (msg2 && rows >= 4) { |
|---|
| 319 | unsigned int len = strlen(msg2); |
|---|
| 320 | if (len <= cols) { |
|---|
| 321 | drv_generic_graphic_render(0, YRES * 3, XRES * ((cols - len) / 2), FG_COL, BG_COL, "norm", msg2); |
|---|
| 322 | flag = 1; |
|---|
| 323 | } |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | return flag; |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | int drv_generic_graphic_draw(WIDGET * W) |
|---|
| 331 | { |
|---|
| 332 | WIDGET_TEXT *Text = W->data; |
|---|
| 333 | RGBA fg, bg; |
|---|
| 334 | |
|---|
| 335 | fg = W->fg_valid ? W->fg_color : FG_COL; |
|---|
| 336 | bg = W->bg_valid ? W->bg_color : BG_COL; |
|---|
| 337 | |
|---|
| 338 | drv_generic_graphic_render(W->layer, YRES * W->row, XRES * W->col, fg, bg, P2S(&Text->style), Text->buffer); |
|---|
| 339 | |
|---|
| 340 | return 0; |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | /****************************************/ |
|---|
| 345 | /*** generic icon handling ***/ |
|---|
| 346 | /****************************************/ |
|---|
| 347 | |
|---|
| 348 | int drv_generic_graphic_icon_draw(WIDGET * W) |
|---|
| 349 | { |
|---|
| 350 | WIDGET_ICON *Icon = W->data; |
|---|
| 351 | RGBA fg, bg; |
|---|
| 352 | unsigned char *bitmap = Icon->bitmap + YRES * Icon->curmap; |
|---|
| 353 | int layer, row, col; |
|---|
| 354 | int x, y; |
|---|
| 355 | int visible; |
|---|
| 356 | |
|---|
| 357 | layer = W->layer; |
|---|
| 358 | row = YRES * W->row; |
|---|
| 359 | col = XRES * W->col; |
|---|
| 360 | |
|---|
| 361 | fg = W->fg_valid ? W->fg_color : FG_COL; |
|---|
| 362 | bg = W->bg_valid ? W->bg_color : BG_COL; |
|---|
| 363 | |
|---|
| 364 | /* sanity check */ |
|---|
| 365 | if (layer < 0 || layer >= LAYERS) { |
|---|
| 366 | error("%s: layer %d out of bounds (0..%d)", Driver, layer, LAYERS - 1); |
|---|
| 367 | return -1; |
|---|
| 368 | } |
|---|
| 369 | |
|---|
| 370 | /* maybe grow layout framebuffer */ |
|---|
| 371 | drv_generic_graphic_resizeFB(row + YRES, col + XRES); |
|---|
| 372 | |
|---|
| 373 | /* Icon visible? */ |
|---|
| 374 | visible = P2N(&Icon->visible) > 0; |
|---|
| 375 | |
|---|
| 376 | /* render icon */ |
|---|
| 377 | for (y = 0; y < YRES; y++) { |
|---|
| 378 | int mask = 1 << XRES; |
|---|
| 379 | for (x = 0; x < XRES; x++) { |
|---|
| 380 | int i = (row + y) * LCOLS + col + x; |
|---|
| 381 | mask >>= 1; |
|---|
| 382 | if (visible) { |
|---|
| 383 | if (bitmap[y] & mask) |
|---|
| 384 | drv_generic_graphic_FB[layer][i] = fg; |
|---|
| 385 | else |
|---|
| 386 | drv_generic_graphic_FB[layer][i] = bg; |
|---|
| 387 | } else { |
|---|
| 388 | drv_generic_graphic_FB[layer][i] = BG_COL; |
|---|
| 389 | } |
|---|
| 390 | } |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | /* flush area */ |
|---|
| 394 | drv_generic_graphic_blit(row, col, YRES, XRES); |
|---|
| 395 | |
|---|
| 396 | return 0; |
|---|
| 397 | |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | |
|---|
| 401 | /****************************************/ |
|---|
| 402 | /*** generic bar handling ***/ |
|---|
| 403 | /****************************************/ |
|---|
| 404 | |
|---|
| 405 | int drv_generic_graphic_bar_draw(WIDGET * W) |
|---|
| 406 | { |
|---|
| 407 | WIDGET_BAR *Bar = W->data; |
|---|
| 408 | RGBA fg, bg, bar[2]; |
|---|
| 409 | int layer, row, col, len, res, rev, max, val1, val2; |
|---|
| 410 | int x, y; |
|---|
| 411 | DIRECTION dir; |
|---|
| 412 | STYLE style; |
|---|
| 413 | |
|---|
| 414 | layer = W->layer; |
|---|
| 415 | row = YRES * W->row; |
|---|
| 416 | col = XRES * W->col; |
|---|
| 417 | dir = Bar->direction; |
|---|
| 418 | style = Bar->style; |
|---|
| 419 | len = Bar->length; |
|---|
| 420 | |
|---|
| 421 | fg = W->fg_valid ? W->fg_color : FG_COL; |
|---|
| 422 | bg = W->bg_valid ? W->bg_color : BG_COL; |
|---|
| 423 | |
|---|
| 424 | bar[0] = Bar->color_valid[0] ? Bar->color[0] : fg; |
|---|
| 425 | bar[1] = Bar->color_valid[1] ? Bar->color[1] : fg; |
|---|
| 426 | |
|---|
| 427 | /* sanity check */ |
|---|
| 428 | if (layer < 0 || layer >= LAYERS) { |
|---|
| 429 | error("%s: layer %d out of bounds (0..%d)", Driver, layer, LAYERS - 1); |
|---|
| 430 | return -1; |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | /* maybe grow layout framebuffer */ |
|---|
| 434 | if (dir & (DIR_EAST | DIR_WEST)) { |
|---|
| 435 | drv_generic_graphic_resizeFB(row + YRES, col + XRES * len); |
|---|
| 436 | } else { |
|---|
| 437 | drv_generic_graphic_resizeFB(row + YRES * len, col + XRES); |
|---|
| 438 | } |
|---|
| 439 | |
|---|
| 440 | res = dir & (DIR_EAST | DIR_WEST) ? XRES : YRES; |
|---|
| 441 | max = len * res; |
|---|
| 442 | val1 = Bar->val1 * (double) (max); |
|---|
| 443 | val2 = Bar->val2 * (double) (max); |
|---|
| 444 | |
|---|
| 445 | if (val1 < 1) |
|---|
| 446 | val1 = 1; |
|---|
| 447 | else if (val1 > max) |
|---|
| 448 | val1 = max; |
|---|
| 449 | |
|---|
| 450 | if (val2 < 1) |
|---|
| 451 | val2 = 1; |
|---|
| 452 | else if (val2 > max) |
|---|
| 453 | val2 = max; |
|---|
| 454 | |
|---|
| 455 | rev = 0; |
|---|
| 456 | |
|---|
| 457 | switch (dir) { |
|---|
| 458 | case DIR_WEST: |
|---|
| 459 | val1 = max - val1; |
|---|
| 460 | val2 = max - val2; |
|---|
| 461 | rev = 1; |
|---|
| 462 | |
|---|
| 463 | case DIR_EAST: |
|---|
| 464 | for (y = 0; y < YRES; y++) { |
|---|
| 465 | int val = y < YRES / 2 ? val1 : val2; |
|---|
| 466 | RGBA bcol = y < YRES / 2 ? bar[0] : bar[1]; |
|---|
| 467 | |
|---|
| 468 | for (x = 0; x < max; x++) { |
|---|
| 469 | if (x < val) |
|---|
| 470 | drv_generic_graphic_FB[layer][(row + y) * LCOLS + col + x] = rev ? bg : bcol; |
|---|
| 471 | else |
|---|
| 472 | drv_generic_graphic_FB[layer][(row + y) * LCOLS + col + x] = rev ? bcol : bg; |
|---|
| 473 | |
|---|
| 474 | if (style) { |
|---|
| 475 | drv_generic_graphic_FB[layer][(row) * LCOLS + col + x] = fg; |
|---|
| 476 | drv_generic_graphic_FB[layer][(row + YRES - 1) * LCOLS + col + x] = fg; |
|---|
| 477 | } |
|---|
| 478 | } |
|---|
| 479 | if (style) { |
|---|
| 480 | drv_generic_graphic_FB[layer][(row + y) * LCOLS + col] = fg; |
|---|
| 481 | drv_generic_graphic_FB[layer][(row + y) * LCOLS + col + max - 1] = fg; |
|---|
| 482 | } |
|---|
| 483 | } |
|---|
| 484 | break; |
|---|
| 485 | |
|---|
| 486 | case DIR_NORTH: |
|---|
| 487 | val1 = max - val1; |
|---|
| 488 | val2 = max - val2; |
|---|
| 489 | rev = 1; |
|---|
| 490 | |
|---|
| 491 | case DIR_SOUTH: |
|---|
| 492 | for (x = 0; x < XRES; x++) { |
|---|
| 493 | int val = x < XRES / 2 ? val1 : val2; |
|---|
| 494 | for (y = 0; y < max; y++) { |
|---|
| 495 | if (y < val) |
|---|
| 496 | drv_generic_graphic_FB[layer][(row + y) * LCOLS + col + x] = rev ? bg : fg; |
|---|
| 497 | else |
|---|
| 498 | drv_generic_graphic_FB[layer][(row + y) * LCOLS + col + x] = rev ? fg : bg; |
|---|
| 499 | } |
|---|
| 500 | } |
|---|
| 501 | break; |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | /* flush area */ |
|---|
| 505 | if (dir & (DIR_EAST | DIR_WEST)) { |
|---|
| 506 | drv_generic_graphic_blit(row, col, YRES, XRES * len); |
|---|
| 507 | } else { |
|---|
| 508 | drv_generic_graphic_blit(row, col, YRES * len, XRES); |
|---|
| 509 | } |
|---|
| 510 | |
|---|
| 511 | return 0; |
|---|
| 512 | } |
|---|
| 513 | |
|---|
| 514 | |
|---|
| 515 | /****************************************/ |
|---|
| 516 | /*** generic image handling ***/ |
|---|
| 517 | /****************************************/ |
|---|
| 518 | |
|---|
| 519 | int drv_generic_graphic_image_draw(WIDGET * W) |
|---|
| 520 | { |
|---|
| 521 | WIDGET_IMAGE *Image = W->data; |
|---|
| 522 | int layer, row, col, width, height; |
|---|
| 523 | int x, y; |
|---|
| 524 | int visible; |
|---|
| 525 | |
|---|
| 526 | layer = W->layer; |
|---|
| 527 | row = W->row; |
|---|
| 528 | col = W->col; |
|---|
| 529 | width = Image->width; |
|---|
| 530 | height = Image->height; |
|---|
| 531 | |
|---|
| 532 | /* sanity check */ |
|---|
| 533 | if (layer < 0 || layer >= LAYERS) { |
|---|
| 534 | error("%s: layer %d out of bounds (0..%d)", Driver, layer, LAYERS - 1); |
|---|
| 535 | return -1; |
|---|
| 536 | } |
|---|
| 537 | |
|---|
| 538 | /* if no size or no image at all, do nothing */ |
|---|
| 539 | if (width <= 0 || height <= 0 || Image->bitmap == NULL) { |
|---|
| 540 | return 0; |
|---|
| 541 | } |
|---|
| 542 | |
|---|
| 543 | /* maybe grow layout framebuffer */ |
|---|
| 544 | drv_generic_graphic_resizeFB(row + height, col + width); |
|---|
| 545 | |
|---|
| 546 | /* render image */ |
|---|
| 547 | visible = P2N(&Image->visible); |
|---|
| 548 | for (y = 0; y < height; y++) { |
|---|
| 549 | for (x = 0; x < width; x++) { |
|---|
| 550 | int i = (row + y) * LCOLS + col + x; |
|---|
| 551 | if (visible) { |
|---|
| 552 | drv_generic_graphic_FB[layer][i] = Image->bitmap[y * width + x]; |
|---|
| 553 | } else { |
|---|
| 554 | drv_generic_graphic_FB[layer][i] = BG_COL; |
|---|
| 555 | } |
|---|
| 556 | } |
|---|
| 557 | } |
|---|
| 558 | |
|---|
| 559 | /* flush area */ |
|---|
| 560 | drv_generic_graphic_blit(row, col, height, width); |
|---|
| 561 | |
|---|
| 562 | return 0; |
|---|
| 563 | |
|---|
| 564 | } |
|---|
| 565 | |
|---|
| 566 | |
|---|
| 567 | /****************************************/ |
|---|
| 568 | /*** generic init/quit ***/ |
|---|
| 569 | /****************************************/ |
|---|
| 570 | |
|---|
| 571 | int drv_generic_graphic_init(const char *section, const char *driver) |
|---|
| 572 | { |
|---|
| 573 | int l; |
|---|
| 574 | char *color; |
|---|
| 575 | WIDGET_CLASS wc; |
|---|
| 576 | |
|---|
| 577 | Section = (char *) section; |
|---|
| 578 | Driver = (char *) driver; |
|---|
| 579 | |
|---|
| 580 | /* init layout framebuffer */ |
|---|
| 581 | LROWS = 0; |
|---|
| 582 | LCOLS = 0; |
|---|
| 583 | |
|---|
| 584 | for (l = 0; l < LAYERS; l++) |
|---|
| 585 | drv_generic_graphic_FB[l] = NULL; |
|---|
| 586 | |
|---|
| 587 | drv_generic_graphic_resizeFB(DROWS, DCOLS); |
|---|
| 588 | |
|---|
| 589 | /* sanity check */ |
|---|
| 590 | for (l = 0; l < LAYERS; l++) { |
|---|
| 591 | if (drv_generic_graphic_FB[l] == NULL) { |
|---|
| 592 | error("%s: framebuffer could not be allocated: malloc() failed", Driver); |
|---|
| 593 | return -1; |
|---|
| 594 | } |
|---|
| 595 | } |
|---|
| 596 | |
|---|
| 597 | /* init generic driver & register plugins */ |
|---|
| 598 | drv_generic_init(); |
|---|
| 599 | |
|---|
| 600 | /* set default colors */ |
|---|
| 601 | color = cfg_get(Section, "foreground", "000000ff"); |
|---|
| 602 | if (color2RGBA(color, &FG_COL) < 0) { |
|---|
| 603 | error("%s: ignoring illegal color '%s'", Driver, color); |
|---|
| 604 | } |
|---|
| 605 | if (color) |
|---|
| 606 | free(color); |
|---|
| 607 | |
|---|
| 608 | color = cfg_get(Section, "background", "ffffff00"); |
|---|
| 609 | if (color2RGBA(color, &BG_COL) < 0) { |
|---|
| 610 | error("%s: ignoring illegal color '%s'", Driver, color); |
|---|
| 611 | } |
|---|
| 612 | if (color) |
|---|
| 613 | free(color); |
|---|
| 614 | |
|---|
| 615 | color = cfg_get(Section, "basecolor", "ffffff"); |
|---|
| 616 | if (color2RGBA(color, &BL_COL) < 0) { |
|---|
| 617 | error("%s: ignoring illegal color '%s'", Driver, color); |
|---|
| 618 | } |
|---|
| 619 | if (color) |
|---|
| 620 | free(color); |
|---|
| 621 | |
|---|
| 622 | /* inverted display? */ |
|---|
| 623 | cfg_number(section, "inverted", 0, 0, 1, &INVERTED); |
|---|
| 624 | |
|---|
| 625 | /* register text widget */ |
|---|
| 626 | wc = Widget_Text; |
|---|
| 627 | wc.draw = drv_generic_graphic_draw; |
|---|
| 628 | widget_register(&wc); |
|---|
| 629 | |
|---|
| 630 | /* register icon widget */ |
|---|
| 631 | wc = Widget_Icon; |
|---|
| 632 | wc.draw = drv_generic_graphic_icon_draw; |
|---|
| 633 | widget_register(&wc); |
|---|
| 634 | |
|---|
| 635 | /* register bar widget */ |
|---|
| 636 | wc = Widget_Bar; |
|---|
| 637 | wc.draw = drv_generic_graphic_bar_draw; |
|---|
| 638 | widget_register(&wc); |
|---|
| 639 | |
|---|
| 640 | /* register image widget */ |
|---|
| 641 | #ifdef WITH_IMAGE |
|---|
| 642 | wc = Widget_Image; |
|---|
| 643 | wc.draw = drv_generic_graphic_image_draw; |
|---|
| 644 | widget_register(&wc); |
|---|
| 645 | #endif |
|---|
| 646 | |
|---|
| 647 | /* clear framebuffer */ |
|---|
| 648 | drv_generic_graphic_clear(); |
|---|
| 649 | |
|---|
| 650 | return 0; |
|---|
| 651 | } |
|---|
| 652 | |
|---|
| 653 | |
|---|
| 654 | int drv_generic_graphic_clear(void) |
|---|
| 655 | { |
|---|
| 656 | int i, l; |
|---|
| 657 | |
|---|
| 658 | for (l = 0; l < LAYERS; l++) |
|---|
| 659 | for (i = 0; i < LCOLS * LROWS; i++) |
|---|
| 660 | drv_generic_graphic_FB[l][i] = NO_COL; |
|---|
| 661 | |
|---|
| 662 | drv_generic_graphic_blit(0, 0, LROWS, LCOLS); |
|---|
| 663 | |
|---|
| 664 | return 0; |
|---|
| 665 | } |
|---|
| 666 | |
|---|
| 667 | |
|---|
| 668 | RGBA drv_generic_graphic_rgb(const int row, const int col) |
|---|
| 669 | { |
|---|
| 670 | return drv_generic_graphic_blend(row, col); |
|---|
| 671 | } |
|---|
| 672 | |
|---|
| 673 | |
|---|
| 674 | unsigned char drv_generic_graphic_gray(const int row, const int col) |
|---|
| 675 | { |
|---|
| 676 | RGBA p = drv_generic_graphic_blend(row, col); |
|---|
| 677 | return (77 * p.R + 150 * p.G + 28 * p.B) / 255; |
|---|
| 678 | } |
|---|
| 679 | |
|---|
| 680 | |
|---|
| 681 | unsigned char drv_generic_graphic_black(const int row, const int col) |
|---|
| 682 | { |
|---|
| 683 | return drv_generic_graphic_gray(row, col) < 127; |
|---|
| 684 | } |
|---|
| 685 | |
|---|
| 686 | |
|---|
| 687 | int drv_generic_graphic_quit(void) |
|---|
| 688 | { |
|---|
| 689 | int l; |
|---|
| 690 | |
|---|
| 691 | for (l = 0; l < LAYERS; l++) { |
|---|
| 692 | if (drv_generic_graphic_FB[l]) { |
|---|
| 693 | free(drv_generic_graphic_FB[l]); |
|---|
| 694 | drv_generic_graphic_FB[l] = NULL; |
|---|
| 695 | } |
|---|
| 696 | } |
|---|
| 697 | widget_unregister(); |
|---|
| 698 | return (0); |
|---|
| 699 | } |
|---|