Published on 25th of October 2017
Handling Application Events

In this lesson we will consider type of events in application and how Tizen handle them. In the end, we'll show you how to add the processing of such event, as clicking on the button, which was created in the last lesson.

Description of an Events Conception in Tizen

As it was mentioned earlier, native applications development in Tizen is based on the EFL libraries. In EFL all events are handled by means of connection between signal and callback functions.

At the system level some parameters are checked in the main loop of the application. For example, if for button click event was set a handler, system will invoke this callback function. Handlers call comes from the main loop of the application and the handler itself also works in the main loop, therefore it is necessary to minimize the functionality of such handlers. Let’s say, in your application, at the button click, you supposed to send a request to the server, in this case, until your request is fulfilled, user interface will be blocked. Therefore, it is more preferable to send the request in a parallel thread. You may use Ecore_Thread module, intended for creating and managing threads. In the next lessons, we will consider work with this module. At this stage, it's enough to remember that in applications, the work that loads the processor must occur in parallel threads, rather than in event handlers.

Various events, like changing the interface language on the device, can occur in the system. In order to be able to perform some action after such event, it is necessary to register a handler for this event by calling required function. In addition, it is necessary to implement the handler function itself, note that the function signature must be appropriate.

Types of Events in EFL

There are 4 main types of events in EFL:

Ecore is the lowest level event of EFL application that comes directly from the system. Evas event - these events are tied to the canvas, on which the graphic interface is drawn. They are sufficiently low-leveled, and they are not advised to use, except the cases when you will do your own drawing. The following types of events will be considered in more detail, since we will commonly use them in our lessons.

Evas object event - these events occur for objects on canvas. It can be either primitive objects or full-scale widgets.

Evas smart event is related to the widgets highest-level events.

The foregoing applies to graphical interface events, such as: resizing or displaying graphic elements.

Events and callback functions are also used in the modules responsible for functionality other than graphics. For each module, there are different types of events, with different handlers. For example, if you need to get device coordinates, you can not call the function and immediately obtain it. First of all, the device must prepare the hardware, then get stable signals from the satellites, calculate where the device is in relation to the satellites and only then coordinates can be obtained. Such actions can last for a while, sometimes even more than for a minute. In this case, there will be following process: call the function for obtaining coordinates, give it to the callback function, which will be executed after all the calculation procedures, and only then you can process the received coordinates.

Events of Evas Object Type

Events of this type occur for all graphic objects, placed on the canvas, whether it's a rectangle or a button. You can catch events for different objects in the same way. Below there is a list of the most frequently used events. The following 3 events are related to clicking in the object area with one finger.

EVAS_CALLBACK_MOUSE_DOWN - start pressing in the subject area.

EVAS_CALLBACK_MOUSE_UP - end of depression.

EVAS_CALLBACK_MOUSE_MOVE - the pointer had moved in relations to the previous position.

Important:

Even if you press on the screen, the event of moving the pointer EVAS_CALLBACK_MOUSE_MOVE, can be called earlier than EVAS_CALLBACK_MOUSE_DOWN. If a computer mouse is connected to the device, the event of pointer movement can be triggered without a click event.

Let’s consider 3 events, during which one finger is already on the screen and there is a simultaneous touch of the object by the second, third etc. finger.

EVAS_CALLBACK_MULTI_DOWN - new finger touched the object area.

EVAS_CALLBACK_MULTI_UP - one of the fingers finished touching

EVAS_CALLBACK_MULTI_MOVE - one of the fingers moved on the screen.

EVAS_CALLBACK_FREE - called before the memory, allocated to the graphic object, will be cleared.

The following events are triggered after the system called certain functions or they were called from programs.

EVAS_CALLBACK_SHOW - triggered after calling a evas_object_show() function.

Important:

The event is triggered not when object just displayed on the screen, but after the evas_object_show() function was called.

EVAS_CALLBACK_HIDE - triggered after calling a evas_object_hide() function.

EVAS_CALLBACK_MOVE - triggered after calling a evas_object_move() function.

EVAS_CALLBACK_RESIZE - triggered after calling a evas_object_resize() function.

EVAS_CALLBACK_DEL - triggered after calling a evas_object_del() function.

