Subversion Repositories HelenOS

Rev

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

  1. /*
  2.  * Copyright (c) 2006 Ondrej Palkovsky
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @defgroup egafb EGA framebuffer
  30.  * @brief   HelenOS EGA framebuffer.
  31.  * @ingroup fbs
  32.  * @{
  33.  */
  34. /** @file
  35.  */
  36.  
  37. #include <stdlib.h>
  38. #include <unistd.h>
  39. #include <align.h>
  40. #include <async.h>
  41. #include <ipc/ipc.h>
  42. #include <errno.h>
  43. #include <stdio.h>
  44. #include <ddi.h>
  45. #include <sysinfo.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 <libarch/ddi.h>
  52. #include <io/style.h>
  53. #include <io/color.h>
  54. #include <sys/types.h>
  55.  
  56. #include "ega.h"
  57. #include "../console/screenbuffer.h"
  58. #include "main.h"
  59.  
  60. #define MAX_SAVED_SCREENS 256
  61. typedef struct saved_screen {
  62.     short *data;
  63. } saved_screen;
  64.  
  65. saved_screen saved_screens[MAX_SAVED_SCREENS];
  66.  
  67. #define EGA_IO_BASE ((ioport8_t *) 0x3d4)
  68. #define EGA_IO_SIZE 2
  69.  
  70. int ega_normal_color = 0x0f;
  71. int ega_inverted_color = 0xf0;
  72.  
  73. #define NORMAL_COLOR        ega_normal_color
  74. #define INVERTED_COLOR      ega_inverted_color
  75.  
  76. /* Allow only 1 connection */
  77. static int client_connected = 0;
  78.  
  79. static unsigned int scr_width;
  80. static unsigned int scr_height;
  81. static uint8_t *scr_addr;
  82.  
  83. static unsigned int style;
  84.  
  85. static unsigned attr_to_ega_style(const attrs_t *a);
  86. static uint8_t ega_glyph(wchar_t ch);
  87.  
  88. static void clrscr(void)
  89. {
  90.     int i;
  91.    
  92.     for (i = 0; i < scr_width * scr_height; i++) {
  93.         scr_addr[i * 2] = ' ';
  94.         scr_addr[i * 2 + 1] = style;
  95.     }
  96. }
  97.  
  98. static void cursor_goto(unsigned int col, unsigned int row)
  99. {
  100.     int ega_cursor;
  101.  
  102.     ega_cursor = col + scr_width * row;
  103.    
  104.     pio_write_8(EGA_IO_BASE, 0xe);
  105.     pio_write_8(EGA_IO_BASE + 1, (ega_cursor >> 8) & 0xff);
  106.     pio_write_8(EGA_IO_BASE, 0xf);
  107.     pio_write_8(EGA_IO_BASE + 1, ega_cursor & 0xff);
  108. }
  109.  
  110. static void cursor_disable(void)
  111. {
  112.     uint8_t stat;
  113.  
  114.     pio_write_8(EGA_IO_BASE, 0xa);
  115.     stat = pio_read_8(EGA_IO_BASE + 1);
  116.     pio_write_8(EGA_IO_BASE, 0xa);
  117.     pio_write_8(EGA_IO_BASE + 1, stat | (1 << 5));
  118. }
  119.  
  120. static void cursor_enable(void)
  121. {
  122.     uint8_t stat;
  123.  
  124.     pio_write_8(EGA_IO_BASE, 0xa);
  125.     stat = pio_read_8(EGA_IO_BASE + 1);
  126.     pio_write_8(EGA_IO_BASE, 0xa);
  127.     pio_write_8(EGA_IO_BASE + 1, stat & (~(1 << 5)));
  128. }
  129.  
  130. static void scroll(int rows)
  131. {
  132.     int i;
  133.     if (rows > 0) {
  134.         memmove(scr_addr, ((char *) scr_addr) + rows * scr_width * 2,
  135.             scr_width * scr_height * 2 - rows * scr_width * 2);
  136.         for (i = 0; i < rows * scr_width; i++)
  137.             (((short *) scr_addr) + scr_width * scr_height - rows *
  138.                 scr_width)[i] = ((style << 8) + ' ');
  139.     } else if (rows < 0) {
  140.         memmove(((char *)scr_addr) - rows * scr_width * 2, scr_addr,
  141.             scr_width * scr_height * 2 + rows * scr_width * 2);
  142.         for (i = 0; i < -rows * scr_width; i++)
  143.             ((short *)scr_addr)[i] = ((style << 8 ) + ' ');
  144.     }
  145. }
  146.  
  147. static void printchar(wchar_t c, unsigned int col, unsigned int row)
  148. {
  149.     scr_addr[(row * scr_width + col) * 2] = ega_glyph(c);
  150.     scr_addr[(row * scr_width + col) * 2 + 1] = style;
  151.    
  152.     cursor_goto(col + 1, row);
  153. }
  154.  
  155. /** Draw text data to viewport.
  156.  *
  157.  * @param vport Viewport id
  158.  * @param data  Text data.
  159.  * @param x Leftmost column of the area.
  160.  * @param y Topmost row of the area.
  161.  * @param w Number of rows.
  162.  * @param h Number of columns.
  163.  */
  164. static void draw_text_data(keyfield_t *data, unsigned int x,
  165.     unsigned int y, unsigned int w, unsigned int h)
  166. {
  167.     unsigned int i, j;
  168.     keyfield_t *field;
  169.     uint8_t *dp;
  170.  
  171.     for (j = 0; j < h; j++) {
  172.         for (i = 0; i < w; i++) {
  173.             field = &data[j * w + i];
  174.             dp = &scr_addr[2 * ((y + j) * scr_width + (x + i))];
  175.  
  176.             dp[0] = ega_glyph(field->character);
  177.             dp[1] = attr_to_ega_style(&field->attrs);
  178.         }
  179.     }
  180. }
  181.  
  182. static int save_screen(void)
  183. {
  184.     int i;
  185.  
  186.     for (i = 0; (i < MAX_SAVED_SCREENS) && (saved_screens[i].data); i++)
  187.         ;
  188.     if (i == MAX_SAVED_SCREENS)
  189.         return EINVAL;
  190.     if (!(saved_screens[i].data = malloc(2 * scr_width * scr_height)))
  191.         return ENOMEM;
  192.     memcpy(saved_screens[i].data, scr_addr, 2 * scr_width * scr_height);
  193.  
  194.     return i;
  195. }
  196.  
  197. static int print_screen(int i)
  198. {
  199.     if (saved_screens[i].data)
  200.         memcpy(scr_addr, saved_screens[i].data, 2 * scr_width *
  201.             scr_height);
  202.     else
  203.         return EINVAL;
  204.     return i;
  205. }
  206.  
  207. static int style_to_ega_style(int style)
  208. {
  209.     unsigned int ega_style;
  210.  
  211.     switch (style) {
  212.     case STYLE_NORMAL:
  213.         ega_style = INVERTED_COLOR;
  214.         break;
  215.     case STYLE_EMPHASIS:
  216.         ega_style = INVERTED_COLOR | 4;
  217.         break;
  218.     default:
  219.         return INVERTED_COLOR;
  220.     }
  221.  
  222.     return ega_style;
  223. }
  224.  
  225. static unsigned int color_to_ega_style(int fg_color, int bg_color, int attr)
  226. {
  227.     unsigned int style;
  228.  
  229.     style = (fg_color & 7) | ((bg_color & 7) << 4);
  230.     if (attr & CATTR_BRIGHT)
  231.         style = style | 0x08;
  232.  
  233.     return style;
  234. }
  235.  
  236. static unsigned int rgb_to_ega_style(uint32_t fg, uint32_t bg)
  237. {
  238.     return (fg > bg) ? NORMAL_COLOR : INVERTED_COLOR;
  239. }
  240.  
  241. static unsigned attr_to_ega_style(const attrs_t *a)
  242. {
  243.     switch (a->t) {
  244.     case at_style:
  245.         return style_to_ega_style(a->a.s.style);
  246.     case at_rgb:
  247.         return rgb_to_ega_style(a->a.r.fg_color, a->a.r.bg_color);
  248.     case at_idx:
  249.         return color_to_ega_style(a->a.i.fg_color,
  250.             a->a.i.bg_color, a->a.i.flags);
  251.     default:
  252.         return INVERTED_COLOR;
  253.     }
  254. }
  255.  
  256. static uint8_t ega_glyph(wchar_t ch)
  257. {
  258.     if (ch >= 0 && ch < 128)
  259.         return ch;
  260.  
  261.     return '?';
  262. }
  263.  
  264. static void ega_client_connection(ipc_callid_t iid, ipc_call_t *icall)
  265. {
  266.     int retval;
  267.     ipc_callid_t callid;
  268.     ipc_call_t call;
  269.     wchar_t c;
  270.     unsigned int row, col, w, h;
  271.     int bg_color, fg_color, attr;
  272.     uint32_t bg_rgb, fg_rgb;
  273.     keyfield_t *interbuf = NULL;
  274.     size_t intersize = 0;
  275.     int i;
  276.  
  277.     if (client_connected) {
  278.         ipc_answer_0(iid, ELIMIT);
  279.         return;
  280.     }
  281.     client_connected = 1;
  282.     ipc_answer_0(iid, EOK); /* Accept connection */
  283.  
  284.     while (1) {
  285.         callid = async_get_call(&call);
  286.         switch (IPC_GET_METHOD(call)) {
  287.         case IPC_M_PHONE_HUNGUP:
  288.             client_connected = 0;
  289.             ipc_answer_0(callid, EOK);
  290.             return; /* Exit thread */
  291.         case IPC_M_SHARE_OUT:
  292.             /* We accept one area for data interchange */
  293.             intersize = IPC_GET_ARG2(call);
  294.             if (intersize >= scr_width * scr_height *
  295.                 sizeof(*interbuf)) {
  296.                 receive_comm_area(callid, &call,
  297.                     (void *) &interbuf);
  298.                 continue;
  299.             }
  300.             retval = EINVAL;
  301.             break;
  302.         case FB_DRAW_TEXT_DATA:
  303.             col = IPC_GET_ARG1(call);
  304.             row = IPC_GET_ARG2(call);
  305.             w = IPC_GET_ARG3(call);
  306.             h = IPC_GET_ARG4(call);
  307.             if (!interbuf) {
  308.                 retval = EINVAL;
  309.                 break;
  310.             }
  311.             if (col + w > scr_width || row + h > scr_height) {
  312.                 retval = EINVAL;
  313.                 break;
  314.             }
  315.             draw_text_data(interbuf, col, row, w, h);
  316.             retval = 0;
  317.             break;
  318.         case FB_GET_CSIZE:
  319.             ipc_answer_2(callid, EOK, scr_width, scr_height);
  320.             continue;
  321.         case FB_CLEAR:
  322.             clrscr();
  323.             retval = 0;
  324.             break;
  325.         case FB_PUTCHAR:
  326.             c = IPC_GET_ARG1(call);
  327.             col = IPC_GET_ARG2(call);
  328.             row = IPC_GET_ARG3(call);
  329.             if (col >= scr_width || row >= scr_height) {
  330.                 retval = EINVAL;
  331.                 break;
  332.             }
  333.             printchar(c, col, row);
  334.             retval = 0;
  335.             break;
  336.         case FB_CURSOR_GOTO:
  337.             col = IPC_GET_ARG1(call);
  338.             row = IPC_GET_ARG2(call);
  339.             if (row >= scr_height || col >= scr_width) {
  340.                 retval = EINVAL;
  341.                 break;
  342.             }
  343.             cursor_goto(col, row);
  344.             retval = 0;
  345.             break;
  346.         case FB_SCROLL:
  347.             i = IPC_GET_ARG1(call);
  348.             if (i > scr_height || i < -((int) scr_height)) {
  349.                 retval = EINVAL;
  350.                 break;
  351.             }
  352.             scroll(i);
  353.             retval = 0;
  354.             break;
  355.         case FB_CURSOR_VISIBILITY:
  356.             if (IPC_GET_ARG1(call))
  357.                 cursor_enable();
  358.             else
  359.                 cursor_disable();
  360.             retval = 0;
  361.             break;
  362.         case FB_SET_STYLE:
  363.             style = style_to_ega_style(IPC_GET_ARG1(call));
  364.             retval = 0;
  365.             break;
  366.         case FB_SET_COLOR:
  367.             fg_color = IPC_GET_ARG1(call);
  368.             bg_color = IPC_GET_ARG2(call);
  369.             attr = IPC_GET_ARG3(call);
  370.             style = color_to_ega_style(fg_color, bg_color, attr);
  371.             retval = 0;
  372.             break;
  373.         case FB_SET_RGB_COLOR:
  374.             fg_rgb = IPC_GET_ARG1(call);
  375.             bg_rgb = IPC_GET_ARG2(call);
  376.             style = rgb_to_ega_style(fg_rgb, bg_rgb);
  377.             retval = 0;
  378.             break;
  379.         case FB_VP_DRAW_PIXMAP:
  380.             i = IPC_GET_ARG2(call);
  381.             retval = print_screen(i);
  382.             break;
  383.         case FB_VP2PIXMAP:
  384.             retval = save_screen();
  385.             break;
  386.         case FB_DROP_PIXMAP:
  387.             i = IPC_GET_ARG1(call);
  388.             if (i >= MAX_SAVED_SCREENS) {
  389.                 retval = EINVAL;
  390.                 break;
  391.             }
  392.             if (saved_screens[i].data) {
  393.                 free(saved_screens[i].data);
  394.                 saved_screens[i].data = NULL;
  395.             }
  396.             retval = 0;
  397.             break;
  398.         case FB_SCREEN_YIELD:
  399.         case FB_SCREEN_RECLAIM:
  400.             retval = EOK;
  401.             break;
  402.         default:
  403.             retval = EINVAL;
  404.         }
  405.         ipc_answer_0(callid, retval);
  406.     }
  407. }
  408.  
  409. int ega_init(void)
  410. {
  411.     void *ega_ph_addr;
  412.     size_t sz;
  413.  
  414.     ega_ph_addr = (void *) sysinfo_value("fb.address.physical");
  415.     scr_width = sysinfo_value("fb.width");
  416.     scr_height = sysinfo_value("fb.height");
  417.  
  418.     if (sysinfo_value("fb.blinking")) {
  419.         ega_normal_color &= 0x77;
  420.         ega_inverted_color &= 0x77;
  421.     }
  422.  
  423.     style = NORMAL_COLOR;
  424.  
  425.     iospace_enable(task_get_id(), (void *) EGA_IO_BASE, 2);
  426.  
  427.     sz = scr_width * scr_height * 2;
  428.     scr_addr = as_get_mappable_page(sz);
  429.  
  430.     if (physmem_map(ega_ph_addr, scr_addr, ALIGN_UP(sz, PAGE_SIZE) >>
  431.         PAGE_WIDTH, AS_AREA_READ | AS_AREA_WRITE) != 0)
  432.         return -1;
  433.  
  434.     async_set_client_connection(ega_client_connection);
  435.  
  436.     return 0;
  437. }
  438.  
  439.  
  440. /**
  441.  * @}
  442.  */
  443.