Subversion Repositories HelenOS-historic

Rev

Rev 1551 | Rev 1564 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1551 Rev 1562
1
/*
1
/*
2
 * Copyright (C) 2006 Ondrej Palkovsky
2
 * Copyright (C) 2006 Ondrej Palkovsky
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
-
 
-
 
28
#include <stdlib.h>
-
 
29
#include <unistd.h>
29
#include <align.h>
30
#include <align.h>
30
#include <async.h>
31
#include <async.h>
31
#include <ipc/ipc.h>
32
#include <ipc/ipc.h>
32
#include <errno.h>
33
#include <errno.h>
33
#include <stdio.h>
34
#include <stdio.h>
34
#include <ddi.h>
35
#include <ddi.h>
35
#include <sysinfo.h>
36
#include <sysinfo.h>
36
#include <as.h>
37
#include <as.h>
37
#include <ipc/fb.h>
38
#include <ipc/fb.h>
38
#include <ipc/ipc.h>
39
#include <ipc/ipc.h>
39
#include <ipc/ns.h>
40
#include <ipc/ns.h>
40
#include <ipc/services.h>
41
#include <ipc/services.h>
41
 
42
 
42
#include "ega.h"
43
#include "ega.h"
43
#include "../console/screenbuffer.h"
44
#include "../console/screenbuffer.h"
44
#include "main.h"
45
#include "main.h"
45
 
46
 
-
 
47
#define MAX_SAVED_SCREENS 256
-
 
48
typedef struct saved_screen {
-
 
49
    short *data;
-
 
50
} saved_screen;
-
 
51
 
-
 
52
saved_screen saved_screens[MAX_SAVED_SCREENS];
-
 
53
 
46
 
54
 
47
#define EGA_IO_ADDRESS 0x3d4
55
#define EGA_IO_ADDRESS 0x3d4
48
#define EGA_IO_SIZE 2
56
#define EGA_IO_SIZE 2
49
 
57
 
50
typedef unsigned char u8;
58
typedef unsigned char u8;
51
typedef unsigned short u16;
59
typedef unsigned short u16;
52
typedef unsigned int u32;
60
typedef unsigned int u32;
53
 
61
 
54
 
62
 
55
/* Allow only 1 connection */
63
/* Allow only 1 connection */
56
static int client_connected = 0;
64
static int client_connected = 0;
57
 
65
 
58
static unsigned int scr_width;
66
static unsigned int scr_width;
59
static unsigned int scr_height;
67
static unsigned int scr_height;
60
static char *scr_addr;
68
static char *scr_addr;
61
 
69
 
62
static unsigned int style = 0x0f;
70
static unsigned int style = 0x1e;
63
 
71
 
64
static inline void outb(u16 port, u8 b)
72
static inline void outb(u16 port, u8 b)
65
{
73
{
66
    asm volatile ("outb %0, %1\n" :: "a" (b), "d" (port));
74
    asm volatile ("outb %0, %1\n" :: "a" (b), "d" (port));
67
}
75
}
68
 
76
 
69
static inline void outw(u16 port, u16 w)
77
static inline void outw(u16 port, u16 w)
70
{
78
{
71
    asm volatile ("outw %0, %1\n" :: "a" (w), "d" (port));
79
    asm volatile ("outw %0, %1\n" :: "a" (w), "d" (port));
72
}
80
}
73
 
81
 
74
static inline void outl(u16 port, u32 l)
82
static inline void outl(u16 port, u32 l)
75
{
83
{
76
    asm volatile ("outl %0, %1\n" :: "a" (l), "d" (port));
84
    asm volatile ("outl %0, %1\n" :: "a" (l), "d" (port));
77
}
85
}
78
 
86
 
79
static inline u8 inb(u16 port)
87
static inline u8 inb(u16 port)
80
{
88
{
81
    u8 val;
89
    u8 val;
82
 
90
 
83
    asm volatile ("inb %1, %0 \n" : "=a" (val) : "d"(port));
91
    asm volatile ("inb %1, %0 \n" : "=a" (val) : "d"(port));
84
    return val;
92
    return val;
85
}
93
}
86
 
94
 
