remove unnecessary codes
[framework/osp/appwidget-service.git] / src / FShell_AppWidgetContextBase.cpp
1 //
2 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (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
7 //
8 //     http://floralicense.org/license/
9 //
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.
15 //
16
17 /**
18  * @file        FShell_AppWidgetContextBase.cpp
19  * @brief       This is the implementation for the _AppWidgetContextBase class.
20  */
21
22 #include <stdlib.h>
23 #include <unique_ptr.h>
24 #include <provider.h>
25 #include <FBaseColHashMap.h>
26 #include <FBaseSysLog.h>
27 #include <FBase_StringConverter.h>
28 #include <FApp_AppMessageImpl.h>
29 #include <FApp_AppArg.h>
30 #include <FApp_AppControlManager.h>
31 #include "FShell_AppWidgetManagerImpl.h"
32 #include "FShell_AppWidgetContextBase.h"
33
34 // provider/src/fb.c
35 struct fb_info {
36         char *id;
37         int w;
38         int h;
39         int bufsz;
40         void *buffer;
41
42         int handle;
43 };
44
45 // provider/inc/provider_buffer_internal.h
46 struct livebox_buffer {
47         enum {
48                 BUFFER_CREATED = 0x00beef00,
49                 BUFFER_DESTROYED = 0x00dead00,
50         } state;
51
52         enum target_type type;
53
54         union {
55                 int fd; /* File handle(descriptor) */
56                 int id; /* SHM handle(id) */
57         } handle;
58
59         char *pkgname;
60         char *id;
61         int width;
62         int height;
63         int pixel_size;
64
65         struct fb_info *fb;
66
67         int (*handler)(struct livebox_buffer *info, enum buffer_event event, double timestamp, double x, double y, void *data);
68         void *data;
69 };
70
71 static int AppWidgetHandleBufferEventCallback(struct livebox_buffer *info, enum buffer_event event, double timestamp, double x, double y, void* data);
72
73 namespace Tizen { namespace Shell  { namespace App
74 {
75
76 using namespace Tizen::App;
77 using namespace Tizen::Base;
78 using namespace Tizen::Base::Collection;
79
80 const wchar_t ARG_KEY_INSTANCE_ID[] = L"_InstanceId";
81 const wchar_t ARG_KEY_PROVIDER_NAME[] = L"_ProviderName";
82 const wchar_t ARG_KEY_USER_INFO[] = L"_UserInfo";
83 const wchar_t ARG_KEY_X[] = L"_X";
84 const wchar_t ARG_KEY_Y[] = L"_Y";
85 const wchar_t ARG_KEY_WIDTH[] = L"_Width";
86 const wchar_t ARG_KEY_HEIGHT[] = L"_Height";
87 const wchar_t ARG_KEY_POPUP_WIDTH[] = L"_PopupWidth";
88 const wchar_t ARG_KEY_POPUP_HEIGHT[] = L"_PopupHeight";
89 const wchar_t ARG_KEY_ARGUMENT[] = L"_Argument";
90 const wchar_t ARG_KEY_EVENT_TYPE[] = L"_EventType";
91 const wchar_t ARG_KEY_TIME_STAMP[] = L"_TimeStamp";
92
93 _AppWidgetContextBase::_AppWidgetContextBase(target_type type, const String& userInfo, const String& providerId, const String& instanceId, int width, int height, int priority)
94         :__type(type)
95         ,__userInfo(userInfo)
96         ,__providerId(providerId)
97         ,__instanceId(instanceId)
98         ,__width(width)
99         ,__height(height)
100         ,__priority(priority)
101         ,__isForeground(true)
102         ,__ipcClientId(-1)
103         ,__buffer_info(null)
104         ,__buffer(null)
105         ,__providerState(INVALID)
106 {
107         _AppWidgetManagerImpl::ExtractAppIdAndProviderName(providerId, __appId, __providerName);
108
109         SysLog(NID_SHELL, "appId(%ls), providerId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __appId.GetPointer(), __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
110 }
111
112 _AppWidgetContextBase::~_AppWidgetContextBase(void)
113 {
114         SysLog(NID_SHELL, "providerId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
115         ReleaseSharedMem();
116 }
117
118 void
119 _AppWidgetContextBase::SetIpcClientId(int clientId)
120 {
121         __ipcClientId = clientId;
122 }
123
124 bool
125 _AppWidgetContextBase::HasValidClientId(void) const
126 {
127         SysLog(NID_SHELL, "%d", __ipcClientId);
128         return (__ipcClientId > -1);
129 }
130
131 bool
132 _AppWidgetContextBase::IsSharedMemCreated(void) const
133 {
134         return ( __buffer_info && __buffer);
135 }
136
137 int
138 _AppWidgetContextBase::GetSharedMemId(int w, int h)
139 {
140     SysLog(NID_SHELL, "Enter");
141
142     bool isResized = (__buffer_info != null) && (__buffer_info->width != w || __buffer_info->height != h);
143     if ( isResized )
144     {
145         ReleaseSharedMem();
146     }
147
148     if( __buffer_info == null)
149     {
150         std::unique_ptr<char[]> packageName(_StringConverter::CopyToCharArrayN(__providerId));
151         std::unique_ptr<char[]> id(_StringConverter::CopyToCharArrayN(__instanceId));
152
153         __buffer_info = provider_buffer_acquire(__type, packageName.get(), id.get(), w, h, sizeof(int), AppWidgetHandleBufferEventCallback, this);
154         SysTryReturnResult(NID_SHELL, __buffer_info , -1, "[E_SYSTEM] failed to provider_buffer_acquire");
155         SysLog(NID_SHELL, "provider_buffer_acquire successed");
156
157         __buffer = provider_buffer_ref(__buffer_info);
158         SysTryReturnResult(NID_SHELL, __buffer , -1, "[E_SYSTEM] failed to provider_buffer_ref");
159         SysLog(NID_SHELL, "provider_buffer_ref successed");
160     }
161
162     int bufferId = __buffer_info->fb->handle;
163     __providerState = RUNNING;
164
165     SysLog(NID_SHELL, "(%d) Exit", bufferId);
166     return bufferId;
167 }
168
169
170 void
171 _AppWidgetContextBase::Suspend(void)
172 {
173         __providerState = SUSPENDED;
174 }
175
176 bool
177 _AppWidgetContextBase::IsRunning(void) const
178 {
179         return (__providerState == RUNNING);
180 }
181
182 result
183 _AppWidgetContextBase::ReleaseSharedMem(void)
184 {
185         SysLog(NID_SHELL, "Enter");
186
187     int ret;
188
189     if( __buffer)
190     {
191         ret = provider_buffer_unref(__buffer);
192         __buffer = null;
193         SysTryReturnResult(NID_SHELL, ret >= 0 , E_SYSTEM, "[E_SYSTEM] failed to provider_buffer_unref");
194         SysLog(NID_SHELL, "provider_buffer_unref successed");
195     }
196
197     if( __buffer_info)
198     {
199                 ret = provider_buffer_release(__buffer_info);
200                 __buffer_info = null;
201                 SysTryReturnResult(NID_SHELL, ret >= 0 , E_SYSTEM, "[E_SYSTEM] failed to provider_buffer_release");
202                 SysLog(NID_SHELL, "provider_buffer_release successed");
203     }
204
205     SysLog(NID_SHELL, "Exit.");
206
207     return E_SUCCESS;
208 }
209
210 result
211 _AppWidgetContextBase::SendRequestToApp(const AppId& appId, const String& operation, HashMap* pArgs)
212 {
213         return _AppWidgetRequestHelper::SendRequestToApp(appId, operation, pArgs);
214 }
215
216 String
217 _AppWidgetContextBase::GetAppId(void) const
218 {
219         return __appId;
220 }
221
222 String
223 _AppWidgetContextBase::GetProviderName(void) const
224 {
225         return __providerName;
226 }
227
228 String
229 _AppWidgetContextBase::GetUserInfo(void) const
230 {
231         return __userInfo;
232 }
233
234 void
235 _AppWidgetContextBase::SetForeground(bool foreground)
236 {
237         __isForeground = foreground;
238 }
239
240 bool
241 _AppWidgetContextBase::IsForeground(void) const
242 {
243         return __isForeground;
244 }
245
246 int
247 _AppWidgetContextBase::GetClientId(void) const
248 {
249         return __ipcClientId;
250 }
251
252 int
253 _AppWidgetContextBase::GetWidth(void) const
254 {
255         return __width;
256 }
257
258 int
259 _AppWidgetContextBase::GetHeight(void) const
260 {
261         return __height;
262 }
263
264 String
265 _AppWidgetContextBase::GetInstanceId(void) const
266 {
267         return __instanceId;
268 }
269
270 String
271 _AppWidgetContextBase::GetProviderId(void) const
272 {
273         return __providerId;
274 }
275
276 int
277 _AppWidgetContextBase::GetPriority(void) const
278 {
279         return __priority;
280 }
281
282 void
283 _AppWidgetContextBase::SetWidth(int width)
284 {
285         __width = width;
286 }
287
288 void
289 _AppWidgetContextBase::SetHeight(int height)
290 {
291         __height = height;
292 }
293
294 result
295 _AppWidgetRequestHelper::SendRequestToApp(const AppId& appId, const String& operation, HashMap* pArgs)
296 {
297         SysLog(NID_SHELL, "appId(%ls), operation(%ls), arg count(%d)", appId.GetPointer(), operation.GetPointer(), pArgs->GetCount() );
298
299         _AppMessageImpl msg;
300         msg.AddData(OSP_K_APPCONTROL_INTERNAL_OPERATION, L"appwidget");
301         Tizen::App::_AppArg::AddStrMap(msg.GetBundle(), pArgs);
302
303         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId) );
304         std::unique_ptr<char[]> pOperation(_StringConverter::CopyToCharArrayN(operation) );
305
306         result r = Tizen::App::_AppControlManager::GetInstance()->LaunchPkg(msg, pAppId.get(), pOperation.get(), null, null, null, null);
307         SysLog(NID_SHELL, "The result of SendRequestToApp [%s]", GetErrorMessage(r));
308
309         return r;
310 }
311
312 }}} // Tizen::Shell::App
313
314 ////////////////////////////////////////////
315 // callback
316 ////////////////////////////////////////////
317 static int AppWidgetHandleBufferEventCallback( struct livebox_buffer *info, enum buffer_event event, double timestamp, double x, double y, void* data)
318 {
319     SysLog(NID_SHELL, "timestamp(%f), x(%f), y(%f)", timestamp, x, y);
320
321     Tizen::Shell::App::_AppWidgetContextBase *pAppWidgetBase = static_cast<Tizen::Shell::App::_AppWidgetContextBase*>(data);
322     SysTryReturn(NID_SHELL, pAppWidgetBase != null, 0, E_SYSTEM, "[E_SYSTEM] retrieved pAppWidgetBase is null");
323
324     if (event ==  BUFFER_EVENT_ENTER)
325     {
326         SysLog(NID_SHELL, "BUFFER_EVENT_ENTER");
327     }
328     else if (event ==  BUFFER_EVENT_LEAVE)
329     {
330         SysLog(NID_SHELL, "BUFFER_EVENT_LEAVE");
331     }
332     else if (event ==  BUFFER_EVENT_DOWN)
333         {
334                 SysLog(NID_SHELL, "BUFFER_EVENT_DOWN");
335         }
336     else if (event ==  BUFFER_EVENT_MOVE)
337     {
338                 SysLog(NID_SHELL, "BUFFER_EVENT_MOVE");
339         }
340     else if(event ==  BUFFER_EVENT_UP)
341         {
342                 SysLog(NID_SHELL, "BUFFER_EVENT_UP");
343         }
344
345     pAppWidgetBase->SendTouchEvent(event, timestamp, x, y);
346
347     return 0;
348 }