Subversion Repositories HelenOS

Rev

Rev 4211 | Rev 4232 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1445 cejka 1
/*
2071 jermar 2
 * Copyright (c) 2006 Josef Cejka
1445 cejka 3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
1649 cejka 28
 
1740 jermar 29
/** @addtogroup console
1649 cejka 30
 * @{ 
31
 */
32
/** @file
33
 */
34
 
2618 jermar 35
#include <libc.h>
1451 cejka 36
#include <fb.h>
1445 cejka 37
#include <ipc/ipc.h>
3924 svoboda 38
#include <kbd.h>
3923 svoboda 39
#include <kbd/keycode.h>
1451 cejka 40
#include <ipc/fb.h>
1445 cejka 41
#include <ipc/services.h>
42
#include <errno.h>
1451 cejka 43
#include <key_buffer.h>
3747 svoboda 44
#include <ipc/console.h>
1453 palkovsky 45
#include <unistd.h>
46
#include <async.h>
1481 cejka 47
#include <libadt/fifo.h>
1487 cejka 48
#include <screenbuffer.h>
1867 jermar 49
#include <sys/mman.h>
3084 decky 50
#include <stdio.h>
4226 svoboda 51
#include <string.h>
3761 decky 52
#include <sysinfo.h>
4173 jermar 53
#include <event.h>
1445 cejka 54
 
3767 svoboda 55
#include "console.h"
1522 palkovsky 56
#include "gcons.h"
57
 
1481 cejka 58
#define MAX_KEYREQUESTS_BUFFERED 32
1451 cejka 59
 
3084 decky 60
#define NAME "console"
1445 cejka 61
 
1526 cejka 62
/** Index of currently used virtual console.
63
 */
1512 cejka 64
int active_console = 0;
3761 decky 65
int prev_console = 0;
1453 palkovsky 66
 
4167 svoboda 67
/** Information about framebuffer */
1487 cejka 68
struct {
69
	int phone;		/**< Framebuffer phone */
1506 cejka 70
	ipcarg_t rows;		/**< Framebuffer rows */
71
	ipcarg_t cols;		/**< Framebuffer columns */
1487 cejka 72
} fb_info;
73
 
1451 cejka 74
typedef struct {
1526 cejka 75
	keybuffer_t keybuffer;		/**< Buffer for incoming keys. */
2025 jermar 76
	/** Buffer for unsatisfied request for keys. */
77
	FIFO_CREATE_STATIC(keyrequests, ipc_callid_t,
78
		MAX_KEYREQUESTS_BUFFERED);	
1526 cejka 79
	int keyrequest_counter;		/**< Number of requests in buffer. */
80
	int client_phone;		/**< Phone to connected client. */
2025 jermar 81
	int used;			/**< 1 if this virtual console is
82
					 * connected to some client.*/
83
	screenbuffer_t screenbuffer;	/**< Screenbuffer for saving screen
84
					 * contents and related settings. */
1451 cejka 85
} connection_t;
86
 
2025 jermar 87
static connection_t connections[CONSOLE_COUNT];	/**< Array of data for virtual
88
						 * consoles */
89
static keyfield_t *interbuffer = NULL;		/**< Pointer to memory shared
90
						 * with framebufer used for
91
						 * faster virtual console
2069 jermar 92
						 * switching */
1506 cejka 93
 
4167 svoboda 94
/** Information on row-span yet unsent to FB driver. */
95
struct {
96
	int row;		/**< Row where the span lies. */
97
	int col;		/**< Leftmost column of the span. */
98
	int n;			/**< Width of the span. */
99
} fb_pending;
1506 cejka 100
 
4164 svoboda 101
/** Size of cwrite_buf. */
102
#define CWRITE_BUF_SIZE 256
103
 
104
/** Buffer for receiving data via the CONSOLE_WRITE call from the client. */
105
static char cwrite_buf[CWRITE_BUF_SIZE];
106
 
4211 svoboda 107
static void fb_putchar(wchar_t c, int row, int col);
4164 svoboda 108
 
4167 svoboda 109
 
1526 cejka 110
/** Find unused virtual console.
111
 *
112
 */
