Beispiel #1
0
QString Console::ReadChar()
{
    if (l->isRunning())          // Not re-enterent
    {
        return QString("\n");
    }
    else
    {
        activateWindow();
        while (1)
        {
            if (InputAvailable())
            {
                QString firstChar = inputBuffer.left(1);
                inputBuffer.remove(0, 1);
                return firstChar;
            }
            // This is a bit tricky and I hope it works on all platforms. If there aren't any
            // characters in the buffer, start a new event loop to wait for keystrokes and block on
            // it. Cannot use semaphores or busy-wait since both actions are on the same thread.
            //
            l->exec();
        }
    }
}
void CheckTimeout(void) {

  char command[80];
  int time;
  U64 nps;

  // Report search speed

  if (!(nodes % 1000000)) DisplaySpeed();

  // We check for timeout or new commands only every so often, 
  // to save some time, unless the engine is operating
  // in the weakening mode or has received "go nodes" command. 
  // In that cases, we check for timeout as often as we can.
  
  if (!Timer.special_mode || Timer.nps_limit > 65535) {
    if (nodes & 4095 || root_depth == 1)
      return;
  }

  if (Timer.GetData(MAX_NODES) > 0
  && nodes >= Timer.GetData(MAX_NODES) ) {
     abort_search = 1;
     return;
  }

  // Slowdown loop

  if (Timer.nps_limit && root_depth > 1) {
    time = Timer.GetElapsedTime() + 1;
    nps = GetNps(time);
    while ((int)nps > Timer.nps_limit) {
      Timer.WasteTime(10);
      time = Timer.GetElapsedTime() + 1;
      nps = GetNps(time);
      if (Timeout()) {
        abort_search = 1;
        return;
      }
    }
  }

  // Process commands that might terminatethe search

  if (InputAvailable()) {
    ReadLine(command, sizeof(command));

    if (strcmp(command, "stop") == 0)
      abort_search = 1;
    else if (strcmp(command, "ponderhit") == 0)
      pondering = 0;
  }

  // Have we already used our allocated time?

  if (Timeout()) abort_search = 1;
}
//++ ------------------------------------------------------------------------------------
// Details: The monitoring on new line data calls back to the visitor object registered
//          with *this stdin monitoring. The monitoring to stops when the visitor returns
//          true for bYesExit flag. Errors output to log file.
//          This function runs in the thread "MI stdin monitor".
// Type:    Method.
//          vrwbYesAlive    - (W) False = yes exit stdin monitoring, true = continue monitor.
// Return:  MIstatus::success - Functional succeeded.
//          MIstatus::failure - Functional failed.
// Throws:  None.
//--
bool
CMICmnStreamStdin::MonitorStdin(bool &vrwbYesAlive)
{
    if (m_bShowPrompt)
    {
        CMICmnStreamStdout &rStdoutMan = CMICmnStreamStdout::Instance();
        rStdoutMan.WriteMIResponse(m_strPromptCurrent.c_str());
        m_bRedrawPrompt = false;
    }

    // CODETAG_DEBUG_SESSION_RUNNING_PROG_RECEIVED_SIGINT_PAUSE_PROGRAM
    if (m_bKeyCtrlCHit)
    {
        CMIDriver &rMIDriver = CMIDriver::Instance();
        rMIDriver.SetExitApplicationFlag(false);
        if (rMIDriver.GetExitApplicationFlag())
        {
            vrwbYesAlive = false;
            return MIstatus::success;
        }

        // Reset - the MI Driver received SIGINT during a running debug programm session
        m_bKeyCtrlCHit = false;
    }

#if MICONFIG_POLL_FOR_STD_IN
    bool bAvail = true;
    // Check if there is stdin available
    if (InputAvailable(bAvail))
    {
        // Early exit when there is no input
        if (!bAvail)
            return MIstatus::success;
    }
    else
    {
        vrwbYesAlive = false;
        CMIDriver::Instance().SetExitApplicationFlag(true);
        return MIstatus::failure;
    }
#endif // MICONFIG_POLL_FOR_STD_IN

    // Read a line from std input
    CMIUtilString stdinErrMsg;
    const MIchar *pText = ReadLine(stdinErrMsg);

    // Did something go wrong
    const bool bHaveError(!stdinErrMsg.empty());
    if ((pText == nullptr) || bHaveError)
    {
        if (bHaveError)
        {
            CMICmnStreamStdout::Instance().Write(stdinErrMsg);
        }
        return MIstatus::failure;
    }

    // We have text so send it off to the visitor
    bool bOk = MIstatus::success;
    if (m_pVisitor != nullptr)
    {
        bool bYesExit = false;
        bOk = m_pVisitor->ReadLine(CMIUtilString(pText), bYesExit);
        m_bRedrawPrompt = true;
        vrwbYesAlive = !bYesExit;
    }

    return bOk;
}