Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
1363 vana 1
/*
2071 jermar 2
 * Copyright (c) 2006 Jakub Vana
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 */
2070 jermar 152
static void
153
rgb_byte0888(void *dst, int rgb)
1363 vana 154
{
1555 palkovsky 155
	*(int *)dst = rgb;
1363 vana 156
}
157
 
2070 jermar 158
static int
159
byte0888_rgb(void *src)
1363 vana 160
{
1555 palkovsky 161
	return (*(int *)src) & 0xffffff;
1363 vana 162
}
163
 
2070 jermar 164
static void
165
bgr_byte0888(void *dst, int rgb)
1994 decky 166
{
2025 jermar 167
	*((uint32_t *) dst) = BLUE(rgb, 8) << 16 | GREEN(rgb, 8) << 8 |
2619 jermar 168
	    RED(rgb, 8);
1994 decky 169
}
170
 
2070 jermar 171
static int
172
byte0888_bgr(void *src)
1994 decky 173
{
174
	int color = *(uint32_t *)(src);
2070 jermar 175
	return ((color & 0xff) << 16) | (((color >> 8) & 0xff) << 8) |
2619 jermar 176
	    ((color >> 16) & 0xff);
1994 decky 177
}
178
 
2070 jermar 179
static void
180
rgb_byte888(void *dst, int rgb)
1363 vana 181
{
1781 jermar 182
	uint8_t *scr = dst;
1871 jermar 183
#if defined(FB_INVERT_ENDIAN)
1555 palkovsky 184
	scr[0] = RED(rgb, 8);
185
	scr[1] = GREEN(rgb, 8);
186
	scr[2] = BLUE(rgb, 8);
1363 vana 187
#else
1555 palkovsky 188
	scr[2] = RED(rgb, 8);
189
	scr[1] = GREEN(rgb, 8);
190
	scr[0] = BLUE(rgb, 8);
1363 vana 191
#endif
192
}
193
 
2070 jermar 194
static int
195
byte888_rgb(void *src)
1363 vana 196
{
1781 jermar 197
	uint8_t *scr = src;
1871 jermar 198
#if defined(FB_INVERT_ENDIAN)
1555 palkovsky 199
	return scr[0] << 16 | scr[1] << 8 | scr[2];
1363 vana 200
#else
1555 palkovsky 201
	return scr[2] << 16 | scr[1] << 8 | scr[0];
202
#endif	
1363 vana 203
}
204
 
1993 decky 205
/**  16-bit depth (5:5:5) */
2070 jermar 206
static void
207
rgb_byte555(void *dst, int rgb)
1993 decky 208
{
209
	/* 5-bit, 5-bits, 5-bits */ 
2025 jermar 210
	*((uint16_t *)(dst)) = RED(rgb, 5) << 10 | GREEN(rgb, 5) << 5 |
2619 jermar 211
	    BLUE(rgb, 5);
1993 decky 212
}
213
 
214
/** 16-bit depth (5:5:5) */
2070 jermar 215
static int
216
byte555_rgb(void *src)
1993 decky 217
{
218
	int color = *(uint16_t *)(src);
2070 jermar 219
	return (((color >> 10) & 0x1f) << (16 + 3)) |
2619 jermar 220
	    (((color >> 5) & 0x1f) << (8 + 3)) | ((color & 0x1f) << 3);
1993 decky 221
}
222
 
1555 palkovsky 223
/**  16-bit depth (5:6:5) */
2070 jermar 224
static void
225
rgb_byte565(void *dst, int rgb)
1363 vana 226
{
227
	/* 5-bit, 6-bits, 5-bits */ 
2025 jermar 228
	*((uint16_t *)(dst)) = RED(rgb, 5) << 11 | GREEN(rgb, 6) << 5 |
2619 jermar 229
	    BLUE(rgb, 5);
1363 vana 230
}
231
 
1555 palkovsky 232
/** 16-bit depth (5:6:5) */
2070 jermar 233
static int
234
byte565_rgb(void *src)
1363 vana 235
{
1781 jermar 236
	int color = *(uint16_t *)(src);
2070 jermar 237
	return (((color >> 11) & 0x1f) << (16 + 3)) |
2619 jermar 238
	    (((color >> 5) & 0x3f) << (8 + 2)) | ((color & 0x1f) << 3);
1363 vana 239
}
240
 
241
/** Put pixel - 8-bit depth (3:2:3) */
2070 jermar 242
static void
243
rgb_byte8(void *dst, int rgb)
1363 vana 244
{
3692 rimsky 245
	*(uint8_t *)dst = 255 - (RED(rgb, 3) << 5 | GREEN(rgb, 2) << 3 |
246
		BLUE(rgb, 3));
1363 vana 247
}
248
 
249
/** Return pixel color - 8-bit depth (3:2:3) */
2070 jermar 250
static int
251
byte8_rgb(void *src)
1363 vana 252
{
3692 rimsky 253
	int color = 255 - (*(uint8_t *)src);
2070 jermar 254
	return (((color >> 5) & 0x7) << (16 + 5)) |
2619 jermar 255
	    (((color >> 3) & 0x3) << (8 + 6)) | ((color & 0x7) << 5);
1363 vana 256
}
257
 
1498 palkovsky 258
/** Put pixel into viewport 
259
 *
1709 jermar 260
 * @param vport Viewport identification
1498 palkovsky 261
 * @param x X coord relative to viewport
262
 * @param y Y coord relative to viewport
263
 * @param color RGB color 
264
 */
2070 jermar 265
static void
266
putpixel(viewport_t *vport, unsigned int x, unsigned int y, int color)
1485 palkovsky 267
{
1673 palkovsky 268
	int dx = vport->x + x;
269
	int dy = vport->y + y;
1672 palkovsky 270
 
1673 palkovsky 271
	if (! (vport->paused && vport->dbdata))
2070 jermar 272
		(*screen.rgb2scr)(&screen.fbaddress[POINTPOS(dx,dy)],
2619 jermar 273
		    COLOR(color));
1672 palkovsky 274
 
1673 palkovsky 275
	if (vport->dbdata) {
276
		int dline = (y + vport->dboffset) % vport->height;
277
		int doffset = screen.pixelbytes * (dline * vport->width + x);
1875 jermar 278
		(*screen.rgb2scr)(&vport->dbdata[doffset], COLOR(color));
1672 palkovsky 279
	}
1485 palkovsky 280
}
1672 palkovsky 281
 
1498 palkovsky 282
/** Get pixel from viewport */
2070 jermar 283
static int
284
getpixel(viewport_t *vport, unsigned int x, unsigned int y)
1485 palkovsky 285
{
1673 palkovsky 286
	int dx = vport->x + x;
287
	int dy = vport->y + y;
1555 palkovsky 288
 
2070 jermar 289
	return COLOR((*screen.scr2rgb)(&screen.fbaddress[POINTPOS(dx, dy)]));
1485 palkovsky 290
}
291
 
