source: mainline/console/gcons.c@ 15039b67

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 15039b67 was 290c0db, checked in by Ondrej Palkovsky <ondrap@…>, 19 years ago

Update to new api.
Fixed problem with hanged up phone when pressing keys during
graphics initialization.

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