Subversion Repositories HelenOS

Rev

Rev 1610 | Rev 1647 | 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};
1646 palkovsky 73
static int animation = -1;
1522 palkovsky 74
 
75
static int active_console = 0;
76
 
1518 palkovsky 77
static void vp_switch(int vp)
78
{
1610 palkovsky 79
    async_msg(fbphone,FB_VIEWPORT_SWITCH, vp);
1518 palkovsky 80
}
81
 
1522 palkovsky 82
/** Create view port */
1518 palkovsky 83
static int vp_create(unsigned int x, unsigned int y,
1522 palkovsky 84
             unsigned int width, unsigned int height)
1518 palkovsky 85
{
1522 palkovsky 86
    /* Init function, use ipc_call_sync */
87
    return ipc_call_sync_2(fbphone, FB_VIEWPORT_CREATE,
88
                   (x << 16) | y, (width << 16) | height,
89
                   NULL, NULL);
1518 palkovsky 90
}
91
 
1522 palkovsky 92
static void clear(void)
1518 palkovsky 93
{
1610 palkovsky 94
    async_msg(fbphone, FB_CLEAR, 0);
1518 palkovsky 95
 
96
}
97
 
1522 palkovsky 98
static void set_style(int fgcolor, int bgcolor)
1518 palkovsky 99
{
1610 palkovsky 100
    async_msg_2(fbphone, FB_SET_STYLE, fgcolor, bgcolor);
1518 palkovsky 101
}
102
 
1558 palkovsky 103
/** Transparent putchar */
104
static void tran_putch(char c, int row, int col)
1522 palkovsky 105
{
1610 palkovsky 106
    async_msg_3(fbphone, FB_TRANS_PUTCHAR, c, row, col);
1522 palkovsky 107
}
108
 
1558 palkovsky 109
/** Redraw the button showing state of a given console */
1555 palkovsky 110
static void redraw_state(int consnum)
1522 palkovsky 111
{
112
    char data[5];
113
    int i;
1555 palkovsky 114
    enum butstate state = console_state[consnum];
115
 
1522 palkovsky 116
    vp_switch(cstatus_vp[consnum]);
1555 palkovsky 117
    if (ic_pixmaps[state] != -1)
1610 palkovsky 118
        async_msg_2(fbphone, FB_VP_DRAW_PIXMAP, cstatus_vp[consnum], ic_pixmaps[state]);
1555 palkovsky 119
 
120
    if (state != CONS_DISCONNECTED && state != CONS_KERNEL && state != CONS_DISCONNECTED_SEL) {
121
        snprintf(data, 5, "%d", consnum+1);
122
        for (i=0;data[i];i++)
1558 palkovsky 123
            tran_putch(data[i], 1, 2+i);
1555 palkovsky 124
    }
1522 palkovsky 125
}
126
 
1555 palkovsky 127
/** Notification run on changing console (except kernel console) */
1518 palkovsky 128
void gcons_change_console(int consnum)
129
{
1555 palkovsky 130
    int i;
131
 
1518 palkovsky 132
    if (!use_gcons)
133
        return;
1552 palkovsky 134
 
1555 palkovsky 135
    if (active_console == KERNEL_CONSOLE) {
136
        for (i=0; i < CONSOLE_COUNT; i++)
137
            redraw_state(i);
138
    } else {
139
        if (console_state[active_console] == CONS_DISCONNECTED_SEL)
140
            console_state[active_console] = CONS_DISCONNECTED;
141
        else
142
            console_state[active_console] = CONS_IDLE;
143
        redraw_state(active_console);
144
    }
1522 palkovsky 145
    active_console = consnum;
1528 palkovsky 146
 
1555 palkovsky 147
    if (console_state[consnum] == CONS_DISCONNECTED) {
148
        console_state[consnum] = CONS_DISCONNECTED_SEL;
149
        redraw_state(consnum);
150
    } else
151
        console_state[consnum] = CONS_SELECTED;
152
    redraw_state(consnum);
153
 
1518 palkovsky 154
    vp_switch(console_vp);
155
}
156
 