2070 jermar 292
static inline void
293
putpixel_mem(char *mem, unsigned int x, unsigned int y, int color)
1363 vana 294
{
2619 jermar 295
	(*screen.rgb2scr)(&mem[POINTPOS(x, y)], COLOR(color));
1363 vana 296
}
297
 
2070 jermar 298
static void
299
draw_rectangle(viewport_t *vport, unsigned int sx, unsigned int sy,
2025 jermar 300
	unsigned int width, unsigned int height, int color)
1559 palkovsky 301
{
302
	unsigned int x, y;
1668 palkovsky 303
	static void *tmpline;
1559 palkovsky 304
 
1668 palkovsky 305
	if (!tmpline)
2619 jermar 306
		tmpline = malloc(screen.scanline * screen.pixelbytes);
1668 palkovsky 307
 
1559 palkovsky 308
	/* Clear first line */
309
	for (x = 0; x < width; x++)
1668 palkovsky 310
		putpixel_mem(tmpline, x, 0, color);
1559 palkovsky 311
 
1673 palkovsky 312
	if (!vport->paused) {
1672 palkovsky 313
		/* Recompute to screen coords */
1673 palkovsky 314
		sx += vport->x;
315
		sy += vport->y;
1672 palkovsky 316
		/* Copy the rest */
317
		for (y = sy;y < sy+height; y++)
318
			memcpy(&screen.fbaddress[POINTPOS(sx,y)], tmpline, 
2619 jermar 319
			    screen.pixelbytes * width);
1672 palkovsky 320
	}
1673 palkovsky 321
	if (vport->dbdata) {
2025 jermar 322
		for (y = sy; y < sy + height; y++) {
1673 palkovsky 323
			int rline = (y + vport->dboffset) % vport->height;
2025 jermar 324
			int rpos = (rline * vport->width + sx) *
2619 jermar 325
			    screen.pixelbytes;
2025 jermar 326
			memcpy(&vport->dbdata[rpos], tmpline,
2619 jermar 327
			    screen.pixelbytes * width);
1672 palkovsky 328
		}
329
	}
1559 palkovsky 330
 
331
}
332
 
1485 palkovsky 333
/** Fill viewport with background color */
2070 jermar 334
static void
335
clear_port(viewport_t *vport)
1363 vana 336
{
2025 jermar 337
	draw_rectangle(vport, 0, 0, vport->width, vport->height,
2619 jermar 338
	    vport->style.bg_color);
1363 vana 339
}
340
 
1672 palkovsky 341
/** Scroll unbuffered viewport up/down
1485 palkovsky 342
 *
1709 jermar 343
 * @param vport Viewport to scroll
344
 * @param lines Positive number - scroll up, negative - scroll down
1485 palkovsky 345
 */
2070 jermar 346
static void
347
scroll_port_nodb(viewport_t *vport, int lines)
1363 vana 348
{
1485 palkovsky 349
	int y;
1672 palkovsky 350
 
351
	if (lines > 0) {
2025 jermar 352
		for (y = vport->y; y < vport->y+vport->height - lines; y++)
1559 palkovsky 353
			memcpy(&screen.fbaddress[POINTPOS(vport->x,y)],
2619 jermar 354
			    &screen.fbaddress[POINTPOS(vport->x,y + lines)],
355
			    screen.pixelbytes * vport->width);
2025 jermar 356
		draw_rectangle(vport, 0, vport->height - lines, vport->width,
2619 jermar 357
		    lines, vport->style.bg_color);
1672 palkovsky 358
	} else if (lines < 0) {
359
		lines = -lines;
2619 jermar 360
		for (y = vport->y + vport->height-1; y >= vport->y + lines; y--)
1559 palkovsky 361
			memcpy(&screen.fbaddress[POINTPOS(vport->x,y)],
2619 jermar 362
			    &screen.fbaddress[POINTPOS(vport->x,y - lines)],
363
			    screen.pixelbytes * vport->width);
2025 jermar 364
		draw_rectangle(vport, 0, 0, vport->width, lines,
2619 jermar 365
		    vport->style.bg_color);
1485 palkovsky 366
	}
1363 vana 367
}
368
 
1672 palkovsky 369
/** Refresh given viewport from double buffer */
2070 jermar 370
static void
371
refresh_viewport_db(viewport_t *vport)
1672 palkovsky 372
{
373
	unsigned int y, srcy, srcoff, dsty, dstx;
374
 
1673 palkovsky 375
	for (y = 0; y < vport->height; y++) {
376
		srcy = (y + vport->dboffset) % vport->height;
377
		srcoff = (vport->width * srcy) * screen.pixelbytes;
1672 palkovsky 378
 
1673 palkovsky 379
		dstx = vport->x;
380
		dsty = vport->y + y;
1672 palkovsky 381
 
382
		memcpy(&screen.fbaddress[POINTPOS(dstx,dsty)],
2619 jermar 383
		    &vport->dbdata[srcoff], vport->width * screen.pixelbytes);
1672 palkovsky 384
	}
385
}
386
 
387
/** Scroll viewport that has double buffering enabled */
2070 jermar 388
static void
389
scroll_port_db(viewport_t *vport, int lines)
1672 palkovsky 390
{
1673 palkovsky 391
	++vport->paused;
1672 palkovsky 392
	if (lines > 0) {
1673 palkovsky 393
		draw_rectangle(vport, 0, 0, vport->width, lines,
2619 jermar 394
		    vport->style.bg_color);
1673 palkovsky 395
		vport->dboffset += lines;
396
		vport->dboffset %= vport->height;
1672 palkovsky 397
	} else if (lines < 0) {
398
		lines = -lines;
2619 jermar 399
		draw_rectangle(vport, 0, vport->height-lines, vport->width,
400
		    lines, vport->style.bg_color);
1672 palkovsky 401
 
1673 palkovsky 402
		if (vport->dboffset < lines)
403
			vport->dboffset += vport->height;
404
		vport->dboffset -= lines;
1672 palkovsky 405
	}
406
 
1673 palkovsky 407
	--vport->paused;
1672 palkovsky 408
 
1673 palkovsky 409
	refresh_viewport_db(vport);
1672 palkovsky 410
}
411
 
412
/** Scrolls viewport given number of lines */
2070 jermar 413
static void
414
scroll_port(viewport_t *vport, int lines)
1672 palkovsky 415
{
1673 palkovsky 416
	if (vport->dbdata)
417
		scroll_port_db(vport, lines);
1672 palkovsky 418
	else
1673 palkovsky 419
		scroll_port_nodb(vport, lines);
1672 palkovsky 420
 
421
}
422
 
2070 jermar 423
static void
424
invert_pixel(viewport_t *vport, unsigned int x, unsigned int y)
1363 vana 425
{
1673 palkovsky 426
	putpixel(vport, x, y, ~getpixel(vport, x, y));
1363 vana 427
}
428
 
429
 
430
/***************************************************************/
431
/* Character-console functions */
432
 
