Subversion Repositories HelenOS

Rev

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

Rev 1888 Rev 1954
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
/**
30
/**
31
 * @defgroup fb Graphical framebuffer
31
 * @defgroup fb Graphical framebuffer
32
 * @brief   HelenOS graphical framebuffer.
32
 * @brief   HelenOS graphical framebuffer.
33
 * @ingroup fbs
33
 * @ingroup fbs
34
 * @{
34
 * @{
35
 */
35
 */
36
 
36
 
37
/** @file
37
/** @file
38
 */
38
 */
39
 
39
 
40
#include <stdlib.h>
40
#include <stdlib.h>
41
#include <unistd.h>
41
#include <unistd.h>
42
#include <string.h>
42
#include <string.h>
43
#include <ddi.h>
43
#include <ddi.h>
44
#include <sysinfo.h>
44
#include <sysinfo.h>
45
#include <align.h>
45
#include <align.h>
46
#include <as.h>
46
#include <as.h>
47
#include <ipc/fb.h>
47
#include <ipc/fb.h>
48
#include <ipc/ipc.h>
48
#include <ipc/ipc.h>
49
#include <ipc/ns.h>
49
#include <ipc/ns.h>
50
#include <ipc/services.h>
50
#include <ipc/services.h>
51
#include <kernel/errno.h>
51
#include <kernel/errno.h>
52
#include <async.h>
52
#include <async.h>
53
 
53
 
54
#include "font-8x16.h"
54
#include "font-8x16.h"
55
#include "fb.h"
55
#include "fb.h"
56
#include "main.h"
56
#include "main.h"
57
#include "../console/screenbuffer.h"
57
#include "../console/screenbuffer.h"
58
#include "ppm.h"
58
#include "ppm.h"
59
 
59
 
60
#include "pointer.xbm"
60
#include "pointer.xbm"
61
#include "pointer_mask.xbm"
61
#include "pointer_mask.xbm"
62
 
62
 
63
#define DEFAULT_BGCOLOR                0xf0f0f0
63
#define DEFAULT_BGCOLOR                0xf0f0f0
64
#define DEFAULT_FGCOLOR                0x0
64
#define DEFAULT_FGCOLOR                0x0
65
 
65
 
66
/***************************************************************/
66
/***************************************************************/
67
/* Pixel specific fuctions */
67
/* Pixel specific fuctions */
68
 
68
 
69
typedef void (*conv2scr_fn_t)(void *, int);
69
typedef void (*conv2scr_fn_t)(void *, int);
70
typedef int (*conv2rgb_fn_t)(void *);
70
typedef int (*conv2rgb_fn_t)(void *);
71
 
71
 
72
struct {
72
struct {
73
    uint8_t *fbaddress;
73
    uint8_t *fbaddress;
74
 
74
 
75
    unsigned int xres;
75
    unsigned int xres;
76
    unsigned int yres;
76
    unsigned int yres;
77
    unsigned int scanline;
77
    unsigned int scanline;
78
    unsigned int pixelbytes;
78
    unsigned int pixelbytes;
79
    unsigned int invert_colors;
79
    unsigned int invert_colors;
80
 
80
 
81
    conv2scr_fn_t rgb2scr;
81
    conv2scr_fn_t rgb2scr;
82
    conv2rgb_fn_t scr2rgb;
82
    conv2rgb_fn_t scr2rgb;
83
} screen;
83
} screen;
84
 
84
 
85
typedef struct {
85
typedef struct {
86
    int initialized;
86
    int initialized;
87
    unsigned int x, y;
87
    unsigned int x, y;
88
    unsigned int width, height;
88
    unsigned int width, height;
89
 
89
 
90
    /* Text support in window */
90
    /* Text support in window */
91
    unsigned int rows, cols;
91
    unsigned int rows, cols;
92
    /* Style for text printing */
92
    /* Style for text printing */
93
    style_t style;
93
    style_t style;
94
    /* Auto-cursor position */
94
    /* Auto-cursor position */
95
    int cursor_active, cur_col, cur_row;
95
    int cursor_active, cur_col, cur_row;
96
    int cursor_shown;
96
    int cursor_shown;
97
    /* Double buffering */
97
    /* Double buffering */
98
    uint8_t *dbdata;
98
    uint8_t *dbdata;
99
    unsigned int dboffset;
99
    unsigned int dboffset;
100
    unsigned int paused;
100
    unsigned int paused;
101
} viewport_t;
101
} viewport_t;
102
 
102
 
103
#define MAX_ANIM_LEN    8
103
#define MAX_ANIM_LEN    8
104
#define MAX_ANIMATIONS  4
104
#define MAX_ANIMATIONS  4
105
typedef struct {
105
typedef struct {
106
    int initialized;
106
    int initialized;
107
    int enabled;
107
    int enabled;
108
    unsigned int vp;
108
    unsigned int vp;
109
 
109
 
110
    unsigned int pos;
110
    unsigned int pos;
111
    unsigned int animlen;
111
    unsigned int animlen;
112
    unsigned int pixmaps[MAX_ANIM_LEN];
112
    unsigned int pixmaps[MAX_ANIM_LEN];
113
} animation_t;
113
} animation_t;
114
static animation_t animations[MAX_ANIMATIONS];
114
static animation_t animations[MAX_ANIMATIONS];
115
static int anims_enabled;
115
static int anims_enabled;
116
 
116
 
117
/** Maximum number of saved pixmaps
117
/** Maximum number of saved pixmaps
118
 * Pixmap is a saved rectangle
118
 * Pixmap is a saved rectangle
119
 */
119
 */
120
#define MAX_PIXMAPS        256
120
#define MAX_PIXMAPS        256
121
typedef struct {
121
typedef struct {
122
    unsigned int width;
122
    unsigned int width;
123
    unsigned int height;
123
    unsigned int height;
124
    uint8_t *data;
124
    uint8_t *data;
125
} pixmap_t;
125
} pixmap_t;
126
static pixmap_t pixmaps[MAX_PIXMAPS];
126
static pixmap_t pixmaps[MAX_PIXMAPS];
127
 
127
 
128
/* Viewport is a rectangular area on the screen */
128
/* Viewport is a rectangular area on the screen */
129
#define MAX_VIEWPORTS 128
129
#define MAX_VIEWPORTS 128
130
static viewport_t viewports[128];
130
static viewport_t viewports[128];
131
 
131
 
132
/* Allow only 1 connection */
132
/* Allow only 1 connection */
133
static int client_connected = 0;
133
static int client_connected = 0;
134
 
134
 
135
#define RED(x, bits)    ((x >> (16 + 8 - bits)) & ((1 << bits) - 1))
135
#define RED(x, bits)    ((x >> (16 + 8 - bits)) & ((1 << bits) - 1))
136
#define GREEN(x, bits)  ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
136
#define GREEN(x, bits)  ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
137
#define BLUE(x, bits)   ((x >> (8 - bits)) & ((1 << bits) - 1))
137
#define BLUE(x, bits)   ((x >> (8 - bits)) & ((1 << bits) - 1))
138
 
138
 
139
#define COL_WIDTH   8
139
#define COL_WIDTH   8
140
#define ROW_BYTES   (screen.scanline * FONT_SCANLINES)
140
#define ROW_BYTES   (screen.scanline * FONT_SCANLINES)
141
 
141
 
142
#define POINTPOS(x, y)  ((y) * screen.scanline + (x) * screen.pixelbytes)
142
#define POINTPOS(x, y)  ((y) * screen.scanline + (x) * screen.pixelbytes)
143
 
143
 
144
static inline int COLOR(int color)
144
static inline int COLOR(int color)
145
{
145
{
146
    return screen.invert_colors ? ~color : color;
146
    return screen.invert_colors ? ~color : color;
147
}
147
}
148
 
148
 
149
/* Conversion routines between different color representations */
149
/* Conversion routines between different color representations */
150
static void rgb_4byte(void *dst, int rgb)
150
static void rgb_4byte(void *dst, int rgb)
151
{
151
{
152
    *(int *)dst = rgb;
152
    *(int *)dst = rgb;
153
}
153
}
154
 
154
 
155
static int byte4_rgb(void *src)
155
static int byte4_rgb(void *src)
156
{
156
{
157
    return (*(int *)src) & 0xffffff;
157
    return (*(int *)src) & 0xffffff;
158
}
158
}
159
 
159
 
160
static void rgb_3byte(void *dst, int rgb)
160
static void rgb_3byte(void *dst, int rgb)
161
{
161
{
162
    uint8_t *scr = dst;
162
    uint8_t *scr = dst;
163
#if defined(FB_INVERT_ENDIAN)
163
#if defined(FB_INVERT_ENDIAN)
164
    scr[0] = RED(rgb, 8);
164
    scr[0] = RED(rgb, 8);
165
    scr[1] = GREEN(rgb, 8);
165
    scr[1] = GREEN(rgb, 8);
166
    scr[2] = BLUE(rgb, 8);
166
    scr[2] = BLUE(rgb, 8);
167
#else
167
#else
168
    scr[2] = RED(rgb, 8);
168
    scr[2] = RED(rgb, 8);
169
    scr[1] = GREEN(rgb, 8);
169
    scr[1] = GREEN(rgb, 8);
170
    scr[0] = BLUE(rgb, 8);
170
    scr[0] = BLUE(rgb, 8);
171
#endif
171
#endif
172
}
172
}
173
 
173
 
174
static int byte3_rgb(void *src)
174
static int byte3_rgb(void *src)
175
{
175
{
176
    uint8_t *scr = src;
176
    uint8_t *scr = src;
177
#if defined(FB_INVERT_ENDIAN)
177
#if defined(FB_INVERT_ENDIAN)
178
    return scr[0] << 16 | scr[1] << 8 | scr[2];
178
    return scr[0] << 16 | scr[1] << 8 | scr[2];
179
#else
179
#else
180
    return scr[2] << 16 | scr[1] << 8 | scr[0];
180
    return scr[2] << 16 | scr[1] << 8 | scr[0];
181
#endif  
181
#endif  
182
}
182
}
183
 
183
 
184
/**  16-bit depth (5:6:5) */
184
/**  16-bit depth (5:6:5) */
185
static void rgb_2byte(void *dst, int rgb)
185
static void rgb_2byte(void *dst, int rgb)
186
{
186
{
187
    /* 5-bit, 6-bits, 5-bits */
187
    /* 5-bit, 6-bits, 5-bits */
188
    *((uint16_t *)(dst)) = RED(rgb, 5) << 11 | GREEN(rgb, 6) << 5 | BLUE(rgb, 5);
188
    *((uint16_t *)(dst)) = RED(rgb, 5) << 11 | GREEN(rgb, 6) << 5 | BLUE(rgb, 5);
189
}
189
}
190
 
190
 
