source: mainline/uspace/srv/hid/fb/ega.c@ 7282582

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7282582 was 369a5f8, checked in by Martin Decky <martin@…>, 16 years ago

add STYLE_SELECTED and STYLE_INVERTED into serial output driver
move screenbuffer.{c|h} into libc, make it slightly more generic
collateral changes

  • Property mode set to 100644
File size: 10.5 KB
RevLine 
[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
[9f1362d4]30 * @brief HelenOS EGA framebuffer.
[ce5bcb4]31 * @ingroup fbs
32 * @{
[9f1362d4]33 */
[ce5bcb4]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>
[8dc12ac]52#include <io/style.h>
53#include <io/color.h>
[369a5f8]54#include <io/screenbuffer.h>
[9cd98796]55#include <sys/types.h>
[df688cd]56
57#include "ega.h"
[0bf84cc]58#include "main.h"
[df688cd]59
[9f1362d4]60#define MAX_SAVED_SCREENS 256
61
[1160f8d]62typedef struct saved_screen {
63 short *data;
64} saved_screen;
65
66saved_screen saved_screens[MAX_SAVED_SCREENS];
67
[9f1362d4]68#define EGA_IO_BASE ((ioport8_t *) 0x3d4)
69#define EGA_IO_SIZE 2
[d3f2cad]70
[df688cd]71/* Allow only 1 connection */
72static int client_connected = 0;
73
[9f1362d4]74static sysarg_t scr_width;
75static sysarg_t scr_height;
[dc033a1]76static uint8_t *scr_addr;
[df688cd]77
[9f1362d4]78static uint8_t style_normal = 0xf0;
79static uint8_t style_inverted = 0x0f;
80static uint8_t style;
81
82static uint8_t style_to_ega_style(uint8_t style)
83{
84 switch (style) {
85 case STYLE_EMPHASIS:
86 return (style_normal | 0x04);
87 case STYLE_SELECTED:
88 return (style_inverted | 0x40);
89 case STYLE_INVERTED:
90 return style_inverted;
91 }
92
93 return style_normal;
94}
95
96static uint8_t color_to_ega_style(uint8_t fg_color, uint8_t bg_color,
97 uint8_t attr)
98{
99 uint8_t style = (fg_color & 7) | ((bg_color & 7) << 4);
100
101 if (attr & CATTR_BRIGHT)
102 style |= 0x08;
103
104 return style;
105}
106
107static uint8_t rgb_to_ega_style(uint32_t fg, uint32_t bg)
108{
109 return (fg > bg) ? style_inverted : style_normal;
110}
111
112static uint8_t attr_to_ega_style(const attrs_t *attr)
113{
114 switch (attr->t) {
115 case at_style:
116 return style_to_ega_style(attr->a.s.style);
117 case at_idx:
118 return color_to_ega_style(attr->a.i.fg_color,
119 attr->a.i.bg_color, attr->a.i.flags);
120 case at_rgb:
121 return rgb_to_ega_style(attr->a.r.fg_color, attr->a.r.bg_color);
122 default:
123 return style_normal;
124 }
125}
[0bf84cc]126
[9f1362d4]127static uint8_t ega_glyph(wchar_t ch)
128{
129 if (ch >= 0 && ch < 128)
130 return ch;
131
132 return '?';
133}
[198a9ef]134
[df688cd]135static void clrscr(void)
136{
[0471786]137 unsigned i;
[df688cd]138
[b74959bd]139 for (i = 0; i < scr_width * scr_height; i++) {
[00bb6965]140 scr_addr[i * 2] = ' ';
141 scr_addr[i * 2 + 1] = style;
[0bf84cc]142 }
[df688cd]143}
144
[9f1362d4]145static void cursor_goto(sysarg_t col, sysarg_t row)
[dc5a0fe1]146{
[9f1362d4]147 sysarg_t cursor = col + scr_width * row;
[dc5a0fe1]148
[9cd98796]149 pio_write_8(EGA_IO_BASE, 0xe);
[9f1362d4]150 pio_write_8(EGA_IO_BASE + 1, (cursor >> 8) & 0xff);
[9cd98796]151 pio_write_8(EGA_IO_BASE, 0xf);
[9f1362d4]152 pio_write_8(EGA_IO_BASE + 1, cursor & 0xff);
[dc5a0fe1]153}
154
[1160f8d]155static void cursor_disable(void)
[dc5a0fe1]156{
[9cd98796]157 pio_write_8(EGA_IO_BASE, 0xa);
[9f1362d4]158
159 uint8_t stat = pio_read_8(EGA_IO_BASE + 1);
160
[9cd98796]161 pio_write_8(EGA_IO_BASE, 0xa);
162 pio_write_8(EGA_IO_BASE + 1, stat | (1 << 5));
[dc5a0fe1]163}
164
[1160f8d]165static void cursor_enable(void)
[dc5a0fe1]166{
[9cd98796]167 pio_write_8(EGA_IO_BASE, 0xa);
[9f1362d4]168
169 uint8_t stat = pio_read_8(EGA_IO_BASE + 1);
170
[9cd98796]171 pio_write_8(EGA_IO_BASE, 0xa);
172 pio_write_8(EGA_IO_BASE + 1, stat & (~(1 << 5)));
[dc5a0fe1]173}
174
[9f1362d4]175static void scroll(ssize_t rows)
[1160f8d]176{
[9f1362d4]177 size_t i;
178
[1160f8d]179 if (rows > 0) {
[8b74af07]180 memmove(scr_addr, ((char *) scr_addr) + rows * scr_width * 2,
[b74959bd]181 scr_width * scr_height * 2 - rows * scr_width * 2);
[00bb6965]182 for (i = 0; i < rows * scr_width; i++)
183 (((short *) scr_addr) + scr_width * scr_height - rows *
[b74959bd]184 scr_width)[i] = ((style << 8) + ' ');
[1160f8d]185 } else if (rows < 0) {
[8b74af07]186 memmove(((char *)scr_addr) - rows * scr_width * 2, scr_addr,
[b74959bd]187 scr_width * scr_height * 2 + rows * scr_width * 2);
[00bb6965]188 for (i = 0; i < -rows * scr_width; i++)
[9f1362d4]189 ((short *) scr_addr)[i] = ((style << 8 ) + ' ');
[1160f8d]190 }
191}
192
[9f1362d4]193static void printchar(wchar_t c, sysarg_t col, sysarg_t row)
[df688cd]194{
[b27eb71]195 scr_addr[(row * scr_width + col) * 2] = ega_glyph(c);
[00bb6965]196 scr_addr[(row * scr_width + col) * 2 + 1] = style;
[dc5a0fe1]197
[ba2a055]198 cursor_goto(col + 1, row);
[0bf84cc]199}
200
[dc033a1]201/** Draw text data to viewport.
202 *
203 * @param vport Viewport id
204 * @param data Text data.
[9f1362d4]205 * @param x Leftmost column of the area.
206 * @param y Topmost row of the area.
207 * @param w Number of rows.
208 * @param h Number of columns.
209 *
[dc033a1]210 */
[9f1362d4]211static void draw_text_data(keyfield_t *data, sysarg_t x, sysarg_t y,
212 sysarg_t w, sysarg_t h)
[0bf84cc]213{
[9f1362d4]214 sysarg_t i;
215 sysarg_t j;
[dc033a1]216 keyfield_t *field;
217 uint8_t *dp;
[9f1362d4]218
[dc033a1]219 for (j = 0; j < h; j++) {
220 for (i = 0; i < w; i++) {
221 field = &data[j * w + i];
222 dp = &scr_addr[2 * ((y + j) * scr_width + (x + i))];
[9f1362d4]223
[b27eb71]224 dp[0] = ega_glyph(field->character);
[dc033a1]225 dp[1] = attr_to_ega_style(&field->attrs);
226 }
[0bf84cc]227 }
[df688cd]228}
229
[1160f8d]230static int save_screen(void)
231{
[9f1362d4]232 ipcarg_t i;
233
234 /* Find empty screen */
235 for (i = 0; (i < MAX_SAVED_SCREENS) && (saved_screens[i].data); i++);
236
237 if (i == MAX_SAVED_SCREENS)
[759c376]238 return EINVAL;
[9f1362d4]239
240 if (!(saved_screens[i].data = malloc(2 * scr_width * scr_height)))
[759c376]241 return ENOMEM;
[9f1362d4]242
[153a209]243 memcpy(saved_screens[i].data, scr_addr, 2 * scr_width * scr_height);
[9f1362d4]244 return (int) i;
[1160f8d]245}
246
[9f1362d4]247static int print_screen(ipcarg_t i)
[1160f8d]248{
[9f1362d4]249 if ((i >= MAX_SAVED_SCREENS) || (saved_screens[i].data))
[00bb6965]250 memcpy(scr_addr, saved_screens[i].data, 2 * scr_width *
[b74959bd]251 scr_height);
[00bb6965]252 else
253 return EINVAL;
[9f1362d4]254
255 return (int) i;
[b27eb71]256}
257
[df688cd]258static void ega_client_connection(ipc_callid_t iid, ipc_call_t *icall)
259{
[0bf84cc]260 size_t intersize = 0;
[9f1362d4]261 keyfield_t *interbuf = NULL;
[369a5f8]262
[df688cd]263 if (client_connected) {
[b74959bd]264 ipc_answer_0(iid, ELIMIT);
[df688cd]265 return;
266 }
[9f1362d4]267
268 /* Accept connection */
[df688cd]269 client_connected = 1;
[9f1362d4]270 ipc_answer_0(iid, EOK);
271
272 while (true) {
273 ipc_call_t call;
274 ipc_callid_t callid = async_get_call(&call);
275
276 wchar_t c;
277
278 ipcarg_t col;
279 ipcarg_t row;
280 ipcarg_t w;
281 ipcarg_t h;
282
283 ssize_t rows;
284
285 uint8_t bg_color;
286 uint8_t fg_color;
287 uint8_t attr;
288
289 uint32_t fg_rgb;
290 uint32_t bg_rgb;
291
292 ipcarg_t scr;
293 int retval;
294
295 switch (IPC_GET_METHOD(call)) {
[df688cd]296 case IPC_M_PHONE_HUNGUP:
297 client_connected = 0;
[b74959bd]298 ipc_answer_0(callid, EOK);
[9f1362d4]299
300 /* Exit thread */
301 return;
[27d293a]302 case IPC_M_SHARE_OUT:
[0bf84cc]303 /* We accept one area for data interchange */
304 intersize = IPC_GET_ARG2(call);
[00bb6965]305 if (intersize >= scr_width * scr_height *
[b74959bd]306 sizeof(*interbuf)) {
307 receive_comm_area(callid, &call,
308 (void *) &interbuf);
[0bf84cc]309 continue;
310 }
[9f1362d4]311
[0bf84cc]312 retval = EINVAL;
313 break;
314 case FB_DRAW_TEXT_DATA:
315 if (!interbuf) {
316 retval = EINVAL;
317 break;
318 }
[9f1362d4]319
320 col = IPC_GET_ARG1(call);
321 row = IPC_GET_ARG2(call);
322 w = IPC_GET_ARG3(call);
323 h = IPC_GET_ARG4(call);
324
325 if ((col + w > scr_width) || (row + h > scr_height)) {
[dc033a1]326 retval = EINVAL;
327 break;
328 }
[9f1362d4]329
[dc033a1]330 draw_text_data(interbuf, col, row, w, h);
[0bf84cc]331 retval = 0;
332 break;
[df688cd]333 case FB_GET_CSIZE:
[ba2a055]334 ipc_answer_2(callid, EOK, scr_width, scr_height);
[df688cd]335 continue;
[50cfa6c]336 case FB_GET_COLOR_CAP:
337 ipc_answer_1(callid, EOK, FB_CCAP_INDEXED);
338 continue;
[df688cd]339 case FB_CLEAR:
340 clrscr();
341 retval = 0;
342 break;
343 case FB_PUTCHAR:
344 c = IPC_GET_ARG1(call);
[8dc12ac]345 col = IPC_GET_ARG2(call);
346 row = IPC_GET_ARG3(call);
[9f1362d4]347
348 if ((col >= scr_width) || (row >= scr_height)) {
[df688cd]349 retval = EINVAL;
350 break;
351 }
[9f1362d4]352
[8dc12ac]353 printchar(c, col, row);
[df688cd]354 retval = 0;
355 break;
[9f1362d4]356 case FB_CURSOR_GOTO:
357 col = IPC_GET_ARG1(call);
[8dc12ac]358 row = IPC_GET_ARG2(call);
[9f1362d4]359
360 if ((row >= scr_height) || (col >= scr_width)) {
[dc5a0fe1]361 retval = EINVAL;
362 break;
363 }
[9f1362d4]364
[8dc12ac]365 cursor_goto(col, row);
[9f1362d4]366 retval = 0;
367 break;
[1160f8d]368 case FB_SCROLL:
[9f1362d4]369 rows = IPC_GET_ARG1(call);
370
371 if (rows >= 0) {
372 if ((ipcarg_t) rows > scr_height) {
373 retval = EINVAL;
374 break;
375 }
376 } else {
377 if ((ipcarg_t) (-rows) > scr_height) {
378 retval = EINVAL;
379 break;
380 }
[1160f8d]381 }
[9f1362d4]382
383 scroll(rows);
[1160f8d]384 retval = 0;
385 break;
[dc5a0fe1]386 case FB_CURSOR_VISIBILITY:
[13ec8055]387 if (IPC_GET_ARG1(call))
[dc5a0fe1]388 cursor_enable();
389 else
390 cursor_disable();
[9f1362d4]391
[dc5a0fe1]392 retval = 0;
393 break;
[0bf84cc]394 case FB_SET_STYLE:
[198a9ef]395 style = style_to_ega_style(IPC_GET_ARG1(call));
[9805cde]396 retval = 0;
397 break;
398 case FB_SET_COLOR:
[198a9ef]399 fg_color = IPC_GET_ARG1(call);
400 bg_color = IPC_GET_ARG2(call);
401 attr = IPC_GET_ARG3(call);
[9f1362d4]402
[198a9ef]403 style = color_to_ega_style(fg_color, bg_color, attr);
[9805cde]404 retval = 0;
405 break;
406 case FB_SET_RGB_COLOR:
[198a9ef]407 fg_rgb = IPC_GET_ARG1(call);
408 bg_rgb = IPC_GET_ARG2(call);
[9f1362d4]409
[198a9ef]410 style = rgb_to_ega_style(fg_rgb, bg_rgb);
[153a209]411 retval = 0;
[ec9623d]412 break;
[1160f8d]413 case FB_VP_DRAW_PIXMAP:
[9f1362d4]414 scr = IPC_GET_ARG2(call);
415 retval = print_screen(scr);
[1160f8d]416 break;
417 case FB_VP2PIXMAP:
418 retval = save_screen();
419 break;
420 case FB_DROP_PIXMAP:
[9f1362d4]421 scr = IPC_GET_ARG1(call);
422
423 if (scr >= MAX_SAVED_SCREENS) {
[1160f8d]424 retval = EINVAL;
425 break;
426 }
[9f1362d4]427
428 if (saved_screens[scr].data) {
429 free(saved_screens[scr].data);
430 saved_screens[scr].data = NULL;
[1160f8d]431 }
[9f1362d4]432
[153a209]433 retval = 0;
[1160f8d]434 break;
[ebfabf6]435 case FB_SCREEN_YIELD:
436 case FB_SCREEN_RECLAIM:
[10270a8]437 retval = EOK;
438 break;
[df688cd]439 default:
[d2cc7e1]440 retval = EINVAL;
[df688cd]441 }
[b74959bd]442 ipc_answer_0(callid, retval);
[df688cd]443 }
444}
445
446int ega_init(void)
447{
[d9fae235]448 sysarg_t paddr;
449 if (sysinfo_get_value("fb.address.physical", &paddr) != EOK)
450 return -1;
451
[9f1362d4]452 if (sysinfo_get_value("fb.width", &scr_width) != EOK)
[d9fae235]453 return -1;
454
[9f1362d4]455 if (sysinfo_get_value("fb.height", &scr_height) != EOK)
[d9fae235]456 return -1;
457
458 sysarg_t blinking;
459 if (sysinfo_get_value("fb.blinking", &blinking) != EOK)
460 blinking = false;
461
[9f1362d4]462 void *ega_ph_addr = (void *) paddr;
[d9fae235]463 if (blinking) {
[9f1362d4]464 style_normal &= 0x77;
465 style_inverted &= 0x77;
[323a5aaf]466 }
[d9fae235]467
[9f1362d4]468 style = style_normal;
[d9fae235]469
[9cd98796]470 iospace_enable(task_get_id(), (void *) EGA_IO_BASE, 2);
[d9fae235]471
[9f1362d4]472 size_t sz = scr_width * scr_height * 2;
[2057572]473 scr_addr = as_get_mappable_page(sz);
[d9fae235]474
[ae318d3]475 if (physmem_map(ega_ph_addr, scr_addr, ALIGN_UP(sz, PAGE_SIZE) >>
476 PAGE_WIDTH, AS_AREA_READ | AS_AREA_WRITE) != 0)
477 return -1;
[9f1362d4]478
[df688cd]479 async_set_client_connection(ega_client_connection);
[9f1362d4]480
[df688cd]481 return 0;
482}
[dc5a0fe1]483
[13ec8055]484/**
[ce5bcb4]485 * @}
486 */
Note: See TracBrowser for help on using the repository browser.