Subversion Repositories HelenOS

Rev

Rev 2070 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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.  
  57. static int use_gcons = 0;
  58. static ipcarg_t xres,yres;
  59.  
  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.  
  70. static int console_vp;
  71. static int cstatus_vp[CONSOLE_COUNT];
  72. static enum butstate console_state[CONSOLE_COUNT];
  73.  
  74. static int fbphone;
  75.  
  76. /** List of pixmaps identifying these icons */
  77. static int ic_pixmaps[CONS_LAST] = {-1, -1, -1, -1, -1, -1};
  78. static int animation = -1;
  79.  
  80. static int active_console = 0;
  81.  
  82. static void vp_switch(int vp)
  83. {
  84.     async_msg(fbphone,FB_VIEWPORT_SWITCH, vp);
  85. }
  86.  
  87. /** Create view port */
  88. static int vp_create(unsigned int x, unsigned int y,
  89.              unsigned int width, unsigned int height)
  90. {
  91.     return async_req_2(fbphone, FB_VIEWPORT_CREATE,
  92.                (x << 16) | y, (width << 16) | height, NULL, NULL);
  93. }
  94.  
  95. static void clear(void)
  96. {
  97.     async_msg(fbphone, FB_CLEAR, 0);
  98. }
  99.  
  100. static void set_style(int fgcolor, int bgcolor)
  101. {
  102.     async_msg_2(fbphone, FB_SET_STYLE, fgcolor, bgcolor);
  103. }
  104.  
  105. /** Transparent putchar */
  106. static void tran_putch(char c, int row, int col)
  107. {
  108.     async_msg_3(fbphone, FB_TRANS_PUTCHAR, c, row, col);
  109. }
  110.  
  111. /** Redraw the button showing state of a given console */
  112. static void redraw_state(int consnum)
  113. {
  114.     char data[5];
  115.     int i;
  116.     enum butstate state = console_state[consnum];
  117.  
  118.     vp_switch(cstatus_vp[consnum]);
  119.     if (ic_pixmaps[state] != -1)
  120.         async_msg_2(fbphone, FB_VP_DRAW_PIXMAP, cstatus_vp[consnum],
  121.             ic_pixmaps[state]);
  122.  
  123.     if (state != CONS_DISCONNECTED && state != CONS_KERNEL &&
  124.         state != CONS_DISCONNECTED_SEL) {
  125.         snprintf(data, 5, "%d", consnum + 1);
  126.         for (i = 0; data[i]; i++)
  127.             tran_putch(data[i], 1, 2 + i);
  128.     }
  129. }
  130.  
  131. /** Notification run on changing console (except kernel console) */
  132. void gcons_change_console(int consnum)
  133. {
  134.     int i;
  135.  
  136.     if (!use_gcons)
  137.         return;
  138.  
  139.     if (active_console == KERNEL_CONSOLE) {
  140.         for (i = 0; i < CONSOLE_COUNT; i++)
  141.             redraw_state(i);
  142.         if (animation != -1)
  143.             async_msg(fbphone, FB_ANIM_START, animation);
  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.     }
  151.     active_console = consnum;
  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);
  159.  
  160.     vp_switch(console_vp);
  161. }
  162.  
  163. /** Notification function that gets called on new output to virtual console */
  164. void gcons_notify_char(int consnum)
  165. {
  166.     if (!use_gcons)
  167.         return;
  168.  
  169.     if (consnum == active_console ||
  170.         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 */
  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.  
  200. /** Notification function called on console connect */
  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);
  215. }
  216.  
  217. /** Change to kernel console */
  218. void 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. /** Return x, where left <= x <= right && |a-x|==min(|a-x|) is smallest */
  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. int mouse_x, mouse_y;
  244. int 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.  */
  251. void gcons_mouse_move(int dx, int dy)
  252. {
  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.  
  259. static int gcons_find_conbut(int x, int y)
  260. {
  261.     int status_start = STATUS_START + (xres - 800) / 2;;
  262.  
  263.     if (y < STATUS_TOP || y >= STATUS_TOP + STATUS_HEIGHT)
  264.         return -1;
  265.    
  266.     if (x < status_start)
  267.         return -1;
  268.    
  269.     if (x >= status_start + (STATUS_WIDTH + STATUS_SPACE) * CONSOLE_COUNT)
  270.         return -1;
  271.     if (((x - status_start) % (STATUS_WIDTH+STATUS_SPACE)) < STATUS_SPACE)
  272.         return -1;
  273.    
  274.     return (x - status_start) / (STATUS_WIDTH+STATUS_SPACE);
  275. }
  276.  
  277. /** Handle mouse click
  278.  *
  279.  * @param state New state (1-pressed, 0-depressed)
  280.  */
  281. int 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;
  302. }
  303.  
  304.  
  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.  */
  312. static void draw_pixmap(char *logo, size_t size, int x, int y)
  313. {
  314.     char *shm;
  315.     int rc;
  316.  
  317.     /* Create area */
  318.     shm = mmap(NULL, size, PROTO_READ | PROTO_WRITE, MAP_SHARED |
  319.         MAP_ANONYMOUS, 0, 0);
  320.     if (shm == MAP_FAILED)
  321.         return;
  322.  
  323.     memcpy(shm, logo, size);
  324.     /* Send area */
  325.     rc = async_req_2(fbphone, FB_PREPARE_SHM, (ipcarg_t) shm, 0, NULL,
  326.         NULL);
  327.     if (rc)
  328.         goto exit;
  329.     rc = async_req_3(fbphone, IPC_M_AS_AREA_SEND, (ipcarg_t) shm, 0,
  330.         PROTO_READ, NULL, NULL, NULL);
  331.     if (rc)
  332.         goto drop;
  333.     /* Draw logo */
  334.     async_msg_2(fbphone, FB_DRAW_PPM, x, y);
  335. drop:
  336.     /* Drop area */
  337.     async_msg(fbphone, FB_DROP_SHM, 0);
  338. exit:      
  339.     /* Remove area */
  340.     munmap(shm, size);
  341. }
  342.  
  343. extern char _binary_helenos_ppm_start[0];
  344. extern int _binary_helenos_ppm_size;
  345. extern char _binary_nameic_ppm_start[0];
  346. extern int _binary_nameic_ppm_size;
  347. /** Redraws console graphics  */
  348. static void gcons_redraw_console(void)
  349. {
  350.     int i;
  351.  
  352.     if (!use_gcons)
  353.         return;
  354.    
  355.     vp_switch(0);
  356.     set_style(MAIN_COLOR, MAIN_COLOR);
  357.     clear();
  358.     draw_pixmap(_binary_helenos_ppm_start,
  359.         (size_t) &_binary_helenos_ppm_size, xres - 66, 2);
  360.     draw_pixmap(_binary_nameic_ppm_start,
  361.         (size_t) &_binary_nameic_ppm_size, 5, 17);
  362.  
  363.     for (i = 0;i < CONSOLE_COUNT; i++)
  364.         redraw_state(i);
  365.     vp_switch(console_vp);
  366. }
  367.  
  368. /** Creates a pixmap on framebuffer
  369.  *
  370.  * @param data PPM data
  371.  * @param size PPM data size
  372.  * @return Pixmap identification
  373.  */
  374. static int make_pixmap(char *data, int size)
  375. {
  376.     char *shm;
  377.     int rc;
  378.     int pxid = -1;
  379.  
  380.     /* Create area */
  381.     shm = mmap(NULL, size, PROTO_READ | PROTO_WRITE, MAP_SHARED |
  382.         MAP_ANONYMOUS, 0, 0);
  383.     if (shm == MAP_FAILED)
  384.         return -1;
  385.  
  386.     memcpy(shm, data, size);
  387.     /* Send area */
  388.     rc = async_req_2(fbphone, FB_PREPARE_SHM, (ipcarg_t) shm, 0, NULL,
  389.         NULL);
  390.     if (rc)
  391.         goto exit;
  392.     rc = async_req_3(fbphone, IPC_M_AS_AREA_SEND, (ipcarg_t) shm, 0,
  393.         PROTO_READ, NULL, NULL, NULL);
  394.     if (rc)
  395.         goto drop;
  396.  
  397.     /* Obtain pixmap */
  398.     rc = async_req(fbphone, FB_SHM2PIXMAP, 0, NULL);
  399.     if (rc < 0)
  400.         goto drop;
  401.     pxid = rc;
  402. drop:
  403.     /* Drop area */
  404.     async_msg(fbphone, FB_DROP_SHM, 0);
  405. exit:      
  406.     /* Remove area */
  407.     munmap(shm, size);
  408.  
  409.     return pxid;
  410. }
  411.  
  412. extern char _binary_anim_1_ppm_start[0];
  413. extern int _binary_anim_1_ppm_size;
  414. extern char _binary_anim_2_ppm_start[0];
  415. extern int _binary_anim_2_ppm_size;
  416. extern char _binary_anim_3_ppm_start[0];
  417. extern int _binary_anim_3_ppm_size;
  418. extern char _binary_anim_4_ppm_start[0];
  419. extern int _binary_anim_4_ppm_size;
  420.  
  421. static void make_anim(void)
  422. {
  423.     int an;
  424.     int pm;
  425.  
  426.     an = async_req(fbphone, FB_ANIM_CREATE, cstatus_vp[KERNEL_CONSOLE],
  427.         NULL);
  428.     if (an < 0)
  429.         return;
  430.  
  431.     pm = make_pixmap(_binary_anim_1_ppm_start,
  432.         (int) &_binary_anim_1_ppm_size);
  433.     async_msg_2(fbphone, FB_ANIM_ADDPIXMAP, an, pm);
  434.  
  435.     pm = make_pixmap(_binary_anim_2_ppm_start,
  436.         (int) &_binary_anim_2_ppm_size);
  437.     async_msg_2(fbphone, FB_ANIM_ADDPIXMAP, an, pm);
  438.  
  439.     pm = make_pixmap(_binary_anim_3_ppm_start,
  440.         (int) &_binary_anim_3_ppm_size);
  441.     async_msg_2(fbphone, FB_ANIM_ADDPIXMAP, an, pm);
  442.  
  443.     pm = make_pixmap(_binary_anim_4_ppm_start,
  444.         (int) &_binary_anim_4_ppm_size);
  445.     async_msg_2(fbphone, FB_ANIM_ADDPIXMAP, an, pm);
  446.  
  447.     async_msg(fbphone, FB_ANIM_START, an);
  448.  
  449.     animation = an;
  450. }
  451.  
  452. extern char _binary_cons_selected_ppm_start[0];
  453. extern int _binary_cons_selected_ppm_size;
  454. extern char _binary_cons_idle_ppm_start[0];
  455. extern int _binary_cons_idle_ppm_size;
  456. extern char _binary_cons_has_data_ppm_start[0];
  457. extern int _binary_cons_has_data_ppm_size;
  458. extern char _binary_cons_kernel_ppm_start[0];
  459. extern int _binary_cons_kernel_ppm_size;
  460.  
  461. /** Initialize nice graphical console environment */
  462. void gcons_init(int phone)
  463. {
  464.     int rc;
  465.     int i;
  466.     int status_start = STATUS_START;
  467.  
  468.     fbphone = phone;
  469.  
  470.     rc = async_req_2(phone, FB_GET_RESOLUTION, 0, 0, &xres, &yres);
  471.     if (rc)
  472.         return;
  473.    
  474.     if (xres < 800 || yres < 600)
  475.         return;
  476.  
  477.     /* create console viewport */
  478.     /* Align width & height to character size */
  479.     console_vp = vp_create(CONSOLE_MARGIN, CONSOLE_TOP,
  480.         ALIGN_DOWN(xres - 2 * CONSOLE_MARGIN, 8),
  481.         ALIGN_DOWN(yres - (CONSOLE_TOP + CONSOLE_MARGIN), 16));
  482.     if (console_vp < 0)
  483.         return;
  484.    
  485.     /* Create status buttons */
  486.     status_start += (xres - 800) / 2;
  487.     for (i = 0; i < CONSOLE_COUNT; i++) {
  488.         cstatus_vp[i] = vp_create(status_start + CONSOLE_MARGIN +
  489.             i * (STATUS_WIDTH + STATUS_SPACE), STATUS_TOP,
  490.             STATUS_WIDTH, STATUS_HEIGHT);
  491.         if (cstatus_vp[i] < 0)
  492.             return;
  493.         vp_switch(cstatus_vp[i]);
  494.         set_style(0x202020, 0xffffff);
  495.     }
  496.    
  497.     /* Initialize icons */
  498.     ic_pixmaps[CONS_SELECTED] =
  499.         make_pixmap(_binary_cons_selected_ppm_start,
  500.         (int) &_binary_cons_selected_ppm_size);
  501.     ic_pixmaps[CONS_IDLE] = make_pixmap(_binary_cons_idle_ppm_start,
  502.         (int) &_binary_cons_idle_ppm_size);
  503.     ic_pixmaps[CONS_HAS_DATA] =
  504.         make_pixmap(_binary_cons_has_data_ppm_start,
  505.         (int) &_binary_cons_has_data_ppm_size);
  506.     ic_pixmaps[CONS_DISCONNECTED] =
  507.         make_pixmap(_binary_cons_idle_ppm_start,
  508.         (int) &_binary_cons_idle_ppm_size);
  509.     ic_pixmaps[CONS_KERNEL] = make_pixmap(_binary_cons_kernel_ppm_start,
  510.         (int) &_binary_cons_kernel_ppm_size);
  511.     ic_pixmaps[CONS_DISCONNECTED_SEL] = ic_pixmaps[CONS_SELECTED];
  512.    
  513.     make_anim();
  514.  
  515.     use_gcons = 1;
  516.     console_state[0] = CONS_DISCONNECTED_SEL;
  517.     console_state[KERNEL_CONSOLE] = CONS_KERNEL;
  518.     gcons_redraw_console();
  519. }
  520.  
  521. /** @}
  522.  */
  523.  
  524.