Subversion Repositories HelenOS

Rev

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