Subversion Repositories HelenOS

Rev

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