Subversion Repositories HelenOS

Rev

Rev 4377 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4377 Rev 4692
Line 25... Line 25...
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 <kbd.h>
38
#include <kbd.h>
39
#include <kbd/keycode.h>
39
#include <io/keycode.h>
40
#include <ipc/fb.h>
40
#include <ipc/fb.h>
41
#include <ipc/services.h>
41
#include <ipc/services.h>
42
#include <errno.h>
42
#include <errno.h>
43
#include <key_buffer.h>
43
#include <keybuffer.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 <adt/fifo.h>
48
#include <screenbuffer.h>
-
 
49
#include <sys/mman.h>
48
#include <sys/mman.h>
50
#include <stdio.h>
49
#include <stdio.h>
51
#include <string.h>
50
#include <string.h>
52
#include <sysinfo.h>
51
#include <sysinfo.h>
53
#include <event.h>
52
#include <event.h>
-
 
53
#include <devmap.h>
-
 
54
#include <fibril_sync.h>
54
 
55
 
55
#include "console.h"
56
#include "console.h"
56
#include "gcons.h"
57
#include "gcons.h"
-
 
58
#include "screenbuffer.h"
57
 
59
 
58
#define MAX_KEYREQUESTS_BUFFERED 32
60
#define NAME  "console"
59
 
61
 
60
#define NAME "console"
62
#define MAX_DEVICE_NAME  32
61
 
-
 
62
/** Index of currently used virtual console.
-
 
63
 */
-
 
64
int active_console = 0;
-
 
65
int prev_console = 0;
-
 
66
 
63
 
67
/** Phone to the keyboard driver. */
64
/** Phone to the keyboard driver. */
68
static int kbd_phone;
65
static int kbd_phone;
69
 
66
 
70
/** Information about framebuffer */
67
/** Information about framebuffer */
71
struct {
68
struct {
72
    int phone;      /**< Framebuffer phone */
69
    int phone;      /**< Framebuffer phone */
73
    ipcarg_t rows;      /**< Framebuffer rows */
70
    ipcarg_t cols;  /**< Framebuffer columns */
74
    ipcarg_t cols;      /**< Framebuffer columns */
71
    ipcarg_t rows;  /**< Framebuffer rows */
-
 
72
    int color_cap;  /**< Color capabilities (FB_CCAP_xxx) */
75
} fb_info;
73
} fb_info;
76
 
74
 
77
typedef struct {
75
typedef struct {
78
    keybuffer_t keybuffer;      /**< Buffer for incoming keys. */
76
    size_t index;             /**< Console index */
79
    /** Buffer for unsatisfied request for keys. */
-
 
80
    FIFO_CREATE_STATIC(keyrequests, ipc_callid_t,
-
 
81
        MAX_KEYREQUESTS_BUFFERED); 
-
 
82
    int keyrequest_counter;     /**< Number of requests in buffer. */
77
    size_t refcount;          /**< Connection reference count */
83
    int client_phone;       /**< Phone to connected client. */
78
    dev_handle_t dev_handle;  /**< Device handle */
84
    int used;           /**< 1 if this virtual console is
79
    keybuffer_t keybuffer;    /**< Buffer for incoming keys. */
85
                     * connected to some client.*/
-
 
86
    screenbuffer_t screenbuffer;    /**< Screenbuffer for saving screen
80
    screenbuffer_t scr;       /**< Screenbuffer for saving screen
87
                     * contents and related settings. */
81
                                   contents and related settings. */
88
} connection_t;
82
} console_t;
89
 
83
 
-
 
84
/** Array of data for virtual consoles */
90
static connection_t connections[CONSOLE_COUNT]; /**< Array of data for virtual
85
static console_t consoles[CONSOLE_COUNT];
-
 
86
 
-
 
87
static console_t *active_console = &consoles[0];
91
                         * consoles */
88
static console_t *prev_console = &consoles[0];
92
static keyfield_t *interbuffer = NULL;      /**< Pointer to memory shared
89
static console_t *kernel_console = &consoles[KERNEL_CONSOLE];
-
 
90
 
93
                         * with framebufer used for
91
/** Pointer to memory shared with framebufer used for
94
                         * faster virtual console
92
    faster virtual console switching */
95
                         * switching */
93
static keyfield_t *interbuffer = NULL;
96
 
94
 
97
/** Information on row-span yet unsent to FB driver. */
95
/** Information on row-span yet unsent to FB driver. */
98
struct {
96
struct {
99
    int row;        /**< Row where the span lies. */
97
    size_t col;  /**< Leftmost column of the span. */
100
    int col;        /**< Leftmost column of the span. */
98
    size_t row;  /**< Row where the span lies. */
101
    int n;          /**< Width of the span. */
99
    size_t cnt;  /**< Width of the span. */
102
} fb_pending;
100
} fb_pending;
103
 
101
 
104
/** Size of cwrite_buf. */
-
 
105
#define CWRITE_BUF_SIZE 256
-
 
106
 
-
 
107
/** Buffer for receiving data via the CONSOLE_WRITE call from the client. */
-
 
108
static char cwrite_buf[CWRITE_BUF_SIZE];
102
static FIBRIL_MUTEX_INITIALIZE(input_mutex);
109
 
-
 
110
static void fb_putchar(wchar_t c, int row, int col);
103
static FIBRIL_CONDVAR_INITIALIZE(input_cv);
111
 
-
 
112
 
-
 
113
/** Find unused virtual console.
-
 
114
 *
-
 
115
 */
-
 
116
static int find_free_connection(void)
-
 
117
{
-
 
118
    int i;
-
 
119
   
-
 
120
    for (i = 0; i < CONSOLE_COUNT; i++) {
-
 
121
        if (!connections[i].used)
-
 
122
            return i;
-
 
123
    }
-
 
124
    return -1;
-
 
125
}
-
 
126
 
-
 
127
static void clrscr(void)
-
 
128
{
-
 
129
    async_msg_0(fb_info.phone, FB_CLEAR);
-
 
130
}
-
 
131
 
104
 
132
static void curs_visibility(bool visible)
105
static void curs_visibility(bool visible)
133
{
106
{
134
    async_msg_1(fb_info.phone, FB_CURSOR_VISIBILITY, visible);
107
    async_msg_1(fb_info.phone, FB_CURSOR_VISIBILITY, visible);
135
}
108
}
Line 137... Line 110...
137
static void curs_hide_sync(void)
110
static void curs_hide_sync(void)
138
{
111
{
139
    ipc_call_sync_1_0(fb_info.phone, FB_CURSOR_VISIBILITY, false);
112
    ipc_call_sync_1_0(fb_info.phone, FB_CURSOR_VISIBILITY, false);
140
}
113
}
141
 
114
 
