Subversion Repositories HelenOS

Rev

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

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