Subversion Repositories HelenOS

Rev

Rev 1552 | Rev 1557 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1518 palkovsky 1
/*
2
 * Copyright (C) 2006 Ondrej Palkovsky
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
#include <ipc/fb.h>
30
#include <ipc/ipc.h>
1522 palkovsky 31
#include <async.h>
32
#include <stdio.h>
1547 palkovsky 33
#include <sys/mman.h>
34
#include <string.h>
1555 palkovsky 35
#include <align.h>
1518 palkovsky 36
 
37
#include "console.h"
1522 palkovsky 38
#include "gcons.h"
1518 palkovsky 39
 
1547 palkovsky 40
#define CONSOLE_TOP      65
1555 palkovsky 41
#define CONSOLE_MARGIN   6
1518 palkovsky 42
 
1555 palkovsky 43
#define STATUS_START    110
44
#define STATUS_TOP      8
45
#define STATUS_SPACE    3
46
#define STATUS_WIDTH    48
47
#define STATUS_HEIGHT   48
1518 palkovsky 48
 
1547 palkovsky 49
#define MAIN_COLOR      0xffffff
1522 palkovsky 50
 
1518 palkovsky 51
static int use_gcons = 0;
52
static ipcarg_t xres,yres;
53
 
1555 palkovsky 54
enum butstate {
55
    CONS_DISCONNECTED = 0,
56
    CONS_SELECTED,
57
    CONS_IDLE,
58
    CONS_HAS_DATA,
59
    CONS_KERNEL,
60
    CONS_DISCONNECTED_SEL,
61
    CONS_LAST
62
};
63
 
1518 palkovsky 64
static int console_vp;
65
static int cstatus_vp[CONSOLE_COUNT];
1522 palkovsky 66
static int cstat_row, cstat_col; /* Size of cstatus buttons */
1555 palkovsky 67
static enum butstate console_state[CONSOLE_COUNT];
1518 palkovsky 68
 
69
static int fbphone;
70
 
1555 palkovsky 71
/** List of pixmaps identifying these icons */
72
static int ic_pixmaps[CONS_LAST] = {-1,-1,-1,-1,-1,-1};
1522 palkovsky 73
 
74
static int active_console = 0;
75
 
1518 palkovsky 76
static void vp_switch(int vp)
77
{
1522 palkovsky 78
    nsend_call(fbphone,FB_VIEWPORT_SWITCH, vp);
1518 palkovsky 79
}
80
 
1522 palkovsky 81
/** Create view port */
1518 palkovsky 82
static int vp_create(unsigned int x, unsigned int y,
1522 palkovsky 83
             unsigned int width, unsigned int height)
1518 palkovsky 84
{
1522 palkovsky 85
    /* Init function, use ipc_call_sync */
86
    return ipc_call_sync_2(fbphone, FB_VIEWPORT_CREATE,
87
                   (x << 16) | y, (width << 16) | height,
88
                   NULL, NULL);
1518 palkovsky 89
}
90
 
1522 palkovsky 91
static void clear(void)
1518 palkovsky 92
{
1522 palkovsky 93
    nsend_call(fbphone, FB_CLEAR, 0);
1518 palkovsky 94
 
95
}
96
 
1522 palkovsky 97
static void set_style(int fgcolor, int bgcolor)
1518 palkovsky 98
{
1522 palkovsky 99
    nsend_call_2(fbphone, FB_SET_STYLE, fgcolor, bgcolor);
1518 palkovsky 100
}
101
 
1522 palkovsky 102
static void putch(char c, int row, int col)
103
{
104
    nsend_call_3(fbphone, FB_PUTCHAR, c, row, col);
105
}
106
 
1555 palkovsky 107
static void redraw_state(int consnum)
1522 palkovsky 108
{
109
    char data[5];
110
    int i;
1555 palkovsky 111
    enum butstate state = console_state[consnum];
112
 
1522 palkovsky 113
    vp_switch(cstatus_vp[consnum]);
1555 palkovsky 114
    if (ic_pixmaps[state] != -1)
115
        nsend_call_2(fbphone, FB_VP_DRAW_PIXMAP, cstatus_vp[consnum], ic_pixmaps[state]);
116
 
117
    if (state != CONS_DISCONNECTED && state != CONS_KERNEL && state != CONS_DISCONNECTED_SEL) {
118
        snprintf(data, 5, "%d", consnum+1);
119
        for (i=0;data[i];i++)
120
            putch(data[i], 1, 2+i);
121
    }
1522 palkovsky 122
}
123
 
