Subversion Repositories HelenOS

Rev

Rev 1476 | Rev 1486 | 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
 
30
#include <stdio.h>
1430 jermar 31
#include <stdlib.h>
32
#include <unistd.h>
33
#include <string.h>
1363 vana 34
#include <ddi.h>
35
#include <task.h>
36
#include <sysinfo.h>
37
#include <align.h>
38
#include <as.h>
39
#include <ipc/fb.h>
40
#include <ipc/ipc.h>
1430 jermar 41
#include <ipc/ns.h>
1363 vana 42
#include <ipc/services.h>
43
#include <kernel/errno.h>
1392 palkovsky 44
#include <async.h>
1404 palkovsky 45
#include "font-8x16.h"
46
#include "helenos.xbm"
1363 vana 47
#include "fb.h"
48
 
1485 palkovsky 49
#define DEFAULT_BGCOLOR                0x000080
50
#define DEFAULT_FGCOLOR                0xffff00
1363 vana 51
 
52
/***************************************************************/
53
/* Pixel specific fuctions */
54
 
1485 palkovsky 55
typedef void (*putpixel_fn_t)(unsigned int x, unsigned int y, int color);
56
typedef int (*getpixel_fn_t)(unsigned int x, unsigned int y);
1363 vana 57
 
1485 palkovsky 58
struct {
1363 vana 59
    __u8 *fbaddress ;
60
 
61
    unsigned int xres ;
62
    unsigned int yres ;
63
    unsigned int scanline ;
64
    unsigned int pixelbytes ;
65
 
66
    putpixel_fn_t putpixel;
67
    getpixel_fn_t getpixel;
1485 palkovsky 68
} screen;
1363 vana 69
 
1485 palkovsky 70
typedef struct {
71
    int initialized;
72
    unsigned int x, y;
73
    unsigned int width, height;
1363 vana 74
 
1485 palkovsky 75
    /* Text support in window */
76
    unsigned int rows, cols;
77
    /* Style for text printing */
78
    int bgcolor, fgcolor;
79
    /* Auto-cursor position */
80
    int cursor_active, cur_x, cur_y;
81
} viewport_t;
1363 vana 82
 
1485 palkovsky 83
#define MAX_VIEWPORTS 128
84
static viewport_t viewports[128];
85
 
86
/* Allow only 1 connection */
87
static int client_connected = 0;
88
 
1363 vana 89
#define RED(x, bits)    ((x >> (16 + 8 - bits)) & ((1 << bits) - 1))
90
#define GREEN(x, bits)  ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
91
#define BLUE(x, bits)   ((x >> (8 - bits)) & ((1 << bits) - 1))
92
 
1485 palkovsky 93
#define COL_WIDTH   8
94
#define ROW_BYTES   (screen.scanline * FONT_SCANLINES)
1363 vana 95
 
1485 palkovsky 96
#define POINTPOS(x, y)  ((y) * screen.scanline + (x) * screen.pixelbytes)
1363 vana 97
 
98
/** Put pixel - 24-bit depth, 1 free byte */
1485 palkovsky 99
static void putpixel_4byte(unsigned int x, unsigned int y, int color)
1363 vana 100
{
1485 palkovsky 101
    *((__u32 *)(screen.fbaddress + POINTPOS(x, y))) = color;
1363 vana 102
}
103
 
104
/** Return pixel color - 24-bit depth, 1 free byte */
1485 palkovsky 105
static int getpixel_4byte(unsigned int x, unsigned int y)
1363 vana 106
{
1485 palkovsky 107
    return *((__u32 *)(screen.fbaddress + POINTPOS(x, y))) & 0xffffff;
1363 vana 108
}
109
 
110
/** Put pixel - 24-bit depth */
1485 palkovsky 111
static void putpixel_3byte(unsigned int x, unsigned int y, int color)
1363 vana 112
{
113
    unsigned int startbyte = POINTPOS(x, y);
114
 
115
#if (defined(BIG_ENDIAN) || defined(FB_BIG_ENDIAN))
1485 palkovsky 116
    screen.fbaddress[startbyte] = RED(color, 8);
117
    screen.fbaddress[startbyte + 1] = GREEN(color, 8);
118
    screen.fbaddress[startbyte + 2] = BLUE(color, 8);
1363 vana 119
#else
1485 palkovsky 120
    screen.fbaddress[startbyte + 2] = RED(color, 8);
121
    screen.fbaddress[startbyte + 1] = GREEN(color, 8);
122
    screen.fbaddress[startbyte + 0] = BLUE(color, 8);
1363 vana 123
#endif
124
 
125
}
126
 