1574 palkovsky 113
static int find_free_connection(void) 
1451 cejka 114
{
2069 jermar 115
	int i;
1451 cejka 116
 
2069 jermar 117
	for (i = 0; i < CONSOLE_COUNT; i++) {
1574 palkovsky 118
		if (!connections[i].used)
1451 cejka 119
			return i;
120
	}
1574 palkovsky 121
	return -1;
1451 cejka 122
}
123
 
1552 palkovsky 124
static void clrscr(void)
125
{
2621 jermar 126
	async_msg_0(fb_info.phone, FB_CLEAR);
1552 palkovsky 127
}
128
 
3735 decky 129
static void curs_visibility(bool visible)
1552 palkovsky 130
{
3735 decky 131
	async_msg_1(fb_info.phone, FB_CURSOR_VISIBILITY, visible); 
1552 palkovsky 132
}
133
 
3735 decky 134
static void curs_hide_sync(void)
135
{
136
	ipc_call_sync_1_0(fb_info.phone, FB_CURSOR_VISIBILITY, false); 
137
}
138
 
1552 palkovsky 139
static void curs_goto(int row, int col)
140
{
1610 palkovsky 141
	async_msg_2(fb_info.phone, FB_CURSOR_GOTO, row, col); 
1552 palkovsky 142
}
143
 
3767 svoboda 144
static void set_style(int style)
1552 palkovsky 145
{
3767 svoboda 146
	async_msg_1(fb_info.phone, FB_SET_STYLE, style); 
1552 palkovsky 147
}
148
 
3767 svoboda 149
static void set_color(int fgcolor, int bgcolor, int flags)
1552 palkovsky 150
{
3767 svoboda 151
	async_msg_3(fb_info.phone, FB_SET_COLOR, fgcolor, bgcolor, flags);
1552 palkovsky 152
}
153
 
3767 svoboda 154
static void set_rgb_color(int fgcolor, int bgcolor)
155
{
156
	async_msg_2(fb_info.phone, FB_SET_RGB_COLOR, fgcolor, bgcolor); 
157
}
158
 
159
static void set_attrs(attrs_t *attrs)
160
{
161
	switch (attrs->t) {
162
	case at_style:
163
		set_style(attrs->a.s.style);
164
		break;
165
 
166
	case at_idx:
167
		set_color(attrs->a.i.fg_color, attrs->a.i.bg_color,
168
		    attrs->a.i.flags);
169
		break;
170
 
171
	case at_rgb:
172
		set_rgb_color(attrs->a.r.fg_color, attrs->a.r.bg_color);
173
		break;
174
	}
175
}
176
 
4167 svoboda 177
/** Send an area of screenbuffer to the FB driver. */
178
static void fb_update_area(connection_t *conn, int x, int y, int w, int h)
4164 svoboda 179
{
4167 svoboda 180
	int i, j;
181
	int rc;
182
	attrs_t *attrs;
183
	keyfield_t *field;
4164 svoboda 184
 
4167 svoboda 185
	if (interbuffer) {
186
		for (j = 0; j < h; j++) {
187
			for (i = 0; i < w; i++) {
188
				interbuffer[i + j * w] =
189
				    *get_field_at(&conn->screenbuffer,
190
					x + i, y + j);
191
			}
192
		}
4164 svoboda 193
 
4167 svoboda 194
		rc = async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
195
		    x, y, w, h);
196
	} else {
197
		rc = ENOTSUP;
4164 svoboda 198
	}
199
 
4167 svoboda 200
	if (rc != 0) {
201
		attrs = &conn->screenbuffer.attrs;
4164 svoboda 202
 
4167 svoboda 203
		for (j = 0; j < h; j++) {
204
			for (i = 0; i < w; i++) {
205
				field = get_field_at(&conn->screenbuffer,
206
				    x + i, y + j);
207
				if (!attrs_same(*attrs, field->attrs))
208
					set_attrs(&field->attrs);
209
				attrs = &field->attrs;
210
 
211
				fb_putchar(field->character, y + j, x + i);
212
			}
213
		}
214
	}
4164 svoboda 215
}
216
 