191
/** 16-bit depth (5:6:5) */
191
/** 16-bit depth (5:6:5) */
192
static int byte2_rgb(void *src)
192
static int byte2_rgb(void *src)
193
{
193
{
194
    int color = *(uint16_t *)(src);
194
    int color = *(uint16_t *)(src);
195
    return (((color >> 11) & 0x1f) << (16 + 3)) | (((color >> 5) & 0x3f) << (8 + 2)) | ((color & 0x1f) << 3);
195
    return (((color >> 11) & 0x1f) << (16 + 3)) | (((color >> 5) & 0x3f) << (8 + 2)) | ((color & 0x1f) << 3);
196
}
196
}
197
 
197
 
198
/** Put pixel - 8-bit depth (3:2:3) */
198
/** Put pixel - 8-bit depth (3:2:3) */
199
static void rgb_1byte(void *dst, int rgb)
199
static void rgb_1byte(void *dst, int rgb)
200
{
200
{
201
    *(uint8_t *)dst = RED(rgb, 3) << 5 | GREEN(rgb, 2) << 3 | BLUE(rgb, 3);
201
    *(uint8_t *)dst = RED(rgb, 3) << 5 | GREEN(rgb, 2) << 3 | BLUE(rgb, 3);
202
}
202
}
203
 
203
 
204
/** Return pixel color - 8-bit depth (3:2:3) */
204
/** Return pixel color - 8-bit depth (3:2:3) */
205
static int byte1_rgb(void *src)
205
static int byte1_rgb(void *src)
206
{
206
{
207
    int color = *(uint8_t *)src;
207
    int color = *(uint8_t *)src;
208
    return (((color >> 5) & 0x7) << (16 + 5)) | (((color >> 3) & 0x3) << (8 + 6)) | ((color & 0x7) << 5);
208
    return (((color >> 5) & 0x7) << (16 + 5)) | (((color >> 3) & 0x3) << (8 + 6)) | ((color & 0x7) << 5);
209
}
209
}
210
 
210
 
211
/** Put pixel into viewport
211
/** Put pixel into viewport
212
 *
212
 *
213
 * @param vport Viewport identification
213
 * @param vport Viewport identification
214
 * @param x X coord relative to viewport
214
 * @param x X coord relative to viewport
215
 * @param y Y coord relative to viewport
215
 * @param y Y coord relative to viewport
216
 * @param color RGB color
216
 * @param color RGB color
217
 */
217
 */
218
static void putpixel(viewport_t *vport, unsigned int x, unsigned int y, int color)
218
static void putpixel(viewport_t *vport, unsigned int x, unsigned int y, int color)
219
{
219
{
220
    int dx = vport->x + x;
220
    int dx = vport->x + x;
221
    int dy = vport->y + y;
221
    int dy = vport->y + y;
222
 
222
 
223
    if (! (vport->paused && vport->dbdata))
223
    if (! (vport->paused && vport->dbdata))
224
        (*screen.rgb2scr)(&screen.fbaddress[POINTPOS(dx,dy)], COLOR(color));
224
        (*screen.rgb2scr)(&screen.fbaddress[POINTPOS(dx,dy)], COLOR(color));
225
 
225
 
226
    if (vport->dbdata) {
226
    if (vport->dbdata) {
227
        int dline = (y + vport->dboffset) % vport->height;
227
        int dline = (y + vport->dboffset) % vport->height;
228
        int doffset = screen.pixelbytes * (dline * vport->width + x);
228
        int doffset = screen.pixelbytes * (dline * vport->width + x);
229
        (*screen.rgb2scr)(&vport->dbdata[doffset], COLOR(color));
229
        (*screen.rgb2scr)(&vport->dbdata[doffset], COLOR(color));
230
    }
230
    }
231
}
231
}
232
 
232
 
233
/** Get pixel from viewport */
233
/** Get pixel from viewport */
234
static int getpixel(viewport_t *vport, unsigned int x, unsigned int y)
234
static int getpixel(viewport_t *vport, unsigned int x, unsigned int y)
235
{
235
{
236
    int dx = vport->x + x;
236
    int dx = vport->x + x;
237
    int dy = vport->y + y;
237
    int dy = vport->y + y;
238
 
238
 
239
    return COLOR((*screen.scr2rgb)(&screen.fbaddress[POINTPOS(dx,dy)]));
239
    return COLOR((*screen.scr2rgb)(&screen.fbaddress[POINTPOS(dx,dy)]));
240
}
240
}
241
 
241
 
242
static inline void putpixel_mem(char *mem, unsigned int x, unsigned int y,
242
static inline void putpixel_mem(char *mem, unsigned int x, unsigned int y,
243
                int color)
243
                int color)
244
{
244
{
245
    (*screen.rgb2scr)(&mem[POINTPOS(x,y)], COLOR(color));
245
    (*screen.rgb2scr)(&mem[POINTPOS(x,y)], COLOR(color));
246
}
246
}
247
 
247
 
248
static void draw_rectangle(viewport_t *vport, unsigned int sx, unsigned int sy,
248
static void draw_rectangle(viewport_t *vport, unsigned int sx, unsigned int sy,
249
               unsigned int width, unsigned int height,
249
               unsigned int width, unsigned int height,
250
               int color)
250
               int color)
251
{
251
{
252
    unsigned int x, y;
252
    unsigned int x, y;
253
    static void *tmpline;
253
    static void *tmpline;
254
 
254
 
255
    if (!tmpline)
255
    if (!tmpline)
256
        tmpline = malloc(screen.scanline*screen.pixelbytes);
256
        tmpline = malloc(screen.scanline*screen.pixelbytes);
257
 
257
 
258
    /* Clear first line */
258
    /* Clear first line */
259
    for (x = 0; x < width; x++)
259
    for (x = 0; x < width; x++)
260
        putpixel_mem(tmpline, x, 0, color);
260
        putpixel_mem(tmpline, x, 0, color);
261
 
261
 
262
    if (!vport->paused) {
262
    if (!vport->paused) {
263
        /* Recompute to screen coords */
263
        /* Recompute to screen coords */
264
        sx += vport->x;
264
        sx += vport->x;
265
        sy += vport->y;
265
        sy += vport->y;
266
        /* Copy the rest */
266
        /* Copy the rest */
267
        for (y = sy;y < sy+height; y++)
267
        for (y = sy;y < sy+height; y++)
268
            memcpy(&screen.fbaddress[POINTPOS(sx,y)], tmpline,
268
            memcpy(&screen.fbaddress[POINTPOS(sx,y)], tmpline,
269
                   screen.pixelbytes * width);
269
                   screen.pixelbytes * width);
270
    }
270
    }
271
    if (vport->dbdata) {
271
    if (vport->dbdata) {
272
        for (y=sy;y < sy+height; y++) {
272
        for (y=sy;y < sy+height; y++) {
273
            int rline = (y + vport->dboffset) % vport->height;
273
            int rline = (y + vport->dboffset) % vport->height;
274
            int rpos = (rline * vport->width + sx) * screen.pixelbytes;
274
            int rpos = (rline * vport->width + sx) * screen.pixelbytes;
275
            memcpy(&vport->dbdata[rpos], tmpline, screen.pixelbytes * width);
275
            memcpy(&vport->dbdata[rpos], tmpline, screen.pixelbytes * width);
276
        }
276
        }
277
    }
277
    }
278
 
278
 
279
}
279
}
280
 
280
 
281
/** Fill viewport with background color */
281
/** Fill viewport with background color */
282
static void clear_port(viewport_t *vport)
282
static void clear_port(viewport_t *vport)
283
{
283
{
284
    draw_rectangle(vport, 0, 0, vport->width, vport->height, vport->style.bg_color);
284
    draw_rectangle(vport, 0, 0, vport->width, vport->height, vport->style.bg_color);
285
}
285
}
286
 
286
 
287
/** Scroll unbuffered viewport up/down
287
/** Scroll unbuffered viewport up/down
288
 *
288
 *
289
 * @param vport Viewport to scroll
289
 * @param vport Viewport to scroll
290
 * @param lines Positive number - scroll up, negative - scroll down
290
 * @param lines Positive number - scroll up, negative - scroll down
291
 */
291
 */
292
static void scroll_port_nodb(viewport_t *vport, int lines)
292
static void scroll_port_nodb(viewport_t *vport, int lines)
293
{
293
{
294
    int y;
294
    int y;
295
 
295
 
296
    if (lines > 0) {
296
    if (lines > 0) {
297
        for (y=vport->y; y < vport->y+vport->height - lines; y++)
297
        for (y=vport->y; y < vport->y+vport->height - lines; y++)
298
            memcpy(&screen.fbaddress[POINTPOS(vport->x,y)],
298
            memcpy(&screen.fbaddress[POINTPOS(vport->x,y)],
299
                   &screen.fbaddress[POINTPOS(vport->x,y + lines)],
299
                   &screen.fbaddress[POINTPOS(vport->x,y + lines)],
300
                   screen.pixelbytes * vport->width);
300
                   screen.pixelbytes * vport->width);
301
        draw_rectangle(vport, 0, vport->height - lines,
301
        draw_rectangle(vport, 0, vport->height - lines,
302
                   vport->width, lines, vport->style.bg_color);
302
                   vport->width, lines, vport->style.bg_color);
303
    } else if (lines < 0) {
303
    } else if (lines < 0) {
304
        lines = -lines;
304
        lines = -lines;
305
        for (y=vport->y + vport->height-1; y >= vport->y + lines; y--)
305
        for (y=vport->y + vport->height-1; y >= vport->y + lines; y--)
306
            memcpy(&screen.fbaddress[POINTPOS(vport->x,y)],
306
            memcpy(&screen.fbaddress[POINTPOS(vport->x,y)],
307
                   &screen.fbaddress[POINTPOS(vport->x,y - lines)],
307
                   &screen.fbaddress[POINTPOS(vport->x,y - lines)],
308
                   screen.pixelbytes * vport->width);
308
                   screen.pixelbytes * vport->width);
309
        draw_rectangle(vport, 0, 0, vport->width, lines, vport->style.bg_color);
309
        draw_rectangle(vport, 0, 0, vport->width, lines, vport->style.bg_color);
310
    }
310
    }
311
}
311
}
312
 
312
 
313
/** Refresh given viewport from double buffer */
313
/** Refresh given viewport from double buffer */
314
static void refresh_viewport_db(viewport_t *vport)
314
static void refresh_viewport_db(viewport_t *vport)
315
{
315
{
316
    unsigned int y, srcy, srcoff, dsty, dstx;
316
    unsigned int y, srcy, srcoff, dsty, dstx;
317
 
317
 
318
    for (y = 0; y < vport->height; y++) {
318
    for (y = 0; y < vport->height; y++) {
319
        srcy = (y + vport->dboffset) % vport->height;
319
        srcy = (y + vport->dboffset) % vport->height;
320
        srcoff = (vport->width * srcy) * screen.pixelbytes;
320
        srcoff = (vport->width * srcy) * screen.pixelbytes;
321
 
321
 
322
        dstx = vport->x;
322
        dstx = vport->x;
323
        dsty = vport->y + y;
323
        dsty = vport->y + y;
324
 
324
 
325
        memcpy(&screen.fbaddress[POINTPOS(dstx,dsty)],
325
        memcpy(&screen.fbaddress[POINTPOS(dstx,dsty)],
326
               &vport->dbdata[srcoff],
326
               &vport->dbdata[srcoff],
327
               vport->width*screen.pixelbytes);
327
               vport->width*screen.pixelbytes);
328
    }
328
    }
329
}
329
}
330
 