127
/** Return pixel color - 24-bit depth */
1485 palkovsky 128
static int getpixel_3byte(unsigned int x, unsigned int y)
1363 vana 129
{
130
    unsigned int startbyte = POINTPOS(x, y);
131
 
132
 
133
 
134
#if (defined(BIG_ENDIAN) || defined(FB_BIG_ENDIAN))
1485 palkovsky 135
    return screen.fbaddress[startbyte] << 16 | screen.fbaddress[startbyte + 1] << 8 | screen.fbaddress[startbyte + 2];
1363 vana 136
#else
1485 palkovsky 137
    return screen.fbaddress[startbyte + 2] << 16 | screen.fbaddress[startbyte + 1] << 8 | screen.fbaddress[startbyte + 0];
1363 vana 138
#endif
139
 
140
 
141
}
142
 
143
/** Put pixel - 16-bit depth (5:6:5) */
1485 palkovsky 144
static void putpixel_2byte(unsigned int x, unsigned int y, int color)
1363 vana 145
{
146
    /* 5-bit, 6-bits, 5-bits */
1485 palkovsky 147
    *((__u16 *)(screen.fbaddress + POINTPOS(x, y))) = RED(color, 5) << 11 | GREEN(color, 6) << 5 | BLUE(color, 5);
1363 vana 148
}
149
 
150
/** Return pixel color - 16-bit depth (5:6:5) */
1485 palkovsky 151
static int getpixel_2byte(unsigned int x, unsigned int y)
1363 vana 152
{
1485 palkovsky 153
    int color = *((__u16 *)(screen.fbaddress + POINTPOS(x, y)));
1363 vana 154
    return (((color >> 11) & 0x1f) << (16 + 3)) | (((color >> 5) & 0x3f) << (8 + 2)) | ((color & 0x1f) << 3);
155
}
156
 
157
/** Put pixel - 8-bit depth (3:2:3) */
1485 palkovsky 158
static void putpixel_1byte(unsigned int x, unsigned int y, int color)
1363 vana 159
{
1485 palkovsky 160
    screen.fbaddress[POINTPOS(x, y)] = RED(color, 3) << 5 | GREEN(color, 2) << 3 | BLUE(color, 3);
1363 vana 161
}
162
 
163
/** Return pixel color - 8-bit depth (3:2:3) */
1485 palkovsky 164
static int getpixel_1byte(unsigned int x, unsigned int y)
1363 vana 165
{
1485 palkovsky 166
    int color = screen.fbaddress[POINTPOS(x, y)];
1363 vana 167
    return (((color >> 5) & 0x7) << (16 + 5)) | (((color >> 3) & 0x3) << (8 + 6)) | ((color & 0x7) << 5);
168
}
169
 
1485 palkovsky 170
static void putpixel(int vp, unsigned int x, unsigned int y, int color)
171
{
172
    screen.putpixel(viewports[vp].x + x, viewports[vp].y + y, color);
173
}
174
static int getpixel(int vp, unsigned int x, unsigned int y)
175
{
176
    return screen.getpixel(viewports[vp].x + x, viewports[vp].y + y);
177
}
178
 
1363 vana 179
/** Fill line with color BGCOLOR */
1485 palkovsky 180
static void clear_line(int vp, unsigned int y)
1363 vana 181
{
182
    unsigned int x;
1485 palkovsky 183
    for (x = 0; x < viewports[vp].width; x++)
184
        putpixel(vp, x, y, viewports[vp].bgcolor);
1363 vana 185
}
186
 
1485 palkovsky 187
/** Fill viewport with background color */
188
static void clear_port(int vp)
1363 vana 189
{
190
    unsigned int y;
1485 palkovsky 191
 
192
    clear_line(vp, 0);
193
    for (y = viewports[vp].y+1; y < viewports[vp].y+viewports[vp].height; y++) {
194
        memcpy(&screen.fbaddress[POINTPOS(viewports[vp].x,y)],
195
               &screen.fbaddress[POINTPOS(viewports[vp].x,viewports[vp].y)],
196
               screen.pixelbytes * viewports[vp].width);
1363 vana 197
    }  
198
}
199
 
1485 palkovsky 200
/** Optimized scroll for windows that cover whole lines */
201
//static void scroll_optim(int vp, int rows)
202
//{
203
    /* TODO */
204
//}
1363 vana 205
 
1485 palkovsky 206
/** Scroll port up/down
207
 *
208
 * @param vp Viewport to scroll
209
 * @param rows Positive number - scroll up, negative - scroll down
210
 */
211
static void scroll_port(int vp, int rows)
1363 vana 212
{
1485 palkovsky 213
    int y;
214
    int startline;
215
    int endline;
216
 
217
    if (rows > 0) {
218
        for (y=viewports[vp].y; y < viewports[vp].y+viewports[vp].height - rows*FONT_SCANLINES; y++)
219
            memcpy(&screen.fbaddress[POINTPOS(viewports[vp].x,y)],
220
                   &screen.fbaddress[POINTPOS(viewports[vp].x,y + rows*FONT_SCANLINES)],
221
                   screen.pixelbytes * viewports[vp].width);
222
        /* Clear last row */
223
        startline = viewports[vp].y+FONT_SCANLINES*(viewports[vp].rows-1);
224
        endline = viewports[vp].y + viewports[vp].height;
225
        clear_line(vp, startline);
226
        for (y=startline+1;y < endline; y++)
227
            memcpy(&screen.fbaddress[POINTPOS(viewports[vp].x,y)],
228
                   &screen.fbaddress[POINTPOS(viewports[vp].x,startline)],
229
                   screen.pixelbytes * viewports[vp].width);
230
 
231
    } else if (rows < 0) {
232
        rows = -rows;
233
        for (y=viewports[vp].y + viewports[vp].height-1; y >= viewports[vp].y + rows*FONT_SCANLINES; y--)
234
            memcpy(&screen.fbaddress[POINTPOS(viewports[vp].x,y)],
235
                &screen.fbaddress[POINTPOS(viewports[vp].x,y - rows*FONT_SCANLINES)],
236
                screen.pixelbytes * viewports[vp].width);
237
        /* Clear first row */
238
        clear_line(0, viewports[vp].bgcolor);
239
        for (y=1;y < rows*FONT_SCANLINES; y++)
240
            memcpy(&screen.fbaddress[POINTPOS(viewports[vp].x,viewports[vp].y+y)],
241
                   &screen.fbaddress[POINTPOS(viewports[vp].x,viewports[vp].y)],
242
                   screen.pixelbytes * viewports[vp].width);
243
    }
1363 vana 244
}
245
 
1485 palkovsky 246
static void invert_pixel(int vp,unsigned int x, unsigned int y)
1363 vana 247
{
1485 palkovsky 248
    putpixel(vp, x, y, ~getpixel(vp, x, y));
1363 vana 249
}
250
 
251
 
252
/** Draw one line of glyph at a given position */
1485 palkovsky 253
static void draw_glyph_line(int vp,unsigned int glline, unsigned int x, unsigned int y)
1363 vana 254
{
255
    unsigned int i;
256
 
257
    for (i = 0; i < 8; i++)
258
        if (glline & (1 << (7 - i))) {
1485 palkovsky 259
            putpixel(vp, x + i, y, viewports[vp].fgcolor);
1363 vana 260
        } else
1485 palkovsky 261
            putpixel(vp, x + i, y, viewports[vp].bgcolor);
1363 vana 262
}
263
 
264
/***************************************************************/
265
/* Character-console functions */
266
 
267
/** Draw character at given position */
1485 palkovsky 268
static void draw_glyph(int vp,__u8 glyph, unsigned int row, unsigned int col)
1363 vana 269
{
270
    unsigned int y;
271
 
272
    for (y = 0; y < FONT_SCANLINES; y++)
1485 palkovsky 273
        draw_glyph_line(vp ,fb_font[glyph * FONT_SCANLINES + y], col * COL_WIDTH, row * FONT_SCANLINES + y);
1363 vana 274
}
275
 
276
/** Invert character at given position */
1485 palkovsky 277
static void invert_char(int vp,unsigned int col, unsigned int row)
1363 vana 278
{
279
    unsigned int x;
280
    unsigned int y;
281
 
282
    for (x = 0; x < COL_WIDTH; x++)
283
        for (y = 0; y < FONT_SCANLINES; y++)
1485 palkovsky 284
            invert_pixel(vp, col * COL_WIDTH + x, row * FONT_SCANLINES + y);
1363 vana 285
}
286
 
1485 palkovsky 287
static void draw_logo(int vp,unsigned int startx, unsigned int starty)
1363 vana 288
{
289
    unsigned int x;
290
    unsigned int y;
291
    unsigned int byte;
292
    unsigned int rowbytes;
293
 
294
    rowbytes = (helenos_width - 1) / 8 + 1;
295
 
296
    for (y = 0; y < helenos_height; y++)
297
        for (x = 0; x < helenos_width; x++) {
298
            byte = helenos_bits[rowbytes * y + x / 8];
299
            byte >>= x % 8;
300
            if (byte & 1)
1485 palkovsky 301
                putpixel(vp ,startx + x, starty + y, viewports[vp].fgcolor);
1363 vana 302
        }
303
}
304
 
305
/***************************************************************/
306
/* Stdout specific functions */
307
 
308
 
1485 palkovsky 309
/** Create new viewport
1363 vana 310
 *
1485 palkovsky 311
 * @return New viewport number
1363 vana 312
 */
1485 palkovsky 313
static int viewport_create(unsigned int x, unsigned int y,unsigned int width,
314
               unsigned int height)
1363 vana 315
{
1485 palkovsky 316
    int i;
317
 
318
    for (i=0; i < MAX_VIEWPORTS; i++) {
319
        if (!viewports[i].initialized)
1363 vana 320
            break;
321
    }
1485 palkovsky 322
    if (i == MAX_VIEWPORTS)
323
        return ELIMIT;
324
 
325
    viewports[i].initialized = 1;
326
    viewports[i].x = x;
327
    viewports[i].y = y;
328
    viewports[i].width = width;
329
    viewports[i].height = height;
1363 vana 330
 
1485 palkovsky 331
    viewports[i].rows = height / FONT_SCANLINES;
332
    viewports[i].cols = width / COL_WIDTH;
333
 
334
    viewports[i].bgcolor = DEFAULT_BGCOLOR;
335
    viewports[i].fgcolor = DEFAULT_FGCOLOR;
1363 vana 336
 
1485 palkovsky 337
    return i;
1363 vana 338
}
339
 
340
 
341
/** Initialize framebuffer as a chardev output device
342
 *
343
 * @param addr Address of theframebuffer
344
 * @param x    Screen width in pixels
345
 * @param y    Screen height in pixels
346
 * @param bpp  Bits per pixel (8, 16, 24, 32)
347
 * @param scan Bytes per one scanline
348
 *
349
 */
1485 palkovsky 350
static void screen_init(__address addr, unsigned int xres, unsigned int yres, unsigned int bpp, unsigned int scan)
1363 vana 351
{
352
    switch (bpp) {
353
        case 8:
1485 palkovsky 354
            screen.putpixel = putpixel_1byte;
355
            screen.getpixel = getpixel_1byte;
356
            screen.pixelbytes = 1;
1363 vana 357
            break;
358
        case 16:
1485 palkovsky 359
            screen.putpixel = putpixel_2byte;
360
            screen.getpixel = getpixel_2byte;
361
            screen.pixelbytes = 2;
1363 vana 362
            break;
363
        case 24:
1485 palkovsky 364
            screen.putpixel = putpixel_3byte;
365
            screen.getpixel = getpixel_3byte;
366
            screen.pixelbytes = 3;
1363 vana 367
            break;
368
        case 32:
1485 palkovsky 369
            screen.putpixel = putpixel_4byte;
370
            screen.getpixel = getpixel_4byte;
371
            screen.pixelbytes = 4;
1363 vana 372
            break;
373
    }
374
 
375
 
1485 palkovsky 376
    screen.fbaddress = (unsigned char *) addr;
377
    screen.xres = xres;
378
    screen.yres = yres;
379
    screen.scanline = scan;
1363 vana 380
 
1485 palkovsky 381
    /* Create first viewport */
382
    viewport_create(0,0,xres,yres);
1363 vana 383
 
1485 palkovsky 384
    clear_port(0);
385
}
1363 vana 386
 
