Subversion Repositories HelenOS

Rev

Rev 1954 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (C) 2006 Jakub Vana
  3.  * Copyright (C) 2006 Ondrej Palkovsky
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  * - Redistributions in binary form must reproduce the above copyright
  13.  *   notice, this list of conditions and the following disclaimer in the
  14.  *   documentation and/or other materials provided with the distribution.
  15.  * - The name of the author may not be used to endorse or promote products
  16.  *   derived from this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28.  */
  29.  
  30. /**
  31.  * @defgroup fb Graphical framebuffer
  32.  * @brief   HelenOS graphical framebuffer.
  33.  * @ingroup fbs
  34.  * @{
  35.  */
  36.  
  37. /** @file
  38.  */
  39.  
  40. #include <stdlib.h>
  41. #include <unistd.h>
  42. #include <string.h>
  43. #include <ddi.h>
  44. #include <sysinfo.h>
  45. #include <align.h>
  46. #include <as.h>
  47. #include <ipc/fb.h>
  48. #include <ipc/ipc.h>
  49. #include <ipc/ns.h>
  50. #include <ipc/services.h>
  51. #include <kernel/errno.h>
  52. #include <kernel/genarch/fb/visuals.h>
  53. #include <async.h>
  54. #include <bool.h>
  55.  
  56. #include "font-8x16.h"
  57. #include "fb.h"
  58. #include "main.h"
  59. #include "../console/screenbuffer.h"
  60. #include "ppm.h"
  61.  
  62. #include "pointer.xbm"
  63. #include "pointer_mask.xbm"
  64.  
  65. #define DEFAULT_BGCOLOR                0xf0f0f0
  66. #define DEFAULT_FGCOLOR                0x0
  67.  
  68. /***************************************************************/
  69. /* Pixel specific fuctions */
  70.  
  71. typedef void (*conv2scr_fn_t)(void *, int);
  72. typedef int (*conv2rgb_fn_t)(void *);
  73.  
  74. struct {
  75.     uint8_t *fbaddress;
  76.  
  77.     unsigned int xres;
  78.     unsigned int yres;
  79.     unsigned int scanline;
  80.     unsigned int pixelbytes;
  81.     unsigned int invert_colors;
  82.  
  83.     conv2scr_fn_t rgb2scr;
  84.     conv2rgb_fn_t scr2rgb;
  85. } screen;
  86.  
  87. typedef struct {
  88.     int initialized;
  89.     unsigned int x, y;
  90.     unsigned int width, height;
  91.  
  92.     /* Text support in window */
  93.     unsigned int rows, cols;
  94.     /* Style for text printing */
  95.     style_t style;
  96.     /* Auto-cursor position */
  97.     int cursor_active, cur_col, cur_row;
  98.     int cursor_shown;
  99.     /* Double buffering */
  100.     uint8_t *dbdata;
  101.     unsigned int dboffset;
  102.     unsigned int paused;
  103. } viewport_t;
  104.  
  105. #define MAX_ANIM_LEN    8
  106. #define MAX_ANIMATIONS  4
  107. typedef struct {
  108.     int initialized;
  109.     int enabled;
  110.     unsigned int vp;
  111.  
  112.     unsigned int pos;
  113.     unsigned int animlen;
  114.     unsigned int pixmaps[MAX_ANIM_LEN];
  115. } animation_t;
  116. static animation_t animations[MAX_ANIMATIONS];
  117. static int anims_enabled;
  118.  
  119. /** Maximum number of saved pixmaps
  120.  * Pixmap is a saved rectangle
  121.  */
  122. #define MAX_PIXMAPS        256
  123. typedef struct {
  124.     unsigned int width;
  125.     unsigned int height;
  126.     uint8_t *data;
  127. } pixmap_t;
  128. static pixmap_t pixmaps[MAX_PIXMAPS];
  129.  
  130. /* Viewport is a rectangular area on the screen */
  131. #define MAX_VIEWPORTS 128
  132. static viewport_t viewports[128];
  133.  
  134. /* Allow only 1 connection */
  135. static int client_connected = 0;
  136.  
  137. #define RED(x, bits)    ((x >> (16 + 8 - bits)) & ((1 << bits) - 1))
  138. #define GREEN(x, bits)  ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
  139. #define BLUE(x, bits)   ((x >> (8 - bits)) & ((1 << bits) - 1))
  140.  
  141. #define COL_WIDTH   8
  142. #define ROW_BYTES   (screen.scanline * FONT_SCANLINES)
  143.  
  144. #define POINTPOS(x, y)  ((y) * screen.scanline + (x) * screen.pixelbytes)
  145.  
  146. static inline int COLOR(int color)
  147. {
  148.     return screen.invert_colors ? ~color : color;
  149. }
  150.  
  151. /* Conversion routines between different color representations */
  152. static void rgb_byte0888(void *dst, int rgb)
  153. {
  154.     *(int *)dst = rgb;
  155. }
  156.  
  157. static int byte0888_rgb(void *src)
  158. {
  159.     return (*(int *)src) & 0xffffff;
  160. }
  161.  
  162. static void rgb_byte888(void *dst, int rgb)
  163. {
  164.     uint8_t *scr = dst;
  165. #if defined(FB_INVERT_ENDIAN)
  166.     scr[0] = RED(rgb, 8);
  167.     scr[1] = GREEN(rgb, 8);
  168.     scr[2] = BLUE(rgb, 8);
  169. #else
  170.     scr[2] = RED(rgb, 8);
  171.     scr[1] = GREEN(rgb, 8);
  172.     scr[0] = BLUE(rgb, 8);
  173. #endif
  174. }
  175.  
  176. static int byte888_rgb(void *src)
  177. {
  178.     uint8_t *scr = src;
  179. #if defined(FB_INVERT_ENDIAN)
  180.     return scr[0] << 16 | scr[1] << 8 | scr[2];
  181. #else
  182.     return scr[2] << 16 | scr[1] << 8 | scr[0];
  183. #endif 
  184. }
  185.  
  186. /**  16-bit depth (5:5:5) */
  187. static void rgb_byte555(void *dst, int rgb)
  188. {
  189.     /* 5-bit, 5-bits, 5-bits */
  190.     *((uint16_t *)(dst)) = RED(rgb, 5) << 10 | GREEN(rgb, 5) << 5 | BLUE(rgb, 5);
  191. }
  192.  
  193. /** 16-bit depth (5:5:5) */
  194. static int byte555_rgb(void *src)
  195. {
  196.     int color = *(uint16_t *)(src);
  197.     return (((color >> 10) & 0x1f) << (16 + 3)) | (((color >> 5) & 0x1f) << (8 + 3)) | ((color & 0x1f) << 3);
  198. }
  199.  
  200. /**  16-bit depth (5:6:5) */
  201. static void rgb_byte565(void *dst, int rgb)
  202. {
  203.     /* 5-bit, 6-bits, 5-bits */
  204.     *((uint16_t *)(dst)) = RED(rgb, 5) << 11 | GREEN(rgb, 6) << 5 | BLUE(rgb, 5);
  205. }
  206.  
  207. /** 16-bit depth (5:6:5) */
  208. static int byte565_rgb(void *src)
  209. {
  210.     int color = *(uint16_t *)(src);
  211.     return (((color >> 11) & 0x1f) << (16 + 3)) | (((color >> 5) & 0x3f) << (8 + 2)) | ((color & 0x1f) << 3);
  212. }
  213.  
  214. /** Put pixel - 8-bit depth (3:2:3) */
  215. static void rgb_byte8(void *dst, int rgb)
  216. {
  217.     *(uint8_t *)dst = RED(rgb, 3) << 5 | GREEN(rgb, 2) << 3 | BLUE(rgb, 3);
  218. }
  219.  
  220. /** Return pixel color - 8-bit depth (3:2:3) */
  221. static int byte8_rgb(void *src)
  222. {
  223.     int color = *(uint8_t *)src;
  224.     return (((color >> 5) & 0x7) << (16 + 5)) | (((color >> 3) & 0x3) << (8 + 6)) | ((color & 0x7) << 5);
  225. }
  226.  
  227. /** Put pixel into viewport
  228.  *
  229.  * @param vport Viewport identification
  230.  * @param x X coord relative to viewport
  231.  * @param y Y coord relative to viewport
  232.  * @param color RGB color
  233.  */
  234. static void putpixel(viewport_t *vport, unsigned int x, unsigned int y, int color)
  235. {
  236.     int dx = vport->x + x;
  237.     int dy = vport->y + y;
  238.  
  239.     if (! (vport->paused && vport->dbdata))
  240.         (*screen.rgb2scr)(&screen.fbaddress[POINTPOS(dx,dy)], COLOR(color));
  241.  
  242.     if (vport->dbdata) {
  243.         int dline = (y + vport->dboffset) % vport->height;
  244.         int doffset = screen.pixelbytes * (dline * vport->width + x);
  245.         (*screen.rgb2scr)(&vport->dbdata[doffset], COLOR(color));
  246.     }
  247. }
  248.  
  249. /** Get pixel from viewport */
  250. static int getpixel(viewport_t *vport, unsigned int x, unsigned int y)
  251. {
  252.     int dx = vport->x + x;
  253.     int dy = vport->y + y;
  254.  
  255.     return COLOR((*screen.scr2rgb)(&screen.fbaddress[POINTPOS(dx,dy)]));
  256. }
  257.  
  258. static inline void putpixel_mem(char *mem, unsigned int x, unsigned int y,
  259.                 int color)
  260. {
  261.     (*screen.rgb2scr)(&mem[POINTPOS(x,y)], COLOR(color));
  262. }
  263.  
  264. static void draw_rectangle(viewport_t *vport, unsigned int sx, unsigned int sy,
  265.                unsigned int width, unsigned int height,
  266.                int color)
  267. {
  268.     unsigned int x, y;
  269.     static void *tmpline;
  270.  
  271.     if (!tmpline)
  272.         tmpline = malloc(screen.scanline*screen.pixelbytes);
  273.  
  274.     /* Clear first line */
  275.     for (x = 0; x < width; x++)
  276.         putpixel_mem(tmpline, x, 0, color);
  277.  
  278.     if (!vport->paused) {
  279.         /* Recompute to screen coords */
  280.         sx += vport->x;
  281.         sy += vport->y;
  282.         /* Copy the rest */
  283.         for (y = sy;y < sy+height; y++)
  284.             memcpy(&screen.fbaddress[POINTPOS(sx,y)], tmpline,
  285.                    screen.pixelbytes * width);
  286.     }
  287.     if (vport->dbdata) {
  288.         for (y=sy;y < sy+height; y++) {
  289.             int rline = (y + vport->dboffset) % vport->height;
  290.             int rpos = (rline * vport->width + sx) * screen.pixelbytes;
  291.             memcpy(&vport->dbdata[rpos], tmpline, screen.pixelbytes * width);
  292.         }
  293.     }
  294.  
  295. }
  296.  
  297. /** Fill viewport with background color */
  298. static void clear_port(viewport_t *vport)
  299. {
  300.     draw_rectangle(vport, 0, 0, vport->width, vport->height, vport->style.bg_color);
  301. }
  302.  
  303. /** Scroll unbuffered viewport up/down
  304.  *
  305.  * @param vport Viewport to scroll
  306.  * @param lines Positive number - scroll up, negative - scroll down
  307.  */
  308. static void scroll_port_nodb(viewport_t *vport, int lines)
  309. {
  310.     int y;
  311.  
  312.     if (lines > 0) {
  313.         for (y=vport->y; y < vport->y+vport->height - lines; y++)
  314.             memcpy(&screen.fbaddress[POINTPOS(vport->x,y)],
  315.                    &screen.fbaddress[POINTPOS(vport->x,y + lines)],
  316.                    screen.pixelbytes * vport->width);
  317.         draw_rectangle(vport, 0, vport->height - lines,
  318.                    vport->width, lines, vport->style.bg_color);
  319.     } else if (lines < 0) {
  320.         lines = -lines;
  321.         for (y=vport->y + vport->height-1; y >= vport->y + lines; y--)
  322.             memcpy(&screen.fbaddress[POINTPOS(vport->x,y)],
  323.                    &screen.fbaddress[POINTPOS(vport->x,y - lines)],
  324.                    screen.pixelbytes * vport->width);
  325.         draw_rectangle(vport, 0, 0, vport->width, lines, vport->style.bg_color);
  326.     }
  327. }
  328.  
  329. /** Refresh given viewport from double buffer */
  330. static void refresh_viewport_db(viewport_t *vport)
  331. {
  332.     unsigned int y, srcy, srcoff, dsty, dstx;
  333.  
  334.     for (y = 0; y < vport->height; y++) {
  335.         srcy = (y + vport->dboffset) % vport->height;
  336.         srcoff = (vport->width * srcy) * screen.pixelbytes;
  337.  
  338.         dstx = vport->x;
  339.         dsty = vport->y + y;
  340.  
  341.         memcpy(&screen.fbaddress[POINTPOS(dstx,dsty)],
  342.                &vport->dbdata[srcoff],
  343.                vport->width*screen.pixelbytes);
  344.     }
  345. }
  346.  
  347. /** Scroll viewport that has double buffering enabled */
  348. static void scroll_port_db(viewport_t *vport, int lines)
  349. {
  350.     ++vport->paused;
  351.     if (lines > 0) {
  352.         draw_rectangle(vport, 0, 0, vport->width, lines,
  353.                    vport->style.bg_color);
  354.         vport->dboffset += lines;
  355.         vport->dboffset %= vport->height;
  356.     } else if (lines < 0) {
  357.         lines = -lines;
  358.         draw_rectangle(vport, 0, vport->height-lines,
  359.                    vport->width, lines,
  360.                    vport->style.bg_color);
  361.  
  362.         if (vport->dboffset < lines)
  363.             vport->dboffset += vport->height;
  364.         vport->dboffset -= lines;
  365.     }
  366.    
  367.     --vport->paused;
  368.    
  369.     refresh_viewport_db(vport);
  370. }
  371.  
  372. /** Scrolls viewport given number of lines */
  373. static void scroll_port(viewport_t *vport, int lines)
  374. {
  375.     if (vport->dbdata)
  376.         scroll_port_db(vport, lines);
  377.     else
  378.         scroll_port_nodb(vport, lines);
  379.    
  380. }
  381.  
  382. static void invert_pixel(viewport_t *vport, unsigned int x, unsigned int y)
  383. {
  384.     putpixel(vport, x, y, ~getpixel(vport, x, y));
  385. }
  386.  
  387.  
  388. /***************************************************************/
  389. /* Character-console functions */
  390.  
  391. /** Draw character at given position
  392.  *
  393.  * @param vport Viewport where the character is printed
  394.  * @param sx Coordinates of top-left of the character
  395.  * @param sy Coordinates of top-left of the character
  396.  * @param style Color of the character
  397.  * @param transparent If false, print background color
  398.  */
  399. static void draw_glyph(viewport_t *vport,uint8_t glyph, unsigned int sx, unsigned int sy,
  400.                style_t style, int transparent)
  401. {
  402.     int i;
  403.     unsigned int y;
  404.     unsigned int glline;
  405.  
  406.     for (y = 0; y < FONT_SCANLINES; y++) {
  407.         glline = fb_font[glyph * FONT_SCANLINES + y];
  408.         for (i = 0; i < 8; i++) {
  409.             if (glline & (1 << (7 - i)))
  410.                 putpixel(vport, sx + i, sy + y, style.fg_color);
  411.             else if (!transparent)
  412.                 putpixel(vport, sx + i, sy + y, style.bg_color);
  413.         }
  414.     }
  415. }
  416.  
  417. /** Invert character at given position */
  418. static void invert_char(viewport_t *vport,unsigned int row, unsigned int col)
  419. {
  420.     unsigned int x;
  421.     unsigned int y;
  422.  
  423.     for (x = 0; x < COL_WIDTH; x++)
  424.         for (y = 0; y < FONT_SCANLINES; y++)
  425.             invert_pixel(vport, col * COL_WIDTH + x, row * FONT_SCANLINES + y);
  426. }
  427.  
  428. /***************************************************************/
  429. /* Stdout specific functions */
  430.  
  431.  
  432. /** Create new viewport
  433.  *
  434.  * @return New viewport number
  435.  */
  436. static int viewport_create(unsigned int x, unsigned int y,unsigned int width,
  437.                unsigned int height)
  438. {
  439.     int i;
  440.  
  441.     for (i=0; i < MAX_VIEWPORTS; i++) {
  442.         if (!viewports[i].initialized)
  443.             break;
  444.     }
  445.     if (i == MAX_VIEWPORTS)
  446.         return ELIMIT;
  447.  
  448.     viewports[i].x = x;
  449.     viewports[i].y = y;
  450.     viewports[i].width = width;
  451.     viewports[i].height = height;
  452.    
  453.     viewports[i].rows = height / FONT_SCANLINES;
  454.     viewports[i].cols = width / COL_WIDTH;
  455.  
  456.     viewports[i].style.bg_color = DEFAULT_BGCOLOR;
  457.     viewports[i].style.fg_color = DEFAULT_FGCOLOR;
  458.    
  459.     viewports[i].cur_col = 0;
  460.     viewports[i].cur_row = 0;
  461.     viewports[i].cursor_active = 0;
  462.  
  463.     viewports[i].initialized = 1;
  464.  
  465.     return i;
  466. }
  467.  
  468.  
  469. /** Initialize framebuffer as a chardev output device
  470.  *
  471.  * @param addr          Address of theframebuffer
  472.  * @param xres          Screen width in pixels
  473.  * @param yres          Screen height in pixels
  474.  * @param visual        Bits per pixel (8, 16, 24, 32)
  475.  * @param scan          Bytes per one scanline
  476.  * @param invert_colors Inverted colors.
  477.  *
  478.  */
  479. static bool screen_init(void *addr, unsigned int xres, unsigned int yres, unsigned int scan, unsigned int visual, bool invert_colors)
  480. {
  481.     switch (visual) {
  482.     case VISUAL_INDIRECT_8:
  483.         screen.rgb2scr = rgb_byte8;
  484.         screen.scr2rgb = byte8_rgb;
  485.         screen.pixelbytes = 1;
  486.         break;
  487.     case VISUAL_RGB_5_5_5:
  488.         screen.rgb2scr = rgb_byte555;
  489.         screen.scr2rgb = byte555_rgb;
  490.         screen.pixelbytes = 2;
  491.         break;
  492.     case VISUAL_RGB_5_6_5:
  493.         screen.rgb2scr = rgb_byte565;
  494.         screen.scr2rgb = byte565_rgb;
  495.         screen.pixelbytes = 2;
  496.         break;
  497.     case VISUAL_RGB_8_8_8:
  498.         screen.rgb2scr = rgb_byte888;
  499.         screen.scr2rgb = byte888_rgb;
  500.         screen.pixelbytes = 3;
  501.         break;
  502.     case VISUAL_RGB_8_8_8_0:
  503.         screen.rgb2scr = rgb_byte888;
  504.         screen.scr2rgb = byte888_rgb;
  505.         screen.pixelbytes = 4;
  506.         break;
  507.     case VISUAL_RGB_0_8_8_8:
  508.         screen.rgb2scr = rgb_byte0888;
  509.         screen.scr2rgb = byte0888_rgb;
  510.         screen.pixelbytes = 4;
  511.         break;
  512.     default:
  513.         return false;
  514.     }
  515.  
  516.     screen.fbaddress = (unsigned char *) addr;
  517.     screen.xres = xres;
  518.     screen.yres = yres;
  519.     screen.scanline = scan;
  520.     screen.invert_colors = invert_colors;
  521.    
  522.     /* Create first viewport */
  523.     viewport_create(0, 0, xres, yres);
  524.    
  525.     return true;
  526. }
  527.  
  528. /** Hide cursor if it is shown */
  529. static void cursor_hide(viewport_t *vport)
  530. {
  531.     if (vport->cursor_active && vport->cursor_shown) {
  532.         invert_char(vport, vport->cur_row, vport->cur_col);
  533.         vport->cursor_shown = 0;
  534.     }
  535. }
  536.  
  537. /** Show cursor if cursor showing is enabled */
  538. static void cursor_print(viewport_t *vport)
  539. {
  540.     /* Do not check for cursor_shown */
  541.     if (vport->cursor_active) {
  542.         invert_char(vport, vport->cur_row, vport->cur_col);
  543.         vport->cursor_shown = 1;
  544.     }
  545. }
  546.  
  547. /** Invert cursor, if it is enabled */
  548. static void cursor_blink(viewport_t *vport)
  549. {
  550.     if (vport->cursor_shown)
  551.         cursor_hide(vport);
  552.     else
  553.         cursor_print(vport);
  554. }
  555.  
  556. /** Draw character at given position relative to viewport
  557.  *
  558.  * @param vport Viewport identification
  559.  * @param c Character to print
  560.  * @param row Screen position relative to viewport
  561.  * @param col Screen position relative to viewport
  562.  * @param transparent If false, print background color with character
  563.  */
  564. static void draw_char(viewport_t *vport, char c, unsigned int row, unsigned int col,
  565.               style_t style, int transparent)
  566. {
  567.     /* Optimize - do not hide cursor if we are going to overwrite it */
  568.     if (vport->cursor_active && vport->cursor_shown &&
  569.         (vport->cur_col != col || vport->cur_row != row))
  570.         invert_char(vport, vport->cur_row, vport->cur_col);
  571.    
  572.     draw_glyph(vport, c, col * COL_WIDTH, row * FONT_SCANLINES, style, transparent);
  573.  
  574.     vport->cur_col = col;
  575.     vport->cur_row = row;
  576.  
  577.     vport->cur_col++;
  578.     if (vport->cur_col>= vport->cols) {
  579.         vport->cur_col = 0;
  580.         vport->cur_row++;
  581.         if (vport->cur_row >= vport->rows)
  582.             vport->cur_row--;
  583.     }
  584.     cursor_print(vport);
  585. }
  586.  
  587. /** Draw text data to viewport
  588.  *
  589.  * @param vport Viewport id
  590.  * @param data Text data fitting exactly into viewport
  591.  */
  592. static void draw_text_data(viewport_t *vport, keyfield_t *data)
  593. {
  594.     int i;
  595.     int col,row;
  596.  
  597.     clear_port(vport);
  598.     for (i=0; i < vport->cols * vport->rows; i++) {
  599.         if (data[i].character == ' ' && style_same(data[i].style,vport->style))
  600.             continue;
  601.         col = i % vport->cols;
  602.         row = i / vport->cols;
  603.         draw_glyph(vport, data[i].character, col * COL_WIDTH, row * FONT_SCANLINES,
  604.                data[i].style, style_same(data[i].style,vport->style));
  605.     }
  606.     cursor_print(vport);
  607. }
  608.  
  609.  
  610. /** Return first free pixmap */
  611. static int find_free_pixmap(void)
  612. {
  613.     int i;
  614.    
  615.     for (i=0;i < MAX_PIXMAPS;i++)
  616.         if (!pixmaps[i].data)
  617.             return i;
  618.     return -1;
  619. }
  620.  
  621. static void putpixel_pixmap(int pm, unsigned int x, unsigned int y, int color)
  622. {
  623.     pixmap_t *pmap = &pixmaps[pm];
  624.     int pos = (y * pmap->width + x) * screen.pixelbytes;
  625.  
  626.     (*screen.rgb2scr)(&pmap->data[pos],COLOR(color));
  627. }
  628.  
  629. /** Create a new pixmap and return appropriate ID */
  630. static int shm2pixmap(unsigned char *shm, size_t size)
  631. {
  632.     int pm;
  633.     pixmap_t *pmap;
  634.  
  635.     pm = find_free_pixmap();
  636.     if (pm == -1)
  637.         return ELIMIT;
  638.     pmap = &pixmaps[pm];
  639.    
  640.     if (ppm_get_data(shm, size, &pmap->width, &pmap->height))
  641.         return EINVAL;
  642.    
  643.     pmap->data = malloc(pmap->width * pmap->height * screen.pixelbytes);
  644.     if (!pmap->data)
  645.         return ENOMEM;
  646.  
  647.     ppm_draw(shm, size, 0, 0, pmap->width, pmap->height,
  648.          (putpixel_cb_t)putpixel_pixmap, (void *)pm);
  649.  
  650.     return pm;
  651. }
  652.  
  653. /** Handle shared memory communication calls
  654.  *
  655.  * Protocol for drawing pixmaps:
  656.  * - FB_PREPARE_SHM(client shm identification)
  657.  * - IPC_M_SEND_AS_AREA
  658.  * - FB_DRAW_PPM(startx,starty)
  659.  * - FB_DROP_SHM
  660.  *
  661.  * Protocol for text drawing
  662.  * - IPC_M_SEND_AS_AREA
  663.  * - FB_DRAW_TEXT_DATA
  664.  *
  665.  * @param callid Callid of the current call
  666.  * @param call Current call data
  667.  * @param vp Active viewport
  668.  * @return 0 if the call was not handled byt this function, 1 otherwise
  669.  *
  670.  * note: this function is not threads safe, you would have
  671.  * to redefine static variables with __thread
  672.  */
  673. static int shm_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
  674. {
  675.     static keyfield_t *interbuffer = NULL;
  676.     static size_t intersize = 0;
  677.  
  678.     static unsigned char *shm = NULL;
  679.     static ipcarg_t shm_id = 0;
  680.     static size_t shm_size;
  681.  
  682.     int handled = 1;
  683.     int retval = 0;
  684.     viewport_t *vport = &viewports[vp];
  685.     unsigned int x,y;
  686.  
  687.     switch (IPC_GET_METHOD(*call)) {
  688.     case IPC_M_AS_AREA_SEND:
  689.         /* We accept one area for data interchange */
  690.         if (IPC_GET_ARG1(*call) == shm_id) {
  691.             void *dest = as_get_mappable_page(IPC_GET_ARG2(*call));
  692.             shm_size = IPC_GET_ARG2(*call);
  693.             if (!ipc_answer_fast(callid, 0, (sysarg_t)dest, 0))
  694.                 shm = dest;
  695.             else
  696.                 shm_id = 0;
  697.             if (shm[0] != 'P')
  698.                 while (1)
  699.                     ;
  700.             return 1;
  701.         } else {
  702.             intersize = IPC_GET_ARG2(*call);
  703.             receive_comm_area(callid,call,(void *)&interbuffer);
  704.         }
  705.         return 1;
  706.     case FB_PREPARE_SHM:
  707.         if (shm_id)
  708.             retval = EBUSY;
  709.         else
  710.             shm_id = IPC_GET_ARG1(*call);
  711.         break;
  712.        
  713.     case FB_DROP_SHM:
  714.         if (shm) {
  715.             as_area_destroy(shm);
  716.             shm = NULL;
  717.         }
  718.         shm_id = 0;
  719.         break;
  720.  
  721.     case FB_SHM2PIXMAP:
  722.         if (!shm) {
  723.             retval = EINVAL;
  724.             break;
  725.         }
  726.         retval = shm2pixmap(shm, shm_size);
  727.         break;
  728.     case FB_DRAW_PPM:
  729.         if (!shm) {
  730.             retval = EINVAL;
  731.             break;
  732.         }
  733.         x = IPC_GET_ARG1(*call);
  734.         y = IPC_GET_ARG2(*call);
  735.         if (x > vport->width || y > vport->height) {
  736.             retval = EINVAL;
  737.             break;
  738.         }
  739.        
  740.         ppm_draw(shm, shm_size, IPC_GET_ARG1(*call), IPC_GET_ARG2(*call),
  741.              vport->width - x, vport->height - y, (putpixel_cb_t)putpixel, vport);
  742.         break;
  743.     case FB_DRAW_TEXT_DATA:
  744.         if (!interbuffer) {
  745.             retval = EINVAL;
  746.             break;
  747.         }
  748.         if (intersize < vport->cols*vport->rows*sizeof(*interbuffer)) {
  749.             retval = EINVAL;
  750.             break;
  751.         }
  752.         draw_text_data(vport, interbuffer);
  753.         break;
  754.     default:
  755.         handled = 0;
  756.     }
  757.    
  758.     if (handled)
  759.         ipc_answer_fast(callid, retval, 0, 0);
  760.     return handled;
  761. }
  762.  
  763. static void copy_vp_to_pixmap(viewport_t *vport, pixmap_t *pmap)
  764. {
  765.     int y;
  766.     int rowsize;
  767.     int tmp;
  768.     int width = vport->width;
  769.     int height = vport->height;
  770.  
  771.     if (width + vport->x > screen.xres)
  772.         width = screen.xres - vport->x;
  773.     if (height + vport->y  > screen.yres)
  774.         height = screen.yres - vport->y;
  775.  
  776.     rowsize = width * screen.pixelbytes;
  777.  
  778.     for (y=0;y < height; y++) {
  779.         tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
  780.         memcpy(pmap->data + rowsize*y, screen.fbaddress + tmp, rowsize);
  781.     }
  782. }
  783.  
  784. /** Save viewport to pixmap */
  785. static int save_vp_to_pixmap(viewport_t *vport)
  786. {
  787.     int pm;
  788.     pixmap_t *pmap;
  789.  
  790.     pm = find_free_pixmap();
  791.     if (pm == -1)
  792.         return ELIMIT;
  793.    
  794.     pmap = &pixmaps[pm];
  795.     pmap->data = malloc(screen.pixelbytes * vport->width * vport->height);
  796.     if (!pmap->data)
  797.         return ENOMEM;
  798.  
  799.     pmap->width = vport->width;
  800.     pmap->height = vport->height;
  801.  
  802.     copy_vp_to_pixmap(vport, pmap);
  803.    
  804.     return pm;
  805. }
  806.  
  807. /** Draw pixmap on screen
  808.  *
  809.  * @param vp Viewport to draw on
  810.  * @param pm Pixmap identifier
  811.  */
  812. static int draw_pixmap(int vp, int pm)
  813. {
  814.     pixmap_t *pmap = &pixmaps[pm];
  815.     viewport_t *vport = &viewports[vp];
  816.     int y;
  817.     int tmp, srcrowsize;
  818.     int realwidth, realheight, realrowsize;
  819.     int width = vport->width;
  820.     int height = vport->height;
  821.  
  822.     if (width + vport->x > screen.xres)
  823.         width = screen.xres - vport->x;
  824.     if (height + vport->y > screen.yres)
  825.         height = screen.yres - vport->y;
  826.  
  827.     if (!pmap->data)
  828.         return EINVAL;
  829.  
  830.     realwidth = pmap->width <= width ? pmap->width : width;
  831.     realheight = pmap->height <= height ? pmap->height : height;
  832.  
  833.     srcrowsize = vport->width * screen.pixelbytes;
  834.     realrowsize = realwidth * screen.pixelbytes;
  835.  
  836.     for (y=0; y < realheight; y++) {
  837.         tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
  838.         memcpy(screen.fbaddress + tmp, pmap->data + y * srcrowsize, realrowsize);
  839.     }
  840.     return 0;
  841. }
  842.  
  843. /** Tick animation one step forward */
  844. static void anims_tick(void)
  845. {
  846.     int i;
  847.     static int counts = 0;
  848.    
  849.     /* Limit redrawing */
  850.     counts = (counts+1) % 8;
  851.     if (counts)
  852.         return;
  853.  
  854.     for (i=0; i < MAX_ANIMATIONS; i++) {
  855.         if (!animations[i].animlen || !animations[i].initialized || !animations[i].enabled)
  856.             continue;
  857.         draw_pixmap(animations[i].vp, animations[i].pixmaps[animations[i].pos]);
  858.         animations[i].pos = (animations[i].pos+1) % animations[i].animlen;
  859.     }
  860. }
  861.  
  862.  
  863. static int pointer_x, pointer_y;
  864. static int pointer_shown, pointer_enabled;
  865. static int pointer_vport = -1;
  866. static int pointer_pixmap = -1;
  867.  
  868. static void mouse_show(void)
  869. {
  870.     int i,j;
  871.     int visibility;
  872.     int color;
  873.     int bytepos;
  874.  
  875.     if (pointer_shown || !pointer_enabled)
  876.         return;
  877.  
  878.     /* Save image under the cursor */
  879.     if (pointer_vport == -1) {
  880.         pointer_vport = viewport_create(pointer_x, pointer_y, pointer_width, pointer_height);
  881.         if (pointer_vport < 0)
  882.             return;
  883.     } else {
  884.         viewports[pointer_vport].x = pointer_x;
  885.         viewports[pointer_vport].y = pointer_y;
  886.     }
  887.  
  888.     if (pointer_pixmap == -1)
  889.         pointer_pixmap = save_vp_to_pixmap(&viewports[pointer_vport]);
  890.     else
  891.         copy_vp_to_pixmap(&viewports[pointer_vport], &pixmaps[pointer_pixmap]);
  892.  
  893.     /* Draw cursor */
  894.     for (i=0; i < pointer_height; i++)
  895.         for (j=0;j < pointer_width; j++) {
  896.             bytepos = i*((pointer_width-1)/8+1) + j/8;
  897.             visibility = pointer_mask_bits[bytepos] & (1 << (j % 8));
  898.             if (visibility) {
  899.                 color = pointer_bits[bytepos] & (1 << (j % 8)) ? 0 : 0xffffff;
  900.                 if (pointer_x+j < screen.xres && pointer_y+i < screen.yres)
  901.                     putpixel(&viewports[0], pointer_x+j, pointer_y+i, color);
  902.             }
  903.         }
  904.     pointer_shown = 1;
  905. }
  906.  
  907. static void mouse_hide(void)
  908. {
  909.     /* Restore image under the cursor */
  910.     if (pointer_shown) {
  911.         draw_pixmap(pointer_vport, pointer_pixmap);
  912.         pointer_shown = 0;
  913.     }
  914. }
  915.  
  916. static void mouse_move(unsigned int x, unsigned int y)
  917. {
  918.     mouse_hide();
  919.     pointer_x = x;
  920.     pointer_y = y;
  921.     mouse_show();
  922. }
  923.  
  924. static int anim_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
  925. {
  926.     int handled = 1;
  927.     int retval = 0;
  928.     int i,nvp;
  929.     int newval;
  930.  
  931.     switch (IPC_GET_METHOD(*call)) {
  932.     case FB_ANIM_CREATE:
  933.         nvp = IPC_GET_ARG1(*call);
  934.         if (nvp == -1)
  935.             nvp = vp;
  936.         if (nvp >= MAX_VIEWPORTS || nvp < 0 || !viewports[nvp].initialized) {
  937.             retval = EINVAL;
  938.             break;
  939.         }
  940.         for (i=0; i < MAX_ANIMATIONS; i++) {
  941.             if (! animations[i].initialized)
  942.                 break;
  943.         }
  944.         if (i == MAX_ANIMATIONS) {
  945.             retval = ELIMIT;
  946.             break;
  947.         }
  948.         animations[i].initialized = 1;
  949.         animations[i].animlen = 0;
  950.         animations[i].pos = 0;
  951.         animations[i].enabled = 0;
  952.         animations[i].vp = nvp;
  953.         retval = i;
  954.         break;
  955.     case FB_ANIM_DROP:
  956.         i = IPC_GET_ARG1(*call);
  957.         if (i >= MAX_ANIMATIONS || i < 0) {
  958.             retval = EINVAL;
  959.             break;
  960.         }
  961.         animations[i].initialized = 0;
  962.         break;
  963.     case FB_ANIM_ADDPIXMAP:
  964.         i = IPC_GET_ARG1(*call);
  965.         if (i >= MAX_ANIMATIONS || i < 0 || !animations[i].initialized) {
  966.             retval = EINVAL;
  967.             break;
  968.         }
  969.         if (animations[i].animlen == MAX_ANIM_LEN) {
  970.             retval = ELIMIT;
  971.             break;
  972.         }
  973.         newval = IPC_GET_ARG2(*call);
  974.         if (newval < 0 || newval > MAX_PIXMAPS || !pixmaps[newval].data) {
  975.             retval = EINVAL;
  976.             break;
  977.         }
  978.         animations[i].pixmaps[animations[i].animlen++] = newval;
  979.         break;
  980.     case FB_ANIM_CHGVP:
  981.         i = IPC_GET_ARG1(*call);
  982.         if (i >= MAX_ANIMATIONS || i < 0) {
  983.             retval = EINVAL;
  984.             break;
  985.         }
  986.         nvp = IPC_GET_ARG2(*call);
  987.         if (nvp == -1)
  988.             nvp = vp;
  989.         if (nvp >= MAX_VIEWPORTS || nvp < 0 || !viewports[nvp].initialized) {
  990.             retval = EINVAL;
  991.             break;
  992.         }
  993.         animations[i].vp = nvp;
  994.         break;
  995.     case FB_ANIM_START:
  996.     case FB_ANIM_STOP:
  997.         i = IPC_GET_ARG1(*call);
  998.         if (i >= MAX_ANIMATIONS || i < 0) {
  999.             retval = EINVAL;
  1000.             break;
  1001.         }
  1002.         newval = (IPC_GET_METHOD(*call) == FB_ANIM_START);
  1003.         if (newval ^ animations[i].enabled) {
  1004.             animations[i].enabled = newval;
  1005.             anims_enabled += newval ? 1 : -1;
  1006.         }
  1007.         break;
  1008.     default:
  1009.         handled = 0;
  1010.     }
  1011.     if (handled)
  1012.         ipc_answer_fast(callid, retval, 0, 0);
  1013.     return handled;
  1014. }
  1015.  
  1016. /** Handler for messages concerning pixmap handling */
  1017. static int pixmap_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
  1018. {
  1019.     int handled = 1;
  1020.     int retval = 0;
  1021.     int i,nvp;
  1022.  
  1023.     switch (IPC_GET_METHOD(*call)) {
  1024.     case FB_VP_DRAW_PIXMAP:
  1025.         nvp = IPC_GET_ARG1(*call);
  1026.         if (nvp == -1)
  1027.             nvp = vp;
  1028.         if (nvp < 0 || nvp >= MAX_VIEWPORTS || !viewports[nvp].initialized) {
  1029.             retval = EINVAL;
  1030.             break;
  1031.         }
  1032.         i = IPC_GET_ARG2(*call);
  1033.         retval = draw_pixmap(nvp, i);
  1034.         break;
  1035.     case FB_VP2PIXMAP:
  1036.         nvp = IPC_GET_ARG1(*call);
  1037.         if (nvp == -1)
  1038.             nvp = vp;
  1039.         if (nvp < 0 || nvp >= MAX_VIEWPORTS || !viewports[nvp].initialized)
  1040.             retval = EINVAL;
  1041.         else
  1042.             retval = save_vp_to_pixmap(&viewports[nvp]);
  1043.         break;
  1044.     case FB_DROP_PIXMAP:
  1045.         i = IPC_GET_ARG1(*call);
  1046.         if (i >= MAX_PIXMAPS) {
  1047.             retval = EINVAL;
  1048.             break;
  1049.         }
  1050.         if (pixmaps[i].data) {
  1051.             free(pixmaps[i].data);
  1052.             pixmaps[i].data = NULL;
  1053.         }
  1054.         break;
  1055.     default:
  1056.         handled = 0;
  1057.     }
  1058.  
  1059.     if (handled)
  1060.         ipc_answer_fast(callid, retval, 0, 0);
  1061.     return handled;
  1062.    
  1063. }
  1064.  
  1065. /** Function for handling connections to FB
  1066.  *
  1067.  */
  1068. static void fb_client_connection(ipc_callid_t iid, ipc_call_t *icall)
  1069. {
  1070.     ipc_callid_t callid;
  1071.     ipc_call_t call;
  1072.     int retval;
  1073.     int i;
  1074.     unsigned int row,col;
  1075.     char c;
  1076.  
  1077.     int vp = 0;
  1078.     viewport_t *vport = &viewports[0];
  1079.  
  1080.     if (client_connected) {
  1081.         ipc_answer_fast(iid, ELIMIT, 0,0);
  1082.         return;
  1083.     }
  1084.     client_connected = 1;
  1085.     ipc_answer_fast(iid, 0, 0, 0); /* Accept connection */
  1086.  
  1087.     while (1) {
  1088.         if (vport->cursor_active || anims_enabled)
  1089.             callid = async_get_call_timeout(&call,250000);
  1090.         else
  1091.             callid = async_get_call(&call);
  1092.  
  1093.         mouse_hide();
  1094.         if (!callid) {
  1095.             cursor_blink(vport);
  1096.             anims_tick();
  1097.             mouse_show();
  1098.             continue;
  1099.         }
  1100.         if (shm_handle(callid, &call, vp))
  1101.             continue;
  1102.         if (pixmap_handle(callid, &call, vp))
  1103.             continue;
  1104.         if (anim_handle(callid, &call, vp))
  1105.             continue;
  1106.  
  1107.         switch (IPC_GET_METHOD(call)) {
  1108.         case IPC_M_PHONE_HUNGUP:
  1109.             client_connected = 0;
  1110.             /* cleanup other viewports */
  1111.             for (i=1; i < MAX_VIEWPORTS; i++)
  1112.                 vport->initialized = 0;
  1113.             return; /* Exit thread */
  1114.  
  1115.         case FB_PUTCHAR:
  1116.         case FB_TRANS_PUTCHAR:
  1117.             c = IPC_GET_ARG1(call);
  1118.             row = IPC_GET_ARG2(call);
  1119.             col = IPC_GET_ARG3(call);
  1120.             if (row >= vport->rows || col >= vport->cols) {
  1121.                 retval = EINVAL;
  1122.                 break;
  1123.             }
  1124.             ipc_answer_fast(callid,0,0,0);
  1125.  
  1126.             draw_char(vport, c, row, col, vport->style, IPC_GET_METHOD(call) == FB_TRANS_PUTCHAR);
  1127.             continue; /* msg already answered */
  1128.         case FB_CLEAR:
  1129.             clear_port(vport);
  1130.             cursor_print(vport);
  1131.             retval = 0;
  1132.             break;
  1133.         case FB_CURSOR_GOTO:
  1134.             row = IPC_GET_ARG1(call);
  1135.             col = IPC_GET_ARG2(call);
  1136.             if (row >= vport->rows || col >= vport->cols) {
  1137.                 retval = EINVAL;
  1138.                 break;
  1139.             }
  1140.             retval = 0;
  1141.             cursor_hide(vport);
  1142.             vport->cur_col = col;
  1143.             vport->cur_row = row;
  1144.             cursor_print(vport);
  1145.             break;
  1146.         case FB_CURSOR_VISIBILITY:
  1147.             cursor_hide(vport);
  1148.             vport->cursor_active = IPC_GET_ARG1(call);
  1149.             cursor_print(vport);
  1150.             retval = 0;
  1151.             break;
  1152.         case FB_GET_CSIZE:
  1153.             ipc_answer_fast(callid, 0, vport->rows, vport->cols);
  1154.             continue;
  1155.         case FB_SCROLL:
  1156.             i = IPC_GET_ARG1(call);
  1157.             if (i > vport->rows || i < (- (int)vport->rows)) {
  1158.                 retval = EINVAL;
  1159.                 break;
  1160.             }
  1161.             cursor_hide(vport);
  1162.             scroll_port(vport, i*FONT_SCANLINES);
  1163.             cursor_print(vport);
  1164.             retval = 0;
  1165.             break;
  1166.         case FB_VIEWPORT_DB:
  1167.             /* Enable double buffering */
  1168.             i = IPC_GET_ARG1(call);
  1169.             if (i == -1)
  1170.                 i = vp;
  1171.             if (i < 0 || i >= MAX_VIEWPORTS) {
  1172.                 retval = EINVAL;
  1173.                 break;
  1174.             }
  1175.             if (! viewports[i].initialized ) {
  1176.                 retval = EADDRNOTAVAIL;
  1177.                 break;
  1178.             }
  1179.             viewports[i].dboffset = 0;
  1180.             if (IPC_GET_ARG2(call) == 1 && !viewports[i].dbdata)
  1181.                 viewports[i].dbdata = malloc(screen.pixelbytes*viewports[i].width * viewports[i].height);
  1182.             else if (IPC_GET_ARG2(call) == 0 && viewports[i].dbdata) {
  1183.                 free(viewports[i].dbdata);
  1184.                 viewports[i].dbdata = NULL;
  1185.             }
  1186.             retval = 0;
  1187.             break;
  1188.         case FB_VIEWPORT_SWITCH:
  1189.             i = IPC_GET_ARG1(call);
  1190.             if (i < 0 || i >= MAX_VIEWPORTS) {
  1191.                 retval = EINVAL;
  1192.                 break;
  1193.             }
  1194.             if (! viewports[i].initialized ) {
  1195.                 retval = EADDRNOTAVAIL;
  1196.                 break;
  1197.             }
  1198.             cursor_hide(vport);
  1199.             vp = i;
  1200.             vport = &viewports[vp];
  1201.             cursor_print(vport);
  1202.             retval = 0;
  1203.             break;
  1204.         case FB_VIEWPORT_CREATE:
  1205.             retval = viewport_create(IPC_GET_ARG1(call) >> 16,
  1206.                          IPC_GET_ARG1(call) & 0xffff,
  1207.                          IPC_GET_ARG2(call) >> 16,
  1208.                          IPC_GET_ARG2(call) & 0xffff);
  1209.             break;
  1210.         case FB_VIEWPORT_DELETE:
  1211.             i = IPC_GET_ARG1(call);
  1212.             if (i < 0 || i >= MAX_VIEWPORTS) {
  1213.                 retval = EINVAL;
  1214.                 break;
  1215.             }
  1216.             if (! viewports[i].initialized ) {
  1217.                 retval = EADDRNOTAVAIL;
  1218.                 break;
  1219.             }
  1220.             viewports[i].initialized = 0;
  1221.             if (viewports[i].dbdata) {
  1222.                 free(viewports[i].dbdata);
  1223.                 viewports[i].dbdata = NULL;
  1224.             }
  1225.             retval = 0;
  1226.             break;
  1227.         case FB_SET_STYLE:
  1228.             vport->style.fg_color = IPC_GET_ARG1(call);
  1229.             vport->style.bg_color = IPC_GET_ARG2(call);
  1230.             retval = 0;
  1231.             break;
  1232.         case FB_GET_RESOLUTION:
  1233.             ipc_answer_fast(callid, 0, screen.xres,screen.yres);
  1234.             continue;
  1235.         case FB_POINTER_MOVE:
  1236.             pointer_enabled = 1;
  1237.             mouse_move(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
  1238.             retval = 0;
  1239.             break;
  1240.         default:
  1241.             retval = ENOENT;
  1242.         }
  1243.         ipc_answer_fast(callid,retval,0,0);
  1244.     }
  1245. }
  1246.  
  1247. /** Initialization of framebuffer */
  1248. int fb_init(void)
  1249. {
  1250.     void *fb_ph_addr;
  1251.     unsigned int fb_width;
  1252.     unsigned int fb_height;
  1253.     unsigned int fb_scanline;
  1254.     unsigned int fb_visual;
  1255.     bool fb_invert_colors;
  1256.     void *fb_addr;
  1257.     size_t asz;
  1258.  
  1259.     async_set_client_connection(fb_client_connection);
  1260.  
  1261.     fb_ph_addr = (void *) sysinfo_value("fb.address.physical");
  1262.     fb_width = sysinfo_value("fb.width");
  1263.     fb_height = sysinfo_value("fb.height");
  1264.     fb_scanline = sysinfo_value("fb.scanline");
  1265.     fb_visual = sysinfo_value("fb.visual");
  1266.     fb_invert_colors = sysinfo_value("fb.invert-colors");
  1267.  
  1268.     asz = fb_scanline * fb_height;
  1269.     fb_addr = as_get_mappable_page(asz);
  1270.    
  1271.     map_physmem(fb_ph_addr, fb_addr, ALIGN_UP(asz, PAGE_SIZE) >> PAGE_WIDTH,
  1272.             AS_AREA_READ | AS_AREA_WRITE);
  1273.  
  1274.     if (screen_init(fb_addr, fb_width, fb_height, fb_scanline, fb_visual, fb_invert_colors))
  1275.         return 0;
  1276.    
  1277.     return -1;
  1278. }
  1279.  
  1280. /**
  1281.  * @}
  1282.  */
  1283.