Subversion Repositories HelenOS-historic

Rev

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

Rev 1563 Rev 1567
1
/*
1
/*
2
 * Copyright (C) 2006 Josef Cejka
2
 * Copyright (C) 2006 Josef Cejka
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
 
29
 
29
 
30
#include <kbd.h>
30
#include <kbd.h>
31
#include <fb.h>
31
#include <fb.h>
32
#include <ipc/ipc.h>
32
#include <ipc/ipc.h>
33
#include <ipc/fb.h>
33
#include <ipc/fb.h>
34
#include <ipc/services.h>
34
#include <ipc/services.h>
35
#include <errno.h>
35
#include <errno.h>
36
#include <key_buffer.h>
36
#include <key_buffer.h>
37
#include <console.h>
37
#include <console.h>
38
#include <unistd.h>
38
#include <unistd.h>
39
#include <async.h>
39
#include <async.h>
40
#include <libadt/fifo.h>
40
#include <libadt/fifo.h>
41
#include <screenbuffer.h>
41
#include <screenbuffer.h>
42
#include <sys/mman.h>
42
#include <sys/mman.h>
43
 
43
 
44
#include "gcons.h"
44
#include "gcons.h"
45
 
45
 
46
#define MAX_KEYREQUESTS_BUFFERED 32
46
#define MAX_KEYREQUESTS_BUFFERED 32
47
 
47
 
48
#define NAME "CONSOLE"
48
#define NAME "CONSOLE"
49
 
49
 
50
/** Index of currently used virtual console.
50
/** Index of currently used virtual console.
51
 */
51
 */
52
int active_console = 0;
52
int active_console = 0;
53
 
53
 
54
/** Information about framebuffer
54
/** Information about framebuffer
55
 */
55
 */
56
struct {
56
struct {
57
    int phone;      /**< Framebuffer phone */
57
    int phone;      /**< Framebuffer phone */
58
    ipcarg_t rows;      /**< Framebuffer rows */
58
    ipcarg_t rows;      /**< Framebuffer rows */
59
    ipcarg_t cols;      /**< Framebuffer columns */
59
    ipcarg_t cols;      /**< Framebuffer columns */
60
} fb_info;
60
} fb_info;
61
 
61
 
62
 
62
 
63
typedef struct {
63
typedef struct {
64
    keybuffer_t keybuffer;      /**< Buffer for incoming keys. */
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. */
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. */
66
    int keyrequest_counter;     /**< Number of requests in buffer. */
67
    int client_phone;       /**< Phone to connected client. */
67
    int client_phone;       /**< Phone to connected client. */
68
    int used;           /**< 1 if this virtual console is connected to some 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. */
69
    screenbuffer_t screenbuffer;    /**< Screenbuffer for saving screen contents and related settings. */
70
} connection_t;
70
} connection_t;
71
 
71
 
72
static connection_t connections[CONSOLE_COUNT]; /**< Array of data for virtual consoles */
72
static connection_t connections[CONSOLE_COUNT]; /**< Array of data for virtual consoles */
73
static keyfield_t *interbuffer = NULL;          /**< Pointer to memory shared with framebufer used for faster virt. console switching */
73
static keyfield_t *interbuffer = NULL;          /**< Pointer to memory shared with framebufer used for faster virt. console switching */
74
 
74
 
75
static int kernel_pixmap = -1;      /**< Number of fb pixmap, where kernel console is stored */
75
static int kernel_pixmap = -1;      /**< Number of fb pixmap, where kernel console is stored */
76
 
76
 
77
 
77
 
78
/** Find unused virtual console.
78
/** Find unused virtual console.
79
 *
79
 *
80
 */
80
 */
81
static int find_free_connection()
81
static int find_free_connection()
82
{
82
{
83
    int i = 0;
83
    int i = 0;
84
   
84
   
85
    while (i < CONSOLE_COUNT) {
85
    while (i < CONSOLE_COUNT) {
86
        if (connections[i].used == 0)
86
        if (connections[i].used == 0)
87
            return i;
87
            return i;
88
        ++i;
88
        ++i;
89
    }
89
    }
90
    return CONSOLE_COUNT;
90
    return CONSOLE_COUNT;
91
}
91
}
92
 
