Subversion Repositories HelenOS

Rev

Rev 1552 | Rev 1558 | 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
 
1430 jermar 30
#include <stdlib.h>
31
#include <unistd.h>
32
#include <string.h>
1363 vana 33
#include <ddi.h>
34
#include <sysinfo.h>
35
#include <align.h>
36
#include <as.h>
37
#include <ipc/fb.h>
38
#include <ipc/ipc.h>
1430 jermar 39
#include <ipc/ns.h>
1363 vana 40
#include <ipc/services.h>
41
#include <kernel/errno.h>
1392 palkovsky 42
#include <async.h>
1505 palkovsky 43
 
1404 palkovsky 44
#include "font-8x16.h"
45
#include "helenos.xbm"
1363 vana 46
#include "fb.h"
1505 palkovsky 47
#include "main.h"
48
#include "../console/screenbuffer.h"
1547 palkovsky 49
#include "ppm.h"
1363 vana 50
 
1485 palkovsky 51
#define DEFAULT_BGCOLOR                0x000080
52
#define DEFAULT_FGCOLOR                0xffff00
1363 vana 53
 
54
/***************************************************************/
55
/* Pixel specific fuctions */
56
 
1555 palkovsky 57
typedef void (*conv2scr_fn_t)(void *, int);
58
typedef int (*conv2rgb_fn_t)(void *);
1363 vana 59
 
1485 palkovsky 60
struct {
1363 vana 61
    __u8 *fbaddress ;
62
 
63
    unsigned int xres ;
64
    unsigned int yres ;
65
    unsigned int scanline ;
66
    unsigned int pixelbytes ;
67
 
1555 palkovsky 68
    conv2scr_fn_t rgb2scr;
69
    conv2rgb_fn_t scr2rgb;
1485 palkovsky 70
} screen;
1363 vana 71
 
1485 palkovsky 72
typedef struct {
73
    int initialized;
74
    unsigned int x, y;
75
    unsigned int width, height;
1363 vana 76
 
1485 palkovsky 77
    /* Text support in window */
78
    unsigned int rows, cols;
79
    /* Style for text printing */
1509 palkovsky 80
    style_t style;
1485 palkovsky 81
    /* Auto-cursor position */
1486 palkovsky 82
    int cursor_active, cur_col, cur_row;
1500 palkovsky 83
    int cursor_shown;
1485 palkovsky 84
} viewport_t;
1363 vana 85
 
1552 palkovsky 86
/** Maximum number of saved pixmaps
87
 * Pixmap is a saved rectangle
88
 */
89
#define MAX_PIXMAPS        256
90
typedef struct {
91
    unsigned int width;
92
    unsigned int height;
1555 palkovsky 93
    __u8 *data;
1552 palkovsky 94
} pixmap_t;
95
static pixmap_t pixmaps[MAX_PIXMAPS];
96
 
97
/* Viewport is a rectangular area on the screen */
1485 palkovsky 98
#define MAX_VIEWPORTS 128
99
static viewport_t viewports[128];
100
 
101
/* Allow only 1 connection */
102
static int client_connected = 0;
103
 
1363 vana 104
#define RED(x, bits)    ((x >> (16 + 8 - bits)) & ((1 << bits) - 1))
105
#define GREEN(x, bits)  ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
106
#define BLUE(x, bits)   ((x >> (8 - bits)) & ((1 << bits) - 1))
107
 
1485 palkovsky 108
#define COL_WIDTH   8
109
#define ROW_BYTES   (screen.scanline * FONT_SCANLINES)
1363 vana 110
 
1485 palkovsky 111
#define POINTPOS(x, y)  ((y) * screen.scanline + (x) * screen.pixelbytes)
1363 vana 112
 
1555 palkovsky 113
/* Conversion routines between different color representations */
114
static void rgb_4byte(void *dst, int rgb)
1363 vana 115
{
1555 palkovsky 116
    *(int *)dst = rgb;
1363 vana 117
}
118
 
1555 palkovsky 119
static int byte4_rgb(void *src)
1363 vana 120
{
1555 palkovsky 121
    return (*(int *)src) & 0xffffff;
1363 vana 122
}
123
 
1555 palkovsky 124
static void rgb_3byte(void *dst, int rgb)
1363 vana 125
{
1555 palkovsky 126
    __u8 *scr = dst;
1363 vana 127
#if (defined(BIG_ENDIAN) || defined(FB_BIG_ENDIAN))
1555 palkovsky 128
    scr[0] = RED(rgb, 8);
129
    scr[1] = GREEN(rgb, 8);
130
    scr[2] = BLUE(rgb, 8);
1363 vana 131
#else
1555 palkovsky 132
    scr[2] = RED(rgb, 8);
133
    scr[1] = GREEN(rgb, 8);
134
    scr[0] = BLUE(rgb, 8);
1363 vana 135
#endif
136
 
1555 palkovsky 137
 
1363 vana 138
}
139
 
1555 palkovsky 140
static int byte3_rgb(void *src)
1363 vana 141
{
1555 palkovsky 142
    __u8 *scr = src;
1363 vana 143
#if (defined(BIG_ENDIAN) || defined(FB_BIG_ENDIAN))
1555 palkovsky 144
    return scr[0] << 16 | scr[1] << 8 | scr[2];
1363 vana 145
#else
1555 palkovsky 146
    return scr[2] << 16 | scr[1] << 8 | scr[0];
147
#endif  
1363 vana 148
}
149
 
1555 palkovsky 150
/**  16-bit depth (5:6:5) */
151
static void rgb_2byte(void *dst, int rgb)
1363 vana 152
{
153
    /* 5-bit, 6-bits, 5-bits */
1555 palkovsky 154
    *((__u16 *)(dst)) = RED(rgb, 5) << 11 | GREEN(rgb, 6) << 5 | BLUE(rgb, 5);
1363 vana 155
}
156
 
1555 palkovsky 157
/** 16-bit depth (5:6:5) */
158
static int byte2_rgb(void *src)
1363 vana 159
{
1555 palkovsky 160
    int color = *(__u16 *)(src);
1363 vana 161
    return (((color >> 11) & 0x1f) << (16 + 3)) | (((color >> 5) & 0x3f) << (8 + 2)) | ((color & 0x1f) << 3);
162
}
163
 
164
/** Put pixel - 8-bit depth (3:2:3) */
1555 palkovsky 165
static void rgb_1byte(void *dst, int rgb)
1363 vana 166
{
1555 palkovsky 167
    *(__u8 *)dst = RED(rgb, 3) << 5 | GREEN(rgb, 2) << 3 | BLUE(rgb, 3);
1363 vana 168
}
169
 
170
/** Return pixel color - 8-bit depth (3:2:3) */
1555 palkovsky 171
static int byte1_rgb(void *src)
1363 vana 172
{
1555 palkovsky 173
    int color = *(__u8 *)src;
1363 vana 174
    return (((color >> 5) & 0x7) << (16 + 5)) | (((color >> 3) & 0x3) << (8 + 6)) | ((color & 0x7) << 5);
175
}
176
 
1498 palkovsky 177
/** Put pixel into viewport
178
 *
179
 * @param vp Viewport identification
180
 * @param x X coord relative to viewport
181
 * @param y Y coord relative to viewport
182
 * @param color RGB color
183
 */
1485 palkovsky 184
static void putpixel(int vp, unsigned int x, unsigned int y, int color)
185
{
1555 palkovsky 186
    int dx = viewports[vp].x + x;
187
    int dy = viewports[vp].y + y;
188
    (*screen.rgb2scr)(&screen.fbaddress[POINTPOS(dx,dy)],color);
1485 palkovsky 189
}
1498 palkovsky 190
/** Get pixel from viewport */
1485 palkovsky 191
static int getpixel(int vp, unsigned int x, unsigned int y)
192
{
1555 palkovsky 193
    int dx = viewports[vp].x + x;
194
    int dy = viewports[vp].y + y;
195
 
196
    return (*screen.scr2rgb)(&screen.fbaddress[POINTPOS(dx,dy)]);
1485 palkovsky 197
}
198
 
1363 vana 199
/** Fill line with color BGCOLOR */
1485 palkovsky 200
static void clear_line(int vp, unsigned int y)
1363 vana 201
{
202
    unsigned int x;
1485 palkovsky 203
    for (x = 0; x < viewports[vp].width; x++)
1509 palkovsky 204
        putpixel(vp, x, y, viewports[vp].style.bg_color);
1363 vana 205
}
206
 
1485 palkovsky 207
/** Fill viewport with background color */
208
static void clear_port(int vp)
1363 vana 209
{
210
    unsigned int y;
1485 palkovsky 211
 
212
    clear_line(vp, 0);
213
    for (y = viewports[vp].y+1; y < viewports[vp].y+viewports[vp].height; y++) {
214
        memcpy(&screen.fbaddress[POINTPOS(viewports[vp].x,y)],
215
               &screen.fbaddress[POINTPOS(viewports[vp].x,viewports[vp].y)],
216
               screen.pixelbytes * viewports[vp].width);
1363 vana 217
    }  
218
}
219
 
1485 palkovsky 220
/** Scroll port up/down
221
 *
222
 * @param vp Viewport to scroll
223
 * @param rows Positive number - scroll up, negative - scroll down
224
 */