330
 
331
/** Scroll viewport that has double buffering enabled */
331
/** Scroll viewport that has double buffering enabled */
332
static void scroll_port_db(viewport_t *vport, int lines)
332
static void scroll_port_db(viewport_t *vport, int lines)
333
{
333
{
334
    ++vport->paused;
334
    ++vport->paused;
335
    if (lines > 0) {
335
    if (lines > 0) {
336
        draw_rectangle(vport, 0, 0, vport->width, lines,
336
        draw_rectangle(vport, 0, 0, vport->width, lines,
337
                   vport->style.bg_color);
337
                   vport->style.bg_color);
338
        vport->dboffset += lines;
338
        vport->dboffset += lines;
339
        vport->dboffset %= vport->height;
339
        vport->dboffset %= vport->height;
340
    } else if (lines < 0) {
340
    } else if (lines < 0) {
341
        lines = -lines;
341
        lines = -lines;
342
        draw_rectangle(vport, 0, vport->height-lines,
342
        draw_rectangle(vport, 0, vport->height-lines,
343
                   vport->width, lines,
343
                   vport->width, lines,
344
                   vport->style.bg_color);
344
                   vport->style.bg_color);
345
 
345
 
346
        if (vport->dboffset < lines)
346
        if (vport->dboffset < lines)
347
            vport->dboffset += vport->height;
347
            vport->dboffset += vport->height;
348
        vport->dboffset -= lines;
348
        vport->dboffset -= lines;
349
    }
349
    }
350
   
350
   
351
    --vport->paused;
351
    --vport->paused;
352
   
352
   
353
    refresh_viewport_db(vport);
353
    refresh_viewport_db(vport);
354
}
354
}
355
 
355
 
356
/** Scrolls viewport given number of lines */
356
/** Scrolls viewport given number of lines */
357
static void scroll_port(viewport_t *vport, int lines)
357
static void scroll_port(viewport_t *vport, int lines)
358
{
358
{
359
    if (vport->dbdata)
359
    if (vport->dbdata)
360
        scroll_port_db(vport, lines);
360
        scroll_port_db(vport, lines);
361
    else
361
    else
362
        scroll_port_nodb(vport, lines);
362
        scroll_port_nodb(vport, lines);
363
   
363
   
364
}
364
}
365
 
365
 
366
static void invert_pixel(viewport_t *vport, unsigned int x, unsigned int y)
366
static void invert_pixel(viewport_t *vport, unsigned int x, unsigned int y)
367
{
367
{
368
    putpixel(vport, x, y, ~getpixel(vport, x, y));
368
    putpixel(vport, x, y, ~getpixel(vport, x, y));
369
}
369
}
370
 
370
 
371
 
371
 
372
/***************************************************************/
372
/***************************************************************/
373
/* Character-console functions */
373
/* Character-console functions */
374
 
374
 
375
/** Draw character at given position
375
/** Draw character at given position
376
 *
376
 *
377
 * @param vport Viewport where the character is printed
377
 * @param vport Viewport where the character is printed
378
 * @param sx Coordinates of top-left of the character
378
 * @param sx Coordinates of top-left of the character
379
 * @param sy Coordinates of top-left of the character
379
 * @param sy Coordinates of top-left of the character
380
 * @param style Color of the character
380
 * @param style Color of the character
381
 * @param transparent If false, print background color
381
 * @param transparent If false, print background color
382
 */
382
 */
383
static void draw_glyph(viewport_t *vport,uint8_t glyph, unsigned int sx, unsigned int sy,
383
static void draw_glyph(viewport_t *vport,uint8_t glyph, unsigned int sx, unsigned int sy,
384
               style_t style, int transparent)
384
               style_t style, int transparent)
385
{
385
{
386
    int i;
386
    int i;
387
    unsigned int y;
387
    unsigned int y;
388
    unsigned int glline;
388
    unsigned int glline;
389
 
389
 
390
    for (y = 0; y < FONT_SCANLINES; y++) {
390
    for (y = 0; y < FONT_SCANLINES; y++) {
391
        glline = fb_font[glyph * FONT_SCANLINES + y];
391
        glline = fb_font[glyph * FONT_SCANLINES + y];
392
        for (i = 0; i < 8; i++) {
392
        for (i = 0; i < 8; i++) {
393
            if (glline & (1 << (7 - i)))
393
            if (glline & (1 << (7 - i)))
394
                putpixel(vport, sx + i, sy + y, style.fg_color);
394
                putpixel(vport, sx + i, sy + y, style.fg_color);
395
            else if (!transparent)
395
            else if (!transparent)
396
                putpixel(vport, sx + i, sy + y, style.bg_color);
396
                putpixel(vport, sx + i, sy + y, style.bg_color);
397
        }
397
        }
398
    }
398
    }
399
}
399
}
400
 
400
 
401
/** Invert character at given position */
401
/** Invert character at given position */
402
static void invert_char(viewport_t *vport,unsigned int row, unsigned int col)
402
static void invert_char(viewport_t *vport,unsigned int row, unsigned int col)
403
{
403
{
404
    unsigned int x;
404
    unsigned int x;
405
    unsigned int y;
405
    unsigned int y;
406
 
406
 
407
    for (x = 0; x < COL_WIDTH; x++)
407
    for (x = 0; x < COL_WIDTH; x++)
408
        for (y = 0; y < FONT_SCANLINES; y++)
408
        for (y = 0; y < FONT_SCANLINES; y++)
409
            invert_pixel(vport, col * COL_WIDTH + x, row * FONT_SCANLINES + y);
409
            invert_pixel(vport, col * COL_WIDTH + x, row * FONT_SCANLINES + y);
410
}
410
}
411
 
411
 
412
/***************************************************************/
412
/***************************************************************/
413
/* Stdout specific functions */
413
/* Stdout specific functions */
414
 
414
 
415
 
415
 
416
/** Create new viewport
416
/** Create new viewport
417
 *
417
 *
418
 * @return New viewport number
418
 * @return New viewport number
419
 */
419
 */
420
static int viewport_create(unsigned int x, unsigned int y,unsigned int width,
420
static int viewport_create(unsigned int x, unsigned int y,unsigned int width,
421
               unsigned int height)
421
               unsigned int height)
422
{
422
{
423
    int i;
423
    int i;
424
 
424
 
425
    for (i=0; i < MAX_VIEWPORTS; i++) {
425
    for (i=0; i < MAX_VIEWPORTS; i++) {
426
        if (!viewports[i].initialized)
426
        if (!viewports[i].initialized)
427
            break;
427
            break;
428
    }
428
    }
429
    if (i == MAX_VIEWPORTS)
429
    if (i == MAX_VIEWPORTS)
430
        return ELIMIT;
430
        return ELIMIT;
431
 
431
 
432
    viewports[i].x = x;
432
    viewports[i].x = x;
433
    viewports[i].y = y;
433
    viewports[i].y = y;
434
    viewports[i].width = width;
434
    viewports[i].width = width;
435
    viewports[i].height = height;
435
    viewports[i].height = height;
436
   
436
   
437
    viewports[i].rows = height / FONT_SCANLINES;
437
    viewports[i].rows = height / FONT_SCANLINES;
438
    viewports[i].cols = width / COL_WIDTH;
438
    viewports[i].cols = width / COL_WIDTH;
439
 
439
 
440
    viewports[i].style.bg_color = DEFAULT_BGCOLOR;
440
    viewports[i].style.bg_color = DEFAULT_BGCOLOR;
441
    viewports[i].style.fg_color = DEFAULT_FGCOLOR;
441
    viewports[i].style.fg_color = DEFAULT_FGCOLOR;
442
   
442
   
443
    viewports[i].cur_col = 0;
443
    viewports[i].cur_col = 0;
444
    viewports[i].cur_row = 0;
444
    viewports[i].cur_row = 0;
445
    viewports[i].cursor_active = 0;
445
    viewports[i].cursor_active = 0;
446
 
446
 
447
    viewports[i].initialized = 1;
447
    viewports[i].initialized = 1;
448
 
448
 
449
    return i;
449
    return i;
450
}
450
}
451
 
451
 
452
 
452
 
453
/** Initialize framebuffer as a chardev output device
453
/** Initialize framebuffer as a chardev output device
454
 *
454
 *
455
 * @param addr Address of theframebuffer
455
 * @param addr Address of theframebuffer
456
 * @param xres Screen width in pixels
456
 * @param xres Screen width in pixels
457
 * @param yres Screen height in pixels
457
 * @param yres Screen height in pixels
458
 * @param bpp  Bits per pixel (8, 16, 24, 32)
458
 * @param bpp  Bits per pixel (8, 16, 24, 32)
459
 * @param scan Bytes per one scanline
459
 * @param scan Bytes per one scanline
460
 * @param align Alignment for 24bpp mode.
460
 * @param align Alignment for 24bpp mode.
461
 * @param invert_colors Inverted colors.
461
 * @param invert_colors Inverted colors.
462
 *
462
 *
463
 */
463
 */
464
static void
464
static void
465
screen_init(void *addr, unsigned int xres, unsigned int yres, unsigned int bpp, unsigned int scan,
465
screen_init(void *addr, unsigned int xres, unsigned int yres, unsigned int bpp, unsigned int scan,
466
    int align, int invert_colors)
466
    int align, int invert_colors)
467
{
467
{
468
    switch (bpp) {
468
    switch (bpp) {
469
    case 8:
469
    case 8:
470
        screen.rgb2scr = rgb_1byte;
470
        screen.rgb2scr = rgb_1byte;
471
        screen.scr2rgb = byte1_rgb;
471
        screen.scr2rgb = byte1_rgb;
472
        screen.pixelbytes = 1;
472
        screen.pixelbytes = 1;
473
        break;
473
        break;
474
    case 16:
474
    case 16:
475
        screen.rgb2scr = rgb_2byte;
475
        screen.rgb2scr = rgb_2byte;
476
        screen.scr2rgb = byte2_rgb;
476
        screen.scr2rgb = byte2_rgb;
477
        screen.pixelbytes = 2;
477
        screen.pixelbytes = 2;
478
        break;
478
        break;
479
    case 24:
479
    case 24:
480
        screen.rgb2scr = rgb_3byte;
480
        screen.rgb2scr = rgb_3byte;
481
        screen.scr2rgb = byte3_rgb;
481
        screen.scr2rgb = byte3_rgb;
482
        if (!align)
482
        if (!align)
483
            screen.pixelbytes = 3;
483
            screen.pixelbytes = 3;
484
        else
484
        else
485
            screen.pixelbytes = 4;
485
            screen.pixelbytes = 4;
486
        break;
486
        break;
487
    case 32:
487
    case 32:
488
        screen.rgb2scr = rgb_4byte;
488
        screen.rgb2scr = rgb_4byte;
489
        screen.scr2rgb = byte4_rgb;
489
        screen.scr2rgb = byte4_rgb;
490
        screen.pixelbytes = 4;
490
        screen.pixelbytes = 4;
491
        break;
491
        break;
492
    }
492
    }
493
 
493
 
494
    screen.fbaddress = (unsigned char *) addr;
494
    screen.fbaddress = (unsigned char *) addr;
495
    screen.xres = xres;
495
    screen.xres = xres;
496
    screen.yres = yres;
496
    screen.yres = yres;
497
    screen.scanline = scan;
497
    screen.scanline = scan;
498
    screen.invert_colors = invert_colors;
498
    screen.invert_colors = invert_colors;
499
   
499
   
500
    /* Create first viewport */
500
    /* Create first viewport */
501
    viewport_create(0,0,xres,yres);
501
    viewport_create(0,0,xres,yres);
502
}
502
}
503
 
503
 
504
/** Hide cursor if it is shown */
504
/** Hide cursor if it is shown */
505
static void cursor_hide(viewport_t *vport)
505
static void cursor_hide(viewport_t *vport)
506
{
506
{
507
    if (vport->cursor_active && vport->cursor_shown) {
507
    if (vport->cursor_active && vport->cursor_shown) {
508
        invert_char(vport, vport->cur_row, vport->cur_col);
508
        invert_char(vport, vport->cur_row, vport->cur_col);
509
        vport->cursor_shown = 0;
509
        vport->cursor_shown = 0;
510
    }
510
    }
511
}
511
}
512
 
512
 
513
/** Show cursor if cursor showing is enabled */
513
/** Show cursor if cursor showing is enabled */
514
static void cursor_print(viewport_t *vport)
514
static void cursor_print(viewport_t *vport)
515
{
515
{
516
    /* Do not check for cursor_shown */
516
    /* Do not check for cursor_shown */
517
    if (vport->cursor_active) {
517
    if (vport->cursor_active) {
518
        invert_char(vport, vport->cur_row, vport->cur_col);
518
        invert_char(vport, vport->cur_row, vport->cur_col);
519
        vport->cursor_shown = 1;
519
        vport->cursor_shown = 1;
520
    }
520
    }
521
}
521
}
522
 
522
 
523
/** Invert cursor, if it is enabled */
523
/** Invert cursor, if it is enabled */
524
static void cursor_blink(viewport_t *vport)
524
static void cursor_blink(viewport_t *vport)
525
{
525
{
526
    if (vport->cursor_shown)
526
    if (vport->cursor_shown)
527
        cursor_hide(vport);
527
        cursor_hide(vport);
528
    else
528
    else
529
        cursor_print(vport);
529
        cursor_print(vport);
530
}
530
}
531
 
531
 
532
/** Draw character at given position relative to viewport
532
/** Draw character at given position relative to viewport
533
 *
533
 *
534
 * @param vport Viewport identification
534
 * @param vport Viewport identification
535
 * @param c Character to print
535
 * @param c Character to print
536
 * @param row Screen position relative to viewport
536
 * @param row Screen position relative to viewport
537
 * @param col Screen position relative to viewport
537
 * @param col Screen position relative to viewport
538
 * @param transparent If false, print background color with character
538
 * @param transparent If false, print background color with character
539
 */
539
 */
540
static void draw_char(viewport_t *vport, char c, unsigned int row, unsigned int col,
540
static void draw_char(viewport_t *vport, char c, unsigned int row, unsigned int col,
541
              style_t style, int transparent)
541
              style_t style, int transparent)
542
{
542
{
543
    /* Optimize - do not hide cursor if we are going to overwrite it */
543
    /* Optimize - do not hide cursor if we are going to overwrite it */
544
    if (vport->cursor_active && vport->cursor_shown &&
544
    if (vport->cursor_active && vport->cursor_shown &&
545
        (vport->cur_col != col || vport->cur_row != row))
545
        (vport->cur_col != col || vport->cur_row != row))
546
        invert_char(vport, vport->cur_row, vport->cur_col);
546
        invert_char(vport, vport->cur_row, vport->cur_col);
547
   
547
   
548
    draw_glyph(vport, c, col * COL_WIDTH, row * FONT_SCANLINES, style, transparent);
548
    draw_glyph(vport, c, col * COL_WIDTH, row * FONT_SCANLINES, style, transparent);
549
 
549
 
550
    vport->cur_col = col;
550
    vport->cur_col = col;
551
    vport->cur_row = row;
551
    vport->cur_row = row;
552
 
552
 
553
    vport->cur_col++;
553
    vport->cur_col++;
554
    if (vport->cur_col>= vport->cols) {
554
    if (vport->cur_col>= vport->cols) {
555
        vport->cur_col = 0;
555
        vport->cur_col = 0;
556
        vport->cur_row++;
556
        vport->cur_row++;
557
        if (vport->cur_row >= vport->rows)
557
        if (vport->cur_row >= vport->rows)
558
            vport->cur_row--;
558
            vport->cur_row--;
559
    }
559
    }
560
    cursor_print(vport);
560
    cursor_print(vport);
561
}
561
}
562
 
562
 
563
/** Draw text data to viewport
563
/** Draw text data to viewport
564
 *
564
 *
565
 * @param vport Viewport id
565
 * @param vport Viewport id
566
 * @param data Text data fitting exactly into viewport
566
 * @param data Text data fitting exactly into viewport
567
 */
567
 */
568
static void draw_text_data(viewport_t *vport, keyfield_t *data)
568
static void draw_text_data(viewport_t *vport, keyfield_t *data)
569
{
569
{
570
    int i;
570
    int i;
571
    int col,row;
571
    int col,row;
572
 
572
 
573
    clear_port(vport);
573
    clear_port(vport);
574
    for (i=0; i < vport->cols * vport->rows; i++) {
574
    for (i=0; i < vport->cols * vport->rows; i++) {
575
        if (data[i].character == ' ' && style_same(data[i].style,vport->style))
575
        if (data[i].character == ' ' && style_same(data[i].style,vport->style))
576
            continue;
576
            continue;
577
        col = i % vport->cols;
577
        col = i % vport->cols;
578
        row = i / vport->cols;
578
        row = i / vport->cols;
579
        draw_glyph(vport, data[i].character, col * COL_WIDTH, row * FONT_SCANLINES,
579
        draw_glyph(vport, data[i].character, col * COL_WIDTH, row * FONT_SCANLINES,
580
               data[i].style, style_same(data[i].style,vport->style));
580
               data[i].style, style_same(data[i].style,vport->style));
581
    }
581
    }
582
    cursor_print(vport);
582
    cursor_print(vport);
583
}
583
}
584
 
584
 
585
 
585
 
586
/** Return first free pixmap */
586
/** Return first free pixmap */
587
static int find_free_pixmap(void)
587
static int find_free_pixmap(void)
588
{
588
{
589
    int i;
589
    int i;
590
   
590
   
591
    for (i=0;i < MAX_PIXMAPS;i++)
591
    for (i=0;i < MAX_PIXMAPS;i++)
592
        if (!pixmaps[i].data)
592
        if (!pixmaps[i].data)
593
            return i;
593
            return i;
594
    return -1;
594
    return -1;
595
}
595
}
596
 
596
 
597
static void putpixel_pixmap(int pm, unsigned int x, unsigned int y, int color)
597
static void putpixel_pixmap(int pm, unsigned int x, unsigned int y, int color)
598
{
598
{
599
    pixmap_t *pmap = &pixmaps[pm];
599
    pixmap_t *pmap = &pixmaps[pm];
600
    int pos = (y * pmap->width + x) * screen.pixelbytes;
600
    int pos = (y * pmap->width + x) * screen.pixelbytes;
601
 
601
 
602
    (*screen.rgb2scr)(&pmap->data[pos],COLOR(color));
602
    (*screen.rgb2scr)(&pmap->data[pos],COLOR(color));
603
}
603
}
604
 
604
 
605
/** Create a new pixmap and return appropriate ID */
605
/** Create a new pixmap and return appropriate ID */
606
static int shm2pixmap(unsigned char *shm, size_t size)
606
static int shm2pixmap(unsigned char *shm, size_t size)
607
{
607
{
608
    int pm;
608
    int pm;
609
    pixmap_t *pmap;
609
    pixmap_t *pmap;
610
 
610
 
611
    pm = find_free_pixmap();
611
    pm = find_free_pixmap();
612
    if (pm == -1)
612
    if (pm == -1)
613
        return ELIMIT;
613
        return ELIMIT;
614
    pmap = &pixmaps[pm];
614
    pmap = &pixmaps[pm];
615
   
615
   
616
    if (ppm_get_data(shm, size, &pmap->width, &pmap->height))
616
    if (ppm_get_data(shm, size, &pmap->width, &pmap->height))
617
        return EINVAL;
617
        return EINVAL;
618
   
618
   
619
    pmap->data = malloc(pmap->width * pmap->height * screen.pixelbytes);
619
    pmap->data = malloc(pmap->width * pmap->height * screen.pixelbytes);
620
    if (!pmap->data)
620
    if (!pmap->data)
621
        return ENOMEM;
621
        return ENOMEM;
622
 
622
 
623
    ppm_draw(shm, size, 0, 0, pmap->width, pmap->height,
623
    ppm_draw(shm, size, 0, 0, pmap->width, pmap->height,
624
         (putpixel_cb_t)putpixel_pixmap, (void *)pm);
624
         (putpixel_cb_t)putpixel_pixmap, (void *)pm);
625
 
625
 
626
    return pm;
626
    return pm;
627
}
627
}
628
 
628
 
629
/** Handle shared memory communication calls
629
/** Handle shared memory communication calls
630
 *
630
 *
631
 * Protocol for drawing pixmaps:
631
 * Protocol for drawing pixmaps:
632
 * - FB_PREPARE_SHM(client shm identification)
632
 * - FB_PREPARE_SHM(client shm identification)
633
 * - IPC_M_SEND_AS_AREA
633
 * - IPC_M_SEND_AS_AREA
634
 * - FB_DRAW_PPM(startx,starty)
634
 * - FB_DRAW_PPM(startx,starty)
635
 * - FB_DROP_SHM
635
 * - FB_DROP_SHM
636
 *
636
 *
637
 * Protocol for text drawing
637
 * Protocol for text drawing
638
 * - IPC_M_SEND_AS_AREA
638
 * - IPC_M_SEND_AS_AREA
639
 * - FB_DRAW_TEXT_DATA
639
 * - FB_DRAW_TEXT_DATA
640
 *
640
 *
641
 * @param callid Callid of the current call
641
 * @param callid Callid of the current call
642
 * @param call Current call data
642
 * @param call Current call data
643
 * @param vp Active viewport
643
 * @param vp Active viewport
644
 * @return 0 if the call was not handled byt this function, 1 otherwise
644
 * @return 0 if the call was not handled byt this function, 1 otherwise
645
 *
645
 *
646
 * note: this function is not threads safe, you would have
646
 * note: this function is not threads safe, you would have
647
 * to redefine static variables with __thread
647
 * to redefine static variables with __thread
648
 */