4167 svoboda 217
/** Flush pending cells to FB. */
218
static void fb_pending_flush(void)
4164 svoboda 219
{
220
	screenbuffer_t *scr;
221
 
4167 svoboda 222
	scr = &(connections[active_console].screenbuffer);
4164 svoboda 223
 
4167 svoboda 224
	if (fb_pending.n > 0) {
225
		fb_update_area(&connections[active_console], fb_pending.col,
226
		    fb_pending.row, fb_pending.n, 1);
227
		fb_pending.n = 0;
4164 svoboda 228
	}
229
}
230
 
4167 svoboda 231
/** Mark a character cell as changed.
232
 *
233
 * This adds the cell to the pending rowspan if possible. Otherwise
234
 * the old span is flushed first.
235
 */
236
static void cell_mark_changed(int row, int col)
1552 palkovsky 237
{
4167 svoboda 238
	if (fb_pending.n != 0) {
239
		if (row != fb_pending.row ||
240
		    col != fb_pending.col + fb_pending.n) {
241
			fb_pending_flush();
242
		}
243
	}
4164 svoboda 244
 
4167 svoboda 245
	if (fb_pending.n == 0) {
246
		fb_pending.row = row;
247
		fb_pending.col = col;
4164 svoboda 248
	}
249
 
4167 svoboda 250
	++fb_pending.n;
1552 palkovsky 251
}
252
 
4167 svoboda 253
 
254
/** Print a character to the active VC with buffering. */
4211 svoboda 255
static void fb_putchar(wchar_t c, int row, int col)
4167 svoboda 256
{
257
	async_msg_3(fb_info.phone, FB_PUTCHAR, c, row, col);
258
}
259
 
260
/** Process a character from the client (TTY emulation). */
4211 svoboda 261
static void write_char(int console, wchar_t ch)
1497 cejka 262
{
4164 svoboda 263
	bool flush_cursor = false;
1497 cejka 264
	screenbuffer_t *scr = &(connections[console].screenbuffer);
4164 svoboda 265
 
4211 svoboda 266
	switch (ch) {
2025 jermar 267
	case '\n':
4167 svoboda 268
		fb_pending_flush();
4164 svoboda 269
		flush_cursor = true;
2069 jermar 270
		scr->position_y++;
271
		scr->position_x = 0;
2025 jermar 272
		break;
273
	case '\r':
274
		break;
275
	case '\t':
276
		scr->position_x += 8;
277
		scr->position_x -= scr->position_x % 8; 
278
		break;
279
	case '\b':
280
		if (scr->position_x == 0) 
1497 cejka 281
			break;
2025 jermar 282
		scr->position_x--;
283
		if (console == active_console)
4167 svoboda 284
			cell_mark_changed(scr->position_y, scr->position_x);
2025 jermar 285
		screenbuffer_putchar(scr, ' ');
286
		break;
287
	default:	
288
		if (console == active_console)
4167 svoboda 289
			cell_mark_changed(scr->position_y, scr->position_x);
1497 cejka 290
 
4211 svoboda 291
		screenbuffer_putchar(scr, ch);
2025 jermar 292
		scr->position_x++;
1497 cejka 293
	}
4164 svoboda 294
 
295
	if (scr->position_x >= scr->size_x) {
296
		flush_cursor = true;
297
		scr->position_y++;
298
	}
1497 cejka 299
 
300
	if (scr->position_y >= scr->size_y) {
4167 svoboda 301
		fb_pending_flush();
1497 cejka 302
		scr->position_y = scr->size_y - 1;
1674 palkovsky 303
		screenbuffer_clear_line(scr, scr->top_line);
2069 jermar 304
		scr->top_line = (scr->top_line + 1) % scr->size_y;
1518 palkovsky 305
		if (console == active_console)
2621 jermar 306
			async_msg_1(fb_info.phone, FB_SCROLL, 1);
1497 cejka 307
	}
4164 svoboda 308
 
1497 cejka 309
	scr->position_x = scr->position_x % scr->size_x;
4164 svoboda 310
 
311
	if (console == active_console && flush_cursor)
1552 palkovsky 312
		curs_goto(scr->position_y, scr->position_x);
1497 cejka 313
}
314
 
