Subversion Repositories HelenOS-historic

Rev

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

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