Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
1363 vana 1
/*
2
 * Copyright (C) 2006 Jakub Vana
1404 palkovsky 3
 * Copyright (C) 2006 Ondrej Palkovsky
1363 vana 4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
15
 * - The name of the author may not be used to endorse or promote products
16
 *   derived from this software without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29
 
1740 jermar 30
/**
31
 * @defgroup fb Graphical framebuffer
32
 * @brief	HelenOS graphical framebuffer.
1649 cejka 33
 * @ingroup fbs
34
 * @{
35
 */ 
1888 jermar 36
 
1649 cejka 37
/** @file
38
 */
39
 
1430 jermar 40
#include <stdlib.h>
41
#include <unistd.h>
42
#include <string.h>
1363 vana 43
#include <ddi.h>
44
#include <sysinfo.h>
45
#include <align.h>
46
#include <as.h>
47
#include <ipc/fb.h>
48
#include <ipc/ipc.h>
1430 jermar 49
#include <ipc/ns.h>
1363 vana 50
#include <ipc/services.h>
51
#include <kernel/errno.h>
1993 decky 52
#include <kernel/genarch/fb/visuals.h>
1392 palkovsky 53
#include <async.h>
1993 decky 54
#include <bool.h>
1505 palkovsky 55
 
1404 palkovsky 56
#include "font-8x16.h"
1363 vana 57
#include "fb.h"
1505 palkovsky 58
#include "main.h"
59
#include "../console/screenbuffer.h"
1547 palkovsky 60
#include "ppm.h"
1363 vana 61
 
1711 palkovsky 62
#include "pointer.xbm"
63
#include "pointer_mask.xbm"
64
 
1630 palkovsky 65
#define DEFAULT_BGCOLOR                0xf0f0f0
66
#define DEFAULT_FGCOLOR                0x0
1363 vana 67
 
68
/***************************************************************/
69
/* Pixel specific fuctions */
70
 
1555 palkovsky 71
typedef void (*conv2scr_fn_t)(void *, int);
72
typedef int (*conv2rgb_fn_t)(void *);
1363 vana 73
 
1485 palkovsky 74
struct {
1875 jermar 75
	uint8_t *fbaddress;
1363 vana 76
 
1875 jermar 77
	unsigned int xres;
78
	unsigned int yres;
79
	unsigned int scanline;
80
	unsigned int pixelbytes;
81
	unsigned int invert_colors;
1363 vana 82
 
1555 palkovsky 83
	conv2scr_fn_t rgb2scr;
84
	conv2rgb_fn_t scr2rgb;
1485 palkovsky 85
} screen;
1363 vana 86
 
1485 palkovsky 87
typedef struct {
88
	int initialized;
89
	unsigned int x, y;
90
	unsigned int width, height;
1363 vana 91
 
1485 palkovsky 92
	/* Text support in window */
93
	unsigned int rows, cols;
94
	/* Style for text printing */
1509 palkovsky 95
	style_t style;
1485 palkovsky 96
	/* Auto-cursor position */
1486 palkovsky 97
	int cursor_active, cur_col, cur_row;
1500 palkovsky 98
	int cursor_shown;
1672 palkovsky 99
	/* Double buffering */
1781 jermar 100
	uint8_t *dbdata;
1672 palkovsky 101
	unsigned int dboffset;
102
	unsigned int paused;
1485 palkovsky 103
} viewport_t;
1363 vana 104
 
1646 palkovsky 105
#define MAX_ANIM_LEN    8
106
#define MAX_ANIMATIONS  4
107
typedef struct {
108
	int initialized;
109
	int enabled;
110
	unsigned int vp;
111
 
112
	unsigned int pos;
113
	unsigned int animlen;
114
	unsigned int pixmaps[MAX_ANIM_LEN];
115
} animation_t;
116
static animation_t animations[MAX_ANIMATIONS];
117
static int anims_enabled;
118
 
1552 palkovsky 119
/** Maximum number of saved pixmaps 
120
 * Pixmap is a saved rectangle
121
 */
122
#define MAX_PIXMAPS        256
123
typedef struct {
124
	unsigned int width;
125
	unsigned int height;
1781 jermar 126
	uint8_t *data;
1552 palkovsky 127
} pixmap_t;
128
static pixmap_t pixmaps[MAX_PIXMAPS];
129
 
130
/* Viewport is a rectangular area on the screen */
1485 palkovsky 131
#define MAX_VIEWPORTS 128
132
static viewport_t viewports[128];
133
 
134
/* Allow only 1 connection */
135
static int client_connected = 0;
136
 
1363 vana 137
#define RED(x, bits)	((x >> (16 + 8 - bits)) & ((1 << bits) - 1))
138
#define GREEN(x, bits)	((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
139
#define BLUE(x, bits)	((x >> (8 - bits)) & ((1 << bits) - 1))
140
 
1485 palkovsky 141
#define COL_WIDTH	8
142
#define ROW_BYTES	(screen.scanline * FONT_SCANLINES)
1363 vana 143
 
1485 palkovsky 144
#define POINTPOS(x, y)	((y) * screen.scanline + (x) * screen.pixelbytes)
1363 vana 145
 
1875 jermar 146
static inline int COLOR(int color)
147
{
148
	return screen.invert_colors ? ~color : color;
149
}
150
 
1555 palkovsky 151
/* Conversion routines between different color representations */
1993 decky 152
static void rgb_byte0888(void *dst, int rgb)
1363 vana 153
{
1555 palkovsky 154
	*(int *)dst = rgb;
1363 vana 155
}
156
 
1993 decky 157
static int byte0888_rgb(void *src)
1363 vana 158
{
1555 palkovsky 159
	return (*(int *)src) & 0xffffff;
1363 vana 160
}
161
 
1993 decky 162
static void rgb_byte888(void *dst, int rgb)
1363 vana 163
{
1781 jermar 164
	uint8_t *scr = dst;
1871 jermar 165
#if defined(FB_INVERT_ENDIAN)
1555 palkovsky 166
	scr[0] = RED(rgb, 8);
167
	scr[1] = GREEN(rgb, 8);
168
	scr[2] = BLUE(rgb, 8);
1363 vana 169
#else
1555 palkovsky 170
	scr[2] = RED(rgb, 8);
171
	scr[1] = GREEN(rgb, 8);
172
	scr[0] = BLUE(rgb, 8);
1363 vana 173
#endif
174
}
175
 
1993 decky 176
static int byte888_rgb(void *src)
1363 vana 177
{
1781 jermar 178
	uint8_t *scr = src;
1871 jermar 179
#if defined(FB_INVERT_ENDIAN)
1555 palkovsky 180
	return scr[0] << 16 | scr[1] << 8 | scr[2];
1363 vana 181
#else
1555 palkovsky 182
	return scr[2] << 16 | scr[1] << 8 | scr[0];
183
#endif	
1363 vana 184
}
185
 