92
 
93
/** Find index of virtual console used by client with given phone.
93
/** Find index of virtual console used by client with given phone.
94
 *
94
 *
95
 */
95
 */
96
static int find_connection(int client_phone)
96
static int find_connection(int client_phone)
97
{
97
{
98
    int i = 0;
98
    int i = 0;
99
   
99
   
100
    while (i < CONSOLE_COUNT) {
100
    while (i < CONSOLE_COUNT) {
101
        if (connections[i].client_phone == client_phone)
101
        if (connections[i].client_phone == client_phone)
102
            return i;
102
            return i;
103
        ++i;
103
        ++i;
104
    }
104
    }
105
    return  CONSOLE_COUNT;
105
    return  CONSOLE_COUNT;
106
}
106
}
107
 
107
 
108
static void clrscr(void)
108
static void clrscr(void)
109
{
109
{
110
    nsend_call(fb_info.phone, FB_CLEAR, 0);
110
    nsend_call(fb_info.phone, FB_CLEAR, 0);
111
}
111
}
112
 
112
 
113
static void curs_visibility(int v)
113
static void curs_visibility(int v)
114
{
114
{
115
    send_call(fb_info.phone, FB_CURSOR_VISIBILITY, v);
115
    send_call(fb_info.phone, FB_CURSOR_VISIBILITY, v);
116
}
116
}
117
 
117
 
118
static void curs_goto(int row, int col)
118
static void curs_goto(int row, int col)
119
{
119
{
120
    nsend_call_2(fb_info.phone, FB_CURSOR_GOTO, row, col);
120
    nsend_call_2(fb_info.phone, FB_CURSOR_GOTO, row, col);
121
   
121
   
122
}
122
}
123
 
123
 
124
static void set_style(style_t *style)
124
static void set_style(style_t *style)
125
{
125
{
126
    nsend_call_2(fb_info.phone, FB_SET_STYLE, style->fg_color, style->bg_color);
126
    nsend_call_2(fb_info.phone, FB_SET_STYLE, style->fg_color, style->bg_color);
127
}
127
}
128
 
128
 
129
static void set_style_col(int fgcolor, int bgcolor)
129
static void set_style_col(int fgcolor, int bgcolor)
130
{
130
{
131
    nsend_call_2(fb_info.phone, FB_SET_STYLE, fgcolor, bgcolor);
131
    nsend_call_2(fb_info.phone, FB_SET_STYLE, fgcolor, bgcolor);
132
}
132
}
133
 
133
 
134
static void prtchr(char c, int row, int col)
134
static void prtchr(char c, int row, int col)
135
{
135
{
136
    nsend_call_3(fb_info.phone, FB_PUTCHAR, c, row, col);
136
    nsend_call_3(fb_info.phone, FB_PUTCHAR, c, row, col);
137
   
137
   
138
}
138
}
139
 
139
 
140
/** Check key and process special keys.
140
/** Check key and process special keys.
141
 *
141
 *
142
 * */
142
 * */
