source: mainline/uspace/srv/console/gcons.c@ 0cc4313

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0cc4313 was 0cc4313, checked in by Jakub Jermar <jakub@…>, 18 years ago

Modify the async framework to make use of all six syscall arguments.
Supply user-friendly macros as in previous cases.

  • Property mode set to 100644
File size: 12.4 KB
RevLine 
[b1f51f0]1/*
[df4ed85]2 * Copyright (c) 2006 Ondrej Palkovsky
[b1f51f0]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
[ce5bcb4]29/** @addtogroup console
30 * @{
31 */
32/** @file
33 */
34
[b1f51f0]35#include <ipc/fb.h>
36#include <ipc/ipc.h>
[e1c4849]37#include <async.h>
38#include <stdio.h>
[2def788]39#include <sys/mman.h>
[90f5d64]40#include <string.h>
[a7d2d78]41#include <align.h>
[b1f51f0]42
43#include "console.h"
[e1c4849]44#include "gcons.h"
[b1f51f0]45
[0a9a324]46#define CONSOLE_TOP 66
[a7d2d78]47#define CONSOLE_MARGIN 6
[b1f51f0]48
[a7d2d78]49#define STATUS_START 110
50#define STATUS_TOP 8
[0a9a324]51#define STATUS_SPACE 4
[a7d2d78]52#define STATUS_WIDTH 48
53#define STATUS_HEIGHT 48
[b1f51f0]54
[90f5d64]55#define MAIN_COLOR 0xffffff
[e1c4849]56
[b1f51f0]57static int use_gcons = 0;
58static ipcarg_t xres,yres;
59
[a7d2d78]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
[b1f51f0]70static int console_vp;
71static int cstatus_vp[CONSOLE_COUNT];
[a7d2d78]72static enum butstate console_state[CONSOLE_COUNT];
[b1f51f0]73
74static int fbphone;
75
[a7d2d78]76/** List of pixmaps identifying these icons */
[00bb6965]77static int ic_pixmaps[CONS_LAST] = {-1, -1, -1, -1, -1, -1};
[1fd7700]78static int animation = -1;
[e1c4849]79
80static int active_console = 0;
81
[b1f51f0]82static void vp_switch(int vp)
83{
[0cc4313]84 async_msg_1(fbphone,FB_VIEWPORT_SWITCH, vp);
[b1f51f0]85}
86
[e1c4849]87/** Create view port */
[0cc4313]88static int vp_create(unsigned int x, unsigned int y, unsigned int width,
89 unsigned int height)
[b1f51f0]90{
[0cc4313]91 return async_req_2_0(fbphone, FB_VIEWPORT_CREATE, (x << 16) | y,
92 (width << 16) | height);
[b1f51f0]93}
94
[e1c4849]95static void clear(void)
[b1f51f0]96{
[0cc4313]97 async_msg_0(fbphone, FB_CLEAR);
[b1f51f0]98}
99
[e1c4849]100static void set_style(int fgcolor, int bgcolor)
101{
[085bd54]102 async_msg_2(fbphone, FB_SET_STYLE, fgcolor, bgcolor);
[e1c4849]103}
104
[d530237a]105/** Transparent putchar */
106static void tran_putch(char c, int row, int col)
[e1c4849]107{
[085bd54]108 async_msg_3(fbphone, FB_TRANS_PUTCHAR, c, row, col);
[e1c4849]109}
110
[d530237a]111/** Redraw the button showing state of a given console */
[a7d2d78]112static void redraw_state(int consnum)
[b1f51f0]113{
[e1c4849]114 char data[5];
115 int i;
[a7d2d78]116 enum butstate state = console_state[consnum];
117
[e1c4849]118 vp_switch(cstatus_vp[consnum]);
[a7d2d78]119 if (ic_pixmaps[state] != -1)
[00bb6965]120 async_msg_2(fbphone, FB_VP_DRAW_PIXMAP, cstatus_vp[consnum],
[0cc4313]121 ic_pixmaps[state]);
[00bb6965]122
[d7baee6]123 if (state != CONS_DISCONNECTED && state != CONS_KERNEL &&
124 state != CONS_DISCONNECTED_SEL) {
[00bb6965]125 snprintf(data, 5, "%d", consnum + 1);
[d7baee6]126 for (i = 0; data[i]; i++)
[00bb6965]127 tran_putch(data[i], 1, 2 + i);
[a7d2d78]128 }
[b1f51f0]129}
130
[a7d2d78]131/** Notification run on changing console (except kernel console) */
[b1f51f0]132void gcons_change_console(int consnum)
133{
[a7d2d78]134 int i;
135
[b1f51f0]136 if (!use_gcons)
137 return;
[429acb9]138
[a7d2d78]139 if (active_console == KERNEL_CONSOLE) {
[00bb6965]140 for (i = 0; i < CONSOLE_COUNT; i++)
[a7d2d78]141 redraw_state(i);
[70178b74]142 if (animation != -1)
[0cc4313]143 async_msg_1(fbphone, FB_ANIM_START, animation);
[a7d2d78]144 } else {
145 if (console_state[active_console] == CONS_DISCONNECTED_SEL)
146 console_state[active_console] = CONS_DISCONNECTED;
147 else
148 console_state[active_console] = CONS_IDLE;
149 redraw_state(active_console);
150 }
[e1c4849]151 active_console = consnum;
[a7d2d78]152
153 if (console_state[consnum] == CONS_DISCONNECTED) {
154 console_state[consnum] = CONS_DISCONNECTED_SEL;
155 redraw_state(consnum);
156 } else
157 console_state[consnum] = CONS_SELECTED;
158 redraw_state(consnum);
[d6cc453]159
[b1f51f0]160 vp_switch(console_vp);
161}
162
[429acb9]163/** Notification function that gets called on new output to virtual console */
[b1f51f0]164void gcons_notify_char(int consnum)
165{
166 if (!use_gcons)
167 return;
168
[d7baee6]169 if (consnum == active_console ||
170 console_state[consnum] == CONS_HAS_DATA)
[d6cc453]171 return;
172
[a7d2d78]173 console_state[consnum] = CONS_HAS_DATA;
[429acb9]174
[a7d2d78]175 if (active_console == KERNEL_CONSOLE)
[429acb9]176 return;
177
[a7d2d78]178 redraw_state(consnum);
[d6cc453]179
[b1f51f0]180 vp_switch(console_vp);
[a7d2d78]181}
[429acb9]182
[e9073f2]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
[d530237a]200/** Notification function called on console connect */
[a7d2d78]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);
[b1f51f0]215}
216
[429acb9]217/** Change to kernel console */
218void gcons_in_kernel(void)
219{
[a78e273]220 if (console_state[active_console] == CONS_DISCONNECTED_SEL)
[a7d2d78]221 console_state[active_console] = CONS_DISCONNECTED;
222 else
223 console_state[active_console] = CONS_IDLE;
224 redraw_state(active_console);
225
[70178b74]226 if (animation != -1)
[0cc4313]227 async_msg_1(fbphone, FB_ANIM_STOP, animation);
[70178b74]228
[a7d2d78]229 active_console = KERNEL_CONSOLE; /* Set to kernel console */
[429acb9]230 vp_switch(0);
231}
232
[d8b42fb2]233/** Return x, where left <= x <= right && |a-x|==min(|a-x|) is smallest */
[830ac99]234static 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
[1f83244]243int mouse_x, mouse_y;
244int btn_pressed, btn_x, btn_y;
245
246/** Handle mouse move
247 *
248 * @param dx Delta X of mouse move
249 * @param dy Delta Y of mouse move
250 */
[830ac99]251void gcons_mouse_move(int dx, int dy)
252{
[1f83244]253 mouse_x = limit(mouse_x+dx, 0, xres);
254 mouse_y = limit(mouse_y+dy, 0, yres);
255
256 async_msg_2(fbphone, FB_POINTER_MOVE, mouse_x, mouse_y);
257}
258
259static int gcons_find_conbut(int x, int y)
260{
[00bb6965]261 int status_start = STATUS_START + (xres - 800) / 2;;
[830ac99]262
[00bb6965]263 if (y < STATUS_TOP || y >= STATUS_TOP + STATUS_HEIGHT)
[1f83244]264 return -1;
265
266 if (x < status_start)
267 return -1;
268
[00bb6965]269 if (x >= status_start + (STATUS_WIDTH + STATUS_SPACE) * CONSOLE_COUNT)
[1f83244]270 return -1;
[b34fab6]271 if (((x - status_start) % (STATUS_WIDTH+STATUS_SPACE)) < STATUS_SPACE)
[1f83244]272 return -1;
273
[00bb6965]274 return (x - status_start) / (STATUS_WIDTH+STATUS_SPACE);
[1f83244]275}
[830ac99]276
[1f83244]277/** Handle mouse click
278 *
279 * @param state New state (1-pressed, 0-depressed)
280 */
281int gcons_mouse_btn(int state)
282{
283 int conbut;
284
285 if (state) {
286 conbut = gcons_find_conbut(mouse_x, mouse_y);
287 if (conbut != -1) {
288 btn_pressed = 1;
289 btn_x = mouse_x;
290 btn_y = mouse_y;
291 }
292 return -1;
293 }
294 if (!state && !btn_pressed)
295 return -1;
296 btn_pressed = 0;
297
298 conbut = gcons_find_conbut(mouse_x, mouse_y);
299 if (conbut == gcons_find_conbut(btn_x, btn_y))
300 return conbut;
301 return -1;
[830ac99]302}
303
304
[429acb9]305/** Draw a PPM pixmap to framebuffer
306 *
307 * @param logo Pointer to PPM data
308 * @param size Size of PPM data
309 * @param x Coordinate of upper left corner
310 * @param y Coordinate of upper left corner
311 */
[90f5d64]312static void draw_pixmap(char *logo, size_t size, int x, int y)
313{
314 char *shm;
315 int rc;
316
317 /* Create area */
[00bb6965]318 shm = mmap(NULL, size, PROTO_READ | PROTO_WRITE, MAP_SHARED |
[0cc4313]319 MAP_ANONYMOUS, 0, 0);
[90f5d64]320 if (shm == MAP_FAILED)
321 return;
322
323 memcpy(shm, logo, size);
324 /* Send area */
[0cc4313]325 rc = async_req_1_0(fbphone, FB_PREPARE_SHM, (ipcarg_t) shm);
[90f5d64]326 if (rc)
327 goto exit;
[0cc4313]328 rc = async_req_3_0(fbphone, IPC_M_AS_AREA_SEND, (ipcarg_t) shm, 0,
329 PROTO_READ);
[90f5d64]330 if (rc)
331 goto drop;
332 /* Draw logo */
[085bd54]333 async_msg_2(fbphone, FB_DRAW_PPM, x, y);
[90f5d64]334drop:
335 /* Drop area */
[0cc4313]336 async_msg_0(fbphone, FB_DROP_SHM);
[90f5d64]337exit:
338 /* Remove area */
339 munmap(shm, size);
340}
341
342extern char _binary_helenos_ppm_start[0];
343extern int _binary_helenos_ppm_size;
344extern char _binary_nameic_ppm_start[0];
345extern int _binary_nameic_ppm_size;
[d530237a]346/** Redraws console graphics */
[a7d2d78]347static void gcons_redraw_console(void)
[b1f51f0]348{
[e1c4849]349 int i;
350
[b1f51f0]351 if (!use_gcons)
352 return;
353
354 vp_switch(0);
[e1c4849]355 set_style(MAIN_COLOR, MAIN_COLOR);
356 clear();
[d7baee6]357 draw_pixmap(_binary_helenos_ppm_start,
[0cc4313]358 (size_t) &_binary_helenos_ppm_size, xres - 66, 2);
[d7baee6]359 draw_pixmap(_binary_nameic_ppm_start,
[0cc4313]360 (size_t) &_binary_nameic_ppm_size, 5, 17);
[90f5d64]361
[0cc4313]362 for (i = 0; i < CONSOLE_COUNT; i++)
[a7d2d78]363 redraw_state(i);
[b1f51f0]364 vp_switch(console_vp);
365}
366
[d530237a]367/** Creates a pixmap on framebuffer
368 *
369 * @param data PPM data
370 * @param size PPM data size
371 * @return Pixmap identification
372 */
[a7d2d78]373static int make_pixmap(char *data, int size)
374{
375 char *shm;
376 int rc;
377 int pxid = -1;
378
379 /* Create area */
[00bb6965]380 shm = mmap(NULL, size, PROTO_READ | PROTO_WRITE, MAP_SHARED |
[0cc4313]381 MAP_ANONYMOUS, 0, 0);
[a7d2d78]382 if (shm == MAP_FAILED)
383 return -1;
384
385 memcpy(shm, data, size);
386 /* Send area */
[0cc4313]387 rc = async_req_1_0(fbphone, FB_PREPARE_SHM, (ipcarg_t) shm);
[a7d2d78]388 if (rc)
389 goto exit;
[0cc4313]390 rc = async_req_3_0(fbphone, IPC_M_AS_AREA_SEND, (ipcarg_t) shm, 0,
391 PROTO_READ);
[a7d2d78]392 if (rc)
393 goto drop;
394
395 /* Obtain pixmap */
[0cc4313]396 rc = async_req_0_0(fbphone, FB_SHM2PIXMAP);
[a7d2d78]397 if (rc < 0)
398 goto drop;
399 pxid = rc;
400drop:
401 /* Drop area */
[0cc4313]402 async_msg_0(fbphone, FB_DROP_SHM);
[a7d2d78]403exit:
404 /* Remove area */
405 munmap(shm, size);
406
407 return pxid;
408}
409
[1fd7700]410extern char _binary_anim_1_ppm_start[0];
411extern int _binary_anim_1_ppm_size;
412extern char _binary_anim_2_ppm_start[0];
413extern int _binary_anim_2_ppm_size;
414extern char _binary_anim_3_ppm_start[0];
415extern int _binary_anim_3_ppm_size;
416extern char _binary_anim_4_ppm_start[0];
417extern int _binary_anim_4_ppm_size;
[00bb6965]418
[1fd7700]419static void make_anim(void)
420{
421 int an;
422 int pm;
423
[0cc4313]424 an = async_req_1_0(fbphone, FB_ANIM_CREATE, cstatus_vp[KERNEL_CONSOLE]);
[1fd7700]425 if (an < 0)
426 return;
427
[d7baee6]428 pm = make_pixmap(_binary_anim_1_ppm_start,
[0cc4313]429 (int) &_binary_anim_1_ppm_size);
[1fd7700]430 async_msg_2(fbphone, FB_ANIM_ADDPIXMAP, an, pm);
431
[d7baee6]432 pm = make_pixmap(_binary_anim_2_ppm_start,
[0cc4313]433 (int) &_binary_anim_2_ppm_size);
[1fd7700]434 async_msg_2(fbphone, FB_ANIM_ADDPIXMAP, an, pm);
435
[d7baee6]436 pm = make_pixmap(_binary_anim_3_ppm_start,
[0cc4313]437 (int) &_binary_anim_3_ppm_size);
[1fd7700]438 async_msg_2(fbphone, FB_ANIM_ADDPIXMAP, an, pm);
439
[d7baee6]440 pm = make_pixmap(_binary_anim_4_ppm_start,
[0cc4313]441 (int) &_binary_anim_4_ppm_size);
[1fd7700]442 async_msg_2(fbphone, FB_ANIM_ADDPIXMAP, an, pm);
443
[0cc4313]444 async_msg_1(fbphone, FB_ANIM_START, an);
[1fd7700]445
446 animation = an;
447}
448
[a7d2d78]449extern char _binary_cons_selected_ppm_start[0];
450extern int _binary_cons_selected_ppm_size;
451extern char _binary_cons_idle_ppm_start[0];
452extern int _binary_cons_idle_ppm_size;
453extern char _binary_cons_has_data_ppm_start[0];
454extern int _binary_cons_has_data_ppm_size;
455extern char _binary_cons_kernel_ppm_start[0];
456extern int _binary_cons_kernel_ppm_size;
[00bb6965]457
[b1f51f0]458/** Initialize nice graphical console environment */
459void gcons_init(int phone)
460{
461 int rc;
462 int i;
[1fd7700]463 int status_start = STATUS_START;
[b1f51f0]464
465 fbphone = phone;
466
[0cc4313]467 rc = async_req_0_2(phone, FB_GET_RESOLUTION, &xres, &yres);
[b1f51f0]468 if (rc)
469 return;
470
471 if (xres < 800 || yres < 600)
472 return;
473
474 /* create console viewport */
[a7d2d78]475 /* Align width & height to character size */
[d7baee6]476 console_vp = vp_create(CONSOLE_MARGIN, CONSOLE_TOP,
[0cc4313]477 ALIGN_DOWN(xres - 2 * CONSOLE_MARGIN, 8),
478 ALIGN_DOWN(yres - (CONSOLE_TOP + CONSOLE_MARGIN), 16));
[b1f51f0]479 if (console_vp < 0)
480 return;
481
482 /* Create status buttons */
[00bb6965]483 status_start += (xres - 800) / 2;
484 for (i = 0; i < CONSOLE_COUNT; i++) {
[d7baee6]485 cstatus_vp[i] = vp_create(status_start + CONSOLE_MARGIN +
[0cc4313]486 i * (STATUS_WIDTH + STATUS_SPACE), STATUS_TOP,
487 STATUS_WIDTH, STATUS_HEIGHT);
[b1f51f0]488 if (cstatus_vp[i] < 0)
489 return;
[a7d2d78]490 vp_switch(cstatus_vp[i]);
491 set_style(0x202020, 0xffffff);
[b1f51f0]492 }
493
[a7d2d78]494 /* Initialize icons */
[00bb6965]495 ic_pixmaps[CONS_SELECTED] =
[0cc4313]496 make_pixmap(_binary_cons_selected_ppm_start,
497 (int) &_binary_cons_selected_ppm_size);
[d7baee6]498 ic_pixmaps[CONS_IDLE] = make_pixmap(_binary_cons_idle_ppm_start,
[0cc4313]499 (int) &_binary_cons_idle_ppm_size);
[00bb6965]500 ic_pixmaps[CONS_HAS_DATA] =
[0cc4313]501 make_pixmap(_binary_cons_has_data_ppm_start,
502 (int) &_binary_cons_has_data_ppm_size);
[00bb6965]503 ic_pixmaps[CONS_DISCONNECTED] =
[0cc4313]504 make_pixmap(_binary_cons_idle_ppm_start,
505 (int) &_binary_cons_idle_ppm_size);
[a7d2d78]506 ic_pixmaps[CONS_KERNEL] = make_pixmap(_binary_cons_kernel_ppm_start,
[0cc4313]507 (int) &_binary_cons_kernel_ppm_size);
[a7d2d78]508 ic_pixmaps[CONS_DISCONNECTED_SEL] = ic_pixmaps[CONS_SELECTED];
[1fd7700]509
510 make_anim();
[a7d2d78]511
[b1f51f0]512 use_gcons = 1;
[a7d2d78]513 console_state[0] = CONS_DISCONNECTED_SEL;
514 console_state[KERNEL_CONSOLE] = CONS_KERNEL;
[e1c4849]515 gcons_redraw_console();
[b1f51f0]516}
[ce5bcb4]517
518/** @}
519 */
520
Note: See TracBrowser for help on using the repository browser.