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)
ecore_evas_init
EAPI int ecore_evas_init(void)
Inits the Ecore_Evas system.
Definition ecore_evas.c:608
return 1;

Once inited we query which engines are available:

engines = ecore_evas_engines_get();
printf("Available engines:\n");
EINA_LIST_FOREACH(engines, l, data)
printf("%s\n", data);
ecore_evas_engines_free(engines);
ecore_evas_engines_get
EAPI Eina_List * ecore_evas_engines_get(void)
Returns a list of supported engine names.
Definition ecore_evas.c:1020
ecore_evas_engines_free
EAPI void ecore_evas_engines_free(Eina_List *engines)
Free list returned by ecore_evas_engines_get().
Definition ecore_evas.c:1026
EINA_LIST_FOREACH
#define EINA_LIST_FOREACH(list, l, _data)
Definition for the macro to iterate over a list.
Definition eina_list.h:1415

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");
ecore_evas_show(ee);
ecore_evas_title_set
EAPI void ecore_evas_title_set(Ecore_Evas *ee, const char *t)
Sets the title of an Ecore_Evas' window.
Definition ecore_evas.c:1541
ecore_evas_show
EAPI void ecore_evas_show(Ecore_Evas *ee)
Shows an Ecore_Evas' window.
Definition ecore_evas.c:1494
ecore_evas_new
EAPI Ecore_Evas * ecore_evas_new(const char *engine_name, int x, int y, int w, int h, const char *extra_options)
Creates a new Ecore_Evas based on engine name and common parameters.
Definition ecore_evas.c:1053

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);
ecore_evas_data_set
EAPI void ecore_evas_data_set(Ecore_Evas *ee, const char *key, const void *data)
Stores user data in an Ecore_Evas structure.
Definition ecore_evas.c:1117

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

ecore_evas_callback_delete_request_set(ee, _on_delete);
ecore_evas_callback_delete_request_set
EAPI void ecore_evas_callback_delete_request_set(Ecore_Evas *ee, Ecore_Evas_Event_Cb func)
Sets a callback for Ecore_Evas delete request events.
Definition ecore_evas.c:1190
static void
_on_delete(Ecore_Evas *ee)
{
free(ecore_evas_data_get(ee, "key"));
ecore_main_loop_quit();
}
ecore_evas_data_get
EAPI void * ecore_evas_data_get(const Ecore_Evas *ee, const char *key)
Retrieves user data associated with an Ecore_Evas.
Definition ecore_evas.c:1106
ecore_main_loop_quit
void ecore_main_loop_quit(void)
Quits the main loop once all the events currently on the queue have been processed.
Definition ecore_main.c:1321

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

printf("Using %s engine!\n", ecore_evas_engine_name_get(ee));
ecore_evas_engine_name_get
EAPI const char * ecore_evas_engine_name_get(const Ecore_Evas *ee)
Gets the engine name used by this Ecore_Evas(window).
Definition ecore_evas.c:1080

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);
ecore_evas_get
EAPI Evas * ecore_evas_get(const Ecore_Evas *ee)
Gets an Ecore_Evas's Evas.
Definition ecore_evas.c:1314

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");
ecore_evas_ecore_evas_get
EAPI Ecore_Evas * ecore_evas_ecore_evas_get(const Evas *e)
Returns the Ecore_Evas for this Evas.
Definition ecore_evas.c:1088

Now we can actually add the background:

bg = evas_object_rectangle_add(canvas);
evas_object_color_set(bg, 0, 0, 255, 255);
evas_object_resize(bg, 200, 200);
evas_object_show(bg);
ecore_evas_object_associate(ee, bg, ECORE_EVAS_OBJECT_ASSOCIATE_BASE);
ecore_evas_object_associate
EAPI Eina_Bool ecore_evas_object_associate(Ecore_Evas *ee, Evas_Object *obj, Ecore_Evas_Object_Associate_Flags flags)
Associates the given object to this ecore evas.
Definition ecore_evas_util.c:223
evas_object_show
EVAS_API void evas_object_show(Evas_Object *eo_obj)
Makes the given Evas object visible.
Definition evas_object_main.c:1814
evas_object_color_set
EVAS_API void evas_object_color_set(Evas_Object *obj, int r, int g, int b, int a)
Sets the general/main color of the given Evas object to the given one.
Definition evas_object_main.c:2024
evas_object_resize
EVAS_API void evas_object_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
Changes the size of the given Evas object.
Definition evas_object_main.c:1236
evas_object_rectangle_add
EVAS_API Evas_Object * evas_object_rectangle_add(Evas *e)
Adds a rectangle to the given evas.
Definition evas_object_rectangle.c:78

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);
ecore_main_fd_handler_add
Ecore_Fd_Handler * ecore_main_fd_handler_add(int fd, Ecore_Fd_Handler_Flags flags, Ecore_Fd_Cb func, const void *data, Ecore_Fd_Cb buf_func, const void *buf_data)
Adds a callback for activity on the given file descriptor.
Definition ecore_main.c:1449
ECORE_FD_READ
@ ECORE_FD_READ
Fd Read mask.
Definition Ecore_Common.h:1394

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)
{
Eina_List *l;
Ecore_Evas *ee;
char c;
int ret = scanf("%c", &c);
if (ret < 1) return ECORE_CALLBACK_RENEW;
if (c == 'h')
EINA_LIST_FOREACH(ecore_evas_ecore_evas_list_get(), l, ee)
ecore_evas_hide(ee);
else if (c == 's')
EINA_LIST_FOREACH(ecore_evas_ecore_evas_list_get(), l, ee)
ecore_evas_show(ee);
return ECORE_CALLBACK_RENEW;
}
ecore_evas_hide
EAPI void ecore_evas_hide(Ecore_Evas *ee)
Hides an Ecore_Evas' window.
Definition ecore_evas.c:1502
ecore_evas_ecore_evas_list_get
EAPI Eina_List * ecore_evas_ecore_evas_list_get(void)
Gets a list of all the ecore_evases.
Definition ecore_evas.c:3841
Ecore_Fd_Handler
struct _Ecore_Fd_Handler Ecore_Fd_Handler
A handle for Fd handlers.
Definition Ecore_Common.h:1386
ECORE_CALLBACK_RENEW
#define ECORE_CALLBACK_RENEW
Return value to keep a callback.
Definition Ecore_Common.h:153
Eina_List
struct _Eina_List Eina_List
Type for a generic double linked list.
Definition eina_list.h:304
Eina_Bool
unsigned char Eina_Bool
Type to mimic a boolean.
Definition eina_types.h:527
EINA_UNUSED
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition eina_types.h:339

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:

ecore_main_loop_begin();
ecore_evas_free(ee);
ecore_evas_shutdown();
ecore_evas_shutdown
EAPI int ecore_evas_shutdown(void)
Shuts down the Ecore_Evas system.
Definition ecore_evas.c:672
ecore_evas_free
EAPI void ecore_evas_free(Ecore_Evas *ee)
Frees an Ecore_Evas.
Definition ecore_evas.c:1097
ecore_main_loop_begin
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition ecore_main.c:1311

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)
{
Eina_List *l;
Ecore_Evas *ee;
char c;
int ret = scanf("%c", &c);
if (ret < 1) return ECORE_CALLBACK_RENEW;
if (c == 'h')
EINA_LIST_FOREACH(ecore_evas_ecore_evas_list_get(), l, ee)
ecore_evas_hide(ee);
else if (c == 's')
EINA_LIST_FOREACH(ecore_evas_ecore_evas_list_get(), l, ee)
ecore_evas_show(ee);
return ECORE_CALLBACK_RENEW;
}
static void
_on_delete(Ecore_Evas *ee)
{
free(ecore_evas_data_get(ee, "key"));
ecore_main_loop_quit();
}
int
main(void)
{
Ecore_Evas *ee;
Evas *canvas;
Evas_Object *bg;
Eina_List *engines, *l;
char *data;
if (ecore_evas_init() <= 0)
return 1;
engines = ecore_evas_engines_get();
printf("Available engines:\n");
EINA_LIST_FOREACH(engines, l, data)
printf("%s\n", data);
ecore_evas_engines_free(engines);
ee = ecore_evas_new(NULL, 0, 0, 200, 200, NULL);
ecore_evas_title_set(ee, "Ecore Evas basics Example");
ecore_evas_show(ee);
data = malloc(sizeof(char) * 6);
sprintf(data, "%s", "hello");
ecore_evas_data_set(ee, "key", data);
ecore_evas_callback_delete_request_set(ee, _on_delete);
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");
bg = evas_object_rectangle_add(canvas);
evas_object_color_set(bg, 0, 0, 255, 255);
evas_object_resize(bg, 200, 200);
evas_object_show(bg);
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);
ecore_main_loop_begin();
ecore_evas_free(ee);
ecore_evas_shutdown();
return 0;
}
Ecore_Evas.h
Evas wrapper functions.
Evas
Eo Evas
An opaque handle to an Evas canvas.
Definition Evas_Common.h:163
Evas_Object
Efl_Canvas_Object Evas_Object
An Evas Object handle.
Definition Evas_Common.h:185