1552 palkovsky 315
/** Switch to new console */
316
static void change_console(int newcons)
317
{
318
	connection_t *conn;
1673 palkovsky 319
	int i, j, rc;
1578 cejka 320
	keyfield_t *field;
3767 svoboda 321
	attrs_t *attrs;
3707 decky 322
 
1552 palkovsky 323
	if (newcons == active_console)
324
		return;
4164 svoboda 325
 
4167 svoboda 326
	fb_pending_flush();
4164 svoboda 327
 
1555 palkovsky 328
	if (newcons == KERNEL_CONSOLE) {
3707 decky 329
		async_serialize_start();
3735 decky 330
		curs_hide_sync();
3707 decky 331
		gcons_in_kernel();
332
		async_serialize_end();
333
 
3761 decky 334
		if (__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE)) {
335
			prev_console = active_console;
3707 decky 336
			active_console = KERNEL_CONSOLE;
3761 decky 337
		} else
3735 decky 338
			newcons = active_console;
3707 decky 339
	}
340
 
341
	if (newcons != KERNEL_CONSOLE) {
342
		async_serialize_start();
343
 
1555 palkovsky 344
		if (active_console == KERNEL_CONSOLE)
3707 decky 345
			gcons_redraw_console();
346
 
347
		active_console = newcons;
348
		gcons_change_console(newcons);
349
		conn = &connections[active_console];
350
 
3767 svoboda 351
		set_attrs(&conn->screenbuffer.attrs);
3735 decky 352
		curs_visibility(false);
3707 decky 353
		if (interbuffer) {
4167 svoboda 354
			for (j = 0; j < conn->screenbuffer.size_y; j++) {
355
				for (i = 0; i < conn->screenbuffer.size_x; i++) {
3707 decky 356
					unsigned int size_x;
357
 
358
					size_x = conn->screenbuffer.size_x; 
4167 svoboda 359
					interbuffer[j * size_x + i] =
3707 decky 360
					    *get_field_at(&conn->screenbuffer, i, j);
361
				}
4167 svoboda 362
			}
3707 decky 363
			/* This call can preempt, but we are already at the end */
4167 svoboda 364
			rc = async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
365
			    0, 0, conn->screenbuffer.size_x,
366
			    conn->screenbuffer.size_y);
3707 decky 367
		}
368
 
369
		if ((!interbuffer) || (rc != 0)) {
3767 svoboda 370
			set_attrs(&conn->screenbuffer.attrs);
1552 palkovsky 371
			clrscr();
3767 svoboda 372
			attrs = &conn->screenbuffer.attrs;
3707 decky 373
 
3761 decky 374
			for (j = 0; j < conn->screenbuffer.size_y; j++)
3707 decky 375
				for (i = 0; i < conn->screenbuffer.size_x; i++) {
376
					field = get_field_at(&conn->screenbuffer, i, j);
3767 svoboda 377
					if (!attrs_same(*attrs, field->attrs))
378
						set_attrs(&field->attrs);
379
					attrs = &field->attrs;
3707 decky 380
					if ((field->character == ' ') &&
3767 svoboda 381
					    (attrs_same(field->attrs,
382
					    conn->screenbuffer.attrs)))
3707 decky 383
						continue;
3767 svoboda 384
 
4167 svoboda 385
					fb_putchar(field->character, j, i);
3707 decky 386
				}
1552 palkovsky 387
		}
3707 decky 388
 
389
		curs_goto(conn->screenbuffer.position_y,
390
		    conn->screenbuffer.position_x);
391
		curs_visibility(conn->screenbuffer.is_cursor_visible);
392
 
1717 palkovsky 393
		async_serialize_end();
1552 palkovsky 394
	}
395
}
396
 