143
static void write_char(int console, char key)
143
static void write_char(int console, char key)
144
{
144
{
145
    screenbuffer_t *scr = &(connections[console].screenbuffer);
145
    screenbuffer_t *scr = &(connections[console].screenbuffer);
146
   
146
   
147
    switch (key) {
147
    switch (key) {
148
        case '\n':
148
        case '\n':
149
            scr->position_y += 1;
149
            scr->position_y += 1;
150
            scr->position_x =  0;
150
            scr->position_x =  0;
151
            break;
151
            break;
152
        case '\r':
152
        case '\r':
153
            break;
153
            break;
154
        case '\t':
154
        case '\t':
155
            scr->position_x += 8;
155
            scr->position_x += 8;
156
            scr->position_x -= scr->position_x % 8;
156
            scr->position_x -= scr->position_x % 8;
157
            break;
157
            break;
158
        case '\b':
158
        case '\b':
159
            if (scr->position_x == 0)
159
            if (scr->position_x == 0)
160
                break;
160
                break;
161
 
161
 
162
            scr->position_x--;
162
            scr->position_x--;
163
 
163
 
164
            if (console == active_console)
164
            if (console == active_console)
165
                prtchr(' ', scr->position_y, scr->position_x);
165
                prtchr(' ', scr->position_y, scr->position_x);
166
   
166
   
167
            screenbuffer_putchar(scr, ' ');
167
            screenbuffer_putchar(scr, ' ');
168
           
168
           
169
            break;
169
            break;
170
        default:   
170
        default:   
171
            if (console == active_console)
171
            if (console == active_console)
172
                prtchr(key, scr->position_y, scr->position_x);
172
                prtchr(key, scr->position_y, scr->position_x);
173
   
173
   
174
            screenbuffer_putchar(scr, key);
174
            screenbuffer_putchar(scr, key);
175
            scr->position_x++;
175
            scr->position_x++;
176
    }
176
    }
177
   
177
   
178
    scr->position_y += (scr->position_x >= scr->size_x);
178
    scr->position_y += (scr->position_x >= scr->size_x);
179
   
179
   
180
    if (scr->position_y >= scr->size_y) {
180
    if (scr->position_y >= scr->size_y) {
181
        scr->position_y = scr->size_y - 1;
181
        scr->position_y = scr->size_y - 1;
182
        screenbuffer_clear_line(scr, scr->top_line++);
182
        screenbuffer_clear_line(scr, scr->top_line++);
183
        if (console == active_console)
183
        if (console == active_console)
184
            nsend_call(fb_info.phone, FB_SCROLL, 1);
184
            nsend_call(fb_info.phone, FB_SCROLL, 1);
185
    }
185
    }
186
   
186
   
187
    scr->position_x = scr->position_x % scr->size_x;
187
    scr->position_x = scr->position_x % scr->size_x;
188
   
188
   
189
    if (console == active_console)
189
    if (console == active_console)
190
        curs_goto(scr->position_y, scr->position_x);
190
        curs_goto(scr->position_y, scr->position_x);
191
   
191
   
192
}
192
}
193
 
193
 
194
/** Save current screen to pixmap, draw old pixmap
194
/** Save current screen to pixmap, draw old pixmap
195
 *
195
 *
196
 * @param oldpixmap Old pixmap
196
 * @param oldpixmap Old pixmap
197
 * @return ID of pixmap of current screen
197
 * @return ID of pixmap of current screen
198
 */
198
 */
199
static int switch_screens(int oldpixmap)
199
static int switch_screens(int oldpixmap)
200
{
200
{
201
    int newpmap;
201
    int newpmap;
202
       
202
       
203
    /* Save screen */
203
    /* Save screen */
204
    newpmap = sync_send(fb_info.phone, FB_VP2PIXMAP, 0, NULL);
204
    newpmap = sync_send(fb_info.phone, FB_VP2PIXMAP, 0, NULL);
205
    if (newpmap < 0)
205
    if (newpmap < 0)
206
        return -1;
206
        return -1;
207
 
207
 
208
    if (oldpixmap != -1) {
208
    if (oldpixmap != -1) {
209
        /* Show old screen */
209
        /* Show old screen */
210
        nsend_call_2(fb_info.phone, FB_VP_DRAW_PIXMAP, 0, oldpixmap);
210
        nsend_call_2(fb_info.phone, FB_VP_DRAW_PIXMAP, 0, oldpixmap);
211
        /* Drop old pixmap */
211
        /* Drop old pixmap */
212
        nsend_call(fb_info.phone, FB_DROP_PIXMAP, oldpixmap);
212
        nsend_call(fb_info.phone, FB_DROP_PIXMAP, oldpixmap);
213
    }
213
    }
214
   
214
   
215
    return newpmap;
215
    return newpmap;
216
}
216
}
217
 
217
 