1558 palkovsky 433
/** Draw character at given position
434
 *
1709 jermar 435
 * @param vport Viewport where the character is printed
1558 palkovsky 436
 * @param sx Coordinates of top-left of the character
437
 * @param sy Coordinates of top-left of the character
438
 * @param style Color of the character
439
 * @param transparent If false, print background color
440
 */
2070 jermar 441
static void
2619 jermar 442
draw_glyph(viewport_t *vport,uint8_t glyph, unsigned int sx, unsigned int sy,
443
    style_t style, int transparent)
1363 vana 444
{
1509 palkovsky 445
	int i;
1363 vana 446
	unsigned int y;
1509 palkovsky 447
	unsigned int glline;
1363 vana 448
 
1509 palkovsky 449
	for (y = 0; y < FONT_SCANLINES; y++) {
450
		glline = fb_font[glyph * FONT_SCANLINES + y];
451
		for (i = 0; i < 8; i++) {
452
			if (glline & (1 << (7 - i)))
2619 jermar 453
				putpixel(vport, sx + i, sy + y, style.fg_color);
1558 palkovsky 454
			else if (!transparent)
2619 jermar 455
				putpixel(vport, sx + i, sy + y, style.bg_color);
1509 palkovsky 456
		}
457
	}
1363 vana 458
}
459
 
460
/** Invert character at given position */
2070 jermar 461
static void
462
invert_char(viewport_t *vport,unsigned int row, unsigned int col)
1363 vana 463
{
464
	unsigned int x;
465
	unsigned int y;
466
 
467
	for (x = 0; x < COL_WIDTH; x++)
468
		for (y = 0; y < FONT_SCANLINES; y++)
2025 jermar 469
			invert_pixel(vport, col * COL_WIDTH + x, row *
2619 jermar 470
			    FONT_SCANLINES + y);
1363 vana 471
}
472
 
473
/***************************************************************/
474
/* Stdout specific functions */
475
 
476
 
1485 palkovsky 477
/** Create new viewport
1363 vana 478
 *
1485 palkovsky 479
 * @return New viewport number
1363 vana 480
 */
2070 jermar 481
static int
482
viewport_create(unsigned int x, unsigned int y,unsigned int width, 
2025 jermar 483
	unsigned int height)
1363 vana 484
{
1485 palkovsky 485
	int i;
486
 
2070 jermar 487
	for (i = 0; i < MAX_VIEWPORTS; i++) {
1485 palkovsky 488
		if (!viewports[i].initialized)
1363 vana 489
			break;
490
	}
1485 palkovsky 491
	if (i == MAX_VIEWPORTS)
492
		return ELIMIT;
493
 
494
	viewports[i].x = x;
495
	viewports[i].y = y;
496
	viewports[i].width = width;
497
	viewports[i].height = height;
1363 vana 498
 
1485 palkovsky 499
	viewports[i].rows = height / FONT_SCANLINES;
500
	viewports[i].cols = width / COL_WIDTH;
501
 
1509 palkovsky 502
	viewports[i].style.bg_color = DEFAULT_BGCOLOR;
503
	viewports[i].style.fg_color = DEFAULT_FGCOLOR;
1363 vana 504
 
1489 palkovsky 505
	viewports[i].cur_col = 0;
506
	viewports[i].cur_row = 0;
507
	viewports[i].cursor_active = 0;
508
 
1486 palkovsky 509
	viewports[i].initialized = 1;
510
 
1485 palkovsky 511
	return i;
1363 vana 512
}
513
 
514
/** Initialize framebuffer as a chardev output device
515
 *
1993 decky 516
 * @param addr          Address of theframebuffer
517
 * @param xres          Screen width in pixels
518
 * @param yres          Screen height in pixels
519
 * @param visual        Bits per pixel (8, 16, 24, 32)
520
 * @param scan          Bytes per one scanline
1875 jermar 521
 * @param invert_colors Inverted colors.
1363 vana 522
 *
523
 */
2070 jermar 524
static bool
3679 rimsky 525
screen_init(void *addr, unsigned int offset, unsigned int xres,
526
	unsigned int yres, unsigned int scan, unsigned int visual,
527
	bool invert_colors)
1363 vana 528
{
1993 decky 529
	switch (visual) {
530
	case VISUAL_INDIRECT_8:
531
		screen.rgb2scr = rgb_byte8;
532
		screen.scr2rgb = byte8_rgb;
1886 jermar 533
		screen.pixelbytes = 1;
534
		break;
1993 decky 535
	case VISUAL_RGB_5_5_5:
536
		screen.rgb2scr = rgb_byte555;
537
		screen.scr2rgb = byte555_rgb;
1886 jermar 538
		screen.pixelbytes = 2;
539
		break;
1993 decky 540
	case VISUAL_RGB_5_6_5:
541
		screen.rgb2scr = rgb_byte565;
542
		screen.scr2rgb = byte565_rgb;
543
		screen.pixelbytes = 2;
1886 jermar 544
		break;
1993 decky 545
	case VISUAL_RGB_8_8_8:
546
		screen.rgb2scr = rgb_byte888;
547
		screen.scr2rgb = byte888_rgb;
548
		screen.pixelbytes = 3;
549
		break;
550
	case VISUAL_RGB_8_8_8_0:
551
		screen.rgb2scr = rgb_byte888;
552
		screen.scr2rgb = byte888_rgb;
1886 jermar 553
		screen.pixelbytes = 4;
554
		break;
1993 decky 555
	case VISUAL_RGB_0_8_8_8:
556
		screen.rgb2scr = rgb_byte0888;
557
		screen.scr2rgb = byte0888_rgb;
558
		screen.pixelbytes = 4;
559
		break;
1994 decky 560
	case VISUAL_BGR_0_8_8_8:
561
		screen.rgb2scr = bgr_byte0888;
562
		screen.scr2rgb = byte0888_bgr;
563
		screen.pixelbytes = 4;
564
		break;
1993 decky 565
	default:
566
		return false;
1363 vana 567
	}
568
 
3692 rimsky 569
	screen.fbaddress = (unsigned char *) (((uintptr_t) addr));
1485 palkovsky 570
	screen.xres = xres;
571
	screen.yres = yres;
572
	screen.scanline = scan;
1875 jermar 573
	screen.invert_colors = invert_colors;
1363 vana 574
 
1485 palkovsky 575
	/* Create first viewport */
1993 decky 576
	viewport_create(0, 0, xres, yres);
577
 
578
	return true;
1485 palkovsky 579
}
1363 vana 580
 
1552 palkovsky 581
/** Hide cursor if it is shown */
2070 jermar 582
static void
583
cursor_hide(viewport_t *vport)
1500 palkovsky 584
{
585
	if (vport->cursor_active && vport->cursor_shown) {
1673 palkovsky 586
		invert_char(vport, vport->cur_row, vport->cur_col);
1500 palkovsky 587
		vport->cursor_shown = 0;
588
	}
589
}
590
 
