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