Subversion Repositories HelenOS-historic

Rev

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

Rev Author Line No. Line
1445 cejka 1
/*
2
 * Copyright (C) 2006 Josef Cejka
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
 
30
#include <kbd.h>
1451 cejka 31
#include <fb.h>
1445 cejka 32
#include <ipc/ipc.h>
1451 cejka 33
#include <ipc/fb.h>
1445 cejka 34
#include <ipc/services.h>
35
#include <errno.h>
1451 cejka 36
#include <key_buffer.h>
37
#include <console.h>
1453 palkovsky 38
#include <unistd.h>
39
#include <async.h>
1481 cejka 40
#include <libadt/fifo.h>
1487 cejka 41
#include <screenbuffer.h>
1512 cejka 42
#include <sys/mman.h>
1445 cejka 43
 
1522 palkovsky 44
#include "gcons.h"
45
 
1481 cejka 46
#define MAX_KEYREQUESTS_BUFFERED 32
1451 cejka 47
 
1445 cejka 48
#define NAME "CONSOLE"
49
 
1526 cejka 50
/** Index of currently used virtual console.
51
 */
1512 cejka 52
int active_console = 0;
1453 palkovsky 53
 
1526 cejka 54
/** Information about framebuffer
55
 */
1487 cejka 56
struct {
57
    int phone;      /**< Framebuffer phone */
1506 cejka 58
    ipcarg_t rows;      /**< Framebuffer rows */
59
    ipcarg_t cols;      /**< Framebuffer columns */
1487 cejka 60
} fb_info;
61
 
1526 cejka 62
 
1451 cejka 63
typedef struct {
1526 cejka 64
    keybuffer_t keybuffer;      /**< Buffer for incoming keys. */
65
    FIFO_CREATE_STATIC(keyrequests, ipc_callid_t , MAX_KEYREQUESTS_BUFFERED);   /**< Buffer for unsatisfied request for keys. */
66
    int keyrequest_counter;     /**< Number of requests in buffer. */
67
    int client_phone;       /**< Phone to connected client. */
68
    int used;           /**< 1 if this virtual console is connected to some client.*/
69
    screenbuffer_t screenbuffer;    /**< Screenbuffer for saving screen contents and related settings. */
1451 cejka 70
} connection_t;
71
 
1526 cejka 72
connection_t connections[CONSOLE_COUNT];    /**< Array of data for virtual consoles */
73
keyfield_t *interbuffer = NULL;         /**< Pointer to memory shared with framebufer used for faster virt. console switching */
1506 cejka 74
 
75
 
1526 cejka 76
/** Find unused virtual console.
77
 *
78
 */
1451 cejka 79
static int find_free_connection()
80
{
81
    int i = 0;
82
 
83
    while (i < CONSOLE_COUNT) {
84
        if (connections[i].used == 0)
85
            return i;
86
        ++i;
87
    }
88
    return CONSOLE_COUNT;
89
}
90
 
1526 cejka 91
/** Find index of virtual console used by client with given phone.
92
 *
93
 */
1451 cejka 94
static int find_connection(int client_phone)
95
{
96
    int i = 0;
97
 
98
    while (i < CONSOLE_COUNT) {
99
        if (connections[i].client_phone == client_phone)
100
            return i;
101
        ++i;
102
    }
103
    return  CONSOLE_COUNT;
104
}
105
 
1497 cejka 106
/** Check key and process special keys.
107
 *
108
 * */
109
static void write_char(int console, char key)
110
{
111
    screenbuffer_t *scr = &(connections[console].screenbuffer);
112
 
113
    switch (key) {
114
        case '\n':
115
            scr->position_y += 1;
116
            scr->position_x =  0;
117
            break;
118
        case '\r':
119
            break;
120
        case '\t':
121
            scr->position_x += 8;
122
            scr->position_x -= scr->position_x % 8;
123
            break;
124
        case '\b':
125
            if (scr->position_x == 0)
126
                break;
127
 
128
            scr->position_x--;
129
 
130
            if (console == active_console) {
1518 palkovsky 131
                nsend_call_3(fb_info.phone, FB_PUTCHAR, ' ', scr->position_y, scr->position_x);
1497 cejka 132
            }
133
 
134
            screenbuffer_putchar(scr, ' ');
135
 
136
            break;
137
        default:   
138
            if (console == active_console) {
1518 palkovsky 139
                nsend_call_3(fb_info.phone, FB_PUTCHAR, key, scr->position_y, scr->position_x);
1497 cejka 140
            }
141
 
142
            screenbuffer_putchar(scr, key);
143
            scr->position_x++;
144
    }
145
 
146
    scr->position_y += (scr->position_x >= scr->size_x);
147
 
148
    if (scr->position_y >= scr->size_y) {
149
        scr->position_y = scr->size_y - 1;
150
        screenbuffer_clear_line(scr, scr->top_line++);
1518 palkovsky 151
        if (console == active_console)
152
            nsend_call(fb_info.phone, FB_SCROLL, 1);
1497 cejka 153
    }
154
 
155
    scr->position_x = scr->position_x % scr->size_x;
156
 
1504 cejka 157
    if (console == active_console) 
1518 palkovsky 158
        send_call_2(fb_info.phone, FB_CURSOR_GOTO, scr->position_y, scr->position_x);
1504 cejka 159
 
1497 cejka 160
}
161
 