1552 palkovsky 157
/** Notification function that gets called on new output to virtual console */
1518 palkovsky 158
void gcons_notify_char(int consnum)
159
{
160
    if (!use_gcons)
161
        return;
162
 
1555 palkovsky 163
    if (consnum == active_console || console_state[consnum] == CONS_HAS_DATA)
1528 palkovsky 164
        return;
165
 
1555 palkovsky 166
    console_state[consnum] = CONS_HAS_DATA;
1552 palkovsky 167
 
1555 palkovsky 168
    if (active_console == KERNEL_CONSOLE)
1552 palkovsky 169
        return;
170
 
1555 palkovsky 171
    redraw_state(consnum);
1528 palkovsky 172
 
1518 palkovsky 173
    vp_switch(console_vp);
1555 palkovsky 174
}
1552 palkovsky 175
 
1592 palkovsky 176
/** Notification function called on service disconnect from console */
177
void gcons_notify_disconnect(int consnum)
178
{
179
    if (!use_gcons)
180
        return;
181
    if (active_console == consnum)
182
        console_state[consnum] = CONS_DISCONNECTED_SEL;
183
    else
184
        console_state[consnum] = CONS_DISCONNECTED;
185
 
186
    if (active_console == KERNEL_CONSOLE)
187
        return;
188
 
189
    redraw_state(consnum);
190
    vp_switch(console_vp);
191
}
192
 
1558 palkovsky 193
/** Notification function called on console connect */
1555 palkovsky 194
void gcons_notify_connect(int consnum)
195
{
196
    if (!use_gcons)
197
        return;
198
    if (active_console == consnum)
199
        console_state[consnum] = CONS_SELECTED;
200
    else
201
        console_state[consnum] = CONS_IDLE;
202
 
203
    if (active_console == KERNEL_CONSOLE)
204
        return;
205
 
206
    redraw_state(consnum);
207
    vp_switch(console_vp);
1518 palkovsky 208
}
209
 
1552 palkovsky 210
/** Change to kernel console */
211
void gcons_in_kernel(void)
212
{
1557 palkovsky 213
    if (console_state[active_console] == CONS_DISCONNECTED_SEL)
1555 palkovsky 214
        console_state[active_console] = CONS_DISCONNECTED;
215
    else
216
        console_state[active_console] = CONS_IDLE;
217
    redraw_state(active_console);
218
 
219
    active_console = KERNEL_CONSOLE; /* Set to kernel console */
1552 palkovsky 220
    vp_switch(0);
221
}
222
 
223
/** Draw a PPM pixmap to framebuffer
224
 *
225
 * @param logo Pointer to PPM data
226
 * @param size Size of PPM data
227
 * @param x Coordinate of upper left corner
228
 * @param y Coordinate of upper left corner
229
 */
