Subversion Repositories HelenOS

Rev

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

Rev 3744 Rev 3767
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 <console/color.h>
-
 
55
#include <console/style.h>
54
#include <async.h>
56
#include <async.h>
55
#include <bool.h>
57
#include <bool.h>
56
 
58
 
57
#include "font-8x16.h"
59
#include "font-8x16.h"
58
#include "fb.h"
60
#include "fb.h"
59
#include "main.h"
61
#include "main.h"
60
#include "../console/screenbuffer.h"
62
#include "../console/screenbuffer.h"
61
#include "ppm.h"
63
#include "ppm.h"
62
 
64
 
63
#include "pointer.xbm"
65
#include "pointer.xbm"
64
#include "pointer_mask.xbm"
66
#include "pointer_mask.xbm"
65
 
67
 
66
#define DEFAULT_BGCOLOR  0xf0f0f0
68
#define DEFAULT_BGCOLOR  0xf0f0f0
67
#define DEFAULT_FGCOLOR  0x000000
69
#define DEFAULT_FGCOLOR  0x000000
68
 
70
 
69
#define MAX_ANIM_LEN     8
71
#define MAX_ANIM_LEN     8
70
#define MAX_ANIMATIONS   4
72
#define MAX_ANIMATIONS   4
71
#define MAX_PIXMAPS      256  /**< Maximum number of saved pixmaps */
73
#define MAX_PIXMAPS      256  /**< Maximum number of saved pixmaps */
72
#define MAX_VIEWPORTS    128  /**< Viewport is a rectangular area on the screen */
74
#define MAX_VIEWPORTS    128  /**< Viewport is a rectangular area on the screen */
73
 
75
 
74
/** Function to render a pixel from a RGB value. */
76
/** Function to render a pixel from a RGB value. */
75
typedef void (*rgb_conv_t)(void *, uint32_t);
77
typedef void (*rgb_conv_t)(void *, uint32_t);
76
 
78
 
77
/** Function to draw a glyph. */
79
/** Function to draw a glyph. */
78
typedef void (*dg_t)(unsigned int x, unsigned int y, bool cursor,
80
typedef void (*dg_t)(unsigned int x, unsigned int y, bool cursor,
79
    uint8_t *glyphs, uint8_t glyph, uint32_t fg_color, uint32_t bg_color);
81
    uint8_t *glyphs, uint8_t glyph, uint32_t fg_color, uint32_t bg_color);
80
 
82
 
81
struct {
83
struct {
82
    uint8_t *fb_addr;
84
    uint8_t *fb_addr;
83
   
85
   
84
    unsigned int xres;
86
    unsigned int xres;
85
    unsigned int yres;
87
    unsigned int yres;
86
   
88
   
87
    unsigned int scanline;
89
    unsigned int scanline;
88
    unsigned int glyphscanline;
90
    unsigned int glyphscanline;
89
   
91
   
90
    unsigned int pixelbytes;
92
    unsigned int pixelbytes;
91
    unsigned int glyphbytes;
93
    unsigned int glyphbytes;
92
   
94
   
93
    rgb_conv_t rgb_conv;
95
    rgb_conv_t rgb_conv;
94
} screen;
96
} screen;
95
 
97
 
-
 
98
/** Backbuffer character cell. */
-
 
99
typedef struct {
-
 
100
    uint8_t glyph;
-
 
101
    uint32_t fg_color;
-
 
102
    uint32_t bg_color;
-
 
103
} bb_cell_t;
-
 
104
 
96
typedef struct {
105
typedef struct {
97
    bool initialized;
106
    bool initialized;
98
    unsigned int x;
107
    unsigned int x;
99
    unsigned int y;
108
    unsigned int y;
100
    unsigned int width;
109
    unsigned int width;
101
    unsigned int height;
110
    unsigned int height;
102
   
111
   
103
    /* Text support in window */
112
    /* Text support in window */
104
    unsigned int cols;
113
    unsigned int cols;
105
    unsigned int rows;
114
    unsigned int rows;
106
   
115
   
107
    /*
116
    /*
108
     * Style and glyphs for text printing
117
     * Style and glyphs for text printing
109
     */
118
     */
110
 
119
 
111
    /** Current style. */
120
    /** Current attributes. */
112
    style_t style;
121
    attr_rgb_t attr;
113
 
122
 
114
    /** Pre-rendered mask for rendering glyphs. Different viewports
123
    /** Pre-rendered mask for rendering glyphs. Different viewports
115
     * might use different drawing functions depending on whether their
124
     * might use different drawing functions depending on whether their
116
     * scanlines are aligned on a word boundary.*/
125
     * scanlines are aligned on a word boundary.*/
117
    uint8_t *glyphs;
126
    uint8_t *glyphs;
118
 
127
 
119
    uint8_t *bgpixel;
128
    uint8_t *bgpixel;
120
 
129
 
121
    /** Glyph drawing function for this viewport. */
130
    /** Glyph drawing function for this viewport. */
122
    dg_t dglyph;
131
    dg_t dglyph;
123
   
132
   
124
    /* Auto-cursor position */
133
    /* Auto-cursor position */
125
    bool cursor_active;
134
    bool cursor_active;
126
    unsigned int cur_col;
135
    unsigned int cur_col;
127
    unsigned int cur_row;
136
    unsigned int cur_row;
128
    bool cursor_shown;
137
    bool cursor_shown;
129
   
138
   
130
    /* Back buffer */
139
    /* Back buffer */
-
 
140
    bb_cell_t *backbuf;
131
    unsigned int bbsize;
141
    unsigned int bbsize;
132
    uint8_t *backbuf;
-
 
133
} viewport_t;
142
} viewport_t;
134
 
143
 
135
typedef struct {
144
typedef struct {
136
    bool initialized;
145
    bool initialized;
137
    bool enabled;
146
    bool enabled;
138
    unsigned int vp;
147
    unsigned int vp;
139
   
148
   
140
    unsigned int pos;
149
    unsigned int pos;
141
    unsigned int animlen;
150
    unsigned int animlen;
142
    unsigned int pixmaps[MAX_ANIM_LEN];
151
    unsigned int pixmaps[MAX_ANIM_LEN];
143
} animation_t;
152
} animation_t;
144
 
153
 
145
static animation_t animations[MAX_ANIMATIONS];
154
static animation_t animations[MAX_ANIMATIONS];
146
static bool anims_enabled;
155
static bool anims_enabled;
147
 
156
 
148
typedef struct {
157
typedef struct {
149
    unsigned int width;
158
    unsigned int width;
150
    unsigned int height;
159
    unsigned int height;
151
    uint8_t *data;
160
    uint8_t *data;
152
} pixmap_t;
161
} pixmap_t;
153
 
162
 
154
static pixmap_t pixmaps[MAX_PIXMAPS];
163
static pixmap_t pixmaps[MAX_PIXMAPS];
155
static viewport_t viewports[128];
164
static viewport_t viewports[128];
156
 
165
 
157
static bool client_connected = false;  /**< Allow only 1 connection */
166
static bool client_connected = false;  /**< Allow only 1 connection */
158
 
167
 
-
 
168
static uint32_t color_table[16] = {
-
 
169
    [COLOR_BLACK]       = 0x000000,
-
 
170
    [COLOR_BLUE]        = 0x0000f0,
-
 
171
    [COLOR_GREEN]       = 0x00f000,
-
 
172
    [COLOR_CYAN]        = 0x00f0f0,
-
 
173
    [COLOR_RED]     = 0xf00000,
-
 
174
    [COLOR_MAGENTA]     = 0xf000f0,
-
 
175
    [COLOR_YELLOW]      = 0xf0f000,
-
 
176
    [COLOR_WHITE]       = 0xf0f0f0,
-
 
177
 
-
 
178
    [8 + COLOR_BLACK]   = 0x000000,
-
 
179
    [8 + COLOR_BLUE]    = 0x0000ff,
-
 
180
    [8 + COLOR_GREEN]   = 0x00ff00,
-
 
181
    [8 + COLOR_CYAN]    = 0x00ffff,
-
 
182
    [8 + COLOR_RED]     = 0xff0000,
-
 
183
    [8 + COLOR_MAGENTA] = 0xff00ff,
-
 
184
    [8 + COLOR_YELLOW]  = 0xffff00,
-
 
185
    [8 + COLOR_WHITE]   = 0xffffff,
-
 
186
};
-
 
187
 
-
 
188
static int rgb_from_style(attr_rgb_t *rgb, int style);
-
 
189
static int rgb_from_idx(attr_rgb_t *rgb, ipcarg_t fg_color,
-
 
190
    ipcarg_t bg_color, ipcarg_t flags);
-
 
191
 
-
 
192
static int fb_set_color(viewport_t *vport, ipcarg_t fg_color,
-
 
193
    ipcarg_t bg_color, ipcarg_t attr);
-
 
194
 
159
static void draw_glyph_aligned(unsigned int x, unsigned int y, bool cursor,
195
static void draw_glyph_aligned(unsigned int x, unsigned int y, bool cursor,
160
    uint8_t *glyphs, uint8_t glyph, uint32_t fg_color, uint32_t bg_color);
196
    uint8_t *glyphs, uint8_t glyph, uint32_t fg_color, uint32_t bg_color);
161
static void draw_glyph_fallback(unsigned int x, unsigned int y, bool cursor,
197
static void draw_glyph_fallback(unsigned int x, unsigned int y, bool cursor,
162
    uint8_t *glyphs, uint8_t glyph, uint32_t fg_color, uint32_t bg_color);
198
    uint8_t *glyphs, uint8_t glyph, uint32_t fg_color, uint32_t bg_color);
163
 
199
 
164
static void draw_vp_glyph(viewport_t *vport, bool cursor, unsigned int col,
200
static void draw_vp_glyph(viewport_t *vport, bool cursor, unsigned int col,
165
    unsigned int row);
201
    unsigned int row);
166
 
202
 
167
 
203
 
168
#define RED(x, bits)                 ((x >> (8 + 8 + 8 - bits)) & ((1 << bits) - 1))
204
#define RED(x, bits)                 ((x >> (8 + 8 + 8 - bits)) & ((1 << bits) - 1))
169
#define GREEN(x, bits)               ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
205
#define GREEN(x, bits)               ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
170
#define BLUE(x, bits)                ((x >> (8 - bits)) & ((1 << bits) - 1))
206
#define BLUE(x, bits)                ((x >> (8 - bits)) & ((1 << bits) - 1))
171
 
207
 
172
#define COL2X(col)                   ((col) * FONT_WIDTH)
208
#define COL2X(col)                   ((col) * FONT_WIDTH)
173
#define ROW2Y(row)                   ((row) * FONT_SCANLINES)
209
#define ROW2Y(row)                   ((row) * FONT_SCANLINES)
174
 
210
 
175
#define X2COL(x)                     ((x) / FONT_WIDTH)
211
#define X2COL(x)                     ((x) / FONT_WIDTH)
176
#define Y2ROW(y)                     ((y) / FONT_SCANLINES)
212
#define Y2ROW(y)                     ((y) / FONT_SCANLINES)
177
 
213
 
178
#define FB_POS(x, y)                 ((y) * screen.scanline + (x) * screen.pixelbytes)
214
#define FB_POS(x, y)                 ((y) * screen.scanline + (x) * screen.pixelbytes)
179
#define BB_POS(vport, col, row)      ((row) * vport->cols + (col))
215
#define BB_POS(vport, col, row)      ((row) * vport->cols + (col))
180
#define GLYPH_POS(glyph, y, cursor)  (((glyph) + (cursor) * FONT_GLYPHS) * screen.glyphbytes + (y) * screen.glyphscanline)
216
#define GLYPH_POS(glyph, y, cursor)  (((glyph) + (cursor) * FONT_GLYPHS) * screen.glyphbytes + (y) * screen.glyphscanline)
181
 
217
 
182
 
218
 
183
/** ARGB 8:8:8:8 conversion
219
/** ARGB 8:8:8:8 conversion
184
 *
220
 *
185
 */
221
 */
186
static void rgb_0888(void *dst, uint32_t rgb)
222
static void rgb_0888(void *dst, uint32_t rgb)
187
{
223
{
188
    *((uint32_t *) dst) = rgb & 0xffffff;
224
    *((uint32_t *) dst) = rgb & 0xffffff;
189
}
225
}
190
 
226
 
191
 
227
 
192
/** ABGR 8:8:8:8 conversion
228
/** ABGR 8:8:8:8 conversion
193
 *
229
 *
194
 */
230
 */
