Tizen 2.1 base
[apps/core/preloaded/ug-camera-efl.git] / src / edc-callback / cam_recording_edc_callback.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *        http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include "cam.h"
19 #include "cam_app.h"
20 #include "cam_rec.h"
21 #include "cam_mm.h"
22 #include "cam_file.h"
23 #include "edc_string.h"
24 #include "camera_utils.h"
25 #include "cam_property.h"
26 #include "cam_toolbar_edc_callback.h"
27 #include "cam_recording_edc_callback.h"
28
29 #define REC_ICON_TIMER_INTERVAL (1)
30 #define REC_TIMER_INTERVAL (0.5)
31 #define PAUSE_TIMER_INTERVAL (0.5)
32 #define PAUSE_TIMEOUT (120)
33 #define SECONDS_IN_HOUR (1*60*60)
34
35 enum _RecState {
36         CAM_REC_STATE_RECORDING,
37         CAM_REC_STATE_PAUSE,
38 };
39 #define TIME_FORMAT_MAX_LEN (128)
40 /* static int rec_state = CAM_REC_STATE_RECORDING; */
41 static double paused_time = 0.0;
42 Eina_Bool recording_update_time_cb(void *data);
43 Eina_Bool recording_update_icon_cb(void *data);
44 Eina_Bool pause_timeout_cb(void *data);
45
46 static void __recording_set_state(void *data, int new_state)
47 {
48         CAM_UI_LOG();
49         struct appdata *ad = (struct appdata *)data;
50         cam_ret_if(ad == NULL);
51
52         if (new_state == CAM_REC_STATE_RECORDING) {
53                 if (ad->rec_time_updater) {
54                         ecore_timer_thaw(ad->rec_time_updater);
55                 }
56                 if (ad->rec_icon_updater) {
57                         ecore_timer_thaw(ad->rec_icon_updater);
58                 }
59                 if (ad->rec_pause_timer) {
60                         ecore_timer_del(ad->rec_pause_timer);
61                         ad->rec_pause_timer = NULL;
62                 }
63                 edje_object_signal_emit(_EDJ(ad->recording_edje), "state,rec", "program");
64         } else if (new_state == CAM_REC_STATE_PAUSE) {
65                 if (ad->rec_time_updater) {
66                         ecore_timer_freeze(ad->rec_time_updater);
67                 }
68                 if (ad->rec_icon_updater) {
69                         ecore_timer_freeze(ad->rec_icon_updater);
70                 }
71                 if (ad->rec_pause_timer) {
72                         ecore_timer_del(ad->rec_pause_timer);
73                         ad->rec_pause_timer = NULL;
74                 }
75                 paused_time = 0.0;
76                 ad->rec_pause_timer = ecore_timer_add(REC_TIMER_INTERVAL, pause_timeout_cb, ad);
77                 edje_object_signal_emit(_EDJ(ad->recording_edje), "state,pause", "program");
78         }
79         ad->recording_state = new_state;
80 }
81
82 /* you can use this function to set time text for recording */
83 void recording_set_time(void *data, int hh, int mm, int ss)
84 {
85         CAM_UI_LOG();
86         struct appdata *ad = (struct appdata *)data;
87         if (ad->recording_edje) {
88                 char time_text[100] = "";
89                 snprintf(time_text, sizeof(time_text), "%" CAM_TIME_FORMAT "", hh, mm, ss);
90                 edje_object_part_text_set(_EDJ(ad->recording_edje), RECORDING_TEXT, time_text);
91         }
92 }
93
94 Eina_Bool pause_timeout_cb(void *data)
95 {
96         struct appdata *ad = (struct appdata *)data;
97         cam_retv_if(ad == NULL, FALSE);
98         CamAppData *camapp = NULL;
99         camapp = ad->camapp_handle;
100         if (camapp == NULL) {
101                 if (ad->rec_pause_timer)
102                         ad->rec_pause_timer = NULL;
103                 return ECORE_CALLBACK_CANCEL;
104         }
105
106         paused_time += PAUSE_TIMER_INTERVAL;
107         if (paused_time >= PAUSE_TIMEOUT) {
108                 camapp->rec_stop_type = CAM_REC_STOP_NORMAL;
109                 ad->recording_commit = ecore_idler_add(cam_video_idler_record_stop, ad);
110
111                 if (ad->rec_pause_timer)
112                 ad->rec_pause_timer = NULL;
113                 return ECORE_CALLBACK_CANCEL;
114         }
115         if (ad->recording_edje) {
116                 const char *state = edje_object_part_state_get(_EDJ(ad->recording_edje), RECORDING_IMAGE, NULL);
117
118                 if (strcmp(state, "pause") == 0) {
119                         edje_object_signal_emit(_EDJ(ad->recording_edje), "indicator,hide", "program");
120                 } else if (strcmp(state, "invisible") == 0) {
121                         edje_object_signal_emit(_EDJ(ad->recording_edje), "indicator,show", "program");
122                 }
123         }
124         return ECORE_CALLBACK_RENEW;
125 }
126
127 Eina_Bool recording_update_icon_cb(void *data)
128 {
129         struct appdata *ad = (struct appdata *)data;
130         static int count = 0;
131         if (ad == NULL) {
132                 cam_critical(LOG_UI, "appdata is NULL");
133                 /*      ad->rec_icon_updater = NULL; */
134                 return ECORE_CALLBACK_CANCEL;
135         }
136         if (count) {
137                 edje_object_signal_emit(_EDJ(ad->recording_edje), "state,rec", "program");
138         } else {
139                 edje_object_signal_emit(_EDJ(ad->recording_edje), "state,rec1", "program");
140         }
141         count = !count;
142         return ECORE_CALLBACK_RENEW;
143 }
144
145 Eina_Bool recording_update_time_cb(void *data)
146 {
147         CAM_UI_LOG();
148         struct appdata *ad = (struct appdata *)data;
149         CamAppData *camapp = NULL;
150         if (ad == NULL) {
151                 cam_critical(LOG_UI, "appdata is NULL");
152                 return ECORE_CALLBACK_CANCEL;
153         }
154         camapp = ad->camapp_handle;
155         if (camapp == NULL) {
156                 cam_critical(LOG_UI, "cam_handle is NULL");
157                 ad->rec_time_updater = NULL;
158                 return ECORE_CALLBACK_CANCEL;
159         }
160         char time_text[100] = "";
161         if (camapp->rec_elapsed < SECONDS_IN_HOUR)
162                 snprintf(time_text, sizeof(time_text), "%" CAM_TIME_FORMAT "", CAM_TIME_ARGS(camapp->rec_elapsed));
163         else
164                 snprintf(time_text, sizeof(time_text), "%" CAM_TIME_FORMAT2 "", CAM_TIME_ARGS2(camapp->rec_elapsed));
165         cam_debug(LOG_UI, "current time %s", time_text);
166         if (ad->recording_edje) {
167                 edje_object_part_text_set(_EDJ(ad->recording_edje), RECORDING_TEXT, _(time_text));
168         }
169
170         if (camapp->size_limit != CAM_REC_NORMAL_MAX_SIZE) {
171                 char str[10] = "";
172                 char str2[10] = "";
173                 char *filename = NULL;
174                 int fname_size = 0;/*unused*/
175                 guint64 size_limit = 0;
176                 gdouble pbar_position = 0.0;
177                 gdouble size_limit_in_mega = 0.0;
178                 gdouble rec_filesize_in_mega = 0.0;
179                 cam_mm_get_filename(&filename, &fname_size);
180                 if (camapp->rec_filesize) {
181                         size_limit = (guint64)camapp->size_limit;
182                         pbar_position = camapp->rec_filesize / (gdouble) size_limit;
183                         cam_app_set_progressbar_value(ad, pbar_position);
184
185                         if (camapp->size_limit < 1024) {
186                                 snprintf(str, sizeof(str), "%dK", camapp->size_limit);
187                         } else {
188                                 size_limit_in_mega = (gdouble)camapp->size_limit/(gdouble)1024;
189                                 snprintf(str, sizeof(str), "%.1fM", size_limit_in_mega);
190                         }
191
192                         if (camapp->rec_filesize < 1024) {
193                                 snprintf(str2, sizeof(str2), "%dK", camapp->rec_filesize);
194                         } else {
195                                 rec_filesize_in_mega = (gdouble)camapp->rec_filesize/(gdouble)1024;
196                                 snprintf(str2, sizeof(str2), "%.1fM", rec_filesize_in_mega);
197                         }
198
199                         edje_object_part_text_set(_EDJ(ad->recording_edje), "progressbar_text", str);
200                         edje_object_part_text_set(_EDJ(ad->recording_edje), "file_size_text", str2);
201                 }
202         }
203
204         gchar * target_path = NULL;
205         target_path = (gchar*)cam_app_get_target_path();
206         double ration = 0.0;
207         if (target_path != NULL) {
208                 gint64 free_space = cam_get_free_space(target_path);
209                 gint64 capacity_space = cam_get_capacity_space(target_path);
210                 ration = (double)free_space/(double)capacity_space;
211         }
212         DEBUG_TRACE("ration : %f ", ration);
213         if (ration < 0.20) {
214                 /*show left time*/
215                 char time_left_text[TIME_FORMAT_MAX_LEN + 1] = { '\0', };
216                 guint64 remain_rec_time = 0;
217                 remain_rec_time = cam_system_get_remain_rec_time((void*)ad);
218                 DEBUG_TRACE("%d",remain_rec_time);
219                 if (remain_rec_time < SECONDS_IN_HOUR) {
220                         snprintf(time_left_text, TIME_FORMAT_MAX_LEN, "%" CAM_TIME_FORMAT "", CAM_TIME_ARGS(remain_rec_time));
221                         DEBUG_TRACE("%s",time_left_text);
222                 } else {
223                         snprintf(time_left_text, TIME_FORMAT_MAX_LEN, "%" CAM_TIME_FORMAT2 "", CAM_TIME_ARGS2(remain_rec_time));
224                         DEBUG_TRACE("%s",time_left_text);
225                 }
226                 edje_object_part_text_set(_EDJ(ad->recording_edje), RECORDING_LEFT_TEXT, _(time_left_text));
227
228         } else {
229                 edje_object_part_text_set(_EDJ(ad->recording_edje), RECORDING_LEFT_TEXT, _(""));
230         }
231         return ECORE_CALLBACK_RENEW;
232 }
233
234 int load_recording_edje(struct appdata *ad)
235 {
236         CamAppData *camapp = NULL;
237         cam_retvm_if(ad == NULL, EXIT_FAILURE, "appdata is NULL");
238         camapp = ad->camapp_handle;
239         cam_retvm_if(camapp == NULL, EXIT_FAILURE, "cam_handle is NULL");
240
241         cam_toolbar_update(ad);
242         DEL_EVAS_OBJECT(ad->recording_edje);
243
244         elm_object_part_content_unset(ad->ug_base, "recording_layout");
245
246         DEBUG_TRACE("ad->target_direction %d", ad->target_direction);
247         switch (ad->target_direction) {
248         case CAM_TARGET_DIRECTION_LANDSCAPE:
249                 ad->recording_edje = cam_app_load_edj(ad->ug_base, CAM_RECORDING_EDJ_NAME, "recording_layout");
250                 break;
251         case CAM_TARGET_DIRECTION_LANDSCAPE_INVERSE:
252                 ad->recording_edje = cam_app_load_edj(ad->ug_base, CAM_RECORDING_INVERSE_EDJ_NAME, "recording_layout");
253                 break;
254         case CAM_TARGET_DIRECTION_PORTRAIT:
255                 ad->recording_edje = cam_app_load_edj(ad->ug_base, CAM_RECORDING_VERTICAL_EDJ_NAME, "recording_layout");
256                 break;
257         case CAM_TARGET_DIRECTION_PORTRAIT_INVERSE:
258                 ad->recording_edje = cam_app_load_edj(ad->ug_base, CAM_RECORDING_VERTICAL_INVERSE_EDJ_NAME, "recording_layout");
259                 break;
260         default:
261                 DEBUG_TRACE("note:reach un-able reached codes");
262         }
263
264         cam_retv_if(ad->recording_edje == NULL, -1);
265         elm_object_part_content_set(ad->ug_base, "recording_layout", ad->recording_edje);
266
267         if (camapp->size_limit != CAM_REC_NORMAL_MAX_SIZE) {
268                 char str[10] = "";
269                 char str2[10] = "";
270                 gdouble size_limit_in_mega = 0.0;
271                 camapp->rec_filesize =0;
272
273                 if (camapp->size_limit < 1024) {
274                         snprintf(str, sizeof(str), "%dK", camapp->size_limit);
275                 } else {
276                         size_limit_in_mega = (gdouble)camapp->size_limit/(gdouble)1024;
277                         snprintf(str, sizeof(str), "%.1fM", size_limit_in_mega);
278                 }
279
280                 snprintf(str2, sizeof(str), "%dK", 0);
281                 cam_app_create_progressbar(ad);
282
283                 /* cam_app_set_progressbar_value(ad, 0.0); */
284                 elm_object_part_content_set(ad->recording_edje, "progressbar", ad->progressbar);
285                 edje_object_part_text_set(_EDJ(ad->recording_edje), "progressbar_text", str);
286                 edje_object_part_text_set(_EDJ(ad->recording_edje), "file_size_text", str2);
287         }
288         camapp->rec_elapsed = 0;
289         __recording_set_state(ad, CAM_REC_STATE_RECORDING);
290         recording_set_time(ad, 0, 0, 0);
291         if (ad->rec_time_updater != NULL)
292                 ecore_timer_del(ad->rec_time_updater);
293         if (ad->rec_icon_updater != NULL)
294                 ecore_timer_del(ad->rec_icon_updater);
295         ad->rec_time_updater = ecore_timer_add(REC_TIMER_INTERVAL, recording_update_time_cb, ad);
296         ad->rec_icon_updater = ecore_timer_add(REC_ICON_TIMER_INTERVAL, recording_update_icon_cb, ad);
297
298         return EXIT_SUCCESS;
299 }
300
301 int unload_recording_edje(struct appdata *ad)
302 {
303         cam_retv_if(ad == NULL, -1);
304         cam_app_destroy_progressbar(ad);
305         DEL_EVAS_OBJECT(ad->recording_edje);
306         REMOVE_TIMER(ad->rec_time_updater);
307         REMOVE_TIMER(ad->rec_icon_updater);
308
309         cam_toolbar_update(ad);
310
311         return EXIT_SUCCESS;
312 }
313
314 int cam_recording_rotate(struct appdata *ad)
315 {
316         cam_retv_if(ad == NULL, -1);
317         CamAppData *camapp = ad->camapp_handle;
318         cam_retvm_if(camapp == NULL, EXIT_FAILURE, "cam_handle is NULL");
319
320         evas_object_hide(ad->indicator_edje);
321         elm_object_part_content_unset(ad->recording_edje, "progressbar");
322         evas_object_hide(ad->progressbar);
323         DEL_EVAS_OBJECT(ad->recording_edje);
324
325         elm_object_part_content_unset(ad->ug_base, "recording_layout");
326
327         switch (ad->target_direction) {
328         case CAM_TARGET_DIRECTION_LANDSCAPE:
329                 ad->recording_edje = cam_app_load_edj(ad->ug_base, CAM_RECORDING_EDJ_NAME, "recording_layout");
330                 break;
331         case CAM_TARGET_DIRECTION_LANDSCAPE_INVERSE:
332                 ad->recording_edje = cam_app_load_edj(ad->ug_base, CAM_RECORDING_INVERSE_EDJ_NAME, "recording_layout");
333                 break;
334         case CAM_TARGET_DIRECTION_PORTRAIT:
335                 ad->recording_edje = cam_app_load_edj(ad->ug_base, CAM_RECORDING_VERTICAL_EDJ_NAME, "recording_layout");
336                 break;
337         case CAM_TARGET_DIRECTION_PORTRAIT_INVERSE:
338                 ad->recording_edje = cam_app_load_edj(ad->ug_base, CAM_RECORDING_VERTICAL_INVERSE_EDJ_NAME, "recording_layout");
339                 break;
340         default:
341                 DEBUG_TRACE("note:reach un-able reached codes");
342         }
343
344         cam_retv_if(ad->recording_edje == NULL, -1);
345         elm_object_part_content_set(ad->ug_base, "recording_layout", ad->recording_edje);
346
347         if (ad->recording_state == CAM_REC_STATE_RECORDING) {
348                 edje_object_signal_emit(_EDJ(ad->recording_edje), "state,rec", "program");
349         } else if (ad->recording_state == CAM_REC_STATE_PAUSE) {
350                 edje_object_signal_emit(_EDJ(ad->recording_edje), "state,pause", "program");
351         }
352         char time_text[100] = "";
353         if (camapp->rec_elapsed < 3600)
354                 snprintf(time_text, sizeof(time_text), "%" CAM_TIME_FORMAT "",
355                          CAM_TIME_ARGS(camapp->rec_elapsed));
356         else
357                 snprintf(time_text, sizeof(time_text), "%" CAM_TIME_FORMAT2 "",
358                          CAM_TIME_ARGS2(camapp->rec_elapsed));
359         cam_debug(LOG_UI, "current time %s", time_text);
360         if (ad->recording_edje) {
361                 edje_object_part_text_set(_EDJ(ad->recording_edje), RECORDING_TEXT, _(time_text));
362         }
363         if (ad->progressbar) {
364                 char str[10] = "";
365                 char str2[10] = "";
366                 char *filename = NULL;
367                 int fname_size = 0;
368                 guint64 size_limit = 0;
369                 gdouble pbar_position = 0.0;
370                 cam_mm_get_filename(&filename, &fname_size);
371                 if (camapp->rec_filesize) {
372                         size_limit = (guint64)camapp->size_limit;
373                         pbar_position =camapp->rec_filesize/ (double)size_limit;
374                         cam_app_set_progressbar_value(ad, pbar_position);
375                         snprintf(str, sizeof(str), "%dK", camapp->size_limit);
376                         snprintf(str2, sizeof(str2), "%dK", camapp->rec_filesize);
377                         elm_object_part_content_set(ad->recording_edje, "progressbar", ad->progressbar);
378                         edje_object_part_text_set(_EDJ(ad->recording_edje), "progressbar_text", str);
379                         edje_object_part_text_set(_EDJ(ad->recording_edje), "progressbar_text", str2);
380                 }
381         }
382         return EXIT_SUCCESS;
383 }