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

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

do not intermix low-level IPC methods with async framework methods

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