1552 palkovsky 591
/** Show cursor if cursor showing is enabled */
2070 jermar 592
static void
593
cursor_print(viewport_t *vport)
1500 palkovsky 594
{
595
	/* Do not check for cursor_shown */
596
	if (vport->cursor_active) {
1673 palkovsky 597
		invert_char(vport, vport->cur_row, vport->cur_col);
1500 palkovsky 598
		vport->cursor_shown = 1;
599
	}
600
}
601
 
1552 palkovsky 602
/** Invert cursor, if it is enabled */
2070 jermar 603
static void
604
cursor_blink(viewport_t *vport)
1500 palkovsky 605
{
606
	if (vport->cursor_shown)
1673 palkovsky 607
		cursor_hide(vport);
1500 palkovsky 608
	else
1673 palkovsky 609
		cursor_print(vport);
1500 palkovsky 610
}
611
 
1498 palkovsky 612
/** Draw character at given position relative to viewport 
613
 * 
1709 jermar 614
 * @param vport Viewport identification
1498 palkovsky 615
 * @param c Character to print
616
 * @param row Screen position relative to viewport
617
 * @param col Screen position relative to viewport
1558 palkovsky 618
 * @param transparent If false, print background color with character
1498 palkovsky 619
 */
2070 jermar 620
static void
621
draw_char(viewport_t *vport, char c, unsigned int row, unsigned int col,
622
	style_t style, int transparent)
1486 palkovsky 623
{
1500 palkovsky 624
	/* Optimize - do not hide cursor if we are going to overwrite it */
625
	if (vport->cursor_active && vport->cursor_shown && 
626
	    (vport->cur_col != col || vport->cur_row != row))
1673 palkovsky 627
		invert_char(vport, vport->cur_row, vport->cur_col);
1486 palkovsky 628
 
2025 jermar 629
	draw_glyph(vport, c, col * COL_WIDTH, row * FONT_SCANLINES, style,
2619 jermar 630
	    transparent);
1489 palkovsky 631
 
632
	vport->cur_col = col;
633
	vport->cur_row = row;
634
 
635
	vport->cur_col++;
2025 jermar 636
	if (vport->cur_col >= vport->cols) {
1489 palkovsky 637
		vport->cur_col = 0;
638
		vport->cur_row++;
639
		if (vport->cur_row >= vport->rows)
640
			vport->cur_row--;
1486 palkovsky 641
	}
1673 palkovsky 642
	cursor_print(vport);
1486 palkovsky 643
}
644
 
1552 palkovsky 645
/** Draw text data to viewport
646
 *
1709 jermar 647
 * @param vport Viewport id
1552 palkovsky 648
 * @param data Text data fitting exactly into viewport
649
 */
2070 jermar 650
static void
651
draw_text_data(viewport_t *vport, keyfield_t *data)
1505 palkovsky 652
{
653
	int i;
1515 palkovsky 654
	int col,row;
1505 palkovsky 655
 
1673 palkovsky 656
	clear_port(vport);
2025 jermar 657
	for (i = 0; i < vport->cols * vport->rows; i++) {
658
		if (data[i].character == ' ' && style_same(data[i].style,
2619 jermar 659
		    vport->style))
1505 palkovsky 660
			continue;
1515 palkovsky 661
		col = i % vport->cols;
662
		row = i / vport->cols;
2025 jermar 663
		draw_glyph(vport, data[i].character, col * COL_WIDTH, row *
2619 jermar 664
		    FONT_SCANLINES, data[i].style, style_same(data[i].style,
665
		    vport->style));
1505 palkovsky 666
	}
1673 palkovsky 667
	cursor_print(vport);
1505 palkovsky 668
}
669
 
1555 palkovsky 670
/** Return first free pixmap */
2070 jermar 671
static int
672
find_free_pixmap(void)
1555 palkovsky 673
{
674
	int i;
675
 
2070 jermar 676
	for (i = 0;i < MAX_PIXMAPS;i++)
1555 palkovsky 677
		if (!pixmaps[i].data)
678
			return i;
679
	return -1;
680
}
681
 
2070 jermar 682
static void
683
putpixel_pixmap(int pm, unsigned int x, unsigned int y, int color)
1555 palkovsky 684
{
685
	pixmap_t *pmap = &pixmaps[pm];
686
	int pos = (y * pmap->width + x) * screen.pixelbytes;
687
 
1875 jermar 688
	(*screen.rgb2scr)(&pmap->data[pos],COLOR(color));
1555 palkovsky 689
}
690
 
691
/** Create a new pixmap and return appropriate ID */
2070 jermar 692
static int
693
shm2pixmap(unsigned char *shm, size_t size)
1555 palkovsky 694
{
695
	int pm;
696
	pixmap_t *pmap;
697
 
698
	pm = find_free_pixmap();
699
	if (pm == -1)
700
		return ELIMIT;
701
	pmap = &pixmaps[pm];
702
 
703
	if (ppm_get_data(shm, size, &pmap->width, &pmap->height))
704
		return EINVAL;
705
 
706
	pmap->data = malloc(pmap->width * pmap->height * screen.pixelbytes);
707
	if (!pmap->data)
708
		return ENOMEM;
709
 
710
	ppm_draw(shm, size, 0, 0, pmap->width, pmap->height, 
2619 jermar 711
	    (putpixel_cb_t)putpixel_pixmap, (void *)pm);
1555 palkovsky 712
 
713
	return pm;
714
}
715
 
1552 palkovsky 716
/** Handle shared memory communication calls
717
 *
718
 * Protocol for drawing pixmaps:
719
 * - FB_PREPARE_SHM(client shm identification)
2025 jermar 720
 * - IPC_M_AS_AREA_SEND
1552 palkovsky 721
 * - FB_DRAW_PPM(startx,starty)
722
 * - FB_DROP_SHM
723
 *
724
 * Protocol for text drawing
2025 jermar 725
 * - IPC_M_AS_AREA_SEND
1552 palkovsky 726
 * - FB_DRAW_TEXT_DATA
727
 *
728
 * @param callid Callid of the current call
729
 * @param call Current call data
730
 * @param vp Active viewport
731
 * @return 0 if the call was not handled byt this function, 1 otherwise
732
 *
733
 * note: this function is not threads safe, you would have
734
 * to redefine static variables with __thread
735
 */