1993 decky 186
/**  16-bit depth (5:5:5) */
187
static void rgb_byte555(void *dst, int rgb)
188
{
189
	/* 5-bit, 5-bits, 5-bits */ 
190
	*((uint16_t *)(dst)) = RED(rgb, 5) << 10 | GREEN(rgb, 5) << 5 | BLUE(rgb, 5);
191
}
192
 
193
/** 16-bit depth (5:5:5) */
194
static int byte555_rgb(void *src)
195
{
196
	int color = *(uint16_t *)(src);
197
	return (((color >> 10) & 0x1f) << (16 + 3)) | (((color >> 5) & 0x1f) << (8 + 3)) | ((color & 0x1f) << 3);
198
}
199
 
1555 palkovsky 200
/**  16-bit depth (5:6:5) */
1993 decky 201
static void rgb_byte565(void *dst, int rgb)
1363 vana 202
{
203
	/* 5-bit, 6-bits, 5-bits */ 
1781 jermar 204
	*((uint16_t *)(dst)) = RED(rgb, 5) << 11 | GREEN(rgb, 6) << 5 | BLUE(rgb, 5);
1363 vana 205
}
206
 
1555 palkovsky 207
/** 16-bit depth (5:6:5) */
1993 decky 208
static int byte565_rgb(void *src)
1363 vana 209
{
1781 jermar 210
	int color = *(uint16_t *)(src);
1363 vana 211
	return (((color >> 11) & 0x1f) << (16 + 3)) | (((color >> 5) & 0x3f) << (8 + 2)) | ((color & 0x1f) << 3);
212
}
213
 
214
/** Put pixel - 8-bit depth (3:2:3) */
1993 decky 215
static void rgb_byte8(void *dst, int rgb)
1363 vana 216
{
1781 jermar 217
	*(uint8_t *)dst = RED(rgb, 3) << 5 | GREEN(rgb, 2) << 3 | BLUE(rgb, 3);
1363 vana 218
}
219
 
220
/** Return pixel color - 8-bit depth (3:2:3) */
1993 decky 221
static int byte8_rgb(void *src)
1363 vana 222
{
1781 jermar 223
	int color = *(uint8_t *)src;
1363 vana 224
	return (((color >> 5) & 0x7) << (16 + 5)) | (((color >> 3) & 0x3) << (8 + 6)) | ((color & 0x7) << 5);
225
}
226
 
1498 palkovsky 227
/** Put pixel into viewport 
228
 *
1709 jermar 229
 * @param vport Viewport identification
1498 palkovsky 230
 * @param x X coord relative to viewport
231
 * @param y Y coord relative to viewport
232
 * @param color RGB color 
233
 */
1673 palkovsky 234
static void putpixel(viewport_t *vport, unsigned int x, unsigned int y, int color)
1485 palkovsky 235
{
1673 palkovsky 236
	int dx = vport->x + x;
237
	int dy = vport->y + y;
1672 palkovsky 238
 
1673 palkovsky 239
	if (! (vport->paused && vport->dbdata))
1875 jermar 240
		(*screen.rgb2scr)(&screen.fbaddress[POINTPOS(dx,dy)], COLOR(color));
1672 palkovsky 241
 
1673 palkovsky 242
	if (vport->dbdata) {
243
		int dline = (y + vport->dboffset) % vport->height;
244
		int doffset = screen.pixelbytes * (dline * vport->width + x);
1875 jermar 245
		(*screen.rgb2scr)(&vport->dbdata[doffset], COLOR(color));
1672 palkovsky 246
	}
1485 palkovsky 247
}
1672 palkovsky 248
 
1498 palkovsky 249
/** Get pixel from viewport */
1673 palkovsky 250
static int getpixel(viewport_t *vport, unsigned int x, unsigned int y)
1485 palkovsky 251
{
1673 palkovsky 252
	int dx = vport->x + x;
253
	int dy = vport->y + y;
1555 palkovsky 254
 
1875 jermar 255
	return COLOR((*screen.scr2rgb)(&screen.fbaddress[POINTPOS(dx,dy)]));
1485 palkovsky 256
}
257
 
1668 palkovsky 258
static inline void putpixel_mem(char *mem, unsigned int x, unsigned int y, 
259
				int color)
1363 vana 260
{
1875 jermar 261
	(*screen.rgb2scr)(&mem[POINTPOS(x,y)], COLOR(color));
1363 vana 262
}
263
 
1673 palkovsky 264
static void draw_rectangle(viewport_t *vport, unsigned int sx, unsigned int sy,
1559 palkovsky 265
			   unsigned int width, unsigned int height,
266
			   int color)
267
{
268
	unsigned int x, y;
1668 palkovsky 269
	static void *tmpline;
1559 palkovsky 270
 
1668 palkovsky 271
	if (!tmpline)
272
		tmpline = malloc(screen.scanline*screen.pixelbytes);
273
 
1559 palkovsky 274
	/* Clear first line */
275
	for (x = 0; x < width; x++)
1668 palkovsky 276
		putpixel_mem(tmpline, x, 0, color);
1559 palkovsky 277
 
1673 palkovsky 278
	if (!vport->paused) {
1672 palkovsky 279
		/* Recompute to screen coords */
1673 palkovsky 280
		sx += vport->x;
281
		sy += vport->y;
1672 palkovsky 282
		/* Copy the rest */
283
		for (y = sy;y < sy+height; y++)
284
			memcpy(&screen.fbaddress[POINTPOS(sx,y)], tmpline, 
285
			       screen.pixelbytes * width);
286
	}
1673 palkovsky 287
	if (vport->dbdata) {
1672 palkovsky 288
		for (y=sy;y < sy+height; y++) {
1673 palkovsky 289
			int rline = (y + vport->dboffset) % vport->height;
290
			int rpos = (rline * vport->width + sx) * screen.pixelbytes;
291
			memcpy(&vport->dbdata[rpos], tmpline, screen.pixelbytes * width);
1672 palkovsky 292
		}
293
	}
1559 palkovsky 294
 
295
}
296
 
1485 palkovsky 297
/** Fill viewport with background color */
1673 palkovsky 298
static void clear_port(viewport_t *vport)
1363 vana 299
{
1673 palkovsky 300
	draw_rectangle(vport, 0, 0, vport->width, vport->height, vport->style.bg_color);
1363 vana 301
}
302
 
1672 palkovsky 303
/** Scroll unbuffered viewport up/down
1485 palkovsky 304
 *
1709 jermar 305
 * @param vport Viewport to scroll
306
 * @param lines Positive number - scroll up, negative - scroll down
1485 palkovsky 307
 */
