b4185b700559946d243e014ea9e8a22efc24669c
[platform/framework/native/uifw.git] / src / ui / FUi_SystemUtilImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 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 #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 <FUi_SystemUtilImpl.h>
30 #include <FGrpPoint.h>
31 #include <FGrpDimension.h>
32 #include <FGrpBitmap.h>
33 #include <FUiSystemUtil.h>
34 #include "FUi_UiKeyEvent.h"
35 #include "FUi_CoordinateSystemUtils.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_ESC, "Escape" },
52         { _KEY_GRAVE, "grave" },
53         { _KEY_1, "1" },
54         { _KEY_2, "2" },
55         { _KEY_3, "3" },
56         { _KEY_4, "4" },
57         { _KEY_5, "5" },
58         { _KEY_6, "6" },
59         { _KEY_7, "7" },
60         { _KEY_8, "8" },
61         { _KEY_9, "9" },
62         { _KEY_0, "0" },
63         { _KEY_MINUS, "minus" },
64         { _KEY_EQUAL, "equal" },
65         { _KEY_BACKSLASH, "backslash" },
66         { _KEY_BACKSPACE, "BackSpace" },
67         { _KEY_TAB, "Tab" },
68         { _KEY_Q, "q" },
69         { _KEY_W, "w" },
70         { _KEY_E, "e" },
71         { _KEY_R, "r" },
72         { _KEY_T, "t" },
73         { _KEY_Y, "y" },
74         { _KEY_U, "u" },
75         { _KEY_I, "i" },
76         { _KEY_O, "o" },
77         { _KEY_P, "p" },
78         { _KEY_LBRACKET, "bracketleft" },
79         { _KEY_RBRACKET, "bracketright" },
80         { _KEY_ENTER, "Return" },
81         { _KEY_CAPSLOCK, "Caps_Lock" },
82         { _KEY_A, "a" },
83         { _KEY_S, "s" },
84         { _KEY_D, "d" },
85         { _KEY_F, "f" },
86         { _KEY_G, "g" },
87         { _KEY_H, "h" },
88         { _KEY_J, "j" },
89         { _KEY_K, "k" },
90         { _KEY_L, "l" },
91         { _KEY_SEMICOLON, "semicolon" },
92         { _KEY_APOSTROPHE, "apostrophe" },
93         { _KEY_SHIFT_L, "Shift_L" },
94         { _KEY_Z, "z" },
95         { _KEY_X, "x" },
96         { _KEY_C, "c" },
97         { _KEY_V, "v" },
98         { _KEY_B, "b" },
99         { _KEY_N, "n" },
100         { _KEY_M, "m" },
101         { _KEY_COMMA, "comma" },
102         { _KEY_DOT, "period" },
103         { _KEY_SLASH, "slash" },
104         { _KEY_SHIFT_R, "Shift_R" },
105         { _KEY_CTRL_L, "Control_L" },
106         { _KEY_LALT, "Alt_L" },
107         { _KEY_SPACE, "space" },
108         { _KEY_RALT, "Alt_R" },
109         { _KEY_CTRL_R, "Control_R" },
110         { _KEY_FN_1, "F1" },
111         { _KEY_FN_2, "F2" },
112         { _KEY_FN_3, "F3" },
113         { _KEY_FN_4, "F4" },
114         { _KEY_FN_5, "F5" },
115         { _KEY_FN_6, "F6" },
116         { _KEY_FN_7, "F7" },
117         { _KEY_FN_8, "F8" },
118         { _KEY_FN_9, "F9" },
119         { _KEY_FN_10, "F10" },
120         { _KEY_FN_11, "F11" },
121         { _KEY_FN_12, "F12" },
122         { _KEY_NUM_LOCK, "Num_Lock" },
123         { _KEY_NUMPAD_DIVIDE, "KP_Divide" },
124         { _KEY_NUMPAD_MULTIPLY, "KP_Multiply" },
125         { _KEY_NUMPAD_SUBTRACT, "KP_Subtract" },
126         { _KEY_NUMPAD_7, "KP_Home" },
127         { _KEY_NUMPAD_8, "KP_Up" },
128         { _KEY_NUMPAD_9, "KP_Prior" },
129         { _KEY_NUMPAD_4, "KP_Left" },
130         { _KEY_NUMPAD_5, "KP_Begin" },
131         { _KEY_NUMPAD_6, "KP_Right" },
132         { _KEY_NUMPAD_ADD, "KP_Add" },
133         { _KEY_NUMPAD_1, "KP_End" },
134         { _KEY_NUMPAD_2, "KP_Down" },
135         { _KEY_NUMPAD_3, "KP_Next" },
136         { _KEY_NUMPAD_0, "KP_Insert" },
137         { _KEY_NUMPAD_DOT, "KP_Delete" },
138         { _KEY_NUMPAD_ENTER, "KP_Enter" },
139         { _KEY_PRINT, "Print" },
140         { _KEY_SCROLL_LOCK, "Scroll_Lock" },
141         { _KEY_PAUSEBREAK, "Pause" },
142         { _KEY_INSERT, "Insert" },
143         { _KEY_HOME_BUTTON, "Home" },
144         { _KEY_PAGE_UP, "Prior" },
145         { _KEY_DELETE, "Delete" },
146         { _KEY_END_BUTTON, "End" },
147         { _KEY_PAGE_DOWN, "Next" },
148         { _KEY_UP, "Up" },
149         { _KEY_LEFT, "Left" },
150         { _KEY_DOWN, "Down" },
151         { _KEY_RIGHT, "Right" },
152         { _KEY_CONTEXT_MENU, "Menu" },
153
154         //{ _KEY_SIDE_UP, "XF86AudioRaiseVolume" },
155         //{ _KEY_SIDE_DOWN, "XF86AudioLowerVolume" },
156         //{ _KEY_POWER_HOLD, "XF86PowerOff" },
157         { _KEY_CLEAR, "XF86Stop" }
158 };
159
160 struct _DisplayDeleter
161 {
162         void operator()(Display* pDisplay)
163         {
164                 XCloseDisplay(pDisplay);
165         }
166 };
167
168 _SystemUtilImpl::_SystemUtilImpl(void)
169 {
170 }
171
172 _SystemUtilImpl::~_SystemUtilImpl(void)
173 {
174 }
175
176 result
177 _SystemUtilImpl::GenerateKeyEvent(Tizen::Ui::KeyEventType keyEvent, Tizen::Ui::_KeyCode keyCode)
178 {
179         result r = E_SUCCESS;
180
181         Display* pDisplay = (Display*)ecore_x_display_get();
182         SysTryReturnResult(NID_UI, pDisplay != null, E_OPERATION_FAILED, "[E_OPERATION_FAILED] pDisplay is invalid. something wrong!");
183
184         std::unique_ptr<Display, _DisplayDeleter> pDpy(XOpenDisplay(NULL));
185         SysTryReturnResult(NID_UI, pDpy.get() != null, E_OPERATION_FAILED, "[E_OPERATION_FAILED] pDpy is invalid. something wrong!");
186
187         int key = -1;
188
189         for (unsigned int idx = 0; idx < sizeof(KeyCodeMap) / sizeof(ConvertKeys); ++idx)
190         {
191                 if (KeyCodeMap[idx].code == keyCode)
192                 {
193                         KeySym keySym = XStringToKeysym(KeyCodeMap[idx].pName);
194                         key = (int) XKeysymToKeycode(pDpy.get(), keySym);
195                         break;
196                 }
197         }
198
199         if (key == -1)
200         {
201                 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Unsupported KeyCode argument.");
202                 return E_INVALID_ARG;
203         }
204
205         switch (keyEvent)
206         {
207         case KEY_EVENT_TYPE_PRESSED:
208                 XTestFakeKeyEvent(pDisplay, key, true, CurrentTime);
209                 break;
210
211         case KEY_EVENT_TYPE_RELEASED:
212                 XTestFakeKeyEvent(pDisplay, key, false, CurrentTime);
213                 break;
214
215         default:
216                 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Invalid KeyEventType argument.");
217                 return E_INVALID_ARG;
218         }
219
220         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
221         return r;
222 }
223
224 result
225 _SystemUtilImpl::GenerateTouchEvent(TouchEventType touchEvent, const Tizen::Graphics::Point& point)
226 {
227         return GenerateTouchEvent(touchEvent, _CoordinateSystemUtils::ConvertToFloat(point));
228 }
229
230 result
231 _SystemUtilImpl::GenerateTouchEvent(TouchEventType touchEvent, const Tizen::Graphics::FloatPoint& point)
232 {
233         result r = E_SUCCESS;
234
235         Display* pDisplay = (Display*)ecore_x_display_get();
236         SysTryReturnResult(NID_UI, pDisplay != null, E_OPERATION_FAILED, "[E_OPERATION_FAILED] pDisplay is invalid. something wrong!");
237
238         std::unique_ptr<Display, _DisplayDeleter> pDpy(XOpenDisplay(NULL));
239         SysTryReturnResult(NID_UI, pDpy.get() != null, E_OPERATION_FAILED, "[E_OPERATION_FAILED] pDpy is invalid. something wrong!");
240
241         int width = DisplayWidth(pDpy.get(), DefaultScreen(pDpy.get()));
242         int height = DisplayHeight(pDpy.get(), DefaultScreen(pDpy.get()));
243
244         if (( point.x<0 || point.x>width) || (point.y<0 || point.y>height))
245         {
246                 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Out of bounds x, y argument.");
247                 return E_INVALID_ARG;
248         }
249
250         int screenNumber = 0; //main screen
251         int button = 1; //left button
252
253         switch (touchEvent)
254         {
255         case TOUCH_EVENT_TYPE_PRESSED:
256                 XWarpPointer(pDisplay, None, XRootWindow(pDisplay, 0), 0, 0, 0, 0, point.x, point.y);
257                 XTestFakeButtonEvent(pDisplay, button, true, CurrentTime);
258                 break;
259
260         case TOUCH_EVENT_TYPE_RELEASED:
261                 XWarpPointer(pDisplay, None, XRootWindow(pDisplay, 0), 0, 0, 0, 0, point.x, point.y);
262                 XTestFakeButtonEvent(pDisplay, button, false, CurrentTime);
263                 break;
264
265         case TOUCH_EVENT_TYPE_MOVED:
266                 XTestFakeMotionEvent(pDisplay, screenNumber, point.x, point.y, CurrentTime);
267                 break;
268
269         default:
270                 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Invalid TouchEventType argument.");
271                 return E_INVALID_ARG;
272         }
273
274         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
275         return r;
276 }
277
278 Tizen::Graphics::Bitmap*
279 _SystemUtilImpl::CaptureScreenN(void)
280 {
281         result r = E_SUCCESS;
282
283         std::unique_ptr<Display, _DisplayDeleter> pDpy(XOpenDisplay(NULL));
284         SysTryReturn(NID_UI, pDpy.get(), null, E_OPERATION_FAILED, "[E_OPERATION_FAILED] pDpy is invalid. something wrong!");
285
286         int width = DisplayWidth(pDpy.get(), DefaultScreen(pDpy.get()));
287         int height = DisplayHeight(pDpy.get(), DefaultScreen(pDpy.get()));
288
289         void* pDump = utilx_create_screen_shot(pDpy.get(), width, height);
290         SysTryReturn(NID_UI, pDump, null, E_OPERATION_FAILED, "[E_OPERATION_FAILED] pDump is invalid. something wrong!");
291
292         std::unique_ptr<ByteBuffer> pBuffer(new (std::nothrow) ByteBuffer());
293         SysTryReturn(NID_UI, pBuffer != null, null, E_OPERATION_FAILED, "[E_OPERATION_FAILED] Memory allocation failure.");
294
295         r = pBuffer->Construct(static_cast<byte*>(pDump), 0, width*height*4, width*height*4);
296         SysTryReturn(NID_UI, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
297
298         Tizen::Graphics::Dimension dim(width, height);
299         std::unique_ptr<Tizen::Graphics::Bitmap> pBitmap(new (std::nothrow) Bitmap());
300         SysTryReturn(NID_UI, pBitmap != null, null, E_OPERATION_FAILED, "[E_OPERATION_FAILED] Memory allocation failure.");
301
302         r = pBitmap->Construct(*pBuffer, dim, BITMAP_PIXEL_FORMAT_ARGB8888);
303         SysTryReturn(NID_UI, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
304
305         utilx_release_screen_shot();
306
307         return pBitmap.release();
308 }
309
310 result
311 _SystemUtilImpl::SendKeyStopMessage(void)
312 {
313         bool r = false;
314         int num;
315         Ecore_X_Window *_keygrab_win = null;
316         char msg_data[] = "PxXF86Stop";
317
318         Ecore_X_Atom type = ecore_x_atom_get("_HWKEY_EMULATION");
319
320         int ret = ecore_x_window_prop_property_get(null, type, ECORE_X_ATOM_WINDOW, 32, (unsigned char **)&_keygrab_win, &num);
321         if (ret && num)
322         {
323                 Ecore_X_Window xWindow = *_keygrab_win;
324
325                 r = ecore_x_client_message8_send(xWindow, type, msg_data, sizeof(msg_data));
326                 SysTryReturn(NID_UI, r == true, E_OPERATION_FAILED, E_OPERATION_FAILED, "ecore_x_client_message8_send failed.");
327         }
328
329         return E_SUCCESS;
330 }
331
332 }} //Tizen::Ui