648
 */
649
static int shm_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
649
static int shm_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
650
{
650
{
651
    static keyfield_t *interbuffer = NULL;
651
    static keyfield_t *interbuffer = NULL;
652
    static size_t intersize = 0;
652
    static size_t intersize = 0;
653
 
653
 
654
    static unsigned char *shm = NULL;
654
    static unsigned char *shm = NULL;
655
    static ipcarg_t shm_id = 0;
655
    static ipcarg_t shm_id = 0;
656
    static size_t shm_size;
656
    static size_t shm_size;
657
 
657
 
658
    int handled = 1;
658
    int handled = 1;
659
    int retval = 0;
659
    int retval = 0;
660
    viewport_t *vport = &viewports[vp];
660
    viewport_t *vport = &viewports[vp];
661
    unsigned int x,y;
661
    unsigned int x,y;
662
 
662
 
663
    switch (IPC_GET_METHOD(*call)) {
663
    switch (IPC_GET_METHOD(*call)) {
664
    case IPC_M_AS_AREA_SEND:
664
    case IPC_M_AS_AREA_SEND:
665
        /* We accept one area for data interchange */
665
        /* We accept one area for data interchange */
666
        if (IPC_GET_ARG1(*call) == shm_id) {
666
        if (IPC_GET_ARG1(*call) == shm_id) {
667
            void *dest = as_get_mappable_page(IPC_GET_ARG2(*call));
667
            void *dest = as_get_mappable_page(IPC_GET_ARG2(*call));
668
            shm_size = IPC_GET_ARG2(*call);
668
            shm_size = IPC_GET_ARG2(*call);
669
            if (!ipc_answer_fast(callid, 0, (sysarg_t)dest, 0))
669
            if (!ipc_answer_fast(callid, 0, (sysarg_t)dest, 0))
670
                shm = dest;
670
                shm = dest;
671
            else
671
            else
672
                shm_id = 0;
672
                shm_id = 0;
673
            if (shm[0] != 'P')
673
            if (shm[0] != 'P')
674
                while (1)
674
                while (1)
675
                    ;
675
                    ;
676
            return 1;
676
            return 1;
677
        } else {
677
        } else {
678
            intersize = IPC_GET_ARG2(*call);
678
            intersize = IPC_GET_ARG2(*call);
679
            receive_comm_area(callid,call,(void *)&interbuffer);
679
            receive_comm_area(callid,call,(void *)&interbuffer);
680
        }
680
        }
681
        return 1;
681
        return 1;
682
    case FB_PREPARE_SHM:
682
    case FB_PREPARE_SHM:
683
        if (shm_id)
683
        if (shm_id)
684
            retval = EBUSY;
684
            retval = EBUSY;
685
        else
685
        else
686
            shm_id = IPC_GET_ARG1(*call);
686
            shm_id = IPC_GET_ARG1(*call);
687
        break;
687
        break;
688
       
688
       
689
    case FB_DROP_SHM:
689
    case FB_DROP_SHM:
690
        if (shm) {
690
        if (shm) {
691
            as_area_destroy(shm);
691
            as_area_destroy(shm);
692
            shm = NULL;
692
            shm = NULL;
693
        }
693
        }
694
        shm_id = 0;
694
        shm_id = 0;
695
        break;
695
        break;
696
 
696
 
697
    case FB_SHM2PIXMAP:
697
    case FB_SHM2PIXMAP:
698
        if (!shm) {
698
        if (!shm) {
699
            retval = EINVAL;
699
            retval = EINVAL;
700
            break;
700
            break;
701
        }
701
        }
702
        retval = shm2pixmap(shm, shm_size);
702
        retval = shm2pixmap(shm, shm_size);
703
        break;
703
        break;
704
    case FB_DRAW_PPM:
704
    case FB_DRAW_PPM:
705
        if (!shm) {
705
        if (!shm) {
706
            retval = EINVAL;
706
            retval = EINVAL;
707
            break;
707
            break;
708
        }
708
        }
709
        x = IPC_GET_ARG1(*call);
709
        x = IPC_GET_ARG1(*call);
710
        y = IPC_GET_ARG2(*call);
710
        y = IPC_GET_ARG2(*call);
711
        if (x > vport->width || y > vport->height) {
711
        if (x > vport->width || y > vport->height) {
712
            retval = EINVAL;
712
            retval = EINVAL;
713
            break;
713
            break;
714
        }
714
        }
715
       
715
       
716
        ppm_draw(shm, shm_size, IPC_GET_ARG1(*call), IPC_GET_ARG2(*call),
716
        ppm_draw(shm, shm_size, IPC_GET_ARG1(*call), IPC_GET_ARG2(*call),
717
             vport->width - x, vport->height - y, (putpixel_cb_t)putpixel, vport);
717
             vport->width - x, vport->height - y, (putpixel_cb_t)putpixel, vport);
718
        break;
718
        break;
719
    case FB_DRAW_TEXT_DATA:
719
    case FB_DRAW_TEXT_DATA:
720
        if (!interbuffer) {
720
        if (!interbuffer) {
721
            retval = EINVAL;
721
            retval = EINVAL;
722
            break;
722
            break;
723
        }
723
        }
724
        if (intersize < vport->cols*vport->rows*sizeof(*interbuffer)) {
724
        if (intersize < vport->cols*vport->rows*sizeof(*interbuffer)) {
725
            retval = EINVAL;
725
            retval = EINVAL;
726
            break;
726
            break;
727
        }
727
        }
728
        draw_text_data(vport, interbuffer);
728
        draw_text_data(vport, interbuffer);
729
        break;
729
        break;
730
    default:
730
    default:
731
        handled = 0;
731
        handled = 0;
732
    }
732
    }
733
   
733
   
734
    if (handled)
734
    if (handled)
735
        ipc_answer_fast(callid, retval, 0, 0);
735
        ipc_answer_fast(callid, retval, 0, 0);
736
    return handled;
736
    return handled;
737
}
737
}
738
 
738
 
739
static void copy_vp_to_pixmap(viewport_t *vport, pixmap_t *pmap)
739
static void copy_vp_to_pixmap(viewport_t *vport, pixmap_t *pmap)
740
{
740
{
741
    int y;
741
    int y;
742
    int rowsize;
742
    int rowsize;
743
    int tmp;
743
    int tmp;
744
    int width = vport->width;
744
    int width = vport->width;
745
    int height = vport->height;
745
    int height = vport->height;
746
 
746
 
747
    if (width + vport->x > screen.xres)
747
    if (width + vport->x > screen.xres)
748
        width = screen.xres - vport->x;
748
        width = screen.xres - vport->x;
749
    if (height + vport->y  > screen.yres)
749
    if (height + vport->y  > screen.yres)
750
        height = screen.yres - vport->y;
750
        height = screen.yres - vport->y;
751
 
751
 
752
    rowsize = width * screen.pixelbytes;
752
    rowsize = width * screen.pixelbytes;
753
 
753
 
754
    for (y=0;y < height; y++) {
754
    for (y=0;y < height; y++) {
755
        tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
755
        tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
756
        memcpy(pmap->data + rowsize*y, screen.fbaddress + tmp, rowsize);
756
        memcpy(pmap->data + rowsize*y, screen.fbaddress + tmp, rowsize);
757
    }
757
    }
758
}
758
}
759
 
759
 
760
/** Save viewport to pixmap */
760
/** Save viewport to pixmap */
761
static int save_vp_to_pixmap(viewport_t *vport)
761
static int save_vp_to_pixmap(viewport_t *vport)
762
{
762
{
763
    int pm;
763
    int pm;
764
    pixmap_t *pmap;
764
    pixmap_t *pmap;
765
 
765
 
766
    pm = find_free_pixmap();
766
    pm = find_free_pixmap();
767
    if (pm == -1)
767
    if (pm == -1)
768
        return ELIMIT;
768
        return ELIMIT;
769
   
769
   
770
    pmap = &pixmaps[pm];
770
    pmap = &pixmaps[pm];
771
    pmap->data = malloc(screen.pixelbytes * vport->width * vport->height);
771
    pmap->data = malloc(screen.pixelbytes * vport->width * vport->height);
772
    if (!pmap->data)
772
    if (!pmap->data)
773
        return ENOMEM;
773
        return ENOMEM;
774
 
774
 
775
    pmap->width = vport->width;
775
    pmap->width = vport->width;
776
    pmap->height = vport->height;
776
    pmap->height = vport->height;
777
 
777
 
778
    copy_vp_to_pixmap(vport, pmap);
778
    copy_vp_to_pixmap(vport, pmap);
779
   
779
   
780
    return pm;
780
    return pm;
781
}
781
}
782
 
782
 
783
/** Draw pixmap on screen
783
/** Draw pixmap on screen
784
 *
784
 *
785
 * @param vp Viewport to draw on
785
 * @param vp Viewport to draw on
786
 * @param pm Pixmap identifier
786
 * @param pm Pixmap identifier
787
 */
787
 */
788
static int draw_pixmap(int vp, int pm)
788
static int draw_pixmap(int vp, int pm)
789
{
789
{
790
    pixmap_t *pmap = &pixmaps[pm];
790
    pixmap_t *pmap = &pixmaps[pm];
791
    viewport_t *vport = &viewports[vp];
791
    viewport_t *vport = &viewports[vp];
792
    int y;
792
    int y;
793
    int tmp, srcrowsize;
793
    int tmp, srcrowsize;
794
    int realwidth, realheight, realrowsize;
794
    int realwidth, realheight, realrowsize;
795
    int width = vport->width;
795
    int width = vport->width;
796
    int height = vport->height;
796
    int height = vport->height;
797
 
797
 
798
    if (width + vport->x > screen.xres)
798
    if (width + vport->x > screen.xres)
799
        width = screen.xres - vport->x;
799
        width = screen.xres - vport->x;
800
    if (height + vport->y > screen.yres)
800
    if (height + vport->y > screen.yres)
801
        height = screen.yres - vport->y;
801
        height = screen.yres - vport->y;
802
 
802
 
803
    if (!pmap->data)
803
    if (!pmap->data)
804
        return EINVAL;
804
        return EINVAL;
805
 
805
 
806
    realwidth = pmap->width <= width ? pmap->width : width;
806
    realwidth = pmap->width <= width ? pmap->width : width;
807
    realheight = pmap->height <= height ? pmap->height : height;
807
    realheight = pmap->height <= height ? pmap->height : height;
808
 
808
 
809
    srcrowsize = vport->width * screen.pixelbytes;
809
    srcrowsize = vport->width * screen.pixelbytes;
810
    realrowsize = realwidth * screen.pixelbytes;
810
    realrowsize = realwidth * screen.pixelbytes;
811
 
811
 
812
    for (y=0; y < realheight; y++) {
812
    for (y=0; y < realheight; y++) {
813
        tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
813
        tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
814
        memcpy(screen.fbaddress + tmp, pmap->data + y * srcrowsize, realrowsize);
814
        memcpy(screen.fbaddress + tmp, pmap->data + y * srcrowsize, realrowsize);
815
    }
815
    }
816
    return 0;
816
    return 0;
817
}
817
}
818
 
818
 
819
/** Tick animation one step forward */
819
/** Tick animation one step forward */
820
static void anims_tick(void)
820
static void anims_tick(void)
821
{
821
{
822
    int i;
822
    int i;
823
    static int counts = 0;
823
    static int counts = 0;
824
   
824
   
825
    /* Limit redrawing */
825
    /* Limit redrawing */
826
    counts = (counts+1) % 8;
826
    counts = (counts+1) % 8;
827
    if (counts)
827
    if (counts)
828
        return;
828
        return;
829
 
829
 
830
    for (i=0; i < MAX_ANIMATIONS; i++) {
830
    for (i=0; i < MAX_ANIMATIONS; i++) {
831
        if (!animations[i].animlen || !animations[i].initialized || !animations[i].enabled)
831
        if (!animations[i].animlen || !animations[i].initialized || !animations[i].enabled)
832
            continue;
832
            continue;
833
        draw_pixmap(animations[i].vp, animations[i].pixmaps[animations[i].pos]);
833
        draw_pixmap(animations[i].vp, animations[i].pixmaps[animations[i].pos]);
834
        animations[i].pos = (animations[i].pos+1) % animations[i].animlen;
834
        animations[i].pos = (animations[i].pos+1) % animations[i].animlen;
835
    }
835
    }
836
}
836
}
837
 
837
 
838
 
838
 
839
static int pointer_x, pointer_y;
839
static int pointer_x, pointer_y;
840
static int pointer_shown, pointer_enabled;
840
static int pointer_shown, pointer_enabled;
841
static int pointer_vport = -1;
841
static int pointer_vport = -1;
842
static int pointer_pixmap = -1;
842
static int pointer_pixmap = -1;
843
 
843
 
844
static void mouse_show(void)
844
static void mouse_show(void)
845
{
845
{
846
    int i,j;
846
    int i,j;
847
    int visibility;
847
    int visibility;
848
    int color;
848
    int color;
849
    int bytepos;
849
    int bytepos;
850
 
850
 
851
    if (pointer_shown || !pointer_enabled)
851
    if (pointer_shown || !pointer_enabled)
852
        return;
852
        return;
853
 
853
 
854
    /* Save image under the cursor */
854
    /* Save image under the cursor */
855
    if (pointer_vport == -1) {
855
    if (pointer_vport == -1) {
856
        pointer_vport = viewport_create(pointer_x, pointer_y, pointer_width, pointer_height);
856
        pointer_vport = viewport_create(pointer_x, pointer_y, pointer_width, pointer_height);
857
        if (pointer_vport < 0)
857
        if (pointer_vport < 0)
858
            return;
858
            return;
859
    } else {
859
    } else {
860
        viewports[pointer_vport].x = pointer_x;
860
        viewports[pointer_vport].x = pointer_x;
861
        viewports[pointer_vport].y = pointer_y;
861
        viewports[pointer_vport].y = pointer_y;
862
    }
862
    }
863
 
863
 
864
    if (pointer_pixmap == -1)
864
    if (pointer_pixmap == -1)
865
        pointer_pixmap = save_vp_to_pixmap(&viewports[pointer_vport]);
865
        pointer_pixmap = save_vp_to_pixmap(&viewports[pointer_vport]);
866
    else
866
    else
867
        copy_vp_to_pixmap(&viewports[pointer_vport], &pixmaps[pointer_pixmap]);
867
        copy_vp_to_pixmap(&viewports[pointer_vport], &pixmaps[pointer_pixmap]);
868
 
868
 
869
    /* Draw cursor */
869
    /* Draw cursor */
870
    for (i=0; i < pointer_height; i++)
870
    for (i=0; i < pointer_height; i++)
871
        for (j=0;j < pointer_width; j++) {
871
        for (j=0;j < pointer_width; j++) {
872
            bytepos = i*((pointer_width-1)/8+1) + j/8;
872
            bytepos = i*((pointer_width-1)/8+1) + j/8;
873
            visibility = pointer_mask_bits[bytepos] & (1 << (j % 8));
873
            visibility = pointer_mask_bits[bytepos] & (1 << (j % 8));
874
            if (visibility) {
874
            if (visibility) {
875
                color = pointer_bits[bytepos] & (1 << (j % 8)) ? 0 : 0xffffff;
875
                color = pointer_bits[bytepos] & (1 << (j % 8)) ? 0 : 0xffffff;
876
                if (pointer_x+j < screen.xres && pointer_y+i < screen.yres)
876
                if (pointer_x+j < screen.xres && pointer_y+i < screen.yres)
877
                    putpixel(&viewports[0], pointer_x+j, pointer_y+i, color);
877
                    putpixel(&viewports[0], pointer_x+j, pointer_y+i, color);
878
            }
878
            }
879
        }
879
        }
880
    pointer_shown = 1;
880
    pointer_shown = 1;
881
}
881
}
882
 
882
 
883
static void mouse_hide(void)
883
static void mouse_hide(void)
884
{
884
{
885
    /* Restore image under the cursor */
885
    /* Restore image under the cursor */
886
    if (pointer_shown) {
886
    if (pointer_shown) {
887
        draw_pixmap(pointer_vport, pointer_pixmap);
887
        draw_pixmap(pointer_vport, pointer_pixmap);
888
        pointer_shown = 0;
888
        pointer_shown = 0;
889
    }
889
    }
890
}
890
}
891
 
891
 
892
static void mouse_move(unsigned int x, unsigned int y)
892
static void mouse_move(unsigned int x, unsigned int y)
893
{
893
{
894
    mouse_hide();
894
    mouse_hide();
895
    pointer_x = x;
895
    pointer_x = x;
896
    pointer_y = y;
896
    pointer_y = y;
897
    mouse_show();
897
    mouse_show();
898
}
898
}
899
 
899
 
900
static int anim_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
900
static int anim_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
901
{
901
{
902
    int handled = 1;
902
    int handled = 1;
903
    int retval = 0;
903
    int retval = 0;
904
    int i,nvp;
904
    int i,nvp;
905
    int newval;
905
    int newval;
906
 
906
 
907
    switch (IPC_GET_METHOD(*call)) {
907
    switch (IPC_GET_METHOD(*call)) {
908
    case FB_ANIM_CREATE:
908
    case FB_ANIM_CREATE:
909
        nvp = IPC_GET_ARG1(*call);
909
        nvp = IPC_GET_ARG1(*call);
910
        if (nvp == -1)
910
        if (nvp == -1)
911
            nvp = vp;
911
            nvp = vp;
912
        if (nvp >= MAX_VIEWPORTS || nvp < 0 || !viewports[nvp].initialized) {
912
        if (nvp >= MAX_VIEWPORTS || nvp < 0 || !viewports[nvp].initialized) {
913
            retval = EINVAL;
913
            retval = EINVAL;
914
            break;
914
            break;
915
        }
915
        }
916
        for (i=0; i < MAX_ANIMATIONS; i++) {
916
        for (i=0; i < MAX_ANIMATIONS; i++) {
917
            if (! animations[i].initialized)
917
            if (! animations[i].initialized)
918
                break;
918
                break;
919
        }
919
        }
920
        if (i == MAX_ANIMATIONS) {
920
        if (i == MAX_ANIMATIONS) {
921
            retval = ELIMIT;
921
            retval = ELIMIT;
922
            break;
922
            break;
923
        }
923
        }
924
        animations[i].initialized = 1;
924
        animations[i].initialized = 1;
925
        animations[i].animlen = 0;
925
        animations[i].animlen = 0;
926
        animations[i].pos = 0;
926
        animations[i].pos = 0;
927
        animations[i].enabled = 0;
927
        animations[i].enabled = 0;
928
        animations[i].vp = nvp;
928
        animations[i].vp = nvp;
929
        retval = i;
929
        retval = i;
930
        break;
930
        break;
931
    case FB_ANIM_DROP:
931
    case FB_ANIM_DROP:
932
        i = IPC_GET_ARG1(*call);
932
        i = IPC_GET_ARG1(*call);
933
        if (i >= MAX_ANIMATIONS || i < 0) {
933
        if (i >= MAX_ANIMATIONS || i < 0) {
934
            retval = EINVAL;
934
            retval = EINVAL;
935
            break;
935
            break;
936
        }
936
        }
937
        animations[i].initialized = 0;
937
        animations[i].initialized = 0;
938
        break;
938
        break;
939
    case FB_ANIM_ADDPIXMAP:
939
    case FB_ANIM_ADDPIXMAP:
940
        i = IPC_GET_ARG1(*call);
940
        i = IPC_GET_ARG1(*call);
941
        if (i >= MAX_ANIMATIONS || i < 0 || !animations[i].initialized) {
941
        if (i >= MAX_ANIMATIONS || i < 0 || !animations[i].initialized) {
942
            retval = EINVAL;
942
            retval = EINVAL;
943
            break;
943
            break;
944
        }
944
        }
945
        if (animations[i].animlen == MAX_ANIM_LEN) {
945
        if (animations[i].animlen == MAX_ANIM_LEN) {
946
            retval = ELIMIT;
946
            retval = ELIMIT;
947
            break;
947
            break;
948
        }
948
        }
949
        newval = IPC_GET_ARG2(*call);
949
        newval = IPC_GET_ARG2(*call);
950
        if (newval < 0 || newval > MAX_PIXMAPS || !pixmaps[newval].data) {
950
        if (newval < 0 || newval > MAX_PIXMAPS || !pixmaps[newval].data) {
951
            retval = EINVAL;
951
            retval = EINVAL;
952
            break;
952
            break;
953
        }
953
        }
954
        animations[i].pixmaps[animations[i].animlen++] = newval;
954
        animations[i].pixmaps[animations[i].animlen++] = newval;
955
        break;
955
        break;
956
    case FB_ANIM_CHGVP:
956
    case FB_ANIM_CHGVP:
957
        i = IPC_GET_ARG1(*call);
957
        i = IPC_GET_ARG1(*call);
958
        if (i >= MAX_ANIMATIONS || i < 0) {
958
        if (i >= MAX_ANIMATIONS || i < 0) {
959
            retval = EINVAL;
959
            retval = EINVAL;
960
            break;
960
            break;
961
        }
961
        }
962
        nvp = IPC_GET_ARG2(*call);
962
        nvp = IPC_GET_ARG2(*call);
963
        if (nvp == -1)
963
        if (nvp == -1)
964
            nvp = vp;
964
            nvp = vp;
965
        if (nvp >= MAX_VIEWPORTS || nvp < 0 || !viewports[nvp].initialized) {
965
        if (nvp >= MAX_VIEWPORTS || nvp < 0 || !viewports[nvp].initialized) {
966
            retval = EINVAL;
966
            retval = EINVAL;
967
            break;
967
            break;
968
        }
968
        }
969
        animations[i].vp = nvp;
969
        animations[i].vp = nvp;
970
        break;
970
        break;
971
    case FB_ANIM_START:
971
    case FB_ANIM_START:
972
    case FB_ANIM_STOP:
972
    case FB_ANIM_STOP:
973
        i = IPC_GET_ARG1(*call);
973
        i = IPC_GET_ARG1(*call);
974
        if (i >= MAX_ANIMATIONS || i < 0) {
974
        if (i >= MAX_ANIMATIONS || i < 0) {
975
            retval = EINVAL;
975
            retval = EINVAL;
976
            break;
976
            break;
977
        }
977
        }
978
        newval = (IPC_GET_METHOD(*call) == FB_ANIM_START);
978
        newval = (IPC_GET_METHOD(*call) == FB_ANIM_START);
979
        if (newval ^ animations[i].enabled) {
979
        if (newval ^ animations[i].enabled) {
980
            animations[i].enabled = newval;
980
            animations[i].enabled = newval;
981
            anims_enabled += newval ? 1 : -1;
981
            anims_enabled += newval ? 1 : -1;
982
        }
982
        }
983
        break;
983
        break;
984
    default:
984
    default:
985
        handled = 0;
985
        handled = 0;
986
    }
986
    }
987
    if (handled)
987
    if (handled)
988
        ipc_answer_fast(callid, retval, 0, 0);
988
        ipc_answer_fast(callid, retval, 0, 0);
989
    return handled;
989
    return handled;
990
}
990
}
991
 
