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