127e8f0025ed39421434279e6dd8528da7542ee6
[profile/ivi/ico-uxf-homescreen.git] / src / homescreen / CicoHSControlBarWindow.cpp
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9 /**
10  * @brief   control bar window
11  *
12  * @date    Aug-08-2013
13  */
14 #include "CicoHSControlBarWindow.h"
15 #include "CicoHSControlBarTouch.h"
16 #include "CicoGKeyFileConfig.h"
17 #include "CicoResourceConfig.h"
18 #include "CicoHomeScreen.h"
19 #include "CicoHSSystemState.h"
20 #include "CicoSound.h"
21
22 /*============================================================================*/
23 /* functions                                                                  */
24 /*============================================================================*/
25 /*--------------------------------------------------------------------------*/
26 /**
27  * @brief   CicoHSControlBarWindow::CicoHSControlBarWindow
28  *          Constractor
29  *
30  * @param[in]   none
31  * @return      none
32  */
33 /*--------------------------------------------------------------------------*/
34 CicoHSControlBarWindow::CicoHSControlBarWindow(void)
35 {
36     evas = NULL;
37
38     CicoResourceConfig::GetImagePath(img_dir_path, ICO_HS_MAX_PATH_BUFF_LEN);
39
40     CicoGKeyFileConfig config;
41     config.Initialize(ICO_HOMESCREEN_CONFIG_FILE);
42     const char *value = config.ConfigGetString("switchzone", "keyname", "m");
43     if (strlen(value) > (sizeof(changeZoneKeyName) - 1)) {
44         ICO_WRN("[switchzone] keyname is strlen overflow. use default keyname(m)");
45     }
46     else {
47         memset(changeZoneKeyName, 0, sizeof(changeZoneKeyName));
48         strncpy(changeZoneKeyName, value, strlen(value));
49     }
50
51     value = config.ConfigGetString("standardswitch", "homekeyname", "h");
52     if (strlen(value) > (sizeof(homeKeyName) - 1)) {
53         ICO_WRN("[standardswitch] keyname is strlen overflow. use default keyname(h)");
54     }
55     else {
56         memset(homeKeyName, 0, sizeof(homeKeyName));
57         strncpy(homeKeyName, value, strlen(value));
58     }
59
60     value = config.ConfigGetString("standardswitch", "backkeyname", "b");
61     if (strlen(value) > (sizeof(backKeyName) - 1)) {
62         ICO_WRN("[standardswitch] keyname is strlen overflow. use default keyname(b)");
63     }
64     else {
65         memset(backKeyName, 0, sizeof(backKeyName));
66         strncpy(backKeyName, value, strlen(value));
67     }
68
69     value = config.ConfigGetString("standardswitch", "menukeyname", "l");
70     menuKeyName[0] = value[0];
71     if (strlen(value) > (sizeof(menuKeyName) - 1)) {
72         ICO_WRN("[standardswitch] keyname is strlen overflow. use default keyname(l)");
73     }
74     else {
75         memset(menuKeyName, 0, sizeof(menuKeyName));
76         strncpy(menuKeyName, value, strlen(value));
77     }
78     ICO_DBG("Assigned key config : changeZone[%s]", changeZoneKeyName);
79     ICO_DBG("Assigned key config : home[%s]", homeKeyName);
80     ICO_DBG("Assigned key config : back[%s]", backKeyName);
81     ICO_DBG("Assigned key config : menu[%s]", menuKeyName);
82
83     char tmp_str[16];
84     for (unsigned int ii = 0; ii < ICO_HS_CONTROL_BAR_SHORTCUT_MAX_NUM; ii++) {
85         sprintf(tmp_str, ICO_HS_CONTROL_BAR_CONFIG_SHORTCUT_APP"%d", ii);
86         value = config.ConfigGetString(ICO_HS_CONFIG_CONTROL_BAR, tmp_str, NULL);
87         if ((NULL != value) && (0 != strcmp(value, "none"))) {
88             shortcut_appid[ii] = value;
89             ICO_DBG("shortcut_appid[%d] = [%s]", ii, shortcut_appid[ii]);
90         }
91         else {
92             shortcut_appid[ii] = NULL;
93             ICO_DBG("shortcut_appid[%d] = NULL", ii);
94         }
95     }
96 }
97
98 /*--------------------------------------------------------------------------*/
99 /**
100  * @brief   CicoHSControlBarWindow::~CicoHSControlBarWindow
101  *          Destractor
102  *
103  * @param[in]   none
104  * @return      none
105  */
106 /*--------------------------------------------------------------------------*/
107 CicoHSControlBarWindow::~CicoHSControlBarWindow(void)
108 {
109     /* not somthing to do */
110 }
111
112 /*--------------------------------------------------------------------------*/
113 /**
114  * @brief   CicoHSControlBarWindow::CreateControlBarWindow
115  *          crate window (control bar)
116  *
117  * @param[in]   pos_x    position x
118  * @param[in]   pos_y    position y
119  * @param[in]   width    width
120  * @param[in]   height   height
121  * @return      OK or ERRROR
122  */
123 /*--------------------------------------------------------------------------*/
124 int 
125 CicoHSControlBarWindow::CreateControlBarWindow(int pos_x, int pos_y,
126                                                int width, int height)
127 {
128     int ret;
129     char img_path[ICO_HS_MAX_PATH_BUFF_LEN];
130     
131     /*create window*/
132     ret = CreateWindow(ICO_HS_CONTROL_BAR_WINDOW_TITLE,
133                        pos_x, pos_y, width, height, EINA_TRUE);
134     if(ret != ICO_OK){
135        return ret;
136     }
137        
138     /* get evas */
139     evas = ecore_evas_get(window);
140     if (!evas) {
141         ICO_ERR("ecore_evas_get failed.");
142         ICO_DBG("CicoHSControlBarWindow::CreateControlBarWindow Leave(ERROR)");
143         return ICO_ERROR;
144     }
145
146     // create background evas object
147     background = evas_object_rectangle_add(evas);
148
149     // add callback functions
150     evas_object_event_callback_add(background, EVAS_CALLBACK_KEY_DOWN, 
151                                    CicoHSControlBarWindow::evasKeyDownCB, this);
152
153     // key grab
154     evas_object_focus_set(background, EINA_FALSE);     
155     Eina_Bool eret = evas_object_key_grab(background, (const char*)changeZoneKeyName,
156                                           0, 0, EINA_TRUE);
157     if (EINA_FALSE == eret) {
158         ICO_WRN("evas_object_key_grab failed.");
159     }
160     eret = evas_object_key_grab(background, (const char*)homeKeyName,
161                                 0, 0, EINA_TRUE);
162     if (EINA_FALSE == eret) {
163         ICO_WRN("evas_object_key_grab failed.");
164     }
165     eret = evas_object_key_grab(background, (const char*)backKeyName,
166                                 0, 0, EINA_TRUE);
167     if (EINA_FALSE == eret) {
168         ICO_WRN("evas_object_key_grab failed.");
169     }
170     eret = evas_object_key_grab(background, (const char*)menuKeyName,
171                                 0, 0, EINA_TRUE);
172     if (EINA_FALSE == eret) {
173         ICO_WRN("evas_object_key_grab failed.");
174     }
175
176     // set background coloer
177     evas_object_color_set(background,128,128,128,255);
178
179     // change window geometry
180     evas_object_move(background, 0, 0);
181     evas_object_resize(background, width,height);
182
183     // show window
184     evas_object_show(background);
185
186     // home button
187     // image file name
188     snprintf(img_path, sizeof(img_path), "%s%s",
189              img_dir_path, ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_DAY);
190
191     // set object
192     menu_btn = evas_object_image_filled_add(evas);
193
194     // preload image
195     snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
196              ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_DAY2);
197     evas_object_image_file_set(menu_btn, img_path, NULL);
198
199     snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
200              ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_NIHGT);
201     evas_object_image_file_set(menu_btn, img_path, NULL);
202
203     snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
204              ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_NIHGT2);
205     evas_object_image_file_set(menu_btn, img_path, NULL);
206
207     // load fisrt show icon image 
208     snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
209              ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_DAY);
210     evas_object_image_file_set(menu_btn, img_path, NULL);
211
212     evas_object_move(menu_btn,
213                      (width/2) - (ICO_HS_CONTROL_BAR_MENU_BTN_WIDTH /2),
214                      ICO_HS_CONTROL_BAR_MENU_BTN_START_POS_Y);
215     evas_object_resize(menu_btn, ICO_HS_CONTROL_BAR_MENU_BTN_WIDTH, 
216                        ICO_HS_CONTROL_BAR_MENU_BTN_HEIGHT);
217     evas_object_event_callback_add(menu_btn, EVAS_CALLBACK_MOUSE_DOWN,
218                                    CicoHSControlBarTouch::TouchDownControlBar,
219                                    NULL);
220     evas_object_event_callback_add(menu_btn, EVAS_CALLBACK_MOUSE_UP,
221                                    CicoHSControlBarTouch::TouchUpControlBar,
222                                    NULL);
223     evas_object_show(menu_btn);    
224
225     /* shortcut */
226     AddShortcut(evas, width);
227  
228     return ICO_OK;
229 }
230
231 /*--------------------------------------------------------------------------*/
232 /**
233  * @brief   CicoHSControlBarWindow::AddShortcut
234  *          shortcut addition (control bar)
235  *
236  * @param[in]   evas     Evas Object
237  * @param[in]   width    width
238  * @return      none
239  */
240 /*--------------------------------------------------------------------------*/
241 void
242 CicoHSControlBarWindow::AddShortcut(Evas *evas, int width)
243 {
244     ICO_TRA("CicoHSControlBarWindow::AddShortcut Enter");
245
246     int escPosX;
247     int x;
248     int tmp_space;
249     int s_cnt = 0;
250     const char *tmp_appid;
251     CicoHSLifeCycleController *life_cycle_controller;
252
253     /* menu button x position */
254     escPosX = (width / 2) - (ICO_HS_CONTROL_BAR_MENU_BTN_WIDTH / 2);
255
256     life_cycle_controller = CicoHSLifeCycleController::getInstance();
257
258     tmp_space = (width - (ICO_HS_CONTROL_BAR_MENU_BTN_WIDTH
259                 * ICO_HS_CONTROL_BAR_BTN_MAX_NUM))
260                 / (ICO_HS_CONTROL_BAR_BTN_MAX_NUM + 1);
261     ICO_DBG("CicoHSControlBarWindow::AddShortcut tmp_space = [%d]",
262             tmp_space);
263
264     for (unsigned ii = 0; ii < ICO_HS_CONTROL_BAR_BTN_MAX_NUM; ii++) {
265         x = (ICO_HS_CONTROL_BAR_SHORTCUT_BTN_WIDTH * ii) +
266             (tmp_space * (ii + 1));
267
268         if ((x <= escPosX) &&
269            ((x + ICO_HS_CONTROL_BAR_SHORTCUT_BTN_WIDTH) >= escPosX)) {
270             /* The position of a menu button is skipped */
271             continue;
272         }
273         ICO_DBG("CicoHSControlBarWindow::AddShortcut x = [%d]", x);
274
275         tmp_appid = (const char *)(shortcut_appid[s_cnt]);
276         if (tmp_appid == NULL) {
277             /* No shortcut appid */
278             s_cnt++;
279             continue;
280         }
281
282         /* get APP information */
283         std::vector<CicoAilItems> aillist =
284         life_cycle_controller->getAilList();
285
286         /* add shortcut object */
287         for (unsigned int kk = 0; kk < aillist.size(); kk++ ) {
288             if (strncmp(aillist[kk].m_appid.c_str(), tmp_appid,
289                 ICO_HS_MAX_PROCESS_NAME) == 0) {
290                 ICO_DBG("CicoHSControlBarWindow::AddShortcut tmp_appid = [%s]",
291                         tmp_appid);
292                 shortcut[s_cnt] = evas_object_image_filled_add(evas);
293                 evas_object_image_file_set(shortcut[s_cnt],
294                                   aillist[kk].m_icon.c_str(), NULL);
295                 evas_object_move(shortcut[s_cnt], x,
296                                   ICO_HS_CONTROL_BAR_SHORTCUT_BTN_START_POS_Y);
297                 evas_object_resize(shortcut[s_cnt],
298                                   ICO_HS_CONTROL_BAR_SHORTCUT_BTN_WIDTH,
299                                   ICO_HS_CONTROL_BAR_SHORTCUT_BTN_HEIGHT);
300                 evas_object_event_callback_add(shortcut[s_cnt],
301                                   EVAS_CALLBACK_MOUSE_UP,
302                                   CicoHSControlBarTouch::TouchUpControlBar,
303                                   tmp_appid);
304                 evas_object_show(shortcut[s_cnt]);
305             }
306         }
307         s_cnt++;
308     }
309
310     ICO_TRA("CicoHSControlBarWindow::AddShortcut Leave");
311     return;
312 }
313
314 /*--------------------------------------------------------------------------*/
315 /**
316  * @brief   CicoHSControlBarWindow::FreeControlBarWindow
317  *          free window (control bar)
318  *
319  * @param[in]   none
320  * @return      none
321  */
322 /*--------------------------------------------------------------------------*/
323 void
324 CicoHSControlBarWindow::FreeControlBarWindow(void)
325 {
326     evas_object_del(background);
327     evas_object_del(menu_btn);
328     FreeWindow();
329 }
330
331 /*--------------------------------------------------------------------------*/
332 /**
333  * @brief   CicoHSControlBarWindow::TouchHome
334  *          touch home button
335  *
336  * @param[in]   none
337  * @return      none
338  */
339 /*--------------------------------------------------------------------------*/
340 void 
341 CicoHSControlBarWindow::TouchHome(void)
342 {
343     ActivationUpdate();
344     if (true == CicoHSSystemState::getInstance()->getRegulation()) {
345         CicoSound::GetInstance()->PlayFailureSound();
346     }
347     else {
348         CicoSound::GetInstance()->PlayOperationSound();
349     }
350         
351     CicoHomeScreen::ChangeMode(ICO_HS_SHOW_HIDE_PATTERN_SLIDE);
352 }
353
354 /*--------------------------------------------------------------------------*/
355 /**
356  * @brief   CicoHSControlBarWindow::SetNightMode
357  *          set night mode color theme chagne
358  *
359  * @param   none
360  * @return  none
361  */
362 /*--------------------------------------------------------------------------*/
363 void
364 CicoHSControlBarWindow::SetNightMode(void)
365 {
366     ICO_TRA("CicoHSControlBarWindow::SetNightMode Enter");
367
368     bool state = CicoHSSystemState::getInstance()->getNightMode();
369
370     char img_path[ICO_HS_MAX_PATH_BUFF_LEN];
371     if (true == state) {
372         ICO_DBG("ICO_SYC_STATE_ON");
373         evas_object_color_set(background,0,0,0,255);
374         if (true == CicoHSSystemState::getInstance()->getRegulation()) {
375             ICO_DBG("Regulation=ON");
376             snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
377                      ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_NIHGT2);
378         }
379         else {
380             ICO_DBG("Regulation=OFF");
381             snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
382                      ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_NIHGT);
383         }
384         evas_object_image_file_set(menu_btn, img_path, NULL);
385     }
386     else {
387         ICO_DBG("ICO_SYC_STATE_OFF");
388         evas_object_color_set(background,128,128,128,255);
389         if (true == CicoHSSystemState::getInstance()->getRegulation()) {
390             ICO_DBG("Regulation=ON");
391             snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
392                      ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_DAY2);
393         }
394         else {
395             ICO_DBG("Regulation=OFF");
396             snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
397                      ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_DAY);
398         }
399         evas_object_image_file_set(menu_btn, img_path, NULL);
400     }
401
402     ICO_TRA("CicoHSControlBarWindow::SetNightMode Leave");
403 }
404
405 /*--------------------------------------------------------------------------*/
406 /**
407  * @brief   CicoHSControlBarWindow::SetRegulation
408  *          set regulation color theme chagne
409  *
410  * @param   none
411  * @return  none
412  */
413 /*--------------------------------------------------------------------------*/
414 void
415 CicoHSControlBarWindow::SetRegulation(void)
416 {
417     ICO_TRA("CicoHSControlBarWindow::SetRegulation Enter");
418
419     char img_path[ICO_HS_MAX_PATH_BUFF_LEN];
420     if (true == CicoHSSystemState::getInstance()->getNightMode()) {
421         ICO_DBG("NightMode=ON");
422         if (true == CicoHSSystemState::getInstance()->getRegulation()) {
423             ICO_DBG("Regulation=ON");
424             snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
425                      ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_NIHGT2);
426         }
427         else {
428             ICO_DBG("Regulation=OFF");
429             snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
430                      ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_NIHGT);
431         }
432     }
433     else {
434         ICO_DBG("NightMode=OFF");
435         if (true == CicoHSSystemState::getInstance()->getRegulation()) {
436             ICO_DBG("Regulation=ON");
437             snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
438                      ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_DAY2);
439         }
440         else {
441             ICO_DBG("Regulation=OFF");
442             snprintf(img_path,sizeof(img_path),"%s%s",img_dir_path,
443                      ICO_HS_IMAGE_FILE_CONTROL_BAR_BUTTON_DAY);
444         }
445     }
446     evas_object_image_file_set(menu_btn, img_path, NULL);
447
448     ICO_TRA("CicoHSControlBarWindow::SetRegulation Leave");
449 }
450
451 /*--------------------------------------------------------------------------*/
452 /**
453  * @brief   CicoHSControlBarWindow::SetWindowID
454  *          set appid and surface
455  *
456  * @param[in]   none
457  * @return      none
458  */
459 /*--------------------------------------------------------------------------*/
460 void
461 CicoHSControlBarWindow::SetWindowID(const char *appid,int surface)
462 {
463     strncpy(this->appid,appid,ICO_HS_MAX_PROCESS_NAME);
464     this->surface = surface;
465 }
466
467 /*--------------------------------------------------------------------------*/
468 /**
469  * @brief   CicoHSControlBarWindow::GetSurfaceId
470  *          get surface id of control bar window
471  *
472  * @return  surface id
473  */
474 /*--------------------------------------------------------------------------*/
475 int
476 CicoHSControlBarWindow::GetSurfaceId(void)
477 {
478     return this->surface;
479 }
480
481 /*--------------------------------------------------------------------------*/
482 /**
483  * @brief   CicoHSControlBarWindow::GetAppId
484  *          get application id of control bar
485  *
486  * @return  application id
487  */
488 /*--------------------------------------------------------------------------*/
489 const char *
490 CicoHSControlBarWindow::GetAppId(void)
491 {
492     return this->appid;
493 }
494
495 /*--------------------------------------------------------------------------*/
496 /**
497  * @brief   CicoHSControlBarWindow::TouchShortcut
498  *          touch shortcut button
499  *
500  * @param[in]   none
501  * @return      none
502  */
503 /*--------------------------------------------------------------------------*/
504 void
505 CicoHSControlBarWindow::TouchShortcut(const char *appid)
506 {
507     ICO_TRA("CicoHSControlBarWindow::TouchShortcut Enter");
508     ActivationUpdate();
509
510     CicoSound::GetInstance()->PlayOperationSound();
511
512     if (appid != NULL) {
513         ICO_DBG("CicoHSControlBarWindow::TouchShortcut appid = [%s]", appid);
514         CicoHomeScreen::ExecuteApp(appid);
515     }
516
517     ICO_TRA("CicoHSControlBarWindow::TouchShortcut Leave");
518 }
519
520 //--------------------------------------------------------------------------
521 /**
522  *  @brief  key up event callback function
523  *
524  *  @pamam [in] data    user data
525  *  @param [in] evas    evas instcance
526  *  @param [in] obj     evas object instcance
527  *  @param [in] info    event information(Evas_Event_Key_Down)
528  */
529 //--------------------------------------------------------------------------
530 void
531 CicoHSControlBarWindow::onKeyDown(void *data, Evas *evas,
532                                   Evas_Object *obj, void *info)
533 {
534     Evas_Event_Key_Down *evinfo = NULL;
535     evinfo = (Evas_Event_Key_Down*)info;
536
537     CicoSound::GetInstance()->PlayOperationSound();
538
539     ICO_PRF("TOUCH_EVENT Key Down keyname=%s, key=%d",
540             evinfo->keyname, (char)*evinfo->key);
541
542     if (0 == strcmp(evinfo->keyname, changeZoneKeyName)) {
543         CicoHomeScreen::ChangeZone();
544     }
545     else if (0 == strcmp(evinfo->keyname, homeKeyName)) {
546         TouchHome();
547     }
548     else if (0 == strcmp(evinfo->keyname, backKeyName)) {
549         // TODO not assinded funciton 
550     }
551     else if (0 == strcmp(evinfo->keyname, menuKeyName)) {
552         // TODO not assinded funciton 
553     }
554 }
555
556 //--------------------------------------------------------------------------
557 /**
558  *  @brief  key up event callback function
559  *
560  *  @pamam [in] data    user data
561  *  @param [in] evas    evas instcance
562  *  @param [in] obj     evas object instcance
563  *  @param [in] info    event information(Evas_Event_Key_Down)
564  */
565 //--------------------------------------------------------------------------
566 void
567 CicoHSControlBarWindow::evasKeyDownCB(void *data, Evas *evas,
568                                       Evas_Object *obj, void *info)
569 {
570     CicoHSControlBarWindow *ctrlbarwin = (CicoHSControlBarWindow*)(data);
571     ctrlbarwin->onKeyDown(data, evas, obj, info);
572 }
573
574 //--------------------------------------------------------------------------
575 /**
576  *  @brief  Activation update swipe app
577  *
578  *  @return bool
579  *  @retval true update
580  *  @retval false no update
581  */
582 //--------------------------------------------------------------------------
583 bool
584 CicoHSControlBarWindow::ActivationUpdate(void)
585 {
586     return CicoHomeScreen::ActivationUpdate();
587 }
588 // vim: set expandtab ts=4 sw=4: