Subversion Repositories HelenOS

Rev

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