195
static void bgr_0888(void *dst, uint32_t rgb)
231
static void bgr_0888(void *dst, uint32_t rgb)
196
{
232
{
197
    *((uint32_t *) dst)
233
    *((uint32_t *) dst)
198
        = (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | RED(rgb, 8);
234
        = (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | RED(rgb, 8);
199
}
235
}
200
 
236
 
201
 
237
 
202
/** BGR 8:8:8 conversion
238
/** BGR 8:8:8 conversion
203
 *
239
 *
204
 */
240
 */
205
static void rgb_888(void *dst, uint32_t rgb)
241
static void rgb_888(void *dst, uint32_t rgb)
206
{
242
{
207
#if defined(FB_INVERT_ENDIAN)
243
#if defined(FB_INVERT_ENDIAN)
208
    ((uint8_t *) dst)[0] = RED(rgb, 8);
244
    ((uint8_t *) dst)[0] = RED(rgb, 8);
209
    ((uint8_t *) dst)[1] = GREEN(rgb, 8);
245
    ((uint8_t *) dst)[1] = GREEN(rgb, 8);
210
    ((uint8_t *) dst)[2] = BLUE(rgb, 8);
246
    ((uint8_t *) dst)[2] = BLUE(rgb, 8);
211
#else
247
#else
212
    ((uint8_t *) dst)[0] = BLUE(rgb, 8);
248
    ((uint8_t *) dst)[0] = BLUE(rgb, 8);
213
    ((uint8_t *) dst)[1] = GREEN(rgb, 8);
249
    ((uint8_t *) dst)[1] = GREEN(rgb, 8);
214
    ((uint8_t *) dst)[2] = RED(rgb, 8);
250
    ((uint8_t *) dst)[2] = RED(rgb, 8);
215
#endif
251
#endif
216
}
252
}
217
 
253
 
218
 
254
 
219
/** RGB 5:5:5 conversion
255
/** RGB 5:5:5 conversion
220
 *
256
 *
221
 */
257
 */
222
static void rgb_555(void *dst, uint32_t rgb)
258
static void rgb_555(void *dst, uint32_t rgb)
223
{
259
{
224
    *((uint16_t *) dst)
260
    *((uint16_t *) dst)
225
        = (RED(rgb, 5) << 10) | (GREEN(rgb, 5) << 5) | BLUE(rgb, 5);
261
        = (RED(rgb, 5) << 10) | (GREEN(rgb, 5) << 5) | BLUE(rgb, 5);
226
}
262
}
227
 
263
 
228
 
264
 
229
/** RGB 5:6:5 conversion
265
/** RGB 5:6:5 conversion
230
 *
266
 *
231
 */
267
 */
232
static void rgb_565(void *dst, uint32_t rgb)
268
static void rgb_565(void *dst, uint32_t rgb)
233
{
269
{
234
    *((uint16_t *) dst)
270
    *((uint16_t *) dst)
235
        = (RED(rgb, 5) << 11) | (GREEN(rgb, 6) << 5) | BLUE(rgb, 5);
271
        = (RED(rgb, 5) << 11) | (GREEN(rgb, 6) << 5) | BLUE(rgb, 5);
236
}
272
}
237
 
273
 
238
 
274
 
239
/** RGB 3:2:3
275
/** RGB 3:2:3
240
 *
276
 *
241
 */
277
 */
242
static void rgb_323(void *dst, uint32_t rgb)
278
static void rgb_323(void *dst, uint32_t rgb)
243
{
279
{
244
    *((uint8_t *) dst)
280
    *((uint8_t *) dst)
245
        = ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
281
        = ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
246
}
282
}
247
 
283
 
248
/** Draw a filled rectangle.
284
/** Draw a filled rectangle.
249
 *
285
 *
250
 * @note Need real implementation that does not access VRAM twice.
286
 * @note Need real implementation that does not access VRAM twice.
251
 */
287
 */
252
static void draw_filled_rect(unsigned int x0, unsigned int y0, unsigned int x1,
288
static void draw_filled_rect(unsigned int x0, unsigned int y0, unsigned int x1,
253
    unsigned int y1, uint32_t color)
289
    unsigned int y1, uint32_t color)
254
{
290
{
255
    unsigned int x, y;
291
    unsigned int x, y;
256
    unsigned int copy_bytes;
292
    unsigned int copy_bytes;
257
 
293
 
258
    uint8_t *sp, *dp;
294
    uint8_t *sp, *dp;
259
    uint8_t cbuf[4];
295
    uint8_t cbuf[4];
260
 
296
 
261
    if (y0 >= y1 || x0 >= x1) return;
297
    if (y0 >= y1 || x0 >= x1) return;
262
    screen.rgb_conv(cbuf, color);
298
    screen.rgb_conv(cbuf, color);
263
 
299
 
264
    sp = &screen.fb_addr[FB_POS(x0, y0)];
300
    sp = &screen.fb_addr[FB_POS(x0, y0)];
265
    dp = sp;
301
    dp = sp;
266
 
302
 
267
    /* Draw the first line. */
303
    /* Draw the first line. */
268
    for (x = x0; x < x1; x++) {
304
    for (x = x0; x < x1; x++) {
269
        memcpy(dp, cbuf, screen.pixelbytes);
305
        memcpy(dp, cbuf, screen.pixelbytes);
270
        dp += screen.pixelbytes;
306
        dp += screen.pixelbytes;
271
    }
307
    }
272
 
308
 
273
    dp = sp + screen.scanline;
309
    dp = sp + screen.scanline;
274
    copy_bytes = (x1 - x0) * screen.pixelbytes;
310
    copy_bytes = (x1 - x0) * screen.pixelbytes;
275
 
311
 
276
    /* Draw the remaining lines by copying. */
312
    /* Draw the remaining lines by copying. */
277
    for (y = y0 + 1; y < y1; y++) {
313
    for (y = y0 + 1; y < y1; y++) {
278
        memcpy(dp, sp, copy_bytes);
314
        memcpy(dp, sp, copy_bytes);
279
        dp += screen.scanline;
315
        dp += screen.scanline;
280
    }
316
    }
281
}
317
}
282
 
318
 
283
/** Redraw viewport.
319
/** Redraw viewport.
284
 *
320
 *
285
 * @param vport Viewport to redraw
321
 * @param vport Viewport to redraw
286
 *
322
 *
287
 */
323
 */
288
static void vport_redraw(viewport_t *vport)
324
static void vport_redraw(viewport_t *vport)
289
{
325
{
290
    unsigned int row, col;
326
    unsigned int row, col;
291
 
327
 
292
    for (row = 0; row < vport->rows; row++) {
328
    for (row = 0; row < vport->rows; row++) {
293
        for (col = 0; col < vport->cols; col++) {
329
        for (col = 0; col < vport->cols; col++) {
294
            draw_vp_glyph(vport, false, col, row);
330
            draw_vp_glyph(vport, false, col, row);
295
        }
331
        }
296
    }
332
    }
297
 
333
 
298
    if (COL2X(vport->cols) < vport->width) {
334
    if (COL2X(vport->cols) < vport->width) {
299
        draw_filled_rect(
335
        draw_filled_rect(
300
            vport->x + COL2X(vport->cols), vport->y,
336
            vport->x + COL2X(vport->cols), vport->y,
301
            vport->x + vport->width, vport->y + vport->height,
337
            vport->x + vport->width, vport->y + vport->height,
302
            vport->style.bg_color);
338
            vport->attr.bg_color);
303
    }
339
    }
304
 
340
 
305
    if (ROW2Y(vport->rows) < vport->height) {
341
    if (ROW2Y(vport->rows) < vport->height) {
306
        draw_filled_rect(
342
        draw_filled_rect(
307
            vport->x, vport->y + ROW2Y(vport->rows),
343
            vport->x, vport->y + ROW2Y(vport->rows),
308
            vport->x + vport->width, vport->y + vport->height,
344
            vport->x + vport->width, vport->y + vport->height,
309
            vport->style.bg_color);
345
            vport->attr.bg_color);
310
    }
346
    }
311
}
347
}
312
 
348
 
-
 
349
static void backbuf_clear(bb_cell_t *backbuf, size_t len, uint32_t fg_color,
-
 
350
    uint32_t bg_color)
-
 
351
{
-
 
352
    unsigned i;
-
 
353
 
-
 
354
    for (i = 0; i < len; i++) {
-
 
355
        backbuf[i].glyph = 0;
-
 
356
        backbuf[i].fg_color = fg_color;
-
 
357
        backbuf[i].bg_color = bg_color;
-
 
358
    }
-
 
359
}
313
 
360
 
314
/** Clear viewport.
361
/** Clear viewport.
315
 *
362
 *
316
 * @param vport Viewport to clear
363
 * @param vport Viewport to clear
317
 *
364
 *
318
 */
365
 */
319
static void vport_clear(viewport_t *vport)
366
static void vport_clear(viewport_t *vport)
320
{
367
{
321
    memset(vport->backbuf, 0, vport->bbsize);
368
    backbuf_clear(vport->backbuf, vport->cols * vport->rows,
-
 
369
        vport->attr.fg_color, vport->attr.bg_color);
322
    vport_redraw(vport);
370
    vport_redraw(vport);
323
}
371
}
324
 
372
 
325
/** Scroll viewport by the specified number of lines.
373
/** Scroll viewport by the specified number of lines.
326
 *
374
 *
327
 * @param vport Viewport to scroll
375
 * @param vport Viewport to scroll
328
 * @param lines Number of lines to scroll
376
 * @param lines Number of lines to scroll
329
 *
377
 *
330
 */
378
 */
331
static void vport_scroll(viewport_t *vport, int lines)
379
static void vport_scroll(viewport_t *vport, int lines)
332
{
380
{
333
    unsigned int row, col;
381
    unsigned int row, col;
334
    unsigned int x, y;
382
    unsigned int x, y;
335
    uint8_t glyph;
383
    uint8_t glyph;
-
 
384
    uint32_t fg_color;
-
 
385
    uint32_t bg_color;
-
 
386
    bb_cell_t *bbp, *xbp;
336
 
387
 
337
    /*
388
    /*
338
     * Redraw.
389
     * Redraw.
339
     */
390
     */
340
 
391
 
341
    y = vport->y;
392
    y = vport->y;
342
    for (row = 0; row < vport->rows; row++) {
393
    for (row = 0; row < vport->rows; row++) {
343
        x = vport->x;
394
        x = vport->x;
344
        for (col = 0; col < vport->cols; col++) {
395
        for (col = 0; col < vport->cols; col++) {
345
            if ((row + lines >= 0) && (row + lines < vport->rows)) {
396
            if ((row + lines >= 0) && (row + lines < vport->rows)) {
346
                glyph = vport->backbuf[BB_POS(vport, col, row + lines)];
397
                xbp = &vport->backbuf[BB_POS(vport, col, row + lines)];
-
 
398
                bbp = &vport->backbuf[BB_POS(vport, col, row)];
347
 
399
 
-
 
400
                glyph = xbp->glyph;
-
 
401
                fg_color = xbp->fg_color;
-
 
402
                bg_color = xbp->bg_color;
-
 
403
 
-
 
404
                if (bbp->glyph == glyph &&
-
 
405
                    bbp->fg_color == xbp->fg_color &&
348
                if (vport->backbuf[BB_POS(vport, col, row)] == glyph) {
406
                    bbp->bg_color == xbp->bg_color) {
349
                    x += FONT_WIDTH;
407
                    x += FONT_WIDTH;
350
                    continue;
408
                    continue;
351
                }
409
                }
352
            } else {
410
            } else {
353
                glyph = 0;
411
                glyph = 0;
-
 
412
                fg_color = vport->attr.fg_color;
-
 
413
                bg_color = vport->attr.bg_color;
354
            }
414
            }
355
 
415
 
356
            (*vport->dglyph)(x, y, false, vport->glyphs, glyph,
416
            (*vport->dglyph)(x, y, false, vport->glyphs, glyph,
357
                vport->style.fg_color, vport->style.bg_color);
417
                fg_color, bg_color);
358
            x += FONT_WIDTH;
418
            x += FONT_WIDTH;
359
        }
419
        }
360
        y += FONT_SCANLINES;
420
        y += FONT_SCANLINES;
361
    }
421
    }
362
 
422
 
363
    /*
423
    /*
364
     * Scroll backbuffer.
424
     * Scroll backbuffer.
365
     */
425
     */
366
 
426
 
367
    if (lines > 0) {
427
    if (lines > 0) {
368
        memmove(vport->backbuf, vport->backbuf + vport->cols * lines,
428
        memmove(vport->backbuf, vport->backbuf + vport->cols * lines,
369
            vport->cols * (vport->rows - lines));
429
            vport->cols * (vport->rows - lines) * sizeof(bb_cell_t));
370
        memset(&vport->backbuf[BB_POS(vport, 0, vport->rows - lines)],
430
        backbuf_clear(&vport->backbuf[BB_POS(vport, 0, vport->rows - lines)],
371
            0, vport->cols * lines);
431
            vport->cols * lines, vport->attr.fg_color, vport->attr.bg_color);
372
    } else {
432
    } else {
373
        memmove(vport->backbuf - vport->cols * lines, vport->backbuf,
433
        memmove(vport->backbuf - vport->cols * lines, vport->backbuf,
374
            vport->cols * (vport->rows + lines));
434
            vport->cols * (vport->rows + lines) * sizeof(bb_cell_t));
375
        memset(vport->backbuf, 0, - vport->cols * lines);
435
        backbuf_clear(vport->backbuf, - vport->cols * lines,
-
 
436
            vport->attr.fg_color, vport->attr.bg_color);
376
    }
437
    }
377
}
438
}
378
 
439
 
379
/** Render glyphs
440
/** Render glyphs
380
 *
441
 *
381
 * Convert glyphs from device independent font
442
 * Convert glyphs from device independent font
382
 * description to current visual representation.
443
 * description to current visual representation.
383
 *
444
 *
384
 * @param vport Viewport
445
 * @param vport Viewport
385
 *
446
 *
386
 */
447
 */
387
static void render_glyphs(viewport_t* vport)
448
static void render_glyphs(viewport_t* vport)
388
{
449
{
389
    unsigned int glyph;
450
    unsigned int glyph;
390
   
451
   
391
    for (glyph = 0; glyph < FONT_GLYPHS; glyph++) {
452
    for (glyph = 0; glyph < FONT_GLYPHS; glyph++) {
392
        unsigned int y;
453
        unsigned int y;
393
       
454
       
394
        for (y = 0; y < FONT_SCANLINES; y++) {
455
        for (y = 0; y < FONT_SCANLINES; y++) {
395
            unsigned int x;
456
            unsigned int x;
396
           
457
           
397
            for (x = 0; x < FONT_WIDTH; x++) {
458
            for (x = 0; x < FONT_WIDTH; x++) {
398
                screen.rgb_conv(&vport->glyphs[GLYPH_POS(glyph, y, false) + x * screen.pixelbytes],
459
                screen.rgb_conv(&vport->glyphs[GLYPH_POS(glyph, y, false) + x * screen.pixelbytes],
399
                    (fb_font[glyph * FONT_SCANLINES + y] & (1 << (7 - x)))
460
                    (fb_font[glyph * FONT_SCANLINES + y] & (1 << (7 - x)))
400
                    ? 0xffffff : 0x000000);
461
                    ? 0xffffff : 0x000000);
401
               
462
               
402
                screen.rgb_conv(&vport->glyphs[GLYPH_POS(glyph, y, true) + x * screen.pixelbytes],
463
                screen.rgb_conv(&vport->glyphs[GLYPH_POS(glyph, y, true) + x * screen.pixelbytes],
403
                    (fb_font[glyph * FONT_SCANLINES + y] & (1 << (7 - x)))
464
                    (fb_font[glyph * FONT_SCANLINES + y] & (1 << (7 - x)))
404
                    ? 0x000000 : 0xffffff);
465
                    ? 0x000000 : 0xffffff);
405
            }
466
            }
406
        }
467
        }
407
    }
468
    }
408
   
469
   
409
    screen.rgb_conv(vport->bgpixel, vport->style.bg_color);
470
    screen.rgb_conv(vport->bgpixel, vport->attr.bg_color);
410
}
471
}
411
 
472
 
412
 
473
 
413
/** Create new viewport
474
/** Create new viewport
414
 *
475
 *
415
 * @param x      Origin of the viewport (x).
476
 * @param x      Origin of the viewport (x).
416
 * @param y      Origin of the viewport (y).
477
 * @param y      Origin of the viewport (y).
417
 * @param width  Width of the viewport.
478
 * @param width  Width of the viewport.
418
 * @param height Height of the viewport.
479
 * @param height Height of the viewport.
419
 *
480
 *
420
 * @return New viewport number.
481
 * @return New viewport number.
421
 *
482
 *
422
 */
483
 */
423
static int vport_create(unsigned int x, unsigned int y,
484
static int vport_create(unsigned int x, unsigned int y,
424
    unsigned int width, unsigned int height)
485
    unsigned int width, unsigned int height)
425
{
486
{
426
    unsigned int i;
487
    unsigned int i;
427
   
488
   
428
    for (i = 0; i < MAX_VIEWPORTS; i++) {
489
    for (i = 0; i < MAX_VIEWPORTS; i++) {
429
        if (!viewports[i].initialized)
490
        if (!viewports[i].initialized)
430
            break;
491
            break;
431
    }
492
    }
432
    if (i == MAX_VIEWPORTS)
493
    if (i == MAX_VIEWPORTS)
433
        return ELIMIT;
494
        return ELIMIT;
434
   
495
   
435
    unsigned int cols = width / FONT_WIDTH;
496
    unsigned int cols = width / FONT_WIDTH;
436
    unsigned int rows = height / FONT_SCANLINES;
497
    unsigned int rows = height / FONT_SCANLINES;
437
    unsigned int bbsize = cols * rows;
498
    unsigned int bbsize = cols * rows * sizeof(bb_cell_t);
438
    unsigned int glyphsize = 2 * FONT_GLYPHS * screen.glyphbytes;
499
    unsigned int glyphsize = 2 * FONT_GLYPHS * screen.glyphbytes;
439
    unsigned int word_size = sizeof(unsigned long);
500
    unsigned int word_size = sizeof(unsigned long);
440
   
501
   
441
    uint8_t *backbuf = (uint8_t *) malloc(bbsize);
502
    bb_cell_t *backbuf = (bb_cell_t *) malloc(bbsize);
442
    if (!backbuf)
503
    if (!backbuf)
443
        return ENOMEM;
504
        return ENOMEM;
444
   
505
   
445
    uint8_t *glyphs = (uint8_t *) malloc(glyphsize);
506
    uint8_t *glyphs = (uint8_t *) malloc(glyphsize);
446
    if (!glyphs) {
507
    if (!glyphs) {
447
        free(backbuf);
508
        free(backbuf);
448
        return ENOMEM;
509
        return ENOMEM;
449
    }
510
    }
450
   
511
   
451
    uint8_t *bgpixel = (uint8_t *) malloc(screen.pixelbytes);
512
    uint8_t *bgpixel = (uint8_t *) malloc(screen.pixelbytes);
452
    if (!bgpixel) {
513
    if (!bgpixel) {
453
        free(glyphs);
514
        free(glyphs);
454
        free(backbuf);
515
        free(backbuf);
455
        return ENOMEM;
516
        return ENOMEM;
456
    }
517
    }
457
   
518
 
458
    memset(backbuf, 0, bbsize);
519
    backbuf_clear(backbuf, cols * rows, DEFAULT_FGCOLOR, DEFAULT_BGCOLOR);
459
    memset(glyphs, 0, glyphsize);
520
    memset(glyphs, 0, glyphsize);
460
    memset(bgpixel, 0, screen.pixelbytes);
521
    memset(bgpixel, 0, screen.pixelbytes);
461
   
522
   
462
    viewports[i].x = x;
523
    viewports[i].x = x;
463
    viewports[i].y = y;
524
    viewports[i].y = y;
464
    viewports[i].width = width;
525
    viewports[i].width = width;
465
    viewports[i].height = height;
526
    viewports[i].height = height;
466
   
527
   
467
    viewports[i].cols = cols;
528
    viewports[i].cols = cols;
468
    viewports[i].rows = rows;
529
    viewports[i].rows = rows;
469
   
530
   
470
    viewports[i].style.bg_color = DEFAULT_BGCOLOR;
531
    viewports[i].attr.bg_color = DEFAULT_BGCOLOR;
471
    viewports[i].style.fg_color = DEFAULT_FGCOLOR;
532
    viewports[i].attr.fg_color = DEFAULT_FGCOLOR;
472
   
533
   
473
    viewports[i].glyphs = glyphs;
534
    viewports[i].glyphs = glyphs;
474
    viewports[i].bgpixel = bgpixel;
535
    viewports[i].bgpixel = bgpixel;
475
 
536
 
476
    /*
537
    /*
477
     * Conditions necessary  to select aligned version:
538
     * Conditions necessary  to select aligned version:
478
     *
539
     *
479
     *   - word size is divisible by pixelbytes
540
     *   - word size is divisible by pixelbytes
480
     *   - cell scanline size is divisible by word size
541
     *   - cell scanline size is divisible by word size
481
     *   - cell scanlines are word-aligned
542
     *   - cell scanlines are word-aligned
482
     */
543
     */
483
    if ((word_size % screen.pixelbytes) == 0 &&
544
    if ((word_size % screen.pixelbytes) == 0 &&
484
        (FONT_WIDTH * screen.pixelbytes) % word_size == 0 &&
545
        (FONT_WIDTH * screen.pixelbytes) % word_size == 0 &&
485
        (x * screen.pixelbytes) % word_size == 0 &&
546
        (x * screen.pixelbytes) % word_size == 0 &&
486
        screen.scanline % word_size == 0) {
547
        screen.scanline % word_size == 0) {
487
 
548
 
488
        viewports[i].dglyph = draw_glyph_aligned;
549
        viewports[i].dglyph = draw_glyph_aligned;
489
    } else {
550
    } else {
490
        viewports[i].dglyph = draw_glyph_fallback;
551
        viewports[i].dglyph = draw_glyph_fallback;
491
    }
552
    }
492
 
553
 
493
    viewports[i].cur_col = 0;
554
    viewports[i].cur_col = 0;
494
    viewports[i].cur_row = 0;
555
    viewports[i].cur_row = 0;
495
    viewports[i].cursor_active = false;
556
    viewports[i].cursor_active = false;
496
    viewports[i].cursor_shown = false;
557
    viewports[i].cursor_shown = false;
497
   
558
   
498
    viewports[i].bbsize = bbsize;
559
    viewports[i].bbsize = bbsize;
499
    viewports[i].backbuf = backbuf;
560
    viewports[i].backbuf = backbuf;
500
   
561
   
501
    viewports[i].initialized = true;
562
    viewports[i].initialized = true;
502
   
563
   
503
    render_glyphs(&viewports[i]);
564
    render_glyphs(&viewports[i]);
504
   
565
   
505
    return i;
566
    return i;
506
}
567
}
507
 
568
 
508
 
569
 
509
/** Initialize framebuffer as a chardev output device
570
/** Initialize framebuffer as a chardev output device
510
 *
571
 *
511
 * @param addr   Address of the framebuffer
572
 * @param addr   Address of the framebuffer
512
 * @param xres   Screen width in pixels
573
 * @param xres   Screen width in pixels
513
 * @param yres   Screen height in pixels
574
 * @param yres   Screen height in pixels
514
 * @param visual Bits per pixel (8, 16, 24, 32)
575
 * @param visual Bits per pixel (8, 16, 24, 32)
515
 * @param scan   Bytes per one scanline
576
 * @param scan   Bytes per one scanline
516
 *
577
 *
517
 */
578
 */
518
static bool screen_init(void *addr, unsigned int xres, unsigned int yres,
579
static bool screen_init(void *addr, unsigned int xres, unsigned int yres,
519
    unsigned int scan, unsigned int visual)
580
    unsigned int scan, unsigned int visual)
520
{
581
{
521
    switch (visual) {
582
    switch (visual) {
522
    case VISUAL_INDIRECT_8:
583
    case VISUAL_INDIRECT_8:
523
        screen.rgb_conv = rgb_323;
584
        screen.rgb_conv = rgb_323;
524
        screen.pixelbytes = 1;
585
        screen.pixelbytes = 1;
525
        break;
586
        break;
526
    case VISUAL_RGB_5_5_5:
587
    case VISUAL_RGB_5_5_5:
527
        screen.rgb_conv = rgb_555;
588
        screen.rgb_conv = rgb_555;
528
        screen.pixelbytes = 2;
589
        screen.pixelbytes = 2;
529
        break;
590
        break;
530
    case VISUAL_RGB_5_6_5:
591
    case VISUAL_RGB_5_6_5:
531
        screen.rgb_conv = rgb_565;
592
        screen.rgb_conv = rgb_565;
532
        screen.pixelbytes = 2;
593
        screen.pixelbytes = 2;
533
        break;
594
        break;
534
    case VISUAL_RGB_8_8_8:
595
    case VISUAL_RGB_8_8_8:
535
        screen.rgb_conv = rgb_888;
596
        screen.rgb_conv = rgb_888;
536
        screen.pixelbytes = 3;
597
        screen.pixelbytes = 3;
537
        break;
598
        break;
538
    case VISUAL_RGB_8_8_8_0:
599
    case VISUAL_RGB_8_8_8_0:
539
        screen.rgb_conv = rgb_888;
600
        screen.rgb_conv = rgb_888;
540
        screen.pixelbytes = 4;
601
        screen.pixelbytes = 4;
541
        break;
602
        break;
542
    case VISUAL_RGB_0_8_8_8:
603
    case VISUAL_RGB_0_8_8_8:
543
        screen.rgb_conv = rgb_0888;
604
        screen.rgb_conv = rgb_0888;
544
        screen.pixelbytes = 4;
605
        screen.pixelbytes = 4;
545
        break;
606
        break;
546
    case VISUAL_BGR_0_8_8_8:
607
    case VISUAL_BGR_0_8_8_8:
547
        screen.rgb_conv = bgr_0888;
608
        screen.rgb_conv = bgr_0888;
548
        screen.pixelbytes = 4;
609
        screen.pixelbytes = 4;
549
        break;
610
        break;
550
    default:
611
    default:
551
        return false;
612
        return false;
552
    }
613
    }
553
 
614
 
554
    screen.fb_addr = (unsigned char *) addr;
615
    screen.fb_addr = (unsigned char *) addr;
555
    screen.xres = xres;
616
    screen.xres = xres;
556
    screen.yres = yres;
617
    screen.yres = yres;
557
    screen.scanline = scan;
618
    screen.scanline = scan;
558
   
619
   
559
    screen.glyphscanline = FONT_WIDTH * screen.pixelbytes;
620
    screen.glyphscanline = FONT_WIDTH * screen.pixelbytes;
560
    screen.glyphbytes = screen.glyphscanline * FONT_SCANLINES;
621
    screen.glyphbytes = screen.glyphscanline * FONT_SCANLINES;
561
   
622
   
562
    /* Create first viewport */
623
    /* Create first viewport */
563
    vport_create(0, 0, xres, yres);
624
    vport_create(0, 0, xres, yres);
564
   
625
   
565
    return true;
626
    return true;
566
}
627
}
567
 
628
 
568
 
629
 
569
/** Draw a glyph, takes advantage of alignment.
630
/** Draw a glyph, takes advantage of alignment.
570
 *
631
 *
571
 * This version can only be used if the following conditions are met:
632
 * This version can only be used if the following conditions are met:
572
 *
633
 *
573
 *   - word size is divisible by pixelbytes
634
 *   - word size is divisible by pixelbytes
574
 *   - cell scanline size is divisible by word size
635
 *   - cell scanline size is divisible by word size
575
 *   - cell scanlines are word-aligned
636
 *   - cell scanlines are word-aligned
576
 *
637
 *
577
 * It makes use of the pre-rendered mask to process (possibly) several
638
 * It makes use of the pre-rendered mask to process (possibly) several
578
 * pixels at once (word size / pixelbytes pixels at a time are processed)
639
 * pixels at once (word size / pixelbytes pixels at a time are processed)
579
 * making it very fast. Most notably this version is not applicable at 24 bits
640
 * making it very fast. Most notably this version is not applicable at 24 bits
580
 * per pixel.
641
 * per pixel.
581
 *
642
 *
582
 * @param x     x coordinate of top-left corner on screen.
643
 * @param x     x coordinate of top-left corner on screen.
583
 * @param y     y coordinate of top-left corner on screen.
644
 * @param y     y coordinate of top-left corner on screen.
584
 * @param cursor    Draw glyph with cursor
645
 * @param cursor    Draw glyph with cursor
585
 * @param glyphs    Pointer to font bitmap.
646
 * @param glyphs    Pointer to font bitmap.
586
 * @param glyph     Code of the glyph to draw.
647
 * @param glyph     Code of the glyph to draw.
587
 * @param fg_color  Foreground color.
648
 * @param fg_color  Foreground color.
588
 * @param bg_color  Backgroudn color.
649
 * @param bg_color  Backgroudn color.
589
 */
650
 */
590
static void draw_glyph_aligned(unsigned int x, unsigned int y, bool cursor,
651
static void draw_glyph_aligned(unsigned int x, unsigned int y, bool cursor,
591
    uint8_t *glyphs, uint8_t glyph, uint32_t fg_color, uint32_t bg_color)
652
    uint8_t *glyphs, uint8_t glyph, uint32_t fg_color, uint32_t bg_color)
592
{
653
{
593
    unsigned int i, yd;
654
    unsigned int i, yd;
594
    unsigned long fg_buf, bg_buf;
655
    unsigned long fg_buf, bg_buf;
595
    unsigned long *maskp, *dp;
656
    unsigned long *maskp, *dp;
596
    unsigned long mask;
657
    unsigned long mask;
597
    unsigned int ww, d_add;
658
    unsigned int ww, d_add;
598
 
659
 
599
    /*
660
    /*
600
     * Prepare a pair of words, one filled with foreground-color
661
     * Prepare a pair of words, one filled with foreground-color
601
     * pattern and the other filled with background-color pattern.
662
     * pattern and the other filled with background-color pattern.
602
     */
663
     */
603
    for (i = 0; i < sizeof(unsigned long) / screen.pixelbytes; i++) {
664
    for (i = 0; i < sizeof(unsigned long) / screen.pixelbytes; i++) {
604
        screen.rgb_conv(&((uint8_t *)&fg_buf)[i * screen.pixelbytes],
665
        screen.rgb_conv(&((uint8_t *)&fg_buf)[i * screen.pixelbytes],
605
            fg_color);
666
            fg_color);
606
        screen.rgb_conv(&((uint8_t *)&bg_buf)[i * screen.pixelbytes],
667
        screen.rgb_conv(&((uint8_t *)&bg_buf)[i * screen.pixelbytes],
607
            bg_color);
668
            bg_color);
608
    }
669
    }
609
 
670
 
610
 
671
 
611
    /* Pointer to the current position in the mask. */
672
    /* Pointer to the current position in the mask. */
612
    maskp = (unsigned long *) &glyphs[GLYPH_POS(glyph, 0, cursor)];
673
    maskp = (unsigned long *) &glyphs[GLYPH_POS(glyph, 0, cursor)];
613
 
674
 
614
    /* Pointer to the current position on the screen. */
675
    /* Pointer to the current position on the screen. */
615
    dp = (unsigned long *) &screen.fb_addr[FB_POS(x, y)];
676
    dp = (unsigned long *) &screen.fb_addr[FB_POS(x, y)];
616
 
677
 
617
    /* Width of the character cell in words. */
678
    /* Width of the character cell in words. */
618
    ww = FONT_WIDTH * screen.pixelbytes / sizeof(unsigned long);
679
    ww = FONT_WIDTH * screen.pixelbytes / sizeof(unsigned long);
619
 
680
 
620
    /* Offset to add when moving to another screen scanline. */
681
    /* Offset to add when moving to another screen scanline. */
621
    d_add = screen.scanline - FONT_WIDTH * screen.pixelbytes;
682
    d_add = screen.scanline - FONT_WIDTH * screen.pixelbytes;
622
 
683
 
623
    for (yd = 0; yd < FONT_SCANLINES; yd++) {
684
    for (yd = 0; yd < FONT_SCANLINES; yd++) {
624
        /*
685
        /*
625
         * Now process the cell scanline, combining foreground
686
         * Now process the cell scanline, combining foreground
626
         * and background color patters using the pre-rendered mask.
687
         * and background color patters using the pre-rendered mask.
627
         */
688
         */
628
        for (i = 0; i < ww; i++) {
689
        for (i = 0; i < ww; i++) {
629
            mask = *maskp++;
690
            mask = *maskp++;
630
            *dp++ = (fg_buf & mask) | (bg_buf & ~mask);
691
            *dp++ = (fg_buf & mask) | (bg_buf & ~mask);
631
        }
692
        }
632
 
693
 
633
        /* Move to the beginning of the next scanline of the cell. */
694
        /* Move to the beginning of the next scanline of the cell. */
634
        dp = (unsigned long *) ((uint8_t *) dp + d_add);
695
        dp = (unsigned long *) ((uint8_t *) dp + d_add);
635
    }
696
    }
636
}
697
}
637
 
698
 
638
/** Draw a glyph, fallback version.
699
/** Draw a glyph, fallback version.
639
 *
700
 *
640
 * This version does not make use of the pre-rendered mask, it uses
701
 * This version does not make use of the pre-rendered mask, it uses
641
 * the font bitmap directly. It works always, but it is slower.
702
 * the font bitmap directly. It works always, but it is slower.
642
 *
703
 *
643
 * @param x     x coordinate of top-left corner on screen.
704
 * @param x     x coordinate of top-left corner on screen.
644
 * @param y     y coordinate of top-left corner on screen.
705
 * @param y     y coordinate of top-left corner on screen.
645
 * @param cursor    Draw glyph with cursor
706
 * @param cursor    Draw glyph with cursor
646
 * @param glyphs    Pointer to font bitmap.
707
 * @param glyphs    Pointer to font bitmap.
647
 * @param glyph     Code of the glyph to draw.
708
 * @param glyph     Code of the glyph to draw.
648
 * @param fg_color  Foreground color.
709
 * @param fg_color  Foreground color.
649
 * @param bg_color  Backgroudn color.
710
 * @param bg_color  Backgroudn color.
650
 */
711
 */
651
void draw_glyph_fallback(unsigned int x, unsigned int y, bool cursor,
712
void draw_glyph_fallback(unsigned int x, unsigned int y, bool cursor,
652
    uint8_t *glyphs, uint8_t glyph, uint32_t fg_color, uint32_t bg_color)
713
    uint8_t *glyphs, uint8_t glyph, uint32_t fg_color, uint32_t bg_color)
653
{
714
{
654
    unsigned int i, j, yd;
715
    unsigned int i, j, yd;
655
    uint8_t fg_buf[4], bg_buf[4];
716
    uint8_t fg_buf[4], bg_buf[4];
656
    uint8_t *dp, *sp;
717
    uint8_t *dp, *sp;
657
    unsigned int d_add;
718
    unsigned int d_add;
658
    uint8_t b;
719
    uint8_t b;
659
 
720
 
660
    /* Pre-render 1x the foreground and background color pixels. */
721
    /* Pre-render 1x the foreground and background color pixels. */
661
    if (cursor) {
722
    if (cursor) {
662
        screen.rgb_conv(fg_buf, bg_color);
723
        screen.rgb_conv(fg_buf, bg_color);
663
        screen.rgb_conv(bg_buf, fg_color);
724
        screen.rgb_conv(bg_buf, fg_color);
664
    } else {
725
    } else {
665
        screen.rgb_conv(fg_buf, fg_color);
726
        screen.rgb_conv(fg_buf, fg_color);
666
        screen.rgb_conv(bg_buf, bg_color);
727
        screen.rgb_conv(bg_buf, bg_color);
667
    }
728
    }
668
 
729
 
669
    /* Pointer to the current position on the screen. */
730
    /* Pointer to the current position on the screen. */
670
    dp = (uint8_t *) &screen.fb_addr[FB_POS(x, y)];
731
    dp = (uint8_t *) &screen.fb_addr[FB_POS(x, y)];
671
 
732
 
672
    /* Offset to add when moving to another screen scanline. */
733
    /* Offset to add when moving to another screen scanline. */
673
    d_add = screen.scanline - FONT_WIDTH * screen.pixelbytes;
734
    d_add = screen.scanline - FONT_WIDTH * screen.pixelbytes;
674
 
735
 
675
    for (yd = 0; yd < FONT_SCANLINES; yd++) {
736
    for (yd = 0; yd < FONT_SCANLINES; yd++) {
676
        /* Byte containing bits of the glyph scanline. */
737
        /* Byte containing bits of the glyph scanline. */
677
        b = fb_font[glyph * FONT_SCANLINES + yd];
738
        b = fb_font[glyph * FONT_SCANLINES + yd];
678
 
739
 
679
        for (i = 0; i < FONT_WIDTH; i++) {
740
        for (i = 0; i < FONT_WIDTH; i++) {
680
            /* Choose color based on the current bit. */
741
            /* Choose color based on the current bit. */
681
            sp = (b & 0x80) ? fg_buf : bg_buf;
742
            sp = (b & 0x80) ? fg_buf : bg_buf;
682
 
743
 
683
            /* Copy the pixel. */
744
            /* Copy the pixel. */
684
            for (j = 0; j < screen.pixelbytes; j++) {
745
            for (j = 0; j < screen.pixelbytes; j++) {
685
                *dp++ = *sp++;
746
                *dp++ = *sp++;
686
            }
747
            }
687
 
748
 
688
            /* Move to the next bit. */
749
            /* Move to the next bit. */
689
            b = b << 1;
750
            b = b << 1;
690
        }
751
        }
691
       
752
       
692
        /* Move to the beginning of the next scanline of the cell. */
753
        /* Move to the beginning of the next scanline of the cell. */
693
        dp += d_add;
754
        dp += d_add;
694
    }
755
    }
695
}
756
}
696
 
757
 
697
/** Draw glyph at specified position in viewport.
758
/** Draw glyph at specified position in viewport.
698
 *
759
 *
699
 * @param vport  Viewport identification
760
 * @param vport  Viewport identification
700
 * @param cursor Draw glyph with cursor
761
 * @param cursor Draw glyph with cursor
701
 * @param col    Screen position relative to viewport
762
 * @param col    Screen position relative to viewport
702
 * @param row    Screen position relative to viewport
763
 * @param row    Screen position relative to viewport
703
 *
764
 *
704
 */
765
 */
705
static void draw_vp_glyph(viewport_t *vport, bool cursor, unsigned int col,
766
static void draw_vp_glyph(viewport_t *vport, bool cursor, unsigned int col,
706
    unsigned int row)
767
    unsigned int row)
707
{
768
{
708
    unsigned int x = vport->x + COL2X(col);
769
    unsigned int x = vport->x + COL2X(col);
709
    unsigned int y = vport->y + ROW2Y(row);
770
    unsigned int y = vport->y + ROW2Y(row);
-
 
771
 
710
    uint8_t glyph;
772
    uint8_t glyph;
-
 
773
    uint32_t fg_color;
-
 
774
    uint32_t bg_color;
711
   
775
   
712
    glyph = vport->backbuf[BB_POS(vport, col, row)];
776
    glyph = vport->backbuf[BB_POS(vport, col, row)].glyph;
-
 
777
    fg_color = vport->backbuf[BB_POS(vport, col, row)].fg_color;
-
 
778
    bg_color = vport->backbuf[BB_POS(vport, col, row)].bg_color;
713
 
779
 
714
    (*vport->dglyph)(x, y, cursor, vport->glyphs, glyph,
780
    (*vport->dglyph)(x, y, cursor, vport->glyphs, glyph,
715
        vport->style.fg_color, vport->style.bg_color);
781
        fg_color, bg_color);
716
}
782
}
717
 
783
 
718
/** Hide cursor if it is shown
784
/** Hide cursor if it is shown
719
 *
785
 *
720
 */
786
 */
721
static void cursor_hide(viewport_t *vport)
787
static void cursor_hide(viewport_t *vport)
722
{
788
{
723
    if ((vport->cursor_active) && (vport->cursor_shown)) {
789
    if ((vport->cursor_active) && (vport->cursor_shown)) {
724
        draw_vp_glyph(vport, false, vport->cur_col, vport->cur_row);
790
        draw_vp_glyph(vport, false, vport->cur_col, vport->cur_row);
725
        vport->cursor_shown = false;
791
        vport->cursor_shown = false;
726
    }
792
    }
727
}
793
}
728
 
794
 
729
 
795
 
730
/** Show cursor if cursor showing is enabled
796
/** Show cursor if cursor showing is enabled
731
 *
797
 *
732
 */
798
 */
733
static void cursor_show(viewport_t *vport)
799
static void cursor_show(viewport_t *vport)
734
{
800
{
735
    /* Do not check for cursor_shown */
801
    /* Do not check for cursor_shown */
736
    if (vport->cursor_active) {
802
    if (vport->cursor_active) {
737
        draw_vp_glyph(vport, true, vport->cur_col, vport->cur_row);
803
        draw_vp_glyph(vport, true, vport->cur_col, vport->cur_row);
738
        vport->cursor_shown = true;
804
        vport->cursor_shown = true;
739
    }
805
    }
740
}
806
}
741
 
807
 
742
 
808
 
743
/** Invert cursor, if it is enabled
809
/** Invert cursor, if it is enabled
744
 *
810
 *
745
 */
811
 */
746
static void cursor_blink(viewport_t *vport)
812
static void cursor_blink(viewport_t *vport)
747
{
813
{
748
    if (vport->cursor_shown)
814
    if (vport->cursor_shown)
749
        cursor_hide(vport);
815
        cursor_hide(vport);
750
    else
816
    else
751
        cursor_show(vport);
817
        cursor_show(vport);
752
}
818
}
753
 
819
 
754
 
820
 
755
/** Draw character at given position relative to viewport
821
/** Draw character at given position relative to viewport
756
 *
822
 *
757
 * @param vport  Viewport identification
823
 * @param vport  Viewport identification
758
 * @param c      Character to draw
824
 * @param c      Character to draw
759
 * @param col    Screen position relative to viewport
825
 * @param col    Screen position relative to viewport
760
 * @param row    Screen position relative to viewport
826
 * @param row    Screen position relative to viewport
761
 *
827
 *
762
 */
828
 */
763
static void draw_char(viewport_t *vport, uint8_t c, unsigned int col, unsigned int row)
829
static void draw_char(viewport_t *vport, uint8_t c, unsigned int col, unsigned int row)
764
{
830
{
-
 
831
    bb_cell_t *bbp;
-
 
832
 
765
    /* Do not hide cursor if we are going to overwrite it */
833
    /* Do not hide cursor if we are going to overwrite it */
766
    if ((vport->cursor_active) && (vport->cursor_shown) &&
834
    if ((vport->cursor_active) && (vport->cursor_shown) &&
767
        ((vport->cur_col != col) || (vport->cur_row != row)))
835
        ((vport->cur_col != col) || (vport->cur_row != row)))
768
        cursor_hide(vport);
836
        cursor_hide(vport);
769
   
837
 
770
    vport->backbuf[BB_POS(vport, col, row)] = c;
838
    bbp = &vport->backbuf[BB_POS(vport, col, row)];
-
 
839
    bbp->glyph = c;
-
 
840
    bbp->fg_color = vport->attr.fg_color;
-
 
841
    bbp->bg_color = vport->attr.bg_color;
-
 
842
 
771
    draw_vp_glyph(vport, false, col, row);
843
    draw_vp_glyph(vport, false, col, row);
772
   
844
   
773
    vport->cur_col = col;
845
    vport->cur_col = col;
774
    vport->cur_row = row;
846
    vport->cur_row = row;
775
   
847
   
776
    vport->cur_col++;
848
    vport->cur_col++;
777
    if (vport->cur_col >= vport->cols) {
849
    if (vport->cur_col >= vport->cols) {
778
        vport->cur_col = 0;
850
        vport->cur_col = 0;
779
        vport->cur_row++;
851
        vport->cur_row++;
780
        if (vport->cur_row >= vport->rows)
852
        if (vport->cur_row >= vport->rows)
781
            vport->cur_row--;
853
            vport->cur_row--;
782
    }
854
    }
783
   
855
   
784
    cursor_show(vport);
856
    cursor_show(vport);
785
}
857
}
786
 
858
 
787
 
859
 
788
/** Draw text data to viewport
860
/** Draw text data to viewport
789
 *
861
 *
790
 * @param vport Viewport id
862
 * @param vport Viewport id
791
 * @param data  Text data fitting exactly into viewport
863
 * @param data  Text data fitting exactly into viewport
792
 *
864
 *
793
 */
865
 */
794
static void draw_text_data(viewport_t *vport, keyfield_t *data)
866
static void draw_text_data(viewport_t *vport, keyfield_t *data)
795
{
867
{
796
    unsigned int i;
868
    unsigned int i;
-
 
869
    bb_cell_t *bbp;
-
 
870
    attrs_t *a;
-
 
871
    attr_rgb_t rgb;
797
   
872
   
798
    for (i = 0; i < vport->cols * vport->rows; i++) {
873
    for (i = 0; i < vport->cols * vport->rows; i++) {
799
        unsigned int col = i % vport->cols;
874
        unsigned int col = i % vport->cols;
800
        unsigned int row = i / vport->cols;
875
        unsigned int row = i / vport->cols;
801
       
876
       
802
        uint8_t glyph = vport->backbuf[BB_POS(vport, col, row)];
877
        bbp = &vport->backbuf[BB_POS(vport, col, row)];
803
       
-
 
804
        // TODO: use data[i].style
878
        uint8_t glyph = bbp->glyph;
805
       
879
 
806
        if (glyph != data[i].character) {
880
        if (glyph != data[i].character) {
807
            vport->backbuf[BB_POS(vport, col, row)] = data[i].character;
881
            bbp->glyph = data[i].character;
-
 
882
            a = &data[i].attrs;
-
 
883
 
-
 
884
            switch (a->t) {
-
 
885
            case at_style:
-
 
886
                rgb_from_style(&rgb, a->a.s.style);
-
 
887
                break;
-
 
888
            case at_idx:
-
 
889
                rgb_from_idx(&rgb, a->a.i.fg_color,
-
 
890
                    a->a.i.bg_color, a->a.i.flags);
-
 
891
                break;
-
 
892
            case at_rgb:
-
 
893
                rgb = a->a.r;
-
 
894
                break;
-
 
895
            }
-
 
896
 
-
 
897
            bbp->fg_color = rgb.fg_color;
-
 
898
            bbp->bg_color = rgb.bg_color;
-
 
899
 
808
            draw_vp_glyph(vport, false, col, row);
900
            draw_vp_glyph(vport, false, col, row);
809
        }
901
        }
810
    }
902
    }
811
    cursor_show(vport);
903
    cursor_show(vport);
812
}
904
}
813
 
905
 
814
 
906
 
815
static void putpixel_pixmap(void *data, unsigned int x, unsigned int y, uint32_t color)
907
static void putpixel_pixmap(void *data, unsigned int x, unsigned int y, uint32_t color)
816
{
908
{
817
    int pm = *((int *) data);
909
    int pm = *((int *) data);
818
    pixmap_t *pmap = &pixmaps[pm];
910
    pixmap_t *pmap = &pixmaps[pm];
819
    unsigned int pos = (y * pmap->width + x) * screen.pixelbytes;
911
    unsigned int pos = (y * pmap->width + x) * screen.pixelbytes;
820
   
912
   
821
    screen.rgb_conv(&pmap->data[pos], color);
913
    screen.rgb_conv(&pmap->data[pos], color);
822
}
914
}
823
 
915
 
824
 
916
 
825
static void putpixel(void *data, unsigned int x, unsigned int y, uint32_t color)
917
static void putpixel(void *data, unsigned int x, unsigned int y, uint32_t color)
826
{
918
{
827
    viewport_t *vport = (viewport_t *) data;
919
    viewport_t *vport = (viewport_t *) data;
828
    unsigned int dx = vport->x + x;
920
    unsigned int dx = vport->x + x;
829
    unsigned int dy = vport->y + y;
921
    unsigned int dy = vport->y + y;
830
   
922
   
831
    screen.rgb_conv(&screen.fb_addr[FB_POS(dx, dy)], color);
923
    screen.rgb_conv(&screen.fb_addr[FB_POS(dx, dy)], color);
832
}
924
}
833
 
925
 
834
 
926
 
835
/** Return first free pixmap
927
/** Return first free pixmap
836
 *
928
 *
837
 */
929
 */
838
static int find_free_pixmap(void)
930
static int find_free_pixmap(void)
839
{
931
{
840
    unsigned int i;
932
    unsigned int i;
841
   
933
   
842
    for (i = 0; i < MAX_PIXMAPS; i++)
934
    for (i = 0; i < MAX_PIXMAPS; i++)
843
        if (!pixmaps[i].data)
935
        if (!pixmaps[i].data)
844
            return i;
936
            return i;
845
   
937
   
846
    return -1;
938
    return -1;
847
}
939
}
848
 
940
 
849
 
941
 
850
/** Create a new pixmap and return appropriate ID
942
/** Create a new pixmap and return appropriate ID
851
 *
943
 *
852
 */
944
 */
853
static int shm2pixmap(unsigned char *shm, size_t size)
945
static int shm2pixmap(unsigned char *shm, size_t size)
854
{
946
{
855
    int pm;
947
    int pm;
856
    pixmap_t *pmap;
948
    pixmap_t *pmap;
857
   
949
   
858
    pm = find_free_pixmap();
950
    pm = find_free_pixmap();
859
    if (pm == -1)
951
    if (pm == -1)
860
        return ELIMIT;
952
        return ELIMIT;
861
   
953
   
862
    pmap = &pixmaps[pm];
954
    pmap = &pixmaps[pm];
863
   
955
   
864
    if (ppm_get_data(shm, size, &pmap->width, &pmap->height))
956
    if (ppm_get_data(shm, size, &pmap->width, &pmap->height))
865
        return EINVAL;
957
        return EINVAL;
866
   
958
   
867
    pmap->data = malloc(pmap->width * pmap->height * screen.pixelbytes);
959
    pmap->data = malloc(pmap->width * pmap->height * screen.pixelbytes);
868
    if (!pmap->data)
960
    if (!pmap->data)
869
        return ENOMEM;
961
        return ENOMEM;
870
   
962
   
871
    ppm_draw(shm, size, 0, 0, pmap->width, pmap->height, putpixel_pixmap, (void *) &pm);
963
    ppm_draw(shm, size, 0, 0, pmap->width, pmap->height, putpixel_pixmap, (void *) &pm);
872
   
964
   
873
    return pm;
965
    return pm;
874
}
966
}
875
 
967
 
876
 
968
 
877
/** Handle shared memory communication calls
969
/** Handle shared memory communication calls
878
 *
970
 *
879
 * Protocol for drawing pixmaps:
971
 * Protocol for drawing pixmaps:
880
 * - FB_PREPARE_SHM(client shm identification)
972
 * - FB_PREPARE_SHM(client shm identification)
881
 * - IPC_M_AS_AREA_SEND
973
 * - IPC_M_AS_AREA_SEND
882
 * - FB_DRAW_PPM(startx, starty)
974
 * - FB_DRAW_PPM(startx, starty)
883
 * - FB_DROP_SHM
975
 * - FB_DROP_SHM
884
 *
976
 *
885
 * Protocol for text drawing
977
 * Protocol for text drawing
886
 * - IPC_M_AS_AREA_SEND
978
 * - IPC_M_AS_AREA_SEND
887
 * - FB_DRAW_TEXT_DATA
979
 * - FB_DRAW_TEXT_DATA
888
 *
980
 *
889
 * @param callid Callid of the current call
981
 * @param callid Callid of the current call
890
 * @param call   Current call data
982
 * @param call   Current call data
891
 * @param vp     Active viewport
983
 * @param vp     Active viewport
892
 *
984
 *
893
 * @return false if the call was not handled byt this function, true otherwise
985
 * @return false if the call was not handled byt this function, true otherwise
894
 *
986
 *
895
 * Note: this function is not threads safe, you would have
987
 * Note: this function is not threads safe, you would have
896
 * to redefine static variables with __thread
988
 * to redefine static variables with __thread
897
 *
989
 *
898
 */
990
 */
899
static bool shm_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
991
static bool shm_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
900
{
992
{
901
    static keyfield_t *interbuffer = NULL;
993
    static keyfield_t *interbuffer = NULL;
902
    static size_t intersize = 0;
994
    static size_t intersize = 0;
903
   
995
   
904
    static unsigned char *shm = NULL;
996
    static unsigned char *shm = NULL;
905
    static ipcarg_t shm_id = 0;
997
    static ipcarg_t shm_id = 0;
906
    static size_t shm_size;
998
    static size_t shm_size;
907
   
999
   
908
    bool handled = true;
1000
    bool handled = true;
909
    int retval = EOK;
1001
    int retval = EOK;
910
    viewport_t *vport = &viewports[vp];
1002
    viewport_t *vport = &viewports[vp];
911
    unsigned int x;
1003
    unsigned int x;
912
    unsigned int y;
1004
    unsigned int y;
913
   
1005
   
914
    switch (IPC_GET_METHOD(*call)) {
1006
    switch (IPC_GET_METHOD(*call)) {
915
    case IPC_M_SHARE_OUT:
1007
    case IPC_M_SHARE_OUT:
916
        /* We accept one area for data interchange */
1008
        /* We accept one area for data interchange */
917
        if (IPC_GET_ARG1(*call) == shm_id) {
1009
        if (IPC_GET_ARG1(*call) == shm_id) {
918
            void *dest = as_get_mappable_page(IPC_GET_ARG2(*call));
1010
            void *dest = as_get_mappable_page(IPC_GET_ARG2(*call));
919
            shm_size = IPC_GET_ARG2(*call);
1011
            shm_size = IPC_GET_ARG2(*call);
920
            if (!ipc_answer_1(callid, EOK, (sysarg_t) dest))
1012
            if (!ipc_answer_1(callid, EOK, (sysarg_t) dest))
921
                shm = dest;
1013
                shm = dest;
922
            else
1014
            else
923
                shm_id = 0;
1015
                shm_id = 0;
924
           
1016
           
925
            if (shm[0] != 'P')
1017
            if (shm[0] != 'P')
926
                return false;
1018
                return false;
927
           
1019
           
928
            return true;
1020
            return true;
929
        } else {
1021
        } else {
930
            intersize = IPC_GET_ARG2(*call);
1022
            intersize = IPC_GET_ARG2(*call);
931
            receive_comm_area(callid, call, (void *) &interbuffer);
1023
            receive_comm_area(callid, call, (void *) &interbuffer);
932
        }
1024
        }
933
        return true;
1025
        return true;
934
    case FB_PREPARE_SHM:
1026
    case FB_PREPARE_SHM:
935
        if (shm_id)
1027
        if (shm_id)
936
            retval = EBUSY;
1028
            retval = EBUSY;
937
        else
1029
        else
938
            shm_id = IPC_GET_ARG1(*call);
1030
            shm_id = IPC_GET_ARG1(*call);
939
        break;
1031
        break;
940
       
1032
       
941
    case FB_DROP_SHM:
1033
    case FB_DROP_SHM:
942
        if (shm) {
1034
        if (shm) {
943
            as_area_destroy(shm);
1035
            as_area_destroy(shm);
944
            shm = NULL;
1036
            shm = NULL;
945
        }
1037
        }
946
        shm_id = 0;
1038
        shm_id = 0;
947
        break;
1039
        break;
948
       
1040
       
949
    case FB_SHM2PIXMAP:
1041
    case FB_SHM2PIXMAP:
950
        if (!shm) {
1042
        if (!shm) {
951
            retval = EINVAL;
1043
            retval = EINVAL;
952
            break;
1044
            break;
953
        }
1045
        }
954
        retval = shm2pixmap(shm, shm_size);
1046
        retval = shm2pixmap(shm, shm_size);
955
        break;
1047
        break;
956
    case FB_DRAW_PPM:
1048
    case FB_DRAW_PPM:
957
        if (!shm) {
1049
        if (!shm) {
958
            retval = EINVAL;
1050
            retval = EINVAL;
959
            break;
1051
            break;
960
        }
1052
        }
961
        x = IPC_GET_ARG1(*call);
1053
        x = IPC_GET_ARG1(*call);
962
        y = IPC_GET_ARG2(*call);
1054
        y = IPC_GET_ARG2(*call);
963
       
1055
       
964
        if ((x > vport->width) || (y > vport->height)) {
1056
        if ((x > vport->width) || (y > vport->height)) {
965
            retval = EINVAL;
1057
            retval = EINVAL;
966
            break;
1058
            break;
967
        }
1059
        }
968
       
1060
       
969
        ppm_draw(shm, shm_size, IPC_GET_ARG1(*call),
1061
        ppm_draw(shm, shm_size, IPC_GET_ARG1(*call),
970
            IPC_GET_ARG2(*call), vport->width - x, vport->height - y, putpixel, (void *) vport);
1062
            IPC_GET_ARG2(*call), vport->width - x, vport->height - y, putpixel, (void *) vport);
971
        break;
1063
        break;
972
    case FB_DRAW_TEXT_DATA:
1064
    case FB_DRAW_TEXT_DATA:
973
        if (!interbuffer) {
1065
        if (!interbuffer) {
974
            retval = EINVAL;
1066
            retval = EINVAL;
975
            break;
1067
            break;
976
        }
1068
        }
977
        if (intersize < vport->cols * vport->rows * sizeof(*interbuffer)) {
1069
        if (intersize < vport->cols * vport->rows * sizeof(*interbuffer)) {
978
            retval = EINVAL;
1070
            retval = EINVAL;
979
            break;
1071
            break;
980
        }
1072
        }
981
        draw_text_data(vport, interbuffer);
1073
        draw_text_data(vport, interbuffer);
982
        break;
1074
        break;
983
    default:
1075
    default:
984
        handled = false;
1076
        handled = false;
985
    }
1077
    }
986
   
1078
   
987
    if (handled)
1079
    if (handled)
988
        ipc_answer_0(callid, retval);
1080
        ipc_answer_0(callid, retval);
989
    return handled;
1081
    return handled;
990
}
1082
}
991
 
1083
 
992
 
1084
 
993
static void copy_vp_to_pixmap(viewport_t *vport, pixmap_t *pmap)
1085
static void copy_vp_to_pixmap(viewport_t *vport, pixmap_t *pmap)
994
{
1086
{
995
    unsigned int width = vport->width;
1087
    unsigned int width = vport->width;
996
    unsigned int height = vport->height;
1088
    unsigned int height = vport->height;
997
   
1089
   
998
    if (width + vport->x > screen.xres)
1090
    if (width + vport->x > screen.xres)
999
        width = screen.xres - vport->x;
1091
        width = screen.xres - vport->x;
1000
    if (height + vport->y > screen.yres)
1092
    if (height + vport->y > screen.yres)
1001
        height = screen.yres - vport->y;
1093
        height = screen.yres - vport->y;
1002
   
1094
   
1003
    unsigned int realwidth = pmap->width <= width ? pmap->width : width;
1095
    unsigned int realwidth = pmap->width <= width ? pmap->width : width;
1004
    unsigned int realheight = pmap->height <= height ? pmap->height : height;
1096
    unsigned int realheight = pmap->height <= height ? pmap->height : height;
1005
   
1097
   
1006
    unsigned int srcrowsize = vport->width * screen.pixelbytes;
1098
    unsigned int srcrowsize = vport->width * screen.pixelbytes;
1007
    unsigned int realrowsize = realwidth * screen.pixelbytes;
1099
    unsigned int realrowsize = realwidth * screen.pixelbytes;
1008
   
1100
   
1009
    unsigned int y;
1101
    unsigned int y;
1010
    for (y = 0; y < realheight; y++) {
1102
    for (y = 0; y < realheight; y++) {
1011
        unsigned int tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
1103
        unsigned int tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
1012
        memcpy(pmap->data + srcrowsize * y, screen.fb_addr + tmp, realrowsize);
1104
        memcpy(pmap->data + srcrowsize * y, screen.fb_addr + tmp, realrowsize);
1013
    }
1105
    }
1014
}
1106
}
1015
 
1107
 
1016
 
1108
 
1017
/** Save viewport to pixmap
1109
/** Save viewport to pixmap
1018
 *
1110
 *
1019
 */
1111
 */
1020
static int save_vp_to_pixmap(viewport_t *vport)
1112
static int save_vp_to_pixmap(viewport_t *vport)
1021
{
1113
{
1022
    int pm;
1114
    int pm;
1023
    pixmap_t *pmap;
1115
    pixmap_t *pmap;
1024
   
1116
   
1025
    pm = find_free_pixmap();
1117
    pm = find_free_pixmap();
1026
    if (pm == -1)
1118
    if (pm == -1)
1027
        return ELIMIT;
1119
        return ELIMIT;
1028
   
1120
   
1029
    pmap = &pixmaps[pm];
1121
    pmap = &pixmaps[pm];
1030
    pmap->data = malloc(screen.pixelbytes * vport->width * vport->height);
1122
    pmap->data = malloc(screen.pixelbytes * vport->width * vport->height);
1031
    if (!pmap->data)
1123
    if (!pmap->data)
1032
        return ENOMEM;
1124
        return ENOMEM;
1033
   
1125
   
1034
    pmap->width = vport->width;
1126
    pmap->width = vport->width;
1035
    pmap->height = vport->height;
1127
    pmap->height = vport->height;
1036
   
1128
   
1037
    copy_vp_to_pixmap(vport, pmap);
1129
    copy_vp_to_pixmap(vport, pmap);
1038
   
1130
   
1039
    return pm;
1131
    return pm;
1040
}
1132
}
1041
 
1133
 
1042
 
1134
 
1043
/** Draw pixmap on screen
1135
/** Draw pixmap on screen
1044
 *
1136
 *
1045
 * @param vp Viewport to draw on
1137
 * @param vp Viewport to draw on
1046
 * @param pm Pixmap identifier
1138
 * @param pm Pixmap identifier
1047
 *
1139
 *
1048
 */
1140
 */
1049
static int draw_pixmap(int vp, int pm)
1141
static int draw_pixmap(int vp, int pm)
1050
{
1142
{
1051
    pixmap_t *pmap = &pixmaps[pm];
1143
    pixmap_t *pmap = &pixmaps[pm];
1052
    viewport_t *vport = &viewports[vp];
1144
    viewport_t *vport = &viewports[vp];
1053
   
1145
   
1054
    unsigned int width = vport->width;
1146
    unsigned int width = vport->width;
1055
    unsigned int height = vport->height;
1147
    unsigned int height = vport->height;
1056
   
1148
   
1057
    if (width + vport->x > screen.xres)
1149
    if (width + vport->x > screen.xres)
1058
        width = screen.xres - vport->x;
1150
        width = screen.xres - vport->x;
1059
    if (height + vport->y > screen.yres)
1151
    if (height + vport->y > screen.yres)
1060
        height = screen.yres - vport->y;
1152
        height = screen.yres - vport->y;
1061
   
1153
   
1062
    if (!pmap->data)
1154
    if (!pmap->data)
1063
        return EINVAL;
1155
        return EINVAL;
1064
   
1156
   
1065
    unsigned int realwidth = pmap->width <= width ? pmap->width : width;
1157
    unsigned int realwidth = pmap->width <= width ? pmap->width : width;
1066
    unsigned int realheight = pmap->height <= height ? pmap->height : height;
1158
    unsigned int realheight = pmap->height <= height ? pmap->height : height;
1067
   
1159
   
1068
    unsigned int srcrowsize = vport->width * screen.pixelbytes;
1160
    unsigned int srcrowsize = vport->width * screen.pixelbytes;
1069
    unsigned int realrowsize = realwidth * screen.pixelbytes;
1161
    unsigned int realrowsize = realwidth * screen.pixelbytes;
1070
   
1162
   
1071
    unsigned int y;
1163
    unsigned int y;
1072
    for (y = 0; y < realheight; y++) {
1164
    for (y = 0; y < realheight; y++) {
1073
        unsigned int tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
1165
        unsigned int tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
1074
        memcpy(screen.fb_addr + tmp, pmap->data + y * srcrowsize, realrowsize);
1166
        memcpy(screen.fb_addr + tmp, pmap->data + y * srcrowsize, realrowsize);
1075
    }
1167
    }
1076
   
1168
   
1077
    return EOK;
1169
    return EOK;
1078
}
1170
}
1079
 
1171
 
1080
 
1172
 
1081
/** Tick animation one step forward
1173
/** Tick animation one step forward
1082
 *
1174
 *
1083
 */
1175
 */
1084
static void anims_tick(void)
1176
static void anims_tick(void)
1085
{
1177
{
1086
    unsigned int i;
1178
    unsigned int i;
1087
    static int counts = 0;
1179
    static int counts = 0;
1088
   
1180
   
1089
    /* Limit redrawing */
1181
    /* Limit redrawing */
1090
    counts = (counts + 1) % 8;
1182
    counts = (counts + 1) % 8;
1091
    if (counts)
1183
    if (counts)
1092
        return;
1184
        return;
1093
 
1185
 
1094
    for (i = 0; i < MAX_ANIMATIONS; i++) {
1186
    for (i = 0; i < MAX_ANIMATIONS; i++) {
1095
        if ((!animations[i].animlen) || (!animations[i].initialized) ||
1187
        if ((!animations[i].animlen) || (!animations[i].initialized) ||
1096
            (!animations[i].enabled))
1188
            (!animations[i].enabled))
1097
            continue;
1189
            continue;
1098
       
1190
       
1099
        draw_pixmap(animations[i].vp, animations[i].pixmaps[animations[i].pos]);
1191
        draw_pixmap(animations[i].vp, animations[i].pixmaps[animations[i].pos]);
1100
        animations[i].pos = (animations[i].pos + 1) % animations[i].animlen;
1192
        animations[i].pos = (animations[i].pos + 1) % animations[i].animlen;
1101
    }
1193
    }
1102
}
1194
}
1103
 
1195
 
1104
 
1196
 
1105
static unsigned int pointer_x;
1197
static unsigned int pointer_x;
1106
static unsigned int pointer_y;
1198
static unsigned int pointer_y;
1107
static bool pointer_shown, pointer_enabled;
1199
static bool pointer_shown, pointer_enabled;
1108
static int pointer_vport = -1;
1200
static int pointer_vport = -1;
1109
static int pointer_pixmap = -1;
1201
static int pointer_pixmap = -1;
1110
 
1202
 
1111
 
1203
 
1112
static void mouse_show(void)
1204
static void mouse_show(void)
1113
{
1205
{
1114
    int i, j;
1206
    int i, j;
1115
    int visibility;
1207
    int visibility;
1116
    int color;
1208
    int color;
1117
    int bytepos;
1209
    int bytepos;
1118
   
1210
   
1119
    if ((pointer_shown) || (!pointer_enabled))
1211
    if ((pointer_shown) || (!pointer_enabled))
1120
        return;
1212
        return;
1121
   
1213
   
1122
    /* Save image under the pointer. */
1214
    /* Save image under the pointer. */
1123
    if (pointer_vport == -1) {
1215
    if (pointer_vport == -1) {
1124
        pointer_vport = vport_create(pointer_x, pointer_y, pointer_width, pointer_height);
1216
        pointer_vport = vport_create(pointer_x, pointer_y, pointer_width, pointer_height);
1125
        if (pointer_vport < 0)
1217
        if (pointer_vport < 0)
1126
            return;
1218
            return;
1127
    } else {
1219
    } else {
1128
        viewports[pointer_vport].x = pointer_x;
1220
        viewports[pointer_vport].x = pointer_x;
1129
        viewports[pointer_vport].y = pointer_y;
1221
        viewports[pointer_vport].y = pointer_y;
1130
    }
1222
    }
1131
   
1223
   
1132
    if (pointer_pixmap == -1)
1224
    if (pointer_pixmap == -1)
1133
        pointer_pixmap = save_vp_to_pixmap(&viewports[pointer_vport]);
1225
        pointer_pixmap = save_vp_to_pixmap(&viewports[pointer_vport]);
1134
    else
1226
    else
1135
        copy_vp_to_pixmap(&viewports[pointer_vport], &pixmaps[pointer_pixmap]);
1227
        copy_vp_to_pixmap(&viewports[pointer_vport], &pixmaps[pointer_pixmap]);
1136
   
1228
   
1137
    /* Draw mouse pointer. */
1229
    /* Draw mouse pointer. */
1138
    for (i = 0; i < pointer_height; i++)
1230
    for (i = 0; i < pointer_height; i++)
1139
        for (j = 0; j < pointer_width; j++) {
1231
        for (j = 0; j < pointer_width; j++) {
1140
            bytepos = i * ((pointer_width - 1) / 8 + 1) + j / 8;
1232
            bytepos = i * ((pointer_width - 1) / 8 + 1) + j / 8;
1141
            visibility = pointer_mask_bits[bytepos] &
1233
            visibility = pointer_mask_bits[bytepos] &
1142
                (1 << (j % 8));
1234
                (1 << (j % 8));
1143
            if (visibility) {
1235
            if (visibility) {
1144
                color = pointer_bits[bytepos] &
1236
                color = pointer_bits[bytepos] &
1145
                    (1 << (j % 8)) ? 0 : 0xffffff;
1237
                    (1 << (j % 8)) ? 0 : 0xffffff;
1146
                if (pointer_x + j < screen.xres && pointer_y +
1238
                if (pointer_x + j < screen.xres && pointer_y +
1147
                    i < screen.yres)
1239
                    i < screen.yres)
1148
                    putpixel(&viewports[0], pointer_x + j,
1240
                    putpixel(&viewports[0], pointer_x + j,
1149
                        pointer_y + i, color);
1241
                        pointer_y + i, color);
1150
            }
1242
            }
1151
        }
1243
        }
1152
    pointer_shown = 1;
1244
    pointer_shown = 1;
1153
}
1245
}
1154
 
1246
 
1155
 
1247
 
1156
static void mouse_hide(void)
1248
static void mouse_hide(void)
1157
{
1249
{
1158
    /* Restore image under the pointer. */
1250
    /* Restore image under the pointer. */
1159
    if (pointer_shown) {
1251
    if (pointer_shown) {
1160
        draw_pixmap(pointer_vport, pointer_pixmap);
1252
        draw_pixmap(pointer_vport, pointer_pixmap);
1161
        pointer_shown = 0;
1253
        pointer_shown = 0;
1162
    }
1254
    }
1163
}
1255
}
1164
 
1256
 
1165
 
1257
 
1166
static void mouse_move(unsigned int x, unsigned int y)
1258
static void mouse_move(unsigned int x, unsigned int y)
1167
{
1259
{
1168
    mouse_hide();
1260
    mouse_hide();
1169
    pointer_x = x;
1261
    pointer_x = x;
1170
    pointer_y = y;
1262
    pointer_y = y;
1171
    mouse_show();
1263
    mouse_show();
1172
}
1264
}
1173
 
1265
 
1174
 
1266
 
1175
static int anim_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
1267
static int anim_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
1176
{
1268
{
1177
    bool handled = true;
1269
    bool handled = true;
1178
    int retval = EOK;
1270
    int retval = EOK;
1179
    int i, nvp;
1271
    int i, nvp;
1180
    int newval;
1272
    int newval;
1181
   
1273
   
1182
    switch (IPC_GET_METHOD(*call)) {
1274
    switch (IPC_GET_METHOD(*call)) {
1183
    case FB_ANIM_CREATE:
1275
    case FB_ANIM_CREATE:
1184
        nvp = IPC_GET_ARG1(*call);
1276
        nvp = IPC_GET_ARG1(*call);
1185
        if (nvp == -1)
1277
        if (nvp == -1)
1186
            nvp = vp;
1278
            nvp = vp;
1187
        if (nvp >= MAX_VIEWPORTS || nvp < 0 ||
1279
        if (nvp >= MAX_VIEWPORTS || nvp < 0 ||
1188
            !viewports[nvp].initialized) {
1280
            !viewports[nvp].initialized) {
1189
            retval = EINVAL;
1281
            retval = EINVAL;
1190
            break;
1282
            break;
1191
        }
1283
        }
1192
        for (i = 0; i < MAX_ANIMATIONS; i++) {
1284
        for (i = 0; i < MAX_ANIMATIONS; i++) {
1193
            if (!animations[i].initialized)
1285
            if (!animations[i].initialized)
1194
                break;
1286
                break;
1195
        }
1287
        }
1196
        if (i == MAX_ANIMATIONS) {
1288
        if (i == MAX_ANIMATIONS) {
1197
            retval = ELIMIT;
1289
            retval = ELIMIT;
1198
            break;
1290
            break;
1199
        }
1291
        }
1200
        animations[i].initialized = 1;
1292
        animations[i].initialized = 1;
1201
        animations[i].animlen = 0;
1293
        animations[i].animlen = 0;
1202
        animations[i].pos = 0;
1294
        animations[i].pos = 0;
1203
        animations[i].enabled = 0;
1295
        animations[i].enabled = 0;
1204
        animations[i].vp = nvp;
1296
        animations[i].vp = nvp;
1205
        retval = i;
1297
        retval = i;
1206
        break;
1298
        break;
1207
    case FB_ANIM_DROP:
1299
    case FB_ANIM_DROP:
1208
        i = IPC_GET_ARG1(*call);
1300
        i = IPC_GET_ARG1(*call);
1209
        if (i >= MAX_ANIMATIONS || i < 0) {
1301
        if (i >= MAX_ANIMATIONS || i < 0) {
1210
            retval = EINVAL;
1302
            retval = EINVAL;
1211
            break;
1303
            break;
1212
        }
1304
        }
1213
        animations[i].initialized = 0;
1305
        animations[i].initialized = 0;
1214
        break;
1306
        break;
1215
    case FB_ANIM_ADDPIXMAP:
1307
    case FB_ANIM_ADDPIXMAP:
1216
        i = IPC_GET_ARG1(*call);
1308
        i = IPC_GET_ARG1(*call);
1217
        if (i >= MAX_ANIMATIONS || i < 0 ||
1309
        if (i >= MAX_ANIMATIONS || i < 0 ||
1218
            !animations[i].initialized) {
1310
            !animations[i].initialized) {
1219
            retval = EINVAL;
1311
            retval = EINVAL;
1220
            break;
1312
            break;
1221
        }
1313
        }
1222
        if (animations[i].animlen == MAX_ANIM_LEN) {
1314
        if (animations[i].animlen == MAX_ANIM_LEN) {
1223
            retval = ELIMIT;
1315
            retval = ELIMIT;
1224
            break;
1316
            break;
1225
        }
1317
        }
1226
        newval = IPC_GET_ARG2(*call);
1318
        newval = IPC_GET_ARG2(*call);
1227
        if (newval < 0 || newval > MAX_PIXMAPS ||
1319
        if (newval < 0 || newval > MAX_PIXMAPS ||
1228
            !pixmaps[newval].data) {
1320
            !pixmaps[newval].data) {
1229
            retval = EINVAL;
1321
            retval = EINVAL;
1230
            break;
1322
            break;
1231
        }
1323
        }
1232
        animations[i].pixmaps[animations[i].animlen++] = newval;
1324
        animations[i].pixmaps[animations[i].animlen++] = newval;
1233
        break;
1325
        break;
1234
    case FB_ANIM_CHGVP:
1326
    case FB_ANIM_CHGVP:
1235
        i = IPC_GET_ARG1(*call);
1327
        i = IPC_GET_ARG1(*call);
1236
        if (i >= MAX_ANIMATIONS || i < 0) {
1328
        if (i >= MAX_ANIMATIONS || i < 0) {
1237
            retval = EINVAL;
1329
            retval = EINVAL;
1238
            break;
1330
            break;
1239
        }
1331
        }
1240
        nvp = IPC_GET_ARG2(*call);
1332
        nvp = IPC_GET_ARG2(*call);
1241
        if (nvp == -1)
1333
        if (nvp == -1)
1242
            nvp = vp;
1334
            nvp = vp;
1243
        if (nvp >= MAX_VIEWPORTS || nvp < 0 ||
1335
        if (nvp >= MAX_VIEWPORTS || nvp < 0 ||
1244
            !viewports[nvp].initialized) {
1336
            !viewports[nvp].initialized) {
1245
            retval = EINVAL;
1337
            retval = EINVAL;
1246
            break;
1338
            break;
1247
        }
1339
        }
1248
        animations[i].vp = nvp;
1340
        animations[i].vp = nvp;
1249
        break;
1341
        break;
1250
    case FB_ANIM_START:
1342
    case FB_ANIM_START:
1251
    case FB_ANIM_STOP:
1343
    case FB_ANIM_STOP:
1252
        i = IPC_GET_ARG1(*call);
1344
        i = IPC_GET_ARG1(*call);
1253
        if (i >= MAX_ANIMATIONS || i < 0) {
1345
        if (i >= MAX_ANIMATIONS || i < 0) {
1254
            retval = EINVAL;
1346
            retval = EINVAL;
1255
            break;
1347
            break;
1256
        }
1348
        }
1257
        newval = (IPC_GET_METHOD(*call) == FB_ANIM_START);
1349
        newval = (IPC_GET_METHOD(*call) == FB_ANIM_START);
1258
        if (newval ^ animations[i].enabled) {
1350
        if (newval ^ animations[i].enabled) {
1259
            animations[i].enabled = newval;
1351
            animations[i].enabled = newval;
1260
            anims_enabled += newval ? 1 : -1;
1352
            anims_enabled += newval ? 1 : -1;
1261
        }
1353
        }
1262
        break;
1354
        break;
1263
    default:
1355
    default:
1264
        handled = 0;
1356
        handled = 0;
1265
    }
1357
    }
1266
    if (handled)
1358
    if (handled)
1267
        ipc_answer_0(callid, retval);
1359
        ipc_answer_0(callid, retval);
1268
    return handled;
1360
    return handled;
1269
}
1361
}
1270
 
1362
 
1271
 
1363
 
1272
/** Handler for messages concerning pixmap handling
1364
/** Handler for messages concerning pixmap handling
1273
 *
1365
 *
1274
 */
1366
 */
1275
static int pixmap_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
1367
static int pixmap_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
1276
{
1368
{
1277
    bool handled = true;
1369
    bool handled = true;
1278
    int retval = EOK;
1370
    int retval = EOK;
1279
    int i, nvp;
1371
    int i, nvp;
1280
   
1372
   
1281
    switch (IPC_GET_METHOD(*call)) {
1373
    switch (IPC_GET_METHOD(*call)) {
1282
    case FB_VP_DRAW_PIXMAP:
1374
    case FB_VP_DRAW_PIXMAP:
1283
        nvp = IPC_GET_ARG1(*call);
1375
        nvp = IPC_GET_ARG1(*call);
1284
        if (nvp == -1)
1376
        if (nvp == -1)
1285
            nvp = vp;
1377
            nvp = vp;
1286
        if (nvp < 0 || nvp >= MAX_VIEWPORTS ||
1378
        if (nvp < 0 || nvp >= MAX_VIEWPORTS ||
1287
            !viewports[nvp].initialized) {
1379
            !viewports[nvp].initialized) {
1288
            retval = EINVAL;
1380
            retval = EINVAL;
1289
            break;
1381
            break;
1290
        }
1382
        }
1291
        i = IPC_GET_ARG2(*call);
1383
        i = IPC_GET_ARG2(*call);
1292
        retval = draw_pixmap(nvp, i);
1384
        retval = draw_pixmap(nvp, i);
1293
        break;
1385
        break;
1294
    case FB_VP2PIXMAP:
1386
    case FB_VP2PIXMAP:
1295
        nvp = IPC_GET_ARG1(*call);
1387
        nvp = IPC_GET_ARG1(*call);
1296
        if (nvp == -1)
1388
        if (nvp == -1)
1297
            nvp = vp;
1389
            nvp = vp;
1298
        if (nvp < 0 || nvp >= MAX_VIEWPORTS ||
1390
        if (nvp < 0 || nvp >= MAX_VIEWPORTS ||
1299
            !viewports[nvp].initialized)
1391
            !viewports[nvp].initialized)
1300
            retval = EINVAL;
1392
            retval = EINVAL;
1301
        else
1393
        else
1302
            retval = save_vp_to_pixmap(&viewports[nvp]);
1394
            retval = save_vp_to_pixmap(&viewports[nvp]);
1303
        break;
1395
        break;
1304
    case FB_DROP_PIXMAP:
1396
    case FB_DROP_PIXMAP:
1305
        i = IPC_GET_ARG1(*call);
1397
        i = IPC_GET_ARG1(*call);
1306
        if (i >= MAX_PIXMAPS) {
1398
        if (i >= MAX_PIXMAPS) {
1307
            retval = EINVAL;
1399
            retval = EINVAL;
1308
            break;
1400
            break;
1309
        }
1401
        }
1310
        if (pixmaps[i].data) {
1402
        if (pixmaps[i].data) {
1311
            free(pixmaps[i].data);
1403
            free(pixmaps[i].data);
1312
            pixmaps[i].data = NULL;
1404
            pixmaps[i].data = NULL;
1313
        }
1405
        }
1314
        break;
1406
        break;
1315
    default:
1407
    default:
1316
        handled = 0;
1408
        handled = 0;
1317
    }
1409
    }
1318
   
1410
   
1319
    if (handled)
1411
    if (handled)
1320
        ipc_answer_0(callid, retval);
1412
        ipc_answer_0(callid, retval);
1321
    return handled;
1413
    return handled;
1322
   
1414
   
1323
}
1415
}
1324
 
1416
 
-
 
1417
static int rgb_from_style(attr_rgb_t *rgb, int style)
-
 
1418
{
-
 
1419
    switch (style) {
-
 
1420
    case STYLE_NORMAL:
-
 
1421
        rgb->fg_color = color_table[COLOR_BLACK];
-
 
1422
        rgb->bg_color = color_table[COLOR_WHITE];
-
 
1423
        break;
-
 
1424
    case STYLE_EMPHASIS:
-
 
1425
        rgb->fg_color = color_table[COLOR_RED];
-
 
1426
        rgb->bg_color = color_table[COLOR_WHITE];
-
 
1427
        break;
-
 
1428
    default:
-
 
1429
        return EINVAL;
-
 
1430
    }
-
 
1431
 
-
 
1432
    return EOK;
-
 
1433
}
-
 
1434
 
-
 
1435
static int rgb_from_idx(attr_rgb_t *rgb, ipcarg_t fg_color,
-
 
1436
    ipcarg_t bg_color, ipcarg_t flags)
-
 
1437
{
-
 
1438
    fg_color = (fg_color & 7) | ((flags & CATTR_BRIGHT) ? 8 : 0);
-
 
1439
    bg_color = (bg_color & 7) | ((flags & CATTR_BRIGHT) ? 8 : 0);
-
 
1440
 
-
 
1441
    rgb->fg_color = color_table[fg_color];
-
 
1442
    rgb->bg_color = color_table[bg_color];
-
 
1443
 
-
 
1444
    return EOK;
-
 
1445
}
-
 
1446
 
-
 
1447
static int fb_set_style(viewport_t *vport, ipcarg_t style)
-
 
1448
{
-
 
1449
    return rgb_from_style(&vport->attr, (int) style);
-
 
1450
}
-
 
1451
 
-
 
1452
static int fb_set_color(viewport_t *vport, ipcarg_t fg_color,
-
 
1453
    ipcarg_t bg_color, ipcarg_t flags)
-
 
1454
{
-
 
1455
    return rgb_from_idx(&vport->attr, fg_color, bg_color, flags);
-
 
1456
}
-
 
1457
 
1325
/** Function for handling connections to FB
1458
/** Function for handling connections to FB
1326
 *
1459
 *
1327
 */
1460
 */
1328
static void fb_client_connection(ipc_callid_t iid, ipc_call_t *icall)
1461
static void fb_client_connection(ipc_callid_t iid, ipc_call_t *icall)
1329
{
1462
{
1330
    unsigned int vp = 0;
1463
    unsigned int vp = 0;
1331
    viewport_t *vport = &viewports[vp];
1464
    viewport_t *vport = &viewports[vp];
1332
   
1465
   
1333
    if (client_connected) {
1466
    if (client_connected) {
1334
        ipc_answer_0(iid, ELIMIT);
1467
        ipc_answer_0(iid, ELIMIT);
1335
        return;
1468
        return;
1336
    }
1469
    }
1337
   
1470
   
1338
    /* Accept connection */
1471
    /* Accept connection */
1339
    client_connected = true;
1472
    client_connected = true;
1340
    ipc_answer_0(iid, EOK);
1473
    ipc_answer_0(iid, EOK);
1341
   
1474
   
1342
    while (true) {
1475
    while (true) {
1343
        ipc_callid_t callid;
1476
        ipc_callid_t callid;
1344
        ipc_call_t call;
1477
        ipc_call_t call;
1345
        int retval;
1478
        int retval;
1346
        unsigned int i;
1479
        unsigned int i;
1347
        int scroll;
1480
        int scroll;
1348
        uint8_t glyph;
1481
        uint8_t glyph;
1349
        unsigned int row, col;
1482
        unsigned int row, col;
1350
       
1483
       
1351
        if ((vport->cursor_active) || (anims_enabled))
1484
        if ((vport->cursor_active) || (anims_enabled))
1352
            callid = async_get_call_timeout(&call, 250000);
1485
            callid = async_get_call_timeout(&call, 250000);
1353
        else
1486
        else
1354
            callid = async_get_call(&call);
1487
            callid = async_get_call(&call);
1355
       
1488
       
1356
        mouse_hide();
1489
        mouse_hide();
1357
        if (!callid) {
1490
        if (!callid) {
1358
            cursor_blink(vport);
1491
            cursor_blink(vport);
1359
            anims_tick();
1492
            anims_tick();
1360
            mouse_show();
1493
            mouse_show();
1361
            continue;
1494
            continue;
1362
        }
1495
        }
1363
       
1496
       
1364
        if (shm_handle(callid, &call, vp))
1497
        if (shm_handle(callid, &call, vp))
1365
            continue;
1498
            continue;
1366
       
1499
       
1367
        if (pixmap_handle(callid, &call, vp))
1500
        if (pixmap_handle(callid, &call, vp))
1368
            continue;
1501
            continue;
1369
       
1502
       
1370
        if (anim_handle(callid, &call, vp))
1503
        if (anim_handle(callid, &call, vp))
1371
            continue;
1504
            continue;
1372
       
1505
       
1373
        switch (IPC_GET_METHOD(call)) {
1506
        switch (IPC_GET_METHOD(call)) {
1374
        case IPC_M_PHONE_HUNGUP:
1507
        case IPC_M_PHONE_HUNGUP:
1375
            client_connected = false;
1508
            client_connected = false;
1376
           
1509
           
1377
            /* Cleanup other viewports */
1510
            /* Cleanup other viewports */
1378
            for (i = 1; i < MAX_VIEWPORTS; i++)
1511
            for (i = 1; i < MAX_VIEWPORTS; i++)
1379
                vport->initialized = false;
1512
                vport->initialized = false;
1380
           
1513
           
1381
            /* Exit thread */
1514
            /* Exit thread */
1382
            return;
1515
            return;
1383
       
1516
       
1384
        case FB_PUTCHAR:
1517
        case FB_PUTCHAR:
1385
            glyph = IPC_GET_ARG1(call);
1518
            glyph = IPC_GET_ARG1(call);
1386
            row = IPC_GET_ARG2(call);
1519
            row = IPC_GET_ARG2(call);
1387
            col = IPC_GET_ARG3(call);
1520
            col = IPC_GET_ARG3(call);
1388
           
1521
           
1389
            if ((col >= vport->cols) || (row >= vport->rows)) {
1522
            if ((col >= vport->cols) || (row >= vport->rows)) {
1390
                retval = EINVAL;
1523
                retval = EINVAL;
1391
                break;
1524
                break;
1392
            }
1525
            }
1393
            ipc_answer_0(callid, EOK);
1526
            ipc_answer_0(callid, EOK);
1394
           
1527
           
1395
            draw_char(vport, glyph, col, row);
1528
            draw_char(vport, glyph, col, row);
1396
           
1529
           
1397
            /* Message already answered */
1530
            /* Message already answered */
1398
            continue;
1531
            continue;
1399
        case FB_CLEAR:
1532
        case FB_CLEAR:
1400
            vport_clear(vport);
1533
            vport_clear(vport);
1401
            cursor_show(vport);
1534
            cursor_show(vport);
1402
            retval = EOK;
1535
            retval = EOK;
1403
            break;
1536
            break;
1404
        case FB_CURSOR_GOTO:
1537
        case FB_CURSOR_GOTO:
1405
            row = IPC_GET_ARG1(call);
1538
            row = IPC_GET_ARG1(call);
1406
            col = IPC_GET_ARG2(call);
1539
            col = IPC_GET_ARG2(call);
1407
           
1540
           
1408
            if ((col >= vport->cols) || (row >= vport->rows)) {
1541
            if ((col >= vport->cols) || (row >= vport->rows)) {
1409
                retval = EINVAL;
1542
                retval = EINVAL;
1410
                break;
1543
                break;
1411
            }
1544
            }
1412
            retval = EOK;
1545
            retval = EOK;
1413
           
1546
           
1414
            cursor_hide(vport);
1547
            cursor_hide(vport);
1415
            vport->cur_col = col;
1548
            vport->cur_col = col;
1416
            vport->cur_row = row;
1549
            vport->cur_row = row;
1417
            cursor_show(vport);
1550
            cursor_show(vport);
1418
            break;
1551
            break;
1419
        case FB_CURSOR_VISIBILITY:
1552
        case FB_CURSOR_VISIBILITY:
1420
            cursor_hide(vport);
1553
            cursor_hide(vport);
1421
            vport->cursor_active = IPC_GET_ARG1(call);
1554
            vport->cursor_active = IPC_GET_ARG1(call);
1422
            cursor_show(vport);
1555
            cursor_show(vport);
1423
            retval = EOK;
1556
            retval = EOK;
1424
            break;
1557
            break;
1425
        case FB_GET_CSIZE:
1558
        case FB_GET_CSIZE:
1426
            ipc_answer_2(callid, EOK, vport->rows, vport->cols);
1559
            ipc_answer_2(callid, EOK, vport->rows, vport->cols);
1427
            continue;
1560
            continue;
1428
        case FB_SCROLL:
1561
        case FB_SCROLL:
1429
            scroll = IPC_GET_ARG1(call);
1562
            scroll = IPC_GET_ARG1(call);
1430
            if ((scroll > (int) vport->rows) || (scroll < (-(int) vport->rows))) {
1563
            if ((scroll > (int) vport->rows) || (scroll < (-(int) vport->rows))) {
1431
                retval = EINVAL;
1564
                retval = EINVAL;
1432
                break;
1565
                break;
1433
            }
1566
            }
1434
            cursor_hide(vport);
1567
            cursor_hide(vport);
1435
            vport_scroll(vport, scroll);
1568
            vport_scroll(vport, scroll);
1436
            cursor_show(vport);
1569
            cursor_show(vport);
1437
            retval = EOK;
1570
            retval = EOK;
1438
            break;
1571
            break;
1439
        case FB_VIEWPORT_SWITCH:
1572
        case FB_VIEWPORT_SWITCH:
1440
            i = IPC_GET_ARG1(call);
1573
            i = IPC_GET_ARG1(call);
1441
            if (i >= MAX_VIEWPORTS) {
1574
            if (i >= MAX_VIEWPORTS) {
1442
                retval = EINVAL;
1575
                retval = EINVAL;
1443
                break;
1576
                break;
1444
            }
1577
            }
1445
            if (!viewports[i].initialized) {
1578
            if (!viewports[i].initialized) {
1446
                retval = EADDRNOTAVAIL;
1579
                retval = EADDRNOTAVAIL;
1447
                break;
1580
                break;
1448
            }
1581
            }
1449
            cursor_hide(vport);
1582
            cursor_hide(vport);
1450
            vp = i;
1583
            vp = i;
1451
            vport = &viewports[vp];
1584
            vport = &viewports[vp];
1452
            cursor_show(vport);
1585
            cursor_show(vport);
1453
            retval = EOK;
1586
            retval = EOK;
1454
            break;
1587
            break;
1455
        case FB_VIEWPORT_CREATE:
1588
        case FB_VIEWPORT_CREATE:
1456
            retval = vport_create(IPC_GET_ARG1(call) >> 16,
1589
            retval = vport_create(IPC_GET_ARG1(call) >> 16,
1457
                IPC_GET_ARG1(call) & 0xffff,
1590
                IPC_GET_ARG1(call) & 0xffff,
1458
                IPC_GET_ARG2(call) >> 16,
1591
                IPC_GET_ARG2(call) >> 16,
1459
                IPC_GET_ARG2(call) & 0xffff);
1592
                IPC_GET_ARG2(call) & 0xffff);
1460
            break;
1593
            break;
1461
        case FB_VIEWPORT_DELETE:
1594
        case FB_VIEWPORT_DELETE:
1462
            i = IPC_GET_ARG1(call);
1595
            i = IPC_GET_ARG1(call);
1463
            if (i >= MAX_VIEWPORTS) {
1596
            if (i >= MAX_VIEWPORTS) {
1464
                retval = EINVAL;
1597
                retval = EINVAL;
1465
                break;
1598
                break;
1466
            }
1599
            }
1467
            if (!viewports[i].initialized) {
1600
            if (!viewports[i].initialized) {
1468
                retval = EADDRNOTAVAIL;
1601
                retval = EADDRNOTAVAIL;
1469
                break;
1602
                break;
1470
            }
1603
            }
1471
            viewports[i].initialized = false;
1604
            viewports[i].initialized = false;
1472
            if (viewports[i].glyphs)
1605
            if (viewports[i].glyphs)
1473
                free(viewports[i].glyphs);
1606
                free(viewports[i].glyphs);
1474
            if (viewports[i].bgpixel)
1607
            if (viewports[i].bgpixel)
1475
                free(viewports[i].bgpixel);
1608
                free(viewports[i].bgpixel);
1476
            if (viewports[i].backbuf)
1609
            if (viewports[i].backbuf)
1477
                free(viewports[i].backbuf);
1610
                free(viewports[i].backbuf);
1478
            retval = EOK;
1611
            retval = EOK;
1479
            break;
1612
            break;
1480
        case FB_SET_STYLE:
1613
        case FB_SET_STYLE:
-
 
1614
            retval = fb_set_style(vport, IPC_GET_ARG1(call));
-
 
1615
            break;
-
 
1616
        case FB_SET_COLOR:
-
 
1617
            retval = fb_set_color(vport, IPC_GET_ARG1(call),
-
 
1618
                IPC_GET_ARG2(call), IPC_GET_ARG3(call));
-
 
1619
            break;
-
 
1620
        case FB_SET_RGB_COLOR:
1481
            vport->style.fg_color = IPC_GET_ARG1(call);
1621
            vport->attr.fg_color = IPC_GET_ARG1(call);
1482
            vport->style.bg_color = IPC_GET_ARG2(call);
1622
            vport->attr.bg_color = IPC_GET_ARG2(call);
1483
            retval = EOK;
1623
            retval = EOK;
1484
            break;
1624
            break;
1485
        case FB_GET_RESOLUTION:
1625
        case FB_GET_RESOLUTION:
1486
            ipc_answer_2(callid, EOK, screen.xres, screen.yres);
1626
            ipc_answer_2(callid, EOK, screen.xres, screen.yres);
1487
            continue;
1627
            continue;
1488
        case FB_POINTER_MOVE:
1628
        case FB_POINTER_MOVE:
1489
            pointer_enabled = true;
1629
            pointer_enabled = true;
1490
            mouse_move(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
1630
            mouse_move(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
1491
            retval = EOK;
1631
            retval = EOK;
1492
            break;
1632
            break;
1493
        default:
1633
        default:
1494
            retval = ENOENT;
1634
            retval = ENOENT;
1495
        }
1635
        }
1496
        ipc_answer_0(callid, retval);
1636
        ipc_answer_0(callid, retval);
1497
    }
1637
    }
1498
}
1638
}
1499
 
1639
 
1500
/** Initialization of framebuffer
1640
/** Initialization of framebuffer
1501
 *
1641
 *
1502
 */
1642
 */
1503
int fb_init(void)
1643
int fb_init(void)
1504
{
1644
{
1505
    async_set_client_connection(fb_client_connection);
1645
    async_set_client_connection(fb_client_connection);
1506
   
1646
   
1507
    void *fb_ph_addr = (void *) sysinfo_value("fb.address.physical");
1647
    void *fb_ph_addr = (void *) sysinfo_value("fb.address.physical");
1508
    unsigned int fb_offset = sysinfo_value("fb.offset");
1648
    unsigned int fb_offset = sysinfo_value("fb.offset");
1509
    unsigned int fb_width = sysinfo_value("fb.width");
1649
    unsigned int fb_width = sysinfo_value("fb.width");
1510
    unsigned int fb_height = sysinfo_value("fb.height");
1650
    unsigned int fb_height = sysinfo_value("fb.height");
1511
    unsigned int fb_scanline = sysinfo_value("fb.scanline");
1651
    unsigned int fb_scanline = sysinfo_value("fb.scanline");
1512
    unsigned int fb_visual = sysinfo_value("fb.visual");
1652
    unsigned int fb_visual = sysinfo_value("fb.visual");
1513
   
1653
   
1514
    unsigned int fbsize = fb_scanline * fb_height;
1654
    unsigned int fbsize = fb_scanline * fb_height;
1515
    void *fb_addr = as_get_mappable_page(fbsize);
1655
    void *fb_addr = as_get_mappable_page(fbsize);
1516
   
1656
   
1517
    physmem_map(fb_ph_addr + fb_offset, fb_addr,
1657
    physmem_map(fb_ph_addr + fb_offset, fb_addr,
1518
        ALIGN_UP(fbsize, PAGE_SIZE) >> PAGE_WIDTH, AS_AREA_READ | AS_AREA_WRITE);
1658
        ALIGN_UP(fbsize, PAGE_SIZE) >> PAGE_WIDTH, AS_AREA_READ | AS_AREA_WRITE);
1519
   
1659
   
1520
    if (screen_init(fb_addr, fb_width, fb_height, fb_scanline, fb_visual))
1660
    if (screen_init(fb_addr, fb_width, fb_height, fb_scanline, fb_visual))
1521
        return 0;
1661
        return 0;
1522
   
1662
   
1523
    return -1;
1663
    return -1;
1524
}
1664
}
1525
 
1665
 
1526
/**
1666
/**
1527
 * @}
1667
 * @}
1528
 */
1668
 */
1529
 
1669