1547 palkovsky 230
static void draw_pixmap(char *logo, size_t size, int x, int y)
231
{
232
    char *shm;
233
    int rc;
234
 
235
    /* Create area */
236
    shm = mmap(NULL, size, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
237
    if (shm == MAP_FAILED)
238
        return;
239
 
240
    memcpy(shm, logo, size);
241
    /* Send area */
1610 palkovsky 242
    rc = async_req_2(fbphone, FB_PREPARE_SHM, (ipcarg_t)shm, 0, NULL, NULL);
1547 palkovsky 243
    if (rc)
244
        goto exit;
1610 palkovsky 245
    rc = async_req_3(fbphone, IPC_M_AS_AREA_SEND, (ipcarg_t)shm, 0, PROTO_READ, NULL, NULL, NULL);
1547 palkovsky 246
    if (rc)
247
        goto drop;
248
    /* Draw logo */
1610 palkovsky 249
    async_msg_2(fbphone, FB_DRAW_PPM, x, y);
1547 palkovsky 250
drop:
251
    /* Drop area */
1610 palkovsky 252
    async_msg(fbphone, FB_DROP_SHM, 0);
1547 palkovsky 253
exit:      
254
    /* Remove area */
255
    munmap(shm, size);
256
}
257
 
258
extern char _binary_helenos_ppm_start[0];
259
extern int _binary_helenos_ppm_size;
260
extern char _binary_nameic_ppm_start[0];
261
extern int _binary_nameic_ppm_size;
1558 palkovsky 262
/** Redraws console graphics  */
1555 palkovsky 263
static void gcons_redraw_console(void)
1518 palkovsky 264
{
1522 palkovsky 265
    int i;
1547 palkovsky 266
    size_t hsize = (size_t)&_binary_helenos_ppm_size;
1522 palkovsky 267
 
1518 palkovsky 268
    if (!use_gcons)
269
        return;
270
 
271
    vp_switch(0);
1522 palkovsky 272
    set_style(MAIN_COLOR, MAIN_COLOR);
273
    clear();
1555 palkovsky 274
    draw_pixmap(_binary_helenos_ppm_start, (size_t)&_binary_helenos_ppm_size, xres-66, 2);
275
    draw_pixmap(_binary_nameic_ppm_start, (size_t)&_binary_nameic_ppm_size, 5, 17);
1522 palkovsky 276
 
277
    for (i=0;i < CONSOLE_COUNT; i++)
1555 palkovsky 278
        redraw_state(i);
1518 palkovsky 279
    vp_switch(console_vp);
280
}
281
 
1558 palkovsky 282
/** Creates a pixmap on framebuffer
283
 *
284
 * @param data PPM data
285
 * @param size PPM data size
286
 * @return Pixmap identification
287
 */
1555 palkovsky 288
static int make_pixmap(char *data, int size)
289
{
290
    char *shm;
291
    int rc;
292
    int pxid = -1;
293
 
294
    /* Create area */
295
    shm = mmap(NULL, size, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
296
    if (shm == MAP_FAILED)
297
        return -1;
298
 
299
    memcpy(shm, data, size);
300
    /* Send area */
1610 palkovsky 301
    rc = async_req_2(fbphone, FB_PREPARE_SHM, (ipcarg_t)shm, 0, NULL, NULL);
1555 palkovsky 302
    if (rc)
303
        goto exit;
1610 palkovsky 304
    rc = async_req_3(fbphone, IPC_M_AS_AREA_SEND, (ipcarg_t)shm, 0, PROTO_READ, NULL, NULL, NULL);
1555 palkovsky 305
    if (rc)
306
        goto drop;
307
 
308
    /* Obtain pixmap */
1610 palkovsky 309
    rc = async_req(fbphone, FB_SHM2PIXMAP, 0, NULL);
1555 palkovsky 310
    if (rc < 0)
311
        goto drop;
312
    pxid = rc;
313
drop:
314
    /* Drop area */
1610 palkovsky 315
    async_msg(fbphone, FB_DROP_SHM, 0);
1555 palkovsky 316
exit:      
317
    /* Remove area */
318
    munmap(shm, size);
319
 
320
    return pxid;
321
}
322
 
1646 palkovsky 323
extern char _binary_anim_1_ppm_start[0];
324
extern int _binary_anim_1_ppm_size;
325
extern char _binary_anim_2_ppm_start[0];
326
extern int _binary_anim_2_ppm_size;
327
extern char _binary_anim_3_ppm_start[0];
328
extern int _binary_anim_3_ppm_size;
329
extern char _binary_anim_4_ppm_start[0];
330
extern int _binary_anim_4_ppm_size;
331
static void make_anim(void)
332
{
333
    int an;
334
    int pm;
335
 
336
    an = async_req(fbphone, FB_ANIM_CREATE, cstatus_vp[KERNEL_CONSOLE], NULL);
337
    if (an < 0)
338
        return;
339
 
340
    pm = make_pixmap(_binary_anim_1_ppm_start, (int)&_binary_anim_1_ppm_size);
341
    async_msg_2(fbphone, FB_ANIM_ADDPIXMAP, an, pm);
342
 
343
    pm = make_pixmap(_binary_anim_2_ppm_start, (int)&_binary_anim_2_ppm_size);
344
    async_msg_2(fbphone, FB_ANIM_ADDPIXMAP, an, pm);
345
 
346
    pm = make_pixmap(_binary_anim_3_ppm_start, (int)&_binary_anim_3_ppm_size);
347
    async_msg_2(fbphone, FB_ANIM_ADDPIXMAP, an, pm);
348
 
349
    pm = make_pixmap(_binary_anim_4_ppm_start, (int)&_binary_anim_4_ppm_size);
350
    async_msg_2(fbphone, FB_ANIM_ADDPIXMAP, an, pm);
351
 
352
    async_msg(fbphone, FB_ANIM_START, an);
353
 
354
    animation = an;
355
}
356
 
1555 palkovsky 357
extern char _binary_cons_selected_ppm_start[0];
358
extern int _binary_cons_selected_ppm_size;
359
extern char _binary_cons_idle_ppm_start[0];
360
extern int _binary_cons_idle_ppm_size;
361
extern char _binary_cons_has_data_ppm_start[0];
362
extern int _binary_cons_has_data_ppm_size;
363
extern char _binary_cons_kernel_ppm_start[0];
364
extern int _binary_cons_kernel_ppm_size;
1518 palkovsky 365
/** Initialize nice graphical console environment */
366
void gcons_init(int phone)
367
{
368
    int rc;
369
    int i;
1646 palkovsky 370
    int status_start = STATUS_START;
1518 palkovsky 371
 
372
    fbphone = phone;
373
 
374
    rc = ipc_call_sync_2(phone, FB_GET_RESOLUTION, 0, 0, &xres, &yres);
375
    if (rc)
376
        return;
377
 
378
    if (xres < 800 || yres < 600)
379
        return;
380
 
381
    /* create console viewport */
1555 palkovsky 382
    /* Align width & height to character size */
383
    console_vp = vp_create(CONSOLE_MARGIN, CONSOLE_TOP,
384
                   ALIGN_DOWN(xres-2*CONSOLE_MARGIN, 8),
385
                   ALIGN_DOWN(yres-(CONSOLE_TOP+CONSOLE_MARGIN),16));
1518 palkovsky 386
    if (console_vp < 0)
387
        return;
388
 
389
    /* Create status buttons */
1646 palkovsky 390
    status_start += (xres-800) / 2;
1518 palkovsky 391
    for (i=0; i < CONSOLE_COUNT; i++) {
1646 palkovsky 392
        cstatus_vp[i] = vp_create(status_start+CONSOLE_MARGIN+i*(STATUS_WIDTH+STATUS_SPACE),
1555 palkovsky 393
                      STATUS_TOP, STATUS_WIDTH, STATUS_HEIGHT);
1518 palkovsky 394
        if (cstatus_vp[i] < 0)
395
            return;
1555 palkovsky 396
        vp_switch(cstatus_vp[i]);
397
        set_style(0x202020, 0xffffff);
1518 palkovsky 398
    }
399
 
1555 palkovsky 400
    /* Initialize icons */
401
    ic_pixmaps[CONS_SELECTED] = make_pixmap(_binary_cons_selected_ppm_start,
402
                          (int)&_binary_cons_selected_ppm_size);
403
    ic_pixmaps[CONS_IDLE] = make_pixmap(_binary_cons_idle_ppm_start,
404
                          (int)&_binary_cons_idle_ppm_size);
405
    ic_pixmaps[CONS_HAS_DATA] = make_pixmap(_binary_cons_has_data_ppm_start,
406
                        (int)&_binary_cons_has_data_ppm_size);
407
    ic_pixmaps[CONS_DISCONNECTED] = make_pixmap(_binary_cons_idle_ppm_start,
408
                          (int)&_binary_cons_idle_ppm_size);
409
    ic_pixmaps[CONS_KERNEL] = make_pixmap(_binary_cons_kernel_ppm_start,
410
                          (int)&_binary_cons_kernel_ppm_size);
411
    ic_pixmaps[CONS_DISCONNECTED_SEL] = ic_pixmaps[CONS_SELECTED];
1646 palkovsky 412
 
413
    make_anim();
1555 palkovsky 414
 
1518 palkovsky 415
    use_gcons = 1;
1555 palkovsky 416
    console_state[0] = CONS_DISCONNECTED_SEL;
417
    console_state[KERNEL_CONSOLE] = CONS_KERNEL;
1522 palkovsky 418
    gcons_redraw_console();
1518 palkovsky 419
}