Subversion Repositories HelenOS-historic

Rev

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

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