Emotion - Basic library usage

This example shows how to setup a simple Emotion object, make it start playing and register a callback that tells when the playback started.

See the full code here.

We start this example by including some header files that will be necessary to work with Emotion, and to display some debug messages:

//Compile with:
// gcc -o emotion_basic_example emotion_basic_example.c `pkg-config --libs --cflags emotion evas ecore ecore-evas eo`
#ifndef EFL_BETA_API_SUPPORT
# define EFL_BETA_API_SUPPORT
#endif
#include <Eo.h>
#include <Ecore.h>
#include <Ecore_Evas.h>
#include <Evas.h>
#include <Emotion.h>
#include <stdio.h>

Then a callback will be declared, to be called when the object starts its playback:

#define WIDTH (320)
#define HEIGHT (240)
static void
_playback_started_cb(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
{
printf("Emotion object started playback.\n");
}

Some basic setup of our canvas, window and background is necessary before displaying our object on it. This setup also includes reading the file to be opened from the program's argument list. Since this is not directly related to Emotion itself, we are just displaying the code for this without an explanation for it:

int
main(int argc, const char *argv[])
{
Ecore_Evas *ee;
Evas *e;
Evas_Object *bg, *em;
const char *filename = NULL;
if (argc < 2)
{
printf("One argument is necessary. Usage:\n");
printf("\t%s <filename>\n", argv[0]);
}
filename = argv[1];
return EXIT_FAILURE;
/* this will give you a window with an Evas canvas under the first
* engine available */
ee = ecore_evas_new(NULL, 10, 10, WIDTH, HEIGHT, NULL);
if (!ee)
goto error;
/* the canvas pointer, de facto */
e = ecore_evas_get(ee);
/* adding a background to this example */
evas_object_name_set(bg, "our dear rectangle");
evas_object_color_set(bg, 255, 255, 255, 255); /* white bg */
evas_object_move(bg, 0, 0); /* at canvas' origin */
evas_object_resize(bg, WIDTH, HEIGHT); /* covers full canvas */

Finally, we start the Emotion part. First we have to create the object in this canvas, and initialize it:

/* Creating the emotion object */
emotion_object_init(em, NULL);

Notice that we didn't specify which module will be used, so emotion will use the first module found. There's no guarantee of the order that the modules will be found, so if you need to use one of them specifically, please be explicit in the second argument of the function emotion_object_init().

Now the callback can be registered to this object. It's a normal Evas smart object callback, so we add it with evas_object_smart_callback_add():

efl_event_callback_add
(em, EFL_CANVAS_VIDEO_EVENT_PLAYBACK_START, _playback_started_cb, NULL);

The object itself is ready for use, but we need to load a file to it. This is done by the following function:

emotion_object_file_set(em, filename);

This object can play audio or video files. For the latter, the image must be displayed in our canvas, and that's why we need to add the object to the canvas. So, like any other Evas object in the canvas, we have to specify its position and size, and explicitly set its visibility. These are the position and dimension where the video will be displayed:

evas_object_move(em, 0, 0);
evas_object_resize(em, WIDTH, HEIGHT);

Since the basic steps were done, we can now start playing our file. For this, we can just call the basic playback control function, and then we can go to the main loop and watch the audio/video playing:

The rest of the code doesn't contain anything special:

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");
return -1;
}

This code just free the canvas, shutdown the library, and has an entry point for exiting on error.