modify license, permission and remove ^M char
[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
153         //{ _KEY_SIDE_UP, "XF86AudioRaiseVolume" },
154         //{ _KEY_SIDE_DOWN, "XF86AudioLowerVolume" },
155         //{ _KEY_POWER_HOLD, "XF86PowerOff" },
156         { _KEY_STOP, "XF86Stop" }
157 };
158
159 struct _DisplayDeleter
160 {
161         void operator()(Display* pDisplay)
162         {
163                 XCloseDisplay(pDisplay);
164         }
165 };
166
167 _SystemUtilImpl::_SystemUtilImpl(void)
168 {
169 }
170
171 _SystemUtilImpl::~_SystemUtilImpl(void)
172 {
173 }
174
175 result
176 _SystemUtilImpl::GenerateKeyEvent(Tizen::Ui::KeyEventType keyEvent, Tizen::Ui::_KeyCode keyCode)
177 {
178         result r = E_SUCCESS;
179
180         Display* pDisplay = (Display*)ecore_x_display_get();
181         SysTryReturnResult(NID_UI, pDisplay != null, E_OPERATION_FAILED, "[E_OPERATION_FAILED] pDisplay is invalid. something wrong!");
182
183         std::unique_ptr<Display, _DisplayDeleter> pDpy(XOpenDisplay(NULL));
184         SysTryReturnResult(NID_UI, pDpy.get() != null, E_OPERATION_FAILED, "[E_OPERATION_FAILED] pDpy is invalid. something wrong!");
185
186         int key = -1;
187
188         for (unsigned int idx = 0; idx < sizeof(KeyCodeMap) / sizeof(ConvertKeys); ++idx)
189         {
190                 if (KeyCodeMap[idx].code == keyCode)
191                 {
192                         KeySym keySym = XStringToKeysym(KeyCodeMap[idx].pName);
193                         key = (int) XKeysymToKeycode(pDpy.get(), keySym);
194                         break;
195                 }
196         }
197
198         if (key == -1)
199         {
200                 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Unsupported KeyCode argument.");
201                 return E_INVALID_ARG;
202         }
203
204         switch (keyEvent)
205         {
206         case KEY_EVENT_TYPE_PRESSED:
207                 XTestFakeKeyEvent(pDisplay, key, true, CurrentTime);
208                 break;
209
210         case KEY_EVENT_TYPE_RELEASED:
211                 XTestFakeKeyEvent(pDisplay, key, false, CurrentTime);
212                 break;
213
214         default:
215                 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Invalid KeyEventType argument.");
216                 return E_INVALID_ARG;
217         }
218
219         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
220         return r;
221 }
222
223 result
224 _SystemUtilImpl::GenerateTouchEvent(TouchEventType touchEvent, const Tizen::Graphics::Point& point)
225 {
226         return GenerateTouchEvent(touchEvent, _CoordinateSystemUtils::ConvertToFloat(point));
227 }
228
229 result
230 _SystemUtilImpl::GenerateTouchEvent(TouchEventType touchEvent, const Tizen::Graphics::FloatPoint& point)
231 {
232         result r = E_SUCCESS;
233
234         Display* pDisplay = (Display*)ecore_x_display_get();
235         SysTryReturnResult(NID_UI, pDisplay != null, E_OPERATION_FAILED, "[E_OPERATION_FAILED] pDisplay is invalid. something wrong!");
236
237         std::unique_ptr<Display, _DisplayDeleter> pDpy(XOpenDisplay(NULL));
238         SysTryReturnResult(NID_UI, pDpy.get() != null, E_OPERATION_FAILED, "[E_OPERATION_FAILED] pDpy is invalid. something wrong!");
239
240         int width = DisplayWidth(pDpy.get(), DefaultScreen(pDpy.get()));
241         int height = DisplayHeight(pDpy.get(), DefaultScreen(pDpy.get()));
242
243         if (( point.x<0 || point.x>width) || (point.y<0 || point.y>height))
244         {
245                 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Out of bounds x, y argument.");
246                 return E_INVALID_ARG;
247         }
248
249         int screenNumber = 0; //main screen
250         int button = 1; //left button
251
252         switch (touchEvent)
253         {
254         case TOUCH_EVENT_TYPE_PRESSED:
255                 XWarpPointer(pDisplay, None, XRootWindow(pDisplay, 0), 0, 0, 0, 0, point.x, point.y);
256                 XTestFakeButtonEvent(pDisplay, button, true, CurrentTime);
257                 break;
258
259         case TOUCH_EVENT_TYPE_RELEASED:
260                 XWarpPointer(pDisplay, None, XRootWindow(pDisplay, 0), 0, 0, 0, 0, point.x, point.y);
261                 XTestFakeButtonEvent(pDisplay, button, false, CurrentTime);
262                 break;
263
264         case TOUCH_EVENT_TYPE_MOVED:
265                 XTestFakeMotionEvent(pDisplay, screenNumber, point.x, point.y, CurrentTime);
266                 break;
267
268         default:
269                 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Invalid TouchEventType argument.");
270                 return E_INVALID_ARG;
271         }
272
273         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
274         return r;
275 }
276
277 Tizen::Graphics::Bitmap*
278 _SystemUtilImpl::CaptureScreenN(void)
279 {
280         result r = E_SUCCESS;
281
282         std::unique_ptr<Display, _DisplayDeleter> pDpy(XOpenDisplay(NULL));
283         SysTryReturn(NID_UI, pDpy.get(), null, E_OPERATION_FAILED, "[E_OPERATION_FAILED] pDpy is invalid. something wrong!");
284
285         int width = DisplayWidth(pDpy.get(), DefaultScreen(pDpy.get()));
286         int height = DisplayHeight(pDpy.get(), DefaultScreen(pDpy.get()));
287
288         void* pDump = utilx_create_screen_shot(pDpy.get(), width, height);
289         SysTryReturn(NID_UI, pDump, null, E_OPERATION_FAILED, "[E_OPERATION_FAILED] pDump is invalid. something wrong!");
290
291         std::unique_ptr<ByteBuffer> pBuffer(new (std::nothrow) ByteBuffer());
292         SysTryReturn(NID_UI, pBuffer != null, null, E_OPERATION_FAILED, "[E_OPERATION_FAILED] Memory allocation failure.");
293
294         r = pBuffer->Construct(static_cast<byte*>(pDump), 0, width*height*4, width*height*4);
295         SysTryReturn(NID_UI, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
296
297         Tizen::Graphics::Dimension dim(width, height);
298         std::unique_ptr<Tizen::Graphics::Bitmap> pBitmap(new (std::nothrow) Bitmap());
299         SysTryReturn(NID_UI, pBitmap != null, null, E_OPERATION_FAILED, "[E_OPERATION_FAILED] Memory allocation failure.");
300
301         r = pBitmap->Construct(*pBuffer, dim, BITMAP_PIXEL_FORMAT_ARGB8888);
302         SysTryReturn(NID_UI, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
303
304         utilx_release_screen_shot();
305
306         return pBitmap.release();
307 }
308
309 result
310 _SystemUtilImpl::SendKeyStopMessage(void)
311 {
312         bool r = false;
313         int num;
314         Ecore_X_Window *_keygrab_win = NULL;
315         char msg_data[] = "PxXF86Stop";
316
317         Ecore_X_Atom type = ecore_x_atom_get("_HWKEY_EMULATION");
318
319         int ret = ecore_x_window_prop_property_get(NULL, type, ECORE_X_ATOM_WINDOW, 32, (unsigned char **)&_keygrab_win, &num);
320         if (ret && num)
321         {
322                 Ecore_X_Window xWindow = *_keygrab_win;
323
324                 r = ecore_x_client_message8_send(xWindow, type, msg_data, sizeof(msg_data));
325                 SysTryReturn(NID_UI, r == true, E_OPERATION_FAILED, E_OPERATION_FAILED, "ecore_x_client_message8_send failed.");
326         }
327
328         return E_SUCCESS;
329 }
330
331 }} //Tizen::Ui