Subversion Repositories HelenOS

Rev

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

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