1673 palkovsky 308
static void scroll_port_nodb(viewport_t *vport, int lines)
1363 vana 309
{
1485 palkovsky 310
	int y;
1672 palkovsky 311
 
312
	if (lines > 0) {
313
		for (y=vport->y; y < vport->y+vport->height - lines; y++)
1559 palkovsky 314
			memcpy(&screen.fbaddress[POINTPOS(vport->x,y)],
1672 palkovsky 315
			       &screen.fbaddress[POINTPOS(vport->x,y + lines)],
1559 palkovsky 316
			       screen.pixelbytes * vport->width);
1673 palkovsky 317
		draw_rectangle(vport, 0, vport->height - lines,
1672 palkovsky 318
			       vport->width, lines, vport->style.bg_color);
319
	} else if (lines < 0) {
320
		lines = -lines;
321
		for (y=vport->y + vport->height-1; y >= vport->y + lines; y--)
1559 palkovsky 322
			memcpy(&screen.fbaddress[POINTPOS(vport->x,y)],
1672 palkovsky 323
			       &screen.fbaddress[POINTPOS(vport->x,y - lines)],
324
			       screen.pixelbytes * vport->width);
1673 palkovsky 325
		draw_rectangle(vport, 0, 0, vport->width, lines, vport->style.bg_color);
1485 palkovsky 326
	}
1363 vana 327
}
328
 
1672 palkovsky 329
/** Refresh given viewport from double buffer */
1673 palkovsky 330
static void refresh_viewport_db(viewport_t *vport)
1672 palkovsky 331
{
332
	unsigned int y, srcy, srcoff, dsty, dstx;
333
 
1673 palkovsky 334
	for (y = 0; y < vport->height; y++) {
335
		srcy = (y + vport->dboffset) % vport->height;
336
		srcoff = (vport->width * srcy) * screen.pixelbytes;
1672 palkovsky 337
 
1673 palkovsky 338
		dstx = vport->x;
339
		dsty = vport->y + y;
1672 palkovsky 340
 
341
		memcpy(&screen.fbaddress[POINTPOS(dstx,dsty)],
1673 palkovsky 342
		       &vport->dbdata[srcoff], 
343
		       vport->width*screen.pixelbytes);
1672 palkovsky 344
	}
345
}
346
 
347
/** Scroll viewport that has double buffering enabled */
1673 palkovsky 348
static void scroll_port_db(viewport_t *vport, int lines)
1672 palkovsky 349
{
1673 palkovsky 350
	++vport->paused;
1672 palkovsky 351
	if (lines > 0) {
1673 palkovsky 352
		draw_rectangle(vport, 0, 0, vport->width, lines,
353
			       vport->style.bg_color);
354
		vport->dboffset += lines;
355
		vport->dboffset %= vport->height;
1672 palkovsky 356
	} else if (lines < 0) {
357
		lines = -lines;
1673 palkovsky 358
		draw_rectangle(vport, 0, vport->height-lines, 
359
			       vport->width, lines,
360
			       vport->style.bg_color);
1672 palkovsky 361
 
1673 palkovsky 362
		if (vport->dboffset < lines)
363
			vport->dboffset += vport->height;
364
		vport->dboffset -= lines;
1672 palkovsky 365
	}
366
 
1673 palkovsky 367
	--vport->paused;
1672 palkovsky 368
 
1673 palkovsky 369
	refresh_viewport_db(vport);
1672 palkovsky 370
}
371
 
372
/** Scrolls viewport given number of lines */
1673 palkovsky 373
static void scroll_port(viewport_t *vport, int lines)
1672 palkovsky 374
{
1673 palkovsky 375
	if (vport->dbdata)
376
		scroll_port_db(vport, lines);
1672 palkovsky 377
	else
1673 palkovsky 378
		scroll_port_nodb(vport, lines);
1672 palkovsky 379
 
380
}
381
 
1673 palkovsky 382
static void invert_pixel(viewport_t *vport, unsigned int x, unsigned int y)
1363 vana 383
{
1673 palkovsky 384
	putpixel(vport, x, y, ~getpixel(vport, x, y));
1363 vana 385
}
386
 
387
 
388
/***************************************************************/
389
/* Character-console functions */
390
 
1558 palkovsky 391
/** Draw character at given position
392
 *
1709 jermar 393
 * @param vport Viewport where the character is printed
1558 palkovsky 394
 * @param sx Coordinates of top-left of the character
395
 * @param sy Coordinates of top-left of the character
396
 * @param style Color of the character
397
 * @param transparent If false, print background color
398
 */
1781 jermar 399
static void draw_glyph(viewport_t *vport,uint8_t glyph, unsigned int sx, unsigned int sy, 
1558 palkovsky 400
		       style_t style, int transparent)
1363 vana 401
{
1509 palkovsky 402
	int i;
1363 vana 403
	unsigned int y;
1509 palkovsky 404
	unsigned int glline;
1363 vana 405
 
1509 palkovsky 406
	for (y = 0; y < FONT_SCANLINES; y++) {
407
		glline = fb_font[glyph * FONT_SCANLINES + y];
408
		for (i = 0; i < 8; i++) {
409
			if (glline & (1 << (7 - i)))
1673 palkovsky 410
				putpixel(vport, sx + i, sy + y, style.fg_color);
1558 palkovsky 411
			else if (!transparent)
1673 palkovsky 412
				putpixel(vport, sx + i, sy + y, style.bg_color);
1509 palkovsky 413
		}
414
	}
1363 vana 415
}
416
 
417
/** Invert character at given position */
1673 palkovsky 418
static void invert_char(viewport_t *vport,unsigned int row, unsigned int col)
1363 vana 419
{
420
	unsigned int x;
421
	unsigned int y;
422
 
423
	for (x = 0; x < COL_WIDTH; x++)
424
		for (y = 0; y < FONT_SCANLINES; y++)
1673 palkovsky 425
			invert_pixel(vport, col * COL_WIDTH + x, row * FONT_SCANLINES + y);
1363 vana 426
}
427
 
428
/***************************************************************/
429
/* Stdout specific functions */
430
 
431
 
1485 palkovsky 432
/** Create new viewport
1363 vana 433
 *
1485 palkovsky 434
 * @return New viewport number
1363 vana 435
 */
1485 palkovsky 436
static int viewport_create(unsigned int x, unsigned int y,unsigned int width, 
437
			   unsigned int height)
1363 vana 438
{
1485 palkovsky 439
	int i;
440
 
1646 palkovsky 441
	for (i=0; i < MAX_VIEWPORTS; i++) {
1485 palkovsky 442
		if (!viewports[i].initialized)
1363 vana 443
			break;
444
	}
1485 palkovsky 445
	if (i == MAX_VIEWPORTS)
446
		return ELIMIT;
447
 
448
	viewports[i].x = x;
449
	viewports[i].y = y;
450
	viewports[i].width = width;
451
	viewports[i].height = height;
1363 vana 452
 
1485 palkovsky 453
	viewports[i].rows = height / FONT_SCANLINES;
454
	viewports[i].cols = width / COL_WIDTH;
455
 
1509 palkovsky 456
	viewports[i].style.bg_color = DEFAULT_BGCOLOR;
457
	viewports[i].style.fg_color = DEFAULT_FGCOLOR;
1363 vana 458
 
1489 palkovsky 459
	viewports[i].cur_col = 0;
460
	viewports[i].cur_row = 0;
461
	viewports[i].cursor_active = 0;
462
 
1486 palkovsky 463
	viewports[i].initialized = 1;
464
 
1485 palkovsky 465
	return i;
1363 vana 466
}
467
 