142
static void curs_goto(int row, int col)
115
static void curs_goto(size_t x, size_t y)
143
{
116
{
144
    async_msg_2(fb_info.phone, FB_CURSOR_GOTO, row, col);
117
    async_msg_2(fb_info.phone, FB_CURSOR_GOTO, x, y);
-
 
118
}
-
 
119
 
-
 
120
static void screen_clear(void)
-
 
121
{
-
 
122
    async_msg_0(fb_info.phone, FB_CLEAR);
145
}
123
}
146
 
124
 
147
static void screen_yield(void)
125
static void screen_yield(void)
148
{
126
{
149
    ipc_call_sync_0_0(fb_info.phone, FB_SCREEN_YIELD);
127
    ipc_call_sync_0_0(fb_info.phone, FB_SCREEN_YIELD);
Line 164... Line 142...
164
    ipc_call_sync_0_0(kbd_phone, KBD_RECLAIM);
142
    ipc_call_sync_0_0(kbd_phone, KBD_RECLAIM);
165
}
143
}
166
 
144
 
167
static void set_style(int style)
145
static void set_style(int style)
168
{
146
{
169
    async_msg_1(fb_info.phone, FB_SET_STYLE, style);
147
    async_msg_1(fb_info.phone, FB_SET_STYLE, style);
170
}
148
}
171
 
149
 
172
static void set_color(int fgcolor, int bgcolor, int flags)
150
static void set_color(int fgcolor, int bgcolor, int flags)
173
{
151
{
174
    async_msg_3(fb_info.phone, FB_SET_COLOR, fgcolor, bgcolor, flags);
152
    async_msg_3(fb_info.phone, FB_SET_COLOR, fgcolor, bgcolor, flags);
Line 183... Line 161...
183
{
161
{
184
    switch (attrs->t) {
162
    switch (attrs->t) {
185
    case at_style:
163
    case at_style:
186
        set_style(attrs->a.s.style);
164
        set_style(attrs->a.s.style);
187
        break;
165
        break;
188
 
-
 
189
    case at_idx:
166
    case at_idx:
190
        set_color(attrs->a.i.fg_color, attrs->a.i.bg_color,
167
        set_color(attrs->a.i.fg_color, attrs->a.i.bg_color,
191
            attrs->a.i.flags);
168
            attrs->a.i.flags);
192
        break;
169
        break;
193
 
-
 
194
    case at_rgb:
170
    case at_rgb:
195
        set_rgb_color(attrs->a.r.fg_color, attrs->a.r.bg_color);
171
        set_rgb_color(attrs->a.r.fg_color, attrs->a.r.bg_color);
196
        break;
172
        break;
197
    }
173
    }
198
}
174
}
199
 
175
 
200
/** Send an area of screenbuffer to the FB driver. */
-
 
201
static void fb_update_area(connection_t *conn, int x, int y, int w, int h)
176
static int ccap_fb_to_con(int ccap_fb, int *ccap_con)
202
{
177
{
203
    int i, j;
-
 
204
    int rc;
-
 
205
    attrs_t *attrs;
-
 
206
    keyfield_t *field;
-
 
207
 
-
 
208
    if (interbuffer) {
178
    switch (ccap_fb) {
209
        for (j = 0; j < h; j++) {
179
    case FB_CCAP_NONE: *ccap_con = CONSOLE_CCAP_NONE; break;
210
            for (i = 0; i < w; i++) {
180
    case FB_CCAP_STYLE: *ccap_con = CONSOLE_CCAP_STYLE; break;
211
                interbuffer[i + j * w] =
-
 
212
                    *get_field_at(&conn->screenbuffer,
181
    case FB_CCAP_INDEXED: *ccap_con = CONSOLE_CCAP_INDEXED; break;
213
                    x + i, y + j);
-
 
214
            }
-
 
215
        }
-
 
216
 
-
 
217
        rc = async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
182
    case FB_CCAP_RGB: *ccap_con = CONSOLE_CCAP_RGB; break;
218
            x, y, w, h);
183
    default: return EINVAL;
219
    } else {
-
 
220
        rc = ENOTSUP;
-
 
221
    }
184
    }
222
 
185
 
223
    if (rc != 0) {
186
    return EOK;
224
        /*
-
 
225
        attrs = &conn->screenbuffer.attrs;
-
 
226
 
187
}
227
        for (j = 0; j < h; j++) {
-
 
228
            for (i = 0; i < w; i++) {
-
 
229
                field = get_field_at(&conn->screenbuffer,
-
 
230
                    x + i, y + j);
-
 
231
                if (!attrs_same(*attrs, field->attrs))
-
 
232
                    set_attrs(&field->attrs);
-
 
233
                attrs = &field->attrs;
-
 
234
 
188
 
-
 
189
/** Send an area of screenbuffer to the FB driver. */
-
 
190
static void fb_update_area(console_t *cons, ipcarg_t x0, ipcarg_t y0, ipcarg_t width, ipcarg_t height)
-
 
191
{
-
 
192
    if (interbuffer) {
-
 
193
        ipcarg_t x;
-
 
194
        ipcarg_t y;
-
 
195
       
-
 
196
        for (y = 0; y < height; y++) {
-
 
197
            for (x = 0; x < width; x++) {
-
 
198
                interbuffer[y * width + x] =
235
                fb_putchar(field->character, y + j, x + i);
199
                    *get_field_at(&cons->scr, x0 + x, y0 + y);
236
            }
200
            }
237
        }*/
201
        }
-
 
202
       
-
 
203
        async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
-
 
204
            x0, y0, width, height);
238
    }
205
    }
239
}
206
}
240
 
207
 
241
/** Flush pending cells to FB. */
208
/** Flush pending cells to FB. */
242
static void fb_pending_flush(void)
209
static void fb_pending_flush(void)
243
{
210
{
244
    screenbuffer_t *scr;
-
 
245
 
-
 
246
    scr = &(connections[active_console].screenbuffer);
-
 
247
 
-
 
248
    if (fb_pending.n > 0) {
211
    if (fb_pending.cnt > 0) {
249
        fb_update_area(&connections[active_console], fb_pending.col,
212
        fb_update_area(active_console, fb_pending.col,
250
            fb_pending.row, fb_pending.n, 1);
213
            fb_pending.row, fb_pending.cnt, 1);
251
        fb_pending.n = 0;
214
        fb_pending.cnt = 0;
252
    }
215
    }
253
}
216
}
254
 
217
 
255
/** Mark a character cell as changed.
218
/** Mark a character cell as changed.
256
 *
219
 *
257
 * This adds the cell to the pending rowspan if possible. Otherwise
220
 * This adds the cell to the pending rowspan if possible. Otherwise
258
 * the old span is flushed first.
221
 * the old span is flushed first.
-
 
222
 *
259
 */
223
 */
