Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
1363 vana 1
/*
3707 decky 2
 * Copyright (c) 2008 Martin Decky
2071 jermar 3
 * Copyright (c) 2006 Jakub Vana
4
 * Copyright (c) 2006 Ondrej Palkovsky
1363 vana 5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 *
11
 * - Redistributions of source code must retain the above copyright
12
 *   notice, this list of conditions and the following disclaimer.
13
 * - Redistributions in binary form must reproduce the above copyright
14
 *   notice, this list of conditions and the following disclaimer in the
15
 *   documentation and/or other materials provided with the distribution.
16
 * - The name of the author may not be used to endorse or promote products
17
 *   derived from this software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 */
30
 
1740 jermar 31
/**
32
 * @defgroup fb Graphical framebuffer
33
 * @brief	HelenOS graphical framebuffer.
1649 cejka 34
 * @ingroup fbs
35
 * @{
3707 decky 36
 */
1888 jermar 37
 
1649 cejka 38
/** @file
39
 */
40
 
1430 jermar 41
#include <stdlib.h>
42
#include <unistd.h>
43
#include <string.h>
1363 vana 44
#include <ddi.h>
45
#include <sysinfo.h>
46
#include <align.h>
47
#include <as.h>
48
#include <ipc/fb.h>
49
#include <ipc/ipc.h>
1430 jermar 50
#include <ipc/ns.h>
1363 vana 51
#include <ipc/services.h>
52
#include <kernel/errno.h>
1993 decky 53
#include <kernel/genarch/fb/visuals.h>
3767 svoboda 54
#include <console/color.h>
55
#include <console/style.h>
1392 palkovsky 56
#include <async.h>
1993 decky 57
#include <bool.h>
1505 palkovsky 58
 
1404 palkovsky 59
#include "font-8x16.h"
1363 vana 60
#include "fb.h"
1505 palkovsky 61
#include "main.h"
62
#include "../console/screenbuffer.h"
1547 palkovsky 63
#include "ppm.h"
1363 vana 64
 
1711 palkovsky 65
#include "pointer.xbm"
66
#include "pointer_mask.xbm"
67
 
3707 decky 68
#define DEFAULT_BGCOLOR  0xf0f0f0
69
#define DEFAULT_FGCOLOR  0x000000
1363 vana 70
 
4226 svoboda 71
#define GLYPH_UNAVAIL	'?'
72
 
3707 decky 73
#define MAX_ANIM_LEN     8
74
#define MAX_ANIMATIONS   4
75
#define MAX_PIXMAPS      256  /**< Maximum number of saved pixmaps */
76
#define MAX_VIEWPORTS    128  /**< Viewport is a rectangular area on the screen */
1363 vana 77
 
3744 svoboda 78
/** Function to render a pixel from a RGB value. */
3707 decky 79
typedef void (*rgb_conv_t)(void *, uint32_t);
1363 vana 80
 
3744 svoboda 81
/** Function to draw a glyph. */
82
typedef void (*dg_t)(unsigned int x, unsigned int y, bool cursor,
4226 svoboda 83
    uint8_t *glyphs, uint32_t glyph, uint32_t fg_color, uint32_t bg_color);
3744 svoboda 84
 
1485 palkovsky 85
struct {
3707 decky 86
	uint8_t *fb_addr;
87
 
1875 jermar 88
	unsigned int xres;
89
	unsigned int yres;
3707 decky 90
 
1875 jermar 91
	unsigned int scanline;
3707 decky 92
	unsigned int glyphscanline;
93
 
1875 jermar 94
	unsigned int pixelbytes;
3707 decky 95
	unsigned int glyphbytes;
4233 svoboda 96
 
97
	/** Pre-rendered mask for rendering glyphs. Specific for the visual. */
98
	uint8_t *glyphs;
3707 decky 99
 
100
	rgb_conv_t rgb_conv;
1485 palkovsky 101
} screen;
1363 vana 102
 
3767 svoboda 103
/** Backbuffer character cell. */
1485 palkovsky 104
typedef struct {
4211 svoboda 105
	uint32_t glyph;
3767 svoboda 106
	uint32_t fg_color;
107
	uint32_t bg_color;
108
} bb_cell_t;
109
 
110
typedef struct {
3707 decky 111
	bool initialized;
112
	unsigned int x;
113
	unsigned int y;
114
	unsigned int width;
115
	unsigned int height;
116
 
1485 palkovsky 117
	/* Text support in window */
3707 decky 118
	unsigned int cols;
119
	unsigned int rows;
120
 
3744 svoboda 121
	/*
122
	 * Style and glyphs for text printing
123
	 */
124
 
3767 svoboda 125
	/** Current attributes. */
126
	attr_rgb_t attr;
3744 svoboda 127
 
3707 decky 128
	uint8_t *bgpixel;
3744 svoboda 129
 
4233 svoboda 130
	/**
131
	 * Glyph drawing function for this viewport.  Different viewports
132
	 * might use different drawing functions depending on whether their
133
	 * scanlines are aligned on a word boundary.
134
	 */
3744 svoboda 135
	dg_t dglyph;
3707 decky 136
 
1485 palkovsky 137
	/* Auto-cursor position */
3707 decky 138
	bool cursor_active;
139
	unsigned int cur_col;
140
	unsigned int cur_row;
141
	bool cursor_shown;
142
 
143
	/* Back buffer */
3767 svoboda 144
	bb_cell_t *backbuf;
3707 decky 145
	unsigned int bbsize;
1485 palkovsky 146
} viewport_t;
1363 vana 147
 
1646 palkovsky 148
typedef struct {
3707 decky 149
	bool initialized;
150
	bool enabled;
1646 palkovsky 151
	unsigned int vp;
3707 decky 152
 
1646 palkovsky 153
	unsigned int pos;
154
	unsigned int animlen;
155
	unsigned int pixmaps[MAX_ANIM_LEN];
156
} animation_t;
3707 decky 157
 
1646 palkovsky 158
static animation_t animations[MAX_ANIMATIONS];
3707 decky 159
static bool anims_enabled;
1646 palkovsky 160
 
1552 palkovsky 161
typedef struct {
162
	unsigned int width;
163
	unsigned int height;
1781 jermar 164
	uint8_t *data;
1552 palkovsky 165
} pixmap_t;
3707 decky 166
 
1552 palkovsky 167
static pixmap_t pixmaps[MAX_PIXMAPS];
1485 palkovsky 168
static viewport_t viewports[128];
169
 
3707 decky 170
static bool client_connected = false;  /**< Allow only 1 connection */
1485 palkovsky 171
 
3767 svoboda 172
static uint32_t color_table[16] = {
173
	[COLOR_BLACK]		= 0x000000,
174
	[COLOR_BLUE]		= 0x0000f0,
175
	[COLOR_GREEN]		= 0x00f000,
176
	[COLOR_CYAN]		= 0x00f0f0,
177
	[COLOR_RED]		= 0xf00000,
178
	[COLOR_MAGENTA]		= 0xf000f0,
179
	[COLOR_YELLOW]		= 0xf0f000,
180
	[COLOR_WHITE]		= 0xf0f0f0,
181
 
182
	[8 + COLOR_BLACK]	= 0x000000,
183
	[8 + COLOR_BLUE]	= 0x0000ff,
184
	[8 + COLOR_GREEN]	= 0x00ff00,
185
	[8 + COLOR_CYAN]	= 0x00ffff,
186
	[8 + COLOR_RED]		= 0xff0000,
187
	[8 + COLOR_MAGENTA]	= 0xff00ff,
188
	[8 + COLOR_YELLOW]	= 0xffff00,
189
	[8 + COLOR_WHITE]	= 0xffffff,
190
};
191
 
3792 svoboda 192
static int rgb_from_attr(attr_rgb_t *rgb, const attrs_t *a);
3767 svoboda 193
static int rgb_from_style(attr_rgb_t *rgb, int style);
194
static int rgb_from_idx(attr_rgb_t *rgb, ipcarg_t fg_color,
195
    ipcarg_t bg_color, ipcarg_t flags);
196
 
197
static int fb_set_color(viewport_t *vport, ipcarg_t fg_color,
198
    ipcarg_t bg_color, ipcarg_t attr);
199
 
3744 svoboda 200
static void draw_glyph_aligned(unsigned int x, unsigned int y, bool cursor,
4211 svoboda 201
    uint8_t *glyphs, uint32_t glyph, uint32_t fg_color, uint32_t bg_color);
3744 svoboda 202
static void draw_glyph_fallback(unsigned int x, unsigned int y, bool cursor,
4211 svoboda 203
    uint8_t *glyphs, uint32_t glyph, uint32_t fg_color, uint32_t bg_color);
3744 svoboda 204
 
3739 svoboda 205
static void draw_vp_glyph(viewport_t *vport, bool cursor, unsigned int col,
3723 svoboda 206
    unsigned int row);
207
 
208
 
3707 decky 209
#define RED(x, bits)                 ((x >> (8 + 8 + 8 - bits)) & ((1 << bits) - 1))
210
#define GREEN(x, bits)               ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
211
#define BLUE(x, bits)                ((x >> (8 - bits)) & ((1 << bits) - 1))
1363 vana 212
 
3707 decky 213
#define COL2X(col)                   ((col) * FONT_WIDTH)
214
#define ROW2Y(row)                   ((row) * FONT_SCANLINES)
1363 vana 215
 
