2 * Copyright 2012 Samsung Electronics Co., Ltd
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
8 * http://www.tizenopensource.org/license
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.
25 #include "camera_utils.h"
28 #include "cam_debug.h"
29 #include "cam_toolbar_edc_callback.h"
30 #include "cam_zoom_edc_callback.h"
31 #include "cam_indicator_edc_callback.h"
32 #include "cam_common_edc_callback.h"
35 #include "cam_menu_composer.h"
36 #include "cam_device_capacity.h"
39 static int __is_idle_lock(void);
41 static int _is_running(void)
43 #ifdef CHECK_RUNNING_PROC
46 const gchar *dir_name;
47 char path[255] = { 0, };
48 char proc_name[255] = { 0, };
49 int fd = 0, count = 0;
50 dir = g_dir_open("/proc", 0, &error);
53 while ((dir_name = g_dir_read_name(dir)) != NULL) {
54 if (!isdigit(dir_name[0]))
57 snprintf(path, sizeof(path), "/proc/%s/cmdline",
59 fd = open(path, O_RDONLY);
60 read(fd, proc_name, sizeof(proc_name) - 1);
62 if (strstr(proc_name, "camera")) {
68 ("camera application[PID: %d] is already running !!!!\n\n",
77 printf("cannot open proc directory [%s]", error->message);
89 static int __convert_orientation_to_angle(app_device_orientation_e orientation)
91 switch (orientation) {
92 case APP_DEVICE_ORIENTATION_0:
95 case APP_DEVICE_ORIENTATION_180:
98 case APP_DEVICE_ORIENTATION_270:
101 case APP_DEVICE_ORIENTATION_90:
109 static bool cam_create(void *data)
111 CAM_TA_ACUM_ITEM_BEGIN("==cam_create==", 0);
112 cam_debug(LOG_UI, "************** cam_create START **************");
114 if (__is_idle_lock()) {
115 return FALSE;/*fix crash*/
118 struct appdata *ad = data;
119 Ecore_X_Display *dpy = NULL;
120 debug_fenter(LOG_CAM);
122 /* create main window */
123 ad->win_main = cam_app_create_win(PACKAGE);
124 cam_retvm_if(ad->win_main == NULL, FALSE, "cam_app_create_win failed");
126 cam_win_transparent_set(ad);
129 dpy = ecore_x_display_get();
132 win = elm_win_xwindow_get(ad->win_main);
134 printf("dpy is not null .. set no effect to display = %d\n",
136 utilx_set_window_effect_style(dpy, win,
137 UTILX_EFFECT_TYPE_ROTATION,
138 UTILX_EFFECT_STYLE_NONE);
142 #ifndef CAMERA_MACHINE_I686
143 elm_win_indicator_mode_set(ad->win_main, ELM_WIN_INDICATOR_HIDE);
147 ad->evas = evas_object_evas_get(ad->win_main);
148 ad->ee = ecore_evas_ecore_evas_get(ad->evas);
149 ad->main_xid = elm_win_xwindow_get(ad->win_main);
150 ad->launch_type = CAM_LAUNCH_FIRST;
152 /* camera application initialization */
153 CAM_TA_ACUM_ITEM_BEGIN(" cam_app_init", 0);
154 if (!cam_app_init(ad))
156 CAM_TA_ACUM_ITEM_END(" cam_app_init", 0);
158 /*add camera exteral engine lib load*/
159 open_cam_ext_handle();
161 ad->rot_current = app_get_device_orientation();
162 ad->rot_previous = ad->rot_current;
163 int win_angle = elm_win_rotation_get(ad->win_main);
164 ad->angle = __convert_orientation_to_angle(ad->rot_current);
166 if(win_angle != ad->angle) {
167 cam_critical(LOG_UI, "win_angle:%d device_angle:%d ", win_angle, ad->angle);
168 elm_win_rotation_with_resize_set(ad->win_main, ad->angle);
171 evas_object_show(ad->win_main);
173 cam_debug(LOG_UI, "******** cam_create END**********");
174 CAM_TA_ACUM_ITEM_END("==cam_create==", 0);
181 static void cam_terminate(void *data)
183 cam_debug(LOG_UI, "############## cam_terminate ##############");
185 struct appdata *ad = (struct appdata *)data;
187 ad->appcore_state = CAM_APPCORE_TERMINATE_STATE;
190 debug_fenter(LOG_CAM);
192 CAM_TA_ACUM_ITEM_BEGIN("cam_terminate", 0);
194 cam_key_grab_deinit(ad);
196 #ifdef USE_FIFO_THREAD
197 cam_app_FIFO_thread_exit();
200 #ifdef USE_FILE_REG_THREAD
201 cam_app_file_register_thread_exit(ad);
204 CAM_TA_ACUM_ITEM_BEGIN(" cam_app_quit", 0);
206 CAM_TA_ACUM_ITEM_END(" cam_app_quit", 0);
208 #ifdef ENABLE_CAM_POWER_CONTROL
209 power_unlock_state(POWER_STATE_NORMAL);
212 CAM_TA_ACUM_ITEM_END("app_terminate", 0);
214 CAM_TA_ACUM_ITEM_SHOW_RESULT_TO(CAM_TA_SHOW_FILE);
217 close_cam_ext_handle();
220 static void cam_pause(void *data)
222 cam_debug(LOG_UI, "############## cam_pause ##############");
224 struct appdata *ad = (struct appdata *)data;
225 cam_ret_if(ad == NULL);
226 CamAppData *camapp = ad->camapp_handle;
227 cam_ret_if(camapp == NULL);
228 debug_fenter(LOG_CAM);
229 ad->appcore_state = CAM_APPCORE_PAUSE_STATE;
231 if ( ad->launching_mode == CAM_LAUNCHING_MODE_EXTERNAL) {
233 mm_state = cam_mm_get_state();
234 ad->appcore_state = CAM_APPCORE_PAUSE_STATE;
236 if (mm_state < CAMERA_STATE_CAPTURING) { /* MM_CAMCORDER_STATE_CAPTURING < MM_CAMCORDER_STATE_RECORDING */
237 if (ad->imageviewer_ug) { /*Is this paused by Video player ? */
238 DEBUG_TRACE("###### ug_pause() ######");
245 } else if (mm_state == RECORDER_STATE_RECORDING && camapp->camera_mode == CAM_CAMCORDER_MODE) {
246 if (!cam_video_record_stop(ad)) {
247 DEBUG_TRACE("record stop fail");
253 if (ad->imageviewer_ug) {
254 DEBUG_TRACE("###### ug_pause() ######");
258 if (ad->foucs_out_from_quickview == TRUE) {
259 DEBUG_TRACE("image viewer UG->image editor");
260 ad->foucs_out_from_quickview = FALSE;
262 #ifdef ENABLE_CAM_POWER_CONTROL
267 CAM_TA_ACUM_ITEM_BEGIN("app_stop", 0);
269 ad->foucs_out_from_quickview = FALSE;
270 ad->launch_type = CAM_LAUNCH_RELAUNCH; /* set launch type */
273 #ifdef ENABLE_CAM_POWER_CONTROL
274 power_unlock_state(POWER_STATE_NORMAL);
277 CAM_TA_ACUM_ITEM_END("app_stop", 0);
281 static void cam_resume(void *data)
283 cam_debug(LOG_UI, "############## cam_resume ##############");
285 struct appdata *ad = (struct appdata *)data;
286 cam_ret_if(ad == NULL);
287 debug_fenter(LOG_CAM);
289 CamAppData *camapp = ad->camapp_handle;
290 cam_ret_if(camapp == NULL);
292 ad->appcore_state = CAM_APPCORE_RESUME_STATE;
294 if (!cam_check_dir()) {
295 cam_app_notice_popup(data, "Cannot make default path",
296 cam_app_timeout_notice_response_cb);
299 if (!cam_utils_check_torchlight_status(ad)) {
300 DEBUG_TRACE("Can not get torchlight status");
303 if (ad->imageviewer_ug) { /* bug fix camera app overlab with imageviewer_ug */
304 DEBUG_TRACE("####### ug_resume() ######");
309 #ifdef ENABLE_CAM_POWER_CONTROL
311 power_lock_state(POWER_STATE_NORMAL, 0);
317 CAM_TA_ACUM_ITEM_BEGIN(" cam_app_resume", 0);
318 if (!cam_app_resume(ad))
320 CAM_TA_ACUM_ITEM_END(" cam_app_resume", 0);
325 static void cam_service(service_h service, void *user_data)
327 CAM_TA_ACUM_ITEM_BEGIN("==cam_service==", 0);
328 cam_debug(LOG_UI, "############## cam_service START##############");
329 struct appdata *ad = (struct appdata *)user_data;
330 cam_ret_if(ad == NULL);
331 CamAppData *camapp = ad->camapp_handle;
332 cam_ret_if(camapp == NULL);
334 DEBUG_TRACE("ad->launch_type = %d ", ad->launch_type);
335 gint latest_mode = 0;
336 ad->appcore_state = CAM_APPCORE_RESET_STATE;
338 if (!cam_check_dir()) {
339 cam_app_notice_popup(ad, "Cannot make default path",
340 cam_app_timeout_notice_response_cb);
343 if (!cam_utils_check_torchlight_status(ad)) {
344 DEBUG_TRACE("Can not get torchlight status");
346 if (ad->launch_type == CAM_LAUNCH_IS_ALREADY_RUNNING) {
347 cam_app_notice_popup(ad, "Camera is alreay running.",
348 cam_app_popup_response_cb);
351 /* remove exe args */
353 if (ad->exe_args->caller) {
354 free(ad->exe_args->caller);
355 ad->exe_args->caller = NULL;
361 char *operation = NULL;
363 int ret = service_get_operation(service, &operation);
364 if (ret != SERVICE_ERROR_NONE)
366 cam_debug(LOG_UI, "service_get_operation failed");
370 if ( operation == NULL )
372 cam_debug(LOG_UI, "operation is null");
376 ret = service_clone(&ad->service_handle, service);
377 if (ret != SERVICE_ERROR_NONE)
379 cam_debug(LOG_UI, "service_clone failed");
383 CamExeArgs *args = (CamExeArgs *)malloc(sizeof(CamExeArgs));
387 memset(args,0,sizeof(CamExeArgs));
389 if (!cam_app_parse_args(args, service)) {
394 if( strcmp(operation, SERVICE_OPERATION_CREATE_CONTENT ) == 0){
395 cam_debug(LOG_UI, "Operation is SERVICE_OPERATION_CREATE_CONTENT");
397 ad->launching_mode = CAM_LAUNCHING_MODE_EXTERNAL;
400 CAM_TA_ACUM_ITEM_BEGIN(" cam_handle_init", 0);
401 if (!cam_handle_init(ad, ad->exe_args->cam_mode)) {
402 cam_critical(LOG_CAM, "cam_handle_init failed");
405 CAM_TA_ACUM_ITEM_END(" cam_handle_init", 0);
407 cam_app_init_with_args(ad);
409 ad->launching_mode = CAM_LAUNCHING_MODE_NORMAL;
411 latest_mode = cam_app_get_latest_mode();
413 CAM_TA_ACUM_ITEM_BEGIN(" cam_handle_init", 0);
414 if (!cam_handle_init(ad, latest_mode)) {
415 cam_critical(LOG_CAM, "cam_handle_init failed");
418 CAM_TA_ACUM_ITEM_END(" cam_handle_init", 0);
421 if (ad->launch_type == CAM_LAUNCH_FIRST) {
422 debug_fenter(LOG_CAM);
424 if (!open_cam_handle((void*)ad)) {
425 cam_critical(LOG_CAM, "open_cam_handle failed");
429 #ifdef ENABLE_CAM_POWER_CONTROL
430 power_lock_state(POWER_STATE_NORMAL, 0);
433 /*elm_win_alpha_set(ad->win_main, EINA_TRUE);*/
435 ad->setting_menu_composer = NULL;
437 if( ad->setting_menu_composer == NULL){
438 ad->setting_menu_composer = malloc(sizeof(ad->setting_menu_composer));
439 cam_compose_setting_menu(ad->setting_menu_composer);
442 CAM_TA_ACUM_ITEM_BEGIN(" cam_layout_init", 0);
443 if (!cam_layout_init(ad)) {
444 cam_critical(LOG_UI, "cam_layout_init failed");
447 CAM_TA_ACUM_ITEM_END(" cam_layout_init", 0);
449 ecore_idler_add(cam_app_start, ad);
451 /* RELUNCHING!! //in paused -> Camera process is called by External process */
453 cam_debug(LOG_SYS, "RELUNCHING !!!");
456 if (ad->imageviewer_ug) {
457 DEBUG_TRACE("destory imageviewer");
458 ug_destroy(ad->imageviewer_ug);
459 ad->imageviewer_ug = NULL;
462 cam_layout_del_all(ad);
464 #ifdef ENABLE_CAM_POWER_CONTROL
465 power_lock_state(POWER_STATE_NORMAL, 0);
468 if (ad->exe_args == NULL)
473 CAM_TA_ACUM_ITEM_BEGIN(" cam_layout_init", 0);
474 if (!cam_layout_init(ad)) {
475 cam_critical(LOG_UI, "cam_layout_init failed");
478 CAM_TA_ACUM_ITEM_END(" cam_layout_init", 0);
480 evas_object_raise(ad->win_main);
481 ecore_idler_add(cam_app_start, ad);
484 cam_debug(LOG_UI, "############## cam_service END##############");
485 CAM_TA_ACUM_ITEM_END("==cam_service==", 0);
488 static int __is_idle_lock(void)
494 vconf_ret = vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &vconf_val);
496 if (vconf_val == VCONFKEY_IDLE_LOCK) {
497 DEBUG_TRACE(" IDLE IN LOCK STATE ");
506 #define CAM_EXT_LIB_PATH "/usr/lib/libcamera-external-engine.so"
509 gboolean open_cam_ext_handle()
513 handle = dlopen(CAM_EXT_LIB_PATH, RTLD_NOW);
521 void close_cam_ext_handle()
530 gboolean open_cam_handle(void* data)
532 struct appdata *ad = (struct appdata *)data;
533 CamAppData *camapp = NULL;
535 cam_retvm_if(ad == NULL, FALSE, "appdata is NULL");
536 camapp = ad->camapp_handle;
537 cam_retvm_if(camapp == NULL, FALSE, "camapp_handle is NULL");
539 if (camapp->camera_mode == CAM_CAMERA_MODE) {
540 DEBUG_TRACE("session_type : SOUND_SESSION_TYPE_SHARE");
541 session_type = SOUND_SESSION_TYPE_SHARE;
543 DEBUG_TRACE("session_type : SOUND_SESSION_TYPE_EXCLUSIVE");
544 session_type = SOUND_SESSION_TYPE_EXCLUSIVE;
546 if (!cam_mm_session_init(session_type)) {
547 cam_critical(LOG_MM, "cam_mm_session_init faild");
551 /*open camera handle*/
552 if (!cam_mm_create(CAM_DEVICE_MEGA, camapp->camera_mode)) {
553 cam_critical(LOG_MM, "cam_mm_create failed");
554 cam_app_notice_popup(ad, "UNABLE TO LAUNCH CAMERA", cam_app_timeout_notice_response_cb);
562 static void cam_low_power_cb(void *data)
564 struct appdata *ad = (struct appdata *)data;
566 cam_info(LOG_SYS, "Low battery !!");
568 cam_retm_if(ad == NULL, "appdata is NULL");
569 CamAppData *camapp = ad->camapp_handle;
570 cam_retm_if(camapp == NULL, "camapp_handle is NULL");
571 if (evas_object_visible_get(ad->win_main)) {
572 int state = cam_mm_get_state();
573 if ((state == RECORDER_STATE_RECORDING
574 || state == RECORDER_STATE_PAUSED)
575 &&camapp->camera_mode == CAM_CAMCORDER_MODE) {
576 camapp->rec_stop_type = CAM_REC_STOP_LOW_BATTERY;
577 ad->recording_commit =
578 ecore_idler_add(cam_video_idler_record_stop, ad);
586 static void cam_update_ts_cb(void *data)
588 struct appdata *ad = (struct appdata *)data;
590 cam_info(LOG_SYS, "#################Update String !!");
592 cam_retm_if(ad == NULL, "appdata is NULL");
595 elm_win_indicator_mode_set(ad->win_main, 0);
597 path = vconf_get_str(VCONFKEY_LANGSET);
604 update_ts_main(data);
606 enum ug_event ev = UG_EVENT_LANG_CHANGE;
610 static void cam_device_orientation_cb(app_device_orientation_e mode, void *data)
612 struct appdata *ad = (struct appdata *)data;
613 cam_retm_if(ad == NULL, "appdata is NULL");
614 if (ad->appcore_state == CAM_APPCORE_TERMINATE_STATE
615 || ad->appcore_state == CAM_APPCORE_PAUSE_STATE) {/*NOTE: in pause state, and terminate state, not cb*/
618 cam_retm_if(ad->camapp_handle == NULL, "ad->camapp_handle is NULL");
620 cam_debug(LOG_UI, "rotated : %d", mode);
622 /*TODO: now just return, if the last rotated is not finished*/
623 if (ad->is_rotating) {
628 ui_gadget_h ug = NULL;
630 enum ug_event ev = UG_EVENT_ROTATE_LANDSCAPE;
631 ad->rot_previous = ad->rot_current;
632 ad->rot_current = mode;
634 if (ad->toolbar_edj_file)
635 free(ad->toolbar_edj_file);
636 ad->toolbar_edj_file = NULL;
639 case APP_DEVICE_ORIENTATION_0:
641 ad->target_direction = CAM_TARGET_DIRECTION_PORTRAIT;
642 ev = UG_EVENT_ROTATE_PORTRAIT;
643 ad->camcorder_rotate = CAMERA_ROTATION_90;
644 ad->toolbar_edj_file = strdup(CAM_TOOLBAR_LAYOUT_VERTICAL_EDJ_NAME);
646 case APP_DEVICE_ORIENTATION_270:
648 ad->target_direction = CAM_TARGET_DIRECTION_LANDSCAPE;
649 ev = UG_EVENT_ROTATE_LANDSCAPE;
650 ad->camcorder_rotate = CAMERA_ROTATION_NONE;
651 ad->toolbar_edj_file = strdup(CAM_TOOLBAR_LAYOUT_EDJ_NAME);
653 case APP_DEVICE_ORIENTATION_180:
655 ad->target_direction = CAM_TARGET_DIRECTION_PORTRAIT_INVERSE;
656 ev = UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN;
657 ad->camcorder_rotate = CAMERA_ROTATION_270;
658 ad->toolbar_edj_file = strdup(CAM_TOOLBAR_LAYOUT_VERTICAL_INVERSE_EDJ_NAME);
660 case APP_DEVICE_ORIENTATION_90:
662 ad->target_direction = CAM_TARGET_DIRECTION_LANDSCAPE_INVERSE;
663 ev = UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN;
664 ad->camcorder_rotate = CAMERA_ROTATION_180;
665 ad->toolbar_edj_file = strdup(CAM_TOOLBAR_LAYOUT_INVERSE_EDJ_NAME);
673 if (ad->imageviewer_ug) {
674 ug = ad->imageviewer_ug;
677 enum ug_mode md = ug_get_mode(ug);
678 if (md == UG_MODE_FULLVIEW) {
679 if(ev == UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN)
681 elm_win_rotation_with_resize_set(ad->win_main, angle);
682 /* send event to ug */
688 unsigned int *main_pipe_op_type = NULL;
689 main_pipe_op_type = (unsigned int *)calloc(1, sizeof(unsigned int));
690 if (main_pipe_op_type)
691 *main_pipe_op_type = CAM_MAIN_PIPE_OP_TYPE_ROTATE_ANIMATOR;
692 ecore_pipe_write(ad->main_pipe, (void *)main_pipe_op_type, sizeof(unsigned int));
696 int main(int argc, char *argv[])
705 /*set appdata init variable*/
706 memset(&ad, 0x0, sizeof(struct appdata));
711 DEBUG_TRACE("argc = %d", argc, argv);
712 for (i = 0; i < argc; i++) {
713 DEBUG_TRACE("argv[%d] = %s", i, argv[i]);
716 app_event_callback_s event_callbacks;
718 event_callbacks.create = cam_create;
719 event_callbacks.terminate = cam_terminate;
720 event_callbacks.pause = cam_pause;
721 event_callbacks.resume = cam_resume;
722 event_callbacks.service = cam_service;
724 event_callbacks.language_changed = cam_update_ts_cb;
725 event_callbacks.low_battery = cam_low_power_cb;
726 event_callbacks.device_orientation = cam_device_orientation_cb;
728 event_callbacks.low_memory = NULL;
729 event_callbacks.region_format_changed = NULL;
731 setenv("ELM_ENGINE", "gl", 1); /*add open gl support for 3D slide_show of image viewer*/
732 ecore_evas_app_comp_sync_set(EINA_FALSE);
734 return app_efl_main(&argc, &argv, &event_callbacks, &ad);
738 int update_ts_main(void *data)
742 struct appdata *ad = (struct appdata *)data;
744 if (ad && ad->layout_main)
745 edje_object_part_text_set(_EDJ(ad->layout_main), "txt_title", dgettext("sys_string", "IDS_COM_BODY_CAMERA"));