Let's look at how to use such events.

Let's say there is a graphic object on the canvas:

Evas_Object *rect;

Register the handler to the object deletion event as follows:

evas_object_event_callback_add(rect, EVAS_CALLBACK_DEL, del_callback, user_data);

Then implement the del_callback function, which should be called when the object is deleted.

static void
del_callback(void *user_data, Evas *evas, Evas_Object *obj, void *event_info)
{
   // Callback body 
}

For all events of the Evas object event type, the signature of the handler is the same. So the first parameter is a pointer to the user data, (passed like the last argument during registering the handler), the second parameter is the canvas object, on which the event occurred (not frequently used). The third parameter is the object, over which the event occurred or the calls of the function that led to the event. The last parameter is a pointer to the structure that describes the event in detail. For example, at a click event, the structure object, in which the click occurred and from which you can get the coordinates, comes in. The last parameter is not always valid, so sometimes NULL passed as parameter. In other lessons, we will describe in more detail the use of the last parameter.

Events of Evas Smart Type

Evas smart events take place only for the full-fledged widgets of the Elementary library.

Each widget has its own unique list of supported events. For example, depressing is available for button events. In the following lessons, we'll tell you which events are obtainable for which widgets.

To register such events, use the function:

evas_object_smart_callback_add(widget, "event", event_callback, user_data);

The handler of a smart event should have the following signature:

void smart_callback(void *user_data, Evas_Object *obj, void *event_info);

The first parameter is a pointer to user data, the second - to the object for which the event was triggered, and the third - to additional information about the event (the parameter can be empty). In your applications, you can create your own widgets and new events for them. For example, you have created a battery charge indicator and you want to use it to capture an event of charge indicator changes. So to emulate an event, let's call it "charge, value, changed", it will be necessary to call the following function at the right moment:

evas_object_smart_callback_call(battery_widget, "charge,value,changed", event_info);

In the function, the first parameter is a widget for which you want to emulate an event, the second - is the name of the event, and the third - is additional information about the event. In the third parameter, you can pass NULL or create a structure with information about the event. Do not forget to add comments about usage of your widget. Thus, you can create your own smart widgets, and reuse them.

Adding Event Processing to the Demo Application

There are such “smart events” available for button widget:

"clicked" – the click event occurs when you press and quickly depressed in the button area.

"pressed" – button is pressed.

"unpressed" – button is released.

"repeated" – customizeable event, may be called several times in case of button long pressing. It is possible to setup start delay and repeat interval time.

Now knowing almost everything about user interface events, let's see how to add processing of these button events to the application.

First of all, register events:

static void
_button_create(Evas_Object *parent)
{
   ...

   evas_object_smart_callback_add(button, "pressed", press_cb, NULL);
   evas_object_smart_callback_add(button, "unpressed", unpress_cb, NULL);
   evas_object_smart_callback_add(button, "repeat", tepeat_cb, NULL);
   evas_object_smart_callback_add(button, "clicked", click_cb, NULL);
}

Implement the handlers, in which information about the event will be displayed in the log.

If you are going to use logs, include the following header file:

#include <dlog.h>

Implement handlers:

static void
press_cb(void *data, Evas_Object *button, void *event_info)
{
   dlog_print(DLOG_DEBUG, "lesson15", "PRESS event!");
}

static void
unpress_cb(void *data, Evas_Object *button, void *event_info)
{
   dlog_print(DLOG_DEBUG, "lesson15", "UNPRESS event!");
}

static void
repeat_cb(void *data, Evas_Object *button, void *event_info)
{
   dlog_print(DLOG_DEBUG, "lesson15", "REPEAT event!");
}

static void
click_cb(void *data, Evas_Object *button, void *event_info)
{
   dlog_print(DLOG_DEBUG, "lesson15", "CLICK event!");
}

Run your application.

As you can see, all button events are processed. It can be concluded that the click event is a collection of buttons pressing and releasing events, as it should be.

In the next lesson, we'll look at a text widget in which we'll change the text after clicking on the button. The full code of this lesson is available WearLesson015.

Leave a Reply

Your email address will not be published. Required fields are marked *