Subversion Repositories HelenOS-historic

Rev

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