Subversion Repositories HelenOS

Rev

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

Rev 3726 Rev 3739
1
/*
1
/*
2
 * Copyright (c) 2008 Martin Decky
2
 * Copyright (c) 2008 Martin Decky
3
 * Copyright (c) 2006 Jakub Vana
3
 * Copyright (c) 2006 Jakub Vana
4
 * Copyright (c) 2006 Ondrej Palkovsky
4
 * Copyright (c) 2006 Ondrej Palkovsky
5
 * All rights reserved.
5
 * All rights reserved.
6
 *
6
 *
7
 * Redistribution and use in source and binary forms, with or without
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
8
 * modification, are permitted provided that the following conditions
9
 * are met:
9
 * are met:
10
 *
10
 *
11
 * - Redistributions of source code must retain the above copyright
11
 * - Redistributions of source code must retain the above copyright
12
 *   notice, this list of conditions and the following disclaimer.
12
 *   notice, this list of conditions and the following disclaimer.
13
 * - Redistributions in binary form must reproduce the above copyright
13
 * - Redistributions in binary form must reproduce the above copyright
14
 *   notice, this list of conditions and the following disclaimer in the
14
 *   notice, this list of conditions and the following disclaimer in the
15
 *   documentation and/or other materials provided with the distribution.
15
 *   documentation and/or other materials provided with the distribution.
16
 * - The name of the author may not be used to endorse or promote products
16
 * - The name of the author may not be used to endorse or promote products
17
 *   derived from this software without specific prior written permission.
17
 *   derived from this software without specific prior written permission.
18
 *
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 */
29
 */
30
 
30
 
31
/**
31
/**
32
 * @defgroup fb Graphical framebuffer
32
 * @defgroup fb Graphical framebuffer
33
 * @brief   HelenOS graphical framebuffer.
33
 * @brief   HelenOS graphical framebuffer.
34
 * @ingroup fbs
34
 * @ingroup fbs
35
 * @{
35
 * @{
36
 */
36
 */
37
 
37
 
38
/** @file
38
/** @file
39
 */
39
 */
40
 
40
 
41
#include <stdlib.h>
41
#include <stdlib.h>
42
#include <unistd.h>
42
#include <unistd.h>
43
#include <string.h>
43
#include <string.h>
44
#include <ddi.h>
44
#include <ddi.h>
45
#include <sysinfo.h>
45
#include <sysinfo.h>
46
#include <align.h>
46
#include <align.h>
47
#include <as.h>
47
#include <as.h>
48
#include <ipc/fb.h>
48
#include <ipc/fb.h>
49
#include <ipc/ipc.h>
49
#include <ipc/ipc.h>
50
#include <ipc/ns.h>
50
#include <ipc/ns.h>
51
#include <ipc/services.h>
51
#include <ipc/services.h>
52
#include <kernel/errno.h>
52
#include <kernel/errno.h>
53
#include <kernel/genarch/fb/visuals.h>
53
#include <kernel/genarch/fb/visuals.h>
54
#include <async.h>
54
#include <async.h>
55
#include <bool.h>
55
#include <bool.h>
56
 
56
 
57
#include "font-8x16.h"
57
#include "font-8x16.h"
58
#include "fb.h"
58
#include "fb.h"
59
#include "main.h"
59
#include "main.h"
60
#include "../console/screenbuffer.h"
60
#include "../console/screenbuffer.h"
61
#include "ppm.h"
61
#include "ppm.h"
62
 
62
 
63
#include "pointer.xbm"
63
#include "pointer.xbm"
64
#include "pointer_mask.xbm"
64
#include "pointer_mask.xbm"
65
 
65
 
66
#define DEFAULT_BGCOLOR  0xf0f0f0
66
#define DEFAULT_BGCOLOR  0xf0f0f0
67
#define DEFAULT_FGCOLOR  0x000000
67
#define DEFAULT_FGCOLOR  0x000000
68
 
68
 
69
#define MAX_ANIM_LEN     8
69
#define MAX_ANIM_LEN     8
70
#define MAX_ANIMATIONS   4
70
#define MAX_ANIMATIONS   4
71
#define MAX_PIXMAPS      256  /**< Maximum number of saved pixmaps */
71
#define MAX_PIXMAPS      256  /**< Maximum number of saved pixmaps */
72
#define MAX_VIEWPORTS    128  /**< Viewport is a rectangular area on the screen */
72
#define MAX_VIEWPORTS    128  /**< Viewport is a rectangular area on the screen */
73
 
73
 
74
typedef void (*rgb_conv_t)(void *, uint32_t);
74
typedef void (*rgb_conv_t)(void *, uint32_t);
75
 
75
 
76
struct {
76
struct {
77
    uint8_t *fb_addr;
77
    uint8_t *fb_addr;
78
   
78
   
79
    unsigned int xres;
79
    unsigned int xres;
80
    unsigned int yres;
80
    unsigned int yres;
81
   
81
   
82
    unsigned int scanline;
82
    unsigned int scanline;
83
    unsigned int glyphscanline;
83
    unsigned int glyphscanline;
84
   
84
   
85
    unsigned int pixelbytes;
85
    unsigned int pixelbytes;
86
    unsigned int glyphbytes;
86
    unsigned int glyphbytes;
87
   
87
   
88
    rgb_conv_t rgb_conv;
88
    rgb_conv_t rgb_conv;
89
} screen;
89
} screen;
90
 
90
 
91
typedef struct {
91
typedef struct {
92
    bool initialized;
92
    bool initialized;
93
    unsigned int x;
93
    unsigned int x;
94
    unsigned int y;
94
    unsigned int y;
95
    unsigned int width;
95
    unsigned int width;
96
    unsigned int height;
96
    unsigned int height;
97
   
97
   
98
    /* Text support in window */
98
    /* Text support in window */
99
    unsigned int cols;
99
    unsigned int cols;
100
    unsigned int rows;
100
    unsigned int rows;
101
   
101
   
102
    /* Style and glyphs for text printing */
102
    /* Style and glyphs for text printing */
103
    style_t style;
103
    style_t style;
104
    uint8_t *glyphs;
104
    uint8_t *glyphs;
105
    uint8_t *bgpixel;
105
    uint8_t *bgpixel;
106
   
106
   
107
    /* Auto-cursor position */
107
    /* Auto-cursor position */
108
    bool cursor_active;
108
    bool cursor_active;
109
    unsigned int cur_col;
109
    unsigned int cur_col;
110
    unsigned int cur_row;
110
    unsigned int cur_row;
111
    bool cursor_shown;
111
    bool cursor_shown;
112
   
112
   
113
    /* Back buffer */
113
    /* Back buffer */
114
    unsigned int bbsize;
114
    unsigned int bbsize;
115
    uint8_t *backbuf;
115
    uint8_t *backbuf;
116
} viewport_t;
116
} viewport_t;
117
 
117
 
118
typedef struct {
118
typedef struct {
119
    bool initialized;
119
    bool initialized;
120
    bool enabled;
120
    bool enabled;
121
    unsigned int vp;
121
    unsigned int vp;
122
   
122
   
123
    unsigned int pos;
123
    unsigned int pos;
124
    unsigned int animlen;
124
    unsigned int animlen;
125
    unsigned int pixmaps[MAX_ANIM_LEN];
125
    unsigned int pixmaps[MAX_ANIM_LEN];
126
} animation_t;
126
} animation_t;
127
 
127
 
128
static animation_t animations[MAX_ANIMATIONS];
128
static animation_t animations[MAX_ANIMATIONS];
129
static bool anims_enabled;
129
static bool anims_enabled;
130
 
130
 
131
typedef struct {
131
typedef struct {
132
    unsigned int width;
132
    unsigned int width;
133
    unsigned int height;
133
    unsigned int height;
134
    uint8_t *data;
134
    uint8_t *data;
135
} pixmap_t;
135
} pixmap_t;
136
 
136
 
137
static pixmap_t pixmaps[MAX_PIXMAPS];
137
static pixmap_t pixmaps[MAX_PIXMAPS];
138
static viewport_t viewports[128];
138
static viewport_t viewports[128];
139
 
139
 
140
static bool client_connected = false;  /**< Allow only 1 connection */
140
static bool client_connected = false;  /**< Allow only 1 connection */
141
 
141
 
-
 
142
static void draw_glyph(unsigned int x, unsigned int y, bool cursor,
-
 
143
    uint8_t *glyphs, uint8_t glyph);
142
static void draw_glyph(viewport_t *vport, bool cursor, unsigned int col,
144
static void draw_vp_glyph(viewport_t *vport, bool cursor, unsigned int col,
143
    unsigned int row);
145
    unsigned int row);
144
 
146
 
145
 
147
 
146
#define RED(x, bits)                 ((x >> (8 + 8 + 8 - bits)) & ((1 << bits) - 1))
148
#define RED(x, bits)                 ((x >> (8 + 8 + 8 - bits)) & ((1 << bits) - 1))
147
#define GREEN(x, bits)               ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
149
#define GREEN(x, bits)               ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
148
#define BLUE(x, bits)                ((x >> (8 - bits)) & ((1 << bits) - 1))
150
#define BLUE(x, bits)                ((x >> (8 - bits)) & ((1 << bits) - 1))
149
 
151
 
150
#define COL2X(col)                   ((col) * FONT_WIDTH)
152
#define COL2X(col)                   ((col) * FONT_WIDTH)
151
#define ROW2Y(row)                   ((row) * FONT_SCANLINES)
153
#define ROW2Y(row)                   ((row) * FONT_SCANLINES)
152
 
154
 
153
#define X2COL(x)                     ((x) / FONT_WIDTH)
155
#define X2COL(x)                     ((x) / FONT_WIDTH)
154
#define Y2ROW(y)                     ((y) / FONT_SCANLINES)
156
#define Y2ROW(y)                     ((y) / FONT_SCANLINES)
155
 
157
 
156
#define FB_POS(x, y)                 ((y) * screen.scanline + (x) * screen.pixelbytes)
158
#define FB_POS(x, y)                 ((y) * screen.scanline + (x) * screen.pixelbytes)
157
#define BB_POS(vport, col, row)      ((row) * vport->cols + (col))
159
#define BB_POS(vport, col, row)      ((row) * vport->cols + (col))
158
#define GLYPH_POS(glyph, y, cursor)  (((glyph) + (cursor) * FONT_GLYPHS) * screen.glyphbytes + (y) * screen.glyphscanline)
160
#define GLYPH_POS(glyph, y, cursor)  (((glyph) + (cursor) * FONT_GLYPHS) * screen.glyphbytes + (y) * screen.glyphscanline)
159
 
161
 
160
 
162
 
161
/** ARGB 8:8:8:8 conversion
163
/** ARGB 8:8:8:8 conversion
162
 *
164
 *
163
 */
165
 */
164
static void rgb_0888(void *dst, uint32_t rgb)
166
static void rgb_0888(void *dst, uint32_t rgb)
165
{
167
{
166
    *((uint32_t *) dst) = rgb & 0xffffff;
168
    *((uint32_t *) dst) = rgb & 0xffffff;
167
}
169
}
168
 
170
 
169
 
171
 
170
/** ABGR 8:8:8:8 conversion
172
/** ABGR 8:8:8:8 conversion
171
 *
173
 *
172
 */
174
 */