1526 cejka 397
/** Handler for keyboard */
1453 palkovsky 398
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
1445 cejka 399
{
1453 palkovsky 400
	ipc_callid_t callid;
1445 cejka 401
	ipc_call_t call;
1453 palkovsky 402
	int retval;
3905 svoboda 403
	kbd_event_t ev;
1489 palkovsky 404
	connection_t *conn;
1717 palkovsky 405
	int newcon;
1506 cejka 406
 
1453 palkovsky 407
	/* Ignore parameters, the connection is alread opened */
408
	while (1) {
409
		callid = async_get_call(&call);
410
		switch (IPC_GET_METHOD(call)) {
411
		case IPC_M_PHONE_HUNGUP:
412
			/* TODO: Handle hangup */
413
			return;
1717 palkovsky 414
		case KBD_MS_LEFT:
415
			newcon = gcons_mouse_btn(IPC_GET_ARG1(call));
416
			if (newcon != -1)
417
				change_console(newcon);
1721 palkovsky 418
			retval = 0;
1717 palkovsky 419
			break;
1707 palkovsky 420
		case KBD_MS_MOVE:
2025 jermar 421
			gcons_mouse_move(IPC_GET_ARG1(call),
2539 jermar 422
			    IPC_GET_ARG2(call));
1721 palkovsky 423
			retval = 0;
1707 palkovsky 424
			break;
3905 svoboda 425
		case KBD_EVENT:
426
			/* Got event from keyboard driver. */
1453 palkovsky 427
			retval = 0;
3905 svoboda 428
			ev.type = IPC_GET_ARG1(call);
429
			ev.key = IPC_GET_ARG2(call);
430
			ev.mods = IPC_GET_ARG3(call);
431
			ev.c = IPC_GET_ARG4(call);
432
 
1453 palkovsky 433
			/* switch to another virtual console */
1481 cejka 434
 
1489 palkovsky 435
			conn = &connections[active_console];
3905 svoboda 436
 
3923 svoboda 437
			if ((ev.key >= KC_F1) && (ev.key < KC_F1 +
3905 svoboda 438
			    CONSOLE_COUNT)) {
3923 svoboda 439
				if (ev.key == KC_F12)
1555 palkovsky 440
					change_console(KERNEL_CONSOLE);
1552 palkovsky 441
				else
3923 svoboda 442
					change_console(ev.key - KC_F1);
1453 palkovsky 443
				break;
444
			}
1481 cejka 445
 
446
			/* if client is awaiting key, send it */
1489 palkovsky 447
			if (conn->keyrequest_counter > 0) {		
448
				conn->keyrequest_counter--;
3905 svoboda 449
				ipc_answer_4(fifo_pop(conn->keyrequests), EOK,
450
				    ev.type, ev.key, ev.mods, ev.c);
1481 cejka 451
				break;
452
			}
3905 svoboda 453
 
454
			keybuffer_push(&conn->keybuffer, &ev);
1721 palkovsky 455
			retval = 0;
3905 svoboda 456
 
1453 palkovsky 457
			break;
458
		default:
1459 palkovsky 459
			retval = ENOENT;
1610 palkovsky 460
		}
2619 jermar 461
		ipc_answer_0(callid, retval);
1453 palkovsky 462
	}
463
}
464
 
4164 svoboda 465
/** Handle CONSOLE_WRITE call. */
466
static void cons_write(int consnum, ipc_callid_t rid, ipc_call_t *request)
467
{
468
	ipc_callid_t callid;
4226 svoboda 469
	size_t size;
470
	wchar_t ch;
471
	size_t off;
4164 svoboda 472
 
4226 svoboda 473
	if (!ipc_data_write_receive(&callid, &size)) {
4164 svoboda 474
		ipc_answer_0(callid, EINVAL);
475
		ipc_answer_0(rid, EINVAL);
476
	}
477
 
4226 svoboda 478
	if (size > CWRITE_BUF_SIZE)
479
		size = CWRITE_BUF_SIZE;
4164 svoboda 480
 
4226 svoboda 481
	(void) ipc_data_write_finalize(callid, cwrite_buf, size);
4164 svoboda 482
 
4226 svoboda 483
	off = 0;
484
	while (off < size) {
485
		ch = str_decode(cwrite_buf, &off, size);
486
		write_char(consnum, ch);
4164 svoboda 487
	}
488
 
489
	gcons_notify_char(consnum);
4226 svoboda 490
	ipc_answer_1(rid, EOK, size);
4164 svoboda 491
}
492
 
