ega.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Ondrej Palkovsky
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00038 #include <stdlib.h>
00039 #include <unistd.h>
00040 #include <align.h>
00041 #include <async.h>
00042 #include <ipc/ipc.h>
00043 #include <errno.h>
00044 #include <stdio.h>
00045 #include <ddi.h>
00046 #include <sysinfo.h>
00047 #include <as.h>
00048 #include <ipc/fb.h>
00049 #include <ipc/ipc.h>
00050 #include <ipc/ns.h>
00051 #include <ipc/services.h>
00052 #include <libarch/ddi.h>
00053 
00054 #include "ega.h"
00055 #include "../console/screenbuffer.h"
00056 #include "main.h"
00057 
00058 #define MAX_SAVED_SCREENS 256
00059 typedef struct saved_screen {
00060         short *data;
00061 } saved_screen;
00062 
00063 saved_screen saved_screens[MAX_SAVED_SCREENS];
00064 
00065 
00066 #define EGA_IO_ADDRESS 0x3d4
00067 #define EGA_IO_SIZE 2
00068 
00069 #define NORMAL_COLOR       0x0f
00070 #define INVERTED_COLOR     0xf0
00071 
00072 #define EGA_STYLE(fg,bg) ((fg) > (bg) ? NORMAL_COLOR : INVERTED_COLOR)
00073 
00074 /* Allow only 1 connection */
00075 static int client_connected = 0;
00076 
00077 static unsigned int scr_width;
00078 static unsigned int scr_height;
00079 static char *scr_addr;
00080 
00081 static unsigned int style = NORMAL_COLOR;
00082 
00083 static void clrscr(void)
00084 {
00085         int i;
00086         
00087         for (i=0; i < scr_width*scr_height; i++) {
00088                 scr_addr[i*2] = ' ';
00089                 scr_addr[i*2+1] = style;
00090         }
00091 }
00092 
00093 static void cursor_goto(unsigned int row, unsigned int col)
00094 {
00095         int ega_cursor;
00096 
00097         ega_cursor=col+scr_width*row;
00098         
00099         outb(EGA_IO_ADDRESS    , 0xe);
00100         outb(EGA_IO_ADDRESS + 1, (ega_cursor >>8) & 0xff);
00101         outb(EGA_IO_ADDRESS    , 0xf);
00102         outb(EGA_IO_ADDRESS + 1, ega_cursor & 0xff);
00103 }
00104 
00105 static void cursor_disable(void)
00106 {
00107         uint8_t stat;
00108 
00109         outb(EGA_IO_ADDRESS , 0xa);
00110         stat=inb(EGA_IO_ADDRESS + 1);
00111         outb(EGA_IO_ADDRESS , 0xa);
00112         outb(EGA_IO_ADDRESS +1 ,stat | (1<<5) );
00113 }
00114 
00115 static void cursor_enable(void)
00116 {
00117         uint8_t stat;
00118 
00119         outb(EGA_IO_ADDRESS , 0xa);
00120         stat=inb(EGA_IO_ADDRESS + 1);
00121         outb(EGA_IO_ADDRESS , 0xa);
00122         outb(EGA_IO_ADDRESS +1 ,stat & (~(1<<5)) );
00123 }
00124 
00125 static void scroll(int rows)
00126 {
00127         int i;
00128         if (rows > 0) {
00129                 memcpy (scr_addr,((char *)scr_addr) + rows * scr_width * 2, scr_width * scr_height * 2 - rows * scr_width * 2);
00130                 for (i = 0; i < rows * scr_width ; i ++)
00131                         (((short *)scr_addr) + scr_width * scr_height - rows * scr_width) [i] = ((style << 8) + ' ');
00132         } else if (rows < 0) {
00133 
00134                 memcpy (((char *)scr_addr) - rows * scr_width * 2 ,scr_addr ,scr_width * scr_height * 2 + rows * scr_width * 2);
00135                 for (i = 0; i < - rows * scr_width ; i++)
00136                         ((short *)scr_addr) [i] = ((style << 8 ) + ' ');
00137         }
00138 }
00139 
00140 static void printchar(char c, unsigned int row, unsigned int col)
00141 {
00142         scr_addr[(row*scr_width + col)*2] = c;
00143         scr_addr[(row*scr_width + col)*2+1] = style;
00144         
00145         cursor_goto(row,col+1);
00146 }
00147 
00148 static void draw_text_data(keyfield_t *data)
00149 {
00150         int i;
00151 
00152         for (i=0; i < scr_width*scr_height; i++) {
00153                 scr_addr[i*2] = data[i].character;
00154                 scr_addr[i*2+1] = EGA_STYLE(data[i].style.fg_color, data[i].style.bg_color);
00155         }
00156 }
00157 
00158 static int save_screen(void)
00159 {
00160         int i;
00161 
00162         for (i=0; ( i < MAX_SAVED_SCREENS ) && (saved_screens[i].data); i++)
00163                 ;
00164         if (i == MAX_SAVED_SCREENS) 
00165                 return EINVAL;
00166         if (!(saved_screens[i].data=malloc( 2 * scr_width*scr_height ))) 
00167                 return ENOMEM;
00168         memcpy(saved_screens[i].data, scr_addr, 2 * scr_width * scr_height);
00169 
00170         return i;
00171 }
00172 
00173 static int print_screen(int i)
00174 {
00175         if (saved_screens[i].data)
00176                 memcpy(scr_addr, saved_screens[i].data, 2 * scr_width * scr_height);
00177         else return EINVAL;
00178         return i;
00179 }
00180 
00181 
00182 static void ega_client_connection(ipc_callid_t iid, ipc_call_t *icall)
00183 {
00184         int retval;
00185         ipc_callid_t callid;
00186         ipc_call_t call;
00187         char c;
00188         unsigned int row, col;
00189         int bgcolor,fgcolor;
00190         keyfield_t *interbuf = NULL;
00191         size_t intersize = 0;
00192         int i;
00193 
00194         if (client_connected) {
00195                 ipc_answer_fast(iid, ELIMIT, 0,0);
00196                 return;
00197         }
00198         client_connected = 1;
00199         ipc_answer_fast(iid, 0, 0, 0); /* Accept connection */
00200 
00201         while (1) {
00202                 callid = async_get_call(&call);
00203                 switch (IPC_GET_METHOD(call)) {
00204                 case IPC_M_PHONE_HUNGUP:
00205                         client_connected = 0;
00206                         ipc_answer_fast(callid,0,0,0);
00207                         return; /* Exit thread */
00208                 case IPC_M_AS_AREA_SEND:
00209                         /* We accept one area for data interchange */
00210                         intersize = IPC_GET_ARG2(call);
00211                         if (intersize >= scr_width*scr_height*sizeof(*interbuf)) {
00212                                 receive_comm_area(callid,&call,(void *)&interbuf);
00213                                 continue;
00214                         }
00215                         retval = EINVAL;
00216                         break;
00217                 case FB_DRAW_TEXT_DATA:
00218                         if (!interbuf) {
00219                                 retval = EINVAL;
00220                                 break;
00221                         }
00222                         draw_text_data(interbuf);
00223                         retval = 0;
00224                         break;
00225                 case FB_GET_CSIZE:
00226                         ipc_answer_fast(callid, 0, scr_height, scr_width);
00227                         continue;
00228                 case FB_CLEAR:
00229                         clrscr();
00230                         retval = 0;
00231                         break;
00232                 case FB_PUTCHAR:
00233                         c = IPC_GET_ARG1(call);
00234                         row = IPC_GET_ARG2(call);
00235                         col = IPC_GET_ARG3(call);
00236                         if (col >= scr_width || row >= scr_height) {
00237                                 retval = EINVAL;
00238                                 break;
00239                         }
00240                         printchar(c,row,col);
00241                         retval = 0;
00242                         break;
00243                 case FB_CURSOR_GOTO:
00244                         row = IPC_GET_ARG1(call);
00245                         col = IPC_GET_ARG2(call);
00246                         if (row >= scr_height || col >= scr_width) {
00247                                 retval = EINVAL;
00248                                 break;
00249                         }
00250                         cursor_goto(row,col);
00251                         retval = 0;
00252                         break;
00253                 case FB_SCROLL:
00254                         i = IPC_GET_ARG1(call);
00255                         if (i > scr_height || i < (- (int)scr_height)) {
00256                                 retval = EINVAL;
00257                                 break;
00258                         }
00259                         scroll(i);
00260                         retval = 0;
00261                         break;
00262                 case FB_CURSOR_VISIBILITY:
00263                         if(IPC_GET_ARG1(call))
00264                                 cursor_enable();
00265                         else
00266                                 cursor_disable();
00267                         retval = 0;
00268                         break;
00269                 case FB_SET_STYLE:
00270                         fgcolor = IPC_GET_ARG1(call);
00271                         bgcolor = IPC_GET_ARG2(call);
00272                         style = EGA_STYLE(fgcolor, bgcolor);
00273                         retval = 0;
00274                         break;
00275                 case FB_VP_DRAW_PIXMAP:
00276                         i = IPC_GET_ARG2(call);
00277                         retval = print_screen(i);
00278                         break;
00279                 case FB_VP2PIXMAP:
00280                         retval = save_screen();
00281                         break;
00282                 case FB_DROP_PIXMAP:
00283                         i = IPC_GET_ARG1(call);
00284                         if (i >= MAX_SAVED_SCREENS) {
00285                                 retval = EINVAL;
00286                                 break;
00287                         }
00288                         if (saved_screens[i].data) {
00289                                 free(saved_screens[i].data);
00290                                 saved_screens[i].data = NULL;
00291                         }
00292                         retval = 0;
00293                         break;
00294 
00295                 default:
00296                         retval = ENOENT;
00297                 }
00298                 ipc_answer_fast(callid,retval,0,0);
00299         }
00300 }
00301 
00302 int ega_init(void)
00303 {
00304         void *ega_ph_addr;
00305         size_t sz;
00306 
00307 
00308         ega_ph_addr=(void *)sysinfo_value("fb.address.physical");
00309         scr_width=sysinfo_value("fb.width");
00310         scr_height=sysinfo_value("fb.height");
00311         iospace_enable(task_get_id(),(void *)EGA_IO_ADDRESS,2);
00312 
00313         sz = scr_width*scr_height*2;
00314         scr_addr = as_get_mappable_page(sz);
00315 
00316         map_physmem(ega_ph_addr, scr_addr, ALIGN_UP(sz,PAGE_SIZE)>>PAGE_WIDTH,
00317                     AS_AREA_READ | AS_AREA_WRITE);
00318 
00319         async_set_client_connection(ega_client_connection);
00320 
00321         return 0;
00322 }
00323 
00324 

Generated on Sun Jun 18 17:54:20 2006 for HelenOS Userspace (ia32) by  doxygen 1.4.6