260
static void cell_mark_changed(int row, int col)
224
static void cell_mark_changed(size_t col, size_t row)
261
{
225
{
262
    if (fb_pending.n != 0) {
226
    if (fb_pending.cnt != 0) {
263
        if (row != fb_pending.row ||
227
        if ((col != fb_pending.col + fb_pending.cnt)
264
            col != fb_pending.col + fb_pending.n) {
228
            || (row != fb_pending.row)) {
265
            fb_pending_flush();
229
            fb_pending_flush();
266
        }
230
        }
267
    }
231
    }
268
 
232
   
269
    if (fb_pending.n == 0) {
233
    if (fb_pending.cnt == 0) {
270
        fb_pending.row = row;
-
 
271
        fb_pending.col = col;
234
        fb_pending.col = col;
-
 
235
        fb_pending.row = row;
272
    }
236
    }
273
 
237
   
274
    ++fb_pending.n;
238
    fb_pending.cnt++;
275
}
239
}
276
 
240
 
277
 
-
 
278
/** Print a character to the active VC with buffering. */
241
/** Print a character to the active VC with buffering. */
279
static void fb_putchar(wchar_t c, int row, int col)
242
static void fb_putchar(wchar_t c, ipcarg_t col, ipcarg_t row)
280
{
243
{
281
    async_msg_3(fb_info.phone, FB_PUTCHAR, c, row, col);
244
    async_msg_3(fb_info.phone, FB_PUTCHAR, c, col, row);
282
}
245
}
283
 
246
 
284
/** Process a character from the client (TTY emulation). */
247
/** Process a character from the client (TTY emulation). */
285
static void write_char(int console, wchar_t ch)
248
static void write_char(console_t *cons, wchar_t ch)
286
{
249
{
287
    bool flush_cursor = false;
250
    bool flush_cursor = false;
288
    screenbuffer_t *scr = &(connections[console].screenbuffer);
-
 
289
 
251
 
290
    switch (ch) {
252
    switch (ch) {
291
    case '\n':
253
    case '\n':
292
        fb_pending_flush();
254
        fb_pending_flush();
293
        flush_cursor = true;
255
        flush_cursor = true;
294
        scr->position_y++;
256
        cons->scr.position_y++;
295
        scr->position_x = 0;
257
        cons->scr.position_x = 0;
296
        break;
258
        break;
297
    case '\r':
259
    case '\r':
298
        break;
260
        break;
299
    case '\t':
261
    case '\t':
300
        scr->position_x += 8;
262
        cons->scr.position_x += 8;
301
        scr->position_x -= scr->position_x % 8;
263
        cons->scr.position_x -= cons->scr.position_x % 8;
302
        break;
264
        break;
303
    case '\b':
265
    case '\b':
304
        if (scr->position_x == 0)
266
        if (cons->scr.position_x == 0)
305
            break;
267
            break;
306
        scr->position_x--;
268
        cons->scr.position_x--;
307
        if (console == active_console)
269
        if (cons == active_console)
308
            cell_mark_changed(scr->position_y, scr->position_x);
270
            cell_mark_changed(cons->scr.position_x, cons->scr.position_y);
309
        screenbuffer_putchar(scr, ' ');
271
        screenbuffer_putchar(&cons->scr, ' ');
310
        break;
272
        break;
311
    default:   
273
    default:
312
        if (console == active_console)
274
        if (cons == active_console)
313
            cell_mark_changed(scr->position_y, scr->position_x);
275
            cell_mark_changed(cons->scr.position_x, cons->scr.position_y);
314
 
276
       
315
        screenbuffer_putchar(scr, ch);
277
        screenbuffer_putchar(&cons->scr, ch);
316
        scr->position_x++;
278
        cons->scr.position_x++;
317
    }
279
    }
318
 
280
   
319
    if (scr->position_x >= scr->size_x) {
281
    if (cons->scr.position_x >= cons->scr.size_x) {
320
        flush_cursor = true;
282
        flush_cursor = true;
321
        scr->position_y++;
283
        cons->scr.position_y++;
322
    }
284
    }
323
   
285
   
324
    if (scr->position_y >= scr->size_y) {
286
    if (cons->scr.position_y >= cons->scr.size_y) {
325
        fb_pending_flush();
287
        fb_pending_flush();
326
        scr->position_y = scr->size_y - 1;
288
        cons->scr.position_y = cons->scr.size_y - 1;
327
        screenbuffer_clear_line(scr, scr->top_line);
289
        screenbuffer_clear_line(&cons->scr, cons->scr.top_line);
328
        scr->top_line = (scr->top_line + 1) % scr->size_y;
290
        cons->scr.top_line = (cons->scr.top_line + 1) % cons->scr.size_y;
-
 
291
       
329
        if (console == active_console)
292
        if (cons == active_console)
330
            async_msg_1(fb_info.phone, FB_SCROLL, 1);
293
            async_msg_1(fb_info.phone, FB_SCROLL, 1);
331
    }
294
    }
332
 
295
 
333
    scr->position_x = scr->position_x % scr->size_x;
-
 
334
 
-
 
335
    if (console == active_console && flush_cursor)
296
    if (cons == active_console && flush_cursor)
336
        curs_goto(scr->position_y, scr->position_x);
297
        curs_goto(cons->scr.position_x, cons->scr.position_y);
-
 
298
    cons->scr.position_x = cons->scr.position_x % cons->scr.size_x;
337
}
299
}
338
 
300
 
339
/** Switch to new console */
301
/** Switch to new console */
340
static void change_console(int newcons)
302
static void change_console(console_t *cons)
341
{
303
{
342
    connection_t *conn;
-
 
343
    int i, j, rc;
-
 
344
    keyfield_t *field;
-
 
345
    attrs_t *attrs;
-
 
346
   
-
 
347
    if (newcons == active_console)
304
    if (cons == active_console)
348
        return;
305
        return;
349
 
306
   
350
    fb_pending_flush();
307
    fb_pending_flush();
351
 
308
   
352
    if (newcons == KERNEL_CONSOLE) {
309
    if (cons == kernel_console) {
353
        async_serialize_start();
310
        async_serialize_start();
354
        curs_hide_sync();
311
        curs_hide_sync();
355
        gcons_in_kernel();
312
        gcons_in_kernel();
356
        screen_yield();
313
        screen_yield();
357
        kbd_yield();
314
        kbd_yield();
358
        async_serialize_end();
315
        async_serialize_end();
359
 
-
 
360
       
316
       
361
        if (__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE)) {
317
        if (__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE)) {
362
            prev_console = active_console;
318
            prev_console = active_console;
363
            active_console = KERNEL_CONSOLE;
319
            active_console = kernel_console;
364
        } else
320
        } else
365
            newcons = active_console;
321
            cons = active_console;
366
    }
322
    }
367
   
323
   