1555 palkovsky 124
/** Notification run on changing console (except kernel console) */
1518 palkovsky 125
void gcons_change_console(int consnum)
126
{
1555 palkovsky 127
    int i;
128
 
1518 palkovsky 129
    if (!use_gcons)
130
        return;
1552 palkovsky 131
 
1555 palkovsky 132
    if (active_console == KERNEL_CONSOLE) {
133
        for (i=0; i < CONSOLE_COUNT; i++)
134
            redraw_state(i);
135
    } else {
136
        if (console_state[active_console] == CONS_DISCONNECTED_SEL)
137
            console_state[active_console] = CONS_DISCONNECTED;
138
        else
139
            console_state[active_console] = CONS_IDLE;
140
        redraw_state(active_console);
141
    }
1522 palkovsky 142
    active_console = consnum;
1528 palkovsky 143
 
1555 palkovsky 144
    if (console_state[consnum] == CONS_DISCONNECTED) {
145
        console_state[consnum] = CONS_DISCONNECTED_SEL;
146
        redraw_state(consnum);
147
    } else
148
        console_state[consnum] = CONS_SELECTED;
149
    redraw_state(consnum);
150
 
1518 palkovsky 151
    vp_switch(console_vp);
152
}
153
 
1552 palkovsky 154
/** Notification function that gets called on new output to virtual console */
1518 palkovsky 155
void gcons_notify_char(int consnum)
156
{
157
    if (!use_gcons)
158
        return;
159
 
1555 palkovsky 160
    if (consnum == active_console || console_state[consnum] == CONS_HAS_DATA)
1528 palkovsky 161
        return;
162
 
1555 palkovsky 163
    console_state[consnum] = CONS_HAS_DATA;
1552 palkovsky 164
 
1555 palkovsky 165
    if (active_console == KERNEL_CONSOLE)
1552 palkovsky 166
        return;
167
 
1555 palkovsky 168
    redraw_state(consnum);
1528 palkovsky 169
 
1518 palkovsky 170
    vp_switch(console_vp);
1555 palkovsky 171
}
1552 palkovsky 172
 
1555 palkovsky 173
void gcons_notify_connect(int consnum)
174
{
175
    if (!use_gcons)
176
        return;
177
    if (active_console == consnum)
178
        console_state[consnum] = CONS_SELECTED;
179
    else
180
        console_state[consnum] = CONS_IDLE;
181
 
182
    if (active_console == KERNEL_CONSOLE)
183
        return;
184
 
185
    redraw_state(consnum);
186
    vp_switch(console_vp);
1518 palkovsky 187
}
188
 
1552 palkovsky 189
/** Change to kernel console */
190
void gcons_in_kernel(void)
191
{
1555 palkovsky 192
    if (console_state[active_console] = CONS_DISCONNECTED_SEL)
193
        console_state[active_console] = CONS_DISCONNECTED;
194
    else
195
        console_state[active_console] = CONS_IDLE;
196
    redraw_state(active_console);
197
 
198
    active_console = KERNEL_CONSOLE; /* Set to kernel console */
1552 palkovsky 199
    vp_switch(0);
200
}
201
 
202
/** Draw a PPM pixmap to framebuffer
203
 *
204
 * @param logo Pointer to PPM data
205
 * @param size Size of PPM data
206
 * @param x Coordinate of upper left corner
207
 * @param y Coordinate of upper left corner
208
 */
