Subversion Repositories HelenOS

Rev

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