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