bug fix: HomeScreen sometimes fails in connection with SystemController at the time...
[profile/ivi/ico-uxf-homescreen.git] / src / onscreen / CicoOnScreen.cpp
1 /*
2  * Copyright (c) 2014, 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   On Screen
11  *
12  * @date    Jan-07-2014
13  */
14 #include "CicoOnScreen.h"
15 #include "CicoOSPopWindow.h"
16 #include <Ecore.h>
17 #include <Ecore_Wayland.h>
18
19 using namespace std;
20
21 //==========================================================================
22 // static members
23 //==========================================================================
24 CicoOnScreen * CicoOnScreen::os_instance=NULL;
25
26 //==========================================================================
27 // functions
28 //==========================================================================
29
30 //--------------------------------------------------------------------------
31 /**
32  * @brief   CicoOnScreen::CicoOnScreen
33  *          Constractor
34  *
35  * @param[in]   none
36  * @return      none
37  */
38 //--------------------------------------------------------------------------
39 CicoOnScreen::CicoOnScreen(void)
40 {
41     ICO_TRA("CicoOnScreen::CicoOnScreen Enter");
42     m_request = NULL;
43     m_del = false;
44     m_reserve = NULL;
45     ICO_TRA("CicoOnScreen::CicoOnScreen Leave");
46 }
47
48 //--------------------------------------------------------------------------
49 /**
50  * @brief   CicoOnScreen::~CicoOnScreen
51  *          Destractor
52  *
53  * @param[in]   none
54  * @return      none
55  */
56 //--------------------------------------------------------------------------
57 CicoOnScreen::~CicoOnScreen(void)
58 {
59 //    ICO_TRA("CicoOnScreen::~CicoOnScreen Enter");
60     list<CicoOSPopWindow*>::iterator p = m_mngWin.begin();
61     while (p != m_mngWin.end()) {
62         CicoOSPopWindow* pt = *p;
63         delete pt;
64     }
65     m_mngWin.clear();
66     p = m_waitMngWin.begin();
67     while (p != m_waitMngWin.end()) {
68         CicoOSPopWindow* pt = *p;
69         delete pt;
70     }
71     m_waitMngWin.clear();
72
73     if (NULL != m_request) {
74         delete m_request;
75         m_request = NULL;
76     }
77 //    ICO_TRA("CicoOnScreen::~CicoOnScreen Leave");
78 }
79
80 //--------------------------------------------------------------------------
81 /**
82  * @brief   CicoOnScreen::StartOnScreen
83  *          Start on screen
84  *
85  * @param[in]   none
86  * @return      none
87  * @return true on success, false on error
88  */
89 //--------------------------------------------------------------------------
90 bool
91 CicoOnScreen::StartOnScreen(void)
92 {
93     ICO_TRA("Enter");
94
95     ico_syc_connect(EventCallBack, NULL);
96
97     // save instance pointer
98     os_instance = this;
99
100     // Initialize
101     ecore_evas_init();
102
103     if (NULL == m_reserve) {
104         m_reserve = new CicoOSPopWindow(NOTIFICATION_TYPE_NONE);
105         m_reserve->createMainWindow();
106     }
107
108     // set notification callback function
109     notiservice_.SetCallback(NotificationCallback, this);
110
111     ICO_TRA("Leave(true)");
112     return true;
113 }
114
115 bool CicoOnScreen::insertNoti(notification_h noti_h)
116 {
117     ICO_TRA("Enter");
118     CicoOSPopWindow* w = new CicoOSPopWindow(noti_h);
119     if (NULL == w) {
120         ICO_ERR("FAIL new class");
121         ICO_TRA("Leave (false)");
122         return false;
123     }
124     if (!w->Empty()) {
125         if (NOTIFICATION_TYPE_NOTI == w->GetType()) {
126             m_waitMngWin.push_back(w);
127             w = NULL;
128         }
129     }
130     if (NULL != w) {
131         delete w;
132         w = NULL;
133         ICO_TRA("Leave (false)");
134         return false;
135     }
136     ICO_TRA("Leave (true)");
137     return true;
138 }
139 bool CicoOnScreen::deleteNoti(int priv_id)
140 {
141     ICO_TRA("Enter");
142     list<CicoOSPopWindow*>::iterator p = m_mngWin.begin();
143     for (; p != m_mngWin.end(); ++p) {
144         CicoOSPopWindow* w = *p;
145         if (priv_id == w->GetPrivId()) {
146             w->hidePopup();
147         }
148     }
149     list<CicoOSPopWindow*> tmp;
150     p = m_waitMngWin.begin();
151     for (; p != m_waitMngWin.end(); ++p) {
152         CicoOSPopWindow* w = *p;
153         if (priv_id == w->GetPrivId()) {
154             tmp.push_back(w);
155         }
156     }
157     p = tmp.begin();
158     for (; p != tmp.end(); ++p) {
159         CicoOSPopWindow* w = *p;
160         m_waitMngWin.remove(w);
161         delete w;
162     }
163     if (NULL != m_request) {
164         if (priv_id == m_request->GetPrivId()) {
165             m_del = true;
166         }
167     }
168     ICO_TRA("Leave (true)");
169     return true;
170 }
171
172 //--------------------------------------------------------------------------
173 /**
174  * @brief   CicoOnScreen::NotificationCallback
175  *          Notification callback function
176  *
177  * @param[in]  data        The user data passed from the callback
178  *                         registration function
179  * @param[in]  type        The type of notification
180  * @return     none
181  */
182 //--------------------------------------------------------------------------
183 void
184 CicoOnScreen::NotificationCallback(void *data, notification_type_e type,
185                                    notification_op *op_list, int num_op)
186 {
187     ICO_TRA("Enter");
188     bool bInsFlag = false;
189     for (int i = 0; i < num_op; i++) {
190         notification_op_type_e op_type = op_list[i].type;
191         int priv_id = op_list[i].priv_id;
192         notification_h noti_h = op_list[i].noti;
193         ICO_DBG("RECV notification op_type=%d priv_id=%d noti=0x%08x",
194                 op_type, priv_id, noti_h);
195
196         switch (op_type) {
197         case NOTIFICATION_OP_INSERT :
198         {
199             ICO_DBG("NOTIFICATION_OP_INSERT");
200             if (true == os_instance->insertNoti(noti_h)) {
201                 bInsFlag = true;
202             }
203             break;  // break of switch op_type
204         }
205         case NOTIFICATION_OP_UPDATE :
206             ICO_DBG("NOTIFICATION_OP_UPDATE");
207             break;  // break of switch op_type
208         case NOTIFICATION_OP_DELETE:
209         {
210             ICO_DBG("NOTIFICATION_OP_DELETE");
211             os_instance->deleteNoti(priv_id);
212             break;  // break of switch op_type
213         }
214         case NOTIFICATION_OP_DELETE_ALL:
215             ICO_DBG("NOTIFICATION_OP_DELETE_ALL");
216             break;  // break of switch op_type
217         case NOTIFICATION_OP_REFRESH:
218             ICO_DBG("NOTIFICATION_OP_REFRESH");
219             break;  // break of switch op_type
220         case NOTIFICATION_OP_SERVICE_READY:
221             ICO_DBG("NOTIFICATION_OP_SERVICE_READY");
222             break;  // break of switch op_type
223         default :
224             ICO_DBG("UNKOWN OP_TYPE(%d)", (int)op_type);
225             break;  // break of switch op_type
226         }
227     }
228
229     if (true == bInsFlag) {
230         os_instance->requestShowSC();
231     }
232
233     ICO_TRA("Leave");
234 }
235
236 bool CicoOnScreen::requestShowSC()
237 {
238     ICO_TRA("Enter");
239     if (NULL != m_request) {
240         ICO_TRA("Leave false(request now)");
241         return false;
242     }
243     if (m_waitMngWin.empty()) {
244         ICO_TRA("Leave false(nothing window)");
245         return false;
246     }
247
248     list<CicoOSPopWindow*>::iterator p = m_waitMngWin.begin();
249     m_del = false;
250     m_request = *p;
251     m_waitMngWin.remove(m_request);
252     bool r = m_request->showPopup();
253     while (false == r) { // fail showPop request
254         ICO_ERR("_____ Fail SHOW POP REQUEST(%d)",
255                 m_request->GetPrivId());
256         delete m_request;
257         m_request = NULL;
258         if (m_waitMngWin.empty()) {
259             break;
260         }
261         p = m_waitMngWin.begin();
262         m_del = false;
263         m_request = *p;
264         m_waitMngWin.remove(m_request);
265         r = m_request->showPopup();
266     }
267     ICO_TRA("Leave %s", r? "true": "false");
268     return r;
269 }
270     
271 //--------------------------------------------------------------------------
272 /**
273  *  @brief   callback for system controller
274  *
275  *  @param [in] event       kind of event
276  *  @param [in] detail      detail
277  *  @param [in] user_data   user data
278  */
279 //--------------------------------------------------------------------------
280 void
281 CicoOnScreen::EventCallBack(const ico_syc_ev_e event,
282                             const void *detail, void *user_data)
283 {
284     ICO_TRA("Enter(event %d, %x, %x)", (int)event, detail, user_data);
285     ico_syc_res_info_t *ri = (ico_syc_res_info_t*) detail;
286     if (NULL == ri) {
287         ICO_ERR("____ CALLBACK NG PARAM");
288         return;
289     }
290     switch (event) {
291     case ICO_SYC_EV_RES_ACQUIRE:
292         ICO_TRA("_____ ICO_SYC_EV_RES_ACQUIRE");
293         break;  // break of switch event
294     case ICO_SYC_EV_RES_DEPRIVE:
295         ICO_TRA("_____ ICO_SYC_EV_RES_DEPRIVE");
296         break;  // break of switch event
297     case ICO_SYC_EV_RES_WAITING:
298         ICO_TRA("_____ ICO_SYC_EV_RES_WAITING");
299         break;  // break of switch event
300     case ICO_SYC_EV_RES_REVERT:
301         ICO_TRA("_____ ICO_SYC_EV_RES_REVERT");
302         break;  // break of switch event
303     case ICO_SYC_EV_RES_RELEASE:
304     {
305         ICO_TRA("_____ ICO_SYC_EV_RES_RELEASE");
306         if (NULL == ri->window) {
307             ICO_TRA("_____ no WINDOW");
308             break;  // break of switch event
309         }
310         os_instance->releaseWindow(ri->window->resourceId);
311         break;  // break of switch event
312     }
313     case ICO_SYC_EV_RES_WINDOW_ID:
314     {
315         ICO_TRA("_____ ICO_SYC_EV_RES_WINDOW_ID");
316         if (NULL == ri->window) {
317             ICO_TRA("_____ no WINDOW");
318             break;  // break of switch event
319         }
320         os_instance->entryWindowId(ri->window->resourceId);
321         break;  // break of switch event
322     }
323     default:
324         ICO_TRA("_____ UNKNOWN event(%d)", (int)event);
325         break;  // break of switch event
326     }
327
328     ICO_TRA("Leave");
329 }
330
331 bool
332 CicoOnScreen::releaseWindow(uint32_t resourceId)
333 {
334     ICO_TRA("Enter(%d)", resourceId);
335     bool bR = false;
336     list<CicoOSPopWindow*> tmp;
337
338     list<CicoOSPopWindow*>::iterator p = m_mngWin.begin();
339     for (; p != m_mngWin.end(); ++p) {
340         CicoOSPopWindow* w = *p;
341         if (resourceId == w->m_resourceId) {
342             tmp.push_back(w);
343         }
344     }
345     p = tmp.begin();
346     for (; p != tmp.end(); ++p) {
347         CicoOSPopWindow* w = *p;
348         m_mngWin.remove(w);
349         delete w;
350         bR = true;
351     }
352     tmp.clear();
353
354     ICO_TRA("Leave %s", bR? "true": "false");
355     return bR;
356 }
357
358 bool
359 CicoOnScreen::entryWindowId(uint32_t resourceId)
360 {
361     ICO_TRA("Enter(%d)", resourceId);
362     bool bR = false;
363     if (NULL != m_request) {
364         m_request->m_resourceId = resourceId;
365         if (true == m_request->acquireRes()) {
366             m_mngWin.push_back(m_request);
367             if (true == m_del) {
368                 ICO_TRA("____ HIDE REQUEST");
369                 m_request->hidePopup();
370             }
371         }
372         else {
373             delete m_request;
374         }
375         m_del = false;
376         m_request = NULL;
377         bR = requestShowSC();
378     }
379     ICO_TRA("Leave %s", bR? "true": "false");
380     return bR;
381 }
382 // vim: set expandtab ts=4 sw=4: