fix prevent violation
[framework/system/dynamic-analysis-probe.git] / probe_ui / osp_capture.cpp
1 /*
2  *  DA probe
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: 
7  *
8  * Jaewon Lim <jaewon81.lim@samsung.com>
9  * Woojin Jung <woojin2.jung@samsung.com>
10  * Juyoung Kim <j0.kim@samsung.com>
11  * 
12  * This library is free software; you can redistribute it and/or modify it under
13  * the terms of the GNU Lesser General Public License as published by the
14  * Free Software Foundation; either version 2.1 of the License, or (at your option)
15  * any later version.
16  * 
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
20  * License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this library; if not, write to the Free Software Foundation, Inc., 51
24  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25  *
26  * Contributors:
27  * - S-Core Co., Ltd
28  * 
29  */
30
31 #include <Evas.h>
32
33 #include <FUi.h>
34 //#include <FMedia.h>
35
36 #include "daprobe.h"
37 #include "dahelper.h"
38 #include "dacollection.h"
39 #include "daerror.h"
40 //#include "da_ui.h"
41 #include "osp_probe.h"
42
43 using namespace Tizen::Base;
44 using namespace Tizen::Ui;
45 using namespace Tizen::Graphics;
46 using namespace Tizen::Media;
47
48 /*
49 #define LIBUTILX        "libutilX.so.1"
50
51 typedef void* (*utilx_create_screen_shot_type)(Display* dpy, int width, int height);
52 typedef void (*utilx_release_screen_shot_type)(void);
53
54 static Tizen::Graphics::Bitmap* sysutil_capture_screen()
55 {
56         static utilx_create_screen_shot_type utilx_create_screen_shotp;
57         static utilx_release_screen_shot_type utilx_release_screen_shotp;
58         result r;
59         Bitmap* pBitmap = NULL;
60
61         if(unlikely(utilx_create_screen_shotp == NULL))
62         {
63                 void* lib_handle = dlopen(LIBUTILX, RTLD_LAZY);
64                 if(lib_handle == NULL) {
65                         SetLastResult(ERR_DLOPEN);
66                         return NULL;
67                 }
68                 utilx_create_screen_shotp = (utilx_create_screen_shot_type)dlsym(lib_handle, "utilx_create_screen_shot");
69                 if(utilx_create_screen_shotp == NULL || dlerror() != NULL) {
70                         SetLastResult(ERR_DLSYM);
71                         return NULL;
72                 }
73         }
74
75         if(unlikely(utilx_release_screen_shotp == NULL))
76         {
77                 void* lib_handle = dlopen(LIBUTILX, RTLD_LAZY);
78                 if(lib_handle == NULL) {
79                         SetLastResult(ERR_DLOPEN);
80                         return NULL;
81                 }
82                 utilx_release_screen_shotp = (utilx_release_screen_shot_type)dlsym(lib_handle, "utilx_release_screen_shot");
83                 if(utilx_release_screen_shotp == NULL || dlerror() != NULL) {
84                         SetLastResult(ERR_DLSYM);
85                         return NULL;
86                 }
87         }
88
89         Display* pDpy = XOpenDisplay(NULL);
90         if(pDpy == NULL)
91         {
92                 SetLastResult(ERR_USER - 1);
93                 return NULL;
94         }
95
96         int width = DisplayWidth(pDpy, DefaultScreen(pDpy));
97         int height = DisplayHeight(pDpy, DefaultScreen(pDpy));
98
99         void* pDump = utilx_create_screen_shotp(pDpy, width, height);
100         if(likely(pDump != NULL))
101         {
102                 ByteBuffer buffer;
103                 r = buffer.Construct(static_cast<byte*>(pDump), 0, width*height*4, width*height*4);\
104                 if(likely(r == E_SUCCESS))
105                 {
106                         Tizen::Graphics::Dimension dim(width, height);
107                         Bitmap* pBitmap  = new Bitmap();
108                         if(likely(pBitmap != NULL))
109                         {
110                                 r = pBitmap->Construct(buffer, dim, BITMAP_PIXEL_FORMAT_ARGB8888);
111                                 if(unlikely(r != E_SUCCESS))
112                                 {
113                                         SetLastResult(r);
114                                         delete pBitmap;
115                                         pBitmap = NULL;
116                                 }
117                                 else { SetLastResult(E_SUCCESS); }
118                         }
119                         else { SetLastResult(ERR_OUTOFMEMORY); }
120                 }
121                 else { SetLastResult(r); }
122         }
123         else { SetLastResult(ERR_USER - 2); }
124
125         utilx_release_screen_shotp();
126
127         return pBitmap;
128 }
129
130 static Bitmap* getBitmapFromBuffer(void* pDump, int width, int height)
131 {
132         result r;
133         ByteBuffer buffer;
134         Bitmap* pBitmap = NULL;
135
136         r = buffer.Construct(static_cast<byte*>(pDump), 0, width*height*4, width*height*4);
137         if(likely(r == E_SUCCESS))
138         {
139                 Tizen::Graphics::Dimension dim(width, height);
140                 pBitmap  = new Bitmap();
141                 if(likely(pBitmap != NULL))
142                 {
143                         r = pBitmap->Construct(buffer, dim, BITMAP_PIXEL_FORMAT_ARGB8888);
144                         if(unlikely(r != E_SUCCESS))
145                         {
146                                 SetLastResult(r);
147                                 delete pBitmap;
148                                 pBitmap = NULL;
149                         }
150                         else { SetLastResult(E_SUCCESS); }
151                 }
152                 else { SetLastResult(ERR_OUTOFMEMORY); }
153         }
154         else { SetLastResult(r); }
155
156         return pBitmap;
157 }
158
159 // return 0 if succeed
160 // return -1 if failed to capture
161 int captureScreen()
162 {
163         Image img;
164         Bitmap* bitmap;
165         DECLARE_COMMON_VARIABLE;
166         result r;
167         int ret = 0, width, height;
168         char* capbuf;
169
170         probeBlockStart();
171
172         capbuf = captureScreenShotX(&width, &height);
173         if(capbuf != NULL)
174         {
175                 bitmap = getBitmapFromBuffer((void*)capbuf, width, height);
176                 if(bitmap != NULL)
177                 {
178                         String dstPath;
179                         setProbePoint(&probeInfo);
180
181                         dstPath.Format(MAX_PATH_LENGTH, L"/tmp/da/%d.jpg", probeInfo.eventIndex);
182                         img.Construct();
183                         r = img.EncodeToFile(*bitmap, IMG_FORMAT_JPG, dstPath, true);
184                         if(r == E_SUCCESS)
185                         {
186                                 INIT_LOG;
187                                 APPEND_LOG_BASIC_NAME(LC_SNAPSHOT, "captureScreen");
188                                 APPEND_LOG_COMMON_NONE(0);
189                                 log.length += sprintf(log.data + log.length, "`,%S", dstPath.GetPointer());
190                                 printLog(&log, MSG_LOG);
191                         }
192                         else
193                         {
194                                 ret = -1;
195                         }
196
197                         delete bitmap;
198                 }
199                 else
200                 {
201                         char buf[128];
202                         r = GetLastResult();
203                         sprintf(buf, "bitmap is null (%p, %d, %s)\n", capbuf, (int)r, GetErrorMessage(r));
204                         PRINTMSG(buf);
205                         ret = -1;
206                 }
207         }
208         else
209         {
210                 ret = -1;
211         }
212
213         releaseScreenShotX();
214         probeBlockEnd();
215
216         return ret;
217 }
218 */
219
220 namespace Tizen { namespace Ui {
221 /*
222 class _EcoreEvas
223 {
224 public:
225         Evas* GetEvas(void) const;
226 };
227
228 class _EcoreEvasMgr
229 {
230         void SetEcoreEvas(const _EcoreEvas& ecoreevas);
231 };
232
233
234 void _EcoreEvasMgr::SetEcoreEvas(const _EcoreEvas& ecoreevas)
235 {
236         typedef void (_EcoreEvasMgr::*methodType)(const _EcoreEvas& ecoreevas);
237         static methodType _ecoreevasmgr_setecoreevasp;
238
239         GET_REAL_FUNC_OSP(_ZN5Tizen2Ui13_EcoreEvasMgr12SetEcoreEvasERKNS0_10_EcoreEvasE,
240                         LIBOSP_UIFW, _ecoreevasmgr_setecoreevasp);
241
242         probeBlockStart();
243         evas_event_callback_add(ecoreevas.GetEvas(),
244                 EVAS_CALLBACK_RENDER_FLUSH_POST, _cb_render_post, NULL);
245         probeBlockEnd();
246
247         (this->*_ecoreevasmgr_setecoreevasp)(ecoreevas);
248 }
249 */
250
251 result Control::SetShowState(bool state)
252 {
253         typedef result (Control::*methodType)(bool state);
254         static methodType control_setshowstatep;
255         result ret;
256
257         GET_REAL_FUNC_OSP(_ZN5Tizen2Ui7Control12SetShowStateEb,
258                         LIBOSP_UIFW, control_setshowstatep);
259
260         ret = (this->*control_setshowstatep)(state);
261
262         probeBlockStart();
263         {
264                 char *type, *classname;
265                 if(find_object_hash((void*)this, &type, &classname) == 1)
266                 {
267                         if(strcmp(type, "Panel") == 0 || strcmp(type, "OverlayPanel") == 0 || strcmp(type, "ScrollPanel") == 0)
268                         {
269                                 SCREENSHOT_SET();
270 //                              SCREENSHOT_DONE();
271                         }
272                         else
273                         {
274                                 // do nothing
275                         }
276                 }
277                 else
278                 {
279                         // never happened
280                 }
281         }
282         probeBlockEnd();
283
284         return ret;
285 }
286
287 namespace Controls {
288
289 result Frame::SetCurrentForm(const Form& form)
290 {
291         typedef result (Frame::*methodType)(const Form& form);
292         static methodType frame_setcurrentformp;
293         result ret;
294
295         GET_REAL_FUNC_OSP(_ZN5Tizen2Ui8Controls5Frame14SetCurrentFormERKNS1_4FormE,
296                         LIBOSP_UIFW, frame_setcurrentformp);
297
298         ret = (this->*frame_setcurrentformp)(form);
299
300         probeBlockStart();
301         SCREENSHOT_SET();
302 //      SCREENSHOT_DONE();
303         probeBlockEnd();
304
305         return ret;
306 }
307
308 }               // end of namespace Tizen::Ui::Controls
309
310 namespace Animations {
311
312 result FrameAnimator::SetCurrentForm(const Tizen::Ui::Controls::Form& form)
313 {
314         typedef result (FrameAnimator::*methodType)(const Tizen::Ui::Controls::Form& form);
315         static methodType frameanimator_setcurrentformp;
316         result ret;
317
318         GET_REAL_FUNC_OSP(_ZN5Tizen2Ui10Animations13FrameAnimator14SetCurrentFormERKNS0_8Controls4FormE,
319                         LIBOSP_UIFW, frameanimator_setcurrentformp);
320
321         ret = (this->*frameanimator_setcurrentformp)(form);
322
323         probeBlockStart();
324         SCREENSHOT_SET();
325 //      SCREENSHOT_DONE();
326         probeBlockEnd();
327
328         return ret;
329 }
330
331 }               // end of namespace Tizen::Ui::Animations
332
333 } }             // end of namespace Tizen::Ui
334
335