1453 palkovsky 493
/** Default thread for new connections */
1518 palkovsky 494
static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
1453 palkovsky 495
{
1445 cejka 496
	ipc_callid_t callid;
1453 palkovsky 497
	ipc_call_t call;
498
	int consnum;
3905 svoboda 499
	ipcarg_t arg1, arg2, arg3, arg4;
1592 palkovsky 500
	connection_t *conn;
4167 svoboda 501
	screenbuffer_t *scr;
3761 decky 502
 
1574 palkovsky 503
	if ((consnum = find_free_connection()) == -1) {
2619 jermar 504
		ipc_answer_0(iid, ELIMIT);
1453 palkovsky 505
		return;
506
	}
1592 palkovsky 507
	conn = &connections[consnum];
1610 palkovsky 508
	conn->used = 1;
1555 palkovsky 509
 
1610 palkovsky 510
	async_serialize_start();
1555 palkovsky 511
	gcons_notify_connect(consnum);
2637 cejka 512
	conn->client_phone = IPC_GET_ARG5(*icall);
1592 palkovsky 513
	screenbuffer_clear(&conn->screenbuffer);
1497 cejka 514
 
1453 palkovsky 515
	/* Accept the connection */
2619 jermar 516
	ipc_answer_0(iid, EOK);
3761 decky 517
 
1453 palkovsky 518
	while (1) {
1610 palkovsky 519
		async_serialize_end();
1453 palkovsky 520
		callid = async_get_call(&call);
1610 palkovsky 521
		async_serialize_start();
3761 decky 522
 
2566 jermar 523
		arg1 = 0;
524
		arg2 = 0;
3905 svoboda 525
		arg3 = 0;
526
		arg4 = 0;
527
 
1453 palkovsky 528
		switch (IPC_GET_METHOD(call)) {
529
		case IPC_M_PHONE_HUNGUP:
1592 palkovsky 530
			gcons_notify_disconnect(consnum);
1610 palkovsky 531
 
1592 palkovsky 532
			/* Answer all pending requests */
3761 decky 533
			while (conn->keyrequest_counter > 0) {
1592 palkovsky 534
				conn->keyrequest_counter--;
2619 jermar 535
				ipc_answer_0(fifo_pop(conn->keyrequests),
536
				    ENOENT);
1592 palkovsky 537
				break;
538
			}
1616 palkovsky 539
			conn->used = 0;
1453 palkovsky 540
			return;
541
		case CONSOLE_PUTCHAR:
1497 cejka 542
			write_char(consnum, IPC_GET_ARG1(call));
1528 palkovsky 543
			gcons_notify_char(consnum);
1453 palkovsky 544
			break;
4164 svoboda 545
		case CONSOLE_WRITE:
546
			cons_write(consnum, callid, &call);
547
			continue;
1476 cejka 548
		case CONSOLE_CLEAR:
1487 cejka 549
			/* Send message to fb */
550
			if (consnum == active_console) {
2621 jermar 551
				async_msg_0(fb_info.phone, FB_CLEAR); 
1487 cejka 552
			}
553
 
1592 palkovsky 554
			screenbuffer_clear(&conn->screenbuffer);
1487 cejka 555
 
1476 cejka 556
			break;
557
		case CONSOLE_GOTO:
2025 jermar 558
			screenbuffer_goto(&conn->screenbuffer,
2539 jermar 559
			    IPC_GET_ARG2(call), IPC_GET_ARG1(call));
1567 palkovsky 560
			if (consnum == active_console)
2025 jermar 561
				curs_goto(IPC_GET_ARG1(call),
2539 jermar 562
				    IPC_GET_ARG2(call));
1476 cejka 563
			break;
1521 cejka 564
		case CONSOLE_GETSIZE:
1528 palkovsky 565
			arg1 = fb_info.rows;
566
			arg2 = fb_info.cols;
1521 cejka 567
			break;
1523 cejka 568
		case CONSOLE_FLUSH:
4167 svoboda 569
			fb_pending_flush();
570
			if (consnum == active_console) {
2621 jermar 571
				async_req_0_0(fb_info.phone, FB_FLUSH);
4167 svoboda 572
 
573
				scr = &(connections[consnum].screenbuffer);
574
				curs_goto(scr->position_y, scr->position_x);
575
			}
1523 cejka 576
			break;
1525 cejka 577
		case CONSOLE_SET_STYLE:
4167 svoboda 578
			fb_pending_flush();
1525 cejka 579
			arg1 = IPC_GET_ARG1(call);
3767 svoboda 580
			screenbuffer_set_style(&conn->screenbuffer, arg1);
581
			if (consnum == active_console)
582
				set_style(arg1);
583
			break;
584
		case CONSOLE_SET_COLOR:
4167 svoboda 585
			fb_pending_flush();
3767 svoboda 586
			arg1 = IPC_GET_ARG1(call);
1525 cejka 587
			arg2 = IPC_GET_ARG2(call);
3767 svoboda 588
			arg3 = IPC_GET_ARG3(call);
589
			screenbuffer_set_color(&conn->screenbuffer, arg1,
590
			    arg2, arg3);
591
			if (consnum == active_console)
592
				set_color(arg1, arg2, arg3);
593
			break;
594
		case CONSOLE_SET_RGB_COLOR:
4167 svoboda 595
			fb_pending_flush();
3767 svoboda 596
			arg1 = IPC_GET_ARG1(call);
597
			arg2 = IPC_GET_ARG2(call);
598
			screenbuffer_set_rgb_color(&conn->screenbuffer, arg1,
2539 jermar 599
			    arg2);
1525 cejka 600
			if (consnum == active_console)
3767 svoboda 601
				set_rgb_color(arg1, arg2);
1525 cejka 602
			break;
1575 cejka 603
		case CONSOLE_CURSOR_VISIBILITY:
4167 svoboda 604
			fb_pending_flush();
1575 cejka 605
			arg1 = IPC_GET_ARG1(call);
1592 palkovsky 606
			conn->screenbuffer.is_cursor_visible = arg1;
1575 cejka 607
			if (consnum == active_console)
608
				curs_visibility(arg1);
609
			break;
3905 svoboda 610
		case CONSOLE_GETKEY:
1592 palkovsky 611
			if (keybuffer_empty(&conn->keybuffer)) {
1481 cejka 612
				/* buffer is empty -> store request */
2025 jermar 613
				if (conn->keyrequest_counter <
614
					MAX_KEYREQUESTS_BUFFERED) {	
1592 palkovsky 615
					fifo_push(conn->keyrequests, callid);
616
					conn->keyrequest_counter++;
1481 cejka 617
				} else {
2025 jermar 618
					/*
619
					 * No key available and too many
620
					 * requests => fail.
621
					*/
2619 jermar 622
					ipc_answer_0(callid, ELIMIT);
1481 cejka 623
				}
624
				continue;
2025 jermar 625
			}
3905 svoboda 626
			kbd_event_t ev;
627
			keybuffer_pop(&conn->keybuffer, &ev);
628
			arg1 = ev.type;
629
			arg2 = ev.key;
630
			arg3 = ev.mods;
631
			arg4 = ev.c;
1453 palkovsky 632
			break;
4168 svoboda 633
		case CONSOLE_KCON_ENABLE:
634
			change_console(KERNEL_CONSOLE);
635
			break;
1453 palkovsky 636
		}
3905 svoboda 637
		ipc_answer_4(callid, EOK, arg1, arg2, arg3, arg4);
1453 palkovsky 638
	}
639
}
640
 
