Subversion Repositories HelenOS

Rev

Rev 3343 | 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.  
  53. #include "ega.h"
  54. #include "../console/screenbuffer.h"
  55. #include "main.h"
  56.  
  57. #define MAX_SAVED_SCREENS 256
  58. typedef struct saved_screen {
  59.     short *data;
  60. } saved_screen;
  61.  
  62. saved_screen saved_screens[MAX_SAVED_SCREENS];
  63.  
  64. #define EGA_IO_ADDRESS 0x3d4
  65. #define EGA_IO_SIZE 2
  66.  
  67. int ega_normal_color=0x0f;
  68. int ega_inverted_color=0xf0;
  69.  
  70. #define NORMAL_COLOR        ega_normal_color      
  71. #define INVERTED_COLOR      ega_inverted_color
  72.  
  73. #define EGA_STYLE(fg,bg) ((fg) > (bg) ? NORMAL_COLOR : INVERTED_COLOR)
  74.  
  75. /* Allow only 1 connection */
  76. static int client_connected = 0;
  77.  
  78. static unsigned int scr_width;
  79. static unsigned int scr_height;
  80. static char *scr_addr;
  81.  
  82. static unsigned int style;
  83.  
  84. static void clrscr(void)
  85. {
  86.     int i;
  87.    
  88.     for (i = 0; i < scr_width * scr_height; i++) {
  89.         scr_addr[i * 2] = ' ';
  90.         scr_addr[i * 2 + 1] = style;
  91.     }
  92. }
  93.  
  94. static void cursor_goto(unsigned int row, unsigned int col)
  95. {
  96.     int ega_cursor;
  97.  
  98.     ega_cursor = col + scr_width * row;
  99.    
  100.     outb(EGA_IO_ADDRESS, 0xe);
  101.     outb(EGA_IO_ADDRESS + 1, (ega_cursor >> 8) & 0xff);
  102.     outb(EGA_IO_ADDRESS, 0xf);
  103.     outb(EGA_IO_ADDRESS + 1, ega_cursor & 0xff);
  104. }
  105.  
  106. static void cursor_disable(void)
  107. {
  108.     uint8_t stat;
  109.  
  110.     outb(EGA_IO_ADDRESS, 0xa);
  111.     stat=inb(EGA_IO_ADDRESS + 1);
  112.     outb(EGA_IO_ADDRESS, 0xa);
  113.     outb(EGA_IO_ADDRESS + 1, stat | (1 << 5));
  114. }
  115.  
  116. static void cursor_enable(void)
  117. {
  118.     uint8_t stat;
  119.  
  120.     outb(EGA_IO_ADDRESS, 0xa);
  121.     stat=inb(EGA_IO_ADDRESS + 1);
  122.     outb(EGA_IO_ADDRESS, 0xa);
  123.     outb(EGA_IO_ADDRESS + 1, stat & (~(1 << 5)));
  124. }
  125.  
  126. static void scroll(int rows)
  127. {
  128.     int i;
  129.     if (rows > 0) {
  130.         memcpy(scr_addr, ((char *) scr_addr) + rows * scr_width * 2,
  131.             scr_width * scr_height * 2 - rows * scr_width * 2);
  132.         for (i = 0; i < rows * scr_width; i++)
  133.             (((short *) scr_addr) + scr_width * scr_height - rows *
  134.                 scr_width)[i] = ((style << 8) + ' ');
  135.     } else if (rows < 0) {
  136.         memcpy(((char *)scr_addr) - rows * scr_width * 2, scr_addr,
  137.             scr_width * scr_height * 2 + rows * scr_width * 2);
  138.         for (i = 0; i < -rows * scr_width; i++)
  139.             ((short *)scr_addr)[i] = ((style << 8 ) + ' ');
  140.     }
  141. }
  142.  
  143. static void printchar(char c, unsigned int row, unsigned int col)
  144. {
  145.     scr_addr[(row * scr_width + col) * 2] = c;
  146.     scr_addr[(row * scr_width + col) * 2 + 1] = style;
  147.    
  148.     cursor_goto(row, col + 1);
  149. }
  150.  
  151. static void draw_text_data(keyfield_t *data)
  152. {
  153.     int i;
  154.  
  155.     for (i = 0; i < scr_width * scr_height; i++) {
  156.         scr_addr[i * 2] = data[i].character;
  157.         scr_addr[i * 2 + 1] = EGA_STYLE(data[i].style.fg_color,
  158.             data[i].style.bg_color);
  159.     }
  160. }
  161.  
  162. static int save_screen(void)
  163. {
  164.     int i;
  165.  
  166.     for (i = 0; (i < MAX_SAVED_SCREENS) && (saved_screens[i].data); i++)
  167.         ;
  168.     if (i == MAX_SAVED_SCREENS)
  169.         return EINVAL;
  170.     if (!(saved_screens[i].data = malloc(2 * scr_width * scr_height)))
  171.         return ENOMEM;
  172.     memcpy(saved_screens[i].data, scr_addr, 2 * scr_width * scr_height);
  173.  
  174.     return i;
  175. }
  176.  
  177. static int print_screen(int i)
  178. {
  179.     if (saved_screens[i].data)
  180.         memcpy(scr_addr, saved_screens[i].data, 2 * scr_width *
  181.             scr_height);
  182.     else
  183.         return EINVAL;
  184.     return i;
  185. }
  186.  
  187.  
  188. static void ega_client_connection(ipc_callid_t iid, ipc_call_t *icall)
  189. {
  190.     int retval;
  191.     ipc_callid_t callid;
  192.     ipc_call_t call;
  193.     char c;
  194.     unsigned int row, col;
  195.     int bgcolor,fgcolor;
  196.     keyfield_t *interbuf = NULL;
  197.     size_t intersize = 0;
  198.     int i;
  199.  
  200.     if (client_connected) {
  201.         ipc_answer_0(iid, ELIMIT);
  202.         return;
  203.     }
  204.     client_connected = 1;
  205.     ipc_answer_0(iid, EOK); /* Accept connection */
  206.  
  207.     while (1) {
  208.         callid = async_get_call(&call);
  209.         switch (IPC_GET_METHOD(call)) {
  210.         case IPC_M_PHONE_HUNGUP:
  211.             client_connected = 0;
  212.             ipc_answer_0(callid, EOK);
  213.             return; /* Exit thread */
  214.         case IPC_M_SHARE_OUT:
  215.             /* We accept one area for data interchange */
  216.             intersize = IPC_GET_ARG2(call);
  217.             if (intersize >= scr_width * scr_height *
  218.                 sizeof(*interbuf)) {
  219.                 receive_comm_area(callid, &call,
  220.                     (void *) &interbuf);
  221.                 continue;
  222.             }
  223.             retval = EINVAL;
  224.             break;
  225.         case FB_DRAW_TEXT_DATA:
  226.             if (!interbuf) {
  227.                 retval = EINVAL;
  228.                 break;
  229.             }
  230.             draw_text_data(interbuf);
  231.             retval = 0;
  232.             break;
  233.         case FB_GET_CSIZE:
  234.             ipc_answer_2(callid, EOK, scr_height, scr_width);
  235.             continue;
  236.         case FB_CLEAR:
  237.             clrscr();
  238.             retval = 0;
  239.             break;
  240.         case FB_PUTCHAR:
  241.             c = IPC_GET_ARG1(call);
  242.             row = IPC_GET_ARG2(call);
  243.             col = IPC_GET_ARG3(call);
  244.             if (col >= scr_width || row >= scr_height) {
  245.                 retval = EINVAL;
  246.                 break;
  247.             }
  248.             printchar(c, row, col);
  249.             retval = 0;
  250.             break;
  251.         case FB_CURSOR_GOTO:
  252.             row = IPC_GET_ARG1(call);
  253.             col = IPC_GET_ARG2(call);
  254.             if (row >= scr_height || col >= scr_width) {
  255.                 retval = EINVAL;
  256.                 break;
  257.             }
  258.             cursor_goto(row, col);
  259.             retval = 0;
  260.             break;
  261.         case FB_SCROLL:
  262.             i = IPC_GET_ARG1(call);
  263.             if (i > scr_height || i < -((int) scr_height)) {
  264.                 retval = EINVAL;
  265.                 break;
  266.             }
  267.             scroll(i);
  268.             retval = 0;
  269.             break;
  270.         case FB_CURSOR_VISIBILITY:
  271.             if(IPC_GET_ARG1(call))
  272.                 cursor_enable();
  273.             else
  274.                 cursor_disable();
  275.             retval = 0;
  276.             break;
  277.         case FB_SET_STYLE:
  278.             fgcolor = IPC_GET_ARG1(call);
  279.             bgcolor = IPC_GET_ARG2(call);
  280.             style = EGA_STYLE(fgcolor, bgcolor);
  281.             retval = 0;
  282.             break;
  283.         case FB_VP_DRAW_PIXMAP:
  284.             i = IPC_GET_ARG2(call);
  285.             retval = print_screen(i);
  286.             break;
  287.         case FB_VP2PIXMAP:
  288.             retval = save_screen();
  289.             break;
  290.         case FB_DROP_PIXMAP:
  291.             i = IPC_GET_ARG1(call);
  292.             if (i >= MAX_SAVED_SCREENS) {
  293.                 retval = EINVAL;
  294.                 break;
  295.             }
  296.             if (saved_screens[i].data) {
  297.                 free(saved_screens[i].data);
  298.                 saved_screens[i].data = NULL;
  299.             }
  300.             retval = 0;
  301.             break;
  302.  
  303.         default:
  304.             retval = ENOENT;
  305.         }
  306.         ipc_answer_0(callid, retval);
  307.     }
  308. }
  309.  
  310. int ega_init(void)
  311. {
  312.     void *ega_ph_addr;
  313.     size_t sz;
  314.  
  315.     ega_ph_addr = (void *) sysinfo_value("fb.address.physical");
  316.     scr_width = sysinfo_value("fb.width");
  317.     scr_height = sysinfo_value("fb.height");
  318.     if(sysinfo_value("fb.blinking"))
  319.     {
  320.             ega_normal_color&=0x77;
  321.             ega_inverted_color&=0x77;
  322.     }
  323.     style = NORMAL_COLOR;
  324.  
  325.     iospace_enable(task_get_id(), (void *) EGA_IO_ADDRESS, 2);
  326.  
  327.     sz = scr_width * scr_height * 2;
  328.     scr_addr = as_get_mappable_page(sz);
  329.  
  330.     physmem_map(ega_ph_addr, scr_addr, ALIGN_UP(sz, PAGE_SIZE) >>
  331.         PAGE_WIDTH, AS_AREA_READ | AS_AREA_WRITE);
  332.  
  333.     async_set_client_connection(ega_client_connection);
  334.  
  335.     return 0;
  336. }
  337.  
  338.  
  339. /**
  340.  * @}
  341.  */
  342.