Subversion Repositories HelenOS-historic

Rev

Rev 1504 | Rev 1512 | 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>
1445 cejka 42
 
1497 cejka 43
#define CONSOLE_COUNT 12 
1481 cejka 44
#define MAX_KEYREQUESTS_BUFFERED 32
1451 cejka 45
 
1445 cejka 46
#define NAME "CONSOLE"
47
 
1476 cejka 48
int active_console = 1;
1453 palkovsky 49
 
1487 cejka 50
struct {
51
    int phone;      /**< Framebuffer phone */
1506 cejka 52
    ipcarg_t rows;      /**< Framebuffer rows */
53
    ipcarg_t cols;      /**< Framebuffer columns */
1487 cejka 54
} fb_info;
55
 
1451 cejka 56
typedef struct {
57
    keybuffer_t keybuffer;
1481 cejka 58
    FIFO_CREATE_STATIC(keyrequests, ipc_callid_t , MAX_KEYREQUESTS_BUFFERED);
59
    int keyrequest_counter;
1451 cejka 60
    int client_phone;
61
    int used;
1487 cejka 62
    screenbuffer_t screenbuffer;
1451 cejka 63
} connection_t;
64
 
1506 cejka 65
 
66
 
1451 cejka 67
connection_t connections[CONSOLE_COUNT];
68
 
69
static int find_free_connection()
70
{
71
    int i = 0;
72
 
73
    while (i < CONSOLE_COUNT) {
74
        if (connections[i].used == 0)
75
            return i;
76
        ++i;
77
    }
78
    return CONSOLE_COUNT;
79
}
80
 
81
 
82
static int find_connection(int client_phone)
83
{
84
    int i = 0;
85
 
86
    while (i < CONSOLE_COUNT) {
87
        if (connections[i].client_phone == client_phone)
88
            return i;
89
        ++i;
90
    }
91
    return  CONSOLE_COUNT;
92
}
93
 
1497 cejka 94
/** Check key and process special keys.
95
 *
96
 * */
97
static void write_char(int console, char key)
98
{
99
    screenbuffer_t *scr = &(connections[console].screenbuffer);
100
 
101
    switch (key) {
102
        case '\n':
103
            scr->position_y += 1;
104
            scr->position_x =  0;
105
            break;
106
        case '\r':
107
            break;
108
        case '\t':
109
            scr->position_x += 8;
110
            scr->position_x -= scr->position_x % 8;
111
            break;
112
        case '\b':
113
            if (scr->position_x == 0)
114
                break;
115
 
116
            scr->position_x--;
117
 
118
            if (console == active_console) {
119
                ipc_call_async_3(fb_info.phone, FB_PUTCHAR, ' ', scr->position_y, scr->position_x, NULL, NULL);
120
            }
121
 
122
            screenbuffer_putchar(scr, ' ');
123
 
124
            break;
125
        default:   
126
            if (console == active_console) {
127
                ipc_call_async_3(fb_info.phone, FB_PUTCHAR, key, scr->position_y, scr->position_x, NULL, NULL);
128
            }
129
 
130
            screenbuffer_putchar(scr, key);
131
            scr->position_x++;
132
    }
133
 
134
    scr->position_y += (scr->position_x >= scr->size_x);
135
 
136
    if (scr->position_y >= scr->size_y) {
137
        scr->position_y = scr->size_y - 1;
138
        screenbuffer_clear_line(scr, scr->top_line++);
139
        ipc_call_async(fb_info.phone, FB_SCROLL, 1, NULL, NULL);
140
    }
141
 
142
    scr->position_x = scr->position_x % scr->size_x;
143
 
1504 cejka 144
    if (console == active_console) 
145
        ipc_call_async_2(fb_info.phone, FB_CURSOR_GOTO, scr->position_y, scr->position_x, NULL, NULL);
146
 
1497 cejka 147
}
148
 
149
 
