Subversion Repositories HelenOS

Rev

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