Exemplo n.º 1
0
void DemonCalc::run(){
	int i  = game.getSteps();
	while(true){
		if(game.getState() == FINISHED){
			showResult();

			printw("\nPress A to enter the next level, anything else to exit: ");
			refresh();
			int ch = getchar();
			if(ch == 'a'){
				// game.changeSteps(game.getSteps()+1);
				game.addSteps();
				preRun();
				i = game.getSteps();
			}else{
				break;		
			}
		}

		clear();
		std::stringstream ss;
		std::string q;
		if(i < game.getQuestionSize()){
			q = game.getQuestionStr(i);
		}else{
			q = "? + ? = ?";
		}
		ss<<"No."<<(i+1)<<": "<<q<<std::endl;
		mvscrolltxt(0, 0, ss.str());
		ss.flush();

		mvprintw(1,0, "Please enter the result of Question No.%d: ", (i-game.getSteps()+1));
		refresh();

		int input;
		int r, c;
		getyx(stdscr, r, c);
		while((input = getchar())){
			if(input <= '9' && input >= '0'){
				break;
			}
			mvprintw(r+1, 0, "Please enter valid input : 0-9");
			move(r, c);
			refresh();
		}
		game.update(input-'0');
		i++;
	}


	getchar();
	endwin();
}
Exemplo n.º 2
0
	void run() {
		running = true;
		preRun();
		while (running) {
			if (selector.wait(waitFor())) {
				receive();
			}
			else {
				timeout();
			}
		}
		postRun();
	}
Exemplo n.º 3
0
void
QLuaApplication::Private::Thread::run()
{
  QMutexLocker locker(&mutex);
  preRun();
  QEventLoop theloop;
  loop = &theloop;
  locker.unlock();
  loop->exec();
  locker.relock();
  loop = 0;
  postRun();
}
Exemplo n.º 4
0
void DemonCalc::init(){
	game.reset();
	//init ncurses
	initscr();

	//show welcome msg
	printw("Welcome to Demon Calculation!\n");
	printw("You will start at level %d\n", game.getSteps());
	printw("Press A to begin, press anything else to exit...\n");
	refresh();

	int ch = getchar();
	switch(ch){
		case 'a':
			preRun();
			run();
			break;
		default:
			getchar();
			endwin();
	}
}
Exemplo n.º 5
0
/**
  @brief Launch program made up of tokens in parameter TokenContainer and terminate when done
  @param tc TokenContainer struct with both the tokens and their count
  @return true to continue execution of the shell
  */