1453 palkovsky 150
/* Handler for keyboard */
151
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
1445 cejka 152
{
1453 palkovsky 153
    ipc_callid_t callid;
1445 cejka 154
    ipc_call_t call;
1453 palkovsky 155
    int retval;
1487 cejka 156
    int i, j;
1489 palkovsky 157
    char c,d;
158
    connection_t *conn;
1506 cejka 159
    keyfield_t *interbuffer = NULL;
1453 palkovsky 160
 
1506 cejka 161
//  interbuffer = mmap(,, PROTO_READ|PROTO_WRITE, MAP_ANONYMOUS, , );   
162
 
1453 palkovsky 163
    /* Ignore parameters, the connection is alread opened */
164
    while (1) {
165
        callid = async_get_call(&call);
166
        switch (IPC_GET_METHOD(call)) {
167
        case IPC_M_PHONE_HUNGUP:
168
            ipc_answer_fast(callid,0,0,0);
169
            /* TODO: Handle hangup */
170
            return;
171
        case KBD_PUSHCHAR:
172
            /* got key from keyboard driver */
1465 cejka 173
 
1453 palkovsky 174
            retval = 0;
1476 cejka 175
            c = IPC_GET_ARG1(call);
1453 palkovsky 176
            /* switch to another virtual console */
1481 cejka 177
 
1489 palkovsky 178
            conn = &connections[active_console];
1490 palkovsky 179
//          if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 + CONSOLE_COUNT)) {
180
            if ((c >= '1') && (c < '1' + CONSOLE_COUNT)) {
1487 cejka 181
                /*FIXME: draw another console content from buffer */
1499 cejka 182
                if (c - '1' == active_console)
1497 cejka 183
                        break;
1490 palkovsky 184
                active_console = c - '1';
1489 palkovsky 185
                conn = &connections[active_console];
186
 
187
                ipc_call_async(fb_info.phone, FB_CURSOR_VISIBILITY, 0, NULL, NULL);
1487 cejka 188
                ipc_call_async_2(fb_info.phone, FB_CLEAR, 0, 0, NULL, NULL);
1497 cejka 189
 
1489 palkovsky 190
                for (i = 0; i < conn->screenbuffer.size_x; i++)
191
                    for (j = 0; j < conn->screenbuffer.size_y; j++) {
192
                        d = get_field_at(&(conn->screenbuffer),i, j)->character;
193
                        if (d && d != ' ')
194
                            ipc_call_async_3(fb_info.phone, FB_PUTCHAR, d, j, i, NULL, NULL);
195
                    }
1499 cejka 196
 
1489 palkovsky 197
                ipc_call_async_2(fb_info.phone, FB_CURSOR_GOTO, conn->screenbuffer.position_y, conn->screenbuffer.position_x, NULL, NULL);
198
                ipc_call_async(fb_info.phone, FB_CURSOR_VISIBILITY, 1, NULL, NULL);
1487 cejka 199
 
1453 palkovsky 200
                break;
201
            }
1481 cejka 202
 
203
            /* if client is awaiting key, send it */
1489 palkovsky 204
            if (conn->keyrequest_counter > 0) {    
205
                conn->keyrequest_counter--;
206
                ipc_answer_fast(fifo_pop(conn->keyrequests), 0, c, 0);
1481 cejka 207
                break;
208
            }
209
 
210
            /*FIXME: else store key to its buffer */
1489 palkovsky 211
            keybuffer_push(&conn->keybuffer, c);
1476 cejka 212
 
1453 palkovsky 213
            break;
214
        default:
1459 palkovsky 215
            retval = ENOENT;
1453 palkovsky 216
        }      
1459 palkovsky 217
        ipc_answer_fast(callid, retval, 0, 0);
1453 palkovsky 218
    }
219
}
220
 
