Subversion Repositories HelenOS-historic

Rev

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

Rev 1562 Rev 1567
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,
207
static void draw_rectangle(int vp, unsigned int sx, unsigned int sy,
208
               unsigned int width, unsigned int height,
208
               unsigned int width, unsigned int height,
209
               int color)
209
               int color)
210
{
210
{
211
    unsigned int x, y;
211
    unsigned int x, y;
212
 
212
 
213
    /* Clear first line */
213
    /* Clear first line */
214
    for (x = 0; x < width; x++)
214
    for (x = 0; x < width; x++)
215
        putpixel(vp, sx + x, sy, color);
215
        putpixel(vp, sx + x, sy, color);
216
 
216
 
217
    /* Recompute to screen coords */
217
    /* Recompute to screen coords */
218
    sx += viewports[vp].x;
218
    sx += viewports[vp].x;
219
    sy += viewports[vp].y;
219
    sy += viewports[vp].y;
220
    /* Copy the rest */
220
    /* Copy the rest */
221
    for (y = sy+1;y < sy+height; y++)
221
    for (y = sy+1;y < sy+height; y++)
222
        memcpy(&screen.fbaddress[POINTPOS(sx,y)],
222
        memcpy(&screen.fbaddress[POINTPOS(sx,y)],
223
               &screen.fbaddress[POINTPOS(sx,sy)],
223
               &screen.fbaddress[POINTPOS(sx,sy)],
224
               screen.pixelbytes * width);
224
               screen.pixelbytes * width);
225
 
225
 
226
}
226
}
227
 
227
 
228
/** Fill viewport with background color */
228
/** Fill viewport with background color */
229
static void clear_port(int vp)
229
static void clear_port(int vp)
230
{
230
{
231
    viewport_t *vport = &viewports[vp];
231
    viewport_t *vport = &viewports[vp];
232
 
232
 
233
    draw_rectangle(vp, 0, 0, vport->width, vport->height, vport->style.bg_color);
233
    draw_rectangle(vp, 0, 0, vport->width, vport->height, vport->style.bg_color);
234
}
234
}
235
 
235
 
236
/** Scroll port up/down
236
/** Scroll port up/down
237
 *
237
 *
238
 * @param vp Viewport to scroll
238
 * @param vp Viewport to scroll
239
 * @param rows Positive number - scroll up, negative - scroll down
239
 * @param rows Positive number - scroll up, negative - scroll down
240
 */
240
 */
241
static void scroll_port(int vp, int rows)
241
static void scroll_port(int vp, int rows)
242
{
242
{
243
    int y;
243
    int y;
244
    int startline;
244
    int startline;
245
    int endline;
245
    int endline;
246
    viewport_t *vport = &viewports[vp];
246
    viewport_t *vport = &viewports[vp];
247
   
247
   
248
    if (rows > 0) {
248
    if (rows > 0) {
249
        for (y=vport->y; y < vport->y+vport->height - rows*FONT_SCANLINES; y++)
249
        for (y=vport->y; y < vport->y+vport->height - rows*FONT_SCANLINES; y++)
250
            memcpy(&screen.fbaddress[POINTPOS(vport->x,y)],
250
            memcpy(&screen.fbaddress[POINTPOS(vport->x,y)],
251
                   &screen.fbaddress[POINTPOS(vport->x,y + rows*FONT_SCANLINES)],
251
                   &screen.fbaddress[POINTPOS(vport->x,y + rows*FONT_SCANLINES)],
252
                   screen.pixelbytes * vport->width);
252
                   screen.pixelbytes * vport->width);
253
        draw_rectangle(vp, 0, FONT_SCANLINES*(vport->rows - 1),
253
        draw_rectangle(vp, 0, FONT_SCANLINES*(vport->rows - 1),
254
                   vport->width, FONT_SCANLINES, vport->style.bg_color);
254
                   vport->width, FONT_SCANLINES, vport->style.bg_color);
255
    } else if (rows < 0) {
255
    } else if (rows < 0) {
256
        rows = -rows;
256
        rows = -rows;
257
        for (y=vport->y + vport->height-1; y >= vport->y + rows*FONT_SCANLINES; y--)
257
        for (y=vport->y + vport->height-1; y >= vport->y + rows*FONT_SCANLINES; y--)
258
            memcpy(&screen.fbaddress[POINTPOS(vport->x,y)],
258
            memcpy(&screen.fbaddress[POINTPOS(vport->x,y)],
259
                &screen.fbaddress[POINTPOS(vport->x,y - rows*FONT_SCANLINES)],
259
                &screen.fbaddress[POINTPOS(vport->x,y - rows*FONT_SCANLINES)],
260
                screen.pixelbytes * vport->width);
260
                screen.pixelbytes * vport->width);
261
        draw_rectangle(vp, 0, 0, vport->width, FONT_SCANLINES, vport->style.bg_color);
261
        draw_rectangle(vp, 0, 0, vport->width, FONT_SCANLINES, vport->style.bg_color);
262
    }
262
    }
263
}
263
}
264
 
264
 
265
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)
266
{
266
{
267
    putpixel(vp, x, y, ~getpixel(vp, x, y));
267
    putpixel(vp, x, y, ~getpixel(vp, x, y));
268
}
268
}
269
 
269
 
270
 
270
 
271
/***************************************************************/
271
/***************************************************************/
272
/* Character-console functions */
272
/* Character-console functions */
273
 
273
 
274
/** Draw character at given position
274
/** Draw character at given position
275
 *
275
 *
276
 * @param vp Viewport where the character is printed
276
 * @param vp Viewport where the character is printed
277
 * @param sx Coordinates of top-left of the character
277
 * @param sx Coordinates of top-left of the character
278
 * @param sy Coordinates of top-left of the character
278
 * @param sy Coordinates of top-left of the character
279
 * @param style Color of the character
279
 * @param style Color of the character
280
 * @param transparent If false, print background color
280
 * @param transparent If false, print background color
281
 */
281
 */
282
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,
283
               style_t style, int transparent)
283
               style_t style, int transparent)
284
{
284
{
285
    int i;
285
    int i;
286
    unsigned int y;
286
    unsigned int y;
287
    unsigned int glline;
287
    unsigned int glline;
288
 
288
 
289
    for (y = 0; y < FONT_SCANLINES; y++) {
289
    for (y = 0; y < FONT_SCANLINES; y++) {
290
        glline = fb_font[glyph * FONT_SCANLINES + y];
290
        glline = fb_font[glyph * FONT_SCANLINES + y];
291
        for (i = 0; i < 8; i++) {
291
        for (i = 0; i < 8; i++) {
292
            if (glline & (1 << (7 - i)))
292
            if (glline & (1 << (7 - i)))
293
                putpixel(vp, sx + i, sy + y, style.fg_color);
293
                putpixel(vp, sx + i, sy + y, style.fg_color);
294
            else if (!transparent)
294
            else if (!transparent)
295
                putpixel(vp, sx + i, sy + y, style.bg_color);
295
                putpixel(vp, sx + i, sy + y, style.bg_color);
296
        }
296
        }
297
    }
297
    }
298
}
298
}
299
 
299
 
300
/** Invert character at given position */
300
/** Invert character at given position */
301
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)
302
{
302
{
303
    unsigned int x;
303
    unsigned int x;
304
    unsigned int y;
304
    unsigned int y;
305
 
305
 
306
    for (x = 0; x < COL_WIDTH; x++)
306
    for (x = 0; x < COL_WIDTH; x++)
307
        for (y = 0; y < FONT_SCANLINES; y++)
307
        for (y = 0; y < FONT_SCANLINES; y++)
308
            invert_pixel(vp, col * COL_WIDTH + x, row * FONT_SCANLINES + y);
308
            invert_pixel(vp, col * COL_WIDTH + x, row * FONT_SCANLINES + y);
309
}
309
}
310
 
310
 
311
/***************************************************************/
311
/***************************************************************/
312
/* Stdout specific functions */
312
/* Stdout specific functions */
313
 
313
 
314
 
314
 
315
/** Create new viewport
315
/** Create new viewport
316
 *
316
 *
317
 * @return New viewport number
317
 * @return New viewport number
318
 */
318
 */
319
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,
320
               unsigned int height)
320
               unsigned int height)
321
{
321
{
322
    int i;
322
    int i;
323
 
323
 
324
for (i=0; i < MAX_VIEWPORTS; i++) {
324
for (i=0; i < MAX_VIEWPORTS; i++) {
325
        if (!viewports[i].initialized)
325
        if (!viewports[i].initialized)
326
            break;
326
            break;
327
    }
327
    }
328
    if (i == MAX_VIEWPORTS)
328
    if (i == MAX_VIEWPORTS)
329
        return ELIMIT;
329
        return ELIMIT;
330
 
330
 
331
    if (width ==0 || height == 0 ||
331
    if (width ==0 || height == 0 ||
332
        x+width > screen.xres || y+height > screen.yres)
332
        x+width > screen.xres || y+height > screen.yres)
333
        return EINVAL;
333
        return EINVAL;
334
    if (width < FONT_SCANLINES || height < COL_WIDTH)
334
    if (width < FONT_SCANLINES || height < COL_WIDTH)
335
        return EINVAL;
335
        return EINVAL;
336
 
336
 
337
    viewports[i].x = x;
337
    viewports[i].x = x;
338
    viewports[i].y = y;
338
    viewports[i].y = y;
339
    viewports[i].width = width;
339
    viewports[i].width = width;
340
    viewports[i].height = height;
340
    viewports[i].height = height;
341
   
341
   
342
    viewports[i].rows = height / FONT_SCANLINES;
342
    viewports[i].rows = height / FONT_SCANLINES;
343
    viewports[i].cols = width / COL_WIDTH;
343
    viewports[i].cols = width / COL_WIDTH;
344
 
344
 
345
    viewports[i].style.bg_color = DEFAULT_BGCOLOR;
345
    viewports[i].style.bg_color = DEFAULT_BGCOLOR;
346
    viewports[i].style.fg_color = DEFAULT_FGCOLOR;
346
    viewports[i].style.fg_color = DEFAULT_FGCOLOR;
347
   
347
   
348
    viewports[i].cur_col = 0;
348
    viewports[i].cur_col = 0;
349
    viewports[i].cur_row = 0;
349
    viewports[i].cur_row = 0;
350
    viewports[i].cursor_active = 0;
350
    viewports[i].cursor_active = 0;
351
 
351
 
352
    viewports[i].initialized = 1;
352
    viewports[i].initialized = 1;
353
 
353
 
354
    return i;
354
    return i;
355
}
355
}
356
 
356
 
357
 
357
 
358
/** Initialize framebuffer as a chardev output device
358
/** Initialize framebuffer as a chardev output device
359
 *
359
 *
360
 * @param addr Address of theframebuffer
360
 * @param addr Address of theframebuffer
361
 * @param x    Screen width in pixels
361
 * @param x    Screen width in pixels
362
 * @param y    Screen height in pixels
362
 * @param y    Screen height in pixels
363
 * @param bpp  Bits per pixel (8, 16, 24, 32)
363
 * @param bpp  Bits per pixel (8, 16, 24, 32)
364
 * @param scan Bytes per one scanline
364
 * @param scan Bytes per one scanline
365
 *
365
 *
366
 */
366
 */
367
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)
368
{
368
{
369
    switch (bpp) {
369
    switch (bpp) {
370
        case 8:
370
        case 8:
371
            screen.rgb2scr = rgb_1byte;
371
            screen.rgb2scr = rgb_1byte;
372
            screen.scr2rgb = byte1_rgb;
372
            screen.scr2rgb = byte1_rgb;
373
            screen.pixelbytes = 1;
373
            screen.pixelbytes = 1;
374
            break;
374
            break;
375
        case 16:
375
        case 16:
376
            screen.rgb2scr = rgb_2byte;
376
            screen.rgb2scr = rgb_2byte;
377
            screen.scr2rgb = byte2_rgb;
377
            screen.scr2rgb = byte2_rgb;
378
            screen.pixelbytes = 2;
378
            screen.pixelbytes = 2;
379
            break;
379
            break;
380
        case 24:
380
        case 24:
381
            screen.rgb2scr = rgb_3byte;
381
            screen.rgb2scr = rgb_3byte;
382
            screen.scr2rgb = byte3_rgb;
382
            screen.scr2rgb = byte3_rgb;
383
            screen.pixelbytes = 3;
383
            screen.pixelbytes = 3;
384
            break;
384
            break;
385
        case 32:
385
        case 32:
386
            screen.rgb2scr = rgb_4byte;
386
            screen.rgb2scr = rgb_4byte;
387
            screen.scr2rgb = byte4_rgb;
387
            screen.scr2rgb = byte4_rgb;
388
            screen.pixelbytes = 4;
388
            screen.pixelbytes = 4;
389
            break;
389
            break;
390
    }
390
    }
391
 
391
 
392
       
392
       
393
    screen.fbaddress = (unsigned char *) addr;
393
    screen.fbaddress = (unsigned char *) addr;
394
    screen.xres = xres;
394
    screen.xres = xres;
395
    screen.yres = yres;
395
    screen.yres = yres;
396
    screen.scanline = scan;
396
    screen.scanline = scan;
397
   
397
   
398
    /* Create first viewport */
398
    /* Create first viewport */
399
    viewport_create(0,0,xres,yres);
399
    viewport_create(0,0,xres,yres);
400
}
400
}
401
 
401
 
402
/** Hide cursor if it is shown */
402
/** Hide cursor if it is shown */
403
static void cursor_hide(int vp)
403
static void cursor_hide(int vp)
404
{
404
{
405
    viewport_t *vport = &viewports[vp];
405
    viewport_t *vport = &viewports[vp];
406
 
406
 
407
    if (vport->cursor_active && vport->cursor_shown) {
407
    if (vport->cursor_active && vport->cursor_shown) {
408
        invert_char(vp, vport->cur_row, vport->cur_col);
408
        invert_char(vp, vport->cur_row, vport->cur_col);
409
        vport->cursor_shown = 0;
409
        vport->cursor_shown = 0;
410
    }
410
    }
411
}
411
}
412
 
412
 
413
/** Show cursor if cursor showing is enabled */
413
/** Show cursor if cursor showing is enabled */
414
static void cursor_print(int vp)
414
static void cursor_print(int vp)
415
{
415
{
416
    viewport_t *vport = &viewports[vp];
416
    viewport_t *vport = &viewports[vp];
417
 
417
 
418
    /* Do not check for cursor_shown */
418
    /* Do not check for cursor_shown */
419
    if (vport->cursor_active) {
419
    if (vport->cursor_active) {
420
        invert_char(vp, vport->cur_row, vport->cur_col);
420
        invert_char(vp, vport->cur_row, vport->cur_col);
421
        vport->cursor_shown = 1;
421
        vport->cursor_shown = 1;
422
    }
422
    }
423
}
423
}
424
 
424
 
425
/** Invert cursor, if it is enabled */
425
/** Invert cursor, if it is enabled */
426
static void cursor_blink(int vp)
426
static void cursor_blink(int vp)
427
{
427
{
428
    viewport_t *vport = &viewports[vp];
428
    viewport_t *vport = &viewports[vp];
429
 
429
 
430
    if (vport->cursor_shown)
430
    if (vport->cursor_shown)
431
        cursor_hide(vp);
431
        cursor_hide(vp);
432
    else
432
    else
433
        cursor_print(vp);
433
        cursor_print(vp);
434
}
434
}
435
 
435
 
436
/** Draw character at given position relative to viewport
436
/** Draw character at given position relative to viewport
437
 *
437
 *
438
 * @param vp Viewport identification
438
 * @param vp Viewport identification
439
 * @param c Character to print
439
 * @param c Character to print
440
 * @param row Screen position relative to viewport
440
 * @param row Screen position relative to viewport
441
 * @param col Screen position relative to viewport
441
 * @param col Screen position relative to viewport
442
 * @param transparent If false, print background color with character
442
 * @param transparent If false, print background color with character
443
 */
443
 */
444
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)
445
{
445
{
446
    viewport_t *vport = &viewports[vp];
446
    viewport_t *vport = &viewports[vp];
447
 
447
 
448
    /* 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 */
449
    if (vport->cursor_active && vport->cursor_shown &&
449
    if (vport->cursor_active && vport->cursor_shown &&
450
        (vport->cur_col != col || vport->cur_row != row))
450
        (vport->cur_col != col || vport->cur_row != row))
451
        invert_char(vp, vport->cur_row, vport->cur_col);
451
        invert_char(vp, vport->cur_row, vport->cur_col);
452
   
452
   
453
    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);
454
 
454
 
455
    vport->cur_col = col;
455
    vport->cur_col = col;
456
    vport->cur_row = row;
456
    vport->cur_row = row;
457
 
457
 
458
    vport->cur_col++;
458
    vport->cur_col++;
459
    if (vport->cur_col>= vport->cols) {
459
    if (vport->cur_col>= vport->cols) {
460
        vport->cur_col = 0;
460
        vport->cur_col = 0;
461
        vport->cur_row++;
461
        vport->cur_row++;
462
        if (vport->cur_row >= vport->rows)
462
        if (vport->cur_row >= vport->rows)
463
            vport->cur_row--;
463
            vport->cur_row--;
464
    }
464
    }
465
    cursor_print(vp);
465
    cursor_print(vp);
466
}
466
}
467
 
467
 
468
/** Draw text data to viewport
468
/** Draw text data to viewport
469
 *
469
 *
470
 * @param vp Viewport id
470
 * @param vp Viewport id
471
 * @param data Text data fitting exactly into viewport
471
 * @param data Text data fitting exactly into viewport
472
 */
472
 */
473
static void draw_text_data(int vp, keyfield_t *data)
473
static void draw_text_data(int vp, keyfield_t *data)
474
{
474
{
475
    viewport_t *vport = &viewports[vp];
475
    viewport_t *vport = &viewports[vp];
476
    int i;
476
    int i;
477
    char c;
477
    char c;
478
    int col,row;
478
    int col,row;
479
 
479
 
480
    clear_port(vp);
480
    clear_port(vp);
481
    for (i=0; i < vport->cols * vport->rows; i++) {
481
    for (i=0; i < vport->cols * vport->rows; i++) {
482
        if (data[i].character == ' ' && style_same(data[i].style,vport->style))
482
        if (data[i].character == ' ' && style_same(data[i].style,vport->style))
483
            continue;
483
            continue;
484
        col = i % vport->cols;
484
        col = i % vport->cols;
485
        row = i / vport->cols;
485
        row = i / vport->cols;
486
        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,
487
               data[i].style, style_same(data[i].style,vport->style));
487
               data[i].style, style_same(data[i].style,vport->style));
488
    }
488
    }
489
    cursor_print(vp);
489
    cursor_print(vp);
490
}
490
}
491
 
491
 
492
 
492
 
493
/** Return first free pixmap */
493
/** Return first free pixmap */
494
static int find_free_pixmap(void)
494
static int find_free_pixmap(void)
495
{
495
{
496
    int i;
496
    int i;
497
   
497
   
498
    for (i=0;i < MAX_PIXMAPS;i++)
498
    for (i=0;i < MAX_PIXMAPS;i++)
499
        if (!pixmaps[i].data)
499
        if (!pixmaps[i].data)
500
            return i;
500
            return i;
501
    return -1;
501
    return -1;
502
}
502
}
503
 
503
 
504
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)
505
{
505
{
506
    pixmap_t *pmap = &pixmaps[pm];
506
    pixmap_t *pmap = &pixmaps[pm];
507
    int pos = (y * pmap->width + x) * screen.pixelbytes;
507
    int pos = (y * pmap->width + x) * screen.pixelbytes;
508
 
508
 
509
    (*screen.rgb2scr)(&pmap->data[pos],color);
509
    (*screen.rgb2scr)(&pmap->data[pos],color);
510
}
510
}
511
 
511
 
512
/** Create a new pixmap and return appropriate ID */
512
/** Create a new pixmap and return appropriate ID */
513
static int shm2pixmap(char *shm, size_t size)
513
static int shm2pixmap(char *shm, size_t size)
514
{
514
{
515
    int pm;
515
    int pm;
516
    pixmap_t *pmap;
516
    pixmap_t *pmap;
517
 
517
 
518
    pm = find_free_pixmap();
518
    pm = find_free_pixmap();
519
    if (pm == -1)
519
    if (pm == -1)
520
        return ELIMIT;
520
        return ELIMIT;
521
    pmap = &pixmaps[pm];
521
    pmap = &pixmaps[pm];
522
   
522
   
523
    if (ppm_get_data(shm, size, &pmap->width, &pmap->height))
523
    if (ppm_get_data(shm, size, &pmap->width, &pmap->height))
524
        return EINVAL;
524
        return EINVAL;
525
   
525
   
526
    pmap->data = malloc(pmap->width * pmap->height * screen.pixelbytes);
526
    pmap->data = malloc(pmap->width * pmap->height * screen.pixelbytes);
527
    if (!pmap->data)
527
    if (!pmap->data)
528
        return ENOMEM;
528
        return ENOMEM;
529
 
529
 
530
    ppm_draw(shm, size, 0, 0, pmap->width, pmap->height,
530
    ppm_draw(shm, size, 0, 0, pmap->width, pmap->height,
531
         putpixel_pixmap, pm);
531
         putpixel_pixmap, pm);
532
 
532
 
533
    return pm;
533
    return pm;
534
}
534
}
535
 
535
 
536
/** Handle shared memory communication calls
536
/** Handle shared memory communication calls
537
 *
537
 *
538
 * Protocol for drawing pixmaps:
538
 * Protocol for drawing pixmaps:
539
 * - FB_PREPARE_SHM(client shm identification)
539
 * - FB_PREPARE_SHM(client shm identification)
540
 * - IPC_M_SEND_AS_AREA
540
 * - IPC_M_SEND_AS_AREA
541
 * - FB_DRAW_PPM(startx,starty)
541
 * - FB_DRAW_PPM(startx,starty)
542
 * - FB_DROP_SHM
542
 * - FB_DROP_SHM
543
 *
543
 *
544
 * Protocol for text drawing
544
 * Protocol for text drawing
545
 * - IPC_M_SEND_AS_AREA
545
 * - IPC_M_SEND_AS_AREA
546
 * - FB_DRAW_TEXT_DATA
546
 * - FB_DRAW_TEXT_DATA
547
 *
547
 *
548
 * @param callid Callid of the current call
548
 * @param callid Callid of the current call
549
 * @param call Current call data
549
 * @param call Current call data
550
 * @param vp Active viewport
550
 * @param vp Active viewport
551
 * @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
552
 *
552
 *
553
 * note: this function is not threads safe, you would have
553
 * note: this function is not threads safe, you would have
554
 * to redefine static variables with __thread
554
 * to redefine static variables with __thread
555
 */
555
 */
556
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)
557
{
557
{
558
    static keyfield_t *interbuffer = NULL;
558
    static keyfield_t *interbuffer = NULL;
559
    static size_t intersize = 0;
559
    static size_t intersize = 0;
560
 
560
 
561
    static char *shm = NULL;
561
    static char *shm = NULL;
562
    static ipcarg_t shm_id = 0;
562
    static ipcarg_t shm_id = 0;
563
    static size_t shm_size;
563
    static size_t shm_size;
564
 
564
 
565
    int handled = 1;
565
    int handled = 1;
566
    int retval = 0;
566
    int retval = 0;
567
    viewport_t *vport = &viewports[vp];
567
    viewport_t *vport = &viewports[vp];
568
    unsigned int x,y;
568
    unsigned int x,y;
569
 
569
 
570
    switch (IPC_GET_METHOD(*call)) {
570
    switch (IPC_GET_METHOD(*call)) {
571
    case IPC_M_AS_AREA_SEND:
571
    case IPC_M_AS_AREA_SEND:
572
        /* We accept one area for data interchange */
572
        /* We accept one area for data interchange */
573
        if (IPC_GET_ARG1(*call) == shm_id) {
573
        if (IPC_GET_ARG1(*call) == shm_id) {
574
            void *dest = as_get_mappable_page(IPC_GET_ARG2(*call));
574
            void *dest = as_get_mappable_page(IPC_GET_ARG2(*call));
575
            shm_size = IPC_GET_ARG2(*call);
575
            shm_size = IPC_GET_ARG2(*call);
576
            if (!ipc_answer_fast(callid, 0, (sysarg_t)dest, 0))
576
            if (!ipc_answer_fast(callid, 0, (sysarg_t)dest, 0))
577
                shm = dest;
577
                shm = dest;
578
            else
578
            else
579
                shm_id = 0;
579
                shm_id = 0;
580
            if (shm[0] != 'P')
580
            if (shm[0] != 'P')
581
                while (1)
581
                while (1)
582
                    ;
582
                    ;
583
            return 1;
583
            return 1;
584
        } else {
584
        } else {
585
            intersize = IPC_GET_ARG2(*call);
585
            intersize = IPC_GET_ARG2(*call);
586
            receive_comm_area(callid,call,(void **)&interbuffer);
586
            receive_comm_area(callid,call,(void **)&interbuffer);
587
        }
587
        }
588
        return 1;
588
        return 1;
589
    case FB_PREPARE_SHM:
589
    case FB_PREPARE_SHM:
590
        if (shm_id)
590
        if (shm_id)
591
            retval = EBUSY;
591
            retval = EBUSY;
592
        else
592
        else
593
            shm_id = IPC_GET_ARG1(*call);
593
            shm_id = IPC_GET_ARG1(*call);
594
        break;
594
        break;
595
       
595
       
596
    case FB_DROP_SHM:
596
    case FB_DROP_SHM:
597
        if (shm) {
597
        if (shm) {
598
            as_area_destroy(shm);
598
            as_area_destroy(shm);
599
            shm = NULL;
599
            shm = NULL;
600
        }
600
        }
601
        shm_id = 0;
601
        shm_id = 0;
602
        break;
602
        break;
603
 
603
 
604
    case FB_SHM2PIXMAP:
604
    case FB_SHM2PIXMAP:
605
        if (!shm) {
605
        if (!shm) {
606
            retval = EINVAL;
606
            retval = EINVAL;
607
            break;
607
            break;
608
        }
608
        }
609
        retval = shm2pixmap(shm, shm_size);
609
        retval = shm2pixmap(shm, shm_size);
610
        break;
610
        break;
611
    case FB_DRAW_PPM:
611
    case FB_DRAW_PPM:
612
        if (!shm) {
612
        if (!shm) {
613
            retval = EINVAL;
613
            retval = EINVAL;
614
            break;
614
            break;
615
        }
615
        }
616
        x = IPC_GET_ARG1(*call);
616
        x = IPC_GET_ARG1(*call);
617
        y = IPC_GET_ARG2(*call);
617
        y = IPC_GET_ARG2(*call);
618
        if (x > vport->width || y > vport->height) {
618
        if (x > vport->width || y > vport->height) {
619
            retval = EINVAL;
619
            retval = EINVAL;
620
            break;
620
            break;
621
        }
621
        }
622
       
622
       
623
        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),
624
             vport->width - x, vport->height - y, putpixel, vp);
624
             vport->width - x, vport->height - y, putpixel, vp);
625
        break;
625
        break;
626
    case FB_DRAW_TEXT_DATA:
626
    case FB_DRAW_TEXT_DATA:
627
        if (!interbuffer) {
627
        if (!interbuffer) {
628
            retval = EINVAL;
628
            retval = EINVAL;
629
            break;
629
            break;
630
        }
630
        }
631
        if (intersize < vport->cols*vport->rows*sizeof(*interbuffer)) {
631
        if (intersize < vport->cols*vport->rows*sizeof(*interbuffer)) {
632
            retval = EINVAL;
632
            retval = EINVAL;
633
            break;
633
            break;
634
        }
634
        }
635
        draw_text_data(vp, interbuffer);
635
        draw_text_data(vp, interbuffer);
636
        break;
636
        break;
637
    default:
637
    default:
638
        handled = 0;
638
        handled = 0;
639
    }
639
    }
640
   
640
   
641
    if (handled)
641
    if (handled)
642
        ipc_answer_fast(callid, retval, 0, 0);
642
        ipc_answer_fast(callid, retval, 0, 0);
643
    return handled;
643
    return handled;
644
}
644
}
645
 
645
 
646
/** Save viewport to pixmap */
646
/** Save viewport to pixmap */
647
static int save_vp_to_pixmap(int vp)
647
static int save_vp_to_pixmap(int vp)
648
{
648
{
649
    int pm;
649
    int pm;
650
    pixmap_t *pmap;
650
    pixmap_t *pmap;
651
    viewport_t *vport = &viewports[vp];
651
    viewport_t *vport = &viewports[vp];
652
    int x,y;
652
    int x,y;
653
    int rowsize;
653
    int rowsize;
654
    int tmp;
654
    int tmp;
655
 
655
 
656
    pm = find_free_pixmap();
656
    pm = find_free_pixmap();
657
    if (pm == -1)
657
    if (pm == -1)
658
        return ELIMIT;
658
        return ELIMIT;
659
   
659
   
660
    pmap = &pixmaps[pm];
660
    pmap = &pixmaps[pm];
661
    pmap->data = malloc(screen.pixelbytes * vport->width * vport->height);
661
    pmap->data = malloc(screen.pixelbytes * vport->width * vport->height);
662
    if (!pmap->data)
662
    if (!pmap->data)
663
        return ENOMEM;
663
        return ENOMEM;
664
 
664
 
665
    pmap->width = vport->width;
665
    pmap->width = vport->width;
666
    pmap->height = vport->height;
666
    pmap->height = vport->height;
667
   
667
   
668
    rowsize = vport->width * screen.pixelbytes;
668
    rowsize = vport->width * screen.pixelbytes;
669
    for (y=0;y < vport->height; y++) {
669
    for (y=0;y < vport->height; y++) {
670
        tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
670
        tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
671
        memcpy(pmap->data + rowsize*y, screen.fbaddress + tmp, rowsize);
671
        memcpy(pmap->data + rowsize*y, screen.fbaddress + tmp, rowsize);
672
    }
672
    }
673
    return pm;
673
    return pm;
674
}
674
}
675
 
675
 
676
/** Draw pixmap on screen
676
/** Draw pixmap on screen
677
 *
677
 *
678
 * @param vp Viewport to draw on
678
 * @param vp Viewport to draw on
679
 * @param pm Pixmap identifier
679
 * @param pm Pixmap identifier
680
 */
680
 */
681
static int draw_pixmap(int vp, int pm)
681
static int draw_pixmap(int vp, int pm)
682
{
682
{
683
    pixmap_t *pmap = &pixmaps[pm];
683
    pixmap_t *pmap = &pixmaps[pm];
684
    viewport_t *vport = &viewports[vp];
684
    viewport_t *vport = &viewports[vp];
685
    int x,y;
685
    int x,y;
686
    int tmp, srcrowsize;
686
    int tmp, srcrowsize;
687
    int realwidth, realheight, realrowsize;
687
    int realwidth, realheight, realrowsize;
688
 
688
 
689
    if (!pmap->data)
689
    if (!pmap->data)
690
        return EINVAL;
690
        return EINVAL;
691
 
691
 
692
    realwidth = pmap->width <= vport->width ? pmap->width : vport->width;
692
    realwidth = pmap->width <= vport->width ? pmap->width : vport->width;
693
    realheight = pmap->height <= vport->height ? pmap->height : vport->height;
693
    realheight = pmap->height <= vport->height ? pmap->height : vport->height;
694
 
694
 
695
    srcrowsize = vport->width * screen.pixelbytes;
695
    srcrowsize = vport->width * screen.pixelbytes;
696
    realrowsize = realwidth * screen.pixelbytes;
696
    realrowsize = realwidth * screen.pixelbytes;
697
 
697
 
698
    for (y=0; y < realheight; y++) {
698
    for (y=0; y < realheight; y++) {
699
        tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
699
        tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
700
        memcpy(screen.fbaddress + tmp, pmap->data + y * srcrowsize, realrowsize);
700
        memcpy(screen.fbaddress + tmp, pmap->data + y * srcrowsize, realrowsize);
701
    }
701
    }
702
    return pm;
702
    return 0;
703
}
703
}
704
 
704
 
705
/** Handler for messages concerning pixmap handling */
705
/** Handler for messages concerning pixmap handling */
706
static int pixmap_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
706
static int pixmap_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
707
{
707
{
708
    int handled = 1;
708
    int handled = 1;
709
    int retval = 0;
709
    int retval = 0;
710
    int i,nvp;
710
    int i,nvp;
711
 
711
 
712
    switch (IPC_GET_METHOD(*call)) {
712
    switch (IPC_GET_METHOD(*call)) {
713
    case FB_VP_DRAW_PIXMAP:
713
    case FB_VP_DRAW_PIXMAP:
714
        nvp = IPC_GET_ARG1(*call);
714
        nvp = IPC_GET_ARG1(*call);
715
        if (nvp == -1)
715
        if (nvp == -1)
716
            nvp = vp;
716
            nvp = vp;
717
        if (nvp < 0 || nvp >= MAX_VIEWPORTS || !viewports[nvp].initialized) {
717
        if (nvp < 0 || nvp >= MAX_VIEWPORTS || !viewports[nvp].initialized) {
718
            retval = EINVAL;
718
            retval = EINVAL;
719
            break;
719
            break;
720
        }
720
        }
721
        i = IPC_GET_ARG2(*call);
721
        i = IPC_GET_ARG2(*call);
722
        retval = draw_pixmap(nvp, i);
722
        retval = draw_pixmap(nvp, i);
723
        break;
723
        break;
724
    case FB_VP2PIXMAP:
724
    case FB_VP2PIXMAP:
725
        nvp = IPC_GET_ARG1(*call);
725
        nvp = IPC_GET_ARG1(*call);
726
        if (nvp == -1)
726
        if (nvp == -1)
727
            nvp = vp;
727
            nvp = vp;
728
        if (nvp < 0 || nvp >= MAX_VIEWPORTS || !viewports[nvp].initialized)
728
        if (nvp < 0 || nvp >= MAX_VIEWPORTS || !viewports[nvp].initialized)
729
            retval = EINVAL;
729
            retval = EINVAL;
730
        else
730
        else
731
            retval = save_vp_to_pixmap(nvp);
731
            retval = save_vp_to_pixmap(nvp);
732
        break;
732
        break;
733
    case FB_DROP_PIXMAP:
733
    case FB_DROP_PIXMAP:
734
        i = IPC_GET_ARG1(*call);
734
        i = IPC_GET_ARG1(*call);
735
        if (i >= MAX_PIXMAPS) {
735
        if (i >= MAX_PIXMAPS) {
736
            retval = EINVAL;
736
            retval = EINVAL;
737
            break;
737
            break;
738
        }
738
        }
739
        if (pixmaps[i].data) {
739
        if (pixmaps[i].data) {
740
            free(pixmaps[i].data);
740
            free(pixmaps[i].data);
741
            pixmaps[i].data = NULL;
741
            pixmaps[i].data = NULL;
742
        }
742
        }
743
        break;
743
        break;
744
    default:
744
    default:
745
        handled = 0;
745
        handled = 0;
746
    }
746
    }
747
 
747
 
748
    if (handled)
748
    if (handled)
749
        ipc_answer_fast(callid, retval, 0, 0);
749
        ipc_answer_fast(callid, retval, 0, 0);
750
    return handled;
750
    return handled;
751
   
751
   
752
}
752
}
753
 
753
 
754
/** Function for handling connections to FB
754
/** Function for handling connections to FB
755
 *
755
 *
756
 */
756
 */
757
static void fb_client_connection(ipc_callid_t iid, ipc_call_t *icall)
757
static void fb_client_connection(ipc_callid_t iid, ipc_call_t *icall)
758
{
758
{
759
    ipc_callid_t callid;
759
    ipc_callid_t callid;
760
    ipc_call_t call;
760
    ipc_call_t call;
761
    int retval;
761
    int retval;
762
    int i;
762
    int i;
763
    unsigned int row,col;
763
    unsigned int row,col;
764
    char c;
764
    char c;
765
 
765
 
766
    int vp = 0;
766
    int vp = 0;
767
    viewport_t *vport = &viewports[0];
767
    viewport_t *vport = &viewports[0];
768
 
768
 
769
    if (client_connected) {
769
    if (client_connected) {
770
        ipc_answer_fast(iid, ELIMIT, 0,0);
770
        ipc_answer_fast(iid, ELIMIT, 0,0);
771
        return;
771
        return;
772
    }
772
    }
773
    client_connected = 1;
773
    client_connected = 1;
774
    ipc_answer_fast(iid, 0, 0, 0); /* Accept connection */
774
    ipc_answer_fast(iid, 0, 0, 0); /* Accept connection */
775
 
775
 
776
    while (1) {
776
    while (1) {
777
        callid = async_get_call_timeout(&call,250000);
777
        callid = async_get_call_timeout(&call,250000);
778
        if (!callid) {
778
        if (!callid) {
779
            cursor_blink(vp);
779
            cursor_blink(vp);
780
            continue;
780
            continue;
781
        }
781
        }
782
        if (shm_handle(callid, &call, vp))
782
        if (shm_handle(callid, &call, vp))
783
            continue;
783
            continue;
784
        if (pixmap_handle(callid, &call, vp))
784
        if (pixmap_handle(callid, &call, vp))
785
            continue;
785
            continue;
786
 
786
 
787
        switch (IPC_GET_METHOD(call)) {
787
        switch (IPC_GET_METHOD(call)) {
788
        case IPC_M_PHONE_HUNGUP:
788
        case IPC_M_PHONE_HUNGUP:
789
            client_connected = 0;
789
            client_connected = 0;
790
            /* cleanup other viewports */
790
            /* cleanup other viewports */
791
            for (i=1; i < MAX_VIEWPORTS; i++)
791
            for (i=1; i < MAX_VIEWPORTS; i++)
792
                vport->initialized = 0;
792
                vport->initialized = 0;
793
            ipc_answer_fast(callid,0,0,0);
793
            ipc_answer_fast(callid,0,0,0);
794
            return; /* Exit thread */
794
            return; /* Exit thread */
795
 
795
 
796
        case FB_PUTCHAR:
796
        case FB_PUTCHAR:
797
        case FB_TRANS_PUTCHAR:
797
        case FB_TRANS_PUTCHAR:
798
            c = IPC_GET_ARG1(call);
798
            c = IPC_GET_ARG1(call);
799
            row = IPC_GET_ARG2(call);
799
            row = IPC_GET_ARG2(call);
800
            col = IPC_GET_ARG3(call);
800
            col = IPC_GET_ARG3(call);
801
            if (row >= vport->rows || col >= vport->cols) {
801
            if (row >= vport->rows || col >= vport->cols) {
802
                retval = EINVAL;
802
                retval = EINVAL;
803
                break;
803
                break;
804
            }
804
            }
805
            ipc_answer_fast(callid,0,0,0);
805
            ipc_answer_fast(callid,0,0,0);
806
 
806
 
807
            draw_char(vp, c, row, col, vport->style, IPC_GET_METHOD(call) == FB_TRANS_PUTCHAR);
807
            draw_char(vp, c, row, col, vport->style, IPC_GET_METHOD(call) == FB_TRANS_PUTCHAR);
808
            continue; /* msg already answered */
808
            continue; /* msg already answered */
809
        case FB_CLEAR:
809
        case FB_CLEAR:
810
            clear_port(vp);
810
            clear_port(vp);
811
            cursor_print(vp);
811
            cursor_print(vp);
812
            retval = 0;
812
            retval = 0;
813
            break;
813
            break;
814
        case FB_CURSOR_GOTO:
814
        case FB_CURSOR_GOTO:
815
            row = IPC_GET_ARG1(call);
815
            row = IPC_GET_ARG1(call);
816
            col = IPC_GET_ARG2(call);
816
            col = IPC_GET_ARG2(call);
817
            if (row >= vport->rows || col >= vport->cols) {
817
            if (row >= vport->rows || col >= vport->cols) {
818
                retval = EINVAL;
818
                retval = EINVAL;
819
                break;
819
                break;
820
            }
820
            }
821
            retval = 0;
821
            retval = 0;
822
            cursor_hide(vp);
822
            cursor_hide(vp);
823
            vport->cur_col = col;
823
            vport->cur_col = col;
824
            vport->cur_row = row;
824
            vport->cur_row = row;
825
            cursor_print(vp);
825
            cursor_print(vp);
826
            break;
826
            break;
827
        case FB_CURSOR_VISIBILITY:
827
        case FB_CURSOR_VISIBILITY:
828
            cursor_hide(vp);
828
            cursor_hide(vp);
829
            vport->cursor_active = IPC_GET_ARG1(call);
829
            vport->cursor_active = IPC_GET_ARG1(call);
830
            cursor_print(vp);
830
            cursor_print(vp);
831
            retval = 0;
831
            retval = 0;
832
            break;
832
            break;
833
        case FB_GET_CSIZE:
833
        case FB_GET_CSIZE:
834
            ipc_answer_fast(callid, 0, vport->rows, vport->cols);
834
            ipc_answer_fast(callid, 0, vport->rows, vport->cols);
835
            continue;
835
            continue;
836
        case FB_SCROLL:
836
        case FB_SCROLL:
837
            i = IPC_GET_ARG1(call);
837
            i = IPC_GET_ARG1(call);
838
            if (i > vport->rows || i < (- (int)vport->rows)) {
838
            if (i > vport->rows || i < (- (int)vport->rows)) {
839
                retval = EINVAL;
839
                retval = EINVAL;
840
                break;
840
                break;
841
            }
841
            }
842
            cursor_hide(vp);
842
            cursor_hide(vp);
843
            scroll_port(vp, i);
843
            scroll_port(vp, i);
844
            cursor_print(vp);
844
            cursor_print(vp);
845
            retval = 0;
845
            retval = 0;
846
            break;
846
            break;
847
        case FB_VIEWPORT_SWITCH:
847
        case FB_VIEWPORT_SWITCH:
848
            i = IPC_GET_ARG1(call);
848
            i = IPC_GET_ARG1(call);
849
            if (i < 0 || i >= MAX_VIEWPORTS) {
849
            if (i < 0 || i >= MAX_VIEWPORTS) {
850
                retval = EINVAL;
850
                retval = EINVAL;
851
                break;
851
                break;
852
            }
852
            }
853
            if (! viewports[i].initialized ) {
853
            if (! viewports[i].initialized ) {
854
                retval = EADDRNOTAVAIL;
854
                retval = EADDRNOTAVAIL;
855
                break;
855
                break;
856
            }
856
            }
857
            cursor_hide(vp);
857
            cursor_hide(vp);
858
            vp = i;
858
            vp = i;
859
            vport = &viewports[vp];
859
            vport = &viewports[vp];
860
            cursor_print(vp);
860
            cursor_print(vp);
861
            retval = 0;
861
            retval = 0;
862
            break;
862
            break;
863
        case FB_VIEWPORT_CREATE:
863
        case FB_VIEWPORT_CREATE:
864
            retval = viewport_create(IPC_GET_ARG1(call) >> 16,
864
            retval = viewport_create(IPC_GET_ARG1(call) >> 16,
865
                         IPC_GET_ARG1(call) & 0xffff,
865
                         IPC_GET_ARG1(call) & 0xffff,
866
                         IPC_GET_ARG2(call) >> 16,
866
                         IPC_GET_ARG2(call) >> 16,
867
                         IPC_GET_ARG2(call) & 0xffff);
867
                         IPC_GET_ARG2(call) & 0xffff);
868
            break;
868
            break;
869
        case FB_VIEWPORT_DELETE:
869
        case FB_VIEWPORT_DELETE:
870
            i = IPC_GET_ARG1(call);
870
            i = IPC_GET_ARG1(call);
871
            if (i < 0 || i >= MAX_VIEWPORTS) {
871
            if (i < 0 || i >= MAX_VIEWPORTS) {
872
                retval = EINVAL;
872
                retval = EINVAL;
873
                break;
873
                break;
874
            }
874
            }
875
            if (! viewports[i].initialized ) {
875
            if (! viewports[i].initialized ) {
876
                retval = EADDRNOTAVAIL;
876
                retval = EADDRNOTAVAIL;
877
                break;
877
                break;
878
            }
878
            }
879
            viewports[i].initialized = 0;
879
            viewports[i].initialized = 0;
880
            retval = 0;
880
            retval = 0;
881
            break;
881
            break;
882
        case FB_SET_STYLE:
882
        case FB_SET_STYLE:
883
            vport->style.fg_color = IPC_GET_ARG1(call);
883
            vport->style.fg_color = IPC_GET_ARG1(call);
884
            vport->style.bg_color = IPC_GET_ARG2(call);
884
            vport->style.bg_color = IPC_GET_ARG2(call);
885
            retval = 0;
885
            retval = 0;
886
            break;
886
            break;
887
        case FB_GET_RESOLUTION:
887
        case FB_GET_RESOLUTION:
888
            ipc_answer_fast(callid, 0, screen.xres,screen.yres);
888
            ipc_answer_fast(callid, 0, screen.xres,screen.yres);
889
            continue;
889
            continue;
890
        default:
890
        default:
891
            retval = ENOENT;
891
            retval = ENOENT;
892
        }
892
        }
893
        ipc_answer_fast(callid,retval,0,0);
893
        ipc_answer_fast(callid,retval,0,0);
894
    }
894
    }
895
}
895
}
896
 