468
 
469
/** Initialize framebuffer as a chardev output device
470
 *
1993 decky 471
 * @param addr          Address of theframebuffer
472
 * @param xres          Screen width in pixels
473
 * @param yres          Screen height in pixels
474
 * @param visual        Bits per pixel (8, 16, 24, 32)
475
 * @param scan          Bytes per one scanline
1875 jermar 476
 * @param invert_colors Inverted colors.
1363 vana 477
 *
478
 */
1993 decky 479
static bool screen_init(void *addr, unsigned int xres, unsigned int yres, unsigned int scan, unsigned int visual, bool invert_colors)
1363 vana 480
{
1993 decky 481
	switch (visual) {
482
	case VISUAL_INDIRECT_8:
483
		screen.rgb2scr = rgb_byte8;
484
		screen.scr2rgb = byte8_rgb;
1886 jermar 485
		screen.pixelbytes = 1;
486
		break;
1993 decky 487
	case VISUAL_RGB_5_5_5:
488
		screen.rgb2scr = rgb_byte555;
489
		screen.scr2rgb = byte555_rgb;
1886 jermar 490
		screen.pixelbytes = 2;
491
		break;
1993 decky 492
	case VISUAL_RGB_5_6_5:
493
		screen.rgb2scr = rgb_byte565;
494
		screen.scr2rgb = byte565_rgb;
495
		screen.pixelbytes = 2;
1886 jermar 496
		break;
1993 decky 497
	case VISUAL_RGB_8_8_8:
498
		screen.rgb2scr = rgb_byte888;
499
		screen.scr2rgb = byte888_rgb;
500
		screen.pixelbytes = 3;
501
		break;
502
	case VISUAL_RGB_8_8_8_0:
503
		screen.rgb2scr = rgb_byte888;
504
		screen.scr2rgb = byte888_rgb;
1886 jermar 505
		screen.pixelbytes = 4;
506
		break;
1993 decky 507
	case VISUAL_RGB_0_8_8_8:
508
		screen.rgb2scr = rgb_byte0888;
509
		screen.scr2rgb = byte0888_rgb;
510
		screen.pixelbytes = 4;
511
		break;
512
	default:
513
		return false;
1363 vana 514
	}
515
 
1485 palkovsky 516
	screen.fbaddress = (unsigned char *) addr;
517
	screen.xres = xres;
518
	screen.yres = yres;
519
	screen.scanline = scan;
1875 jermar 520
	screen.invert_colors = invert_colors;
1363 vana 521
 
1485 palkovsky 522
	/* Create first viewport */
1993 decky 523
	viewport_create(0, 0, xres, yres);
524
 
525
	return true;
1485 palkovsky 526
}
1363 vana 527
 
1552 palkovsky 528
/** Hide cursor if it is shown */
1673 palkovsky 529
static void cursor_hide(viewport_t *vport)
1500 palkovsky 530
{
531
	if (vport->cursor_active && vport->cursor_shown) {
1673 palkovsky 532
		invert_char(vport, vport->cur_row, vport->cur_col);
1500 palkovsky 533
		vport->cursor_shown = 0;
534
	}
535
}
536
 
1552 palkovsky 537
/** Show cursor if cursor showing is enabled */
1673 palkovsky 538
static void cursor_print(viewport_t *vport)
1500 palkovsky 539
{
540
	/* Do not check for cursor_shown */
541
	if (vport->cursor_active) {
1673 palkovsky 542
		invert_char(vport, vport->cur_row, vport->cur_col);
1500 palkovsky 543
		vport->cursor_shown = 1;
544
	}
545
}
546
 
1552 palkovsky 547
/** Invert cursor, if it is enabled */
1673 palkovsky 548
static void cursor_blink(viewport_t *vport)
1500 palkovsky 549
{
550
	if (vport->cursor_shown)
1673 palkovsky 551
		cursor_hide(vport);
1500 palkovsky 552
	else
1673 palkovsky 553
		cursor_print(vport);
1500 palkovsky 554
}
555
 
1498 palkovsky 556
/** Draw character at given position relative to viewport 
557
 * 
1709 jermar 558
 * @param vport Viewport identification
1498 palkovsky 559
 * @param c Character to print
560
 * @param row Screen position relative to viewport
561
 * @param col Screen position relative to viewport
1558 palkovsky 562
 * @param transparent If false, print background color with character
1498 palkovsky 563
 */
1673 palkovsky 564
static void draw_char(viewport_t *vport, char c, unsigned int row, unsigned int col, 
565
		      style_t style, int transparent)
1486 palkovsky 566
{
1500 palkovsky 567
	/* Optimize - do not hide cursor if we are going to overwrite it */
568
	if (vport->cursor_active && vport->cursor_shown && 
569
	    (vport->cur_col != col || vport->cur_row != row))
1673 palkovsky 570
		invert_char(vport, vport->cur_row, vport->cur_col);
1486 palkovsky 571
 
1673 palkovsky 572
	draw_glyph(vport, c, col * COL_WIDTH, row * FONT_SCANLINES, style, transparent);
1489 palkovsky 573
 
574
	vport->cur_col = col;
575
	vport->cur_row = row;
576
 
577
	vport->cur_col++;
578
	if (vport->cur_col>= vport->cols) {
579
		vport->cur_col = 0;
580
		vport->cur_row++;
581
		if (vport->cur_row >= vport->rows)
582
			vport->cur_row--;
1486 palkovsky 583
	}
1673 palkovsky 584
	cursor_print(vport);
1486 palkovsky 585
}
586
 
1552 palkovsky 587
/** Draw text data to viewport
588
 *
1709 jermar 589
 * @param vport Viewport id
1552 palkovsky 590
 * @param data Text data fitting exactly into viewport
591
 */
1673 palkovsky 592
static void draw_text_data(viewport_t *vport, keyfield_t *data)
1505 palkovsky 593
{
594
	int i;
1515 palkovsky 595
	int col,row;
1505 palkovsky 596
 
1673 palkovsky 597
	clear_port(vport);
1505 palkovsky 598
	for (i=0; i < vport->cols * vport->rows; i++) {
1509 palkovsky 599
		if (data[i].character == ' ' && style_same(data[i].style,vport->style))
1505 palkovsky 600
			continue;
1515 palkovsky 601
		col = i % vport->cols;
602
		row = i / vport->cols;
1673 palkovsky 603
		draw_glyph(vport, data[i].character, col * COL_WIDTH, row * FONT_SCANLINES, 
1558 palkovsky 604
			   data[i].style, style_same(data[i].style,vport->style));
1505 palkovsky 605
	}
1673 palkovsky 606
	cursor_print(vport);
1505 palkovsky 607
}
608
 
1555 palkovsky 609
 
