Subversion Repositories HelenOS

Rev

Rev 3343 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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