Difference between revisions of "GE"

From GIMX
Jump to: navigation, search
(The GIMX Event Library)
(Linux)
Line 9: Line 9:
  
 
=Linux=
 
=Linux=
 +
 +
To use GE in Linux, make sure to have read access to the input devices ([[http://sourceforge.net/apps/mediawiki/gizmod/index.php?title=HOWTO_-_Setting_Input_Device_Permissions_-_Creating_a_udev_Rule check this page]]).
  
 
==Interrupt-driven, with periodic task==
 
==Interrupt-driven, with periodic task==

Revision as of 13:33, 14 January 2013

The GIMX Event Library

GE is a cross-platform event library that can be used in Windows or Linux.
GE supports multiple mice, keyboards and joysticks, which means the source of each event can be identified.
In Linux, it directly interfaces with evdev, and allows to write "interrupt-based" event processing.
In Windows, it uses a modified SDL library, and allows to write "polling-based" event processing.

This page gives pseudo-code examples.

Linux

To use GE in Linux, make sure to have read access to the input devices ([check this page]).

Interrupt-driven, with periodic task

 #include <GE.h>
 
 int process_event(GE_Event* event)
 {
   //handle the event
 }
 
 int main(int argc, char* argv[])
 {
   if (!GE_initialize())
   {
     //handle the error
   }
 
   struct timespec period = {.tv_sec = 0, .tv_nsec = 10000000};
 
   GE_TimerStart(&period);
 
   GE_SetCallback(process_event);
 
   while(!done)
   {
     GE_PumpEvents();//blocks until timer fires
 
     //do something periodically
   }
 
   GE_quit();
   
   return 0;
 }

Interrupt-driven, without periodic task

 #include <GE.h>
 
 int process_event(GE_Event* event)
 {
   //process the event
 }
 
 int main(int argc, char* argv[])
 {
   if (!GE_initialize())
   {
     //handle the error
   }
 
   GE_SetCallback(GE_PushEvent);
 
   while(!done)
   {
     GE_PumpEvents();//retrieve 1 event and return (blocks until event reception)
 
     num_evt = GE_PeepEvents(events, 1);
 
     if (num_evt > 0)
     {
       //process the event
     }
   }
 
   GE_quit();
   
   return 0;
 }

Windows

Polling-driven

 #include <GE.h>
 
 int main(int argc, char* argv[])
 {
   if (!GE_initialize())
   {
     //handle the error
   }
 
   while(!done)
   {
     t0 = get_time();
 
     GE_PumpEvents();//retrieve all pending events and return (does not block)
 
     num_evt = GE_PeepEvents(events, sizeof(events) / sizeof(events[0]));
 
     if (num_evt > 0)
     {
       for (event = events; event < events + num_evt; ++event)
       {
         //process the event
       }
     }
 
     //do something periodically
 
     t1 = get_time();
     time_to_sleep = t1-t0;//avoid the period to drift
 
     usleep(PERIOD - time_to_sleep);//sleep some time (removing this would hug the cpu...)
   }
 
   GE_quit();
   
   return 0;
 }