Subversion Repositories HelenOS

Rev

Rev 4647 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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