9bce5a26d40cafc041f44ab2d861a68469083e89
[framework/osp/appwidget-service.git] / src / FShell_AppWidgetContextBase.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 String ARG_KEY_INSTANCE_ID = L"_InstanceId";
89 const String ARG_KEY_PROVIDER_NAME = L"_ProviderName";
90 const String ARG_KEY_USER_INFO = L"_UserInfo";
91 const String ARG_KEY_WIDTH = L"_Width";
92 const String ARG_KEY_HEIGHT = L"_Height";
93
94
95 _AppWidgetContextBase::_AppWidgetContextBase(target_type type, const String& userInfo, const String& providerId, const String& instanceId, int width, int height, int priority)
96         :__type(type)
97         ,__userInfo(userInfo)
98         ,__providerId(providerId)
99         ,__instanceId(instanceId)
100         ,__width(width)
101         ,__height(height)
102         ,__priority(priority)
103         ,__isForeground(true)
104         ,__ipcClientId(-1)
105         ,__buffer_info(null)
106         ,__buffer(null)
107         ,__providerState(INVALID)
108 {
109         _AppWidgetManagerImpl::ExtractAppIdAndProviderName(providerId, __appId, __providerName);
110
111         SysLog(NID_APP, "appId(%ls), providerId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __appId.GetPointer(), __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
112 }
113
114 _AppWidgetContextBase::~_AppWidgetContextBase()
115 {
116         SysLog(NID_APP, "providerId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
117         ReleaseSharedMem();
118 }
119
120
121 void
122 _AppWidgetContextBase::SetIpcClientId(int clientId)
123 {
124         __ipcClientId = clientId;
125 }
126
127 bool
128 _AppWidgetContextBase::HasValidClientId(void) const
129 {
130         SysLog(NID_APP, "%d", __ipcClientId);
131         return (__ipcClientId > -1);
132 }
133
134 bool
135 _AppWidgetContextBase::IsSharedMemCreated(void) const
136 {
137         return ( __buffer_info && __buffer_info);
138 }
139
140 int
141 _AppWidgetContextBase::GetSharedMemId(int w, int h)
142 {
143         SysLog(NID_APP, "Enter");
144
145         if( __buffer_info == null)
146         {
147                 std::unique_ptr<char[]> packageName(_StringConverter::CopyToCharArrayN(__providerId));
148                 std::unique_ptr<char[]> id(_StringConverter::CopyToCharArrayN(__instanceId));
149
150                 __buffer_info = provider_buffer_acquire(__type, packageName.get(), id.get(), w, h, sizeof(int), AppWidgetHandleBufferEventCallback, this);
151                 SysTryReturnResult(NID_APP, __buffer_info , -1, "[E_SYSTEM] failed to provider_buffer_acquire");
152                 SysLog(NID_APP, "provider_buffer_acquire successed");
153
154                 __buffer = provider_buffer_ref(__buffer_info);
155                 SysTryReturnResult(NID_APP, __buffer , -1, "[E_SYSTEM] failed to provider_buffer_ref");
156                 SysLog(NID_APP, "provider_buffer_ref successed");
157         }
158
159     int bufferId = __buffer_info->fb->handle;
160     __providerState = RUNNING;
161
162     SysLog(NID_APP, "(%d) Exit", bufferId);
163     return bufferId;
164 }
165
166
167 void
168 _AppWidgetContextBase::Suspend()
169 {
170         __providerState = SUSPENDED;
171 }
172
173 bool
174 _AppWidgetContextBase::IsRunning() const
175 {
176         return (__providerState == RUNNING);
177 }
178
179 result
180 _AppWidgetContextBase::ReleaseSharedMem()
181 {
182         SysLog(NID_APP, "Enter");
183
184     int ret;
185
186     if( __buffer)
187     {
188         ret = provider_buffer_unref(__buffer);
189         __buffer = null;
190         SysTryReturnResult(NID_APP, ret >= 0 , E_SYSTEM, "[E_SYSTEM] failed to provider_buffer_unref");
191         SysLog(NID_APP, "provider_buffer_unref successed");
192     }
193
194     if( __buffer_info)
195     {
196                 ret = provider_buffer_release(__buffer_info);
197                 __buffer_info = null;
198                 SysTryReturnResult(NID_APP, ret >= 0 , E_SYSTEM, "[E_SYSTEM] failed to provider_buffer_release");
199                 SysLog(NID_APP, "provider_buffer_release successed");
200     }
201
202     SysLog(NID_APP, "Exit.");
203
204     return E_SUCCESS;
205 }
206
207 Tizen::Base::Collection::HashMap*
208 _AppWidgetContextBase::CreateRequestArgs(void)
209 {
210         HashMap* pArgs = new (std::nothrow) HashMap(SingleObjectDeleter);
211         pArgs->Construct();
212         pArgs->Add(new String(ARG_KEY_INSTANCE_ID), new String(__instanceId));
213         pArgs->Add(new String(ARG_KEY_PROVIDER_NAME), new String(__providerName));
214         pArgs->Add(new String(ARG_KEY_USER_INFO), new String(__userInfo));
215         pArgs->Add(new String(ARG_KEY_WIDTH), new String(Integer::ToString(__width)));
216         pArgs->Add(new String(ARG_KEY_HEIGHT), new String(Integer::ToString(__height)));
217
218         return pArgs;
219 }
220
221 result
222 _AppWidgetContextBase::SendRequestToApp(const AppId& appId, const String& operation, HashMap* pArgs)
223 {
224         if( __isForeground == false)
225         {
226                 SysLog(NID_APP, "appWidget isn't foreground, so, message skip");
227                 return E_SUCCESS;
228         }
229
230         return _AppWidgetRequestHelper::SendRequestToApp(appId, operation, pArgs);
231 }
232
233 result
234 _AppWidgetRequestHelper::SendRequestToApp(const AppId& appId, const String& operation, HashMap* pArgs)
235 {
236         SysLog(NID_APP, "appId(%ls), operation(%ls), arg count(%d)", appId.GetPointer(), operation.GetPointer(), pArgs->GetCount() );
237
238         _AppMessageImpl msg;
239         msg.AddData(OSP_K_APPCONTROL_INTERNAL_OPERATION, L"livebox");
240         Tizen::App::_AppArg::AddStrMap(msg.GetBundle(), pArgs);
241
242         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId) );
243         std::unique_ptr<char[]> pOperation(_StringConverter::CopyToCharArrayN(operation) );
244
245         return Tizen::App::_AppControlManager::GetInstance()->LaunchPkg(msg, pAppId.get(), pOperation.get(), null, null, null, null);
246 }
247
248 } } } // Tizen::Shell::App {
249
250
251 ////////////////////////////////////////////
252 // callback
253 ////////////////////////////////////////////
254 static int AppWidgetHandleBufferEventCallback( struct livebox_buffer *info, enum buffer_event event,
255                 double timestamp, double x, double y, void* data)
256 {
257     SysLog(NID_APP, "timestamp(%f), x(%f), y(%f)", timestamp, x, y);
258
259     Tizen::Shell::App::_AppWidgetContextBase *pAppWidgetBase = static_cast<Tizen::Shell::App::_AppWidgetContextBase*>(data);
260     SysTryReturn(NID_APP, pAppWidgetBase != null, 0, E_SYSTEM, "[E_SYSTEM] retrieved pAppWidgetBase is null");
261
262 //    const char *pkgname = provider_buffer_pkgname(info);
263 //    const char *id = provider_buffer_id(info);
264 //    enum target_type type = provider_buffer_type(info);
265
266     if( event ==  BUFFER_EVENT_ENTER)
267     {
268         SysLog(NID_APP, "BUFFER_EVENT_ENTER");
269     }
270     else if(   event ==  BUFFER_EVENT_LEAVE)
271     {
272         SysLog(NID_APP, "BUFFER_EVENT_LEAVE");
273     }
274     else if(   event ==  BUFFER_EVENT_DOWN)
275         {
276                 SysLog(NID_APP, "BUFFER_EVENT_DOWN");
277         }
278     else if(   event ==  BUFFER_EVENT_MOVE)
279     {
280                 SysLog(NID_APP, "BUFFER_EVENT_MOVE");
281         }
282     else if(   event ==  BUFFER_EVENT_UP)
283         {
284                 SysLog(NID_APP, "BUFFER_EVENT_UP");
285         }
286
287     pAppWidgetBase->SendTouchEvent(event, timestamp, x, y);
288
289     return 0;
290 }