bool launchCommands(TokenContainer *tc, char *input)
{
    pid_t child;
    struct rusage start;
    bool bg = checkBackgroundJob(tc);

    preRun(tc);
    getrusage(RUSAGE_CHILDREN, &start);

    addToHistory(input);

    if (strcmp(tc->tokens[0], "last10") == 0)
        printLastTen();
    else if (strcmp(tc->tokens[0], "cd") == 0)
        changeDirectory(tc);
    else
    {
        child = fork();

        if(!child)
        {
            if(bg)
            {
                int out = open("/dev/null", O_WRONLY);
                dup2(out, 0);
                dup2(out, 1);
                dup2(out, 2);
                close(out);
            }
        }

        // Make sure pid is child process
        if (child == 0)
        {
            if (execvp(tc->tokens[0], tc->tokens) == -1 && !bg)
            {
                perror("Failed on child process in launchCommands");
            }
            exit(EXIT_FAILURE);
        }
        else if (child < 0)
        {
            perror("Error forking in launchCommands");
        }
        // Parent processes code
        else
        {
            if (bg)
            {
                // Add the child to the backgroundJobs container
                bgJobs.backgroundJobs[bgJobs.bgJobCounter] = child;
                // Collect command for postrun output
                strcpy(bgJobs.bgCommands[bgJobs.bgJobCounter],tc->tokens[0]);
                // Associate starting time with background process
                bgJobs.starts[bgJobs.bgJobCounter++] = start;
                printf("[%d] %d : Running\n", bgJobs.bgJobCounter, child);
            }
            else
            {
                int returnStatus;
                waitpid(child, &returnStatus, 0);
                postRun(tc, &start, child);
            }
        }
    }
    return true;
}
void
FFmpegLibAvStreamImpl::run()
{
    Mutex                   lockMutex;
    ScopedLock              lock (lockMutex);

    preRun();

    // Minimal samples for time-period passed by one frame multiplied by two(speed of filling audio buffer should be faster than audio playback),
    // limited by 32767 as restriction of ffmpeg-wrapper
    const unsigned short    samplesPart = std::min((double)32767, (double)(m_audioFormat.m_bytePerSample * m_audioFormat.m_channelsNb * m_audioFormat.m_sampleRate) / m_frame_rate * 2);
    const unsigned int      minBlockSize = samplesPart * m_audioFormat.m_bytePerSample * m_audioFormat.m_channelsNb;
    unsigned char *         pAudioData = minBlockSize > 0 ? new unsigned char[minBlockSize * 2] : NULL; // ... * 2], because it could read more than minBlockSize
    try
    {
        //
        bool                videoGrabbingInProcess;
        bool                audioGrabbingInProcess;
        //
        bool                isPlaybackStarted   = false;
        size_t              drop_frame_nb       = 0;
        //
        while (m_shadowThreadStop == false)
        {
            unsigned int    videoWriteFlag = 0;
            audioGrabbingInProcess = false;
            videoGrabbingInProcess = false;
            //
            // Grab Audio Buffer
            //
            if (isHasAudio() && pAudioData != NULL)
            {
                const unsigned int space_audio_size = m_audio_buffer.freeSpaceSize();

                if (space_audio_size > minBlockSize && m_audio_buffering_finished == false)
                {
                    double  max_avail_time_micros = -1.0;
                    //
                    // Limit audio-grabbing by time to avoid video artifacts
                    //
                    if (isPlaybackStarted)
                    {
                        //
                        // 1. To avoid audio-artifacts, we should guaranty that audio-buffer filled.
                        //    So, as less audio-buffer filled, as more available time for audio grabbing.
                        //    Even if it will become to appear the video artifacts.
                        //
                        // 2. To avoid video-artifacts, we should TRY spend as less as possible time for
                        //    audio grabbing.
                        //
                        const double audio_bufferedTimeSec  = (double)(m_audio_buffer.size() - space_audio_size) / (double)(m_audioFormat.m_bytePerSample * m_audioFormat.m_channelsNb * m_audioFormat.m_sampleRate);
                        //
                        const double audio_bufferedRatio    = (double)(m_audio_buffer.size() - space_audio_size) / (double)m_audio_buffer.size();
                        if (audio_bufferedRatio > 0.25)
                        {
                            const double video_frameTimeSec = 1.0 / m_frame_rate;

                                                                                                    // audio_bufferedRatio:       0.25 ... 0.5       ... 1.0
                            double coeff = 0.25 / (audio_bufferedRatio - 0.25 + std::numeric_limits<double>::min()) - 0.333;   // +INF ... 0.7       ... 0.0
                            max_avail_time_micros = video_frameTimeSec * coeff * 1000000.0;         //                            +INF ... frame*0.7 ... 0.0
                        }
                        else
                        {
                            if (m_useRibbonTimeStrategy == false)
                                videoWriteFlag |= 1; // disable decoding during searching
                        }
                    }
                    const int bytesread = FFmpegWrapper::getAudioSamples(m_audioIndex,
                                                                            123456789,
                                                                            m_audioFormat.m_channelsNb,
                                                                            m_audioFormat.m_avSampleFormat,
                                                                            m_audioFormat.m_sampleRate,
                                                                            samplesPart,
                                                                            pAudioData,
                                                                            max_avail_time_micros) * m_audioFormat.m_bytePerSample * m_audioFormat.m_channelsNb;
                    if (bytesread > 0)
                    {
                        m_audio_buffer.write (pAudioData, bytesread);
                        audioGrabbingInProcess = true;
                    }
                    else
                    {
                        m_audio_buffering_finished = true;
                        if (bytesread < 0)
                            throw std::runtime_error("Audio failed");
                    }
                }
            }
            //
            // Grab Video Buffer
            //
            if (isHasVideo())
            {
                if (m_video_buffer.isStreamFinished() == false)
                {
                    if (m_video_buffer.isBufferFull() == false)
                    {
                        if (m_useRibbonTimeStrategy == true)
                        {
                            drop_frame_nb = 0;
                        }
                        else
                        {
                            //
                            // Provide little acceleration for video grabbing
                            // Test shows, that 20-frame buffer accelerated enough by 1-frame dropping in half of the buffer-size.
                            //
                            if (isPlaybackStarted && m_video_buffer.isStreamFinished() == false)
                            {
                                double aspect = (double)m_video_buffer.freeSpaceSize() / (double)m_video_buffer.size();
                                drop_frame_nb = aspect * 2;
                            }
                        }
                        m_video_buffer.writeFrame (videoWriteFlag, drop_frame_nb);

                        videoGrabbingInProcess = true;
                    }
                }
                else
                {
                    m_renderer.quit(false);
                }
            }
            if (isPlaybackStarted && isPlaybackFinished())
            {
                break;
            }
            //
            // If grabbings do not work(waste a time):
            //
            // 1. If it is first wasting of the time and playback did not started before, we may start playback.
            // 2. It may couse to get too many empty loops, so locks this thread till some buffer opens for writing, or interrupt for exit
            //
            if (audioGrabbingInProcess == false &&
                videoGrabbingInProcess == false)
            {
                if (isPlaybackStarted == false)
                {
                    isPlaybackStarted = true;
                    startPlayback();
                }
                if (m_shadowThreadStop == false)
                    m_threadLocker.wait (& lockMutex);
            }
        }
    }
    catch (const std::exception & error)
    {
        //OSG_WARN << "FFmpegLibAvStreamImpl::run : " << error.what() << std::endl;
        av_log(NULL, AV_LOG_WARNING, "FFmpegLibAvStreamImpl::run : %s", error.what());
    }

    catch (...)
    {
        //OSG_WARN << "FFmpegLibAvStreamImpl::run : unhandled exception" << std::endl;
        av_log(NULL, AV_LOG_WARNING, "FFmpegLibAvStreamImpl::run : unhandled exception");
    }

    if (pAudioData)
        delete []pAudioData;

    m_shadowThreadStop = true;

    postRun();
}