Subversion Repositories HelenOS-historic

Rev

Rev 1489 | Rev 1497 | 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
 
1476 cejka 43
#define CONSOLE_COUNT 8
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 */
52
    int rows;       /**< Framebuffer rows */
53
    int cols;       /**< Framebuffer columns */
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
 
65
connection_t connections[CONSOLE_COUNT];
66
 
67
static int find_free_connection()
68
{
69
    int i = 0;
70
 
71
    while (i < CONSOLE_COUNT) {
72
        if (connections[i].used == 0)
73
            return i;
74
        ++i;
75
    }
76
    return CONSOLE_COUNT;
77
}
78
 
79
 
80
static int find_connection(int client_phone)
81
{
82
    int i = 0;
83
 
84
    while (i < CONSOLE_COUNT) {
85
        if (connections[i].client_phone == client_phone)
86
            return i;
87
        ++i;
88
    }
89
    return  CONSOLE_COUNT;
90
}
91
 
1453 palkovsky 92
/* Handler for keyboard */
93
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
1445 cejka 94
{
1453 palkovsky 95
    ipc_callid_t callid;
1445 cejka 96
    ipc_call_t call;
1453 palkovsky 97
    int retval;
1487 cejka 98
    int i, j;
1489 palkovsky 99
    char c,d;
100
    connection_t *conn;
1453 palkovsky 101
 
102
    /* Ignore parameters, the connection is alread opened */
103
    while (1) {
104
        callid = async_get_call(&call);
105
        switch (IPC_GET_METHOD(call)) {
106
        case IPC_M_PHONE_HUNGUP:
107
            ipc_answer_fast(callid,0,0,0);
108
            /* TODO: Handle hangup */
109
            return;
110
        case KBD_PUSHCHAR:
111
            /* got key from keyboard driver */
1465 cejka 112
 
1453 palkovsky 113
            retval = 0;
1476 cejka 114
            c = IPC_GET_ARG1(call);
1453 palkovsky 115
            /* switch to another virtual console */
1481 cejka 116
 
1489 palkovsky 117
            conn = &connections[active_console];
1490 palkovsky 118
//          if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 + CONSOLE_COUNT)) {
119
            if ((c >= '1') && (c < '1' + CONSOLE_COUNT)) {
1487 cejka 120
                /*FIXME: draw another console content from buffer */
121
 
1490 palkovsky 122
                active_console = c - '1';
1489 palkovsky 123
                conn = &connections[active_console];
124
 
125
                ipc_call_async(fb_info.phone, FB_CURSOR_VISIBILITY, 0, NULL, NULL);
1487 cejka 126
                ipc_call_async_2(fb_info.phone, FB_CLEAR, 0, 0, NULL, NULL);
1489 palkovsky 127
                for (i = 0; i < conn->screenbuffer.size_x; i++)
128
                    for (j = 0; j < conn->screenbuffer.size_y; j++) {
129
                        d = get_field_at(&(conn->screenbuffer),i, j)->character;
130
                        if (d && d != ' ')
131
                            ipc_call_async_3(fb_info.phone, FB_PUTCHAR, d, j, i, NULL, NULL);
132
                    }
133
                ipc_call_async_2(fb_info.phone, FB_CURSOR_GOTO, conn->screenbuffer.position_y, conn->screenbuffer.position_x, NULL, NULL);
134
                ipc_call_async(fb_info.phone, FB_CURSOR_VISIBILITY, 1, NULL, NULL);
1487 cejka 135
 
1453 palkovsky 136
                break;
137
            }
1481 cejka 138
 
139
            /* if client is awaiting key, send it */
1489 palkovsky 140
            if (conn->keyrequest_counter > 0) {    
141
                conn->keyrequest_counter--;
142
                ipc_answer_fast(fifo_pop(conn->keyrequests), 0, c, 0);
1481 cejka 143
                break;
144
            }
145
 
146
            /*FIXME: else store key to its buffer */
1489 palkovsky 147
            keybuffer_push(&conn->keybuffer, c);
1476 cejka 148
 
1453 palkovsky 149
            break;
150
        default:
1459 palkovsky 151
            retval = ENOENT;
1453 palkovsky 152
        }      
1459 palkovsky 153
        ipc_answer_fast(callid, retval, 0, 0);
1453 palkovsky 154
    }
155
}
156
 