173
static void bgr_0888(void *dst, uint32_t rgb)
175
static void bgr_0888(void *dst, uint32_t rgb)
174
{
176
{
175
    *((uint32_t *) dst)
177
    *((uint32_t *) dst)
176
        = (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | RED(rgb, 8);
178
        = (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | RED(rgb, 8);
177
}
179
}
178
 
180
 
179
 
181
 
180
/** BGR 8:8:8 conversion
182
/** BGR 8:8:8 conversion
181
 *
183
 *
182
 */
184
 */
183
static void rgb_888(void *dst, uint32_t rgb)
185
static void rgb_888(void *dst, uint32_t rgb)
184
{
186
{
185
#if defined(FB_INVERT_ENDIAN)
187
#if defined(FB_INVERT_ENDIAN)
186
    ((uint8_t *) dst)[0] = RED(rgb, 8);
188
    ((uint8_t *) dst)[0] = RED(rgb, 8);
187
    ((uint8_t *) dst)[1] = GREEN(rgb, 8);
189
    ((uint8_t *) dst)[1] = GREEN(rgb, 8);
188
    ((uint8_t *) dst)[2] = BLUE(rgb, 8);
190
    ((uint8_t *) dst)[2] = BLUE(rgb, 8);
189
#else
191
#else
190
    ((uint8_t *) dst)[0] = BLUE(rgb, 8);
192
    ((uint8_t *) dst)[0] = BLUE(rgb, 8);
191
    ((uint8_t *) dst)[1] = GREEN(rgb, 8);
193
    ((uint8_t *) dst)[1] = GREEN(rgb, 8);
192
    ((uint8_t *) dst)[2] = RED(rgb, 8);
194
    ((uint8_t *) dst)[2] = RED(rgb, 8);
193
#endif
195
#endif
194
}
196
}
195
 
197
 
196
 
198
 
197
/** RGB 5:5:5 conversion
199
/** RGB 5:5:5 conversion
198
 *
200
 *
199
 */
201
 */
200
static void rgb_555(void *dst, uint32_t rgb)
202
static void rgb_555(void *dst, uint32_t rgb)
201
{
203
{
202
    *((uint16_t *) dst)
204
    *((uint16_t *) dst)
203
        = (RED(rgb, 5) << 10) | (GREEN(rgb, 5) << 5) | BLUE(rgb, 5);
205
        = (RED(rgb, 5) << 10) | (GREEN(rgb, 5) << 5) | BLUE(rgb, 5);
204
}
206
}
205
 
207
 
206
 
208
 
207
/** RGB 5:6:5 conversion
209
/** RGB 5:6:5 conversion
208
 *
210
 *
209
 */
211
 */
210
static void rgb_565(void *dst, uint32_t rgb)
212
static void rgb_565(void *dst, uint32_t rgb)
211
{
213
{
212
    *((uint16_t *) dst)
214
    *((uint16_t *) dst)
213
        = (RED(rgb, 5) << 11) | (GREEN(rgb, 6) << 5) | BLUE(rgb, 5);
215
        = (RED(rgb, 5) << 11) | (GREEN(rgb, 6) << 5) | BLUE(rgb, 5);
214
}
216
}
215
 
217
 
216
 
218
 
217
/** RGB 3:2:3
219
/** RGB 3:2:3
218
 *
220
 *
219
 */
221
 */
220
static void rgb_323(void *dst, uint32_t rgb)
222
static void rgb_323(void *dst, uint32_t rgb)
221
{
223
{
222
    *((uint8_t *) dst)
224
    *((uint8_t *) dst)
223
        = ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
225
        = ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
224
}
226
}
225
 
227
 
226
/** Draw a filled rectangle.
228
/** Draw a filled rectangle.
227
 *
229
 *
228
 * @note Need real implementation that does not access VRAM twice.
230
 * @note Need real implementation that does not access VRAM twice.
229
 */
231
 */
230
static void draw_filled_rect(unsigned int x0, unsigned int y0, unsigned int x1,
232
static void draw_filled_rect(unsigned int x0, unsigned int y0, unsigned int x1,
231
    unsigned int y1, uint32_t color)
233
    unsigned int y1, uint32_t color)
232
{
234
{
233
    unsigned int x, y;
235
    unsigned int x, y;
234
    unsigned int copy_bytes;
236
    unsigned int copy_bytes;
235
 
237
 
236
    uint8_t *sp, *dp;
238
    uint8_t *sp, *dp;
237
    uint8_t cbuf[4];
239
    uint8_t cbuf[4];
238
 
240
 
239
    if (y0 >= y1 || x0 >= x1) return;
241
    if (y0 >= y1 || x0 >= x1) return;
240
    screen.rgb_conv(cbuf, color);
242
    screen.rgb_conv(cbuf, color);
241
 
243
 
242
    sp = &screen.fb_addr[FB_POS(x0, y0)];
244
    sp = &screen.fb_addr[FB_POS(x0, y0)];
243
    dp = sp;
245
    dp = sp;
244
 
246
 
245
    /* Draw the first line. */
247
    /* Draw the first line. */
246
    for (x = x0; x < x1; x++) {
248
    for (x = x0; x < x1; x++) {
247
        memcpy(dp, cbuf, screen.pixelbytes);
249
        memcpy(dp, cbuf, screen.pixelbytes);
248
        dp += screen.pixelbytes;
250
        dp += screen.pixelbytes;
249
    }
251
    }
250
 
252
 
251
    dp = sp + screen.scanline;
253
    dp = sp + screen.scanline;
252
    copy_bytes = (x1 - x0) * screen.pixelbytes;
254
    copy_bytes = (x1 - x0) * screen.pixelbytes;
253
 
255
 
254
    /* Draw the remaining lines by copying. */
256
    /* Draw the remaining lines by copying. */
255
    for (y = y0 + 1; y < y1; y++) {
257
    for (y = y0 + 1; y < y1; y++) {
256
        memcpy(dp, sp, copy_bytes);
258
        memcpy(dp, sp, copy_bytes);
257
        dp += screen.scanline;
259
        dp += screen.scanline;
258
    }
260
    }
259
}
261
}
260
 
262
 
261
/** Redraw viewport.
263
/** Redraw viewport.
262
 *
264
 *
263
 * @param vport Viewport to redraw
265
 * @param vport Viewport to redraw
264
 *
266
 *
265
 */
267
 */
266
static void vport_redraw(viewport_t *vport)
268
static void vport_redraw(viewport_t *vport)
267
{
269
{
268
    unsigned int row, col;
270
    unsigned int row, col;
269
 
271
 
270
    for (row = 0; row < vport->rows; row++) {
272
    for (row = 0; row < vport->rows; row++) {
271
        for (col = 0; col < vport->cols; col++) {
273
        for (col = 0; col < vport->cols; col++) {
272
            draw_glyph(vport, false, col, row);
274
            draw_vp_glyph(vport, false, col, row);
273
        }
275
        }
274
    }
276
    }
275
 
277
 
276
    if (COL2X(vport->cols) < vport->width) {
278
    if (COL2X(vport->cols) < vport->width) {
277
        draw_filled_rect(
279
        draw_filled_rect(
278
            vport->x + COL2X(vport->cols), vport->y,
280
            vport->x + COL2X(vport->cols), vport->y,
279
            vport->x + vport->width, vport->y + vport->height,
281
            vport->x + vport->width, vport->y + vport->height,
280
            vport->style.bg_color);
282
            vport->style.bg_color);
281
    }
283
    }
282
 
284
 
283
    if (ROW2Y(vport->rows) < vport->height) {
285
    if (ROW2Y(vport->rows) < vport->height) {
284
        draw_filled_rect(
286
        draw_filled_rect(
285
            vport->x, vport->y + ROW2Y(vport->rows),
287
            vport->x, vport->y + ROW2Y(vport->rows),
286
            vport->x + vport->width, vport->y + vport->height,
288
            vport->x + vport->width, vport->y + vport->height,
287
            vport->style.bg_color);
289
            vport->style.bg_color);
288
    }
290
    }
289
}
291
}
290
 
292
 
291
 
293
 
292
/** Clear viewport.
294
/** Clear viewport.
293
 *
295
 *
294
 * @param vport Viewport to clear
296
 * @param vport Viewport to clear
295
 *
297
 *
296
 */
298
 */
297
static void vport_clear(viewport_t *vport)
299
static void vport_clear(viewport_t *vport)
298
{
300
{
299
    memset(vport->backbuf, 0, vport->bbsize);
301
    memset(vport->backbuf, 0, vport->bbsize);
300
    vport_redraw(vport);
302
    vport_redraw(vport);
301
}
303
}
302
 
304
 
303
/** Scroll viewport by the specified number of lines.
305
/** Scroll viewport by the specified number of lines.
304
 *
306
 *
305
 * @param vport Viewport to scroll
307
 * @param vport Viewport to scroll
306
 * @param lines Number of lines to scroll
308
 * @param lines Number of lines to scroll
307
 *
309
 *
308
 */
310
 */
309
static void vport_scroll(viewport_t *vport, int lines)
311
static void vport_scroll(viewport_t *vport, int lines)
310
{
312
{
311
    unsigned int row, col;
313
    unsigned int row, col;
-
 
314
    unsigned int x, y;
-
 
315
    uint8_t glyph;
-
 
316
 
-
 
317
    /*
-
 
318
     * Redraw.
-
 
319
     */
-
 
320
 
-
 
321
    y = vport->y;
-
 
322
    for (row = 0; row < vport->rows; row++) {
-
 
323
        x = vport->x;
-
 
324
        for (col = 0; col < vport->cols; col++) {
-
 
325
            if ((row + lines >= 0) && (row + lines < vport->rows)) {
-
 
326
                glyph = vport->backbuf[BB_POS(vport, col, row + lines)];
-
 
327
 
-
 
328
                if (vport->backbuf[BB_POS(vport, col, row)] == glyph) {
-
 
329
                    x += FONT_WIDTH;
-
 
330
                    continue;
-
 
331
                }
-
 
332
            } else {
-
 
333
                glyph = 0;
-
 
334
            }
-
 
335
 
-
 
336
            draw_glyph(x, y, false, vport->glyphs, glyph);
-
 
337
            x += FONT_WIDTH;
-
 
338
        }
-
 
339
        y += FONT_SCANLINES;
-
 
340
    }
312
 
341
 
313
    /*
342
    /*
314
     * Scroll backbuffer.
343
     * Scroll backbuffer.
315
     */
344
     */
316
 
345
 
317
    if (lines > 0) {
346
    if (lines > 0) {
318
        memcpy(vport->backbuf, vport->backbuf + vport->cols * lines,
347
        memmove(vport->backbuf, vport->backbuf + vport->cols * lines,
319
            vport->cols * (vport->rows - lines));
348
            vport->cols * (vport->rows - lines));
320
        memset(&vport->backbuf[BB_POS(vport, 0, vport->rows - lines)],
349
        memset(&vport->backbuf[BB_POS(vport, 0, vport->rows - lines)],
321
            0, vport->cols * lines);
350
            0, vport->cols * lines);
322
    } else {
351
    } else {
323
        memcpy(vport->backbuf - vport->cols * lines, vport->backbuf,
352
        memmove(vport->backbuf - vport->cols * lines, vport->backbuf,
324
            vport->cols * (vport->rows + lines));
353
            vport->cols * (vport->rows + lines));
325
        memset(vport->backbuf, 0, - vport->cols * lines);
354
        memset(vport->backbuf, 0, - vport->cols * lines);
326
    }
355
    }
327
 
-
 
328
    /*
-
 
329
     * Redraw.
-
 
330
     */
-
 
331
 
-
 
332
    for (row = 0; row < vport->rows; row++) {
-
 
333
        for (col = 0; col < vport->cols; col++) {
-
 
334
            draw_glyph(vport, false, col, row);
-
 
335
        }
-
 
336
    }
-
 
337
}
356
}
338
 
357
 
339
/** Render glyphs
358
/** Render glyphs
340
 *
359
 *
341
 * Convert glyphs from device independent font
360
 * Convert glyphs from device independent font
342
 * description to current visual representation.
361
 * description to current visual representation.
343
 *
362
 *
344
 * @param vport Viewport
363
 * @param vport Viewport
345
 *
364
 *
346
 */
365
 */
347
static void render_glyphs(viewport_t* vport)
366
static void render_glyphs(viewport_t* vport)
348
{
367
{
349
    unsigned int glyph;
368
    unsigned int glyph;
350
   
369
   
351
    for (glyph = 0; glyph < FONT_GLYPHS; glyph++) {
370
    for (glyph = 0; glyph < FONT_GLYPHS; glyph++) {
352
        unsigned int y;
371
        unsigned int y;
353
       
372
       
354
        for (y = 0; y < FONT_SCANLINES; y++) {
373
        for (y = 0; y < FONT_SCANLINES; y++) {
355
            unsigned int x;
374
            unsigned int x;
356
           
375
           
357
            for (x = 0; x < FONT_WIDTH; x++) {
376
            for (x = 0; x < FONT_WIDTH; x++) {
358
                screen.rgb_conv(&vport->glyphs[GLYPH_POS(glyph, y, false) + x * screen.pixelbytes],
377
                screen.rgb_conv(&vport->glyphs[GLYPH_POS(glyph, y, false) + x * screen.pixelbytes],
359
                    (fb_font[glyph * FONT_SCANLINES + y] & (1 << (7 - x)))
378
                    (fb_font[glyph * FONT_SCANLINES + y] & (1 << (7 - x)))
360
                    ? vport->style.fg_color : vport->style.bg_color);
379
                    ? vport->style.fg_color : vport->style.bg_color);
361
               
380
               
362
                uint32_t curcolor;
381
                uint32_t curcolor;
363
               
382
               
364
                if (y < FONT_SCANLINES - 2)
383
                if (y < FONT_SCANLINES - 2)
365
                    curcolor =
384
                    curcolor =
366
                        (fb_font[glyph * FONT_SCANLINES + y] & (1 << (7 - x)))
385
                        (fb_font[glyph * FONT_SCANLINES + y] & (1 << (7 - x)))
367
                        ? vport->style.fg_color : vport->style.bg_color;
386
                        ? vport->style.fg_color : vport->style.bg_color;
368
                else
387
                else
369
                    curcolor = vport->style.fg_color;
388
                    curcolor = vport->style.fg_color;
370
               
389
               
371
                screen.rgb_conv(&vport->glyphs[GLYPH_POS(glyph, y, true) + x * screen.pixelbytes], curcolor);
390
                screen.rgb_conv(&vport->glyphs[GLYPH_POS(glyph, y, true) + x * screen.pixelbytes], curcolor);
372
            }
391
            }
373
        }
392
        }
374
    }
393
    }
375
   
394
   
376
    screen.rgb_conv(vport->bgpixel, vport->style.bg_color);
395
    screen.rgb_conv(vport->bgpixel, vport->style.bg_color);
377
}
396
}
378
 
397
 
379
 
398
 
380
/** Create new viewport
399
/** Create new viewport
381
 *
400
 *
382
 * @param x      Origin of the viewport (x).
401
 * @param x      Origin of the viewport (x).
383
 * @param y      Origin of the viewport (y).
402
 * @param y      Origin of the viewport (y).
384
 * @param width  Width of the viewport.
403
 * @param width  Width of the viewport.
385
 * @param height Height of the viewport.
404
 * @param height Height of the viewport.
386
 *
405
 *
387
 * @return New viewport number.
406
 * @return New viewport number.
388
 *
407
 *
389
 */
408
 */
390
static int vport_create(unsigned int x, unsigned int y,
409
static int vport_create(unsigned int x, unsigned int y,
391
    unsigned int width, unsigned int height)
410
    unsigned int width, unsigned int height)
392
{
411
{
393
    unsigned int i;
412
    unsigned int i;
394
   
413
   
395
    for (i = 0; i < MAX_VIEWPORTS; i++) {
414
    for (i = 0; i < MAX_VIEWPORTS; i++) {
396
        if (!viewports[i].initialized)
415
        if (!viewports[i].initialized)
397
            break;
416
            break;
398
    }
417
    }
399
    if (i == MAX_VIEWPORTS)
418
    if (i == MAX_VIEWPORTS)
400
        return ELIMIT;
419
        return ELIMIT;
401
   
420
   
402
    unsigned int cols = width / FONT_WIDTH;
421
    unsigned int cols = width / FONT_WIDTH;
403
    unsigned int rows = height / FONT_SCANLINES;
422
    unsigned int rows = height / FONT_SCANLINES;
404
    unsigned int bbsize = cols * rows;
423
    unsigned int bbsize = cols * rows;
405
    unsigned int glyphsize = 2 * FONT_GLYPHS * screen.glyphbytes;
424
    unsigned int glyphsize = 2 * FONT_GLYPHS * screen.glyphbytes;
406
   
425
   
407
    uint8_t *backbuf = (uint8_t *) malloc(bbsize);
426
    uint8_t *backbuf = (uint8_t *) malloc(bbsize);
408
    if (!backbuf)
427
    if (!backbuf)
409
        return ENOMEM;
428
        return ENOMEM;
410
   
429
   
411
    uint8_t *glyphs = (uint8_t *) malloc(glyphsize);
430
    uint8_t *glyphs = (uint8_t *) malloc(glyphsize);
412
    if (!glyphs) {
431
    if (!glyphs) {
413
        free(backbuf);
432
        free(backbuf);
414
        return ENOMEM;
433
        return ENOMEM;
415
    }
434
    }
416
   
435
   
417
    uint8_t *bgpixel = (uint8_t *) malloc(screen.pixelbytes);
436
    uint8_t *bgpixel = (uint8_t *) malloc(screen.pixelbytes);
418
    if (!bgpixel) {
437
    if (!bgpixel) {
419
        free(glyphs);
438
        free(glyphs);
420
        free(backbuf);
439
        free(backbuf);
421
        return ENOMEM;
440
        return ENOMEM;
422
    }
441
    }
423
   
442
   
424
    memset(backbuf, 0, bbsize);
443
    memset(backbuf, 0, bbsize);
425
    memset(glyphs, 0, glyphsize);
444
    memset(glyphs, 0, glyphsize);
426
    memset(bgpixel, 0, screen.pixelbytes);
445
    memset(bgpixel, 0, screen.pixelbytes);
427
   
446
   
428
    viewports[i].x = x;
447
    viewports[i].x = x;
429
    viewports[i].y = y;
448
    viewports[i].y = y;
430
    viewports[i].width = width;
449
    viewports[i].width = width;
431
    viewports[i].height = height;
450
    viewports[i].height = height;
432
   
451
   
433
    viewports[i].cols = cols;
452
    viewports[i].cols = cols;
434
    viewports[i].rows = rows;
453
    viewports[i].rows = rows;
435
   
454
   
436
    viewports[i].style.bg_color = DEFAULT_BGCOLOR;
455
    viewports[i].style.bg_color = DEFAULT_BGCOLOR;
437
    viewports[i].style.fg_color = DEFAULT_FGCOLOR;
456
    viewports[i].style.fg_color = DEFAULT_FGCOLOR;
438
   
457
   
439
    viewports[i].glyphs = glyphs;
458
    viewports[i].glyphs = glyphs;
440
    viewports[i].bgpixel = bgpixel;
459
    viewports[i].bgpixel = bgpixel;
441
   
460
   
442
    viewports[i].cur_col = 0;
461
    viewports[i].cur_col = 0;
443
    viewports[i].cur_row = 0;
462
    viewports[i].cur_row = 0;
444
    viewports[i].cursor_active = false;
463
    viewports[i].cursor_active = false;
445
    viewports[i].cursor_shown = false;
464
    viewports[i].cursor_shown = false;
446
   
465
   
447
    viewports[i].bbsize = bbsize;
466
    viewports[i].bbsize = bbsize;
448
    viewports[i].backbuf = backbuf;
467
    viewports[i].backbuf = backbuf;
449
   
468
   
450
    viewports[i].initialized = true;
469
    viewports[i].initialized = true;
451
   
470
   
452
    render_glyphs(&viewports[i]);
471
    render_glyphs(&viewports[i]);
453
   
472
   
454
    return i;
473
    return i;
455
}
474
}
456
 
475
 
457
 
476
 
458
/** Initialize framebuffer as a chardev output device
477
/** Initialize framebuffer as a chardev output device
459
 *
478
 *
460
 * @param addr   Address of the framebuffer
479
 * @param addr   Address of the framebuffer
461
 * @param xres   Screen width in pixels
480
 * @param xres   Screen width in pixels
462
 * @param yres   Screen height in pixels
481
 * @param yres   Screen height in pixels
463
 * @param visual Bits per pixel (8, 16, 24, 32)
482
 * @param visual Bits per pixel (8, 16, 24, 32)
464
 * @param scan   Bytes per one scanline
483
 * @param scan   Bytes per one scanline
465
 *
484
 *
466
 */
485
 */
467
static bool screen_init(void *addr, unsigned int xres, unsigned int yres,
486
static bool screen_init(void *addr, unsigned int xres, unsigned int yres,
468
    unsigned int scan, unsigned int visual)
487
    unsigned int scan, unsigned int visual)
469
{
488
{
470
    switch (visual) {
489
    switch (visual) {
471
    case VISUAL_INDIRECT_8:
490
    case VISUAL_INDIRECT_8:
472
        screen.rgb_conv = rgb_323;
491
        screen.rgb_conv = rgb_323;
473
        screen.pixelbytes = 1;
492
        screen.pixelbytes = 1;
474
        break;
493
        break;
475
    case VISUAL_RGB_5_5_5:
494
    case VISUAL_RGB_5_5_5:
476
        screen.rgb_conv = rgb_555;
495
        screen.rgb_conv = rgb_555;
477
        screen.pixelbytes = 2;
496
        screen.pixelbytes = 2;
478
        break;
497
        break;
479
    case VISUAL_RGB_5_6_5:
498
    case VISUAL_RGB_5_6_5:
480
        screen.rgb_conv = rgb_565;
499
        screen.rgb_conv = rgb_565;
481
        screen.pixelbytes = 2;
500
        screen.pixelbytes = 2;
482
        break;
501
        break;
483
    case VISUAL_RGB_8_8_8:
502
    case VISUAL_RGB_8_8_8:
484
        screen.rgb_conv = rgb_888;
503
        screen.rgb_conv = rgb_888;
485
        screen.pixelbytes = 3;
504
        screen.pixelbytes = 3;
486
        break;
505
        break;
487
    case VISUAL_RGB_8_8_8_0:
506
    case VISUAL_RGB_8_8_8_0:
488
        screen.rgb_conv = rgb_888;
507
        screen.rgb_conv = rgb_888;
489
        screen.pixelbytes = 4;
508
        screen.pixelbytes = 4;
490
        break;
509
        break;
491
    case VISUAL_RGB_0_8_8_8:
510
    case VISUAL_RGB_0_8_8_8:
492
        screen.rgb_conv = rgb_0888;
511
        screen.rgb_conv = rgb_0888;
493
        screen.pixelbytes = 4;
512
        screen.pixelbytes = 4;
494
        break;
513
        break;
495
    case VISUAL_BGR_0_8_8_8:
514
    case VISUAL_BGR_0_8_8_8:
496
        screen.rgb_conv = bgr_0888;
515
        screen.rgb_conv = bgr_0888;
497
        screen.pixelbytes = 4;
516
        screen.pixelbytes = 4;
498
        break;
517
        break;
499
    default:
518
    default:
500
        return false;
519
        return false;
501
    }
520
    }
502
 
521
 
503
    screen.fb_addr = (unsigned char *) addr;
522
    screen.fb_addr = (unsigned char *) addr;
504
    screen.xres = xres;
523
    screen.xres = xres;
505
    screen.yres = yres;
524
    screen.yres = yres;
506
    screen.scanline = scan;
525
    screen.scanline = scan;
507
   
526
   
508
    screen.glyphscanline = FONT_WIDTH * screen.pixelbytes;
527
    screen.glyphscanline = FONT_WIDTH * screen.pixelbytes;
509
    screen.glyphbytes = screen.glyphscanline * FONT_SCANLINES;
528
    screen.glyphbytes = screen.glyphscanline * FONT_SCANLINES;
510
   
529
   
511
    /* Create first viewport */
530
    /* Create first viewport */
512
    vport_create(0, 0, xres, yres);
531
    vport_create(0, 0, xres, yres);
513
   
532
   
514
    return true;
533
    return true;
515
}
534
}
516
 
535
 
517
 
536
 
-
 
537
/** Draw a glyph.
-
 
538
 *
-
 
539
 * @param x  x coordinate of top-left corner on screen.
-
 
540
 * @param y  y coordinate of top-left corner on screen.
-
 
541
 * @param cursor Draw glyph with cursor
-
 
542
 * @param glyphs Pointer to font bitmap.
-
 
543
 * @param glyph  Code of the glyph to draw.
-
 
544
 *
-
 
545
 */
-
 
546
static void draw_glyph(unsigned int x, unsigned int y, bool cursor,
-
 
547
    uint8_t *glyphs, uint8_t glyph)
-
 
548
{
-
 
549
    unsigned int yd;
-
 
550
   
-
 
551
    for (yd = 0; yd < FONT_SCANLINES; yd++)
-
 
552
        memcpy(&screen.fb_addr[FB_POS(x, y + yd)],
-
 
553
            &glyphs[GLYPH_POS(glyph, yd, cursor)], screen.glyphscanline);
-
 
554
}
-
 
555
 
518
/** Draw glyph at given position relative to viewport
556
/** Draw glyph at specified position in viewport.
519
 *
557
 *
520
 * @param vport  Viewport identification
558
 * @param vport  Viewport identification
521
 * @param cursor Draw glyph with cursor
559
 * @param cursor Draw glyph with cursor
522
 * @param col    Screen position relative to viewport
560
 * @param col    Screen position relative to viewport
523
 * @param row    Screen position relative to viewport
561
 * @param row    Screen position relative to viewport
524
 *
562
 *
525
 */
563
 */
526
static void draw_glyph(viewport_t *vport, bool cursor, unsigned int col, unsigned int row)
564
static void draw_vp_glyph(viewport_t *vport, bool cursor, unsigned int col,
-
 
565
    unsigned int row)
527
{
566
{
528
    unsigned int x = vport->x + COL2X(col);
567
    unsigned int x = vport->x + COL2X(col);
529
    unsigned int y = vport->y + ROW2Y(row);
568
    unsigned int y = vport->y + ROW2Y(row);
530
    unsigned int yd;
569
    uint8_t glyph;
531
   
-
 
532
    uint8_t glyph = vport->backbuf[BB_POS(vport, col, row)];
-
 
533
   
570
   
534
    for (yd = 0; yd < FONT_SCANLINES; yd++)
-
 
535
        memcpy(&screen.fb_addr[FB_POS(x, y + yd)],
571
    glyph = vport->backbuf[BB_POS(vport, col, row)];
536
            &vport->glyphs[GLYPH_POS(glyph, yd, cursor)], screen.glyphscanline);
572
    draw_glyph(x, y, cursor, vport->glyphs, glyph);
537
}
573
}
538
 
574
 
539
 
575
 
540
/** Hide cursor if it is shown
576
/** Hide cursor if it is shown
541
 *
577
 *
542
 */
578
 */
543
static void cursor_hide(viewport_t *vport)
579
static void cursor_hide(viewport_t *vport)
544
{
580
{
545
    if ((vport->cursor_active) && (vport->cursor_shown)) {
581
    if ((vport->cursor_active) && (vport->cursor_shown)) {
546
        draw_glyph(vport, false, vport->cur_col, vport->cur_row);
582
        draw_vp_glyph(vport, false, vport->cur_col, vport->cur_row);
547
        vport->cursor_shown = false;
583
        vport->cursor_shown = false;
548
    }
584
    }
549
}
585
}
550
 
586
 
551
 
587
 
552
/** Show cursor if cursor showing is enabled
588
/** Show cursor if cursor showing is enabled
553
 *
589
 *
554
 */
590
 */
555
static void cursor_show(viewport_t *vport)
591
static void cursor_show(viewport_t *vport)
556
{
592
{
557
    /* Do not check for cursor_shown */
593
    /* Do not check for cursor_shown */
558
    if (vport->cursor_active) {
594
    if (vport->cursor_active) {
559
        draw_glyph(vport, true, vport->cur_col, vport->cur_row);
595
        draw_vp_glyph(vport, true, vport->cur_col, vport->cur_row);
560
        vport->cursor_shown = true;
596
        vport->cursor_shown = true;
561
    }
597
    }
562
}
598
}
563
 
599
 
564
 
600
 
565
/** Invert cursor, if it is enabled
601
/** Invert cursor, if it is enabled
566
 *
602
 *
567
 */
603
 */
568
static void cursor_blink(viewport_t *vport)
604
static void cursor_blink(viewport_t *vport)
569
{
605
{
570
    if (vport->cursor_shown)
606
    if (vport->cursor_shown)
571
        cursor_hide(vport);
607
        cursor_hide(vport);
572
    else
608
    else
573
        cursor_show(vport);
609
        cursor_show(vport);
574
}
610
}
575
 
611
 
576
 
612
 
577
/** Draw character at given position relative to viewport
613
/** Draw character at given position relative to viewport
578
 *
614
 *
579
 * @param vport  Viewport identification
615
 * @param vport  Viewport identification
580
 * @param c      Character to draw
616
 * @param c      Character to draw
581
 * @param col    Screen position relative to viewport
617
 * @param col    Screen position relative to viewport
582
 * @param row    Screen position relative to viewport
618
 * @param row    Screen position relative to viewport
583
 *
619
 *
584
 */
620
 */
585
static void draw_char(viewport_t *vport, uint8_t c, unsigned int col, unsigned int row)
621
static void draw_char(viewport_t *vport, uint8_t c, unsigned int col, unsigned int row)
586
{
622
{
587
    /* Do not hide cursor if we are going to overwrite it */
623
    /* Do not hide cursor if we are going to overwrite it */
588
    if ((vport->cursor_active) && (vport->cursor_shown) &&
624
    if ((vport->cursor_active) && (vport->cursor_shown) &&
589
        ((vport->cur_col != col) || (vport->cur_row != row)))
625
        ((vport->cur_col != col) || (vport->cur_row != row)))
590
        cursor_hide(vport);
626
        cursor_hide(vport);
591
   
627
   
592
    vport->backbuf[BB_POS(vport, col, row)] = c;
628
    vport->backbuf[BB_POS(vport, col, row)] = c;
593
    draw_glyph(vport, false, col, row);
629
    draw_vp_glyph(vport, false, col, row);
594
   
630
   
595
    vport->cur_col = col;
631
    vport->cur_col = col;
596
    vport->cur_row = row;
632
    vport->cur_row = row;
597
   
633
   
598
    vport->cur_col++;
634
    vport->cur_col++;
599
    if (vport->cur_col >= vport->cols) {
635
    if (vport->cur_col >= vport->cols) {
600
        vport->cur_col = 0;
636
        vport->cur_col = 0;
601
        vport->cur_row++;
637
        vport->cur_row++;
602
        if (vport->cur_row >= vport->rows)
638
        if (vport->cur_row >= vport->rows)
603
            vport->cur_row--;
639
            vport->cur_row--;
604
    }
640
    }
605
   
641
   
606
    cursor_show(vport);
642
    cursor_show(vport);
607
}
643
}
608
 
644
 
609
 
645
 
610
/** Draw text data to viewport
646
/** Draw text data to viewport
611
 *
647
 *
612
 * @param vport Viewport id
648
 * @param vport Viewport id
613
 * @param data  Text data fitting exactly into viewport
649
 * @param data  Text data fitting exactly into viewport
614
 *
650
 *
615
 */
651
 */
616
static void draw_text_data(viewport_t *vport, keyfield_t *data)
652
static void draw_text_data(viewport_t *vport, keyfield_t *data)
617
{
653
{
618
    unsigned int i;
654
    unsigned int i;
619
   
655
   
620
    for (i = 0; i < vport->cols * vport->rows; i++) {
656
    for (i = 0; i < vport->cols * vport->rows; i++) {
621
        unsigned int col = i % vport->cols;
657
        unsigned int col = i % vport->cols;
622
        unsigned int row = i / vport->cols;
658
        unsigned int row = i / vport->cols;
623
       
659
       
624
        uint8_t glyph = vport->backbuf[BB_POS(vport, col, row)];
660
        uint8_t glyph = vport->backbuf[BB_POS(vport, col, row)];
625
       
661
       
626
        // TODO: use data[i].style
662
        // TODO: use data[i].style
627
       
663
       
628
        if (glyph != data[i].character) {
664
        if (glyph != data[i].character) {
629
            vport->backbuf[BB_POS(vport, col, row)] = data[i].character;
665
            vport->backbuf[BB_POS(vport, col, row)] = data[i].character;
630
            draw_glyph(vport, false, col, row);
666
            draw_vp_glyph(vport, false, col, row);
631
        }
667
        }
632
    }
668
    }
633
    cursor_show(vport);
669
    cursor_show(vport);
634
}
670
}
635
 
671
 
636
 
672
 
637
static void putpixel_pixmap(void *data, unsigned int x, unsigned int y, uint32_t color)
673
static void putpixel_pixmap(void *data, unsigned int x, unsigned int y, uint32_t color)
638
{
674
{
639
    int pm = *((int *) data);
675
    int pm = *((int *) data);
640
    pixmap_t *pmap = &pixmaps[pm];
676
    pixmap_t *pmap = &pixmaps[pm];
641
    unsigned int pos = (y * pmap->width + x) * screen.pixelbytes;
677
    unsigned int pos = (y * pmap->width + x) * screen.pixelbytes;
642
   
678
   
643
    screen.rgb_conv(&pmap->data[pos], color);
679
    screen.rgb_conv(&pmap->data[pos], color);
644
}
680
}
645
 
681
 
646
 
682
 
647
static void putpixel(void *data, unsigned int x, unsigned int y, uint32_t color)
683
static void putpixel(void *data, unsigned int x, unsigned int y, uint32_t color)
648
{
684
{
649
    viewport_t *vport = (viewport_t *) data;
685
    viewport_t *vport = (viewport_t *) data;
650
    unsigned int dx = vport->x + x;
686
    unsigned int dx = vport->x + x;
651
    unsigned int dy = vport->y + y;
687
    unsigned int dy = vport->y + y;
652
   
688
   
653
    screen.rgb_conv(&screen.fb_addr[FB_POS(dx, dy)], color);
689
    screen.rgb_conv(&screen.fb_addr[FB_POS(dx, dy)], color);
654
}
690
}
655
 
691
 
656
 
692
 
657
/** Return first free pixmap
693
/** Return first free pixmap
658
 *
694
 *
659
 */
695
 */
660
static int find_free_pixmap(void)
696
static int find_free_pixmap(void)
661
{
697
{
662
    unsigned int i;
698
    unsigned int i;
663
   
699
   
664
    for (i = 0; i < MAX_PIXMAPS; i++)
700
    for (i = 0; i < MAX_PIXMAPS; i++)
665
        if (!pixmaps[i].data)
701
        if (!pixmaps[i].data)
666
            return i;
702
            return i;
667
   
703
   
668
    return -1;
704
    return -1;
669
}
705
}
670
 
706
 
671
 
707
 
672
/** Create a new pixmap and return appropriate ID
708
/** Create a new pixmap and return appropriate ID
673
 *
709
 *
674
 */
710
 */
675
static int shm2pixmap(unsigned char *shm, size_t size)
711
static int shm2pixmap(unsigned char *shm, size_t size)
676
{
712
{
677
    int pm;
713
    int pm;
678
    pixmap_t *pmap;
714
    pixmap_t *pmap;
679
   
715
   
680
    pm = find_free_pixmap();
716
    pm = find_free_pixmap();
681
    if (pm == -1)
717
    if (pm == -1)
682
        return ELIMIT;
718
        return ELIMIT;
683
   
719
   
684
    pmap = &pixmaps[pm];
720
    pmap = &pixmaps[pm];
685
   
721
   
686
    if (ppm_get_data(shm, size, &pmap->width, &pmap->height))
722
    if (ppm_get_data(shm, size, &pmap->width, &pmap->height))
687
        return EINVAL;
723
        return EINVAL;
688
   
724
   
689
    pmap->data = malloc(pmap->width * pmap->height * screen.pixelbytes);
725
    pmap->data = malloc(pmap->width * pmap->height * screen.pixelbytes);
690
    if (!pmap->data)
726
    if (!pmap->data)
691
        return ENOMEM;
727
        return ENOMEM;
692
   
728
   
693
    ppm_draw(shm, size, 0, 0, pmap->width, pmap->height, putpixel_pixmap, (void *) &pm);
729
    ppm_draw(shm, size, 0, 0, pmap->width, pmap->height, putpixel_pixmap, (void *) &pm);
694
   
730
   
695
    return pm;
731
    return pm;
696
}
732
}
697
 
733
 
698
 
734
 
699
/** Handle shared memory communication calls
735
/** Handle shared memory communication calls
700
 *
736
 *
701
 * Protocol for drawing pixmaps:
737
 * Protocol for drawing pixmaps:
702
 * - FB_PREPARE_SHM(client shm identification)
738
 * - FB_PREPARE_SHM(client shm identification)
703
 * - IPC_M_AS_AREA_SEND
739
 * - IPC_M_AS_AREA_SEND
704
 * - FB_DRAW_PPM(startx, starty)
740
 * - FB_DRAW_PPM(startx, starty)
705
 * - FB_DROP_SHM
741
 * - FB_DROP_SHM
706
 *
742
 *
707
 * Protocol for text drawing
743
 * Protocol for text drawing
708
 * - IPC_M_AS_AREA_SEND
744
 * - IPC_M_AS_AREA_SEND
709
 * - FB_DRAW_TEXT_DATA
745
 * - FB_DRAW_TEXT_DATA
710
 *
746
 *
711
 * @param callid Callid of the current call
747
 * @param callid Callid of the current call
712
 * @param call   Current call data
748
 * @param call   Current call data
713
 * @param vp     Active viewport
749
 * @param vp     Active viewport
714
 *
750
 *
715
 * @return false if the call was not handled byt this function, true otherwise
751
 * @return false if the call was not handled byt this function, true otherwise
716
 *
752
 *
717
 * Note: this function is not threads safe, you would have
753
 * Note: this function is not threads safe, you would have
718
 * to redefine static variables with __thread
754
 * to redefine static variables with __thread
719
 *
755
 *
720
 */
756
 */
721
static bool shm_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
757
static bool shm_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
722
{
758
{
723
    static keyfield_t *interbuffer = NULL;
759
    static keyfield_t *interbuffer = NULL;
724
    static size_t intersize = 0;
760
    static size_t intersize = 0;
725
   
761
   
726
    static unsigned char *shm = NULL;
762
    static unsigned char *shm = NULL;
727
    static ipcarg_t shm_id = 0;
763
    static ipcarg_t shm_id = 0;
728
    static size_t shm_size;
764
    static size_t shm_size;
729
   
765
   
730
    bool handled = true;
766
    bool handled = true;
731
    int retval = EOK;
767
    int retval = EOK;
732
    viewport_t *vport = &viewports[vp];
768
    viewport_t *vport = &viewports[vp];
733
    unsigned int x;
769
    unsigned int x;
734
    unsigned int y;
770
    unsigned int y;
735
   
771
   
736
    switch (IPC_GET_METHOD(*call)) {
772
    switch (IPC_GET_METHOD(*call)) {
737
    case IPC_M_SHARE_OUT:
773
    case IPC_M_SHARE_OUT:
738
        /* We accept one area for data interchange */
774
        /* We accept one area for data interchange */
739
        if (IPC_GET_ARG1(*call) == shm_id) {
775
        if (IPC_GET_ARG1(*call) == shm_id) {
740
            void *dest = as_get_mappable_page(IPC_GET_ARG2(*call));
776
            void *dest = as_get_mappable_page(IPC_GET_ARG2(*call));
741
            shm_size = IPC_GET_ARG2(*call);
777
            shm_size = IPC_GET_ARG2(*call);
742
            if (!ipc_answer_1(callid, EOK, (sysarg_t) dest))
778
            if (!ipc_answer_1(callid, EOK, (sysarg_t) dest))
743
                shm = dest;
779
                shm = dest;
744
            else
780
            else
745
                shm_id = 0;
781
                shm_id = 0;
746
           
782
           
747
            if (shm[0] != 'P')
783
            if (shm[0] != 'P')
748
                return false;
784
                return false;
749
           
785
           
750
            return true;
786
            return true;
751
        } else {
787
        } else {
752
            intersize = IPC_GET_ARG2(*call);
788
            intersize = IPC_GET_ARG2(*call);
753
            receive_comm_area(callid, call, (void *) &interbuffer);
789
            receive_comm_area(callid, call, (void *) &interbuffer);
754
        }
790
        }
755
        return true;
791
        return true;
756
    case FB_PREPARE_SHM:
792
    case FB_PREPARE_SHM:
757
        if (shm_id)
793
        if (shm_id)
758
            retval = EBUSY;
794
            retval = EBUSY;
759
        else
795
        else
760
            shm_id = IPC_GET_ARG1(*call);
796
            shm_id = IPC_GET_ARG1(*call);
761
        break;
797
        break;
762
       
798
       
763
    case FB_DROP_SHM:
799
    case FB_DROP_SHM:
764
        if (shm) {
800
        if (shm) {
765
            as_area_destroy(shm);
801
            as_area_destroy(shm);
766
            shm = NULL;
802
            shm = NULL;
767
        }
803
        }
768
        shm_id = 0;
804
        shm_id = 0;
769
        break;
805
        break;
770
       
806
       
771
    case FB_SHM2PIXMAP:
807
    case FB_SHM2PIXMAP:
772
        if (!shm) {
808
        if (!shm) {
773
            retval = EINVAL;
809
            retval = EINVAL;
774
            break;
810
            break;
775
        }
811
        }
776
        retval = shm2pixmap(shm, shm_size);
812
        retval = shm2pixmap(shm, shm_size);
777
        break;
813
        break;
778
    case FB_DRAW_PPM:
814
    case FB_DRAW_PPM:
779
        if (!shm) {
815
        if (!shm) {
780
            retval = EINVAL;
816
            retval = EINVAL;
781
            break;
817
            break;
782
        }
818
        }
783
        x = IPC_GET_ARG1(*call);
819
        x = IPC_GET_ARG1(*call);
784
        y = IPC_GET_ARG2(*call);
820
        y = IPC_GET_ARG2(*call);
785
       
821
       
786
        if ((x > vport->width) || (y > vport->height)) {
822
        if ((x > vport->width) || (y > vport->height)) {
787
            retval = EINVAL;
823
            retval = EINVAL;
788
            break;
824
            break;
789
        }
825
        }
790
       
826
       
791
        ppm_draw(shm, shm_size, IPC_GET_ARG1(*call),
827
        ppm_draw(shm, shm_size, IPC_GET_ARG1(*call),
792
            IPC_GET_ARG2(*call), vport->width - x, vport->height - y, putpixel, (void *) vport);
828
            IPC_GET_ARG2(*call), vport->width - x, vport->height - y, putpixel, (void *) vport);
793
        break;
829
        break;
794
    case FB_DRAW_TEXT_DATA:
830
    case FB_DRAW_TEXT_DATA:
795
        if (!interbuffer) {
831
        if (!interbuffer) {
796
            retval = EINVAL;
832
            retval = EINVAL;
797
            break;
833
            break;
798
        }
834
        }
799
        if (intersize < vport->cols * vport->rows * sizeof(*interbuffer)) {
835
        if (intersize < vport->cols * vport->rows * sizeof(*interbuffer)) {
800
            retval = EINVAL;
836
            retval = EINVAL;
801
            break;
837
            break;
802
        }
838
        }
803
        draw_text_data(vport, interbuffer);
839
        draw_text_data(vport, interbuffer);
804
        break;
840
        break;
805
    default:
841
    default:
806
        handled = false;
842
        handled = false;
807
    }
843
    }
808
   
844
   
809
    if (handled)
845
    if (handled)
810
        ipc_answer_0(callid, retval);
846
        ipc_answer_0(callid, retval);
811
    return handled;
847
    return handled;
812
}
848
}
813
 
849
 
814
 
850
 
815
static void copy_vp_to_pixmap(viewport_t *vport, pixmap_t *pmap)
851
static void copy_vp_to_pixmap(viewport_t *vport, pixmap_t *pmap)
816
{
852
{
817
    unsigned int width = vport->width;
853
    unsigned int width = vport->width;
818
    unsigned int height = vport->height;
854
    unsigned int height = vport->height;
819
   
855
   
820
    if (width + vport->x > screen.xres)
856
    if (width + vport->x > screen.xres)
821
        width = screen.xres - vport->x;
857
        width = screen.xres - vport->x;
822
    if (height + vport->y > screen.yres)
858
    if (height + vport->y > screen.yres)
823
        height = screen.yres - vport->y;
859
        height = screen.yres - vport->y;
824
   
860
   
825
    unsigned int realwidth = pmap->width <= width ? pmap->width : width;
861
    unsigned int realwidth = pmap->width <= width ? pmap->width : width;
826
    unsigned int realheight = pmap->height <= height ? pmap->height : height;
862
    unsigned int realheight = pmap->height <= height ? pmap->height : height;
827
   
863
   
828
    unsigned int srcrowsize = vport->width * screen.pixelbytes;
864
    unsigned int srcrowsize = vport->width * screen.pixelbytes;
829
    unsigned int realrowsize = realwidth * screen.pixelbytes;
865
    unsigned int realrowsize = realwidth * screen.pixelbytes;
830
   
866
   
831
    unsigned int y;
867
    unsigned int y;
832
    for (y = 0; y < realheight; y++) {
868
    for (y = 0; y < realheight; y++) {
833
        unsigned int tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
869
        unsigned int tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
834
        memcpy(pmap->data + srcrowsize * y, screen.fb_addr + tmp, realrowsize);
870
        memcpy(pmap->data + srcrowsize * y, screen.fb_addr + tmp, realrowsize);
835
    }
871
    }
836
}
872
}
837
 
873
 
838
 
874
 
839
/** Save viewport to pixmap
875
/** Save viewport to pixmap
840
 *
876
 *
841
 */
877
 */
842
static int save_vp_to_pixmap(viewport_t *vport)
878
static int save_vp_to_pixmap(viewport_t *vport)
843
{
879
{
844
    int pm;
880
    int pm;
845
    pixmap_t *pmap;
881
    pixmap_t *pmap;
846
   
882
   
847
    pm = find_free_pixmap();
883
    pm = find_free_pixmap();
848
    if (pm == -1)
884
    if (pm == -1)
849
        return ELIMIT;
885
        return ELIMIT;
850
   
886
   
851
    pmap = &pixmaps[pm];
887
    pmap = &pixmaps[pm];
852
    pmap->data = malloc(screen.pixelbytes * vport->width * vport->height);
888
    pmap->data = malloc(screen.pixelbytes * vport->width * vport->height);
853
    if (!pmap->data)
889
    if (!pmap->data)
854
        return ENOMEM;
890
        return ENOMEM;
855
   
891
   
856
    pmap->width = vport->width;
892
    pmap->width = vport->width;
857
    pmap->height = vport->height;
893
    pmap->height = vport->height;
858
   
894
   
859
    copy_vp_to_pixmap(vport, pmap);
895
    copy_vp_to_pixmap(vport, pmap);
860
   
896
   
861
    return pm;
897
    return pm;
862
}
898
}
863
 
899
 
864
 
900
 
865
/** Draw pixmap on screen
901
/** Draw pixmap on screen
866
 *
902
 *
867
 * @param vp Viewport to draw on
903
 * @param vp Viewport to draw on
868
 * @param pm Pixmap identifier
904
 * @param pm Pixmap identifier
869
 *
905
 *
870
 */
906
 */
871
static int draw_pixmap(int vp, int pm)
907
static int draw_pixmap(int vp, int pm)
872
{
908
{
873
    pixmap_t *pmap = &pixmaps[pm];
909
    pixmap_t *pmap = &pixmaps[pm];
874
    viewport_t *vport = &viewports[vp];
910
    viewport_t *vport = &viewports[vp];
875
   
911
   
876
    unsigned int width = vport->width;
912
    unsigned int width = vport->width;
877
    unsigned int height = vport->height;
913
    unsigned int height = vport->height;
878
   
914
   
879
    if (width + vport->x > screen.xres)
915
    if (width + vport->x > screen.xres)
880
        width = screen.xres - vport->x;
916
        width = screen.xres - vport->x;
881
    if (height + vport->y > screen.yres)
917
    if (height + vport->y > screen.yres)
882
        height = screen.yres - vport->y;
918
        height = screen.yres - vport->y;
883
   
919
   
884
    if (!pmap->data)
920
    if (!pmap->data)
885
        return EINVAL;
921
        return EINVAL;
886
   
922
   
887
    unsigned int realwidth = pmap->width <= width ? pmap->width : width;
923
    unsigned int realwidth = pmap->width <= width ? pmap->width : width;
888
    unsigned int realheight = pmap->height <= height ? pmap->height : height;
924
    unsigned int realheight = pmap->height <= height ? pmap->height : height;
889
   
925
   
890
    unsigned int srcrowsize = vport->width * screen.pixelbytes;
926
    unsigned int srcrowsize = vport->width * screen.pixelbytes;
891
    unsigned int realrowsize = realwidth * screen.pixelbytes;
927
    unsigned int realrowsize = realwidth * screen.pixelbytes;
892
   
928
   
893
    unsigned int y;
929
    unsigned int y;
894
    for (y = 0; y < realheight; y++) {
930
    for (y = 0; y < realheight; y++) {
895
        unsigned int tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
931
        unsigned int tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
896
        memcpy(screen.fb_addr + tmp, pmap->data + y * srcrowsize, realrowsize);
932
        memcpy(screen.fb_addr + tmp, pmap->data + y * srcrowsize, realrowsize);
897
    }
933
    }
898
   
934
   
899
    return EOK;
935
    return EOK;
900
}
936
}
901
 
937
 
902
 
938
 
903
/** Tick animation one step forward
939
/** Tick animation one step forward
904
 *
940
 *
905
 */
941
 */
906
static void anims_tick(void)
942
static void anims_tick(void)
907
{
943
{
908
    unsigned int i;
944
    unsigned int i;
909
    static int counts = 0;
945
    static int counts = 0;
910
   
946
   
911
    /* Limit redrawing */
947
    /* Limit redrawing */
912
    counts = (counts + 1) % 8;
948
    counts = (counts + 1) % 8;
913
    if (counts)
949
    if (counts)
914
        return;
950
        return;
915
 
951
 
916
    for (i = 0; i < MAX_ANIMATIONS; i++) {
952
    for (i = 0; i < MAX_ANIMATIONS; i++) {
917
        if ((!animations[i].animlen) || (!animations[i].initialized) ||
953
        if ((!animations[i].animlen) || (!animations[i].initialized) ||
918
            (!animations[i].enabled))
954
            (!animations[i].enabled))
919
            continue;
955
            continue;
920
       
956
       
921
        draw_pixmap(animations[i].vp, animations[i].pixmaps[animations[i].pos]);
957
        draw_pixmap(animations[i].vp, animations[i].pixmaps[animations[i].pos]);
922
        animations[i].pos = (animations[i].pos + 1) % animations[i].animlen;
958
        animations[i].pos = (animations[i].pos + 1) % animations[i].animlen;
923
    }
959
    }
924
}
960
}
925
 
961
 
926
 
962
 
927
static unsigned int pointer_x;
963
static unsigned int pointer_x;
928
static unsigned int pointer_y;
964
static unsigned int pointer_y;
929
static bool pointer_shown, pointer_enabled;
965
static bool pointer_shown, pointer_enabled;
930
static int pointer_vport = -1;
966
static int pointer_vport = -1;
931
static int pointer_pixmap = -1;
967
static int pointer_pixmap = -1;
932
 
968
 
933
 
969
 
934
static void mouse_show(void)
970
static void mouse_show(void)
935
{
971
{
936
    int i, j;
972
    int i, j;
937
    int visibility;
973
    int visibility;
938
    int color;
974
    int color;
939
    int bytepos;
975
    int bytepos;
940
   
976
   
941
    if ((pointer_shown) || (!pointer_enabled))
977
    if ((pointer_shown) || (!pointer_enabled))
942
        return;
978
        return;
943
   
979
   
944
    /* Save image under the pointer. */
980
    /* Save image under the pointer. */
945
    if (pointer_vport == -1) {
981
    if (pointer_vport == -1) {
946
        pointer_vport = vport_create(pointer_x, pointer_y, pointer_width, pointer_height);
982
        pointer_vport = vport_create(pointer_x, pointer_y, pointer_width, pointer_height);
947
        if (pointer_vport < 0)
983
        if (pointer_vport < 0)
948
            return;
984
            return;
949
    } else {
985
    } else {
950
        viewports[pointer_vport].x = pointer_x;
986
        viewports[pointer_vport].x = pointer_x;
951
        viewports[pointer_vport].y = pointer_y;
987
        viewports[pointer_vport].y = pointer_y;
952
    }
988
    }
953
   
989
   
954
    if (pointer_pixmap == -1)
990
    if (pointer_pixmap == -1)
955
        pointer_pixmap = save_vp_to_pixmap(&viewports[pointer_vport]);
991
        pointer_pixmap = save_vp_to_pixmap(&viewports[pointer_vport]);
956
    else
992
    else
957
        copy_vp_to_pixmap(&viewports[pointer_vport], &pixmaps[pointer_pixmap]);
993
        copy_vp_to_pixmap(&viewports[pointer_vport], &pixmaps[pointer_pixmap]);
958
   
994
   
959
    /* Draw mouse pointer. */
995
    /* Draw mouse pointer. */
960
    for (i = 0; i < pointer_height; i++)
996
    for (i = 0; i < pointer_height; i++)
961
        for (j = 0; j < pointer_width; j++) {
997
        for (j = 0; j < pointer_width; j++) {
962
            bytepos = i * ((pointer_width - 1) / 8 + 1) + j / 8;
998
            bytepos = i * ((pointer_width - 1) / 8 + 1) + j / 8;
963
            visibility = pointer_mask_bits[bytepos] &
999
            visibility = pointer_mask_bits[bytepos] &
964
                (1 << (j % 8));
1000
                (1 << (j % 8));
965
            if (visibility) {
1001
            if (visibility) {
966
                color = pointer_bits[bytepos] &
1002
                color = pointer_bits[bytepos] &
967
                    (1 << (j % 8)) ? 0 : 0xffffff;
1003
                    (1 << (j % 8)) ? 0 : 0xffffff;
968
                if (pointer_x + j < screen.xres && pointer_y +
1004
                if (pointer_x + j < screen.xres && pointer_y +
969
                    i < screen.yres)
1005
                    i < screen.yres)
970
                    putpixel(&viewports[0], pointer_x + j,
1006
                    putpixel(&viewports[0], pointer_x + j,
971
                        pointer_y + i, color);
1007
                        pointer_y + i, color);
972
            }
1008
            }
973
        }
1009
        }
974
    pointer_shown = 1;
1010
    pointer_shown = 1;
975
}
1011
}
976
 
1012
 
977
 
1013
 
978
static void mouse_hide(void)
1014
static void mouse_hide(void)
979
{
1015
{
980
    /* Restore image under the pointer. */
1016
    /* Restore image under the pointer. */
981
    if (pointer_shown) {
1017
    if (pointer_shown) {
982
        draw_pixmap(pointer_vport, pointer_pixmap);
1018
        draw_pixmap(pointer_vport, pointer_pixmap);
983
        pointer_shown = 0;
1019
        pointer_shown = 0;
984
    }
1020
    }
985
}
1021
}
986
 
1022
 
987
 
1023
 
988
static void mouse_move(unsigned int x, unsigned int y)
1024
static void mouse_move(unsigned int x, unsigned int y)
989
{
1025
{
990
    mouse_hide();
1026
    mouse_hide();
991
    pointer_x = x;
1027
    pointer_x = x;
992
    pointer_y = y;
1028
    pointer_y = y;
993
    mouse_show();
1029
    mouse_show();
994
}
1030
}
995
 
1031
 
996
 
1032
 
997
static int anim_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
1033
static int anim_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
998
{
1034
{
999
    bool handled = true;
1035
    bool handled = true;
1000
    int retval = EOK;
1036
    int retval = EOK;
1001
    int i, nvp;
1037
    int i, nvp;
1002
    int newval;
1038
    int newval;
1003
   
1039
   
1004
    switch (IPC_GET_METHOD(*call)) {
1040
    switch (IPC_GET_METHOD(*call)) {
1005
    case FB_ANIM_CREATE:
1041
    case FB_ANIM_CREATE:
1006
        nvp = IPC_GET_ARG1(*call);
1042
        nvp = IPC_GET_ARG1(*call);
1007
        if (nvp == -1)
1043
        if (nvp == -1)
1008
            nvp = vp;
1044
            nvp = vp;
1009
        if (nvp >= MAX_VIEWPORTS || nvp < 0 ||
1045
        if (nvp >= MAX_VIEWPORTS || nvp < 0 ||
1010
            !viewports[nvp].initialized) {
1046
            !viewports[nvp].initialized) {
1011
            retval = EINVAL;
1047
            retval = EINVAL;
1012
            break;
1048
            break;
1013
        }
1049
        }
1014
        for (i = 0; i < MAX_ANIMATIONS; i++) {
1050
        for (i = 0; i < MAX_ANIMATIONS; i++) {
1015
            if (!animations[i].initialized)
1051
            if (!animations[i].initialized)
1016
                break;
1052
                break;
1017
        }
1053
        }
1018
        if (i == MAX_ANIMATIONS) {
1054
        if (i == MAX_ANIMATIONS) {
1019
            retval = ELIMIT;
1055
            retval = ELIMIT;
1020
            break;
1056
            break;
1021
        }
1057
        }
1022
        animations[i].initialized = 1;
1058
        animations[i].initialized = 1;
1023
        animations[i].animlen = 0;
1059
        animations[i].animlen = 0;
1024
        animations[i].pos = 0;
1060
        animations[i].pos = 0;
1025
        animations[i].enabled = 0;
1061
        animations[i].enabled = 0;
1026
        animations[i].vp = nvp;
1062
        animations[i].vp = nvp;
1027
        retval = i;
1063
        retval = i;
1028
        break;
1064
        break;
1029
    case FB_ANIM_DROP:
1065
    case FB_ANIM_DROP:
1030
        i = IPC_GET_ARG1(*call);
1066
        i = IPC_GET_ARG1(*call);
1031
        if (i >= MAX_ANIMATIONS || i < 0) {
1067
        if (i >= MAX_ANIMATIONS || i < 0) {
1032
            retval = EINVAL;
1068
            retval = EINVAL;
1033
            break;
1069
            break;
1034
        }
1070
        }
1035
        animations[i].initialized = 0;
1071
        animations[i].initialized = 0;
1036
        break;
1072
        break;
1037
    case FB_ANIM_ADDPIXMAP:
1073
    case FB_ANIM_ADDPIXMAP:
1038
        i = IPC_GET_ARG1(*call);
1074
        i = IPC_GET_ARG1(*call);
1039
        if (i >= MAX_ANIMATIONS || i < 0 ||
1075
        if (i >= MAX_ANIMATIONS || i < 0 ||
1040
            !animations[i].initialized) {
1076
            !animations[i].initialized) {
1041
            retval = EINVAL;
1077
            retval = EINVAL;
1042
            break;
1078
            break;
1043
        }
1079
        }
1044
        if (animations[i].animlen == MAX_ANIM_LEN) {
1080
        if (animations[i].animlen == MAX_ANIM_LEN) {
1045
            retval = ELIMIT;
1081
            retval = ELIMIT;
1046
            break;
1082
            break;
1047
        }
1083
        }
1048
        newval = IPC_GET_ARG2(*call);
1084
        newval = IPC_GET_ARG2(*call);
1049
        if (newval < 0 || newval > MAX_PIXMAPS ||
1085
        if (newval < 0 || newval > MAX_PIXMAPS ||
1050
            !pixmaps[newval].data) {
1086
            !pixmaps[newval].data) {
1051
            retval = EINVAL;
1087
            retval = EINVAL;
1052
            break;
1088
            break;
1053
        }
1089
        }
1054
        animations[i].pixmaps[animations[i].animlen++] = newval;
1090
        animations[i].pixmaps[animations[i].animlen++] = newval;
1055
        break;
1091
        break;
1056
    case FB_ANIM_CHGVP:
1092
    case FB_ANIM_CHGVP:
1057
        i = IPC_GET_ARG1(*call);
1093
        i = IPC_GET_ARG1(*call);
1058
        if (i >= MAX_ANIMATIONS || i < 0) {
1094
        if (i >= MAX_ANIMATIONS || i < 0) {
1059
            retval = EINVAL;
1095
            retval = EINVAL;
1060
            break;
1096
            break;
1061
        }
1097
        }
1062
        nvp = IPC_GET_ARG2(*call);
1098
        nvp = IPC_GET_ARG2(*call);
1063
        if (nvp == -1)
1099
        if (nvp == -1)
1064
            nvp = vp;
1100
            nvp = vp;
1065
        if (nvp >= MAX_VIEWPORTS || nvp < 0 ||
1101
        if (nvp >= MAX_VIEWPORTS || nvp < 0 ||
1066
            !viewports[nvp].initialized) {
1102
            !viewports[nvp].initialized) {
1067
            retval = EINVAL;
1103
            retval = EINVAL;
1068
            break;
1104
            break;
1069
        }
1105
        }
1070
        animations[i].vp = nvp;
1106
        animations[i].vp = nvp;
1071
        break;
1107
        break;
1072
    case FB_ANIM_START:
1108
    case FB_ANIM_START:
1073
    case FB_ANIM_STOP:
1109
    case FB_ANIM_STOP:
1074
        i = IPC_GET_ARG1(*call);
1110
        i = IPC_GET_ARG1(*call);
1075
        if (i >= MAX_ANIMATIONS || i < 0) {
1111
        if (i >= MAX_ANIMATIONS || i < 0) {
1076
            retval = EINVAL;
1112
            retval = EINVAL;
1077
            break;
1113
            break;
1078
        }
1114
        }
1079
        newval = (IPC_GET_METHOD(*call) == FB_ANIM_START);
1115
        newval = (IPC_GET_METHOD(*call) == FB_ANIM_START);
1080
        if (newval ^ animations[i].enabled) {
1116
        if (newval ^ animations[i].enabled) {
1081
            animations[i].enabled = newval;
1117
            animations[i].enabled = newval;
1082
            anims_enabled += newval ? 1 : -1;
1118
            anims_enabled += newval ? 1 : -1;
1083
        }
1119
        }
1084
        break;
1120
        break;
1085
    default:
1121
    default:
1086
        handled = 0;
1122
        handled = 0;
1087
    }
1123
    }
1088
    if (handled)
1124
    if (handled)
1089
        ipc_answer_0(callid, retval);
1125
        ipc_answer_0(callid, retval);
1090
    return handled;
1126
    return handled;
1091
}
1127
}
1092
 
1128
 
1093
 
1129
 
1094
/** Handler for messages concerning pixmap handling
1130
/** Handler for messages concerning pixmap handling
1095
 *
1131
 *
1096
 */
1132
 */
1097
static int pixmap_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
1133
static int pixmap_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
1098
{
1134
{
1099
    bool handled = true;
1135
    bool handled = true;
1100
    int retval = EOK;
1136
    int retval = EOK;
1101
    int i, nvp;
1137
    int i, nvp;
1102
   
1138
   
1103
    switch (IPC_GET_METHOD(*call)) {
1139
    switch (IPC_GET_METHOD(*call)) {
1104
    case FB_VP_DRAW_PIXMAP:
1140
    case FB_VP_DRAW_PIXMAP:
1105
        nvp = IPC_GET_ARG1(*call);
1141
        nvp = IPC_GET_ARG1(*call);
1106
        if (nvp == -1)
1142
        if (nvp == -1)
1107
            nvp = vp;
1143
            nvp = vp;
1108
        if (nvp < 0 || nvp >= MAX_VIEWPORTS ||
1144
        if (nvp < 0 || nvp >= MAX_VIEWPORTS ||
1109
            !viewports[nvp].initialized) {
1145
            !viewports[nvp].initialized) {
1110
            retval = EINVAL;
1146
            retval = EINVAL;
1111
            break;
1147
            break;
1112
        }
1148
        }
1113
        i = IPC_GET_ARG2(*call);
1149
        i = IPC_GET_ARG2(*call);
1114
        retval = draw_pixmap(nvp, i);
1150
        retval = draw_pixmap(nvp, i);
1115
        break;
1151
        break;
1116
    case FB_VP2PIXMAP:
1152
    case FB_VP2PIXMAP:
1117
        nvp = IPC_GET_ARG1(*call);
1153
        nvp = IPC_GET_ARG1(*call);
1118
        if (nvp == -1)
1154
        if (nvp == -1)
1119
            nvp = vp;
1155
            nvp = vp;
1120
        if (nvp < 0 || nvp >= MAX_VIEWPORTS ||
1156
        if (nvp < 0 || nvp >= MAX_VIEWPORTS ||
1121
            !viewports[nvp].initialized)
1157
            !viewports[nvp].initialized)
1122
            retval = EINVAL;
1158
            retval = EINVAL;
1123
        else
1159
        else
1124
            retval = save_vp_to_pixmap(&viewports[nvp]);
1160
            retval = save_vp_to_pixmap(&viewports[nvp]);
1125
        break;
1161
        break;
1126
    case FB_DROP_PIXMAP:
1162
    case FB_DROP_PIXMAP:
1127
        i = IPC_GET_ARG1(*call);
1163
        i = IPC_GET_ARG1(*call);
1128
        if (i >= MAX_PIXMAPS) {
1164
        if (i >= MAX_PIXMAPS) {
1129
            retval = EINVAL;
1165
            retval = EINVAL;
1130
            break;
1166
            break;
1131
        }
1167
        }
1132
        if (pixmaps[i].data) {
1168
        if (pixmaps[i].data) {
1133
            free(pixmaps[i].data);
1169
            free(pixmaps[i].data);
1134
            pixmaps[i].data = NULL;
1170
            pixmaps[i].data = NULL;
1135
        }
1171
        }
1136
        break;
1172
        break;
1137
    default:
1173
    default:
1138
        handled = 0;
1174
        handled = 0;
1139
    }
1175
    }
1140
   
1176
   
1141
    if (handled)
1177
    if (handled)
1142
        ipc_answer_0(callid, retval);
1178
        ipc_answer_0(callid, retval);
1143
    return handled;
1179
    return handled;
1144
   
1180
   
1145
}
1181
}
1146
 
1182
 
1147
/** Function for handling connections to FB
1183
/** Function for handling connections to FB
1148
 *
1184
 *
1149
 */
1185
 */
1150
static void fb_client_connection(ipc_callid_t iid, ipc_call_t *icall)
1186
static void fb_client_connection(ipc_callid_t iid, ipc_call_t *icall)
1151
{
1187
{
1152
    unsigned int vp = 0;
1188
    unsigned int vp = 0;
1153
    viewport_t *vport = &viewports[vp];
1189
    viewport_t *vport = &viewports[vp];
1154
   
1190
   
1155
    if (client_connected) {
1191
    if (client_connected) {
1156
        ipc_answer_0(iid, ELIMIT);
1192
        ipc_answer_0(iid, ELIMIT);
1157
        return;
1193
        return;
1158
    }
1194
    }
1159
   
1195
   
1160
    /* Accept connection */
1196
    /* Accept connection */
1161
    client_connected = true;
1197
    client_connected = true;
1162
    ipc_answer_0(iid, EOK);
1198
    ipc_answer_0(iid, EOK);
1163
   
1199
   
1164
    while (true) {
1200
    while (true) {
1165
        ipc_callid_t callid;
1201
        ipc_callid_t callid;
1166
        ipc_call_t call;
1202
        ipc_call_t call;
1167
        int retval;
1203
        int retval;
1168
        unsigned int i;
1204
        unsigned int i;
1169
        int scroll;
1205
        int scroll;
1170
        uint8_t glyph;
1206
        uint8_t glyph;
1171
        unsigned int row, col;
1207
        unsigned int row, col;
1172
       
1208
       
1173
        if ((vport->cursor_active) || (anims_enabled))
1209
        if ((vport->cursor_active) || (anims_enabled))
1174
            callid = async_get_call_timeout(&call, 250000);
1210
            callid = async_get_call_timeout(&call, 250000);
1175
        else
1211
        else
1176
            callid = async_get_call(&call);
1212
            callid = async_get_call(&call);
1177
       
1213
       
1178
        mouse_hide();
1214
        mouse_hide();
1179
        if (!callid) {
1215
        if (!callid) {
1180
            cursor_blink(vport);
1216
            cursor_blink(vport);
1181
            anims_tick();
1217
            anims_tick();
1182
            mouse_show();
1218
            mouse_show();
1183
            continue;
1219
            continue;
1184
        }
1220
        }
1185
       
1221
       
1186
        if (shm_handle(callid, &call, vp))
1222
        if (shm_handle(callid, &call, vp))
1187
            continue;
1223
            continue;
1188
       
1224
       
1189
        if (pixmap_handle(callid, &call, vp))
1225
        if (pixmap_handle(callid, &call, vp))
1190
            continue;
1226
            continue;
1191
       
1227
       
1192
        if (anim_handle(callid, &call, vp))
1228
        if (anim_handle(callid, &call, vp))
1193
            continue;
1229
            continue;
1194
       
1230
       
1195
        switch (IPC_GET_METHOD(call)) {
1231
        switch (IPC_GET_METHOD(call)) {
1196
        case IPC_M_PHONE_HUNGUP:
1232
        case IPC_M_PHONE_HUNGUP:
1197
            client_connected = false;
1233
            client_connected = false;
1198
           
1234
           
1199
            /* Cleanup other viewports */
1235
            /* Cleanup other viewports */
1200
            for (i = 1; i < MAX_VIEWPORTS; i++)
1236
            for (i = 1; i < MAX_VIEWPORTS; i++)
1201
                vport->initialized = false;
1237
                vport->initialized = false;
1202
           
1238
           
1203
            /* Exit thread */
1239
            /* Exit thread */
1204
            return;
1240
            return;
1205
       
1241
       
1206
        case FB_PUTCHAR:
1242
        case FB_PUTCHAR:
1207
            glyph = IPC_GET_ARG1(call);
1243
            glyph = IPC_GET_ARG1(call);
1208
            row = IPC_GET_ARG2(call);
1244
            row = IPC_GET_ARG2(call);
1209
            col = IPC_GET_ARG3(call);
1245
            col = IPC_GET_ARG3(call);
1210
           
1246
           
1211
            if ((col >= vport->cols) || (row >= vport->rows)) {
1247
            if ((col >= vport->cols) || (row >= vport->rows)) {
1212
                retval = EINVAL;
1248
                retval = EINVAL;
1213
                break;
1249
                break;
1214
            }
1250
            }
1215
            ipc_answer_0(callid, EOK);
1251
            ipc_answer_0(callid, EOK);
1216
           
1252
           
1217
            draw_char(vport, glyph, col, row);
1253
            draw_char(vport, glyph, col, row);
1218
           
1254
           
1219
            /* Message already answered */
1255
            /* Message already answered */
1220
            continue;
1256
            continue;
1221
        case FB_CLEAR:
1257
        case FB_CLEAR:
1222
            vport_clear(vport);
1258
            vport_clear(vport);
1223
            cursor_show(vport);
1259
            cursor_show(vport);
1224
            retval = EOK;
1260
            retval = EOK;
1225
            break;
1261
            break;
1226
        case FB_CURSOR_GOTO:
1262
        case FB_CURSOR_GOTO:
1227
            row = IPC_GET_ARG1(call);
1263
            row = IPC_GET_ARG1(call);
1228
            col = IPC_GET_ARG2(call);
1264
            col = IPC_GET_ARG2(call);
1229
           
1265
           
1230
            if ((col >= vport->cols) || (row >= vport->rows)) {
1266
            if ((col >= vport->cols) || (row >= vport->rows)) {
1231
                retval = EINVAL;
1267
                retval = EINVAL;
1232
                break;
1268
                break;
1233
            }
1269
            }
1234
            retval = EOK;
1270
            retval = EOK;
1235
           
1271
           
1236
            cursor_hide(vport);
1272
            cursor_hide(vport);
1237
            vport->cur_col = col;
1273
            vport->cur_col = col;
1238
            vport->cur_row = row;
1274
            vport->cur_row = row;
1239
            cursor_show(vport);
1275
            cursor_show(vport);
1240
            break;
1276
            break;
1241
        case FB_CURSOR_VISIBILITY:
1277
        case FB_CURSOR_VISIBILITY:
1242
            cursor_hide(vport);
1278
            cursor_hide(vport);
1243
            vport->cursor_active = IPC_GET_ARG1(call);
1279
            vport->cursor_active = IPC_GET_ARG1(call);
1244
            cursor_show(vport);
1280
            cursor_show(vport);
1245
            retval = EOK;
1281
            retval = EOK;
1246
            break;
1282
            break;
1247
        case FB_GET_CSIZE:
1283
        case FB_GET_CSIZE:
1248
            ipc_answer_2(callid, EOK, vport->rows, vport->cols);
1284
            ipc_answer_2(callid, EOK, vport->rows, vport->cols);
1249
            continue;
1285
            continue;
1250
        case FB_SCROLL:
1286
        case FB_SCROLL:
1251
            scroll = IPC_GET_ARG1(call);
1287
            scroll = IPC_GET_ARG1(call);
1252
            if ((scroll > (int) vport->rows) || (scroll < (-(int) vport->rows))) {
1288
            if ((scroll > (int) vport->rows) || (scroll < (-(int) vport->rows))) {
1253
                retval = EINVAL;
1289
                retval = EINVAL;
1254
                break;
1290
                break;
1255
            }
1291
            }
1256
            cursor_hide(vport);
1292
            cursor_hide(vport);
1257
            vport_scroll(vport, scroll);
1293
            vport_scroll(vport, scroll);
1258
            cursor_show(vport);
1294
            cursor_show(vport);
1259
            retval = EOK;
1295
            retval = EOK;
1260
            break;
1296
            break;
1261
        case FB_VIEWPORT_SWITCH:
1297
        case FB_VIEWPORT_SWITCH:
1262
            i = IPC_GET_ARG1(call);
1298
            i = IPC_GET_ARG1(call);
1263
            if (i >= MAX_VIEWPORTS) {
1299
            if (i >= MAX_VIEWPORTS) {
1264
                retval = EINVAL;
1300
                retval = EINVAL;
1265
                break;
1301
                break;
1266
            }
1302
            }
1267
            if (!viewports[i].initialized) {
1303
            if (!viewports[i].initialized) {
1268
                retval = EADDRNOTAVAIL;
1304
                retval = EADDRNOTAVAIL;
1269
                break;
1305
                break;
1270
            }
1306
            }
1271
            cursor_hide(vport);
1307
            cursor_hide(vport);
1272
            vp = i;
1308
            vp = i;
1273
            vport = &viewports[vp];
1309
            vport = &viewports[vp];
1274
            cursor_show(vport);
1310
            cursor_show(vport);
1275
            retval = EOK;
1311
            retval = EOK;
1276
            break;
1312
            break;
1277
        case FB_VIEWPORT_CREATE:
1313
        case FB_VIEWPORT_CREATE:
1278
            retval = vport_create(IPC_GET_ARG1(call) >> 16,
1314
            retval = vport_create(IPC_GET_ARG1(call) >> 16,
1279
                IPC_GET_ARG1(call) & 0xffff,
1315
                IPC_GET_ARG1(call) & 0xffff,
1280
                IPC_GET_ARG2(call) >> 16,
1316
                IPC_GET_ARG2(call) >> 16,
1281
                IPC_GET_ARG2(call) & 0xffff);
1317
                IPC_GET_ARG2(call) & 0xffff);
1282
            break;
1318
            break;
1283
        case FB_VIEWPORT_DELETE:
1319
        case FB_VIEWPORT_DELETE:
1284
            i = IPC_GET_ARG1(call);
1320
            i = IPC_GET_ARG1(call);
1285
            if (i >= MAX_VIEWPORTS) {
1321
            if (i >= MAX_VIEWPORTS) {
1286
                retval = EINVAL;
1322
                retval = EINVAL;
1287
                break;
1323
                break;
1288
            }
1324
            }
1289
            if (!viewports[i].initialized) {
1325
            if (!viewports[i].initialized) {
1290
                retval = EADDRNOTAVAIL;
1326
                retval = EADDRNOTAVAIL;
1291
                break;
1327
                break;
1292
            }
1328
            }
1293
            viewports[i].initialized = false;
1329
            viewports[i].initialized = false;
1294
            if (viewports[i].glyphs)
1330
            if (viewports[i].glyphs)
1295
                free(viewports[i].glyphs);
1331
                free(viewports[i].glyphs);
1296
            if (viewports[i].bgpixel)
1332
            if (viewports[i].bgpixel)
1297
                free(viewports[i].bgpixel);
1333
                free(viewports[i].bgpixel);
1298
            if (viewports[i].backbuf)
1334
            if (viewports[i].backbuf)
1299
                free(viewports[i].backbuf);
1335
                free(viewports[i].backbuf);
1300
            retval = EOK;
1336
            retval = EOK;
1301
            break;
1337
            break;
1302
        case FB_SET_STYLE:
1338
        case FB_SET_STYLE:
1303
            vport->style.fg_color = IPC_GET_ARG1(call);
1339
            vport->style.fg_color = IPC_GET_ARG1(call);
1304
            vport->style.bg_color = IPC_GET_ARG2(call);
1340
            vport->style.bg_color = IPC_GET_ARG2(call);
1305
            render_glyphs(vport);
1341
            render_glyphs(vport);
1306
            retval = EOK;
1342
            retval = EOK;
1307
            break;
1343
            break;
1308
        case FB_GET_RESOLUTION:
1344
        case FB_GET_RESOLUTION:
1309
            ipc_answer_2(callid, EOK, screen.xres, screen.yres);
1345
            ipc_answer_2(callid, EOK, screen.xres, screen.yres);
1310
            continue;
1346
            continue;
1311
        case FB_POINTER_MOVE:
1347
        case FB_POINTER_MOVE:
1312
            pointer_enabled = true;
1348
            pointer_enabled = true;
1313
            mouse_move(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
1349
            mouse_move(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
1314
            retval = EOK;
1350
            retval = EOK;
1315
            break;
1351
            break;
1316
        default:
1352
        default:
1317
            retval = ENOENT;
1353
            retval = ENOENT;
1318
        }
1354
        }
1319
        ipc_answer_0(callid, retval);
1355
        ipc_answer_0(callid, retval);
1320
    }
1356
    }
1321
}
1357
}
1322
 
1358
 
1323
/** Initialization of framebuffer
1359
/** Initialization of framebuffer
1324
 *
1360
 *
1325
 */
1361
 */
1326
int fb_init(void)
1362
int fb_init(void)
1327
{
1363
{
1328
    async_set_client_connection(fb_client_connection);
1364
    async_set_client_connection(fb_client_connection);
1329
   
1365
   
1330
    void *fb_ph_addr = (void *) sysinfo_value("fb.address.physical");
1366
    void *fb_ph_addr = (void *) sysinfo_value("fb.address.physical");
1331
    unsigned int fb_offset = sysinfo_value("fb.offset");
1367
    unsigned int fb_offset = sysinfo_value("fb.offset");
1332
    unsigned int fb_width = sysinfo_value("fb.width");
1368
    unsigned int fb_width = sysinfo_value("fb.width");
1333
    unsigned int fb_height = sysinfo_value("fb.height");
1369
    unsigned int fb_height = sysinfo_value("fb.height");
1334
    unsigned int fb_scanline = sysinfo_value("fb.scanline");
1370
    unsigned int fb_scanline = sysinfo_value("fb.scanline");
1335
    unsigned int fb_visual = sysinfo_value("fb.visual");
1371
    unsigned int fb_visual = sysinfo_value("fb.visual");
1336
   
1372
   
1337
    unsigned int fbsize = fb_scanline * fb_height;
1373
    unsigned int fbsize = fb_scanline * fb_height;
1338
    void *fb_addr = as_get_mappable_page(fbsize);
1374
    void *fb_addr = as_get_mappable_page(fbsize);
1339
   
1375
   
1340
    physmem_map(fb_ph_addr + fb_offset, fb_addr,
1376
    physmem_map(fb_ph_addr + fb_offset, fb_addr,
1341
        ALIGN_UP(fbsize, PAGE_SIZE) >> PAGE_WIDTH, AS_AREA_READ | AS_AREA_WRITE);
1377
        ALIGN_UP(fbsize, PAGE_SIZE) >> PAGE_WIDTH, AS_AREA_READ | AS_AREA_WRITE);
1342
   
1378
   
1343
    if (screen_init(fb_addr, fb_width, fb_height, fb_scanline, fb_visual))
1379
    if (screen_init(fb_addr, fb_width, fb_height, fb_scanline, fb_visual))
1344
        return 0;
1380
        return 0;
1345
   
1381
   
1346
    return -1;
1382
    return -1;
1347
}
1383
}
1348
 
1384
 
1349
/**
1385
/**
1350
 * @}
1386
 * @}
1351
 */
1387
 */
1352
 
1388