87
static inline u16 inw(u16 port)
95
static inline u16 inw(u16 port)
88
{
96
{
89
    u16 val;
97
    u16 val;
90
 
98
 
91
    asm volatile ("inw %1, %0 \n" : "=a" (val) : "d"(port));
99
    asm volatile ("inw %1, %0 \n" : "=a" (val) : "d"(port));
92
    return val;
100
    return val;
93
}
101
}
94
 
102
 
95
static inline u32 inl(u16 port)
103
static inline u32 inl(u16 port)
96
{
104
{
97
    u32 val;
105
    u32 val;
98
 
106
 
99
    asm volatile ("inl %1, %0 \n" : "=a" (val) : "d"(port));
107
    asm volatile ("inl %1, %0 \n" : "=a" (val) : "d"(port));
100
    return val;
108
    return val;
101
}
109
}
102
 
110
 
103
 
111
 
104
 
112
 
105
 
113
 
106
static void clrscr(void)
114
static void clrscr(void)
107
{
115
{
108
    int i;
116
    int i;
109
   
117
   
110
    for (i=0; i < scr_width*scr_height; i++) {
118
    for (i=0; i < scr_width*scr_height; i++) {
111
        scr_addr[i*2] = ' ';
119
        scr_addr[i*2] = ' ';
112
        scr_addr[i*2+1] = style;
120
        scr_addr[i*2+1] = style;
113
    }
121
    }
114
}
122
}
115
 
123
 
116
void cursor_goto(unsigned int row, unsigned int col)
124
static void cursor_goto(unsigned int row, unsigned int col)
117
{
125
{
118
    int ega_cursor;
126
    int ega_cursor;
119
 
127
 
120
    ega_cursor=col+scr_width*row;
128
    ega_cursor=col+scr_width*row;
121
   
129
   
122
    outb(EGA_IO_ADDRESS    , 0xe);
130
    outb(EGA_IO_ADDRESS    , 0xe);
123
    outb(EGA_IO_ADDRESS + 1, (ega_cursor >>8) & 0xff);
131
    outb(EGA_IO_ADDRESS + 1, (ega_cursor >>8) & 0xff);
124
    outb(EGA_IO_ADDRESS    , 0xf);
132
    outb(EGA_IO_ADDRESS    , 0xf);
125
    outb(EGA_IO_ADDRESS + 1, ega_cursor & 0xff);
133
    outb(EGA_IO_ADDRESS + 1, ega_cursor & 0xff);
126
}
134
}
127
 
135
 
128
void cursor_disable(void)
136
static void cursor_disable(void)
129
{
137
{
130
    u8 stat;
138
    u8 stat;
131
    outb(EGA_IO_ADDRESS , 0xa);
139
    outb(EGA_IO_ADDRESS , 0xa);
132
    stat=inb(EGA_IO_ADDRESS + 1);
140
    stat=inb(EGA_IO_ADDRESS + 1);
133
    outb(EGA_IO_ADDRESS , 0xa);
141
    outb(EGA_IO_ADDRESS , 0xa);
134
    outb(EGA_IO_ADDRESS +1 ,stat | (1<<5) );
142
    outb(EGA_IO_ADDRESS +1 ,stat | (1<<5) );
135
}
143
}
136
 
144
 
137
void cursor_enable(void)
145
static void cursor_enable(void)
138
{
146
{
139
    u8 stat;
147
    u8 stat;
140
    outb(EGA_IO_ADDRESS , 0xa);
148
    outb(EGA_IO_ADDRESS , 0xa);
141
    stat=inb(EGA_IO_ADDRESS + 1);
149
    stat=inb(EGA_IO_ADDRESS + 1);
142
    outb(EGA_IO_ADDRESS , 0xa);
150
    outb(EGA_IO_ADDRESS , 0xa);
143
    outb(EGA_IO_ADDRESS +1 ,stat & (~(1<<5)) );
151
    outb(EGA_IO_ADDRESS +1 ,stat & (~(1<<5)) );
144
}
152
}
145
 
153
 
-
 
154
static void scroll(int rows)
-
 
