Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / FUiSystemUtil.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.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://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 #include <X11/extensions/XTest.h>
18 #include <X11/Xlib.h>
19 #include <X11/keysym.h>
20 #include <utilX.h>
21
22 //to avoid camera key code definition
23 #ifdef KEY_CAMERA
24 #undef KEY_CAMERA
25 #endif
26 #include <unique_ptr.h>
27 #include <Ecore_X.h>
28 #include <FBaseSysLog.h>
29 #include <FUiSystemUtil.h>
30 #include <FGrpPoint.h>
31 #include <FGrpDimension.h>
32 #include <FGrpBitmap.h>
33 #include <FAppAppManager.h>
34 #include "FSec_AccessController.h"
35 #include "FApp_AppInfo.h"
36
37 using namespace Tizen::Security;
38 using namespace Tizen::Graphics;
39 using namespace Tizen::Base;
40
41 namespace Tizen { namespace Ui
42 {
43
44 struct ConvertKeys
45 {
46         KeyCode code;
47         const char* pName;
48 };
49
50 const ConvertKeys KeyCodeMap[] = {
51         { KEY_INVALID, "" },
52         { KEY_SIDE_UP, "XF86AudioRaiseVolume" },
53         { KEY_SIDE_DOWN, "XF86AudioLowerVolume" },
54         { KEY_OK, "XF86Phone" },
55         { KEY_CLEAR, "" },
56         { KEY_CAMERA, "" },
57         { KEY_SWITCH, "" },
58         { KEY_0, "0" },
59         { KEY_1, "1" },
60         { KEY_2, "2" },
61         { KEY_3, "3" },
62         { KEY_4, "4" },
63         { KEY_5, "5" },
64         { KEY_6, "6" },
65         { KEY_7, "7" },
66         { KEY_8, "8" },
67         { KEY_9, "9" },
68         { KEY_ASTERISK, "" },
69         { KEY_SHARP, "" },
70         { KEY_LEFT, "KP_Left" },
71         { KEY_UP, "KP_Up" },
72         { KEY_DOWN, "KP_Down" },
73         { KEY_RIGHT, "KP_Right" },
74         { KEY_MAX, "" },
75         { KEY_A, "a" },
76         { KEY_B, "b" },
77         { KEY_C, "c" },
78         { KEY_D, "d" },
79         { KEY_E, "e" },
80         { KEY_F, "f" },
81         { KEY_G, "g" },
82         { KEY_H, "h" },
83         { KEY_I, "i" },
84         { KEY_J, "j" },
85         { KEY_K, "k" },
86         { KEY_L, "l" },
87         { KEY_M, "m" },
88         { KEY_N, "n" },
89         { KEY_O, "o" },
90         { KEY_P, "p" },
91         { KEY_Q, "q" },
92         { KEY_R, "r" },
93         { KEY_S, "s" },
94         { KEY_T, "t" },
95         { KEY_U, "u" },
96         { KEY_V, "v" },
97         { KEY_W, "w" },
98         { KEY_Y, "y" },
99         { KEY_X, "x" },
100         { KEY_Z, "z" },
101         { KEY_BACKSPACE, "BackSpace" },
102         { KEY_COMMA, "comma" },
103         { KEY_ENTER, "" },
104         { KEY_CAPSLOCK, "Caps_Lock" },
105         { KEY_QUESTION, "" },
106         { KEY_ALT, "Alt_L" },
107         { KEY_SYM, "" },
108         { KEY_SETTING, "" },
109         { KEY_SPACE, "space" },
110         { KEY_DOT, "" },
111         { KEY_FN, "" },
112         { KEY_CAMERA_HALF_SHUTTER, "" },
113         { KEY_CHAR_SYM_1, "" },
114         { KEY_CHAR_SYM_2, "" },
115         { KEY_CHAR_SYM_3, "" },
116         { KEY_CHAR_SYM_4, "" },
117         { KEY_CHAR_SYM_5, "" },
118         { KEY_CHAR_SYM_6, "" },
119         { KEY_CHAR_SYM_7, "" },
120         { KEY_CHAR_SYM_8, "" },
121         { KEY_CHAR_SYM_9, "" },
122         { KEY_CHAR_SYM_A, "" },
123         { KEY_CHAR_SYM_B, "" },
124         { KEY_CHAR_SYM_C, "" },
125         { KEY_CHAR_SYM_D, "" },
126         { KEY_CHAR_SYM_E, "" },
127         { KEY_CHAR_SYM_F, "" },
128         { KEY_FN_1, "" },
129         { KEY_FN_2, "" },
130         { KEY_FN_3, "" },
131         { KEY_FN_4, "" },
132         { KEY_FN_5, "" },
133         { KEY_POWER_HOLD, "XF86PowerOff" },
134         { KEY_DELETE, "" },
135         { KEY_HARDWARE_MAX, "" },
136 };
137
138 struct _DisplayDeleter
139 {
140         void operator()(Display* pDisplay)
141         {
142                 XCloseDisplay(pDisplay);
143         }
144 };
145
146 SystemUtil::SystemUtil(void)
147 {
148 }
149
150 SystemUtil::~SystemUtil(void)
151 {
152 }
153
154 result
155 SystemUtil::GenerateKeyEvent(KeyEventType KeyEvent, KeyCode keyCode)
156 {
157         result r = E_SUCCESS;
158
159         const String& appId = Tizen::App::_AppInfo::GetAppId();
160
161         r = _AccessController::CheckSystemPrivilege(appId, _PRV_INPUTMANAGER);
162
163         SysTryReturn(NID_UI, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED, ("[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method."));
164
165         Display* pDisplay = (Display*)ecore_x_display_get();
166         SysTryReturnResult(NID_UI, pDisplay != null, E_SYSTEM, "[E_SYSTEM] pDisplay is invalid. something wrong!");
167
168         std::unique_ptr<Display, _DisplayDeleter> pDpy(XOpenDisplay(NULL));
169         SysTryReturnResult(NID_UI, pDpy.get() != null, E_SYSTEM, "[E_SYSTEM] pDpy is invalid. something wrong!");
170
171         int key = -1;
172
173         for (unsigned int idx = 0; idx < sizeof(KeyCodeMap) / sizeof(ConvertKeys); ++idx)
174         {
175                 if (KeyCodeMap[idx].code == keyCode)
176                 {
177                         KeySym keySym = XStringToKeysym(KeyCodeMap[idx].pName);
178                         key = (int) XKeysymToKeycode(pDpy.get(), keySym);
179                         break;
180                 }
181         }
182
183         if (key == -1)
184         {
185                 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Unsupported KeyCode argument.");
186                 return E_INVALID_ARG;
187         }
188
189         switch (KeyEvent)
190         {
191         case KEY_EVENT_TYPE_PRESSED:
192                 XTestFakeKeyEvent(pDisplay, key, true, CurrentTime);
193                 break;
194
195         case KEY_EVENT_TYPE_RELEASED:
196                 XTestFakeKeyEvent(pDisplay, key, false, CurrentTime);
197                 break;
198
199         default:
200                 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Invalid KeyEventType argument.");
201                 return E_INVALID_ARG;
202         }
203
204         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
205         return r;
206 }
207
208 result
209 SystemUtil::GenerateTouchEvent(TouchEventType touchEvent, const Tizen::Graphics::Point& point)
210 {
211         result r = E_SUCCESS;
212         const String& appId = Tizen::App::_AppInfo::GetAppId();
213
214         r = _AccessController::CheckSystemPrivilege(appId, _PRV_INPUTMANAGER);
215
216         SysTryReturn(NID_UI, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED, ("[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method."));
217
218         Display* pDisplay = (Display*)ecore_x_display_get();
219         SysTryReturnResult(NID_UI, pDisplay != null, E_SYSTEM, "[E_SYSTEM] pDisplay is invalid. something wrong!");
220
221         std::unique_ptr<Display, _DisplayDeleter> pDpy(XOpenDisplay(NULL));
222         SysTryReturnResult(NID_UI, pDpy.get() != null, E_SYSTEM, "[E_SYSTEM] pDpy is invalid. something wrong!");
223
224         int width = DisplayWidth(pDpy.get(), DefaultScreen(pDpy.get()));
225         int height = DisplayHeight(pDpy.get(), DefaultScreen(pDpy.get()));
226
227         if (( point.x<0 || point.x>width) || (point.y<0 || point.y>height))
228         {
229                 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Out of bounds x, y argument.");
230                 return E_INVALID_ARG;
231         }
232
233         int screenNumber = 0; //main screen
234         int button = 1; //left button
235
236         switch (touchEvent)
237         {
238         case TOUCH_EVENT_TYPE_PRESSED:
239                 XWarpPointer(pDisplay, None, XRootWindow(pDisplay, 0), 0, 0, 0, 0, point.x, point.y);
240                 XTestFakeButtonEvent(pDisplay, button, true, CurrentTime);
241                 break;
242
243         case TOUCH_EVENT_TYPE_RELEASED:
244                 XWarpPointer(pDisplay, None, XRootWindow(pDisplay, 0), 0, 0, 0, 0, point.x, point.y);
245                 XTestFakeButtonEvent(pDisplay, button, false, CurrentTime);
246                 break;
247
248         case TOUCH_EVENT_TYPE_MOVED:
249                 XTestFakeMotionEvent(pDisplay, screenNumber, point.x, point.y, CurrentTime);
250                 break;
251
252         default:
253                 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Invalid TouchEventType argument.");
254                 return E_INVALID_ARG;
255         }
256
257         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r)); //fix me
258         return r;
259 }
260
261 Tizen::Graphics::Bitmap*
262 SystemUtil::CaptureScreenN(void)
263 {
264         result r = E_SUCCESS;
265         const String& appId = Tizen::App::_AppInfo::GetAppId();
266
267         r = _AccessController::CheckSystemPrivilege(appId, _PRV_INPUTMANAGER);
268
269         SysTryReturn(NID_UI, r == E_SUCCESS, null, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
270         std::unique_ptr<Display, _DisplayDeleter> pDpy(XOpenDisplay(NULL));
271         SysTryReturn(NID_UI, pDpy, null, E_SYSTEM, "[E_SYSTEM] pDpy is invalid. something wrong!");
272
273         int width = DisplayWidth(pDpy.get(), DefaultScreen(pDpy.get()));
274         int height = DisplayHeight(pDpy.get(), DefaultScreen(pDpy.get()));
275
276         void* pDump = utilx_create_screen_shot(pDpy.get(), width, height);
277         SysTryReturn(NID_UI, pDump, null, E_SYSTEM, "[E_SYSTEM] pDump is invalid. something wrong!");
278
279         std::unique_ptr<ByteBuffer> pBuffer(new (std::nothrow) ByteBuffer());
280         SysTryReturn(NID_UI, pBuffer != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failure.");
281
282         r = pBuffer->Construct(static_cast<byte*>(pDump), 0, width*height*4, width*height*4);
283         SysTryReturn(NID_UI, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
284
285         Tizen::Graphics::Dimension dim(width, height);
286         std::unique_ptr<Tizen::Graphics::Bitmap> pBitmap(new (std::nothrow) Bitmap());
287         SysTryReturn(NID_UI, pBitmap != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failure.");
288
289         r = pBitmap->Construct(*pBuffer, dim, BITMAP_PIXEL_FORMAT_ARGB8888);
290         SysTryReturn(NID_UI, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
291
292         utilx_release_screen_shot();
293
294         return pBitmap.release();
295 }
296
297 }} //Tizen::Ui