368
    if (newcons != KERNEL_CONSOLE) {
324
    if (cons != kernel_console) {
-
 
325
        size_t x;
-
 
326
        size_t y;
-
 
327
        int rc = 0;
-
 
328
       
369
        async_serialize_start();
329
        async_serialize_start();
370
       
330
       
371
        if (active_console == KERNEL_CONSOLE) {
331
        if (active_console == kernel_console) {
372
            screen_reclaim();
332
            screen_reclaim();
373
            kbd_reclaim();
333
            kbd_reclaim();
374
            gcons_redraw_console();
334
            gcons_redraw_console();
375
        }
335
        }
376
       
336
       
377
        active_console = newcons;
337
        active_console = cons;
378
        gcons_change_console(newcons);
338
        gcons_change_console(cons->index);
379
        conn = &connections[active_console];
-
 
380
       
339
       
381
        set_attrs(&conn->screenbuffer.attrs);
340
        set_attrs(&cons->scr.attrs);
382
        curs_visibility(false);
341
        curs_visibility(false);
383
        if (interbuffer) {
342
        if (interbuffer) {
384
            for (j = 0; j < conn->screenbuffer.size_y; j++) {
343
            for (y = 0; y < cons->scr.size_y; y++) {
385
                for (i = 0; i < conn->screenbuffer.size_x; i++) {
344
                for (x = 0; x < cons->scr.size_x; x++) {
386
                    unsigned int size_x;
-
 
387
                   
-
 
388
                    size_x = conn->screenbuffer.size_x;
-
 
389
                    interbuffer[j * size_x + i] =
345
                    interbuffer[y * cons->scr.size_x + x] =
390
                        *get_field_at(&conn->screenbuffer, i, j);
346
                        *get_field_at(&cons->scr, x, y);
391
                }
347
                }
392
            }
348
            }
-
 
349
           
393
            /* This call can preempt, but we are already at the end */
350
            /* This call can preempt, but we are already at the end */
394
            rc = async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
351
            rc = async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
395
                0, 0, conn->screenbuffer.size_x,
352
                0, 0, cons->scr.size_x,
396
                conn->screenbuffer.size_y);
353
                cons->scr.size_y);
397
        }
354
        }
398
       
355
       
399
        if ((!interbuffer) || (rc != 0)) {
356
        if ((!interbuffer) || (rc != 0)) {
400
            set_attrs(&conn->screenbuffer.attrs);
357
            set_attrs(&cons->scr.attrs);
401
            clrscr();
358
            screen_clear();
402
            attrs = &conn->screenbuffer.attrs;
-
 
403
           
359
           
404
            for (j = 0; j < conn->screenbuffer.size_y; j++)
360
            for (y = 0; y < cons->scr.size_y; y++)
405
                for (i = 0; i < conn->screenbuffer.size_x; i++) {
361
                for (x = 0; x < cons->scr.size_x; x++) {
406
                    field = get_field_at(&conn->screenbuffer, i, j);
362
                    keyfield_t *field = get_field_at(&cons->scr, x, y);
-
 
363
                   
407
                    if (!attrs_same(*attrs, field->attrs))
364
                    if (!attrs_same(cons->scr.attrs, field->attrs))
408
                        set_attrs(&field->attrs);
365
                        set_attrs(&field->attrs);
-
 
366
                   
409
                    attrs = &field->attrs;
367
                    cons->scr.attrs = field->attrs;
410
                    if ((field->character == ' ') &&
368
                    if ((field->character == ' ') &&
411
                        (attrs_same(field->attrs,
369
                        (attrs_same(field->attrs, cons->scr.attrs)))
412
                        conn->screenbuffer.attrs)))
-
 
413
                        continue;
370
                        continue;
414
 
371
                   
415
                    fb_putchar(field->character, j, i);
372
                    fb_putchar(field->character, x, y);
416
                }
373
                }
417
        }
374
        }
418
       
375
       
419
        curs_goto(conn->screenbuffer.position_y,
376
        curs_goto(cons->scr.position_x, cons->scr.position_y);
420
            conn->screenbuffer.position_x);
-
 
421
        curs_visibility(conn->screenbuffer.is_cursor_visible);
377
        curs_visibility(cons->scr.is_cursor_visible);
422
       
378
       
423
        async_serialize_end();
379
        async_serialize_end();
424
    }
380
    }
425
}
381
}
426
 
382
 
427
/** Handler for keyboard */
383
/** Handler for keyboard */
428
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
384
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
429
{
385
{
-
 
386
    /* Ignore parameters, the connection is already opened */
430
    ipc_callid_t callid;
387
    while (true) {
-
 
388
       
431
    ipc_call_t call;
389
        ipc_call_t call;
-
 
390
        ipc_callid_t callid = async_get_call(&call);
-
 
391
       
432
    int retval;
392
        int retval;
433
    kbd_event_t ev;
393
        console_event_t ev;
434
    connection_t *conn;
-
 
435
    int newcon;
-
 
436
   
394
       
437
    /* Ignore parameters, the connection is alread opened */
-
 
438
    while (1) {
-
 
439
        callid = async_get_call(&call);
-
 
440
        switch (IPC_GET_METHOD(call)) {
395
        switch (IPC_GET_METHOD(call)) {
441
        case IPC_M_PHONE_HUNGUP:
396
        case IPC_M_PHONE_HUNGUP:
442
            /* TODO: Handle hangup */
397
            /* TODO: Handle hangup */
443
            return;
398
            return;
444
        case KBD_MS_LEFT:
-
 
445
            newcon = gcons_mouse_btn(IPC_GET_ARG1(call));
-
 
446
            if (newcon != -1)
-
 
447
                change_console(newcon);
-
 
448
            retval = 0;
-
 
449
            break;
-
 
450
        case KBD_MS_MOVE:
-
 
451
            gcons_mouse_move(IPC_GET_ARG1(call),
-
 
452
                IPC_GET_ARG2(call));
-
 
453
            retval = 0;
-
 
454
            break;
-
 
455
        case KBD_EVENT:
399
        case KBD_EVENT:
456
            /* Got event from keyboard driver. */
400
            /* Got event from keyboard driver. */
457
            retval = 0;
401
            retval = 0;
458
            ev.type = IPC_GET_ARG1(call);
402
            ev.type = IPC_GET_ARG1(call);
459
            ev.key = IPC_GET_ARG2(call);
403
            ev.key = IPC_GET_ARG2(call);
460
            ev.mods = IPC_GET_ARG3(call);
404
            ev.mods = IPC_GET_ARG3(call);
461
            ev.c = IPC_GET_ARG4(call);
405
            ev.c = IPC_GET_ARG4(call);
462
           
406
           
463
            /* switch to another virtual console */
-
 
464
           
-
 
465
            conn = &connections[active_console];
-
 
466
 
-
 
467
            if ((ev.key >= KC_F1) && (ev.key < KC_F1 +
407
            if ((ev.key >= KC_F1) && (ev.key < KC_F1 +
468
                CONSOLE_COUNT) && ((ev.mods & KM_CTRL) == 0)) {
408
                CONSOLE_COUNT) && ((ev.mods & KM_CTRL) == 0)) {
469
                if (ev.key == KC_F12)
409
                if (ev.key == KC_F1 + KERNEL_CONSOLE)
470
                    change_console(KERNEL_CONSOLE);
410
                    change_console(kernel_console);
471
                else
411
                else
472
                    change_console(ev.key - KC_F1);
412
                    change_console(&consoles[ev.key - KC_F1]);
473
                break;
413
                break;
474
            }
414
            }
475
           
415
           
476
            /* if client is awaiting key, send it */
-
 
477
            if (conn->keyrequest_counter > 0) {    
-
 
478
                conn->keyrequest_counter--;
416
            fibril_mutex_lock(&input_mutex);
479
                ipc_answer_4(fifo_pop(conn->keyrequests), EOK,
417
            keybuffer_push(&active_console->keybuffer, &ev);
480
                    ev.type, ev.key, ev.mods, ev.c);
418
            fibril_condvar_broadcast(&input_cv);
481
                break;
-
 
482
            }
-
 
483
 
-
 
484
            keybuffer_push(&conn->keybuffer, &ev);
419
            fibril_mutex_unlock(&input_mutex);
485
            retval = 0;
-
 
486
 
-
 
487
            break;
420
            break;
488
        default:
421
        default:
489
            retval = ENOENT;
422
            retval = ENOENT;
490
        }
423
        }
491
        ipc_answer_0(callid, retval);
424
        ipc_answer_0(callid, retval);
492
    }
425
    }
493
}
426
}
494
 
