EZ
Up Prev Next Contents


6.2 Timer Events

There are two lists of timers in the EZwgl, a list of repeating timers and a list of one-time timers. The second list is an ordered list sorted according to the timeouts while the first one is not sorted.

Timers are created by

EZ_Timer *EZ_CreateTimer(long sec, long usec, int repetition, EZ_CallBack callback, void *pdata, int idata);

This function creates a timer and returns the allocated timer data structure. If repetition is 0, the timer created is put in the one-time timer list and will be fired exactly once if at all. If repetition is nonzero, the timer is inserted into the repeating timer list and it will be fired exactly repetition times or indefinitely depending on whether repetition is positive or not.

The following three functions can be used to modify the state of an existing timer.

EZ_Timer *EZ_ResetTimer(EZ_Timer *timer, long sec, long usec, int repetition, EZ_CallBack callback, void *pdata, int idata); EZ_Timer *EZ_RestartTimer(EZ_Timer *timer); void EZ_CancelTimer(EZ_Timer *timer);

Here are a few more interface functions.

void EZ_SetTimerPtrData(EZ_Timer *timer, void *data);

attach client pointer data to a timer.

void EZ_SetTimerIntData(EZ_Timer *timer, int value);

attach an integer to a timer.

void EZ_SetTimerClientData(EZ_Timer *timer, void *p, int i);

attach client data to a timer.

void EZ_SetTimerCallBack(EZ_Timer *timer, EZ_CallBack cb, void *data);

set timer callback procedure.

void EZ_GetTimerState(EZ_Timer *timer, long *sec, long *usec, int *repetition);

return the current state of a timer.

void EZ_GetTimerPtrData(EZ_Timer *timer,void **dat_return);

return the client data attached to a timer.

int EZ_GetTimerdIntData(EZ_Timer *timer);

return the integer attached to a timer.

void EZ_GetTimerdClientData(EZ_Timer *timer, void **p, int *i);

return the pointer and the integer attached to a timer.

6.2.1 An Example

/************************** ExampleTimer ************************/
#include "EZ.h"

static void rtimer_callback(EZ_Timer *timer, void *pdata)
{
  long delay_sec, delay_usec; 
  int  n_calls_remaining;
  char str[256];

  EZ_GetTimerState(timer, &delay_sec, &delay_usec, &n_calls_remaining);
  if(n_calls_remaining == 0) {EZ_Shutdown(); exit(0);}
  sprintf(str,"%.1f seconds remaining before destruction",
          (float) (delay_usec * n_calls_remaining)/1000000.0);
  EZ_ConfigureWidget((EZ_Widget*)pdata, EZ_LABEL_STRING, str, 0);
}

main(int ac, char **av)
{
  EZ_Widget *frame, *label;
  EZ_Timer  *RTimer;

  EZ_Initialize(ac,av,0);
  frame = EZ_CreateWidget(EZ_WIDGET_FRAME, NULL,
                          EZ_LABEL_STRING, "A Timer",	
                          0);
  label = EZ_CreateWidget(EZ_WIDGET_NORMAL_BUTTON, frame,
                          EZ_FOREGROUND,  "red",
                          EZ_PROPAGATE,    0,
                          EZ_LABEL_STRING, "20.0 seconds remaining before destruction",
                          EZ_LABEL_JUSTIFICATION, EZ_CENTER, 
                          0);
  RTimer = EZ_CreateTimer(0, 100000, 200, rtimer_callback, label, 0);
  EZ_DisplayWidget(frame);
  EZ_EventMainLoop();
}


Up Prev Next Contents

HTML Documentation Maintainance:Arturo Espinosa <arturo@nuclecu.unam.mx>