1485 palkovsky 387
static int init_fb(void)
388
{
389
    __address fb_ph_addr;
390
    unsigned int fb_width;
391
    unsigned int fb_height;
392
    unsigned int fb_bpp;
393
    unsigned int fb_scanline;
394
    __address fb_addr;
395
    int a=0;
396
    int i,j,k;
397
    int w;
398
    char text[]="HelenOS Framebuffer driver\non Virtual Framebuffer\nVFB ";
1363 vana 399
 
1485 palkovsky 400
    fb_ph_addr=sysinfo_value("fb.address.physical");
401
    fb_width=sysinfo_value("fb.width");
402
    fb_height=sysinfo_value("fb.height");
403
    fb_bpp=sysinfo_value("fb.bpp");
404
    fb_scanline=sysinfo_value("fb.scanline");
1363 vana 405
 
1485 palkovsky 406
    fb_addr=ALIGN_UP(((__address)set_maxheapsize(USER_ADDRESS_SPACE_SIZE_ARCH>>1)),PAGE_SIZE);
407
 
408
    map_physmem(task_get_id(),(void *)((__address)fb_ph_addr),(void *)fb_addr,
409
            (fb_scanline*fb_height+PAGE_SIZE-1)>>PAGE_WIDTH,
410
            AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE);
411
 
412
    screen_init(fb_addr, fb_width, fb_height, fb_bpp, fb_scanline);
1363 vana 413
 
1485 palkovsky 414
    return 0;
1363 vana 415
}
416
 
1485 palkovsky 417
void client_connection(ipc_callid_t iid, ipc_call_t *icall)
1363 vana 418
{
1485 palkovsky 419
    ipc_callid_t callid;
420
    ipc_call_t call;
421
    int retval;
422
    int i;
423
    unsigned int row,col;
424
    char c;
425
    int vp = 0;
1363 vana 426
 
1485 palkovsky 427
    if (client_connected) {
428
        ipc_answer_fast(iid, ELIMIT, 0,0);
429
        return;
430
    }
431
    ipc_answer_fast(iid, 0, 0, 0); /* Accept connection */
1363 vana 432
 
1485 palkovsky 433
    while (1) {
434
        callid = async_get_call(&call);
435
        switch (IPC_GET_METHOD(call)) {
436
        case IPC_M_PHONE_HUNGUP:
437
            client_connected = 0;
438
            /* cleanup other viewports */
439
            for (i=1; i < MAX_VIEWPORTS; i++)
440
                viewports[i].initialized = 0;
441
            ipc_answer_fast(callid,0,0,0);
442
            return; /* Exit thread */
443
        case FB_PUTCHAR:
444
            c = IPC_GET_ARG1(call);
445
            row = IPC_GET_ARG2(call);
446
            col = IPC_GET_ARG3(call);
447
            if (row >= viewports[vp].rows || col >= viewports[vp].cols) {
448
                retval = EINVAL;
449
                break;
450
            }
451
            ipc_answer_fast(callid,0,0,0);
452
            draw_glyph(vp,c, row, col);
453
            continue; /* msg already answered */
454
        case FB_CLEAR:
455
            clear_port(vp);
456
            retval = 0;
457
            break;
458
/*      case FB_CURSOR_GOTO: */
459
/*          retval = 0; */
460
/*          break; */
461
/*      case FB_CURSOR_VISIBILITY: */
462
/*          retval = 0; */
463
/*          break; */
464
        case FB_GET_CSIZE:
465
            ipc_answer_fast(callid, 0, viewports[vp].rows, viewports[vp].cols);
466
            continue;
467
        default:
468
            retval = ENOENT;
469
        }
470
        retval = ENOENT;
471
        ipc_answer_fast(callid,retval,0,0);
1363 vana 472
    }
1485 palkovsky 473
}
1363 vana 474
 
1485 palkovsky 475
int main(int argc, char *argv[])
476
{
477
    char connected = 0;
478
    int res;
479
    ipcarg_t phonead;
1363 vana 480
 
1485 palkovsky 481
    if(!sysinfo_value("fb"))
482
        return -1;
1363 vana 483
 
1485 palkovsky 484
    if ((res = ipc_connect_to_me(PHONE_NS, SERVICE_VIDEO, 0, &phonead)) != 0)
485
        return -1;
486
 
487
    if (init_fb() != 0)
488
        return -1;
1363 vana 489
 
1485 palkovsky 490
    async_manager();
491
    /* Never reached */
492
    return 0;
1363 vana 493
}