427
 
495
/** Handle CONSOLE_WRITE call. */
-
 
496
static void cons_write(int consnum, ipc_callid_t rid, ipc_call_t *request)
428
static void cons_write(console_t *cons, ipc_callid_t rid, ipc_call_t *request)
497
{
429
{
498
    ipc_callid_t callid;
430
    ipc_callid_t callid;
499
    size_t size;
431
    size_t size;
500
    wchar_t ch;
-
 
501
    size_t off;
-
 
502
 
-
 
503
    if (!ipc_data_write_receive(&callid, &size)) {
432
    if (!ipc_data_write_receive(&callid, &size)) {
504
        ipc_answer_0(callid, EINVAL);
433
        ipc_answer_0(callid, EINVAL);
505
        ipc_answer_0(rid, EINVAL);
434
        ipc_answer_0(rid, EINVAL);
506
        return;
435
        return;
507
    }
436
    }
508
 
437
   
-
 
438
    char *buf = (char *) malloc(size);
509
    if (size > CWRITE_BUF_SIZE)
439
    if (buf == NULL) {
-
 
440
        ipc_answer_0(callid, ENOMEM);
510
        size = CWRITE_BUF_SIZE;
441
        ipc_answer_0(rid, ENOMEM);
-
 
442
        return;
-
 
443
    }
511
 
444
   
512
    (void) ipc_data_write_finalize(callid, cwrite_buf, size);
445
    (void) ipc_data_write_finalize(callid, buf, size);
513
 
446
   
514
    async_serialize_start();
447
    async_serialize_start();
515
 
448
   
516
    off = 0;
449
    size_t off = 0;
517
    while (off < size) {
450
    while (off < size) {
518
        ch = str_decode(cwrite_buf, &off, size);
451
        wchar_t ch = str_decode(buf, &off, size);
519
        write_char(consnum, ch);
452
        write_char(cons, ch);
520
    }
453
    }
521
 
454
   
522
    async_serialize_end();
455
    async_serialize_end();
523
 
456
   
524
    gcons_notify_char(consnum);
457
    gcons_notify_char(cons->index);
525
    ipc_answer_1(rid, EOK, size);
458
    ipc_answer_1(rid, EOK, size);
-
 
459
   
-
 
460
    free(buf);
-
 
461
}
-
 
462
 
-
 
463
static void cons_read(console_t *cons, ipc_callid_t rid, ipc_call_t *request)
-
 
464
{
-
 
465
    ipc_callid_t callid;
-
 
466
    size_t size;
-
 
467
    if (!ipc_data_read_receive(&callid, &size)) {
-
 
468
        ipc_answer_0(callid, EINVAL);
-
 
469
        ipc_answer_0(rid, EINVAL);
-
 
470
        return;
-
 
471
    }
-
 
472
   
-
 
473
    char *buf = (char *) malloc(size);
-
 
474
    if (buf == NULL) {
-
 
475
        ipc_answer_0(callid, ENOMEM);
-
 
476
        ipc_answer_0(rid, ENOMEM);
-
 
477
        return;
-
 
478
    }
-
 
479
   
-
 
480
    size_t pos = 0;
-
 
481
    console_event_t ev;
-
 
482
    fibril_mutex_lock(&input_mutex);
-
 
483
recheck:
-
 
484
    while ((keybuffer_pop(&cons->keybuffer, &ev)) && (pos < size)) {
-
 
485
        if (ev.type == KEY_PRESS) {
-
 
486
            buf[pos] = ev.c;
-
 
487
            pos++;
-
 
488
        }
-
 
489
    }
-
 
490
   
-
 
491
    if (pos == size) {
-
 
492
        (void) ipc_data_read_finalize(callid, buf, size);
-
 
493
        ipc_answer_1(rid, EOK, size);
-
 
494
        free(buf);
-
 
495
    } else {
-
 
496
        fibril_condvar_wait(&input_cv, &input_mutex);
-
 
497
        goto recheck;
-
 
498
    }
-
 
499
    fibril_mutex_unlock(&input_mutex);
-
 
500
}
-
 
501
 
-
 
502
static void cons_get_event(console_t *cons, ipc_callid_t rid, ipc_call_t *request)
-
 
503
{
-
 
504
    console_event_t ev;
-
 
505
 
-
 
506
    fibril_mutex_lock(&input_mutex);
-
 
507
recheck:
-
 
508
    if (keybuffer_pop(&cons->keybuffer, &ev)) {
-
 
509
        ipc_answer_4(rid, EOK, ev.type, ev.key, ev.mods, ev.c);
-
 
510
    } else {
-
 
511
        fibril_condvar_wait(&input_cv, &input_mutex);
-
 
512
        goto recheck;
-
 
513
    }
-
 
514
    fibril_mutex_unlock(&input_mutex);
526
}
515
}
527
 
516
 
