Subversion Repositories HelenOS

Rev

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