225
static void scroll_port(int vp, int rows)
1363 vana 226
{
1485 palkovsky 227
    int y;
228
    int startline;
229
    int endline;
230
 
231
    if (rows > 0) {
232
        for (y=viewports[vp].y; y < viewports[vp].y+viewports[vp].height - rows*FONT_SCANLINES; y++)
233
            memcpy(&screen.fbaddress[POINTPOS(viewports[vp].x,y)],
234
                   &screen.fbaddress[POINTPOS(viewports[vp].x,y + rows*FONT_SCANLINES)],
235
                   screen.pixelbytes * viewports[vp].width);
236
        /* Clear last row */
237
        startline = viewports[vp].y+FONT_SCANLINES*(viewports[vp].rows-1);
238
        endline = viewports[vp].y + viewports[vp].height;
239
        clear_line(vp, startline);
240
        for (y=startline+1;y < endline; y++)
241
            memcpy(&screen.fbaddress[POINTPOS(viewports[vp].x,y)],
242
                   &screen.fbaddress[POINTPOS(viewports[vp].x,startline)],
243
                   screen.pixelbytes * viewports[vp].width);
244
 
245
    } else if (rows < 0) {
246
        rows = -rows;
247
        for (y=viewports[vp].y + viewports[vp].height-1; y >= viewports[vp].y + rows*FONT_SCANLINES; y--)
248
            memcpy(&screen.fbaddress[POINTPOS(viewports[vp].x,y)],
249
                &screen.fbaddress[POINTPOS(viewports[vp].x,y - rows*FONT_SCANLINES)],
250
                screen.pixelbytes * viewports[vp].width);
251
        /* Clear first row */
1509 palkovsky 252
        clear_line(0, viewports[vp].style.bg_color);
1485 palkovsky 253
        for (y=1;y < rows*FONT_SCANLINES; y++)
254
            memcpy(&screen.fbaddress[POINTPOS(viewports[vp].x,viewports[vp].y+y)],
255
                   &screen.fbaddress[POINTPOS(viewports[vp].x,viewports[vp].y)],
256
                   screen.pixelbytes * viewports[vp].width);
257
    }
1363 vana 258
}
259
 
1485 palkovsky 260
static void invert_pixel(int vp,unsigned int x, unsigned int y)
1363 vana 261
{
1485 palkovsky 262
    putpixel(vp, x, y, ~getpixel(vp, x, y));
1363 vana 263
}
264
 
265
 
266
/***************************************************************/
267
/* Character-console functions */
268
 
269
/** Draw character at given position */
1509 palkovsky 270
static void draw_glyph(int vp,__u8 glyph, unsigned int sx, unsigned int sy, style_t style)
1363 vana 271
{
1509 palkovsky 272
    int i;
1363 vana 273
    unsigned int y;
1509 palkovsky 274
    unsigned int glline;
1363 vana 275
 
1509 palkovsky 276
    for (y = 0; y < FONT_SCANLINES; y++) {
277
        glline = fb_font[glyph * FONT_SCANLINES + y];
278
        for (i = 0; i < 8; i++) {
279
            if (glline & (1 << (7 - i)))
280
                putpixel(vp, sx + i, sy + y, style.fg_color);
281
            else
282
                putpixel(vp, sx + i, sy + y, style.bg_color);
283
        }
284
    }
1363 vana 285
}
286
 
287
/** Invert character at given position */
1489 palkovsky 288
static void invert_char(int vp,unsigned int row, unsigned int col)
1363 vana 289
{
290
    unsigned int x;
291
    unsigned int y;
292
 
293
    for (x = 0; x < COL_WIDTH; x++)
294
        for (y = 0; y < FONT_SCANLINES; y++)
1485 palkovsky 295
            invert_pixel(vp, col * COL_WIDTH + x, row * FONT_SCANLINES + y);
1363 vana 296
}
297
 
298
/***************************************************************/
299
/* Stdout specific functions */
300
 
301
 
1485 palkovsky 302
/** Create new viewport
1363 vana 303
 *
1485 palkovsky 304
 * @return New viewport number
1363 vana 305
 */
1485 palkovsky 306
static int viewport_create(unsigned int x, unsigned int y,unsigned int width,
307
               unsigned int height)
