Subversion Repositories HelenOS

Rev

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