2070 jermar 736
static int
737
shm_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
1547 palkovsky 738
{
739
	static keyfield_t *interbuffer = NULL;
740
	static size_t intersize = 0;
1505 palkovsky 741
 
1720 palkovsky 742
	static unsigned char *shm = NULL;
1555 palkovsky 743
	static ipcarg_t shm_id = 0;
744
	static size_t shm_size;
1547 palkovsky 745
 
746
	int handled = 1;
747
	int retval = 0;
748
	viewport_t *vport = &viewports[vp];
2070 jermar 749
	unsigned int x, y;
1547 palkovsky 750
 
751
	switch (IPC_GET_METHOD(*call)) {
2677 jermar 752
	case IPC_M_SHARE_OUT:
1547 palkovsky 753
		/* We accept one area for data interchange */
1555 palkovsky 754
		if (IPC_GET_ARG1(*call) == shm_id) {
2141 jermar 755
			void *dest = as_get_mappable_page(IPC_GET_ARG2(*call));
1555 palkovsky 756
			shm_size = IPC_GET_ARG2(*call);
2619 jermar 757
			if (!ipc_answer_1(callid, EOK, (sysarg_t) dest)) 
1555 palkovsky 758
				shm = dest;
1547 palkovsky 759
			else
1555 palkovsky 760
				shm_id = 0;
761
			if (shm[0] != 'P')
1547 palkovsky 762
				while (1)
763
					;
764
			return 1;
765
		} else {
766
			intersize = IPC_GET_ARG2(*call);
2015 jermar 767
			receive_comm_area(callid, call, (void *) &interbuffer);
1547 palkovsky 768
		}
769
		return 1;
770
	case FB_PREPARE_SHM:
1555 palkovsky 771
		if (shm_id)
1547 palkovsky 772
			retval = EBUSY;
773
		else 
1555 palkovsky 774
			shm_id = IPC_GET_ARG1(*call);
1547 palkovsky 775
		break;
776
 
777
	case FB_DROP_SHM:
1555 palkovsky 778
		if (shm) {
779
			as_area_destroy(shm);
780
			shm = NULL;
1547 palkovsky 781
		}
1555 palkovsky 782
		shm_id = 0;
1547 palkovsky 783
		break;
1555 palkovsky 784
 
785
	case FB_SHM2PIXMAP:
786
		if (!shm) {
787
			retval = EINVAL;
788
			break;
789
		}
790
		retval = shm2pixmap(shm, shm_size);
791
		break;
1547 palkovsky 792
	case FB_DRAW_PPM:
1555 palkovsky 793
		if (!shm) {
1547 palkovsky 794
			retval = EINVAL;
795
			break;
796
		}
797
		x = IPC_GET_ARG1(*call);
798
		y = IPC_GET_ARG2(*call);
799
		if (x > vport->width || y > vport->height) {
800
			retval = EINVAL;
801
			break;
802
		}
803
 
2025 jermar 804
		ppm_draw(shm, shm_size, IPC_GET_ARG1(*call),
2619 jermar 805
		    IPC_GET_ARG2(*call), vport->width - x, vport->height - y,
806
		    (putpixel_cb_t)putpixel, vport);
1547 palkovsky 807
		break;
808
	case FB_DRAW_TEXT_DATA:
809
		if (!interbuffer) {
810
			retval = EINVAL;
811
			break;
812
		}
2025 jermar 813
		if (intersize < vport->cols * vport->rows *
2619 jermar 814
		    sizeof(*interbuffer)) {
1547 palkovsky 815
			retval = EINVAL;
816
			break;
817
		}
1673 palkovsky 818
		draw_text_data(vport, interbuffer);
1547 palkovsky 819
		break;
820
	default:
821
		handled = 0;
822
	}
823
 
824
	if (handled)
2619 jermar 825
		ipc_answer_0(callid, retval);
1547 palkovsky 826
	return handled;
827
}
828
 
2070 jermar 829
static void
830
copy_vp_to_pixmap(viewport_t *vport, pixmap_t *pmap)
1552 palkovsky 831
{
1720 palkovsky 832
	int y;
2301 decky 833
	int tmp, srcrowsize;
834
	int realwidth, realheight, realrowsize;
1711 palkovsky 835
	int width = vport->width;
836
	int height = vport->height;
1552 palkovsky 837
 
1711 palkovsky 838
	if (width + vport->x > screen.xres)
839
		width = screen.xres - vport->x;
2301 decky 840
	if (height + vport->y > screen.yres)
1711 palkovsky 841
		height = screen.yres - vport->y;
2301 decky 842
 
843
	realwidth = pmap->width <= width ? pmap->width : width;
844
	realheight = pmap->height <= height ? pmap->height : height;
1711 palkovsky 845
 
2301 decky 846
	srcrowsize = vport->width * screen.pixelbytes;
847
	realrowsize = realwidth * screen.pixelbytes;
848
 
849
	for (y = 0; y < realheight; y++) {
2070 jermar 850
		tmp = (vport->y + y) * screen.scanline +
2619 jermar 851
		    vport->x * screen.pixelbytes;
2301 decky 852
		memcpy(pmap->data + srcrowsize * y, screen.fbaddress + tmp,
2619 jermar 853
		    realrowsize); 
1711 palkovsky 854
	}
855
}
856
 
857
/** Save viewport to pixmap */
2070 jermar 858
static int
859
save_vp_to_pixmap(viewport_t *vport)
1711 palkovsky 860
{
861
	int pm;
862
	pixmap_t *pmap;
863
 
1552 palkovsky 864
	pm = find_free_pixmap();
865
	if (pm == -1)
866
		return ELIMIT;
867
 
868
	pmap = &pixmaps[pm];
869
	pmap->data = malloc(screen.pixelbytes * vport->width * vport->height);
870
	if (!pmap->data)
871
		return ENOMEM;
872
 
873
	pmap->width = vport->width;
874
	pmap->height = vport->height;
1711 palkovsky 875
 
876
	copy_vp_to_pixmap(vport, pmap);
1552 palkovsky 877
 
878
	return pm;
879
}
880
 
881
/** Draw pixmap on screen
882
 *
883
 * @param vp Viewport to draw on
884
 * @param pm Pixmap identifier
885
 */
886
static int draw_pixmap(int vp, int pm)
887
{
888
	pixmap_t *pmap = &pixmaps[pm];
889
	viewport_t *vport = &viewports[vp];
1720 palkovsky 890
	int y;
1552 palkovsky 891
	int tmp, srcrowsize;
892
	int realwidth, realheight, realrowsize;
1711 palkovsky 893
	int width = vport->width;
894
	int height = vport->height;
1552 palkovsky 895
 
1711 palkovsky 896
	if (width + vport->x > screen.xres)
897
		width = screen.xres - vport->x;
898
	if (height + vport->y > screen.yres)
899
		height = screen.yres - vport->y;
900
 
1552 palkovsky 901
	if (!pmap->data)
902
		return EINVAL;
903
 
1711 palkovsky 904
	realwidth = pmap->width <= width ? pmap->width : width;
905
	realheight = pmap->height <= height ? pmap->height : height;
1552 palkovsky 906
 
907
	srcrowsize = vport->width * screen.pixelbytes;
908
	realrowsize = realwidth * screen.pixelbytes;
909
 
2070 jermar 910
	for (y = 0; y < realheight; y++) {
911
		tmp = (vport->y + y) * screen.scanline + 
2619 jermar 912
		    vport->x * screen.pixelbytes;
2025 jermar 913
		memcpy(screen.fbaddress + tmp, pmap->data + y * srcrowsize,
2619 jermar 914
		    realrowsize);
1552 palkovsky 915
	}
1567 palkovsky 916
	return 0;
1552 palkovsky 917
}
918
 