218
/** Switch to new console */
218
/** Switch to new console */
219
static void change_console(int newcons)
219
static void change_console(int newcons)
220
{
220
{
221
    connection_t *conn;
221
    connection_t *conn;
222
    static int console_pixmap = -1;
222
    static int console_pixmap = -1;
223
    int i, j;
223
    int i, j;
224
    char c;
224
    char c;
225
 
225
 
226
    if (newcons == active_console)
226
    if (newcons == active_console)
227
        return;
227
        return;
228
 
228
 
229
    if (newcons == KERNEL_CONSOLE) {
229
    if (newcons == KERNEL_CONSOLE) {
230
        if (active_console == KERNEL_CONSOLE)
230
        if (active_console == KERNEL_CONSOLE)
231
            return;
231
            return;
232
        active_console = KERNEL_CONSOLE;
232
        active_console = KERNEL_CONSOLE;
233
        curs_visibility(0);
233
        curs_visibility(0);
234
 
234
 
235
        if (kernel_pixmap == -1) {
235
        if (kernel_pixmap == -1) {
236
            /* store/restore unsupported */
236
            /* store/restore unsupported */
237
            set_style_col(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
237
            set_style_col(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
238
            clrscr();
238
            clrscr();
239
        } else {
239
        } else {
240
            gcons_in_kernel();
240
            gcons_in_kernel();
241
            console_pixmap = switch_screens(kernel_pixmap);
241
            console_pixmap = switch_screens(kernel_pixmap);
242
            kernel_pixmap = -1;
242
            kernel_pixmap = -1;
243
        }
243
        }
244
 
244
 
245
        __SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE);
245
        __SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE);
246
        return;
246
        return;
247
    }
247
    }
248
   
248
   
249
    if (console_pixmap != -1) {
249
    if (console_pixmap != -1) {
250
        kernel_pixmap = switch_screens(console_pixmap);
250
        kernel_pixmap = switch_screens(console_pixmap);
251
        console_pixmap = -1;
251
        console_pixmap = -1;
252
    }
252
    }
253
   
253
   
254
    active_console = newcons;
254
    active_console = newcons;
255
    gcons_change_console(newcons);
255
    gcons_change_console(newcons);
256
    conn = &connections[active_console];
256
    conn = &connections[active_console];
257
 
257
 
258
    set_style(&conn->screenbuffer.style);
258
    set_style(&conn->screenbuffer.style);
259
    curs_goto(conn->screenbuffer.position_y, conn->screenbuffer.position_x);
259
    curs_goto(conn->screenbuffer.position_y, conn->screenbuffer.position_x);
260
    if (interbuffer) {
260
    if (interbuffer) {
261
        for (i = 0; i < conn->screenbuffer.size_x; i++)
261
        for (i = 0; i < conn->screenbuffer.size_x; i++)
262
            for (j = 0; j < conn->screenbuffer.size_y; j++)
262
            for (j = 0; j < conn->screenbuffer.size_y; j++)
263
                interbuffer[i + j*conn->screenbuffer.size_x] = *get_field_at(&(conn->screenbuffer),i, j);
263
                interbuffer[i + j*conn->screenbuffer.size_x] = *get_field_at(&(conn->screenbuffer),i, j);
264
        /* This call can preempt, but we are already at the end */
264
        /* This call can preempt, but we are already at the end */
265
        sync_send_2(fb_info.phone, FB_DRAW_TEXT_DATA, 0, 0, NULL, NULL);       
265
        sync_send_2(fb_info.phone, FB_DRAW_TEXT_DATA, 0, 0, NULL, NULL);       
-
 
266
        curs_visibility(1);
266
    } else {
267
    } else {
267
        curs_visibility(0);
268
        curs_visibility(0);
268
        clrscr();
269
        clrscr();
269
       
270
       
270
        for (i = 0; i < conn->screenbuffer.size_x; i++)
271
        for (i = 0; i < conn->screenbuffer.size_x; i++)
271
            for (j = 0; j < conn->screenbuffer.size_y; j++) {
272
            for (j = 0; j < conn->screenbuffer.size_y; j++) {
272
                c = get_field_at(&(conn->screenbuffer),i, j)->character;
273
                c = get_field_at(&(conn->screenbuffer),i, j)->character;
273
                if (c && c != ' ')
274
                if (c && c != ' ')
274
                    prtchr(c, j, i);
275
                    prtchr(c, j, i);
275
            }
276
            }
276
       
277
       
277
        curs_visibility(1);
278
        curs_visibility(1);
278
    }
279
    }
279
}
280
}
280
 
