[df688cd] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
[df688cd] | 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 | */
|
---|
[ce5bcb4] | 28 |
|
---|
| 29 | /** @defgroup egafb EGA framebuffer
|
---|
| 30 | * @brief HelenOS EGA framebuffer.
|
---|
| 31 | * @ingroup fbs
|
---|
| 32 | * @{
|
---|
| 33 | */
|
---|
| 34 | /** @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[1160f8d] | 37 | #include <stdlib.h>
|
---|
| 38 | #include <unistd.h>
|
---|
[df688cd] | 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>
|
---|
[15039b67] | 51 | #include <libarch/ddi.h>
|
---|
[df688cd] | 52 |
|
---|
| 53 | #include "ega.h"
|
---|
[0bf84cc] | 54 | #include "../console/screenbuffer.h"
|
---|
| 55 | #include "main.h"
|
---|
[df688cd] | 56 |
|
---|
[1160f8d] | 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 |
|
---|
[dc5a0fe1] | 64 | #define EGA_IO_ADDRESS 0x3d4
|
---|
| 65 | #define EGA_IO_SIZE 2
|
---|
| 66 |
|
---|
[323a5aaf] | 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
|
---|
[d3f2cad] | 72 |
|
---|
| 73 | #define EGA_STYLE(fg,bg) ((fg) > (bg) ? NORMAL_COLOR : INVERTED_COLOR)
|
---|
| 74 |
|
---|
[df688cd] | 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 |
|
---|
[323a5aaf] | 82 | static unsigned int style;
|
---|
[0bf84cc] | 83 |
|
---|
[df688cd] | 84 | static void clrscr(void)
|
---|
| 85 | {
|
---|
| 86 | int i;
|
---|
| 87 |
|
---|
[b74959bd] | 88 | for (i = 0; i < scr_width * scr_height; i++) {
|
---|
[00bb6965] | 89 | scr_addr[i * 2] = ' ';
|
---|
| 90 | scr_addr[i * 2 + 1] = style;
|
---|
[0bf84cc] | 91 | }
|
---|
[df688cd] | 92 | }
|
---|
| 93 |
|
---|
[1160f8d] | 94 | static void cursor_goto(unsigned int row, unsigned int col)
|
---|
[dc5a0fe1] | 95 | {
|
---|
| 96 | int ega_cursor;
|
---|
| 97 |
|
---|
[00bb6965] | 98 | ega_cursor = col + scr_width * row;
|
---|
[dc5a0fe1] | 99 |
|
---|
[00bb6965] | 100 | outb(EGA_IO_ADDRESS, 0xe);
|
---|
| 101 | outb(EGA_IO_ADDRESS + 1, (ega_cursor >> 8) & 0xff);
|
---|
| 102 | outb(EGA_IO_ADDRESS, 0xf);
|
---|
[dc5a0fe1] | 103 | outb(EGA_IO_ADDRESS + 1, ega_cursor & 0xff);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[1160f8d] | 106 | static void cursor_disable(void)
|
---|
[dc5a0fe1] | 107 | {
|
---|
[15039b67] | 108 | uint8_t stat;
|
---|
| 109 |
|
---|
[00bb6965] | 110 | outb(EGA_IO_ADDRESS, 0xa);
|
---|
[dc5a0fe1] | 111 | stat=inb(EGA_IO_ADDRESS + 1);
|
---|
[00bb6965] | 112 | outb(EGA_IO_ADDRESS, 0xa);
|
---|
| 113 | outb(EGA_IO_ADDRESS + 1, stat | (1 << 5));
|
---|
[dc5a0fe1] | 114 | }
|
---|
| 115 |
|
---|
[1160f8d] | 116 | static void cursor_enable(void)
|
---|
[dc5a0fe1] | 117 | {
|
---|
[15039b67] | 118 | uint8_t stat;
|
---|
| 119 |
|
---|
[00bb6965] | 120 | outb(EGA_IO_ADDRESS, 0xa);
|
---|
[dc5a0fe1] | 121 | stat=inb(EGA_IO_ADDRESS + 1);
|
---|
[00bb6965] | 122 | outb(EGA_IO_ADDRESS, 0xa);
|
---|
| 123 | outb(EGA_IO_ADDRESS + 1, stat & (~(1 << 5)));
|
---|
[dc5a0fe1] | 124 | }
|
---|
| 125 |
|
---|
[1160f8d] | 126 | static void scroll(int rows)
|
---|
| 127 | {
|
---|
| 128 | int i;
|
---|
| 129 | if (rows > 0) {
|
---|
[00bb6965] | 130 | memcpy(scr_addr, ((char *) scr_addr) + rows * scr_width * 2,
|
---|
[b74959bd] | 131 | scr_width * scr_height * 2 - rows * scr_width * 2);
|
---|
[00bb6965] | 132 | for (i = 0; i < rows * scr_width; i++)
|
---|
| 133 | (((short *) scr_addr) + scr_width * scr_height - rows *
|
---|
[b74959bd] | 134 | scr_width)[i] = ((style << 8) + ' ');
|
---|
[1160f8d] | 135 | } else if (rows < 0) {
|
---|
[00bb6965] | 136 | memcpy(((char *)scr_addr) - rows * scr_width * 2, scr_addr,
|
---|
[b74959bd] | 137 | scr_width * scr_height * 2 + rows * scr_width * 2);
|
---|
[00bb6965] | 138 | for (i = 0; i < -rows * scr_width; i++)
|
---|
| 139 | ((short *)scr_addr)[i] = ((style << 8 ) + ' ');
|
---|
[1160f8d] | 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[df688cd] | 143 | static void printchar(char c, unsigned int row, unsigned int col)
|
---|
| 144 | {
|
---|
[00bb6965] | 145 | scr_addr[(row * scr_width + col) * 2] = c;
|
---|
| 146 | scr_addr[(row * scr_width + col) * 2 + 1] = style;
|
---|
[dc5a0fe1] | 147 |
|
---|
[00bb6965] | 148 | cursor_goto(row, col + 1);
|
---|
[0bf84cc] | 149 | }
|
---|
| 150 |
|
---|
| 151 | static void draw_text_data(keyfield_t *data)
|
---|
| 152 | {
|
---|
| 153 | int i;
|
---|
| 154 |
|
---|
[00bb6965] | 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,
|
---|
[b74959bd] | 158 | data[i].style.bg_color);
|
---|
[0bf84cc] | 159 | }
|
---|
[df688cd] | 160 | }
|
---|
| 161 |
|
---|
[1160f8d] | 162 | static int save_screen(void)
|
---|
| 163 | {
|
---|
| 164 | int i;
|
---|
[153a209] | 165 |
|
---|
[b74959bd] | 166 | for (i = 0; (i < MAX_SAVED_SCREENS) && (saved_screens[i].data); i++)
|
---|
[153a209] | 167 | ;
|
---|
[1ed15cd] | 168 | if (i == MAX_SAVED_SCREENS)
|
---|
[759c376] | 169 | return EINVAL;
|
---|
[00bb6965] | 170 | if (!(saved_screens[i].data = malloc(2 * scr_width * scr_height)))
|
---|
[759c376] | 171 | return ENOMEM;
|
---|
[153a209] | 172 | memcpy(saved_screens[i].data, scr_addr, 2 * scr_width * scr_height);
|
---|
| 173 |
|
---|
[1160f8d] | 174 | return i;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | static int print_screen(int i)
|
---|
| 178 | {
|
---|
[1ed15cd] | 179 | if (saved_screens[i].data)
|
---|
[00bb6965] | 180 | memcpy(scr_addr, saved_screens[i].data, 2 * scr_width *
|
---|
[b74959bd] | 181 | scr_height);
|
---|
[00bb6965] | 182 | else
|
---|
| 183 | return EINVAL;
|
---|
[1160f8d] | 184 | return i;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 |
|
---|
[df688cd] | 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;
|
---|
[0bf84cc] | 195 | int bgcolor,fgcolor;
|
---|
| 196 | keyfield_t *interbuf = NULL;
|
---|
| 197 | size_t intersize = 0;
|
---|
[1160f8d] | 198 | int i;
|
---|
[df688cd] | 199 |
|
---|
| 200 | if (client_connected) {
|
---|
[b74959bd] | 201 | ipc_answer_0(iid, ELIMIT);
|
---|
[df688cd] | 202 | return;
|
---|
| 203 | }
|
---|
| 204 | client_connected = 1;
|
---|
[b74959bd] | 205 | ipc_answer_0(iid, EOK); /* Accept connection */
|
---|
[df688cd] | 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;
|
---|
[b74959bd] | 212 | ipc_answer_0(callid, EOK);
|
---|
[df688cd] | 213 | return; /* Exit thread */
|
---|
[27d293a] | 214 | case IPC_M_SHARE_OUT:
|
---|
[0bf84cc] | 215 | /* We accept one area for data interchange */
|
---|
| 216 | intersize = IPC_GET_ARG2(call);
|
---|
[00bb6965] | 217 | if (intersize >= scr_width * scr_height *
|
---|
[b74959bd] | 218 | sizeof(*interbuf)) {
|
---|
| 219 | receive_comm_area(callid, &call,
|
---|
| 220 | (void *) &interbuf);
|
---|
[0bf84cc] | 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;
|
---|
[df688cd] | 233 | case FB_GET_CSIZE:
|
---|
[b74959bd] | 234 | ipc_answer_2(callid, EOK, scr_height, scr_width);
|
---|
[df688cd] | 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);
|
---|
[0bf84cc] | 244 | if (col >= scr_width || row >= scr_height) {
|
---|
[df688cd] | 245 | retval = EINVAL;
|
---|
| 246 | break;
|
---|
| 247 | }
|
---|
[00bb6965] | 248 | printchar(c, row, col);
|
---|
[df688cd] | 249 | retval = 0;
|
---|
| 250 | break;
|
---|
[dc5a0fe1] | 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 | }
|
---|
[00bb6965] | 258 | cursor_goto(row, col);
|
---|
[dc5a0fe1] | 259 | retval = 0;
|
---|
| 260 | break;
|
---|
[1160f8d] | 261 | case FB_SCROLL:
|
---|
| 262 | i = IPC_GET_ARG1(call);
|
---|
[00bb6965] | 263 | if (i > scr_height || i < -((int) scr_height)) {
|
---|
[1160f8d] | 264 | retval = EINVAL;
|
---|
| 265 | break;
|
---|
| 266 | }
|
---|
| 267 | scroll(i);
|
---|
| 268 | retval = 0;
|
---|
| 269 | break;
|
---|
[dc5a0fe1] | 270 | case FB_CURSOR_VISIBILITY:
|
---|
| 271 | if(IPC_GET_ARG1(call))
|
---|
| 272 | cursor_enable();
|
---|
| 273 | else
|
---|
| 274 | cursor_disable();
|
---|
| 275 | retval = 0;
|
---|
| 276 | break;
|
---|
[0bf84cc] | 277 | case FB_SET_STYLE:
|
---|
| 278 | fgcolor = IPC_GET_ARG1(call);
|
---|
| 279 | bgcolor = IPC_GET_ARG2(call);
|
---|
[d3f2cad] | 280 | style = EGA_STYLE(fgcolor, bgcolor);
|
---|
[153a209] | 281 | retval = 0;
|
---|
[ec9623d] | 282 | break;
|
---|
[1160f8d] | 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 | }
|
---|
[153a209] | 300 | retval = 0;
|
---|
[1160f8d] | 301 | break;
|
---|
| 302 |
|
---|
[df688cd] | 303 | default:
|
---|
| 304 | retval = ENOENT;
|
---|
| 305 | }
|
---|
[b74959bd] | 306 | ipc_answer_0(callid, retval);
|
---|
[df688cd] | 307 | }
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | int ega_init(void)
|
---|
| 311 | {
|
---|
| 312 | void *ega_ph_addr;
|
---|
[bf9afa07] | 313 | size_t sz;
|
---|
[df688cd] | 314 |
|
---|
[00bb6965] | 315 | ega_ph_addr = (void *) sysinfo_value("fb.address.physical");
|
---|
| 316 | scr_width = sysinfo_value("fb.width");
|
---|
| 317 | scr_height = sysinfo_value("fb.height");
|
---|
[323a5aaf] | 318 | if(sysinfo_value("fb.blinking"))
|
---|
| 319 | {
|
---|
| 320 | ega_normal_color&=0x77;
|
---|
| 321 | ega_inverted_color&=0x77;
|
---|
| 322 | }
|
---|
| 323 | style = NORMAL_COLOR;
|
---|
| 324 |
|
---|
[f8ddd17] | 325 | iospace_enable(task_get_id(), (void *) EGA_IO_ADDRESS, 2);
|
---|
[df688cd] | 326 |
|
---|
[f8ddd17] | 327 | sz = scr_width * scr_height * 2;
|
---|
[2057572] | 328 | scr_addr = as_get_mappable_page(sz);
|
---|
[df688cd] | 329 |
|
---|
[f8ddd17] | 330 | physmem_map(ega_ph_addr, scr_addr, ALIGN_UP(sz, PAGE_SIZE) >>
|
---|
[b74959bd] | 331 | PAGE_WIDTH, AS_AREA_READ | AS_AREA_WRITE);
|
---|
[df688cd] | 332 |
|
---|
| 333 | async_set_client_connection(ega_client_connection);
|
---|
| 334 |
|
---|
| 335 | return 0;
|
---|
| 336 | }
|
---|
[dc5a0fe1] | 337 |
|
---|
[ce5bcb4] | 338 |
|
---|
| 339 | /**
|
---|
| 340 | * @}
|
---|
| 341 | */
|
---|