1646 palkovsky 919
/** Tick animation one step forward */
2070 jermar 920
static void
921
anims_tick(void)
1646 palkovsky 922
{
923
	int i;
924
	static int counts = 0;
925
 
926
	/* Limit redrawing */
2025 jermar 927
	counts = (counts + 1) % 8;
1646 palkovsky 928
	if (counts)
929
		return;
930
 
2070 jermar 931
	for (i = 0; i < MAX_ANIMATIONS; i++) {
2025 jermar 932
		if (!animations[i].animlen || !animations[i].initialized ||
2619 jermar 933
		    !animations[i].enabled)
1646 palkovsky 934
			continue;
2025 jermar 935
		draw_pixmap(animations[i].vp,
2619 jermar 936
		    animations[i].pixmaps[animations[i].pos]);
2025 jermar 937
		animations[i].pos = (animations[i].pos + 1) %
2619 jermar 938
		    animations[i].animlen;
1646 palkovsky 939
	}
940
}
941
 
1711 palkovsky 942
 
943
static int pointer_x, pointer_y;
1723 palkovsky 944
static int pointer_shown, pointer_enabled;
1711 palkovsky 945
static int pointer_vport = -1;
946
static int pointer_pixmap = -1;
947
 
2070 jermar 948
static void
949
mouse_show(void)
1711 palkovsky 950
{
2070 jermar 951
	int i, j;
1711 palkovsky 952
	int visibility;
953
	int color;
954
	int bytepos;
955
 
1723 palkovsky 956
	if (pointer_shown || !pointer_enabled)
957
		return;
958
 
1711 palkovsky 959
	/* Save image under the cursor */
960
	if (pointer_vport == -1) {
2025 jermar 961
		pointer_vport = viewport_create(pointer_x, pointer_y,
2619 jermar 962
		    pointer_width, pointer_height);
1711 palkovsky 963
		if (pointer_vport < 0)
964
			return;
965
	} else {
966
		viewports[pointer_vport].x = pointer_x;
967
		viewports[pointer_vport].y = pointer_y;
968
	}
969
 
970
	if (pointer_pixmap == -1)
971
		pointer_pixmap = save_vp_to_pixmap(&viewports[pointer_vport]);
972
	else
2025 jermar 973
		copy_vp_to_pixmap(&viewports[pointer_vport],
2619 jermar 974
		    &pixmaps[pointer_pixmap]);
1711 palkovsky 975
 
976
	/* Draw cursor */
2025 jermar 977
	for (i = 0; i < pointer_height; i++)
978
		for (j = 0; j < pointer_width; j++) {
979
			bytepos = i * ((pointer_width - 1) / 8 + 1) + j / 8;
2070 jermar 980
			visibility = pointer_mask_bits[bytepos] &
2619 jermar 981
			    (1 << (j % 8));
1711 palkovsky 982
			if (visibility) {
2619 jermar 983
				color = pointer_bits[bytepos] &
984
				    (1 << (j % 8)) ? 0 : 0xffffff;
2025 jermar 985
				if (pointer_x + j < screen.xres && pointer_y +
2619 jermar 986
				    i < screen.yres)
2025 jermar 987
					putpixel(&viewports[0], pointer_x + j,
2619 jermar 988
					    pointer_y + i, color);
1711 palkovsky 989
			}
990
		}
991
	pointer_shown = 1;
992
}
993
 
2070 jermar 994
static void
995
mouse_hide(void)
1711 palkovsky 996
{
997
	/* Restore image under the cursor */
998
	if (pointer_shown) {
999
		draw_pixmap(pointer_vport, pointer_pixmap);
1000
		pointer_shown = 0;
1001
	}
1002
}
1003
 
2070 jermar 1004
static void
1005
mouse_move(unsigned int x, unsigned int y)
1711 palkovsky 1006
{
1007
	mouse_hide();
1008
	pointer_x = x;
1009
	pointer_y = y;
1010
	mouse_show();
1011
}
1012
 
2070 jermar 1013
static int
1014
anim_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
1646 palkovsky 1015
{
1016
	int handled = 1;
1017
	int retval = 0;
1018
	int i,nvp;
1019
	int newval;
1020
 
1021
	switch (IPC_GET_METHOD(*call)) {
1022
	case FB_ANIM_CREATE:
1023
		nvp = IPC_GET_ARG1(*call);
1024
		if (nvp == -1)
1025
			nvp = vp;
2025 jermar 1026
		if (nvp >= MAX_VIEWPORTS || nvp < 0 ||
1027
			!viewports[nvp].initialized) {
1646 palkovsky 1028
			retval = EINVAL;
1029
			break;
1030
		}
2025 jermar 1031
		for (i = 0; i < MAX_ANIMATIONS; i++) {
1032
			if (!animations[i].initialized)
1646 palkovsky 1033
				break;
1034
		}
1035
		if (i == MAX_ANIMATIONS) {
1036
			retval = ELIMIT;
1037
			break;
1038
		}
1039
		animations[i].initialized = 1;
1040
		animations[i].animlen = 0;
1041
		animations[i].pos = 0;
1042
		animations[i].enabled = 0;
1043
		animations[i].vp = nvp;
1044
		retval = i;
1045
		break;
1046
	case FB_ANIM_DROP:
1047
		i = IPC_GET_ARG1(*call);
1720 palkovsky 1048
		if (i >= MAX_ANIMATIONS || i < 0) {
1646 palkovsky 1049
			retval = EINVAL;
1050
			break;
1051
		}
1052
		animations[i].initialized = 0;
1053
		break;
1054
	case FB_ANIM_ADDPIXMAP:
1055
		i = IPC_GET_ARG1(*call);
2025 jermar 1056
		if (i >= MAX_ANIMATIONS || i < 0 ||
1057
			!animations[i].initialized) {
1646 palkovsky 1058
			retval = EINVAL;
1059
			break;
1060
		}
1061
		if (animations[i].animlen == MAX_ANIM_LEN) {
1062
			retval = ELIMIT;
1063
			break;
1064
		}
1065
		newval = IPC_GET_ARG2(*call);
2025 jermar 1066
		if (newval < 0 || newval > MAX_PIXMAPS ||
1067
			!pixmaps[newval].data) {
1646 palkovsky 1068
			retval = EINVAL;
1069
			break;
1070
		}
1071
		animations[i].pixmaps[animations[i].animlen++] = newval;
1072
		break;
1073
	case FB_ANIM_CHGVP:
1074
		i = IPC_GET_ARG1(*call);
1075
		if (i >= MAX_ANIMATIONS || i < 0) {
1076
			retval = EINVAL;
1077
			break;
1078
		}
1079
		nvp = IPC_GET_ARG2(*call);
1080
		if (nvp == -1)
1081
			nvp = vp;
2025 jermar 1082
		if (nvp >= MAX_VIEWPORTS || nvp < 0 ||
1083
			!viewports[nvp].initialized) {
1646 palkovsky 1084
			retval = EINVAL;
1085
			break;
1086
		}
1087
		animations[i].vp = nvp;
1088
		break;
1089
	case FB_ANIM_START:
1090
	case FB_ANIM_STOP:
1091
		i = IPC_GET_ARG1(*call);
1092
		if (i >= MAX_ANIMATIONS || i < 0) {
1093
			retval = EINVAL;
1094
			break;
1095
		}
1096
		newval = (IPC_GET_METHOD(*call) == FB_ANIM_START);
1097
		if (newval ^ animations[i].enabled) {
1098
			animations[i].enabled = newval;
1099
			anims_enabled += newval ? 1 : -1;
1100
		}
1101
		break;
1102
	default:
1103
		handled = 0;
1104
	}
1105
	if (handled)
2619 jermar 1106
		ipc_answer_0(callid, retval);
1646 palkovsky 1107
	return handled;
1108
}
1109
 
