Subversion Repositories HelenOS

Rev

Rev 4327 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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