155
{
-
 
156
    int i;
-
 
157
    if (rows > 0) {
-
 
158
        memcpy(scr_addr,((char *)scr_addr)+rows*scr_width*2,scr_width*scr_height*2-rows*scr_width*2);
-
 
159
        for(i=0;i<rows*scr_width;i++)
-
 
160
            (((short *)scr_addr)+scr_width*scr_height-rows*scr_width)[i]=((style<<8)+' ');
-
 
161
    } else if (rows < 0) {
-
 
162
 
-
 
163
        memcpy(((char *)scr_addr)-rows*scr_width*2,scr_addr,scr_width*scr_height*2+rows*scr_width*2);
-
 
164
        for(i=0;i<-rows*scr_width;i++)
-
 
165
            ((short *)scr_addr)[i]=((style<<8)+' ');
-
 
166
    }
-
 
167
}
-
 
168
 
146
static void printchar(char c, unsigned int row, unsigned int col)
169
static void printchar(char c, unsigned int row, unsigned int col)
147
{
170
{
148
    scr_addr[(row*scr_width + col)*2] = c;
171
    scr_addr[(row*scr_width + col)*2] = c;
149
    scr_addr[(row*scr_width + col)*2+1] = style;
172
    scr_addr[(row*scr_width + col)*2+1] = style;
150
   
173
   
151
    cursor_goto(row,col+1);
174
    cursor_goto(row,col+1);
152
}
175
}
153
 
176
 
154
static void draw_text_data(keyfield_t *data)
177
static void draw_text_data(keyfield_t *data)
155
{
178
{
156
    int i;
179
    int i;
157
 
180
 
158
    for (i=0; i < scr_width*scr_height; i++) {
181
    for (i=0; i < scr_width*scr_height; i++) {
159
        scr_addr[i*2] = data[i].character;
182
        scr_addr[i*2] = data[i].character;
160
        if (data[i].style.fg_color > data[i].style.bg_color)
183
        if (data[i].style.fg_color > data[i].style.bg_color)
161
            scr_addr[i*2+1] = 0x0f;
184
            scr_addr[i*2+1] = 0x1e;
162
        else
185
        else
163
            scr_addr[i*2+1] = 0xf0;
186
            scr_addr[i*2+1] = 0xe1;
164
    }
187
    }
165
}
188
}
166
 
189
 
-
 
190
static int save_screen(void)
-
 
191
{
-
 
192
    int i;
-
 
193
    short *mem;
-
 
194
    for(i=0;(i<MAX_SAVED_SCREENS)&&(saved_screens[i].data);i++);
-
 
195
    if(i==MAX_SAVED_SCREENS) return EINVAL;
-
 
196
    if(!(saved_screens[i].data=malloc(2*scr_width*scr_height))) return ENOMEM;
-
 
197
    memcpy(saved_screens[i].data,scr_addr,2*scr_width*scr_height);
-
 
198
    return i;
-
 
199
}
-
 
200
 
-
 
201
static int print_screen(int i)
-
 
202
{
-
 
203
    if(saved_screens[i].data)
-
 
204
            memcpy(scr_addr,saved_screens[i].data,2*scr_width*scr_height);
-
 
205
    else return EINVAL;
-
 
206
    return i;
-
 
207
}
-
 
208
 
-
 
209
 
167
static void ega_client_connection(ipc_callid_t iid, ipc_call_t *icall)
210
static void ega_client_connection(ipc_callid_t iid, ipc_call_t *icall)
168
{
211
{
169
    int retval;
212
    int retval;
170
    ipc_callid_t callid;
213
    ipc_callid_t callid;
171
    ipc_call_t call;
214
    ipc_call_t call;
172
    char c;
215
    char c;
173
    unsigned int row, col;
216
    unsigned int row, col;
174
    int bgcolor,fgcolor;
217
    int bgcolor,fgcolor;
175
    keyfield_t *interbuf = NULL;
218
    keyfield_t *interbuf = NULL;
176
    size_t intersize = 0;
219
    size_t intersize = 0;
-
 
220
    int i;
177
 
221
 
178
    if (client_connected) {
222
    if (client_connected) {
179
        ipc_answer_fast(iid, ELIMIT, 0,0);
223
        ipc_answer_fast(iid, ELIMIT, 0,0);
180
        return;
224
        return;
181
    }
225
    }
182
    client_connected = 1;
226
    client_connected = 1;
183
    ipc_answer_fast(iid, 0, 0, 0); /* Accept connection */
227
    ipc_answer_fast(iid, 0, 0, 0); /* Accept connection */
184
 
228
 
185
    while (1) {
229
    while (1) {
186
        callid = async_get_call(&call);
230
        callid = async_get_call(&call);
187
        switch (IPC_GET_METHOD(call)) {
231
        switch (IPC_GET_METHOD(call)) {
188
        case IPC_M_PHONE_HUNGUP:
232
        case IPC_M_PHONE_HUNGUP:
189
            client_connected = 0;
233
            client_connected = 0;
190
            ipc_answer_fast(callid,0,0,0);
234
            ipc_answer_fast(callid,0,0,0);
191
            return; /* Exit thread */
235
            return; /* Exit thread */
192
        case IPC_M_AS_AREA_SEND:
236
        case IPC_M_AS_AREA_SEND:
193
            /* We accept one area for data interchange */
237
            /* We accept one area for data interchange */
194
            intersize = IPC_GET_ARG2(call);
238
            intersize = IPC_GET_ARG2(call);
195
            if (intersize >= scr_width*scr_height*sizeof(*interbuf)) {
239
            if (intersize >= scr_width*scr_height*sizeof(*interbuf)) {
196
                receive_comm_area(callid,&call,(void **)&interbuf);
240
                receive_comm_area(callid,&call,(void **)&interbuf);
197
                continue;
241
                continue;
198
            }
242
            }
199
            retval = EINVAL;
243
            retval = EINVAL;
200
            break;
244
            break;
201
        case FB_DRAW_TEXT_DATA:
245
        case FB_DRAW_TEXT_DATA:
202
            if (!interbuf) {
246
            if (!interbuf) {
203
                retval = EINVAL;
247
                retval = EINVAL;
204
                break;
248
                break;
205
            }
249
            }
206
            draw_text_data(interbuf);
250
            draw_text_data(interbuf);
207
            retval = 0;
251
            retval = 0;
208
            break;
252
            break;
209
        case FB_GET_CSIZE:
253
        case FB_GET_CSIZE:
210
            ipc_answer_fast(callid, 0, scr_height, scr_width);
254
            ipc_answer_fast(callid, 0, scr_height, scr_width);
211
            continue;
255
            continue;
212
        case FB_CLEAR:
256
        case FB_CLEAR:
213
            clrscr();
257
            clrscr();
214
            retval = 0;
258
            retval = 0;
215
            break;
259
            break;
216
        case FB_PUTCHAR:
260
        case FB_PUTCHAR:
217
            c = IPC_GET_ARG1(call);
261
            c = IPC_GET_ARG1(call);
218
            row = IPC_GET_ARG2(call);
262
            row = IPC_GET_ARG2(call);
219
            col = IPC_GET_ARG3(call);
263
            col = IPC_GET_ARG3(call);
220
            if (col >= scr_width || row >= scr_height) {
264
            if (col >= scr_width || row >= scr_height) {
221
                retval = EINVAL;
265
                retval = EINVAL;
222
                break;
266
                break;
223
            }
267
            }
224
            printchar(c,row,col);
268
            printchar(c,row,col);
225
            retval = 0;
269
            retval = 0;
226
            break;
270
            break;
227
        case FB_CURSOR_GOTO:
271
        case FB_CURSOR_GOTO:
228
            row = IPC_GET_ARG1(call);
272
            row = IPC_GET_ARG1(call);
229
            col = IPC_GET_ARG2(call);
273
            col = IPC_GET_ARG2(call);
230
            if (row >= scr_height || col >= scr_width) {
274
            if (row >= scr_height || col >= scr_width) {
231
                retval = EINVAL;
275
                retval = EINVAL;
232
                break;
276
                break;
233
            }
277
            }
234
            cursor_goto(row,col);
278
            cursor_goto(row,col);
235
            retval = 0;
279
            retval = 0;
236
            break;
280
            break;
-
 
281
        case FB_SCROLL:
-
 
282
            i = IPC_GET_ARG1(call);
-
 
283
            if (i > scr_height || i < (- (int)scr_height)) {
-
 
284
                retval = EINVAL;
-
 
285
                break;
-
 
286
            }
-
 
287
            scroll(i);
-
 
288
            retval = 0;
-
 
289
            break;
237
        case FB_CURSOR_VISIBILITY:
290
        case FB_CURSOR_VISIBILITY:
238
            if(IPC_GET_ARG1(call))
291
            if(IPC_GET_ARG1(call))
239
                cursor_enable();
292
                cursor_enable();
240
            else
293
            else
241
                cursor_disable();
294
                cursor_disable();
242
            retval = 0;
295
            retval = 0;
243
            break;
296
            break;
244
        case FB_SET_STYLE:
297
        case FB_SET_STYLE:
245
            fgcolor = IPC_GET_ARG1(call);
298
            fgcolor = IPC_GET_ARG1(call);
246
            bgcolor = IPC_GET_ARG2(call);
299
            bgcolor = IPC_GET_ARG2(call);
247
            if (fgcolor > bgcolor)
300
            if (fgcolor > bgcolor)
248
                style = 0x0f;
301
                style = 0x1e;
249
            else
302
            else
250
                style = 0xf0;
303
                style = 0xe1;
-
 
304
 
-
 
305
        case FB_VP_DRAW_PIXMAP:
-
 
306
            i = IPC_GET_ARG2(call);
-
 
307
            retval = print_screen(i);
-
 
308
            break;
-
 
309
        case FB_VP2PIXMAP:
-
 
310
            retval = save_screen();
-
 
311
            break;
-
 
312
        case FB_DROP_PIXMAP:
-
 
313
            i = IPC_GET_ARG1(call);
-
 
314
            if (i >= MAX_SAVED_SCREENS) {
-
 
315
                retval = EINVAL;
-
 
316
                break;
-
 
317
            }
-
 
318
            if (saved_screens[i].data) {
-
 
319
                free(saved_screens[i].data);
-
 
320
                saved_screens[i].data = NULL;
-
 
321
            }
-
 
322
            break;
-
 
323
 
251
        default:
324
        default:
252
            retval = ENOENT;
325
            retval = ENOENT;
253
        }
326
        }
254
        ipc_answer_fast(callid,retval,0,0);
327
        ipc_answer_fast(callid,retval,0,0);
255
    }
328
    }
256
}
329
}
257
 
330
 
258
int ega_init(void)
331
int ega_init(void)
259
{
332
{
260
    void *ega_ph_addr;
333
    void *ega_ph_addr;
261
    size_t sz;
334
    size_t sz;
262
 
335
 
263
 
336
 
264
    ega_ph_addr=(void *)sysinfo_value("fb.address.physical");
337
    ega_ph_addr=(void *)sysinfo_value("fb.address.physical");
265
    scr_width=sysinfo_value("fb.width");
338
    scr_width=sysinfo_value("fb.width");
266
    scr_height=sysinfo_value("fb.height");
339
    scr_height=sysinfo_value("fb.height");
267
    iospace_enable(task_get_id(),(void *)EGA_IO_ADDRESS,2);
340
    iospace_enable(task_get_id(),(void *)EGA_IO_ADDRESS,2);
268
 
341
 
269
    sz = scr_width*scr_height*2;
342
    sz = scr_width*scr_height*2;
270
    scr_addr = as_get_mappable_page(sz);
343
    scr_addr = as_get_mappable_page(sz);
271
 
344
 
272
    map_physmem(ega_ph_addr, scr_addr, ALIGN_UP(sz,PAGE_SIZE)>>PAGE_WIDTH,
345
    map_physmem(ega_ph_addr, scr_addr, ALIGN_UP(sz,PAGE_SIZE)>>PAGE_WIDTH,
273
            AS_AREA_READ | AS_AREA_WRITE);
346
            AS_AREA_READ | AS_AREA_WRITE);
274
 
347
 
275
    async_set_client_connection(ega_client_connection);
348
    async_set_client_connection(ega_client_connection);
276
 
349
 
277
    clrscr();
-
 
278
 
-
 
279
    return 0;
350
    return 0;
280
}
351
}
281
 
352
 
282
 
353