Subversion Repositories HelenOS

Rev

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

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