In this example we illustrate how to interact with canvas' (and its objects') events, including the key input ones. We also demonstrate precise point collision on objects and canvas "obscured regions", here.
The example application consists of a window with a white background and an image – the Enlightenment logo. The application begins with this image switching back and forth into two sizes: the exact canvas' size and one quarter of it (when it's placed on the top left quadrant). Thus, we'll have an animation going on, with image states set to change each 2 elapsed seconds.
There's a global variable to aid accessing our desired context variables from anywhere in the code:
struct test_data
{
Ecore_Evas *ee;
};
static struct test_data d = {0};
What interests us there are the canvas
pointer, our image handle – img
– and the background one, bg
.
The first interesting thing on the example is the registration of a callback on each canvas resizing event, where we put our canvas' size and the background rectangle's one in synchrony, so that we don't get bogus content on rendering with canvas resizes:
Than, after grabbing our canvas pointer from the Ecore Evas helper infrastructure, we registrate an event callbacks on it:
static void
{
printf("Canvas is about to flush its rendering pipeline!\n");
}
It will be called whenever our canvas has to flush its rendering pipeline. In this example, two ways of observing that message which is printed in the cited callback are:
- to resize the example's window (thus resizing the canvas' viewport)
- let the animation run
When one resizes the canvas, there's at least one operation it has to do which will require new calculation for rendering: the resizing of the background rectangle, in a callback we already shown you.
The creation of our background rectangle is so that we give it a name, via evas_object_name_set() and we give it the canvas focus:
Still exemplifying events and callbacks, we register a callback on the canvas event of an object being focused:
_object_focus_in_cb, NULL);
{
fprintf(stderr, "ERROR: Callback registering failed! Aborting.\n");
goto panic;
}
static void
void *event_info)
{
printf("An object got focused: %s\n",
printf("Let's recheck it: %s\n",
"OK!" : "Oops, something is bad.");
}
In that call, event_info
is going to be the focused object's handle, in this case our background rectangle. We print its name, so you can check it's the same. We check that pointer is the same reported by Evas' API with regard to the newest focused object. Finally, we check whether that object is really flagged as focused, now using an Evas object API function.
The animation we talked about comes from a timer we register just before we start the example's main loop. As we said, the resizing of the image will also force the canvas to repaint itself, thus flushing the rendering pipeline whenever the timer ticks:
{
int w, h, cw, ch;
if (w < cw)
else
}
When you start this example, this animation will be running, by default. To interact with the program, there's a command line interface. A help string can be asked for with the 'h' key:
static const char *commands = \
"commands are:\n"
"\ta - toggle animation timer\n"
"\tc - cycle between focus and key grabs for key input\n"
"\td - delete canvas callbacks\n"
"\tf - freeze input for 3 seconds\n"
"\tp - toggle precise point collision detection on image\n"
"\tControl + o - add an obscured rectangle\n"
"\th - print help\n";
These are the commands the example will accept at any time, except when one triggers the 'f' one. This command will exemplify evas_event_freeze(), which interrupts all input events processing for the canvas (in the example, just for 3 seconds). Try to issue events for it during that freeze time:
if (strcmp(ev->
key,
"f") == 0)
{
printf("Freezing input for 3 seconds\n");
return;
}
The 'd' command will unregister those two canvas callbacks for you, so you won't see the messages about the focused object and the rendering process anymore:
if (strcmp(ev->
key,
"d") == 0)
{
printf("Deleting canvas event callbacks\n");
_render_flush_cb, NULL);
_object_focus_in_cb, NULL);
return;
}
In this example, we start using a focused object to handle the input events – the background rectangle. We register a callback on an key input event occurring on it, so that we can act on each key stroke:
{
fprintf(stderr, "ERROR: Callback registering failed! Aborting.\n");
goto panic;
}
static void
void *einfo)
{
const Evas_Modifier *mods;
printf(
"We've got key input: %s\n", ev->
key);
printf("It actually came from %s\n",
d.focus ? "focus" : "key grab");
We do so by examining the ev->key
string (remember the event information struct for key down events is the Evas_Event_Key_Down one). There's one more trick for grabbing input events on this example – evas_object_key_grab(). The 'c' command will, when firstly used, unfocus the background rectangle. Unfocused objects on an Evas canvas will never receive key events. We grab, then, the keys we're interested at to the object forcefully:
if (d.focus)
{
printf("Focused object is now %s\n",
"still valid! Something went wrong." : "none.");
if (!ret)
{
printf("Something went wrong with key grabs.\n");
goto c_end;
}
if (!ret)
{
printf("Something went wrong with key grabs.\n");
goto c_end;
}
if (!ret)
{
printf("Something went wrong with key grabs.\n");
goto c_end;
}
if (!ret)
{
printf("Something went wrong with key grabs.\n");
goto c_end;
}
if (!ret)
{
printf("Something went wrong with key grabs.\n");
goto c_end;
}
if (!ret)
{
printf("Something went wrong with key grabs.\n");
goto c_end;
}
if (!ret)
{
printf("Something went wrong with key grabs.\n");
goto c_end;
}
}
else
This shows how one can handle input not depending on focus issues – you can grab them globally. Switch back and forth focus and forced key grabbing with the 'c' key, and observe the messages printed about the focused object. Observe, also, that we register two more object callbacks, this time on the image object (Enlightenment logo), where we just print messages telling the mouse pointer has entered or exited it area:
static void
{
printf("Enlightenment logo has had the mouse in.\n");
}
static void
{
printf("Enlightenment logo has had the mouse out.\n");
}
Experiment with moving the mouse pointer over the image, letting it enter and exit its area (stop the animation with 'a', for a better experience). When you start the example, Evas will consider this area by being the whole boundary rectangle around the picture. If you issue the 'p' command, though, you get a demonstration of Evas' precise point collision detection on objects. With evas_object_precise_is_inside_get(), one can make Evas consider the transparent areas of an object (the middle of the logo's E letter, in the case) as not belonging to it when calculating mouse in/out/up/down events:
if (strcmp(ev->
key,
"p") == 0)
{
printf("Toggling precise point collision detection %s on Enlightenment logo\n",
precise ? "off" : "on");
return;
}
To finish the example, try the command bound to Control + 'o', which exemplifies Evas' obscured regions. When firstly pressed, you'll get the same contents, in a region in the middle of the canvas, at the time the key was pressed, until you toggle the effect off again (make sure the animation is running on to get the idea better). When you toggle this effect off, we also demonstrate the use of evas_render_updates(), which will force immediate updates on the canvas rendering, bringing back the obscured region's contents to normal.
(strcmp(ev->
key,
"o") == 0))
{
printf("Toggling obscured rectangle on canvas\n");
if (!d.obscured)
{
int w, h;
}
else
{
int w, h;
{
printf("Rectangle (%d, %d, %d, %d) on canvas got a"
" rendering update.\n", rect->
x, rect->
y,
}
}
d.obscured = !d.obscured;
}
What follows is the complete code for this example.
#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define PACKAGE_EXAMPLES_DIR "."
#endif
#include <Ecore.h>
#include <stdio.h>
#include <errno.h>
#include "evas-common.h"
#define WIDTH (320)
#define HEIGHT (240)
static const char *img_path = PACKAGE_EXAMPLES_DIR EVAS_IMAGE_FOLDER "/enlightenment.png";
static const char *commands = \
"commands are:\n"
"\ta - toggle animation timer\n"
"\tc - cycle between focus and key grabs for key input\n"
"\td - delete canvas callbacks\n"
"\tf - freeze input for 3 seconds\n"
"\tp - toggle precise point collision detection on image\n"
"\tControl + o - add an obscured rectangle\n"
"\th - print help\n";
struct test_data
{
Ecore_Evas *ee;
};
static struct test_data d = {0};
static void
_canvas_resize_cb(Ecore_Evas *ee)
{
int w, h;
}
static void
void *event_info)
{
printf("An object got focused: %s\n",
printf("Let's recheck it: %s\n",
"OK!" : "Oops, something is bad.");
}
static void
{
printf("Canvas is about to flush its rendering pipeline!\n");
}
{
int w, h, cw, ch;
if (w < cw)
else
}
{
printf("Canvas was frozen %d times, now thawing.\n",
}
static void
{
printf("Enlightenment logo has had the mouse in.\n");
}
static void
{
printf("Enlightenment logo has had the mouse out.\n");
}
static void
void *einfo)
{
const Evas_Modifier *mods;
printf(
"We've got key input: %s\n", ev->
key);
printf("It actually came from %s\n",
d.focus ? "focus" : "key grab");
if (strcmp(ev->
key,
"h") == 0)
{
puts(commands);
return;
}
if (strcmp(ev->
key,
"a") == 0)
{
if (d.resize_timer != NULL)
{
printf("Stopping animation timer\n");
d.resize_timer = NULL;
}
else
{
printf("Re-issuing animation timer\n");
}
return;
}
if (strcmp(ev->
key,
"c") == 0)
{
printf("Switching to %s for key input\n",
d.focus ? "key grabs" : "focus");
if (d.focus)
{
printf("Focused object is now %s\n",
"still valid! Something went wrong." : "none.");
if (!ret)
{
printf("Something went wrong with key grabs.\n");
goto c_end;
}
if (!ret)
{
printf("Something went wrong with key grabs.\n");
goto c_end;
}
if (!ret)
{
printf("Something went wrong with key grabs.\n");
goto c_end;
}
if (!ret)
{
printf("Something went wrong with key grabs.\n");
goto c_end;
}
if (!ret)
{
printf("Something went wrong with key grabs.\n");
goto c_end;
}
if (!ret)
{
printf("Something went wrong with key grabs.\n");
goto c_end;
}
if (!ret)
{
printf("Something went wrong with key grabs.\n");
goto c_end;
}
}
else
{
}
c_end:
d.focus = !d.focus;
return;
}
if (strcmp(ev->
key,
"d") == 0)
{
printf("Deleting canvas event callbacks\n");
_render_flush_cb, NULL);
_object_focus_in_cb, NULL);
return;
}
if (strcmp(ev->
key,
"f") == 0)
{
printf("Freezing input for 3 seconds\n");
return;
}
if (strcmp(ev->
key,
"p") == 0)
{
printf("Toggling precise point collision detection %s on Enlightenment logo\n",
precise ? "off" : "on");
return;
}
(strcmp(ev->
key,
"o") == 0))
{
printf("Toggling obscured rectangle on canvas\n");
if (!d.obscured)
{
int w, h;
}
else
{
int w, h;
{
printf("Rectangle (%d, %d, %d, %d) on canvas got a"
" rendering update.\n", rect->
x, rect->
y,
}
}
d.obscured = !d.obscured;
}
}
int
main(void)
{
int err;
return EXIT_FAILURE;
if (!d.ee)
goto error;
_render_flush_cb, NULL);
{
fprintf(stderr, "ERROR: Callback registering failed! Aborting.\n");
goto panic;
}
_object_focus_in_cb, NULL);
{
fprintf(stderr, "ERROR: Callback registering failed! Aborting.\n");
goto panic;
}
{
fprintf(stderr, "ERROR: Callback registering failed! Aborting.\n");
goto panic;
}
{
fprintf(stderr, "ERROR: Image loading failed! Aborting.\n");
goto panic;
}
else
{
}
puts(commands);
return 0;
error:
fprintf(stderr, "error: Requires at least one Evas engine built and linked"
" to ecore-evas for this example to run properly.\n");
panic:
return -1;
}