162
 
1526 cejka 163
/** Handler for keyboard */
1453 palkovsky 164
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
1445 cejka 165
{
1453 palkovsky 166
    ipc_callid_t callid;
1445 cejka 167
    ipc_call_t call;
1453 palkovsky 168
    int retval;
1487 cejka 169
    int i, j;
1489 palkovsky 170
    char c,d;
171
    connection_t *conn;
1506 cejka 172
 
1453 palkovsky 173
    /* Ignore parameters, the connection is alread opened */
174
    while (1) {
175
        callid = async_get_call(&call);
176
        switch (IPC_GET_METHOD(call)) {
177
        case IPC_M_PHONE_HUNGUP:
178
            ipc_answer_fast(callid,0,0,0);
179
            /* TODO: Handle hangup */
180
            return;
181
        case KBD_PUSHCHAR:
182
            /* got key from keyboard driver */
1465 cejka 183
 
1453 palkovsky 184
            retval = 0;
1520 cejka 185
            c = IPC_GET_ARG1(call);
1453 palkovsky 186
            /* switch to another virtual console */
1481 cejka 187
 
1489 palkovsky 188
            conn = &connections[active_console];
1490 palkovsky 189
//          if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 + CONSOLE_COUNT)) {
1520 cejka 190
            if ((c >= '0') && (c < '0' + CONSOLE_COUNT)) {
191
                if (c == '0') {
1517 cejka 192
                    /* switch to kernel console*/
1523 cejka 193
                    nsend_call(fb_info.phone, FB_CURSOR_VISIBILITY, 0);
1520 cejka 194
                    nsend_call_2(fb_info.phone, FB_SET_STYLE, DEFAULT_FOREGROUND_COLOR, DEFAULT_BACKGROUND_COLOR);
195
                    nsend_call(fb_info.phone, FB_CLEAR, 0);
196
                    /* FIXME: restore kernel console */
1517 cejka 197
                     __SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE);
1520 cejka 198
                     break;
1517 cejka 199
 
200
                } else {
1520 cejka 201
                    c = c - '1';
202
                    if (c == active_console)
203
                        break;
1517 cejka 204
                    active_console = c;
1522 palkovsky 205
                    gcons_change_console(c);
206
 
1517 cejka 207
                }
208
 
1489 palkovsky 209
                conn = &connections[active_console];
210
 
1518 palkovsky 211
                nsend_call(fb_info.phone, FB_CURSOR_VISIBILITY, 0);
1512 cejka 212
 
213
                if (interbuffer) {
1516 cejka 214
                    for (i = 0; i < conn->screenbuffer.size_x; i++)
215
                        for (j = 0; j < conn->screenbuffer.size_y; j++)
216
                            interbuffer[i + j*conn->screenbuffer.size_x] = *get_field_at(&(conn->screenbuffer),i, j);
217
 
1517 cejka 218
                    sync_send_2(fb_info.phone, FB_DRAW_TEXT_DATA, 0, 0, NULL, NULL);       
1512 cejka 219
                } else {
1518 palkovsky 220
                    nsend_call(fb_info.phone, FB_CLEAR, 0);
1497 cejka 221
 
1512 cejka 222
 
223
                    for (i = 0; i < conn->screenbuffer.size_x; i++)
224
                        for (j = 0; j < conn->screenbuffer.size_y; j++) {
225
                            d = get_field_at(&(conn->screenbuffer),i, j)->character;
226
                            if (d && d != ' ')
1518 palkovsky 227
                                nsend_call_3(fb_info.phone, FB_PUTCHAR, d, j, i);
1512 cejka 228
                        }
1499 cejka 229
 
1512 cejka 230
                }
1518 palkovsky 231
                nsend_call_2(fb_info.phone, FB_CURSOR_GOTO, conn->screenbuffer.position_y, conn->screenbuffer.position_x);
232
                nsend_call_2(fb_info.phone, FB_SET_STYLE, conn->screenbuffer.style.fg_color, \
233
                        conn->screenbuffer.style.bg_color);
234
                send_call(fb_info.phone, FB_CURSOR_VISIBILITY, 1);
1487 cejka 235
 
1453 palkovsky 236
                break;
237
            }
1481 cejka 238
 
239
            /* if client is awaiting key, send it */
1489 palkovsky 240
            if (conn->keyrequest_counter > 0) {    
241
                conn->keyrequest_counter--;
242
                ipc_answer_fast(fifo_pop(conn->keyrequests), 0, c, 0);
1481 cejka 243
                break;
244
            }
245
 
246
            /*FIXME: else store key to its buffer */
1489 palkovsky 247
            keybuffer_push(&conn->keybuffer, c);
1476 cejka 248
 
1453 palkovsky 249
            break;
250
        default:
1459 palkovsky 251
            retval = ENOENT;
1453 palkovsky 252
        }      
1459 palkovsky 253
        ipc_answer_fast(callid, retval, 0, 0);
1453 palkovsky 254
    }