3761 decky 641
static void interrupt_received(ipc_callid_t callid, ipc_call_t *call)
642
{
643
	change_console(prev_console);
644
}
645
 
1453 palkovsky 646
int main(int argc, char *argv[])
647
{
3084 decky 648
	printf(NAME ": HelenOS Console service\n");
649
 
1453 palkovsky 650
	ipcarg_t phonehash;
1721 palkovsky 651
	int kbd_phone;
3791 svoboda 652
	size_t ib_size;
1451 cejka 653
	int i;
3761 decky 654
 
1490 palkovsky 655
	async_set_client_connection(client_connection);
1445 cejka 656
 
657
	/* Connect to keyboard driver */
4004 decky 658
	kbd_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_KEYBOARD, 0, 0);
659
	if (kbd_phone < 0) {
660
		printf(NAME ": Failed to connect to keyboard service\n");
661
		return -1;
2025 jermar 662
	}
1445 cejka 663
 
4004 decky 664
	if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, 0, &phonehash) != 0) {
665
		printf(NAME ": Failed to create callback from keyboard service\n");
1445 cejka 666
		return -1;
4004 decky 667
	}
668
 
1689 palkovsky 669
	async_new_connection(phonehash, 0, NULL, keyboard_events);
670
 
1445 cejka 671
	/* Connect to framebuffer driver */
