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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 64d2b10 was 228e490, checked in by Martin Decky <martin@…>, 15 years ago

initial modifications for supporting declarative IPC interfaces

  • Property mode set to 100644
File size: 10.5 KB
Line 
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#include <io/style.h>
53#include <io/color.h>
54#include <io/screenbuffer.h>
55#include <sys/types.h>
56
57#include "ega.h"
58#include "main.h"
59
60#define MAX_SAVED_SCREENS 256
61
62typedef struct saved_screen {
63 short *data;
64} saved_screen;
65
66saved_screen saved_screens[MAX_SAVED_SCREENS];
67
68#define EGA_IO_BASE ((ioport8_t *) 0x3d4)
69#define EGA_IO_SIZE 2
70
71/* Allow only 1 connection */
72static int client_connected = 0;
73
74static sysarg_t scr_width;
75static sysarg_t scr_height;
76static uint8_t *scr_addr;
77
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}
126
127static uint8_t ega_glyph(wchar_t ch)
128{
129 if (ch >= 0 && ch < 128)
130 return ch;
131
132 return '?';
133}
134
135static void clrscr(void)
136{
137 unsigned i;
138
139 for (i = 0; i < scr_width * scr_height; i++) {
140 scr_addr[i * 2] = ' ';
141 scr_addr[i * 2 + 1] = style;
142 }
143}
144
145static void cursor_goto(sysarg_t col, sysarg_t row)
146{
147 sysarg_t cursor = col + scr_width * row;
148
149 pio_write_8(EGA_IO_BASE, 0xe);
150 pio_write_8(EGA_IO_BASE + 1, (cursor >> 8) & 0xff);
151 pio_write_8(EGA_IO_BASE, 0xf);
152 pio_write_8(EGA_IO_BASE + 1, cursor & 0xff);
153}
154
155static void cursor_disable(void)
156{
157 pio_write_8(EGA_IO_BASE, 0xa);
158
159 uint8_t stat = pio_read_8(EGA_IO_BASE + 1);
160
161 pio_write_8(EGA_IO_BASE, 0xa);
162 pio_write_8(EGA_IO_BASE + 1, stat | (1 << 5));
163}
164
165static void cursor_enable(void)
166{
167 pio_write_8(EGA_IO_BASE, 0xa);
168
169 uint8_t stat = pio_read_8(EGA_IO_BASE + 1);
170
171 pio_write_8(EGA_IO_BASE, 0xa);
172 pio_write_8(EGA_IO_BASE + 1, stat & (~(1 << 5)));
173}
174
175static void scroll(ssize_t rows)
176{
177 size_t i;
178
179 if (rows > 0) {
180 memmove(scr_addr, ((char *) scr_addr) + rows * scr_width * 2,
181 scr_width * scr_height * 2 - rows * scr_width * 2);
182 for (i = 0; i < rows * scr_width; i++)
183 (((short *) scr_addr) + scr_width * scr_height - rows *
184 scr_width)[i] = ((style << 8) + ' ');
185 } else if (rows < 0) {
186 memmove(((char *)scr_addr) - rows * scr_width * 2, scr_addr,
187 scr_width * scr_height * 2 + rows * scr_width * 2);
188 for (i = 0; i < -rows * scr_width; i++)
189 ((short *) scr_addr)[i] = ((style << 8 ) + ' ');
190 }
191}
192
193static void printchar(wchar_t c, sysarg_t col, sysarg_t row)
194{
195 scr_addr[(row * scr_width + col) * 2] = ega_glyph(c);
196 scr_addr[(row * scr_width + col) * 2 + 1] = style;
197
198 cursor_goto(col + 1, row);
199}
200
201/** Draw text data to viewport.
202 *
203 * @param vport Viewport id
204 * @param data Text data.
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 *
210 */
211static void draw_text_data(keyfield_t *data, sysarg_t x, sysarg_t y,
212 sysarg_t w, sysarg_t h)
213{
214 sysarg_t i;
215 sysarg_t j;
216 keyfield_t *field;
217 uint8_t *dp;
218
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))];
223
224 dp[0] = ega_glyph(field->character);
225 dp[1] = attr_to_ega_style(&field->attrs);
226 }
227 }
228}
229
230static int save_screen(void)
231{
232 sysarg_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)
238 return EINVAL;
239
240 if (!(saved_screens[i].data = malloc(2 * scr_width * scr_height)))
241 return ENOMEM;
242
243 memcpy(saved_screens[i].data, scr_addr, 2 * scr_width * scr_height);
244 return (int) i;
245}
246
247static int print_screen(sysarg_t i)
248{
249 if ((i >= MAX_SAVED_SCREENS) || (saved_screens[i].data))
250 memcpy(scr_addr, saved_screens[i].data, 2 * scr_width *
251 scr_height);
252 else
253 return EINVAL;
254
255 return (int) i;
256}
257
258static void ega_client_connection(ipc_callid_t iid, ipc_call_t *icall)
259{
260 size_t intersize = 0;
261 keyfield_t *interbuf = NULL;
262
263 if (client_connected) {
264 ipc_answer_0(iid, ELIMIT);
265 return;
266 }
267
268 /* Accept connection */
269 client_connected = 1;
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 sysarg_t col;
279 sysarg_t row;
280 sysarg_t w;
281 sysarg_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 sysarg_t scr;
293 int retval;
294
295 switch (IPC_GET_IMETHOD(call)) {
296 case IPC_M_PHONE_HUNGUP:
297 client_connected = 0;
298 ipc_answer_0(callid, EOK);
299
300 /* Exit thread */
301 return;
302 case IPC_M_SHARE_OUT:
303 /* We accept one area for data interchange */
304 intersize = IPC_GET_ARG2(call);
305 if (intersize >= scr_width * scr_height *
306 sizeof(*interbuf)) {
307 receive_comm_area(callid, &call,
308 (void *) &interbuf);
309 continue;
310 }
311
312 retval = EINVAL;
313 break;
314 case FB_DRAW_TEXT_DATA:
315 if (!interbuf) {
316 retval = EINVAL;
317 break;
318 }
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)) {
326 retval = EINVAL;
327 break;
328 }
329
330 draw_text_data(interbuf, col, row, w, h);
331 retval = 0;
332 break;
333 case FB_GET_CSIZE:
334 ipc_answer_2(callid, EOK, scr_width, scr_height);
335 continue;
336 case FB_GET_COLOR_CAP:
337 ipc_answer_1(callid, EOK, FB_CCAP_INDEXED);
338 continue;
339 case FB_CLEAR:
340 clrscr();
341 retval = 0;
342 break;
343 case FB_PUTCHAR:
344 c = IPC_GET_ARG1(call);
345 col = IPC_GET_ARG2(call);
346 row = IPC_GET_ARG3(call);
347
348 if ((col >= scr_width) || (row >= scr_height)) {
349 retval = EINVAL;
350 break;
351 }
352
353 printchar(c, col, row);
354 retval = 0;
355 break;
356 case FB_CURSOR_GOTO:
357 col = IPC_GET_ARG1(call);
358 row = IPC_GET_ARG2(call);
359
360 if ((row >= scr_height) || (col >= scr_width)) {
361 retval = EINVAL;
362 break;
363 }
364
365 cursor_goto(col, row);
366 retval = 0;
367 break;
368 case FB_SCROLL:
369 rows = IPC_GET_ARG1(call);
370
371 if (rows >= 0) {
372 if ((sysarg_t) rows > scr_height) {
373 retval = EINVAL;
374 break;
375 }
376 } else {
377 if ((sysarg_t) (-rows) > scr_height) {
378 retval = EINVAL;
379 break;
380 }
381 }
382
383 scroll(rows);
384 retval = 0;
385 break;
386 case FB_CURSOR_VISIBILITY:
387 if (IPC_GET_ARG1(call))
388 cursor_enable();
389 else
390 cursor_disable();
391
392 retval = 0;
393 break;
394 case FB_SET_STYLE:
395 style = style_to_ega_style(IPC_GET_ARG1(call));
396 retval = 0;
397 break;
398 case FB_SET_COLOR:
399 fg_color = IPC_GET_ARG1(call);
400 bg_color = IPC_GET_ARG2(call);
401 attr = IPC_GET_ARG3(call);
402
403 style = color_to_ega_style(fg_color, bg_color, attr);
404 retval = 0;
405 break;
406 case FB_SET_RGB_COLOR:
407 fg_rgb = IPC_GET_ARG1(call);
408 bg_rgb = IPC_GET_ARG2(call);
409
410 style = rgb_to_ega_style(fg_rgb, bg_rgb);
411 retval = 0;
412 break;
413 case FB_VP_DRAW_PIXMAP:
414 scr = IPC_GET_ARG2(call);
415 retval = print_screen(scr);
416 break;
417 case FB_VP2PIXMAP:
418 retval = save_screen();
419 break;
420 case FB_DROP_PIXMAP:
421 scr = IPC_GET_ARG1(call);
422
423 if (scr >= MAX_SAVED_SCREENS) {
424 retval = EINVAL;
425 break;
426 }
427
428 if (saved_screens[scr].data) {
429 free(saved_screens[scr].data);
430 saved_screens[scr].data = NULL;
431 }
432
433 retval = 0;
434 break;
435 case FB_SCREEN_YIELD:
436 case FB_SCREEN_RECLAIM:
437 retval = EOK;
438 break;
439 default:
440 retval = EINVAL;
441 }
442 ipc_answer_0(callid, retval);
443 }
444}
445
446int ega_init(void)
447{
448 sysarg_t paddr;
449 if (sysinfo_get_value("fb.address.physical", &paddr) != EOK)
450 return -1;
451
452 if (sysinfo_get_value("fb.width", &scr_width) != EOK)
453 return -1;
454
455 if (sysinfo_get_value("fb.height", &scr_height) != EOK)
456 return -1;
457
458 sysarg_t blinking;
459 if (sysinfo_get_value("fb.blinking", &blinking) != EOK)
460 blinking = false;
461
462 void *ega_ph_addr = (void *) paddr;
463 if (blinking) {
464 style_normal &= 0x77;
465 style_inverted &= 0x77;
466 }
467
468 style = style_normal;
469
470 iospace_enable(task_get_id(), (void *) EGA_IO_BASE, 2);
471
472 size_t sz = scr_width * scr_height * 2;
473 scr_addr = as_get_mappable_page(sz);
474
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;
478
479 async_set_client_connection(ega_client_connection);
480
481 return 0;
482}
483
484/**
485 * @}
486 */
Note: See TracBrowser for help on using the repository browser.