281
 
281
/** Handler for keyboard */
282
/** Handler for keyboard */
282
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
283
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
283
{
284
{
284
    ipc_callid_t callid;
285
    ipc_callid_t callid;
285
    ipc_call_t call;
286
    ipc_call_t call;
286
    int retval;
287
    int retval;
287
    int c;
288
    int c;
288
    connection_t *conn;
289
    connection_t *conn;
289
   
290
   
290
    /* Ignore parameters, the connection is alread opened */
291
    /* Ignore parameters, the connection is alread opened */
291
    while (1) {
292
    while (1) {
292
        callid = async_get_call(&call);
293
        callid = async_get_call(&call);
293
        switch (IPC_GET_METHOD(call)) {
294
        switch (IPC_GET_METHOD(call)) {
294
        case IPC_M_PHONE_HUNGUP:
295
        case IPC_M_PHONE_HUNGUP:
295
            ipc_answer_fast(callid,0,0,0);
296
            ipc_answer_fast(callid,0,0,0);
296
            /* TODO: Handle hangup */
297
            /* TODO: Handle hangup */
297
            return;
298
            return;
298
        case KBD_PUSHCHAR:
299
        case KBD_PUSHCHAR:
299
            /* got key from keyboard driver */
300
            /* got key from keyboard driver */
300
           
301
           
301
            retval = 0;
302
            retval = 0;
302
            c = IPC_GET_ARG1(call);
303
            c = IPC_GET_ARG1(call);
303
            /* switch to another virtual console */
304
            /* switch to another virtual console */
304
           
305
           
305
            conn = &connections[active_console];
306
            conn = &connections[active_console];
306
//          if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 + CONSOLE_COUNT)) {
307
//          if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 + CONSOLE_COUNT)) {
307
            if ((c >= 0x101) && (c < 0x101 + CONSOLE_COUNT)) {
308
            if ((c >= 0x101) && (c < 0x101 + CONSOLE_COUNT)) {
308
                if (c == 0x112)
309
                if (c == 0x112)
309
                    change_console(KERNEL_CONSOLE);
310
                    change_console(KERNEL_CONSOLE);
310
                else
311
                else
311
                    change_console(c - 0x101);
312
                    change_console(c - 0x101);
312
                break;
313
                break;
313
            }
314
            }
314
           
315
           
315
            /* if client is awaiting key, send it */
316
            /* if client is awaiting key, send it */
316
            if (conn->keyrequest_counter > 0) {    
317
            if (conn->keyrequest_counter > 0) {    
317
                conn->keyrequest_counter--;
318
                conn->keyrequest_counter--;
318
                ipc_answer_fast(fifo_pop(conn->keyrequests), 0, c, 0);
319
                ipc_answer_fast(fifo_pop(conn->keyrequests), 0, c, 0);
319
                break;
320
                break;
320
            }
321
            }
321
           
322
           
322
            /*FIXME: else store key to its buffer */
323
            /*FIXME: else store key to its buffer */
323
            keybuffer_push(&conn->keybuffer, c);
324
            keybuffer_push(&conn->keybuffer, c);
324
           
325
           
325
            break;
326
            break;
326
        default:
327
        default:
327
            retval = ENOENT;
328
            retval = ENOENT;
328
        }      
329
        }      
329
        ipc_answer_fast(callid, retval, 0, 0);
330
        ipc_answer_fast(callid, retval, 0, 0);
330
    }
331
    }
331
}
332
}
332
 
333
 