610
/** Return first free pixmap */
611
static int find_free_pixmap(void)
612
{
613
	int i;
614
 
615
	for (i=0;i < MAX_PIXMAPS;i++)
616
		if (!pixmaps[i].data)
617
			return i;
618
	return -1;
619
}
620
 
621
static void putpixel_pixmap(int pm, unsigned int x, unsigned int y, int color)
622
{
623
	pixmap_t *pmap = &pixmaps[pm];
624
	int pos = (y * pmap->width + x) * screen.pixelbytes;
625
 
1875 jermar 626
	(*screen.rgb2scr)(&pmap->data[pos],COLOR(color));
1555 palkovsky 627
}
628
 
629
/** Create a new pixmap and return appropriate ID */
1720 palkovsky 630
static int shm2pixmap(unsigned char *shm, size_t size)
1555 palkovsky 631
{
632
	int pm;
633
	pixmap_t *pmap;
634
 
635
	pm = find_free_pixmap();
636
	if (pm == -1)
637
		return ELIMIT;
638
	pmap = &pixmaps[pm];
639
 
640
	if (ppm_get_data(shm, size, &pmap->width, &pmap->height))
641
		return EINVAL;
642
 
643
	pmap->data = malloc(pmap->width * pmap->height * screen.pixelbytes);
644
	if (!pmap->data)
645
		return ENOMEM;
646
 
647
	ppm_draw(shm, size, 0, 0, pmap->width, pmap->height, 
1673 palkovsky 648
		 (putpixel_cb_t)putpixel_pixmap, (void *)pm);
1555 palkovsky 649
 
650
	return pm;
651
}
652
 
1552 palkovsky 653
/** Handle shared memory communication calls
654
 *
655
 * Protocol for drawing pixmaps:
656
 * - FB_PREPARE_SHM(client shm identification)
657
 * - IPC_M_SEND_AS_AREA
658
 * - FB_DRAW_PPM(startx,starty)
659
 * - FB_DROP_SHM
660
 *
661
 * Protocol for text drawing
662
 * - IPC_M_SEND_AS_AREA
663
 * - FB_DRAW_TEXT_DATA
664
 *
665
 * @param callid Callid of the current call
666
 * @param call Current call data
667
 * @param vp Active viewport
668
 * @return 0 if the call was not handled byt this function, 1 otherwise
669
 *
670
 * note: this function is not threads safe, you would have
671
 * to redefine static variables with __thread
672
 */
1547 palkovsky 673
static int shm_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
674
{
675
	static keyfield_t *interbuffer = NULL;
676
	static size_t intersize = 0;
1505 palkovsky 677
 
1720 palkovsky 678
	static unsigned char *shm = NULL;
1555 palkovsky 679
	static ipcarg_t shm_id = 0;
680
	static size_t shm_size;
1547 palkovsky 681
 
682
	int handled = 1;
683
	int retval = 0;
684
	viewport_t *vport = &viewports[vp];
685
	unsigned int x,y;
686
 
687
	switch (IPC_GET_METHOD(*call)) {
688
	case IPC_M_AS_AREA_SEND:
689
		/* We accept one area for data interchange */
1555 palkovsky 690
		if (IPC_GET_ARG1(*call) == shm_id) {
1547 palkovsky 691
			void *dest = as_get_mappable_page(IPC_GET_ARG2(*call));
1555 palkovsky 692
			shm_size = IPC_GET_ARG2(*call);
1547 palkovsky 693
			if (!ipc_answer_fast(callid, 0, (sysarg_t)dest, 0)) 
1555 palkovsky 694
				shm = dest;
1547 palkovsky 695
			else
1555 palkovsky 696
				shm_id = 0;
697
			if (shm[0] != 'P')
1547 palkovsky 698
				while (1)
699
					;
700
			return 1;
701
		} else {
702
			intersize = IPC_GET_ARG2(*call);
1720 palkovsky 703
			receive_comm_area(callid,call,(void *)&interbuffer);
1547 palkovsky 704
		}
705
		return 1;
706
	case FB_PREPARE_SHM:
1555 palkovsky 707
		if (shm_id)
1547 palkovsky 708
			retval = EBUSY;
709
		else 
1555 palkovsky 710
			shm_id = IPC_GET_ARG1(*call);
1547 palkovsky 711
		break;
712
 
713
	case FB_DROP_SHM:
1555 palkovsky 714
		if (shm) {
715
			as_area_destroy(shm);
716
			shm = NULL;
1547 palkovsky 717
		}
1555 palkovsky 718
		shm_id = 0;
1547 palkovsky 719
		break;
1555 palkovsky 720
 
721
	case FB_SHM2PIXMAP:
722
		if (!shm) {
723
			retval = EINVAL;
724
			break;
725
		}
726
		retval = shm2pixmap(shm, shm_size);
727
		break;
1547 palkovsky 728
	case FB_DRAW_PPM:
1555 palkovsky 729
		if (!shm) {
1547 palkovsky 730
			retval = EINVAL;
731
			break;
732
		}
733
		x = IPC_GET_ARG1(*call);
734
		y = IPC_GET_ARG2(*call);
735
		if (x > vport->width || y > vport->height) {
736
			retval = EINVAL;
737
			break;
738
		}
739
 
1555 palkovsky 740
		ppm_draw(shm, shm_size, IPC_GET_ARG1(*call), IPC_GET_ARG2(*call),
1673 palkovsky 741
			 vport->width - x, vport->height - y, (putpixel_cb_t)putpixel, vport);
1547 palkovsky 742
		break;
743
	case FB_DRAW_TEXT_DATA:
744
		if (!interbuffer) {
745
			retval = EINVAL;
746
			break;
747
		}
748
		if (intersize < vport->cols*vport->rows*sizeof(*interbuffer)) {
749
			retval = EINVAL;
750
			break;
751
		}
1673 palkovsky 752
		draw_text_data(vport, interbuffer);
1547 palkovsky 753
		break;
754
	default:
755
		handled = 0;
756
	}
757
 
758
	if (handled)
759
		ipc_answer_fast(callid, retval, 0, 0);
760
	return handled;
761
}
762
 
1711 palkovsky 763
static void copy_vp_to_pixmap(viewport_t *vport, pixmap_t *pmap)
1552 palkovsky 764
{
1720 palkovsky 765
	int y;
1552 palkovsky 766
	int rowsize;
767
	int tmp;
1711 palkovsky 768
	int width = vport->width;
769
	int height = vport->height;
1552 palkovsky 770
 
1711 palkovsky 771
	if (width + vport->x > screen.xres)
772
		width = screen.xres - vport->x;
773
	if (height + vport->y  > screen.yres)
774
		height = screen.yres - vport->y;
775
 
776
	rowsize = width * screen.pixelbytes;
777
 
778
	for (y=0;y < height; y++) {
779
		tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
780
		memcpy(pmap->data + rowsize*y, screen.fbaddress + tmp, rowsize); 
781
	}
782
}
783
 
