Difference between revisions of "GE"

From GIMX
Jump to: navigation, search
(Build the test)
Line 3: Line 3:
 
GE is a cross-platform event library for handling input from keyboards, mice, and joysticks.<br />
 
GE is a cross-platform event library for handling input from keyboards, mice, and joysticks.<br />
 
It can be used in Windows or Linux.<br />
 
It can be used in Windows or Linux.<br />
It is released under the [http://www.gnu.org/licenses/gpl.html GPLv3 license] and is available in the [https://github.com/matlo/GIMX/tree/master/shared/event GIMX git repository].<br />
+
It is released under the [http://www.gnu.org/licenses/gpl.html GPLv3 license] and is available in the [https://github.com/matlo/GIMX/tree/dev/shared/event GIMX git repository].<br />
 
GE supports multiple mice, keyboards and joysticks, which means the source of each event can be identified.<br />
 
GE supports multiple mice, keyboards and joysticks, which means the source of each event can be identified.<br />
 
In Linux, it directly interfaces with [http://en.wikipedia.org/wiki/Evdev evdev], and allows to write "interrupt-based" event processing.<br />
 
In Linux, it directly interfaces with [http://en.wikipedia.org/wiki/Evdev evdev], and allows to write "interrupt-based" event processing.<br />
In Windows, it uses a modified SDL library, and allows to write "polling-based" event processing.<br />
+
In Windows, it interfaces with the RawInput API to get keyboard and mouse events. To get joystick events it uses the SDL 2.0 library, which handles standard joysticks and Xinput gamepads. RawInput event processing is "interrupt-based", whereas SDL 2.0 event processing is "polling-based".<br />
  
This page only gives pseudo-code examples. Check the [https://github.com/matlo/GIMX/tree/master/shared/event/test/ test directory] to read complete examples.
+
This page only gives a pseudo-code example. Check the [https://github.com/matlo/GIMX/tree/dev/shared/event/test/ test directory] for a complete example.
 
 
=Linux=
 
  
 
To use GE in Linux, make sure to have [[Development_on_Linux#Set_Input_Device_Permissions_.28required_since_r1277.29|read access to the input devices]].
 
To use GE in Linux, make sure to have [[Development_on_Linux#Set_Input_Device_Permissions_.28required_since_r1277.29|read access to the input devices]].
  
==Interrupt-driven, with periodic task==
+
=Example=
  
 
   #include <GE.h>
 
   #include <GE.h>
Line 30: Line 28:
 
     }
 
     }
 
    
 
    
    struct timespec period = {.tv_sec = 0, .tv_nsec = 10000000};
+
     GE_TimerStart(10000); //microseconds
 
 
     GE_TimerStart(&period);
 
 
    
 
    
 
     GE_SetCallback(process_event);
 
     GE_SetCallback(process_event);
 
    
 
    
     while(!done)
+
     GE_grab(); //grab the mouse pointer
    {
 
      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)
 
     while(!done)
 
     {
 
     {
      t0 = get_time();
+
       GE_PumpEvents();//returns when timer fires
 
 
       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
 
       //do something periodically
 
 
      t1 = get_time();
 
      time_to_sleep = PERIOD - (t1-t0);//avoid the period to drift
 
 
 
      usleep(time_to_sleep);//sleep some time (removing this would hug the cpu...)
 
 
     }
 
     }
 
    
 
    
Line 129: Line 50:
 
==Build the static library==
 
==Build the static library==
 
  cd ~
 
  cd ~
  git clone -b master --single-branch --depth 1 <nowiki>https://github.com/matlo/GIMX.git</nowiki>
+
  git clone -b dev --single-branch --depth 1 <nowiki>https://github.com/matlo/GIMX.git</nowiki>
 
  cd GIMX/shared/event
 
  cd GIMX/shared/event
 
  make
 
  make
Line 138: Line 59:
  
 
==Run the test==
 
==Run the test==
On Linux:
 
cd ~/GIMX/event/test
 
./linux_test
 
 
On Windows:
 
 
  cd ~/GIMX/event/test
 
  cd ~/GIMX/event/test
  ./windows_test
+
  ./GE_test
  
 
The test displays all keyboards, mice and joysticks, and it then displays all input events.
 
The test displays all keyboards, mice and joysticks, and it then displays all input events.
 
Press Escape or Ctrl+c to exit.
 
Press Escape or Ctrl+c to exit.

Revision as of 15:31, 23 May 2014

The GIMX Event Library

GE is a cross-platform event library for handling input from keyboards, mice, and joysticks.
It can be used in Windows or Linux.
It is released under the GPLv3 license and is available in the GIMX git repository.
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 interfaces with the RawInput API to get keyboard and mouse events. To get joystick events it uses the SDL 2.0 library, which handles standard joysticks and Xinput gamepads. RawInput event processing is "interrupt-based", whereas SDL 2.0 event processing is "polling-based".

This page only gives a pseudo-code example. Check the test directory for a complete example.

To use GE in Linux, make sure to have read access to the input devices.

Example

 #include <GE.h>
 
 int process_event(GE_Event* event)
 {
   //handle the event
 }
 
 int main(int argc, char* argv[])
 {
   if (!GE_initialize())
   {
     //handle the error
   }
 
   GE_TimerStart(10000); //microseconds
 
   GE_SetCallback(process_event);
 
   GE_grab(); //grab the mouse pointer
 
   while(!done)
   {
     GE_PumpEvents();//returns when timer fires
 
     //do something periodically
   }
 
   GE_quit();
   
   return 0;
 }

Build and test

Build the static library

cd ~
git clone -b dev --single-branch --depth 1 https://github.com/matlo/GIMX.git
cd GIMX/shared/event
make

Build the test

cd ~/GIMX/event/test
make

Run the test

cd ~/GIMX/event/test
./GE_test

The test displays all keyboards, mice and joysticks, and it then displays all input events. Press Escape or Ctrl+c to exit.