1363 vana 308
{
1485 palkovsky 309
    int i;
310
 
1498 palkovsky 311
for (i=0; i < MAX_VIEWPORTS; i++) {
1485 palkovsky 312
        if (!viewports[i].initialized)
1363 vana 313
            break;
314
    }
1485 palkovsky 315
    if (i == MAX_VIEWPORTS)
316
        return ELIMIT;
317
 
1486 palkovsky 318
    if (width ==0 || height == 0 ||
319
        x+width > screen.xres || y+height > screen.yres)
320
        return EINVAL;
321
    if (width < FONT_SCANLINES || height < COL_WIDTH)
322
        return EINVAL;
323
 
1485 palkovsky 324
    viewports[i].x = x;
325
    viewports[i].y = y;
326
    viewports[i].width = width;
327
    viewports[i].height = height;
1363 vana 328
 
1485 palkovsky 329
    viewports[i].rows = height / FONT_SCANLINES;
330
    viewports[i].cols = width / COL_WIDTH;
331
 
1509 palkovsky 332
    viewports[i].style.bg_color = DEFAULT_BGCOLOR;
333
    viewports[i].style.fg_color = DEFAULT_FGCOLOR;
1363 vana 334
 
1489 palkovsky 335
    viewports[i].cur_col = 0;
336
    viewports[i].cur_row = 0;
337
    viewports[i].cursor_active = 0;
338
 
1486 palkovsky 339
    viewports[i].initialized = 1;
340
 
1485 palkovsky 341
    return i;
1363 vana 342
}
343
 
344
 
345
/** Initialize framebuffer as a chardev output device
346
 *
347
 * @param addr Address of theframebuffer
348
 * @param x    Screen width in pixels
349
 * @param y    Screen height in pixels
350
 * @param bpp  Bits per pixel (8, 16, 24, 32)
351
 * @param scan Bytes per one scanline
352
 *
353
 */
1501 palkovsky 354
static void screen_init(void *addr, unsigned int xres, unsigned int yres, unsigned int bpp, unsigned int scan)
1363 vana 355
{
356
    switch (bpp) {
357
        case 8:
1555 palkovsky 358
            screen.rgb2scr = rgb_1byte;
359
            screen.scr2rgb = byte1_rgb;
1485 palkovsky 360
            screen.pixelbytes = 1;
1363 vana 361
            break;
362
        case 16:
1555 palkovsky 363
            screen.rgb2scr = rgb_2byte;
364
            screen.scr2rgb = byte2_rgb;
1485 palkovsky 365
            screen.pixelbytes = 2;
1363 vana 366
            break;
367
        case 24:
1555 palkovsky 368
            screen.rgb2scr = rgb_3byte;
369
            screen.scr2rgb = byte3_rgb;
1485 palkovsky 370
            screen.pixelbytes = 3;
1363 vana 371
            break;
372
        case 32:
1555 palkovsky 373
            screen.rgb2scr = rgb_4byte;
374
            screen.scr2rgb = byte4_rgb;
1485 palkovsky 375
            screen.pixelbytes = 4;
1363 vana 376
            break;
377
    }
378
 
379
 
1485 palkovsky 380
    screen.fbaddress = (unsigned char *) addr;
381
    screen.xres = xres;
382
    screen.yres = yres;
383
    screen.scanline = scan;
1363 vana 384
 
1485 palkovsky 385
    /* Create first viewport */
386
    viewport_create(0,0,xres,yres);
387
}
1363 vana 388
 
1552 palkovsky 389
/** Hide cursor if it is shown */
1500 palkovsky 390
static void cursor_hide(int vp)
391
{
392
    viewport_t *vport = &viewports[vp];
393
 
394
    if (vport->cursor_active && vport->cursor_shown) {
395
        invert_char(vp, vport->cur_row, vport->cur_col);
396
        vport->cursor_shown = 0;
397
    }
398
}
399
 
1552 palkovsky 400
/** Show cursor if cursor showing is enabled */
1500 palkovsky 401
static void cursor_print(int vp)
402
{
403
    viewport_t *vport = &viewports[vp];
404
 
405
    /* Do not check for cursor_shown */
406
    if (vport->cursor_active) {
407
        invert_char(vp, vport->cur_row, vport->cur_col);
408
        vport->cursor_shown = 1;
409
    }
410
}
411
 
1552 palkovsky 412
/** Invert cursor, if it is enabled */
1500 palkovsky 413
static void cursor_blink(int vp)
414
{
415
    viewport_t *vport = &viewports[vp];
416
 
417
    if (vport->cursor_shown)
418
        cursor_hide(vp);
419
    else
420
        cursor_print(vp);
421
}
422
 
1498 palkovsky 423
/** Draw character at given position relative to viewport
424
 *
425
 * @param vp Viewport identification
426
 * @param c Character to print
427
 * @param row Screen position relative to viewport
428
 * @param col Screen position relative to viewport
429
 */