528
/** Default thread for new connections */
517
/** Default thread for new connections */
529
static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
518
static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
530
{
519
{
531
    ipc_callid_t callid;
520
    console_t *cons = NULL;
-
 
521
   
532
    ipc_call_t call;
522
    size_t i;
-
 
523
    for (i = 0; i < CONSOLE_COUNT; i++) {
-
 
524
        if (i == KERNEL_CONSOLE)
533
    int consnum;
525
            continue;
-
 
526
       
534
    ipcarg_t arg1, arg2, arg3, arg4;
527
        if (consoles[i].dev_handle == (dev_handle_t) IPC_GET_ARG1(*icall)) {
535
    connection_t *conn;
528
            cons = &consoles[i];
536
    screenbuffer_t *scr;
529
            break;
-
 
530
        }
-
 
531
    }
537
   
532
   
538
    if ((consnum = find_free_connection()) == -1) {
533
    if (cons == NULL) {
539
        ipc_answer_0(iid, ELIMIT);
534
        ipc_answer_0(iid, ENOENT);
540
        return;
535
        return;
541
    }
536
    }
-
 
537
   
542
    conn = &connections[consnum];
538
    ipc_callid_t callid;
-
 
539
    ipc_call_t call;
543
    conn->used = 1;
540
    ipcarg_t arg1;
-
 
541
    ipcarg_t arg2;
-
 
542
    ipcarg_t arg3;
-
 
543
 
-
 
544
    int cons_ccap;
-
 
545
    int rc;
544
   
546
   
545
    async_serialize_start();
547
    async_serialize_start();
546
    gcons_notify_connect(consnum);
548
    if (cons->refcount == 0)
547
    conn->client_phone = IPC_GET_ARG5(*icall);
-
 
548
    screenbuffer_clear(&conn->screenbuffer);
549
        gcons_notify_connect(cons->index);
549
    if (consnum == active_console)
-
 
-
 
550
   
550
        clrscr();
551
    cons->refcount++;
551
   
552
   
552
    /* Accept the connection */
553
    /* Accept the connection */
553
    ipc_answer_0(iid, EOK);
554
    ipc_answer_0(iid, EOK);
554
   
555
   
555
    while (1) {
556
    while (true) {
556
        async_serialize_end();
557
        async_serialize_end();
557
        callid = async_get_call(&call);
558
        callid = async_get_call(&call);
558
        async_serialize_start();
559
        async_serialize_start();
559
       
560
       
560
        arg1 = 0;
561
        arg1 = 0;
561
        arg2 = 0;
562
        arg2 = 0;
562
        arg3 = 0;
563
        arg3 = 0;
563
        arg4 = 0;
-
 
564
 
564
       
565
        switch (IPC_GET_METHOD(call)) {
565
        switch (IPC_GET_METHOD(call)) {
566
        case IPC_M_PHONE_HUNGUP:
566
        case IPC_M_PHONE_HUNGUP:
567
            gcons_notify_disconnect(consnum);
567
            cons->refcount--;
568
           
-
 
569
            /* Answer all pending requests */
-
 
570
            while (conn->keyrequest_counter > 0) {
568
            if (cons->refcount == 0)
571
                conn->keyrequest_counter--;
569
                gcons_notify_disconnect(cons->index);
572
                ipc_answer_0(fifo_pop(conn->keyrequests),
-
 
573
                    ENOENT);
-
 
574
                break;
-
 
575
            }
-
 
576
            conn->used = 0;
-
 
577
            return;
570
            return;
578
        case CONSOLE_PUTCHAR:
571
        case VFS_OUT_READ:
-
 
572
            async_serialize_end();
579
            write_char(consnum, IPC_GET_ARG1(call));
573
            cons_read(cons, callid, &call);
580
            gcons_notify_char(consnum);
574
            async_serialize_start();
581
            break;
575
            continue;
582
        case CONSOLE_WRITE:
576
        case VFS_OUT_WRITE:
583
            async_serialize_end();
577
            async_serialize_end();
584
            cons_write(consnum, callid, &call);
578
            cons_write(cons, callid, &call);
585
            async_serialize_start();
579
            async_serialize_start();
586
            continue;
580
            continue;
-
 
581
        case VFS_OUT_SYNC:
-
 
582
            fb_pending_flush();
-
 
583
            if (cons == active_console) {
-
 
584
                async_req_0_0(fb_info.phone, FB_FLUSH);
-
 
585
               
-
 
586
                curs_goto(cons->scr.position_x, cons->scr.position_y);
-
 
587
            }
-
 
588
            break;
587
        case CONSOLE_CLEAR:
589
        case CONSOLE_CLEAR:
588
            /* Send message to fb */
590
            /* Send message to fb */
589
            if (consnum == active_console) {
591
            if (cons == active_console)
590
                async_msg_0(fb_info.phone, FB_CLEAR);
592
                async_msg_0(fb_info.phone, FB_CLEAR);
591
            }
-
 
592
           
593
           
593
            screenbuffer_clear(&conn->screenbuffer);
594
            screenbuffer_clear(&cons->scr);
594
           
595
           
595
            break;
596
            break;
596
        case CONSOLE_GOTO:
597
        case CONSOLE_GOTO:
597
            screenbuffer_goto(&conn->screenbuffer,
598
            screenbuffer_goto(&cons->scr,
598
                IPC_GET_ARG2(call), IPC_GET_ARG1(call));
599
                IPC_GET_ARG1(call), IPC_GET_ARG2(call));
599
            if (consnum == active_console)
600
            if (cons == active_console)
600
                curs_goto(IPC_GET_ARG1(call),
601
                curs_goto(IPC_GET_ARG1(call),
601
                    IPC_GET_ARG2(call));
602
                    IPC_GET_ARG2(call));
602
            break;
603
            break;
603
        case CONSOLE_GETSIZE:
604
        case CONSOLE_GET_SIZE:
604
            arg1 = fb_info.rows;
605
            arg1 = fb_info.cols;
605
            arg2 = fb_info.cols;
606
            arg2 = fb_info.rows;
606
            break;
607
            break;
607
        case CONSOLE_FLUSH:
608
        case CONSOLE_GET_COLOR_CAP:
608
            fb_pending_flush();
609
            rc = ccap_fb_to_con(fb_info.color_cap, &cons_ccap);
609
            if (consnum == active_console) {
610
            if (rc != EOK) {
610
                async_req_0_0(fb_info.phone, FB_FLUSH);
611
                ipc_answer_0(callid, rc);
611
 
-
 
612
                scr = &(connections[consnum].screenbuffer);
612
                continue;
613
                curs_goto(scr->position_y, scr->position_x);
-
 
614
            }
613
            }
-
 
614
            arg1 = cons_ccap;
615
            break;
615
            break;
616
        case CONSOLE_SET_STYLE:
616
        case CONSOLE_SET_STYLE:
617
            fb_pending_flush();
617
            fb_pending_flush();
618
            arg1 = IPC_GET_ARG1(call);
618
            arg1 = IPC_GET_ARG1(call);
619
            screenbuffer_set_style(&conn->screenbuffer, arg1);
619
            screenbuffer_set_style(&cons->scr, arg1);
620
            if (consnum == active_console)
620
            if (cons == active_console)
621
                set_style(arg1);
621
                set_style(arg1);
622
            break;
622
            break;
623
        case CONSOLE_SET_COLOR:
623
        case CONSOLE_SET_COLOR:
624
            fb_pending_flush();
624
            fb_pending_flush();
625
            arg1 = IPC_GET_ARG1(call);
625
            arg1 = IPC_GET_ARG1(call);
626
            arg2 = IPC_GET_ARG2(call);
626
            arg2 = IPC_GET_ARG2(call);
627
            arg3 = IPC_GET_ARG3(call);
627
            arg3 = IPC_GET_ARG3(call);
628
            screenbuffer_set_color(&conn->screenbuffer, arg1,
628
            screenbuffer_set_color(&cons->scr, arg1, arg2, arg3);
629
                arg2, arg3);
-
 
630
            if (consnum == active_console)
629
            if (cons == active_console)
631
                set_color(arg1, arg2, arg3);
630
                set_color(arg1, arg2, arg3);
632
            break;
631
            break;
633
        case CONSOLE_SET_RGB_COLOR:
632
        case CONSOLE_SET_RGB_COLOR:
634
            fb_pending_flush();
633
            fb_pending_flush();
635
            arg1 = IPC_GET_ARG1(call);
634
            arg1 = IPC_GET_ARG1(call);
636
            arg2 = IPC_GET_ARG2(call);
635
            arg2 = IPC_GET_ARG2(call);
637
            screenbuffer_set_rgb_color(&conn->screenbuffer, arg1,
636
            screenbuffer_set_rgb_color(&cons->scr, arg1, arg2);
638
                arg2);
-
 
639
            if (consnum == active_console)
637
            if (cons == active_console)
640
                set_rgb_color(arg1, arg2);
638
                set_rgb_color(arg1, arg2);
641
            break;
639
            break;
642
        case CONSOLE_CURSOR_VISIBILITY:
640
        case CONSOLE_CURSOR_VISIBILITY:
643
            fb_pending_flush();
641
            fb_pending_flush();
644
            arg1 = IPC_GET_ARG1(call);
642
            arg1 = IPC_GET_ARG1(call);
645
            conn->screenbuffer.is_cursor_visible = arg1;
643
            cons->scr.is_cursor_visible = arg1;
646
            if (consnum == active_console)
644
            if (cons == active_console)
647
                curs_visibility(arg1);
645
                curs_visibility(arg1);
648
            break;
646
            break;
649
        case CONSOLE_GETKEY:
647
        case CONSOLE_GET_EVENT:
650
            if (keybuffer_empty(&conn->keybuffer)) {
-
 
651
                /* buffer is empty -> store request */
-
 
652
                if (conn->keyrequest_counter <
648
            async_serialize_end();
653
                    MAX_KEYREQUESTS_BUFFERED) {
-
 
654
                    fifo_push(conn->keyrequests, callid);
649
            cons_get_event(cons, callid, &call);
655
                    conn->keyrequest_counter++;
-
 
656
                } else {
-
 
657
                    /*
-
 
658
                     * No key available and too many
-
 
659
                     * requests => fail.
-
 
660
                    */
-
 
661
                    ipc_answer_0(callid, ELIMIT);
650
            async_serialize_start();
662
                }
-
 
663
                continue;
651
            continue;
664
            }
-
 
665
            kbd_event_t ev;
-
 
666
            keybuffer_pop(&conn->keybuffer, &ev);
-
 
667
            arg1 = ev.type;
-
 
668
            arg2 = ev.key;
-
 
669
            arg3 = ev.mods;
-
 
670
            arg4 = ev.c;
-
 
671
            break;
-
 
672
        case CONSOLE_KCON_ENABLE:
652
        case CONSOLE_KCON_ENABLE:
673
            change_console(KERNEL_CONSOLE);
653
            change_console(kernel_console);
674
            break;
654
            break;
675
        }
655
        }
676
        ipc_answer_4(callid, EOK, arg1, arg2, arg3, arg4);
656
        ipc_answer_3(callid, EOK, arg1, arg2, arg3);
677
    }
657
    }
678
}
658
}
679
 