221
/** Default thread for new connections */
222
void client_connection(ipc_callid_t iid, ipc_call_t *icall)
223
{
1445 cejka 224
    ipc_callid_t callid;
1453 palkovsky 225
    ipc_call_t call;
226
    int consnum;
227
    ipcarg_t arg1;
228
 
229
    if ((consnum = find_free_connection()) == CONSOLE_COUNT) {
230
        ipc_answer_fast(iid,ELIMIT,0,0);
231
        return;
232
    }
1497 cejka 233
 
1453 palkovsky 234
    connections[consnum].used = 1;
235
    connections[consnum].client_phone = IPC_GET_ARG3(call);
1487 cejka 236
    screenbuffer_clear(&(connections[consnum].screenbuffer));
1497 cejka 237
 
1453 palkovsky 238
    /* Accept the connection */
239
    ipc_answer_fast(iid,0,0,0);
240
 
241
    while (1) {
242
        callid = async_get_call(&call);
243
        switch (IPC_GET_METHOD(call)) {
244
        case IPC_M_PHONE_HUNGUP:
245
            /* TODO */
246
            ipc_answer_fast(callid, 0,0,0);
247
            return;
248
        case CONSOLE_PUTCHAR:
1497 cejka 249
            write_char(consnum, IPC_GET_ARG1(call));
1453 palkovsky 250
            break;
1476 cejka 251
        case CONSOLE_CLEAR:
1487 cejka 252
            /* Send message to fb */
253
            if (consnum == active_console) {
254
                ipc_call_async_2(fb_info.phone, FB_CLEAR, 0, 0, NULL, NULL);
255
            }
256
 
257
            screenbuffer_clear(&(connections[consnum].screenbuffer));
258
 
1476 cejka 259
            break;
260
        case CONSOLE_GOTO:
1487 cejka 261
 
262
            screenbuffer_goto(&(connections[consnum].screenbuffer), IPC_GET_ARG1(call), IPC_GET_ARG2(call));
263
 
1476 cejka 264
            break;
265
 
1453 palkovsky 266
        case CONSOLE_GETCHAR:
1481 cejka 267
            if (keybuffer_empty(&(connections[consnum].keybuffer))) {
268
                /* buffer is empty -> store request */
269
                if (connections[consnum].keyrequest_counter < MAX_KEYREQUESTS_BUFFERED) {      
270
                    fifo_push(connections[consnum].keyrequests, callid);
271
                    connections[consnum].keyrequest_counter++;
272
                } else {
273
                    /* no key available and too many requests => fail */
274
                    ipc_answer_fast(callid, ELIMIT, 0, 0);
275
                }
276
                continue;
1453 palkovsky 277
            };
1476 cejka 278
            keybuffer_pop(&(connections[consnum].keybuffer), (char *)&arg1);
1465 cejka 279
 
1453 palkovsky 280
            break;
281
        }
1465 cejka 282
        ipc_answer_fast(callid, 0, arg1, 0);
1453 palkovsky 283
    }
284
}
285
 
286
int main(int argc, char *argv[])
287
{
288
    ipcarg_t phonehash;
1451 cejka 289
    int kbd_phone, fb_phone;
1445 cejka 290
    ipcarg_t retval, arg1 = 0xdead, arg2 = 0xbeef;
1451 cejka 291
    int i;
1490 palkovsky 292
 
293
    async_set_client_connection(client_connection);
1445 cejka 294
 
295
    /* Connect to keyboard driver */
296
 
1451 cejka 297
    while ((kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0)) < 0) {
1453 palkovsky 298
        usleep(10000);
1445 cejka 299
    };
300
 
1453 palkovsky 301
    if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonehash) != 0) {
1445 cejka 302
        return -1;
303
    };
304
 
305
    /* Connect to framebuffer driver */
306
 
1487 cejka 307
    while ((fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
308
        usleep(10000);
309
    }
310
 
311
    ipc_call_sync_2(fb_info.phone, FB_GET_CSIZE, 0, 0, &(fb_info.rows), &(fb_info.cols));
1489 palkovsky 312
    ipc_call_sync(fb_info.phone, FB_CURSOR_VISIBILITY, 1, NULL);
1487 cejka 313
 
314
    /* Init virtual consoles */
1451 cejka 315
    for (i = 0; i < CONSOLE_COUNT; i++) {
316
        connections[i].used = 0;
317
        keybuffer_init(&(connections[i].keybuffer));
1481 cejka 318
 
319
        connections[i].keyrequests.head = connections[i].keyrequests.tail = 0;
320
        connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
321
        connections[i].keyrequest_counter = 0;
1487 cejka 322
 
323
        if (screenbuffer_init(&(connections[i].screenbuffer), fb_info.cols, fb_info.rows ) == NULL) {
324
            /*FIXME: handle error */
325
            return -1;
326
        }
1451 cejka 327
    }
1445 cejka 328
 
1506 cejka 329
    async_new_connection(phonehash, 0, NULL, keyboard_events);
330
 
1504 cejka 331
    ipc_call_async_2(fb_info.phone, FB_CURSOR_GOTO, 0, 0, NULL, NULL);
1497 cejka 332
 
1453 palkovsky 333
    if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonehash) != 0) {
1445 cejka 334
        return -1;
335
    };
336
 
1453 palkovsky 337
    async_manager();
1451 cejka 338
 
1445 cejka 339
    return 0;  
340
}