Subversion Repositories HelenOS

Rev

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