659
 
680
static void interrupt_received(ipc_callid_t callid, ipc_call_t *call)
660
static void interrupt_received(ipc_callid_t callid, ipc_call_t *call)
681
{
661
{
682
    change_console(prev_console);
662
    change_console(prev_console);
683
}
663
}
684
 
664
 
685
int main(int argc, char *argv[])
665
static bool console_init(void)
686
{
666
{
687
    printf(NAME ": HelenOS Console service\n");
-
 
688
   
-
 
689
    ipcarg_t phonehash;
667
    ipcarg_t color_cap;
690
    size_t ib_size;
-
 
691
    int i;
-
 
692
   
-
 
693
    async_set_client_connection(client_connection);
-
 
694
   
668
 
695
    /* Connect to keyboard driver */
669
    /* Connect to keyboard driver */
696
    kbd_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_KEYBOARD, 0, 0);
670
    kbd_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_KEYBOARD, 0, 0);
697
    if (kbd_phone < 0) {
671
    if (kbd_phone < 0) {
698
        printf(NAME ": Failed to connect to keyboard service\n");
672
        printf(NAME ": Failed to connect to keyboard service\n");
699
        return -1;
673
        return false;
700
    }
674
    }
701
   
675
   
-
 
676
    ipcarg_t phonehash;
702
    if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, 0, &phonehash) != 0) {
677
    if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, 0, &phonehash) != 0) {
703
        printf(NAME ": Failed to create callback from keyboard service\n");
678
        printf(NAME ": Failed to create callback from keyboard service\n");
704
        return -1;
679
        return false;
705
    }
680
    }
706
   
681
   
707
    async_new_connection(phonehash, 0, NULL, keyboard_events);
682
    async_new_connection(phonehash, 0, NULL, keyboard_events);
708
   
683
 
709
    /* Connect to framebuffer driver */
684
    /* Connect to framebuffer driver */
710
    fb_info.phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VIDEO, 0, 0);
685
    fb_info.phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VIDEO, 0, 0);
711
    if (fb_info.phone < 0) {
686
    if (fb_info.phone < 0) {
712
        printf(NAME ": Failed to connect to video service\n");
687
        printf(NAME ": Failed to connect to video service\n");
713
        return -1;
688
        return -1;
714
    }
689
    }
715
   
690
   
716
    /* Disable kernel output to the console */
691
    /* Register driver */
717
    __SYSCALL0(SYS_DEBUG_DISABLE_CONSOLE);
