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