Ecore Evas basics example

This example will illustrates the usage of some basic Ecore_Evas functions. This example will list the available evas engines, check which one we used to create our window and set some data on our Ecore_Evas. It also allows you to hide/show all windows in this process(we only have one, but if there were more they would be hidden), to hide the windows type 'h' and hit return, to show them, type 's' and hit return.

The very first thing we'll do is initialize ecore_evas:

if (ecore_evas_init() <= 0)
return 1;

Once inited we query which engines are available:

printf("Available engines:\n");
EINA_LIST_FOREACH(engines, l, data)
printf("%s\n", data);

We then create an Ecore_Evas(window) with the first available engine, on position 0,0 with size 200,200 and no especial flags, set it's title and show it:

ee = ecore_evas_new(NULL, 0, 0, 200, 200, NULL);
ecore_evas_title_set(ee, "Ecore Evas basics Example");

We now add some important data to our Ecore_Evas:

data = malloc(sizeof(char) * 6);
sprintf(data, "%s", "hello");
ecore_evas_data_set(ee, "key", data);

And since our data is dynamically allocated we'll need to free it when the Ecore_Evas dies:

static void
_on_delete(Ecore_Evas *ee)
{
free(ecore_evas_data_get(ee, "key"));
}

We now print which Evas engine is being used for our example:

printf("Using %s engine!\n", ecore_evas_engine_name_get(ee));

We are going to add a background to our window but before we can do that we'll need to get the canvas(Evas) on which to draw it:

canvas = ecore_evas_get(ee);

We then do a sanity check, verifying if the Ecore_Evas of the Evas is the Ecore_Evas from which we got the Evas:

if (ecore_evas_ecore_evas_get(canvas) == ee)
printf("Everything is sane!\n");

Now we can actually add the background:

evas_object_color_set(bg, 0, 0, 255, 255);
evas_object_resize(bg, 200, 200);
ecore_evas_object_associate(ee, bg, ECORE_EVAS_OBJECT_ASSOCIATE_BASE);

To hide and show the windows of this process when the user presses 'h' and 's' respectively we need to know when the user types something, so we register a callback for when we can read something from stdin:

ecore_main_fd_handler_add(STDIN_FILENO, ECORE_FD_READ, _stdin_cb, NULL, NULL, NULL);

The callback that actually does the hiding and showing is pretty simple, it does a scanf(which we know won't block since there is something to read on stdin) and if the character is an 'h' we iterate over all windows calling ecore_evas_hide on them, if the character is an 's' we call ecore_evas_show instead:

static Eina_Bool
_stdin_cb(void *data EINA_UNUSED, Ecore_Fd_Handler *handler EINA_UNUSED)
{
Ecore_Evas *ee;
char c;
int ret = scanf("%c", &c);
if (ret < 1) return ECORE_CALLBACK_RENEW;
if (c == 'h')
else if (c == 's')
}

Once all is done we run our main loop, and when that is done(application is exiting) we free our Ecore_Evas and shutdown the ecore_evas subsystem:

Here you have the full-source of the code:

#include <Ecore.h>
#include <Ecore_Evas.h>
#include <unistd.h>
static Eina_Bool
_stdin_cb(void *data EINA_UNUSED, Ecore_Fd_Handler *handler EINA_UNUSED)
{
Ecore_Evas *ee;
char c;
int ret = scanf("%c", &c);
if (ret < 1) return ECORE_CALLBACK_RENEW;
if (c == 'h')
else if (c == 's')
}
static void
_on_delete(Ecore_Evas *ee)
{
free(ecore_evas_data_get(ee, "key"));
}
int
main(void)
{
Ecore_Evas *ee;
Evas *canvas;
Eina_List *engines, *l;
char *data;
if (ecore_evas_init() <= 0)
return 1;
printf("Available engines:\n");
EINA_LIST_FOREACH(engines, l, data)
printf("%s\n", data);
ee = ecore_evas_new(NULL, 0, 0, 200, 200, NULL);
ecore_evas_title_set(ee, "Ecore Evas basics Example");
data = malloc(sizeof(char) * 6);
sprintf(data, "%s", "hello");
ecore_evas_data_set(ee, "key", data);
printf("Using %s engine!\n", ecore_evas_engine_name_get(ee));
canvas = ecore_evas_get(ee);
if (ecore_evas_ecore_evas_get(canvas) == ee)
printf("Everything is sane!\n");
evas_object_color_set(bg, 0, 0, 255, 255);
evas_object_resize(bg, 200, 200);
ecore_evas_object_associate(ee, bg, ECORE_EVAS_OBJECT_ASSOCIATE_BASE);
ecore_main_fd_handler_add(STDIN_FILENO, ECORE_FD_READ, _stdin_cb, NULL, NULL, NULL);
return 0;
}