ExtFunc void CloseRobot(void) { RemoveEventGen(&robotGen); if (robotProcess > 0) RobotCmd(1, "Exit\n"); fclose(toRobot); close(fromRobotFd); }
ExtFunc int RefreshBoard(int scr) { int y, x, any = 0; unsigned int c; BlockType b; for (y = boardVisible[scr] - 1; y >= 0; --y) if ((c = changed[scr][y])) { if (robotEnable) { RobotCmd(0, "RowUpdate %d %d", scr, y); for (x = 0; x < boardWidth[scr]; ++x) { b = board[scr][y][x]; if (fairRobot) b = abs(b); RobotCmd(0, " %d", b); } RobotCmd(0, "\n"); } changed[scr][y] = 0; any = 1; for (x = 0; c; (c >>= 1), (++x)) if ((c & 1) && B_OLD(board[scr][y][x])!=oldBoard[scr][y][x]) { PlotBlock(scr, y, x, B_OLD(board[scr][y][x])); oldBoard[scr][y][x] = B_OLD(board[scr][y][x]); } } if (robotEnable) RobotTimeStamp(); for (x = 0; x < boardWidth[scr]; ++x) if (oldFalling[scr][x] != !!falling[scr][x]) { oldFalling[scr][x] = !!falling[scr][x]; PlotUnderline(scr, x, oldFalling[scr][x]); any = 1; } return any; }
ExtFunc void InitRobot(char *robotProg) { int to[2], from[2]; int status; MyEvent event; signal(SIGPIPE, CatchPipe); AtExit(CloseRobot); if (pipe(to) || pipe(from)) die("pipe"); robotProcess = fork(); if (robotProcess < 0) die("fork"); if (robotProcess == 0) { dup2(to[0], STDIN_FILENO); dup2(from[1], STDOUT_FILENO); close(to[0]); close(to[1]); close(from[0]); close(from[1]); execl("/bin/sh", "sh", "-c", robotProg, NULL); die("execl failed"); } close(to[0]); close(from[1]); toRobotFd = to[1]; robotGen.fd = fromRobotFd = from[0]; if (!(toRobot = fdopen(toRobotFd, "w"))) die("fdopen"); if ((status = fcntl(fromRobotFd, F_GETFL, 0)) < 0) die("fcntl/F_GETFL"); status |= O_NONBLOCK; if (fcntl(fromRobotFd, F_SETFL, status) < 0) die("fcntl/F_SETFL"); AddEventGen(&robotGen); RobotCmd(1, "Version %d\n", ROBOT_VERSION); if (WaitMyEvent(&event, EM_robot) != E_robot) fatal("Robot didn't start successfully"); if (1 > sscanf(event.u.robot.data, "Version %d", &robotVersion) || robotVersion < 1) fatal("Invalid Version line from robot"); if (robotVersion > ROBOT_VERSION) robotVersion = ROBOT_VERSION; }
ExtFunc void RobotTimeStamp(void) { RobotCmd(1, "TimeStamp %.3f\n", CurTimeval() / 1.0e6); }