nux-1.16.0
nux::TimerHandler Class Reference

List of all members.

Public Types

enum  { TIMERTYPE_UNKNONW = 0L, TIMERTYPE_PERIODIC, TIMERTYPE_DURATION, TIMERTYPE_ITERATION }

Public Member Functions

TimerHandle AddTimerHandler (unsigned int Period, TimerFunctor *Callback, void *Data, WindowThread *window_thread=NULL)
 Add a timer callback.
TimerHandle AddPeriodicTimerHandler (unsigned int Period, int Duration, TimerFunctor *Callback, void *Data)
 Add a periodic timer callback.
TimerHandle AddCountIterationTimerHandler (unsigned int Period, int NumberOfIteration, TimerFunctor *Callback, void *Data)
 Add a timer callback to be called a finite number of time.
bool FindTimerHandle (TimerHandle &handle)
 Search for a timer handle.
bool RemoveTimerHandler (TimerHandle &handle)
 Remove a timer;.
int DelayUntilNextTimerExpires ()
 Return the delay until the next timer expires.
int ExecTimerHandler ()
void StartEarlyTimerObjects ()
 Start the timers that were sett before the system was fully initialized.

Detailed Description

Definition at line 65 of file TimerProc.h.


Member Function Documentation

TimerHandle nux::TimerHandler::AddCountIterationTimerHandler ( unsigned int  Period,
int  NumberOfIteration,
TimerFunctor Callback,
void *  Data 
)

Add a timer callback to be called a finite number of time.

Add a timer callback to the timer manager. The timer callback will be call N times exactly. Every time the timer expires, the callback function is executed. The returned TimerHandle should not be deleted by the caller.

Parameters:
MillisecondsPeriod delay before the callback is executed.
NumberOfIterationThe number of time to repeat the the wait period.
CallbackThe callback to execute when the timer expires.
DataThe callback data
Returns:
A handle to the timer.

Definition at line 266 of file TimerProc.cpp.

References nux::TimerObject::Period, nux::TimerObject::ScheduledIteration, and nux::TimerObject::when.

  {
    TimerObject *timer_object = new TimerObject();
    TimeRightNow (&timer_object->when);
    Addmillisecs (&timer_object->when, Period);
    timer_object->CallbackData = Data;
    timer_object->TimerCallback = Callback;

    timer_object->Period = Period;
    timer_object->ScheduledIteration  = (NumberOfIterations < 0) ? -1 : NumberOfIterations;
    timer_object->Type = TIMERTYPE_ITERATION;
    AddHandle (timer_object);

#if (defined(NUX_OS_LINUX) || defined(NUX_USE_GLIB_LOOP_ON_WINDOWS)) && (!defined(NUX_DISABLE_GLIB_LOOP))
    {
      timer_object->glibid = GetWindowThread ()->AddGLibTimeout (Period);

      if (timer_object->glibid == 0)
      {
        _early_timer_objects.push_back(timer_object);
        // Probably trying to set a timeout before Glib main context and loop have been created.
        // Sometimes later, this timer will be examined when ExecTimerHandler is called.
        // This happens when trying to set a callback before the mainloop has been initialized.
      }

      //nuxDebugMsg(TEXT("[TimerHandler::AddTimerHandler] Adding Timeout ID: %d"), timer_object->glibid);
    }
#endif
    TimerHandle handle (timer_object);
    return handle;
  }
TimerHandle nux::TimerHandler::AddPeriodicTimerHandler ( unsigned int  Period,
int  Duration,
TimerFunctor Callback,
void *  Data 
)

Add a periodic timer callback.

Add a timer callback to the timer manager. Every time the timer expires, the callback function is executed. The returned TimerHandle should not be deleted by the caller.

Parameters:
MillisecondsPeriod delay before the callback is executed.
DurationThe duration over which the timer is repeated.
CallbackThe callback to execute when the timer expires.
DataThe callback data
Returns:
A handle to the timer.

Definition at line 233 of file TimerProc.cpp.

References nux::TimerObject::Duration, nux::TimerObject::Period, and nux::TimerObject::when.

  {
    TimerObject *timer_object = new TimerObject();
    TimeRightNow (&timer_object->when);
    Addmillisecs (&timer_object->when, Period);
    timer_object->CallbackData = Data;
    timer_object->TimerCallback = Callback;

    timer_object->Period = Period;
    timer_object->Duration = (Duration < 0) ? -1 : Duration;
    timer_object->Type = TIMERTYPE_DURATION;
    AddHandle (timer_object);

#if (defined(NUX_OS_LINUX) || defined(NUX_USE_GLIB_LOOP_ON_WINDOWS)) && (!defined(NUX_DISABLE_GLIB_LOOP))
    {
      timer_object->glibid = GetWindowThread ()->AddGLibTimeout (Period);

      if (timer_object->glibid == 0)
      {
        _early_timer_objects.push_back(timer_object);
        // Probably trying to set a timeout before Glib main context and loop have been created.
        // Sometimes later, this timer will be examined when ExecTimerHandler is called.
        // This happens when trying to set a callback before the mainloop has been initialized.
      }

      //nuxDebugMsg(TEXT("[TimerHandler::AddTimerHandler] Adding Timeout ID: %d"), timer_object->glibid);
    }
#endif

    TimerHandle handle (timer_object);
    return handle;
  }
