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`
#define EFL_BETA_API_SUPPORT
#define EFL_EO_API_SUPPORT
#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 Eina_Bool
_playback_started_cb(void *data EINA_UNUSED,
Eo *obj EINA_UNUSED, const Eo_Event_Description *desc EINA_UNUSED, void *event_info EINA_UNUSED)
{
printf("Emotion object started playback.\n");
return EINA_TRUE;
}

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];
if (!ecore_evas_init())
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;
ecore_evas_show(ee);
/* the canvas pointer, de facto */
e = ecore_evas_get(ee);
/* adding a background to this example */
bg = evas_object_rectangle_add(e);
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 */
evas_object_show(bg);

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

/* Creating the emotion object */
em = emotion_object_add(e);
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():

eo_do(em, eo_event_callback_add
(EMOTION_OBJECT_EVENT_PLAYBACK_STARTED, _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);
evas_object_show(em);

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:

emotion_object_play_set(em, EINA_TRUE);
ecore_main_loop_begin();

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

ecore_evas_free(ee);
ecore_evas_shutdown();
return 0;
error:
fprintf(stderr, "you got to have at least one evas engine built and linked"
" up to ecore-evas for this example to run properly.\n");
ecore_evas_shutdown();
return -1;
}

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