EFL 1.7 svn doobies
[profile/ivi/emotion.git] / src / examples / emotion_generic_example.c
1 #include <Ecore.h>
2 #include <Ecore_Evas.h>
3 #include <Evas.h>
4 #include <Emotion.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <unistd.h>
8
9 #define WIDTH  (320)
10 #define HEIGHT (240)
11
12 static Eina_List *filenames = NULL;
13 static Eina_List *curfile = NULL;
14
15 static void
16 _playback_started_cb(void *data, Evas_Object *o, void *event_info)
17 {
18     printf("Emotion object started playback.\n");
19 }
20
21 static void
22 _playback_stopped_cb(void *data, Evas_Object *o, void *event_info)
23 {
24    printf("Emotion playback stopped.\n");
25    emotion_object_play_set(o, EINA_FALSE);
26    emotion_object_position_set(o, 0);
27 }
28
29 static Evas_Object *
30 _create_emotion_object(Evas *e)
31 {
32    Evas_Object *em = emotion_object_add(e);
33
34    emotion_object_init(em, "generic");
35
36    evas_object_smart_callback_add(
37        em, "playback_started", _playback_started_cb, NULL);
38    evas_object_smart_callback_add(
39        em, "playback_finished", _playback_stopped_cb, NULL);
40
41    return em;
42 }
43
44 static void
45 _on_key_down(void *data, Evas *e, Evas_Object *o, void *event_info)
46 {
47    Evas_Event_Key_Down *ev = event_info;
48    Evas_Object *em = data;
49
50    if (!strcmp(ev->keyname, "Return"))
51      {
52         emotion_object_play_set(em, EINA_TRUE);
53      }
54    else if (!strcmp(ev->keyname, "space"))
55      {
56         emotion_object_play_set(em, EINA_FALSE);
57      }
58    else if (!strcmp(ev->keyname, "Escape"))
59      {
60         ecore_main_loop_quit();
61      }
62    else if (!strcmp(ev->keyname, "t"))
63      {
64         int w, h;
65         emotion_object_size_get(em, &w, &h);
66         fprintf(stderr, "example -> size: %dx%d\n", w, h);
67      }
68    else if (!strcmp(ev->keyname, "s"))
69      {
70         float len, pos;
71         len = emotion_object_play_length_get(em);
72         pos = 0.98 * len;
73         fprintf(stderr, "skipping to position %0.3f\n", pos);
74         emotion_object_position_set(em, pos);
75      }
76    else if (!strcmp(ev->keyname, "1"))
77      {
78         fprintf(stderr, "setting speed to 1.0\n");
79         emotion_object_play_speed_set(em, 1.0);
80      }
81    else if (!strcmp(ev->keyname, "2"))
82      {
83         fprintf(stderr, "setting speed to 2.0\n");
84         emotion_object_play_speed_set(em, 2.0);
85      }
86    else if (!strcmp(ev->keyname, "n"))
87      {
88         const char *file;
89         if (!curfile)
90           curfile = filenames;
91         else
92           curfile = eina_list_next(curfile);
93         file = eina_list_data_get(curfile);
94         fprintf(stderr, "playing next file: %s\n", file);
95         emotion_object_file_set(em, file);
96      }
97    else if (!strcmp(ev->keyname, "p"))
98      {
99         const char *file;
100         if (!curfile)
101           curfile = eina_list_last(filenames);
102         else
103           curfile = eina_list_prev(curfile);
104         file = eina_list_data_get(curfile);
105         fprintf(stderr, "playing next file: %s\n", file);
106         emotion_object_file_set(em, file);
107      }
108    else if (!strcmp(ev->keyname, "d"))
109      {
110         evas_object_del(em);
111      }
112    else if (!strcmp(ev->keyname, "l"))
113      {
114         // force frame dropping
115         sleep(5);
116      }
117    else
118      {
119         fprintf(stderr, "unhandled key: %s\n", ev->keyname);
120      }
121 }
122
123 static void
124 _frame_decode_cb(void *data, Evas_Object *o, void *event_info)
125 {
126    // fprintf(stderr, "smartcb: frame_decode\n");
127 }
128
129 static void
130 _length_change_cb(void *data, Evas_Object *o, void *event_info)
131 {
132    fprintf(stderr, "smartcb: length_change: %0.3f\n", emotion_object_play_length_get(o));
133 }
134
135 static void
136 _position_update_cb(void *data, Evas_Object *o, void *event_info)
137 {
138    fprintf(stderr, "smartcb: position_update: %0.3f\n", emotion_object_position_get(o));
139 }
140
141 static void
142 _progress_change_cb(void *data, Evas_Object *o, void *event_info)
143 {
144    fprintf(stderr, "smartcb: progress_change: %0.3f, %s\n",
145            emotion_object_progress_status_get(o),
146            emotion_object_progress_info_get(o));
147 }
148
149 static void
150 _frame_resize_cb(void *data, Evas_Object *o, void *event_info)
151 {
152    int w, h;
153    emotion_object_size_get(o, &w, &h);
154    fprintf(stderr, "smartcb: frame_resize: %dx%d\n", w, h);
155 }
156
157 int
158 main(int argc, const char *argv[])
159 {
160    Ecore_Evas *ee;
161    Evas *e;
162    Evas_Object *bg, *em;
163    int i;
164
165    if (argc < 2)
166      {
167         printf("One argument is necessary. Usage:\n");
168         printf("\t%s <filename>\n", argv[0]);
169      }
170
171    eina_init();
172    for (i = 1; i < argc; i++)
173      filenames = eina_list_append(filenames, eina_stringshare_add(argv[i]));
174
175    curfile = filenames;
176
177    if (!ecore_evas_init())
178      return EXIT_FAILURE;
179
180    /* this will give you a window with an Evas canvas under the first
181     * engine available */
182    ee = ecore_evas_new(NULL, 10, 10, WIDTH, HEIGHT, NULL);
183    if (!ee)
184      goto error;
185
186    ecore_evas_show(ee);
187
188    /* the canvas pointer, de facto */
189    e = ecore_evas_get(ee);
190
191    /* adding a background to this example */
192    bg = evas_object_rectangle_add(e);
193    evas_object_name_set(bg, "our dear rectangle");
194    evas_object_color_set(bg, 255, 255, 255, 255); /* white bg */
195    evas_object_move(bg, 0, 0); /* at canvas' origin */
196    evas_object_resize(bg, WIDTH, HEIGHT); /* covers full canvas */
197    evas_object_show(bg);
198
199    /* Creating the emotion object */
200    em = _create_emotion_object(e);
201    emotion_object_file_set(em, eina_list_data_get(curfile));
202    evas_object_move(em, 0, 0);
203    evas_object_resize(em, WIDTH, HEIGHT);
204    evas_object_show(em);
205
206    evas_object_smart_callback_add(em, "frame_decode", _frame_decode_cb, NULL);
207    evas_object_smart_callback_add(em, "length_change", _length_change_cb, NULL);
208    evas_object_smart_callback_add(em, "position_update", _position_update_cb, NULL);
209    evas_object_smart_callback_add(em, "progress_change", _progress_change_cb, NULL);
210    evas_object_smart_callback_add(em, "frame_resize", _frame_resize_cb, NULL);
211
212    evas_object_event_callback_add(bg, EVAS_CALLBACK_KEY_DOWN, _on_key_down, em);
213    evas_object_focus_set(bg, EINA_TRUE);
214
215    emotion_object_play_set(em, EINA_TRUE);
216
217    ecore_main_loop_begin();
218
219    ecore_evas_free(ee);
220    ecore_evas_shutdown();
221    return 0;
222
223 error:
224    fprintf(stderr, "you got to have at least one evas engine built and linked"
225                    " up to ecore-evas for this example to run properly.\n");
226
227    EINA_LIST_FREE(filenames, curfile)
228       eina_stringshare_del(eina_list_data_get(curfile));
229
230    ecore_evas_shutdown();
231    eina_shutdown();
232    return -1;
233 }