333
/** Default thread for new connections */
334
/** Default thread for new connections */
334
static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
335
static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
335
{
336
{
336
    ipc_callid_t callid;
337
    ipc_callid_t callid;
337
    ipc_call_t call;
338
    ipc_call_t call;
338
    int consnum;
339
    int consnum;
339
    ipcarg_t arg1, arg2;
340
    ipcarg_t arg1, arg2;
340
 
341
 
341
    if ((consnum = find_free_connection()) == CONSOLE_COUNT) {
342
    if ((consnum = find_free_connection()) == CONSOLE_COUNT) {
342
        ipc_answer_fast(iid,ELIMIT,0,0);
343
        ipc_answer_fast(iid,ELIMIT,0,0);
343
        return;
344
        return;
344
    }
345
    }
345
   
346
   
346
    gcons_notify_connect(consnum);
347
    gcons_notify_connect(consnum);
347
    connections[consnum].used = 1;
348
    connections[consnum].used = 1;
348
    connections[consnum].client_phone = IPC_GET_ARG3(call);
349
    connections[consnum].client_phone = IPC_GET_ARG3(call);
349
    screenbuffer_clear(&(connections[consnum].screenbuffer));
350
    screenbuffer_clear(&(connections[consnum].screenbuffer));
350
   
351
   
351
    /* Accept the connection */
352
    /* Accept the connection */
352
    ipc_answer_fast(iid,0,0,0);
353
    ipc_answer_fast(iid,0,0,0);
353
   
354
   
354
    while (1) {
355
    while (1) {
355
        callid = async_get_call(&call);
356
        callid = async_get_call(&call);
356
        arg1 = arg2 = 0;
357
        arg1 = arg2 = 0;
357
        switch (IPC_GET_METHOD(call)) {
358
        switch (IPC_GET_METHOD(call)) {
358
        case IPC_M_PHONE_HUNGUP:
359
        case IPC_M_PHONE_HUNGUP:
359
            /* TODO */
360
            /* TODO */
360
            ipc_answer_fast(callid, 0,0,0);
361
            ipc_answer_fast(callid, 0,0,0);
361
            return;
362
            return;
362
        case CONSOLE_PUTCHAR:
363
        case CONSOLE_PUTCHAR:
363
            write_char(consnum, IPC_GET_ARG1(call));
364
            write_char(consnum, IPC_GET_ARG1(call));
364
            gcons_notify_char(consnum);
365
            gcons_notify_char(consnum);
365
            break;
366
            break;
366
        case CONSOLE_CLEAR:
367
        case CONSOLE_CLEAR:
367
            /* Send message to fb */
368
            /* Send message to fb */
368
            if (consnum == active_console) {
369
            if (consnum == active_console) {
369
                send_call(fb_info.phone, FB_CLEAR, 0);
370
                send_call(fb_info.phone, FB_CLEAR, 0);
370
            }
371
            }
371
           
372
           
372
            screenbuffer_clear(&(connections[consnum].screenbuffer));
373
            screenbuffer_clear(&(connections[consnum].screenbuffer));
373
           
374
           
374
            break;
375
            break;
375
        case CONSOLE_GOTO:
376
        case CONSOLE_GOTO:
376
           
377
           
377
            screenbuffer_goto(&(connections[consnum].screenbuffer), IPC_GET_ARG2(call), IPC_GET_ARG1(call));
378
            screenbuffer_goto(&(connections[consnum].screenbuffer), IPC_GET_ARG2(call), IPC_GET_ARG1(call));
-
 
379
            if (consnum == active_console)
378
            curs_goto(IPC_GET_ARG1(call),IPC_GET_ARG2(call));
380
                curs_goto(IPC_GET_ARG1(call),IPC_GET_ARG2(call));
379
           
381
           
380
            break;
382
            break;
381
 
383
 
382
        case CONSOLE_GETSIZE:
384
        case CONSOLE_GETSIZE:
383
            arg1 = fb_info.rows;
385
            arg1 = fb_info.rows;
384
            arg2 = fb_info.cols;
386
            arg2 = fb_info.cols;
385
            break;
387
            break;
386
        case CONSOLE_FLUSH:
388
        case CONSOLE_FLUSH:
387
            sync_send_2(fb_info.phone, FB_FLUSH, 0, 0, NULL, NULL);    
389
            sync_send_2(fb_info.phone, FB_FLUSH, 0, 0, NULL, NULL);    
388
            break;
390
            break;
389
        case CONSOLE_SET_STYLE:
391
        case CONSOLE_SET_STYLE:
390
           
392
           
391
            arg1 = IPC_GET_ARG1(call);
393
            arg1 = IPC_GET_ARG1(call);
392
            arg2 = IPC_GET_ARG2(call);
394
            arg2 = IPC_GET_ARG2(call);
393
            screenbuffer_set_style(&(connections[consnum].screenbuffer),arg1, arg2);
395
            screenbuffer_set_style(&(connections[consnum].screenbuffer),arg1, arg2);
394
            if (consnum == active_console)
396
            if (consnum == active_console)
395
                set_style_col(arg1, arg2);
397
                set_style_col(arg1, arg2);
396
               
398
               
397
            break;
399
            break;
398
        case CONSOLE_GETCHAR:
400
        case CONSOLE_GETCHAR:
399
            if (keybuffer_empty(&(connections[consnum].keybuffer))) {
401
            if (keybuffer_empty(&(connections[consnum].keybuffer))) {
400
                /* buffer is empty -> store request */
402
                /* buffer is empty -> store request */
401
                if (connections[consnum].keyrequest_counter < MAX_KEYREQUESTS_BUFFERED) {      
403
                if (connections[consnum].keyrequest_counter < MAX_KEYREQUESTS_BUFFERED) {      
402
                    fifo_push(connections[consnum].keyrequests, callid);
404
                    fifo_push(connections[consnum].keyrequests, callid);
403
                    connections[consnum].keyrequest_counter++;
405
                    connections[consnum].keyrequest_counter++;
404
                } else {
406
                } else {
405
                    /* no key available and too many requests => fail */
407
                    /* no key available and too many requests => fail */
406
                    ipc_answer_fast(callid, ELIMIT, 0, 0);
408
                    ipc_answer_fast(callid, ELIMIT, 0, 0);
407
                }
409
                }
408
                continue;
410
                continue;
409
            };
411
            };
410
            keybuffer_pop(&(connections[consnum].keybuffer), (char *)&arg1);
412
            keybuffer_pop(&(connections[consnum].keybuffer), (char *)&arg1);
411
           
413
           
412
            break;
414
            break;
413
        }
415
        }
414
        ipc_answer_fast(callid, 0, arg1, arg2);
416
        ipc_answer_fast(callid, 0, arg1, arg2);
415
    }
417
    }
416
}
418
}
417
 