1552 palkovsky 1110
/** Handler for messages concerning pixmap handling */
2070 jermar 1111
static int
1112
pixmap_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
1552 palkovsky 1113
{
1114
	int handled = 1;
1115
	int retval = 0;
1116
	int i,nvp;
1117
 
1118
	switch (IPC_GET_METHOD(*call)) {
1119
	case FB_VP_DRAW_PIXMAP:
1120
		nvp = IPC_GET_ARG1(*call);
1121
		if (nvp == -1)
1122
			nvp = vp;
2025 jermar 1123
		if (nvp < 0 || nvp >= MAX_VIEWPORTS ||
1124
			!viewports[nvp].initialized) {
1552 palkovsky 1125
			retval = EINVAL;
1126
			break;
1127
		}
1128
		i = IPC_GET_ARG2(*call);
1129
		retval = draw_pixmap(nvp, i);
1130
		break;
1131
	case FB_VP2PIXMAP:
1132
		nvp = IPC_GET_ARG1(*call);
1133
		if (nvp == -1)
1134
			nvp = vp;
2025 jermar 1135
		if (nvp < 0 || nvp >= MAX_VIEWPORTS ||
1136
			!viewports[nvp].initialized)
1552 palkovsky 1137
			retval = EINVAL;
1138
		else
1711 palkovsky 1139
			retval = save_vp_to_pixmap(&viewports[nvp]);
1552 palkovsky 1140
		break;
1141
	case FB_DROP_PIXMAP:
1142
		i = IPC_GET_ARG1(*call);
1143
		if (i >= MAX_PIXMAPS) {
1144
			retval = EINVAL;
1145
			break;
1146
		}
1147
		if (pixmaps[i].data) {
1148
			free(pixmaps[i].data);
1149
			pixmaps[i].data = NULL;
1150
		}
1151
		break;
1152
	default:
1153
		handled = 0;
1154
	}
1155
 
1156
	if (handled)
2619 jermar 1157
		ipc_answer_0(callid, retval);
1552 palkovsky 1158
	return handled;
1159
 
1160
}
1161
 
1498 palkovsky 1162
/** Function for handling connections to FB
1163
 *
1164
 */