TimerHandle nux::TimerHandler::AddTimerHandler ( unsigned int  Period,
TimerFunctor Callback,
void *  Data,
WindowThread window_thread = NULL 
)

Add a timer callback.

Add a timer callback to the timer manager. When the timer expires, the callback function is executed. The returned TimerObject should not be deleted by the caller.

Parameters:
MillisecondsPeriod delay before the callback is executed.
CallbackThe callback to execute when the timer expires.
DataThe callback data
window_threadThread safety mesure. Pass the WindowThread associated to this TimerHandler if it is called from a diferent thread than the one where the main thread was created.
Returns:
A handle to the timer.

Definition at line 192 of file TimerProc.cpp.

References nux::WindowCompositor::GetProcessingTopView(), nux::TimerObject::Period, nux::TimerObject::when, and nux::TimerObject::Window.

  {
    TimerObject *timer_object = new TimerObject();

    TimeRightNow(&timer_object->when);
    Addmillisecs(&timer_object->when, Period);

    timer_object->CallbackData  = Data;
    timer_object->TimerCallback = Callback;
    timer_object->Period        = Period;
    timer_object->Type          = TIMERTYPE_PERIODIC;
    if (window_thread)
      timer_object->Window        = window_thread->GetWindowCompositor ().GetProcessingTopView();
    else
      timer_object->Window        = GetWindowCompositor ().GetProcessingTopView();
    
    AddHandle (timer_object);

#if (defined(NUX_OS_LINUX) || defined(NUX_USE_GLIB_LOOP_ON_WINDOWS)) && (!defined(NUX_DISABLE_GLIB_LOOP))
    {
      if (window_thread)
        timer_object->glibid = window_thread->AddGLibTimeout (Period);
      else
        timer_object->glibid = GetWindowThread ()->AddGLibTimeout (Period);
      
      if (timer_object->glibid == 0)
      {
        _early_timer_objects.push_back(timer_object);
        // Probably trying to set a timeout before Glib main context and loop have been created.
        // Sometimes later, this timer will be examined when ExecTimerHandler is called.
        // This happens when trying to set a callback before the mainloop has been initialized.
      }

      //nuxDebugMsg(TEXT("[TimerHandler::AddTimerHandler] Adding Timeout ID: %d"), timer_object->glibid);
    }
#endif

    TimerHandle handle (timer_object);
    return handle;
  }
int nux::TimerHandler::DelayUntilNextTimerExpires ( )

Return the delay until the next timer expires.

Returns:
Delay to next timer expiration in milliseconds.

Definition at line 618 of file TimerProc.cpp.

References nux::TimerObject::when.

  {
    TimeStruct now;
    TimeStruct delay;

    if (m_timer_object_queue == NULL)
    {
      // The return value of this function is only valid if there _are_ timers active.
      return 0;
    }
    else
    {
      TimeRightNow (&now);

      if (TimeIsGreater (now, m_timer_object_queue->when) )
      {
        return 0;
      }
      else
      {
        delay.sec = m_timer_object_queue->when.sec - now.sec;
        delay.usec = m_timer_object_queue->when.usec - now.usec;

        // make sure that usec cannot be less than -1000000 before applying this code
        if (delay.usec < 0)
        {
          delay.usec += 1000000;
          delay.sec--;
        }

        return  (delay.sec * 1000000 + delay.usec) / 1000; // return delay in milliseconds
      }
    }
  }
bool nux::TimerHandler::FindTimerHandle ( TimerHandle handle)

Search for a timer handle.

Search for a timer handle in the timer handle queue. Return true if the timer is found.

Parameters:
handleTimer handle to search.
Returns:
Return true if the timer is found; false otherwise.

Definition at line 600 of file TimerProc.cpp.

  {
    TimerObject *tmp = m_timer_object_queue;

    while (tmp)
    {
      if (tmp == timer_object.m_d && (tmp->uid == timer_object.m_d->uid) )
      {
        return true;
      }

      tmp = tmp->next;
    }

    return false;
  }
bool nux::TimerHandler::RemoveTimerHandler ( TimerHandle handle)

Remove a timer;.

Parameters:
handleTimer handle to search.
Returns:
Return True if the timer is found.

Definition at line 360 of file TimerProc.cpp.

  {
    NUX_RETURN_VALUE_IF_NULL (timer_object.m_d, false);
    NUX_RETURN_VALUE_IF_NULL (m_timer_object_queue, false);

    TimerObject *tmp;

    tmp = m_timer_object_queue;

    while (tmp)
    {
      if ( (tmp == timer_object.m_d) && (tmp->uid == timer_object.m_d->uid) )
      {
        if (!m_IsProceesingTimers)
        {
          if (tmp->next)
            tmp->next->prev = tmp->prev;

          if (tmp->prev)
            tmp->prev->next = tmp->next;

          if ( (timer_object.m_d == m_timer_object_queue) && (timer_object.m_d->uid == m_timer_object_queue->uid) )
            m_timer_object_queue = timer_object.m_d->next;

          NUX_SAFE_DELETE (timer_object.m_d);
        }
        else
        {
          timer_object.m_d->MarkedForRemoval = true;
        }

        return true;
      }

      tmp = tmp->next;
    }

    return false;
  }

The documentation for this class was generated from the following files:
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends