2 min read

Plugins so easy, electrical outlets are jealous: Part 2

Topics:

When last we left our plugin, it was printing out messages when it started and stopped.  All was right with the world, but then the evil concept design functionality reared it's head.  Our plugin will have to be beefed up in order to handle this new requirement.  Will it be up to the task?   Will our plugin be crushed under the weight of expectations?  Will I ever get to the point?  Find out in Plugins so easy, electrical outlets are jealous:  Part 2 ( insert John Williams composed theme here ).

Note, if you haven't read Part 1, you should, or you might get confused.  That confusion could lead to anger, and that just wouldn't be good.

So in this example, lets listen for touch events.  A touch event is a complete press-release combination at the same location.   So what we are going to do is add the following to our initlialization event:

static int
myfirst_init(gr_application_t *app, const char *args, void *pdata) {
gr_io_mgr_t *iomgr = app->linfo->io_mgr;

/* Called when the plugin is starting up */
printf ("Hello, I am starting\n");

/* This will add a listener for touch events */
iomgr->add_event_listener (iomgr,  GR_EVENT_TOUCH, touch_event, NULL);
}

As the comment says, this is going to add a listener for touch events. The definition of the add_event_listener call is:

add_event_listener (IO_MGR_T *mgr, const char *regex, io_listener_t listener, void *data);

Where:

mgr  = The io manager

regex  = Regular expression for the evne to listen for

listener =  The listener callback

data = User data

Now that we have added the event listener in the init callback of the plugin, we should really remove it in the fini callback of the plugin.  We do this by adding the following to the fini function:

static void
myfirst_fini(gr_application_t *app, void *pdata) {
gr_io_mgr_t *iomgr = app->linfo->io_mgr;

/* Called when the plugin is going away */
printf ("Goodbye I am stopping\n");

/* This removes the listerner */
iomgr->rem_event_listener (iomgr, touch_event, pdata);
}

The definition of the rem_event_listener call is:

rem_event_listener (IO_MGR_T *mgr, io_listener_t listener, void *data);

Where:

mgr  = The io manager

listener =  The listener callback

data = User data

Now that we are adding and removing a listener for touch events, we need to add the function that is actually going to get called when the touch event happens:

void
touch_event (IO_MGR_T *mgr, gr_event_t *event, void *arg){
gr_ptr_event_t *ptr_event = (gr_ptr_event_t *) event->data;

printf ("Got a touch event at x = %, y = %d\n", ptr_event->x, ptr_event->y);
}

So now, whenever a touch event is generated, this function will be called, and we can printout where the touch happened. Inn order for this to compile, you will need to add the following includes to your C file. These includes will bring in the event manager defines and pieces.

#include "iodefs.h"
#include "gre/sdk/io_mgr.h"

So now we have added an event listener. Next up, how do we modify data that is in the system.

- Rodney