784
/** Save viewport to pixmap */
785
static int save_vp_to_pixmap(viewport_t *vport)
786
{
787
	int pm;
788
	pixmap_t *pmap;
789
 
1552 palkovsky 790
	pm = find_free_pixmap();
791
	if (pm == -1)
792
		return ELIMIT;
793
 
794
	pmap = &pixmaps[pm];
795
	pmap->data = malloc(screen.pixelbytes * vport->width * vport->height);
796
	if (!pmap->data)
797
		return ENOMEM;
798
 
799
	pmap->width = vport->width;
800
	pmap->height = vport->height;
1711 palkovsky 801
 
802
	copy_vp_to_pixmap(vport, pmap);
1552 palkovsky 803
 
804
	return pm;
805
}
806
 
807
/** Draw pixmap on screen
808
 *
809
 * @param vp Viewport to draw on
810
 * @param pm Pixmap identifier
811
 */
812
static int draw_pixmap(int vp, int pm)
813
{
814
	pixmap_t *pmap = &pixmaps[pm];
815
	viewport_t *vport = &viewports[vp];
1720 palkovsky 816
	int y;
1552 palkovsky 817
	int tmp, srcrowsize;
818
	int realwidth, realheight, realrowsize;
1711 palkovsky 819
	int width = vport->width;
820
	int height = vport->height;
1552 palkovsky 821
 
1711 palkovsky 822
	if (width + vport->x > screen.xres)
823
		width = screen.xres - vport->x;
824
	if (height + vport->y > screen.yres)
825
		height = screen.yres - vport->y;
826
 
1552 palkovsky 827
	if (!pmap->data)
828
		return EINVAL;
829
 
1711 palkovsky 830
	realwidth = pmap->width <= width ? pmap->width : width;
831
	realheight = pmap->height <= height ? pmap->height : height;
1552 palkovsky 832
 
833
	srcrowsize = vport->width * screen.pixelbytes;
834
	realrowsize = realwidth * screen.pixelbytes;
835
 
836
	for (y=0; y < realheight; y++) {
837
		tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
838
		memcpy(screen.fbaddress + tmp, pmap->data + y * srcrowsize, realrowsize);
839
	}
1567 palkovsky 840
	return 0;
1552 palkovsky 841
}
842
 
1646 palkovsky 843
/** Tick animation one step forward */
844
static void anims_tick(void)
845
{
846
	int i;
847
	static int counts = 0;
848
 
849
	/* Limit redrawing */
850
	counts = (counts+1) % 8;
851
	if (counts)
852
		return;
853
 
854
	for (i=0; i < MAX_ANIMATIONS; i++) {
855
		if (!animations[i].animlen || !animations[i].initialized || !animations[i].enabled)
856
			continue;
857
		draw_pixmap(animations[i].vp, animations[i].pixmaps[animations[i].pos]);
858
		animations[i].pos = (animations[i].pos+1) % animations[i].animlen;
859
	}
860
}
861
 
1711 palkovsky 862
 
863
static int pointer_x, pointer_y;
1723 palkovsky 864
static int pointer_shown, pointer_enabled;
1711 palkovsky 865
static int pointer_vport = -1;
866
static int pointer_pixmap = -1;
867
 
868
static void mouse_show(void)
869
{
870
	int i,j;
871
	int visibility;
872
	int color;
873
	int bytepos;
874
 
1723 palkovsky 875
	if (pointer_shown || !pointer_enabled)
876
		return;
877
 
1711 palkovsky 878
	/* Save image under the cursor */
879
	if (pointer_vport == -1) {
880
		pointer_vport = viewport_create(pointer_x, pointer_y, pointer_width, pointer_height);
881
		if (pointer_vport < 0)
882
			return;
883
	} else {
884
		viewports[pointer_vport].x = pointer_x;
885
		viewports[pointer_vport].y = pointer_y;
886
	}
887
 
888
	if (pointer_pixmap == -1)
889
		pointer_pixmap = save_vp_to_pixmap(&viewports[pointer_vport]);
890
	else
891
		copy_vp_to_pixmap(&viewports[pointer_vport], &pixmaps[pointer_pixmap]);
892
 
893
	/* Draw cursor */
894
	for (i=0; i < pointer_height; i++)
895
		for (j=0;j < pointer_width; j++) {
896
			bytepos = i*((pointer_width-1)/8+1) + j/8;
897
			visibility = pointer_mask_bits[bytepos] & (1 << (j % 8));
898
			if (visibility) {
899
				color = pointer_bits[bytepos] & (1 << (j % 8)) ? 0 : 0xffffff;
900
				if (pointer_x+j < screen.xres && pointer_y+i < screen.yres)
901
					putpixel(&viewports[0], pointer_x+j, pointer_y+i, color);
902
			}
903
		}
904
	pointer_shown = 1;
905
}
906
 
907
static void mouse_hide(void)
908
{
909
	/* Restore image under the cursor */
910
	if (pointer_shown) {
911
		draw_pixmap(pointer_vport, pointer_pixmap);
912
		pointer_shown = 0;
913
	}
914
}
915
 
916
static void mouse_move(unsigned int x, unsigned int y)
917
{
918
	mouse_hide();
919
	pointer_x = x;
920
	pointer_y = y;
921
	mouse_show();
922
}
923
 
1646 palkovsky 924
static int anim_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
925
{
926
	int handled = 1;
927
	int retval = 0;
928
	int i,nvp;
929
	int newval;
930
 
931
	switch (IPC_GET_METHOD(*call)) {
932
	case FB_ANIM_CREATE:
933
		nvp = IPC_GET_ARG1(*call);
934
		if (nvp == -1)
935
			nvp = vp;
936
		if (nvp >= MAX_VIEWPORTS || nvp < 0 || !viewports[nvp].initialized) {
937
			retval = EINVAL;
938
			break;
939
		}
940
		for (i=0; i < MAX_ANIMATIONS; i++) {
941
			if (! animations[i].initialized)
942
				break;
943
		}
944
		if (i == MAX_ANIMATIONS) {
945
			retval = ELIMIT;
946
			break;
947
		}
948
		animations[i].initialized = 1;
949
		animations[i].animlen = 0;
950
		animations[i].pos = 0;
951
		animations[i].enabled = 0;
952
		animations[i].vp = nvp;
953
		retval = i;
954
		break;
955
	case FB_ANIM_DROP:
956
		i = IPC_GET_ARG1(*call);
1720 palkovsky 957
		if (i >= MAX_ANIMATIONS || i < 0) {
1646 palkovsky 958
			retval = EINVAL;
959
			break;
960
		}
961
		animations[i].initialized = 0;
962
		break;
963
	case FB_ANIM_ADDPIXMAP:
964
		i = IPC_GET_ARG1(*call);
965
		if (i >= MAX_ANIMATIONS || i < 0 || !animations[i].initialized) {
966
			retval = EINVAL;
967
			break;
968
		}
969
		if (animations[i].animlen == MAX_ANIM_LEN) {
970
			retval = ELIMIT;
971
			break;
972
		}
973
		newval = IPC_GET_ARG2(*call);
974
		if (newval < 0 || newval > MAX_PIXMAPS || !pixmaps[newval].data) {
975
			retval = EINVAL;
976
			break;
977
		}
978
		animations[i].pixmaps[animations[i].animlen++] = newval;
979
		break;
980
	case FB_ANIM_CHGVP:
981
		i = IPC_GET_ARG1(*call);
982
		if (i >= MAX_ANIMATIONS || i < 0) {
983
			retval = EINVAL;
984
			break;
985
		}
986
		nvp = IPC_GET_ARG2(*call);
987
		if (nvp == -1)
988
			nvp = vp;
989
		if (nvp >= MAX_VIEWPORTS || nvp < 0 || !viewports[nvp].initialized) {
990
			retval = EINVAL;
991
			break;
992
		}
993
		animations[i].vp = nvp;
994
		break;
995
	case FB_ANIM_START:
996
	case FB_ANIM_STOP:
997
		i = IPC_GET_ARG1(*call);
998
		if (i >= MAX_ANIMATIONS || i < 0) {
999
			retval = EINVAL;
1000
			break;
1001
		}
1002
		newval = (IPC_GET_METHOD(*call) == FB_ANIM_START);
1003
		if (newval ^ animations[i].enabled) {
1004
			animations[i].enabled = newval;
1005
			anims_enabled += newval ? 1 : -1;
1006
		}
1007
		break;
1008
	default:
1009
		handled = 0;
1010
	}
1011
	if (handled)
1012
		ipc_answer_fast(callid, retval, 0, 0);
1013
	return handled;
1014
}
1015
 
