Subversion Repositories HelenOS

Rev

Rev 4644 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2008 Martin Decky
  3.  * Copyright (c) 2006 Jakub Vana
  4.  * Copyright (c) 2006 Ondrej Palkovsky
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * - Redistributions of source code must retain the above copyright
  12.  *   notice, this list of conditions and the following disclaimer.
  13.  * - Redistributions in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in the
  15.  *   documentation and/or other materials provided with the distribution.
  16.  * - The name of the author may not be used to endorse or promote products
  17.  *   derived from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  20.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  23.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  */
  30.  
  31. /**
  32.  * @defgroup fb Graphical framebuffer
  33.  * @brief HelenOS graphical framebuffer.
  34.  * @ingroup fbs
  35.  * @{
  36.  */
  37.  
  38. /** @file
  39.  */
  40.  
  41. #include <stdlib.h>
  42. #include <unistd.h>
  43. #include <string.h>
  44. #include <ddi.h>
  45. #include <sysinfo.h>
  46. #include <align.h>
  47. #include <as.h>
  48. #include <ipc/fb.h>
  49. #include <ipc/ipc.h>
  50. #include <ipc/ns.h>
  51. #include <ipc/services.h>
  52. #include <kernel/errno.h>
  53. #include <kernel/genarch/fb/visuals.h>
  54. #include <io/color.h>
  55. #include <io/style.h>
  56. #include <async.h>
  57. #include <fibril.h>
  58. #include <bool.h>
  59. #include <stdio.h>
  60.  
  61. #include "font-8x16.h"
  62. #include "fb.h"
  63. #include "main.h"
  64. #include "../console/screenbuffer.h"
  65. #include "ppm.h"
  66.  
  67. #include "pointer.xbm"
  68. #include "pointer_mask.xbm"
  69.  
  70. #define DEFAULT_BGCOLOR  0xf0f0f0
  71. #define DEFAULT_FGCOLOR  0x000000
  72.  
  73. #define GLYPH_UNAVAIL    '?'
  74.  
  75. #define MAX_ANIM_LEN     8
  76. #define MAX_ANIMATIONS   4
  77. #define MAX_PIXMAPS      256  /**< Maximum number of saved pixmaps */
  78. #define MAX_VIEWPORTS    128  /**< Viewport is a rectangular area on the screen */
  79.  
  80. /** Function to render a pixel from a RGB value. */
  81. typedef void (*rgb_conv_t)(void *, uint32_t);
  82.  
  83. /** Function to render a bit mask. */
  84. typedef void (*mask_conv_t)(void *, bool);
  85.  
  86. /** Function to draw a glyph. */
  87. typedef void (*dg_t)(unsigned int x, unsigned int y, bool cursor,
  88.     uint8_t *glyphs, uint32_t glyph, uint32_t fg_color, uint32_t bg_color);
  89.  
  90. struct {
  91.     uint8_t *fb_addr;
  92.    
  93.     unsigned int xres;
  94.     unsigned int yres;
  95.    
  96.     unsigned int scanline;
  97.     unsigned int glyphscanline;
  98.    
  99.     unsigned int pixelbytes;
  100.     unsigned int glyphbytes;
  101.    
  102.     /** Pre-rendered mask for rendering glyphs. Specific for the visual. */
  103.     uint8_t *glyphs;
  104.    
  105.     rgb_conv_t rgb_conv;
  106.     mask_conv_t mask_conv;
  107. } screen;
  108.  
  109. /** Backbuffer character cell. */
  110. typedef struct {
  111.     uint32_t glyph;
  112.     uint32_t fg_color;
  113.     uint32_t bg_color;
  114. } bb_cell_t;
  115.  
  116. typedef struct {
  117.     bool initialized;
  118.     unsigned int x;
  119.     unsigned int y;
  120.     unsigned int width;
  121.     unsigned int height;
  122.    
  123.     /* Text support in window */
  124.     unsigned int cols;
  125.     unsigned int rows;
  126.    
  127.     /*
  128.      * Style and glyphs for text printing
  129.      */
  130.    
  131.     /** Current attributes. */
  132.     attr_rgb_t attr;
  133.    
  134.     uint8_t *bgpixel;
  135.    
  136.     /**
  137.      * Glyph drawing function for this viewport.  Different viewports
  138.      * might use different drawing functions depending on whether their
  139.      * scanlines are aligned on a word boundary.
  140.      */
  141.     dg_t dglyph;
  142.    
  143.     /* Auto-cursor position */
  144.     bool cursor_active;
  145.     unsigned int cur_col;
  146.     unsigned int cur_row;
  147.     bool cursor_shown;
  148.    
  149.     /* Back buffer */
  150.     bb_cell_t *backbuf;
  151.     unsigned int bbsize;
  152. } viewport_t;
  153.  
  154. typedef struct {
  155.     bool initialized;
  156.     bool enabled;
  157.     unsigned int vp;
  158.    
  159.     unsigned int pos;
  160.     unsigned int animlen;
  161.     unsigned int pixmaps[MAX_ANIM_LEN];
  162. } animation_t;
  163.  
  164. static animation_t animations[MAX_ANIMATIONS];
  165. static bool anims_enabled;
  166.  
  167. typedef struct {
  168.     unsigned int width;
  169.     unsigned int height;
  170.     uint8_t *data;
  171. } pixmap_t;
  172.  
  173. static pixmap_t pixmaps[MAX_PIXMAPS];
  174. static viewport_t viewports[128];
  175.  
  176. static bool client_connected = false;  /**< Allow only 1 connection */
  177.  
  178. static uint32_t color_table[16] = {
  179.     [COLOR_BLACK]       = 0x000000,
  180.     [COLOR_BLUE]        = 0x0000f0,
  181.     [COLOR_GREEN]       = 0x00f000,
  182.     [COLOR_CYAN]        = 0x00f0f0,
  183.     [COLOR_RED]         = 0xf00000,
  184.     [COLOR_MAGENTA]     = 0xf000f0,
  185.     [COLOR_YELLOW]      = 0xf0f000,
  186.     [COLOR_WHITE]       = 0xf0f0f0,
  187.    
  188.     [8 + COLOR_BLACK]   = 0x000000,
  189.     [8 + COLOR_BLUE]    = 0x0000ff,
  190.     [8 + COLOR_GREEN]   = 0x00ff00,
  191.     [8 + COLOR_CYAN]    = 0x00ffff,
  192.     [8 + COLOR_RED]     = 0xff0000,
  193.     [8 + COLOR_MAGENTA] = 0xff00ff,
  194.     [8 + COLOR_YELLOW]  = 0xffff00,
  195.     [8 + COLOR_WHITE]   = 0xffffff,
  196. };
  197.  
  198. static int rgb_from_attr(attr_rgb_t *rgb, const attrs_t *a);
  199. static int rgb_from_style(attr_rgb_t *rgb, int style);
  200. static int rgb_from_idx(attr_rgb_t *rgb, ipcarg_t fg_color,
  201.     ipcarg_t bg_color, ipcarg_t flags);
  202.  
  203. static int fb_set_color(viewport_t *vport, ipcarg_t fg_color,
  204.     ipcarg_t bg_color, ipcarg_t attr);
  205.  
  206. static void draw_glyph_aligned(unsigned int x, unsigned int y, bool cursor,
  207.     uint8_t *glyphs, uint32_t glyph, uint32_t fg_color, uint32_t bg_color);
  208. static void draw_glyph_fallback(unsigned int x, unsigned int y, bool cursor,
  209.     uint8_t *glyphs, uint32_t glyph, uint32_t fg_color, uint32_t bg_color);
  210.  
  211. static void draw_vp_glyph(viewport_t *vport, bool cursor, unsigned int col,
  212.     unsigned int row);
  213.  
  214.  
  215. #define RED(x, bits)                 ((x >> (8 + 8 + 8 - bits)) & ((1 << bits) - 1))
  216. #define GREEN(x, bits)               ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
  217. #define BLUE(x, bits)                ((x >> (8 - bits)) & ((1 << bits) - 1))
  218.  
  219. #define COL2X(col)                   ((col) * FONT_WIDTH)
  220. #define ROW2Y(row)                   ((row) * FONT_SCANLINES)
  221.  
  222. #define X2COL(x)                     ((x) / FONT_WIDTH)
  223. #define Y2ROW(y)                     ((y) / FONT_SCANLINES)
  224.  
  225. #define FB_POS(x, y)                 ((y) * screen.scanline + (x) * screen.pixelbytes)
  226. #define BB_POS(vport, col, row)      ((row) * vport->cols + (col))
  227. #define GLYPH_POS(glyph, y, cursor)  (((glyph) + (cursor) * FONT_GLYPHS) * screen.glyphbytes + (y) * screen.glyphscanline)
  228.  
  229.  
  230. /** ARGB 8:8:8:8 conversion
  231.  *
  232.  */
  233. static void rgb_0888(void *dst, uint32_t rgb)
  234. {
  235.     *((uint32_t *) dst) = rgb & 0x00ffffff;
  236. }
  237.  
  238. static void mask_0888(void *dst, bool mask)
  239. {
  240.     *((uint32_t *) dst) = (mask ? 0x00ffffff : 0);
  241. }
  242.  
  243.  
  244. /** ABGR 8:8:8:8 conversion
  245.  *
  246.  */
  247. static void bgr_0888(void *dst, uint32_t rgb)
  248. {
  249.     *((uint32_t *) dst)
  250.         = (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | RED(rgb, 8);
  251. }
  252.  
  253. static void rgb_8880(void *dst, uint32_t rgb)
  254. {
  255.     *((uint32_t *) dst)
  256.         = (RED(rgb, 8) << 24) | (GREEN(rgb, 8) << 16) | BLUE(rgb, 8) << 8;
  257. }
  258.  
  259. static void mask_8880(void *dst, bool mask)
  260. {
  261.     *((uint32_t *) dst) = (mask ? 0xffffff00 : 0);
  262. }
  263.  
  264. /** RGB 8:8:8 conversion
  265.  *
  266.  */
  267. static void rgb_888(void *dst, uint32_t rgb)
  268. {
  269.     ((uint8_t *) dst)[0] = BLUE(rgb, 8);
  270.     ((uint8_t *) dst)[1] = GREEN(rgb, 8);
  271.     ((uint8_t *) dst)[2] = RED(rgb, 8);
  272. }
  273.  
  274. static void mask_888(void *dst, bool mask)
  275. {
  276.     if (mask) {
  277.         ((uint8_t *) dst)[0] = 0xff;
  278.         ((uint8_t *) dst)[1] = 0xff;
  279.         ((uint8_t *) dst)[2] = 0xff;
  280.     } else {
  281.         ((uint8_t *) dst)[0] = 0;
  282.         ((uint8_t *) dst)[1] = 0;
  283.         ((uint8_t *) dst)[2] = 0;
  284.     }
  285. }
  286.  
  287.  
  288. /** BGR 8:8:8 conversion
  289.  *
  290.  */
  291. static void bgr_888(void *dst, uint32_t rgb)
  292. {
  293.     ((uint8_t *) dst)[0] = RED(rgb, 8);
  294.     ((uint8_t *) dst)[1] = GREEN(rgb, 8);
  295.     ((uint8_t *) dst)[2] = BLUE(rgb, 8);
  296. }
  297.  
  298.  
  299. /** RGB 5:5:5 conversion
  300.  *
  301.  */
  302. static void rgb_555(void *dst, uint32_t rgb)
  303. {
  304.     *((uint16_t *) dst)
  305.         = (RED(rgb, 5) << 10) | (GREEN(rgb, 5) << 5) | BLUE(rgb, 5);
  306. }
  307.  
  308. static void mask_555(void *dst, bool mask)
  309. {
  310.     *((uint16_t *) dst) = (mask ? 0x7fff : 0);
  311. }
  312.  
  313.  
  314. /** RGB 5:6:5 conversion
  315.  *
  316.  */
  317. static void rgb_565(void *dst, uint32_t rgb)
  318. {
  319.     *((uint16_t *) dst)
  320.         = (RED(rgb, 5) << 11) | (GREEN(rgb, 6) << 5) | BLUE(rgb, 5);
  321. }
  322.  
  323. static void mask_565(void *dst, bool mask)
  324. {
  325.     *((uint16_t *) dst) = (mask ? 0xffff : 0);
  326. }
  327.  
  328.  
  329. /** RGB 3:2:3
  330.  *
  331.  */
  332. static void rgb_323(void *dst, uint32_t rgb)
  333. {
  334.     *((uint8_t *) dst)
  335.         = ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
  336. }
  337.  
  338. static void mask_323(void *dst, bool mask)
  339. {
  340.     *((uint8_t *) dst) = (mask ? 0xff : 0);
  341. }
  342.  
  343. /** Draw a filled rectangle.
  344.  *
  345.  * @note Need real implementation that does not access VRAM twice.
  346.  *
  347.  */
  348. static void draw_filled_rect(unsigned int x0, unsigned int y0, unsigned int x1,
  349.     unsigned int y1, uint32_t color)
  350. {
  351.     unsigned int x;
  352.     unsigned int y;
  353.     unsigned int copy_bytes;
  354.    
  355.     uint8_t *sp;
  356.     uint8_t *dp;
  357.     uint8_t cbuf[4];
  358.    
  359.     if ((y0 >= y1) || (x0 >= x1))
  360.         return;
  361.    
  362.     screen.rgb_conv(cbuf, color);
  363.    
  364.     sp = &screen.fb_addr[FB_POS(x0, y0)];
  365.     dp = sp;
  366.    
  367.     /* Draw the first line. */
  368.     for (x = x0; x < x1; x++) {
  369.         memcpy(dp, cbuf, screen.pixelbytes);
  370.         dp += screen.pixelbytes;
  371.     }
  372.    
  373.     dp = sp + screen.scanline;
  374.     copy_bytes = (x1 - x0) * screen.pixelbytes;
  375.    
  376.     /* Draw the remaining lines by copying. */
  377.     for (y = y0 + 1; y < y1; y++) {
  378.         memcpy(dp, sp, copy_bytes);
  379.         dp += screen.scanline;
  380.     }
  381. }
  382.  
  383. /** Redraw viewport.
  384.  *
  385.  * @param vport Viewport to redraw
  386.  *
  387.  */
  388. static void vport_redraw(viewport_t *vport)
  389. {
  390.     unsigned int col;
  391.     unsigned int row;
  392.    
  393.     for (row = 0; row < vport->rows; row++) {
  394.         for (col = 0; col < vport->cols; col++) {
  395.             draw_vp_glyph(vport, false, col, row);
  396.         }
  397.     }
  398.    
  399.     if (COL2X(vport->cols) < vport->width) {
  400.         draw_filled_rect(
  401.             vport->x + COL2X(vport->cols), vport->y,
  402.             vport->x + vport->width, vport->y + vport->height,
  403.             vport->attr.bg_color);
  404.     }
  405.    
  406.     if (ROW2Y(vport->rows) < vport->height) {
  407.         draw_filled_rect(
  408.             vport->x, vport->y + ROW2Y(vport->rows),
  409.             vport->x + vport->width, vport->y + vport->height,
  410.             vport->attr.bg_color);
  411.     }
  412. }
  413.  
  414. static void backbuf_clear(bb_cell_t *backbuf, size_t len, uint32_t fg_color,
  415.     uint32_t bg_color)
  416. {
  417.     size_t i;
  418.    
  419.     for (i = 0; i < len; i++) {
  420.         backbuf[i].glyph = 0;
  421.         backbuf[i].fg_color = fg_color;
  422.         backbuf[i].bg_color = bg_color;
  423.     }
  424. }
  425.  
  426. /** Clear viewport.
  427.  *
  428.  * @param vport Viewport to clear
  429.  *
  430.  */
  431. static void vport_clear(viewport_t *vport)
  432. {
  433.     backbuf_clear(vport->backbuf, vport->cols * vport->rows,
  434.         vport->attr.fg_color, vport->attr.bg_color);
  435.     vport_redraw(vport);
  436. }
  437.  
  438. /** Scroll viewport by the specified number of lines.
  439.  *
  440.  * @param vport Viewport to scroll
  441.  * @param lines Number of lines to scroll
  442.  *
  443.  */
  444. static void vport_scroll(viewport_t *vport, int lines)
  445. {
  446.     unsigned int col;
  447.     unsigned int row;
  448.     unsigned int x;
  449.     unsigned int y;
  450.     uint32_t glyph;
  451.     uint32_t fg_color;
  452.     uint32_t bg_color;
  453.     bb_cell_t *bbp;
  454.     bb_cell_t *xbp;
  455.    
  456.     /*
  457.      * Redraw.
  458.      */
  459.    
  460.     y = vport->y;
  461.     for (row = 0; row < vport->rows; row++) {
  462.         x = vport->x;
  463.         for (col = 0; col < vport->cols; col++) {
  464.             if (((int) row + lines >= 0) &&
  465.                 ((int) row + lines < (int) vport->rows)) {
  466.                 xbp = &vport->backbuf[BB_POS(vport, col, row + lines)];
  467.                 bbp = &vport->backbuf[BB_POS(vport, col, row)];
  468.                
  469.                 glyph = xbp->glyph;
  470.                 fg_color = xbp->fg_color;
  471.                 bg_color = xbp->bg_color;
  472.                
  473.                 if ((bbp->glyph == glyph)
  474.                    && (bbp->fg_color == xbp->fg_color)
  475.                    && (bbp->bg_color == xbp->bg_color)) {
  476.                     x += FONT_WIDTH;
  477.                     continue;
  478.                 }
  479.             } else {
  480.                 glyph = 0;
  481.                 fg_color = vport->attr.fg_color;
  482.                 bg_color = vport->attr.bg_color;
  483.             }
  484.            
  485.             (*vport->dglyph)(x, y, false, screen.glyphs, glyph,
  486.                 fg_color, bg_color);
  487.             x += FONT_WIDTH;
  488.         }
  489.         y += FONT_SCANLINES;
  490.     }
  491.    
  492.     /*
  493.      * Scroll backbuffer.
  494.      */
  495.    
  496.     if (lines > 0) {
  497.         memmove(vport->backbuf, vport->backbuf + vport->cols * lines,
  498.             vport->cols * (vport->rows - lines) * sizeof(bb_cell_t));
  499.         backbuf_clear(&vport->backbuf[BB_POS(vport, 0, vport->rows - lines)],
  500.             vport->cols * lines, vport->attr.fg_color, vport->attr.bg_color);
  501.     } else {
  502.         memmove(vport->backbuf - vport->cols * lines, vport->backbuf,
  503.             vport->cols * (vport->rows + lines) * sizeof(bb_cell_t));
  504.         backbuf_clear(vport->backbuf, - vport->cols * lines,
  505.             vport->attr.fg_color, vport->attr.bg_color);
  506.     }
  507. }
  508.  
  509. /** Render glyphs
  510.  *
  511.  * Convert glyphs from device independent font
  512.  * description to current visual representation.
  513.  *
  514.  */
  515. static void render_glyphs(void)
  516. {
  517.     unsigned int glyph;
  518.    
  519.     for (glyph = 0; glyph < FONT_GLYPHS; glyph++) {
  520.         unsigned int y;
  521.        
  522.         for (y = 0; y < FONT_SCANLINES; y++) {
  523.             unsigned int x;
  524.            
  525.             for (x = 0; x < FONT_WIDTH; x++) {
  526.                 screen.mask_conv(&screen.glyphs[GLYPH_POS(glyph, y, false) + x * screen.pixelbytes],
  527.                     (fb_font[glyph][y] & (1 << (7 - x))) ? true : false);
  528.                
  529.                 screen.mask_conv(&screen.glyphs[GLYPH_POS(glyph, y, true) + x * screen.pixelbytes],
  530.                     (fb_font[glyph][y] & (1 << (7 - x))) ? false : true);
  531.             }
  532.         }
  533.     }
  534. }
  535.  
  536. /** Create new viewport
  537.  *
  538.  * @param x      Origin of the viewport (x).
  539.  * @param y      Origin of the viewport (y).
  540.  * @param width  Width of the viewport.
  541.  * @param height Height of the viewport.
  542.  *
  543.  * @return New viewport number.
  544.  *
  545.  */
  546. static int vport_create(unsigned int x, unsigned int y,
  547.     unsigned int width, unsigned int height)
  548. {
  549.     unsigned int i;
  550.    
  551.     for (i = 0; i < MAX_VIEWPORTS; i++) {
  552.         if (!viewports[i].initialized)
  553.             break;
  554.     }
  555.    
  556.     if (i == MAX_VIEWPORTS)
  557.         return ELIMIT;
  558.    
  559.     unsigned int cols = width / FONT_WIDTH;
  560.     unsigned int rows = height / FONT_SCANLINES;
  561.     unsigned int bbsize = cols * rows * sizeof(bb_cell_t);
  562.     unsigned int word_size = sizeof(unsigned long);
  563.    
  564.     bb_cell_t *backbuf = (bb_cell_t *) malloc(bbsize);
  565.     if (!backbuf)
  566.         return ENOMEM;
  567.    
  568.     uint8_t *bgpixel = (uint8_t *) malloc(screen.pixelbytes);
  569.     if (!bgpixel) {
  570.         free(backbuf);
  571.         return ENOMEM;
  572.     }
  573.    
  574.     backbuf_clear(backbuf, cols * rows, DEFAULT_FGCOLOR, DEFAULT_BGCOLOR);
  575.     memset(bgpixel, 0, screen.pixelbytes);
  576.    
  577.     viewports[i].x = x;
  578.     viewports[i].y = y;
  579.     viewports[i].width = width;
  580.     viewports[i].height = height;
  581.    
  582.     viewports[i].cols = cols;
  583.     viewports[i].rows = rows;
  584.    
  585.     viewports[i].attr.bg_color = DEFAULT_BGCOLOR;
  586.     viewports[i].attr.fg_color = DEFAULT_FGCOLOR;
  587.    
  588.     viewports[i].bgpixel = bgpixel;
  589.    
  590.     /*
  591.      * Conditions necessary to select aligned version:
  592.      *  - word size is divisible by pixelbytes
  593.      *  - cell scanline size is divisible by word size
  594.      *  - cell scanlines are word-aligned
  595.      *
  596.      */
  597.     if (((word_size % screen.pixelbytes) == 0)
  598.         && ((FONT_WIDTH * screen.pixelbytes) % word_size == 0)
  599.         && ((x * screen.pixelbytes) % word_size == 0)
  600.         && (screen.scanline % word_size == 0)) {
  601.         viewports[i].dglyph = draw_glyph_aligned;
  602.     } else {
  603.         viewports[i].dglyph = draw_glyph_fallback;
  604.     }
  605.    
  606.     viewports[i].cur_col = 0;
  607.     viewports[i].cur_row = 0;
  608.     viewports[i].cursor_active = false;
  609.     viewports[i].cursor_shown = false;
  610.    
  611.     viewports[i].bbsize = bbsize;
  612.     viewports[i].backbuf = backbuf;
  613.    
  614.     viewports[i].initialized = true;
  615.    
  616.     screen.rgb_conv(viewports[i].bgpixel, viewports[i].attr.bg_color);
  617.    
  618.     return i;
  619. }
  620.  
  621.  
  622. /** Initialize framebuffer as a chardev output device
  623.  *
  624.  * @param addr   Address of the framebuffer
  625.  * @param xres   Screen width in pixels
  626.  * @param yres   Screen height in pixels
  627.  * @param visual Bits per pixel (8, 16, 24, 32)
  628.  * @param scan   Bytes per one scanline
  629.  *
  630.  */
  631. static bool screen_init(void *addr, unsigned int xres, unsigned int yres,
  632.     unsigned int scan, unsigned int visual)
  633. {
  634.    
  635.    
  636.     switch (visual) {
  637.     case VISUAL_INDIRECT_8:
  638.         screen.rgb_conv = rgb_323;
  639.         screen.mask_conv = mask_323;
  640.         screen.pixelbytes = 1;
  641.         break;
  642.     case VISUAL_RGB_5_5_5:
  643.         screen.rgb_conv = rgb_555;
  644.         screen.mask_conv = mask_555;
  645.         screen.pixelbytes = 2;
  646.         break;
  647.     case VISUAL_RGB_5_6_5:
  648.         screen.rgb_conv = rgb_565;
  649.         screen.mask_conv = mask_565;
  650.         screen.pixelbytes = 2;
  651.         break;
  652.     case VISUAL_RGB_8_8_8:
  653.         screen.rgb_conv = rgb_888;
  654.         screen.mask_conv = mask_888;
  655.         screen.pixelbytes = 3;
  656.         break;
  657.     case VISUAL_BGR_8_8_8:
  658.         screen.rgb_conv = bgr_888;
  659.         screen.mask_conv = mask_888;
  660.         screen.pixelbytes = 3;
  661.         break;
  662.     case VISUAL_RGB_8_8_8_0:
  663.         screen.rgb_conv = rgb_8880;
  664.         screen.mask_conv = mask_8880;
  665.         screen.pixelbytes = 4;
  666.         break;
  667.     case VISUAL_RGB_0_8_8_8:
  668.         screen.rgb_conv = rgb_0888;
  669.         screen.mask_conv = mask_0888;
  670.         screen.pixelbytes = 4;
  671.         break;
  672.     case VISUAL_BGR_0_8_8_8:
  673.         screen.rgb_conv = bgr_0888;
  674.         screen.mask_conv = mask_0888;
  675.         screen.pixelbytes = 4;
  676.         break;
  677.     default:
  678.         return false;
  679.     }
  680.    
  681.     screen.fb_addr = (unsigned char *) addr;
  682.     screen.xres = xres;
  683.     screen.yres = yres;
  684.     screen.scanline = scan;
  685.    
  686.     screen.glyphscanline = FONT_WIDTH * screen.pixelbytes;
  687.     screen.glyphbytes = screen.glyphscanline * FONT_SCANLINES;
  688.    
  689.     size_t glyphsize = 2 * FONT_GLYPHS * screen.glyphbytes;
  690.     uint8_t *glyphs = (uint8_t *) malloc(glyphsize);
  691.     if (!glyphs)
  692.         return false;
  693.    
  694.     memset(glyphs, 0, glyphsize);
  695.     screen.glyphs = glyphs;
  696.    
  697.     render_glyphs();
  698.    
  699.     /* Create first viewport */
  700.     vport_create(0, 0, xres, yres);
  701.    
  702.     return true;
  703. }
  704.  
  705.  
  706. /** Draw a glyph, takes advantage of alignment.
  707.  *
  708.  * This version can only be used if the following conditions are met:
  709.  *
  710.  *   - word size is divisible by pixelbytes
  711.  *   - cell scanline size is divisible by word size
  712.  *   - cell scanlines are word-aligned
  713.  *
  714.  * It makes use of the pre-rendered mask to process (possibly) several
  715.  * pixels at once (word size / pixelbytes pixels at a time are processed)
  716.  * making it very fast. Most notably this version is not applicable at 24 bits
  717.  * per pixel.
  718.  *
  719.  * @param x        x coordinate of top-left corner on screen.
  720.  * @param y        y coordinate of top-left corner on screen.
  721.  * @param cursor   Draw glyph with cursor
  722.  * @param glyphs   Pointer to font bitmap.
  723.  * @param glyph    Code of the glyph to draw.
  724.  * @param fg_color Foreground color.
  725.  * @param bg_color Backgroudn color.
  726.  *
  727.  */
  728. static void draw_glyph_aligned(unsigned int x, unsigned int y, bool cursor,
  729.     uint8_t *glyphs, uint32_t glyph, uint32_t fg_color, uint32_t bg_color)
  730. {
  731.     unsigned int i;
  732.     unsigned int yd;
  733.     unsigned long fg_buf;
  734.     unsigned long bg_buf;
  735.     unsigned long mask;
  736.    
  737.     /*
  738.      * Prepare a pair of words, one filled with foreground-color
  739.      * pattern and the other filled with background-color pattern.
  740.      */
  741.     for (i = 0; i < sizeof(unsigned long) / screen.pixelbytes; i++) {
  742.         screen.rgb_conv(&((uint8_t *) &fg_buf)[i * screen.pixelbytes],
  743.             fg_color);
  744.         screen.rgb_conv(&((uint8_t *) &bg_buf)[i * screen.pixelbytes],
  745.             bg_color);
  746.     }
  747.    
  748.     /* Pointer to the current position in the mask. */
  749.     unsigned long *maskp = (unsigned long *) &glyphs[GLYPH_POS(glyph, 0, cursor)];
  750.    
  751.     /* Pointer to the current position on the screen. */
  752.     unsigned long *dp = (unsigned long *) &screen.fb_addr[FB_POS(x, y)];
  753.    
  754.     /* Width of the character cell in words. */
  755.     unsigned int ww = FONT_WIDTH * screen.pixelbytes / sizeof(unsigned long);
  756.    
  757.     /* Offset to add when moving to another screen scanline. */
  758.     unsigned int d_add = screen.scanline - FONT_WIDTH * screen.pixelbytes;
  759.    
  760.     for (yd = 0; yd < FONT_SCANLINES; yd++) {
  761.         /*
  762.          * Now process the cell scanline, combining foreground
  763.          * and background color patters using the pre-rendered mask.
  764.          */
  765.         for (i = 0; i < ww; i++) {
  766.             mask = *maskp++;
  767.             *dp++ = (fg_buf & mask) | (bg_buf & ~mask);
  768.         }
  769.        
  770.         /* Move to the beginning of the next scanline of the cell. */
  771.         dp = (unsigned long *) ((uint8_t *) dp + d_add);
  772.     }
  773. }
  774.  
  775. /** Draw a glyph, fallback version.
  776.  *
  777.  * This version does not make use of the pre-rendered mask, it uses
  778.  * the font bitmap directly. It works always, but it is slower.
  779.  *
  780.  * @param x        x coordinate of top-left corner on screen.
  781.  * @param y        y coordinate of top-left corner on screen.
  782.  * @param cursor   Draw glyph with cursor
  783.  * @param glyphs   Pointer to font bitmap.
  784.  * @param glyph    Code of the glyph to draw.
  785.  * @param fg_color Foreground color.
  786.  * @param bg_color Backgroudn color.
  787.  *
  788.  */
  789. void draw_glyph_fallback(unsigned int x, unsigned int y, bool cursor,
  790.     uint8_t *glyphs, uint32_t glyph, uint32_t fg_color, uint32_t bg_color)
  791. {
  792.     unsigned int i;
  793.     unsigned int j;
  794.     unsigned int yd;
  795.     uint8_t fg_buf[4];
  796.     uint8_t bg_buf[4];
  797.     uint8_t *sp;
  798.     uint8_t b;
  799.    
  800.     /* Pre-render 1x the foreground and background color pixels. */
  801.     if (cursor) {
  802.         screen.rgb_conv(fg_buf, bg_color);
  803.         screen.rgb_conv(bg_buf, fg_color);
  804.     } else {
  805.         screen.rgb_conv(fg_buf, fg_color);
  806.         screen.rgb_conv(bg_buf, bg_color);
  807.     }
  808.    
  809.     /* Pointer to the current position on the screen. */
  810.     uint8_t *dp = (uint8_t *) &screen.fb_addr[FB_POS(x, y)];
  811.    
  812.     /* Offset to add when moving to another screen scanline. */
  813.     unsigned int d_add = screen.scanline - FONT_WIDTH * screen.pixelbytes;
  814.    
  815.     for (yd = 0; yd < FONT_SCANLINES; yd++) {
  816.         /* Byte containing bits of the glyph scanline. */
  817.         b = fb_font[glyph][yd];
  818.        
  819.         for (i = 0; i < FONT_WIDTH; i++) {
  820.             /* Choose color based on the current bit. */
  821.             sp = (b & 0x80) ? fg_buf : bg_buf;
  822.            
  823.             /* Copy the pixel. */
  824.             for (j = 0; j < screen.pixelbytes; j++) {
  825.                 *dp++ = *sp++;
  826.             }
  827.            
  828.             /* Move to the next bit. */
  829.             b = b << 1;
  830.         }
  831.        
  832.         /* Move to the beginning of the next scanline of the cell. */
  833.         dp += d_add;
  834.     }
  835. }
  836.  
  837. /** Draw glyph at specified position in viewport.
  838.  *
  839.  * @param vport  Viewport identification
  840.  * @param cursor Draw glyph with cursor
  841.  * @param col    Screen position relative to viewport
  842.  * @param row    Screen position relative to viewport
  843.  *
  844.  */
  845. static void draw_vp_glyph(viewport_t *vport, bool cursor, unsigned int col,
  846.     unsigned int row)
  847. {
  848.     unsigned int x = vport->x + COL2X(col);
  849.     unsigned int y = vport->y + ROW2Y(row);
  850.    
  851.     uint32_t glyph = vport->backbuf[BB_POS(vport, col, row)].glyph;
  852.     uint32_t fg_color = vport->backbuf[BB_POS(vport, col, row)].fg_color;
  853.     uint32_t bg_color = vport->backbuf[BB_POS(vport, col, row)].bg_color;
  854.    
  855.     (*vport->dglyph)(x, y, cursor, screen.glyphs, glyph,
  856.         fg_color, bg_color);
  857. }
  858.  
  859. /** Hide cursor if it is shown
  860.  *
  861.  */
  862. static void cursor_hide(viewport_t *vport)
  863. {
  864.     if ((vport->cursor_active) && (vport->cursor_shown)) {
  865.         draw_vp_glyph(vport, false, vport->cur_col, vport->cur_row);
  866.         vport->cursor_shown = false;
  867.     }
  868. }
  869.  
  870.  
  871. /** Show cursor if cursor showing is enabled
  872.  *
  873.  */
  874. static void cursor_show(viewport_t *vport)
  875. {
  876.     /* Do not check for cursor_shown */
  877.     if (vport->cursor_active) {
  878.         draw_vp_glyph(vport, true, vport->cur_col, vport->cur_row);
  879.         vport->cursor_shown = true;
  880.     }
  881. }
  882.  
  883.  
  884. /** Invert cursor, if it is enabled
  885.  *
  886.  */
  887. static void cursor_blink(viewport_t *vport)
  888. {
  889.     if (vport->cursor_shown)
  890.         cursor_hide(vport);
  891.     else
  892.         cursor_show(vport);
  893. }
  894.  
  895.  
  896. /** Draw character at given position relative to viewport
  897.  *
  898.  * @param vport  Viewport identification
  899.  * @param c      Character to draw
  900.  * @param col    Screen position relative to viewport
  901.  * @param row    Screen position relative to viewport
  902.  *
  903.  */
  904. static void draw_char(viewport_t *vport, wchar_t c, unsigned int col, unsigned int row)
  905. {
  906.     bb_cell_t *bbp;
  907.    
  908.     /* Do not hide cursor if we are going to overwrite it */
  909.     if ((vport->cursor_active) && (vport->cursor_shown) &&
  910.         ((vport->cur_col != col) || (vport->cur_row != row)))
  911.         cursor_hide(vport);
  912.    
  913.     bbp = &vport->backbuf[BB_POS(vport, col, row)];
  914.     bbp->glyph = fb_font_glyph(c);
  915.     bbp->fg_color = vport->attr.fg_color;
  916.     bbp->bg_color = vport->attr.bg_color;
  917.    
  918.     draw_vp_glyph(vport, false, col, row);
  919.    
  920.     vport->cur_col = col;
  921.     vport->cur_row = row;
  922.    
  923.     vport->cur_col++;
  924.     if (vport->cur_col >= vport->cols) {
  925.         vport->cur_col = 0;
  926.         vport->cur_row++;
  927.         if (vport->cur_row >= vport->rows)
  928.             vport->cur_row--;
  929.     }
  930.    
  931.     cursor_show(vport);
  932. }
  933.  
  934. /** Draw text data to viewport.
  935.  *
  936.  * @param vport Viewport id
  937.  * @param data  Text data.
  938.  * @param x     Leftmost column of the area.
  939.  * @param y     Topmost row of the area.
  940.  * @param w     Number of rows.
  941.  * @param h     Number of columns.
  942.  *
  943.  */
  944. static void draw_text_data(viewport_t *vport, keyfield_t *data, unsigned int x,
  945.     unsigned int y, unsigned int w, unsigned int h)
  946. {
  947.     unsigned int i;
  948.     unsigned int j;
  949.     bb_cell_t *bbp;
  950.     attrs_t *a;
  951.     attr_rgb_t rgb;
  952.    
  953.     for (j = 0; j < h; j++) {
  954.         for (i = 0; i < w; i++) {
  955.             unsigned int col = x + i;
  956.             unsigned int row = y + j;
  957.            
  958.             bbp = &vport->backbuf[BB_POS(vport, col, row)];
  959.            
  960.             a = &data[j * w + i].attrs;
  961.             rgb_from_attr(&rgb, a);
  962.            
  963.             bbp->glyph = fb_font_glyph(data[j * w + i].character);
  964.             bbp->fg_color = rgb.fg_color;
  965.             bbp->bg_color = rgb.bg_color;
  966.            
  967.             draw_vp_glyph(vport, false, col, row);
  968.         }
  969.     }
  970.     cursor_show(vport);
  971. }
  972.  
  973.  
  974. static void putpixel_pixmap(void *data, unsigned int x, unsigned int y, uint32_t color)
  975. {
  976.     int pm = *((int *) data);
  977.     pixmap_t *pmap = &pixmaps[pm];
  978.     unsigned int pos = (y * pmap->width + x) * screen.pixelbytes;
  979.    
  980.     screen.rgb_conv(&pmap->data[pos], color);
  981. }
  982.  
  983.  
  984. static void putpixel(void *data, unsigned int x, unsigned int y, uint32_t color)
  985. {
  986.     viewport_t *vport = (viewport_t *) data;
  987.     unsigned int dx = vport->x + x;
  988.     unsigned int dy = vport->y + y;
  989.    
  990.     screen.rgb_conv(&screen.fb_addr[FB_POS(dx, dy)], color);
  991. }
  992.  
  993.  
  994. /** Return first free pixmap
  995.  *
  996.  */
  997. static int find_free_pixmap(void)
  998. {
  999.     unsigned int i;
  1000.    
  1001.     for (i = 0; i < MAX_PIXMAPS; i++)
  1002.         if (!pixmaps[i].data)
  1003.             return i;
  1004.    
  1005.     return -1;
  1006. }
  1007.  
  1008.  
  1009. /** Create a new pixmap and return appropriate ID
  1010.  *
  1011.  */
  1012. static int shm2pixmap(unsigned char *shm, size_t size)
  1013. {
  1014.     int pm;
  1015.     pixmap_t *pmap;
  1016.    
  1017.     pm = find_free_pixmap();
  1018.     if (pm == -1)
  1019.         return ELIMIT;
  1020.    
  1021.     pmap = &pixmaps[pm];
  1022.    
  1023.     if (ppm_get_data(shm, size, &pmap->width, &pmap->height))
  1024.         return EINVAL;
  1025.    
  1026.     pmap->data = malloc(pmap->width * pmap->height * screen.pixelbytes);
  1027.     if (!pmap->data)
  1028.         return ENOMEM;
  1029.    
  1030.     ppm_draw(shm, size, 0, 0, pmap->width, pmap->height, putpixel_pixmap, (void *) &pm);
  1031.    
  1032.     return pm;
  1033. }
  1034.  
  1035.  
  1036. /** Handle shared memory communication calls
  1037.  *
  1038.  * Protocol for drawing pixmaps:
  1039.  * - FB_PREPARE_SHM(client shm identification)
  1040.  * - IPC_M_AS_AREA_SEND
  1041.  * - FB_DRAW_PPM(startx, starty)
  1042.  * - FB_DROP_SHM
  1043.  *
  1044.  * Protocol for text drawing
  1045.  * - IPC_M_AS_AREA_SEND
  1046.  * - FB_DRAW_TEXT_DATA
  1047.  *
  1048.  * @param callid Callid of the current call
  1049.  * @param call   Current call data
  1050.  * @param vp     Active viewport
  1051.  *
  1052.  * @return false if the call was not handled byt this function, true otherwise
  1053.  *
  1054.  * Note: this function is not thread-safe, you would have
  1055.  * to redefine static variables with fibril_local.
  1056.  *
  1057.  */
  1058. static bool shm_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
  1059. {
  1060.     static keyfield_t *interbuffer = NULL;
  1061.     static size_t intersize = 0;
  1062.    
  1063.     static unsigned char *shm = NULL;
  1064.     static ipcarg_t shm_id = 0;
  1065.     static size_t shm_size;
  1066.    
  1067.     bool handled = true;
  1068.     int retval = EOK;
  1069.     viewport_t *vport = &viewports[vp];
  1070.     unsigned int x;
  1071.     unsigned int y;
  1072.     unsigned int w;
  1073.     unsigned int h;
  1074.    
  1075.     switch (IPC_GET_METHOD(*call)) {
  1076.     case IPC_M_SHARE_OUT:
  1077.         /* We accept one area for data interchange */
  1078.         if (IPC_GET_ARG1(*call) == shm_id) {
  1079.             void *dest = as_get_mappable_page(IPC_GET_ARG2(*call));
  1080.             shm_size = IPC_GET_ARG2(*call);
  1081.             if (ipc_answer_1(callid, EOK, (sysarg_t) dest)) {
  1082.                 shm_id = 0;
  1083.                 return false;
  1084.             }
  1085.             shm = dest;
  1086.            
  1087.             if (shm[0] != 'P')
  1088.                 return false;
  1089.            
  1090.             return true;
  1091.         } else {
  1092.             intersize = IPC_GET_ARG2(*call);
  1093.             receive_comm_area(callid, call, (void *) &interbuffer);
  1094.         }
  1095.         return true;
  1096.     case FB_PREPARE_SHM:
  1097.         if (shm_id)
  1098.             retval = EBUSY;
  1099.         else
  1100.             shm_id = IPC_GET_ARG1(*call);
  1101.         break;
  1102.        
  1103.     case FB_DROP_SHM:
  1104.         if (shm) {
  1105.             as_area_destroy(shm);
  1106.             shm = NULL;
  1107.         }
  1108.         shm_id = 0;
  1109.         break;
  1110.        
  1111.     case FB_SHM2PIXMAP:
  1112.         if (!shm) {
  1113.             retval = EINVAL;
  1114.             break;
  1115.         }
  1116.         retval = shm2pixmap(shm, shm_size);
  1117.         break;
  1118.     case FB_DRAW_PPM:
  1119.         if (!shm) {
  1120.             retval = EINVAL;
  1121.             break;
  1122.         }
  1123.         x = IPC_GET_ARG1(*call);
  1124.         y = IPC_GET_ARG2(*call);
  1125.        
  1126.         if ((x > vport->width) || (y > vport->height)) {
  1127.             retval = EINVAL;
  1128.             break;
  1129.         }
  1130.        
  1131.         ppm_draw(shm, shm_size, IPC_GET_ARG1(*call),
  1132.             IPC_GET_ARG2(*call), vport->width - x, vport->height - y, putpixel, (void *) vport);
  1133.         break;
  1134.     case FB_DRAW_TEXT_DATA:
  1135.         x = IPC_GET_ARG1(*call);
  1136.         y = IPC_GET_ARG2(*call);
  1137.         w = IPC_GET_ARG3(*call);
  1138.         h = IPC_GET_ARG4(*call);
  1139.         if (!interbuffer) {
  1140.             retval = EINVAL;
  1141.             break;
  1142.         }
  1143.         if (x + w > vport->cols || y + h > vport->rows) {
  1144.             retval = EINVAL;
  1145.             break;
  1146.         }
  1147.         if (intersize < w * h * sizeof(*interbuffer)) {
  1148.             retval = EINVAL;
  1149.             break;
  1150.         }
  1151.         draw_text_data(vport, interbuffer, x, y, w, h);
  1152.         break;
  1153.     default:
  1154.         handled = false;
  1155.     }
  1156.    
  1157.     if (handled)
  1158.         ipc_answer_0(callid, retval);
  1159.     return handled;
  1160. }
  1161.  
  1162.  
  1163. static void copy_vp_to_pixmap(viewport_t *vport, pixmap_t *pmap)
  1164. {
  1165.     unsigned int width = vport->width;
  1166.     unsigned int height = vport->height;
  1167.    
  1168.     if (width + vport->x > screen.xres)
  1169.         width = screen.xres - vport->x;
  1170.     if (height + vport->y > screen.yres)
  1171.         height = screen.yres - vport->y;
  1172.    
  1173.     unsigned int realwidth = pmap->width <= width ? pmap->width : width;
  1174.     unsigned int realheight = pmap->height <= height ? pmap->height : height;
  1175.    
  1176.     unsigned int srcrowsize = vport->width * screen.pixelbytes;
  1177.     unsigned int realrowsize = realwidth * screen.pixelbytes;
  1178.    
  1179.     unsigned int y;
  1180.     for (y = 0; y < realheight; y++) {
  1181.         unsigned int tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
  1182.         memcpy(pmap->data + srcrowsize * y, screen.fb_addr + tmp, realrowsize);
  1183.     }
  1184. }
  1185.  
  1186.  
  1187. /** Save viewport to pixmap
  1188.  *
  1189.  */
  1190. static int save_vp_to_pixmap(viewport_t *vport)
  1191. {
  1192.     int pm;
  1193.     pixmap_t *pmap;
  1194.    
  1195.     pm = find_free_pixmap();
  1196.     if (pm == -1)
  1197.         return ELIMIT;
  1198.    
  1199.     pmap = &pixmaps[pm];
  1200.     pmap->data = malloc(screen.pixelbytes * vport->width * vport->height);
  1201.     if (!pmap->data)
  1202.         return ENOMEM;
  1203.    
  1204.     pmap->width = vport->width;
  1205.     pmap->height = vport->height;
  1206.    
  1207.     copy_vp_to_pixmap(vport, pmap);
  1208.    
  1209.     return pm;
  1210. }
  1211.  
  1212.  
  1213. /** Draw pixmap on screen
  1214.  *
  1215.  * @param vp Viewport to draw on
  1216.  * @param pm Pixmap identifier
  1217.  *
  1218.  */
  1219. static int draw_pixmap(int vp, int pm)
  1220. {
  1221.     pixmap_t *pmap = &pixmaps[pm];
  1222.     viewport_t *vport = &viewports[vp];
  1223.    
  1224.     unsigned int width = vport->width;
  1225.     unsigned int height = vport->height;
  1226.    
  1227.     if (width + vport->x > screen.xres)
  1228.         width = screen.xres - vport->x;
  1229.     if (height + vport->y > screen.yres)
  1230.         height = screen.yres - vport->y;
  1231.    
  1232.     if (!pmap->data)
  1233.         return EINVAL;
  1234.    
  1235.     unsigned int realwidth = pmap->width <= width ? pmap->width : width;
  1236.     unsigned int realheight = pmap->height <= height ? pmap->height : height;
  1237.    
  1238.     unsigned int srcrowsize = vport->width * screen.pixelbytes;
  1239.     unsigned int realrowsize = realwidth * screen.pixelbytes;
  1240.    
  1241.     unsigned int y;
  1242.     for (y = 0; y < realheight; y++) {
  1243.         unsigned int tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
  1244.         memcpy(screen.fb_addr + tmp, pmap->data + y * srcrowsize, realrowsize);
  1245.     }
  1246.    
  1247.     return EOK;
  1248. }
  1249.  
  1250.  
  1251. /** Tick animation one step forward
  1252.  *
  1253.  */
  1254. static void anims_tick(void)
  1255. {
  1256.     unsigned int i;
  1257.     static int counts = 0;
  1258.    
  1259.     /* Limit redrawing */
  1260.     counts = (counts + 1) % 8;
  1261.     if (counts)
  1262.         return;
  1263.    
  1264.     for (i = 0; i < MAX_ANIMATIONS; i++) {
  1265.         if ((!animations[i].animlen) || (!animations[i].initialized) ||
  1266.             (!animations[i].enabled))
  1267.             continue;
  1268.        
  1269.         draw_pixmap(animations[i].vp, animations[i].pixmaps[animations[i].pos]);
  1270.         animations[i].pos = (animations[i].pos + 1) % animations[i].animlen;
  1271.     }
  1272. }
  1273.  
  1274.  
  1275. static unsigned int pointer_x;
  1276. static unsigned int pointer_y;
  1277. static bool pointer_shown, pointer_enabled;
  1278. static int pointer_vport = -1;
  1279. static int pointer_pixmap = -1;
  1280.  
  1281.  
  1282. static void mouse_show(void)
  1283. {
  1284.     int i, j;
  1285.     int visibility;
  1286.     int color;
  1287.     int bytepos;
  1288.    
  1289.     if ((pointer_shown) || (!pointer_enabled))
  1290.         return;
  1291.    
  1292.     /* Save image under the pointer. */
  1293.     if (pointer_vport == -1) {
  1294.         pointer_vport = vport_create(pointer_x, pointer_y, pointer_width, pointer_height);
  1295.         if (pointer_vport < 0)
  1296.             return;
  1297.     } else {
  1298.         viewports[pointer_vport].x = pointer_x;
  1299.         viewports[pointer_vport].y = pointer_y;
  1300.     }
  1301.    
  1302.     if (pointer_pixmap == -1)
  1303.         pointer_pixmap = save_vp_to_pixmap(&viewports[pointer_vport]);
  1304.     else
  1305.         copy_vp_to_pixmap(&viewports[pointer_vport], &pixmaps[pointer_pixmap]);
  1306.    
  1307.     /* Draw mouse pointer. */
  1308.     for (i = 0; i < pointer_height; i++)
  1309.         for (j = 0; j < pointer_width; j++) {
  1310.             bytepos = i * ((pointer_width - 1) / 8 + 1) + j / 8;
  1311.             visibility = pointer_mask_bits[bytepos] &
  1312.                 (1 << (j % 8));
  1313.             if (visibility) {
  1314.                 color = pointer_bits[bytepos] &
  1315.                     (1 << (j % 8)) ? 0 : 0xffffff;
  1316.                 if (pointer_x + j < screen.xres && pointer_y +
  1317.                     i < screen.yres)
  1318.                     putpixel(&viewports[0], pointer_x + j,
  1319.                         pointer_y + i, color);
  1320.             }
  1321.         }
  1322.     pointer_shown = 1;
  1323. }
  1324.  
  1325.  
  1326. static void mouse_hide(void)
  1327. {
  1328.     /* Restore image under the pointer. */
  1329.     if (pointer_shown) {
  1330.         draw_pixmap(pointer_vport, pointer_pixmap);
  1331.         pointer_shown = 0;
  1332.     }
  1333. }
  1334.  
  1335.  
  1336. static void mouse_move(unsigned int x, unsigned int y)
  1337. {
  1338.     mouse_hide();
  1339.     pointer_x = x;
  1340.     pointer_y = y;
  1341.     mouse_show();
  1342. }
  1343.  
  1344.  
  1345. static int anim_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
  1346. {
  1347.     bool handled = true;
  1348.     int retval = EOK;
  1349.     int i, nvp;
  1350.     int newval;
  1351.    
  1352.     switch (IPC_GET_METHOD(*call)) {
  1353.     case FB_ANIM_CREATE:
  1354.         nvp = IPC_GET_ARG1(*call);
  1355.         if (nvp == -1)
  1356.             nvp = vp;
  1357.         if (nvp >= MAX_VIEWPORTS || nvp < 0 ||
  1358.             !viewports[nvp].initialized) {
  1359.             retval = EINVAL;
  1360.             break;
  1361.         }
  1362.         for (i = 0; i < MAX_ANIMATIONS; i++) {
  1363.             if (!animations[i].initialized)
  1364.                 break;
  1365.         }
  1366.         if (i == MAX_ANIMATIONS) {
  1367.             retval = ELIMIT;
  1368.             break;
  1369.         }
  1370.         animations[i].initialized = 1;
  1371.         animations[i].animlen = 0;
  1372.         animations[i].pos = 0;
  1373.         animations[i].enabled = 0;
  1374.         animations[i].vp = nvp;
  1375.         retval = i;
  1376.         break;
  1377.     case FB_ANIM_DROP:
  1378.         i = IPC_GET_ARG1(*call);
  1379.         if (i >= MAX_ANIMATIONS || i < 0) {
  1380.             retval = EINVAL;
  1381.             break;
  1382.         }
  1383.         animations[i].initialized = 0;
  1384.         break;
  1385.     case FB_ANIM_ADDPIXMAP:
  1386.         i = IPC_GET_ARG1(*call);
  1387.         if (i >= MAX_ANIMATIONS || i < 0 ||
  1388.             !animations[i].initialized) {
  1389.             retval = EINVAL;
  1390.             break;
  1391.         }
  1392.         if (animations[i].animlen == MAX_ANIM_LEN) {
  1393.             retval = ELIMIT;
  1394.             break;
  1395.         }
  1396.         newval = IPC_GET_ARG2(*call);
  1397.         if (newval < 0 || newval > MAX_PIXMAPS ||
  1398.             !pixmaps[newval].data) {
  1399.             retval = EINVAL;
  1400.             break;
  1401.         }
  1402.         animations[i].pixmaps[animations[i].animlen++] = newval;
  1403.         break;
  1404.     case FB_ANIM_CHGVP:
  1405.         i = IPC_GET_ARG1(*call);
  1406.         if (i >= MAX_ANIMATIONS || i < 0) {
  1407.             retval = EINVAL;
  1408.             break;
  1409.         }
  1410.         nvp = IPC_GET_ARG2(*call);
  1411.         if (nvp == -1)
  1412.             nvp = vp;
  1413.         if (nvp >= MAX_VIEWPORTS || nvp < 0 ||
  1414.             !viewports[nvp].initialized) {
  1415.             retval = EINVAL;
  1416.             break;
  1417.         }
  1418.         animations[i].vp = nvp;
  1419.         break;
  1420.     case FB_ANIM_START:
  1421.     case FB_ANIM_STOP:
  1422.         i = IPC_GET_ARG1(*call);
  1423.         if (i >= MAX_ANIMATIONS || i < 0) {
  1424.             retval = EINVAL;
  1425.             break;
  1426.         }
  1427.         newval = (IPC_GET_METHOD(*call) == FB_ANIM_START);
  1428.         if (newval ^ animations[i].enabled) {
  1429.             animations[i].enabled = newval;
  1430.             anims_enabled += newval ? 1 : -1;
  1431.         }
  1432.         break;
  1433.     default:
  1434.         handled = 0;
  1435.     }
  1436.     if (handled)
  1437.         ipc_answer_0(callid, retval);
  1438.     return handled;
  1439. }
  1440.  
  1441.  
  1442. /** Handler for messages concerning pixmap handling
  1443.  *
  1444.  */
  1445. static int pixmap_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
  1446. {
  1447.     bool handled = true;
  1448.     int retval = EOK;
  1449.     int i, nvp;
  1450.    
  1451.     switch (IPC_GET_METHOD(*call)) {
  1452.     case FB_VP_DRAW_PIXMAP:
  1453.         nvp = IPC_GET_ARG1(*call);
  1454.         if (nvp == -1)
  1455.             nvp = vp;
  1456.         if (nvp < 0 || nvp >= MAX_VIEWPORTS ||
  1457.             !viewports[nvp].initialized) {
  1458.             retval = EINVAL;
  1459.             break;
  1460.         }
  1461.         i = IPC_GET_ARG2(*call);
  1462.         retval = draw_pixmap(nvp, i);
  1463.         break;
  1464.     case FB_VP2PIXMAP:
  1465.         nvp = IPC_GET_ARG1(*call);
  1466.         if (nvp == -1)
  1467.             nvp = vp;
  1468.         if (nvp < 0 || nvp >= MAX_VIEWPORTS ||
  1469.             !viewports[nvp].initialized)
  1470.             retval = EINVAL;
  1471.         else
  1472.             retval = save_vp_to_pixmap(&viewports[nvp]);
  1473.         break;
  1474.     case FB_DROP_PIXMAP:
  1475.         i = IPC_GET_ARG1(*call);
  1476.         if (i >= MAX_PIXMAPS) {
  1477.             retval = EINVAL;
  1478.             break;
  1479.         }
  1480.         if (pixmaps[i].data) {
  1481.             free(pixmaps[i].data);
  1482.             pixmaps[i].data = NULL;
  1483.         }
  1484.         break;
  1485.     default:
  1486.         handled = 0;
  1487.     }
  1488.    
  1489.     if (handled)
  1490.         ipc_answer_0(callid, retval);
  1491.     return handled;
  1492.    
  1493. }
  1494.  
  1495. static int rgb_from_style(attr_rgb_t *rgb, int style)
  1496. {
  1497.     switch (style) {
  1498.     case STYLE_NORMAL:
  1499.         rgb->fg_color = color_table[COLOR_BLACK];
  1500.         rgb->bg_color = color_table[COLOR_WHITE];
  1501.         break;
  1502.     case STYLE_EMPHASIS:
  1503.         rgb->fg_color = color_table[COLOR_RED];
  1504.         rgb->bg_color = color_table[COLOR_WHITE];
  1505.         break;
  1506.     default:
  1507.         return EINVAL;
  1508.     }
  1509.  
  1510.     return EOK;
  1511. }
  1512.  
  1513. static int rgb_from_idx(attr_rgb_t *rgb, ipcarg_t fg_color,
  1514.     ipcarg_t bg_color, ipcarg_t flags)
  1515. {
  1516.     fg_color = (fg_color & 7) | ((flags & CATTR_BRIGHT) ? 8 : 0);
  1517.     bg_color = (bg_color & 7) | ((flags & CATTR_BRIGHT) ? 8 : 0);
  1518.  
  1519.     rgb->fg_color = color_table[fg_color];
  1520.     rgb->bg_color = color_table[bg_color];
  1521.  
  1522.     return EOK;
  1523. }
  1524.  
  1525. static int rgb_from_attr(attr_rgb_t *rgb, const attrs_t *a)
  1526. {
  1527.     int rc;
  1528.  
  1529.     switch (a->t) {
  1530.     case at_style:
  1531.         rc = rgb_from_style(rgb, a->a.s.style);
  1532.         break;
  1533.     case at_idx:
  1534.         rc = rgb_from_idx(rgb, a->a.i.fg_color,
  1535.             a->a.i.bg_color, a->a.i.flags);
  1536.         break;
  1537.     case at_rgb:
  1538.         *rgb = a->a.r;
  1539.         rc = EOK;
  1540.         break;
  1541.     }
  1542.  
  1543.     return rc;
  1544. }
  1545.  
  1546. static int fb_set_style(viewport_t *vport, ipcarg_t style)
  1547. {
  1548.     return rgb_from_style(&vport->attr, (int) style);
  1549. }
  1550.  
  1551. static int fb_set_color(viewport_t *vport, ipcarg_t fg_color,
  1552.     ipcarg_t bg_color, ipcarg_t flags)
  1553. {
  1554.     return rgb_from_idx(&vport->attr, fg_color, bg_color, flags);
  1555. }
  1556.  
  1557. /** Function for handling connections to FB
  1558.  *
  1559.  */
  1560. static void fb_client_connection(ipc_callid_t iid, ipc_call_t *icall)
  1561. {
  1562.     unsigned int vp = 0;
  1563.     viewport_t *vport = &viewports[vp];
  1564.    
  1565.     if (client_connected) {
  1566.         ipc_answer_0(iid, ELIMIT);
  1567.         return;
  1568.     }
  1569.    
  1570.     /* Accept connection */
  1571.     client_connected = true;
  1572.     ipc_answer_0(iid, EOK);
  1573.    
  1574.     while (true) {
  1575.         ipc_callid_t callid;
  1576.         ipc_call_t call;
  1577.         int retval;
  1578.         unsigned int i;
  1579.         int scroll;
  1580.         wchar_t ch;
  1581.         unsigned int col, row;
  1582.        
  1583.         if ((vport->cursor_active) || (anims_enabled))
  1584.             callid = async_get_call_timeout(&call, 250000);
  1585.         else
  1586.             callid = async_get_call(&call);
  1587.        
  1588.         mouse_hide();
  1589.         if (!callid) {
  1590.             cursor_blink(vport);
  1591.             anims_tick();
  1592.             mouse_show();
  1593.             continue;
  1594.         }
  1595.        
  1596.         if (shm_handle(callid, &call, vp))
  1597.             continue;
  1598.        
  1599.         if (pixmap_handle(callid, &call, vp))
  1600.             continue;
  1601.        
  1602.         if (anim_handle(callid, &call, vp))
  1603.             continue;
  1604.        
  1605.         switch (IPC_GET_METHOD(call)) {
  1606.         case IPC_M_PHONE_HUNGUP:
  1607.             client_connected = false;
  1608.            
  1609.             /* Cleanup other viewports */
  1610.             for (i = 1; i < MAX_VIEWPORTS; i++)
  1611.                 vport->initialized = false;
  1612.            
  1613.             /* Exit thread */
  1614.             return;
  1615.        
  1616.         case FB_PUTCHAR:
  1617.             ch = IPC_GET_ARG1(call);
  1618.             col = IPC_GET_ARG2(call);
  1619.             row = IPC_GET_ARG3(call);
  1620.            
  1621.             if ((col >= vport->cols) || (row >= vport->rows)) {
  1622.                 retval = EINVAL;
  1623.                 break;
  1624.             }
  1625.             ipc_answer_0(callid, EOK);
  1626.            
  1627.             draw_char(vport, ch, col, row);
  1628.            
  1629.             /* Message already answered */
  1630.             continue;
  1631.         case FB_CLEAR:
  1632.             vport_clear(vport);
  1633.             cursor_show(vport);
  1634.             retval = EOK;
  1635.             break;
  1636.         case FB_CURSOR_GOTO:
  1637.             col = IPC_GET_ARG1(call);
  1638.             row = IPC_GET_ARG2(call);
  1639.            
  1640.             if ((col >= vport->cols) || (row >= vport->rows)) {
  1641.                 retval = EINVAL;
  1642.                 break;
  1643.             }
  1644.             retval = EOK;
  1645.            
  1646.             cursor_hide(vport);
  1647.             vport->cur_col = col;
  1648.             vport->cur_row = row;
  1649.             cursor_show(vport);
  1650.             break;
  1651.         case FB_CURSOR_VISIBILITY:
  1652.             cursor_hide(vport);
  1653.             vport->cursor_active = IPC_GET_ARG1(call);
  1654.             cursor_show(vport);
  1655.             retval = EOK;
  1656.             break;
  1657.         case FB_GET_CSIZE:
  1658.             ipc_answer_2(callid, EOK, vport->cols, vport->rows);
  1659.             continue;
  1660.         case FB_GET_COLOR_CAP:
  1661.             ipc_answer_1(callid, EOK, FB_CCAP_RGB);
  1662.             continue;
  1663.         case FB_SCROLL:
  1664.             scroll = IPC_GET_ARG1(call);
  1665.             if ((scroll > (int) vport->rows) || (scroll < (-(int) vport->rows))) {
  1666.                 retval = EINVAL;
  1667.                 break;
  1668.             }
  1669.             cursor_hide(vport);
  1670.             vport_scroll(vport, scroll);
  1671.             cursor_show(vport);
  1672.             retval = EOK;
  1673.             break;
  1674.         case FB_VIEWPORT_SWITCH:
  1675.             i = IPC_GET_ARG1(call);
  1676.             if (i >= MAX_VIEWPORTS) {
  1677.                 retval = EINVAL;
  1678.                 break;
  1679.             }
  1680.             if (!viewports[i].initialized) {
  1681.                 retval = EADDRNOTAVAIL;
  1682.                 break;
  1683.             }
  1684.             cursor_hide(vport);
  1685.             vp = i;
  1686.             vport = &viewports[vp];
  1687.             cursor_show(vport);
  1688.             retval = EOK;
  1689.             break;
  1690.         case FB_VIEWPORT_CREATE:
  1691.             retval = vport_create(IPC_GET_ARG1(call) >> 16,
  1692.                 IPC_GET_ARG1(call) & 0xffff,
  1693.                 IPC_GET_ARG2(call) >> 16,
  1694.                 IPC_GET_ARG2(call) & 0xffff);
  1695.             break;
  1696.         case FB_VIEWPORT_DELETE:
  1697.             i = IPC_GET_ARG1(call);
  1698.             if (i >= MAX_VIEWPORTS) {
  1699.                 retval = EINVAL;
  1700.                 break;
  1701.             }
  1702.             if (!viewports[i].initialized) {
  1703.                 retval = EADDRNOTAVAIL;
  1704.                 break;
  1705.             }
  1706.             viewports[i].initialized = false;
  1707.             if (viewports[i].bgpixel)
  1708.                 free(viewports[i].bgpixel);
  1709.             if (viewports[i].backbuf)
  1710.                 free(viewports[i].backbuf);
  1711.             retval = EOK;
  1712.             break;
  1713.         case FB_SET_STYLE:
  1714.             retval = fb_set_style(vport, IPC_GET_ARG1(call));
  1715.             break;
  1716.         case FB_SET_COLOR:
  1717.             retval = fb_set_color(vport, IPC_GET_ARG1(call),
  1718.                 IPC_GET_ARG2(call), IPC_GET_ARG3(call));
  1719.             break;
  1720.         case FB_SET_RGB_COLOR:
  1721.             vport->attr.fg_color = IPC_GET_ARG1(call);
  1722.             vport->attr.bg_color = IPC_GET_ARG2(call);
  1723.             retval = EOK;
  1724.             break;
  1725.         case FB_GET_RESOLUTION:
  1726.             ipc_answer_2(callid, EOK, screen.xres, screen.yres);
  1727.             continue;
  1728.         case FB_POINTER_MOVE:
  1729.             pointer_enabled = true;
  1730.             mouse_move(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
  1731.             retval = EOK;
  1732.             break;
  1733.         case FB_SCREEN_YIELD:
  1734.         case FB_SCREEN_RECLAIM:
  1735.             retval = EOK;
  1736.             break;
  1737.         default:
  1738.             retval = ENOENT;
  1739.         }
  1740.         ipc_answer_0(callid, retval);
  1741.     }
  1742. }
  1743.  
  1744. /** Initialization of framebuffer
  1745.  *
  1746.  */
  1747. int fb_init(void)
  1748. {
  1749.     async_set_client_connection(fb_client_connection);
  1750.    
  1751.     void *fb_ph_addr = (void *) sysinfo_value("fb.address.physical");
  1752.     unsigned int fb_offset = sysinfo_value("fb.offset");
  1753.     unsigned int fb_width = sysinfo_value("fb.width");
  1754.     unsigned int fb_height = sysinfo_value("fb.height");
  1755.     unsigned int fb_scanline = sysinfo_value("fb.scanline");
  1756.     unsigned int fb_visual = sysinfo_value("fb.visual");
  1757.  
  1758.     unsigned int fbsize = fb_scanline * fb_height;
  1759.     void *fb_addr = as_get_mappable_page(fbsize);
  1760.  
  1761.     if (physmem_map(fb_ph_addr + fb_offset, fb_addr,
  1762.         ALIGN_UP(fbsize, PAGE_SIZE) >> PAGE_WIDTH, AS_AREA_READ | AS_AREA_WRITE) != 0)
  1763.         return -1;
  1764.  
  1765.     if (screen_init(fb_addr, fb_width, fb_height, fb_scanline, fb_visual))
  1766.         return 0;
  1767.  
  1768.     return -1;
  1769. }
  1770.  
  1771. /**
  1772.  * @}
  1773.  */
  1774.