Subversion Repositories HelenOS

Rev

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

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