1509 palkovsky 430
static void draw_char(int vp, char c, unsigned int row, unsigned int col, style_t style)
1486 palkovsky 431
{
432
    viewport_t *vport = &viewports[vp];
433
 
1500 palkovsky 434
    /* Optimize - do not hide cursor if we are going to overwrite it */
435
    if (vport->cursor_active && vport->cursor_shown &&
436
        (vport->cur_col != col || vport->cur_row != row))
1489 palkovsky 437
        invert_char(vp, vport->cur_row, vport->cur_col);
1486 palkovsky 438
 
1509 palkovsky 439
    draw_glyph(vp, c, col * COL_WIDTH, row * FONT_SCANLINES, style);
1489 palkovsky 440
 
441
    vport->cur_col = col;
442
    vport->cur_row = row;
443
 
444
    vport->cur_col++;
445
    if (vport->cur_col>= vport->cols) {
446
        vport->cur_col = 0;
447
        vport->cur_row++;
448
        if (vport->cur_row >= vport->rows)
449
            vport->cur_row--;
1486 palkovsky 450
    }
1500 palkovsky 451
    cursor_print(vp);
1486 palkovsky 452
}
453
 
1552 palkovsky 454
/** Draw text data to viewport
455
 *
456
 * @param vp Viewport id
457
 * @param data Text data fitting exactly into viewport
458
 */
1505 palkovsky 459
static void draw_text_data(int vp, keyfield_t *data)
460
{
461
    viewport_t *vport = &viewports[vp];
462
    int i;
463
    char c;
1515 palkovsky 464
    int col,row;
1505 palkovsky 465
 
466
    clear_port(vp);
467
    for (i=0; i < vport->cols * vport->rows; i++) {
1509 palkovsky 468
        if (data[i].character == ' ' && style_same(data[i].style,vport->style))
1505 palkovsky 469
            continue;
1515 palkovsky 470
        col = i % vport->cols;
471
        row = i / vport->cols;
472
        draw_glyph(vp, data[i].character, col * COL_WIDTH, row * FONT_SCANLINES, data[i].style);
1505 palkovsky 473
    }
474
    cursor_print(vp);
475
}
476
 
1555 palkovsky 477
 
478
/** Return first free pixmap */
479
static int find_free_pixmap(void)
480
{
481
    int i;
482
 
483
    for (i=0;i < MAX_PIXMAPS;i++)
484
        if (!pixmaps[i].data)
485
            return i;
486
    return -1;
487
}
488
 
489
static void putpixel_pixmap(int pm, unsigned int x, unsigned int y, int color)
490
{
491
    pixmap_t *pmap = &pixmaps[pm];
492
    int pos = (y * pmap->width + x) * screen.pixelbytes;
493
 
494
    (*screen.rgb2scr)(&pmap->data[pos],color);
495
}
496
 
497
/** Create a new pixmap and return appropriate ID */
498
static int shm2pixmap(char *shm, size_t size)
499
{
500
    int pm;
501
    pixmap_t *pmap;
502
 
503
    pm = find_free_pixmap();
504
    if (pm == -1)
505
        return ELIMIT;
506
    pmap = &pixmaps[pm];
507
 
508
    if (ppm_get_data(shm, size, &pmap->width, &pmap->height))
509
        return EINVAL;
510
 
511
    pmap->data = malloc(pmap->width * pmap->height * screen.pixelbytes);
512
    if (!pmap->data)
513
        return ENOMEM;
514
 
515
    ppm_draw(shm, size, 0, 0, pmap->width, pmap->height,
516
         putpixel_pixmap, pm);
517
 
518
    return pm;
519
}
520
 
1552 palkovsky 521
/** Handle shared memory communication calls
522
 *
523
 * Protocol for drawing pixmaps:
524
 * - FB_PREPARE_SHM(client shm identification)
525
 * - IPC_M_SEND_AS_AREA
526
 * - FB_DRAW_PPM(startx,starty)
527
 * - FB_DROP_SHM
528
 *
529
 * Protocol for text drawing
530
 * - IPC_M_SEND_AS_AREA
531
 * - FB_DRAW_TEXT_DATA
532
 *
533
 * @param callid Callid of the current call
534
 * @param call Current call data
535
 * @param vp Active viewport
536
 * @return 0 if the call was not handled byt this function, 1 otherwise
537
 *
538
 * note: this function is not threads safe, you would have
539
 * to redefine static variables with __thread
540
 */
1547 palkovsky 541
static int shm_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
542
{
543
    static keyfield_t *interbuffer = NULL;
544
    static size_t intersize = 0;
1505 palkovsky 545
 
1555 palkovsky 546
    static char *shm = NULL;
547
    static ipcarg_t shm_id = 0;
548
    static size_t shm_size;
1547 palkovsky 549
 
550
    int handled = 1;
551
    int retval = 0;
552
    viewport_t *vport = &viewports[vp];
553
    unsigned int x,y;
554
 
555
    switch (IPC_GET_METHOD(*call)) {
556
    case IPC_M_AS_AREA_SEND:
557
        /* We accept one area for data interchange */
1555 palkovsky 558
        if (IPC_GET_ARG1(*call) == shm_id) {
1547 palkovsky 559
            void *dest = as_get_mappable_page(IPC_GET_ARG2(*call));
1555 palkovsky 560
            shm_size = IPC_GET_ARG2(*call);
1547 palkovsky 561
            if (!ipc_answer_fast(callid, 0, (sysarg_t)dest, 0))
1555 palkovsky 562
                shm = dest;
1547 palkovsky 563
            else
1555 palkovsky 564
                shm_id = 0;
565
            if (shm[0] != 'P')
1547 palkovsky 566
                while (1)
567
                    ;
568
            return 1;
569
        } else {
570
            intersize = IPC_GET_ARG2(*call);
571
            receive_comm_area(callid,call,(void **)&interbuffer);
572
        }
573
        return 1;
574
    case FB_PREPARE_SHM:
1555 palkovsky 575
        if (shm_id)
1547 palkovsky 576
            retval = EBUSY;
577
        else
1555 palkovsky 578
            shm_id = IPC_GET_ARG1(*call);
1547 palkovsky 579
        break;
580
 
581
    case FB_DROP_SHM:
1555 palkovsky 582
        if (shm) {
583
            as_area_destroy(shm);
584
            shm = NULL;
1547 palkovsky 585
        }
1555 palkovsky 586
        shm_id = 0;
1547 palkovsky 587
        break;
1555 palkovsky 588
 
589
    case FB_SHM2PIXMAP:
590
        if (!shm) {
591
            retval = EINVAL;
592
            break;
593
        }
594
        retval = shm2pixmap(shm, shm_size);
595
        break;
1547 palkovsky 596
    case FB_DRAW_PPM:
1555 palkovsky 597
        if (!shm) {
1547 palkovsky 598
            retval = EINVAL;
599
            break;
600
        }
601
        x = IPC_GET_ARG1(*call);
602
        y = IPC_GET_ARG2(*call);
603
        if (x > vport->width || y > vport->height) {
604
            retval = EINVAL;
605
            break;
606
        }
607
 
1555 palkovsky 608
        ppm_draw(shm, shm_size, IPC_GET_ARG1(*call), IPC_GET_ARG2(*call),
1547 palkovsky 609
             vport->width - x, vport->height - y, putpixel, vp);
610
        break;
611
    case FB_DRAW_TEXT_DATA:
612
        if (!interbuffer) {
613
            retval = EINVAL;
614
            break;
615
        }
616
        if (intersize < vport->cols*vport->rows*sizeof(*interbuffer)) {
617
            retval = EINVAL;
618
            break;
619
        }
620
        draw_text_data(vp, interbuffer);
621
        break;
622
    default:
623
        handled = 0;
624
    }
625
 
626
    if (handled)
627
        ipc_answer_fast(callid, retval, 0, 0);
628
    return handled;
629
}
630
 
1552 palkovsky 631
/** Save viewport to pixmap */
632
static int save_vp_to_pixmap(int vp)
633
{
634
    int pm;
635
    pixmap_t *pmap;
636
    viewport_t *vport = &viewports[vp];
637
    int x,y;
638
    int rowsize;
639
    int tmp;
640
 
641
    pm = find_free_pixmap();
642
    if (pm == -1)
643
        return ELIMIT;
644
 
645
    pmap = &pixmaps[pm];
646
    pmap->data = malloc(screen.pixelbytes * vport->width * vport->height);
647
    if (!pmap->data)
648
        return ENOMEM;
649
 
650
    pmap->width = vport->width;
651
    pmap->height = vport->height;
652
 
653
    rowsize = vport->width * screen.pixelbytes;
654
    for (y=0;y < vport->height; y++) {
655
        tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
656
        memcpy(pmap->data + rowsize*y, screen.fbaddress + tmp, rowsize);
657
    }
658
    return pm;
659
}
660
 
661
/** Draw pixmap on screen
662
 *
663
 * @param vp Viewport to draw on
664
 * @param pm Pixmap identifier
665
 */
666
static int draw_pixmap(int vp, int pm)
667
{
668
    pixmap_t *pmap = &pixmaps[pm];
669
    viewport_t *vport = &viewports[vp];
670
    int x,y;
671
    int tmp, srcrowsize;
672
    int realwidth, realheight, realrowsize;
673
 
674
    if (!pmap->data)
675
        return EINVAL;
676
 
677
    realwidth = pmap->width <= vport->width ? pmap->width : vport->width;
678
    realheight = pmap->height <= vport->height ? pmap->height : vport->height;
679
 
680
    srcrowsize = vport->width * screen.pixelbytes;
681
    realrowsize = realwidth * screen.pixelbytes;
682
 
683
    for (y=0; y < realheight; y++) {
684
        tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
685
        memcpy(screen.fbaddress + tmp, pmap->data + y * srcrowsize, realrowsize);
686
    }
687
}
688
 
