f8d69160d874c750b71c324b5cfb7d68b8d6218b
[framework/osp/appwidget-service.git] / src / FShell_LiveboxBase.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // All rights reserved.
5 //
6 // This software contains confidential and proprietary information
7 // of Samsung Electronics Co., Ltd.
8 // The user of this software agrees not to disclose, disseminate or copy such
9 // Confidential Information and shall use the software only in accordance with
10 // the terms of the license agreement the user entered into with Samsung.
11 //
12
13 /**
14  * @file        FShell_LiveboxBase.cpp
15  * @brief       This is the implementation for the Livebox class.
16  */
17
18 #include <stdlib.h>
19 #include <unique_ptr.h>
20
21 #include "provider_buffer.h"
22
23 #include <FBase.h>
24 #include <FBaseSysLog.h>
25 #include <FBase_StringConverter.h>
26
27 #include <FApp_AppMessageImpl.h>
28 #include <FApp_AppArg.h>
29 #include <FApp_AppControlManager.h>
30 #include <FShell_TemplateUtil.h>
31 #include <FShell_LiveboxManagerImpl.h>
32
33 #include "FShell_LiveboxBase.h"
34
35 // provider/src/fb.c
36 struct fb_info {
37         char *id;
38         int w;
39         int h;
40         int bufsz;
41         void *buffer;
42
43         int handle;
44 };
45
46 // provider/inc/provider_buffer_internal.h
47 struct livebox_buffer {
48         enum {
49                 BUFFER_CREATED = 0x00beef00,
50                 BUFFER_DESTROYED = 0x00dead00,
51         } state;
52
53         enum target_type type;
54
55         union {
56                 int fd; /* File handle(descriptor) */
57                 int id; /* SHM handle(id) */
58         } handle;
59
60         char *pkgname;
61         char *id;
62         int width;
63         int height;
64         int pixel_size;
65
66         struct fb_info *fb;
67
68         int (*handler)(struct livebox_buffer *info, enum buffer_event event, double timestamp, double x, double y, void *data);
69         void *data;
70 };
71
72 static int LiveboxHandleBufferEventCallback( struct livebox_buffer *info, enum buffer_event event,
73                 double timestamp, double x, double y, void* data);
74
75 namespace Tizen { namespace Shell  { namespace App
76 {
77
78 using namespace Tizen::App;
79 using namespace Tizen::Base;
80 using namespace Tizen::Base::Collection;
81
82
83 const String LIVEBOX_ON_ADD(L"Livebox='event=add'");
84 const String LIVEBOX_ON_REMOVE(L"Livebox='event=remove'");
85 const String LIVEBOX_ON_UPDATE(L"Livebox='event=update'");
86 const String LIVEBOX_ON_RESIZE(L"Livebox='event=resize'");
87 const String LIVEBOX_ON_TOUCH(L"Livebox='event=touch'");
88 const String LIVEBOX_TRAY_ON_CREATE(L"LiveboxTray='event=create'");
89 const String LIVEBOX_TRAY_ON_DESTROY(L"LiveboxTray='event=destroy'");
90 const String LIVEBOX_TRAY_ON_TOUCH(L"LiveboxTray='event=touch'");
91
92 const String ARG_KEY_INSTANCE_ID = L"_InstanceId";
93 const String ARG_KEY_PROVIDER_NAME = L"_ProviderName";
94 const String ARG_KEY_USER_INFO = L"_UserInfo";
95
96
97 _LiveboxBase::_LiveboxBase(target_type type, const String& userInfo, const String& providerId, const String& instanceId, int width, int height, int priority)
98         :__type(type)
99         ,__userInfo(userInfo)
100         ,__providerId(providerId)
101         ,__instanceId(instanceId)
102         ,__width(width)
103         ,__height(height)
104         ,__priority(priority)
105         ,__buffer_info(null)
106         ,__buffer(null)  // __pEcoreEvas(null), __pEvas(null), __current(0)
107         ,__isForeground(true)
108         ,__ipcClientId(-1)
109 {
110         _LiveboxManagerImpl::ExtractAppIdAndProviderName(providerId, __appId, __providerName);
111
112         SysLog(NID_APP, "appId(%ls), providerId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __appId.GetPointer(), __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
113 }
114
115 _LiveboxBase::~_LiveboxBase()
116 {
117         SysLog(NID_APP, "providerId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
118 }
119
120
121 void
122 _LiveboxBase::SetClientId(int clientId)
123 {
124         __ipcClientId = clientId;
125 }
126
127 bool
128 _LiveboxBase::HasValidClientId() const
129 {
130         SysLog(NID_APP, "%d", __ipcClientId);
131         return (__ipcClientId > -1);
132 }
133
134 int
135 _LiveboxBase::GetSharedMemId(int w, int h)
136 {
137         SysLog(NID_APP, "Enter");
138
139         if( __buffer_info == null)
140         {
141                 std::unique_ptr<char[]> packageName(_StringConverter::CopyToCharArrayN(__providerId));
142                 std::unique_ptr<char[]> id(_StringConverter::CopyToCharArrayN(__instanceId));
143
144                 __buffer_info = provider_buffer_acquire(__type, packageName.get(), id.get(), w, h, sizeof(int), LiveboxHandleBufferEventCallback, this);
145                 SysTryReturnResult(NID_APP, __buffer_info , -1, "[E_SYSTEM] failed to provider_buffer_acquire");
146                 SysLog(NID_APP, "provider_buffer_acquire successed");
147
148                 __buffer = provider_buffer_ref(__buffer_info);
149                 SysTryReturnResult(NID_APP, __buffer , -1, "[E_SYSTEM] failed to provider_buffer_ref");
150                 SysLog(NID_APP, "provider_buffer_ref successed");
151         }
152
153     int bufferId = __buffer_info->fb->handle;
154
155     SysLog(NID_APP, "(%d) Exit", bufferId);
156     return bufferId;
157 }
158
159 result
160 _LiveboxBase::ReleaseSharedMem()
161 {
162         SysLog(NID_APP, "Enter");
163     int ret;
164
165     if( __buffer)
166     {
167         ret = provider_buffer_unref(__buffer);
168         __buffer = null;
169         SysTryReturnResult(NID_APP, ret >= 0 , E_SYSTEM, "[E_SYSTEM] failed to provider_buffer_unref");
170         SysLog(NID_APP, "provider_buffer_unref successed");
171     }
172
173     if( __buffer_info)
174     {
175                 ret = provider_buffer_release(__buffer_info);
176                 __buffer_info = null;
177                 SysTryReturnResult(NID_APP, ret >= 0 , E_SYSTEM, "[E_SYSTEM] failed to provider_buffer_release");
178                 SysLog(NID_APP, "provider_buffer_release successed");
179     }
180
181     SysLog(NID_APP, "Exit.");
182
183     return E_SUCCESS;
184 }
185
186 Tizen::Base::Collection::HashMap*
187 _LiveboxBase::CreateRequestArgs(void)
188 {
189         HashMap* pArgs = new (std::nothrow) HashMap(SingleObjectDeleter);
190         pArgs->Construct();
191         pArgs->Add(new String(ARG_KEY_INSTANCE_ID), new String(__instanceId));
192         pArgs->Add(new String(ARG_KEY_PROVIDER_NAME), new String(__providerName));
193         pArgs->Add(new String(ARG_KEY_USER_INFO), new String(__userInfo));
194
195         return pArgs;
196 }
197
198 result
199 _LiveboxBase::SendRequestToApp(const AppId& appId, const String& operation, HashMap* pArgs)
200 {
201         if( __isForeground == false)
202         {
203                 SysLog(NID_APP, "livebox isn't foreground, so, message skip");
204                 return E_SUCCESS;
205         }
206
207         return _LiveboxRequestHelper::SendRequestToApp(appId, operation, pArgs);
208 }
209
210 result
211 _LiveboxRequestHelper::SendRequestToApp(const AppId& appId, const String& operation, HashMap* pArgs)
212 {
213         SysLog(NID_APP, "appId(%ls), operation(%ls), arg count(%d)", appId.GetPointer(), operation.GetPointer(), pArgs->GetCount() );
214
215 /*      std::unique_ptr<IMapEnumerator> pMapEnum(pArgs->GetMapEnumeratorN());
216         SysTryReturnResult(NID_APP, pMapEnum.get(), E_SYSTEM, "failed to get GetMapEnumeratorN");
217
218         while( pMapEnum->MoveNext() == E_SUCCESS)
219         {
220                 const String* pKey = dynamic_cast<const String*>(pMapEnum->GetKey());
221                 const String* pValue = dynamic_cast<const String*>(pMapEnum->GetValue());
222
223                 if( pKey == null)
224                 {
225                         SysLog(NID_APP, "key is null");
226                         continue;
227                 }
228                 if( pValue == null)
229                 {
230                         SysLog(NID_APP, "pValue is null");
231                         continue;
232                 }
233
234                 SysLog(NID_APP, "key(%ls), value(%ls)", pKey->GetPointer(), pValue->GetPointer() );
235
236         }*/
237
238         _AppMessageImpl msg;
239
240         msg.AddData(OSP_K_APPCONTROL_INTERNAL_OPERATION, L"livebox");
241         //      AddStrArrayToBundle(msg.GetBundle(), OSP_K_ARG, pArgs);
242         Tizen::App::_AppArg::AddStrMap(msg.GetBundle(), pArgs);
243         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId) );
244         std::unique_ptr<char[]> pOperation(_StringConverter::CopyToCharArrayN(operation) );
245
246         return Tizen::App::_AppControlManager::GetInstance()->LaunchPkg(msg, pAppId.get(), pOperation.get(), null, null, null, null);
247 }
248
249 /*result
250 _LiveboxRequestHelper::ExtractPackageIdAndExecutableName(AppId appId, AppId& outPackageId, String& outExecutableName)
251 {
252         const int APP_ID_LEN = 10;
253
254         if (appId.GetLength() > APP_ID_LEN)
255         {
256                 result r = appId.SubString(APP_ID_LEN + 1, appId.GetLength() - (APP_ID_LEN + 1), outExecutableName);
257                 SysTryReturnResult(NID_APP, !IsFailed(r), E_INVALID_ARG, "invalid AppId(%ls)", appId.GetPointer());
258
259                 r = appId.SubString(0, APP_ID_LEN, outPackageId);
260                 SysTryReturnResult(NID_APP, !IsFailed(r), E_INVALID_ARG, "invalid AppId(%ls)", appId.GetPointer());
261
262                 return E_SUCCESS;
263         }
264         return E_SYSTEM;
265 }*/
266
267 /*
268 // helper for bundle
269 result
270 _LiveboxRequestHelper::AddStrArrayToBundle(bundle* b, const char* key, const IList* pList)
271 {
272         bundle* pb = b;
273         SysTryReturnResult(NID_APP, pb != NULL, E_INVALID_ARG, "Empty bundle.");
274
275         if (pList == null || pList->GetCount() == 0)
276         {
277                 SysLog(NID_APP, "No element added for bundle.");
278                 return E_SUCCESS;
279         }
280
281         int i = 0;
282         const int count = pList->GetCount();
283
284         const char** pSa = new (std::nothrow) const char*[count];
285         SysTryReturnResult(NID_APP, pSa != null, E_OUT_OF_MEMORY, "Memory allocation failure with cound %d.", count);
286
287         for (i = 0; i < count; i++)
288         {
289                 pSa[i] = null;
290
291                 const String* pStr = static_cast<const String*>(pList->GetAt(i));
292                 if (pStr)
293                 {
294                         pSa[i] = _StringConverter::CopyToCharArrayN(*pStr);
295                 }
296         }
297
298         result r = E_SUCCESS;
299
300         int ret = bundle_add_str_array(pb, key, pSa, count);
301         SysTryReturnResult(NID_APP, ret >= 0, E_SYSTEM, "Bundle add failre with %d.", strerror(errno));
302
303 //CATCH:
304         for (i = 0; i < count; i++)
305         {
306                 delete[] pSa[i];
307         }
308
309         delete[] pSa;
310
311         return r;
312 }
313 */
314
315 } } } // Tizen::Shell::App {
316
317
318 ////////////////////////////////////////////
319 // callback
320 ////////////////////////////////////////////
321 static int LiveboxHandleBufferEventCallback( struct livebox_buffer *info, enum buffer_event event,
322                 double timestamp, double x, double y, void* data)
323 {
324     SysLog(NID_APP, "timestamp(%f), x(%f), y(%f)", timestamp, x, y);
325
326     Tizen::Shell::App::_LiveboxBase *pLiveboxBase = static_cast<Tizen::Shell::App::_LiveboxBase*>(data);
327     SysTryReturn(NID_APP, pLiveboxBase != null, 0, E_SYSTEM, "[E_SYSTEM] retrieved pLiveboxBase is null");
328
329 //    const char *pkgname = provider_buffer_pkgname(info);
330 //    const char *id = provider_buffer_id(info);
331 //    enum target_type type = provider_buffer_type(info);
332
333     if( event ==  BUFFER_EVENT_ENTER)
334     {
335         SysLog(NID_APP, "BUFFER_EVENT_ENTER");
336     }
337     else if(   event ==  BUFFER_EVENT_LEAVE)
338     {
339         SysLog(NID_APP, "BUFFER_EVENT_LEAVE");
340     }
341     else if(   event ==  BUFFER_EVENT_DOWN)
342         {
343                 SysLog(NID_APP, "BUFFER_EVENT_DOWN");
344         }
345     else if(   event ==  BUFFER_EVENT_MOVE)
346     {
347                 SysLog(NID_APP, "BUFFER_EVENT_MOVE");
348         }
349     else if(   event ==  BUFFER_EVENT_UP)
350         {
351                 SysLog(NID_APP, "BUFFER_EVENT_UP");
352         }
353
354     pLiveboxBase->SendTouchEvent(event, timestamp, x, y);
355
356     return 0;
357 }