1552 palkovsky 1016
/** Handler for messages concerning pixmap handling */
1017
static int pixmap_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
1018
{
1019
	int handled = 1;
1020
	int retval = 0;
1021
	int i,nvp;
1022
 
1023
	switch (IPC_GET_METHOD(*call)) {
1024
	case FB_VP_DRAW_PIXMAP:
1025
		nvp = IPC_GET_ARG1(*call);
1026
		if (nvp == -1)
1027
			nvp = vp;
1028
		if (nvp < 0 || nvp >= MAX_VIEWPORTS || !viewports[nvp].initialized) {
1029
			retval = EINVAL;
1030
			break;
1031
		}
1032
		i = IPC_GET_ARG2(*call);
1033
		retval = draw_pixmap(nvp, i);
1034
		break;
1035
	case FB_VP2PIXMAP:
1036
		nvp = IPC_GET_ARG1(*call);
1037
		if (nvp == -1)
1038
			nvp = vp;
1039
		if (nvp < 0 || nvp >= MAX_VIEWPORTS || !viewports[nvp].initialized)
1040
			retval = EINVAL;
1041
		else
1711 palkovsky 1042
			retval = save_vp_to_pixmap(&viewports[nvp]);
1552 palkovsky 1043
		break;
1044
	case FB_DROP_PIXMAP:
1045
		i = IPC_GET_ARG1(*call);
1046
		if (i >= MAX_PIXMAPS) {
1047
			retval = EINVAL;
1048
			break;
1049
		}
1050
		if (pixmaps[i].data) {
1051
			free(pixmaps[i].data);
1052
			pixmaps[i].data = NULL;
1053
		}
1054
		break;
1055
	default:
1056
		handled = 0;
1057
	}
1058
 
1059
	if (handled)
1060
		ipc_answer_fast(callid, retval, 0, 0);
1061
	return handled;
1062
 
1063
}
1064
 
1498 palkovsky 1065
/** Function for handling connections to FB
1066
 *
1067
 */
