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