Subversion Repositories HelenOS

Rev

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

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