1547 palkovsky 209
static void draw_pixmap(char *logo, size_t size, int x, int y)
210
{
211
    char *shm;
212
    int rc;
213
 
214
    /* Create area */
215
    shm = mmap(NULL, size, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
216
    if (shm == MAP_FAILED)
217
        return;
218
 
219
    memcpy(shm, logo, size);
220
    /* Send area */
221
    rc = sync_send_2(fbphone, FB_PREPARE_SHM, (ipcarg_t)shm, 0, NULL, NULL);
222
    if (rc)
223
        goto exit;
224
    rc = sync_send_3(fbphone, IPC_M_AS_AREA_SEND, (ipcarg_t)shm, 0, PROTO_READ, NULL, NULL, NULL);
225
    if (rc)
226
        goto drop;
227
    /* Draw logo */
1555 palkovsky 228
    nsend_call_2(fbphone, FB_DRAW_PPM, x, y);
1547 palkovsky 229
drop:
230
    /* Drop area */
231
    nsend_call(fbphone, FB_DROP_SHM, 0);
232
exit:      
233
    /* Remove area */
234
    munmap(shm, size);
235
}
236
 
237
extern char _binary_helenos_ppm_start[0];
238
extern int _binary_helenos_ppm_size;
239
extern char _binary_nameic_ppm_start[0];
240
extern int _binary_nameic_ppm_size;
1555 palkovsky 241
static void gcons_redraw_console(void)
1518 palkovsky 242
{
1522 palkovsky 243
    int i;
1547 palkovsky 244
    size_t hsize = (size_t)&_binary_helenos_ppm_size;
1522 palkovsky 245
 
1518 palkovsky 246
    if (!use_gcons)
247
        return;
248
 
249
    vp_switch(0);
1522 palkovsky 250
    set_style(MAIN_COLOR, MAIN_COLOR);
251
    clear();
1555 palkovsky 252
    draw_pixmap(_binary_helenos_ppm_start, (size_t)&_binary_helenos_ppm_size, xres-66, 2);
253
    draw_pixmap(_binary_nameic_ppm_start, (size_t)&_binary_nameic_ppm_size, 5, 17);
1522 palkovsky 254
 
1547 palkovsky 255
 
1522 palkovsky 256
    for (i=0;i < CONSOLE_COUNT; i++)
1555 palkovsky 257
        redraw_state(i);
1518 palkovsky 258
    vp_switch(console_vp);
259
}
260
 
1555 palkovsky 261
static int make_pixmap(char *data, int size)
262
{
263
    char *shm;
264
    int rc;
265
    int pxid = -1;
266
 
267
    /* Create area */
268
    shm = mmap(NULL, size, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
269
    if (shm == MAP_FAILED)
270
        return -1;
271
 
272
    memcpy(shm, data, size);
273
    /* Send area */
274
    rc = sync_send_2(fbphone, FB_PREPARE_SHM, (ipcarg_t)shm, 0, NULL, NULL);
275
    if (rc)
276
        goto exit;
277
    rc = sync_send_3(fbphone, IPC_M_AS_AREA_SEND, (ipcarg_t)shm, 0, PROTO_READ, NULL, NULL, NULL);
278
    if (rc)
279
        goto drop;
280
 
281
    /* Obtain pixmap */
282
    rc = sync_send(fbphone, FB_SHM2PIXMAP, 0, NULL);
283
    if (rc < 0)
284
        goto drop;
285
    pxid = rc;
286
drop:
287
    /* Drop area */
288
    nsend_call(fbphone, FB_DROP_SHM, 0);
289
exit:      
290
    /* Remove area */
291
    munmap(shm, size);
292
 
293
    return pxid;
294
}
295
 
296
extern char _binary_cons_selected_ppm_start[0];
297
extern int _binary_cons_selected_ppm_size;
298
extern char _binary_cons_idle_ppm_start[0];
299
extern int _binary_cons_idle_ppm_size;
300
extern char _binary_cons_has_data_ppm_start[0];
301
extern int _binary_cons_has_data_ppm_size;
302
extern char _binary_cons_kernel_ppm_start[0];
303
extern int _binary_cons_kernel_ppm_size;
1518 palkovsky 304
/** Initialize nice graphical console environment */
305
void gcons_init(int phone)
306
{
307
    int rc;
308
    int i;
309
 
310
    fbphone = phone;
311
 
312
    rc = ipc_call_sync_2(phone, FB_GET_RESOLUTION, 0, 0, &xres, &yres);
313
    if (rc)
314
        return;
315
 
316
    if (xres < 800 || yres < 600)
317
        return;
318
 
319
    /* create console viewport */
1555 palkovsky 320
    /* Align width & height to character size */
321
    console_vp = vp_create(CONSOLE_MARGIN, CONSOLE_TOP,
322
                   ALIGN_DOWN(xres-2*CONSOLE_MARGIN, 8),
323
                   ALIGN_DOWN(yres-(CONSOLE_TOP+CONSOLE_MARGIN),16));
1518 palkovsky 324
    if (console_vp < 0)
325
        return;
326
 
327
    /* Create status buttons */
328
    for (i=0; i < CONSOLE_COUNT; i++) {
1547 palkovsky 329
        cstatus_vp[i] = vp_create(STATUS_START+CONSOLE_MARGIN+i*(STATUS_WIDTH+STATUS_SPACE),
1555 palkovsky 330
                      STATUS_TOP, STATUS_WIDTH, STATUS_HEIGHT);
1518 palkovsky 331
        if (cstatus_vp[i] < 0)
332
            return;
1555 palkovsky 333
        vp_switch(cstatus_vp[i]);
334
        set_style(0x202020, 0xffffff);
1518 palkovsky 335
    }
336
 
1555 palkovsky 337
    /* Initialize icons */
338
    ic_pixmaps[CONS_SELECTED] = make_pixmap(_binary_cons_selected_ppm_start,
339
                          (int)&_binary_cons_selected_ppm_size);
340
    ic_pixmaps[CONS_IDLE] = make_pixmap(_binary_cons_idle_ppm_start,
341
                          (int)&_binary_cons_idle_ppm_size);
342
    ic_pixmaps[CONS_HAS_DATA] = make_pixmap(_binary_cons_has_data_ppm_start,
343
                        (int)&_binary_cons_has_data_ppm_size);
344
    ic_pixmaps[CONS_DISCONNECTED] = make_pixmap(_binary_cons_idle_ppm_start,
345
                          (int)&_binary_cons_idle_ppm_size);
346
    ic_pixmaps[CONS_KERNEL] = make_pixmap(_binary_cons_kernel_ppm_start,
347
                          (int)&_binary_cons_kernel_ppm_size);
348
    ic_pixmaps[CONS_DISCONNECTED_SEL] = ic_pixmaps[CONS_SELECTED];
349
 
1518 palkovsky 350
    use_gcons = 1;
1555 palkovsky 351
    console_state[0] = CONS_DISCONNECTED_SEL;
352
    console_state[KERNEL_CONSOLE] = CONS_KERNEL;
1522 palkovsky 353
    gcons_redraw_console();
1518 palkovsky 354
}