Subversion Repositories HelenOS

Rev

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

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