255
}
256
 
257
/** Default thread for new connections */
1518 palkovsky 258
static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
1453 palkovsky 259
{
1445 cejka 260
    ipc_callid_t callid;
1453 palkovsky 261
    ipc_call_t call;
262
    int consnum;
1521 cejka 263
    ipcarg_t arg1, arg2;
1453 palkovsky 264
 
265
    if ((consnum = find_free_connection()) == CONSOLE_COUNT) {
266
        ipc_answer_fast(iid,ELIMIT,0,0);
267
        return;
268
    }
1497 cejka 269
 
1453 palkovsky 270
    connections[consnum].used = 1;
271
    connections[consnum].client_phone = IPC_GET_ARG3(call);
1487 cejka 272
    screenbuffer_clear(&(connections[consnum].screenbuffer));
1497 cejka 273
 
1453 palkovsky 274
    /* Accept the connection */
275
    ipc_answer_fast(iid,0,0,0);
276
 
277
    while (1) {
278
        callid = async_get_call(&call);
1521 cejka 279
        arg1 = arg2 = 0;
1453 palkovsky 280
        switch (IPC_GET_METHOD(call)) {
281
        case IPC_M_PHONE_HUNGUP:
282
            /* TODO */
283
            ipc_answer_fast(callid, 0,0,0);
284
            return;
285
        case CONSOLE_PUTCHAR:
1497 cejka 286
            write_char(consnum, IPC_GET_ARG1(call));
1453 palkovsky 287
            break;
1476 cejka 288
        case CONSOLE_CLEAR:
1487 cejka 289
            /* Send message to fb */
290
            if (consnum == active_console) {
1518 palkovsky 291
                send_call(fb_info.phone, FB_CLEAR, 0);
1487 cejka 292
            }
293
 
294
            screenbuffer_clear(&(connections[consnum].screenbuffer));
295
 
1476 cejka 296
            break;
297
        case CONSOLE_GOTO:
1487 cejka 298
 
299
            screenbuffer_goto(&(connections[consnum].screenbuffer), IPC_GET_ARG1(call), IPC_GET_ARG2(call));
300
 
1476 cejka 301
            break;
1523 cejka 302
 
1521 cejka 303
        case CONSOLE_GETSIZE:
304
            arg1 = fb_info.cols;
305
            arg2 = fb_info.rows;
306
            break;
1523 cejka 307
        case CONSOLE_FLUSH:
308
            sync_send_2(fb_info.phone, FB_FLUSH, 0, 0, NULL, NULL);    
309
            break;
1525 cejka 310
        case CONSOLE_SET_STYLE:
311
 
312
            arg1 = IPC_GET_ARG1(call);
313
            arg2 = IPC_GET_ARG2(call);
314
            screenbuffer_set_style(&(connections[consnum]),arg1, arg2);
315
            if (consnum == active_console)
316
                nsend_call_2(fb_info.phone, FB_SET_STYLE, arg1, arg2);
317
 
318
            break;
1453 palkovsky 319
        case CONSOLE_GETCHAR:
1481 cejka 320
            if (keybuffer_empty(&(connections[consnum].keybuffer))) {
321
                /* buffer is empty -> store request */
322
                if (connections[consnum].keyrequest_counter < MAX_KEYREQUESTS_BUFFERED) {      
323
                    fifo_push(connections[consnum].keyrequests, callid);
324
                    connections[consnum].keyrequest_counter++;
325
                } else {
326
                    /* no key available and too many requests => fail */
327
                    ipc_answer_fast(callid, ELIMIT, 0, 0);
328
                }
329
                continue;
1453 palkovsky 330
            };
1476 cejka 331
            keybuffer_pop(&(connections[consnum].keybuffer), (char *)&arg1);
1465 cejka 332
 
1453 palkovsky 333
            break;
334
        }
1521 cejka 335
        ipc_answer_fast(callid, 0, arg1, arg2);
1453 palkovsky 336
    }
337
}
338
 
