Subversion Repositories HelenOS-historic

Rev

Rev 1558 | Rev 1562 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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