Subversion Repositories HelenOS

Rev

Rev 3742 | Details | Compare with Previous | Last modification | View Log | RSS feed

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