4004 decky 672
	fb_info.phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VIDEO, 0, 0);
673
	if (fb_info.phone < 0) {
674
		printf(NAME ": Failed to connect to video service\n");
675
		return -1;
1487 cejka 676
	}
1552 palkovsky 677
 
3844 decky 678
	/* Disable kernel output to the console */
679
	__SYSCALL0(SYS_DEBUG_DISABLE_CONSOLE);
680
 
1522 palkovsky 681
	/* Initialize gcons */
682
	gcons_init(fb_info.phone);
683
	/* Synchronize, the gcons can have something in queue */
2621 jermar 684
	async_req_0_0(fb_info.phone, FB_FLUSH);
1487 cejka 685
 
2621 jermar 686
	async_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.rows,
2539 jermar 687
	    &fb_info.cols); 
3767 svoboda 688
	set_rgb_color(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
1552 palkovsky 689
	clrscr();
1487 cejka 690
 
691
	/* Init virtual consoles */
1451 cejka 692
	for (i = 0; i < CONSOLE_COUNT; i++) {
693
		connections[i].used = 0;
2539 jermar 694
		keybuffer_init(&connections[i].keybuffer);
1481 cejka 695
 
2539 jermar 696
		connections[i].keyrequests.head = 0;
697
		connections[i].keyrequests.tail = 0;
1481 cejka 698
		connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
699
		connections[i].keyrequest_counter = 0;
1487 cejka 700
 
2539 jermar 701
		if (screenbuffer_init(&connections[i].screenbuffer,
702
		    fb_info.cols, fb_info.rows) == NULL) {
2069 jermar 703
			/* FIXME: handle error */
1487 cejka 704
			return -1;
705
		}
1451 cejka 706
	}
1574 palkovsky 707
	connections[KERNEL_CONSOLE].used = 1;
3791 svoboda 708
 
709
	/* Set up shared memory buffer. */
710
	ib_size = sizeof(keyfield_t) * fb_info.cols * fb_info.rows;
711
	interbuffer = as_get_mappable_page(ib_size);
712
 
4167 svoboda 713
	fb_pending.n = 0;
714
 
3791 svoboda 715
	if (as_area_create(interbuffer, ib_size, AS_AREA_READ |
716
	    AS_AREA_WRITE | AS_AREA_CACHEABLE) != interbuffer) {
717
		interbuffer = NULL;
718
	}
719
 
720
	if (interbuffer) {
2678 jermar 721
		if (ipc_share_out_start(fb_info.phone, interbuffer,
2677 jermar 722
		    AS_AREA_READ) != EOK) {
3791 svoboda 723
			as_area_destroy(interbuffer);
1512 cejka 724
			interbuffer = NULL;
725
		}
726
	}
3761 decky 727
 
2069 jermar 728
	curs_goto(0, 0);
2539 jermar 729
	curs_visibility(
730
	    connections[active_console].screenbuffer.is_cursor_visible);
3761 decky 731
 
1518 palkovsky 732
	/* Register at NS */
3084 decky 733
	if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, 0, &phonehash) != 0)
1445 cejka 734
		return -1;
735
 
3761 decky 736
	/* Receive kernel notifications */
4173 jermar 737
	if (event_subscribe(EVENT_KCONSOLE, 0) != EOK)
738
		printf(NAME ": Error registering kconsole notifications\n");
739
 
740
	async_set_interrupt_received(interrupt_received);
3761 decky 741
 
3084 decky 742
	// FIXME: avoid connectiong to itself, keep using klog
743
	// printf(NAME ": Accepting connections\n");
1453 palkovsky 744
	async_manager();
3761 decky 745
 
746
	return 0;
1445 cejka 747
}
3844 decky 748
 
1649 cejka 749
/** @}
750
 */