04f46bb8dc20719baf9d3fde1919300c0ff752ed
[framework/osp/appwidget-service.git] / src / FShell_AppWidgetContextBase.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.1 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FShell_AppWidgetContextBase.cpp
20  * @brief       This is the implementation for the _AppWidgetContextBase class.
21  */
22
23 #include <stdlib.h>
24 #include <unique_ptr.h>
25
26 #include "provider_buffer.h"
27
28 #include <FBase.h>
29 #include <FBaseSysLog.h>
30 #include <FBase_StringConverter.h>
31
32 #include <FApp_AppMessageImpl.h>
33 #include <FApp_AppArg.h>
34 #include <FApp_AppControlManager.h>
35 #include <FShell_TemplateUtil.h>
36 #include <FShell_AppWidgetManagerImpl.h>
37
38 #include "FShell_AppWidgetContextBase.h"
39
40 // provider/src/fb.c
41 struct fb_info {
42         char *id;
43         int w;
44         int h;
45         int bufsz;
46         void *buffer;
47
48         int handle;
49 };
50
51 // provider/inc/provider_buffer_internal.h
52 struct livebox_buffer {
53         enum {
54                 BUFFER_CREATED = 0x00beef00,
55                 BUFFER_DESTROYED = 0x00dead00,
56         } state;
57
58         enum target_type type;
59
60         union {
61                 int fd; /* File handle(descriptor) */
62                 int id; /* SHM handle(id) */
63         } handle;
64
65         char *pkgname;
66         char *id;
67         int width;
68         int height;
69         int pixel_size;
70
71         struct fb_info *fb;
72
73         int (*handler)(struct livebox_buffer *info, enum buffer_event event, double timestamp, double x, double y, void *data);
74         void *data;
75 };
76
77 static int AppWidgetHandleBufferEventCallback( struct livebox_buffer *info, enum buffer_event event,
78                 double timestamp, double x, double y, void* data);
79
80 namespace Tizen { namespace Shell  { namespace App
81 {
82
83 using namespace Tizen::App;
84 using namespace Tizen::Base;
85 using namespace Tizen::Base::Collection;
86
87
88 const wchar_t ARG_KEY_INSTANCE_ID[] = L"_InstanceId";
89 const wchar_t ARG_KEY_PROVIDER_NAME[] = L"_ProviderName";
90 const wchar_t ARG_KEY_USER_INFO[] = L"_UserInfo";
91 const wchar_t ARG_KEY_X[] = L"_X";
92 const wchar_t ARG_KEY_Y[] = L"_Y";
93 const wchar_t ARG_KEY_WIDTH[] = L"_Width";
94 const wchar_t ARG_KEY_HEIGHT[] = L"_Height";
95 const wchar_t ARG_KEY_POPUP_WIDTH[] = L"_PopupWidth";
96 const wchar_t ARG_KEY_POPUP_HEIGHT[] = L"_PopupHeight";
97 const wchar_t ARG_KEY_ARGUMENT[] = L"_Argument";
98 const wchar_t ARG_KEY_EVENT_TYPE[] = L"_EventType";
99 const wchar_t ARG_KEY_TIME_STAMP[] = L"_TimeStamp";
100
101
102 _AppWidgetContextBase::_AppWidgetContextBase(target_type type, const String& userInfo, const String& providerId, const String& instanceId, int width, int height, int priority)
103         :__type(type)
104         ,__userInfo(userInfo)
105         ,__providerId(providerId)
106         ,__instanceId(instanceId)
107         ,__width(width)
108         ,__height(height)
109         ,__priority(priority)
110         ,__isForeground(true)
111         ,__ipcClientId(-1)
112         ,__buffer_info(null)
113         ,__buffer(null)
114         ,__providerState(INVALID)
115 {
116         _AppWidgetManagerImpl::ExtractAppIdAndProviderName(providerId, __appId, __providerName);
117
118         SysLog(NID_SHELL, "appId(%ls), providerId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __appId.GetPointer(), __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
119 }
120
121 _AppWidgetContextBase::~_AppWidgetContextBase()
122 {
123         SysLog(NID_SHELL, "providerId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
124         ReleaseSharedMem();
125 }
126
127
128 void
129 _AppWidgetContextBase::SetIpcClientId(int clientId)
130 {
131         __ipcClientId = clientId;
132 }
133
134 bool
135 _AppWidgetContextBase::HasValidClientId(void) const
136 {
137         SysLog(NID_SHELL, "%d", __ipcClientId);
138         return (__ipcClientId > -1);
139 }
140
141 bool
142 _AppWidgetContextBase::IsSharedMemCreated(void) const
143 {
144         return ( __buffer_info && __buffer);
145 }
146
147 int
148 _AppWidgetContextBase::GetSharedMemId(int w, int h)
149 {
150     SysLog(NID_SHELL, "Enter");
151
152     bool isResized = (__buffer_info != null) && (__buffer_info->width != w || __buffer_info->height != h);
153     if ( isResized )
154     {
155         ReleaseSharedMem();
156     }
157
158     if( __buffer_info == null)
159     {
160         std::unique_ptr<char[]> packageName(_StringConverter::CopyToCharArrayN(__providerId));
161         std::unique_ptr<char[]> id(_StringConverter::CopyToCharArrayN(__instanceId));
162
163         __buffer_info = provider_buffer_acquire(__type, packageName.get(), id.get(), w, h, sizeof(int), AppWidgetHandleBufferEventCallback, this);
164         SysTryReturnResult(NID_SHELL, __buffer_info , -1, "[E_SYSTEM] failed to provider_buffer_acquire");
165         SysLog(NID_SHELL, "provider_buffer_acquire successed");
166
167         __buffer = provider_buffer_ref(__buffer_info);
168         SysTryReturnResult(NID_SHELL, __buffer , -1, "[E_SYSTEM] failed to provider_buffer_ref");
169         SysLog(NID_SHELL, "provider_buffer_ref successed");
170     }
171
172     int bufferId = __buffer_info->fb->handle;
173     __providerState = RUNNING;
174
175     SysLog(NID_SHELL, "(%d) Exit", bufferId);
176     return bufferId;
177 }
178
179
180 void
181 _AppWidgetContextBase::Suspend()
182 {
183         __providerState = SUSPENDED;
184 }
185
186 bool
187 _AppWidgetContextBase::IsRunning() const
188 {
189         return (__providerState == RUNNING);
190 }
191
192 result
193 _AppWidgetContextBase::ReleaseSharedMem()
194 {
195         SysLog(NID_SHELL, "Enter");
196
197     int ret;
198
199     if( __buffer)
200     {
201         ret = provider_buffer_unref(__buffer);
202         __buffer = null;
203         SysTryReturnResult(NID_SHELL, ret >= 0 , E_SYSTEM, "[E_SYSTEM] failed to provider_buffer_unref");
204         SysLog(NID_SHELL, "provider_buffer_unref successed");
205     }
206
207     if( __buffer_info)
208     {
209                 ret = provider_buffer_release(__buffer_info);
210                 __buffer_info = null;
211                 SysTryReturnResult(NID_SHELL, ret >= 0 , E_SYSTEM, "[E_SYSTEM] failed to provider_buffer_release");
212                 SysLog(NID_SHELL, "provider_buffer_release successed");
213     }
214
215     SysLog(NID_SHELL, "Exit.");
216
217     return E_SUCCESS;
218 }
219
220 result
221 _AppWidgetContextBase::SendRequestToApp(const AppId& appId, const String& operation, HashMap* pArgs)
222 {
223         if( __isForeground == false)
224         {
225                 SysLog(NID_SHELL, "appWidget isn't foreground, so, message skip");
226                 return E_SUCCESS;
227         }
228
229         return _AppWidgetRequestHelper::SendRequestToApp(appId, operation, pArgs);
230 }
231
232 result
233 _AppWidgetRequestHelper::SendRequestToApp(const AppId& appId, const String& operation, HashMap* pArgs)
234 {
235         SysLog(NID_SHELL, "appId(%ls), operation(%ls), arg count(%d)", appId.GetPointer(), operation.GetPointer(), pArgs->GetCount() );
236
237         _AppMessageImpl msg;
238         msg.AddData(OSP_K_APPCONTROL_INTERNAL_OPERATION, L"livebox");
239         Tizen::App::_AppArg::AddStrMap(msg.GetBundle(), pArgs);
240
241         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId) );
242         std::unique_ptr<char[]> pOperation(_StringConverter::CopyToCharArrayN(operation) );
243
244         return Tizen::App::_AppControlManager::GetInstance()->LaunchPkg(msg, pAppId.get(), pOperation.get(), null, null, null, null);
245 }
246
247 } } } // Tizen::Shell::App {
248
249
250 ////////////////////////////////////////////
251 // callback
252 ////////////////////////////////////////////
253 static int AppWidgetHandleBufferEventCallback( struct livebox_buffer *info, enum buffer_event event,
254                 double timestamp, double x, double y, void* data)
255 {
256     SysLog(NID_SHELL, "timestamp(%f), x(%f), y(%f)", timestamp, x, y);
257
258     Tizen::Shell::App::_AppWidgetContextBase *pAppWidgetBase = static_cast<Tizen::Shell::App::_AppWidgetContextBase*>(data);
259     SysTryReturn(NID_SHELL, pAppWidgetBase != null, 0, E_SYSTEM, "[E_SYSTEM] retrieved pAppWidgetBase is null");
260
261 //    const char *pkgname = provider_buffer_pkgname(info);
262 //    const char *id = provider_buffer_id(info);
263 //    enum target_type type = provider_buffer_type(info);
264
265     if( event ==  BUFFER_EVENT_ENTER)
266     {
267         SysLog(NID_SHELL, "BUFFER_EVENT_ENTER");
268     }
269     else if(   event ==  BUFFER_EVENT_LEAVE)
270     {
271         SysLog(NID_SHELL, "BUFFER_EVENT_LEAVE");
272     }
273     else if(   event ==  BUFFER_EVENT_DOWN)
274         {
275                 SysLog(NID_SHELL, "BUFFER_EVENT_DOWN");
276         }
277     else if(   event ==  BUFFER_EVENT_MOVE)
278     {
279                 SysLog(NID_SHELL, "BUFFER_EVENT_MOVE");
280         }
281     else if(   event ==  BUFFER_EVENT_UP)
282         {
283                 SysLog(NID_SHELL, "BUFFER_EVENT_UP");
284         }
285
286     pAppWidgetBase->SendTouchEvent(event, timestamp, x, y);
287
288     return 0;
289 }