991
 
992
/** Handler for messages concerning pixmap handling */
992
/** Handler for messages concerning pixmap handling */
993
static int pixmap_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
993
static int pixmap_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
994
{
994
{
995
    int handled = 1;
995
    int handled = 1;
996
    int retval = 0;
996
    int retval = 0;
997
    int i,nvp;
997
    int i,nvp;
998
 
998
 
999
    switch (IPC_GET_METHOD(*call)) {
999
    switch (IPC_GET_METHOD(*call)) {
1000
    case FB_VP_DRAW_PIXMAP:
1000
    case FB_VP_DRAW_PIXMAP:
1001
        nvp = IPC_GET_ARG1(*call);
1001
        nvp = IPC_GET_ARG1(*call);
1002
        if (nvp == -1)
1002
        if (nvp == -1)
1003
            nvp = vp;
1003
            nvp = vp;
1004
        if (nvp < 0 || nvp >= MAX_VIEWPORTS || !viewports[nvp].initialized) {
1004
        if (nvp < 0 || nvp >= MAX_VIEWPORTS || !viewports[nvp].initialized) {
1005
            retval = EINVAL;
1005
            retval = EINVAL;
1006
            break;
1006
            break;
1007
        }
1007
        }
1008
        i = IPC_GET_ARG2(*call);
1008
        i = IPC_GET_ARG2(*call);
1009
        retval = draw_pixmap(nvp, i);
1009
        retval = draw_pixmap(nvp, i);
1010
        break;
1010
        break;
1011
    case FB_VP2PIXMAP:
1011
    case FB_VP2PIXMAP:
1012
        nvp = IPC_GET_ARG1(*call);
1012
        nvp = IPC_GET_ARG1(*call);
1013
        if (nvp == -1)
1013
        if (nvp == -1)
1014
            nvp = vp;
1014
            nvp = vp;
1015
        if (nvp < 0 || nvp >= MAX_VIEWPORTS || !viewports[nvp].initialized)
1015
        if (nvp < 0 || nvp >= MAX_VIEWPORTS || !viewports[nvp].initialized)
1016
            retval = EINVAL;
1016
            retval = EINVAL;
1017
        else
1017
        else
1018
            retval = save_vp_to_pixmap(&viewports[nvp]);
1018
            retval = save_vp_to_pixmap(&viewports[nvp]);
1019
        break;
1019
        break;
1020
    case FB_DROP_PIXMAP:
1020
    case FB_DROP_PIXMAP:
1021
        i = IPC_GET_ARG1(*call);
1021
        i = IPC_GET_ARG1(*call);
1022
        if (i >= MAX_PIXMAPS) {
1022
        if (i >= MAX_PIXMAPS) {
1023
            retval = EINVAL;
1023
            retval = EINVAL;
1024
            break;
1024
            break;
1025
        }
1025
        }
1026
        if (pixmaps[i].data) {
1026
        if (pixmaps[i].data) {
1027
            free(pixmaps[i].data);
1027
            free(pixmaps[i].data);
1028
            pixmaps[i].data = NULL;
1028
            pixmaps[i].data = NULL;
1029
        }
1029
        }
1030
        break;
1030
        break;
1031
    default:
1031
    default:
1032
        handled = 0;
1032
        handled = 0;
1033
    }
1033
    }
1034
 
1034
 
1035
    if (handled)
1035
    if (handled)
1036
        ipc_answer_fast(callid, retval, 0, 0);
1036
        ipc_answer_fast(callid, retval, 0, 0);
1037
    return handled;
1037
    return handled;
1038
   
1038
   
1039
}
1039
}
1040
 
1040
 
1041
/** Function for handling connections to FB
1041
/** Function for handling connections to FB
1042
 *
1042
 *
1043
 */
1043
 */