419
 
418
int main(int argc, char *argv[])
420
int main(int argc, char *argv[])
419
{
421
{
420
    ipcarg_t phonehash;
422
    ipcarg_t phonehash;
421
    int kbd_phone, fb_phone;
423
    int kbd_phone, fb_phone;
422
    ipcarg_t retval, arg1 = 0xdead, arg2 = 0xbeef;
424
    ipcarg_t retval, arg1 = 0xdead, arg2 = 0xbeef;
423
    int i;
425
    int i;
424
 
426
 
425
    async_set_client_connection(client_connection);
427
    async_set_client_connection(client_connection);
426
   
428
   
427
    /* Connect to keyboard driver */
429
    /* Connect to keyboard driver */
428
 
430
 
429
    while ((kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0)) < 0) {
431
    while ((kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0)) < 0) {
430
        usleep(10000);
432
        usleep(10000);
431
    };
433
    };
432
   
434
   
433
    if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonehash) != 0) {
435
    if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonehash) != 0) {
434
        return -1;
436
        return -1;
435
    };
437
    };
436
 
438
 
437
    /* Connect to framebuffer driver */
439
    /* Connect to framebuffer driver */
438
   
440
   
439
    while ((fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
441
    while ((fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
440
        usleep(10000);
442
        usleep(10000);
441
    }
443
    }
442
   
444
   
443
    /* Save old kernel screen */
445
    /* Save old kernel screen */
444
    kernel_pixmap = switch_screens(-1);
446
    kernel_pixmap = switch_screens(-1);
445
 
447
 
446
    /* Initialize gcons */
448
    /* Initialize gcons */
447
    gcons_init(fb_info.phone);
449
    gcons_init(fb_info.phone);
448
    /* Synchronize, the gcons can have something in queue */
450
    /* Synchronize, the gcons can have something in queue */
449
    sync_send_2(fb_info.phone, FB_GET_CSIZE, 0, 0, NULL, NULL);
451
    sync_send_2(fb_info.phone, FB_GET_CSIZE, 0, 0, NULL, NULL);
450
 
452
 
451
   
453
   
452
    ipc_call_sync_2(fb_info.phone, FB_GET_CSIZE, 0, 0, &(fb_info.rows), &(fb_info.cols));
454
    ipc_call_sync_2(fb_info.phone, FB_GET_CSIZE, 0, 0, &(fb_info.rows), &(fb_info.cols));
453
    set_style_col(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
455
    set_style_col(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
454
    clrscr();
456
    clrscr();
455
   
457
   
456
    /* Init virtual consoles */
458
    /* Init virtual consoles */
457
    for (i = 0; i < CONSOLE_COUNT; i++) {
459
    for (i = 0; i < CONSOLE_COUNT; i++) {
458
        connections[i].used = 0;
460
        connections[i].used = 0;
459
        keybuffer_init(&(connections[i].keybuffer));
461
        keybuffer_init(&(connections[i].keybuffer));
460
       
462
       
461
        connections[i].keyrequests.head = connections[i].keyrequests.tail = 0;
463
        connections[i].keyrequests.head = connections[i].keyrequests.tail = 0;
462
        connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
464
        connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
463
        connections[i].keyrequest_counter = 0;
465
        connections[i].keyrequest_counter = 0;
464
       
466
       
465
        if (screenbuffer_init(&(connections[i].screenbuffer), fb_info.cols, fb_info.rows ) == NULL) {
467
        if (screenbuffer_init(&(connections[i].screenbuffer), fb_info.cols, fb_info.rows ) == NULL) {
466
            /*FIXME: handle error */
468
            /*FIXME: handle error */
467
            return -1;
469
            return -1;
468
        }
470
        }
469
    }
471
    }
470
   
472
   
471
    if ((interbuffer = mmap(NULL, sizeof(keyfield_t) * fb_info.cols * fb_info.rows , PROTO_READ|PROTO_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, 0 ,0 )) != NULL) {
473
    if ((interbuffer = mmap(NULL, sizeof(keyfield_t) * fb_info.cols * fb_info.rows , PROTO_READ|PROTO_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, 0 ,0 )) != NULL) {
472
        if (ipc_call_sync_3(fb_info.phone, IPC_M_AS_AREA_SEND, (ipcarg_t)interbuffer, 0, AS_AREA_READ, NULL, NULL, NULL) != 0) {
474
        if (ipc_call_sync_3(fb_info.phone, IPC_M_AS_AREA_SEND, (ipcarg_t)interbuffer, 0, AS_AREA_READ, NULL, NULL, NULL) != 0) {
473
            munmap(interbuffer, sizeof(keyfield_t) * fb_info.cols * fb_info.rows);
475
            munmap(interbuffer, sizeof(keyfield_t) * fb_info.cols * fb_info.rows);
474
            interbuffer = NULL;
476
            interbuffer = NULL;
475
        }
477
        }
476
    }
478
    }
477
 
479
 
478
    async_new_connection(phonehash, 0, NULL, keyboard_events);
480
    async_new_connection(phonehash, 0, NULL, keyboard_events);
479
   
481
   
480
    sync_send_2(fb_info.phone, FB_CURSOR_GOTO, 0, 0, NULL, NULL);
482
    sync_send_2(fb_info.phone, FB_CURSOR_GOTO, 0, 0, NULL, NULL);
481
    nsend_call(fb_info.phone, FB_CURSOR_VISIBILITY, 1);
483
    nsend_call(fb_info.phone, FB_CURSOR_VISIBILITY, 1);
482
 
484
 
483
    /* Register at NS */
485
    /* Register at NS */
484
    if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonehash) != 0) {
486
    if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonehash) != 0) {
485
        return -1;
487
        return -1;
486
    };
488
    };
487
   
489
   
488
    async_manager();
490
    async_manager();
489
 
491
 
490
    return 0;  
492
    return 0;  
491
}
493
}
492
 
494
 
493
 
495