689
/** Handler for messages concerning pixmap handling */
690
static int pixmap_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
691
{
692
    int handled = 1;
693
    int retval = 0;
694
    int i,nvp;
695
 
696
    switch (IPC_GET_METHOD(*call)) {
697
    case FB_VP_DRAW_PIXMAP:
698
        nvp = IPC_GET_ARG1(*call);
699
        if (nvp == -1)
700
            nvp = vp;
701
        if (nvp < 0 || nvp >= MAX_VIEWPORTS || !viewports[nvp].initialized) {
702
            retval = EINVAL;
703
            break;
704
        }
705
        i = IPC_GET_ARG2(*call);
706
        retval = draw_pixmap(nvp, i);
707
        break;
708
    case FB_VP2PIXMAP:
709
        nvp = IPC_GET_ARG1(*call);
710
        if (nvp == -1)
711
            nvp = vp;
712
        if (nvp < 0 || nvp >= MAX_VIEWPORTS || !viewports[nvp].initialized)
713
            retval = EINVAL;
714
        else
715
            retval = save_vp_to_pixmap(nvp);
716
        break;
717
    case FB_DROP_PIXMAP:
718
        i = IPC_GET_ARG1(*call);
719
        if (i >= MAX_PIXMAPS) {
720
            retval = EINVAL;
721
            break;
722
        }
723
        if (pixmaps[i].data) {
724
            free(pixmaps[i].data);
725
            pixmaps[i].data = NULL;
726
        }
727
        break;
728
    default:
729
        handled = 0;
730
    }
731
 
732
    if (handled)
733
        ipc_answer_fast(callid, retval, 0, 0);
734
    return handled;
735
 
736
}
737
 
1498 palkovsky 738
/** Function for handling connections to FB
739
 *
740
 */