2070 jermar 1165
static void
1166
fb_client_connection(ipc_callid_t iid, ipc_call_t *icall)
1363 vana 1167
{
1485 palkovsky 1168
	ipc_callid_t callid;
1169
	ipc_call_t call;
1170
	int retval;
1171
	int i;
1172
	unsigned int row,col;
1173
	char c;
1486 palkovsky 1174
 
1485 palkovsky 1175
	int vp = 0;
1486 palkovsky 1176
	viewport_t *vport = &viewports[0];
1363 vana 1177
 
1485 palkovsky 1178
	if (client_connected) {
2619 jermar 1179
		ipc_answer_0(iid, ELIMIT);
1485 palkovsky 1180
		return;
1181
	}
1486 palkovsky 1182
	client_connected = 1;
2619 jermar 1183
	ipc_answer_0(iid, EOK); /* Accept connection */
1363 vana 1184
 
1485 palkovsky 1185
	while (1) {
1646 palkovsky 1186
		if (vport->cursor_active || anims_enabled)
2070 jermar 1187
			callid = async_get_call_timeout(&call, 250000);
1640 palkovsky 1188
		else
1189
			callid = async_get_call(&call);
1190
 
1713 palkovsky 1191
		mouse_hide();
1500 palkovsky 1192
		if (!callid) {
1673 palkovsky 1193
			cursor_blink(vport);
1646 palkovsky 1194
			anims_tick();
1723 palkovsky 1195
			mouse_show();
1500 palkovsky 1196
			continue;
1197
		}
1547 palkovsky 1198
		if (shm_handle(callid, &call, vp))
1199
			continue;
1552 palkovsky 1200
		if (pixmap_handle(callid, &call, vp))
1201
			continue;
1646 palkovsky 1202
		if (anim_handle(callid, &call, vp))
1203
			continue;
1547 palkovsky 1204
 
1485 palkovsky 1205
 		switch (IPC_GET_METHOD(call)) {
1206
		case IPC_M_PHONE_HUNGUP:
1207
			client_connected = 0;
1208
			/* cleanup other viewports */
2025 jermar 1209
			for (i = 1; i < MAX_VIEWPORTS; i++)
1486 palkovsky 1210
				vport->initialized = 0;
1485 palkovsky 1211
			return; /* Exit thread */
1505 palkovsky 1212
 
1485 palkovsky 1213
		case FB_PUTCHAR:
1558 palkovsky 1214
		case FB_TRANS_PUTCHAR:
1485 palkovsky 1215
			c = IPC_GET_ARG1(call);
1216
			row = IPC_GET_ARG2(call);
1217
			col = IPC_GET_ARG3(call);
1486 palkovsky 1218
			if (row >= vport->rows || col >= vport->cols) {
1485 palkovsky 1219
				retval = EINVAL;
1220
				break;
1221
			}
2619 jermar 1222
			ipc_answer_0(callid, EOK);
1486 palkovsky 1223
 
2025 jermar 1224
			draw_char(vport, c, row, col, vport->style,
2619 jermar 1225
			    IPC_GET_METHOD(call) == FB_TRANS_PUTCHAR);
1485 palkovsky 1226
			continue; /* msg already answered */
1227
		case FB_CLEAR:
1673 palkovsky 1228
			clear_port(vport);
1229
			cursor_print(vport);
1485 palkovsky 1230
			retval = 0;
1231
			break;
1486 palkovsky 1232
 		case FB_CURSOR_GOTO:
1233
			row = IPC_GET_ARG1(call);
1234
			col = IPC_GET_ARG2(call);
1235
			if (row >= vport->rows || col >= vport->cols) {
1236
				retval = EINVAL;
1237
				break;
1238
			}
1239
 			retval = 0;
1673 palkovsky 1240
			cursor_hide(vport);
1486 palkovsky 1241
			vport->cur_col = col;
1242
			vport->cur_row = row;
1673 palkovsky 1243
			cursor_print(vport);
1486 palkovsky 1244
 			break;
1245
		case FB_CURSOR_VISIBILITY:
1673 palkovsky 1246
			cursor_hide(vport);
1500 palkovsky 1247
			vport->cursor_active = IPC_GET_ARG1(call);
1673 palkovsky 1248
			cursor_print(vport);
1486 palkovsky 1249
			retval = 0;
1250
			break;
1485 palkovsky 1251
		case FB_GET_CSIZE:
2619 jermar 1252
			ipc_answer_2(callid, EOK, vport->rows, vport->cols);
1485 palkovsky 1253
			continue;
1486 palkovsky 1254
		case FB_SCROLL:
1255
			i = IPC_GET_ARG1(call);
1256
			if (i > vport->rows || i < (- (int)vport->rows)) {
1257
				retval = EINVAL;
1258
				break;
1259
			}
1673 palkovsky 1260
			cursor_hide(vport);
1261
			scroll_port(vport, i*FONT_SCANLINES);
1262
			cursor_print(vport);
1486 palkovsky 1263
			retval = 0;
1264
			break;
1672 palkovsky 1265
		case FB_VIEWPORT_DB:
1266
			/* Enable double buffering */
1267
			i = IPC_GET_ARG1(call);
1268
			if (i == -1)
1269
				i = vp;
1270
			if (i < 0 || i >= MAX_VIEWPORTS) {
1271
				retval = EINVAL;
1272
				break;
1273
			}
2619 jermar 1274
			if (!viewports[i].initialized ) {
1672 palkovsky 1275
				retval = EADDRNOTAVAIL;
1276
				break;
1277
			}
1278
			viewports[i].dboffset = 0;
1279
			if (IPC_GET_ARG2(call) == 1 && !viewports[i].dbdata)
2619 jermar 1280
				viewports[i].dbdata =
1281
				    malloc(screen.pixelbytes *
1282
				    viewports[i].width * viewports[i].height);
2025 jermar 1283
			else if (IPC_GET_ARG2(call) == 0 &&
2619 jermar 1284
			    viewports[i].dbdata) {
1672 palkovsky 1285
				free(viewports[i].dbdata);
1286
				viewports[i].dbdata = NULL;
1287
			}
1288
			retval = 0;
1289
			break;
1486 palkovsky 1290
		case FB_VIEWPORT_SWITCH:
1291
			i = IPC_GET_ARG1(call);
1292
			if (i < 0 || i >= MAX_VIEWPORTS) {
1293
				retval = EINVAL;
1294
				break;
1295
			}
1296
			if (! viewports[i].initialized ) {
1297
				retval = EADDRNOTAVAIL;
1298
				break;
1299
			}
1673 palkovsky 1300
			cursor_hide(vport);
1486 palkovsky 1301
			vp = i;
1302
			vport = &viewports[vp];
1673 palkovsky 1303
			cursor_print(vport);
1486 palkovsky 1304
			retval = 0;
1305
			break;
1306
		case FB_VIEWPORT_CREATE:
1307
			retval = viewport_create(IPC_GET_ARG1(call) >> 16,
2619 jermar 1308
			    IPC_GET_ARG1(call) & 0xffff,
1309
			    IPC_GET_ARG2(call) >> 16,
1310
			    IPC_GET_ARG2(call) & 0xffff);
1486 palkovsky 1311
			break;
1312
		case FB_VIEWPORT_DELETE:
1313
			i = IPC_GET_ARG1(call);
1314
			if (i < 0 || i >= MAX_VIEWPORTS) {
1315
				retval = EINVAL;
1316
				break;
1317
			}
1318
			if (! viewports[i].initialized ) {
1319
				retval = EADDRNOTAVAIL;
1320
				break;
1321
			}
1322
			viewports[i].initialized = 0;
1672 palkovsky 1323
			if (viewports[i].dbdata) {
1324
				free(viewports[i].dbdata);
1325
				viewports[i].dbdata = NULL;
1326
			}
1486 palkovsky 1327
			retval = 0;
1328
			break;
1329
		case FB_SET_STYLE:
1509 palkovsky 1330
			vport->style.fg_color = IPC_GET_ARG1(call);
1331
			vport->style.bg_color = IPC_GET_ARG2(call);
1486 palkovsky 1332
			retval = 0;
1333
			break;
1334
		case FB_GET_RESOLUTION:
2619 jermar 1335
			ipc_answer_2(callid, EOK, screen.xres, screen.yres);
1486 palkovsky 1336
			continue;
1707 palkovsky 1337
		case FB_POINTER_MOVE:
1723 palkovsky 1338
			pointer_enabled = 1;
1711 palkovsky 1339
			mouse_move(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
1720 palkovsky 1340
			retval = 0;
1707 palkovsky 1341
			break;
1485 palkovsky 1342
		default:
1343
			retval = ENOENT;
1344
		}
2619 jermar 1345
		ipc_answer_0(callid, retval);
1363 vana 1346
	}
1485 palkovsky 1347
}
1363 vana 1348
 
1498 palkovsky 1349
/** Initialization of framebuffer */
2070 jermar 1350
int
1351
fb_init(void)
1485 palkovsky 1352
{
1501 palkovsky 1353
	void *fb_ph_addr;
1490 palkovsky 1354
	unsigned int fb_width;
1355
	unsigned int fb_height;
1356
	unsigned int fb_scanline;
1993 decky 1357
	unsigned int fb_visual;
3679 rimsky 1358
	unsigned int fb_offset;
1993 decky 1359
	bool fb_invert_colors;
1501 palkovsky 1360
	void *fb_addr;
1361
	size_t asz;
1363 vana 1362
 
1490 palkovsky 1363
	async_set_client_connection(fb_client_connection);
1363 vana 1364
 
1886 jermar 1365
	fb_ph_addr = (void *) sysinfo_value("fb.address.physical");
3679 rimsky 1366
	fb_offset = sysinfo_value("fb.offset");
1886 jermar 1367
	fb_width = sysinfo_value("fb.width");
1368
	fb_height = sysinfo_value("fb.height");
1369
	fb_scanline = sysinfo_value("fb.scanline");
1993 decky 1370
	fb_visual = sysinfo_value("fb.visual");
1886 jermar 1371
	fb_invert_colors = sysinfo_value("fb.invert-colors");
1490 palkovsky 1372
 
1993 decky 1373
	asz = fb_scanline * fb_height;
2141 jermar 1374
	fb_addr = as_get_mappable_page(asz);
1485 palkovsky 1375
 
3679 rimsky 1376
	physmem_map(fb_ph_addr + fb_offset, fb_addr, ALIGN_UP(asz, PAGE_SIZE) >>
2619 jermar 1377
	    PAGE_WIDTH, AS_AREA_READ | AS_AREA_WRITE);
1886 jermar 1378
 
3679 rimsky 1379
	if (screen_init(fb_addr, fb_offset, fb_width, fb_height, fb_scanline, fb_visual,
2619 jermar 1380
	    fb_invert_colors))
1993 decky 1381
		return 0;
1382
 
1383
	return -1;
1363 vana 1384
}
1490 palkovsky 1385
 
1649 cejka 1386
/** 
1387
 * @}
1388
 */