1044
static void fb_client_connection(ipc_callid_t iid, ipc_call_t *icall)
1044
static void fb_client_connection(ipc_callid_t iid, ipc_call_t *icall)
1045
{
1045
{
1046
    ipc_callid_t callid;
1046
    ipc_callid_t callid;
1047
    ipc_call_t call;
1047
    ipc_call_t call;
1048
    int retval;
1048
    int retval;
1049
    int i;
1049
    int i;
1050
    unsigned int row,col;
1050
    unsigned int row,col;
1051
    char c;
1051
    char c;
1052
 
1052
 
1053
    int vp = 0;
1053
    int vp = 0;
1054
    viewport_t *vport = &viewports[0];
1054
    viewport_t *vport = &viewports[0];
1055
 
1055
 
1056
    if (client_connected) {
1056
    if (client_connected) {
1057
        ipc_answer_fast(iid, ELIMIT, 0,0);
1057
        ipc_answer_fast(iid, ELIMIT, 0,0);
1058
        return;
1058
        return;
1059
    }
1059
    }
1060
    client_connected = 1;
1060
    client_connected = 1;
1061
    ipc_answer_fast(iid, 0, 0, 0); /* Accept connection */
1061
    ipc_answer_fast(iid, 0, 0, 0); /* Accept connection */
1062
 
1062
 
1063
    while (1) {
1063
    while (1) {
1064
        if (vport->cursor_active || anims_enabled)
1064
        if (vport->cursor_active || anims_enabled)
1065
            callid = async_get_call_timeout(&call,250000);
1065
            callid = async_get_call_timeout(&call,250000);
1066
        else
1066
        else
1067
            callid = async_get_call(&call);
1067
            callid = async_get_call(&call);
1068
 
1068
 
1069
        mouse_hide();
1069
        mouse_hide();
1070
        if (!callid) {
1070
        if (!callid) {
1071
            cursor_blink(vport);
1071
            cursor_blink(vport);
1072
            anims_tick();
1072
            anims_tick();
1073
            mouse_show();
1073
            mouse_show();
1074
            continue;
1074
            continue;
1075
        }
1075
        }
1076
        if (shm_handle(callid, &call, vp))
1076
        if (shm_handle(callid, &call, vp))
1077
            continue;
1077
            continue;
1078
        if (pixmap_handle(callid, &call, vp))
1078
        if (pixmap_handle(callid, &call, vp))
1079
            continue;
1079
            continue;
1080
        if (anim_handle(callid, &call, vp))
1080
        if (anim_handle(callid, &call, vp))
1081
            continue;
1081
            continue;
1082
 
1082
 
1083
        switch (IPC_GET_METHOD(call)) {
1083
        switch (IPC_GET_METHOD(call)) {
1084
        case IPC_M_PHONE_HUNGUP:
1084
        case IPC_M_PHONE_HUNGUP:
1085
            client_connected = 0;
1085
            client_connected = 0;
1086
            /* cleanup other viewports */
1086
            /* cleanup other viewports */
1087
            for (i=1; i < MAX_VIEWPORTS; i++)
1087
            for (i=1; i < MAX_VIEWPORTS; i++)
1088
                vport->initialized = 0;
1088
                vport->initialized = 0;
1089
            return; /* Exit thread */
1089
            return; /* Exit thread */
1090
 
1090
 
1091
        case FB_PUTCHAR:
1091
        case FB_PUTCHAR:
1092
        case FB_TRANS_PUTCHAR:
1092
        case FB_TRANS_PUTCHAR:
1093
            c = IPC_GET_ARG1(call);
1093
            c = IPC_GET_ARG1(call);
1094
            row = IPC_GET_ARG2(call);
1094
            row = IPC_GET_ARG2(call);
1095
            col = IPC_GET_ARG3(call);
1095
            col = IPC_GET_ARG3(call);
1096
            if (row >= vport->rows || col >= vport->cols) {
1096
            if (row >= vport->rows || col >= vport->cols) {
1097
                retval = EINVAL;
1097
                retval = EINVAL;
1098
                break;
1098
                break;
1099
            }
1099
            }
1100
            ipc_answer_fast(callid,0,0,0);
1100
            ipc_answer_fast(callid,0,0,0);
1101
 
1101
 
1102
            draw_char(vport, c, row, col, vport->style, IPC_GET_METHOD(call) == FB_TRANS_PUTCHAR);
1102
            draw_char(vport, c, row, col, vport->style, IPC_GET_METHOD(call) == FB_TRANS_PUTCHAR);
1103
            continue; /* msg already answered */
1103
            continue; /* msg already answered */
1104
        case FB_CLEAR:
1104
        case FB_CLEAR:
1105
            clear_port(vport);
1105
            clear_port(vport);
1106
            cursor_print(vport);
1106
            cursor_print(vport);
1107
            retval = 0;
1107
            retval = 0;
1108
            break;
1108
            break;
1109
        case FB_CURSOR_GOTO:
1109
        case FB_CURSOR_GOTO:
1110
            row = IPC_GET_ARG1(call);
1110
            row = IPC_GET_ARG1(call);
1111
            col = IPC_GET_ARG2(call);
1111
            col = IPC_GET_ARG2(call);
1112
            if (row >= vport->rows || col >= vport->cols) {
1112
            if (row >= vport->rows || col >= vport->cols) {
1113
                retval = EINVAL;
1113
                retval = EINVAL;
1114
                break;
1114
                break;
1115
            }
1115
            }
1116
            retval = 0;
1116
            retval = 0;
1117
            cursor_hide(vport);
1117
            cursor_hide(vport);
1118
            vport->cur_col = col;
1118
            vport->cur_col = col;
1119
            vport->cur_row = row;
1119
            vport->cur_row = row;
1120
            cursor_print(vport);
1120
            cursor_print(vport);
1121
            break;
1121
            break;
1122
        case FB_CURSOR_VISIBILITY:
1122
        case FB_CURSOR_VISIBILITY:
1123
            cursor_hide(vport);
1123
            cursor_hide(vport);
1124
            vport->cursor_active = IPC_GET_ARG1(call);
1124
            vport->cursor_active = IPC_GET_ARG1(call);
1125
            cursor_print(vport);
1125
            cursor_print(vport);
1126
            retval = 0;
1126
            retval = 0;
1127
            break;
1127
            break;
1128
        case FB_GET_CSIZE:
1128
        case FB_GET_CSIZE:
1129
            ipc_answer_fast(callid, 0, vport->rows, vport->cols);
1129
            ipc_answer_fast(callid, 0, vport->rows, vport->cols);
1130
            continue;
1130
            continue;
1131
        case FB_SCROLL:
1131
        case FB_SCROLL:
1132
            i = IPC_GET_ARG1(call);
1132
            i = IPC_GET_ARG1(call);
1133
            if (i > vport->rows || i < (- (int)vport->rows)) {
1133
            if (i > vport->rows || i < (- (int)vport->rows)) {
1134
                retval = EINVAL;
1134
                retval = EINVAL;
1135
                break;
1135
                break;
1136
            }
1136
            }
1137
            cursor_hide(vport);
1137
            cursor_hide(vport);
1138
            scroll_port(vport, i*FONT_SCANLINES);
1138
            scroll_port(vport, i*FONT_SCANLINES);
1139
            cursor_print(vport);
1139
            cursor_print(vport);
1140
            retval = 0;
1140
            retval = 0;
1141
            break;
1141
            break;
1142
        case FB_VIEWPORT_DB:
1142
        case FB_VIEWPORT_DB:
1143
            /* Enable double buffering */
1143
            /* Enable double buffering */
1144
            i = IPC_GET_ARG1(call);
1144
            i = IPC_GET_ARG1(call);
1145
            if (i == -1)
1145
            if (i == -1)
1146
                i = vp;
1146
                i = vp;
1147
            if (i < 0 || i >= MAX_VIEWPORTS) {
1147
            if (i < 0 || i >= MAX_VIEWPORTS) {
1148
                retval = EINVAL;
1148
                retval = EINVAL;
1149
                break;
1149
                break;
1150
            }
1150
            }
1151
            if (! viewports[i].initialized ) {
1151
            if (! viewports[i].initialized ) {
1152
                retval = EADDRNOTAVAIL;
1152
                retval = EADDRNOTAVAIL;
1153
                break;
1153
                break;
1154
            }
1154
            }
1155
            viewports[i].dboffset = 0;
1155
            viewports[i].dboffset = 0;
1156
            if (IPC_GET_ARG2(call) == 1 && !viewports[i].dbdata)
1156
            if (IPC_GET_ARG2(call) == 1 && !viewports[i].dbdata)
1157
                viewports[i].dbdata = malloc(screen.pixelbytes*viewports[i].width * viewports[i].height);
1157
                viewports[i].dbdata = malloc(screen.pixelbytes*viewports[i].width * viewports[i].height);
1158
            else if (IPC_GET_ARG2(call) == 0 && viewports[i].dbdata) {
1158
            else if (IPC_GET_ARG2(call) == 0 && viewports[i].dbdata) {
1159
                free(viewports[i].dbdata);
1159
                free(viewports[i].dbdata);
1160
                viewports[i].dbdata = NULL;
1160
                viewports[i].dbdata = NULL;
1161
            }
1161
            }
1162
            retval = 0;
1162
            retval = 0;
1163
            break;
1163
            break;
1164
        case FB_VIEWPORT_SWITCH:
1164
        case FB_VIEWPORT_SWITCH:
1165
            i = IPC_GET_ARG1(call);
1165
            i = IPC_GET_ARG1(call);
1166
            if (i < 0 || i >= MAX_VIEWPORTS) {
1166
            if (i < 0 || i >= MAX_VIEWPORTS) {
1167
                retval = EINVAL;
1167
                retval = EINVAL;
1168
                break;
1168
                break;
1169
            }
1169
            }
1170
            if (! viewports[i].initialized ) {
1170
            if (! viewports[i].initialized ) {
1171
                retval = EADDRNOTAVAIL;
1171
                retval = EADDRNOTAVAIL;
1172
                break;
1172
                break;
1173
            }
1173
            }
1174
            cursor_hide(vport);
1174
            cursor_hide(vport);
1175
            vp = i;
1175
            vp = i;
1176
            vport = &viewports[vp];
1176
            vport = &viewports[vp];
1177
            cursor_print(vport);
1177
            cursor_print(vport);
1178
            retval = 0;
1178
            retval = 0;
1179
            break;
1179
            break;
1180
        case FB_VIEWPORT_CREATE:
1180
        case FB_VIEWPORT_CREATE:
1181
            retval = viewport_create(IPC_GET_ARG1(call) >> 16,
1181
            retval = viewport_create(IPC_GET_ARG1(call) >> 16,
1182
                         IPC_GET_ARG1(call) & 0xffff,
1182
                         IPC_GET_ARG1(call) & 0xffff,
1183
                         IPC_GET_ARG2(call) >> 16,
1183
                         IPC_GET_ARG2(call) >> 16,
1184
                         IPC_GET_ARG2(call) & 0xffff);
1184
                         IPC_GET_ARG2(call) & 0xffff);
1185
            break;
1185
            break;
1186
        case FB_VIEWPORT_DELETE:
1186
        case FB_VIEWPORT_DELETE:
1187
            i = IPC_GET_ARG1(call);
1187
            i = IPC_GET_ARG1(call);
1188
            if (i < 0 || i >= MAX_VIEWPORTS) {
1188
            if (i < 0 || i >= MAX_VIEWPORTS) {
1189
                retval = EINVAL;
1189
                retval = EINVAL;
1190
                break;
1190
                break;
1191
            }
1191
            }
1192
            if (! viewports[i].initialized ) {
1192
            if (! viewports[i].initialized ) {
1193
                retval = EADDRNOTAVAIL;
1193
                retval = EADDRNOTAVAIL;
1194
                break;
1194
                break;
1195
            }
1195
            }
1196
            viewports[i].initialized = 0;
1196
            viewports[i].initialized = 0;
1197
            if (viewports[i].dbdata) {
1197
            if (viewports[i].dbdata) {
1198
                free(viewports[i].dbdata);
1198
                free(viewports[i].dbdata);
1199
                viewports[i].dbdata = NULL;
1199
                viewports[i].dbdata = NULL;
1200
            }
1200
            }
1201
            retval = 0;
1201
            retval = 0;
1202
            break;
1202
            break;
1203
        case FB_SET_STYLE:
1203
        case FB_SET_STYLE:
1204
            vport->style.fg_color = IPC_GET_ARG1(call);
1204
            vport->style.fg_color = IPC_GET_ARG1(call);
1205
            vport->style.bg_color = IPC_GET_ARG2(call);
1205
            vport->style.bg_color = IPC_GET_ARG2(call);
1206
            retval = 0;
1206
            retval = 0;
1207
            break;
1207
            break;
1208
        case FB_GET_RESOLUTION:
1208
        case FB_GET_RESOLUTION:
1209
            ipc_answer_fast(callid, 0, screen.xres,screen.yres);
1209
            ipc_answer_fast(callid, 0, screen.xres,screen.yres);
1210
            continue;
1210
            continue;
1211
        case FB_POINTER_MOVE:
1211
        case FB_POINTER_MOVE:
1212
            pointer_enabled = 1;
1212
            pointer_enabled = 1;
1213
            mouse_move(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
1213
            mouse_move(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
1214
            retval = 0;
1214
            retval = 0;
1215
            break;
1215
            break;
1216
        default:
1216
        default:
1217
            retval = ENOENT;
1217
            retval = ENOENT;
1218
        }
1218
        }
1219
        ipc_answer_fast(callid,retval,0,0);
1219
        ipc_answer_fast(callid,retval,0,0);
1220
    }
1220
    }
1221
}
1221
}
1222
 
1222
 
1223
/** Initialization of framebuffer */
1223
/** Initialization of framebuffer */
1224
int fb_init(void)
1224
int fb_init(void)
1225
{
1225
{
1226
    void *fb_ph_addr;
1226
    void *fb_ph_addr;
1227
    unsigned int fb_width;
1227
    unsigned int fb_width;
1228
    unsigned int fb_height;
1228
    unsigned int fb_height;
1229
    unsigned int fb_bpp;
1229
    unsigned int fb_bpp;
1230
    unsigned int fb_bpp_align;
1230
    unsigned int fb_bpp_align;
1231
    unsigned int fb_scanline;
1231
    unsigned int fb_scanline;
1232
    unsigned int fb_invert_colors;
1232
    unsigned int fb_invert_colors;
1233
    void *fb_addr;
1233
    void *fb_addr;
1234
    size_t asz;
1234
    size_t asz;
1235
 
1235
 
1236
    async_set_client_connection(fb_client_connection);
1236
    async_set_client_connection(fb_client_connection);
1237
 
1237
 
1238
    fb_ph_addr = (void *) sysinfo_value("fb.address.physical");
1238
    fb_ph_addr = (void *) sysinfo_value("fb.address.physical");
1239
    fb_width = sysinfo_value("fb.width");
1239
    fb_width = sysinfo_value("fb.width");
1240
    fb_height = sysinfo_value("fb.height");
1240
    fb_height = sysinfo_value("fb.height");
1241
    fb_bpp = sysinfo_value("fb.bpp");
1241
    fb_bpp = sysinfo_value("fb.bpp");
1242
    fb_bpp_align = sysinfo_value("fb.bpp-align");
1242
    fb_bpp_align = sysinfo_value("fb.bpp-align");
1243
    fb_scanline = sysinfo_value("fb.scanline");
1243
    fb_scanline = sysinfo_value("fb.scanline");
1244
    fb_invert_colors = sysinfo_value("fb.invert-colors");
1244
    fb_invert_colors = sysinfo_value("fb.invert-colors");
1245
 
1245
 
1246
    asz = fb_scanline*fb_height;
1246
    asz = fb_scanline*fb_height;
1247
    fb_addr = as_get_mappable_page(asz);
1247
    fb_addr = as_get_mappable_page(asz);
1248
   
1248
   
1249
    map_physmem(fb_ph_addr, fb_addr, ALIGN_UP(asz,PAGE_SIZE) >>PAGE_WIDTH,
1249
    map_physmem(fb_ph_addr, fb_addr, ALIGN_UP(asz, PAGE_SIZE) >> PAGE_WIDTH,
1250
            AS_AREA_READ | AS_AREA_WRITE);
1250
            AS_AREA_READ | AS_AREA_WRITE);
1251
 
1251
 
1252
    screen_init(fb_addr, fb_width, fb_height, fb_bpp, fb_scanline, fb_bpp_align, fb_invert_colors);
1252
    screen_init(fb_addr, fb_width, fb_height, fb_bpp, fb_scanline, fb_bpp_align, fb_invert_colors);
1253
    return 0;
1253
    return 0;
1254
}
1254
}
1255
 
1255
 
1256
/**
1256
/**
1257
 * @}
1257
 * @}
1258
 */
1258
 */
1259
 
1259