1490 palkovsky 741
static void fb_client_connection(ipc_callid_t iid, ipc_call_t *icall)
1363 vana 742
{
1485 palkovsky 743
    ipc_callid_t callid;
744
    ipc_call_t call;
745
    int retval;
746
    int i;
747
    unsigned int row,col;
748
    char c;
1486 palkovsky 749
 
1485 palkovsky 750
    int vp = 0;
1486 palkovsky 751
    viewport_t *vport = &viewports[0];
1363 vana 752
 
1485 palkovsky 753
    if (client_connected) {
754
        ipc_answer_fast(iid, ELIMIT, 0,0);
755
        return;
756
    }
1486 palkovsky 757
    client_connected = 1;
1485 palkovsky 758
    ipc_answer_fast(iid, 0, 0, 0); /* Accept connection */
1363 vana 759
 
1485 palkovsky 760
    while (1) {
1500 palkovsky 761
        callid = async_get_call_timeout(&call,250000);
762
        if (!callid) {
763
            cursor_blink(vp);
764
            continue;
765
        }
1547 palkovsky 766
        if (shm_handle(callid, &call, vp))
767
            continue;
1552 palkovsky 768
        if (pixmap_handle(callid, &call, vp))
769
            continue;
1547 palkovsky 770
 
1485 palkovsky 771
        switch (IPC_GET_METHOD(call)) {
772
        case IPC_M_PHONE_HUNGUP:
773
            client_connected = 0;
774
            /* cleanup other viewports */
775
            for (i=1; i < MAX_VIEWPORTS; i++)
1486 palkovsky 776
                vport->initialized = 0;
1485 palkovsky 777
            ipc_answer_fast(callid,0,0,0);
778
            return; /* Exit thread */
1505 palkovsky 779
 
1485 palkovsky 780
        case FB_PUTCHAR:
781
            c = IPC_GET_ARG1(call);
782
            row = IPC_GET_ARG2(call);
783
            col = IPC_GET_ARG3(call);
1486 palkovsky 784
            if (row >= vport->rows || col >= vport->cols) {
1485 palkovsky 785
                retval = EINVAL;
786
                break;
787
            }
788
            ipc_answer_fast(callid,0,0,0);
1486 palkovsky 789
 
1509 palkovsky 790
            draw_char(vp, c, row, col, vport->style);
1485 palkovsky 791
            continue; /* msg already answered */
792
        case FB_CLEAR:
793
            clear_port(vp);
1500 palkovsky 794
            cursor_print(vp);
1485 palkovsky 795
            retval = 0;
796
            break;
1486 palkovsky 797
        case FB_CURSOR_GOTO:
798
            row = IPC_GET_ARG1(call);
799
            col = IPC_GET_ARG2(call);
800
            if (row >= vport->rows || col >= vport->cols) {
801
                retval = EINVAL;
802
                break;
803
            }
804
            retval = 0;
1500 palkovsky 805
            cursor_hide(vp);
1486 palkovsky 806
            vport->cur_col = col;
807
            vport->cur_row = row;
1500 palkovsky 808
            cursor_print(vp);
1486 palkovsky 809
            break;
810
        case FB_CURSOR_VISIBILITY:
1500 palkovsky 811
            cursor_hide(vp);
812
            vport->cursor_active = IPC_GET_ARG1(call);
813
            cursor_print(vp);
1486 palkovsky 814
            retval = 0;
815
            break;
1485 palkovsky 816
        case FB_GET_CSIZE:
1486 palkovsky 817
            ipc_answer_fast(callid, 0, vport->rows, vport->cols);
1485 palkovsky 818
            continue;
1486 palkovsky 819
        case FB_SCROLL:
820
            i = IPC_GET_ARG1(call);
821
            if (i > vport->rows || i < (- (int)vport->rows)) {
822
                retval = EINVAL;
823
                break;
824
            }
1500 palkovsky 825
            cursor_hide(vp);
1486 palkovsky 826
            scroll_port(vp, i);
1500 palkovsky 827
            cursor_print(vp);
1486 palkovsky 828
            retval = 0;
829
            break;
830
        case FB_VIEWPORT_SWITCH:
831
            i = IPC_GET_ARG1(call);
832
            if (i < 0 || i >= MAX_VIEWPORTS) {
833
                retval = EINVAL;
834
                break;
835
            }
836
            if (! viewports[i].initialized ) {
837
                retval = EADDRNOTAVAIL;
838
                break;
839
            }
1500 palkovsky 840
            cursor_hide(vp);
1486 palkovsky 841
            vp = i;
842
            vport = &viewports[vp];
1500 palkovsky 843
            cursor_print(vp);
1486 palkovsky 844
            retval = 0;
845
            break;
846
        case FB_VIEWPORT_CREATE:
847
            retval = viewport_create(IPC_GET_ARG1(call) >> 16,
848
                         IPC_GET_ARG1(call) & 0xffff,
849
                         IPC_GET_ARG2(call) >> 16,
850
                         IPC_GET_ARG2(call) & 0xffff);
851
            break;
852
        case FB_VIEWPORT_DELETE:
853
            i = IPC_GET_ARG1(call);
854
            if (i < 0 || i >= MAX_VIEWPORTS) {
855
                retval = EINVAL;
856
                break;
857
            }
858
            if (! viewports[i].initialized ) {
859
                retval = EADDRNOTAVAIL;
860
                break;
861
            }
862
            viewports[i].initialized = 0;
863
            retval = 0;
864
            break;
865
        case FB_SET_STYLE:
1509 palkovsky 866
            vport->style.fg_color = IPC_GET_ARG1(call);
867
            vport->style.bg_color = IPC_GET_ARG2(call);
1486 palkovsky 868
            retval = 0;
869
            break;
870
        case FB_GET_RESOLUTION:
871
            ipc_answer_fast(callid, 0, screen.xres,screen.yres);
872
            continue;
1485 palkovsky 873
        default:
874
            retval = ENOENT;
875
        }
876
        ipc_answer_fast(callid,retval,0,0);
1363 vana 877
    }
1485 palkovsky 878
}
1363 vana 879
 
1498 palkovsky 880
/** Initialization of framebuffer */
1490 palkovsky 881
int fb_init(void)
1485 palkovsky 882
{
1501 palkovsky 883
    void *fb_ph_addr;
1490 palkovsky 884
    unsigned int fb_width;
885
    unsigned int fb_height;
886
    unsigned int fb_bpp;
887
    unsigned int fb_scanline;
1501 palkovsky 888
    void *fb_addr;
889
    size_t asz;
1363 vana 890
 
1490 palkovsky 891
    async_set_client_connection(fb_client_connection);
1363 vana 892
 
1501 palkovsky 893
    fb_ph_addr=(void *)sysinfo_value("fb.address.physical");
1490 palkovsky 894
    fb_width=sysinfo_value("fb.width");
895
    fb_height=sysinfo_value("fb.height");
896
    fb_bpp=sysinfo_value("fb.bpp");
897
    fb_scanline=sysinfo_value("fb.scanline");
898
 
1501 palkovsky 899
    asz = fb_scanline*fb_height;
900
    fb_addr = as_get_mappable_page(asz);
1485 palkovsky 901
 
1501 palkovsky 902
    map_physmem(fb_ph_addr, fb_addr, ALIGN_UP(asz,PAGE_SIZE) >>PAGE_WIDTH,
1490 palkovsky 903
            AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE);
904
 
905
    screen_init(fb_addr, fb_width, fb_height, fb_bpp, fb_scanline);
1363 vana 906
 
1485 palkovsky 907
    return 0;
1363 vana 908
}
1490 palkovsky 909