3707 decky 216
#define X2COL(x)                     ((x) / FONT_WIDTH)
217
#define Y2ROW(y)                     ((y) / FONT_SCANLINES)
1363 vana 218
 
3707 decky 219
#define FB_POS(x, y)                 ((y) * screen.scanline + (x) * screen.pixelbytes)
220
#define BB_POS(vport, col, row)      ((row) * vport->cols + (col))
221
#define GLYPH_POS(glyph, y, cursor)  (((glyph) + (cursor) * FONT_GLYPHS) * screen.glyphbytes + (y) * screen.glyphscanline)
1875 jermar 222
 
1363 vana 223
 
3707 decky 224
/** ARGB 8:8:8:8 conversion
225
 *
226
 */
227
static void rgb_0888(void *dst, uint32_t rgb)
1363 vana 228
{
3707 decky 229
	*((uint32_t *) dst) = rgb & 0xffffff;
1363 vana 230
}
231
 
1994 decky 232
 
3707 decky 233
/** ABGR 8:8:8:8 conversion
234
 *
235
 */
236
static void bgr_0888(void *dst, uint32_t rgb)
1994 decky 237
{
3707 decky 238
	*((uint32_t *) dst)
239
	    = (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | RED(rgb, 8);
1994 decky 240
}
241
 
3707 decky 242
 
3882 decky 243
/** RGB 8:8:8 conversion
3707 decky 244
 *
245
 */
246
static void rgb_888(void *dst, uint32_t rgb)
1363 vana 247
{
3710 decky 248
	((uint8_t *) dst)[0] = BLUE(rgb, 8);
249
	((uint8_t *) dst)[1] = GREEN(rgb, 8);
250
	((uint8_t *) dst)[2] = RED(rgb, 8);
1363 vana 251
}
252
 
253
 
3882 decky 254
/** BGR 8:8:8 conversion
255
 *
256
 */
257
static void bgr_888(void *dst, uint32_t rgb)
258
{
259
	((uint8_t *) dst)[0] = RED(rgb, 8);
260
	((uint8_t *) dst)[1] = GREEN(rgb, 8);
261
	((uint8_t *) dst)[2] = BLUE(rgb, 8);
262
}
263
 
264
 
3707 decky 265
/** RGB 5:5:5 conversion
266
 *
267
 */
268
static void rgb_555(void *dst, uint32_t rgb)
1993 decky 269
{
3707 decky 270
	*((uint16_t *) dst)
271
	    = (RED(rgb, 5) << 10) | (GREEN(rgb, 5) << 5) | BLUE(rgb, 5);
1993 decky 272
}
273
 
274
 
3707 decky 275
/** RGB 5:6:5 conversion
276
 *
277
 */
278
static void rgb_565(void *dst, uint32_t rgb)
1363 vana 279
{
3707 decky 280
	*((uint16_t *) dst)
281
	    = (RED(rgb, 5) << 11) | (GREEN(rgb, 6) << 5) | BLUE(rgb, 5);
1363 vana 282
}
283
 
284
 
3707 decky 285
/** RGB 3:2:3
286
 *
287
 */
288
static void rgb_323(void *dst, uint32_t rgb)
1363 vana 289
{
3707 decky 290
	*((uint8_t *) dst)
291
	    = ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
1363 vana 292
}
293
 
3725 svoboda 294
/** Draw a filled rectangle.
295
 *
296
 * @note Need real implementation that does not access VRAM twice.
297
 */
3723 svoboda 298
static void draw_filled_rect(unsigned int x0, unsigned int y0, unsigned int x1,
299
    unsigned int y1, uint32_t color)
300
{
301
	unsigned int x, y;
3725 svoboda 302
	unsigned int copy_bytes;
303
 
304
	uint8_t *sp, *dp;
3723 svoboda 305
	uint8_t cbuf[4];
1363 vana 306
 
3725 svoboda 307
	if (y0 >= y1 || x0 >= x1) return;
3723 svoboda 308
	screen.rgb_conv(cbuf, color);
309
 
3725 svoboda 310
	sp = &screen.fb_addr[FB_POS(x0, y0)];
311
	dp = sp;
312
 
313
	/* Draw the first line. */
314
	for (x = x0; x < x1; x++) {
315
		memcpy(dp, cbuf, screen.pixelbytes);
316
		dp += screen.pixelbytes;
3723 svoboda 317
	}
3725 svoboda 318
 
319
	dp = sp + screen.scanline;
320
	copy_bytes = (x1 - x0) * screen.pixelbytes;
321
 
322
	/* Draw the remaining lines by copying. */
323
	for (y = y0 + 1; y < y1; y++) {
324
		memcpy(dp, sp, copy_bytes);
325
		dp += screen.scanline;
326
	}
3723 svoboda 327
}
328
 
329
/** Redraw viewport.
1498 palkovsky 330
 *
3707 decky 331
 * @param vport Viewport to redraw
332
 *
1498 palkovsky 333
 */
3707 decky 334
static void vport_redraw(viewport_t *vport)
1485 palkovsky 335
{
3723 svoboda 336
	unsigned int row, col;
337
 
3707 decky 338
	for (row = 0; row < vport->rows; row++) {
3723 svoboda 339
		for (col = 0; col < vport->cols; col++) {
3739 svoboda 340
			draw_vp_glyph(vport, false, col, row);
3707 decky 341
		}
1672 palkovsky 342
	}
3723 svoboda 343
 
3707 decky 344
	if (COL2X(vport->cols) < vport->width) {
3723 svoboda 345
		draw_filled_rect(
346
		    vport->x + COL2X(vport->cols), vport->y,
347
		    vport->x + vport->width, vport->y + vport->height,
3767 svoboda 348
		    vport->attr.bg_color);
1672 palkovsky 349
	}
3723 svoboda 350
 
3707 decky 351
	if (ROW2Y(vport->rows) < vport->height) {
3723 svoboda 352
		draw_filled_rect(
353
		    vport->x, vport->y + ROW2Y(vport->rows),
354
		    vport->x + vport->width, vport->y + vport->height,
3767 svoboda 355
		    vport->attr.bg_color);
1672 palkovsky 356
	}
1559 palkovsky 357
}
358
 
3767 svoboda 359
static void backbuf_clear(bb_cell_t *backbuf, size_t len, uint32_t fg_color,
360
    uint32_t bg_color)
361
{
362
	unsigned i;
3707 decky 363
 
3767 svoboda 364
	for (i = 0; i < len; i++) {
365
		backbuf[i].glyph = 0;
366
		backbuf[i].fg_color = fg_color;
367
		backbuf[i].bg_color = bg_color;
368
	}
369
}
370
 
3724 svoboda 371
/** Clear viewport.
3707 decky 372
 *
373
 * @param vport Viewport to clear
374
 *
375
 */
376
static void vport_clear(viewport_t *vport)
1363 vana 377
{
3767 svoboda 378
	backbuf_clear(vport->backbuf, vport->cols * vport->rows,
379
	    vport->attr.fg_color, vport->attr.bg_color);
3707 decky 380
	vport_redraw(vport);
1363 vana 381
}
382
 
3724 svoboda 383
/** Scroll viewport by the specified number of lines.
1485 palkovsky 384
 *
1709 jermar 385
 * @param vport Viewport to scroll
3707 decky 386
 * @param lines Number of lines to scroll
387
 *
1485 palkovsky 388
 */
3707 decky 389
static void vport_scroll(viewport_t *vport, int lines)
1363 vana 390
{
3726 svoboda 391
	unsigned int row, col;
3739 svoboda 392
	unsigned int x, y;
4232 svoboda 393
	uint32_t glyph;
3767 svoboda 394
	uint32_t fg_color;
395
	uint32_t bg_color;
396
	bb_cell_t *bbp, *xbp;
3726 svoboda 397
 
3724 svoboda 398
	/*
3739 svoboda 399
	 * Redraw.
400
	 */
401
 
402
	y = vport->y;
403
	for (row = 0; row < vport->rows; row++) {
404
		x = vport->x;
405
		for (col = 0; col < vport->cols; col++) {
406
			if ((row + lines >= 0) && (row + lines < vport->rows)) {
3767 svoboda 407
				xbp = &vport->backbuf[BB_POS(vport, col, row + lines)];
408
				bbp = &vport->backbuf[BB_POS(vport, col, row)];
3739 svoboda 409
 
3767 svoboda 410
				glyph = xbp->glyph;
411
				fg_color = xbp->fg_color;
412
				bg_color = xbp->bg_color;
413
 
414
				if (bbp->glyph == glyph &&
415
				    bbp->fg_color == xbp->fg_color &&
416
				    bbp->bg_color == xbp->bg_color) {
3739 svoboda 417
					x += FONT_WIDTH;
418
					continue;
419
				}
420
			} else {
421
				glyph = 0;
3767 svoboda 422
				fg_color = vport->attr.fg_color;
423
				bg_color = vport->attr.bg_color;
3739 svoboda 424
			}
425
 
4233 svoboda 426
			(*vport->dglyph)(x, y, false, screen.glyphs, glyph,
3767 svoboda 427
			    fg_color, bg_color);
3739 svoboda 428
			x += FONT_WIDTH;
429
		}
430
		y += FONT_SCANLINES;
431
	}
432
 
433
	/*
3724 svoboda 434
	 * Scroll backbuffer.
435
	 */
3723 svoboda 436
 
1672 palkovsky 437
	if (lines > 0) {
3739 svoboda 438
		memmove(vport->backbuf, vport->backbuf + vport->cols * lines,
3767 svoboda 439
		    vport->cols * (vport->rows - lines) * sizeof(bb_cell_t));
440
		backbuf_clear(&vport->backbuf[BB_POS(vport, 0, vport->rows - lines)],
441
		    vport->cols * lines, vport->attr.fg_color, vport->attr.bg_color);
3707 decky 442
	} else {
3739 svoboda 443
		memmove(vport->backbuf - vport->cols * lines, vport->backbuf,
3767 svoboda 444
		    vport->cols * (vport->rows + lines) * sizeof(bb_cell_t));
445
		backbuf_clear(vport->backbuf, - vport->cols * lines,
446
		    vport->attr.fg_color, vport->attr.bg_color);
1672 palkovsky 447
	}
448
}
449
 
3707 decky 450
/** Render glyphs
1558 palkovsky 451
 *
3707 decky 452
 * Convert glyphs from device independent font
453
 * description to current visual representation.
1558 palkovsky 454
 */
4233 svoboda 455
static void render_glyphs(void)
1363 vana 456
{
3707 decky 457
	unsigned int glyph;
4233 svoboda 458
 
3707 decky 459
	for (glyph = 0; glyph < FONT_GLYPHS; glyph++) {
460
		unsigned int y;
4233 svoboda 461
 
3707 decky 462
		for (y = 0; y < FONT_SCANLINES; y++) {
463
			unsigned int x;
4233 svoboda 464
 
3707 decky 465
			for (x = 0; x < FONT_WIDTH; x++) {
4233 svoboda 466
				screen.rgb_conv(&screen.glyphs[GLYPH_POS(glyph, y, false) + x * screen.pixelbytes],
4232 svoboda 467
				    (fb_font[glyph][y] & (1 << (7 - x)))
3744 svoboda 468
				    ? 0xffffff : 0x000000);
4233 svoboda 469
 
470
				screen.rgb_conv(&screen.glyphs[GLYPH_POS(glyph, y, true) + x * screen.pixelbytes],
4232 svoboda 471
				    (fb_font[glyph][y] & (1 << (7 - x)))
3744 svoboda 472
				    ? 0x000000 : 0xffffff);
3707 decky 473
			}
1509 palkovsky 474
		}
475
	}
1363 vana 476
}
477
 
478
 
1485 palkovsky 479
/** Create new viewport
1363 vana 480
 *
3707 decky 481
 * @param x      Origin of the viewport (x).
482
 * @param y      Origin of the viewport (y).
483
 * @param width  Width of the viewport.
484
 * @param height Height of the viewport.
485
 *
486
 * @return New viewport number.
487
 *
1363 vana 488
 */
3707 decky 489
static int vport_create(unsigned int x, unsigned int y,
490
    unsigned int width, unsigned int height)
1363 vana 491
{
3707 decky 492
	unsigned int i;
493
 
2070 jermar 494
	for (i = 0; i < MAX_VIEWPORTS; i++) {
1485 palkovsky 495
		if (!viewports[i].initialized)
1363 vana 496
			break;
497
	}
1485 palkovsky 498
	if (i == MAX_VIEWPORTS)
499
		return ELIMIT;
3707 decky 500
 
501
	unsigned int cols = width / FONT_WIDTH;
502
	unsigned int rows = height / FONT_SCANLINES;
3767 svoboda 503
	unsigned int bbsize = cols * rows * sizeof(bb_cell_t);
3744 svoboda 504
	unsigned int word_size = sizeof(unsigned long);
3707 decky 505
 
3767 svoboda 506
	bb_cell_t *backbuf = (bb_cell_t *) malloc(bbsize);
3707 decky 507
	if (!backbuf)
508
		return ENOMEM;
509
 
510
	uint8_t *bgpixel = (uint8_t *) malloc(screen.pixelbytes);
511
	if (!bgpixel) {
512
		free(backbuf);
513
		return ENOMEM;
514
	}
3767 svoboda 515
 
516
	backbuf_clear(backbuf, cols * rows, DEFAULT_FGCOLOR, DEFAULT_BGCOLOR);
3707 decky 517
	memset(bgpixel, 0, screen.pixelbytes);
518
 
1485 palkovsky 519
	viewports[i].x = x;
520
	viewports[i].y = y;
521
	viewports[i].width = width;
522
	viewports[i].height = height;
1363 vana 523
 
3707 decky 524
	viewports[i].cols = cols;
525
	viewports[i].rows = rows;
526
 
3767 svoboda 527
	viewports[i].attr.bg_color = DEFAULT_BGCOLOR;
528
	viewports[i].attr.fg_color = DEFAULT_FGCOLOR;
1363 vana 529
 
3707 decky 530
	viewports[i].bgpixel = bgpixel;
3744 svoboda 531
 
532
	/*
533
	 * Conditions necessary  to select aligned version:
534
	 *
535
	 *   - word size is divisible by pixelbytes
536
	 *   - cell scanline size is divisible by word size
537
	 *   - cell scanlines are word-aligned
538
	 */
539
	if ((word_size % screen.pixelbytes) == 0 &&
540
	    (FONT_WIDTH * screen.pixelbytes) % word_size == 0 &&
541
	    (x * screen.pixelbytes) % word_size == 0 &&
542
	    screen.scanline % word_size == 0) {
543
 
544
		viewports[i].dglyph = draw_glyph_aligned;
545
	} else {
546
		viewports[i].dglyph = draw_glyph_fallback;
547
	}
548
 
1489 palkovsky 549
	viewports[i].cur_col = 0;
550
	viewports[i].cur_row = 0;
3707 decky 551
	viewports[i].cursor_active = false;
552
	viewports[i].cursor_shown = false;
553
 
554
	viewports[i].bbsize = bbsize;
555
	viewports[i].backbuf = backbuf;
556
 
557
	viewports[i].initialized = true;
558
 
4233 svoboda 559
	screen.rgb_conv(viewports[i].bgpixel, viewports[i].attr.bg_color);
3707 decky 560
 
1485 palkovsky 561
	return i;
1363 vana 562
}
563
 
3707 decky 564
 
1363 vana 565
/** Initialize framebuffer as a chardev output device
566
 *
3707 decky 567
 * @param addr   Address of the framebuffer
568
 * @param xres   Screen width in pixels
569
 * @param yres   Screen height in pixels
570
 * @param visual Bits per pixel (8, 16, 24, 32)
571
 * @param scan   Bytes per one scanline
1363 vana 572
 *
573
 */
3707 decky 574
static bool screen_init(void *addr, unsigned int xres, unsigned int yres,
575
    unsigned int scan, unsigned int visual)
1363 vana 576
{
4233 svoboda 577
	unsigned int glyphsize;
578
	uint8_t *glyphs;
1993 decky 579
	switch (visual) {
580
	case VISUAL_INDIRECT_8:
3707 decky 581
		screen.rgb_conv = rgb_323;
1886 jermar 582
		screen.pixelbytes = 1;
583
		break;
1993 decky 584
	case VISUAL_RGB_5_5_5:
3707 decky 585
		screen.rgb_conv = rgb_555;
1886 jermar 586
		screen.pixelbytes = 2;
587
		break;
1993 decky 588
	case VISUAL_RGB_5_6_5:
3707 decky 589
		screen.rgb_conv = rgb_565;
1993 decky 590
		screen.pixelbytes = 2;
1886 jermar 591
		break;
1993 decky 592
	case VISUAL_RGB_8_8_8:
3707 decky 593
		screen.rgb_conv = rgb_888;
1993 decky 594
		screen.pixelbytes = 3;
595
		break;
3882 decky 596
	case VISUAL_BGR_8_8_8:
597
		screen.rgb_conv = bgr_888;
598
		screen.pixelbytes = 3;
599
		break;
1993 decky 600
	case VISUAL_RGB_8_8_8_0:
3707 decky 601
		screen.rgb_conv = rgb_888;
1886 jermar 602
		screen.pixelbytes = 4;
603
		break;
1993 decky 604
	case VISUAL_RGB_0_8_8_8:
3707 decky 605
		screen.rgb_conv = rgb_0888;
1993 decky 606
		screen.pixelbytes = 4;
607
		break;
1994 decky 608
	case VISUAL_BGR_0_8_8_8:
3707 decky 609
		screen.rgb_conv = bgr_0888;
1994 decky 610
		screen.pixelbytes = 4;
611
		break;
1993 decky 612
	default:
613
		return false;
1363 vana 614
	}
615
 
3707 decky 616
	screen.fb_addr = (unsigned char *) addr;
1485 palkovsky 617
	screen.xres = xres;
618
	screen.yres = yres;
619
	screen.scanline = scan;
1363 vana 620
 
3707 decky 621
	screen.glyphscanline = FONT_WIDTH * screen.pixelbytes;
622
	screen.glyphbytes = screen.glyphscanline * FONT_SCANLINES;
4233 svoboda 623
 
624
	glyphsize = 2 * FONT_GLYPHS * screen.glyphbytes;
625
	glyphs = (uint8_t *) malloc(glyphsize);
626
	if (!glyphs)
627
		return false;
3707 decky 628
 
4233 svoboda 629
	memset(glyphs, 0, glyphsize);
630
	screen.glyphs = glyphs;
631
 
632
	render_glyphs();
633
 
1485 palkovsky 634
	/* Create first viewport */
3707 decky 635
	vport_create(0, 0, xres, yres);
1993 decky 636
 
637
	return true;
1485 palkovsky 638
}
1363 vana 639
 
3707 decky 640
 
3744 svoboda 641
/** Draw a glyph, takes advantage of alignment.
3707 decky 642
 *
3744 svoboda 643
 * This version can only be used if the following conditions are met:
3739 svoboda 644
 *
3744 svoboda 645
 *   - word size is divisible by pixelbytes
646
 *   - cell scanline size is divisible by word size
647
 *   - cell scanlines are word-aligned
648
 *
649
 * It makes use of the pre-rendered mask to process (possibly) several
650
 * pixels at once (word size / pixelbytes pixels at a time are processed)
651
 * making it very fast. Most notably this version is not applicable at 24 bits
652
 * per pixel.
653
 *
654
 * @param x		x coordinate of top-left corner on screen.
655
 * @param y		y coordinate of top-left corner on screen.
656
 * @param cursor	Draw glyph with cursor
657
 * @param glyphs	Pointer to font bitmap.
658
 * @param glyph		Code of the glyph to draw.
659
 * @param fg_color	Foreground color.
660
 * @param bg_color	Backgroudn color.
3739 svoboda 661
 */
3744 svoboda 662
static void draw_glyph_aligned(unsigned int x, unsigned int y, bool cursor,
4211 svoboda 663
    uint8_t *glyphs, uint32_t glyph, uint32_t fg_color, uint32_t bg_color)
3739 svoboda 664
{
3744 svoboda 665
	unsigned int i, yd;
666
	unsigned long fg_buf, bg_buf;
667
	unsigned long *maskp, *dp;
668
	unsigned long mask;
669
	unsigned int ww, d_add;
670
 
671
	/*
672
	 * Prepare a pair of words, one filled with foreground-color
673
	 * pattern and the other filled with background-color pattern.
674
	 */
675
	for (i = 0; i < sizeof(unsigned long) / screen.pixelbytes; i++) {
676
		screen.rgb_conv(&((uint8_t *)&fg_buf)[i * screen.pixelbytes],
677
		    fg_color);
678
		screen.rgb_conv(&((uint8_t *)&bg_buf)[i * screen.pixelbytes],
679
		    bg_color);
680
	}
681
 
682
	/* Pointer to the current position in the mask. */
683
	maskp = (unsigned long *) &glyphs[GLYPH_POS(glyph, 0, cursor)];
684
 
685
	/* Pointer to the current position on the screen. */
686
	dp = (unsigned long *) &screen.fb_addr[FB_POS(x, y)];
687
 
688
	/* Width of the character cell in words. */
689
	ww = FONT_WIDTH * screen.pixelbytes / sizeof(unsigned long);
690
 
691
	/* Offset to add when moving to another screen scanline. */
692
	d_add = screen.scanline - FONT_WIDTH * screen.pixelbytes;
693
 
694
	for (yd = 0; yd < FONT_SCANLINES; yd++) {
695
		/*
696
		 * Now process the cell scanline, combining foreground
697
		 * and background color patters using the pre-rendered mask.
698
		 */
699
		for (i = 0; i < ww; i++) {
700
			mask = *maskp++;
701
			*dp++ = (fg_buf & mask) | (bg_buf & ~mask);
702
		}
703
 
704
		/* Move to the beginning of the next scanline of the cell. */
705
		dp = (unsigned long *) ((uint8_t *) dp + d_add);
706
	}
3739 svoboda 707
}
708
 
3744 svoboda 709
/** Draw a glyph, fallback version.
710
 *
711
 * This version does not make use of the pre-rendered mask, it uses
712
 * the font bitmap directly. It works always, but it is slower.
713
 *
714
 * @param x		x coordinate of top-left corner on screen.
715
 * @param y		y coordinate of top-left corner on screen.
716
 * @param cursor	Draw glyph with cursor
717
 * @param glyphs	Pointer to font bitmap.
718
 * @param glyph		Code of the glyph to draw.
719
 * @param fg_color	Foreground color.
720
 * @param bg_color	Backgroudn color.
721
 */
722
void draw_glyph_fallback(unsigned int x, unsigned int y, bool cursor,
4211 svoboda 723
    uint8_t *glyphs, uint32_t glyph, uint32_t fg_color, uint32_t bg_color)
3744 svoboda 724
{
725
	unsigned int i, j, yd;
726
	uint8_t fg_buf[4], bg_buf[4];
727
	uint8_t *dp, *sp;
728
	unsigned int d_add;
729
	uint8_t b;
730
 
731
	/* Pre-render 1x the foreground and background color pixels. */
732
	if (cursor) {
733
		screen.rgb_conv(fg_buf, bg_color);
734
		screen.rgb_conv(bg_buf, fg_color);
735
	} else {
736
		screen.rgb_conv(fg_buf, fg_color);
737
		screen.rgb_conv(bg_buf, bg_color);
738
	}
739
 
740
	/* Pointer to the current position on the screen. */
741
	dp = (uint8_t *) &screen.fb_addr[FB_POS(x, y)];
742
 
743
	/* Offset to add when moving to another screen scanline. */
744
	d_add = screen.scanline - FONT_WIDTH * screen.pixelbytes;
745
 
746
	for (yd = 0; yd < FONT_SCANLINES; yd++) {
747
		/* Byte containing bits of the glyph scanline. */
4232 svoboda 748
		b = fb_font[glyph][yd];
3744 svoboda 749
 
750
		for (i = 0; i < FONT_WIDTH; i++) {
751
			/* Choose color based on the current bit. */
752
			sp = (b & 0x80) ? fg_buf : bg_buf;
753
 
754
			/* Copy the pixel. */
755
			for (j = 0; j < screen.pixelbytes; j++) {
756
				*dp++ = *sp++;
757
			}
758
 
759
			/* Move to the next bit. */
760
			b = b << 1;
761
		}
762
 
763
		/* Move to the beginning of the next scanline of the cell. */
764
		dp += d_add;
765
	}
766
}
767
 
3739 svoboda 768
/** Draw glyph at specified position in viewport. 
769
 *
3707 decky 770
 * @param vport  Viewport identification
771
 * @param cursor Draw glyph with cursor
772
 * @param col    Screen position relative to viewport
773
 * @param row    Screen position relative to viewport
774
 *
775
 */
3739 svoboda 776
static void draw_vp_glyph(viewport_t *vport, bool cursor, unsigned int col,
777
    unsigned int row)
1500 palkovsky 778
{
3707 decky 779
	unsigned int x = vport->x + COL2X(col);
780
	unsigned int y = vport->y + ROW2Y(row);
3767 svoboda 781
 
4211 svoboda 782
	uint32_t glyph;
3767 svoboda 783
	uint32_t fg_color;
784
	uint32_t bg_color;
3707 decky 785
 
3767 svoboda 786
	glyph = vport->backbuf[BB_POS(vport, col, row)].glyph;
787
	fg_color = vport->backbuf[BB_POS(vport, col, row)].fg_color;
788
	bg_color = vport->backbuf[BB_POS(vport, col, row)].bg_color;
3744 svoboda 789
 
4233 svoboda 790
	(*vport->dglyph)(x, y, cursor, screen.glyphs, glyph,
3767 svoboda 791
	    fg_color, bg_color);
3707 decky 792
}
793
 
794
/** Hide cursor if it is shown
795
 *
796
 */
797
static void cursor_hide(viewport_t *vport)
798
{
799
	if ((vport->cursor_active) && (vport->cursor_shown)) {
3739 svoboda 800
		draw_vp_glyph(vport, false, vport->cur_col, vport->cur_row);
3707 decky 801
		vport->cursor_shown = false;
1500 palkovsky 802
	}
803
}
804
 
3707 decky 805
 
806
/** Show cursor if cursor showing is enabled
807
 *
808
 */
809
static void cursor_show(viewport_t *vport)
1500 palkovsky 810
{
811
	/* Do not check for cursor_shown */
812
	if (vport->cursor_active) {
3739 svoboda 813
		draw_vp_glyph(vport, true, vport->cur_col, vport->cur_row);
3707 decky 814
		vport->cursor_shown = true;
1500 palkovsky 815
	}
816
}
817
 
3707 decky 818
 
819
/** Invert cursor, if it is enabled
820
 *
821
 */
822
static void cursor_blink(viewport_t *vport)
1500 palkovsky 823
{
824
	if (vport->cursor_shown)
1673 palkovsky 825
		cursor_hide(vport);
1500 palkovsky 826
	else
3707 decky 827
		cursor_show(vport);
1500 palkovsky 828
}
829
 
3707 decky 830
 
831
/** Draw character at given position relative to viewport
832
 *
833
 * @param vport  Viewport identification
834
 * @param c      Character to draw
835
 * @param col    Screen position relative to viewport
836
 * @param row    Screen position relative to viewport
837
 *
1498 palkovsky 838
 */
4211 svoboda 839
static void draw_char(viewport_t *vport, wchar_t c, unsigned int col, unsigned int row)
1486 palkovsky 840
{
3767 svoboda 841
	bb_cell_t *bbp;
842
 
3707 decky 843
	/* Do not hide cursor if we are going to overwrite it */
844
	if ((vport->cursor_active) && (vport->cursor_shown) &&
845
	    ((vport->cur_col != col) || (vport->cur_row != row)))
846
		cursor_hide(vport);
3767 svoboda 847
 
848
	bbp = &vport->backbuf[BB_POS(vport, col, row)];
4232 svoboda 849
	bbp->glyph = fb_font_glyph(c);
3767 svoboda 850
	bbp->fg_color = vport->attr.fg_color;
851
	bbp->bg_color = vport->attr.bg_color;
852
 
3739 svoboda 853
	draw_vp_glyph(vport, false, col, row);
3707 decky 854
 
1489 palkovsky 855
	vport->cur_col = col;
856
	vport->cur_row = row;
3707 decky 857
 
1489 palkovsky 858
	vport->cur_col++;
2025 jermar 859
	if (vport->cur_col >= vport->cols) {
1489 palkovsky 860
		vport->cur_col = 0;
861
		vport->cur_row++;
862
		if (vport->cur_row >= vport->rows)
863
			vport->cur_row--;
1486 palkovsky 864
	}
3707 decky 865
 
866
	cursor_show(vport);
1486 palkovsky 867
}
868
 
4167 svoboda 869
/** Draw text data to viewport.
1552 palkovsky 870
 *
1709 jermar 871
 * @param vport Viewport id
4167 svoboda 872
 * @param data  Text data.
873
 * @param x	Leftmost column of the area.
874
 * @param y	Topmost row of the area.
875
 * @param w	Number of rows.
876
 * @param h	Number of columns.
1552 palkovsky 877
 */
4167 svoboda 878
static void draw_text_data(viewport_t *vport, keyfield_t *data, unsigned int x,
879
    unsigned int y, unsigned int w, unsigned int h)
1505 palkovsky 880
{
4167 svoboda 881
	unsigned int i, j;
3767 svoboda 882
	bb_cell_t *bbp;
883
	attrs_t *a;
884
	attr_rgb_t rgb;
885
 
4167 svoboda 886
	for (j = 0; j < h; j++) {
887
		for (i = 0; i < w; i++) {
888
			unsigned int col = x + i;
889
			unsigned int row = y + j;
3767 svoboda 890
 
4167 svoboda 891
			bbp = &vport->backbuf[BB_POS(vport, col, row)];
3767 svoboda 892
 
4167 svoboda 893
			a = &data[j * w + i].attrs;
894
			rgb_from_attr(&rgb, a);
895
 
4232 svoboda 896
			bbp->glyph = fb_font_glyph(data[j * w + i].character);
4167 svoboda 897
			bbp->fg_color = rgb.fg_color;
898
			bbp->bg_color = rgb.bg_color;
899
 
900
			draw_vp_glyph(vport, false, col, row);
901
		}
1505 palkovsky 902
	}
3707 decky 903
	cursor_show(vport);
1505 palkovsky 904
}
905
 
3707 decky 906
 
907
static void putpixel_pixmap(void *data, unsigned int x, unsigned int y, uint32_t color)
1555 palkovsky 908
{
3707 decky 909
	int pm = *((int *) data);
910
	pixmap_t *pmap = &pixmaps[pm];
911
	unsigned int pos = (y * pmap->width + x) * screen.pixelbytes;
1555 palkovsky 912
 
3707 decky 913
	screen.rgb_conv(&pmap->data[pos], color);
914
}
915
 
916
 
917
static void putpixel(void *data, unsigned int x, unsigned int y, uint32_t color)
918
{
919
	viewport_t *vport = (viewport_t *) data;
920
	unsigned int dx = vport->x + x;
921
	unsigned int dy = vport->y + y;
922
 
923
	screen.rgb_conv(&screen.fb_addr[FB_POS(dx, dy)], color);
924
}
925
 
926
 
927
/** Return first free pixmap
928
 *
929
 */
930
static int find_free_pixmap(void)
931
{
932
	unsigned int i;
933
 
934
	for (i = 0; i < MAX_PIXMAPS; i++)
1555 palkovsky 935
		if (!pixmaps[i].data)
936
			return i;
3707 decky 937
 
1555 palkovsky 938
	return -1;
939
}
940
 
941
 
3707 decky 942
/** Create a new pixmap and return appropriate ID
943
 *
944
 */
945
static int shm2pixmap(unsigned char *shm, size_t size)
1555 palkovsky 946
{
947
	int pm;
948
	pixmap_t *pmap;
3707 decky 949
 
1555 palkovsky 950
	pm = find_free_pixmap();
951
	if (pm == -1)
952
		return ELIMIT;
3707 decky 953
 
1555 palkovsky 954
	pmap = &pixmaps[pm];
955
 
956
	if (ppm_get_data(shm, size, &pmap->width, &pmap->height))
957
		return EINVAL;
958
 
959
	pmap->data = malloc(pmap->width * pmap->height * screen.pixelbytes);
960
	if (!pmap->data)
961
		return ENOMEM;
3707 decky 962
 
963
	ppm_draw(shm, size, 0, 0, pmap->width, pmap->height, putpixel_pixmap, (void *) &pm);
964
 
1555 palkovsky 965
	return pm;
966
}
967
 
3707 decky 968
 
1552 palkovsky 969
/** Handle shared memory communication calls
970
 *
971
 * Protocol for drawing pixmaps:
972
 * - FB_PREPARE_SHM(client shm identification)
2025 jermar 973
 * - IPC_M_AS_AREA_SEND
3707 decky 974
 * - FB_DRAW_PPM(startx, starty)
1552 palkovsky 975
 * - FB_DROP_SHM
976
 *
977
 * Protocol for text drawing
2025 jermar 978
 * - IPC_M_AS_AREA_SEND
1552 palkovsky 979
 * - FB_DRAW_TEXT_DATA
980
 *
981
 * @param callid Callid of the current call
3707 decky 982
 * @param call   Current call data
983
 * @param vp     Active viewport
1552 palkovsky 984
 *
3707 decky 985
 * @return false if the call was not handled byt this function, true otherwise
986
 *
987
 * Note: this function is not threads safe, you would have
1552 palkovsky 988
 * to redefine static variables with __thread
3707 decky 989
 *
1552 palkovsky 990
 */
3707 decky 991
static bool shm_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
1547 palkovsky 992
{
993
	static keyfield_t *interbuffer = NULL;
994
	static size_t intersize = 0;
3707 decky 995
 
1720 palkovsky 996
	static unsigned char *shm = NULL;
1555 palkovsky 997
	static ipcarg_t shm_id = 0;
998
	static size_t shm_size;
3707 decky 999
 
1000
	bool handled = true;
1001
	int retval = EOK;
1547 palkovsky 1002
	viewport_t *vport = &viewports[vp];
3707 decky 1003
	unsigned int x;
1004
	unsigned int y;
4167 svoboda 1005
	unsigned int w;
1006
	unsigned int h;
3707 decky 1007
 
1547 palkovsky 1008
	switch (IPC_GET_METHOD(*call)) {
2677 jermar 1009
	case IPC_M_SHARE_OUT:
1547 palkovsky 1010
		/* We accept one area for data interchange */
1555 palkovsky 1011
		if (IPC_GET_ARG1(*call) == shm_id) {
2141 jermar 1012
			void *dest = as_get_mappable_page(IPC_GET_ARG2(*call));
1555 palkovsky 1013
			shm_size = IPC_GET_ARG2(*call);
3707 decky 1014
			if (!ipc_answer_1(callid, EOK, (sysarg_t) dest))
1555 palkovsky 1015
				shm = dest;
1547 palkovsky 1016
			else
1555 palkovsky 1017
				shm_id = 0;
3707 decky 1018
 
1555 palkovsky 1019
			if (shm[0] != 'P')
3707 decky 1020
				return false;
1021
 
1022
			return true;
1547 palkovsky 1023
		} else {
1024
			intersize = IPC_GET_ARG2(*call);
2015 jermar 1025
			receive_comm_area(callid, call, (void *) &interbuffer);
1547 palkovsky 1026
		}
3707 decky 1027
		return true;
1547 palkovsky 1028
	case FB_PREPARE_SHM:
1555 palkovsky 1029
		if (shm_id)
1547 palkovsky 1030
			retval = EBUSY;
1031
		else 
1555 palkovsky 1032
			shm_id = IPC_GET_ARG1(*call);
1547 palkovsky 1033
		break;
1034
 
1035
	case FB_DROP_SHM:
1555 palkovsky 1036
		if (shm) {
1037
			as_area_destroy(shm);
1038
			shm = NULL;
1547 palkovsky 1039
		}
1555 palkovsky 1040
		shm_id = 0;
1547 palkovsky 1041
		break;
3707 decky 1042
 
1555 palkovsky 1043
	case FB_SHM2PIXMAP:
1044
		if (!shm) {
1045
			retval = EINVAL;
1046
			break;
1047
		}
1048
		retval = shm2pixmap(shm, shm_size);
1049
		break;
1547 palkovsky 1050
	case FB_DRAW_PPM:
1555 palkovsky 1051
		if (!shm) {
1547 palkovsky 1052
			retval = EINVAL;
1053
			break;
1054
		}
1055
		x = IPC_GET_ARG1(*call);
1056
		y = IPC_GET_ARG2(*call);
3707 decky 1057
 
1058
		if ((x > vport->width) || (y > vport->height)) {
1547 palkovsky 1059
			retval = EINVAL;
1060
			break;
1061
		}
1062
 
2025 jermar 1063
		ppm_draw(shm, shm_size, IPC_GET_ARG1(*call),
3707 decky 1064
		    IPC_GET_ARG2(*call), vport->width - x, vport->height - y, putpixel, (void *) vport);
1547 palkovsky 1065
		break;
1066
	case FB_DRAW_TEXT_DATA:
4167 svoboda 1067
		x = IPC_GET_ARG1(*call);
1068
		y = IPC_GET_ARG2(*call);
1069
		w = IPC_GET_ARG3(*call);
1070
		h = IPC_GET_ARG4(*call);
1547 palkovsky 1071
		if (!interbuffer) {
1072
			retval = EINVAL;
1073
			break;
1074
		}
4167 svoboda 1075
		if (x + w > vport->cols || y + h > vport->rows) {
1547 palkovsky 1076
			retval = EINVAL;
1077
			break;
1078
		}
4167 svoboda 1079
		if (intersize < w * h * sizeof(*interbuffer)) {
1080
			retval = EINVAL;
1081
			break;
1082
		}
1083
		draw_text_data(vport, interbuffer, x, y, w, h);
1547 palkovsky 1084
		break;
1085
	default:
3707 decky 1086
		handled = false;
1547 palkovsky 1087
	}
1088
 
1089
	if (handled)
2619 jermar 1090
		ipc_answer_0(callid, retval);
1547 palkovsky 1091
	return handled;
1092
}
1093
 
3707 decky 1094
 
1095
static void copy_vp_to_pixmap(viewport_t *vport, pixmap_t *pmap)
1552 palkovsky 1096
{
3707 decky 1097
	unsigned int width = vport->width;
1098
	unsigned int height = vport->height;
1099
 
1711 palkovsky 1100
	if (width + vport->x > screen.xres)
1101
		width = screen.xres - vport->x;
2301 decky 1102
	if (height + vport->y > screen.yres)
1711 palkovsky 1103
		height = screen.yres - vport->y;
2301 decky 1104
 
3707 decky 1105
	unsigned int realwidth = pmap->width <= width ? pmap->width : width;
1106
	unsigned int realheight = pmap->height <= height ? pmap->height : height;
2301 decky 1107
 
3707 decky 1108
	unsigned int srcrowsize = vport->width * screen.pixelbytes;
1109
	unsigned int realrowsize = realwidth * screen.pixelbytes;
1110
 
1111
	unsigned int y;
2301 decky 1112
	for (y = 0; y < realheight; y++) {
3707 decky 1113
		unsigned int tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
1114
		memcpy(pmap->data + srcrowsize * y, screen.fb_addr + tmp, realrowsize);
1711 palkovsky 1115
	}
1116
}
1117
 
3707 decky 1118
 
1119
/** Save viewport to pixmap
1120
 *
1121
 */
1122
static int save_vp_to_pixmap(viewport_t *vport)
1711 palkovsky 1123
{
1124
	int pm;
1125
	pixmap_t *pmap;
3707 decky 1126
 
1552 palkovsky 1127
	pm = find_free_pixmap();
1128
	if (pm == -1)
1129
		return ELIMIT;
1130
 
1131
	pmap = &pixmaps[pm];
1132
	pmap->data = malloc(screen.pixelbytes * vport->width * vport->height);
1133
	if (!pmap->data)
1134
		return ENOMEM;
3707 decky 1135
 
1552 palkovsky 1136
	pmap->width = vport->width;
1137
	pmap->height = vport->height;
3707 decky 1138
 
1711 palkovsky 1139
	copy_vp_to_pixmap(vport, pmap);
1552 palkovsky 1140
 
1141
	return pm;
1142
}
1143
 
3707 decky 1144
 
1552 palkovsky 1145
/** Draw pixmap on screen
1146
 *
1147
 * @param vp Viewport to draw on
1148
 * @param pm Pixmap identifier
3707 decky 1149
 *
1552 palkovsky 1150
 */
1151
static int draw_pixmap(int vp, int pm)
1152
{
1153
	pixmap_t *pmap = &pixmaps[pm];
1154
	viewport_t *vport = &viewports[vp];
3707 decky 1155
 
1156
	unsigned int width = vport->width;
1157
	unsigned int height = vport->height;
1158
 
1711 palkovsky 1159
	if (width + vport->x > screen.xres)
1160
		width = screen.xres - vport->x;
1161
	if (height + vport->y > screen.yres)
1162
		height = screen.yres - vport->y;
3707 decky 1163
 
1552 palkovsky 1164
	if (!pmap->data)
1165
		return EINVAL;
3707 decky 1166
 
1167
	unsigned int realwidth = pmap->width <= width ? pmap->width : width;
1168
	unsigned int realheight = pmap->height <= height ? pmap->height : height;
1169
 
1170
	unsigned int srcrowsize = vport->width * screen.pixelbytes;
1171
	unsigned int realrowsize = realwidth * screen.pixelbytes;
1172
 
1173
	unsigned int y;
2070 jermar 1174
	for (y = 0; y < realheight; y++) {
3707 decky 1175
		unsigned int tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
1176
		memcpy(screen.fb_addr + tmp, pmap->data + y * srcrowsize, realrowsize);
1552 palkovsky 1177
	}
3707 decky 1178
 
1179
	return EOK;
1552 palkovsky 1180
}
1181
 
3707 decky 1182
 
1183
/** Tick animation one step forward
1184
 *
1185
 */
1186
static void anims_tick(void)
1646 palkovsky 1187
{
3707 decky 1188
	unsigned int i;
1646 palkovsky 1189
	static int counts = 0;
1190
 
1191
	/* Limit redrawing */
2025 jermar 1192
	counts = (counts + 1) % 8;
1646 palkovsky 1193
	if (counts)
1194
		return;
1195
 
2070 jermar 1196
	for (i = 0; i < MAX_ANIMATIONS; i++) {
3707 decky 1197
		if ((!animations[i].animlen) || (!animations[i].initialized) ||
1198
		    (!animations[i].enabled))
1646 palkovsky 1199
			continue;
3707 decky 1200
 
1201
		draw_pixmap(animations[i].vp, animations[i].pixmaps[animations[i].pos]);
1202
		animations[i].pos = (animations[i].pos + 1) % animations[i].animlen;
1646 palkovsky 1203
	}
1204
}
1205
 
1711 palkovsky 1206
 
3707 decky 1207
static unsigned int pointer_x;
1208
static unsigned int pointer_y;
1209
static bool pointer_shown, pointer_enabled;
1711 palkovsky 1210
static int pointer_vport = -1;
1211
static int pointer_pixmap = -1;
1212
 
3707 decky 1213
 
1214
static void mouse_show(void)
1711 palkovsky 1215
{
2070 jermar 1216
	int i, j;
1711 palkovsky 1217
	int visibility;
1218
	int color;
1219
	int bytepos;
3707 decky 1220
 
1221
	if ((pointer_shown) || (!pointer_enabled))
1723 palkovsky 1222
		return;
3707 decky 1223
 
3723 svoboda 1224
	/* Save image under the pointer. */
1711 palkovsky 1225
	if (pointer_vport == -1) {
3707 decky 1226
		pointer_vport = vport_create(pointer_x, pointer_y, pointer_width, pointer_height);
1711 palkovsky 1227
		if (pointer_vport < 0)
1228
			return;
1229
	} else {
1230
		viewports[pointer_vport].x = pointer_x;
1231
		viewports[pointer_vport].y = pointer_y;
1232
	}
3707 decky 1233
 
1711 palkovsky 1234
	if (pointer_pixmap == -1)
1235
		pointer_pixmap = save_vp_to_pixmap(&viewports[pointer_vport]);
1236
	else
3707 decky 1237
		copy_vp_to_pixmap(&viewports[pointer_vport], &pixmaps[pointer_pixmap]);
1238
 
3723 svoboda 1239
	/* Draw mouse pointer. */
2025 jermar 1240
	for (i = 0; i < pointer_height; i++)
1241
		for (j = 0; j < pointer_width; j++) {
1242
			bytepos = i * ((pointer_width - 1) / 8 + 1) + j / 8;
2070 jermar 1243
			visibility = pointer_mask_bits[bytepos] &
2619 jermar 1244
			    (1 << (j % 8));
1711 palkovsky 1245
			if (visibility) {
2619 jermar 1246
				color = pointer_bits[bytepos] &
1247
				    (1 << (j % 8)) ? 0 : 0xffffff;
2025 jermar 1248
				if (pointer_x + j < screen.xres && pointer_y +
2619 jermar 1249
				    i < screen.yres)
2025 jermar 1250
					putpixel(&viewports[0], pointer_x + j,
2619 jermar 1251
					    pointer_y + i, color);
1711 palkovsky 1252
			}
1253
		}
1254
	pointer_shown = 1;
1255
}
1256
 
3707 decky 1257
 
1258
static void mouse_hide(void)
1711 palkovsky 1259
{
3723 svoboda 1260
	/* Restore image under the pointer. */
1711 palkovsky 1261
	if (pointer_shown) {
1262
		draw_pixmap(pointer_vport, pointer_pixmap);
1263
		pointer_shown = 0;
1264
	}
1265
}
1266
 
3707 decky 1267
 
1268
static void mouse_move(unsigned int x, unsigned int y)
1711 palkovsky 1269
{
1270
	mouse_hide();
1271
	pointer_x = x;
1272
	pointer_y = y;
1273
	mouse_show();
1274
}
1275
 
3707 decky 1276
 
1277
static int anim_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
1646 palkovsky 1278
{
3707 decky 1279
	bool handled = true;
1280
	int retval = EOK;
1281
	int i, nvp;
1646 palkovsky 1282
	int newval;
3707 decky 1283
 
1646 palkovsky 1284
	switch (IPC_GET_METHOD(*call)) {
1285
	case FB_ANIM_CREATE:
1286
		nvp = IPC_GET_ARG1(*call);
1287
		if (nvp == -1)
1288
			nvp = vp;
2025 jermar 1289
		if (nvp >= MAX_VIEWPORTS || nvp < 0 ||
1290
			!viewports[nvp].initialized) {
1646 palkovsky 1291
			retval = EINVAL;
1292
			break;
1293
		}
2025 jermar 1294
		for (i = 0; i < MAX_ANIMATIONS; i++) {
1295
			if (!animations[i].initialized)
1646 palkovsky 1296
				break;
1297
		}
1298
		if (i == MAX_ANIMATIONS) {
1299
			retval = ELIMIT;
1300
			break;
1301
		}
1302
		animations[i].initialized = 1;
1303
		animations[i].animlen = 0;
1304
		animations[i].pos = 0;
1305
		animations[i].enabled = 0;
1306
		animations[i].vp = nvp;
1307
		retval = i;
1308
		break;
1309
	case FB_ANIM_DROP:
1310
		i = IPC_GET_ARG1(*call);
1720 palkovsky 1311
		if (i >= MAX_ANIMATIONS || i < 0) {
1646 palkovsky 1312
			retval = EINVAL;
1313
			break;
1314
		}
1315
		animations[i].initialized = 0;
1316
		break;
1317
	case FB_ANIM_ADDPIXMAP:
1318
		i = IPC_GET_ARG1(*call);
2025 jermar 1319
		if (i >= MAX_ANIMATIONS || i < 0 ||
1320
			!animations[i].initialized) {
1646 palkovsky 1321
			retval = EINVAL;
1322
			break;
1323
		}
1324
		if (animations[i].animlen == MAX_ANIM_LEN) {
1325
			retval = ELIMIT;
1326
			break;
1327
		}
1328
		newval = IPC_GET_ARG2(*call);
2025 jermar 1329
		if (newval < 0 || newval > MAX_PIXMAPS ||
1330
			!pixmaps[newval].data) {
1646 palkovsky 1331
			retval = EINVAL;
1332
			break;
1333
		}
1334
		animations[i].pixmaps[animations[i].animlen++] = newval;
1335
		break;
1336
	case FB_ANIM_CHGVP:
1337
		i = IPC_GET_ARG1(*call);
1338
		if (i >= MAX_ANIMATIONS || i < 0) {
1339
			retval = EINVAL;
1340
			break;
1341
		}
1342
		nvp = IPC_GET_ARG2(*call);
1343
		if (nvp == -1)
1344
			nvp = vp;
2025 jermar 1345
		if (nvp >= MAX_VIEWPORTS || nvp < 0 ||
1346
			!viewports[nvp].initialized) {
1646 palkovsky 1347
			retval = EINVAL;
1348
			break;
1349
		}
1350
		animations[i].vp = nvp;
1351
		break;
1352
	case FB_ANIM_START:
1353
	case FB_ANIM_STOP:
1354
		i = IPC_GET_ARG1(*call);
1355
		if (i >= MAX_ANIMATIONS || i < 0) {
1356
			retval = EINVAL;
1357
			break;
1358
		}
1359
		newval = (IPC_GET_METHOD(*call) == FB_ANIM_START);
1360
		if (newval ^ animations[i].enabled) {
1361
			animations[i].enabled = newval;
1362
			anims_enabled += newval ? 1 : -1;
1363
		}
1364
		break;
1365
	default:
1366
		handled = 0;
1367
	}
1368
	if (handled)
2619 jermar 1369
		ipc_answer_0(callid, retval);
1646 palkovsky 1370
	return handled;
1371
}
1372
 
3707 decky 1373
 
1374
/** Handler for messages concerning pixmap handling
1375
 *
1376
 */
1377
static int pixmap_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
1552 palkovsky 1378
{
3707 decky 1379
	bool handled = true;
1380
	int retval = EOK;
1381
	int i, nvp;
1382
 
1552 palkovsky 1383
	switch (IPC_GET_METHOD(*call)) {
1384
	case FB_VP_DRAW_PIXMAP:
1385
		nvp = IPC_GET_ARG1(*call);
1386
		if (nvp == -1)
1387
			nvp = vp;
2025 jermar 1388
		if (nvp < 0 || nvp >= MAX_VIEWPORTS ||
1389
			!viewports[nvp].initialized) {
1552 palkovsky 1390
			retval = EINVAL;
1391
			break;
1392
		}
1393
		i = IPC_GET_ARG2(*call);
1394
		retval = draw_pixmap(nvp, i);
1395
		break;
1396
	case FB_VP2PIXMAP:
1397
		nvp = IPC_GET_ARG1(*call);
1398
		if (nvp == -1)
1399
			nvp = vp;
2025 jermar 1400
		if (nvp < 0 || nvp >= MAX_VIEWPORTS ||
1401
			!viewports[nvp].initialized)
1552 palkovsky 1402
			retval = EINVAL;
1403
		else
1711 palkovsky 1404
			retval = save_vp_to_pixmap(&viewports[nvp]);
1552 palkovsky 1405
		break;
1406
	case FB_DROP_PIXMAP:
1407
		i = IPC_GET_ARG1(*call);
1408
		if (i >= MAX_PIXMAPS) {
1409
			retval = EINVAL;
1410
			break;
1411
		}
1412
		if (pixmaps[i].data) {
1413
			free(pixmaps[i].data);
1414
			pixmaps[i].data = NULL;
1415
		}
1416
		break;
1417
	default:
1418
		handled = 0;
1419
	}
3707 decky 1420
 
1552 palkovsky 1421
	if (handled)
2619 jermar 1422
		ipc_answer_0(callid, retval);
1552 palkovsky 1423
	return handled;
1424
 
1425
}
1426
 
3767 svoboda 1427
static int rgb_from_style(attr_rgb_t *rgb, int style)
1428
{
1429
	switch (style) {
1430
	case STYLE_NORMAL:
1431
		rgb->fg_color = color_table[COLOR_BLACK];
1432
		rgb->bg_color = color_table[COLOR_WHITE];
1433
		break;
1434
	case STYLE_EMPHASIS:
1435
		rgb->fg_color = color_table[COLOR_RED];
1436
		rgb->bg_color = color_table[COLOR_WHITE];
1437
		break;
1438
	default:
1439
		return EINVAL;
1440
	}
1441
 
1442
	return EOK;
1443
}
1444
 
1445
static int rgb_from_idx(attr_rgb_t *rgb, ipcarg_t fg_color,
1446
    ipcarg_t bg_color, ipcarg_t flags)
1447
{
1448
	fg_color = (fg_color & 7) | ((flags & CATTR_BRIGHT) ? 8 : 0);
1449
	bg_color = (bg_color & 7) | ((flags & CATTR_BRIGHT) ? 8 : 0);
1450
 
1451
	rgb->fg_color = color_table[fg_color];
1452
	rgb->bg_color = color_table[bg_color];
1453
 
1454
	return EOK;
1455
}
1456
 
3792 svoboda 1457
static int rgb_from_attr(attr_rgb_t *rgb, const attrs_t *a)
1458
{
1459
	int rc;
1460
 
1461
	switch (a->t) {
1462
	case at_style:
1463
		rc = rgb_from_style(rgb, a->a.s.style);
1464
		break;
1465
	case at_idx:
1466
		rc = rgb_from_idx(rgb, a->a.i.fg_color,
1467
		    a->a.i.bg_color, a->a.i.flags);
1468
		break;
1469
	case at_rgb:
1470
		*rgb = a->a.r;
1471
		rc = EOK;
1472
		break;
1473
	}
1474
 
1475
	return rc;
1476
}
1477
 
3767 svoboda 1478
static int fb_set_style(viewport_t *vport, ipcarg_t style)
1479
{
1480
	return rgb_from_style(&vport->attr, (int) style);
1481
}
1482
 
1483
static int fb_set_color(viewport_t *vport, ipcarg_t fg_color,
1484
    ipcarg_t bg_color, ipcarg_t flags)
1485
{
1486
	return rgb_from_idx(&vport->attr, fg_color, bg_color, flags);
1487
}
1488
 
1498 palkovsky 1489
/** Function for handling connections to FB
1490
 *
1491
 */
3707 decky 1492
static void fb_client_connection(ipc_callid_t iid, ipc_call_t *icall)
1363 vana 1493
{
3707 decky 1494
	unsigned int vp = 0;
1495
	viewport_t *vport = &viewports[vp];
1496
 
1485 palkovsky 1497
	if (client_connected) {
2619 jermar 1498
		ipc_answer_0(iid, ELIMIT);
1485 palkovsky 1499
		return;
1500
	}
3707 decky 1501
 
1502
	/* Accept connection */
1503
	client_connected = true;
1504
	ipc_answer_0(iid, EOK);
1505
 
1506
	while (true) {
1507
		ipc_callid_t callid;
1508
		ipc_call_t call;
1509
		int retval;
1510
		unsigned int i;
1511
		int scroll;
4232 svoboda 1512
		wchar_t ch;
3707 decky 1513
		unsigned int row, col;
1514
 
1515
		if ((vport->cursor_active) || (anims_enabled))
2070 jermar 1516
			callid = async_get_call_timeout(&call, 250000);
1640 palkovsky 1517
		else
1518
			callid = async_get_call(&call);
3707 decky 1519
 
1713 palkovsky 1520
		mouse_hide();
1500 palkovsky 1521
		if (!callid) {
1673 palkovsky 1522
			cursor_blink(vport);
1646 palkovsky 1523
			anims_tick();
1723 palkovsky 1524
			mouse_show();
1500 palkovsky 1525
			continue;
1526
		}
3707 decky 1527
 
1547 palkovsky 1528
		if (shm_handle(callid, &call, vp))
1529
			continue;
3707 decky 1530
 
1552 palkovsky 1531
		if (pixmap_handle(callid, &call, vp))
1532
			continue;
3707 decky 1533
 
1646 palkovsky 1534
		if (anim_handle(callid, &call, vp))
1535
			continue;
3707 decky 1536
 
1537
		switch (IPC_GET_METHOD(call)) {
1485 palkovsky 1538
		case IPC_M_PHONE_HUNGUP:
3707 decky 1539
			client_connected = false;
1540
 
1541
			/* Cleanup other viewports */
2025 jermar 1542
			for (i = 1; i < MAX_VIEWPORTS; i++)
3707 decky 1543
				vport->initialized = false;
1544
 
1545
			/* Exit thread */
1546
			return;
1547
 
1485 palkovsky 1548
		case FB_PUTCHAR:
4232 svoboda 1549
			ch = IPC_GET_ARG1(call);
1485 palkovsky 1550
			row = IPC_GET_ARG2(call);
1551
			col = IPC_GET_ARG3(call);
3707 decky 1552
 
1553
			if ((col >= vport->cols) || (row >= vport->rows)) {
1485 palkovsky 1554
				retval = EINVAL;
1555
				break;
1556
			}
2619 jermar 1557
			ipc_answer_0(callid, EOK);
3707 decky 1558
 
4232 svoboda 1559
			draw_char(vport, ch, col, row);
3707 decky 1560
 
1561
			/* Message already answered */
1562
			continue;
1485 palkovsky 1563
		case FB_CLEAR:
3707 decky 1564
			vport_clear(vport);
1565
			cursor_show(vport);
1566
			retval = EOK;
1485 palkovsky 1567
			break;
3707 decky 1568
		case FB_CURSOR_GOTO:
1486 palkovsky 1569
			row = IPC_GET_ARG1(call);
1570
			col = IPC_GET_ARG2(call);
3707 decky 1571
 
1572
			if ((col >= vport->cols) || (row >= vport->rows)) {
1486 palkovsky 1573
				retval = EINVAL;
1574
				break;
1575
			}
3707 decky 1576
			retval = EOK;
1577
 
1673 palkovsky 1578
			cursor_hide(vport);
1486 palkovsky 1579
			vport->cur_col = col;
1580
			vport->cur_row = row;
3707 decky 1581
			cursor_show(vport);
1582
			break;
1486 palkovsky 1583
		case FB_CURSOR_VISIBILITY:
1673 palkovsky 1584
			cursor_hide(vport);
1500 palkovsky 1585
			vport->cursor_active = IPC_GET_ARG1(call);
3707 decky 1586
			cursor_show(vport);
1587
			retval = EOK;
1486 palkovsky 1588
			break;
1485 palkovsky 1589
		case FB_GET_CSIZE:
2619 jermar 1590
			ipc_answer_2(callid, EOK, vport->rows, vport->cols);
1485 palkovsky 1591
			continue;
1486 palkovsky 1592
		case FB_SCROLL:
3707 decky 1593
			scroll = IPC_GET_ARG1(call);
1594
			if ((scroll > (int) vport->rows) || (scroll < (-(int) vport->rows))) {
1486 palkovsky 1595
				retval = EINVAL;
1596
				break;
1597
			}
1673 palkovsky 1598
			cursor_hide(vport);
3707 decky 1599
			vport_scroll(vport, scroll);
1600
			cursor_show(vport);
1601
			retval = EOK;
1486 palkovsky 1602
			break;
1603
		case FB_VIEWPORT_SWITCH:
1604
			i = IPC_GET_ARG1(call);
3707 decky 1605
			if (i >= MAX_VIEWPORTS) {
1486 palkovsky 1606
				retval = EINVAL;
1607
				break;
1608
			}
3707 decky 1609
			if (!viewports[i].initialized) {
1486 palkovsky 1610
				retval = EADDRNOTAVAIL;
1611
				break;
1612
			}
1673 palkovsky 1613
			cursor_hide(vport);
1486 palkovsky 1614
			vp = i;
1615
			vport = &viewports[vp];
3707 decky 1616
			cursor_show(vport);
1617
			retval = EOK;
1486 palkovsky 1618
			break;
1619
		case FB_VIEWPORT_CREATE:
3707 decky 1620
			retval = vport_create(IPC_GET_ARG1(call) >> 16,
2619 jermar 1621
			    IPC_GET_ARG1(call) & 0xffff,
1622
			    IPC_GET_ARG2(call) >> 16,
1623
			    IPC_GET_ARG2(call) & 0xffff);
1486 palkovsky 1624
			break;
1625
		case FB_VIEWPORT_DELETE:
1626
			i = IPC_GET_ARG1(call);
3707 decky 1627
			if (i >= MAX_VIEWPORTS) {
1486 palkovsky 1628
				retval = EINVAL;
1629
				break;
1630
			}
3707 decky 1631
			if (!viewports[i].initialized) {
1486 palkovsky 1632
				retval = EADDRNOTAVAIL;
1633
				break;
1634
			}
3707 decky 1635
			viewports[i].initialized = false;
1636
			if (viewports[i].bgpixel)
1637
				free(viewports[i].bgpixel);
1638
			if (viewports[i].backbuf)
1639
				free(viewports[i].backbuf);
1640
			retval = EOK;
1486 palkovsky 1641
			break;
1642
		case FB_SET_STYLE:
3767 svoboda 1643
			retval = fb_set_style(vport, IPC_GET_ARG1(call));
1644
			break;
1645
		case FB_SET_COLOR:
1646
			retval = fb_set_color(vport, IPC_GET_ARG1(call),
1647
			    IPC_GET_ARG2(call), IPC_GET_ARG3(call));
1648
			break;
1649
		case FB_SET_RGB_COLOR:
1650
			vport->attr.fg_color = IPC_GET_ARG1(call);
1651
			vport->attr.bg_color = IPC_GET_ARG2(call);
3707 decky 1652
			retval = EOK;
1486 palkovsky 1653
			break;
1654
		case FB_GET_RESOLUTION:
2619 jermar 1655
			ipc_answer_2(callid, EOK, screen.xres, screen.yres);
1486 palkovsky 1656
			continue;
1707 palkovsky 1657
		case FB_POINTER_MOVE:
3707 decky 1658
			pointer_enabled = true;
1711 palkovsky 1659
			mouse_move(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
3707 decky 1660
			retval = EOK;
1707 palkovsky 1661
			break;
1485 palkovsky 1662
		default:
1663
			retval = ENOENT;
1664
		}
2619 jermar 1665
		ipc_answer_0(callid, retval);
1363 vana 1666
	}
1485 palkovsky 1667
}
1363 vana 1668
 
3707 decky 1669
/** Initialization of framebuffer
1670
 *
1671
 */
1672
int fb_init(void)
1485 palkovsky 1673
{
1490 palkovsky 1674
	async_set_client_connection(fb_client_connection);
1485 palkovsky 1675
 
3707 decky 1676
	void *fb_ph_addr = (void *) sysinfo_value("fb.address.physical");
1677
	unsigned int fb_offset = sysinfo_value("fb.offset");
1678
	unsigned int fb_width = sysinfo_value("fb.width");
1679
	unsigned int fb_height = sysinfo_value("fb.height");
1680
	unsigned int fb_scanline = sysinfo_value("fb.scanline");
1681
	unsigned int fb_visual = sysinfo_value("fb.visual");
1682
 
1683
	unsigned int fbsize = fb_scanline * fb_height;
1684
	void *fb_addr = as_get_mappable_page(fbsize);
1685
 
3908 decky 1686
	if (physmem_map(fb_ph_addr + fb_offset, fb_addr,
1687
	    ALIGN_UP(fbsize, PAGE_SIZE) >> PAGE_WIDTH, AS_AREA_READ | AS_AREA_WRITE) != 0)
1688
		return -1;
3707 decky 1689
 
1690
	if (screen_init(fb_addr, fb_width, fb_height, fb_scanline, fb_visual))
1993 decky 1691
		return 0;
1692
 
1693
	return -1;
1363 vana 1694
}
1490 palkovsky 1695
 
3707 decky 1696
/**
1649 cejka 1697
 * @}
1698
 */