Subversion Repositories HelenOS

Rev

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