692
    int rc = devmap_driver_register(NAME, client_connection);
-
 
693
    if (rc < 0) {
-
 
694
        printf(NAME ": Unable to register driver (%d)\n", rc);
-
 
695
        return false;
-
 
696
    }
718
   
697
   
719
    /* Initialize gcons */
698
    /* Initialize gcons */
720
    gcons_init(fb_info.phone);
699
    gcons_init(fb_info.phone);
721
    /* Synchronize, the gcons can have something in queue */
-
 
722
    async_req_0_0(fb_info.phone, FB_FLUSH);
-
 
723
   
700
   
-
 
701
    /* Synchronize, the gcons could put something in queue */
724
    async_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.rows,
702
    async_req_0_0(fb_info.phone, FB_FLUSH);
725
        &fb_info.cols);
703
    async_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.cols, &fb_info.rows);
726
    set_rgb_color(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
704
    async_req_0_1(fb_info.phone, FB_GET_COLOR_CAP, &color_cap);
727
    clrscr();
705
    fb_info.color_cap = color_cap;
728
   
706
   
729
    /* Init virtual consoles */
-
 
730
    for (i = 0; i < CONSOLE_COUNT; i++) {
-
 
731
        connections[i].used = 0;
-
 
732
        keybuffer_init(&connections[i].keybuffer);
-
 
733
       
-
 
734
        connections[i].keyrequests.head = 0;
-
 
735
        connections[i].keyrequests.tail = 0;
-
 
736
        connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
-
 
737
        connections[i].keyrequest_counter = 0;
-
 
738
       
-
 
739
        if (screenbuffer_init(&connections[i].screenbuffer,
-
 
740
            fb_info.cols, fb_info.rows) == NULL) {
-
 
741
            /* FIXME: handle error */
-
 
742
            return -1;
-
 
743
        }
-
 
744
    }
-
 
745
    connections[KERNEL_CONSOLE].used = 1;
-
 
746
 
-
 
747
    /* Set up shared memory buffer. */
707
    /* Set up shared memory buffer. */
748
    ib_size = sizeof(keyfield_t) * fb_info.cols * fb_info.rows;
708
    size_t ib_size = sizeof(keyfield_t) * fb_info.cols * fb_info.rows;
749
    interbuffer = as_get_mappable_page(ib_size);
709
    interbuffer = as_get_mappable_page(ib_size);
750
 
-
 
751
    fb_pending.n = 0;
-
 
752
 
710
   
753
    if (as_area_create(interbuffer, ib_size, AS_AREA_READ |
711
    if (as_area_create(interbuffer, ib_size, AS_AREA_READ |
754
        AS_AREA_WRITE | AS_AREA_CACHEABLE) != interbuffer) {
712
        AS_AREA_WRITE | AS_AREA_CACHEABLE) != interbuffer)
755
        interbuffer = NULL;
713
        interbuffer = NULL;
756
    }
714
   
757
 
-
 
758
    if (interbuffer) {
715
    if (interbuffer) {
759
        if (ipc_share_out_start(fb_info.phone, interbuffer,
716
        if (ipc_share_out_start(fb_info.phone, interbuffer,
760
            AS_AREA_READ) != EOK) {
717
            AS_AREA_READ) != EOK) {
761
            as_area_destroy(interbuffer);
718
            as_area_destroy(interbuffer);
762
            interbuffer = NULL;
719
            interbuffer = NULL;
763
        }
720
        }
764
    }
721
    }
765
   
722
   
766
    curs_goto(0, 0);
723
    fb_pending.cnt = 0;
767
    curs_visibility(
-
 
768
        connections[active_console].screenbuffer.is_cursor_visible);
-
 
769
   
724
   
770
    /* Register at NS */
725
    /* Inititalize consoles */
-
 
726
    size_t i;
-
 
727
    for (i = 0; i < CONSOLE_COUNT; i++) {
-
 
728
        if (i != KERNEL_CONSOLE) {
-
 
729
            if (screenbuffer_init(&consoles[i].scr,
-
 
730
                fb_info.cols, fb_info.rows) == NULL) {
-
 
731
                printf(NAME ": Unable to allocate screen buffer %u\n", i);
-
 
732
                return false;
-
 
733
            }
-
 
734
            screenbuffer_clear(&consoles[i].scr);
-
 
735
            keybuffer_init(&consoles[i].keybuffer);
-
 
736
            consoles[i].index = i;
-
 
737
            consoles[i].refcount = 0;
-
 
738
           
-
 
739
            char vc[MAX_DEVICE_NAME];
-
 
740
            snprintf(vc, MAX_DEVICE_NAME, "vc%u", i);
-
 
741
           
771
    if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, 0, &phonehash) != 0)
742
            if (devmap_device_register(vc, &consoles[i].dev_handle) != EOK) {
-
 
743
                devmap_hangup_phone(DEVMAP_DRIVER);
-
 
744
                printf(NAME ": Unable to register device %s\n", vc);
772
        return -1;
745
                return false;
-
 
746
            }
-
 
747
        }
-
 
748
    }
-
 
749
   
-
 
750
    /* Disable kernel output to the console */
-
 
751
    __SYSCALL0(SYS_DEBUG_DISABLE_CONSOLE);
-
 
752
   
-
 
753
    /* Initialize the screen */
-
 
754
    async_serialize_start();
-
 
755
    gcons_redraw_console();
-
 
756
    set_rgb_color(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
-
 
757
    screen_clear();
-
 
758
    curs_goto(0, 0);
-
 
759
    curs_visibility(active_console->scr.is_cursor_visible);
-
 
760
    async_serialize_end();
773
   
761
   
774
    /* Receive kernel notifications */
762
    /* Receive kernel notifications */
775
    if (event_subscribe(EVENT_KCONSOLE, 0) != EOK)
763
    if (event_subscribe(EVENT_KCONSOLE, 0) != EOK)
776
        printf(NAME ": Error registering kconsole notifications\n");
764
        printf(NAME ": Error registering kconsole notifications\n");
777
       
765
   
778
    async_set_interrupt_received(interrupt_received);
766
    async_set_interrupt_received(interrupt_received);
779
   
767
   
-
 
768
    return true;
-
 
769
}
-
 
770
 
-
 
771
int main(int argc, char *argv[])
-
 
772
{
780
    // FIXME: avoid connectiong to itself, keep using klog
773
    printf(NAME ": HelenOS Console service\n");
-
 
774
   
-
 
775
    if (!console_init())
-
 
776
        return -1;
-
 
777
   
781
    // printf(NAME ": Accepting connections\n");
778
    printf(NAME ": Accepting connections\n");
782
    async_manager();
779
    async_manager();
783
   
780
   
784
    return 0;
781
    return 0;
785
}
782
}
786
 
783