int wgetch()

in graphics/pdcurs34/pdcurses/pdc_getch.c [210:393]


int wgetch(WINDOW *win)
{
#ifndef CONFIG_PDCURSES_MULTITHREAD
  static int buffer[_INBUFSIZ]; /* character buffer */
#endif
  int key, waitcount;
#ifdef CONFIG_PDCURSES_MULTITHREAD
  FAR struct pdc_context_s *ctx = PDC_ctx();
#endif

  PDC_LOG(("wgetch() - called\n"));

  if (!win)
    {
      return ERR;
    }

  waitcount = 0;

  /* Set the number of 1/20th second napms() calls */

  if (SP->delaytenths)
    {
      waitcount = 2 * SP->delaytenths;
    }
  else if (win->_delayms)
    {
      /* Can't really do millisecond intervals, so delay in 1/20ths of a
       * second (50ms).
       */

      waitcount = win->_delayms / 50;
      if (!waitcount)
        {
          waitcount = 1;
        }
    }

  /* refresh window when wgetch is called if there have been changes to it
   * and it is not a pad.
   */

  if (!(win->_flags & _PAD) &&
      ((!win->_leaveit &&
       (win->_begx + win->_curx != SP->curscol ||
        win->_begy + win->_cury != SP->cursrow)) ||
       is_wintouched(win)))
    {
      wrefresh(win);
    }

  /* if ungotten char exists, remove and return it */

  if (c_ungind)
    {
      return c_ungch[--c_ungind];
    }

  /* if normal and data in buffer */

  if ((!SP->raw_inp && !SP->cbreak) && (c_gindex < c_pindex))
    {
#ifndef CONFIG_PDCURSES_MULTITHREAD
      return buffer[c_gindex++];
#else
      return c_buffer[c_gindex++];
#endif
    }

  /* prepare to buffer data */

  c_pindex = 0;
  c_gindex = 0;

  /* to get here, no keys are buffered. go and get one. */

  for (;;)                      /* loop for any buffering */
    {
      /* is there a keystroke ready? */

      if (!PDC_check_key())
        {
          /* if not, handle timeout() and halfdelay() */

          if (SP->delaytenths || win->_delayms)
            {
              if (!waitcount)
                {
                  return ERR;
                }

              waitcount--;
            }
          else if (win->_nodelay)
            {
              return ERR;
            }

          napms(50);            /* Sleep for 1/20th second */
          continue;             /* Then check again */
        }

      /* If there is, fetch it */

      key = PDC_get_key();

      if (SP->key_code)
        {
          /* Filter special keys if not in keypad mode */

          if (!win->_use_keypad)
            {
              key = -1;
            }

          /* Filter mouse events; translate mouse clicks in the slk area to
           * function keys.
           */

          else if (key == KEY_MOUSE)
            {
              key = _mouse_key(win);
            }
        }

      /* Unwanted key? loop back */

      if (key == -1)
        {
          continue;
        }

      /* Translate CR */

      if (key == '\r' && SP->autocr && !SP->raw_inp)
        {
          key = '\n';
        }

      /* If echo is enabled */

      if (SP->echo && !SP->key_code)
        {
          waddch(win, key);
          wrefresh(win);
        }

      /* If no buffering */

      if (SP->raw_inp || SP->cbreak)
        {
          return key;
        }

      /* If no overflow, put data in buffer */

      if (key == '\b')
        {
          if (c_pindex > c_gindex)
            {
              c_pindex--;
            }
        }
      else if (c_pindex < _INBUFSIZ - 2)
        {
#ifndef CONFIG_PDCURSES_MULTITHREAD
          buffer[c_pindex++] = key;
#else
          c_buffer[c_pindex++] = key;
#endif
        }

      /* If we got a line */

      if (key == '\n' || key == '\r')
        {
#ifndef CONFIG_PDCURSES_MULTITHREAD
          return buffer[c_gindex++];
#else
          return c_buffer[c_gindex++];
#endif
        }
    }
}