Subversion Repositories HelenOS

Rev

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

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