157
/** Default thread for new connections */
158
void client_connection(ipc_callid_t iid, ipc_call_t *icall)
159
{
1445 cejka 160
    ipc_callid_t callid;
1453 palkovsky 161
    ipc_call_t call;
162
    int consnum;
163
    ipcarg_t arg1;
164
 
165
    if ((consnum = find_free_connection()) == CONSOLE_COUNT) {
166
        ipc_answer_fast(iid,ELIMIT,0,0);
167
        return;
168
    }
1487 cejka 169
 
1453 palkovsky 170
    connections[consnum].used = 1;
171
    connections[consnum].client_phone = IPC_GET_ARG3(call);
1487 cejka 172
    screenbuffer_clear(&(connections[consnum].screenbuffer));
1453 palkovsky 173
 
174
    /* Accept the connection */
175
    ipc_answer_fast(iid,0,0,0);
176
 
177
    while (1) {
178
        callid = async_get_call(&call);
179
        switch (IPC_GET_METHOD(call)) {
180
        case IPC_M_PHONE_HUNGUP:
181
            /* TODO */
182
            ipc_answer_fast(callid, 0,0,0);
183
            return;
184
        case CONSOLE_PUTCHAR:
1487 cejka 185
 
186
            /* Send message to fb */
187
            if (consnum == active_console) {
1489 palkovsky 188
                ipc_call_async_3(fb_info.phone, FB_PUTCHAR, IPC_GET_ARG2(call), connections[consnum].screenbuffer.position_y, \
189
                        connections[consnum].screenbuffer.position_x, NULL, NULL);
1476 cejka 190
            }
1487 cejka 191
 
192
            screenbuffer_putchar(&(connections[consnum].screenbuffer), IPC_GET_ARG2(call));
1453 palkovsky 193
            break;
1476 cejka 194
        case CONSOLE_CLEAR:
1487 cejka 195
            /* Send message to fb */
196
            if (consnum == active_console) {
197
                ipc_call_async_2(fb_info.phone, FB_CLEAR, 0, 0, NULL, NULL);
198
            }
199
 
200
            screenbuffer_clear(&(connections[consnum].screenbuffer));
201
 
1476 cejka 202
            break;
203
        case CONSOLE_GOTO:
1487 cejka 204
 
205
            screenbuffer_goto(&(connections[consnum].screenbuffer), IPC_GET_ARG1(call), IPC_GET_ARG2(call));
206
 
1476 cejka 207
            break;
208
 
1453 palkovsky 209
        case CONSOLE_GETCHAR:
1481 cejka 210
            if (keybuffer_empty(&(connections[consnum].keybuffer))) {
211
                /* buffer is empty -> store request */
212
                if (connections[consnum].keyrequest_counter < MAX_KEYREQUESTS_BUFFERED) {      
213
                    fifo_push(connections[consnum].keyrequests, callid);
214
                    connections[consnum].keyrequest_counter++;
215
                } else {
216
                    /* no key available and too many requests => fail */
217
                    ipc_answer_fast(callid, ELIMIT, 0, 0);
218
                }
219
                continue;
1453 palkovsky 220
            };
1476 cejka 221
            keybuffer_pop(&(connections[consnum].keybuffer), (char *)&arg1);
1465 cejka 222
 
1453 palkovsky 223
            break;
224
        }
1465 cejka 225
        ipc_answer_fast(callid, 0, arg1, 0);
1453 palkovsky 226
    }
227
}
228
 
229
int main(int argc, char *argv[])
230
{
231
    ipcarg_t phonehash;
1451 cejka 232
    int kbd_phone, fb_phone;
1445 cejka 233
    ipcarg_t retval, arg1 = 0xdead, arg2 = 0xbeef;
1451 cejka 234
    int i;
1490 palkovsky 235
 
236
    async_set_client_connection(client_connection);
1445 cejka 237
 
238
    /* Connect to keyboard driver */
239
 
1451 cejka 240
    while ((kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0)) < 0) {
1453 palkovsky 241
        usleep(10000);
1445 cejka 242
    };
243
 
1453 palkovsky 244
    if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonehash) != 0) {
1445 cejka 245
        return -1;
246
    };
1453 palkovsky 247
    async_new_connection(phonehash, 0, NULL, keyboard_events);
1445 cejka 248
 
249
    /* Connect to framebuffer driver */
250
 
1487 cejka 251
    while ((fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
252
        usleep(10000);
253
    }
254
 
255
    ipc_call_sync_2(fb_info.phone, FB_GET_CSIZE, 0, 0, &(fb_info.rows), &(fb_info.cols));
1489 palkovsky 256
    ipc_call_sync(fb_info.phone, FB_CURSOR_VISIBILITY, 1, NULL);
1487 cejka 257
 
258
    /* Init virtual consoles */
1451 cejka 259
    for (i = 0; i < CONSOLE_COUNT; i++) {
260
        connections[i].used = 0;
261
        keybuffer_init(&(connections[i].keybuffer));
1481 cejka 262
 
263
        connections[i].keyrequests.head = connections[i].keyrequests.tail = 0;
264
        connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
265
        connections[i].keyrequest_counter = 0;
1487 cejka 266
 
267
        if (screenbuffer_init(&(connections[i].screenbuffer), fb_info.cols, fb_info.rows ) == NULL) {
268
            /*FIXME: handle error */
269
            return -1;
270
        }
1451 cejka 271
    }
1445 cejka 272
 
1453 palkovsky 273
    if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonehash) != 0) {
1445 cejka 274
        return -1;
275
    };
276
 
1453 palkovsky 277
    async_manager();
1451 cejka 278
 
1445 cejka 279
    return 0;  
280
}