Subversion Repositories HelenOS-historic

Rev

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

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