896
 
897
/** Initialization of framebuffer */
897
/** Initialization of framebuffer */
898
int fb_init(void)
898
int fb_init(void)
899
{
899
{
900
    void *fb_ph_addr;
900
    void *fb_ph_addr;
901
    unsigned int fb_width;
901
    unsigned int fb_width;
902
    unsigned int fb_height;
902
    unsigned int fb_height;
903
    unsigned int fb_bpp;
903
    unsigned int fb_bpp;
904
    unsigned int fb_scanline;
904
    unsigned int fb_scanline;
905
    void *fb_addr;
905
    void *fb_addr;
906
    size_t asz;
906
    size_t asz;
907
 
907
 
908
    async_set_client_connection(fb_client_connection);
908
    async_set_client_connection(fb_client_connection);
909
 
909
 
910
    fb_ph_addr=(void *)sysinfo_value("fb.address.physical");
910
    fb_ph_addr=(void *)sysinfo_value("fb.address.physical");
911
    fb_width=sysinfo_value("fb.width");
911
    fb_width=sysinfo_value("fb.width");
912
    fb_height=sysinfo_value("fb.height");
912
    fb_height=sysinfo_value("fb.height");
913
    fb_bpp=sysinfo_value("fb.bpp");
913
    fb_bpp=sysinfo_value("fb.bpp");
914
    fb_scanline=sysinfo_value("fb.scanline");
914
    fb_scanline=sysinfo_value("fb.scanline");
915
 
915
 
916
    asz = fb_scanline*fb_height;
916
    asz = fb_scanline*fb_height;
917
    fb_addr = as_get_mappable_page(asz);
917
    fb_addr = as_get_mappable_page(asz);
918
   
918
   
919
    map_physmem(fb_ph_addr, fb_addr, ALIGN_UP(asz,PAGE_SIZE) >>PAGE_WIDTH,
919
    map_physmem(fb_ph_addr, fb_addr, ALIGN_UP(asz,PAGE_SIZE) >>PAGE_WIDTH,
920
            AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE);
920
            AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE);
921
   
921
   
922
    screen_init(fb_addr, fb_width, fb_height, fb_bpp, fb_scanline);
922
    screen_init(fb_addr, fb_width, fb_height, fb_bpp, fb_scanline);
923
 
923
 
924
    return 0;
924
    return 0;
925
}
925
}
926
 
926
 
927
 
927