2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0/
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.
17 #include <X11/extensions/XTest.h>
19 #include <X11/keysym.h>
22 //to avoid camera key code definition
26 #include <unique_ptr.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"
37 using namespace Tizen::Security;
38 using namespace Tizen::Graphics;
39 using namespace Tizen::Base;
41 namespace Tizen { namespace Ui
50 const ConvertKeys KeyCodeMap[] = {
51 { _KEY_ESC, "Escape" },
52 { _KEY_GRAVE, "grave" },
63 { _KEY_MINUS, "minus" },
64 { _KEY_EQUAL, "equal" },
65 { _KEY_BACKSLASH, "backslash" },
66 { _KEY_BACKSPACE, "BackSpace" },
78 { _KEY_LBRACKET, "bracketleft" },
79 { _KEY_RBRACKET, "bracketright" },
80 { _KEY_ENTER, "Return" },
81 { _KEY_CAPSLOCK, "Caps_Lock" },
91 { _KEY_SEMICOLON, "semicolon" },
92 { _KEY_APOSTROPHE, "apostrophe" },
93 { _KEY_SHIFT_L, "Shift_L" },
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" },
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" },
149 { _KEY_LEFT, "Left" },
150 { _KEY_DOWN, "Down" },
151 { _KEY_RIGHT, "Right" },
152 { _KEY_CONTEXT_MENU, "Menu" },
154 //{ _KEY_SIDE_UP, "XF86AudioRaiseVolume" },
155 //{ _KEY_SIDE_DOWN, "XF86AudioLowerVolume" },
156 //{ _KEY_POWER_HOLD, "XF86PowerOff" },
157 { _KEY_CLEAR, "XF86Stop" }
160 struct _DisplayDeleter
162 void operator()(Display* pDisplay)
164 XCloseDisplay(pDisplay);
168 _SystemUtilImpl::_SystemUtilImpl(void)
172 _SystemUtilImpl::~_SystemUtilImpl(void)
177 _SystemUtilImpl::GenerateKeyEvent(Tizen::Ui::KeyEventType keyEvent, Tizen::Ui::_KeyCode keyCode)
179 result r = E_SUCCESS;
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!");
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!");
189 for (unsigned int idx = 0; idx < sizeof(KeyCodeMap) / sizeof(ConvertKeys); ++idx)
191 if (KeyCodeMap[idx].code == keyCode)
193 KeySym keySym = XStringToKeysym(KeyCodeMap[idx].pName);
194 key = (int) XKeysymToKeycode(pDpy.get(), keySym);
201 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Unsupported KeyCode argument.");
202 return E_INVALID_ARG;
207 case KEY_EVENT_TYPE_PRESSED:
208 XTestFakeKeyEvent(pDisplay, key, true, CurrentTime);
211 case KEY_EVENT_TYPE_RELEASED:
212 XTestFakeKeyEvent(pDisplay, key, false, CurrentTime);
216 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Invalid KeyEventType argument.");
217 return E_INVALID_ARG;
220 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
225 _SystemUtilImpl::GenerateTouchEvent(TouchEventType touchEvent, const Tizen::Graphics::Point& point)
227 return GenerateTouchEvent(touchEvent, _CoordinateSystemUtils::ConvertToFloat(point));
231 _SystemUtilImpl::GenerateTouchEvent(TouchEventType touchEvent, const Tizen::Graphics::FloatPoint& point)
233 result r = E_SUCCESS;
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!");
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!");
241 int width = DisplayWidth(pDpy.get(), DefaultScreen(pDpy.get()));
242 int height = DisplayHeight(pDpy.get(), DefaultScreen(pDpy.get()));
244 if (( point.x<0 || point.x>width) || (point.y<0 || point.y>height))
246 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Out of bounds x, y argument.");
247 return E_INVALID_ARG;
250 int screenNumber = 0; //main screen
251 int button = 1; //left button
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);
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);
265 case TOUCH_EVENT_TYPE_MOVED:
266 XTestFakeMotionEvent(pDisplay, screenNumber, point.x, point.y, CurrentTime);
270 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Invalid TouchEventType argument.");
271 return E_INVALID_ARG;
274 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
278 Tizen::Graphics::Bitmap*
279 _SystemUtilImpl::CaptureScreenN(void)
281 result r = E_SUCCESS;
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!");
286 int width = DisplayWidth(pDpy.get(), DefaultScreen(pDpy.get()));
287 int height = DisplayHeight(pDpy.get(), DefaultScreen(pDpy.get()));
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!");
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.");
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));
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.");
302 r = pBitmap->Construct(*pBuffer, dim, BITMAP_PIXEL_FORMAT_ARGB8888);
303 SysTryReturn(NID_UI, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
305 utilx_release_screen_shot();
307 return pBitmap.release();
311 _SystemUtilImpl::SendKeyStopMessage(void)
315 Ecore_X_Window *_keygrab_win = null;
316 char msg_data[] = "PxXF86Stop";
318 Ecore_X_Atom type = ecore_x_atom_get("_HWKEY_EMULATION");
320 int ret = ecore_x_window_prop_property_get(null, type, ECORE_X_ATOM_WINDOW, 32, (unsigned char **)&_keygrab_win, &num);
323 Ecore_X_Window xWindow = *_keygrab_win;
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.");