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 
00037 #include <stdlib.h>
00038 #include <unistd.h>
00039 #include <align.h>
00040 #include <async.h>
00041 #include <errno.h>
00042 #include <stdio.h>
00043 #include <ddi.h>
00044 #include <sysinfo.h>
00045 #include <as.h>
00046 #include <ipc/fb.h>
00047 #include <ipc/ns.h>
00048 #include <ipc/services.h>
00049 #include <libarch/ddi.h>
00050 #include <io/style.h>
00051 #include <io/color.h>
00052 #include <io/screenbuffer.h>
00053 #include <sys/types.h>
00054 
00055 #include "ega.h"
00056 #include "main.h"
00057 
00058 #define MAX_SAVED_SCREENS  256
00059 
00060 typedef struct saved_screen {
00061         short *data;
00062 } saved_screen;
00063 
00064 saved_screen saved_screens[MAX_SAVED_SCREENS];
00065 
00066 #define EGA_IO_BASE  ((ioport8_t *) 0x3d4)
00067 #define EGA_IO_SIZE  2
00068 
00069 /* Allow only 1 connection */
00070 static int client_connected = 0;
00071 
00072 static sysarg_t scr_width;
00073 static sysarg_t scr_height;
00074 static uint8_t *scr_addr;
00075 
00076 static uint8_t style_normal = 0xf0;
00077 static uint8_t style_inverted = 0x0f;
00078 static uint8_t style;
00079 
00080 static uint8_t style_to_ega_style(uint8_t style)
00081 {
00082         switch (style) {
00083         case STYLE_EMPHASIS:
00084                 return (style_normal | 0x04);
00085         case STYLE_SELECTED:
00086                 return (style_inverted | 0x40);
00087         case STYLE_INVERTED:
00088                 return style_inverted;
00089         }
00090         
00091         return style_normal;
00092 }
00093 
00094 static uint8_t color_to_ega_style(uint8_t fg_color, uint8_t bg_color,
00095     uint8_t attr)
00096 {
00097         uint8_t style = (fg_color & 7) | ((bg_color & 7) << 4);
00098         
00099         if (attr & CATTR_BRIGHT)
00100                 style |= 0x08;
00101         
00102         return style;
00103 }
00104 
00105 static uint8_t rgb_to_ega_style(uint32_t fg, uint32_t bg)
00106 {
00107         return (fg > bg) ? style_inverted : style_normal;
00108 }
00109 
00110 static uint8_t attr_to_ega_style(const attrs_t *attr)
00111 {
00112         switch (attr->t) {
00113         case at_style:
00114                 return style_to_ega_style(attr->a.s.style);
00115         case at_idx:
00116                 return color_to_ega_style(attr->a.i.fg_color,
00117                     attr->a.i.bg_color, attr->a.i.flags);
00118         case at_rgb:
00119                 return rgb_to_ega_style(attr->a.r.fg_color, attr->a.r.bg_color);
00120         default:
00121                 return style_normal;
00122         }
00123 }
00124 
00125 static uint8_t ega_glyph(wchar_t ch)
00126 {
00127         if (ch >= 0 && ch < 128)
00128                 return ch;
00129         
00130         return '?';
00131 }
00132 
00133 static void clrscr(void)
00134 {
00135         unsigned i;
00136         
00137         for (i = 0; i < scr_width * scr_height; i++) {
00138                 scr_addr[i * 2] = ' ';
00139                 scr_addr[i * 2 + 1] = style;
00140         }
00141 }
00142 
00143 static void cursor_goto(sysarg_t col, sysarg_t row)
00144 {
00145         sysarg_t cursor = col + scr_width * row;
00146         
00147         pio_write_8(EGA_IO_BASE, 0xe);
00148         pio_write_8(EGA_IO_BASE + 1, (cursor >> 8) & 0xff);
00149         pio_write_8(EGA_IO_BASE, 0xf);
00150         pio_write_8(EGA_IO_BASE + 1, cursor & 0xff);
00151 }
00152 
00153 static void cursor_disable(void)
00154 {
00155         pio_write_8(EGA_IO_BASE, 0xa);
00156         
00157         uint8_t stat = pio_read_8(EGA_IO_BASE + 1);
00158         
00159         pio_write_8(EGA_IO_BASE, 0xa);
00160         pio_write_8(EGA_IO_BASE + 1, stat | (1 << 5));
00161 }
00162 
00163 static void cursor_enable(void)
00164 {
00165         pio_write_8(EGA_IO_BASE, 0xa);
00166         
00167         uint8_t stat = pio_read_8(EGA_IO_BASE + 1);
00168         
00169         pio_write_8(EGA_IO_BASE, 0xa);
00170         pio_write_8(EGA_IO_BASE + 1, stat & (~(1 << 5)));
00171 }
00172 
00173 static void scroll(ssize_t rows)
00174 {
00175         size_t i;
00176         
00177         if (rows > 0) {
00178                 memmove(scr_addr, ((char *) scr_addr) + rows * scr_width * 2,
00179                     scr_width * scr_height * 2 - rows * scr_width * 2);
00180                 for (i = 0; i < rows * scr_width; i++)
00181                         (((short *) scr_addr) + scr_width * scr_height - rows *
00182                             scr_width)[i] = ((style << 8) + ' ');
00183         } else if (rows < 0) {
00184                 memmove(((char *)scr_addr) - rows * scr_width * 2, scr_addr,
00185                     scr_width * scr_height * 2 + rows * scr_width * 2);
00186                 for (i = 0; i < -rows * scr_width; i++)
00187                         ((short *) scr_addr)[i] = ((style << 8 ) + ' ');
00188         }
00189 }
00190 
00191 static void printchar(wchar_t c, sysarg_t col, sysarg_t row)
00192 {
00193         scr_addr[(row * scr_width + col) * 2] = ega_glyph(c);
00194         scr_addr[(row * scr_width + col) * 2 + 1] = style;
00195         
00196         cursor_goto(col + 1, row);
00197 }
00198 
00209 static void draw_text_data(keyfield_t *data, sysarg_t x, sysarg_t y,
00210     sysarg_t w, sysarg_t h)
00211 {
00212         sysarg_t i;
00213         sysarg_t j;
00214         keyfield_t *field;
00215         uint8_t *dp;
00216         
00217         for (j = 0; j < h; j++) {
00218                 for (i = 0; i < w; i++) {
00219                         field = &data[j * w + i];
00220                         dp = &scr_addr[2 * ((y + j) * scr_width + (x + i))];
00221                         
00222                         dp[0] = ega_glyph(field->character);
00223                         dp[1] = attr_to_ega_style(&field->attrs);
00224                 }
00225         }
00226 }
00227 
00228 static int save_screen(void)
00229 {
00230         sysarg_t i;
00231         
00232         /* Find empty screen */
00233         for (i = 0; (i < MAX_SAVED_SCREENS) && (saved_screens[i].data); i++);
00234         
00235         if (i == MAX_SAVED_SCREENS)
00236                 return EINVAL;
00237         
00238         if (!(saved_screens[i].data = malloc(2 * scr_width * scr_height)))
00239                 return ENOMEM;
00240         
00241         memcpy(saved_screens[i].data, scr_addr, 2 * scr_width * scr_height);
00242         return (int) i;
00243 }
00244 
00245 static int print_screen(sysarg_t i)
00246 {
00247         if ((i >= MAX_SAVED_SCREENS) || (saved_screens[i].data))
00248                 memcpy(scr_addr, saved_screens[i].data, 2 * scr_width *
00249                     scr_height);
00250         else
00251                 return EINVAL;
00252         
00253         return (int) i;
00254 }
00255 
00256 static void ega_client_connection(ipc_callid_t iid, ipc_call_t *icall)
00257 {
00258         size_t intersize = 0;
00259         keyfield_t *interbuf = NULL;
00260         
00261         if (client_connected) {
00262                 async_answer_0(iid, ELIMIT);
00263                 return;
00264         }
00265         
00266         /* Accept connection */
00267         client_connected = 1;
00268         async_answer_0(iid, EOK);
00269         
00270         while (true) {
00271                 ipc_call_t call;
00272                 ipc_callid_t callid = async_get_call(&call);
00273                 
00274                 wchar_t c;
00275                 
00276                 sysarg_t col;
00277                 sysarg_t row;
00278                 sysarg_t w;
00279                 sysarg_t h;
00280                 
00281                 ssize_t rows;
00282                 
00283                 uint8_t bg_color;
00284                 uint8_t fg_color;
00285                 uint8_t attr;
00286                 
00287                 uint32_t fg_rgb;
00288                 uint32_t bg_rgb;
00289                 
00290                 sysarg_t scr;
00291                 int retval;
00292                 
00293                 switch (IPC_GET_IMETHOD(call)) {
00294                 case IPC_M_PHONE_HUNGUP:
00295                         client_connected = 0;
00296                         async_answer_0(callid, EOK);
00297                         
00298                         /* Exit thread */
00299                         return;
00300                 case IPC_M_SHARE_OUT:
00301                         /* We accept one area for data interchange */
00302                         intersize = IPC_GET_ARG2(call);
00303                         if (intersize >= scr_width * scr_height *
00304                             sizeof(*interbuf)) {
00305                                 receive_comm_area(callid, &call,
00306                                     (void *) &interbuf);
00307                                 continue;
00308                         }
00309                         
00310                         retval = EINVAL;
00311                         break;
00312                 case FB_DRAW_TEXT_DATA:
00313                         if (!interbuf) {
00314                                 retval = EINVAL;
00315                                 break;
00316                         }
00317                         
00318                         col = IPC_GET_ARG1(call);
00319                         row = IPC_GET_ARG2(call);
00320                         w = IPC_GET_ARG3(call);
00321                         h = IPC_GET_ARG4(call);
00322                         
00323                         if ((col + w > scr_width) || (row + h > scr_height)) {
00324                                 retval = EINVAL;
00325                                 break;
00326                         }
00327                         
00328                         draw_text_data(interbuf, col, row, w, h);
00329                         retval = 0;
00330                         break;
00331                 case FB_GET_CSIZE:
00332                         async_answer_2(callid, EOK, scr_width, scr_height);
00333                         continue;
00334                 case FB_GET_COLOR_CAP:
00335                         async_answer_1(callid, EOK, FB_CCAP_INDEXED);
00336                         continue;
00337                 case FB_CLEAR:
00338                         clrscr();
00339                         retval = 0;
00340                         break;
00341                 case FB_PUTCHAR:
00342                         c = IPC_GET_ARG1(call);
00343                         col = IPC_GET_ARG2(call);
00344                         row = IPC_GET_ARG3(call);
00345                         
00346                         if ((col >= scr_width) || (row >= scr_height)) {
00347                                 retval = EINVAL;
00348                                 break;
00349                         }
00350                         
00351                         printchar(c, col, row);
00352                         retval = 0;
00353                         break;
00354                 case FB_CURSOR_GOTO:
00355                         col = IPC_GET_ARG1(call);
00356                         row = IPC_GET_ARG2(call);
00357                         
00358                         if ((row >= scr_height) || (col >= scr_width)) {
00359                                 retval = EINVAL;
00360                                 break;
00361                         }
00362                         
00363                         cursor_goto(col, row);
00364                         retval = 0;
00365                         break;
00366                 case FB_SCROLL:
00367                         rows = IPC_GET_ARG1(call);
00368                         
00369                         if (rows >= 0) {
00370                                 if ((sysarg_t) rows > scr_height) {
00371                                         retval = EINVAL;
00372                                         break;
00373                                 }
00374                         } else {
00375                                 if ((sysarg_t) (-rows) > scr_height) {
00376                                         retval = EINVAL;
00377                                         break;
00378                                 }
00379                         }
00380                         
00381                         scroll(rows);
00382                         retval = 0;
00383                         break;
00384                 case FB_CURSOR_VISIBILITY:
00385                         if (IPC_GET_ARG1(call))
00386                                 cursor_enable();
00387                         else
00388                                 cursor_disable();
00389                         
00390                         retval = 0;
00391                         break;
00392                 case FB_SET_STYLE:
00393                         style = style_to_ega_style(IPC_GET_ARG1(call));
00394                         retval = 0;
00395                         break;
00396                 case FB_SET_COLOR:
00397                         fg_color = IPC_GET_ARG1(call);
00398                         bg_color = IPC_GET_ARG2(call);
00399                         attr = IPC_GET_ARG3(call);
00400                         
00401                         style = color_to_ega_style(fg_color, bg_color, attr);
00402                         retval = 0;
00403                         break;
00404                 case FB_SET_RGB_COLOR:
00405                         fg_rgb = IPC_GET_ARG1(call);
00406                         bg_rgb = IPC_GET_ARG2(call);
00407                         
00408                         style = rgb_to_ega_style(fg_rgb, bg_rgb);
00409                         retval = 0;
00410                         break;
00411                 case FB_VP_DRAW_PIXMAP:
00412                         scr = IPC_GET_ARG2(call);
00413                         retval = print_screen(scr);
00414                         break;
00415                 case FB_VP2PIXMAP:
00416                         retval = save_screen();
00417                         break;
00418                 case FB_DROP_PIXMAP:
00419                         scr = IPC_GET_ARG1(call);
00420                         
00421                         if (scr >= MAX_SAVED_SCREENS) {
00422                                 retval = EINVAL;
00423                                 break;
00424                         }
00425                         
00426                         if (saved_screens[scr].data) {
00427                                 free(saved_screens[scr].data);
00428                                 saved_screens[scr].data = NULL;
00429                         }
00430                         
00431                         retval = 0;
00432                         break;
00433                 case FB_SCREEN_YIELD:
00434                 case FB_SCREEN_RECLAIM:
00435                         retval = EOK;
00436                         break;
00437                 default:
00438                         retval = EINVAL;
00439                 }
00440                 async_answer_0(callid, retval);
00441         }
00442 }
00443 
00444 int ega_init(void)
00445 {
00446         sysarg_t paddr;
00447         if (sysinfo_get_value("fb.address.physical", &paddr) != EOK)
00448                 return -1;
00449         
00450         if (sysinfo_get_value("fb.width", &scr_width) != EOK)
00451                 return -1;
00452         
00453         if (sysinfo_get_value("fb.height", &scr_height) != EOK)
00454                 return -1;
00455         
00456         sysarg_t blinking;
00457         if (sysinfo_get_value("fb.blinking", &blinking) != EOK)
00458                 blinking = false;
00459         
00460         void *ega_ph_addr = (void *) paddr;
00461         if (blinking) {
00462                 style_normal &= 0x77;
00463                 style_inverted &= 0x77;
00464         }
00465         
00466         style = style_normal;
00467         
00468         iospace_enable(task_get_id(), (void *) EGA_IO_BASE, 2);
00469         
00470         size_t sz = scr_width * scr_height * 2;
00471         scr_addr = as_get_mappable_page(sz);
00472         
00473         if (physmem_map(ega_ph_addr, scr_addr, ALIGN_UP(sz, PAGE_SIZE) >>
00474             PAGE_WIDTH, AS_AREA_READ | AS_AREA_WRITE) != 0)
00475                 return -1;
00476         
00477         async_set_client_connection(ega_client_connection);
00478         
00479         return 0;
00480 }
00481 

Generated on Thu Jun 2 07:45:49 2011 for HelenOS/USB by  doxygen 1.4.7