void MoSyncStub::sleep(uint32 duration) { int startTime = maGetMilliSecondCount(); do { int curTime = maGetMilliSecondCount() - startTime; if(curTime>=(int)duration) break; maWait(duration-curTime); } while(true); }
void MoSyncStub::sleep(uint32 duration) { int startTime = maGetMilliSecondCount(); int dur = (int)duration; do { int curTime = maGetMilliSecondCount() - startTime; if(curTime>=dur) break; maWait(dur-curTime); } while(true); processEvents(); }
extern "C" int MAMain() { InitConsole(); gConsoleLogging = 1; printf("dumping File List...\n"); int startTime = maGetMilliSecondCount(); dumpFileList(""); int endTime = maGetMilliSecondCount(); printf("Done in %i ms\n", endTime - startTime); FREEZE; }
//TestCase void start() { printf("Don't touch anything now...\n"); MAEvent e; while(maGetEvent(&e)) { //clear event buffer if(e.type == EVENT_TYPE_CLOSE) maExit(0); } int startTime = maGetMilliSecondCount(); maWait(TEST_TIMEOUT); int delta = maGetMilliSecondCount() - startTime; bool success = delta > TEST_TIMEOUT - TEST_TIME_EPSILON; printf("%s (%i ms)\n", success ? "Success" : "Failure", delta); assert(name, success); suite->runNextCase(); }
void WidgetSkin::flushCacheUntilNewImageFits(int numPixels) { int totalPixelsInCache = numPixels; HashMap<CacheKey, CacheElement>::Iterator iter = sCache.begin(); while(iter != sCache.end()) { totalPixelsInCache += iter->first.w*iter->first.h; iter++; } int currentTime = maGetMilliSecondCount(); while(totalPixelsInCache>maxCacheSize) { int oldest = currentTime; iter = sCache.begin(); HashMap<CacheKey, CacheElement>::Iterator best = sCache.end(); while(iter != sCache.end()) { if(iter->second.lastUsed<oldest) { oldest = iter->second.lastUsed; best = iter; } iter++; } if(best == sCache.end()) break; maDestroyPlaceholder(best->second.image); sCache.erase(best); totalPixelsInCache-=iter->first.w*iter->first.h; } }
//TimerListener void runTimerEvent() { int delta = maGetMilliSecondCount() - mStartTime; bool success = delta > TEST_TIMEOUT - TEST_TIME_EPSILON; printf("%s (%i ms)\n", success ? "Success" : "Failure", delta); assert(name, success); suite->runNextCase(); }
// handler for pointer touch events bool AnsiWidget::pointerTouchEvent(MAEvent &event) { bool result = false; // hit test buttons on the front screen if (setActiveButton(event, _front)) { _focus = _front; } else { // hit test buttons on remaining screens for (int i = 0; i < MAX_SCREENS; i++) { if (_screens[i] != NULL && _screens[i] != _front) { if (setActiveButton(event, _screens[i])) { _focus = _screens[i]; break; } } } } // paint the pressed button if (_activeButton != NULL) { _activeButton->clicked(event.point.x, event.point.y, true); drawActiveButton(); } // setup vars for page scrolling if (_front->overlaps(event.point.x, event.point.y)) { _touchTime = maGetMilliSecondCount(); _xTouch = _xMove = event.point.x; _yTouch = _yMove = event.point.y; result = true; } return result; }
// commence runtime state void Controller::setRunning(bool running) { if (running) { dev_fgcolor = -DEFAULT_FOREGROUND; dev_bgcolor = -DEFAULT_BACKGROUND; os_graf_mx = _output->getWidth(); os_graf_my = _output->getHeight(); os_ver = 1; os_color = 1; os_color_depth = 16; setsysvar_str(SYSVAR_OSNAME, "MoSync"); dev_clrkb(); ui_reset(); _runMode = run_state; _loadPath.empty(); _output->reset(); _lastEventTime = maGetMilliSecondCount(); _eventTicks = 0; _drainError = false; } else { _runMode = init_state; } }
void MoSyncStub::init(const char *title) { memset(&_pi, 0, sizeof(_pi)); currentOrientation = 0; FrameBuffer_init(SCREEN_W, SCREEN_H, currentOrientation, FLAG_4BPP|FLAG_RGB666); timerActive = false; timeOfStart = maGetMilliSecondCount(); }
void MainScreen::pointerPressEvent(MAPoint2d p) { lastPoint = p; mRotating = false; mStartTime = (double) maGetMilliSecondCount() * 0.001; mRotationVelocity = 0.0; mRotationStart = grid->getRotation(); }
void SMV::pointerMoveEvent(MAPoint2d point) { engine->gPointer.x = point.x - engine->gPointer2.x; engine->gPointer.y = point.y - engine->gPointer2.y; engine->center_view = false; milisec = maGetMilliSecondCount(); engine->draw(); }
float GLMoblet::getActualFramesPerSecond() const { if(!isDrawing()) return 0.0f; else { int millisSinceStartDrawing = maGetMilliSecondCount() - mMillisOfStartDrawing; if(millisSinceStartDrawing == 0) return 0; return mFramesSinceStartDrawing / (0.001f * (float)millisSinceStartDrawing); } }
void sleep(int s) { lprintfln("sleep(%i)", s); const int start = maGetMilliSecondCount(); const int end = start + s * 1000; do { int left = end - maGetMilliSecondCount(); int res; MAEvent e; if(left <= 0) break; while(maGetEvent(&e)) { if(e.type = EVENT_TYPE_CLOSE) { lprintfln("EVENT_TYPE_CLOSE"); exit(42); } } maWait(left); } while(1); }
void GLMoblet::startDrawing() { if(mIsDrawing) return; if(mPreferredMillisecondsPerFrame <= 0) Environment::getEnvironment().addIdleListener(mListener); else Environment::getEnvironment().addTimer(mListener, mPreferredMillisecondsPerFrame, -1); mIsDrawing = true; mFramesSinceStartDrawing = 0; mMillisOfStartDrawing = maGetMilliSecondCount(); }
void MainScreen::pointerReleaseEvent(MAPoint2d p) { pointerMoveEvent(p); double mstime = (double) maGetMilliSecondCount() * 0.001; double time = mstime - mStartTime; if (time == 0.0) return; mRotationVelocity /= time; mStartTime = mstime; mRotating = true; }
unsigned sleep(unsigned s) { lprintfln("sleep(%i)", s); const int start = maGetMilliSecondCount(); const int end = start + s * 1000; do { int left = end - maGetMilliSecondCount(); MAEvent e; if(left <= 0) break; while(maGetEvent(&e)) { if(e.type == EVENT_TYPE_CLOSE) { lprintfln("EVENT_TYPE_CLOSE"); exit(42); } } maWait(left); } while(1); int passed = maGetMilliSecondCount() - start; MAASSERT(passed >= (int)s*1000); return 0; }
int MAMain ( void ) { char buf[80]; int arsize; long arsize2d,memreq,nreps; size_t malloc_arg; g_startTime = maGetMilliSecondCount( ); //while (1) { /* printf("Enter array size (q to quit) [200]: "); fgets(buf,79,stdin); if (buf[0]=='q' || buf[0]=='Q') break; if (buf[0]=='\0' || buf[0]=='\n') arsize=200; else arsize=atoi(buf); */ arsize = 200; arsize/=2; arsize*=2; if (arsize<10) { printf("Too small.\n"); //continue; } arsize2d = (long)arsize*(long)arsize; memreq=arsize2d*sizeof(REAL)+(long)arsize*sizeof(REAL)+(long)arsize*sizeof(int); printf("Memory required: %ldK.\n",(memreq+512L)>>10); malloc_arg=(size_t)memreq; if (malloc_arg!=memreq || (mempool=malloc(malloc_arg))==NULL) { printf("Not enough memory available for given array size.\n\n"); //continue; } printf("\n\nLINPACK benchmark, %s precision.\n",PREC); //printf("Machine precision: %d digits.\n",BASE10DIG); printf("Array size %d X %d.\n",arsize,arsize); printf("Average rolled and unrolled performance:\n\n"); printf(" Reps Time(s) DGEFA DGESL OVERHEAD MFLOPS\n"); printf("----------------------------------------------------\n"); nreps=4; // linpack(nreps, arsize); while (linpack(nreps,arsize)<10.) nreps*=2; free(mempool); printf("Finished!\n"); } FREEZE; }
// display any pending images changed void AnsiWidget::flush(bool force, bool vscroll, int maxPending) { if (_front != NULL && _autoflush) { bool update = false; if (force) { update = _front->_dirty; } else if (_front->_dirty) { update = (maGetMilliSecondCount() - _front->_dirty >= maxPending); } if (update) { _front->drawBase(vscroll); } } }
//always returns >= 0 int Moblet::timeToNextTimer() { int now = maGetMilliSecondCount(); int minTime = -1; ListenerSet_each(TimerEventInstance, tei, mTimerEvents) { int ttn = tei->nextInvoke - now; if(ttn < 0) ttn = 0; if(minTime == -1) { minTime = ttn; } else { minTime = MIN(minTime, ttn); } }
int AnimatedConnWait() { int startTime = maGetMilliSecondCount(); int res; #ifdef ANIMATED int lPos = gPos; int i=0; int screenWidth = EXTENT_X(maGetScrSize()); maSetColor(0); maFillRect(0, lPos, screenWidth - 1, gTextHeight); printlnf("a%i",i++); #endif //ANIMATED while(1) { EVENT event; #ifdef ANIMATED gPos = lPos; maSetColor(0); maFillRect(0, lPos, screenWidth - 1, gTextHeight); printlnf("b%i",i++); #else maWait(0); #endif //ANIMATED if(maGetEvent(&event)) { if(event.type == EVENT_TYPE_CLOSE || (event.type == EVENT_TYPE_KEY_PRESSED && event.key == MAK_0)) { maExit(0); } else if(event.type == EVENT_TYPE_CONN) { res = event.conn.result; break; } } } printlnf("w%i", maGetMilliSecondCount() - startTime); return res; }
int MAMain() { set_malloc_handler(nil); startTime = maGetMilliSecondCount(); //srand(maTime()); //urand(); //urand(); allocHalf(0); while(1) { allocHalf(3); freeHalf(0); allocHalf(0); freeHalf(3); } FREEZE; }
/** * Waits for an event and returns the event object. * * @param millisecondTimeout Maximum number of milliseconds to wait. * @param eventType Event type to wait for. * @param returnedEvent Event object [out]. * * @return True if event was caught with the given time limit. */ bool EventBase::waitForEvent(int millisecondTimeout, int eventType, MAEvent &returnedEvent) { MAEvent event; long startTime = maGetMilliSecondCount(); while (maGetMilliSecondCount() - startTime < millisecondTimeout) { maWait(100); while(maGetEvent(&event) != 0) { if (event.type == eventType) { returnedEvent = event; return true; } else if (event.type == EVENT_TYPE_CLOSE) { maExit(0); } } } return false; }
void SMV::customEvent(const MAEvent& event) { if(event.type == EVENT_TYPE_LOCATION) { MALocation& loc = *(MALocation*)event.data; engine->gps = &loc; if(maGetMilliSecondCount()-milisec>2000) engine->center_view = true; engine->draw(); } else if(event.type == EVENT_TYPE_LOCATION_PROVIDER) { const char *strings[]= {"AVAILABLE","TEMPORARILY_UNAVAILABLE","OUT_OF_SERVICE"}; //maPanic(1, "No GPS available"); //printf("gps provider: %s\n", strings[event.state-1]); } }
void WidgetSkin::draw(int x, int y, int width, int height, eType type) { MAHandle cached = 0; // Calculate numTiles needed to be drawn, if they are many, we need to cache, otherwise draw directly... int numTiles = calculateNumTiles(width, height); if(!useCache || numTiles<100) { drawDirect(x, y, width, height, type); return; } CacheKey newKey = CacheKey(this, width, height, type); cached = getFromCache(newKey); // If we didn't find a cached widgetskin, let's generate one and save it in the cache. if(!cached) { // set malloc handler to null so that we can catch if we're out of heap and write directly to the screen then. #ifndef MOSYNC_NATIVE malloc_handler mh = set_malloc_handler(NULL); #endif int *data = new int[width*height]; if(!data) { drawDirect(x, y, width, height, type); return; } #ifndef MOSYNC_NATIVE set_malloc_handler(mh); #endif drawToData(data, 0, 0, width, height, type); CacheElement cacheElem; flushCacheUntilNewImageFits(width*height); cacheElem.image = maCreatePlaceholder(); if(maCreateImageRaw(cacheElem.image,data,EXTENT(width,height),1)!=RES_OK) { maPanic(1, "Could not create raw image"); } delete data; cacheElem.lastUsed = maGetMilliSecondCount(); cached = cacheElem.image; addToCache(newKey, cacheElem); } // Draw the cached widgetskin. Gfx_drawImage(cached, x, y); }
void TouchMotionTracker::calculateVelocity( double &directionX, double &directionY, double &velocityX, double &velocityY) { // TODO: Take unit into acount? (pixels per second or ms). double time = maGetMilliSecondCount()-mStartTime; double timeInSeconds = time/1000.0; double friction = pow(1.0-mFrictionPerSecond, 1+timeInSeconds); if(friction<0.0) friction = 0.0; double dx = mDirx; double dy = mDiry; dx*=friction; dy*=friction; double dirX = dx; double dirY = dy; // Normalize directions. double len = sqrt(dx*dx+dy*dy); if(len < 0.0000000001) { velocityX = velocityY = 0.0; directionX = directionY = 0.0; return; } double len_recip = 1.0/len; dx*=len_recip; dy*=len_recip; directionX = dx; directionY = dy; if(time < 0.0000000001) { velocityX = velocityY = 0.0; return; } velocityX = dirX/timeInSeconds; velocityY = dirY/timeInSeconds; }
// handler for pointer release events void AnsiWidget::pointerReleaseEvent(MAEvent &event) { if (_activeButton != NULL && _front == _screens[MENU_SCREEN]) { _activeButton->clicked(event.point.x, event.point.y, false); } else if (_activeButton != NULL && _activeButton->_pressed) { _activeButton->_pressed = false; drawActiveButton(); _activeButton->clicked(event.point.x, event.point.y, false); } else if (_swipeExit) { _swipeExit = false; } else { int maxScroll = (_front->_curY - _front->_height) + (2 * _fontSize); if (_yMove != -1 && maxScroll > 0) { _front->drawInto(); // swipe test - min distance and not max duration int deltaX = _xTouch - event.point.x; int deltaY = _yTouch - event.point.y; int distance = (int) fabs(sqrt(deltaX * deltaX + deltaY * deltaY)); int now = maGetMilliSecondCount(); if (distance >= SWIPE_MIN_DISTANCE && (now - _touchTime) < SWIPE_MAX_DURATION) { bool moveDown = (deltaY >= SWIPE_MIN_DISTANCE); doSwipe(now, moveDown, distance, maxScroll); } else if (_front->_scrollY > maxScroll) { _front->_scrollY = maxScroll; } // ensure the scrollbar is removed _front->_dirty = true; flush(true); _touchTime = 0; } } if (_hoverInput) { int dx = _front->_x; int dy = _front->_y - _front->_scrollY; _hoverInput->drawHover(dx, dy, false); _hoverInput = NULL; } _xTouch = _xMove = -1; _yTouch = _yMove = -1; _activeButton = NULL; _focus = NULL; }
void TouchMotionTracker::addPoint(MAPoint2d p, int &relX, int &relY) { if(!mStarted) { mLast = mStart = p; mStartTime = maGetMilliSecondCount(); mDirx = 0; mDiry = 0; relX = 0; relY = 0; mStarted = true; } else { relX = p.x-mLast.x; relY = p.y-mLast.y; double dx = relX; double dy = relY; mDirx+=dx; mDiry+=dy; mLast = p; } }
//------------------------------------------------------------------------- bool MapDemoScreen::handlePointerPress( MAPoint2d point ) //------------------------------------------------------------------------- { if ( AppScreen::handlePointerPress( point ) ) return true; mMap->getViewport( )->beginPanning( point ); int curTime = maGetMilliSecondCount(); int deltaTime = curTime - lastPointerPress; if(deltaTime < 200) { maExit(0); } else { lastPointerPress = curTime; } return true; }
void MainScreen::idle() { if (!mDrawing) { return; } if (mRotating) { double rotationTime = 1.0; double time = (double) maGetMilliSecondCount() * 0.001 - mStartTime; if (time < rotationTime) { double t = time / rotationTime; double f = -1 / (t * 4.0 + 1) + 1; double rotation = mRotationStart + 0.2 * mRotationVelocity * f; grid->setRotation(rotation); } else { mRotating = false; } } grid->render(); mGLView->setProperty("invalidate", ""); }
// swipe handler for pointerReleaseEvent() void AnsiWidget::doSwipe(int start, bool moveDown, int distance, int maxScroll) { MAEvent event; int elapsed = 0; int vscroll = _front->_scrollY; int scrollSize = distance / 3; int swipeStep = SWIPE_DELAY_STEP; while (elapsed < SWIPE_MAX_TIMER) { if (maGetEvent(&event) && event.type == EVENT_TYPE_POINTER_RELEASED) { // ignore the next move and release events _swipeExit = true; break; } elapsed += (maGetMilliSecondCount() - start); if (elapsed > swipeStep && scrollSize > 1) { // step down to a lesser scroll amount scrollSize -= 1; swipeStep += SWIPE_DELAY_STEP; } if (scrollSize == 1) { maWait(20); } vscroll += moveDown ? scrollSize : -scrollSize; if (vscroll < 0) { vscroll = 0; } else if (vscroll > maxScroll) { vscroll = maxScroll; } if (vscroll != _front->_scrollY) { _front->_dirty = true; // forced _front->_scrollY = vscroll; flush(true, true); } else { break; } } // pause before removing the scrollbar maWait(500); }