339
int main(int argc, char *argv[])
340
{
341
    ipcarg_t phonehash;
1451 cejka 342
    int kbd_phone, fb_phone;
1445 cejka 343
    ipcarg_t retval, arg1 = 0xdead, arg2 = 0xbeef;
1451 cejka 344
    int i;
1490 palkovsky 345
 
346
    async_set_client_connection(client_connection);
1445 cejka 347
 
348
    /* Connect to keyboard driver */
349
 
1451 cejka 350
    while ((kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0)) < 0) {
1453 palkovsky 351
        usleep(10000);
1445 cejka 352
    };
353
 
1453 palkovsky 354
    if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonehash) != 0) {
1445 cejka 355
        return -1;
356
    };
357
 
358
    /* Connect to framebuffer driver */
359
 
1487 cejka 360
    while ((fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
361
        usleep(10000);
362
    }
1522 palkovsky 363
 
364
    /* Initialize gcons */
365
    gcons_init(fb_info.phone);
366
    /* Synchronize, the gcons can have something in queue */
367
    sync_send_2(fb_info.phone, FB_GET_CSIZE, 0, 0, NULL, NULL);
368
 
1487 cejka 369
 
370
    ipc_call_sync_2(fb_info.phone, FB_GET_CSIZE, 0, 0, &(fb_info.rows), &(fb_info.cols));
1518 palkovsky 371
    nsend_call_2(fb_info.phone, FB_SET_STYLE, DEFAULT_FOREGROUND_COLOR, DEFAULT_BACKGROUND_COLOR);
1520 cejka 372
    nsend_call(fb_info.phone, FB_CLEAR, 0);
1487 cejka 373
 
374
    /* Init virtual consoles */
1451 cejka 375
    for (i = 0; i < CONSOLE_COUNT; i++) {
376
        connections[i].used = 0;
377
        keybuffer_init(&(connections[i].keybuffer));
1481 cejka 378
 
379
        connections[i].keyrequests.head = connections[i].keyrequests.tail = 0;
380
        connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
381
        connections[i].keyrequest_counter = 0;
1487 cejka 382
 
383
        if (screenbuffer_init(&(connections[i].screenbuffer), fb_info.cols, fb_info.rows ) == NULL) {
384
            /*FIXME: handle error */
385
            return -1;
386
        }
1451 cejka 387
    }
1445 cejka 388
 
1512 cejka 389
    if ((interbuffer = mmap(NULL, sizeof(keyfield_t) * fb_info.cols * fb_info.rows , PROTO_READ|PROTO_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, 0 ,0 )) != NULL) {
390
        if (ipc_call_sync_3(fb_info.phone, IPC_M_AS_AREA_SEND, (ipcarg_t)interbuffer, 0, AS_AREA_READ | AS_AREA_CACHEABLE, NULL, NULL, NULL) != 0) {
391
            munmap(interbuffer, sizeof(keyfield_t) * fb_info.cols * fb_info.rows);
392
            interbuffer = NULL;
393
        }
394
    }
1520 cejka 395
 
396
    /* FIXME: save kernel console screen */
1512 cejka 397
 
1506 cejka 398
    async_new_connection(phonehash, 0, NULL, keyboard_events);
399
 
1522 palkovsky 400
    sync_send_2(fb_info.phone, FB_CURSOR_GOTO, 0, 0, NULL, NULL);
1520 cejka 401
    nsend_call(fb_info.phone, FB_CURSOR_VISIBILITY, 1);
1497 cejka 402
 
1518 palkovsky 403
    /* Register at NS */
1453 palkovsky 404
    if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonehash) != 0) {
1445 cejka 405
        return -1;
406
    };
407
 
1453 palkovsky 408
    async_manager();
1451 cejka 409
 
1445 cejka 410
    return 0;  
411
}
1520 cejka 412