Subversion Repositories HelenOS

Rev

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

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