1490 palkovsky 1068
static void fb_client_connection(ipc_callid_t iid, ipc_call_t *icall)
1363 vana 1069
{
1485 palkovsky 1070
	ipc_callid_t callid;
1071
	ipc_call_t call;
1072
	int retval;
1073
	int i;
1074
	unsigned int row,col;
1075
	char c;
1486 palkovsky 1076
 
1485 palkovsky 1077
	int vp = 0;
1486 palkovsky 1078
	viewport_t *vport = &viewports[0];
1363 vana 1079
 
1485 palkovsky 1080
	if (client_connected) {
1081
		ipc_answer_fast(iid, ELIMIT, 0,0);
1082
		return;
1083
	}
1486 palkovsky 1084
	client_connected = 1;
1485 palkovsky 1085
	ipc_answer_fast(iid, 0, 0, 0); /* Accept connection */
1363 vana 1086
 
1485 palkovsky 1087
	while (1) {
1646 palkovsky 1088
		if (vport->cursor_active || anims_enabled)
1640 palkovsky 1089
			callid = async_get_call_timeout(&call,250000);
1090
		else
1091
			callid = async_get_call(&call);
1092
 
1713 palkovsky 1093
		mouse_hide();
1500 palkovsky 1094
		if (!callid) {
1673 palkovsky 1095
			cursor_blink(vport);
1646 palkovsky 1096
			anims_tick();
1723 palkovsky 1097
			mouse_show();
1500 palkovsky 1098
			continue;
1099
		}
1547 palkovsky 1100
		if (shm_handle(callid, &call, vp))
1101
			continue;
1552 palkovsky 1102
		if (pixmap_handle(callid, &call, vp))
1103
			continue;
1646 palkovsky 1104
		if (anim_handle(callid, &call, vp))
1105
			continue;
1547 palkovsky 1106
 
1485 palkovsky 1107
 		switch (IPC_GET_METHOD(call)) {
1108
		case IPC_M_PHONE_HUNGUP:
1109
			client_connected = 0;
1110
			/* cleanup other viewports */
1111
			for (i=1; i < MAX_VIEWPORTS; i++)
1486 palkovsky 1112
				vport->initialized = 0;
1485 palkovsky 1113
			return; /* Exit thread */
1505 palkovsky 1114
 
1485 palkovsky 1115
		case FB_PUTCHAR:
1558 palkovsky 1116
		case FB_TRANS_PUTCHAR:
1485 palkovsky 1117
			c = IPC_GET_ARG1(call);
1118
			row = IPC_GET_ARG2(call);
1119
			col = IPC_GET_ARG3(call);
1486 palkovsky 1120
			if (row >= vport->rows || col >= vport->cols) {
1485 palkovsky 1121
				retval = EINVAL;
1122
				break;
1123
			}
1124
			ipc_answer_fast(callid,0,0,0);
1486 palkovsky 1125
 
1673 palkovsky 1126
			draw_char(vport, c, row, col, vport->style, IPC_GET_METHOD(call) == FB_TRANS_PUTCHAR);
1485 palkovsky 1127
			continue; /* msg already answered */
1128
		case FB_CLEAR:
1673 palkovsky 1129
			clear_port(vport);
1130
			cursor_print(vport);
1485 palkovsky 1131
			retval = 0;
1132
			break;
1486 palkovsky 1133
 		case FB_CURSOR_GOTO:
1134
			row = IPC_GET_ARG1(call);
1135
			col = IPC_GET_ARG2(call);
1136
			if (row >= vport->rows || col >= vport->cols) {
1137
				retval = EINVAL;
1138
				break;
1139
			}
1140
 			retval = 0;
1673 palkovsky 1141
			cursor_hide(vport);
1486 palkovsky 1142
			vport->cur_col = col;
1143
			vport->cur_row = row;
1673 palkovsky 1144
			cursor_print(vport);
1486 palkovsky 1145
 			break;
1146
		case FB_CURSOR_VISIBILITY:
1673 palkovsky 1147
			cursor_hide(vport);
1500 palkovsky 1148
			vport->cursor_active = IPC_GET_ARG1(call);
1673 palkovsky 1149
			cursor_print(vport);
1486 palkovsky 1150
			retval = 0;
1151
			break;
1485 palkovsky 1152
		case FB_GET_CSIZE:
1486 palkovsky 1153
			ipc_answer_fast(callid, 0, vport->rows, vport->cols);
1485 palkovsky 1154
			continue;
1486 palkovsky 1155
		case FB_SCROLL:
1156
			i = IPC_GET_ARG1(call);
1157
			if (i > vport->rows || i < (- (int)vport->rows)) {
1158
				retval = EINVAL;
1159
				break;
1160
			}
1673 palkovsky 1161
			cursor_hide(vport);
1162
			scroll_port(vport, i*FONT_SCANLINES);
1163
			cursor_print(vport);
1486 palkovsky 1164
			retval = 0;
1165
			break;
1672 palkovsky 1166
		case FB_VIEWPORT_DB:
1167
			/* Enable double buffering */
1168
			i = IPC_GET_ARG1(call);
1169
			if (i == -1)
1170
				i = vp;
1171
			if (i < 0 || i >= MAX_VIEWPORTS) {
1172
				retval = EINVAL;
1173
				break;
1174
			}
1175
			if (! viewports[i].initialized ) {
1176
				retval = EADDRNOTAVAIL;
1177
				break;
1178
			}
1179
			viewports[i].dboffset = 0;
1180
			if (IPC_GET_ARG2(call) == 1 && !viewports[i].dbdata)
1181
				viewports[i].dbdata = malloc(screen.pixelbytes*viewports[i].width * viewports[i].height);
1182
			else if (IPC_GET_ARG2(call) == 0 && viewports[i].dbdata) {
1183
				free(viewports[i].dbdata);
1184
				viewports[i].dbdata = NULL;
1185
			}
1186
			retval = 0;
1187
			break;
1486 palkovsky 1188
		case FB_VIEWPORT_SWITCH:
1189
			i = IPC_GET_ARG1(call);
1190
			if (i < 0 || i >= MAX_VIEWPORTS) {
1191
				retval = EINVAL;
1192
				break;
1193
			}
1194
			if (! viewports[i].initialized ) {
1195
				retval = EADDRNOTAVAIL;
1196
				break;
1197
			}
1673 palkovsky 1198
			cursor_hide(vport);
1486 palkovsky 1199
			vp = i;
1200
			vport = &viewports[vp];
1673 palkovsky 1201
			cursor_print(vport);
1486 palkovsky 1202
			retval = 0;
1203
			break;
1204
		case FB_VIEWPORT_CREATE:
1205
			retval = viewport_create(IPC_GET_ARG1(call) >> 16,
1206
						 IPC_GET_ARG1(call) & 0xffff,
1207
						 IPC_GET_ARG2(call) >> 16,
1208
						 IPC_GET_ARG2(call) & 0xffff);
1209
			break;
1210
		case FB_VIEWPORT_DELETE:
1211
			i = IPC_GET_ARG1(call);
1212
			if (i < 0 || i >= MAX_VIEWPORTS) {
1213
				retval = EINVAL;
1214
				break;
1215
			}
1216
			if (! viewports[i].initialized ) {
1217
				retval = EADDRNOTAVAIL;
1218
				break;
1219
			}
1220
			viewports[i].initialized = 0;
1672 palkovsky 1221
			if (viewports[i].dbdata) {
1222
				free(viewports[i].dbdata);
1223
				viewports[i].dbdata = NULL;
1224
			}
1486 palkovsky 1225
			retval = 0;
1226
			break;
1227
		case FB_SET_STYLE:
1509 palkovsky 1228
			vport->style.fg_color = IPC_GET_ARG1(call);
1229
			vport->style.bg_color = IPC_GET_ARG2(call);
1486 palkovsky 1230
			retval = 0;
1231
			break;
1232
		case FB_GET_RESOLUTION:
1233
			ipc_answer_fast(callid, 0, screen.xres,screen.yres);
1234
			continue;
1707 palkovsky 1235
		case FB_POINTER_MOVE:
1723 palkovsky 1236
			pointer_enabled = 1;
1711 palkovsky 1237
			mouse_move(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
1720 palkovsky 1238
			retval = 0;
1707 palkovsky 1239
			break;
1485 palkovsky 1240
		default:
1241
			retval = ENOENT;
1242
		}
1243
		ipc_answer_fast(callid,retval,0,0);
1363 vana 1244
	}
1485 palkovsky 1245
}
1363 vana 1246
 
1498 palkovsky 1247
/** Initialization of framebuffer */
1490 palkovsky 1248
int fb_init(void)
1485 palkovsky 1249
{
1501 palkovsky 1250
	void *fb_ph_addr;
1490 palkovsky 1251
	unsigned int fb_width;
1252
	unsigned int fb_height;
1253
	unsigned int fb_scanline;
1993 decky 1254
	unsigned int fb_visual;
1255
	bool fb_invert_colors;
1501 palkovsky 1256
	void *fb_addr;
1257
	size_t asz;
1363 vana 1258
 
1490 palkovsky 1259
	async_set_client_connection(fb_client_connection);
1363 vana 1260
 
1886 jermar 1261
	fb_ph_addr = (void *) sysinfo_value("fb.address.physical");
1262
	fb_width = sysinfo_value("fb.width");
1263
	fb_height = sysinfo_value("fb.height");
1264
	fb_scanline = sysinfo_value("fb.scanline");
1993 decky 1265
	fb_visual = sysinfo_value("fb.visual");
1886 jermar 1266
	fb_invert_colors = sysinfo_value("fb.invert-colors");
1490 palkovsky 1267
 
1993 decky 1268
	asz = fb_scanline * fb_height;
1501 palkovsky 1269
	fb_addr = as_get_mappable_page(asz);
1485 palkovsky 1270
 
1954 jermar 1271
	map_physmem(fb_ph_addr, fb_addr, ALIGN_UP(asz, PAGE_SIZE) >> PAGE_WIDTH,
1886 jermar 1272
		    AS_AREA_READ | AS_AREA_WRITE);
1273
 
1993 decky 1274
	if (screen_init(fb_addr, fb_width, fb_height, fb_scanline, fb_visual, fb_invert_colors))
1275
		return 0;
1276
 
1277
	return -1;
1363 vana 1278
}
1490 palkovsky 1279
 
1649 cejka 1280
/** 
1281
 * @}
1282
 */